Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, The Linux Foundation. |
| 3 | * Copyright (c) 2018-2024, Laurence Lundblade. |
| 4 | * Copyright (c) 2021, Arm Limited. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 10 | * Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above |
| 13 | copyright notice, this list of conditions and the following |
| 14 | disclaimer in the documentation and/or other materials provided |
| 15 | with the distribution. |
| 16 | * Neither the name of The Linux Foundation nor the names of its |
| 17 | contributors, nor the name "Laurence Lundblade" may be used to |
| 18 | endorse or promote products derived from this software without |
| 19 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 20 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 21 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 25 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 30 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 32 | =============================================================================*/ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 33 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 34 | #include "qcbor_decode_tests.h" |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 35 | #include "qcbor/qcbor_encode.h" |
| 36 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 37 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | d4728fd | 2018-12-17 15:15:56 -0800 | [diff] [blame] | 38 | #include <string.h> |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 39 | #include <math.h> // for fabs() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 40 | #include "not_well_formed_cbor.h" |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 41 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 42 | // Handy macro to compare a UsefulBuf to a C string |
| 43 | #define UsefulBufCompareToSZ(x, y) \ |
| 44 | UsefulBuf_Compare(x, UsefulBuf_FromSZ(y)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 45 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 46 | #ifdef PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 47 | #include <stdio.h> |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 48 | |
| 49 | static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 50 | { |
| 51 | if(szLabel) { |
| 52 | printf("%s ", szLabel); |
| 53 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 54 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 55 | size_t i; |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 56 | for(i = 0; i < Buf.len; i++) { |
| 57 | uint8_t Z = ((uint8_t *)Buf.ptr)[i]; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 58 | printf("%02x ", Z); |
| 59 | } |
| 60 | printf("\n"); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 61 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 62 | fflush(stdout); |
| 63 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 64 | #endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 65 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 66 | |
| 67 | /* Make a test results code that includes three components. Return code |
| 68 | * is xxxyyyzzz where zz is the error code, yy is the test number and |
| 69 | * zz is check being performed |
| 70 | */ |
| 71 | static int32_t |
| 72 | MakeTestResultCode(uint32_t uTestCase, |
| 73 | uint32_t uTestNumber, |
| 74 | QCBORError uErrorCode) |
| 75 | { |
| 76 | uint32_t uCode = (uTestCase * 1000000) + |
| 77 | (uTestNumber * 1000) + |
| 78 | (uint32_t)uErrorCode; |
| 79 | return (int32_t)uCode; |
| 80 | } |
| 81 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 82 | /* |
| 83 | [ |
| 84 | -9223372036854775808, |
| 85 | -4294967297, |
| 86 | -4294967296, |
| 87 | -4294967295, |
| 88 | -4294967294, |
| 89 | -2147483648, |
| 90 | -2147483647, |
| 91 | -65538, |
| 92 | -65537, |
| 93 | -65536, |
| 94 | -65535, |
| 95 | -65534, |
| 96 | -257, |
| 97 | -256, |
| 98 | -255, |
| 99 | -254, |
| 100 | -25, |
| 101 | -24, |
| 102 | -23, |
| 103 | -1, |
| 104 | 0, |
| 105 | 0, |
| 106 | 1, |
| 107 | 22, |
| 108 | 23, |
| 109 | 24, |
| 110 | 25, |
| 111 | 26, |
| 112 | 254, |
| 113 | 255, |
| 114 | 256, |
| 115 | 257, |
| 116 | 65534, |
| 117 | 65535, |
| 118 | 65536, |
| 119 | 65537, |
| 120 | 65538, |
| 121 | 2147483647, |
| 122 | 2147483647, |
| 123 | 2147483648, |
| 124 | 2147483649, |
| 125 | 4294967294, |
| 126 | 4294967295, |
| 127 | 4294967296, |
| 128 | 4294967297, |
| 129 | 9223372036854775807, |
| 130 | 18446744073709551615 |
| 131 | ] |
| 132 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 133 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 134 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 135 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 136 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 137 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 138 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 139 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 140 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 141 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 142 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 143 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 144 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 145 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 146 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 147 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 148 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 149 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 150 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 151 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 152 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 153 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 154 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 155 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 156 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 157 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 158 | 0xff, 0xff}; |
| 159 | |
| 160 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 161 | // return CBOR error or -1 if type of value doesn't match |
| 162 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 163 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 164 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 165 | QCBORItem Item; |
| 166 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 167 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 168 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 169 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 170 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 171 | return -1; |
| 172 | |
| 173 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 174 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 175 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 176 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 177 | return -1; |
| 178 | |
| 179 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 180 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 181 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 182 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 183 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 184 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 185 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 186 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 187 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 188 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 189 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 190 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 191 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 192 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 193 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 194 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 195 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 196 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 197 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 198 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 199 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 200 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 202 | |
| 203 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 204 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 205 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 206 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 207 | Item.val.int64 != -2147483648) |
| 208 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 209 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 210 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 211 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 212 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 213 | Item.val.int64 != -2147483647) |
| 214 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 215 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 216 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 217 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 218 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 219 | Item.val.int64 != -65538) |
| 220 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 221 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 222 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 223 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 224 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 225 | Item.val.int64 != -65537) |
| 226 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 227 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 228 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 229 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 230 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 231 | Item.val.int64 != -65536) |
| 232 | return -1; |
| 233 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 234 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 235 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 236 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 237 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 238 | Item.val.int64 != -65535) |
| 239 | return -1; |
| 240 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 241 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 242 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 243 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 244 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 245 | Item.val.int64 != -65534) |
| 246 | return -1; |
| 247 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 248 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 249 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 250 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 251 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 252 | Item.val.int64 != -257) |
| 253 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 254 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 255 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 256 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 257 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 258 | Item.val.int64 != -256) |
| 259 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 260 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 261 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 262 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 263 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 264 | Item.val.int64 != -255) |
| 265 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 266 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 267 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 268 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 269 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 270 | Item.val.int64 != -254) |
| 271 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 272 | |
| 273 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 274 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 275 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 276 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 277 | Item.val.int64 != -25) |
| 278 | return -1; |
| 279 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 280 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 281 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 282 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 283 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 284 | Item.val.int64 != -24) |
| 285 | return -1; |
| 286 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 287 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 288 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 289 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 290 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 291 | Item.val.int64 != -23) |
| 292 | return -1; |
| 293 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 294 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 295 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 296 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 297 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 298 | Item.val.int64 != -1) |
| 299 | return -1; |
| 300 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 301 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 302 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 303 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 304 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 305 | Item.val.int64 != 0) |
| 306 | return -1; |
| 307 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 308 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 309 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 310 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 311 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 312 | Item.val.int64 != 0) |
| 313 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 314 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 315 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 316 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 317 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 318 | Item.val.int64 != 1) |
| 319 | return -1; |
| 320 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 321 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 322 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 323 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 324 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 325 | Item.val.int64 != 22) |
| 326 | return -1; |
| 327 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 328 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 329 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 330 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 331 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 332 | Item.val.int64 != 23) |
| 333 | return -1; |
| 334 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 335 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 336 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 337 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 338 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 339 | Item.val.int64 != 24) |
| 340 | return -1; |
| 341 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 342 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 343 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 344 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 345 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 346 | Item.val.int64 != 25) |
| 347 | return -1; |
| 348 | |
| 349 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 350 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 351 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 352 | Item.val.int64 != 26) |
| 353 | return -1; |
| 354 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 355 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 356 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 357 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 358 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 359 | Item.val.int64 != 254) |
| 360 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 361 | |
| 362 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 363 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 364 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 365 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 366 | Item.val.int64 != 255) |
| 367 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 368 | |
| 369 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 370 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 371 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 372 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 373 | Item.val.int64 != 256) |
| 374 | return -1; |
| 375 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 376 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 377 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 378 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 379 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 380 | Item.val.int64 != 257) |
| 381 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 382 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 383 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 384 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 385 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 386 | Item.val.int64 != 65534) |
| 387 | return -1; |
| 388 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 389 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 390 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 391 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 392 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 393 | Item.val.int64 != 65535) |
| 394 | return -1; |
| 395 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 396 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 397 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 398 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 399 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 400 | Item.val.int64 != 65536) |
| 401 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 402 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 403 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 404 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 405 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 406 | Item.val.int64 != 65537) |
| 407 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 408 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 409 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 410 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 411 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 412 | Item.val.int64 != 65538) |
| 413 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 414 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 415 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 416 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 417 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 418 | Item.val.int64 != 2147483647) |
| 419 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 420 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 421 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 422 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 423 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 424 | Item.val.int64 != 2147483647) |
| 425 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 426 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 427 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 428 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 429 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 430 | Item.val.int64 != 2147483648) |
| 431 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 432 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 433 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 434 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 435 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 436 | Item.val.int64 != 2147483649) |
| 437 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 438 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 439 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 440 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 441 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 442 | Item.val.int64 != 4294967294) |
| 443 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 444 | |
| 445 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 446 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 447 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 448 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 449 | Item.val.int64 != 4294967295) |
| 450 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 451 | |
| 452 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 453 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 454 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 455 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 456 | Item.val.int64 != 4294967296) |
| 457 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 458 | |
| 459 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 460 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 461 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 462 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 463 | Item.val.int64 != 4294967297) |
| 464 | return -1; |
| 465 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 466 | |
| 467 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 468 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 469 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 470 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 471 | Item.val.int64 != 9223372036854775807LL) |
| 472 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 473 | |
| 474 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 475 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 476 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 477 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 478 | Item.val.uint64 != 18446744073709551615ULL) |
| 479 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 480 | |
| 481 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 482 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 483 | return -1; |
| 484 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 485 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 490 | /* One less than the smallest negative integer allowed in C. Decoding |
| 491 | this should fail. |
| 492 | -9223372036854775809 |
| 493 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 494 | static const uint8_t spTooSmallNegative[] = { |
| 495 | 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 499 | /* |
| 500 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 501 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 502 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 503 | int32_t IntegerValuesParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 504 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 505 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 506 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 507 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 508 | QCBORDecode_Init(&DCtx, |
| 509 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 510 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 511 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 512 | // The really big test of all successes |
| 513 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 514 | if(nReturn) { |
| 515 | return nReturn; |
| 516 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 517 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 518 | // The one large negative integer that can be parsed |
| 519 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 520 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative), |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 521 | QCBOR_DECODE_MODE_NORMAL); |
| 522 | |
| 523 | QCBORItem item; |
| 524 | if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) { |
| 525 | nReturn = -4000; |
| 526 | } |
| 527 | |
| 528 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 533 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 534 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 535 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 536 | Two of the inputs can be set. Two other items in the array are fixed. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 537 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 538 | */ |
| 539 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 540 | static uint8_t spSimpleArrayBuffer[50]; |
| 541 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 542 | static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 543 | { |
| 544 | QCBOREncodeContext ECtx; |
| 545 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 546 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 547 | *pEncoded = NULL; |
| 548 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 549 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 550 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 551 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 552 | // and last with the buffer to do the actual encoding |
| 553 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 554 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 555 | QCBOREncode_OpenArray(&ECtx); |
| 556 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 557 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 558 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 559 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 560 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 561 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 562 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 563 | goto Done; |
| 564 | |
| 565 | if(*pEncoded != NULL) { |
| 566 | nReturn = 0; |
| 567 | goto Done; |
| 568 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 569 | |
| 570 | // Use static buffer to avoid dependency on malloc() |
| 571 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 572 | goto Done; |
| 573 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 574 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 575 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 576 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 577 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 578 | Done: |
| 579 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 583 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 584 | Some basic CBOR with map and array used in a lot of tests. |
| 585 | The map labels are all strings |
| 586 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 587 | { |
| 588 | "first integer": 42, |
| 589 | "an array of two strings": [ |
| 590 | "string1", "string2" |
| 591 | ], |
| 592 | "map in a map": { |
| 593 | "bytes 1": h'78787878', |
| 594 | "bytes 2": h'79797979', |
| 595 | "another int": 98, |
| 596 | "text 2": "lies, damn lies and statistics" |
| 597 | } |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 598 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 599 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 600 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 601 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 602 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 603 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 604 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 605 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 606 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 607 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 608 | 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, |
| 609 | 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4, |
| 610 | 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 611 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, |
| 612 | 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, |
| 613 | 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68, |
| 614 | 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, |
| 615 | 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 616 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, |
| 617 | 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, |
| 618 | 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, |
| 619 | 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 620 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 621 | |
| 622 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 623 | // Same as above, but with indefinite lengths. |
| 624 | static const uint8_t pValidMapIndefEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 625 | 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 626 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 627 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 628 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 629 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 630 | 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 631 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 632 | 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20, |
| 633 | 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, |
| 634 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 635 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 636 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 637 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 638 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 639 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 640 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 641 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 642 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 643 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 644 | 0xff, 0xff}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 645 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 646 | |
| 647 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 648 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 649 | size_t nLen, |
| 650 | int64_t *pInt1, |
| 651 | int64_t *pInt2, |
| 652 | const uint8_t **pBuf3, |
| 653 | size_t *pBuf3Len, |
| 654 | const uint8_t **pBuf4, |
| 655 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 656 | { |
| 657 | QCBORDecodeContext DCtx; |
| 658 | QCBORItem Item; |
| 659 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 660 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 661 | QCBORDecode_Init(&DCtx, |
| 662 | (UsefulBufC){pEncoded, nLen}, |
| 663 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 664 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 665 | // Make sure the first thing is a map |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 666 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 667 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 668 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 669 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 670 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 671 | // First integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 672 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 673 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 674 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 675 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 676 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 677 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 678 | // Second integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 679 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 680 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 681 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 682 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 683 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 684 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 685 | // First string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 686 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 687 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 688 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 689 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 690 | *pBuf3 = Item.val.string.ptr; |
| 691 | *pBuf3Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 692 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 693 | // Second string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 694 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 695 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 696 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 697 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 698 | *pBuf4 = Item.val.string.ptr; |
| 699 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 700 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 701 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 702 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 703 | Done: |
| 704 | return(nReturn); |
| 705 | } |
| 706 | |
| 707 | |
| 708 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 709 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 710 | int32_t SimpleArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 711 | { |
| 712 | uint8_t *pEncoded; |
| 713 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 714 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 715 | int64_t i1=0, i2=0; |
| 716 | size_t i3=0, i4=0; |
| 717 | const uint8_t *s3= (uint8_t *)""; |
| 718 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 719 | |
| 720 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 721 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 722 | return(-1); |
| 723 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 724 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 725 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 726 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 727 | if(i1 != 23 || |
| 728 | i2 != 6000 || |
| 729 | i3 != 8 || |
| 730 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 731 | memcmp("galactic", s3, 8) !=0 || |
| 732 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 733 | return(-1); |
| 734 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 735 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 736 | return(0); |
| 737 | } |
| 738 | |
| 739 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 740 | /* |
| 741 | [ |
| 742 | 0, |
| 743 | [], |
| 744 | [ |
| 745 | [], |
| 746 | [ |
| 747 | 0 |
| 748 | ], |
| 749 | {}, |
| 750 | { |
| 751 | 1: {}, |
| 752 | 2: {}, |
| 753 | 3: [] |
| 754 | } |
| 755 | ] |
| 756 | ] |
| 757 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 758 | static uint8_t sEmpties[] = { |
| 759 | 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 760 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 761 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 762 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 763 | /* Same as above, but with indefinte lengths */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 764 | static const uint8_t sEmptiesIndef[] = { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 765 | 0x9F, |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 766 | 0x00, |
| 767 | 0x9F, |
| 768 | 0xFF, |
| 769 | 0x9F, |
| 770 | 0x9F, |
| 771 | 0xFF, |
| 772 | 0x9F, |
| 773 | 0x00, |
| 774 | 0xFF, |
| 775 | 0xBF, |
| 776 | 0xFF, |
| 777 | 0xBF, |
| 778 | 0x01, |
| 779 | 0xBF, |
| 780 | 0xFF, |
| 781 | 0x02, |
| 782 | 0xBF, |
| 783 | 0xFF, |
| 784 | 0x03, |
| 785 | 0x9F, |
| 786 | 0xFF, |
| 787 | 0xFF, |
| 788 | 0xFF, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 789 | 0xFF}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 790 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 791 | |
| 792 | |
| 793 | static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts) |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 794 | { |
| 795 | QCBORDecodeContext DCtx; |
| 796 | QCBORItem Item; |
| 797 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 798 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 799 | input, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 800 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 801 | |
| 802 | // Array with 3 items |
| 803 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 804 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 805 | Item.uNestingLevel != 0 || |
| 806 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 807 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 808 | return -1; |
| 809 | } |
| 810 | |
| 811 | // An integer 0 |
| 812 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 813 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 814 | Item.uNestingLevel != 1 || |
| 815 | Item.uNextNestLevel != 1 || |
| 816 | Item.val.uint64 != 0) { |
| 817 | return -2; |
| 818 | } |
| 819 | |
| 820 | // An empty array |
| 821 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 822 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 823 | Item.uNestingLevel != 1 || |
| 824 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 825 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 826 | return -3; |
| 827 | } |
| 828 | |
| 829 | // An array with 4 items |
| 830 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 831 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 832 | Item.uNestingLevel != 1 || |
| 833 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 834 | (bCheckCounts && Item.val.uCount != 4)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 835 | return -4; |
| 836 | } |
| 837 | |
| 838 | // An empty array |
| 839 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 840 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 841 | Item.uNestingLevel != 2 || |
| 842 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 843 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 844 | return -5; |
| 845 | } |
| 846 | |
| 847 | // An array with 1 item |
| 848 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 849 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 850 | Item.uNestingLevel != 2 || |
| 851 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 852 | (bCheckCounts && Item.val.uCount != 1)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 853 | return -6; |
| 854 | } |
| 855 | |
| 856 | // An integer 0 |
| 857 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 858 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 859 | Item.uNestingLevel != 3 || |
| 860 | Item.uNextNestLevel != 2 || |
| 861 | Item.val.uint64 != 0) { |
| 862 | return -7; |
| 863 | } |
| 864 | |
| 865 | // An empty map |
| 866 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 867 | Item.uDataType != QCBOR_TYPE_MAP || |
| 868 | Item.uNestingLevel != 2 || |
| 869 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 870 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 871 | return -8; |
| 872 | } |
| 873 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 874 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 875 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 876 | Item.uDataType != QCBOR_TYPE_MAP || |
| 877 | Item.uNestingLevel != 2 || |
| 878 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 879 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 880 | return -9; |
| 881 | } |
| 882 | |
| 883 | // An empty map |
| 884 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 885 | Item.uDataType != QCBOR_TYPE_MAP || |
| 886 | Item.uNestingLevel != 3 || |
| 887 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 888 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 889 | return -10; |
| 890 | } |
| 891 | |
| 892 | // An empty map |
| 893 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 894 | Item.uDataType != QCBOR_TYPE_MAP || |
| 895 | Item.uNestingLevel != 3 || |
| 896 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 897 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 898 | return -11; |
| 899 | } |
| 900 | |
| 901 | // An empty array |
| 902 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 903 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 904 | Item.uNestingLevel != 3 || |
| 905 | Item.uNextNestLevel != 0 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 906 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 907 | return -12; |
| 908 | } |
| 909 | |
| 910 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 911 | return -13; |
| 912 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 917 | int32_t EmptyMapsAndArraysTest(void) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 918 | { |
| 919 | int nResult; |
| 920 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 921 | true); |
| 922 | if(nResult) { |
| 923 | return nResult; |
| 924 | } |
| 925 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 926 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 927 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 928 | false); |
| 929 | |
| 930 | if(nResult) { |
| 931 | return nResult -100; |
| 932 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 933 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 938 | |
| 939 | static const uint8_t sEmptyMap[] = { |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 940 | 0xA1, //# map(1) |
| 941 | 0x02, //# unsigned(2) |
| 942 | 0xA0, //# map(0) |
| 943 | }; |
| 944 | |
| 945 | int32_t ParseEmptyMapInMapTest(void) |
| 946 | { |
| 947 | QCBORDecodeContext DCtx; |
| 948 | QCBORItem Item; |
| 949 | int nReturn = 0; |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 950 | QCBORError uErr; |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 951 | |
| 952 | QCBORDecode_Init(&DCtx, |
| 953 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap), |
| 954 | QCBOR_DECODE_MODE_NORMAL); |
| 955 | |
| 956 | /* now open the first Map */ |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 957 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 958 | if(uErr != QCBOR_SUCCESS || |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 959 | Item.uDataType != QCBOR_TYPE_MAP) { |
| 960 | nReturn = -3; |
| 961 | goto done; |
| 962 | } |
| 963 | |
| 964 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0) { |
| 965 | nReturn = -1; |
| 966 | goto done; |
| 967 | } |
| 968 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 969 | Item.uNestingLevel != 1 || |
| 970 | Item.label.int64 != 2) { |
| 971 | nReturn = -2; |
| 972 | goto done; |
| 973 | } |
| 974 | |
| 975 | done: |
| 976 | return(nReturn); |
| 977 | } |
| 978 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 979 | |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 980 | /* [[[[[[[[[[]]]]]]]]]] */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 981 | static const uint8_t spDeepArrays[] = { |
| 982 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 983 | 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 984 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 985 | int32_t ParseDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 986 | { |
| 987 | QCBORDecodeContext DCtx; |
| 988 | int nReturn = 0; |
| 989 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 990 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 991 | QCBORDecode_Init(&DCtx, |
| 992 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 993 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 994 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 995 | for(i = 0; i < 10; i++) { |
| 996 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 997 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 998 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 999 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1000 | Item.uNestingLevel != i) { |
| 1001 | nReturn = -1; |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1005 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1006 | return(nReturn); |
| 1007 | } |
| 1008 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1009 | /* Big enough to test nesting to the depth of 24 |
| 1010 | [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]] |
| 1011 | */ |
| 1012 | static const uint8_t spTooDeepArrays[] = { |
| 1013 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1014 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1015 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1016 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1017 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1018 | int32_t ParseTooDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1019 | { |
| 1020 | QCBORDecodeContext DCtx; |
| 1021 | int nReturn = 0; |
| 1022 | int i; |
| 1023 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1024 | |
| 1025 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1026 | QCBORDecode_Init(&DCtx, |
| 1027 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 1028 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1029 | |
Laurence Lundblade | 3e0b230 | 2023-12-04 14:02:38 -0700 | [diff] [blame] | 1030 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1031 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1032 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 1033 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1034 | Item.uNestingLevel != i) { |
| 1035 | nReturn = -1; |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1039 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1040 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1041 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1042 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1043 | return(nReturn); |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1049 | int32_t ShortBufferParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1050 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1051 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1052 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1053 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1054 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1056 | QCBORDecode_Init(&DCtx, |
| 1057 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 1058 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1059 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1060 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1061 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1062 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1063 | nResult = -1; |
| 1064 | goto Done; |
| 1065 | } |
| 1066 | } |
| 1067 | Done: |
| 1068 | return nResult; |
| 1069 | } |
| 1070 | |
| 1071 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1072 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1073 | int32_t ShortBufferParseTest2(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1074 | { |
| 1075 | uint8_t *pEncoded; |
| 1076 | int nReturn; |
| 1077 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1078 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1079 | int64_t i1, i2; |
| 1080 | size_t i3, i4; |
| 1081 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1082 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1083 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1084 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1085 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 1086 | return(-1); |
| 1087 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1088 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1089 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1090 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 1091 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1092 | if(nResult == 0) { |
| 1093 | nReturn = -1; |
| 1094 | } |
| 1095 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1096 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1097 | return(nReturn); |
| 1098 | } |
| 1099 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1100 | |
| 1101 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1102 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1103 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1104 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1105 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1106 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1107 | |
| 1108 | static const uint8_t pPerverseLabels[] = { |
| 1109 | 0xae, |
| 1110 | |
| 1111 | 0xf5, 0x61, 0x61, |
| 1112 | |
| 1113 | 0xf6, 0x61, 0x62, |
| 1114 | |
| 1115 | 0xf8, 0xff, 0x61, 0x63, |
| 1116 | |
| 1117 | 0xf9, 0x7e, 0x00, 0x61, 0x64, |
| 1118 | |
| 1119 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0x61, 0x65, |
| 1120 | |
| 1121 | 0xfb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x66, |
| 1122 | |
| 1123 | 0xa1, 0x19, 0x03, 0xe8, 0x10, 0x61, 0x67, |
| 1124 | |
| 1125 | 0x81, 0x81, 0x81, 0x80, 0x61, 0x68, |
| 1126 | |
| 1127 | 0xc1, 0x09, 0x61, 0x69, |
| 1128 | |
| 1129 | 0x82, 0x05, 0xa2, 0x01, 0x02, 0x03, 0x04, 0x61, 0x6a, |
| 1130 | |
| 1131 | 0xbf, 0xff, 0x61, 0x6b, |
| 1132 | |
| 1133 | 0x9f, 0x11, 0x12, 0x13, 0xff, 0x61, 0x6c, |
| 1134 | |
| 1135 | 0x7f, 0x62, 0x41, 0x42, 0x62, 0x43, 0x44, 0xff, 0x61, 0x6d, |
| 1136 | |
| 1137 | 0xd9, 0x01, 0x02, 0xbf, 0x7f, 0x61, 0x4a, 0x61, 0x4b, 0xff, 0x00, 0xf4, 0xd7, 0x80 ,0xff, 0x61, 0x6e |
| 1138 | }; |
| 1139 | #endif |
| 1140 | |
| 1141 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1142 | /* |
| 1143 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1144 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 1145 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1146 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1147 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1148 | { |
| 1149 | QCBORDecodeContext DCtx; |
| 1150 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1151 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1152 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1153 | QCBORDecode_Init(&DCtx, |
| 1154 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1155 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1156 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1157 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1158 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1159 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1160 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1161 | Item.val.uCount != 3) |
| 1162 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1163 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1164 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1165 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1166 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1167 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1168 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1169 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1170 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1171 | Item.uDataAlloc || |
| 1172 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1173 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1174 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1175 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1176 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1177 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1178 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1179 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1180 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1181 | Item.uDataAlloc || |
| 1182 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1183 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1184 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1185 | Item.val.uCount != 2) |
| 1186 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1187 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1188 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1189 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1190 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1191 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1192 | Item.uDataAlloc || |
| 1193 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1194 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1195 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1196 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1197 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1198 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1199 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1200 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1201 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1202 | Item.uDataAlloc || |
| 1203 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1204 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1205 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1206 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1207 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1208 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1209 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1210 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1211 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1212 | Item.uDataAlloc || |
| 1213 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1214 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1215 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1216 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1217 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1218 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1219 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1220 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1221 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1222 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1223 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1224 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1225 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1226 | Item.uDataAlloc || |
| 1227 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1228 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1229 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1230 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1231 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1232 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1233 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1234 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1235 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1236 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1237 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1238 | Item.uDataAlloc || |
| 1239 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1240 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1241 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1243 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1244 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1245 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1246 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1247 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1248 | Item.uDataAlloc || |
| 1249 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1250 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1251 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1252 | Item.val.int64 != 98) |
| 1253 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1254 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1255 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1256 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1257 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1258 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1259 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1260 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1261 | Item.uDataAlloc || |
| 1262 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1263 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1264 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1265 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1266 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1271 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1272 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1273 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1274 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1275 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1276 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1277 | |
| 1278 | /* Utility to decode a one byte string and match to letter. */ |
| 1279 | static QCBORError |
| 1280 | CheckOneLetterString(QCBORDecodeContext *pDecode, uint8_t letter) |
| 1281 | { |
| 1282 | UsefulBufC Text; |
| 1283 | QCBORError uErr; |
| 1284 | |
| 1285 | QCBORDecode_GetTextString(pDecode, &Text); |
| 1286 | uErr = QCBORDecode_GetError(pDecode); |
| 1287 | if(uErr) { |
| 1288 | return uErr; |
| 1289 | } |
| 1290 | |
| 1291 | if(Text.len != 1) { |
| 1292 | return QCBOR_ERR_FIRST_USER_DEFINED; |
| 1293 | } |
| 1294 | |
| 1295 | if(*(const uint8_t *)Text.ptr != letter) { |
| 1296 | return QCBOR_ERR_FIRST_USER_DEFINED; |
| 1297 | } |
| 1298 | |
| 1299 | return QCBOR_SUCCESS; |
| 1300 | } |
| 1301 | #endif |
| 1302 | |
| 1303 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1304 | /* |
| 1305 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1306 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1307 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1308 | int32_t ParseMapAsArrayTest(void) |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1309 | { |
| 1310 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1311 | QCBORItem Item; |
| 1312 | QCBORError uErr; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1313 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1314 | QCBORDecode_Init(&DCtx, |
| 1315 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1316 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1317 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1318 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1319 | return MakeTestResultCode(1, 1, uErr); |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1320 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1321 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1322 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1323 | return -1; |
| 1324 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1325 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1326 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1327 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1328 | } |
| 1329 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1330 | Item.uDataAlloc || |
| 1331 | Item.uLabelAlloc || |
| 1332 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1333 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1334 | return -2; |
| 1335 | } |
| 1336 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1337 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1338 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1339 | } |
| 1340 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1341 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1342 | Item.val.int64 != 42 || |
| 1343 | Item.uDataAlloc || |
| 1344 | Item.uLabelAlloc) { |
| 1345 | return -3; |
| 1346 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1347 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1348 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1349 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1350 | } |
| 1351 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1352 | Item.uDataAlloc || |
| 1353 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1354 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1355 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1356 | return -4; |
| 1357 | } |
| 1358 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1359 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1360 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1361 | } |
| 1362 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1363 | Item.uDataAlloc || |
| 1364 | Item.uLabelAlloc || |
| 1365 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1366 | Item.val.uCount != 2) { |
| 1367 | return -5; |
| 1368 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1369 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1370 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1371 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1372 | } |
| 1373 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1374 | Item.val.string.len != 7 || |
| 1375 | Item.uDataAlloc || |
| 1376 | Item.uLabelAlloc || |
| 1377 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1378 | return -6; |
| 1379 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1380 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1381 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1382 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1383 | } |
| 1384 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1385 | Item.uDataAlloc || |
| 1386 | Item.uLabelAlloc || |
| 1387 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1388 | return -7; |
| 1389 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1390 | |
| 1391 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1392 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1393 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1394 | } |
| 1395 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1396 | Item.uDataAlloc || |
| 1397 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1398 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1399 | return -8; |
| 1400 | } |
| 1401 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1402 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1403 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1404 | } |
| 1405 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1406 | Item.uDataAlloc || |
| 1407 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1408 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1409 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1410 | return -9; |
| 1411 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1412 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1413 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1414 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1415 | } |
| 1416 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1417 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1418 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1419 | Item.uDataAlloc || |
| 1420 | Item.uLabelAlloc) { |
| 1421 | return -10; |
| 1422 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1423 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1424 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1425 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1426 | } |
| 1427 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1428 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1429 | Item.uDataAlloc || |
| 1430 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1431 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1432 | return -11; |
| 1433 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1434 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1435 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1436 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1437 | } |
| 1438 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1439 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1440 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1441 | Item.uDataAlloc || |
| 1442 | Item.uLabelAlloc) { |
| 1443 | return -12; |
| 1444 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1445 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1446 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1447 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1448 | } |
| 1449 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1450 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1451 | Item.uDataAlloc || |
| 1452 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1453 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1454 | return -13; |
| 1455 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1456 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1457 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1458 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1459 | } |
| 1460 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1461 | Item.uDataAlloc || |
| 1462 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1463 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1464 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1465 | return -14; |
| 1466 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1467 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1468 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1469 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1470 | } |
| 1471 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1472 | Item.uDataAlloc || |
| 1473 | Item.uLabelAlloc || |
| 1474 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1475 | Item.val.int64 != 98) { |
| 1476 | return -15; |
| 1477 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1478 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1479 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1480 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1481 | } |
| 1482 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1483 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1484 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1485 | Item.uDataAlloc || |
| 1486 | Item.uLabelAlloc) { |
| 1487 | return -16; |
| 1488 | } |
| 1489 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1490 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1491 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1492 | } |
| 1493 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1494 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1495 | Item.uDataAlloc || |
| 1496 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1497 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1498 | return -17; |
| 1499 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1500 | |
| 1501 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1502 | /* |
| 1503 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1504 | map that when interpreted as an array will be too many. Test |
| 1505 | data just has the start of the map, not all the items in the map. |
| 1506 | */ |
| 1507 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1508 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1509 | QCBORDecode_Init(&DCtx, |
| 1510 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1511 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1512 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1513 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1514 | return -50; |
| 1515 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1516 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 1517 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1518 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1519 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1520 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1521 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1522 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1523 | |
| 1524 | UsefulBufC Encoded; |
| 1525 | |
| 1526 | /* Big decode of a map with a wide variety or labels */ |
| 1527 | QCBORDecode_Init(&DCtx, |
| 1528 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels), |
| 1529 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 1530 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1531 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1532 | |
| 1533 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1534 | if(uErr) { |
| 1535 | return MakeTestResultCode(10, 1, uErr); |
| 1536 | } |
| 1537 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1538 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) { |
| 1539 | return MakeTestResultCode(10, 2, 0); |
| 1540 | } |
| 1541 | |
| 1542 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1543 | if(uErr) { |
| 1544 | return MakeTestResultCode(10, 3, uErr); |
| 1545 | } |
| 1546 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1547 | Item.uDataType != QCBOR_TYPE_TRUE) { |
| 1548 | return MakeTestResultCode(10, 4, 0); |
| 1549 | } |
| 1550 | |
| 1551 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1552 | if(uErr) { |
| 1553 | return MakeTestResultCode(10, 5, uErr); |
| 1554 | } |
| 1555 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1556 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1557 | ((const char *)Item.val.string.ptr)[0] != 'a') { |
| 1558 | return MakeTestResultCode(10, 6, 0); |
| 1559 | } |
| 1560 | |
| 1561 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1562 | if(uErr) { |
| 1563 | return MakeTestResultCode(10, 7, uErr); |
| 1564 | } |
| 1565 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1566 | Item.uDataType != QCBOR_TYPE_NULL) { |
| 1567 | return MakeTestResultCode(10, 8, 0); |
| 1568 | } |
| 1569 | |
| 1570 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1571 | if(uErr) { |
| 1572 | return MakeTestResultCode(10, 9, uErr); |
| 1573 | } |
| 1574 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1575 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1576 | ((const char *)Item.val.string.ptr)[0] != 'b') { |
| 1577 | return MakeTestResultCode(10, 10, 0); |
| 1578 | } |
| 1579 | |
| 1580 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1581 | if(uErr) { |
| 1582 | return MakeTestResultCode(10, 11, uErr); |
| 1583 | } |
| 1584 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1585 | Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || |
| 1586 | Item.val.int64 != 255) { |
| 1587 | return MakeTestResultCode(10, 12, 0); |
| 1588 | } |
| 1589 | |
| 1590 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1591 | if(uErr) { |
| 1592 | return MakeTestResultCode(10, 13, uErr); |
| 1593 | } |
| 1594 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1595 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1596 | ((const char *)Item.val.string.ptr)[0] != 'c') { |
| 1597 | return MakeTestResultCode(10, 14, 0); |
| 1598 | } |
| 1599 | |
| 1600 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1601 | if(uErr) { |
| 1602 | return MakeTestResultCode(10, 15, uErr); |
| 1603 | } |
| 1604 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1605 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1606 | !isnan(Item.val.dfnum)) { |
| 1607 | return MakeTestResultCode(10, 16, 0); |
| 1608 | } |
| 1609 | |
| 1610 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1611 | if(uErr) { |
| 1612 | return MakeTestResultCode(10, 17, uErr); |
| 1613 | } |
| 1614 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1615 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1616 | ((const char *)Item.val.string.ptr)[0] != 'd') { |
| 1617 | return MakeTestResultCode(10, 18, 0); |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1622 | if(uErr) { |
| 1623 | return MakeTestResultCode(10, 19, uErr); |
| 1624 | } |
| 1625 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1626 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1627 | Item.val.dfnum != 3.4028234663852886E+38) { |
| 1628 | return MakeTestResultCode(10, 20, 0); |
| 1629 | } |
| 1630 | |
| 1631 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1632 | if(uErr) { |
| 1633 | return MakeTestResultCode(10, 21, uErr); |
| 1634 | } |
| 1635 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1636 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1637 | ((const char *)Item.val.string.ptr)[0] != 'e') { |
| 1638 | return MakeTestResultCode(10, 22, 0); |
| 1639 | } |
| 1640 | |
| 1641 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1642 | if(uErr) { |
| 1643 | return MakeTestResultCode(10, 23, uErr); |
| 1644 | } |
| 1645 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1646 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1647 | Item.val.dfnum != -INFINITY) { |
| 1648 | return MakeTestResultCode(10, 24, 0); |
| 1649 | } |
| 1650 | |
| 1651 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1652 | if(uErr) { |
| 1653 | return MakeTestResultCode(10, 25, uErr); |
| 1654 | } |
| 1655 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1656 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1657 | ((const char *)Item.val.string.ptr)[0] != 'f') { |
| 1658 | return MakeTestResultCode(10, 26, 0); |
| 1659 | } |
| 1660 | |
| 1661 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1662 | if(uErr) { |
| 1663 | return MakeTestResultCode(10, 26, uErr); |
| 1664 | } |
| 1665 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1666 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1667 | Item.val.uCount != 2) { |
| 1668 | return MakeTestResultCode(10, 27, 0); |
| 1669 | } |
| 1670 | |
| 1671 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1672 | if(uErr) { |
| 1673 | return MakeTestResultCode(10, 28, uErr); |
| 1674 | } |
| 1675 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1676 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1677 | Item.val.int64 != 1000) { |
| 1678 | return MakeTestResultCode(10, 29, 0); |
| 1679 | } |
| 1680 | |
| 1681 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1682 | if(uErr) { |
| 1683 | return MakeTestResultCode(10, 30, uErr); |
| 1684 | } |
| 1685 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1686 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1687 | Item.val.int64 != 16) { |
| 1688 | return MakeTestResultCode(10, 31, 0); |
| 1689 | } |
| 1690 | |
| 1691 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1692 | if(uErr) { |
| 1693 | return MakeTestResultCode(10, 32, uErr); |
| 1694 | } |
| 1695 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1696 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1697 | ((const char *)Item.val.string.ptr)[0] != 'g') { |
| 1698 | return MakeTestResultCode(10, 33, 0); |
| 1699 | } |
| 1700 | |
| 1701 | for(int i = 0 ; i < 4; i++) { |
| 1702 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1703 | if(uErr) { |
| 1704 | return MakeTestResultCode(10, 34, uErr); |
| 1705 | } |
| 1706 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1707 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 1708 | return MakeTestResultCode(10, 35, 0); |
| 1709 | } |
| 1710 | if(i != 3) { |
| 1711 | if(Item.val.uCount != 1) { |
| 1712 | return MakeTestResultCode(10, 35, 0); |
| 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1717 | if(uErr) { |
| 1718 | return MakeTestResultCode(10, 36, uErr); |
| 1719 | } |
| 1720 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1721 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1722 | ((const char *)Item.val.string.ptr)[0] != 'h') { |
| 1723 | return MakeTestResultCode(10, 37, 0); |
| 1724 | } |
| 1725 | |
| 1726 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1727 | if(uErr) { |
| 1728 | return MakeTestResultCode(10, 38, uErr); |
| 1729 | } |
| 1730 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1731 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 1732 | return MakeTestResultCode(10, 39, 0); |
| 1733 | } |
| 1734 | |
| 1735 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1736 | if(uErr) { |
| 1737 | return MakeTestResultCode(10, 40, uErr); |
| 1738 | } |
| 1739 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1740 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1741 | ((const char *)Item.val.string.ptr)[0] != 'i') { |
| 1742 | return MakeTestResultCode(10, 41, 0); |
| 1743 | } |
| 1744 | |
| 1745 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1746 | if(uErr) { |
| 1747 | return MakeTestResultCode(10, 42, uErr); |
| 1748 | } |
| 1749 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1750 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1751 | Item.val.uCount != 2) { |
| 1752 | return MakeTestResultCode(10, 31, 0); |
| 1753 | } |
| 1754 | |
| 1755 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1756 | if(uErr) { |
| 1757 | return MakeTestResultCode(10, 43, uErr); |
| 1758 | } |
| 1759 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1760 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1761 | return MakeTestResultCode(10, 31, 0); |
| 1762 | } |
| 1763 | |
| 1764 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1765 | if(uErr) { |
| 1766 | return MakeTestResultCode(10, 44, uErr); |
| 1767 | } |
| 1768 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1769 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1770 | Item.val.uCount != 4) { |
| 1771 | return MakeTestResultCode(10, 45, 0); |
| 1772 | } |
| 1773 | |
| 1774 | for(int i = 0 ; i < 4; i++) { |
| 1775 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1776 | if(uErr) { |
| 1777 | return MakeTestResultCode(10, 46, uErr); |
| 1778 | } |
| 1779 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1780 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1781 | return MakeTestResultCode(10, 47, 0); |
| 1782 | } |
| 1783 | } |
| 1784 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1785 | if(uErr) { |
| 1786 | return MakeTestResultCode(10, 48, uErr); |
| 1787 | } |
| 1788 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1789 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1790 | ((const char *)Item.val.string.ptr)[0] != 'j') { |
| 1791 | return MakeTestResultCode(10, 49, 0); |
| 1792 | } |
| 1793 | |
| 1794 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1795 | if(uErr) { |
| 1796 | return MakeTestResultCode(10, 50, uErr); |
| 1797 | } |
| 1798 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1799 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1800 | Item.val.uCount != UINT16_MAX) { |
| 1801 | return MakeTestResultCode(10, 51, 0); |
| 1802 | } |
| 1803 | |
| 1804 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1805 | if(uErr) { |
| 1806 | return MakeTestResultCode(10, 52, uErr); |
| 1807 | } |
| 1808 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1809 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1810 | ((const char *)Item.val.string.ptr)[0] != 'k') { |
| 1811 | return MakeTestResultCode(10, 53, 0); |
| 1812 | } |
| 1813 | |
| 1814 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1815 | if(uErr) { |
| 1816 | return MakeTestResultCode(10, 54, uErr); |
| 1817 | } |
| 1818 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1819 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1820 | Item.val.uCount != UINT16_MAX) { |
| 1821 | return MakeTestResultCode(10, 55, 0); |
| 1822 | } |
| 1823 | |
| 1824 | for(int i = 0 ; i < 3; i++) { |
| 1825 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1826 | if(uErr) { |
| 1827 | return MakeTestResultCode(10, 56, uErr); |
| 1828 | } |
| 1829 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1830 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1831 | return MakeTestResultCode(10, 57, 0); |
| 1832 | } |
| 1833 | } |
| 1834 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1835 | if(uErr) { |
| 1836 | return MakeTestResultCode(10, 58, uErr); |
| 1837 | } |
| 1838 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1839 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1840 | ((const char *)Item.val.string.ptr)[0] != 'l') { |
| 1841 | return MakeTestResultCode(10, 59, 0); |
| 1842 | } |
| 1843 | |
| 1844 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1845 | if(uErr) { |
| 1846 | return MakeTestResultCode(10, 60, uErr); |
| 1847 | } |
| 1848 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1849 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1850 | Item.val.string.len != 4) { |
| 1851 | return MakeTestResultCode(10, 61, 0); |
| 1852 | } |
| 1853 | |
| 1854 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1855 | if(uErr) { |
| 1856 | return MakeTestResultCode(10, 62, uErr); |
| 1857 | } |
| 1858 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1859 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1860 | ((const char *)Item.val.string.ptr)[0] != 'm') { |
| 1861 | return MakeTestResultCode(10, 63, 0); |
| 1862 | } |
| 1863 | |
| 1864 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1865 | if(uErr) { |
| 1866 | return MakeTestResultCode(10, 64, uErr); |
| 1867 | } |
| 1868 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1869 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1870 | !QCBORDecode_IsTagged(&DCtx, &Item, 258) || |
| 1871 | Item.val.uCount != UINT16_MAX) { |
| 1872 | return MakeTestResultCode(10, 65, 0); |
| 1873 | } |
| 1874 | |
| 1875 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1876 | if(uErr) { |
| 1877 | return MakeTestResultCode(10, 66, uErr); |
| 1878 | } |
| 1879 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1880 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1881 | Item.val.string.len != 2) { |
| 1882 | return MakeTestResultCode(10, 67, 0); |
| 1883 | } |
| 1884 | |
| 1885 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1886 | if(uErr) { |
| 1887 | return MakeTestResultCode(10, 68, uErr); |
| 1888 | } |
| 1889 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1890 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1891 | Item.val.int64 != 0) { |
| 1892 | return MakeTestResultCode(10, 69, 0); |
| 1893 | } |
| 1894 | |
| 1895 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1896 | if(uErr) { |
| 1897 | return MakeTestResultCode(10, 70, uErr); |
| 1898 | } |
| 1899 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1900 | Item.uDataType != QCBOR_TYPE_FALSE) { |
| 1901 | return MakeTestResultCode(10, 71, 0); |
| 1902 | } |
| 1903 | |
| 1904 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1905 | if(uErr) { |
| 1906 | return MakeTestResultCode(10, 72, uErr); |
| 1907 | } |
| 1908 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1909 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1910 | !QCBORDecode_IsTagged(&DCtx, &Item, 23) || |
| 1911 | Item.val.uCount != 0) { |
| 1912 | return MakeTestResultCode(10, 73, 0); |
| 1913 | } |
| 1914 | |
| 1915 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1916 | if(uErr) { |
| 1917 | return MakeTestResultCode(10, 74, uErr); |
| 1918 | } |
| 1919 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1920 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1921 | ((const char *)Item.val.string.ptr)[0] != 'n') { |
| 1922 | return MakeTestResultCode(10, 75, 0); |
| 1923 | } |
| 1924 | |
| 1925 | |
| 1926 | /* Big decode of a map with a wide variety or labels */ |
| 1927 | QCBORDecode_Init(&DCtx, |
| 1928 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels), |
| 1929 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 1930 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1931 | |
| 1932 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1933 | bool b; |
| 1934 | QCBORDecode_GetBool(&DCtx, &b); |
| 1935 | |
| 1936 | uErr = CheckOneLetterString(&DCtx, 'a'); |
| 1937 | if(uErr) { |
| 1938 | return MakeTestResultCode(11, 1, uErr); |
| 1939 | } |
| 1940 | |
| 1941 | QCBORDecode_GetNull(&DCtx); |
| 1942 | uErr = CheckOneLetterString(&DCtx, 'b'); |
| 1943 | if(uErr) { |
| 1944 | return MakeTestResultCode(11, 2, uErr); |
| 1945 | } |
| 1946 | |
| 1947 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 1948 | uErr = CheckOneLetterString(&DCtx, 'c'); |
| 1949 | if(uErr) { |
| 1950 | return MakeTestResultCode(11, 3, uErr); |
| 1951 | } |
| 1952 | |
| 1953 | double dNum; |
| 1954 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1955 | if(!isnan(dNum)) { |
| 1956 | return MakeTestResultCode(11, 4, 0); |
| 1957 | } |
| 1958 | uErr = CheckOneLetterString(&DCtx, 'd'); |
| 1959 | if(uErr) { |
| 1960 | return MakeTestResultCode(11, 5, uErr); |
| 1961 | } |
| 1962 | |
| 1963 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1964 | if( dNum != 3.4028234663852886E+38 ) { |
| 1965 | return MakeTestResultCode(11, 6, 0); |
| 1966 | } |
| 1967 | uErr = CheckOneLetterString(&DCtx, 'e'); |
| 1968 | if(uErr) { |
| 1969 | return MakeTestResultCode(11, 7, uErr); |
| 1970 | } |
| 1971 | |
| 1972 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1973 | if(dNum != -INFINITY) { |
| 1974 | return MakeTestResultCode(11, 8, 0); |
| 1975 | } |
| 1976 | uErr = CheckOneLetterString(&DCtx, 'f'); |
| 1977 | if(uErr) { |
| 1978 | return MakeTestResultCode(11, 9, uErr); |
| 1979 | } |
| 1980 | |
| 1981 | int64_t nInt; |
| 1982 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1983 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 1984 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 1985 | QCBORDecode_ExitArray(&DCtx); |
| 1986 | uErr = CheckOneLetterString(&DCtx, 'g'); |
| 1987 | if(uErr) { |
| 1988 | return MakeTestResultCode(11, 10, uErr); |
| 1989 | } |
| 1990 | |
| 1991 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1992 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1993 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1994 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1995 | QCBORDecode_ExitArray(&DCtx); |
| 1996 | QCBORDecode_ExitArray(&DCtx); |
| 1997 | QCBORDecode_ExitArray(&DCtx); |
| 1998 | QCBORDecode_ExitArray(&DCtx); |
| 1999 | uErr = CheckOneLetterString(&DCtx, 'h'); |
| 2000 | if(uErr) { |
| 2001 | return MakeTestResultCode(11, 11, uErr); |
| 2002 | } |
| 2003 | QCBORDecode_GetEpochDate(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 2004 | uErr = CheckOneLetterString(&DCtx, 'i'); |
| 2005 | if(uErr) { |
| 2006 | return MakeTestResultCode(11, 12, uErr); |
| 2007 | } |
| 2008 | |
| 2009 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2010 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2011 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2012 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2013 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2014 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2015 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2016 | QCBORDecode_ExitArray(&DCtx); |
| 2017 | QCBORDecode_ExitArray(&DCtx); |
| 2018 | uErr = CheckOneLetterString(&DCtx, 'j'); |
| 2019 | if(uErr) { |
| 2020 | return MakeTestResultCode(11, 13, uErr); |
| 2021 | } |
| 2022 | |
| 2023 | QCBORDecode_GetArray(&DCtx, &Item, &Encoded); |
| 2024 | uErr = CheckOneLetterString(&DCtx, 'k'); |
| 2025 | if(uErr) { |
| 2026 | return MakeTestResultCode(11, 14, uErr); |
| 2027 | } |
| 2028 | |
| 2029 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2030 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2031 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2032 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2033 | QCBORDecode_ExitArray(&DCtx); |
| 2034 | uErr = CheckOneLetterString(&DCtx, 'l'); |
| 2035 | if(uErr) { |
| 2036 | return MakeTestResultCode(11, 15, uErr); |
| 2037 | } |
| 2038 | |
| 2039 | QCBORDecode_GetTextString(&DCtx, &Encoded); |
| 2040 | uErr = CheckOneLetterString(&DCtx, 'm'); |
| 2041 | if(uErr) { |
| 2042 | return MakeTestResultCode(11, 16, uErr); |
| 2043 | } |
| 2044 | |
| 2045 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2046 | if(!QCBORDecode_IsTagged(&DCtx, &Item, 258)) { |
| 2047 | return MakeTestResultCode(11, 17, 0); |
| 2048 | } |
| 2049 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) { |
| 2050 | return MakeTestResultCode(11, 18, 0); |
| 2051 | } |
| 2052 | if(Item.val.uCount != UINT16_MAX) { |
| 2053 | return MakeTestResultCode(11, 19, 0); |
| 2054 | } |
| 2055 | QCBORDecode_GetTextString(&DCtx, &Encoded); |
| 2056 | if(Encoded.len != 2) { |
| 2057 | return MakeTestResultCode(11, 20, 0); |
| 2058 | } QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2059 | QCBORDecode_GetBool(&DCtx, &b); |
| 2060 | if(b != false) { |
| 2061 | return MakeTestResultCode(11, 21, 0); |
| 2062 | } |
| 2063 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2064 | if(!QCBORDecode_IsTagged(&DCtx, &Item, 23)) { |
| 2065 | return MakeTestResultCode(11, 22, 0); |
| 2066 | } |
| 2067 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2068 | return MakeTestResultCode(11, 23, 0); |
| 2069 | } |
| 2070 | if(Item.val.uCount != 0) { |
| 2071 | return MakeTestResultCode(11, 24, 0); |
| 2072 | } |
| 2073 | QCBORDecode_ExitArray(&DCtx); |
| 2074 | QCBORDecode_ExitArray(&DCtx); |
| 2075 | uErr = CheckOneLetterString(&DCtx, 'n'); |
| 2076 | if(uErr) { |
| 2077 | return MakeTestResultCode(11, 25, uErr); |
| 2078 | } |
| 2079 | |
| 2080 | QCBORDecode_ExitArray(&DCtx); |
| 2081 | uErr = QCBORDecode_Finish(&DCtx); |
| 2082 | if(uErr) { |
| 2083 | return MakeTestResultCode(11, 26, uErr); |
| 2084 | } |
| 2085 | #endif /* QCBOR_DISABLE_... */ |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 2086 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2087 | return 0; |
| 2088 | } |
| 2089 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2090 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame^] | 2091 | |
| 2092 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2093 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2094 | Fully or partially decode pValidMapEncoded. When |
| 2095 | partially decoding check for the right error code. |
| 2096 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2097 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2098 | The partial decodes test error conditions of |
| 2099 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2100 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2101 | This could be combined with the above test |
| 2102 | and made prettier and maybe a little more |
| 2103 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2104 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2105 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2106 | { |
| 2107 | QCBORDecodeContext DCtx; |
| 2108 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2109 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2110 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2111 | QCBORDecode_Init(&DCtx, |
| 2112 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 2113 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2114 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2115 | if(nLevel < 1) { |
| 2116 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 2117 | return -1; |
| 2118 | } else { |
| 2119 | return 0; |
| 2120 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2121 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2122 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2123 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2124 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2125 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2126 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2127 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2128 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2129 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2130 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2131 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2132 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2133 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2134 | } else { |
| 2135 | return 0; |
| 2136 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2137 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2138 | |
| 2139 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2140 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2141 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2142 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2143 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2144 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 2145 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2146 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2147 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2148 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2149 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2150 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2151 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2152 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2153 | } else { |
| 2154 | return 0; |
| 2155 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2156 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2157 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2158 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2159 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2160 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2161 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2162 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2163 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2164 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2165 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2166 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2167 | |
| 2168 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2169 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2170 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2171 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2172 | } else { |
| 2173 | return 0; |
| 2174 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2175 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2176 | |
| 2177 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2178 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2179 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2180 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2181 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2182 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2183 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2184 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2185 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2186 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2187 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2188 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2189 | } else { |
| 2190 | return 0; |
| 2191 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2192 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2193 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2194 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2195 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2196 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2197 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2198 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2199 | return -10; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2200 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2201 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2202 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2203 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2204 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2205 | } else { |
| 2206 | return 0; |
| 2207 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2208 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2209 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2210 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2211 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2212 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2213 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2214 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2215 | Item.uDataType != QCBOR_TYPE_MAP || |
| 2216 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2217 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2218 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2219 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2220 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2221 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2222 | } else { |
| 2223 | return 0; |
| 2224 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2225 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2226 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2227 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2228 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2229 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2230 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2231 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2232 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2233 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2234 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2235 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2236 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2237 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2238 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2239 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2240 | } else { |
| 2241 | return 0; |
| 2242 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2243 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2244 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2245 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2246 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2247 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2248 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2249 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2250 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2251 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2252 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2253 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2254 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2255 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2256 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2257 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2258 | } else { |
| 2259 | return 0; |
| 2260 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2261 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2262 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2263 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2264 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2265 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2266 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2267 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2268 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 2269 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2270 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2271 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2272 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2273 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2274 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2275 | } else { |
| 2276 | return 0; |
| 2277 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2278 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2279 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2280 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2281 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2282 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2283 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2284 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2285 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2286 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2287 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2288 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2289 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2290 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2291 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2292 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2293 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2294 | return 0; |
| 2295 | } |
| 2296 | |
| 2297 | |
| 2298 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2299 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2300 | int32_t ParseMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2301 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2302 | // Parse a moderatly complex map structure very thoroughly |
| 2303 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 2304 | if(nResult) { |
| 2305 | return nResult; |
| 2306 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2307 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2308 | // Again, but in strings-only mode. It should succeed since the input |
| 2309 | // map has only string labels. |
| 2310 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 2311 | if(nResult) { |
| 2312 | return nResult; |
| 2313 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2314 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2315 | // Again, but try to finish the decoding before the end of the |
| 2316 | // input at 10 different place and see that the right error code |
| 2317 | // is returned. |
| 2318 | for(int i = 0; i < 10; i++) { |
| 2319 | nResult = ExtraBytesTest(i); |
| 2320 | if(nResult) { |
| 2321 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2322 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2323 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2324 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2325 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2329 | /* The simple-values including some not well formed */ |
| 2330 | static const uint8_t spSimpleValues[] = { |
| 2331 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 2332 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 2333 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2334 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2335 | int32_t ParseSimpleTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2336 | { |
| 2337 | QCBORDecodeContext DCtx; |
| 2338 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2339 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2340 | |
| 2341 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2342 | QCBORDecode_Init(&DCtx, |
| 2343 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2344 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2345 | |
| 2346 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2347 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2348 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2349 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2350 | Item.val.uCount != 10) |
| 2351 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2352 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2353 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2354 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2355 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 2356 | return -1; |
| 2357 | |
| 2358 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2359 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2360 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 2361 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2362 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2363 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2364 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2365 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 2366 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2367 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2368 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2369 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2370 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 2371 | return -1; |
| 2372 | |
| 2373 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 2374 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2375 | return -1; |
| 2376 | |
| 2377 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2378 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2379 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 2380 | return -1; |
| 2381 | |
| 2382 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2383 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2384 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 2385 | return -1; |
| 2386 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2387 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2388 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2389 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2390 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2391 | return -1; |
| 2392 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2393 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2394 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2395 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2396 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2397 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2398 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 2399 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2400 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2401 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2402 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2403 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 2404 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2405 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2406 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2407 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2411 | int32_t NotWellFormedTests(void) |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2412 | { |
| 2413 | // Loop over all the not-well-formed instance of CBOR |
| 2414 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2415 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 2416 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2417 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 2418 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 2419 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 2420 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2421 | if(nIterate == 86) { |
| 2422 | nIterate = 86; |
| 2423 | } |
| 2424 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2425 | // Set up decoder context. String allocator needed for indefinite |
| 2426 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2427 | QCBORDecodeContext DCtx; |
| 2428 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2429 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2430 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 2431 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2432 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2433 | |
| 2434 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2435 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2436 | do { |
| 2437 | QCBORItem Item; |
| 2438 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2439 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2440 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2441 | |
| 2442 | // Every test vector must fail with |
| 2443 | // a not-well-formed error. If not |
| 2444 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2445 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2446 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2447 | /* Return index of failure and QCBOR error in the result */ |
| 2448 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | return 0; |
| 2452 | } |
| 2453 | |
| 2454 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2455 | struct DecodeFailTestInput { |
| 2456 | const char *szDescription; /* Description of the test */ |
| 2457 | QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */ |
| 2458 | UsefulBufC Input; /* Chunk of CBOR that cases error */ |
| 2459 | QCBORError nError; /* The expected error */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2460 | }; |
| 2461 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2462 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2463 | static int32_t |
| 2464 | ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2465 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2466 | int nIndex; |
| 2467 | QCBORDecodeContext DCtx; |
| 2468 | QCBORError uCBORError; |
| 2469 | QCBORItem Item; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2470 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2471 | for(nIndex = 0; nIndex < nNumFails; nIndex++) { |
| 2472 | const struct DecodeFailTestInput *pF = &pFailInputs[nIndex]; |
| 2473 | |
| 2474 | QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2475 | |
| 2476 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2477 | /* Set up the decoding context including a memory pool so that |
| 2478 | * indefinite length items can be checked. |
| 2479 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2480 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2481 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2482 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2483 | if(uCBORError != QCBOR_SUCCESS) { |
| 2484 | return -1; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2485 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2486 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2487 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2488 | if(nIndex == 8) { |
| 2489 | uCBORError = 9; /* For setting break points */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2490 | } |
| 2491 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2492 | /* Iterate until there is an error of some sort of error */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2493 | do { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2494 | /* Set to something none-zero, something other than QCBOR_TYPE_NONE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2495 | memset(&Item, 0x33, sizeof(Item)); |
| 2496 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2497 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2498 | } while(uCBORError == QCBOR_SUCCESS); |
| 2499 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2500 | /* Must get the expected error or the this test fails |
| 2501 | * The data and label type must also be QCBOR_TYPE_NONE. |
| 2502 | */ |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2503 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2504 | Item.uDataType != QCBOR_TYPE_NONE || |
| 2505 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2506 | return (int32_t)(nIndex * 1000 + (int)uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | return 0; |
| 2511 | } |
| 2512 | |
| 2513 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2514 | static const struct DecodeFailTestInput Failures[] = { |
| 2515 | /* Most of this is copied from not_well_formed.h. Here the error |
| 2516 | * code returned is also checked. |
| 2517 | */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2518 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2519 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2520 | /* Indefinite length strings must be closed off */ |
| 2521 | { "An indefinite length byte string not closed off", |
| 2522 | QCBOR_DECODE_MODE_NORMAL, |
| 2523 | {"0x5f\x41\x00", 3}, |
| 2524 | QCBOR_ERR_HIT_END |
| 2525 | }, |
| 2526 | { "An indefinite length text string not closed off", |
| 2527 | QCBOR_DECODE_MODE_NORMAL, |
| 2528 | {"\x7f\x61\x00", 3}, |
| 2529 | QCBOR_ERR_HIT_END |
| 2530 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2531 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2532 | /* All the chunks in an indefinite length string must be of the |
| 2533 | * type of indefinite length string |
| 2534 | */ |
| 2535 | { "Indefinite length byte string with text string chunk", |
| 2536 | QCBOR_DECODE_MODE_NORMAL, |
| 2537 | {"\x5f\x61\x00\xff", 4}, |
| 2538 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2539 | }, |
| 2540 | { "Indefinite length text string with a byte string chunk", |
| 2541 | QCBOR_DECODE_MODE_NORMAL, |
| 2542 | {"\x7f\x41\x00\xff", 4}, |
| 2543 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2544 | }, |
| 2545 | { "Indefinite length byte string with a positive integer chunk", |
| 2546 | QCBOR_DECODE_MODE_NORMAL, |
| 2547 | {"\x5f\x00\xff", 3}, |
| 2548 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2549 | }, |
| 2550 | { "Indefinite length byte string with an negative integer chunk", |
| 2551 | QCBOR_DECODE_MODE_NORMAL, |
| 2552 | {"\x5f\x21\xff", 3}, |
| 2553 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2554 | }, |
| 2555 | { "Indefinite length byte string with an array chunk", |
| 2556 | QCBOR_DECODE_MODE_NORMAL, |
| 2557 | {"\x5f\x80\xff", 3}, |
| 2558 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2559 | }, |
| 2560 | { "Indefinite length byte string with an map chunk", |
| 2561 | QCBOR_DECODE_MODE_NORMAL, |
| 2562 | {"\x5f\xa0\xff", 3}, |
| 2563 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2564 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2565 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2566 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2567 | { "Indefinite length byte string with tagged integer chunk", |
| 2568 | QCBOR_DECODE_MODE_NORMAL, |
| 2569 | {"\x5f\xc0\x00\xff", 4}, |
| 2570 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2571 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2572 | #else |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2573 | { "Indefinite length byte string with tagged integer chunk", |
| 2574 | QCBOR_DECODE_MODE_NORMAL, |
| 2575 | {"\x5f\xc0\x00\xff", 4}, |
| 2576 | QCBOR_ERR_TAGS_DISABLED |
| 2577 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2578 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2579 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2580 | { "Indefinite length byte string with an simple type chunk", |
| 2581 | QCBOR_DECODE_MODE_NORMAL, |
| 2582 | {"\x5f\xe0\xff", 3}, |
| 2583 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2584 | }, |
| 2585 | { "???", |
| 2586 | QCBOR_DECODE_MODE_NORMAL, |
| 2587 | {"\x5f\x5f\x41\x00\xff\xff", 6}, |
| 2588 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2589 | }, |
| 2590 | { "indefinite length text string with indefinite string inside", |
| 2591 | QCBOR_DECODE_MODE_NORMAL, |
| 2592 | {"\x7f\x7f\x61\x00\xff\xff", 6}, |
| 2593 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2594 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2595 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2596 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2597 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2598 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2599 | /* Definte length maps and arrays must be closed by having the right number of items */ |
| 2600 | { "A definte length array that is supposed to have 1 item, but has none", |
| 2601 | QCBOR_DECODE_MODE_NORMAL, |
| 2602 | {"\x81", 1}, |
| 2603 | QCBOR_ERR_NO_MORE_ITEMS |
| 2604 | }, |
| 2605 | { "A definte length array that is supposed to have 2 items, but has only 1", |
| 2606 | QCBOR_DECODE_MODE_NORMAL, |
| 2607 | {"\x82\x00", 2}, |
| 2608 | QCBOR_ERR_NO_MORE_ITEMS |
| 2609 | }, |
| 2610 | { "A definte length array that is supposed to have 511 items, but has only 1", |
| 2611 | QCBOR_DECODE_MODE_NORMAL, |
| 2612 | {"\x9a\x01\xff\x00", 4}, |
| 2613 | QCBOR_ERR_HIT_END |
| 2614 | }, |
| 2615 | { "A definte length map that is supposed to have 1 item, but has none", |
| 2616 | QCBOR_DECODE_MODE_NORMAL, |
| 2617 | {"\xa1", 1}, |
| 2618 | QCBOR_ERR_NO_MORE_ITEMS |
| 2619 | }, |
| 2620 | { "A definte length map that is supposed to have s item, but has only 1", |
| 2621 | QCBOR_DECODE_MODE_NORMAL, |
| 2622 | {"\xa2\x01\x02", 3}, |
| 2623 | QCBOR_ERR_NO_MORE_ITEMS |
| 2624 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2625 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2626 | /* Indefinte length maps and arrays must be ended by a break */ |
| 2627 | { "Indefinite length array with zero items and no break", |
| 2628 | QCBOR_DECODE_MODE_NORMAL, |
| 2629 | {"\x9f", 1}, |
| 2630 | QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2631 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2632 | { "Indefinite length array with two items and no break", |
| 2633 | QCBOR_DECODE_MODE_NORMAL, |
| 2634 | {"\x9\x01\x02", 3}, |
| 2635 | QCBOR_ERR_NO_MORE_ITEMS |
| 2636 | }, |
| 2637 | { "Indefinite length map with zero items and no break", |
| 2638 | QCBOR_DECODE_MODE_NORMAL, |
| 2639 | {"\xbf", 1}, |
| 2640 | QCBOR_ERR_NO_MORE_ITEMS |
| 2641 | }, |
| 2642 | { "Indefinite length map with two items and no break", |
| 2643 | QCBOR_DECODE_MODE_NORMAL, |
| 2644 | {"\xbf\x01\x02\x01\x02", 5}, |
| 2645 | QCBOR_ERR_NO_MORE_ITEMS |
| 2646 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2647 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2648 | /* Nested maps and arrays must be closed off (some extra nested test vectors) */ |
| 2649 | { "Unclosed indefinite array containing a closed definite length array", |
| 2650 | QCBOR_DECODE_MODE_NORMAL, |
| 2651 | {"\x9f\x80\x00", 3}, |
| 2652 | QCBOR_ERR_NO_MORE_ITEMS |
| 2653 | }, |
| 2654 | |
| 2655 | { "Definite length array containing an unclosed indefinite length array", |
| 2656 | QCBOR_DECODE_MODE_NORMAL, |
| 2657 | {"\x81\x9f", 2}, |
| 2658 | QCBOR_ERR_NO_MORE_ITEMS |
| 2659 | }, |
| 2660 | { "Unclosed indefinite map containing a closed definite length array", |
| 2661 | QCBOR_DECODE_MODE_NORMAL, |
| 2662 | {"\xbf\x01\x80\x00\xa0", 5}, |
| 2663 | QCBOR_ERR_NO_MORE_ITEMS |
| 2664 | }, |
| 2665 | { "Definite length map containing an unclosed indefinite length array", |
| 2666 | QCBOR_DECODE_MODE_NORMAL, |
| 2667 | {"\xa1\x02\x9f", 3}, |
| 2668 | QCBOR_ERR_NO_MORE_ITEMS |
| 2669 | }, |
| 2670 | { "Deeply nested definite length arrays with deepest one unclosed", |
| 2671 | QCBOR_DECODE_MODE_NORMAL, |
| 2672 | {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9}, |
| 2673 | QCBOR_ERR_NO_MORE_ITEMS |
| 2674 | }, |
| 2675 | { "Deeply nested indefinite length arrays with deepest one unclosed", |
| 2676 | QCBOR_DECODE_MODE_NORMAL, |
| 2677 | {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9}, |
| 2678 | QCBOR_ERR_NO_MORE_ITEMS |
| 2679 | }, |
| 2680 | { "Mixed nesting with indefinite unclosed", |
| 2681 | QCBOR_DECODE_MODE_NORMAL, |
| 2682 | {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9}, |
| 2683 | QCBOR_ERR_NO_MORE_ITEMS }, |
| 2684 | { "Mixed nesting with definite unclosed", |
| 2685 | QCBOR_DECODE_MODE_NORMAL, |
| 2686 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2687 | QCBOR_ERR_BAD_BREAK |
| 2688 | }, |
| 2689 | { "Unclosed indefinite length map in definite length maps", |
| 2690 | QCBOR_DECODE_MODE_NORMAL, |
| 2691 | {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8}, |
| 2692 | QCBOR_ERR_NO_MORE_ITEMS |
| 2693 | }, |
| 2694 | { "Unclosed definite length map in indefinite length maps", |
| 2695 | QCBOR_DECODE_MODE_NORMAL, |
| 2696 | {"\xbf\x01\xbf\x02\xa1", 5}, |
| 2697 | QCBOR_ERR_NO_MORE_ITEMS |
| 2698 | }, |
| 2699 | { "Unclosed indefinite length array in definite length maps", |
| 2700 | QCBOR_DECODE_MODE_NORMAL, |
| 2701 | {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8}, |
| 2702 | QCBOR_ERR_NO_MORE_ITEMS |
| 2703 | }, |
| 2704 | { "Unclosed definite length array in indefinite length maps", |
| 2705 | QCBOR_DECODE_MODE_NORMAL, |
| 2706 | {"\xbf\x01\xbf\x02\x81", 5}, |
| 2707 | QCBOR_ERR_NO_MORE_ITEMS |
| 2708 | }, |
| 2709 | { "Unclosed indefinite length map in definite length arrays", |
| 2710 | QCBOR_DECODE_MODE_NORMAL, |
| 2711 | {"\x81\x82\xbf\xff\xbf", 5}, |
| 2712 | QCBOR_ERR_NO_MORE_ITEMS |
| 2713 | }, |
| 2714 | { "Unclosed definite length map in indefinite length arrays", |
| 2715 | QCBOR_DECODE_MODE_NORMAL, |
| 2716 | {"\x9f\x9f\xa1", 3}, |
| 2717 | QCBOR_ERR_NO_MORE_ITEMS |
| 2718 | }, |
| 2719 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2720 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2721 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2722 | /* The "argument" for the data item is incomplete */ |
| 2723 | { "Positive integer missing 1 byte argument", |
| 2724 | QCBOR_DECODE_MODE_NORMAL, |
| 2725 | {"\x18", 1}, |
| 2726 | QCBOR_ERR_HIT_END |
| 2727 | }, |
| 2728 | { "Positive integer missing 2 byte argument", |
| 2729 | QCBOR_DECODE_MODE_NORMAL, |
| 2730 | {"\x19", 1}, |
| 2731 | QCBOR_ERR_HIT_END |
| 2732 | }, |
| 2733 | { "Positive integer missing 4 byte argument", |
| 2734 | QCBOR_DECODE_MODE_NORMAL, |
| 2735 | {"\x1a", 1}, |
| 2736 | QCBOR_ERR_HIT_END |
| 2737 | }, |
| 2738 | { "Positive integer missing 8 byte argument", |
| 2739 | QCBOR_DECODE_MODE_NORMAL, |
| 2740 | {"\x1b", 1}, |
| 2741 | QCBOR_ERR_HIT_END |
| 2742 | }, |
| 2743 | { "Positive integer missing 1 byte of 2 byte argument", |
| 2744 | QCBOR_DECODE_MODE_NORMAL, |
| 2745 | {"\x19\x01", 2}, |
| 2746 | QCBOR_ERR_HIT_END |
| 2747 | }, |
| 2748 | { "Positive integer missing 2 bytes of 4 byte argument", |
| 2749 | QCBOR_DECODE_MODE_NORMAL, |
| 2750 | {"\x1a\x01\x02", 3}, |
| 2751 | QCBOR_ERR_HIT_END |
| 2752 | }, |
| 2753 | { "Positive integer missing 1 bytes of 7 byte argument", |
| 2754 | QCBOR_DECODE_MODE_NORMAL, |
| 2755 | {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8}, |
| 2756 | QCBOR_ERR_HIT_END |
| 2757 | }, |
| 2758 | { "Negative integer missing 1 byte argument", |
| 2759 | QCBOR_DECODE_MODE_NORMAL, |
| 2760 | {"\x38", 1}, |
| 2761 | QCBOR_ERR_HIT_END |
| 2762 | }, |
| 2763 | { "Binary string missing 1 byte argument", |
| 2764 | QCBOR_DECODE_MODE_NORMAL, |
| 2765 | {"\x58", 1}, |
| 2766 | QCBOR_ERR_HIT_END |
| 2767 | }, |
| 2768 | { "Text string missing 1 byte argument", |
| 2769 | QCBOR_DECODE_MODE_NORMAL, |
| 2770 | {"\x78", 1}, |
| 2771 | QCBOR_ERR_HIT_END |
| 2772 | }, |
| 2773 | { "Array missing 1 byte argument", |
| 2774 | QCBOR_DECODE_MODE_NORMAL, |
| 2775 | {"\x98", 1}, |
| 2776 | QCBOR_ERR_HIT_END |
| 2777 | }, |
| 2778 | { "Map missing 1 byte argument", |
| 2779 | QCBOR_DECODE_MODE_NORMAL, |
| 2780 | {"\xb8", 1}, |
| 2781 | QCBOR_ERR_HIT_END |
| 2782 | }, |
| 2783 | { "Tag missing 1 byte argument", |
| 2784 | QCBOR_DECODE_MODE_NORMAL, |
| 2785 | {"\xd8", 1}, |
| 2786 | QCBOR_ERR_HIT_END |
| 2787 | }, |
| 2788 | { "Simple missing 1 byte argument", |
| 2789 | QCBOR_DECODE_MODE_NORMAL, |
| 2790 | {"\xf8", 1}, |
| 2791 | QCBOR_ERR_HIT_END |
| 2792 | }, |
| 2793 | { "half-precision with 1 byte argument", |
| 2794 | QCBOR_DECODE_MODE_NORMAL, |
| 2795 | {"\xf9\x00", 2}, |
| 2796 | QCBOR_ERR_HIT_END |
| 2797 | }, |
| 2798 | { "single-precision with 2 byte argument", |
| 2799 | QCBOR_DECODE_MODE_NORMAL, |
| 2800 | {"\0xfa\x00\x00", 3}, |
| 2801 | QCBOR_ERR_HIT_END |
| 2802 | }, |
| 2803 | { "double-precision with 3 byte argument", |
| 2804 | QCBOR_DECODE_MODE_NORMAL, |
| 2805 | {"\xfb\x00\x00\x00", 4}, |
| 2806 | QCBOR_ERR_HIT_END |
| 2807 | }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2808 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2809 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2810 | { "Tag with no content", |
| 2811 | QCBOR_DECODE_MODE_NORMAL, |
| 2812 | {"\xc0", 1}, |
| 2813 | QCBOR_ERR_HIT_END |
| 2814 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2815 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2816 | { "Tag with no content", |
| 2817 | QCBOR_DECODE_MODE_NORMAL, |
| 2818 | {"\xc0", 1}, |
| 2819 | QCBOR_ERR_TAGS_DISABLED |
| 2820 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2821 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2822 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2823 | /* Breaks must not occur in definite length arrays and maps */ |
| 2824 | { "Array of length 1 with sole member replaced by a break", |
| 2825 | QCBOR_DECODE_MODE_NORMAL, |
| 2826 | {"\x81\xff", 2}, |
| 2827 | QCBOR_ERR_BAD_BREAK |
| 2828 | }, |
| 2829 | { "Array of length 2 with 2nd member replaced by a break", |
| 2830 | QCBOR_DECODE_MODE_NORMAL, |
| 2831 | {"\x82\x00\xff", 3}, |
| 2832 | QCBOR_ERR_BAD_BREAK |
| 2833 | }, |
| 2834 | { "Map of length 1 with sole member label replaced by a break", |
| 2835 | QCBOR_DECODE_MODE_NORMAL, |
| 2836 | {"\xa1\xff", 2}, |
| 2837 | QCBOR_ERR_BAD_BREAK |
| 2838 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2839 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2840 | /* Map of length 1 with sole member label replaced by break */ |
| 2841 | { "Alternate representation that some decoders handle differently", |
| 2842 | QCBOR_DECODE_MODE_NORMAL, |
| 2843 | {"\xa1\xff\x00", 3}, |
| 2844 | QCBOR_ERR_BAD_BREAK |
| 2845 | }, |
| 2846 | { "Array of length 1 with 2nd member value replaced by a break", |
| 2847 | QCBOR_DECODE_MODE_NORMAL, |
| 2848 | {"\xa1\x00\xff", 3}, |
| 2849 | QCBOR_ERR_BAD_BREAK |
| 2850 | }, |
| 2851 | { "Map of length 2 with 2nd member replaced by a break", |
| 2852 | QCBOR_DECODE_MODE_NORMAL, |
| 2853 | {"\xa2\x00\x00\xff", 4}, |
| 2854 | QCBOR_ERR_BAD_BREAK |
| 2855 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2856 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2857 | /* Breaks must not occur on their own out of an indefinite length data item */ |
| 2858 | { "A bare break is not well formed", |
| 2859 | QCBOR_DECODE_MODE_NORMAL, |
| 2860 | {"\xff", 1}, |
| 2861 | QCBOR_ERR_BAD_BREAK |
| 2862 | }, |
| 2863 | { "A bare break after a zero length definite length array", |
| 2864 | QCBOR_DECODE_MODE_NORMAL, |
| 2865 | {"\x80\xff", 2}, |
| 2866 | QCBOR_ERR_BAD_BREAK |
| 2867 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2868 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2869 | { "A bare break after a zero length indefinite length map", |
| 2870 | QCBOR_DECODE_MODE_NORMAL, |
| 2871 | {"\x9f\xff\xff", 3}, |
| 2872 | QCBOR_ERR_BAD_BREAK |
| 2873 | }, |
| 2874 | { "A break inside a definite length array inside an indefenite length array", |
| 2875 | QCBOR_DECODE_MODE_NORMAL, |
| 2876 | {"\x9f\x81\xff", 3}, |
| 2877 | QCBOR_ERR_BAD_BREAK |
| 2878 | }, |
| 2879 | { "Complicated mixed nesting with break outside indefinite length array", |
| 2880 | QCBOR_DECODE_MODE_NORMAL, |
| 2881 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2882 | QCBOR_ERR_BAD_BREAK }, |
| 2883 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 2884 | |
| 2885 | /* Forbidden two byte encodings of simple types */ |
| 2886 | { "Must use 0xe0 instead", |
| 2887 | QCBOR_DECODE_MODE_NORMAL, |
| 2888 | {"\xf8\x00", 2}, |
| 2889 | QCBOR_ERR_BAD_TYPE_7 |
| 2890 | }, |
| 2891 | { "Should use 0xe1 instead", |
| 2892 | QCBOR_DECODE_MODE_NORMAL, |
| 2893 | {"\xf8\x01", 2}, |
| 2894 | QCBOR_ERR_BAD_TYPE_7 |
| 2895 | }, |
| 2896 | { "Should use 0xe2 instead", |
| 2897 | QCBOR_DECODE_MODE_NORMAL, |
| 2898 | {"\xf8\x02", 2}, |
| 2899 | QCBOR_ERR_BAD_TYPE_7 |
| 2900 | }, { "Should use 0xe3 instead", |
| 2901 | QCBOR_DECODE_MODE_NORMAL, |
| 2902 | {"\xf8\x03", 2}, |
| 2903 | QCBOR_ERR_BAD_TYPE_7 |
| 2904 | }, |
| 2905 | { "Should use 0xe4 instead", |
| 2906 | QCBOR_DECODE_MODE_NORMAL, |
| 2907 | {"\xf8\x04", 2}, |
| 2908 | QCBOR_ERR_BAD_TYPE_7 |
| 2909 | }, |
| 2910 | { "Should use 0xe5 instead", |
| 2911 | QCBOR_DECODE_MODE_NORMAL, |
| 2912 | {"\xf8\x05", 2}, |
| 2913 | QCBOR_ERR_BAD_TYPE_7 |
| 2914 | }, |
| 2915 | { "Should use 0xe6 instead", |
| 2916 | QCBOR_DECODE_MODE_NORMAL, |
| 2917 | {"\xf8\x06", 2}, |
| 2918 | QCBOR_ERR_BAD_TYPE_7 |
| 2919 | }, |
| 2920 | { "Should use 0xe7 instead", |
| 2921 | QCBOR_DECODE_MODE_NORMAL, |
| 2922 | {"\xf8\x07", 2}, |
| 2923 | QCBOR_ERR_BAD_TYPE_7 |
| 2924 | }, |
| 2925 | { "Should use 0xe8 instead", |
| 2926 | QCBOR_DECODE_MODE_NORMAL, |
| 2927 | {"\xf8\x08", 2}, |
| 2928 | QCBOR_ERR_BAD_TYPE_7 |
| 2929 | }, |
| 2930 | { "Should use 0xe9 instead", |
| 2931 | QCBOR_DECODE_MODE_NORMAL, |
| 2932 | {"\xf8\x09", 2}, |
| 2933 | QCBOR_ERR_BAD_TYPE_7 |
| 2934 | }, |
| 2935 | { "Should use 0xea instead", |
| 2936 | QCBOR_DECODE_MODE_NORMAL, |
| 2937 | {"\xf8\x0a", 2}, |
| 2938 | QCBOR_ERR_BAD_TYPE_7 |
| 2939 | }, |
| 2940 | { "Should use 0xeb instead", |
| 2941 | QCBOR_DECODE_MODE_NORMAL, |
| 2942 | {"\xf8\x0b", 2}, |
| 2943 | QCBOR_ERR_BAD_TYPE_7 |
| 2944 | }, |
| 2945 | { "Should use 0xec instead", |
| 2946 | QCBOR_DECODE_MODE_NORMAL, |
| 2947 | {"\xf8\x0c", 2}, |
| 2948 | QCBOR_ERR_BAD_TYPE_7 |
| 2949 | }, |
| 2950 | { "Should use 0xed instead", |
| 2951 | QCBOR_DECODE_MODE_NORMAL, |
| 2952 | {"\xf8\x0d", 2}, |
| 2953 | QCBOR_ERR_BAD_TYPE_7 |
| 2954 | }, |
| 2955 | { "Should use 0xee instead", |
| 2956 | QCBOR_DECODE_MODE_NORMAL, |
| 2957 | {"\xf8\x0e", 2}, |
| 2958 | QCBOR_ERR_BAD_TYPE_7 |
| 2959 | }, |
| 2960 | { "Should use 0xef instead", |
| 2961 | QCBOR_DECODE_MODE_NORMAL, |
| 2962 | {"\xf8\x0f", 2}, |
| 2963 | QCBOR_ERR_BAD_TYPE_7 |
| 2964 | }, |
| 2965 | { "Should use 0xf0 instead", |
| 2966 | QCBOR_DECODE_MODE_NORMAL, |
| 2967 | {"\xf8\x10", 2}, |
| 2968 | QCBOR_ERR_BAD_TYPE_7 |
| 2969 | }, |
| 2970 | { "Should use 0xf1 instead", |
| 2971 | QCBOR_DECODE_MODE_NORMAL, |
| 2972 | {"\xf8\x11", 2}, |
| 2973 | QCBOR_ERR_BAD_TYPE_7 |
| 2974 | }, |
| 2975 | { "Should use 0xf2 instead", |
| 2976 | QCBOR_DECODE_MODE_NORMAL, |
| 2977 | {"\xf8\x12", 2}, |
| 2978 | QCBOR_ERR_BAD_TYPE_7 |
| 2979 | }, |
| 2980 | { "Should use 0xf3 instead", |
| 2981 | QCBOR_DECODE_MODE_NORMAL, |
| 2982 | {"\xf8\x13", 2}, |
| 2983 | QCBOR_ERR_BAD_TYPE_7 |
| 2984 | }, |
| 2985 | { "Should use 0xf4 instead", |
| 2986 | QCBOR_DECODE_MODE_NORMAL, |
| 2987 | {"\xf8\x14", 2}, |
| 2988 | QCBOR_ERR_BAD_TYPE_7 |
| 2989 | }, |
| 2990 | { "Should use 0xf5 instead", |
| 2991 | QCBOR_DECODE_MODE_NORMAL, |
| 2992 | {"\xf8\x15", 2}, |
| 2993 | QCBOR_ERR_BAD_TYPE_7 |
| 2994 | }, |
| 2995 | { "Should use 0xf6 instead", |
| 2996 | QCBOR_DECODE_MODE_NORMAL, |
| 2997 | {"\xf8\x16", 2}, |
| 2998 | QCBOR_ERR_BAD_TYPE_7 |
| 2999 | }, |
| 3000 | { "Should use 0xef7 instead", |
| 3001 | QCBOR_DECODE_MODE_NORMAL, |
| 3002 | {"\xf8\x17", 2}, |
| 3003 | QCBOR_ERR_BAD_TYPE_7 |
| 3004 | }, |
| 3005 | { "Should use 0xef8 instead", |
| 3006 | QCBOR_DECODE_MODE_NORMAL, |
| 3007 | {"\xf8\x18", 2}, |
| 3008 | QCBOR_ERR_BAD_TYPE_7 |
| 3009 | }, |
| 3010 | { "Reserved", |
| 3011 | QCBOR_DECODE_MODE_NORMAL, |
| 3012 | {"\xf8\x18", 2}, |
| 3013 | QCBOR_ERR_BAD_TYPE_7 |
| 3014 | }, |
| 3015 | |
| 3016 | /* Maps must have an even number of data items (key & value) */ |
| 3017 | { "Map with 1 item when it should have 2", |
| 3018 | QCBOR_DECODE_MODE_NORMAL, |
| 3019 | {"\xa1\x00", 2}, |
| 3020 | QCBOR_ERR_HIT_END |
| 3021 | }, |
| 3022 | { "Map with 3 item when it should have 4", |
| 3023 | QCBOR_DECODE_MODE_NORMAL, |
| 3024 | {"\xa2\x00\x00\x00", 2}, |
| 3025 | QCBOR_ERR_HIT_END |
| 3026 | }, |
| 3027 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 3028 | { "Map with 1 item when it should have 2", |
| 3029 | QCBOR_DECODE_MODE_NORMAL, |
| 3030 | {"\xbf\x00\xff", 3}, |
| 3031 | QCBOR_ERR_BAD_BREAK |
| 3032 | }, |
| 3033 | { "Map with 3 item when it should have 4", |
| 3034 | QCBOR_DECODE_MODE_NORMAL, |
| 3035 | {"\xbf\x00\x00\x00\xff", 5}, |
| 3036 | QCBOR_ERR_BAD_BREAK |
| 3037 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3038 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3039 | |
| 3040 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3041 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3042 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3043 | { "Text-based date, with an integer", |
| 3044 | QCBOR_DECODE_MODE_NORMAL, |
| 3045 | {"\xc0\x00", 2}, |
| 3046 | QCBOR_ERR_BAD_OPT_TAG |
| 3047 | }, |
| 3048 | { "Epoch date, with an byte string", |
| 3049 | QCBOR_DECODE_MODE_NORMAL, |
| 3050 | {"\xc1\x41\x33", 3}, |
| 3051 | QCBOR_ERR_BAD_OPT_TAG |
| 3052 | }, |
| 3053 | { "tagged as both epoch and string dates", |
| 3054 | QCBOR_DECODE_MODE_NORMAL, |
| 3055 | {"\xc1\xc0\x00", 3}, |
| 3056 | QCBOR_ERR_BAD_OPT_TAG |
| 3057 | }, |
| 3058 | { "big num tagged an int, not a byte string", |
| 3059 | QCBOR_DECODE_MODE_NORMAL, |
| 3060 | {"\xc2\x00", 2}, |
| 3061 | QCBOR_ERR_BAD_OPT_TAG |
| 3062 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3063 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3064 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3065 | { "Text-based date, with an integer", |
| 3066 | QCBOR_DECODE_MODE_NORMAL, |
| 3067 | {"\xc0\x00", 2}, |
| 3068 | QCBOR_ERR_TAGS_DISABLED |
| 3069 | }, |
| 3070 | { "Epoch date, with an byte string", |
| 3071 | QCBOR_DECODE_MODE_NORMAL, |
| 3072 | {"\xc1\x41\x33", 3}, |
| 3073 | QCBOR_ERR_TAGS_DISABLED |
| 3074 | }, |
| 3075 | { "tagged as both epoch and string dates", |
| 3076 | QCBOR_DECODE_MODE_NORMAL, |
| 3077 | {"\xc1\xc0\x00", 3}, |
| 3078 | QCBOR_ERR_TAGS_DISABLED |
| 3079 | }, |
| 3080 | { "big num tagged an int, not a byte string", |
| 3081 | QCBOR_DECODE_MODE_NORMAL, |
| 3082 | {"\xc2\x00", 2}, |
| 3083 | QCBOR_ERR_TAGS_DISABLED |
| 3084 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3085 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3086 | }; |
| 3087 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3088 | |
| 3089 | |
| 3090 | int32_t |
| 3091 | DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3092 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3093 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3094 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3095 | nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3096 | if(nResult) { |
| 3097 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3098 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3099 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3100 | // Corrupt the UsefulInputBuf and see that |
| 3101 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3102 | QCBORDecodeContext DCtx; |
| 3103 | QCBORItem Item; |
| 3104 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3105 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3106 | QCBORDecode_Init(&DCtx, |
| 3107 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 3108 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3109 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3110 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3111 | return (int32_t)uQCBORError; |
| 3112 | } |
| 3113 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 3114 | // This wasn't supposed to happen |
| 3115 | return -1; |
| 3116 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3117 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3118 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3119 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3120 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3121 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 3122 | // Did not get back the error expected |
| 3123 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3124 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3125 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3126 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3127 | /* |
| 3128 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 3129 | tests here can be performed to see that the max length |
| 3130 | error check works correctly. See DecodeBytes(). If the max |
| 3131 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3132 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3133 | This test will automatocally adapt the all CPU sizes |
| 3134 | through the use of SIZE_MAX. |
| 3135 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3136 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 3137 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3138 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3139 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3140 | // This makes a CBOR head with a text string that is very long |
| 3141 | // but doesn't fill in the bytes of the text string as that is |
| 3142 | // not needed to test this part of QCBOR. |
| 3143 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 3144 | |
| 3145 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 3146 | |
| 3147 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3148 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3149 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3150 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3151 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3155 | /* Try all 256 values of the byte at nLen including recursing for |
| 3156 | each of the values to try values at nLen+1 ... up to nLenMax |
| 3157 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 3158 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3159 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3160 | if(nLen >= nLenMax) { |
| 3161 | return; |
| 3162 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3163 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3164 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 3165 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3166 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3167 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3168 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3169 | // Get ready to parse |
| 3170 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3171 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3172 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3173 | // Parse by getting the next item until an error occurs |
| 3174 | // Just about every possible decoder error can occur here |
| 3175 | // The goal of this test is not to check for the correct |
| 3176 | // error since that is not really possible. It is to |
| 3177 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3178 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3179 | QCBORItem Item; |
| 3180 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3181 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3182 | break; |
| 3183 | } |
| 3184 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3185 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3186 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3191 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3192 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3193 | // Size 2 tests 64K inputs and runs quickly |
| 3194 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3195 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3196 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3197 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3198 | return 0; |
| 3199 | } |
| 3200 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3201 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3202 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3203 | { |
| 3204 | // size 3 tests 16 million inputs and runs OK |
| 3205 | // in seconds on fast machines. Size 4 takes |
| 3206 | // 10+ minutes and 5 half a day on fast |
| 3207 | // machines. This test is kept separate from |
| 3208 | // the others so as to no slow down the use |
| 3209 | // of them as a very frequent regression. |
| 3210 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3211 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3212 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3213 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3214 | return 0; |
| 3215 | } |
| 3216 | |
| 3217 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3218 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3219 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3220 | 0xc0, // tag for string date |
| 3221 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3222 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3223 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3224 | 0xc0, // tag for string date |
| 3225 | 0x00, // Wrong type for a string date |
| 3226 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3227 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3228 | 0xc1, // tag for epoch date |
| 3229 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3230 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3231 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3232 | 0xc1, |
| 3233 | 0x62, 'h', 'i', // wrong type tagged |
| 3234 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3235 | /* 5. Valid epoch date tag as content for a two other nested tags */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 3236 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3237 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3238 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 3239 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3240 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3241 | 0xc1, // tag for epoch date |
| 3242 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3243 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3244 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3245 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 3246 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3247 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3248 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3249 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3250 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3251 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3252 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3253 | 0xc1, // tag for epoch date |
| 3254 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 3255 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 3256 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3257 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3258 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3259 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3260 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3261 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3262 | 0xc1, // tag for epoch date |
| 3263 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 3264 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3265 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3266 | 0xc1, |
| 3267 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 3268 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3269 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3270 | 0xc1, // tag for epoch date |
| 3271 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3272 | }; |
| 3273 | |
| 3274 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3275 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3276 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3277 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 3278 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3279 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3280 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3281 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3282 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3283 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3284 | return diff > 0.0000001; |
| 3285 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3286 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3287 | |
| 3288 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3289 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3290 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3291 | { |
| 3292 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3293 | QCBORItem Item; |
| 3294 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3295 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3296 | QCBORDecode_Init(&DCtx, |
| 3297 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 3298 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3299 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3300 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3301 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3302 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3303 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3304 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | ba58768 | 2024-02-07 16:46:43 -0800 | [diff] [blame] | 3305 | UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3306 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3307 | } |
| 3308 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3309 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3310 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3311 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3312 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3315 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 3316 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3317 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3318 | return -4; |
| 3319 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3320 | if(uError == QCBOR_SUCCESS) { |
| 3321 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3322 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3323 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3324 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3325 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3326 | ) { |
| 3327 | return -5; |
| 3328 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3331 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3332 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3333 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3334 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3335 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3336 | /* 5. Valid epoch date tag as content for a two other nested tags */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 3337 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 3338 | // The date is decoded and the two tags are returned. This is to |
| 3339 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3340 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3341 | return -7; |
| 3342 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3343 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3344 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3345 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3346 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3347 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 3348 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3349 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3350 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3351 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3352 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3353 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3354 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3355 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3356 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3357 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 3358 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3359 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3360 | return -10; |
| 3361 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3362 | if(uError == QCBOR_SUCCESS) { |
| 3363 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3364 | Item.val.epochDate.nSeconds != 1 |
| 3365 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3366 | || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1) |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3367 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3368 | ) { |
| 3369 | return -11; |
| 3370 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3371 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3372 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3373 | /* 8. Epoch date with too-large single-precision float */ |
| 3374 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3375 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3376 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3377 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3378 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3379 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 3380 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3381 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3382 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3383 | } |
| 3384 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3385 | /* 10. Epoch date with largest supported double-precision value */ |
| 3386 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3387 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3388 | return -14; |
| 3389 | } |
| 3390 | if(uError == QCBOR_SUCCESS) { |
| 3391 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3392 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 3393 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3394 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 3395 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 3396 | ) { |
| 3397 | return -14; |
| 3398 | } |
| 3399 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3400 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3401 | /* 11. Epoch date with single-precision NaN */ |
| 3402 | if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3403 | return -15; |
| 3404 | } |
| 3405 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3406 | /* 12. Epoch date with double-precision plus infinity */ |
| 3407 | if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3408 | return -16; |
| 3409 | } |
| 3410 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3411 | /* 13. Epoch date with half-precision negative infinity */ |
| 3412 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3413 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3414 | return -17; |
| 3415 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3416 | |
| 3417 | return 0; |
| 3418 | } |
| 3419 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3420 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3421 | /* |
| 3422 | Test cases covered here. Some items cover more than one of these. |
| 3423 | positive integer (zero counts as a positive integer) |
| 3424 | negative integer |
| 3425 | half-precision float |
| 3426 | single-precision float |
| 3427 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3428 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3429 | float Overflow error |
| 3430 | Wrong type error for epoch |
| 3431 | Wrong type error for date string |
| 3432 | float disabled error |
| 3433 | half-precision disabled error |
| 3434 | -Infinity |
| 3435 | Slightly too large integer |
| 3436 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3437 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3438 | Get epoch by int |
| 3439 | Get string by int |
| 3440 | Get epoch by string |
| 3441 | Get string by string |
| 3442 | Fail to get epoch by wrong int label |
| 3443 | Fail to get string by wrong string label |
| 3444 | Fail to get epoch by string because it is invalid |
| 3445 | Fail to get epoch by int because it is invalid |
| 3446 | |
| 3447 | Untagged values |
| 3448 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3449 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3450 | 0x87, // array of 7 items |
| 3451 | |
| 3452 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 3453 | |
| 3454 | // Untagged integer 0 |
| 3455 | 0x08, |
| 3456 | 0x00, |
| 3457 | |
| 3458 | // Utagged date string with string label y |
| 3459 | 0x61, 0x79, |
| 3460 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 3461 | |
| 3462 | // Untagged single-precision float with value 3.14 with string label x |
| 3463 | 0x61, 0x78, |
| 3464 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 3465 | |
| 3466 | // Untagged half-precision float with value -2 |
| 3467 | 0x09, |
| 3468 | 0xF9, 0xC0, 0x00, |
| 3469 | |
| 3470 | /* Untagged date-only date string */ |
| 3471 | 0x18, 0x63, |
| 3472 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3473 | |
| 3474 | /* Untagged days-count epoch date */ |
| 3475 | 0x11, |
| 3476 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 3477 | |
| 3478 | // End of map, back to array |
| 3479 | |
| 3480 | 0xa7, // Open map of tagged items with labels |
| 3481 | |
| 3482 | 0x00, |
| 3483 | 0xc0, // tag for string date |
| 3484 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 3485 | |
| 3486 | |
| 3487 | 0x01, |
| 3488 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 3489 | 0xc1, // tag for epoch date |
| 3490 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3491 | |
| 3492 | 0x05, |
| 3493 | 0xc1, |
| 3494 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 3495 | |
| 3496 | |
| 3497 | 0x07, |
| 3498 | 0xc1, // tag for epoch date |
| 3499 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3500 | |
| 3501 | /* Tagged days-count epoch date */ |
| 3502 | 0x63, 0x53, 0x44, 0x45, |
| 3503 | 0xD8, 0x64, /* tag(100) */ |
| 3504 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 3505 | |
| 3506 | // Untagged -1000 with label z |
| 3507 | 0x61, 0x7a, |
| 3508 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 3509 | 0x39, 0x03, 0xe7, |
| 3510 | |
| 3511 | /* Tagged date-only date string */ |
| 3512 | 0x63, 0x53, 0x44, 0x53, |
| 3513 | 0xD9, 0x03, 0xEC, |
| 3514 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3515 | |
| 3516 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3517 | |
| 3518 | 0xc1, |
| 3519 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 3520 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3521 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3522 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 3523 | |
| 3524 | 0xc1, // tag for epoch date |
| 3525 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 3526 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3527 | // These two at the end because they are unrecoverable errors |
| 3528 | 0xc1, // tag for epoch date |
| 3529 | 0x80, // Erroneous empty array as content for date |
| 3530 | |
| 3531 | 0xc0, // tag for string date |
| 3532 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3533 | }; |
| 3534 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3535 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3536 | { |
| 3537 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3538 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3539 | int64_t nEpochDate3, nEpochDate5, |
| 3540 | nEpochDate4, nEpochDate6, |
| 3541 | nEpochDays2; |
| 3542 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3543 | |
| 3544 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3545 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3546 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3547 | |
| 3548 | /* Items are in an array or map to test look up by label and other |
| 3549 | * that might not occur in isolated items. But it does make the |
| 3550 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3551 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3552 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3553 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3554 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3555 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3556 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3557 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3558 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3559 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3560 | return 104; |
| 3561 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3562 | if(uError == QCBOR_SUCCESS) { |
| 3563 | if(nEpochDate5 != 3) { |
| 3564 | return 103; |
| 3565 | } |
| 3566 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3567 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3568 | // A half-precision date with value -2 FFF |
| 3569 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3570 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3571 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3572 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3573 | return 106; |
| 3574 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3575 | if(uError == QCBOR_SUCCESS) { |
| 3576 | if(nEpochDate4 != -2) { |
| 3577 | return 105; |
| 3578 | } |
| 3579 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3580 | |
| 3581 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3582 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 3583 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3584 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3585 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3586 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3587 | return 107; |
| 3588 | } |
| 3589 | |
| 3590 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3591 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3592 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3593 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3594 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3595 | return 108; |
| 3596 | } |
| 3597 | |
| 3598 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3599 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 3600 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3601 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3602 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3603 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3604 | return 109; |
| 3605 | } |
| 3606 | |
| 3607 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3608 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3609 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3610 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3611 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3612 | return 110; |
| 3613 | } |
| 3614 | |
| 3615 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3616 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3617 | |
| 3618 | // Untagged integer 0 |
| 3619 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3620 | &nEpochDate3); |
| 3621 | // Untagged date string |
| 3622 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3623 | &StringDate2); |
| 3624 | |
| 3625 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3626 | &StringDays2); |
| 3627 | |
| 3628 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3629 | &nEpochDays2); |
| 3630 | |
| 3631 | QCBORDecode_ExitMap(&DC); |
| 3632 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3633 | return 3001; |
| 3634 | } |
| 3635 | |
| 3636 | // The map of tagged items |
| 3637 | QCBORDecode_EnterMap(&DC, NULL); |
| 3638 | |
| 3639 | #ifndef QCBOR_DISABLE_TAGS |
| 3640 | int64_t nEpochDate2, |
| 3641 | nEpochDateFail, |
| 3642 | nEpochDate1400000000, nEpochDays1; |
| 3643 | UsefulBufC StringDays1; |
| 3644 | uint64_t uTag1, uTag2; |
| 3645 | |
| 3646 | // Tagged date string |
| 3647 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3648 | &StringDate1); |
| 3649 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3650 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3651 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3652 | 1, |
| 3653 | QCBOR_TAG_REQUIREMENT_TAG | |
| 3654 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3655 | &nEpochDate1400000000); |
| 3656 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3657 | |
| 3658 | // Get largest negative double precision epoch date allowed |
| 3659 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3660 | 5, |
| 3661 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 3662 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3663 | &nEpochDate2); |
| 3664 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3665 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3666 | return 102; |
| 3667 | } |
| 3668 | if(uError == QCBOR_SUCCESS) { |
| 3669 | if(nEpochDate2 != -9223372036854773760LL) { |
| 3670 | return 101; |
| 3671 | } |
| 3672 | } |
| 3673 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3674 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3675 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 3676 | "z", |
| 3677 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 3678 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3679 | &nEpochDate6); |
| 3680 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3681 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3682 | |
| 3683 | // Get largest double precision epoch date allowed |
| 3684 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3685 | &nEpochDate2); |
| 3686 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3687 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3688 | return 112; |
| 3689 | } |
| 3690 | if(uError == QCBOR_SUCCESS) { |
| 3691 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 3692 | return 111; |
| 3693 | } |
| 3694 | } |
| 3695 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3696 | /* The days format is much simpler than the date format |
| 3697 | * because it can't be a floating point value. The test |
| 3698 | * of the spiffy decode functions sufficiently covers |
| 3699 | * the test of the non-spiffy decode days date decoding. |
| 3700 | * There is no full fan out of the error conditions |
| 3701 | * and decode options as that is implemented by code |
| 3702 | * that is tested well by the date testing above. |
| 3703 | */ |
| 3704 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 3705 | &StringDays1); |
| 3706 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3707 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 3708 | &nEpochDays1); |
| 3709 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3710 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3711 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3712 | return 3001; |
| 3713 | } |
| 3714 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3715 | // Too-negative float, -9.2233720368547748E+18 |
| 3716 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3717 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3718 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3719 | return 1111; |
| 3720 | } |
| 3721 | |
| 3722 | // Too-large integer |
| 3723 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3724 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3725 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 3726 | return 1; |
| 3727 | } |
| 3728 | |
| 3729 | // Half-precision minus infinity |
| 3730 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3731 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3732 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3733 | return 2; |
| 3734 | } |
| 3735 | |
| 3736 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3737 | // Bad content for epoch date |
| 3738 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3739 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3740 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3741 | return 3; |
| 3742 | } |
| 3743 | |
| 3744 | // Bad content for string date |
| 3745 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 3746 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3747 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3748 | return 4; |
| 3749 | } |
| 3750 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3751 | QCBORDecode_ExitArray(&DC); |
| 3752 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3753 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3754 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3755 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3756 | #else /* QCBOR_DISABLE_TAGS */ |
| 3757 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3758 | &StringDate1); |
| 3759 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3760 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 3761 | return 4; |
| 3762 | } |
| 3763 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3764 | |
| 3765 | |
| 3766 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3767 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3768 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3769 | return 200; |
| 3770 | } |
| 3771 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3772 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3773 | return 201; |
| 3774 | } |
| 3775 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3776 | if(nEpochDays1 != -10676) { |
| 3777 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3778 | } |
| 3779 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3780 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3781 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3782 | } |
| 3783 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3784 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3785 | return 204; |
| 3786 | } |
| 3787 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3788 | if(nEpochDate6 != -1000) { |
| 3789 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3790 | } |
| 3791 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3792 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3793 | return 205; |
| 3794 | } |
| 3795 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3796 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3797 | |
| 3798 | if(nEpochDate3 != 0) { |
| 3799 | return 202; |
| 3800 | } |
| 3801 | |
| 3802 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3803 | return 206; |
| 3804 | } |
| 3805 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3806 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 3807 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3811 | return 208; |
| 3812 | } |
| 3813 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3814 | return 0; |
| 3815 | } |
| 3816 | |
| 3817 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3818 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3819 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3820 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3821 | 0x81, // Array of one |
| 3822 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 3823 | 0x82, // Array of two that is the faction 1/3 |
| 3824 | 0x01, |
| 3825 | 0x03, |
| 3826 | |
| 3827 | /* |
| 3828 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3829 | */ |
| 3830 | 0xd8, 0xe1, |
| 3831 | 0xd8, 0xe2, |
| 3832 | 0xd8, 0xe3, |
| 3833 | 0xd8, 0xe4, |
| 3834 | 0xd8, 0xe5, |
| 3835 | 0x80, |
| 3836 | |
| 3837 | /* tag 10489608748473423768( |
| 3838 | 2442302356( |
| 3839 | 21590( |
| 3840 | 240( |
| 3841 | [])))) |
| 3842 | */ |
| 3843 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3844 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 3845 | 0xd9, 0x54, 0x56, |
| 3846 | 0xd8, 0xf0, |
| 3847 | 0x80, |
| 3848 | |
| 3849 | /* tag 21590( |
| 3850 | 10489608748473423768( |
| 3851 | 2442302357( |
| 3852 | 65534( |
| 3853 | [])))) |
| 3854 | */ |
| 3855 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 3856 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3857 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3858 | 0xd9, 0xff, 0xfe, |
| 3859 | 0x80, |
| 3860 | |
| 3861 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3862 | works in conjuntion with entries above. |
| 3863 | 269488144(269488145(269488146(269488147([])))) |
| 3864 | */ |
| 3865 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 3866 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 3867 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 3868 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 3869 | 0x80, |
| 3870 | |
| 3871 | /* An invalid decimal fraction with an additional tag */ |
| 3872 | 0xd9, 0xff, 0xfa, |
| 3873 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 3874 | 0x00, // the integer 0; should be a byte string |
| 3875 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3876 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3877 | /* |
| 3878 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3879 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3880 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3881 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3882 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3883 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3884 | /* |
| 3885 | DB 9192939495969798 # tag(10489608748473423768) |
| 3886 | D8 88 # tag(136) |
| 3887 | C6 # tag(6) |
| 3888 | C7 # tag(7) |
| 3889 | 80 # array(0) |
| 3890 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3891 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3892 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3893 | |
| 3894 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3895 | 55799(55799(55799({ |
| 3896 | 6(7(-23)): 5859837686836516696(7({ |
| 3897 | 7(-20): 11({ |
| 3898 | 17(-18): 17(17(17("Organization"))), |
| 3899 | 9(-17): 773("SSG"), |
| 3900 | -15: 16(17(6(7("Confusion")))), |
| 3901 | 17(-16): 17("San Diego"), |
| 3902 | 17(-14): 17("US") |
| 3903 | }), |
| 3904 | 23(-19): 19({ |
| 3905 | -11: 9({ |
| 3906 | -9: -7 |
| 3907 | }), |
| 3908 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 3909 | }) |
| 3910 | })), |
| 3911 | 16(-22): 23({ |
| 3912 | 11(8(7(-5))): 8(-3) |
| 3913 | }) |
| 3914 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3915 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3916 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3917 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 3918 | 0xc6, 0xc7, 0x36, |
| 3919 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 3920 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 3921 | 0xcb, 0xa5, |
| 3922 | 0xd1, 0x31, |
| 3923 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 3924 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 3925 | 0xc9, 0x30, |
| 3926 | 0xd9, 0x03, 0x05, 0x63, |
| 3927 | 0x53, 0x53, 0x47, |
| 3928 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3929 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 3930 | 0x69, |
| 3931 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3932 | 0xd1, 0x2f, |
| 3933 | 0xd1, 0x69, |
| 3934 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 3935 | 0xd1, 0x2d, |
| 3936 | 0xd1, 0x62, |
| 3937 | 0x55, 0x53, |
| 3938 | 0xd7, 0x32, |
| 3939 | 0xd3, 0xa2, |
| 3940 | 0x2a, |
| 3941 | 0xc9, 0xa1, |
| 3942 | 0x28, |
| 3943 | 0x26, |
| 3944 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 3945 | 0xcc, 0x4a, |
| 3946 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 3947 | 0xd0, 0x35, |
| 3948 | 0xd7, 0xa1, |
| 3949 | 0xcb, 0xc8, 0xc7, 0x24, |
| 3950 | 0xc8, 0x22}; |
| 3951 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3952 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3953 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3954 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3955 | |
| 3956 | 0xc0, // tag for string date |
| 3957 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3958 | |
| 3959 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3960 | |
| 3961 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 3962 | |
| 3963 | 0xd8, 0x23, // tag for regex |
| 3964 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3965 | |
| 3966 | 0xc0, // tag for string date |
| 3967 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3968 | |
| 3969 | // This last case makes the array untraversable because it is |
| 3970 | // an uncrecoverable error. Make sure it stays last and is the only |
| 3971 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3972 | }; |
| 3973 | |
| 3974 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3975 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3976 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3977 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3978 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3979 | { |
| 3980 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3981 | QCBORItem Item; |
| 3982 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3983 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3984 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3985 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3986 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3987 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3988 | /* |
| 3989 | This test matches the magic number tag and the fraction tag |
| 3990 | 55799([...]) |
| 3991 | */ |
| 3992 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3993 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3994 | return -2; |
| 3995 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3996 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3997 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 3998 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3999 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4000 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4001 | /* |
| 4002 | 4([1,3]) |
| 4003 | */ |
| 4004 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4005 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4006 | if(uError != QCBOR_SUCCESS || |
| 4007 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4008 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 4009 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 4010 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4011 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4012 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4013 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 4014 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4015 | return -4; |
| 4016 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4017 | // consume the items in the array |
| 4018 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4019 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4020 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4021 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4022 | if(uError != QCBOR_SUCCESS || |
| 4023 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4024 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 4025 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4026 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4027 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4028 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 4029 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4030 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4031 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4032 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4033 | /* |
| 4034 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 4035 | */ |
| 4036 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4037 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4038 | return -6; |
| 4039 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4040 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 4041 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 4042 | return -106; |
| 4043 | } |
| 4044 | |
| 4045 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4046 | /* tag 10489608748473423768( |
| 4047 | 2442302356( |
| 4048 | 21590( |
| 4049 | 240( |
| 4050 | [])))) |
| 4051 | */ |
| 4052 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4053 | if(uError != QCBOR_SUCCESS || |
| 4054 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4055 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 4056 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 4057 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 4058 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4059 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | /* tag 21590( |
| 4063 | 10489608748473423768( |
| 4064 | 2442302357( |
| 4065 | 21591( |
| 4066 | [])))) |
| 4067 | */ |
| 4068 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4069 | if(uError != QCBOR_SUCCESS || |
| 4070 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4071 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 4072 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 4073 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 4074 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 4075 | return -8; |
| 4076 | } |
| 4077 | |
| 4078 | /* Make sure to blow past the limit of tags that must be mapped. |
| 4079 | works in conjuntion with entries above. |
| 4080 | 269488144(269488145(269488146(269488147([])))) |
| 4081 | */ |
| 4082 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4083 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4084 | return -9; |
| 4085 | } |
| 4086 | |
| 4087 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4088 | if(uError == QCBOR_SUCCESS) { |
| 4089 | return -10; |
| 4090 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4091 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4092 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4093 | // This test sets up a caller-config list that includes the very large |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4094 | // tage and then matches it. Caller-config lists are no longer |
| 4095 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4096 | QCBORDecode_Init(&DCtx, |
| 4097 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4098 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4099 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 4100 | const QCBORTagListIn TL = {2, puList}; |
| 4101 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4102 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4103 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4104 | return -8; |
| 4105 | } |
| 4106 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4107 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 4108 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 4109 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 4110 | Item.val.uCount != 0) { |
| 4111 | return -9; |
| 4112 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4113 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4114 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4115 | // Sets up a caller-configured list and look up something not in it |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4116 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4117 | const uint64_t puLongList[17] = {1,2,1}; |
| 4118 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4119 | QCBORDecode_Init(&DCtx, |
| 4120 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4121 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4122 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 4123 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4124 | return -11; |
| 4125 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4126 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4127 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4128 | QCBORTagListOut Out = {0, 4, puTags}; |
| 4129 | |
| 4130 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4131 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4132 | QCBORDecode_Init(&DCtx, |
| 4133 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4134 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4135 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4136 | return -12; |
| 4137 | } |
| 4138 | if(puTags[0] != 0x9192939495969798 || |
| 4139 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4140 | puTags[2] != 0x06 || |
| 4141 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4142 | return -13; |
| 4143 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4144 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4145 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4146 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4147 | QCBORDecode_Init(&DCtx, |
| 4148 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4149 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4150 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 4151 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4152 | return -14; |
| 4153 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4154 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4155 | |
| 4156 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4157 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4158 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 4159 | // It is a bit of a messy test and maybe could be improved, but |
| 4160 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4161 | QCBORDecode_Init(&DCtx, |
| 4162 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4163 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4164 | int n = CheckCSRMaps(&DCtx); |
| 4165 | if(n) { |
| 4166 | return n-2000; |
| 4167 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4168 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4169 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4170 | QCBORDecode_Init(&DCtx, |
| 4171 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4172 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4173 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4174 | /* With the spiffy decode revision, this tag list is not used. |
| 4175 | It doesn't matter if a tag is in this list or not so some |
| 4176 | tests that couldn't process a tag because it isn't in this list |
| 4177 | now can process these unlisted tags. The tests have been |
| 4178 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4179 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 4180 | const QCBORTagListIn TagList = {3, puTagList}; |
| 4181 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4182 | |
| 4183 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4184 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4185 | return -100; |
| 4186 | } |
| 4187 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4188 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4189 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 4190 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 4191 | Item.val.uCount != 2 || |
| 4192 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 4193 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 4194 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 4195 | Out.uNumUsed != 3) { |
| 4196 | return -101; |
| 4197 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4198 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4199 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4200 | return -102; |
| 4201 | } |
| 4202 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4203 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4204 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4205 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4206 | Item.val.uCount != 2 || |
| 4207 | puTags[0] != 5859837686836516696 || |
| 4208 | puTags[1] != 7 || |
| 4209 | Out.uNumUsed != 2) { |
| 4210 | return -103; |
| 4211 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4212 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4213 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4214 | return -104; |
| 4215 | } |
| 4216 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4217 | Item.val.uCount != 5 || |
| 4218 | puTags[0] != 0x0b || |
| 4219 | Out.uNumUsed != 1) { |
| 4220 | return -105; |
| 4221 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4222 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4223 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4224 | return -106; |
| 4225 | } |
| 4226 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4227 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 4228 | Item.val.string.len != 12 || |
| 4229 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 4230 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 4231 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 4232 | Out.uNumUsed != 3) { |
| 4233 | return -105; |
| 4234 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4235 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4236 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4237 | return -107; |
| 4238 | } |
| 4239 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4240 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 4241 | Item.val.string.len != 3 || |
| 4242 | puTags[0] != 773 || |
| 4243 | Out.uNumUsed != 1) { |
| 4244 | return -108; |
| 4245 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4246 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4247 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4248 | return -109; |
| 4249 | } |
| 4250 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4251 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4252 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4253 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4254 | puTags[3] != 7 || |
| 4255 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4256 | return -110; |
| 4257 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4258 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4259 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4260 | return -111; |
| 4261 | } |
| 4262 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4263 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4264 | Item.val.string.len != 9 || |
| 4265 | puTags[0] != 17 || |
| 4266 | Out.uNumUsed != 1) { |
| 4267 | return -112; |
| 4268 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4269 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4270 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4271 | return -111; |
| 4272 | } |
| 4273 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4274 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4275 | Item.val.string.len != 2 || |
| 4276 | puTags[0] != 17 || |
| 4277 | Out.uNumUsed != 1) { |
| 4278 | return -112; |
| 4279 | } |
| 4280 | |
| 4281 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4282 | return -113; |
| 4283 | } |
| 4284 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4285 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4286 | Item.val.uCount != 2 || |
| 4287 | puTags[0] != 19 || |
| 4288 | Out.uNumUsed != 1) { |
| 4289 | return -114; |
| 4290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4291 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4292 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4293 | return -115; |
| 4294 | } |
| 4295 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4296 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4297 | Item.val.uCount != 1 || |
| 4298 | puTags[0] != 9 || |
| 4299 | Out.uNumUsed != 1) { |
| 4300 | return -116; |
| 4301 | } |
| 4302 | |
| 4303 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4304 | return -116; |
| 4305 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4306 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4307 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4308 | Out.uNumUsed != 0) { |
| 4309 | return -117; |
| 4310 | } |
| 4311 | |
| 4312 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4313 | return -118; |
| 4314 | } |
| 4315 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 4316 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4317 | puTags[0] != 12 || |
| 4318 | Out.uNumUsed != 1) { |
| 4319 | return -119; |
| 4320 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4321 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4322 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4323 | return -120; |
| 4324 | } |
| 4325 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4326 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 4327 | Item.val.uCount != 1 || |
| 4328 | puTags[0] != 0x17 || |
| 4329 | Out.uNumUsed != 1) { |
| 4330 | return -121; |
| 4331 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4332 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4333 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4334 | return -122; |
| 4335 | } |
| 4336 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4337 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4338 | Item.val.int64 != -3 || |
| 4339 | puTags[0] != 8 || |
| 4340 | Out.uNumUsed != 1) { |
| 4341 | return -123; |
| 4342 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4343 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4344 | if(QCBORDecode_Finish(&DCtx)) { |
| 4345 | return -124; |
| 4346 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4347 | |
| 4348 | UsefulBufC DateString; |
| 4349 | QCBORDecode_Init(&DCtx, |
| 4350 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4351 | QCBOR_DECODE_MODE_NORMAL); |
| 4352 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4353 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4354 | // tagged date string |
| 4355 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4356 | // untagged date string |
| 4357 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4358 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4359 | return 100; |
| 4360 | } |
| 4361 | // untagged byte string |
| 4362 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4363 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4364 | return 101; |
| 4365 | } |
| 4366 | // tagged regex |
| 4367 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4368 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4369 | return 102; |
| 4370 | } |
| 4371 | // tagged date string with a byte string |
| 4372 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4373 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4374 | return 103; |
| 4375 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4376 | // The exit errors out because the last item, the date string with |
| 4377 | // bad content makes the array untraversable (the bad date string |
| 4378 | // could have tag content of an array or such that is not consumed |
| 4379 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4380 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4381 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4382 | return 104; |
| 4383 | } |
| 4384 | |
| 4385 | |
| 4386 | QCBORDecode_Init(&DCtx, |
| 4387 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4388 | QCBOR_DECODE_MODE_NORMAL); |
| 4389 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4390 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4391 | // tagged date string |
| 4392 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4393 | // untagged date string |
| 4394 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4395 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4396 | return 200; |
| 4397 | } |
| 4398 | // untagged byte string |
| 4399 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4400 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4401 | return 201; |
| 4402 | } |
| 4403 | // tagged regex |
| 4404 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4405 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4406 | return 202; |
| 4407 | } |
| 4408 | // tagged date string with a byte string |
| 4409 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4410 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4411 | return 203; |
| 4412 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4413 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4414 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4415 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4416 | return 204; |
| 4417 | } |
| 4418 | |
| 4419 | QCBORDecode_Init(&DCtx, |
| 4420 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4421 | QCBOR_DECODE_MODE_NORMAL); |
| 4422 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4423 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4424 | // tagged date string |
| 4425 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4426 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4427 | return 300; |
| 4428 | } |
| 4429 | // untagged date string |
| 4430 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4431 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4432 | return 301; |
| 4433 | } |
| 4434 | // untagged byte string |
| 4435 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4436 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4437 | return 302; |
| 4438 | } |
| 4439 | // tagged regex |
| 4440 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4441 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4442 | return 303; |
| 4443 | } |
| 4444 | // tagged date string with a byte string |
| 4445 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4446 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4447 | return 304; |
| 4448 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4449 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4450 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4451 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4452 | return 305; |
| 4453 | } |
| 4454 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4455 | return 0; |
| 4456 | } |
| 4457 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4458 | /* |
| 4459 | * These are showing the big numbers converted to integers. |
| 4460 | * The tag numbers are not shown. |
| 4461 | * |
| 4462 | * [ 18446744073709551616, |
| 4463 | * -18446744073709551617, |
| 4464 | * {"BN+": 18446744073709551616, |
| 4465 | * 64: 18446744073709551616, |
| 4466 | * "BN-": -18446744073709551617, |
| 4467 | * -64: -18446744073709551617 |
| 4468 | * } |
| 4469 | * ] |
| 4470 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4471 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4472 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4473 | 0x83, |
| 4474 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4475 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4476 | 0xA4, |
| 4477 | 0x63, 0x42, 0x4E, 0x2B, |
| 4478 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4479 | 0x18, 0x40, |
| 4480 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4481 | 0x63, 0x42, 0x4E, 0x2D, |
| 4482 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4483 | 0x38, 0x3F, |
| 4484 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 4485 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4486 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4487 | /* The expected big num */ |
| 4488 | static const uint8_t spBigNum[] = { |
| 4489 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4490 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4491 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4492 | |
| 4493 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4494 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4495 | { |
| 4496 | QCBORDecodeContext DCtx; |
| 4497 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4498 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4499 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4500 | QCBORDecode_Init(&DCtx, |
| 4501 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 4502 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4503 | |
| 4504 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4505 | // |
| 4506 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4507 | return -1; |
| 4508 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4509 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4510 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4511 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4512 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4513 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4514 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4515 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4516 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4517 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4518 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | // |
| 4522 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4523 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4524 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4525 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4526 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4527 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4528 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4529 | // |
| 4530 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4531 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4532 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4533 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4534 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4535 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4536 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4537 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4538 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4539 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4540 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4541 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4545 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4546 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4547 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4548 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4549 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4550 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4551 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4552 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4553 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4554 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4555 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4556 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4557 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4558 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4559 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4560 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4561 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4562 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4563 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4564 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4565 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4566 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4567 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4568 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4569 | #else |
| 4570 | |
| 4571 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 4572 | return -100; |
| 4573 | } |
| 4574 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4575 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4576 | return 0; |
| 4577 | } |
| 4578 | |
| 4579 | |
| 4580 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4581 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4582 | uint8_t uDataType, |
| 4583 | uint8_t uNestingLevel, |
| 4584 | uint8_t uNextNest, |
| 4585 | int64_t nLabel, |
| 4586 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4587 | { |
| 4588 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4589 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4590 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4591 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 4592 | if(Item.uDataType != uDataType) return -1; |
| 4593 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4594 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 4595 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 4596 | return -1; |
| 4597 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4598 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 4599 | if(Item.label.int64 != nLabel) return -1; |
| 4600 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4601 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4602 | } |
| 4603 | } |
| 4604 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4605 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4606 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4607 | if(pItem) { |
| 4608 | *pItem = Item; |
| 4609 | } |
| 4610 | return 0; |
| 4611 | } |
| 4612 | |
| 4613 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4614 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4615 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4616 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4617 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4618 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4619 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4620 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4621 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4622 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4623 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 4624 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 4625 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 4626 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 4627 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4628 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4629 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 4630 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4631 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4632 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 4633 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4634 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4635 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 4636 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4637 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4638 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4639 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4640 | return 0; |
| 4641 | } |
| 4642 | |
| 4643 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4644 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4645 | { |
| 4646 | -23: { |
| 4647 | -20: { |
| 4648 | -18: "Organization", |
| 4649 | -17: "SSG", |
| 4650 | -15: "Confusion", |
| 4651 | -16: "San Diego", |
| 4652 | -14: "US" |
| 4653 | }, |
| 4654 | -19: { |
| 4655 | -11: { |
| 4656 | -9: -7 |
| 4657 | }, |
| 4658 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 4659 | } |
| 4660 | }, |
| 4661 | -22: { |
| 4662 | -5: -3 |
| 4663 | } |
| 4664 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4665 | */ |
| 4666 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4667 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 4668 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4669 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4670 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4671 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4672 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4673 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 4674 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 4675 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 4676 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4677 | // Same map as above, but using indefinite lengths |
| 4678 | static const uint8_t spCSRInputIndefLen[] = { |
| 4679 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 4680 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4681 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4682 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4683 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4684 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4685 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 4686 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 4687 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 4688 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 4689 | |
| 4690 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4691 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4692 | { |
| 4693 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4694 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4695 | QCBORDecode_Init(&DCtx, |
| 4696 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4697 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4698 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4699 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4700 | } |
| 4701 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4702 | |
| 4703 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4704 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4705 | { |
| 4706 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4707 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4708 | QCBORDecode_Init(&DCtx, |
| 4709 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4710 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4711 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4712 | QCBORItem Item; |
| 4713 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4714 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4715 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4716 | return -1; |
| 4717 | } |
| 4718 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4719 | return -2; |
| 4720 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4721 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4722 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4723 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 4724 | return -3; |
| 4725 | } |
| 4726 | |
| 4727 | return 0; |
| 4728 | } |
| 4729 | |
| 4730 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4731 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4732 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4733 | { |
| 4734 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4735 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4736 | QCBORDecode_Init(&DCtx, |
| 4737 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 4738 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4739 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4740 | return CheckCSRMaps(&DCtx); |
| 4741 | } |
| 4742 | |
| 4743 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4744 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4745 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 4746 | { |
| 4747 | UsefulOutBuf UOB; |
| 4748 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4749 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4750 | int i; |
| 4751 | for(i = 0; i < n; i++) { |
| 4752 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 4753 | } |
| 4754 | |
| 4755 | for(i = 0; i < n; i++) { |
| 4756 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4757 | } |
| 4758 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4759 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4760 | |
| 4761 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4762 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4763 | { |
| 4764 | QCBORDecodeContext DC; |
| 4765 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4766 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4767 | int j; |
| 4768 | for(j = 0; j < nNestLevel; j++) { |
| 4769 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4770 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4771 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 4772 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4773 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4774 | return -4; |
| 4775 | } else { |
| 4776 | return 0; // Decoding doesn't recover after an error |
| 4777 | } |
| 4778 | } else { |
| 4779 | // Should be no error |
| 4780 | if(nReturn) { |
| 4781 | return -9; // Should not have got an error |
| 4782 | } |
| 4783 | } |
| 4784 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4785 | return -7; |
| 4786 | } |
| 4787 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4788 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4789 | if(nReturn) { |
| 4790 | return -3; |
| 4791 | } |
| 4792 | return 0; |
| 4793 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4794 | |
| 4795 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4796 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4797 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4798 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4799 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4800 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4801 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4802 | int nReturn = parse_indeflen_nested(Nested, i); |
| 4803 | if(nReturn) { |
| 4804 | return nReturn; |
| 4805 | } |
| 4806 | } |
| 4807 | return 0; |
| 4808 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4809 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4810 | // [1, [2, 3]] |
| 4811 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 4812 | // No closing break |
| 4813 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 4814 | // Not enough closing breaks |
| 4815 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 4816 | // Too many closing breaks |
| 4817 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 4818 | // Unclosed indeflen inside def len |
| 4819 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 4820 | // confused tag |
| 4821 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4822 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4823 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4824 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4825 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4826 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4827 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4828 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4829 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4830 | QCBORDecodeContext DC; |
| 4831 | QCBORItem Item; |
| 4832 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4833 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4834 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4835 | |
| 4836 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4837 | Item.uNestingLevel != 0 || |
| 4838 | Item.uNextNestLevel != 1) { |
| 4839 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4840 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4841 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4842 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4843 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 4844 | Item.uNestingLevel != 1 || |
| 4845 | Item.uNextNestLevel != 1) { |
| 4846 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4847 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4848 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4849 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4850 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4851 | Item.uNestingLevel != 1 || |
| 4852 | Item.uNextNestLevel != 2) { |
| 4853 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4854 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4855 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4856 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4857 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4858 | Item.uNestingLevel != 2 || |
| 4859 | Item.uNextNestLevel != 2) { |
| 4860 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4861 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4862 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4863 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4864 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4865 | Item.uNestingLevel != 2 || |
| 4866 | Item.uNextNestLevel != 0) { |
| 4867 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4868 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4869 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4870 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4871 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4872 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4873 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4874 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4875 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4876 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4877 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4878 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4879 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4880 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4881 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4882 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4883 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4884 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4885 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4886 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4887 | } |
| 4888 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4889 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4890 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4891 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4892 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4893 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4894 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4895 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4896 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4897 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4901 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4902 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4903 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4904 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4905 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4906 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4907 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4908 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4909 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4910 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4911 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4912 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4913 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4914 | |
| 4915 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4916 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4917 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4918 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4919 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4920 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4921 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4922 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4923 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4924 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4925 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4926 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4927 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4928 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4929 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4930 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4931 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4932 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4933 | return -140; |
| 4934 | } |
| 4935 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4936 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4937 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4938 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4939 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4940 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4941 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4942 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4943 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4944 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4945 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4946 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4947 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4948 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4949 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4950 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4951 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4952 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4953 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4954 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4955 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4956 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4957 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4958 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4959 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4960 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4961 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4962 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4963 | |
| 4964 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4965 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4966 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4967 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4968 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4969 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4970 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4971 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4972 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4973 | #else /* QCBOR_DISABLE_TAGS */ |
| 4974 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 4975 | return -20; |
| 4976 | } |
| 4977 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4978 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4979 | return 0; |
| 4980 | } |
| 4981 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4982 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4983 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 4984 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4985 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4986 | 0x81, // Array of length one |
| 4987 | 0x7f, // text string marked with indefinite length |
| 4988 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4989 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4990 | 0xff // ending break |
| 4991 | }; |
| 4992 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4993 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4994 | 0x81, // Array of length one |
| 4995 | 0x7f, // text string marked with indefinite length |
| 4996 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4997 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 4998 | 0xff // ending break |
| 4999 | }; |
| 5000 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5001 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5002 | 0x81, // Array of length one |
| 5003 | 0x7f, // text string marked with indefinite length |
| 5004 | 0x01, 0x02, // Not a string |
| 5005 | 0xff // ending break |
| 5006 | }; |
| 5007 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5008 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5009 | 0x81, // Array of length one |
| 5010 | 0x7f, // text string marked with indefinite length |
| 5011 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5012 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5013 | // missing end of string |
| 5014 | }; |
| 5015 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5016 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5017 | 0xa1, // Array of length one |
| 5018 | 0x7f, // text string marked with indefinite length |
| 5019 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 5020 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5021 | 0xff, // ending break |
| 5022 | 0x01 // integer being labeled. |
| 5023 | }; |
| 5024 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5025 | /** |
| 5026 | Make an indefinite length string |
| 5027 | |
| 5028 | @param Storage Storage for string, must be 144 bytes in size |
| 5029 | @return The indefinite length string |
| 5030 | |
| 5031 | This makes an array with one indefinite length string that has 7 chunks |
| 5032 | from size of 1 byte up to 64 bytes. |
| 5033 | */ |
| 5034 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5035 | { |
| 5036 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5037 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5038 | UsefulOutBuf_Init(&UOB, Storage); |
| 5039 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 5040 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5041 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5042 | uint8_t uStringByte = 0; |
| 5043 | // Use of type int is intentional |
| 5044 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 5045 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5046 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5047 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 5048 | for(int j = 0; j < uChunkSize; j++) { |
| 5049 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 5050 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5051 | } |
| 5052 | } |
| 5053 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 5054 | |
| 5055 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5056 | } |
| 5057 | |
| 5058 | static int CheckBigString(UsefulBufC BigString) |
| 5059 | { |
| 5060 | if(BigString.len != 255) { |
| 5061 | return 1; |
| 5062 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5063 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5064 | for(uint8_t i = 0; i < 255; i++){ |
| 5065 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 5066 | return 1; |
| 5067 | } |
| 5068 | } |
| 5069 | return 0; |
| 5070 | } |
| 5071 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5072 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5073 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5074 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5075 | QCBORDecodeContext DC; |
| 5076 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5077 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5078 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5079 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5080 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5081 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5082 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5083 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5084 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5085 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5086 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5087 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5088 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5089 | return -2; |
| 5090 | } |
| 5091 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 5092 | return -3; |
| 5093 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5094 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5095 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5096 | return -4; |
| 5097 | } |
| 5098 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 5099 | return -5; |
| 5100 | } |
| 5101 | if(QCBORDecode_Finish(&DC)) { |
| 5102 | return -6; |
| 5103 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5104 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5105 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5106 | QCBORDecode_Init(&DC, |
| 5107 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 5108 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5109 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5110 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5111 | return -7; |
| 5112 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5113 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5114 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5115 | return -8; |
| 5116 | } |
| 5117 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5118 | return -9; |
| 5119 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5120 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5121 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5122 | return -10; |
| 5123 | } |
| 5124 | |
| 5125 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5126 | QCBORDecode_Init(&DC, |
| 5127 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 5128 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5129 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5130 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5131 | return -11; |
| 5132 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5133 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5134 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5135 | return -12; |
| 5136 | } |
| 5137 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5138 | return -13; |
| 5139 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5140 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5141 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5142 | return -14; |
| 5143 | } |
| 5144 | |
| 5145 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5146 | QCBORDecode_Init(&DC, |
| 5147 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 5148 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5149 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5150 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5151 | return -15; |
| 5152 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5153 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5154 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5155 | return -16; |
| 5156 | } |
| 5157 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5158 | return -17; |
| 5159 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5160 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5161 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 5162 | return -18; |
| 5163 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5164 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5165 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5166 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5167 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5168 | QCBORDecode_GetNext(&DC, &Item); |
| 5169 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5170 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5171 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5172 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5173 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5174 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5175 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5176 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5177 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5178 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5179 | |
| 5180 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 5181 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5182 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5183 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5184 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5185 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 5186 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 5187 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5188 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5189 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 5190 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5191 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5192 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5193 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5194 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5195 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5196 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5197 | QCBORDecode_GetNext(&DC, &Item); |
| 5198 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5199 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5200 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5201 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5202 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5203 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5204 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5205 | // ---- big bstr ----- |
| 5206 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5207 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5208 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5209 | return -25; |
| 5210 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5211 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5212 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5213 | return -26; |
| 5214 | } |
| 5215 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5216 | return -26; |
| 5217 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5218 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5219 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5220 | return -27; |
| 5221 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5222 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5223 | return -28; |
| 5224 | } |
| 5225 | if(CheckBigString(Item.val.string)) { |
| 5226 | return -3; |
| 5227 | } |
| 5228 | if(QCBORDecode_Finish(&DC)) { |
| 5229 | return -29; |
| 5230 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5231 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5232 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5233 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5234 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5235 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5236 | return -30; |
| 5237 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5238 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5239 | QCBORDecode_GetNext(&DC, &Item); |
| 5240 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 5241 | return -31; |
| 5242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5243 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5244 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 5245 | return -32; |
| 5246 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5247 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5248 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5249 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 5250 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 5251 | return -33; |
| 5252 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5253 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5254 | if(QCBORDecode_Finish(&DC)) { |
| 5255 | return -34; |
| 5256 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5257 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5258 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5262 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5263 | { |
| 5264 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5265 | QCBORError nCBORError; |
| 5266 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5267 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5268 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5269 | QCBORDecode_Init(&DC, |
| 5270 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 5271 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5272 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5273 | UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5274 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5275 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 5276 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5277 | return -1; |
| 5278 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5279 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5280 | if(CheckCSRMaps(&DC)) { |
| 5281 | return -2; |
| 5282 | } |
| 5283 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5284 | // Next parse, save pointers to a few strings, destroy original and |
| 5285 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5286 | UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 5287 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5288 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5289 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 5290 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5291 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5292 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5293 | QCBORItem Item1, Item2, Item3, Item4; |
| 5294 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5295 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5296 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5297 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5298 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5299 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5300 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5301 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5302 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5303 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5304 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5305 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5306 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5307 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 5308 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5309 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5310 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5311 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 5312 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5313 | Item1.uDataAlloc != 0 || |
| 5314 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5315 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5316 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5317 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5318 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5319 | |
| 5320 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5321 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5322 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5323 | Item2.uDataAlloc != 0 || |
| 5324 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5325 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5326 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5327 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5328 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5329 | Item3.uDataAlloc == 0 || |
| 5330 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5331 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5332 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5333 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5334 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5335 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5336 | Item4.uDataAlloc == 0 || |
| 5337 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5338 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5339 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5340 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5341 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5342 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5343 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5344 | QCBORDecode_Init(&DC, |
| 5345 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 5346 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5347 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 5348 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5349 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5350 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5351 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5352 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5353 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5354 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 5355 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 5356 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 5357 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 5358 | } |
| 5359 | } |
| 5360 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5361 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5362 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | return 0; |
| 5366 | } |
| 5367 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5368 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5369 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 5370 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5371 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5372 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5373 | QCBORDecodeContext DC; |
| 5374 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 5375 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5376 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5377 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5378 | // Then fish into the internals of the decode context |
| 5379 | // to get the allocator function so it can be called directly. |
| 5380 | // Also figure out how much pool is available for use |
| 5381 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5382 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5383 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 5384 | if(nError) { |
| 5385 | return -9; |
| 5386 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5387 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 5388 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5389 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5390 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5391 | // First test -- ask for one more byte than available and see failure |
| 5392 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5393 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 5394 | return -1; |
| 5395 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5396 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5397 | // Re do the set up for the next test that will do a successful alloc, |
| 5398 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5399 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5400 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5401 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5402 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5403 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5404 | // Allocate one byte less than available and see success |
| 5405 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5406 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5407 | return -2; |
| 5408 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5409 | // Ask for some more and see failure |
| 5410 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5411 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 5412 | return -3; |
| 5413 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5414 | // Free the first allocate, retry the second and see success |
| 5415 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 5416 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5417 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 5418 | return -4; |
| 5419 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5420 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5421 | // Re do set up for next test that involves a successful alloc, |
| 5422 | // and a successful realloc and a failed realloc |
| 5423 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5424 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5425 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5426 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5427 | // Allocate half the pool and see success |
| 5428 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5429 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5430 | return -5; |
| 5431 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5432 | // Reallocate to take up the whole pool and see success |
| 5433 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5434 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 5435 | return -6; |
| 5436 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5437 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5438 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 5439 | return -7; |
| 5440 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5441 | // Try to allocate more to be sure there is failure after a realloc |
| 5442 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 5443 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5444 | return -8; |
| 5445 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5446 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5447 | return 0; |
| 5448 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 5449 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5450 | |
| 5451 | /* Just enough of an allocator to test configuration of one */ |
| 5452 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 5453 | { |
| 5454 | (void)pOldMem; // unused variable |
| 5455 | |
| 5456 | if(uNewSize) { |
| 5457 | // Assumes the context pointer is the buffer and |
| 5458 | // nothing too big will ever be asked for. |
| 5459 | // This is only good for this basic test! |
| 5460 | return (UsefulBuf) {pCtx, uNewSize}; |
| 5461 | } else { |
| 5462 | return NULLUsefulBuf; |
| 5463 | } |
| 5464 | } |
| 5465 | |
| 5466 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5467 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5468 | { |
| 5469 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5470 | // nothing can be done with it unless that is set up. |
| 5471 | QCBORDecodeContext DC; |
| 5472 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 5473 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 5474 | |
| 5475 | uint8_t pAllocatorBuffer[50]; |
| 5476 | |
| 5477 | // This is really just to test that this call works. |
| 5478 | // The full functionality of string allocators is tested |
| 5479 | // elsewhere with the MemPool internal allocator. |
| 5480 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 5481 | |
| 5482 | QCBORItem Item; |
| 5483 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 5484 | return -1; |
| 5485 | } |
| 5486 | |
| 5487 | if(Item.uDataAlloc == 0 || |
| 5488 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 5489 | Item.val.string.ptr != pAllocatorBuffer) { |
| 5490 | return -2; |
| 5491 | } |
| 5492 | |
| 5493 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 5494 | return -3; |
| 5495 | } |
| 5496 | |
| 5497 | return 0; |
| 5498 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5499 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5500 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5501 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5502 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5503 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5504 | struct EaMTest { |
| 5505 | const char *szName; |
| 5506 | UsefulBufC Input; |
| 5507 | uint8_t uTagRequirement; |
| 5508 | bool bHasTags; |
| 5509 | |
| 5510 | /* Expected values for GetNext */ |
| 5511 | QCBORError uExpectedErrorGN; |
| 5512 | uint8_t uQCBORTypeGN; |
| 5513 | int64_t nExponentGN; |
| 5514 | int64_t nMantissaGN; |
| 5515 | UsefulBufC MantissaGN; |
| 5516 | |
| 5517 | /* Expected values for GetDecimalFraction */ |
| 5518 | QCBORError uExpectedErrorGDF; |
| 5519 | int64_t nExponentGDF; |
| 5520 | int64_t nMantissaGDF; |
| 5521 | |
| 5522 | /* Expected values for GetDecimalFractionBig */ |
| 5523 | QCBORError uExpectedErrorGDFB; |
| 5524 | int64_t nExponentGDFB; |
| 5525 | UsefulBufC MantissaGDFB; |
| 5526 | bool IsNegativeGDFB; |
| 5527 | |
| 5528 | /* Expected values for GetBigFloat */ |
| 5529 | QCBORError uExpectedErrorGBF; |
| 5530 | int64_t nExponentGBF; |
| 5531 | int64_t nMantissaGBF; |
| 5532 | |
| 5533 | /* Expected values for GetBigFloatBig */ |
| 5534 | QCBORError uExpectedErrorGBFB; |
| 5535 | int64_t nExponentGBFB; |
| 5536 | UsefulBufC MantissaGBFB; |
| 5537 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5538 | }; |
| 5539 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5540 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5541 | |
| 5542 | static const struct EaMTest pEaMTests[] = { |
| 5543 | { |
| 5544 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 5545 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5546 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 5547 | false, |
| 5548 | |
| 5549 | QCBOR_SUCCESS, /* for GetNext */ |
| 5550 | QCBOR_TYPE_ARRAY, |
| 5551 | 0, |
| 5552 | 0, |
| 5553 | {(const uint8_t []){0x00}, 1}, |
| 5554 | |
| 5555 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 5556 | -1, |
| 5557 | 3, |
| 5558 | |
| 5559 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5560 | -1, |
| 5561 | {(const uint8_t []){0x03}, 1}, |
| 5562 | false, |
| 5563 | |
| 5564 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5565 | -1, |
| 5566 | 3, |
| 5567 | |
| 5568 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5569 | -1, |
| 5570 | {(const uint8_t []){0x03}, 1}, |
| 5571 | false |
| 5572 | }, |
| 5573 | |
| 5574 | { |
| 5575 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 5576 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5577 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5578 | false, |
| 5579 | |
| 5580 | QCBOR_SUCCESS, /* for GetNext */ |
| 5581 | QCBOR_TYPE_ARRAY, |
| 5582 | 0, |
| 5583 | 0, |
| 5584 | {(const uint8_t []){0x00}, 1}, |
| 5585 | |
| 5586 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5587 | 0, |
| 5588 | 0, |
| 5589 | |
| 5590 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5591 | 0, |
| 5592 | {(const uint8_t []){0x00}, 1}, |
| 5593 | false, |
| 5594 | |
| 5595 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5596 | 0, |
| 5597 | 0, |
| 5598 | |
| 5599 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5600 | 0, |
| 5601 | {(const uint8_t []){0x00}, 1}, |
| 5602 | false |
| 5603 | |
| 5604 | }, |
| 5605 | |
| 5606 | { |
| 5607 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 5608 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 5609 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5610 | true, |
| 5611 | |
| 5612 | QCBOR_SUCCESS, /* for GetNext */ |
| 5613 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 5614 | -1, |
| 5615 | 3, |
| 5616 | {(const uint8_t []){0x00}, 1}, |
| 5617 | |
| 5618 | |
| 5619 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 5620 | -1, |
| 5621 | 3, |
| 5622 | |
| 5623 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5624 | -1, |
| 5625 | {(const uint8_t []){0x03}, 1}, |
| 5626 | false, |
| 5627 | |
| 5628 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5629 | 0, |
| 5630 | 0, |
| 5631 | |
| 5632 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5633 | 0, |
| 5634 | {(const uint8_t []){0x00}, 1}, |
| 5635 | false |
| 5636 | }, |
| 5637 | { |
| 5638 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 5639 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 5640 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5641 | true, |
| 5642 | |
| 5643 | QCBOR_SUCCESS, /* for GetNext */ |
| 5644 | QCBOR_TYPE_BIGFLOAT, |
| 5645 | 300, |
| 5646 | 100, |
| 5647 | {(const uint8_t []){0x00}, 1}, |
| 5648 | |
| 5649 | |
| 5650 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5651 | 0, |
| 5652 | 0, |
| 5653 | |
| 5654 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5655 | 0, |
| 5656 | {(const uint8_t []){0x03}, 1}, |
| 5657 | false, |
| 5658 | |
| 5659 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5660 | 300, |
| 5661 | 100, |
| 5662 | |
| 5663 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5664 | 300, |
| 5665 | {(const uint8_t []){0x64}, 1}, |
| 5666 | false |
| 5667 | }, |
| 5668 | |
| 5669 | { |
| 5670 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 5671 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5672 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 5673 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5674 | true, |
| 5675 | |
| 5676 | QCBOR_SUCCESS, /* for GetNext */ |
| 5677 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5678 | -20, |
| 5679 | 0, |
| 5680 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5681 | |
| 5682 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 5683 | 0, |
| 5684 | 0, |
| 5685 | |
| 5686 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5687 | -20, |
| 5688 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5689 | false, |
| 5690 | |
| 5691 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5692 | 0, |
| 5693 | 0, |
| 5694 | |
| 5695 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5696 | 0, |
| 5697 | {(const uint8_t []){0x00}, 0}, |
| 5698 | false |
| 5699 | }, |
| 5700 | |
| 5701 | { |
| 5702 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 5703 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5704 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 5705 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5706 | true, |
| 5707 | |
| 5708 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 5709 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5710 | 0, |
| 5711 | 0, |
| 5712 | {(const uint8_t []){0x00}, 0}, |
| 5713 | |
| 5714 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 5715 | 0, |
| 5716 | 0, |
| 5717 | |
| 5718 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 5719 | 0, |
| 5720 | {(const uint8_t []){0x00}, 0}, |
| 5721 | false, |
| 5722 | |
| 5723 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 5724 | 0, |
| 5725 | 0, |
| 5726 | |
| 5727 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 5728 | 0, |
| 5729 | {(const uint8_t []){0x00}, 0}, |
| 5730 | false |
| 5731 | }, |
| 5732 | { |
| 5733 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 5734 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 5735 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5736 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5737 | true, |
| 5738 | |
| 5739 | QCBOR_SUCCESS, /* for GetNext */ |
| 5740 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5741 | -20, |
| 5742 | 0, |
| 5743 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5744 | |
| 5745 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5746 | 0, |
| 5747 | 0, |
| 5748 | |
| 5749 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5750 | -20, |
| 5751 | {(const uint8_t []){0x00}, 1}, |
| 5752 | false, |
| 5753 | |
| 5754 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5755 | -20, |
| 5756 | 4294967295, |
| 5757 | |
| 5758 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5759 | -20, |
| 5760 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5761 | false |
| 5762 | }, |
| 5763 | |
| 5764 | { |
| 5765 | /* Special case for test 8. Don't renumber it. */ |
| 5766 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 5767 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 5768 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5769 | true, |
| 5770 | |
| 5771 | QCBOR_SUCCESS, /* for GetNext */ |
| 5772 | QCBOR_TYPE_ARRAY, |
| 5773 | 0, |
| 5774 | 0, |
| 5775 | {(const uint8_t []){0x00}, 1}, |
| 5776 | |
| 5777 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5778 | 0, |
| 5779 | 0, |
| 5780 | |
| 5781 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5782 | -20, |
| 5783 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5784 | false, |
| 5785 | |
| 5786 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 5787 | 0, |
| 5788 | 0, |
| 5789 | |
| 5790 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5791 | -20, |
| 5792 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5793 | false |
| 5794 | }, |
| 5795 | |
| 5796 | { |
| 5797 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 5798 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5799 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 5800 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5801 | true, |
| 5802 | |
| 5803 | QCBOR_SUCCESS, /* for GetNext */ |
| 5804 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 5805 | 9223372036854775807, |
| 5806 | 0, |
| 5807 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5808 | |
| 5809 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5810 | 0, |
| 5811 | 0, |
| 5812 | |
| 5813 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5814 | 9223372036854775807, |
| 5815 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5816 | true, |
| 5817 | |
| 5818 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5819 | 0, |
| 5820 | 0, |
| 5821 | |
| 5822 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5823 | 0, |
| 5824 | {(const uint8_t []){0x00}, 1}, |
| 5825 | false |
| 5826 | }, |
| 5827 | }; |
| 5828 | |
| 5829 | |
| 5830 | |
| 5831 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5832 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5833 | size_t uIndex; |
| 5834 | QCBORDecodeContext DCtx; |
| 5835 | QCBORItem Item; |
| 5836 | QCBORError uError; |
| 5837 | int64_t nMantissa, nExponent; |
| 5838 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 5839 | UsefulBufC Mantissa; |
| 5840 | bool bMantissaIsNegative; |
| 5841 | |
| 5842 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 5843 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 5844 | /* Decode with GetNext */ |
| 5845 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5846 | |
| 5847 | if(uIndex + 1 == 9) { |
| 5848 | nExponent = 99; // just to set a break point |
| 5849 | } |
| 5850 | |
| 5851 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 5852 | #ifdef QCBOR_DISABLE_TAGS |
| 5853 | /* Test 8 is a special case when tags are disabled */ |
| 5854 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 5855 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5856 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 5857 | } |
| 5858 | } else { |
| 5859 | #endif |
| 5860 | /* Now check return code, data type, mantissa and exponent */ |
| 5861 | if(pT->uExpectedErrorGN != uError) { |
| 5862 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 5863 | } |
| 5864 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 5865 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 5866 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 5867 | } |
| 5868 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 5869 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 5870 | } |
| 5871 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 5872 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 5873 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 5874 | } |
| 5875 | } else { |
| 5876 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 5877 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 5878 | } |
| 5879 | } |
| 5880 | } |
| 5881 | #ifdef QCBOR_DISABLE_TAGS |
| 5882 | } |
| 5883 | #endif |
| 5884 | |
| 5885 | /* Decode with GetDecimalFraction */ |
| 5886 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5887 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 5888 | pT->uTagRequirement, |
| 5889 | &nMantissa, |
| 5890 | &nExponent); |
| 5891 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5892 | #ifdef QCBOR_DISABLE_TAGS |
| 5893 | if(pT->bHasTags) { |
| 5894 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5895 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 5896 | } |
| 5897 | } else { |
| 5898 | #endif |
| 5899 | /* Now check return code, mantissa and exponent */ |
| 5900 | if(pT->uExpectedErrorGDF != uError) { |
| 5901 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 5902 | } |
| 5903 | if(uError == QCBOR_SUCCESS) { |
| 5904 | if(pT->nExponentGDF != nExponent) { |
| 5905 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 5906 | } |
| 5907 | if(pT->nMantissaGDF != nMantissa) { |
| 5908 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 5909 | } |
| 5910 | } |
| 5911 | #ifdef QCBOR_DISABLE_TAGS |
| 5912 | } |
| 5913 | #endif |
| 5914 | |
| 5915 | /* Decode with GetDecimalFractionBig */ |
| 5916 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5917 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 5918 | pT->uTagRequirement, |
| 5919 | MantissaBuf, |
| 5920 | &Mantissa, |
| 5921 | &bMantissaIsNegative, |
| 5922 | &nExponent); |
| 5923 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5924 | #ifdef QCBOR_DISABLE_TAGS |
| 5925 | if(pT->bHasTags) { |
| 5926 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5927 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 5928 | } |
| 5929 | } else { |
| 5930 | #endif |
| 5931 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5932 | if(pT->uExpectedErrorGDFB != uError) { |
| 5933 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 5934 | } |
| 5935 | if(uError == QCBOR_SUCCESS) { |
| 5936 | if(pT->nExponentGDFB != nExponent) { |
| 5937 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 5938 | } |
| 5939 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 5940 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 5941 | } |
| 5942 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 5943 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 5944 | } |
| 5945 | } |
| 5946 | #ifdef QCBOR_DISABLE_TAGS |
| 5947 | } |
| 5948 | #endif |
| 5949 | |
| 5950 | /* Decode with GetBigFloat */ |
| 5951 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5952 | QCBORDecode_GetBigFloat(&DCtx, |
| 5953 | pT->uTagRequirement, |
| 5954 | &nMantissa, |
| 5955 | &nExponent); |
| 5956 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5957 | #ifdef QCBOR_DISABLE_TAGS |
| 5958 | if(pT->bHasTags) { |
| 5959 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5960 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 5961 | } |
| 5962 | } else { |
| 5963 | #endif |
| 5964 | /* Now check return code, mantissa and exponent */ |
| 5965 | if(pT->uExpectedErrorGBF != uError) { |
| 5966 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 5967 | } |
| 5968 | if(uError == QCBOR_SUCCESS) { |
| 5969 | if(pT->nExponentGBF != nExponent) { |
| 5970 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 5971 | } |
| 5972 | if(pT->nMantissaGBF != nMantissa) { |
| 5973 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 5974 | } |
| 5975 | } |
| 5976 | #ifdef QCBOR_DISABLE_TAGS |
| 5977 | } |
| 5978 | #endif |
| 5979 | |
| 5980 | /* Decode with GetBigFloatBig */ |
| 5981 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5982 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 5983 | pT->uTagRequirement, |
| 5984 | MantissaBuf, |
| 5985 | &Mantissa, |
| 5986 | &bMantissaIsNegative, |
| 5987 | &nExponent); |
| 5988 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5989 | #ifdef QCBOR_DISABLE_TAGS |
| 5990 | if(pT->bHasTags) { |
| 5991 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5992 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 5993 | } |
| 5994 | } else { |
| 5995 | #endif |
| 5996 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5997 | if(pT->uExpectedErrorGBFB != uError) { |
| 5998 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 5999 | } |
| 6000 | if(uError == QCBOR_SUCCESS) { |
| 6001 | if(pT->nExponentGBFB != nExponent) { |
| 6002 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 6003 | } |
| 6004 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 6005 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 6006 | } |
| 6007 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 6008 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 6009 | } |
| 6010 | } |
| 6011 | #ifdef QCBOR_DISABLE_TAGS |
| 6012 | } |
| 6013 | #endif |
| 6014 | } |
| 6015 | |
| 6016 | return 0; |
| 6017 | } |
| 6018 | |
| 6019 | |
| 6020 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 6021 | { |
| 6022 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6023 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6024 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6025 | QCBORItem item; |
| 6026 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 6027 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 6028 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 6029 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6030 | |
| 6031 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6032 | |
| 6033 | /* Now encode some stuff and then decode it */ |
| 6034 | uint8_t pBuf[40]; |
| 6035 | QCBOREncodeContext EC; |
| 6036 | UsefulBufC Encoded; |
| 6037 | |
| 6038 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 6039 | QCBOREncode_OpenArray(&EC); |
| 6040 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 6041 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 6042 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 6043 | QCBOREncode_CloseArray(&EC); |
| 6044 | QCBOREncode_Finish(&EC, &Encoded); |
| 6045 | |
| 6046 | |
| 6047 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6048 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6049 | if(uErr != QCBOR_SUCCESS) { |
| 6050 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6051 | } |
| 6052 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6053 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6054 | if(uErr != QCBOR_SUCCESS) { |
| 6055 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6056 | } |
| 6057 | |
| 6058 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 6059 | item.val.expAndMantissa.nExponent != 1000 || |
| 6060 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6061 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6062 | } |
| 6063 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6064 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6065 | if(uErr != QCBOR_SUCCESS) { |
| 6066 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 6070 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 6071 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6072 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6073 | } |
| 6074 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6075 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6076 | if(uErr != QCBOR_SUCCESS) { |
| 6077 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 6081 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 6082 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6083 | return 106; |
| 6084 | } |
| 6085 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6086 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6087 | |
| 6088 | return 0; |
| 6089 | } |
| 6090 | |
| 6091 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6092 | int32_t ExponentAndMantissaDecodeTests(void) |
| 6093 | { |
| 6094 | int32_t rv = ProcessEaMTests(); |
| 6095 | if(rv) { |
| 6096 | return rv; |
| 6097 | } |
| 6098 | |
| 6099 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 6100 | } |
| 6101 | |
| 6102 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6103 | static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = { |
| 6104 | { "Exponent > INT64_MAX", |
| 6105 | QCBOR_DECODE_MODE_NORMAL, |
| 6106 | {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20}, |
| 6107 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6108 | }, |
| 6109 | { "Mantissa > INT64_MAX", |
| 6110 | QCBOR_DECODE_MODE_NORMAL, |
| 6111 | {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23}, |
| 6112 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6113 | }, |
| 6114 | { |
| 6115 | "End of input", |
| 6116 | QCBOR_DECODE_MODE_NORMAL, |
| 6117 | {"\xC4\x82", 2}, |
| 6118 | QCBOR_ERR_NO_MORE_ITEMS |
| 6119 | }, |
| 6120 | {"bad content for big num", |
| 6121 | QCBOR_DECODE_MODE_NORMAL, |
| 6122 | {"\xC4\x82\x01\xc3\x01", 5}, |
| 6123 | QCBOR_ERR_BAD_OPT_TAG |
| 6124 | }, |
| 6125 | {"bad content for big num", |
| 6126 | QCBOR_DECODE_MODE_NORMAL, |
| 6127 | {"\xC4\x82\xc2\x01\x1f", 5}, |
| 6128 | QCBOR_ERR_BAD_INT |
| 6129 | }, |
| 6130 | {"Bad integer for exponent", |
| 6131 | QCBOR_DECODE_MODE_NORMAL, |
| 6132 | {"\xC4\x82\x01\x1f", 4}, |
| 6133 | QCBOR_ERR_BAD_INT |
| 6134 | }, |
| 6135 | {"Bad integer for mantissa", |
| 6136 | QCBOR_DECODE_MODE_NORMAL, |
| 6137 | {"\xC4\x82\x1f\x01", 4}, |
| 6138 | QCBOR_ERR_BAD_INT |
| 6139 | }, |
| 6140 | {"3 items in array", |
| 6141 | QCBOR_DECODE_MODE_NORMAL, |
| 6142 | {"\xC4\x83\x03\x01\x02", 5}, |
| 6143 | QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6144 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6145 | {"unterminated indefinite length array", |
| 6146 | QCBOR_DECODE_MODE_NORMAL, |
| 6147 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6148 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6149 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6150 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6151 | {"unterminated indefinite length array", |
| 6152 | QCBOR_DECODE_MODE_NORMAL, |
| 6153 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6154 | QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 6155 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6156 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6157 | {"Empty array", |
| 6158 | QCBOR_DECODE_MODE_NORMAL, |
| 6159 | {"\xC4\x80", 2}, |
| 6160 | QCBOR_ERR_NO_MORE_ITEMS |
| 6161 | }, |
| 6162 | {"Second is not an integer", |
| 6163 | QCBOR_DECODE_MODE_NORMAL, |
| 6164 | {"\xC4\x82\x03\x40", 4}, |
| 6165 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6166 | }, |
| 6167 | {"First is not an integer", |
| 6168 | QCBOR_DECODE_MODE_NORMAL, |
| 6169 | {"\xC4\x82\x40", 3}, |
| 6170 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6171 | }, |
| 6172 | {"Not an array", |
| 6173 | QCBOR_DECODE_MODE_NORMAL, |
| 6174 | {"\xC4\xA2", 2}, |
| 6175 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6176 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6177 | }; |
| 6178 | |
| 6179 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6180 | int32_t |
| 6181 | ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6182 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6183 | return ProcessDecodeFailures(ExponentAndMantissaFailures, |
| 6184 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 6185 | struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6186 | } |
| 6187 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6188 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6189 | |
| 6190 | |
| 6191 | |
| 6192 | /* |
| 6193 | Some basic CBOR with map and array used in a lot of tests. |
| 6194 | The map labels are all strings |
| 6195 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6196 | { |
| 6197 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6198 | "an array of two strings": [ |
| 6199 | "string1", "string2" |
| 6200 | ], |
| 6201 | "map in a map": { |
| 6202 | "bytes 1": h'78787878', |
| 6203 | "bytes 2": h'79797979', |
| 6204 | "another int": 98, |
| 6205 | "text 2": "lies, damn lies and statistics" |
| 6206 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6207 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6208 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6209 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6210 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6211 | { |
| 6212 | QCBORItem Item1, Item2, Item3; |
| 6213 | int64_t nDecodedInt1, nDecodedInt2; |
| 6214 | UsefulBufC B1, B2, S1, S2, S3; |
| 6215 | |
| 6216 | QCBORDecodeContext DCtx; |
| 6217 | QCBORError nCBORError; |
| 6218 | |
| 6219 | QCBORDecode_Init(&DCtx, input, 0); |
| 6220 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6221 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6222 | |
| 6223 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 6224 | |
| 6225 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6226 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6227 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 6228 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 6229 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6230 | QCBORDecode_ExitMap(&DCtx); |
| 6231 | |
| 6232 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6233 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6234 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 6235 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6236 | return -400; |
| 6237 | } |
| 6238 | QCBORDecode_ExitArray(&DCtx); |
| 6239 | |
| 6240 | // Parse the same array again using GetText() instead of GetItem() |
| 6241 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6242 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 6243 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6244 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6245 | return 5000; |
| 6246 | } |
| 6247 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 6248 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6249 | return 5001; |
| 6250 | } */ |
| 6251 | |
| 6252 | QCBORDecode_ExitArray(&DCtx); |
| 6253 | |
| 6254 | QCBORDecode_ExitMap(&DCtx); |
| 6255 | |
| 6256 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 6257 | |
| 6258 | if(nCBORError) { |
| 6259 | return (int32_t)nCBORError; |
| 6260 | } |
| 6261 | |
| 6262 | if(nDecodedInt1 != 42) { |
| 6263 | return 1001; |
| 6264 | } |
| 6265 | |
| 6266 | if(nDecodedInt2 != 98) { |
| 6267 | return 1002; |
| 6268 | } |
| 6269 | |
| 6270 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6271 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6272 | return 1003; |
| 6273 | } |
| 6274 | |
| 6275 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6276 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6277 | return 1004; |
| 6278 | } |
| 6279 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6280 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6281 | return 1005; |
| 6282 | } |
| 6283 | |
| 6284 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 6285 | return 1006; |
| 6286 | } |
| 6287 | |
| 6288 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 6289 | return 1007; |
| 6290 | } |
| 6291 | |
| 6292 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 6293 | return 1008; |
| 6294 | } |
| 6295 | |
| 6296 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 6297 | return 1009; |
| 6298 | } |
| 6299 | |
| 6300 | return 0; |
| 6301 | } |
| 6302 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6303 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6304 | { |
| 6305 | -75008: h'05083399', |
| 6306 | 88: [], |
| 6307 | 100100: { |
| 6308 | "sub1": { |
| 6309 | 10: [ |
| 6310 | 0 |
| 6311 | ], |
| 6312 | -75009: h'A46823990001', |
| 6313 | 100100: { |
| 6314 | "json": "{ \"ueid\", \"xyz\"}", |
| 6315 | "subsub": { |
| 6316 | 100002: h'141813191001' |
| 6317 | } |
| 6318 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6319 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6320 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6321 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6322 | */ |
| 6323 | |
| 6324 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6325 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 6326 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 6327 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 6328 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 6329 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 6330 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 6331 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 6332 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 6333 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 6334 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 6335 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 6336 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6337 | }; |
| 6338 | |
| 6339 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 6340 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 6341 | { |
| 6342 | UsefulBufC String; |
| 6343 | |
| 6344 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 6345 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 6346 | |
| 6347 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6348 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 6349 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6350 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 6351 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 6352 | if(QCBORDecode_GetError(pDCtx)) { |
| 6353 | return 4001; |
| 6354 | } |
| 6355 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 6356 | return 4002; |
| 6357 | } |
| 6358 | QCBORDecode_ExitMap(pDCtx); |
| 6359 | QCBORDecode_ExitMap(pDCtx); |
| 6360 | QCBORDecode_ExitMap(pDCtx); |
| 6361 | QCBORDecode_ExitMap(pDCtx); |
| 6362 | |
| 6363 | return 0; |
| 6364 | } |
| 6365 | |
| 6366 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 6367 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 6368 | { |
| 6369 | QCBORItem Item; |
| 6370 | QCBORError uErr; |
| 6371 | |
| 6372 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 6373 | for(int x = 0; x < 20; x++) { |
| 6374 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6375 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6376 | return 4100; |
| 6377 | |
| 6378 | } |
| 6379 | } |
| 6380 | QCBORDecode_ExitArray(pDCtx); |
| 6381 | if(QCBORDecode_GetError(pDCtx)) { |
| 6382 | return 4101; |
| 6383 | } |
| 6384 | |
| 6385 | return 0; |
| 6386 | } |
| 6387 | |
| 6388 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 6389 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 6390 | { |
| 6391 | QCBORError uErr; |
| 6392 | |
| 6393 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6394 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 6395 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6396 | int64_t nInt = 99; |
| 6397 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 6398 | if(nInt != 0) { |
| 6399 | return 4200; |
| 6400 | } |
| 6401 | for(int x = 0; x < 20; x++) { |
| 6402 | QCBORItem Item; |
| 6403 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6404 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6405 | return 4201; |
| 6406 | |
| 6407 | } |
| 6408 | } |
| 6409 | QCBORDecode_ExitArray(pDCtx); |
| 6410 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 6411 | return 4202; |
| 6412 | } |
| 6413 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6414 | UsefulBufC dD; |
| 6415 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6416 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6417 | return 4203; |
| 6418 | } |
| 6419 | for(int x = 0; x < 20; x++) { |
| 6420 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6421 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 6422 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6423 | return 4204; |
| 6424 | } |
| 6425 | } |
| 6426 | QCBORDecode_ExitArray(pDCtx); |
| 6427 | QCBORDecode_ExitMap(pDCtx); |
| 6428 | QCBORDecode_ExitMap(pDCtx); |
| 6429 | |
| 6430 | return 0; |
| 6431 | } |
| 6432 | |
| 6433 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 6434 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 6435 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6436 | { |
| 6437 | QCBORDecodeContext DCtx; |
| 6438 | int32_t nReturn; |
| 6439 | QCBORError uErr; |
| 6440 | |
| 6441 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 6442 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6443 | |
| 6444 | for(int j = 0; j < 5; j++) { |
| 6445 | for(int i = 0; i < 20; i++) { |
| 6446 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 6447 | if(nReturn) { |
| 6448 | return nReturn; |
| 6449 | } |
| 6450 | } |
| 6451 | |
| 6452 | for(int i = 0; i < 20; i++) { |
| 6453 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 6454 | if(nReturn ) { |
| 6455 | return nReturn; |
| 6456 | } |
| 6457 | } |
| 6458 | |
| 6459 | for(int i = 0; i < 20; i++) { |
| 6460 | nReturn = DecodeNestedGetZero(&DCtx); |
| 6461 | if(nReturn ) { |
| 6462 | return nReturn; |
| 6463 | } |
| 6464 | } |
| 6465 | } |
| 6466 | |
| 6467 | QCBORDecode_ExitMap(&DCtx); |
| 6468 | uErr = QCBORDecode_Finish(&DCtx); |
| 6469 | if(uErr) { |
| 6470 | return (int32_t)uErr + 4100; |
| 6471 | } |
| 6472 | |
| 6473 | return 0; |
| 6474 | } |
| 6475 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6476 | |
| 6477 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6478 | [ |
| 6479 | 23, |
| 6480 | 6000, |
| 6481 | h'67616C6163746963', |
| 6482 | h'686176656E20746F6B656E' |
| 6483 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6484 | */ |
| 6485 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6486 | 0x84, |
| 6487 | 0x17, |
| 6488 | 0x19, 0x17, 0x70, |
| 6489 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 6490 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6491 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6492 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6493 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 6494 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6495 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6496 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 6497 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6498 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6499 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6500 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6501 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6502 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6503 | /* |
| 6504 | { |
| 6505 | 0: [], |
| 6506 | 9: [ |
| 6507 | [], |
| 6508 | [] |
| 6509 | ], |
| 6510 | 8: { |
| 6511 | 1: [], |
| 6512 | 2: {}, |
| 6513 | 3: [] |
| 6514 | }, |
| 6515 | 4: {}, |
| 6516 | 5: [], |
| 6517 | 6: [ |
| 6518 | [], |
| 6519 | [] |
| 6520 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6521 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6522 | */ |
| 6523 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6524 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 6525 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 6526 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 6527 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6528 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6529 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6530 | |
| 6531 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6532 | /* |
| 6533 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6534 | Duplicate label |
| 6535 | Integer overflow |
| 6536 | Date overflow |
| 6537 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6538 | { |
| 6539 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6540 | 3: -18446744073709551616, |
| 6541 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6542 | 5: 0, |
| 6543 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6544 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6545 | */ |
| 6546 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6547 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6548 | 0xa6, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6549 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6550 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 6551 | #else |
| 6552 | 0xa4, |
| 6553 | #endif |
| 6554 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6555 | 0x05, 0x00, |
| 6556 | 0x05, 0x00, |
| 6557 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6558 | }; |
| 6559 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6560 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6561 | static const uint8_t spUnRecoverableMapError1[] = { |
| 6562 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6563 | }; |
| 6564 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6565 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6566 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6567 | static const uint8_t spUnRecoverableMapError2[] = { |
| 6568 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6569 | }; |
| 6570 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6571 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6572 | static const uint8_t spUnRecoverableMapError3[] = { |
| 6573 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 6574 | }; |
| 6575 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6576 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6577 | static const uint8_t spUnRecoverableMapError4[] = { |
| 6578 | 0xbf, |
| 6579 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6580 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6581 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6582 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6583 | 0xff |
| 6584 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6585 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6586 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6587 | const unsigned char not_well_formed_submod_section[] = { |
| 6588 | 0xa1, 0x14, 0x1f, |
| 6589 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6590 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6591 | |
| 6592 | /* Array of length 3, but only two items. */ |
| 6593 | const unsigned char spBadConsumeInput[] = { |
| 6594 | 0x83, 0x00, 0x00 |
| 6595 | }; |
| 6596 | |
| 6597 | /* Tag nesting too deep. */ |
| 6598 | const unsigned char spBadConsumeInput2[] = { |
| 6599 | 0x81, |
| 6600 | 0xD8, 0x37, |
| 6601 | 0xD8, 0x2C, |
| 6602 | 0xD8, 0x21, |
| 6603 | 0xD6, |
| 6604 | 0xCB, |
| 6605 | 00 |
| 6606 | }; |
| 6607 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6608 | |
| 6609 | const unsigned char spBadConsumeInput4[] = { |
| 6610 | 0x81, 0x9f, 0x00, 0xff |
| 6611 | }; |
| 6612 | |
| 6613 | const unsigned char spBadConsumeInput5[] = { |
| 6614 | 0xa1, 0x80, 0x00 |
| 6615 | }; |
| 6616 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6617 | /* |
| 6618 | Lots of nesting for various nesting tests. |
| 6619 | { 1:1, |
| 6620 | 2:{ |
| 6621 | 21:21, |
| 6622 | 22:{ |
| 6623 | 221:[2111, 2112, 2113], |
| 6624 | 222:222, |
| 6625 | 223: {} |
| 6626 | }, |
| 6627 | 23: 23 |
| 6628 | }, |
| 6629 | 3:3, |
| 6630 | 4: [ {} ] |
| 6631 | } |
| 6632 | */ |
| 6633 | static const uint8_t spNested[] = { |
| 6634 | 0xA4, /* Map of 4 */ |
| 6635 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 6636 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 6637 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 6638 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 6639 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 6640 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 6641 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 6642 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 6643 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 6644 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 6645 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 6646 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 6647 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 6648 | 0xA0 /* Array entry {}, an empty map */ |
| 6649 | }; |
| 6650 | |
| 6651 | |
| 6652 | static int32_t EnterMapCursorTest(void) |
| 6653 | { |
| 6654 | QCBORDecodeContext DCtx; |
| 6655 | QCBORItem Item1; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6656 | int64_t nInt; |
| 6657 | QCBORError uErr; |
| 6658 | |
| 6659 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6660 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6661 | QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt); |
| 6662 | uErr = QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 6663 | if(uErr != QCBOR_SUCCESS) { |
| 6664 | return 701; |
| 6665 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6666 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6667 | return 700; |
| 6668 | } |
| 6669 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6670 | |
| 6671 | int i; |
| 6672 | for(i = 0; i < 13; i++) { |
| 6673 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6674 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6675 | int j; |
| 6676 | /* Move travesal cursor */ |
| 6677 | for(j = 0; j < i; j++) { |
| 6678 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6679 | } |
| 6680 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6681 | QCBORDecode_ExitMap(&DCtx); |
| 6682 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6683 | if(Item1.label.int64 != 3) { |
| 6684 | return 8000; |
| 6685 | } |
| 6686 | } |
| 6687 | |
| 6688 | for(i = 0; i < 13; i++) { |
| 6689 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6690 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6691 | int j; |
| 6692 | /* Move travesal cursor */ |
| 6693 | for(j = 0; j < i; j++) { |
| 6694 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6695 | } |
| 6696 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6697 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6698 | QCBORDecode_ExitMap(&DCtx); |
| 6699 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6700 | if(Item1.label.int64 != 23) { |
| 6701 | return 8000; |
| 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | for(i = 0; i < 13; i++) { |
| 6706 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6707 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6708 | int j; |
| 6709 | /* Move travesal cursor */ |
| 6710 | for(j = 0; j < i; j++) { |
| 6711 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6712 | } |
| 6713 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6714 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6715 | for(j = 0; j < i; j++) { |
| 6716 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6717 | } |
| 6718 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 6719 | QCBORDecode_ExitArray(&DCtx); |
| 6720 | QCBORDecode_ExitMap(&DCtx); |
| 6721 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6722 | if(Item1.label.int64 != 23) { |
| 6723 | return 8000; |
| 6724 | } |
| 6725 | QCBORDecode_ExitMap(&DCtx); |
| 6726 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6727 | if(Item1.label.int64 != 3) { |
| 6728 | return 8000; |
| 6729 | } |
| 6730 | } |
| 6731 | |
| 6732 | return 0; |
| 6733 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6734 | |
| 6735 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6736 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6737 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6738 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6739 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6740 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6741 | int32_t nReturn; |
| 6742 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6743 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6744 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6745 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6746 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6747 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6748 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6749 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6750 | QCBORDecode_ExitArray(&DCtx); |
| 6751 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6752 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 6753 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6754 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6755 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6756 | QCBORDecode_ExitArray(&DCtx); |
| 6757 | QCBORDecode_ExitArray(&DCtx); |
| 6758 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6759 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 6760 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6761 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6762 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6763 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6764 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6765 | QCBORDecode_ExitArray(&DCtx); |
| 6766 | QCBORDecode_ExitMap(&DCtx); |
| 6767 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6768 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6769 | QCBORDecode_ExitMap(&DCtx); |
| 6770 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6771 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6772 | QCBORDecode_ExitArray(&DCtx); |
| 6773 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6774 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 6775 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6776 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6777 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6778 | QCBORDecode_ExitArray(&DCtx); |
| 6779 | QCBORDecode_ExitArray(&DCtx); |
| 6780 | |
| 6781 | QCBORDecode_ExitMap(&DCtx); |
| 6782 | |
| 6783 | uErr = QCBORDecode_Finish(&DCtx); |
| 6784 | if(uErr != QCBOR_SUCCESS){ |
| 6785 | return 3011; |
| 6786 | } |
| 6787 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6788 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6789 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6790 | if(nReturn) { |
| 6791 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6792 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6793 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6794 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6795 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6796 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6797 | if(nReturn) { |
| 6798 | return nReturn; |
| 6799 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6800 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6801 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6802 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 6803 | // These tests confirm the cursor is at the right place after entering |
| 6804 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6805 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6806 | |
| 6807 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6808 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6809 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6810 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6811 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6812 | return 2001; |
| 6813 | } |
| 6814 | |
| 6815 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6816 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 6817 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6818 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6819 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 6820 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6821 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6822 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 6823 | return 2051; |
| 6824 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6825 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6826 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 6827 | return 2002; |
| 6828 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6829 | QCBORDecode_ExitArray(&DCtx); |
| 6830 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 6831 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6832 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6833 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 6834 | return 2052; |
| 6835 | } |
| 6836 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6837 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6838 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6839 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6840 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6841 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6842 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6843 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6844 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6845 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 6846 | return 2003; |
| 6847 | } |
| 6848 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6849 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6850 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6851 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6852 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6853 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6854 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6855 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6856 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6857 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6858 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6859 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6860 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6861 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6862 | } |
| 6863 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6864 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6865 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 6866 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6867 | QCBORDecode_ExitArray(&DCtx); |
| 6868 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6869 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 6870 | return 2006; |
| 6871 | } |
| 6872 | QCBORDecode_ExitMap(&DCtx); |
| 6873 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6874 | return 2007; |
| 6875 | } |
| 6876 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6877 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6878 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6879 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6880 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6881 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6882 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6883 | return 2008; |
| 6884 | } |
| 6885 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6886 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6887 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6888 | return 2009; |
| 6889 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6890 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6891 | |
| 6892 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6893 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6894 | // This will fail because the map is empty. |
| 6895 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6896 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6897 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6898 | return 2010; |
| 6899 | } |
| 6900 | QCBORDecode_ExitMap(&DCtx); |
| 6901 | uErr = QCBORDecode_Finish(&DCtx); |
| 6902 | if(uErr != QCBOR_SUCCESS){ |
| 6903 | return 2011; |
| 6904 | } |
| 6905 | |
| 6906 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6907 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6908 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6909 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6910 | // This will fail because the map is empty. |
| 6911 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6912 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6913 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6914 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6915 | } |
| 6916 | QCBORDecode_ExitMap(&DCtx); |
| 6917 | uErr = QCBORDecode_Finish(&DCtx); |
| 6918 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6919 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6920 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6921 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6922 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6923 | |
| 6924 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6925 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6926 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6927 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6928 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6929 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6930 | QCBORDecode_ExitArray(&DCtx); |
| 6931 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 6932 | QCBORDecode_ExitArray(&DCtx); |
| 6933 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6934 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6935 | return 2014; |
| 6936 | } |
| 6937 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6938 | int64_t nInt; |
| 6939 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6940 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6941 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6942 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6943 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6944 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 6945 | return 2021; |
| 6946 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6947 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 6948 | return 2121; |
| 6949 | } |
| 6950 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6951 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6952 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6953 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6954 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 6955 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6956 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 6957 | return 2023; |
| 6958 | } |
| 6959 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6960 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6961 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 6962 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6963 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6964 | return 2024; |
| 6965 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6966 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6967 | |
| 6968 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 6969 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6970 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 6971 | return 2025; |
| 6972 | } |
| 6973 | |
| 6974 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 6975 | |
| 6976 | QCBORDecode_ExitMap(&DCtx); |
| 6977 | uErr = QCBORDecode_Finish(&DCtx); |
| 6978 | if(uErr != QCBOR_SUCCESS) { |
| 6979 | return 2026; |
| 6980 | } |
| 6981 | |
| 6982 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6983 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6984 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6985 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6986 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 6987 | return 2030; |
| 6988 | } |
| 6989 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6990 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6991 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6992 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6993 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6994 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6995 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6996 | return 2031; |
| 6997 | } |
| 6998 | |
| 6999 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7000 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7001 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7002 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7003 | if(uErr != QCBOR_ERR_HIT_END) { |
| 7004 | return 2032; |
| 7005 | } |
| 7006 | |
| 7007 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7008 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7009 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7010 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7011 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 7012 | return 2033; |
| 7013 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7014 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7015 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 7016 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7017 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7018 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 7019 | return 2401; |
| 7020 | } |
| 7021 | if(QCBORDecode_GetError(&DCtx)) { |
| 7022 | return 2402; |
| 7023 | } |
| 7024 | |
| 7025 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7026 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 7027 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7028 | Item1.val.uCount != 3 || |
| 7029 | Item1.uNextNestLevel != 1) { |
| 7030 | return 2403; |
| 7031 | } |
| 7032 | if(QCBORDecode_GetError(&DCtx)) { |
| 7033 | return 2404; |
| 7034 | } |
| 7035 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7036 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 7037 | Item1.uNextNestLevel != 1 || |
| 7038 | Item1.val.int64 != 42) { |
| 7039 | return 2405; |
| 7040 | } |
| 7041 | if(QCBORDecode_GetError(&DCtx)) { |
| 7042 | return 2406; |
| 7043 | } |
| 7044 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7045 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 7046 | Item1.uNestingLevel != 1 || |
| 7047 | Item1.uNextNestLevel != 1 || |
| 7048 | Item1.val.uCount != 2) { |
| 7049 | return 2407; |
| 7050 | } |
| 7051 | if(QCBORDecode_GetError(&DCtx)) { |
| 7052 | return 2408; |
| 7053 | } |
| 7054 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7055 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7056 | Item1.uNestingLevel != 1 || |
| 7057 | Item1.uNextNestLevel != 0 || |
| 7058 | Item1.val.uCount != 4) { |
| 7059 | return 2409; |
| 7060 | } |
| 7061 | if(QCBORDecode_GetError(&DCtx)) { |
| 7062 | return 2410; |
| 7063 | } |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7064 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7065 | nReturn = DecodeNestedIterate(); |
| 7066 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 7067 | |
| 7068 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 7069 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7070 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 7071 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 7072 | return 2500; |
| 7073 | } |
| 7074 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7075 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 7076 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7077 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7078 | return 2600; |
| 7079 | } |
| 7080 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7081 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7082 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 7083 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7084 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7085 | return 2700; |
| 7086 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7087 | #endif |
| 7088 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7089 | |
| 7090 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 7091 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7092 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7093 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7094 | return 2900; |
| 7095 | } |
| 7096 | #else |
| 7097 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7098 | return 2901; |
| 7099 | } |
| 7100 | #endif |
| 7101 | |
| 7102 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 7103 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7104 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 7105 | return 3000; |
| 7106 | } |
| 7107 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 7108 | nReturn = EnterMapCursorTest(); |
| 7109 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7110 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7114 | struct NumberConversion { |
| 7115 | char *szDescription; |
| 7116 | UsefulBufC CBOR; |
| 7117 | int64_t nConvertedToInt64; |
| 7118 | QCBORError uErrorInt64; |
| 7119 | uint64_t uConvertToUInt64; |
| 7120 | QCBORError uErrorUint64; |
| 7121 | double dConvertToDouble; |
| 7122 | QCBORError uErrorDouble; |
| 7123 | }; |
| 7124 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7125 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 7126 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 7127 | #else |
| 7128 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 7129 | #endif |
| 7130 | |
| 7131 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7132 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7133 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7134 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 7135 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 7136 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 7137 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 7138 | }, 15}, |
| 7139 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 7140 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 7141 | 0, |
| 7142 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7143 | -4.6116860184273879E+18, |
| 7144 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 7145 | }, |
| 7146 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7147 | "too large to fit into int64_t", |
| 7148 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 7149 | 0, |
| 7150 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7151 | 0, |
| 7152 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7153 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7154 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7155 | }, |
| 7156 | { |
| 7157 | "largest negative int that fits in int64_t", |
| 7158 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 7159 | INT64_MIN, |
| 7160 | QCBOR_SUCCESS, |
| 7161 | 0, |
| 7162 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7163 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7164 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7165 | }, |
| 7166 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7167 | "negative bignum -1", |
| 7168 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 7169 | -1, |
| 7170 | QCBOR_SUCCESS, |
| 7171 | 0, |
| 7172 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7173 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7174 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7175 | }, |
| 7176 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7177 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7178 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7179 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7180 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7181 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7182 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7183 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7184 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7185 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7186 | }, |
| 7187 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7188 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7189 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7190 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7191 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7192 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7193 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7194 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7195 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7196 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7197 | }, |
| 7198 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7199 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7200 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7201 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7202 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7203 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7204 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7205 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7206 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7207 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7208 | }, |
| 7209 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7210 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7211 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 7212 | 0, |
| 7213 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7214 | 0, |
| 7215 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7216 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7217 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7218 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7219 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7220 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7221 | "Positive bignum 0x01020304 indefinite length string", |
| 7222 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 7223 | 0x01020304, |
| 7224 | QCBOR_SUCCESS, |
| 7225 | 0x01020304, |
| 7226 | QCBOR_SUCCESS, |
| 7227 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7228 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7229 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7230 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7231 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7232 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7233 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 7234 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 7235 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7236 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7237 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7238 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7239 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7240 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7241 | }, |
| 7242 | { |
| 7243 | "big float [9223372036854775806, 9223372036854775806]", |
| 7244 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7245 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 7246 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7247 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7248 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7249 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7250 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7251 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7252 | }, |
| 7253 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7254 | "Big float 3 * 2^^2", |
| 7255 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 7256 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7257 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7258 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7259 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7260 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7261 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7262 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7263 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7264 | "Decimal fraction 3/10", |
| 7265 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 7266 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7267 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7268 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7269 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7270 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7271 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 7272 | }, |
| 7273 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7274 | "extreme pos bignum", |
| 7275 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 7276 | // 50 rows of 8 is 400 digits. |
| 7277 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7278 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7279 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7280 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7281 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7282 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7283 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7284 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7285 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7286 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7287 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7288 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7289 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7290 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7291 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7292 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7293 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7294 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7295 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7296 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7297 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7298 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7299 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7300 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7301 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7302 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7303 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7304 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7305 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7306 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7307 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7308 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7309 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7310 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7311 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7312 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7313 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7314 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7315 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7316 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7317 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7318 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7319 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7320 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7321 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7322 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7323 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7324 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7325 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7326 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7327 | 404}, |
| 7328 | 0, |
| 7329 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7330 | 0, |
| 7331 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7332 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7333 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7334 | }, |
| 7335 | |
| 7336 | { |
| 7337 | "extreme neg bignum", |
| 7338 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 7339 | // 50 rows of 8 is 400 digits. |
| 7340 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7341 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7342 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7343 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7344 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7345 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7346 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7347 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7348 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7349 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7350 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7351 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7352 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7353 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7354 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7355 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7356 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7357 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7358 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7359 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7360 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7361 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7362 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7363 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7364 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7365 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7366 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7367 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7368 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7369 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7370 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7371 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7372 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7373 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7374 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7375 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7376 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7377 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7378 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7379 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7380 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7381 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7382 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7383 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7384 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7385 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7386 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7387 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7388 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7389 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7390 | 404}, |
| 7391 | 0, |
| 7392 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7393 | 0, |
| 7394 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7395 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7396 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7397 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7398 | |
| 7399 | { |
| 7400 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 7401 | {(uint8_t[]){ |
| 7402 | 0xC5, 0x82, |
| 7403 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7404 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7405 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7406 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7407 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7408 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7409 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7410 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7411 | }, |
| 7412 | |
| 7413 | { |
| 7414 | "bigfloat that evaluates to -INFINITY", |
| 7415 | {(uint8_t[]){ |
| 7416 | 0xC5, 0x82, |
| 7417 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7418 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7419 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7420 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7421 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7422 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7423 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7424 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7425 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7426 | { |
| 7427 | "Positive bignum 0xffff", |
| 7428 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 7429 | 65536-1, |
| 7430 | QCBOR_SUCCESS, |
| 7431 | 0xffff, |
| 7432 | QCBOR_SUCCESS, |
| 7433 | 65535.0, |
| 7434 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7435 | }, |
| 7436 | #endif /* QCBOR_DISABLE_TAGS */ |
| 7437 | { |
| 7438 | "Positive integer 18446744073709551615", |
| 7439 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 7440 | 0, |
| 7441 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7442 | 18446744073709551615ULL, |
| 7443 | QCBOR_SUCCESS, |
| 7444 | 18446744073709551615.0, |
| 7445 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7446 | }, |
| 7447 | |
| 7448 | { |
| 7449 | "Postive integer 0", |
| 7450 | {(uint8_t[]){0x0}, 1}, |
| 7451 | 0LL, |
| 7452 | QCBOR_SUCCESS, |
| 7453 | 0ULL, |
| 7454 | QCBOR_SUCCESS, |
| 7455 | 0.0, |
| 7456 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7457 | }, |
| 7458 | { |
| 7459 | "Negative integer -18446744073709551616", |
| 7460 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 7461 | -9223372036854775807-1, // INT64_MIN |
| 7462 | QCBOR_SUCCESS, |
| 7463 | 0ULL, |
| 7464 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7465 | -9223372036854775808.0, |
| 7466 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7467 | }, |
| 7468 | { |
| 7469 | "Double Floating point value 100.3", |
| 7470 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 7471 | 100L, |
| 7472 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7473 | 100ULL, |
| 7474 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7475 | 100.3, |
| 7476 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 7477 | }, |
| 7478 | { |
| 7479 | "Floating point value NaN 0xfa7fc00000", |
| 7480 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 7481 | 0, |
| 7482 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7483 | 0, |
| 7484 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7485 | NAN, |
| 7486 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7487 | }, |
| 7488 | { |
| 7489 | "half-precision Floating point value -4", |
| 7490 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 7491 | // Normal case with all enabled. |
| 7492 | -4, |
| 7493 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7494 | 0, |
| 7495 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7496 | -4.0, |
| 7497 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 7498 | }, |
| 7499 | { |
| 7500 | "+inifinity single precision", |
| 7501 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 7502 | 0, |
| 7503 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7504 | 0, |
| 7505 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 7506 | INFINITY, |
| 7507 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7508 | }, |
| 7509 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7510 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7511 | |
| 7512 | |
| 7513 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7514 | |
| 7515 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 7516 | { |
| 7517 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 7518 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7519 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 7520 | return 1; |
| 7521 | } |
| 7522 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7523 | (void)Pool; |
| 7524 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7525 | return 0; |
| 7526 | } |
| 7527 | |
| 7528 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7529 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7530 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7531 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 7532 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7533 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7534 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 7535 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7536 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7537 | // Set up the decoding context including a memory pool so that |
| 7538 | // indefinite length items can be checked |
| 7539 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7540 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7541 | |
| 7542 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7543 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7544 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7545 | } |
| 7546 | |
| 7547 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7548 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7549 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7550 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7551 | } |
| 7552 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7553 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7554 | } |
| 7555 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7556 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7557 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7558 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7559 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7560 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7561 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7562 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7563 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7564 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7565 | } |
| 7566 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7567 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7568 | } |
| 7569 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7570 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7571 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7572 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7573 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7574 | |
| 7575 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7576 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7577 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7578 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7579 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7580 | } |
| 7581 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 7582 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7583 | // NaN's can't be compared for equality. A NaN is |
| 7584 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7585 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7586 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7587 | } |
| 7588 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7589 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7590 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7591 | } |
| 7592 | } |
| 7593 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7594 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7595 | } |
| 7596 | |
| 7597 | return 0; |
| 7598 | } |
| 7599 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7600 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7601 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7602 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7603 | { |
| 7604 | QCBORDecodeContext DCtx; |
| 7605 | QCBORItem Item; |
| 7606 | QCBORError uCBORError; |
| 7607 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7608 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7609 | QCBORDecode_Init(&DCtx, |
| 7610 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 7611 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7612 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7613 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 7614 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7615 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7616 | do { |
| 7617 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7618 | } while (QCBOR_SUCCESS == uCBORError); |
| 7619 | |
| 7620 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7621 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 7622 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7623 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7624 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7625 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7626 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7627 | |
| 7628 | |
| 7629 | static const uint8_t spSequenceTestInput[] = { |
| 7630 | /* 1. The valid date string "1985-04-12" */ |
| 7631 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 7632 | |
| 7633 | /* 2. */ |
| 7634 | 0x00, |
| 7635 | |
| 7636 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 7637 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 7638 | |
| 7639 | /* 4. */ |
| 7640 | 0x62, 'h', 'i', |
| 7641 | }; |
| 7642 | |
| 7643 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7644 | int32_t CBORSequenceDecodeTests(void) |
| 7645 | { |
| 7646 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7647 | QCBORItem Item; |
| 7648 | QCBORError uCBORError; |
| 7649 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7650 | |
| 7651 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7652 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7653 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7654 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7655 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7656 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7657 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7658 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7659 | if(uCBORError != QCBOR_SUCCESS) { |
| 7660 | return 1; |
| 7661 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7662 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7663 | return 2; |
| 7664 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7665 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7666 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7667 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7668 | uConsumed != 11) { |
| 7669 | return 102; |
| 7670 | } |
| 7671 | |
| 7672 | // Get 2. |
| 7673 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7674 | if(uCBORError != QCBOR_SUCCESS) { |
| 7675 | return 66; |
| 7676 | } |
| 7677 | |
| 7678 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7679 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7680 | uConsumed != 12) { |
| 7681 | return 102; |
| 7682 | } |
| 7683 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7684 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7685 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7686 | if(uCBORError != QCBOR_SUCCESS) { |
| 7687 | return 2; |
| 7688 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7689 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7690 | return 3; |
| 7691 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7692 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7693 | // A sequence can have stuff at the end that may |
| 7694 | // or may not be valid CBOR. The protocol decoder knows |
| 7695 | // when to stop by definition of the protocol, not |
| 7696 | // when the top-level map or array is ended. |
| 7697 | // Finish still has to be called to know that |
| 7698 | // maps and arrays (if there were any) were closed |
| 7699 | // off correctly. When called like this it |
| 7700 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 7701 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7702 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 7703 | return 4; |
| 7704 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7705 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7706 | // --- Test an empty input ---- |
| 7707 | uint8_t empty[1]; |
| 7708 | UsefulBufC Empty = {empty, 0}; |
| 7709 | QCBORDecode_Init(&DCtx, |
| 7710 | Empty, |
| 7711 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7712 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7713 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7714 | if(uCBORError != QCBOR_SUCCESS) { |
| 7715 | return 5; |
| 7716 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7717 | |
| 7718 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7719 | // --- Sequence with unclosed indefinite length array --- |
| 7720 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7721 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7722 | QCBORDecode_Init(&DCtx, |
| 7723 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 7724 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7725 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7726 | // Get the first item |
| 7727 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7728 | if(uCBORError != QCBOR_SUCCESS) { |
| 7729 | return 7; |
| 7730 | } |
| 7731 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7732 | return 8; |
| 7733 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7734 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7735 | // Get a second item |
| 7736 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7737 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7738 | if(uCBORError != QCBOR_SUCCESS) { |
| 7739 | return 9; |
| 7740 | } |
| 7741 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7742 | return 10; |
| 7743 | } |
| 7744 | |
| 7745 | // Try to finish before consuming all bytes to confirm |
| 7746 | // that the still-open error is returned. |
| 7747 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7748 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7749 | return 11; |
| 7750 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7751 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7752 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7753 | return 20; |
| 7754 | } |
| 7755 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7756 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7757 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7758 | // --- Sequence with a closed indefinite length array --- |
| 7759 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7760 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7761 | QCBORDecode_Init(&DCtx, |
| 7762 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 7763 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7764 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7765 | // Get the first item |
| 7766 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7767 | if(uCBORError != QCBOR_SUCCESS) { |
| 7768 | return 12; |
| 7769 | } |
| 7770 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7771 | return 13; |
| 7772 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7773 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7774 | // Get a second item |
| 7775 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7776 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7777 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7778 | if(uCBORError != QCBOR_SUCCESS) { |
| 7779 | return 14; |
| 7780 | } |
| 7781 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7782 | return 15; |
| 7783 | } |
| 7784 | |
| 7785 | // Try to finish before consuming all bytes to confirm |
| 7786 | // that the still-open error is returned. |
| 7787 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7788 | if(uCBORError != QCBOR_SUCCESS) { |
| 7789 | return 16; |
| 7790 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7791 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7792 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7793 | return 20; |
| 7794 | } |
| 7795 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7796 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7797 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7798 | return 0; |
| 7799 | } |
| 7800 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7801 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 7802 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7803 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7804 | { |
| 7805 | int nErrCode; |
| 7806 | int32_t n32; |
| 7807 | int16_t n16; |
| 7808 | int8_t n8; |
| 7809 | uint32_t u32; |
| 7810 | uint16_t u16; |
| 7811 | uint8_t u8; |
| 7812 | uint64_t u64; |
| 7813 | |
| 7814 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 7815 | if(nErrCode == -1 || n32 != 1) { |
| 7816 | return 1; |
| 7817 | } |
| 7818 | |
| 7819 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 7820 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 7821 | return 2; |
| 7822 | } |
| 7823 | |
| 7824 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 7825 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 7826 | return 3; |
| 7827 | } |
| 7828 | |
| 7829 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 7830 | if(nErrCode != -1) { |
| 7831 | return 4; |
| 7832 | } |
| 7833 | |
| 7834 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 7835 | if(nErrCode != -1) { |
| 7836 | return 5; |
| 7837 | } |
| 7838 | |
| 7839 | |
| 7840 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 7841 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 7842 | return 6; |
| 7843 | } |
| 7844 | |
| 7845 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 7846 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 7847 | return 7; |
| 7848 | } |
| 7849 | |
| 7850 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 7851 | if(nErrCode == -1 || n16 != 1) { |
| 7852 | return 8; |
| 7853 | } |
| 7854 | |
| 7855 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 7856 | if(nErrCode != -1) { |
| 7857 | return 9; |
| 7858 | } |
| 7859 | |
| 7860 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 7861 | if(nErrCode != -1) { |
| 7862 | return 10; |
| 7863 | } |
| 7864 | |
| 7865 | |
| 7866 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 7867 | if(nErrCode == -1 || n8 != 1) { |
| 7868 | return 11; |
| 7869 | } |
| 7870 | |
| 7871 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 7872 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 7873 | return 12; |
| 7874 | } |
| 7875 | |
| 7876 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 7877 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 7878 | return 13; |
| 7879 | } |
| 7880 | |
| 7881 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 7882 | if(nErrCode != -1) { |
| 7883 | return 14; |
| 7884 | } |
| 7885 | |
| 7886 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 7887 | if(nErrCode != -1) { |
| 7888 | return 15; |
| 7889 | } |
| 7890 | |
| 7891 | |
| 7892 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 7893 | if(nErrCode == -1 || u32 != 1) { |
| 7894 | return 16; |
| 7895 | } |
| 7896 | |
| 7897 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 7898 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 7899 | return 17; |
| 7900 | } |
| 7901 | |
| 7902 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 7903 | if(nErrCode == -1 || u32 != 0) { |
| 7904 | return 18; |
| 7905 | } |
| 7906 | |
| 7907 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 7908 | if(nErrCode != -1) { |
| 7909 | return 19; |
| 7910 | } |
| 7911 | |
| 7912 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 7913 | if(nErrCode != -1) { |
| 7914 | return 20; |
| 7915 | } |
| 7916 | |
| 7917 | |
| 7918 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 7919 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 7920 | return 21; |
| 7921 | } |
| 7922 | |
| 7923 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 7924 | if(nErrCode == -1 || u16 != 0) { |
| 7925 | return 22; |
| 7926 | } |
| 7927 | |
| 7928 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 7929 | if(nErrCode == -1 || u16 != 1) { |
| 7930 | return 23; |
| 7931 | } |
| 7932 | |
| 7933 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 7934 | if(nErrCode != -1) { |
| 7935 | return 24; |
| 7936 | } |
| 7937 | |
| 7938 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 7939 | if(nErrCode != -1) { |
| 7940 | return 25; |
| 7941 | } |
| 7942 | |
| 7943 | |
| 7944 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 7945 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 7946 | return 26; |
| 7947 | } |
| 7948 | |
| 7949 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 7950 | if(nErrCode == -1 || u8 != 0) { |
| 7951 | return 27; |
| 7952 | } |
| 7953 | |
| 7954 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 7955 | if(nErrCode == -1 || u8 != 1) { |
| 7956 | return 28; |
| 7957 | } |
| 7958 | |
| 7959 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 7960 | if(nErrCode != -1) { |
| 7961 | return 29; |
| 7962 | } |
| 7963 | |
| 7964 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 7965 | if(nErrCode != -1) { |
| 7966 | return 30; |
| 7967 | } |
| 7968 | |
| 7969 | |
| 7970 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 7971 | if(nErrCode == -1 || u64 != 1) { |
| 7972 | return 31; |
| 7973 | } |
| 7974 | |
| 7975 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 7976 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 7977 | return 32; |
| 7978 | } |
| 7979 | |
| 7980 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 7981 | if(nErrCode == -1 || u64 != 0) { |
| 7982 | return 33; |
| 7983 | } |
| 7984 | |
| 7985 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 7986 | if(nErrCode != -1) { |
| 7987 | return 34; |
| 7988 | } |
| 7989 | |
| 7990 | return 0; |
| 7991 | } |
| 7992 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7993 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7994 | |
| 7995 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7996 | /* |
| 7997 | A sequence with |
| 7998 | A wrapping bstr |
| 7999 | containing a map |
| 8000 | 1 |
| 8001 | 2 |
| 8002 | A wrapping bstr |
| 8003 | containing an array |
| 8004 | 3 |
| 8005 | wrapping bstr |
| 8006 | 4 |
| 8007 | 5 |
| 8008 | 6 |
| 8009 | array |
| 8010 | 7 |
| 8011 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8012 | */ |
| 8013 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8014 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8015 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8016 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8017 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8018 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8019 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8020 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8021 | |
| 8022 | QCBOREncode_BstrWrap(&EC); |
| 8023 | QCBOREncode_OpenMap(&EC); |
| 8024 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 8025 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 8026 | QCBOREncode_CloseMap(&EC); |
| 8027 | QCBOREncode_BstrWrap(&EC); |
| 8028 | QCBOREncode_OpenArray(&EC); |
| 8029 | QCBOREncode_AddInt64(&EC, 3); |
| 8030 | QCBOREncode_BstrWrap(&EC); |
| 8031 | QCBOREncode_AddInt64(&EC, 4); |
| 8032 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8033 | QCBOREncode_AddInt64(&EC, 5); |
| 8034 | QCBOREncode_CloseArray(&EC); |
| 8035 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8036 | QCBOREncode_AddInt64(&EC, 6); |
| 8037 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8038 | QCBOREncode_OpenArray(&EC); |
| 8039 | QCBOREncode_AddInt64(&EC, 7); |
| 8040 | QCBOREncode_AddInt64(&EC, 8); |
| 8041 | QCBOREncode_CloseArray(&EC); |
| 8042 | |
| 8043 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 8044 | if(uErr) { |
| 8045 | Encoded = NULLUsefulBufC; |
| 8046 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8047 | |
| 8048 | return Encoded; |
| 8049 | } |
| 8050 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8051 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8052 | static const uint8_t spBreakInByteString[] = { |
| 8053 | 0x41, 0xff |
| 8054 | }; |
| 8055 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8056 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8057 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8058 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8059 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8060 | |
| 8061 | QCBORDecodeContext DC; |
| 8062 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8063 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8064 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8065 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8066 | |
| 8067 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8068 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8069 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8070 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 8071 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8072 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8073 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8074 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8075 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8076 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8077 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8078 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8079 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8080 | QCBORDecode_ExitArray(&DC); |
| 8081 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8082 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8083 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8084 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8085 | QCBORDecode_GetInt64(&DC, &n7); |
| 8086 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8087 | QCBORDecode_ExitArray(&DC); |
| 8088 | |
| 8089 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8090 | if(uErr) { |
| 8091 | return (int32_t)uErr; |
| 8092 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8093 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8094 | |
| 8095 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 8096 | * Successful because no items are fetched from byte string. |
| 8097 | */ |
| 8098 | QCBORDecode_Init(&DC, |
| 8099 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8100 | 0); |
| 8101 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8102 | uErr = QCBORDecode_GetError(&DC); |
| 8103 | if(uErr) { |
| 8104 | return 100 + (int32_t)uErr; |
| 8105 | } |
| 8106 | |
| 8107 | QCBORDecode_ExitBstrWrapped(&DC); |
| 8108 | uErr = QCBORDecode_GetError(&DC); |
| 8109 | if(uErr) { |
| 8110 | return 200 + (int32_t)uErr; |
| 8111 | } |
| 8112 | |
| 8113 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 8114 | * It fails because there should be no break. |
| 8115 | */ |
| 8116 | QCBORDecode_Init(&DC, |
| 8117 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8118 | 0); |
| 8119 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8120 | QCBORItem Item; |
| 8121 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 8122 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 8123 | return 300 + (int32_t)uErr; |
| 8124 | } |
| 8125 | |
| 8126 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8127 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8128 | |
| 8129 | |
| 8130 | |
| 8131 | |
| 8132 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8133 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8134 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8135 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8136 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8137 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 8138 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 8139 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8140 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8141 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8142 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 8143 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 8144 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8145 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8146 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8147 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8148 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8149 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8150 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8151 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8152 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8153 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8154 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8155 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8156 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8157 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 8158 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8159 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8160 | 21, |
| 8161 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 8162 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 8163 | |
| 8164 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8165 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8166 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8167 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8168 | |
| 8169 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8170 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8171 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8172 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8173 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8174 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8175 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8176 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8177 | |
| 8178 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8179 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8180 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8181 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8182 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8183 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8184 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 8185 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8186 | |
| 8187 | 0x18, 0x33, |
| 8188 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 8189 | |
| 8190 | // MIME |
| 8191 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8192 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 8193 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 8194 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8195 | |
| 8196 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8197 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8198 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8199 | |
| 8200 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8201 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 8202 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 8203 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8204 | |
| 8205 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8206 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8207 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8208 | |
| 8209 | // UUID |
| 8210 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8211 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 8212 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8213 | |
| 8214 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8215 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 8216 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8217 | }; |
| 8218 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8219 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8220 | { |
| 8221 | QCBORDecodeContext DC; |
| 8222 | QCBORError uErr; |
| 8223 | |
| 8224 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 8225 | |
| 8226 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8227 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8228 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8229 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8230 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8231 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8232 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 8233 | return 1; |
| 8234 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8235 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8236 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8237 | return 2; |
| 8238 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8239 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8240 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8241 | return 3; |
| 8242 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8243 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8244 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8245 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8246 | return 4; |
| 8247 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8248 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8249 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8250 | return 5; |
| 8251 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8252 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8253 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8254 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8255 | bNeg != false) { |
| 8256 | return 10; |
| 8257 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8258 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8259 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8260 | bNeg != true) { |
| 8261 | return 11; |
| 8262 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8263 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8264 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8265 | return 12; |
| 8266 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8267 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8268 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8269 | return 13; |
| 8270 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8271 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8272 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8273 | return 14; |
| 8274 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8275 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8276 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8277 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8278 | return 20; |
| 8279 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8280 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8281 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8282 | return 21; |
| 8283 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8284 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8285 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8286 | return 22; |
| 8287 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8288 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8289 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8290 | return 23; |
| 8291 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8292 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8293 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8294 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8295 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8296 | return 30; |
| 8297 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8298 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8299 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8300 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8301 | return 31; |
| 8302 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8303 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8304 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8305 | return 32; |
| 8306 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8307 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8308 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8309 | return 33; |
| 8310 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8311 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8312 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8313 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8314 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8315 | return 40; |
| 8316 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8317 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8318 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8319 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8320 | return 41; |
| 8321 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8322 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8323 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8324 | return 42; |
| 8325 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8326 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8327 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8328 | return 43; |
| 8329 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8330 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8331 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8332 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8333 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8334 | return 50; |
| 8335 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8336 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8337 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8338 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8339 | return 51; |
| 8340 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8341 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8342 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8343 | return 52; |
| 8344 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8345 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8346 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8347 | return 53; |
| 8348 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8349 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8350 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8351 | // MIME |
| 8352 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8353 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8354 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8355 | bIsNot7Bit == true) { |
| 8356 | return 60; |
| 8357 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8358 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8359 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8360 | bIsNot7Bit == true) { |
| 8361 | return 61; |
| 8362 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8363 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8364 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8365 | bIsNot7Bit == false) { |
| 8366 | return 62; |
| 8367 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8368 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8369 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8370 | bIsNot7Bit == false) { |
| 8371 | return 63; |
| 8372 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8373 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8374 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8375 | return 64; |
| 8376 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8377 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8378 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8379 | return 65; |
| 8380 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8381 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8382 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8383 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8384 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8385 | return 70; |
| 8386 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8387 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 8388 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8389 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8390 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8391 | return 71; |
| 8392 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8393 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8394 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8395 | return 72; |
| 8396 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8397 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8398 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8399 | return 73; |
| 8400 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8401 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8402 | // Improvement: add some more error test cases |
| 8403 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8404 | QCBORDecode_ExitMap(&DC); |
| 8405 | |
| 8406 | uErr = QCBORDecode_Finish(&DC); |
| 8407 | if(uErr != QCBOR_SUCCESS) { |
| 8408 | return 100; |
| 8409 | } |
| 8410 | |
| 8411 | return 0; |
| 8412 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8413 | |
| 8414 | |
| 8415 | |
| 8416 | |
| 8417 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8418 | [ |
| 8419 | "aaaaaaaaaa", |
| 8420 | {} |
| 8421 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8422 | */ |
| 8423 | static const uint8_t spTooLarge1[] = { |
| 8424 | 0x9f, |
| 8425 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8426 | 0xa0, |
| 8427 | 0xff |
| 8428 | }; |
| 8429 | |
| 8430 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8431 | [ |
| 8432 | { |
| 8433 | 0: "aaaaaaaaaa" |
| 8434 | } |
| 8435 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8436 | */ |
| 8437 | static const uint8_t spTooLarge2[] = { |
| 8438 | 0x9f, |
| 8439 | 0xa1, |
| 8440 | 0x00, |
| 8441 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8442 | 0xff |
| 8443 | }; |
| 8444 | |
| 8445 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8446 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8447 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8448 | { |
| 8449 | 0: "aaaaaaaaaa" |
| 8450 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8451 | */ |
| 8452 | static const uint8_t spTooLarge3[] = { |
| 8453 | 0x4d, |
| 8454 | 0xa1, |
| 8455 | 0x00, |
| 8456 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8457 | }; |
| 8458 | |
| 8459 | int32_t TooLargeInputTest(void) |
| 8460 | { |
| 8461 | QCBORDecodeContext DC; |
| 8462 | QCBORError uErr; |
| 8463 | UsefulBufC String; |
| 8464 | |
| 8465 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 8466 | // to 10 There's not really any way to test this error |
| 8467 | // condition. The error condition is not complex, so setting |
| 8468 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 8469 | |
| 8470 | // The input CBOR is only too large because the |
| 8471 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 8472 | // |
| 8473 | // This test is disabled for the normal test runs because of the |
| 8474 | // special build requirement. |
| 8475 | |
| 8476 | |
| 8477 | // Tests the start of a map being too large |
| 8478 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8479 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8480 | QCBORDecode_GetTextString(&DC, &String); |
| 8481 | uErr = QCBORDecode_GetError(&DC); |
| 8482 | if(uErr != QCBOR_SUCCESS) { |
| 8483 | return 1; |
| 8484 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8485 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8486 | uErr = QCBORDecode_GetError(&DC); |
| 8487 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8488 | return 2; |
| 8489 | } |
| 8490 | |
| 8491 | // Tests the end of a map being too large |
| 8492 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8493 | QCBORDecode_EnterArray(&DC, NULL); |
| 8494 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8495 | uErr = QCBORDecode_GetError(&DC); |
| 8496 | if(uErr != QCBOR_SUCCESS) { |
| 8497 | return 3; |
| 8498 | } |
| 8499 | QCBORDecode_ExitMap(&DC); |
| 8500 | uErr = QCBORDecode_GetError(&DC); |
| 8501 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8502 | return 4; |
| 8503 | } |
| 8504 | |
| 8505 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 8506 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 8507 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8508 | uErr = QCBORDecode_GetError(&DC); |
| 8509 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8510 | return 5; |
| 8511 | } |
| 8512 | |
| 8513 | return 0; |
| 8514 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8515 | |
| 8516 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8517 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 8518 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8519 | /* |
| 8520 | An array of three map entries |
| 8521 | 1) Indefinite length string label for indefinite lenght byte string |
| 8522 | 2) Indefinite length string label for an integer |
| 8523 | 3) Indefinite length string label for an indefinite-length negative big num |
| 8524 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8525 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8526 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8527 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 8528 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 8529 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 8530 | 0x03, |
| 8531 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 8532 | 0xc3, |
| 8533 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8534 | }; |
| 8535 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8536 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8537 | { |
| 8538 | QCBORDecodeContext DCtx; |
| 8539 | |
| 8540 | QCBORDecode_Init(&DCtx, |
| 8541 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 8542 | QCBOR_DECODE_MODE_NORMAL); |
| 8543 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8544 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8545 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 8546 | |
| 8547 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8548 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8549 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8550 | |
| 8551 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8552 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8553 | return 1; |
| 8554 | } |
| 8555 | |
| 8556 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 8557 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 8558 | return 2; |
| 8559 | } |
| 8560 | |
| 8561 | uint64_t uInt; |
| 8562 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 8563 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8564 | return 3; |
| 8565 | } |
| 8566 | if(uInt != 3) { |
| 8567 | return 4; |
| 8568 | } |
| 8569 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8570 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8571 | double uDouble; |
| 8572 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 8573 | "label2", |
| 8574 | 0xff, |
| 8575 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8576 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8577 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8578 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8579 | return 5; |
| 8580 | } |
| 8581 | if(uDouble != -16777474) { |
| 8582 | return 6; |
| 8583 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8584 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8585 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 8586 | return 7; |
| 8587 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8588 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8589 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8590 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8591 | QCBORDecode_ExitMap(&DCtx); |
| 8592 | |
| 8593 | if(QCBORDecode_Finish(&DCtx)) { |
| 8594 | return 99; |
| 8595 | } |
| 8596 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8597 | #else /* QCBOR_DISABLE_TAGS */ |
| 8598 | /* The big num in the input is a CBOR tag and you can't do |
| 8599 | * map lookups in a map with a tag so this test does very little |
| 8600 | * when tags are disabled. That is OK, the test coverage is still |
| 8601 | * good when they are not. |
| 8602 | */ |
| 8603 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 8604 | return 1002; |
| 8605 | } |
| 8606 | #endif /*QCBOR_DISABLE_TAGS */ |
| 8607 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8608 | return 0; |
| 8609 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8610 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8611 | |
| 8612 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8613 | /* |
| 8614 | * An array of an integer and an array. The second array contains |
| 8615 | * a bstr-wrapped map. |
| 8616 | * |
| 8617 | * [7, [h'A36D6669... (see next lines) 73']] |
| 8618 | * |
| 8619 | * {"first integer": 42, |
| 8620 | * "an array of two strings": ["string1", "string2"], |
| 8621 | * "map in a map": |
| 8622 | * { "bytes 1": h'78787878', |
| 8623 | * "bytes 2": h'79797979', |
| 8624 | * "another int": 98, |
| 8625 | * "text 2": "lies, damn lies and statistics" |
| 8626 | * } |
| 8627 | * } |
| 8628 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8629 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8630 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 8631 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 8632 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8633 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8634 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8635 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8636 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8637 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8638 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8639 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8640 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8641 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8642 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8643 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8644 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8645 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8646 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8647 | 0x73 |
| 8648 | }; |
| 8649 | |
| 8650 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8651 | |
| 8652 | /* As above, but the arrays are indefinite length */ |
| 8653 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 8654 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 8655 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8656 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8657 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8658 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8659 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8660 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8661 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8662 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8663 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8664 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8665 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8666 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8667 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8668 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8669 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8670 | 0x73, |
| 8671 | 0xff, 0xff |
| 8672 | }; |
| 8673 | #endif |
| 8674 | |
| 8675 | |
| 8676 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 8677 | |
| 8678 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8679 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 8680 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8681 | |
| 8682 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8683 | /* |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 8684 | * An array of one that contains a byte string that is an indefinite |
| 8685 | * length string that CBOR wraps an array of three numbers [42, 43, |
| 8686 | * 44]. The byte string is an implicit tag 24. |
| 8687 | * |
| 8688 | * [ |
| 8689 | * (_ h'83', h'18', h'2A182B', h'182C') |
| 8690 | * ] |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8691 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8692 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 8693 | 0x81, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8694 | 0x5f, |
| 8695 | 0x41, 0x83, |
| 8696 | 0x41, 0x18, |
| 8697 | 0x43, 0x2A, 0x18, 0x2B, |
| 8698 | 0x42, 0x18, 0x2C, |
| 8699 | 0xff |
| 8700 | }; |
| 8701 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8702 | |
| 8703 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8704 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8705 | { |
| 8706 | QCBORItem Item; |
| 8707 | QCBORError nCBORError; |
| 8708 | QCBORDecodeContext DCtx; |
| 8709 | |
| 8710 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8711 | |
| 8712 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8713 | return 100+(int32_t)nCBORError; |
| 8714 | } |
| 8715 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8716 | return 200; |
| 8717 | } |
| 8718 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8719 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8720 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 8721 | return 150+(int32_t)nCBORError; |
| 8722 | } |
| 8723 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8724 | return 250; |
| 8725 | } |
| 8726 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8727 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8728 | return (int32_t)nCBORError; |
| 8729 | } |
| 8730 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8731 | return 300; |
| 8732 | } |
| 8733 | |
| 8734 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8735 | return 400 + (int32_t)nCBORError; |
| 8736 | } |
| 8737 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8738 | return 500; |
| 8739 | } |
| 8740 | |
| 8741 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8742 | return (int32_t)nCBORError; |
| 8743 | } |
| 8744 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8745 | return 600; |
| 8746 | } |
| 8747 | |
| 8748 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8749 | return 900 + (int32_t)nCBORError; |
| 8750 | } |
| 8751 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8752 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8753 | Item.val.int64 != 42 || |
| 8754 | Item.uDataAlloc || |
| 8755 | Item.uLabelAlloc || |
| 8756 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8757 | return 1000; |
| 8758 | } |
| 8759 | |
| 8760 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8761 | return 1100 + (int32_t)nCBORError; |
| 8762 | } |
| 8763 | |
| 8764 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8765 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8766 | Item.val.int64 != 42 || |
| 8767 | Item.uDataAlloc || |
| 8768 | Item.uLabelAlloc || |
| 8769 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8770 | return 1200; |
| 8771 | } |
| 8772 | |
| 8773 | |
| 8774 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8775 | return 1300 + (int32_t)nCBORError; |
| 8776 | } |
| 8777 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8778 | Item.uDataAlloc || |
| 8779 | Item.uLabelAlloc || |
| 8780 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8781 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8782 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8783 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8784 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8785 | |
| 8786 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8787 | return 1500 + (int32_t)nCBORError; |
| 8788 | } |
| 8789 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8790 | Item.uDataAlloc || |
| 8791 | Item.uLabelAlloc || |
| 8792 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8793 | return 1600; |
| 8794 | } |
| 8795 | |
| 8796 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8797 | return 1700 + (int32_t)nCBORError; |
| 8798 | } |
| 8799 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8800 | Item.uDataAlloc || |
| 8801 | Item.uLabelAlloc || |
| 8802 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8803 | return 1800; |
| 8804 | } |
| 8805 | |
| 8806 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8807 | return (int32_t)nCBORError; |
| 8808 | } |
| 8809 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8810 | Item.uDataAlloc || |
| 8811 | Item.uLabelAlloc || |
| 8812 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8813 | return 1900; |
| 8814 | } |
| 8815 | |
| 8816 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8817 | return (int32_t)nCBORError; |
| 8818 | } |
| 8819 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8820 | Item.uDataAlloc || |
| 8821 | Item.uLabelAlloc || |
| 8822 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8823 | return 2000; |
| 8824 | } |
| 8825 | |
| 8826 | |
| 8827 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8828 | return 2100 + (int32_t)nCBORError; |
| 8829 | } |
| 8830 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8831 | Item.uDataAlloc || |
| 8832 | Item.uLabelAlloc || |
| 8833 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 8834 | Item.uDataType != QCBOR_TYPE_MAP || |
| 8835 | Item.val.uCount != 4) { |
| 8836 | return 2100; |
| 8837 | } |
| 8838 | |
| 8839 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8840 | return 2200 + (int32_t)nCBORError; |
| 8841 | } |
| 8842 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8843 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 8844 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8845 | Item.uDataAlloc || |
| 8846 | Item.uLabelAlloc || |
| 8847 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 8848 | return 2300; |
| 8849 | } |
| 8850 | |
| 8851 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8852 | return 2400 + (int32_t)nCBORError; |
| 8853 | } |
| 8854 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8855 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8856 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8857 | Item.uDataAlloc || |
| 8858 | Item.uLabelAlloc || |
| 8859 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8860 | return 2500; |
| 8861 | } |
| 8862 | |
| 8863 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8864 | return 2600 + (int32_t)nCBORError; |
| 8865 | } |
| 8866 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8867 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8868 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8869 | Item.uDataAlloc || |
| 8870 | Item.uLabelAlloc || |
| 8871 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8872 | return 2700; |
| 8873 | } |
| 8874 | |
| 8875 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8876 | return 2800 + (int32_t)nCBORError; |
| 8877 | } |
| 8878 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8879 | Item.uDataAlloc || |
| 8880 | Item.uLabelAlloc || |
| 8881 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 8882 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8883 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8884 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8885 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8886 | |
| 8887 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8888 | return 3000 + (int32_t)nCBORError; |
| 8889 | } |
| 8890 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8891 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8892 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8893 | Item.uDataAlloc || |
| 8894 | Item.uLabelAlloc || |
| 8895 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8896 | return 3100; |
| 8897 | } |
| 8898 | |
| 8899 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8900 | return 3200 + (int32_t)nCBORError; |
| 8901 | } |
| 8902 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8903 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8904 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8905 | Item.uDataAlloc || |
| 8906 | Item.uLabelAlloc || |
| 8907 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8908 | return 3300; |
| 8909 | } |
| 8910 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8911 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 8912 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8913 | return 3300 + (int32_t)nCBORError; |
| 8914 | } |
| 8915 | |
| 8916 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8917 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8918 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8919 | return 3400 + (int32_t)nCBORError; |
| 8920 | } |
| 8921 | |
| 8922 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8923 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8924 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8925 | return 3500 + (int32_t)nCBORError; |
| 8926 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8927 | |
| 8928 | |
| 8929 | // Rewind to top level after entering several maps |
| 8930 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8931 | |
| 8932 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8933 | return (int32_t)nCBORError; |
| 8934 | } |
| 8935 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8936 | Item.val.uCount != 3) { |
| 8937 | return 400; |
| 8938 | } |
| 8939 | |
| 8940 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8941 | return 4000+(int32_t)nCBORError; |
| 8942 | } |
| 8943 | |
| 8944 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8945 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8946 | Item.val.int64 != 42 || |
| 8947 | Item.uDataAlloc || |
| 8948 | Item.uLabelAlloc || |
| 8949 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8950 | return 4100; |
| 8951 | } |
| 8952 | |
| 8953 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8954 | return 4100+(int32_t)nCBORError; |
| 8955 | } |
| 8956 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8957 | Item.uDataAlloc || |
| 8958 | Item.uLabelAlloc || |
| 8959 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8960 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8961 | Item.val.uCount != 2) { |
| 8962 | return 4200; |
| 8963 | } |
| 8964 | |
| 8965 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8966 | return 4200+(int32_t)nCBORError; |
| 8967 | } |
| 8968 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8969 | Item.uDataAlloc || |
| 8970 | Item.uLabelAlloc || |
| 8971 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8972 | return 4300; |
| 8973 | } |
| 8974 | |
| 8975 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8976 | return 4300+(int32_t)nCBORError; |
| 8977 | } |
| 8978 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8979 | Item.uDataAlloc || |
| 8980 | Item.uLabelAlloc || |
| 8981 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8982 | return 4400; |
| 8983 | } |
| 8984 | |
| 8985 | QCBORDecode_Rewind(&DCtx); |
| 8986 | |
| 8987 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8988 | return 4400+(int32_t)nCBORError; |
| 8989 | } |
| 8990 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8991 | Item.val.uCount != 3) { |
| 8992 | return 4500; |
| 8993 | } |
| 8994 | |
| 8995 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8996 | return (int32_t)nCBORError; |
| 8997 | } |
| 8998 | |
| 8999 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9000 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9001 | Item.val.int64 != 42 || |
| 9002 | Item.uDataAlloc || |
| 9003 | Item.uLabelAlloc || |
| 9004 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9005 | return 4600; |
| 9006 | } |
| 9007 | |
| 9008 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9009 | return (int32_t)nCBORError; |
| 9010 | } |
| 9011 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9012 | Item.uDataAlloc || |
| 9013 | Item.uLabelAlloc || |
| 9014 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9015 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9016 | Item.val.uCount != 2) { |
| 9017 | return 4700; |
| 9018 | } |
| 9019 | |
| 9020 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9021 | return (int32_t)nCBORError; |
| 9022 | } |
| 9023 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9024 | Item.uDataAlloc || |
| 9025 | Item.uLabelAlloc || |
| 9026 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9027 | return 4800; |
| 9028 | } |
| 9029 | |
| 9030 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9031 | return 4900+(int32_t)nCBORError; |
| 9032 | } |
| 9033 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9034 | Item.uDataAlloc || |
| 9035 | Item.uLabelAlloc || |
| 9036 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9037 | return 5000; |
| 9038 | } |
| 9039 | |
| 9040 | |
| 9041 | // Rewind an entered map |
| 9042 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9043 | |
| 9044 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9045 | |
| 9046 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9047 | return 5100+(int32_t)nCBORError; |
| 9048 | } |
| 9049 | |
| 9050 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9051 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9052 | Item.val.int64 != 42 || |
| 9053 | Item.uDataAlloc || |
| 9054 | Item.uLabelAlloc || |
| 9055 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9056 | return 5200; |
| 9057 | } |
| 9058 | |
| 9059 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9060 | return 5200+(int32_t)nCBORError; |
| 9061 | } |
| 9062 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9063 | Item.uDataAlloc || |
| 9064 | Item.uLabelAlloc || |
| 9065 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9066 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9067 | Item.val.uCount != 2) { |
| 9068 | return -5300; |
| 9069 | } |
| 9070 | |
| 9071 | QCBORDecode_Rewind(&DCtx); |
| 9072 | |
| 9073 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9074 | return 5300+(int32_t)nCBORError; |
| 9075 | } |
| 9076 | |
| 9077 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9078 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9079 | Item.val.int64 != 42 || |
| 9080 | Item.uDataAlloc || |
| 9081 | Item.uLabelAlloc || |
| 9082 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9083 | return 5400; |
| 9084 | } |
| 9085 | |
| 9086 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9087 | return 5400+(int32_t)nCBORError; |
| 9088 | } |
| 9089 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9090 | Item.uDataAlloc || |
| 9091 | Item.uLabelAlloc || |
| 9092 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9093 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9094 | Item.val.uCount != 2) { |
| 9095 | return 5500; |
| 9096 | } |
| 9097 | |
| 9098 | |
| 9099 | // Rewind and entered array inside an entered map |
| 9100 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9101 | |
| 9102 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9103 | |
| 9104 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 9105 | |
| 9106 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9107 | return 5600+(int32_t)nCBORError; |
| 9108 | } |
| 9109 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9110 | Item.uDataAlloc || |
| 9111 | Item.uLabelAlloc || |
| 9112 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9113 | return 5700; |
| 9114 | } |
| 9115 | |
| 9116 | QCBORDecode_Rewind(&DCtx); |
| 9117 | |
| 9118 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9119 | return 5700+(int32_t)nCBORError; |
| 9120 | } |
| 9121 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9122 | Item.uDataAlloc || |
| 9123 | Item.uLabelAlloc || |
| 9124 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9125 | return 5800; |
| 9126 | } |
| 9127 | |
| 9128 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9129 | return (int32_t)nCBORError; |
| 9130 | } |
| 9131 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9132 | Item.uDataAlloc || |
| 9133 | Item.uLabelAlloc || |
| 9134 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9135 | return 5900; |
| 9136 | } |
| 9137 | |
| 9138 | QCBORDecode_Rewind(&DCtx); |
| 9139 | |
| 9140 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9141 | return 5900+(int32_t)nCBORError; |
| 9142 | } |
| 9143 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9144 | Item.uDataAlloc || |
| 9145 | Item.uLabelAlloc || |
| 9146 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9147 | return 6000; |
| 9148 | } |
| 9149 | |
| 9150 | |
| 9151 | // Rewind a byte string inside an array inside an array |
| 9152 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 9153 | |
| 9154 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9155 | |
| 9156 | uint64_t i; |
| 9157 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9158 | |
| 9159 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9160 | |
| 9161 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9162 | if(QCBORDecode_GetError(&DCtx)) { |
| 9163 | return 6100; |
| 9164 | } |
| 9165 | |
| 9166 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9167 | return (int32_t)nCBORError; |
| 9168 | } |
| 9169 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9170 | return 6200; |
| 9171 | } |
| 9172 | |
| 9173 | QCBORDecode_Rewind(&DCtx); |
| 9174 | |
| 9175 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9176 | return 6300+(int32_t)nCBORError; |
| 9177 | } |
| 9178 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9179 | return 6400; |
| 9180 | } |
| 9181 | |
| 9182 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9183 | // Rewind a byte string inside an indefinite-length array inside |
| 9184 | // indefinite-length array |
| 9185 | |
| 9186 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 9187 | |
| 9188 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9189 | |
| 9190 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9191 | |
| 9192 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9193 | |
| 9194 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9195 | if(QCBORDecode_GetError(&DCtx)) { |
| 9196 | return 6500; |
| 9197 | } |
| 9198 | |
| 9199 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9200 | return 6600+(int32_t)nCBORError; |
| 9201 | } |
| 9202 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9203 | return 6700; |
| 9204 | } |
| 9205 | |
| 9206 | QCBORDecode_Rewind(&DCtx); |
| 9207 | |
| 9208 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9209 | return 6800+(int32_t)nCBORError; |
| 9210 | } |
| 9211 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9212 | return 6900; |
| 9213 | } |
| 9214 | #endif |
| 9215 | |
| 9216 | // Rewind an empty map |
| 9217 | // [100, {}] |
| 9218 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 9219 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9220 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9221 | if(i != 100) { |
| 9222 | return 7010; |
| 9223 | } |
| 9224 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9225 | |
| 9226 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9227 | for(int n = 0; n < 5; n++) { |
| 9228 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9229 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9230 | return 7000 + n; |
| 9231 | } |
| 9232 | QCBORDecode_Rewind(&DCtx); |
| 9233 | } |
| 9234 | QCBORDecode_ExitMap(&DCtx); |
| 9235 | QCBORDecode_Rewind(&DCtx); |
| 9236 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9237 | if(i != 100) { |
| 9238 | return 7010; |
| 9239 | } |
| 9240 | QCBORDecode_ExitArray(&DCtx); |
| 9241 | QCBORDecode_Rewind(&DCtx); |
| 9242 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9243 | i = 9; |
| 9244 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9245 | if(i != 100) { |
| 9246 | return 7020; |
| 9247 | } |
| 9248 | if(QCBORDecode_GetError(&DCtx)){ |
| 9249 | return 7030; |
| 9250 | } |
| 9251 | |
| 9252 | // Rewind an empty indefinite length map |
| 9253 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9254 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 9255 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9256 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9257 | if(i != 100) { |
| 9258 | return 7810; |
| 9259 | } |
| 9260 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9261 | |
| 9262 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9263 | for(int n = 0; n < 5; n++) { |
| 9264 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9265 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9266 | return 7800 + n; |
| 9267 | } |
| 9268 | QCBORDecode_Rewind(&DCtx); |
| 9269 | } |
| 9270 | QCBORDecode_ExitMap(&DCtx); |
| 9271 | QCBORDecode_Rewind(&DCtx); |
| 9272 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9273 | if(i != 100) { |
| 9274 | return 7810; |
| 9275 | } |
| 9276 | QCBORDecode_ExitArray(&DCtx); |
| 9277 | QCBORDecode_Rewind(&DCtx); |
| 9278 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9279 | i = 9; |
| 9280 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9281 | if(i != 100) { |
| 9282 | return 7820; |
| 9283 | } |
| 9284 | if(QCBORDecode_GetError(&DCtx)){ |
| 9285 | return 7830; |
| 9286 | } |
| 9287 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9288 | |
| 9289 | // Rewind an indefnite length byte-string wrapped sequence |
| 9290 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 9291 | QCBORDecode_Init(&DCtx, |
| 9292 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 9293 | 0); |
| 9294 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 9295 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 9296 | |
| 9297 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9298 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9299 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9300 | return 7300; |
| 9301 | } |
| 9302 | |
| 9303 | /* |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9304 | Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on |
| 9305 | allocated strings. This is a fairly big job because of all the |
| 9306 | UsefulBuf internal book keeping that needs tweaking. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9307 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9308 | if(i != 42) { |
| 9309 | return 7110; |
| 9310 | } |
| 9311 | QCBORDecode_Rewind(&DCtx); |
| 9312 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9313 | if(i != 42) { |
| 9314 | return 7220; |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9315 | } |
| 9316 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 9317 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9318 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 9319 | |
| 9320 | |
| 9321 | // Rewind an indefnite length byte-string wrapped sequence |
| 9322 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9323 | return 0; |
| 9324 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9325 | |
| 9326 | |
| 9327 | |
| 9328 | |
| 9329 | static const uint8_t spBooleansInMap[] = |
| 9330 | { |
| 9331 | 0xa1, 0x08, 0xf5 |
| 9332 | }; |
| 9333 | |
| 9334 | static const uint8_t spBooleansInMapWrongType[] = |
| 9335 | { |
| 9336 | 0xa1, 0x08, 0xf6 |
| 9337 | }; |
| 9338 | |
| 9339 | static const uint8_t spBooleansInMapNWF[] = |
| 9340 | { |
| 9341 | 0xa1, 0x08, 0x1a |
| 9342 | }; |
| 9343 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9344 | static const uint8_t spNullInMap[] = |
| 9345 | { |
| 9346 | 0xa1, 0x08, 0xf6 |
| 9347 | }; |
| 9348 | |
| 9349 | static const uint8_t spUndefinedInMap[] = |
| 9350 | { |
| 9351 | 0xa1, 0x08, 0xf7 |
| 9352 | }; |
| 9353 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9354 | |
| 9355 | int32_t BoolTest(void) |
| 9356 | { |
| 9357 | QCBORDecodeContext DCtx; |
| 9358 | bool b; |
| 9359 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9360 | QCBORDecode_Init(&DCtx, |
| 9361 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9362 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9363 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9364 | QCBORDecode_GetBool(&DCtx, &b); |
| 9365 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9366 | return 1; |
| 9367 | } |
| 9368 | |
| 9369 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 9370 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9371 | return 2; |
| 9372 | } |
| 9373 | |
| 9374 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 9375 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9376 | return 3; |
| 9377 | } |
| 9378 | |
| 9379 | |
| 9380 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 9381 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9382 | return 4; |
| 9383 | } |
| 9384 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9385 | QCBORDecode_Init(&DCtx, |
| 9386 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 9387 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9388 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9389 | QCBORDecode_GetBool(&DCtx, &b); |
| 9390 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9391 | return 5; |
| 9392 | } |
| 9393 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9394 | QCBORDecode_Init(&DCtx, |
| 9395 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9396 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9397 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9398 | QCBORDecode_GetBool(&DCtx, &b); |
| 9399 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9400 | return 6; |
| 9401 | } |
| 9402 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9403 | |
| 9404 | QCBORDecode_Init(&DCtx, |
| 9405 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9406 | 0); |
| 9407 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9408 | QCBORDecode_GetNull(&DCtx); |
| 9409 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9410 | return 7; |
| 9411 | } |
| 9412 | |
| 9413 | QCBORDecode_Init(&DCtx, |
| 9414 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9415 | 0); |
| 9416 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9417 | QCBORDecode_GetNull(&DCtx); |
| 9418 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9419 | return 8; |
| 9420 | } |
| 9421 | |
| 9422 | QCBORDecode_Init(&DCtx, |
| 9423 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9424 | 0); |
| 9425 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9426 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9427 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9428 | return 9; |
| 9429 | } |
| 9430 | |
| 9431 | QCBORDecode_Init(&DCtx, |
| 9432 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9433 | 0); |
| 9434 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9435 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9436 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9437 | return 10; |
| 9438 | } |
| 9439 | |
| 9440 | QCBORDecode_Init(&DCtx, |
| 9441 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9442 | 0); |
| 9443 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9444 | QCBORDecode_GetUndefined(&DCtx); |
| 9445 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9446 | return 11; |
| 9447 | } |
| 9448 | |
| 9449 | QCBORDecode_Init(&DCtx, |
| 9450 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9451 | 0); |
| 9452 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9453 | QCBORDecode_GetUndefined(&DCtx); |
| 9454 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9455 | return 12; |
| 9456 | } |
| 9457 | |
| 9458 | QCBORDecode_Init(&DCtx, |
| 9459 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9460 | 0); |
| 9461 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9462 | QCBORDecode_GetUndefined(&DCtx); |
| 9463 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9464 | return 13; |
| 9465 | } |
| 9466 | |
| 9467 | QCBORDecode_Init(&DCtx, |
| 9468 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9469 | 0); |
| 9470 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9471 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9472 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9473 | return 14; |
| 9474 | } |
| 9475 | |
| 9476 | QCBORDecode_Init(&DCtx, |
| 9477 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9478 | 0); |
| 9479 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9480 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9481 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9482 | return 15; |
| 9483 | } |
| 9484 | |
| 9485 | QCBORDecode_Init(&DCtx, |
| 9486 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9487 | 0); |
| 9488 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9489 | QCBORDecode_GetUndefined(&DCtx); |
| 9490 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9491 | return 15; |
| 9492 | } |
| 9493 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9494 | return 0; |
| 9495 | } |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9496 | |
| 9497 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9498 | |
| 9499 | static const uint8_t spExpectedArray2s[] = { |
| 9500 | 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9501 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9502 | 0x32}; |
| 9503 | |
| 9504 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9505 | static const uint8_t spExpectedArray2sIndef[] = { |
| 9506 | 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9507 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9508 | 0x32, 0xff}; |
| 9509 | #endif |
| 9510 | |
| 9511 | static const uint8_t spExpectedMap4[] = { |
| 9512 | 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9513 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9514 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9515 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9516 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9517 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9518 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9519 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9520 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9521 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73}; |
| 9522 | |
| 9523 | |
| 9524 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9525 | |
| 9526 | static const uint8_t spExpectedMap4Indef[] = { |
| 9527 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9528 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9529 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9530 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9531 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9532 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9533 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9534 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9535 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9536 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 9537 | 0xff}; |
| 9538 | |
| 9539 | /* |
| 9540 | * [[[[[0, []]]]], 0] |
| 9541 | */ |
| 9542 | static const uint8_t spDefAndIndef[] = { |
| 9543 | 0x82, |
| 9544 | 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00 |
| 9545 | }; |
| 9546 | #endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9547 | |
| 9548 | |
| 9549 | #ifndef QCBOR_DISABLE_TAGS |
| 9550 | /* An exp / mant tag in two nested arrays */ |
| 9551 | static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03}; |
| 9552 | #endif /* !QCBOR_DISABLE_TAGS */ |
| 9553 | |
| 9554 | |
| 9555 | int32_t GetMapAndArrayTest(void) |
| 9556 | { |
| 9557 | QCBORDecodeContext DCtx; |
| 9558 | size_t uPosition ; |
| 9559 | QCBORItem Item; |
| 9560 | UsefulBufC ReturnedEncodedCBOR; |
| 9561 | |
| 9562 | |
| 9563 | QCBORDecode_Init(&DCtx, |
| 9564 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9565 | 0); |
| 9566 | |
| 9567 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9568 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9569 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9570 | if(QCBORDecode_GetError(&DCtx)) { |
| 9571 | return 1; |
| 9572 | } |
| 9573 | if(Item.val.uCount != 2) { |
| 9574 | return 2; |
| 9575 | } |
| 9576 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9577 | return 3; |
| 9578 | } |
| 9579 | |
| 9580 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9581 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9582 | return 4; |
| 9583 | } |
| 9584 | |
| 9585 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9586 | |
| 9587 | |
| 9588 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9589 | if(QCBORDecode_GetError(&DCtx)) { |
| 9590 | return 10; |
| 9591 | } |
| 9592 | if(Item.val.uCount != 4) { |
| 9593 | return 11; |
| 9594 | } |
| 9595 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9596 | return 12; |
| 9597 | } |
| 9598 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9599 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 9600 | "an array of two strings", |
| 9601 | &Item, |
| 9602 | &ReturnedEncodedCBOR); |
| 9603 | if(QCBORDecode_GetError(&DCtx)) { |
| 9604 | return 20; |
| 9605 | } |
| 9606 | if(Item.val.uCount != 2) { |
| 9607 | return 21; |
| 9608 | } |
| 9609 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9610 | return 22; |
| 9611 | } |
| 9612 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9613 | return 23; |
| 9614 | } |
| 9615 | |
| 9616 | QCBORDecode_Rewind(&DCtx); |
| 9617 | |
| 9618 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9619 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9620 | if(QCBORDecode_GetError(&DCtx)) { |
| 9621 | return 30; |
| 9622 | } |
| 9623 | if(Item.val.uCount != 4) { |
| 9624 | return 31; |
| 9625 | } |
| 9626 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9627 | return 32; |
| 9628 | } |
| 9629 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9630 | return 33; |
| 9631 | } |
| 9632 | |
| 9633 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9634 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9635 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9636 | return 40; |
| 9637 | } |
| 9638 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 9639 | return 41; |
| 9640 | } |
| 9641 | QCBORDecode_GetAndResetError(&DCtx); |
| 9642 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9643 | return 42; |
| 9644 | } |
| 9645 | |
| 9646 | |
| 9647 | #ifndef QCBOR_DISABLE_TAGS |
| 9648 | UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant); |
| 9649 | QCBORDecode_Init(&DCtx, ExpMant, 0); |
| 9650 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9651 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9652 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9653 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9654 | return 200; |
| 9655 | } |
| 9656 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 9657 | return 201; |
| 9658 | } |
| 9659 | if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) { |
| 9660 | return 202; |
| 9661 | } |
| 9662 | if(Item.val.uCount != 2) { |
| 9663 | return 201; |
| 9664 | } |
| 9665 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) { |
| 9666 | return 205; |
| 9667 | } |
| 9668 | #endif /* !QCBOR_DISABLE_TAGS */ |
| 9669 | |
| 9670 | |
| 9671 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9672 | |
| 9673 | UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef); |
| 9674 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9675 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9676 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9677 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9678 | return 50; |
| 9679 | } |
| 9680 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) { |
| 9681 | return 51; |
| 9682 | } |
| 9683 | |
| 9684 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9685 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9686 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9687 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9688 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9689 | return 52; |
| 9690 | } |
| 9691 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) { |
| 9692 | return 53; |
| 9693 | } |
| 9694 | |
| 9695 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9696 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9697 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9698 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9699 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9700 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9701 | return 54; |
| 9702 | } |
| 9703 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) { |
| 9704 | return 55; |
| 9705 | } |
| 9706 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9707 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9708 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9709 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9710 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9711 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9712 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9713 | return 56; |
| 9714 | } |
| 9715 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) { |
| 9716 | return 57; |
| 9717 | } |
| 9718 | |
| 9719 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9720 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9721 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9722 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9723 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9724 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9725 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9726 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9727 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9728 | return 58; |
| 9729 | } |
| 9730 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) { |
| 9731 | return 59; |
| 9732 | } |
| 9733 | |
| 9734 | |
| 9735 | QCBORDecode_Init(&DCtx, |
| 9736 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 9737 | 0); |
| 9738 | |
| 9739 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9740 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9741 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9742 | if(QCBORDecode_GetError(&DCtx)) { |
| 9743 | return 60; |
| 9744 | } |
| 9745 | if(Item.val.uCount != UINT16_MAX) { |
| 9746 | return 61; |
| 9747 | } |
| 9748 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 9749 | return 62; |
| 9750 | } |
| 9751 | |
| 9752 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9753 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9754 | return 63; |
| 9755 | } |
| 9756 | |
| 9757 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9758 | |
| 9759 | |
| 9760 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9761 | if(QCBORDecode_GetError(&DCtx)) { |
| 9762 | return 70; |
| 9763 | } |
| 9764 | if(Item.val.uCount != UINT16_MAX) { |
| 9765 | return 71; |
| 9766 | } |
| 9767 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 9768 | return 72; |
| 9769 | } |
| 9770 | |
| 9771 | |
| 9772 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9773 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 9774 | "an array of two strings", |
| 9775 | &Item, |
| 9776 | &ReturnedEncodedCBOR); |
| 9777 | if(QCBORDecode_GetError(&DCtx)) { |
| 9778 | return 80; |
| 9779 | } |
| 9780 | if(Item.val.uCount != UINT16_MAX) { |
| 9781 | return 81; |
| 9782 | } |
| 9783 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 9784 | return 82; |
| 9785 | } |
| 9786 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9787 | return 83; |
| 9788 | } |
| 9789 | |
| 9790 | QCBORDecode_Rewind(&DCtx); |
| 9791 | |
| 9792 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9793 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9794 | if(QCBORDecode_GetError(&DCtx)) { |
| 9795 | return 90; |
| 9796 | } |
| 9797 | if(Item.val.uCount != UINT16_MAX) { |
| 9798 | return 91; |
| 9799 | } |
| 9800 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 9801 | return 92; |
| 9802 | } |
| 9803 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9804 | return 93; |
| 9805 | } |
| 9806 | |
| 9807 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9808 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9809 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9810 | return 100; |
| 9811 | } |
| 9812 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 9813 | return 101; |
| 9814 | } |
| 9815 | QCBORDecode_GetAndResetError(&DCtx); |
| 9816 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9817 | return 102; |
| 9818 | } |
| 9819 | #endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9820 | |
| 9821 | return 0; |
| 9822 | } |
| 9823 | |
| 9824 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9825 | int32_t |
| 9826 | ErrorHandlingTests(void) |
| 9827 | { |
| 9828 | QCBORDecodeContext DCtx; |
| 9829 | QCBORItem Item; |
| 9830 | QCBORError uError; |
| 9831 | int64_t integer; |
| 9832 | |
| 9833 | /* Test QCBORDecode_SetError() */ |
| 9834 | QCBORDecode_Init(&DCtx, |
| 9835 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9836 | QCBOR_DECODE_MODE_NORMAL); |
| 9837 | |
| 9838 | QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED); |
| 9839 | |
| 9840 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9841 | |
| 9842 | uError = QCBORDecode_GetError(&DCtx); |
| 9843 | |
| 9844 | if(uError != QCBOR_ERR_FIRST_USER_DEFINED) { |
| 9845 | return -1; |
| 9846 | } |
| 9847 | |
| 9848 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 9849 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 9850 | return -2; |
| 9851 | } |
| 9852 | |
| 9853 | |
| 9854 | /* Test data type returned from previous error */ |
| 9855 | QCBORDecode_Init(&DCtx, |
| 9856 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9857 | QCBOR_DECODE_MODE_NORMAL); |
| 9858 | QCBORDecode_GetInt64(&DCtx, &integer); |
| 9859 | uError = QCBORDecode_GetError(&DCtx); |
| 9860 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9861 | return -3; |
| 9862 | } |
| 9863 | |
| 9864 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9865 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 9866 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 9867 | return -2; |
| 9868 | } |
| 9869 | uError = QCBORDecode_GetError(&DCtx); |
| 9870 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9871 | return -3; |
| 9872 | } |
| 9873 | |
| 9874 | |
| 9875 | /* Test error classification functions */ |
| 9876 | |
| 9877 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 9878 | return -10; |
| 9879 | } |
| 9880 | if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) { |
| 9881 | return -11; |
| 9882 | } |
| 9883 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 9884 | return -12; |
| 9885 | } |
| 9886 | if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) { |
| 9887 | return -13; |
| 9888 | } |
| 9889 | |
| 9890 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) { |
| 9891 | return -20; |
| 9892 | } |
| 9893 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) { |
| 9894 | return -21; |
| 9895 | } |
| 9896 | if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) { |
| 9897 | return -22; |
| 9898 | } |
| 9899 | if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) { |
| 9900 | return -23; |
| 9901 | } |
| 9902 | |
| 9903 | return 0; |
| 9904 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 9905 | |
| 9906 | |
| 9907 | int32_t TellTests(void) |
| 9908 | { |
| 9909 | QCBORDecodeContext DCtx; |
| 9910 | QCBORItem Item; |
| 9911 | uint32_t uPosition; |
| 9912 | int nIndex; |
| 9913 | int64_t nDecodedInt; |
| 9914 | |
| 9915 | static const uint32_t aPos[] = |
| 9916 | {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, UINT32_MAX}; |
| 9917 | QCBORDecode_Init(&DCtx, |
| 9918 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9919 | 0); |
| 9920 | for(nIndex = 0; ; nIndex++) { |
| 9921 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9922 | if(uPosition != aPos[nIndex]) { |
| 9923 | return nIndex; |
| 9924 | } |
| 9925 | |
| 9926 | if(uPosition == UINT32_MAX) { |
| 9927 | break; |
| 9928 | } |
| 9929 | |
| 9930 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9931 | } |
| 9932 | |
| 9933 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9934 | static const uint32_t aPosIndef[] = |
| 9935 | {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, UINT32_MAX}; |
| 9936 | QCBORDecode_Init(&DCtx, |
| 9937 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 9938 | 0); |
| 9939 | |
| 9940 | for(nIndex = 0; ; nIndex++) { |
| 9941 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9942 | if(uPosition != aPosIndef[nIndex]) { |
| 9943 | return nIndex + 100; |
| 9944 | } |
| 9945 | |
| 9946 | if(uPosition == UINT32_MAX) { |
| 9947 | break; |
| 9948 | } |
| 9949 | |
| 9950 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9951 | } |
| 9952 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9953 | |
| 9954 | /* Next, some tests with entered maps and arrays */ |
| 9955 | QCBORDecode_Init(&DCtx, |
| 9956 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9957 | 0); |
| 9958 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 9959 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 9960 | return 1001; |
| 9961 | } |
| 9962 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 9963 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 9964 | return 1002; |
| 9965 | } |
| 9966 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 9967 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 9968 | return 1003; |
| 9969 | } |
| 9970 | |
| 9971 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 9972 | if(nDecodedInt != 98) { |
| 9973 | return 1004; |
| 9974 | } |
| 9975 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 9976 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 9977 | return 1005; |
| 9978 | } |
| 9979 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9980 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 9981 | return 1006; |
| 9982 | } |
| 9983 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 9984 | if(nDecodedInt != 98) { |
| 9985 | return 1007; |
| 9986 | } |
| 9987 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 9988 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 9989 | return 1008; |
| 9990 | } |
| 9991 | |
| 9992 | QCBORDecode_ExitMap(&DCtx); |
| 9993 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 9994 | return 1009; |
| 9995 | } |
| 9996 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9997 | return 1010; |
| 9998 | } |
| 9999 | |
| 10000 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10001 | /* Next, some tests with entered maps and arrays */ |
| 10002 | QCBORDecode_Init(&DCtx, |
| 10003 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10004 | 0); |
| 10005 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10006 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10007 | return 2000; |
| 10008 | } |
| 10009 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10010 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10011 | return 2001; |
| 10012 | } |
| 10013 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10014 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10015 | return 2002; |
| 10016 | } |
| 10017 | |
| 10018 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10019 | if(nDecodedInt != 98) { |
| 10020 | return 2003; |
| 10021 | } |
| 10022 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10023 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10024 | return 2004; |
| 10025 | } |
| 10026 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10027 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10028 | return 2005; |
| 10029 | } |
| 10030 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10031 | if(nDecodedInt != 98) { |
| 10032 | return 2006; |
| 10033 | } |
| 10034 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10035 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10036 | return 2007; |
| 10037 | } |
| 10038 | |
| 10039 | QCBORDecode_ExitMap(&DCtx); |
| 10040 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10041 | return 2008; |
| 10042 | } |
| 10043 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10044 | return 2010; |
| 10045 | } |
| 10046 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10047 | |
| 10048 | |
| 10049 | |
| 10050 | /* Error state test */ |
| 10051 | QCBORDecode_Init(&DCtx, |
| 10052 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10053 | 0); |
| 10054 | /* Cause an error */ |
| 10055 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10056 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10057 | return 3000; |
| 10058 | } |
| 10059 | |
| 10060 | /* Empties tests */ |
| 10061 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 10062 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 10063 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10064 | return 4000; |
| 10065 | } |
| 10066 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10067 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10068 | return 4001; |
| 10069 | } |
| 10070 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10071 | return 4002; |
| 10072 | } |
| 10073 | QCBORDecode_ExitMap(&DCtx); |
| 10074 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10075 | return 4001; |
| 10076 | } |
| 10077 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10078 | return 4002; |
| 10079 | } |
| 10080 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10081 | return 4010; |
| 10082 | } |
| 10083 | |
| 10084 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10085 | const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map |
| 10086 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0); |
| 10087 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10088 | return 4100; |
| 10089 | } |
| 10090 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10091 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10092 | return 4101; |
| 10093 | } |
| 10094 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10095 | return 4102; |
| 10096 | } |
| 10097 | QCBORDecode_ExitMap(&DCtx); |
| 10098 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10099 | return 4101; |
| 10100 | } |
| 10101 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10102 | return 4102; |
| 10103 | } |
| 10104 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10105 | return 4110; |
| 10106 | } |
| 10107 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10108 | |
| 10109 | /* Test on a CBOR sequence */ |
| 10110 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0); |
| 10111 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10112 | return 5000; |
| 10113 | } |
| 10114 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10115 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10116 | return 5001; |
| 10117 | } |
| 10118 | if(QCBORDecode_Tell(&DCtx) != 11) { |
| 10119 | return 5002; |
| 10120 | } |
| 10121 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10122 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10123 | return 5003; |
| 10124 | } |
| 10125 | if(QCBORDecode_Tell(&DCtx) != 12) { |
| 10126 | return 5004; |
| 10127 | } |
| 10128 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10129 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10130 | return 5005; |
| 10131 | } |
| 10132 | if(QCBORDecode_Tell(&DCtx) != 17) { |
| 10133 | return 5006; |
| 10134 | } |
| 10135 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10136 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10137 | return 5007; |
| 10138 | } |
| 10139 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10140 | return 5008; |
| 10141 | } |
| 10142 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10143 | return 5010; |
| 10144 | } |
| 10145 | |
| 10146 | |
| 10147 | QCBORDecode_Init(&DCtx, |
| 10148 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10149 | 0); |
| 10150 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10151 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 10152 | if(QCBORDecode_Tell(&DCtx) != 42) { |
| 10153 | return 6001; |
| 10154 | } |
| 10155 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10156 | if(QCBORDecode_Tell(&DCtx) != 50) { |
| 10157 | return 6002; |
| 10158 | } |
| 10159 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10160 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10161 | return 6008; |
| 10162 | } |
| 10163 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10164 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 10165 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10166 | return 6003; |
| 10167 | } |
| 10168 | QCBORDecode_ExitArray(&DCtx); |
| 10169 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10170 | return 6004; |
| 10171 | } |
| 10172 | |
| 10173 | static const uint32_t aEmptiesPos[] = |
| 10174 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, UINT32_MAX}; |
| 10175 | QCBORDecode_Init(&DCtx, |
| 10176 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 10177 | 0); |
| 10178 | for(nIndex = 0; ; nIndex++) { |
| 10179 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10180 | if(uPosition != aEmptiesPos[nIndex]) { |
| 10181 | return nIndex + 200; |
| 10182 | } |
| 10183 | |
| 10184 | if(uPosition == UINT32_MAX) { |
| 10185 | break; |
| 10186 | } |
| 10187 | |
| 10188 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10189 | } |
| 10190 | |
| 10191 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10192 | static const uint32_t aIndefEmptiesPos[] = |
| 10193 | {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, UINT32_MAX}; |
| 10194 | QCBORDecode_Init(&DCtx, |
| 10195 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 10196 | 0); |
| 10197 | for(nIndex = 0; ; nIndex++) { |
| 10198 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10199 | if(uPosition != aIndefEmptiesPos[nIndex]) { |
| 10200 | return nIndex + 300; |
| 10201 | } |
| 10202 | |
| 10203 | if(uPosition == UINT32_MAX) { |
| 10204 | break; |
| 10205 | } |
| 10206 | |
| 10207 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10208 | } |
| 10209 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10210 | |
| 10211 | |
| 10212 | return 0; |
| 10213 | } |