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 | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 135 | 0x98, 0x31, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 136 | 0xff, 0xff, 0xff, 0x3b, 0xFf, 0xff, 0xff, 0xff, 0xff, |
| 137 | 0xff, 0xff, 0xfe, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 138 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 139 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 140 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 141 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 142 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 143 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 144 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 145 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 146 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 147 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 148 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 149 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 150 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 151 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 152 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 153 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 154 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 155 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 156 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 157 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 158 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 159 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 160 | 0xff, 0xff}; |
| 161 | |
| 162 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 163 | // return CBOR error or -1 if type of value doesn't match |
| 164 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 165 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 166 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 167 | QCBORItem Item; |
| 168 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 169 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 170 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 171 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 172 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 173 | return -1; |
| 174 | |
| 175 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 176 | return (int32_t)nCBORError; |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 177 | if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT || |
| 178 | Item.val.uint64 != 18446744073709551615ULL) |
| 179 | return -1; |
| 180 | |
| 181 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 182 | return (int32_t)nCBORError; |
| 183 | if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT || |
| 184 | Item.val.uint64 != 18446744073709551614ULL) |
| 185 | return -1; |
| 186 | |
| 187 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 188 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 189 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 190 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 191 | return -1; |
| 192 | |
| 193 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 194 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 195 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 196 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 197 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 198 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 199 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 200 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 202 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 203 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 204 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 205 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 206 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 207 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 208 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 209 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 210 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 211 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 212 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 213 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 214 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 215 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 216 | |
| 217 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 218 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 219 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 220 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 221 | Item.val.int64 != -2147483648) |
| 222 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 223 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 224 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 225 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 226 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 227 | Item.val.int64 != -2147483647) |
| 228 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 229 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 230 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 231 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 232 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 233 | Item.val.int64 != -65538) |
| 234 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 235 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 236 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 237 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 238 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 239 | Item.val.int64 != -65537) |
| 240 | return -1; |
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 != -65536) |
| 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 != -65535) |
| 253 | return -1; |
| 254 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 255 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 256 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 257 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 258 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 259 | Item.val.int64 != -65534) |
| 260 | return -1; |
| 261 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 262 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 263 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 264 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 265 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 266 | Item.val.int64 != -257) |
| 267 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 268 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 269 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 270 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 271 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 272 | Item.val.int64 != -256) |
| 273 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 274 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 275 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 276 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 277 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 278 | Item.val.int64 != -255) |
| 279 | return -1; |
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 != -254) |
| 285 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 286 | |
| 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 != -25) |
| 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 != -24) |
| 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 != -23) |
| 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 != -1) |
| 313 | return -1; |
| 314 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 315 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 316 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 317 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 318 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 319 | Item.val.int64 != 0) |
| 320 | return -1; |
| 321 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 322 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 323 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 324 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 325 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 326 | Item.val.int64 != 0) |
| 327 | return -1; |
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 != 1) |
| 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 != 22) |
| 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 != 23) |
| 347 | return -1; |
| 348 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 349 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 350 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 351 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 352 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 353 | Item.val.int64 != 24) |
| 354 | return -1; |
| 355 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 356 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 357 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 358 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 359 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 360 | Item.val.int64 != 25) |
| 361 | return -1; |
| 362 | |
| 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 != 26) |
| 367 | return -1; |
| 368 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 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 != 254) |
| 374 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 375 | |
| 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 != 255) |
| 381 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 382 | |
| 383 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 384 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 385 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 386 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 387 | Item.val.int64 != 256) |
| 388 | return -1; |
| 389 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 390 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 391 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 392 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 393 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 394 | Item.val.int64 != 257) |
| 395 | return -1; |
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 != 65534) |
| 401 | return -1; |
| 402 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 403 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 404 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 405 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 406 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 407 | Item.val.int64 != 65535) |
| 408 | return -1; |
| 409 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 410 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 411 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 412 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 413 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 414 | Item.val.int64 != 65536) |
| 415 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 416 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 417 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 418 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 419 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 420 | Item.val.int64 != 65537) |
| 421 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 422 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 423 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 424 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 425 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 426 | Item.val.int64 != 65538) |
| 427 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 428 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 429 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 430 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 431 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 432 | Item.val.int64 != 2147483647) |
| 433 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 434 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 435 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 436 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 437 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 438 | Item.val.int64 != 2147483647) |
| 439 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 440 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 441 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 442 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 443 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 444 | Item.val.int64 != 2147483648) |
| 445 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 446 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 447 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 448 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 449 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 450 | Item.val.int64 != 2147483649) |
| 451 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 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 != 4294967294) |
| 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 != 4294967295) |
| 464 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 465 | |
| 466 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 467 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 468 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 469 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 470 | Item.val.int64 != 4294967296) |
| 471 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 472 | |
| 473 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 474 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 475 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 476 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 477 | Item.val.int64 != 4294967297) |
| 478 | return -1; |
| 479 | |
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(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 483 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 484 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 485 | Item.val.int64 != 9223372036854775807LL) |
| 486 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 487 | |
| 488 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 489 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 490 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 491 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 492 | Item.val.uint64 != 18446744073709551615ULL) |
| 493 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 494 | |
| 495 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 496 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 497 | return -1; |
| 498 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 499 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 504 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 505 | /* |
| 506 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 507 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 508 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 509 | int32_t IntegerValuesParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 510 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 511 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 512 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 513 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 514 | QCBORDecode_Init(&DCtx, |
| 515 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 516 | QCBOR_DECODE_MODE_NORMAL); |
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 really big test of all successes |
| 519 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 520 | if(nReturn) { |
| 521 | return nReturn; |
| 522 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 523 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 524 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
| 528 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 529 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 530 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 531 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 532 | 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] | 533 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 534 | */ |
| 535 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 536 | static uint8_t spSimpleArrayBuffer[50]; |
| 537 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 538 | 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] | 539 | { |
| 540 | QCBOREncodeContext ECtx; |
| 541 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 542 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 543 | *pEncoded = NULL; |
| 544 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 545 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 546 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 547 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 548 | // and last with the buffer to do the actual encoding |
| 549 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 550 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 551 | QCBOREncode_OpenArray(&ECtx); |
| 552 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 553 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 554 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 555 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 556 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 557 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 558 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 559 | goto Done; |
| 560 | |
| 561 | if(*pEncoded != NULL) { |
| 562 | nReturn = 0; |
| 563 | goto Done; |
| 564 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 565 | |
| 566 | // Use static buffer to avoid dependency on malloc() |
| 567 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 568 | goto Done; |
| 569 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 570 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 571 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 572 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 573 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 574 | Done: |
| 575 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 579 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 580 | Some basic CBOR with map and array used in a lot of tests. |
| 581 | The map labels are all strings |
| 582 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 583 | { |
| 584 | "first integer": 42, |
| 585 | "an array of two strings": [ |
| 586 | "string1", "string2" |
| 587 | ], |
| 588 | "map in a map": { |
| 589 | "bytes 1": h'78787878', |
| 590 | "bytes 2": h'79797979', |
| 591 | "another int": 98, |
| 592 | "text 2": "lies, damn lies and statistics" |
| 593 | } |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 594 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 595 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 596 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 597 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 598 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 599 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 600 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 601 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 602 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 603 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 604 | 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, |
| 605 | 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4, |
| 606 | 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 607 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, |
| 608 | 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, |
| 609 | 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68, |
| 610 | 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, |
| 611 | 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 612 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, |
| 613 | 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, |
| 614 | 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, |
| 615 | 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 616 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 617 | |
| 618 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 619 | // Same as above, but with indefinite lengths. |
| 620 | static const uint8_t pValidMapIndefEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 621 | 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 622 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 623 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 624 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 625 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 626 | 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 627 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 628 | 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20, |
| 629 | 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, |
| 630 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 631 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 632 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 633 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 634 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 635 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 636 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 637 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 638 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 639 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 640 | 0xff, 0xff}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 641 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 642 | |
| 643 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 644 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 645 | size_t nLen, |
| 646 | int64_t *pInt1, |
| 647 | int64_t *pInt2, |
| 648 | const uint8_t **pBuf3, |
| 649 | size_t *pBuf3Len, |
| 650 | const uint8_t **pBuf4, |
| 651 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 652 | { |
| 653 | QCBORDecodeContext DCtx; |
| 654 | QCBORItem Item; |
| 655 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 656 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 657 | QCBORDecode_Init(&DCtx, |
| 658 | (UsefulBufC){pEncoded, nLen}, |
| 659 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 660 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 661 | // Make sure the first thing is a map |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 662 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 663 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 664 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 665 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 666 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 667 | // First integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 668 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 669 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 670 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 671 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 672 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 673 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 674 | // Second integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 675 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 676 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 677 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 678 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 679 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 680 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 681 | // First string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 682 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 683 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 684 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 685 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 686 | *pBuf3 = Item.val.string.ptr; |
| 687 | *pBuf3Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 688 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 689 | // Second string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 690 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 691 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 692 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 693 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 694 | *pBuf4 = Item.val.string.ptr; |
| 695 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 696 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 697 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 698 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 699 | Done: |
| 700 | return(nReturn); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 705 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 706 | int32_t SimpleArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 707 | { |
| 708 | uint8_t *pEncoded; |
| 709 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 710 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 711 | int64_t i1=0, i2=0; |
| 712 | size_t i3=0, i4=0; |
| 713 | const uint8_t *s3= (uint8_t *)""; |
| 714 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 715 | |
| 716 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 717 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 718 | return(-1); |
| 719 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 720 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 721 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 722 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 723 | if(i1 != 23 || |
| 724 | i2 != 6000 || |
| 725 | i3 != 8 || |
| 726 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 727 | memcmp("galactic", s3, 8) !=0 || |
| 728 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 729 | return(-1); |
| 730 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 731 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 732 | return(0); |
| 733 | } |
| 734 | |
| 735 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 736 | /* |
| 737 | [ |
| 738 | 0, |
| 739 | [], |
| 740 | [ |
| 741 | [], |
| 742 | [ |
| 743 | 0 |
| 744 | ], |
| 745 | {}, |
| 746 | { |
| 747 | 1: {}, |
| 748 | 2: {}, |
| 749 | 3: [] |
| 750 | } |
| 751 | ] |
| 752 | ] |
| 753 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 754 | static uint8_t sEmpties[] = { |
| 755 | 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 756 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 757 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 758 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 759 | /* Same as above, but with indefinte lengths */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 760 | static const uint8_t sEmptiesIndef[] = { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 761 | 0x9F, |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 762 | 0x00, |
| 763 | 0x9F, |
| 764 | 0xFF, |
| 765 | 0x9F, |
| 766 | 0x9F, |
| 767 | 0xFF, |
| 768 | 0x9F, |
| 769 | 0x00, |
| 770 | 0xFF, |
| 771 | 0xBF, |
| 772 | 0xFF, |
| 773 | 0xBF, |
| 774 | 0x01, |
| 775 | 0xBF, |
| 776 | 0xFF, |
| 777 | 0x02, |
| 778 | 0xBF, |
| 779 | 0xFF, |
| 780 | 0x03, |
| 781 | 0x9F, |
| 782 | 0xFF, |
| 783 | 0xFF, |
| 784 | 0xFF, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 785 | 0xFF}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 786 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 787 | |
| 788 | |
| 789 | static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts) |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 790 | { |
| 791 | QCBORDecodeContext DCtx; |
| 792 | QCBORItem Item; |
| 793 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 794 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 795 | input, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 796 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 797 | |
| 798 | // Array with 3 items |
| 799 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 800 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 801 | Item.uNestingLevel != 0 || |
| 802 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 803 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | // An integer 0 |
| 808 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 809 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 810 | Item.uNestingLevel != 1 || |
| 811 | Item.uNextNestLevel != 1 || |
| 812 | Item.val.uint64 != 0) { |
| 813 | return -2; |
| 814 | } |
| 815 | |
| 816 | // An empty array |
| 817 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 818 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 819 | Item.uNestingLevel != 1 || |
| 820 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 821 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 822 | return -3; |
| 823 | } |
| 824 | |
| 825 | // An array with 4 items |
| 826 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 827 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 828 | Item.uNestingLevel != 1 || |
| 829 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 830 | (bCheckCounts && Item.val.uCount != 4)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 831 | return -4; |
| 832 | } |
| 833 | |
| 834 | // An empty array |
| 835 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 836 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 837 | Item.uNestingLevel != 2 || |
| 838 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 839 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 840 | return -5; |
| 841 | } |
| 842 | |
| 843 | // An array with 1 item |
| 844 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 845 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 846 | Item.uNestingLevel != 2 || |
| 847 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 848 | (bCheckCounts && Item.val.uCount != 1)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 849 | return -6; |
| 850 | } |
| 851 | |
| 852 | // An integer 0 |
| 853 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 854 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 855 | Item.uNestingLevel != 3 || |
| 856 | Item.uNextNestLevel != 2 || |
| 857 | Item.val.uint64 != 0) { |
| 858 | return -7; |
| 859 | } |
| 860 | |
| 861 | // An empty map |
| 862 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 863 | Item.uDataType != QCBOR_TYPE_MAP || |
| 864 | Item.uNestingLevel != 2 || |
| 865 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 866 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 867 | return -8; |
| 868 | } |
| 869 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 870 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 871 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 872 | Item.uDataType != QCBOR_TYPE_MAP || |
| 873 | Item.uNestingLevel != 2 || |
| 874 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 875 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 876 | return -9; |
| 877 | } |
| 878 | |
| 879 | // An empty map |
| 880 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 881 | Item.uDataType != QCBOR_TYPE_MAP || |
| 882 | Item.uNestingLevel != 3 || |
| 883 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 884 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 885 | return -10; |
| 886 | } |
| 887 | |
| 888 | // An empty map |
| 889 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 890 | Item.uDataType != QCBOR_TYPE_MAP || |
| 891 | Item.uNestingLevel != 3 || |
| 892 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 893 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 894 | return -11; |
| 895 | } |
| 896 | |
| 897 | // An empty array |
| 898 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 899 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 900 | Item.uNestingLevel != 3 || |
| 901 | Item.uNextNestLevel != 0 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 902 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 903 | return -12; |
| 904 | } |
| 905 | |
| 906 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 907 | return -13; |
| 908 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 913 | int32_t EmptyMapsAndArraysTest(void) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 914 | { |
| 915 | int nResult; |
| 916 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 917 | true); |
| 918 | if(nResult) { |
| 919 | return nResult; |
| 920 | } |
| 921 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 922 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 923 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 924 | false); |
| 925 | |
| 926 | if(nResult) { |
| 927 | return nResult -100; |
| 928 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 929 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 934 | |
| 935 | static const uint8_t sEmptyMap[] = { |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 936 | 0xA1, //# map(1) |
| 937 | 0x02, //# unsigned(2) |
| 938 | 0xA0, //# map(0) |
| 939 | }; |
| 940 | |
| 941 | int32_t ParseEmptyMapInMapTest(void) |
| 942 | { |
| 943 | QCBORDecodeContext DCtx; |
| 944 | QCBORItem Item; |
| 945 | int nReturn = 0; |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 946 | QCBORError uErr; |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 947 | |
| 948 | QCBORDecode_Init(&DCtx, |
| 949 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap), |
| 950 | QCBOR_DECODE_MODE_NORMAL); |
| 951 | |
| 952 | /* now open the first Map */ |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 953 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 954 | if(uErr != QCBOR_SUCCESS || |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 955 | Item.uDataType != QCBOR_TYPE_MAP) { |
| 956 | nReturn = -3; |
| 957 | goto done; |
| 958 | } |
| 959 | |
| 960 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0) { |
| 961 | nReturn = -1; |
| 962 | goto done; |
| 963 | } |
| 964 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 965 | Item.uNestingLevel != 1 || |
| 966 | Item.label.int64 != 2) { |
| 967 | nReturn = -2; |
| 968 | goto done; |
| 969 | } |
| 970 | |
| 971 | done: |
| 972 | return(nReturn); |
| 973 | } |
| 974 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 975 | |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 976 | /* [[[[[[[[[[]]]]]]]]]] */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 977 | static const uint8_t spDeepArrays[] = { |
| 978 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 979 | 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 980 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 981 | int32_t ParseDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 982 | { |
| 983 | QCBORDecodeContext DCtx; |
| 984 | int nReturn = 0; |
| 985 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 986 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 987 | QCBORDecode_Init(&DCtx, |
| 988 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 989 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 990 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 991 | for(i = 0; i < 10; i++) { |
| 992 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 993 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 994 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 995 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 996 | Item.uNestingLevel != i) { |
| 997 | nReturn = -1; |
| 998 | break; |
| 999 | } |
| 1000 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1001 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1002 | return(nReturn); |
| 1003 | } |
| 1004 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1005 | /* Big enough to test nesting to the depth of 24 |
| 1006 | [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]] |
| 1007 | */ |
| 1008 | static const uint8_t spTooDeepArrays[] = { |
| 1009 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1010 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1011 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1012 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1013 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1014 | int32_t ParseTooDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1015 | { |
| 1016 | QCBORDecodeContext DCtx; |
| 1017 | int nReturn = 0; |
| 1018 | int i; |
| 1019 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1020 | |
| 1021 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1022 | QCBORDecode_Init(&DCtx, |
| 1023 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 1024 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1025 | |
Laurence Lundblade | 3e0b230 | 2023-12-04 14:02:38 -0700 | [diff] [blame] | 1026 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1027 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1028 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 1029 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1030 | Item.uNestingLevel != i) { |
| 1031 | nReturn = -1; |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1035 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1036 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1037 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1038 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1039 | return(nReturn); |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1045 | int32_t ShortBufferParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1046 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1047 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1048 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1049 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1050 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1051 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1052 | QCBORDecode_Init(&DCtx, |
| 1053 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 1054 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1056 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1057 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1058 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1059 | nResult = -1; |
| 1060 | goto Done; |
| 1061 | } |
| 1062 | } |
| 1063 | Done: |
| 1064 | return nResult; |
| 1065 | } |
| 1066 | |
| 1067 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1068 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1069 | int32_t ShortBufferParseTest2(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1070 | { |
| 1071 | uint8_t *pEncoded; |
| 1072 | int nReturn; |
| 1073 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1074 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1075 | int64_t i1, i2; |
| 1076 | size_t i3, i4; |
| 1077 | const uint8_t *s3, *s4; |
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 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1080 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1081 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 1082 | return(-1); |
| 1083 | } |
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 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1086 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 1087 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1088 | if(nResult == 0) { |
| 1089 | nReturn = -1; |
| 1090 | } |
| 1091 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1092 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1093 | return(nReturn); |
| 1094 | } |
| 1095 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1096 | |
| 1097 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1098 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1099 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1100 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1101 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1102 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1103 | |
| 1104 | static const uint8_t pPerverseLabels[] = { |
| 1105 | 0xae, |
| 1106 | |
| 1107 | 0xf5, 0x61, 0x61, |
| 1108 | |
| 1109 | 0xf6, 0x61, 0x62, |
| 1110 | |
| 1111 | 0xf8, 0xff, 0x61, 0x63, |
| 1112 | |
| 1113 | 0xf9, 0x7e, 0x00, 0x61, 0x64, |
| 1114 | |
| 1115 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0x61, 0x65, |
| 1116 | |
| 1117 | 0xfb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x66, |
| 1118 | |
| 1119 | 0xa1, 0x19, 0x03, 0xe8, 0x10, 0x61, 0x67, |
| 1120 | |
| 1121 | 0x81, 0x81, 0x81, 0x80, 0x61, 0x68, |
| 1122 | |
| 1123 | 0xc1, 0x09, 0x61, 0x69, |
| 1124 | |
| 1125 | 0x82, 0x05, 0xa2, 0x01, 0x02, 0x03, 0x04, 0x61, 0x6a, |
| 1126 | |
| 1127 | 0xbf, 0xff, 0x61, 0x6b, |
| 1128 | |
| 1129 | 0x9f, 0x11, 0x12, 0x13, 0xff, 0x61, 0x6c, |
| 1130 | |
| 1131 | 0x7f, 0x62, 0x41, 0x42, 0x62, 0x43, 0x44, 0xff, 0x61, 0x6d, |
| 1132 | |
| 1133 | 0xd9, 0x01, 0x02, 0xbf, 0x7f, 0x61, 0x4a, 0x61, 0x4b, 0xff, 0x00, 0xf4, 0xd7, 0x80 ,0xff, 0x61, 0x6e |
| 1134 | }; |
| 1135 | #endif |
| 1136 | |
| 1137 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1138 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1139 | /* |
| 1140 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1141 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 1142 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1143 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1144 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1145 | { |
| 1146 | QCBORDecodeContext DCtx; |
| 1147 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1148 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1149 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1150 | QCBORDecode_Init(&DCtx, |
| 1151 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1152 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1153 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1154 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1155 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1156 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1157 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1158 | Item.val.uCount != 3) |
| 1159 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1160 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1161 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1162 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1163 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1164 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1165 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1166 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1167 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1168 | Item.uDataAlloc || |
| 1169 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1170 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1171 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1172 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1173 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1174 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1175 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1176 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1177 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1178 | Item.uDataAlloc || |
| 1179 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1180 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1181 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1182 | Item.val.uCount != 2) |
| 1183 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1184 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1185 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1186 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1187 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1188 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1189 | Item.uDataAlloc || |
| 1190 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1191 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1192 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1193 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1194 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1195 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1196 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1197 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1198 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1199 | Item.uDataAlloc || |
| 1200 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1201 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1202 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1203 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1204 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1205 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1206 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1207 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1208 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1209 | Item.uDataAlloc || |
| 1210 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1211 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1212 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1213 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1214 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1215 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1216 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1217 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1218 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1219 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1220 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1221 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1222 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1223 | Item.uDataAlloc || |
| 1224 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1225 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1226 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1227 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1228 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1229 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1230 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1231 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1232 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1233 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1234 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1235 | Item.uDataAlloc || |
| 1236 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1237 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1238 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1239 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1240 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1241 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1242 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1243 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1244 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1245 | Item.uDataAlloc || |
| 1246 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1247 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1248 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1249 | Item.val.int64 != 98) |
| 1250 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1251 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1252 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1253 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1254 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1255 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1256 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1257 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1258 | Item.uDataAlloc || |
| 1259 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1260 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1261 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1262 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1263 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1266 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1267 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1268 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1269 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1270 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1271 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1272 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1273 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1274 | |
| 1275 | /* Utility to decode a one byte string and match to letter. */ |
| 1276 | static QCBORError |
| 1277 | CheckOneLetterString(QCBORDecodeContext *pDecode, uint8_t letter) |
| 1278 | { |
| 1279 | UsefulBufC Text; |
| 1280 | QCBORError uErr; |
| 1281 | |
| 1282 | QCBORDecode_GetTextString(pDecode, &Text); |
| 1283 | uErr = QCBORDecode_GetError(pDecode); |
| 1284 | if(uErr) { |
| 1285 | return uErr; |
| 1286 | } |
| 1287 | |
| 1288 | if(Text.len != 1) { |
| 1289 | return QCBOR_ERR_FIRST_USER_DEFINED; |
| 1290 | } |
| 1291 | |
| 1292 | if(*(const uint8_t *)Text.ptr != letter) { |
| 1293 | return QCBOR_ERR_FIRST_USER_DEFINED; |
| 1294 | } |
| 1295 | |
| 1296 | return QCBOR_SUCCESS; |
| 1297 | } |
| 1298 | #endif |
| 1299 | |
| 1300 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1301 | /* |
| 1302 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1303 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1304 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1305 | int32_t ParseMapAsArrayTest(void) |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1306 | { |
| 1307 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1308 | QCBORItem Item; |
| 1309 | QCBORError uErr; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1310 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1311 | QCBORDecode_Init(&DCtx, |
| 1312 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1313 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1314 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1315 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1316 | return MakeTestResultCode(1, 1, uErr); |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1317 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1318 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1319 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1320 | return -1; |
| 1321 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1322 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1323 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1324 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1325 | } |
| 1326 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1327 | Item.uDataAlloc || |
| 1328 | Item.uLabelAlloc || |
| 1329 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1330 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1331 | return -2; |
| 1332 | } |
| 1333 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1334 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1335 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1336 | } |
| 1337 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1338 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1339 | Item.val.int64 != 42 || |
| 1340 | Item.uDataAlloc || |
| 1341 | Item.uLabelAlloc) { |
| 1342 | return -3; |
| 1343 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1344 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1345 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1346 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1347 | } |
| 1348 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1349 | Item.uDataAlloc || |
| 1350 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1351 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1352 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1353 | return -4; |
| 1354 | } |
| 1355 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1356 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1357 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1358 | } |
| 1359 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1360 | Item.uDataAlloc || |
| 1361 | Item.uLabelAlloc || |
| 1362 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1363 | Item.val.uCount != 2) { |
| 1364 | return -5; |
| 1365 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1366 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1367 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1368 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1369 | } |
| 1370 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1371 | Item.val.string.len != 7 || |
| 1372 | Item.uDataAlloc || |
| 1373 | Item.uLabelAlloc || |
| 1374 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1375 | return -6; |
| 1376 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1377 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1378 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1379 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1380 | } |
| 1381 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1382 | Item.uDataAlloc || |
| 1383 | Item.uLabelAlloc || |
| 1384 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1385 | return -7; |
| 1386 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1387 | |
| 1388 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1389 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1390 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1391 | } |
| 1392 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1393 | Item.uDataAlloc || |
| 1394 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1395 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1396 | return -8; |
| 1397 | } |
| 1398 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1399 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1400 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1401 | } |
| 1402 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1403 | Item.uDataAlloc || |
| 1404 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1405 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1406 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1407 | return -9; |
| 1408 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1409 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1410 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1411 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1412 | } |
| 1413 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1414 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1415 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1416 | Item.uDataAlloc || |
| 1417 | Item.uLabelAlloc) { |
| 1418 | return -10; |
| 1419 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1420 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1421 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1422 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1423 | } |
| 1424 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1425 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1426 | Item.uDataAlloc || |
| 1427 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1428 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1429 | return -11; |
| 1430 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1431 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1432 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1433 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1434 | } |
| 1435 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1436 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1437 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1438 | Item.uDataAlloc || |
| 1439 | Item.uLabelAlloc) { |
| 1440 | return -12; |
| 1441 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1442 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1443 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1444 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1445 | } |
| 1446 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1447 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1448 | Item.uDataAlloc || |
| 1449 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1450 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1451 | return -13; |
| 1452 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1453 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1454 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1455 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1456 | } |
| 1457 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1458 | Item.uDataAlloc || |
| 1459 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1460 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1461 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1462 | return -14; |
| 1463 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1464 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1465 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1466 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1467 | } |
| 1468 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1469 | Item.uDataAlloc || |
| 1470 | Item.uLabelAlloc || |
| 1471 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1472 | Item.val.int64 != 98) { |
| 1473 | return -15; |
| 1474 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1475 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1476 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1477 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1478 | } |
| 1479 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1480 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1481 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1482 | Item.uDataAlloc || |
| 1483 | Item.uLabelAlloc) { |
| 1484 | return -16; |
| 1485 | } |
| 1486 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1487 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1488 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1489 | } |
| 1490 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1491 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1492 | Item.uDataAlloc || |
| 1493 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1494 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1495 | return -17; |
| 1496 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1497 | |
| 1498 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1499 | /* |
| 1500 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1501 | map that when interpreted as an array will be too many. Test |
| 1502 | data just has the start of the map, not all the items in the map. |
| 1503 | */ |
| 1504 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1505 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1506 | QCBORDecode_Init(&DCtx, |
| 1507 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1508 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1509 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1510 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1511 | return -50; |
| 1512 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1513 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1514 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1515 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1516 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1517 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1518 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1519 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1520 | |
| 1521 | UsefulBufC Encoded; |
| 1522 | |
| 1523 | /* Big decode of a map with a wide variety or labels */ |
| 1524 | QCBORDecode_Init(&DCtx, |
| 1525 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels), |
| 1526 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 1527 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1528 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1529 | |
| 1530 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1531 | if(uErr) { |
| 1532 | return MakeTestResultCode(10, 1, uErr); |
| 1533 | } |
| 1534 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1535 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) { |
| 1536 | return MakeTestResultCode(10, 2, 0); |
| 1537 | } |
| 1538 | |
| 1539 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1540 | if(uErr) { |
| 1541 | return MakeTestResultCode(10, 3, uErr); |
| 1542 | } |
| 1543 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1544 | Item.uDataType != QCBOR_TYPE_TRUE) { |
| 1545 | return MakeTestResultCode(10, 4, 0); |
| 1546 | } |
| 1547 | |
| 1548 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1549 | if(uErr) { |
| 1550 | return MakeTestResultCode(10, 5, uErr); |
| 1551 | } |
| 1552 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1553 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1554 | ((const char *)Item.val.string.ptr)[0] != 'a') { |
| 1555 | return MakeTestResultCode(10, 6, 0); |
| 1556 | } |
| 1557 | |
| 1558 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1559 | if(uErr) { |
| 1560 | return MakeTestResultCode(10, 7, uErr); |
| 1561 | } |
| 1562 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1563 | Item.uDataType != QCBOR_TYPE_NULL) { |
| 1564 | return MakeTestResultCode(10, 8, 0); |
| 1565 | } |
| 1566 | |
| 1567 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1568 | if(uErr) { |
| 1569 | return MakeTestResultCode(10, 9, uErr); |
| 1570 | } |
| 1571 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1572 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1573 | ((const char *)Item.val.string.ptr)[0] != 'b') { |
| 1574 | return MakeTestResultCode(10, 10, 0); |
| 1575 | } |
| 1576 | |
| 1577 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1578 | if(uErr) { |
| 1579 | return MakeTestResultCode(10, 11, uErr); |
| 1580 | } |
| 1581 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1582 | Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || |
| 1583 | Item.val.int64 != 255) { |
| 1584 | return MakeTestResultCode(10, 12, 0); |
| 1585 | } |
| 1586 | |
| 1587 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1588 | if(uErr) { |
| 1589 | return MakeTestResultCode(10, 13, uErr); |
| 1590 | } |
| 1591 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1592 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1593 | ((const char *)Item.val.string.ptr)[0] != 'c') { |
| 1594 | return MakeTestResultCode(10, 14, 0); |
| 1595 | } |
| 1596 | |
| 1597 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1598 | if(uErr) { |
| 1599 | return MakeTestResultCode(10, 15, uErr); |
| 1600 | } |
| 1601 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1602 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1603 | !isnan(Item.val.dfnum)) { |
| 1604 | return MakeTestResultCode(10, 16, 0); |
| 1605 | } |
| 1606 | |
| 1607 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1608 | if(uErr) { |
| 1609 | return MakeTestResultCode(10, 17, uErr); |
| 1610 | } |
| 1611 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1612 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1613 | ((const char *)Item.val.string.ptr)[0] != 'd') { |
| 1614 | return MakeTestResultCode(10, 18, 0); |
| 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1619 | if(uErr) { |
| 1620 | return MakeTestResultCode(10, 19, uErr); |
| 1621 | } |
| 1622 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1623 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1624 | Item.val.dfnum != 3.4028234663852886E+38) { |
| 1625 | return MakeTestResultCode(10, 20, 0); |
| 1626 | } |
| 1627 | |
| 1628 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1629 | if(uErr) { |
| 1630 | return MakeTestResultCode(10, 21, uErr); |
| 1631 | } |
| 1632 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1633 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1634 | ((const char *)Item.val.string.ptr)[0] != 'e') { |
| 1635 | return MakeTestResultCode(10, 22, 0); |
| 1636 | } |
| 1637 | |
| 1638 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1639 | if(uErr) { |
| 1640 | return MakeTestResultCode(10, 23, uErr); |
| 1641 | } |
| 1642 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1643 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1644 | Item.val.dfnum != -INFINITY) { |
| 1645 | return MakeTestResultCode(10, 24, 0); |
| 1646 | } |
| 1647 | |
| 1648 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1649 | if(uErr) { |
| 1650 | return MakeTestResultCode(10, 25, uErr); |
| 1651 | } |
| 1652 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1653 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1654 | ((const char *)Item.val.string.ptr)[0] != 'f') { |
| 1655 | return MakeTestResultCode(10, 26, 0); |
| 1656 | } |
| 1657 | |
| 1658 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1659 | if(uErr) { |
| 1660 | return MakeTestResultCode(10, 26, uErr); |
| 1661 | } |
| 1662 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1663 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1664 | Item.val.uCount != 2) { |
| 1665 | return MakeTestResultCode(10, 27, 0); |
| 1666 | } |
| 1667 | |
| 1668 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1669 | if(uErr) { |
| 1670 | return MakeTestResultCode(10, 28, uErr); |
| 1671 | } |
| 1672 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1673 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1674 | Item.val.int64 != 1000) { |
| 1675 | return MakeTestResultCode(10, 29, 0); |
| 1676 | } |
| 1677 | |
| 1678 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1679 | if(uErr) { |
| 1680 | return MakeTestResultCode(10, 30, uErr); |
| 1681 | } |
| 1682 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1683 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1684 | Item.val.int64 != 16) { |
| 1685 | return MakeTestResultCode(10, 31, 0); |
| 1686 | } |
| 1687 | |
| 1688 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1689 | if(uErr) { |
| 1690 | return MakeTestResultCode(10, 32, uErr); |
| 1691 | } |
| 1692 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1693 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1694 | ((const char *)Item.val.string.ptr)[0] != 'g') { |
| 1695 | return MakeTestResultCode(10, 33, 0); |
| 1696 | } |
| 1697 | |
| 1698 | for(int i = 0 ; i < 4; i++) { |
| 1699 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1700 | if(uErr) { |
| 1701 | return MakeTestResultCode(10, 34, uErr); |
| 1702 | } |
| 1703 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1704 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 1705 | return MakeTestResultCode(10, 35, 0); |
| 1706 | } |
| 1707 | if(i != 3) { |
| 1708 | if(Item.val.uCount != 1) { |
| 1709 | return MakeTestResultCode(10, 35, 0); |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1714 | if(uErr) { |
| 1715 | return MakeTestResultCode(10, 36, uErr); |
| 1716 | } |
| 1717 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1718 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1719 | ((const char *)Item.val.string.ptr)[0] != 'h') { |
| 1720 | return MakeTestResultCode(10, 37, 0); |
| 1721 | } |
| 1722 | |
| 1723 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1724 | if(uErr) { |
| 1725 | return MakeTestResultCode(10, 38, uErr); |
| 1726 | } |
| 1727 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1728 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 1729 | return MakeTestResultCode(10, 39, 0); |
| 1730 | } |
| 1731 | |
| 1732 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1733 | if(uErr) { |
| 1734 | return MakeTestResultCode(10, 40, uErr); |
| 1735 | } |
| 1736 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1737 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1738 | ((const char *)Item.val.string.ptr)[0] != 'i') { |
| 1739 | return MakeTestResultCode(10, 41, 0); |
| 1740 | } |
| 1741 | |
| 1742 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1743 | if(uErr) { |
| 1744 | return MakeTestResultCode(10, 42, uErr); |
| 1745 | } |
| 1746 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1747 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1748 | Item.val.uCount != 2) { |
| 1749 | return MakeTestResultCode(10, 31, 0); |
| 1750 | } |
| 1751 | |
| 1752 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1753 | if(uErr) { |
| 1754 | return MakeTestResultCode(10, 43, uErr); |
| 1755 | } |
| 1756 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1757 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1758 | return MakeTestResultCode(10, 31, 0); |
| 1759 | } |
| 1760 | |
| 1761 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1762 | if(uErr) { |
| 1763 | return MakeTestResultCode(10, 44, uErr); |
| 1764 | } |
| 1765 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1766 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1767 | Item.val.uCount != 4) { |
| 1768 | return MakeTestResultCode(10, 45, 0); |
| 1769 | } |
| 1770 | |
| 1771 | for(int i = 0 ; i < 4; i++) { |
| 1772 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1773 | if(uErr) { |
| 1774 | return MakeTestResultCode(10, 46, uErr); |
| 1775 | } |
| 1776 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1777 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1778 | return MakeTestResultCode(10, 47, 0); |
| 1779 | } |
| 1780 | } |
| 1781 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1782 | if(uErr) { |
| 1783 | return MakeTestResultCode(10, 48, uErr); |
| 1784 | } |
| 1785 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1786 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1787 | ((const char *)Item.val.string.ptr)[0] != 'j') { |
| 1788 | return MakeTestResultCode(10, 49, 0); |
| 1789 | } |
| 1790 | |
| 1791 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1792 | if(uErr) { |
| 1793 | return MakeTestResultCode(10, 50, uErr); |
| 1794 | } |
| 1795 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1796 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1797 | Item.val.uCount != UINT16_MAX) { |
| 1798 | return MakeTestResultCode(10, 51, 0); |
| 1799 | } |
| 1800 | |
| 1801 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1802 | if(uErr) { |
| 1803 | return MakeTestResultCode(10, 52, uErr); |
| 1804 | } |
| 1805 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1806 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1807 | ((const char *)Item.val.string.ptr)[0] != 'k') { |
| 1808 | return MakeTestResultCode(10, 53, 0); |
| 1809 | } |
| 1810 | |
| 1811 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1812 | if(uErr) { |
| 1813 | return MakeTestResultCode(10, 54, uErr); |
| 1814 | } |
| 1815 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1816 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1817 | Item.val.uCount != UINT16_MAX) { |
| 1818 | return MakeTestResultCode(10, 55, 0); |
| 1819 | } |
| 1820 | |
| 1821 | for(int i = 0 ; i < 3; i++) { |
| 1822 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1823 | if(uErr) { |
| 1824 | return MakeTestResultCode(10, 56, uErr); |
| 1825 | } |
| 1826 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1827 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1828 | return MakeTestResultCode(10, 57, 0); |
| 1829 | } |
| 1830 | } |
| 1831 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1832 | if(uErr) { |
| 1833 | return MakeTestResultCode(10, 58, uErr); |
| 1834 | } |
| 1835 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1836 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1837 | ((const char *)Item.val.string.ptr)[0] != 'l') { |
| 1838 | return MakeTestResultCode(10, 59, 0); |
| 1839 | } |
| 1840 | |
| 1841 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1842 | if(uErr) { |
| 1843 | return MakeTestResultCode(10, 60, uErr); |
| 1844 | } |
| 1845 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1846 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1847 | Item.val.string.len != 4) { |
| 1848 | return MakeTestResultCode(10, 61, 0); |
| 1849 | } |
| 1850 | |
| 1851 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1852 | if(uErr) { |
| 1853 | return MakeTestResultCode(10, 62, uErr); |
| 1854 | } |
| 1855 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1856 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1857 | ((const char *)Item.val.string.ptr)[0] != 'm') { |
| 1858 | return MakeTestResultCode(10, 63, 0); |
| 1859 | } |
| 1860 | |
| 1861 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1862 | if(uErr) { |
| 1863 | return MakeTestResultCode(10, 64, uErr); |
| 1864 | } |
| 1865 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1866 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1867 | !QCBORDecode_IsTagged(&DCtx, &Item, 258) || |
| 1868 | Item.val.uCount != UINT16_MAX) { |
| 1869 | return MakeTestResultCode(10, 65, 0); |
| 1870 | } |
| 1871 | |
| 1872 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1873 | if(uErr) { |
| 1874 | return MakeTestResultCode(10, 66, uErr); |
| 1875 | } |
| 1876 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1877 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1878 | Item.val.string.len != 2) { |
| 1879 | return MakeTestResultCode(10, 67, 0); |
| 1880 | } |
| 1881 | |
| 1882 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1883 | if(uErr) { |
| 1884 | return MakeTestResultCode(10, 68, uErr); |
| 1885 | } |
| 1886 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1887 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1888 | Item.val.int64 != 0) { |
| 1889 | return MakeTestResultCode(10, 69, 0); |
| 1890 | } |
| 1891 | |
| 1892 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1893 | if(uErr) { |
| 1894 | return MakeTestResultCode(10, 70, uErr); |
| 1895 | } |
| 1896 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1897 | Item.uDataType != QCBOR_TYPE_FALSE) { |
| 1898 | return MakeTestResultCode(10, 71, 0); |
| 1899 | } |
| 1900 | |
| 1901 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1902 | if(uErr) { |
| 1903 | return MakeTestResultCode(10, 72, uErr); |
| 1904 | } |
| 1905 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1906 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1907 | !QCBORDecode_IsTagged(&DCtx, &Item, 23) || |
| 1908 | Item.val.uCount != 0) { |
| 1909 | return MakeTestResultCode(10, 73, 0); |
| 1910 | } |
| 1911 | |
| 1912 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1913 | if(uErr) { |
| 1914 | return MakeTestResultCode(10, 74, uErr); |
| 1915 | } |
| 1916 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1917 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1918 | ((const char *)Item.val.string.ptr)[0] != 'n') { |
| 1919 | return MakeTestResultCode(10, 75, 0); |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | /* Big decode of a map with a wide variety or labels */ |
| 1924 | QCBORDecode_Init(&DCtx, |
| 1925 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels), |
| 1926 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 1927 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1928 | |
| 1929 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1930 | bool b; |
| 1931 | QCBORDecode_GetBool(&DCtx, &b); |
| 1932 | |
| 1933 | uErr = CheckOneLetterString(&DCtx, 'a'); |
| 1934 | if(uErr) { |
| 1935 | return MakeTestResultCode(11, 1, uErr); |
| 1936 | } |
| 1937 | |
| 1938 | QCBORDecode_GetNull(&DCtx); |
| 1939 | uErr = CheckOneLetterString(&DCtx, 'b'); |
| 1940 | if(uErr) { |
| 1941 | return MakeTestResultCode(11, 2, uErr); |
| 1942 | } |
| 1943 | |
| 1944 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 1945 | uErr = CheckOneLetterString(&DCtx, 'c'); |
| 1946 | if(uErr) { |
| 1947 | return MakeTestResultCode(11, 3, uErr); |
| 1948 | } |
| 1949 | |
| 1950 | double dNum; |
| 1951 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1952 | if(!isnan(dNum)) { |
| 1953 | return MakeTestResultCode(11, 4, 0); |
| 1954 | } |
| 1955 | uErr = CheckOneLetterString(&DCtx, 'd'); |
| 1956 | if(uErr) { |
| 1957 | return MakeTestResultCode(11, 5, uErr); |
| 1958 | } |
| 1959 | |
| 1960 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1961 | if( dNum != 3.4028234663852886E+38 ) { |
| 1962 | return MakeTestResultCode(11, 6, 0); |
| 1963 | } |
| 1964 | uErr = CheckOneLetterString(&DCtx, 'e'); |
| 1965 | if(uErr) { |
| 1966 | return MakeTestResultCode(11, 7, uErr); |
| 1967 | } |
| 1968 | |
| 1969 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1970 | if(dNum != -INFINITY) { |
| 1971 | return MakeTestResultCode(11, 8, 0); |
| 1972 | } |
| 1973 | uErr = CheckOneLetterString(&DCtx, 'f'); |
| 1974 | if(uErr) { |
| 1975 | return MakeTestResultCode(11, 9, uErr); |
| 1976 | } |
| 1977 | |
| 1978 | int64_t nInt; |
| 1979 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1980 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 1981 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 1982 | QCBORDecode_ExitArray(&DCtx); |
| 1983 | uErr = CheckOneLetterString(&DCtx, 'g'); |
| 1984 | if(uErr) { |
| 1985 | return MakeTestResultCode(11, 10, uErr); |
| 1986 | } |
| 1987 | |
| 1988 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1989 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1990 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1991 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1992 | QCBORDecode_ExitArray(&DCtx); |
| 1993 | QCBORDecode_ExitArray(&DCtx); |
| 1994 | QCBORDecode_ExitArray(&DCtx); |
| 1995 | QCBORDecode_ExitArray(&DCtx); |
| 1996 | uErr = CheckOneLetterString(&DCtx, 'h'); |
| 1997 | if(uErr) { |
| 1998 | return MakeTestResultCode(11, 11, uErr); |
| 1999 | } |
| 2000 | QCBORDecode_GetEpochDate(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 2001 | uErr = CheckOneLetterString(&DCtx, 'i'); |
| 2002 | if(uErr) { |
| 2003 | return MakeTestResultCode(11, 12, uErr); |
| 2004 | } |
| 2005 | |
| 2006 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2007 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2008 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2009 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2010 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2011 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2012 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2013 | QCBORDecode_ExitArray(&DCtx); |
| 2014 | QCBORDecode_ExitArray(&DCtx); |
| 2015 | uErr = CheckOneLetterString(&DCtx, 'j'); |
| 2016 | if(uErr) { |
| 2017 | return MakeTestResultCode(11, 13, uErr); |
| 2018 | } |
| 2019 | |
| 2020 | QCBORDecode_GetArray(&DCtx, &Item, &Encoded); |
| 2021 | uErr = CheckOneLetterString(&DCtx, 'k'); |
| 2022 | if(uErr) { |
| 2023 | return MakeTestResultCode(11, 14, uErr); |
| 2024 | } |
| 2025 | |
| 2026 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2027 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2028 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2029 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2030 | QCBORDecode_ExitArray(&DCtx); |
| 2031 | uErr = CheckOneLetterString(&DCtx, 'l'); |
| 2032 | if(uErr) { |
| 2033 | return MakeTestResultCode(11, 15, uErr); |
| 2034 | } |
| 2035 | |
| 2036 | QCBORDecode_GetTextString(&DCtx, &Encoded); |
| 2037 | uErr = CheckOneLetterString(&DCtx, 'm'); |
| 2038 | if(uErr) { |
| 2039 | return MakeTestResultCode(11, 16, uErr); |
| 2040 | } |
| 2041 | |
| 2042 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2043 | if(!QCBORDecode_IsTagged(&DCtx, &Item, 258)) { |
| 2044 | return MakeTestResultCode(11, 17, 0); |
| 2045 | } |
| 2046 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) { |
| 2047 | return MakeTestResultCode(11, 18, 0); |
| 2048 | } |
| 2049 | if(Item.val.uCount != UINT16_MAX) { |
| 2050 | return MakeTestResultCode(11, 19, 0); |
| 2051 | } |
| 2052 | QCBORDecode_GetTextString(&DCtx, &Encoded); |
| 2053 | if(Encoded.len != 2) { |
| 2054 | return MakeTestResultCode(11, 20, 0); |
| 2055 | } QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2056 | QCBORDecode_GetBool(&DCtx, &b); |
| 2057 | if(b != false) { |
| 2058 | return MakeTestResultCode(11, 21, 0); |
| 2059 | } |
| 2060 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2061 | if(!QCBORDecode_IsTagged(&DCtx, &Item, 23)) { |
| 2062 | return MakeTestResultCode(11, 22, 0); |
| 2063 | } |
| 2064 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2065 | return MakeTestResultCode(11, 23, 0); |
| 2066 | } |
| 2067 | if(Item.val.uCount != 0) { |
| 2068 | return MakeTestResultCode(11, 24, 0); |
| 2069 | } |
| 2070 | QCBORDecode_ExitArray(&DCtx); |
| 2071 | QCBORDecode_ExitArray(&DCtx); |
| 2072 | uErr = CheckOneLetterString(&DCtx, 'n'); |
| 2073 | if(uErr) { |
| 2074 | return MakeTestResultCode(11, 25, uErr); |
| 2075 | } |
| 2076 | |
| 2077 | QCBORDecode_ExitArray(&DCtx); |
| 2078 | uErr = QCBORDecode_Finish(&DCtx); |
| 2079 | if(uErr) { |
| 2080 | return MakeTestResultCode(11, 26, uErr); |
| 2081 | } |
| 2082 | #endif /* QCBOR_DISABLE_... */ |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 2083 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2084 | return 0; |
| 2085 | } |
| 2086 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2087 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 2088 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2089 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 2090 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2091 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2092 | Fully or partially decode pValidMapEncoded. When |
| 2093 | partially decoding check for the right error code. |
| 2094 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2095 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2096 | The partial decodes test error conditions of |
| 2097 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2098 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2099 | This could be combined with the above test |
| 2100 | and made prettier and maybe a little more |
| 2101 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2102 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2103 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2104 | { |
| 2105 | QCBORDecodeContext DCtx; |
| 2106 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2107 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2108 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2109 | QCBORDecode_Init(&DCtx, |
| 2110 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 2111 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2112 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2113 | if(nLevel < 1) { |
| 2114 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 2115 | return -1; |
| 2116 | } else { |
| 2117 | return 0; |
| 2118 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2119 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2120 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2121 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2122 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2123 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2124 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2125 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2126 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2127 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2128 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2129 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2130 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2131 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2132 | } else { |
| 2133 | return 0; |
| 2134 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2135 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2136 | |
| 2137 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2138 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2139 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2140 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2141 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2142 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 2143 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2144 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2145 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2146 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2147 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2148 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2149 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2150 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2151 | } else { |
| 2152 | return 0; |
| 2153 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2154 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2155 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2156 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2157 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2158 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2159 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2160 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2161 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2162 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2163 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2164 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2165 | |
| 2166 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2167 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2168 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2169 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2170 | } else { |
| 2171 | return 0; |
| 2172 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2173 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2174 | |
| 2175 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2176 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2177 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2178 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2179 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2180 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2181 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2182 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2183 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2184 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2185 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2186 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2187 | } else { |
| 2188 | return 0; |
| 2189 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2190 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2191 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2192 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2193 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2194 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2195 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2196 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2197 | return -10; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2198 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2199 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2200 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2201 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2202 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2203 | } else { |
| 2204 | return 0; |
| 2205 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2206 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2207 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2208 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2209 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2210 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2211 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2212 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2213 | Item.uDataType != QCBOR_TYPE_MAP || |
| 2214 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2215 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2216 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2217 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2218 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2219 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2220 | } else { |
| 2221 | return 0; |
| 2222 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2223 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2224 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2225 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2226 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2227 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2228 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2229 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2230 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2231 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2232 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2233 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2234 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2235 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2236 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2237 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2238 | } else { |
| 2239 | return 0; |
| 2240 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2241 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2242 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2243 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2244 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2245 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2246 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2247 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2248 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2249 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2250 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2251 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2252 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2253 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2254 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2255 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2256 | } else { |
| 2257 | return 0; |
| 2258 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2260 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2261 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2262 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2263 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2264 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2265 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2266 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 2267 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2268 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2269 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2270 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2271 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2272 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2273 | } else { |
| 2274 | return 0; |
| 2275 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2276 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2277 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2278 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2279 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2280 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2281 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2282 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2283 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2284 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2285 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2286 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2287 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2288 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2289 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2291 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2292 | return 0; |
| 2293 | } |
| 2294 | |
| 2295 | |
| 2296 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2297 | int32_t ParseMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2298 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2299 | // Parse a moderatly complex map structure very thoroughly |
| 2300 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 2301 | if(nResult) { |
| 2302 | return nResult; |
| 2303 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2304 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2305 | // Again, but in strings-only mode. It should succeed since the input |
| 2306 | // map has only string labels. |
| 2307 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 2308 | if(nResult) { |
| 2309 | return nResult; |
| 2310 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2311 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2312 | // Again, but try to finish the decoding before the end of the |
| 2313 | // input at 10 different place and see that the right error code |
| 2314 | // is returned. |
| 2315 | for(int i = 0; i < 10; i++) { |
| 2316 | nResult = ExtraBytesTest(i); |
| 2317 | if(nResult) { |
| 2318 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2319 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2320 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2321 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2322 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2323 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2324 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2325 | |
| 2326 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2327 | /* The simple-values including some not well formed */ |
| 2328 | static const uint8_t spSimpleValues[] = { |
| 2329 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 2330 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 2331 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2332 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2333 | /* A map of good simple values, plus one well-formed integer */ |
| 2334 | static const uint8_t spGoodSimpleValues[] = { |
| 2335 | 0xa9, 0x01, 0xf4, 0x02, 0xf5, 0x03, 0xf6, 0x04, 0xf7, |
| 2336 | 0x05, 0xe0, 0x06, 0xf3, 0x07, 0xf8, 0x20, 0x61, 0x40, |
| 2337 | 0xf8, 0xff, 0x0f, 0x0f}; |
| 2338 | |
| 2339 | int32_t SimpleValueDecodeTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2340 | { |
| 2341 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2342 | QCBORItem Item; |
| 2343 | QCBORError uErr; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2344 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2345 | QCBORDecode_Init(&DCtx, |
| 2346 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2347 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2348 | |
| 2349 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2350 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2351 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2352 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2353 | Item.val.uCount != 10) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2354 | return 1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2355 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2356 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2357 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2358 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2359 | return 2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2360 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2361 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2362 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2363 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2364 | return 3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2365 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2366 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2367 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2368 | if(Item.uDataType != QCBOR_TYPE_NULL) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2369 | return 4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2370 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2371 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2372 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2373 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2374 | return 5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2375 | |
| 2376 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 2377 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2378 | return 6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2379 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2380 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2381 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2382 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2383 | return 7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2384 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2385 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2386 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2387 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2388 | return 8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +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 | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2391 | return 9; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 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 | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2394 | return 10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2395 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2396 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2397 | return 11; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2398 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2399 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2400 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2401 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2402 | return 12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2403 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2404 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2405 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2406 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2407 | return 13; |
| 2408 | |
| 2409 | |
| 2410 | QCBORDecode_Init(&DCtx, |
| 2411 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues), |
| 2412 | QCBOR_DECODE_MODE_NORMAL); |
| 2413 | |
| 2414 | uint8_t uSimple; |
| 2415 | |
| 2416 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 2417 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2418 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) { |
| 2419 | return 20; |
| 2420 | } |
| 2421 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2422 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) { |
| 2423 | return 21; |
| 2424 | } |
| 2425 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2426 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) { |
| 2427 | return 22; |
| 2428 | } |
| 2429 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2430 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) { |
| 2431 | return 23; |
| 2432 | } |
| 2433 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2434 | if(QCBORDecode_GetError(&DCtx) || uSimple != 0) { |
| 2435 | return 24; |
| 2436 | } |
| 2437 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2438 | if(QCBORDecode_GetError(&DCtx) || uSimple != 19) { |
| 2439 | return 25; |
| 2440 | } |
| 2441 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2442 | if(QCBORDecode_GetError(&DCtx) || uSimple != 32) { |
| 2443 | return 26; |
| 2444 | } |
| 2445 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2446 | if(QCBORDecode_GetError(&DCtx) || uSimple != 255) { |
| 2447 | return 27; |
| 2448 | } |
| 2449 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 2450 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2451 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2452 | return 28; |
| 2453 | } |
| 2454 | |
| 2455 | QCBORDecode_Rewind(&DCtx); |
| 2456 | |
| 2457 | QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple); |
| 2458 | if(QCBORDecode_GetError(&DCtx) || uSimple != 19) { |
| 2459 | return 30; |
| 2460 | } |
| 2461 | |
| 2462 | QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple); |
| 2463 | if(QCBORDecode_GetError(&DCtx) || uSimple != 255) { |
| 2464 | return 31; |
| 2465 | } |
| 2466 | |
| 2467 | QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple); |
| 2468 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 2469 | return 32; |
| 2470 | } |
| 2471 | |
| 2472 | QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple); |
| 2473 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 2474 | return 33; |
| 2475 | } |
| 2476 | |
| 2477 | QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple); |
| 2478 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 2479 | return 34; |
| 2480 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2481 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2482 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2486 | int32_t NotWellFormedTests(void) |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2487 | { |
| 2488 | // Loop over all the not-well-formed instance of CBOR |
| 2489 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2490 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 2491 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2492 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 2493 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 2494 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 2495 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2496 | // Set up decoder context. String allocator needed for indefinite |
| 2497 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2498 | QCBORDecodeContext DCtx; |
| 2499 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2500 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2501 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 2502 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2503 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2504 | |
| 2505 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2506 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2507 | do { |
| 2508 | QCBORItem Item; |
| 2509 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2510 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2511 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2512 | |
| 2513 | // Every test vector must fail with |
| 2514 | // a not-well-formed error. If not |
| 2515 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2516 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2517 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2518 | /* Return index of failure and QCBOR error in the result */ |
| 2519 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2526 | struct DecodeFailTestInput { |
| 2527 | const char *szDescription; /* Description of the test */ |
| 2528 | QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */ |
| 2529 | UsefulBufC Input; /* Chunk of CBOR that cases error */ |
| 2530 | QCBORError nError; /* The expected error */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2531 | }; |
| 2532 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2533 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2534 | static int32_t |
| 2535 | ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2536 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2537 | int nIndex; |
| 2538 | QCBORDecodeContext DCtx; |
| 2539 | QCBORError uCBORError; |
| 2540 | QCBORItem Item; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2541 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2542 | for(nIndex = 0; nIndex < nNumFails; nIndex++) { |
| 2543 | const struct DecodeFailTestInput *pF = &pFailInputs[nIndex]; |
| 2544 | |
| 2545 | QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2546 | |
| 2547 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2548 | /* Set up the decoding context including a memory pool so that |
| 2549 | * indefinite length items can be checked. |
| 2550 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2551 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2552 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2553 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2554 | if(uCBORError != QCBOR_SUCCESS) { |
| 2555 | return -1; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2556 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2557 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2558 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2559 | if(nIndex == 8) { |
| 2560 | uCBORError = 9; /* For setting break points */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2563 | /* Iterate until there is an error of some sort of error */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2564 | do { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2565 | /* Set to something none-zero, something other than QCBOR_TYPE_NONE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2566 | memset(&Item, 0x33, sizeof(Item)); |
| 2567 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2568 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2569 | } while(uCBORError == QCBOR_SUCCESS); |
| 2570 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2571 | /* Must get the expected error or the this test fails |
| 2572 | * The data and label type must also be QCBOR_TYPE_NONE. |
| 2573 | */ |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2574 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2575 | Item.uDataType != QCBOR_TYPE_NONE || |
| 2576 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2577 | return (int32_t)(nIndex * 1000 + (int)uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | return 0; |
| 2582 | } |
| 2583 | |
| 2584 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2585 | static const struct DecodeFailTestInput Failures[] = { |
| 2586 | /* Most of this is copied from not_well_formed.h. Here the error |
| 2587 | * code returned is also checked. |
| 2588 | */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2589 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2590 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2591 | /* Indefinite length strings must be closed off */ |
| 2592 | { "An indefinite length byte string not closed off", |
| 2593 | QCBOR_DECODE_MODE_NORMAL, |
| 2594 | {"0x5f\x41\x00", 3}, |
| 2595 | QCBOR_ERR_HIT_END |
| 2596 | }, |
| 2597 | { "An indefinite length text string not closed off", |
| 2598 | QCBOR_DECODE_MODE_NORMAL, |
| 2599 | {"\x7f\x61\x00", 3}, |
| 2600 | QCBOR_ERR_HIT_END |
| 2601 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2602 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2603 | /* All the chunks in an indefinite length string must be of the |
| 2604 | * type of indefinite length string |
| 2605 | */ |
| 2606 | { "Indefinite length byte string with text string chunk", |
| 2607 | QCBOR_DECODE_MODE_NORMAL, |
| 2608 | {"\x5f\x61\x00\xff", 4}, |
| 2609 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2610 | }, |
| 2611 | { "Indefinite length text string with a byte string chunk", |
| 2612 | QCBOR_DECODE_MODE_NORMAL, |
| 2613 | {"\x7f\x41\x00\xff", 4}, |
| 2614 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2615 | }, |
| 2616 | { "Indefinite length byte string with a positive integer chunk", |
| 2617 | QCBOR_DECODE_MODE_NORMAL, |
| 2618 | {"\x5f\x00\xff", 3}, |
| 2619 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2620 | }, |
| 2621 | { "Indefinite length byte string with an negative integer chunk", |
| 2622 | QCBOR_DECODE_MODE_NORMAL, |
| 2623 | {"\x5f\x21\xff", 3}, |
| 2624 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2625 | }, |
| 2626 | { "Indefinite length byte string with an array chunk", |
| 2627 | QCBOR_DECODE_MODE_NORMAL, |
| 2628 | {"\x5f\x80\xff", 3}, |
| 2629 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2630 | }, |
| 2631 | { "Indefinite length byte string with an map chunk", |
| 2632 | QCBOR_DECODE_MODE_NORMAL, |
| 2633 | {"\x5f\xa0\xff", 3}, |
| 2634 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2635 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2636 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2637 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2638 | { "Indefinite length byte string with tagged integer chunk", |
| 2639 | QCBOR_DECODE_MODE_NORMAL, |
| 2640 | {"\x5f\xc0\x00\xff", 4}, |
| 2641 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2642 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2643 | #else |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2644 | { "Indefinite length byte string with tagged integer chunk", |
| 2645 | QCBOR_DECODE_MODE_NORMAL, |
| 2646 | {"\x5f\xc0\x00\xff", 4}, |
| 2647 | QCBOR_ERR_TAGS_DISABLED |
| 2648 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2649 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2650 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2651 | { "Indefinite length byte string with an simple type chunk", |
| 2652 | QCBOR_DECODE_MODE_NORMAL, |
| 2653 | {"\x5f\xe0\xff", 3}, |
| 2654 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2655 | }, |
| 2656 | { "???", |
| 2657 | QCBOR_DECODE_MODE_NORMAL, |
| 2658 | {"\x5f\x5f\x41\x00\xff\xff", 6}, |
| 2659 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2660 | }, |
| 2661 | { "indefinite length text string with indefinite string inside", |
| 2662 | QCBOR_DECODE_MODE_NORMAL, |
| 2663 | {"\x7f\x7f\x61\x00\xff\xff", 6}, |
| 2664 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2665 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2666 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2667 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2668 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2669 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2670 | /* Definte length maps and arrays must be closed by having the right number of items */ |
| 2671 | { "A definte length array that is supposed to have 1 item, but has none", |
| 2672 | QCBOR_DECODE_MODE_NORMAL, |
| 2673 | {"\x81", 1}, |
| 2674 | QCBOR_ERR_NO_MORE_ITEMS |
| 2675 | }, |
| 2676 | { "A definte length array that is supposed to have 2 items, but has only 1", |
| 2677 | QCBOR_DECODE_MODE_NORMAL, |
| 2678 | {"\x82\x00", 2}, |
| 2679 | QCBOR_ERR_NO_MORE_ITEMS |
| 2680 | }, |
| 2681 | { "A definte length array that is supposed to have 511 items, but has only 1", |
| 2682 | QCBOR_DECODE_MODE_NORMAL, |
| 2683 | {"\x9a\x01\xff\x00", 4}, |
| 2684 | QCBOR_ERR_HIT_END |
| 2685 | }, |
| 2686 | { "A definte length map that is supposed to have 1 item, but has none", |
| 2687 | QCBOR_DECODE_MODE_NORMAL, |
| 2688 | {"\xa1", 1}, |
| 2689 | QCBOR_ERR_NO_MORE_ITEMS |
| 2690 | }, |
| 2691 | { "A definte length map that is supposed to have s item, but has only 1", |
| 2692 | QCBOR_DECODE_MODE_NORMAL, |
| 2693 | {"\xa2\x01\x02", 3}, |
| 2694 | QCBOR_ERR_NO_MORE_ITEMS |
| 2695 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2696 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2697 | /* Indefinte length maps and arrays must be ended by a break */ |
| 2698 | { "Indefinite length array with zero items and no break", |
| 2699 | QCBOR_DECODE_MODE_NORMAL, |
| 2700 | {"\x9f", 1}, |
| 2701 | QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2702 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2703 | { "Indefinite length array with two items and no break", |
| 2704 | QCBOR_DECODE_MODE_NORMAL, |
| 2705 | {"\x9\x01\x02", 3}, |
| 2706 | QCBOR_ERR_NO_MORE_ITEMS |
| 2707 | }, |
| 2708 | { "Indefinite length map with zero items and no break", |
| 2709 | QCBOR_DECODE_MODE_NORMAL, |
| 2710 | {"\xbf", 1}, |
| 2711 | QCBOR_ERR_NO_MORE_ITEMS |
| 2712 | }, |
| 2713 | { "Indefinite length map with two items and no break", |
| 2714 | QCBOR_DECODE_MODE_NORMAL, |
| 2715 | {"\xbf\x01\x02\x01\x02", 5}, |
| 2716 | QCBOR_ERR_NO_MORE_ITEMS |
| 2717 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2718 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2719 | /* Nested maps and arrays must be closed off (some extra nested test vectors) */ |
| 2720 | { "Unclosed indefinite array containing a closed definite length array", |
| 2721 | QCBOR_DECODE_MODE_NORMAL, |
| 2722 | {"\x9f\x80\x00", 3}, |
| 2723 | QCBOR_ERR_NO_MORE_ITEMS |
| 2724 | }, |
| 2725 | |
| 2726 | { "Definite length array containing an unclosed indefinite length array", |
| 2727 | QCBOR_DECODE_MODE_NORMAL, |
| 2728 | {"\x81\x9f", 2}, |
| 2729 | QCBOR_ERR_NO_MORE_ITEMS |
| 2730 | }, |
| 2731 | { "Unclosed indefinite map containing a closed definite length array", |
| 2732 | QCBOR_DECODE_MODE_NORMAL, |
| 2733 | {"\xbf\x01\x80\x00\xa0", 5}, |
| 2734 | QCBOR_ERR_NO_MORE_ITEMS |
| 2735 | }, |
| 2736 | { "Definite length map containing an unclosed indefinite length array", |
| 2737 | QCBOR_DECODE_MODE_NORMAL, |
| 2738 | {"\xa1\x02\x9f", 3}, |
| 2739 | QCBOR_ERR_NO_MORE_ITEMS |
| 2740 | }, |
| 2741 | { "Deeply nested definite length arrays with deepest one unclosed", |
| 2742 | QCBOR_DECODE_MODE_NORMAL, |
| 2743 | {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9}, |
| 2744 | QCBOR_ERR_NO_MORE_ITEMS |
| 2745 | }, |
| 2746 | { "Deeply nested indefinite length arrays with deepest one unclosed", |
| 2747 | QCBOR_DECODE_MODE_NORMAL, |
| 2748 | {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9}, |
| 2749 | QCBOR_ERR_NO_MORE_ITEMS |
| 2750 | }, |
| 2751 | { "Mixed nesting with indefinite unclosed", |
| 2752 | QCBOR_DECODE_MODE_NORMAL, |
| 2753 | {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9}, |
| 2754 | QCBOR_ERR_NO_MORE_ITEMS }, |
| 2755 | { "Mixed nesting with definite unclosed", |
| 2756 | QCBOR_DECODE_MODE_NORMAL, |
| 2757 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2758 | QCBOR_ERR_BAD_BREAK |
| 2759 | }, |
| 2760 | { "Unclosed indefinite length map in definite length maps", |
| 2761 | QCBOR_DECODE_MODE_NORMAL, |
| 2762 | {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8}, |
| 2763 | QCBOR_ERR_NO_MORE_ITEMS |
| 2764 | }, |
| 2765 | { "Unclosed definite length map in indefinite length maps", |
| 2766 | QCBOR_DECODE_MODE_NORMAL, |
| 2767 | {"\xbf\x01\xbf\x02\xa1", 5}, |
| 2768 | QCBOR_ERR_NO_MORE_ITEMS |
| 2769 | }, |
| 2770 | { "Unclosed indefinite length array in definite length maps", |
| 2771 | QCBOR_DECODE_MODE_NORMAL, |
| 2772 | {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8}, |
| 2773 | QCBOR_ERR_NO_MORE_ITEMS |
| 2774 | }, |
| 2775 | { "Unclosed definite length array in indefinite length maps", |
| 2776 | QCBOR_DECODE_MODE_NORMAL, |
| 2777 | {"\xbf\x01\xbf\x02\x81", 5}, |
| 2778 | QCBOR_ERR_NO_MORE_ITEMS |
| 2779 | }, |
| 2780 | { "Unclosed indefinite length map in definite length arrays", |
| 2781 | QCBOR_DECODE_MODE_NORMAL, |
| 2782 | {"\x81\x82\xbf\xff\xbf", 5}, |
| 2783 | QCBOR_ERR_NO_MORE_ITEMS |
| 2784 | }, |
| 2785 | { "Unclosed definite length map in indefinite length arrays", |
| 2786 | QCBOR_DECODE_MODE_NORMAL, |
| 2787 | {"\x9f\x9f\xa1", 3}, |
| 2788 | QCBOR_ERR_NO_MORE_ITEMS |
| 2789 | }, |
| 2790 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2791 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2792 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2793 | /* The "argument" for the data item is incomplete */ |
| 2794 | { "Positive integer missing 1 byte argument", |
| 2795 | QCBOR_DECODE_MODE_NORMAL, |
| 2796 | {"\x18", 1}, |
| 2797 | QCBOR_ERR_HIT_END |
| 2798 | }, |
| 2799 | { "Positive integer missing 2 byte argument", |
| 2800 | QCBOR_DECODE_MODE_NORMAL, |
| 2801 | {"\x19", 1}, |
| 2802 | QCBOR_ERR_HIT_END |
| 2803 | }, |
| 2804 | { "Positive integer missing 4 byte argument", |
| 2805 | QCBOR_DECODE_MODE_NORMAL, |
| 2806 | {"\x1a", 1}, |
| 2807 | QCBOR_ERR_HIT_END |
| 2808 | }, |
| 2809 | { "Positive integer missing 8 byte argument", |
| 2810 | QCBOR_DECODE_MODE_NORMAL, |
| 2811 | {"\x1b", 1}, |
| 2812 | QCBOR_ERR_HIT_END |
| 2813 | }, |
| 2814 | { "Positive integer missing 1 byte of 2 byte argument", |
| 2815 | QCBOR_DECODE_MODE_NORMAL, |
| 2816 | {"\x19\x01", 2}, |
| 2817 | QCBOR_ERR_HIT_END |
| 2818 | }, |
| 2819 | { "Positive integer missing 2 bytes of 4 byte argument", |
| 2820 | QCBOR_DECODE_MODE_NORMAL, |
| 2821 | {"\x1a\x01\x02", 3}, |
| 2822 | QCBOR_ERR_HIT_END |
| 2823 | }, |
| 2824 | { "Positive integer missing 1 bytes of 7 byte argument", |
| 2825 | QCBOR_DECODE_MODE_NORMAL, |
| 2826 | {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8}, |
| 2827 | QCBOR_ERR_HIT_END |
| 2828 | }, |
| 2829 | { "Negative integer missing 1 byte argument", |
| 2830 | QCBOR_DECODE_MODE_NORMAL, |
| 2831 | {"\x38", 1}, |
| 2832 | QCBOR_ERR_HIT_END |
| 2833 | }, |
| 2834 | { "Binary string missing 1 byte argument", |
| 2835 | QCBOR_DECODE_MODE_NORMAL, |
| 2836 | {"\x58", 1}, |
| 2837 | QCBOR_ERR_HIT_END |
| 2838 | }, |
| 2839 | { "Text string missing 1 byte argument", |
| 2840 | QCBOR_DECODE_MODE_NORMAL, |
| 2841 | {"\x78", 1}, |
| 2842 | QCBOR_ERR_HIT_END |
| 2843 | }, |
| 2844 | { "Array missing 1 byte argument", |
| 2845 | QCBOR_DECODE_MODE_NORMAL, |
| 2846 | {"\x98", 1}, |
| 2847 | QCBOR_ERR_HIT_END |
| 2848 | }, |
| 2849 | { "Map missing 1 byte argument", |
| 2850 | QCBOR_DECODE_MODE_NORMAL, |
| 2851 | {"\xb8", 1}, |
| 2852 | QCBOR_ERR_HIT_END |
| 2853 | }, |
| 2854 | { "Tag missing 1 byte argument", |
| 2855 | QCBOR_DECODE_MODE_NORMAL, |
| 2856 | {"\xd8", 1}, |
| 2857 | QCBOR_ERR_HIT_END |
| 2858 | }, |
| 2859 | { "Simple missing 1 byte argument", |
| 2860 | QCBOR_DECODE_MODE_NORMAL, |
| 2861 | {"\xf8", 1}, |
| 2862 | QCBOR_ERR_HIT_END |
| 2863 | }, |
| 2864 | { "half-precision with 1 byte argument", |
| 2865 | QCBOR_DECODE_MODE_NORMAL, |
| 2866 | {"\xf9\x00", 2}, |
| 2867 | QCBOR_ERR_HIT_END |
| 2868 | }, |
| 2869 | { "single-precision with 2 byte argument", |
| 2870 | QCBOR_DECODE_MODE_NORMAL, |
| 2871 | {"\0xfa\x00\x00", 3}, |
| 2872 | QCBOR_ERR_HIT_END |
| 2873 | }, |
| 2874 | { "double-precision with 3 byte argument", |
| 2875 | QCBOR_DECODE_MODE_NORMAL, |
| 2876 | {"\xfb\x00\x00\x00", 4}, |
| 2877 | QCBOR_ERR_HIT_END |
| 2878 | }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2879 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2880 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2881 | { "Tag with no content", |
| 2882 | QCBOR_DECODE_MODE_NORMAL, |
| 2883 | {"\xc0", 1}, |
| 2884 | QCBOR_ERR_HIT_END |
| 2885 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2886 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2887 | { "Tag with no content", |
| 2888 | QCBOR_DECODE_MODE_NORMAL, |
| 2889 | {"\xc0", 1}, |
| 2890 | QCBOR_ERR_TAGS_DISABLED |
| 2891 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2892 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2893 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2894 | /* Breaks must not occur in definite length arrays and maps */ |
| 2895 | { "Array of length 1 with sole member replaced by a break", |
| 2896 | QCBOR_DECODE_MODE_NORMAL, |
| 2897 | {"\x81\xff", 2}, |
| 2898 | QCBOR_ERR_BAD_BREAK |
| 2899 | }, |
| 2900 | { "Array of length 2 with 2nd member replaced by a break", |
| 2901 | QCBOR_DECODE_MODE_NORMAL, |
| 2902 | {"\x82\x00\xff", 3}, |
| 2903 | QCBOR_ERR_BAD_BREAK |
| 2904 | }, |
| 2905 | { "Map of length 1 with sole member label replaced by a break", |
| 2906 | QCBOR_DECODE_MODE_NORMAL, |
| 2907 | {"\xa1\xff", 2}, |
| 2908 | QCBOR_ERR_BAD_BREAK |
| 2909 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2910 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2911 | /* Map of length 1 with sole member label replaced by break */ |
| 2912 | { "Alternate representation that some decoders handle differently", |
| 2913 | QCBOR_DECODE_MODE_NORMAL, |
| 2914 | {"\xa1\xff\x00", 3}, |
| 2915 | QCBOR_ERR_BAD_BREAK |
| 2916 | }, |
| 2917 | { "Array of length 1 with 2nd member value replaced by a break", |
| 2918 | QCBOR_DECODE_MODE_NORMAL, |
| 2919 | {"\xa1\x00\xff", 3}, |
| 2920 | QCBOR_ERR_BAD_BREAK |
| 2921 | }, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2922 | { "Map of length 2 with 2nd entry label replaced by a break", |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2923 | QCBOR_DECODE_MODE_NORMAL, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2924 | {"\xa2\x00\x00\xff\x00", 5}, |
| 2925 | QCBOR_ERR_BAD_BREAK |
| 2926 | }, |
| 2927 | { "Map of length 2 with 2nd entry value replaced by a break", |
| 2928 | QCBOR_DECODE_MODE_NORMAL, |
| 2929 | {"\xa2\x00\x00\x01\xff", 5}, |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2930 | QCBOR_ERR_BAD_BREAK |
| 2931 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2932 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2933 | /* Breaks must not occur on their own out of an indefinite length data item */ |
| 2934 | { "A bare break is not well formed", |
| 2935 | QCBOR_DECODE_MODE_NORMAL, |
| 2936 | {"\xff", 1}, |
| 2937 | QCBOR_ERR_BAD_BREAK |
| 2938 | }, |
| 2939 | { "A bare break after a zero length definite length array", |
| 2940 | QCBOR_DECODE_MODE_NORMAL, |
| 2941 | {"\x80\xff", 2}, |
| 2942 | QCBOR_ERR_BAD_BREAK |
| 2943 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2944 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2945 | { "A bare break after a zero length indefinite length map", |
| 2946 | QCBOR_DECODE_MODE_NORMAL, |
| 2947 | {"\x9f\xff\xff", 3}, |
| 2948 | QCBOR_ERR_BAD_BREAK |
| 2949 | }, |
| 2950 | { "A break inside a definite length array inside an indefenite length array", |
| 2951 | QCBOR_DECODE_MODE_NORMAL, |
| 2952 | {"\x9f\x81\xff", 3}, |
| 2953 | QCBOR_ERR_BAD_BREAK |
| 2954 | }, |
| 2955 | { "Complicated mixed nesting with break outside indefinite length array", |
| 2956 | QCBOR_DECODE_MODE_NORMAL, |
| 2957 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2958 | QCBOR_ERR_BAD_BREAK }, |
| 2959 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 2960 | |
| 2961 | /* Forbidden two byte encodings of simple types */ |
| 2962 | { "Must use 0xe0 instead", |
| 2963 | QCBOR_DECODE_MODE_NORMAL, |
| 2964 | {"\xf8\x00", 2}, |
| 2965 | QCBOR_ERR_BAD_TYPE_7 |
| 2966 | }, |
| 2967 | { "Should use 0xe1 instead", |
| 2968 | QCBOR_DECODE_MODE_NORMAL, |
| 2969 | {"\xf8\x01", 2}, |
| 2970 | QCBOR_ERR_BAD_TYPE_7 |
| 2971 | }, |
| 2972 | { "Should use 0xe2 instead", |
| 2973 | QCBOR_DECODE_MODE_NORMAL, |
| 2974 | {"\xf8\x02", 2}, |
| 2975 | QCBOR_ERR_BAD_TYPE_7 |
| 2976 | }, { "Should use 0xe3 instead", |
| 2977 | QCBOR_DECODE_MODE_NORMAL, |
| 2978 | {"\xf8\x03", 2}, |
| 2979 | QCBOR_ERR_BAD_TYPE_7 |
| 2980 | }, |
| 2981 | { "Should use 0xe4 instead", |
| 2982 | QCBOR_DECODE_MODE_NORMAL, |
| 2983 | {"\xf8\x04", 2}, |
| 2984 | QCBOR_ERR_BAD_TYPE_7 |
| 2985 | }, |
| 2986 | { "Should use 0xe5 instead", |
| 2987 | QCBOR_DECODE_MODE_NORMAL, |
| 2988 | {"\xf8\x05", 2}, |
| 2989 | QCBOR_ERR_BAD_TYPE_7 |
| 2990 | }, |
| 2991 | { "Should use 0xe6 instead", |
| 2992 | QCBOR_DECODE_MODE_NORMAL, |
| 2993 | {"\xf8\x06", 2}, |
| 2994 | QCBOR_ERR_BAD_TYPE_7 |
| 2995 | }, |
| 2996 | { "Should use 0xe7 instead", |
| 2997 | QCBOR_DECODE_MODE_NORMAL, |
| 2998 | {"\xf8\x07", 2}, |
| 2999 | QCBOR_ERR_BAD_TYPE_7 |
| 3000 | }, |
| 3001 | { "Should use 0xe8 instead", |
| 3002 | QCBOR_DECODE_MODE_NORMAL, |
| 3003 | {"\xf8\x08", 2}, |
| 3004 | QCBOR_ERR_BAD_TYPE_7 |
| 3005 | }, |
| 3006 | { "Should use 0xe9 instead", |
| 3007 | QCBOR_DECODE_MODE_NORMAL, |
| 3008 | {"\xf8\x09", 2}, |
| 3009 | QCBOR_ERR_BAD_TYPE_7 |
| 3010 | }, |
| 3011 | { "Should use 0xea instead", |
| 3012 | QCBOR_DECODE_MODE_NORMAL, |
| 3013 | {"\xf8\x0a", 2}, |
| 3014 | QCBOR_ERR_BAD_TYPE_7 |
| 3015 | }, |
| 3016 | { "Should use 0xeb instead", |
| 3017 | QCBOR_DECODE_MODE_NORMAL, |
| 3018 | {"\xf8\x0b", 2}, |
| 3019 | QCBOR_ERR_BAD_TYPE_7 |
| 3020 | }, |
| 3021 | { "Should use 0xec instead", |
| 3022 | QCBOR_DECODE_MODE_NORMAL, |
| 3023 | {"\xf8\x0c", 2}, |
| 3024 | QCBOR_ERR_BAD_TYPE_7 |
| 3025 | }, |
| 3026 | { "Should use 0xed instead", |
| 3027 | QCBOR_DECODE_MODE_NORMAL, |
| 3028 | {"\xf8\x0d", 2}, |
| 3029 | QCBOR_ERR_BAD_TYPE_7 |
| 3030 | }, |
| 3031 | { "Should use 0xee instead", |
| 3032 | QCBOR_DECODE_MODE_NORMAL, |
| 3033 | {"\xf8\x0e", 2}, |
| 3034 | QCBOR_ERR_BAD_TYPE_7 |
| 3035 | }, |
| 3036 | { "Should use 0xef instead", |
| 3037 | QCBOR_DECODE_MODE_NORMAL, |
| 3038 | {"\xf8\x0f", 2}, |
| 3039 | QCBOR_ERR_BAD_TYPE_7 |
| 3040 | }, |
| 3041 | { "Should use 0xf0 instead", |
| 3042 | QCBOR_DECODE_MODE_NORMAL, |
| 3043 | {"\xf8\x10", 2}, |
| 3044 | QCBOR_ERR_BAD_TYPE_7 |
| 3045 | }, |
| 3046 | { "Should use 0xf1 instead", |
| 3047 | QCBOR_DECODE_MODE_NORMAL, |
| 3048 | {"\xf8\x11", 2}, |
| 3049 | QCBOR_ERR_BAD_TYPE_7 |
| 3050 | }, |
| 3051 | { "Should use 0xf2 instead", |
| 3052 | QCBOR_DECODE_MODE_NORMAL, |
| 3053 | {"\xf8\x12", 2}, |
| 3054 | QCBOR_ERR_BAD_TYPE_7 |
| 3055 | }, |
| 3056 | { "Should use 0xf3 instead", |
| 3057 | QCBOR_DECODE_MODE_NORMAL, |
| 3058 | {"\xf8\x13", 2}, |
| 3059 | QCBOR_ERR_BAD_TYPE_7 |
| 3060 | }, |
| 3061 | { "Should use 0xf4 instead", |
| 3062 | QCBOR_DECODE_MODE_NORMAL, |
| 3063 | {"\xf8\x14", 2}, |
| 3064 | QCBOR_ERR_BAD_TYPE_7 |
| 3065 | }, |
| 3066 | { "Should use 0xf5 instead", |
| 3067 | QCBOR_DECODE_MODE_NORMAL, |
| 3068 | {"\xf8\x15", 2}, |
| 3069 | QCBOR_ERR_BAD_TYPE_7 |
| 3070 | }, |
| 3071 | { "Should use 0xf6 instead", |
| 3072 | QCBOR_DECODE_MODE_NORMAL, |
| 3073 | {"\xf8\x16", 2}, |
| 3074 | QCBOR_ERR_BAD_TYPE_7 |
| 3075 | }, |
| 3076 | { "Should use 0xef7 instead", |
| 3077 | QCBOR_DECODE_MODE_NORMAL, |
| 3078 | {"\xf8\x17", 2}, |
| 3079 | QCBOR_ERR_BAD_TYPE_7 |
| 3080 | }, |
| 3081 | { "Should use 0xef8 instead", |
| 3082 | QCBOR_DECODE_MODE_NORMAL, |
| 3083 | {"\xf8\x18", 2}, |
| 3084 | QCBOR_ERR_BAD_TYPE_7 |
| 3085 | }, |
| 3086 | { "Reserved", |
| 3087 | QCBOR_DECODE_MODE_NORMAL, |
| 3088 | {"\xf8\x18", 2}, |
| 3089 | QCBOR_ERR_BAD_TYPE_7 |
| 3090 | }, |
| 3091 | |
| 3092 | /* Maps must have an even number of data items (key & value) */ |
| 3093 | { "Map with 1 item when it should have 2", |
| 3094 | QCBOR_DECODE_MODE_NORMAL, |
| 3095 | {"\xa1\x00", 2}, |
| 3096 | QCBOR_ERR_HIT_END |
| 3097 | }, |
| 3098 | { "Map with 3 item when it should have 4", |
| 3099 | QCBOR_DECODE_MODE_NORMAL, |
| 3100 | {"\xa2\x00\x00\x00", 2}, |
| 3101 | QCBOR_ERR_HIT_END |
| 3102 | }, |
| 3103 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 3104 | { "Map with 1 item when it should have 2", |
| 3105 | QCBOR_DECODE_MODE_NORMAL, |
| 3106 | {"\xbf\x00\xff", 3}, |
| 3107 | QCBOR_ERR_BAD_BREAK |
| 3108 | }, |
| 3109 | { "Map with 3 item when it should have 4", |
| 3110 | QCBOR_DECODE_MODE_NORMAL, |
| 3111 | {"\xbf\x00\x00\x00\xff", 5}, |
| 3112 | QCBOR_ERR_BAD_BREAK |
| 3113 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3114 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3115 | |
| 3116 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3117 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3118 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3119 | { "Text-based date, with an integer", |
| 3120 | QCBOR_DECODE_MODE_NORMAL, |
| 3121 | {"\xc0\x00", 2}, |
| 3122 | QCBOR_ERR_BAD_OPT_TAG |
| 3123 | }, |
| 3124 | { "Epoch date, with an byte string", |
| 3125 | QCBOR_DECODE_MODE_NORMAL, |
| 3126 | {"\xc1\x41\x33", 3}, |
| 3127 | QCBOR_ERR_BAD_OPT_TAG |
| 3128 | }, |
| 3129 | { "tagged as both epoch and string dates", |
| 3130 | QCBOR_DECODE_MODE_NORMAL, |
| 3131 | {"\xc1\xc0\x00", 3}, |
| 3132 | QCBOR_ERR_BAD_OPT_TAG |
| 3133 | }, |
| 3134 | { "big num tagged an int, not a byte string", |
| 3135 | QCBOR_DECODE_MODE_NORMAL, |
| 3136 | {"\xc2\x00", 2}, |
| 3137 | QCBOR_ERR_BAD_OPT_TAG |
| 3138 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3139 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3140 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3141 | { "Text-based date, with an integer", |
| 3142 | QCBOR_DECODE_MODE_NORMAL, |
| 3143 | {"\xc0\x00", 2}, |
| 3144 | QCBOR_ERR_TAGS_DISABLED |
| 3145 | }, |
| 3146 | { "Epoch date, with an byte string", |
| 3147 | QCBOR_DECODE_MODE_NORMAL, |
| 3148 | {"\xc1\x41\x33", 3}, |
| 3149 | QCBOR_ERR_TAGS_DISABLED |
| 3150 | }, |
| 3151 | { "tagged as both epoch and string dates", |
| 3152 | QCBOR_DECODE_MODE_NORMAL, |
| 3153 | {"\xc1\xc0\x00", 3}, |
| 3154 | QCBOR_ERR_TAGS_DISABLED |
| 3155 | }, |
| 3156 | { "big num tagged an int, not a byte string", |
| 3157 | QCBOR_DECODE_MODE_NORMAL, |
| 3158 | {"\xc2\x00", 2}, |
| 3159 | QCBOR_ERR_TAGS_DISABLED |
| 3160 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3161 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3162 | }; |
| 3163 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3164 | |
| 3165 | |
| 3166 | int32_t |
| 3167 | DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3168 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3169 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3170 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3171 | nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3172 | if(nResult) { |
| 3173 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3174 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3175 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3176 | // Corrupt the UsefulInputBuf and see that |
| 3177 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3178 | QCBORDecodeContext DCtx; |
| 3179 | QCBORItem Item; |
| 3180 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3181 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3182 | QCBORDecode_Init(&DCtx, |
| 3183 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 3184 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3185 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3186 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3187 | return (int32_t)uQCBORError; |
| 3188 | } |
| 3189 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 3190 | // This wasn't supposed to happen |
| 3191 | return -1; |
| 3192 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3193 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3194 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3195 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3196 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3197 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 3198 | // Did not get back the error expected |
| 3199 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3200 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3201 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3202 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3203 | /* |
| 3204 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 3205 | tests here can be performed to see that the max length |
| 3206 | error check works correctly. See DecodeBytes(). If the max |
| 3207 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3208 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3209 | This test will automatocally adapt the all CPU sizes |
| 3210 | through the use of SIZE_MAX. |
| 3211 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3212 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 3213 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3214 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3215 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3216 | // This makes a CBOR head with a text string that is very long |
| 3217 | // but doesn't fill in the bytes of the text string as that is |
| 3218 | // not needed to test this part of QCBOR. |
| 3219 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 3220 | |
| 3221 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 3222 | |
| 3223 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3224 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3225 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3226 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3227 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3231 | /* Try all 256 values of the byte at nLen including recursing for |
| 3232 | each of the values to try values at nLen+1 ... up to nLenMax |
| 3233 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 3234 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3235 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3236 | if(nLen >= nLenMax) { |
| 3237 | return; |
| 3238 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3239 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3240 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 3241 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3242 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3243 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3244 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3245 | // Get ready to parse |
| 3246 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3247 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3248 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3249 | // Parse by getting the next item until an error occurs |
| 3250 | // Just about every possible decoder error can occur here |
| 3251 | // The goal of this test is not to check for the correct |
| 3252 | // error since that is not really possible. It is to |
| 3253 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3254 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3255 | QCBORItem Item; |
| 3256 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3257 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3258 | break; |
| 3259 | } |
| 3260 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3261 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3262 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3267 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3268 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3269 | // Size 2 tests 64K inputs and runs quickly |
| 3270 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3271 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3272 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3273 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3274 | return 0; |
| 3275 | } |
| 3276 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3277 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3278 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3279 | { |
| 3280 | // size 3 tests 16 million inputs and runs OK |
| 3281 | // in seconds on fast machines. Size 4 takes |
| 3282 | // 10+ minutes and 5 half a day on fast |
| 3283 | // machines. This test is kept separate from |
| 3284 | // the others so as to no slow down the use |
| 3285 | // of them as a very frequent regression. |
| 3286 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3287 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3288 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3289 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3290 | return 0; |
| 3291 | } |
| 3292 | |
| 3293 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3294 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3295 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3296 | 0xc0, // tag for string date |
| 3297 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3298 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3299 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3300 | 0xc0, // tag for string date |
| 3301 | 0x00, // Wrong type for a string date |
| 3302 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3303 | /* 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] | 3304 | 0xc1, // tag for epoch date |
| 3305 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3306 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3307 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3308 | 0xc1, |
| 3309 | 0x62, 'h', 'i', // wrong type tagged |
| 3310 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3311 | /* 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] | 3312 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3313 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3314 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 3315 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3316 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3317 | 0xc1, // tag for epoch date |
| 3318 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3319 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3320 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3321 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 3322 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3323 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3324 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3325 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3326 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3327 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3328 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3329 | 0xc1, // tag for epoch date |
| 3330 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 3331 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 3332 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3333 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3334 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3335 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3336 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3337 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3338 | 0xc1, // tag for epoch date |
| 3339 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 3340 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3341 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3342 | 0xc1, |
| 3343 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 3344 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3345 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3346 | 0xc1, // tag for epoch date |
| 3347 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3348 | }; |
| 3349 | |
| 3350 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3351 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3352 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3353 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 3354 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3355 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3356 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3357 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3358 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3359 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3360 | return diff > 0.0000001; |
| 3361 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3362 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3363 | |
| 3364 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3365 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3366 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3367 | { |
| 3368 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3369 | QCBORItem Item; |
| 3370 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3371 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3372 | QCBORDecode_Init(&DCtx, |
| 3373 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 3374 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3375 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3376 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3377 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3378 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3379 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3380 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | ba58768 | 2024-02-07 16:46:43 -0800 | [diff] [blame] | 3381 | UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3382 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3383 | } |
| 3384 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3385 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3386 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3387 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3388 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3389 | } |
| 3390 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3391 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 3392 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3393 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3394 | return -4; |
| 3395 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3396 | if(uError == QCBOR_SUCCESS) { |
| 3397 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3398 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3399 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3400 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3401 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3402 | ) { |
| 3403 | return -5; |
| 3404 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3405 | } |
| 3406 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3407 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3408 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3409 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3410 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3411 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3412 | /* 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] | 3413 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 3414 | // The date is decoded and the two tags are returned. This is to |
| 3415 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3416 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3417 | return -7; |
| 3418 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3419 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3420 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3421 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3422 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3423 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 3424 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3425 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3426 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3427 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3428 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3429 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3430 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3431 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3432 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3433 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 3434 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3435 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3436 | return -10; |
| 3437 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3438 | if(uError == QCBOR_SUCCESS) { |
| 3439 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3440 | Item.val.epochDate.nSeconds != 1 |
| 3441 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3442 | || 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] | 3443 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3444 | ) { |
| 3445 | return -11; |
| 3446 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3447 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3448 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3449 | /* 8. Epoch date with too-large single-precision float */ |
| 3450 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3451 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3452 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3453 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3454 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3455 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 3456 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3457 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3458 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3459 | } |
| 3460 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3461 | /* 10. Epoch date with largest supported double-precision value */ |
| 3462 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3463 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3464 | return -14; |
| 3465 | } |
| 3466 | if(uError == QCBOR_SUCCESS) { |
| 3467 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3468 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 3469 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3470 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 3471 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 3472 | ) { |
| 3473 | return -14; |
| 3474 | } |
| 3475 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3476 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3477 | /* 11. Epoch date with single-precision NaN */ |
| 3478 | 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] | 3479 | return -15; |
| 3480 | } |
| 3481 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3482 | /* 12. Epoch date with double-precision plus infinity */ |
| 3483 | 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] | 3484 | return -16; |
| 3485 | } |
| 3486 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3487 | /* 13. Epoch date with half-precision negative infinity */ |
| 3488 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3489 | 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] | 3490 | return -17; |
| 3491 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3492 | |
| 3493 | return 0; |
| 3494 | } |
| 3495 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3496 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3497 | /* |
| 3498 | Test cases covered here. Some items cover more than one of these. |
| 3499 | positive integer (zero counts as a positive integer) |
| 3500 | negative integer |
| 3501 | half-precision float |
| 3502 | single-precision float |
| 3503 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3504 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3505 | float Overflow error |
| 3506 | Wrong type error for epoch |
| 3507 | Wrong type error for date string |
| 3508 | float disabled error |
| 3509 | half-precision disabled error |
| 3510 | -Infinity |
| 3511 | Slightly too large integer |
| 3512 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3513 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3514 | Get epoch by int |
| 3515 | Get string by int |
| 3516 | Get epoch by string |
| 3517 | Get string by string |
| 3518 | Fail to get epoch by wrong int label |
| 3519 | Fail to get string by wrong string label |
| 3520 | Fail to get epoch by string because it is invalid |
| 3521 | Fail to get epoch by int because it is invalid |
| 3522 | |
| 3523 | Untagged values |
| 3524 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3525 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3526 | 0x87, // array of 7 items |
| 3527 | |
| 3528 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 3529 | |
| 3530 | // Untagged integer 0 |
| 3531 | 0x08, |
| 3532 | 0x00, |
| 3533 | |
| 3534 | // Utagged date string with string label y |
| 3535 | 0x61, 0x79, |
| 3536 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 3537 | |
| 3538 | // Untagged single-precision float with value 3.14 with string label x |
| 3539 | 0x61, 0x78, |
| 3540 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 3541 | |
| 3542 | // Untagged half-precision float with value -2 |
| 3543 | 0x09, |
| 3544 | 0xF9, 0xC0, 0x00, |
| 3545 | |
| 3546 | /* Untagged date-only date string */ |
| 3547 | 0x18, 0x63, |
| 3548 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3549 | |
| 3550 | /* Untagged days-count epoch date */ |
| 3551 | 0x11, |
| 3552 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 3553 | |
| 3554 | // End of map, back to array |
| 3555 | |
| 3556 | 0xa7, // Open map of tagged items with labels |
| 3557 | |
| 3558 | 0x00, |
| 3559 | 0xc0, // tag for string date |
| 3560 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 3561 | |
| 3562 | |
| 3563 | 0x01, |
| 3564 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 3565 | 0xc1, // tag for epoch date |
| 3566 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3567 | |
| 3568 | 0x05, |
| 3569 | 0xc1, |
| 3570 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 3571 | |
| 3572 | |
| 3573 | 0x07, |
| 3574 | 0xc1, // tag for epoch date |
| 3575 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3576 | |
| 3577 | /* Tagged days-count epoch date */ |
| 3578 | 0x63, 0x53, 0x44, 0x45, |
| 3579 | 0xD8, 0x64, /* tag(100) */ |
| 3580 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 3581 | |
| 3582 | // Untagged -1000 with label z |
| 3583 | 0x61, 0x7a, |
| 3584 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 3585 | 0x39, 0x03, 0xe7, |
| 3586 | |
| 3587 | /* Tagged date-only date string */ |
| 3588 | 0x63, 0x53, 0x44, 0x53, |
| 3589 | 0xD9, 0x03, 0xEC, |
| 3590 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3591 | |
| 3592 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3593 | |
| 3594 | 0xc1, |
| 3595 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 3596 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3597 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3598 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 3599 | |
| 3600 | 0xc1, // tag for epoch date |
| 3601 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 3602 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3603 | // These two at the end because they are unrecoverable errors |
| 3604 | 0xc1, // tag for epoch date |
| 3605 | 0x80, // Erroneous empty array as content for date |
| 3606 | |
| 3607 | 0xc0, // tag for string date |
| 3608 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3609 | }; |
| 3610 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3611 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3612 | { |
| 3613 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3614 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3615 | int64_t nEpochDate3, nEpochDate5, |
| 3616 | nEpochDate4, nEpochDate6, |
| 3617 | nEpochDays2; |
| 3618 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3619 | |
| 3620 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3621 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3622 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3623 | |
| 3624 | /* Items are in an array or map to test look up by label and other |
| 3625 | * that might not occur in isolated items. But it does make the |
| 3626 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3627 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3628 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3629 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3630 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3631 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3632 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3633 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3634 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3635 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3636 | return 104; |
| 3637 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3638 | if(uError == QCBOR_SUCCESS) { |
| 3639 | if(nEpochDate5 != 3) { |
| 3640 | return 103; |
| 3641 | } |
| 3642 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3643 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3644 | // A half-precision date with value -2 FFF |
| 3645 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3646 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3647 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3648 | 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] | 3649 | return 106; |
| 3650 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3651 | if(uError == QCBOR_SUCCESS) { |
| 3652 | if(nEpochDate4 != -2) { |
| 3653 | return 105; |
| 3654 | } |
| 3655 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3656 | |
| 3657 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3658 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 3659 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3660 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3661 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3662 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3663 | return 107; |
| 3664 | } |
| 3665 | |
| 3666 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3667 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3668 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3669 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3670 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3671 | return 108; |
| 3672 | } |
| 3673 | |
| 3674 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3675 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 3676 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3677 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3678 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3679 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3680 | return 109; |
| 3681 | } |
| 3682 | |
| 3683 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3684 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3685 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3686 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3687 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3688 | return 110; |
| 3689 | } |
| 3690 | |
| 3691 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3692 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3693 | |
| 3694 | // Untagged integer 0 |
| 3695 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3696 | &nEpochDate3); |
| 3697 | // Untagged date string |
| 3698 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3699 | &StringDate2); |
| 3700 | |
| 3701 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3702 | &StringDays2); |
| 3703 | |
| 3704 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3705 | &nEpochDays2); |
| 3706 | |
| 3707 | QCBORDecode_ExitMap(&DC); |
| 3708 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3709 | return 3001; |
| 3710 | } |
| 3711 | |
| 3712 | // The map of tagged items |
| 3713 | QCBORDecode_EnterMap(&DC, NULL); |
| 3714 | |
| 3715 | #ifndef QCBOR_DISABLE_TAGS |
| 3716 | int64_t nEpochDate2, |
| 3717 | nEpochDateFail, |
| 3718 | nEpochDate1400000000, nEpochDays1; |
| 3719 | UsefulBufC StringDays1; |
| 3720 | uint64_t uTag1, uTag2; |
| 3721 | |
| 3722 | // Tagged date string |
| 3723 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3724 | &StringDate1); |
| 3725 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3726 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3727 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3728 | 1, |
| 3729 | QCBOR_TAG_REQUIREMENT_TAG | |
| 3730 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3731 | &nEpochDate1400000000); |
| 3732 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3733 | |
| 3734 | // Get largest negative double precision epoch date allowed |
| 3735 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3736 | 5, |
| 3737 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 3738 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3739 | &nEpochDate2); |
| 3740 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3741 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3742 | return 102; |
| 3743 | } |
| 3744 | if(uError == QCBOR_SUCCESS) { |
| 3745 | if(nEpochDate2 != -9223372036854773760LL) { |
| 3746 | return 101; |
| 3747 | } |
| 3748 | } |
| 3749 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3750 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3751 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 3752 | "z", |
| 3753 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 3754 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3755 | &nEpochDate6); |
| 3756 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3757 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3758 | |
| 3759 | // Get largest double precision epoch date allowed |
| 3760 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3761 | &nEpochDate2); |
| 3762 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3763 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3764 | return 112; |
| 3765 | } |
| 3766 | if(uError == QCBOR_SUCCESS) { |
| 3767 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 3768 | return 111; |
| 3769 | } |
| 3770 | } |
| 3771 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3772 | /* The days format is much simpler than the date format |
| 3773 | * because it can't be a floating point value. The test |
| 3774 | * of the spiffy decode functions sufficiently covers |
| 3775 | * the test of the non-spiffy decode days date decoding. |
| 3776 | * There is no full fan out of the error conditions |
| 3777 | * and decode options as that is implemented by code |
| 3778 | * that is tested well by the date testing above. |
| 3779 | */ |
| 3780 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 3781 | &StringDays1); |
| 3782 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3783 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 3784 | &nEpochDays1); |
| 3785 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3786 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3787 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3788 | return 3001; |
| 3789 | } |
| 3790 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3791 | // Too-negative float, -9.2233720368547748E+18 |
| 3792 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3793 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3794 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3795 | return 1111; |
| 3796 | } |
| 3797 | |
| 3798 | // Too-large integer |
| 3799 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3800 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3801 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 3802 | return 1; |
| 3803 | } |
| 3804 | |
| 3805 | // Half-precision minus infinity |
| 3806 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3807 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3808 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3809 | return 2; |
| 3810 | } |
| 3811 | |
| 3812 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3813 | // Bad content for epoch date |
| 3814 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3815 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3816 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3817 | return 3; |
| 3818 | } |
| 3819 | |
| 3820 | // Bad content for string date |
| 3821 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 3822 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3823 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3824 | return 4; |
| 3825 | } |
| 3826 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3827 | QCBORDecode_ExitArray(&DC); |
| 3828 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3829 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3830 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3831 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3832 | #else /* QCBOR_DISABLE_TAGS */ |
| 3833 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3834 | &StringDate1); |
| 3835 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3836 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 3837 | return 4; |
| 3838 | } |
| 3839 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3840 | |
| 3841 | |
| 3842 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3843 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3844 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3845 | return 200; |
| 3846 | } |
| 3847 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3848 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3849 | return 201; |
| 3850 | } |
| 3851 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3852 | if(nEpochDays1 != -10676) { |
| 3853 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3856 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3857 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3858 | } |
| 3859 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3860 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3861 | return 204; |
| 3862 | } |
| 3863 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3864 | if(nEpochDate6 != -1000) { |
| 3865 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3866 | } |
| 3867 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3868 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3869 | return 205; |
| 3870 | } |
| 3871 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3872 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3873 | |
| 3874 | if(nEpochDate3 != 0) { |
| 3875 | return 202; |
| 3876 | } |
| 3877 | |
| 3878 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3879 | return 206; |
| 3880 | } |
| 3881 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3882 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 3883 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3887 | return 208; |
| 3888 | } |
| 3889 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3890 | return 0; |
| 3891 | } |
| 3892 | |
| 3893 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3894 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3895 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3896 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3897 | 0x81, // Array of one |
| 3898 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 3899 | 0x82, // Array of two that is the faction 1/3 |
| 3900 | 0x01, |
| 3901 | 0x03, |
| 3902 | |
| 3903 | /* |
| 3904 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3905 | */ |
| 3906 | 0xd8, 0xe1, |
| 3907 | 0xd8, 0xe2, |
| 3908 | 0xd8, 0xe3, |
| 3909 | 0xd8, 0xe4, |
| 3910 | 0xd8, 0xe5, |
| 3911 | 0x80, |
| 3912 | |
| 3913 | /* tag 10489608748473423768( |
| 3914 | 2442302356( |
| 3915 | 21590( |
| 3916 | 240( |
| 3917 | [])))) |
| 3918 | */ |
| 3919 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3920 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 3921 | 0xd9, 0x54, 0x56, |
| 3922 | 0xd8, 0xf0, |
| 3923 | 0x80, |
| 3924 | |
| 3925 | /* tag 21590( |
| 3926 | 10489608748473423768( |
| 3927 | 2442302357( |
| 3928 | 65534( |
| 3929 | [])))) |
| 3930 | */ |
| 3931 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 3932 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3933 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3934 | 0xd9, 0xff, 0xfe, |
| 3935 | 0x80, |
| 3936 | |
| 3937 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3938 | works in conjuntion with entries above. |
| 3939 | 269488144(269488145(269488146(269488147([])))) |
| 3940 | */ |
| 3941 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 3942 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 3943 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 3944 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 3945 | 0x80, |
| 3946 | |
| 3947 | /* An invalid decimal fraction with an additional tag */ |
| 3948 | 0xd9, 0xff, 0xfa, |
| 3949 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 3950 | 0x00, // the integer 0; should be a byte string |
| 3951 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3952 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3953 | /* |
| 3954 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3955 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3956 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3957 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3958 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3959 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3960 | /* |
| 3961 | DB 9192939495969798 # tag(10489608748473423768) |
| 3962 | D8 88 # tag(136) |
| 3963 | C6 # tag(6) |
| 3964 | C7 # tag(7) |
| 3965 | 80 # array(0) |
| 3966 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3967 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3968 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3969 | |
| 3970 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3971 | 55799(55799(55799({ |
| 3972 | 6(7(-23)): 5859837686836516696(7({ |
| 3973 | 7(-20): 11({ |
| 3974 | 17(-18): 17(17(17("Organization"))), |
| 3975 | 9(-17): 773("SSG"), |
| 3976 | -15: 16(17(6(7("Confusion")))), |
| 3977 | 17(-16): 17("San Diego"), |
| 3978 | 17(-14): 17("US") |
| 3979 | }), |
| 3980 | 23(-19): 19({ |
| 3981 | -11: 9({ |
| 3982 | -9: -7 |
| 3983 | }), |
| 3984 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 3985 | }) |
| 3986 | })), |
| 3987 | 16(-22): 23({ |
| 3988 | 11(8(7(-5))): 8(-3) |
| 3989 | }) |
| 3990 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3991 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3992 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3993 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 3994 | 0xc6, 0xc7, 0x36, |
| 3995 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 3996 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 3997 | 0xcb, 0xa5, |
| 3998 | 0xd1, 0x31, |
| 3999 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 4000 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 4001 | 0xc9, 0x30, |
| 4002 | 0xd9, 0x03, 0x05, 0x63, |
| 4003 | 0x53, 0x53, 0x47, |
| 4004 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4005 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 4006 | 0x69, |
| 4007 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4008 | 0xd1, 0x2f, |
| 4009 | 0xd1, 0x69, |
| 4010 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 4011 | 0xd1, 0x2d, |
| 4012 | 0xd1, 0x62, |
| 4013 | 0x55, 0x53, |
| 4014 | 0xd7, 0x32, |
| 4015 | 0xd3, 0xa2, |
| 4016 | 0x2a, |
| 4017 | 0xc9, 0xa1, |
| 4018 | 0x28, |
| 4019 | 0x26, |
| 4020 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 4021 | 0xcc, 0x4a, |
| 4022 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 4023 | 0xd0, 0x35, |
| 4024 | 0xd7, 0xa1, |
| 4025 | 0xcb, 0xc8, 0xc7, 0x24, |
| 4026 | 0xc8, 0x22}; |
| 4027 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4028 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4029 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4030 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4031 | |
| 4032 | 0xc0, // tag for string date |
| 4033 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4034 | |
| 4035 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4036 | |
| 4037 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 4038 | |
| 4039 | 0xd8, 0x23, // tag for regex |
| 4040 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4041 | |
| 4042 | 0xc0, // tag for string date |
| 4043 | 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] | 4044 | |
| 4045 | // This last case makes the array untraversable because it is |
| 4046 | // an uncrecoverable error. Make sure it stays last and is the only |
| 4047 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4048 | }; |
| 4049 | |
| 4050 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4051 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4052 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4053 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4054 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4055 | { |
| 4056 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4057 | QCBORItem Item; |
| 4058 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4059 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4060 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4061 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4062 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4063 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4064 | /* |
| 4065 | This test matches the magic number tag and the fraction tag |
| 4066 | 55799([...]) |
| 4067 | */ |
| 4068 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4069 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4070 | return -2; |
| 4071 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4072 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4073 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 4074 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4075 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4076 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4077 | /* |
| 4078 | 4([1,3]) |
| 4079 | */ |
| 4080 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4081 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4082 | if(uError != QCBOR_SUCCESS || |
| 4083 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4084 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 4085 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 4086 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4087 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4088 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4089 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 4090 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4091 | return -4; |
| 4092 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4093 | // consume the items in the array |
| 4094 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4095 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4096 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4097 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4098 | if(uError != QCBOR_SUCCESS || |
| 4099 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4100 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 4101 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4102 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4103 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4104 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 4105 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4106 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4107 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4108 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4109 | /* |
| 4110 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 4111 | */ |
| 4112 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4113 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4114 | return -6; |
| 4115 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4116 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 4117 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 4118 | return -106; |
| 4119 | } |
| 4120 | |
| 4121 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4122 | /* tag 10489608748473423768( |
| 4123 | 2442302356( |
| 4124 | 21590( |
| 4125 | 240( |
| 4126 | [])))) |
| 4127 | */ |
| 4128 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4129 | if(uError != QCBOR_SUCCESS || |
| 4130 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4131 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 4132 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 4133 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 4134 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4135 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | /* tag 21590( |
| 4139 | 10489608748473423768( |
| 4140 | 2442302357( |
| 4141 | 21591( |
| 4142 | [])))) |
| 4143 | */ |
| 4144 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4145 | if(uError != QCBOR_SUCCESS || |
| 4146 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4147 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 4148 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 4149 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 4150 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 4151 | return -8; |
| 4152 | } |
| 4153 | |
| 4154 | /* Make sure to blow past the limit of tags that must be mapped. |
| 4155 | works in conjuntion with entries above. |
| 4156 | 269488144(269488145(269488146(269488147([])))) |
| 4157 | */ |
| 4158 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4159 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4160 | return -9; |
| 4161 | } |
| 4162 | |
| 4163 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4164 | if(uError == QCBOR_SUCCESS) { |
| 4165 | return -10; |
| 4166 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4167 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4168 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4169 | // 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] | 4170 | // tage and then matches it. Caller-config lists are no longer |
| 4171 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4172 | QCBORDecode_Init(&DCtx, |
| 4173 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4174 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4175 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 4176 | const QCBORTagListIn TL = {2, puList}; |
| 4177 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4178 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4179 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4180 | return -8; |
| 4181 | } |
| 4182 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4183 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 4184 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 4185 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 4186 | Item.val.uCount != 0) { |
| 4187 | return -9; |
| 4188 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4189 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4190 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4191 | // 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] | 4192 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4193 | const uint64_t puLongList[17] = {1,2,1}; |
| 4194 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4195 | QCBORDecode_Init(&DCtx, |
| 4196 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4197 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4198 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 4199 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4200 | return -11; |
| 4201 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4202 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4203 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4204 | QCBORTagListOut Out = {0, 4, puTags}; |
| 4205 | |
| 4206 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4207 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4208 | QCBORDecode_Init(&DCtx, |
| 4209 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4210 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4211 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4212 | return -12; |
| 4213 | } |
| 4214 | if(puTags[0] != 0x9192939495969798 || |
| 4215 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4216 | puTags[2] != 0x06 || |
| 4217 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4218 | return -13; |
| 4219 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4220 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4221 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4222 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4223 | QCBORDecode_Init(&DCtx, |
| 4224 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4225 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4226 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 4227 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4228 | return -14; |
| 4229 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4230 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4231 | |
| 4232 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4233 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4234 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 4235 | // It is a bit of a messy test and maybe could be improved, but |
| 4236 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4237 | QCBORDecode_Init(&DCtx, |
| 4238 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4239 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4240 | int n = CheckCSRMaps(&DCtx); |
| 4241 | if(n) { |
| 4242 | return n-2000; |
| 4243 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4244 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4245 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4246 | QCBORDecode_Init(&DCtx, |
| 4247 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4248 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4249 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4250 | /* With the spiffy decode revision, this tag list is not used. |
| 4251 | It doesn't matter if a tag is in this list or not so some |
| 4252 | tests that couldn't process a tag because it isn't in this list |
| 4253 | now can process these unlisted tags. The tests have been |
| 4254 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4255 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 4256 | const QCBORTagListIn TagList = {3, puTagList}; |
| 4257 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4258 | |
| 4259 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4260 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4261 | return -100; |
| 4262 | } |
| 4263 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4264 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4265 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 4266 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 4267 | Item.val.uCount != 2 || |
| 4268 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 4269 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 4270 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 4271 | Out.uNumUsed != 3) { |
| 4272 | return -101; |
| 4273 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4274 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4275 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4276 | return -102; |
| 4277 | } |
| 4278 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4279 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4280 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4281 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4282 | Item.val.uCount != 2 || |
| 4283 | puTags[0] != 5859837686836516696 || |
| 4284 | puTags[1] != 7 || |
| 4285 | Out.uNumUsed != 2) { |
| 4286 | return -103; |
| 4287 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4288 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4289 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4290 | return -104; |
| 4291 | } |
| 4292 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4293 | Item.val.uCount != 5 || |
| 4294 | puTags[0] != 0x0b || |
| 4295 | Out.uNumUsed != 1) { |
| 4296 | return -105; |
| 4297 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4298 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4299 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4300 | return -106; |
| 4301 | } |
| 4302 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4303 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 4304 | Item.val.string.len != 12 || |
| 4305 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 4306 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 4307 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 4308 | Out.uNumUsed != 3) { |
| 4309 | return -105; |
| 4310 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4311 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4312 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4313 | return -107; |
| 4314 | } |
| 4315 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4316 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 4317 | Item.val.string.len != 3 || |
| 4318 | puTags[0] != 773 || |
| 4319 | Out.uNumUsed != 1) { |
| 4320 | return -108; |
| 4321 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4322 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4323 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4324 | return -109; |
| 4325 | } |
| 4326 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4327 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4328 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4329 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4330 | puTags[3] != 7 || |
| 4331 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4332 | return -110; |
| 4333 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4334 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4335 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4336 | return -111; |
| 4337 | } |
| 4338 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4339 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4340 | Item.val.string.len != 9 || |
| 4341 | puTags[0] != 17 || |
| 4342 | Out.uNumUsed != 1) { |
| 4343 | return -112; |
| 4344 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4345 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4346 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4347 | return -111; |
| 4348 | } |
| 4349 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4350 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4351 | Item.val.string.len != 2 || |
| 4352 | puTags[0] != 17 || |
| 4353 | Out.uNumUsed != 1) { |
| 4354 | return -112; |
| 4355 | } |
| 4356 | |
| 4357 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4358 | return -113; |
| 4359 | } |
| 4360 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4361 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4362 | Item.val.uCount != 2 || |
| 4363 | puTags[0] != 19 || |
| 4364 | Out.uNumUsed != 1) { |
| 4365 | return -114; |
| 4366 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4367 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4368 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4369 | return -115; |
| 4370 | } |
| 4371 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4372 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4373 | Item.val.uCount != 1 || |
| 4374 | puTags[0] != 9 || |
| 4375 | Out.uNumUsed != 1) { |
| 4376 | return -116; |
| 4377 | } |
| 4378 | |
| 4379 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4380 | return -116; |
| 4381 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4382 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4383 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4384 | Out.uNumUsed != 0) { |
| 4385 | return -117; |
| 4386 | } |
| 4387 | |
| 4388 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4389 | return -118; |
| 4390 | } |
| 4391 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 4392 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4393 | puTags[0] != 12 || |
| 4394 | Out.uNumUsed != 1) { |
| 4395 | return -119; |
| 4396 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4397 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4398 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4399 | return -120; |
| 4400 | } |
| 4401 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4402 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 4403 | Item.val.uCount != 1 || |
| 4404 | puTags[0] != 0x17 || |
| 4405 | Out.uNumUsed != 1) { |
| 4406 | return -121; |
| 4407 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4408 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4409 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4410 | return -122; |
| 4411 | } |
| 4412 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4413 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4414 | Item.val.int64 != -3 || |
| 4415 | puTags[0] != 8 || |
| 4416 | Out.uNumUsed != 1) { |
| 4417 | return -123; |
| 4418 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4419 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4420 | if(QCBORDecode_Finish(&DCtx)) { |
| 4421 | return -124; |
| 4422 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4423 | |
| 4424 | UsefulBufC DateString; |
| 4425 | QCBORDecode_Init(&DCtx, |
| 4426 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4427 | QCBOR_DECODE_MODE_NORMAL); |
| 4428 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4429 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4430 | // tagged date string |
| 4431 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4432 | // untagged date string |
| 4433 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4434 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4435 | return 100; |
| 4436 | } |
| 4437 | // untagged byte string |
| 4438 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4439 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4440 | return 101; |
| 4441 | } |
| 4442 | // tagged regex |
| 4443 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4444 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4445 | return 102; |
| 4446 | } |
| 4447 | // tagged date string with a byte string |
| 4448 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4449 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4450 | return 103; |
| 4451 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4452 | // The exit errors out because the last item, the date string with |
| 4453 | // bad content makes the array untraversable (the bad date string |
| 4454 | // could have tag content of an array or such that is not consumed |
| 4455 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4456 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4457 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4458 | return 104; |
| 4459 | } |
| 4460 | |
| 4461 | |
| 4462 | QCBORDecode_Init(&DCtx, |
| 4463 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4464 | QCBOR_DECODE_MODE_NORMAL); |
| 4465 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4466 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4467 | // tagged date string |
| 4468 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4469 | // untagged date string |
| 4470 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4471 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4472 | return 200; |
| 4473 | } |
| 4474 | // untagged byte string |
| 4475 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4476 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4477 | return 201; |
| 4478 | } |
| 4479 | // tagged regex |
| 4480 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4481 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4482 | return 202; |
| 4483 | } |
| 4484 | // tagged date string with a byte string |
| 4485 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4486 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4487 | return 203; |
| 4488 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4489 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4490 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4491 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4492 | return 204; |
| 4493 | } |
| 4494 | |
| 4495 | QCBORDecode_Init(&DCtx, |
| 4496 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4497 | QCBOR_DECODE_MODE_NORMAL); |
| 4498 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4499 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4500 | // tagged date string |
| 4501 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4502 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4503 | return 300; |
| 4504 | } |
| 4505 | // untagged date string |
| 4506 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4507 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4508 | return 301; |
| 4509 | } |
| 4510 | // untagged byte string |
| 4511 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4512 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4513 | return 302; |
| 4514 | } |
| 4515 | // tagged regex |
| 4516 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4517 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4518 | return 303; |
| 4519 | } |
| 4520 | // tagged date string with a byte string |
| 4521 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4522 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4523 | return 304; |
| 4524 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4525 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4526 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4527 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4528 | return 305; |
| 4529 | } |
| 4530 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4531 | return 0; |
| 4532 | } |
| 4533 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4534 | /* |
| 4535 | * These are showing the big numbers converted to integers. |
| 4536 | * The tag numbers are not shown. |
| 4537 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4538 | * [ |
| 4539 | * 18446744073709551616, |
| 4540 | * -18446744073709551617, |
| 4541 | * { |
| 4542 | * -64: -18446744073709551617, |
| 4543 | * 64: 18446744073709551616, |
| 4544 | * "BN+": 18446744073709551616, |
| 4545 | * "BN-": -18446744073709551617 |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4546 | * } |
| 4547 | * ] |
| 4548 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4549 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4550 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4551 | 0x83, |
| 4552 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4553 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4554 | 0xA4, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4555 | 0x38, 0x3F, |
| 4556 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4557 | 0x18, 0x40, |
| 4558 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4559 | 0x63, 0x42, 0x4E, 0x2B, |
| 4560 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4561 | 0x63, 0x42, 0x4E, 0x2D, |
| 4562 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4563 | }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4564 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4565 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4566 | /* The expected big num */ |
| 4567 | static const uint8_t spBigNum[] = { |
| 4568 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4569 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4570 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4571 | |
| 4572 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4573 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4574 | { |
| 4575 | QCBORDecodeContext DCtx; |
| 4576 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4577 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4578 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4579 | QCBORDecode_Init(&DCtx, |
| 4580 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 4581 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4582 | |
| 4583 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4584 | // |
| 4585 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4586 | return -1; |
| 4587 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4588 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4589 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4590 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4591 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4592 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4593 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4594 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4595 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4596 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4597 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | // |
| 4601 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4602 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4603 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4604 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4605 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4606 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4607 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4608 | // |
| 4609 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4610 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4611 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4612 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4613 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4614 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4615 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4616 | return -15; |
| 4617 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4618 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4619 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4620 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4621 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4625 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4626 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4627 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4628 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4629 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4630 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4631 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4632 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4633 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 4634 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4635 | return -9; |
| 4636 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4637 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4638 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
| 4639 | return -10; |
| 4640 | } |
| 4641 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4642 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4643 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4644 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4645 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4646 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4647 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4648 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4649 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4650 | |
| 4651 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4652 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4653 | #else |
| 4654 | |
| 4655 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 4656 | return -100; |
| 4657 | } |
| 4658 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4659 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4660 | return 0; |
| 4661 | } |
| 4662 | |
| 4663 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4664 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4665 | uint8_t uDataType, |
| 4666 | uint8_t uNestingLevel, |
| 4667 | uint8_t uNextNest, |
| 4668 | int64_t nLabel, |
| 4669 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4670 | { |
| 4671 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4672 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4673 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4674 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 4675 | if(Item.uDataType != uDataType) return -1; |
| 4676 | if(uNestingLevel > 0) { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4677 | if(Item.uLabelType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4678 | return -1; |
| 4679 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4680 | if(Item.label.int64 != nLabel) { |
| 4681 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4682 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4683 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4684 | } |
| 4685 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4686 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4687 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4688 | if(pItem) { |
| 4689 | *pItem = Item; |
| 4690 | } |
| 4691 | return 0; |
| 4692 | } |
| 4693 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4694 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4695 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4696 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4697 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4698 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4699 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4700 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4701 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4702 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4703 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 4704 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 4705 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 4706 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 4707 | 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] | 4708 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4709 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 4710 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4711 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4712 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 4713 | 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] | 4714 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4715 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 4716 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4717 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4718 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4719 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4720 | return 0; |
| 4721 | } |
| 4722 | |
| 4723 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4724 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4725 | { |
| 4726 | -23: { |
| 4727 | -20: { |
| 4728 | -18: "Organization", |
| 4729 | -17: "SSG", |
| 4730 | -15: "Confusion", |
| 4731 | -16: "San Diego", |
| 4732 | -14: "US" |
| 4733 | }, |
| 4734 | -19: { |
| 4735 | -11: { |
| 4736 | -9: -7 |
| 4737 | }, |
| 4738 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 4739 | } |
| 4740 | }, |
| 4741 | -22: { |
| 4742 | -5: -3 |
| 4743 | } |
| 4744 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4745 | */ |
| 4746 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4747 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 4748 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4749 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4750 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4751 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4752 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4753 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 4754 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 4755 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 4756 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4757 | // Same map as above, but using indefinite lengths |
| 4758 | static const uint8_t spCSRInputIndefLen[] = { |
| 4759 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 4760 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4761 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4762 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4763 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4764 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4765 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 4766 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 4767 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 4768 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 4769 | |
| 4770 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4771 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4772 | { |
| 4773 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4774 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4775 | QCBORDecode_Init(&DCtx, |
| 4776 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4777 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4778 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4779 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4780 | } |
| 4781 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4782 | |
| 4783 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4784 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4785 | { |
| 4786 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4787 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4788 | QCBORDecode_Init(&DCtx, |
| 4789 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4790 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4791 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4792 | QCBORItem Item; |
| 4793 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4794 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4795 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4796 | return -1; |
| 4797 | } |
| 4798 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4799 | return -2; |
| 4800 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4801 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4802 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4803 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 4804 | return -3; |
| 4805 | } |
| 4806 | |
| 4807 | return 0; |
| 4808 | } |
| 4809 | |
| 4810 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4811 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4812 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4813 | { |
| 4814 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4815 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4816 | QCBORDecode_Init(&DCtx, |
| 4817 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 4818 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4819 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4820 | return CheckCSRMaps(&DCtx); |
| 4821 | } |
| 4822 | |
| 4823 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4824 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4825 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 4826 | { |
| 4827 | UsefulOutBuf UOB; |
| 4828 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4829 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4830 | int i; |
| 4831 | for(i = 0; i < n; i++) { |
| 4832 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 4833 | } |
| 4834 | |
| 4835 | for(i = 0; i < n; i++) { |
| 4836 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4837 | } |
| 4838 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4839 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4840 | |
| 4841 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4842 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4843 | { |
| 4844 | QCBORDecodeContext DC; |
| 4845 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4846 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4847 | int j; |
| 4848 | for(j = 0; j < nNestLevel; j++) { |
| 4849 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4850 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4851 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 4852 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4853 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4854 | return -4; |
| 4855 | } else { |
| 4856 | return 0; // Decoding doesn't recover after an error |
| 4857 | } |
| 4858 | } else { |
| 4859 | // Should be no error |
| 4860 | if(nReturn) { |
| 4861 | return -9; // Should not have got an error |
| 4862 | } |
| 4863 | } |
| 4864 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4865 | return -7; |
| 4866 | } |
| 4867 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4868 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4869 | if(nReturn) { |
| 4870 | return -3; |
| 4871 | } |
| 4872 | return 0; |
| 4873 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4874 | |
| 4875 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4876 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4877 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4878 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4879 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4880 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4881 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4882 | int nReturn = parse_indeflen_nested(Nested, i); |
| 4883 | if(nReturn) { |
| 4884 | return nReturn; |
| 4885 | } |
| 4886 | } |
| 4887 | return 0; |
| 4888 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4889 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4890 | // [1, [2, 3]] |
| 4891 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 4892 | // No closing break |
| 4893 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 4894 | // Not enough closing breaks |
| 4895 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 4896 | // Too many closing breaks |
| 4897 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 4898 | // Unclosed indeflen inside def len |
| 4899 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 4900 | // confused tag |
| 4901 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4902 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4903 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4904 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4905 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4906 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4907 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4908 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4909 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4910 | QCBORDecodeContext DC; |
| 4911 | QCBORItem Item; |
| 4912 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4913 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4914 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4915 | |
| 4916 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4917 | Item.uNestingLevel != 0 || |
| 4918 | Item.uNextNestLevel != 1) { |
| 4919 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4920 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4921 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4922 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4923 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 4924 | Item.uNestingLevel != 1 || |
| 4925 | Item.uNextNestLevel != 1) { |
| 4926 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4927 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4928 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4929 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4930 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4931 | Item.uNestingLevel != 1 || |
| 4932 | Item.uNextNestLevel != 2) { |
| 4933 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4934 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4935 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4936 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4937 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4938 | Item.uNestingLevel != 2 || |
| 4939 | Item.uNextNestLevel != 2) { |
| 4940 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4941 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4942 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4943 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4944 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4945 | Item.uNestingLevel != 2 || |
| 4946 | Item.uNextNestLevel != 0) { |
| 4947 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4948 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4949 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4950 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4951 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4952 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4953 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4954 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4955 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4956 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4957 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4958 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4959 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4960 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4961 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4962 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4963 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4964 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4965 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4966 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4967 | } |
| 4968 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4969 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4970 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4971 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4972 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4973 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4974 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4975 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4976 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4977 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4978 | } |
| 4979 | |
| 4980 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4981 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4982 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4983 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4984 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4985 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4986 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4987 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4988 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4989 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4990 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4991 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4992 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4993 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4994 | |
| 4995 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4996 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4997 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4998 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4999 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5000 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5001 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5002 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5003 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5004 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5005 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5006 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 5007 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5008 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5009 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5010 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 5011 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5012 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 5013 | return -140; |
| 5014 | } |
| 5015 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5016 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5017 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5018 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5019 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5020 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5021 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5022 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5023 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5024 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5025 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5026 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5027 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5028 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5029 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5030 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5031 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5032 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5033 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5034 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5035 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5036 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5037 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5038 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5039 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5040 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5041 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5042 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5043 | |
| 5044 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5045 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5046 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5047 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5048 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5049 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5050 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 5051 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5052 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5053 | #else /* QCBOR_DISABLE_TAGS */ |
| 5054 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 5055 | return -20; |
| 5056 | } |
| 5057 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5058 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5059 | return 0; |
| 5060 | } |
| 5061 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5062 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5063 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 5064 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5065 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5066 | 0x81, // Array of length one |
| 5067 | 0x7f, // text string marked with indefinite length |
| 5068 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5069 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5070 | 0xff // ending break |
| 5071 | }; |
| 5072 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5073 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5074 | 0x81, // Array of length one |
| 5075 | 0x7f, // text string marked with indefinite length |
| 5076 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5077 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 5078 | 0xff // ending break |
| 5079 | }; |
| 5080 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5081 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5082 | 0x81, // Array of length one |
| 5083 | 0x7f, // text string marked with indefinite length |
| 5084 | 0x01, 0x02, // Not a string |
| 5085 | 0xff // ending break |
| 5086 | }; |
| 5087 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5088 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5089 | 0x81, // Array of length one |
| 5090 | 0x7f, // text string marked with indefinite length |
| 5091 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5092 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5093 | // missing end of string |
| 5094 | }; |
| 5095 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5096 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5097 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5098 | 0xa1, // Array of length one |
| 5099 | 0x7f, // text string marked with indefinite length |
| 5100 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 5101 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5102 | 0xff, // ending break |
| 5103 | 0x01 // integer being labeled. |
| 5104 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5105 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5106 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5107 | /** |
| 5108 | Make an indefinite length string |
| 5109 | |
| 5110 | @param Storage Storage for string, must be 144 bytes in size |
| 5111 | @return The indefinite length string |
| 5112 | |
| 5113 | This makes an array with one indefinite length string that has 7 chunks |
| 5114 | from size of 1 byte up to 64 bytes. |
| 5115 | */ |
| 5116 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5117 | { |
| 5118 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5119 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5120 | UsefulOutBuf_Init(&UOB, Storage); |
| 5121 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 5122 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5123 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5124 | uint8_t uStringByte = 0; |
| 5125 | // Use of type int is intentional |
| 5126 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 5127 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5128 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5129 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 5130 | for(int j = 0; j < uChunkSize; j++) { |
| 5131 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 5132 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 5136 | |
| 5137 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5138 | } |
| 5139 | |
| 5140 | static int CheckBigString(UsefulBufC BigString) |
| 5141 | { |
| 5142 | if(BigString.len != 255) { |
| 5143 | return 1; |
| 5144 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5145 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5146 | for(uint8_t i = 0; i < 255; i++){ |
| 5147 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 5148 | return 1; |
| 5149 | } |
| 5150 | } |
| 5151 | return 0; |
| 5152 | } |
| 5153 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5154 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5155 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5156 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5157 | QCBORDecodeContext DC; |
| 5158 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5159 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5160 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5161 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5162 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5163 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5164 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5165 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5166 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5167 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5168 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5169 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5170 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5171 | return -2; |
| 5172 | } |
| 5173 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 5174 | return -3; |
| 5175 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5176 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5177 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5178 | return -4; |
| 5179 | } |
| 5180 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 5181 | return -5; |
| 5182 | } |
| 5183 | if(QCBORDecode_Finish(&DC)) { |
| 5184 | return -6; |
| 5185 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5186 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5187 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5188 | QCBORDecode_Init(&DC, |
| 5189 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 5190 | QCBOR_DECODE_MODE_NORMAL); |
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 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5193 | return -7; |
| 5194 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5195 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5196 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5197 | return -8; |
| 5198 | } |
| 5199 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5200 | return -9; |
| 5201 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5202 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5203 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5204 | return -10; |
| 5205 | } |
| 5206 | |
| 5207 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5208 | QCBORDecode_Init(&DC, |
| 5209 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 5210 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5211 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5212 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5213 | return -11; |
| 5214 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5215 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5216 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5217 | return -12; |
| 5218 | } |
| 5219 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5220 | return -13; |
| 5221 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5222 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5223 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5224 | return -14; |
| 5225 | } |
| 5226 | |
| 5227 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5228 | QCBORDecode_Init(&DC, |
| 5229 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 5230 | QCBOR_DECODE_MODE_NORMAL); |
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 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5233 | return -15; |
| 5234 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5235 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5236 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5237 | return -16; |
| 5238 | } |
| 5239 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5240 | return -17; |
| 5241 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5242 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5243 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 5244 | return -18; |
| 5245 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5246 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5247 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5248 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5249 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5250 | QCBORDecode_GetNext(&DC, &Item); |
| 5251 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5252 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5253 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5254 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5255 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5256 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5257 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5258 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5259 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5260 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5261 | |
| 5262 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 5263 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5264 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5265 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5266 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5267 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 5268 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 5269 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5270 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5271 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 5272 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5273 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5274 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5275 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5276 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5277 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5278 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5279 | QCBORDecode_GetNext(&DC, &Item); |
| 5280 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5281 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5282 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5283 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5284 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5285 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5286 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5287 | // ---- big bstr ----- |
| 5288 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5289 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5290 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5291 | return -25; |
| 5292 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5293 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5294 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5295 | return -26; |
| 5296 | } |
| 5297 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5298 | return -26; |
| 5299 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5300 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5301 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5302 | return -27; |
| 5303 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5304 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5305 | return -28; |
| 5306 | } |
| 5307 | if(CheckBigString(Item.val.string)) { |
| 5308 | return -3; |
| 5309 | } |
| 5310 | if(QCBORDecode_Finish(&DC)) { |
| 5311 | return -29; |
| 5312 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5313 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5314 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5315 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5316 | 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] | 5317 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5318 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5319 | return -30; |
| 5320 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5321 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5322 | QCBORDecode_GetNext(&DC, &Item); |
| 5323 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 5324 | return -31; |
| 5325 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5326 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5327 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 5328 | return -32; |
| 5329 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5330 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5331 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5332 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 5333 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 5334 | return -33; |
| 5335 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5336 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5337 | if(QCBORDecode_Finish(&DC)) { |
| 5338 | return -34; |
| 5339 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5340 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5341 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5342 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5346 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5347 | { |
| 5348 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5349 | QCBORError nCBORError; |
| 5350 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5351 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5352 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5353 | QCBORDecode_Init(&DC, |
| 5354 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 5355 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5356 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5357 | 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] | 5358 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5359 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 5360 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5361 | return -1; |
| 5362 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5363 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5364 | if(CheckCSRMaps(&DC)) { |
| 5365 | return -2; |
| 5366 | } |
| 5367 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5368 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5369 | // Next parse, save pointers to a few strings, destroy original and |
| 5370 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5371 | 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] | 5372 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5373 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5374 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 5375 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5376 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5377 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5378 | QCBORItem Item1, Item2, Item3, Item4; |
| 5379 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5380 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5381 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5382 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5383 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5384 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5385 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5386 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5387 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5388 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5389 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5390 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5391 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5392 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 5393 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5394 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5395 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5396 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 5397 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5398 | Item1.uDataAlloc != 0 || |
| 5399 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5400 | UsefulBufCompareToSZ(Item1.label.string, "first integer") || |
| 5401 | Item1.label.string.ptr < Pool.ptr || |
| 5402 | Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5403 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5404 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5405 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5406 | |
| 5407 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5408 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5409 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5410 | Item2.uDataAlloc != 0 || |
| 5411 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5412 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5413 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5414 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5415 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5416 | Item3.uDataAlloc == 0 || |
| 5417 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5418 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5419 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5420 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5421 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5422 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5423 | Item4.uDataAlloc == 0 || |
| 5424 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5425 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5426 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5427 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5428 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5429 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5430 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5431 | QCBORDecode_Init(&DC, |
| 5432 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 5433 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5434 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 5435 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5436 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5437 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5438 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5439 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5440 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5441 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 5442 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 5443 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 5444 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 5445 | } |
| 5446 | } |
| 5447 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5448 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5449 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5450 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5451 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5452 | |
| 5453 | return 0; |
| 5454 | } |
| 5455 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5456 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5457 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 5458 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5459 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5460 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5461 | QCBORDecodeContext DC; |
| 5462 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 5463 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5464 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5465 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5466 | // Then fish into the internals of the decode context |
| 5467 | // to get the allocator function so it can be called directly. |
| 5468 | // Also figure out how much pool is available for use |
| 5469 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5470 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5471 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 5472 | if(nError) { |
| 5473 | return -9; |
| 5474 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5475 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 5476 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5477 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5478 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5479 | // First test -- ask for one more byte than available and see failure |
| 5480 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5481 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 5482 | return -1; |
| 5483 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5484 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5485 | // Re do the set up for the next test that will do a successful alloc, |
| 5486 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5487 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5488 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5489 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5490 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5491 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5492 | // Allocate one byte less than available and see success |
| 5493 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5494 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5495 | return -2; |
| 5496 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5497 | // Ask for some more and see failure |
| 5498 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5499 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 5500 | return -3; |
| 5501 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5502 | // Free the first allocate, retry the second and see success |
| 5503 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 5504 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5505 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 5506 | return -4; |
| 5507 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5508 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5509 | // Re do set up for next test that involves a successful alloc, |
| 5510 | // and a successful realloc and a failed realloc |
| 5511 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5512 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5513 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5514 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5515 | // Allocate half the pool and see success |
| 5516 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5517 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5518 | return -5; |
| 5519 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5520 | // Reallocate to take up the whole pool and see success |
| 5521 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5522 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 5523 | return -6; |
| 5524 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5525 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5526 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 5527 | return -7; |
| 5528 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5529 | // Try to allocate more to be sure there is failure after a realloc |
| 5530 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 5531 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5532 | return -8; |
| 5533 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5534 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5535 | return 0; |
| 5536 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 5537 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5538 | |
| 5539 | /* Just enough of an allocator to test configuration of one */ |
| 5540 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 5541 | { |
| 5542 | (void)pOldMem; // unused variable |
| 5543 | |
| 5544 | if(uNewSize) { |
| 5545 | // Assumes the context pointer is the buffer and |
| 5546 | // nothing too big will ever be asked for. |
| 5547 | // This is only good for this basic test! |
| 5548 | return (UsefulBuf) {pCtx, uNewSize}; |
| 5549 | } else { |
| 5550 | return NULLUsefulBuf; |
| 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5555 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5556 | { |
| 5557 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5558 | // nothing can be done with it unless that is set up. |
| 5559 | QCBORDecodeContext DC; |
| 5560 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 5561 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 5562 | |
| 5563 | uint8_t pAllocatorBuffer[50]; |
| 5564 | |
| 5565 | // This is really just to test that this call works. |
| 5566 | // The full functionality of string allocators is tested |
| 5567 | // elsewhere with the MemPool internal allocator. |
| 5568 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 5569 | |
| 5570 | QCBORItem Item; |
| 5571 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 5572 | return -1; |
| 5573 | } |
| 5574 | |
| 5575 | if(Item.uDataAlloc == 0 || |
| 5576 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 5577 | Item.val.string.ptr != pAllocatorBuffer) { |
| 5578 | return -2; |
| 5579 | } |
| 5580 | |
| 5581 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 5582 | return -3; |
| 5583 | } |
| 5584 | |
| 5585 | return 0; |
| 5586 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5587 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5588 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5589 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5590 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5591 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5592 | struct EaMTest { |
| 5593 | const char *szName; |
| 5594 | UsefulBufC Input; |
| 5595 | uint8_t uTagRequirement; |
| 5596 | bool bHasTags; |
| 5597 | |
| 5598 | /* Expected values for GetNext */ |
| 5599 | QCBORError uExpectedErrorGN; |
| 5600 | uint8_t uQCBORTypeGN; |
| 5601 | int64_t nExponentGN; |
| 5602 | int64_t nMantissaGN; |
| 5603 | UsefulBufC MantissaGN; |
| 5604 | |
| 5605 | /* Expected values for GetDecimalFraction */ |
| 5606 | QCBORError uExpectedErrorGDF; |
| 5607 | int64_t nExponentGDF; |
| 5608 | int64_t nMantissaGDF; |
| 5609 | |
| 5610 | /* Expected values for GetDecimalFractionBig */ |
| 5611 | QCBORError uExpectedErrorGDFB; |
| 5612 | int64_t nExponentGDFB; |
| 5613 | UsefulBufC MantissaGDFB; |
| 5614 | bool IsNegativeGDFB; |
| 5615 | |
| 5616 | /* Expected values for GetBigFloat */ |
| 5617 | QCBORError uExpectedErrorGBF; |
| 5618 | int64_t nExponentGBF; |
| 5619 | int64_t nMantissaGBF; |
| 5620 | |
| 5621 | /* Expected values for GetBigFloatBig */ |
| 5622 | QCBORError uExpectedErrorGBFB; |
| 5623 | int64_t nExponentGBFB; |
| 5624 | UsefulBufC MantissaGBFB; |
| 5625 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5626 | }; |
| 5627 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5628 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5629 | |
| 5630 | static const struct EaMTest pEaMTests[] = { |
| 5631 | { |
| 5632 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 5633 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5634 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 5635 | false, |
| 5636 | |
| 5637 | QCBOR_SUCCESS, /* for GetNext */ |
| 5638 | QCBOR_TYPE_ARRAY, |
| 5639 | 0, |
| 5640 | 0, |
| 5641 | {(const uint8_t []){0x00}, 1}, |
| 5642 | |
| 5643 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 5644 | -1, |
| 5645 | 3, |
| 5646 | |
| 5647 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5648 | -1, |
| 5649 | {(const uint8_t []){0x03}, 1}, |
| 5650 | false, |
| 5651 | |
| 5652 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5653 | -1, |
| 5654 | 3, |
| 5655 | |
| 5656 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5657 | -1, |
| 5658 | {(const uint8_t []){0x03}, 1}, |
| 5659 | false |
| 5660 | }, |
| 5661 | |
| 5662 | { |
| 5663 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 5664 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5665 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5666 | false, |
| 5667 | |
| 5668 | QCBOR_SUCCESS, /* for GetNext */ |
| 5669 | QCBOR_TYPE_ARRAY, |
| 5670 | 0, |
| 5671 | 0, |
| 5672 | {(const uint8_t []){0x00}, 1}, |
| 5673 | |
| 5674 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5675 | 0, |
| 5676 | 0, |
| 5677 | |
| 5678 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5679 | 0, |
| 5680 | {(const uint8_t []){0x00}, 1}, |
| 5681 | false, |
| 5682 | |
| 5683 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5684 | 0, |
| 5685 | 0, |
| 5686 | |
| 5687 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5688 | 0, |
| 5689 | {(const uint8_t []){0x00}, 1}, |
| 5690 | false |
| 5691 | |
| 5692 | }, |
| 5693 | |
| 5694 | { |
| 5695 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 5696 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 5697 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5698 | true, |
| 5699 | |
| 5700 | QCBOR_SUCCESS, /* for GetNext */ |
| 5701 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 5702 | -1, |
| 5703 | 3, |
| 5704 | {(const uint8_t []){0x00}, 1}, |
| 5705 | |
| 5706 | |
| 5707 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 5708 | -1, |
| 5709 | 3, |
| 5710 | |
| 5711 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5712 | -1, |
| 5713 | {(const uint8_t []){0x03}, 1}, |
| 5714 | false, |
| 5715 | |
| 5716 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5717 | 0, |
| 5718 | 0, |
| 5719 | |
| 5720 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5721 | 0, |
| 5722 | {(const uint8_t []){0x00}, 1}, |
| 5723 | false |
| 5724 | }, |
| 5725 | { |
| 5726 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 5727 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 5728 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5729 | true, |
| 5730 | |
| 5731 | QCBOR_SUCCESS, /* for GetNext */ |
| 5732 | QCBOR_TYPE_BIGFLOAT, |
| 5733 | 300, |
| 5734 | 100, |
| 5735 | {(const uint8_t []){0x00}, 1}, |
| 5736 | |
| 5737 | |
| 5738 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5739 | 0, |
| 5740 | 0, |
| 5741 | |
| 5742 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5743 | 0, |
| 5744 | {(const uint8_t []){0x03}, 1}, |
| 5745 | false, |
| 5746 | |
| 5747 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5748 | 300, |
| 5749 | 100, |
| 5750 | |
| 5751 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5752 | 300, |
| 5753 | {(const uint8_t []){0x64}, 1}, |
| 5754 | false |
| 5755 | }, |
| 5756 | |
| 5757 | { |
| 5758 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 5759 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5760 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 5761 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5762 | true, |
| 5763 | |
| 5764 | QCBOR_SUCCESS, /* for GetNext */ |
| 5765 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5766 | -20, |
| 5767 | 0, |
| 5768 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5769 | |
| 5770 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 5771 | 0, |
| 5772 | 0, |
| 5773 | |
| 5774 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5775 | -20, |
| 5776 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5777 | false, |
| 5778 | |
| 5779 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5780 | 0, |
| 5781 | 0, |
| 5782 | |
| 5783 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5784 | 0, |
| 5785 | {(const uint8_t []){0x00}, 0}, |
| 5786 | false |
| 5787 | }, |
| 5788 | |
| 5789 | { |
| 5790 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 5791 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5792 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 5793 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5794 | true, |
| 5795 | |
| 5796 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 5797 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5798 | 0, |
| 5799 | 0, |
| 5800 | {(const uint8_t []){0x00}, 0}, |
| 5801 | |
| 5802 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 5803 | 0, |
| 5804 | 0, |
| 5805 | |
| 5806 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 5807 | 0, |
| 5808 | {(const uint8_t []){0x00}, 0}, |
| 5809 | false, |
| 5810 | |
| 5811 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 5812 | 0, |
| 5813 | 0, |
| 5814 | |
| 5815 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 5816 | 0, |
| 5817 | {(const uint8_t []){0x00}, 0}, |
| 5818 | false |
| 5819 | }, |
| 5820 | { |
| 5821 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 5822 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 5823 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5824 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5825 | true, |
| 5826 | |
| 5827 | QCBOR_SUCCESS, /* for GetNext */ |
| 5828 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5829 | -20, |
| 5830 | 0, |
| 5831 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5832 | |
| 5833 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5834 | 0, |
| 5835 | 0, |
| 5836 | |
| 5837 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5838 | -20, |
| 5839 | {(const uint8_t []){0x00}, 1}, |
| 5840 | false, |
| 5841 | |
| 5842 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5843 | -20, |
| 5844 | 4294967295, |
| 5845 | |
| 5846 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5847 | -20, |
| 5848 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5849 | false |
| 5850 | }, |
| 5851 | |
| 5852 | { |
| 5853 | /* Special case for test 8. Don't renumber it. */ |
| 5854 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 5855 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 5856 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5857 | true, |
| 5858 | |
| 5859 | QCBOR_SUCCESS, /* for GetNext */ |
| 5860 | QCBOR_TYPE_ARRAY, |
| 5861 | 0, |
| 5862 | 0, |
| 5863 | {(const uint8_t []){0x00}, 1}, |
| 5864 | |
| 5865 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5866 | 0, |
| 5867 | 0, |
| 5868 | |
| 5869 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5870 | -20, |
| 5871 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5872 | false, |
| 5873 | |
| 5874 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 5875 | 0, |
| 5876 | 0, |
| 5877 | |
| 5878 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5879 | -20, |
| 5880 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5881 | false |
| 5882 | }, |
| 5883 | |
| 5884 | { |
| 5885 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 5886 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5887 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 5888 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5889 | true, |
| 5890 | |
| 5891 | QCBOR_SUCCESS, /* for GetNext */ |
| 5892 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 5893 | 9223372036854775807, |
| 5894 | 0, |
| 5895 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5896 | |
| 5897 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5898 | 0, |
| 5899 | 0, |
| 5900 | |
| 5901 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5902 | 9223372036854775807, |
| 5903 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5904 | true, |
| 5905 | |
| 5906 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5907 | 0, |
| 5908 | 0, |
| 5909 | |
| 5910 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5911 | 0, |
| 5912 | {(const uint8_t []){0x00}, 1}, |
| 5913 | false |
| 5914 | }, |
| 5915 | }; |
| 5916 | |
| 5917 | |
| 5918 | |
| 5919 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5920 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5921 | size_t uIndex; |
| 5922 | QCBORDecodeContext DCtx; |
| 5923 | QCBORItem Item; |
| 5924 | QCBORError uError; |
| 5925 | int64_t nMantissa, nExponent; |
| 5926 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 5927 | UsefulBufC Mantissa; |
| 5928 | bool bMantissaIsNegative; |
| 5929 | |
| 5930 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 5931 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 5932 | /* Decode with GetNext */ |
| 5933 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5934 | |
| 5935 | if(uIndex + 1 == 9) { |
| 5936 | nExponent = 99; // just to set a break point |
| 5937 | } |
| 5938 | |
| 5939 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 5940 | #ifdef QCBOR_DISABLE_TAGS |
| 5941 | /* Test 8 is a special case when tags are disabled */ |
| 5942 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 5943 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5944 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 5945 | } |
| 5946 | } else { |
| 5947 | #endif |
| 5948 | /* Now check return code, data type, mantissa and exponent */ |
| 5949 | if(pT->uExpectedErrorGN != uError) { |
| 5950 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 5951 | } |
| 5952 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 5953 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 5954 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 5955 | } |
| 5956 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 5957 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 5958 | } |
| 5959 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 5960 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 5961 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 5962 | } |
| 5963 | } else { |
| 5964 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 5965 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 5966 | } |
| 5967 | } |
| 5968 | } |
| 5969 | #ifdef QCBOR_DISABLE_TAGS |
| 5970 | } |
| 5971 | #endif |
| 5972 | |
| 5973 | /* Decode with GetDecimalFraction */ |
| 5974 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5975 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 5976 | pT->uTagRequirement, |
| 5977 | &nMantissa, |
| 5978 | &nExponent); |
| 5979 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5980 | #ifdef QCBOR_DISABLE_TAGS |
| 5981 | if(pT->bHasTags) { |
| 5982 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5983 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 5984 | } |
| 5985 | } else { |
| 5986 | #endif |
| 5987 | /* Now check return code, mantissa and exponent */ |
| 5988 | if(pT->uExpectedErrorGDF != uError) { |
| 5989 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 5990 | } |
| 5991 | if(uError == QCBOR_SUCCESS) { |
| 5992 | if(pT->nExponentGDF != nExponent) { |
| 5993 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 5994 | } |
| 5995 | if(pT->nMantissaGDF != nMantissa) { |
| 5996 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 5997 | } |
| 5998 | } |
| 5999 | #ifdef QCBOR_DISABLE_TAGS |
| 6000 | } |
| 6001 | #endif |
| 6002 | |
| 6003 | /* Decode with GetDecimalFractionBig */ |
| 6004 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6005 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 6006 | pT->uTagRequirement, |
| 6007 | MantissaBuf, |
| 6008 | &Mantissa, |
| 6009 | &bMantissaIsNegative, |
| 6010 | &nExponent); |
| 6011 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6012 | #ifdef QCBOR_DISABLE_TAGS |
| 6013 | if(pT->bHasTags) { |
| 6014 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6015 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 6016 | } |
| 6017 | } else { |
| 6018 | #endif |
| 6019 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 6020 | if(pT->uExpectedErrorGDFB != uError) { |
| 6021 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 6022 | } |
| 6023 | if(uError == QCBOR_SUCCESS) { |
| 6024 | if(pT->nExponentGDFB != nExponent) { |
| 6025 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 6026 | } |
| 6027 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 6028 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 6029 | } |
| 6030 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 6031 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 6032 | } |
| 6033 | } |
| 6034 | #ifdef QCBOR_DISABLE_TAGS |
| 6035 | } |
| 6036 | #endif |
| 6037 | |
| 6038 | /* Decode with GetBigFloat */ |
| 6039 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6040 | QCBORDecode_GetBigFloat(&DCtx, |
| 6041 | pT->uTagRequirement, |
| 6042 | &nMantissa, |
| 6043 | &nExponent); |
| 6044 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6045 | #ifdef QCBOR_DISABLE_TAGS |
| 6046 | if(pT->bHasTags) { |
| 6047 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6048 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 6049 | } |
| 6050 | } else { |
| 6051 | #endif |
| 6052 | /* Now check return code, mantissa and exponent */ |
| 6053 | if(pT->uExpectedErrorGBF != uError) { |
| 6054 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 6055 | } |
| 6056 | if(uError == QCBOR_SUCCESS) { |
| 6057 | if(pT->nExponentGBF != nExponent) { |
| 6058 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 6059 | } |
| 6060 | if(pT->nMantissaGBF != nMantissa) { |
| 6061 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 6062 | } |
| 6063 | } |
| 6064 | #ifdef QCBOR_DISABLE_TAGS |
| 6065 | } |
| 6066 | #endif |
| 6067 | |
| 6068 | /* Decode with GetBigFloatBig */ |
| 6069 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6070 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 6071 | pT->uTagRequirement, |
| 6072 | MantissaBuf, |
| 6073 | &Mantissa, |
| 6074 | &bMantissaIsNegative, |
| 6075 | &nExponent); |
| 6076 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6077 | #ifdef QCBOR_DISABLE_TAGS |
| 6078 | if(pT->bHasTags) { |
| 6079 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6080 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 6081 | } |
| 6082 | } else { |
| 6083 | #endif |
| 6084 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 6085 | if(pT->uExpectedErrorGBFB != uError) { |
| 6086 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 6087 | } |
| 6088 | if(uError == QCBOR_SUCCESS) { |
| 6089 | if(pT->nExponentGBFB != nExponent) { |
| 6090 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 6091 | } |
| 6092 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 6093 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 6094 | } |
| 6095 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 6096 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 6097 | } |
| 6098 | } |
| 6099 | #ifdef QCBOR_DISABLE_TAGS |
| 6100 | } |
| 6101 | #endif |
| 6102 | } |
| 6103 | |
| 6104 | return 0; |
| 6105 | } |
| 6106 | |
| 6107 | |
| 6108 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 6109 | { |
| 6110 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6111 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6112 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6113 | QCBORItem item; |
| 6114 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 6115 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 6116 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 6117 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6118 | |
| 6119 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6120 | |
| 6121 | /* Now encode some stuff and then decode it */ |
| 6122 | uint8_t pBuf[40]; |
| 6123 | QCBOREncodeContext EC; |
| 6124 | UsefulBufC Encoded; |
| 6125 | |
| 6126 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 6127 | QCBOREncode_OpenArray(&EC); |
| 6128 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 6129 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 6130 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 6131 | QCBOREncode_CloseArray(&EC); |
| 6132 | QCBOREncode_Finish(&EC, &Encoded); |
| 6133 | |
| 6134 | |
| 6135 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6136 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6137 | if(uErr != QCBOR_SUCCESS) { |
| 6138 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6139 | } |
| 6140 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6141 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6142 | if(uErr != QCBOR_SUCCESS) { |
| 6143 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 6147 | item.val.expAndMantissa.nExponent != 1000 || |
| 6148 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6149 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6150 | } |
| 6151 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6152 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6153 | if(uErr != QCBOR_SUCCESS) { |
| 6154 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 6158 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 6159 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6160 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6161 | } |
| 6162 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6163 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6164 | if(uErr != QCBOR_SUCCESS) { |
| 6165 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 6169 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 6170 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6171 | return 106; |
| 6172 | } |
| 6173 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6174 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6175 | |
| 6176 | return 0; |
| 6177 | } |
| 6178 | |
| 6179 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6180 | int32_t ExponentAndMantissaDecodeTests(void) |
| 6181 | { |
| 6182 | int32_t rv = ProcessEaMTests(); |
| 6183 | if(rv) { |
| 6184 | return rv; |
| 6185 | } |
| 6186 | |
| 6187 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 6188 | } |
| 6189 | |
| 6190 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6191 | static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = { |
| 6192 | { "Exponent > INT64_MAX", |
| 6193 | QCBOR_DECODE_MODE_NORMAL, |
| 6194 | {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20}, |
| 6195 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6196 | }, |
| 6197 | { "Mantissa > INT64_MAX", |
| 6198 | QCBOR_DECODE_MODE_NORMAL, |
| 6199 | {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23}, |
| 6200 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6201 | }, |
| 6202 | { |
| 6203 | "End of input", |
| 6204 | QCBOR_DECODE_MODE_NORMAL, |
| 6205 | {"\xC4\x82", 2}, |
| 6206 | QCBOR_ERR_NO_MORE_ITEMS |
| 6207 | }, |
| 6208 | {"bad content for big num", |
| 6209 | QCBOR_DECODE_MODE_NORMAL, |
| 6210 | {"\xC4\x82\x01\xc3\x01", 5}, |
| 6211 | QCBOR_ERR_BAD_OPT_TAG |
| 6212 | }, |
| 6213 | {"bad content for big num", |
| 6214 | QCBOR_DECODE_MODE_NORMAL, |
| 6215 | {"\xC4\x82\xc2\x01\x1f", 5}, |
| 6216 | QCBOR_ERR_BAD_INT |
| 6217 | }, |
| 6218 | {"Bad integer for exponent", |
| 6219 | QCBOR_DECODE_MODE_NORMAL, |
| 6220 | {"\xC4\x82\x01\x1f", 4}, |
| 6221 | QCBOR_ERR_BAD_INT |
| 6222 | }, |
| 6223 | {"Bad integer for mantissa", |
| 6224 | QCBOR_DECODE_MODE_NORMAL, |
| 6225 | {"\xC4\x82\x1f\x01", 4}, |
| 6226 | QCBOR_ERR_BAD_INT |
| 6227 | }, |
| 6228 | {"3 items in array", |
| 6229 | QCBOR_DECODE_MODE_NORMAL, |
| 6230 | {"\xC4\x83\x03\x01\x02", 5}, |
| 6231 | QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6232 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6233 | {"unterminated indefinite length array", |
| 6234 | QCBOR_DECODE_MODE_NORMAL, |
| 6235 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6236 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6237 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6238 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6239 | {"unterminated indefinite length array", |
| 6240 | QCBOR_DECODE_MODE_NORMAL, |
| 6241 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6242 | QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 6243 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6244 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6245 | {"Empty array", |
| 6246 | QCBOR_DECODE_MODE_NORMAL, |
| 6247 | {"\xC4\x80", 2}, |
| 6248 | QCBOR_ERR_NO_MORE_ITEMS |
| 6249 | }, |
| 6250 | {"Second is not an integer", |
| 6251 | QCBOR_DECODE_MODE_NORMAL, |
| 6252 | {"\xC4\x82\x03\x40", 4}, |
| 6253 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6254 | }, |
| 6255 | {"First is not an integer", |
| 6256 | QCBOR_DECODE_MODE_NORMAL, |
| 6257 | {"\xC4\x82\x40", 3}, |
| 6258 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6259 | }, |
| 6260 | {"Not an array", |
| 6261 | QCBOR_DECODE_MODE_NORMAL, |
| 6262 | {"\xC4\xA2", 2}, |
| 6263 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6264 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6265 | }; |
| 6266 | |
| 6267 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6268 | int32_t |
| 6269 | ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6270 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6271 | return ProcessDecodeFailures(ExponentAndMantissaFailures, |
| 6272 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 6273 | struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6274 | } |
| 6275 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6276 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6277 | |
| 6278 | |
| 6279 | |
| 6280 | /* |
| 6281 | Some basic CBOR with map and array used in a lot of tests. |
| 6282 | The map labels are all strings |
| 6283 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6284 | { |
| 6285 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6286 | "an array of two strings": [ |
| 6287 | "string1", "string2" |
| 6288 | ], |
| 6289 | "map in a map": { |
| 6290 | "bytes 1": h'78787878', |
| 6291 | "bytes 2": h'79797979', |
| 6292 | "another int": 98, |
| 6293 | "text 2": "lies, damn lies and statistics" |
| 6294 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6295 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6296 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6297 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6298 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6299 | { |
| 6300 | QCBORItem Item1, Item2, Item3; |
| 6301 | int64_t nDecodedInt1, nDecodedInt2; |
| 6302 | UsefulBufC B1, B2, S1, S2, S3; |
| 6303 | |
| 6304 | QCBORDecodeContext DCtx; |
| 6305 | QCBORError nCBORError; |
| 6306 | |
| 6307 | QCBORDecode_Init(&DCtx, input, 0); |
| 6308 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6309 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6310 | |
| 6311 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 6312 | |
| 6313 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6314 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6315 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 6316 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 6317 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6318 | QCBORDecode_ExitMap(&DCtx); |
| 6319 | |
| 6320 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6321 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6322 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 6323 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6324 | return -400; |
| 6325 | } |
| 6326 | QCBORDecode_ExitArray(&DCtx); |
| 6327 | |
| 6328 | // Parse the same array again using GetText() instead of GetItem() |
| 6329 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6330 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 6331 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6332 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6333 | return 5000; |
| 6334 | } |
| 6335 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 6336 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6337 | return 5001; |
| 6338 | } */ |
| 6339 | |
| 6340 | QCBORDecode_ExitArray(&DCtx); |
| 6341 | |
| 6342 | QCBORDecode_ExitMap(&DCtx); |
| 6343 | |
| 6344 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 6345 | |
| 6346 | if(nCBORError) { |
| 6347 | return (int32_t)nCBORError; |
| 6348 | } |
| 6349 | |
| 6350 | if(nDecodedInt1 != 42) { |
| 6351 | return 1001; |
| 6352 | } |
| 6353 | |
| 6354 | if(nDecodedInt2 != 98) { |
| 6355 | return 1002; |
| 6356 | } |
| 6357 | |
| 6358 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6359 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6360 | return 1003; |
| 6361 | } |
| 6362 | |
| 6363 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6364 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6365 | return 1004; |
| 6366 | } |
| 6367 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6368 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6369 | return 1005; |
| 6370 | } |
| 6371 | |
| 6372 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 6373 | return 1006; |
| 6374 | } |
| 6375 | |
| 6376 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 6377 | return 1007; |
| 6378 | } |
| 6379 | |
| 6380 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 6381 | return 1008; |
| 6382 | } |
| 6383 | |
| 6384 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 6385 | return 1009; |
| 6386 | } |
| 6387 | |
| 6388 | return 0; |
| 6389 | } |
| 6390 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6391 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6392 | { |
| 6393 | -75008: h'05083399', |
| 6394 | 88: [], |
| 6395 | 100100: { |
| 6396 | "sub1": { |
| 6397 | 10: [ |
| 6398 | 0 |
| 6399 | ], |
| 6400 | -75009: h'A46823990001', |
| 6401 | 100100: { |
| 6402 | "json": "{ \"ueid\", \"xyz\"}", |
| 6403 | "subsub": { |
| 6404 | 100002: h'141813191001' |
| 6405 | } |
| 6406 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6407 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6408 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6409 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6410 | */ |
| 6411 | |
| 6412 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6413 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 6414 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 6415 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 6416 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 6417 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 6418 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 6419 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 6420 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 6421 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 6422 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 6423 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 6424 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6425 | }; |
| 6426 | |
| 6427 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 6428 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 6429 | { |
| 6430 | UsefulBufC String; |
| 6431 | |
| 6432 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 6433 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 6434 | |
| 6435 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6436 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 6437 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6438 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 6439 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 6440 | if(QCBORDecode_GetError(pDCtx)) { |
| 6441 | return 4001; |
| 6442 | } |
| 6443 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 6444 | return 4002; |
| 6445 | } |
| 6446 | QCBORDecode_ExitMap(pDCtx); |
| 6447 | QCBORDecode_ExitMap(pDCtx); |
| 6448 | QCBORDecode_ExitMap(pDCtx); |
| 6449 | QCBORDecode_ExitMap(pDCtx); |
| 6450 | |
| 6451 | return 0; |
| 6452 | } |
| 6453 | |
| 6454 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 6455 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 6456 | { |
| 6457 | QCBORItem Item; |
| 6458 | QCBORError uErr; |
| 6459 | |
| 6460 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 6461 | for(int x = 0; x < 20; x++) { |
| 6462 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6463 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6464 | return 4100; |
| 6465 | |
| 6466 | } |
| 6467 | } |
| 6468 | QCBORDecode_ExitArray(pDCtx); |
| 6469 | if(QCBORDecode_GetError(pDCtx)) { |
| 6470 | return 4101; |
| 6471 | } |
| 6472 | |
| 6473 | return 0; |
| 6474 | } |
| 6475 | |
| 6476 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 6477 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 6478 | { |
| 6479 | QCBORError uErr; |
| 6480 | |
| 6481 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6482 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 6483 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6484 | int64_t nInt = 99; |
| 6485 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 6486 | if(nInt != 0) { |
| 6487 | return 4200; |
| 6488 | } |
| 6489 | for(int x = 0; x < 20; x++) { |
| 6490 | QCBORItem Item; |
| 6491 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6492 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6493 | return 4201; |
| 6494 | |
| 6495 | } |
| 6496 | } |
| 6497 | QCBORDecode_ExitArray(pDCtx); |
| 6498 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 6499 | return 4202; |
| 6500 | } |
| 6501 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6502 | UsefulBufC dD; |
| 6503 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6504 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6505 | return 4203; |
| 6506 | } |
| 6507 | for(int x = 0; x < 20; x++) { |
| 6508 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6509 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 6510 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6511 | return 4204; |
| 6512 | } |
| 6513 | } |
| 6514 | QCBORDecode_ExitArray(pDCtx); |
| 6515 | QCBORDecode_ExitMap(pDCtx); |
| 6516 | QCBORDecode_ExitMap(pDCtx); |
| 6517 | |
| 6518 | return 0; |
| 6519 | } |
| 6520 | |
| 6521 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 6522 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 6523 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6524 | { |
| 6525 | QCBORDecodeContext DCtx; |
| 6526 | int32_t nReturn; |
| 6527 | QCBORError uErr; |
| 6528 | |
| 6529 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 6530 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6531 | |
| 6532 | for(int j = 0; j < 5; j++) { |
| 6533 | for(int i = 0; i < 20; i++) { |
| 6534 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 6535 | if(nReturn) { |
| 6536 | return nReturn; |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | for(int i = 0; i < 20; i++) { |
| 6541 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 6542 | if(nReturn ) { |
| 6543 | return nReturn; |
| 6544 | } |
| 6545 | } |
| 6546 | |
| 6547 | for(int i = 0; i < 20; i++) { |
| 6548 | nReturn = DecodeNestedGetZero(&DCtx); |
| 6549 | if(nReturn ) { |
| 6550 | return nReturn; |
| 6551 | } |
| 6552 | } |
| 6553 | } |
| 6554 | |
| 6555 | QCBORDecode_ExitMap(&DCtx); |
| 6556 | uErr = QCBORDecode_Finish(&DCtx); |
| 6557 | if(uErr) { |
| 6558 | return (int32_t)uErr + 4100; |
| 6559 | } |
| 6560 | |
| 6561 | return 0; |
| 6562 | } |
| 6563 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6564 | |
| 6565 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6566 | [ |
| 6567 | 23, |
| 6568 | 6000, |
| 6569 | h'67616C6163746963', |
| 6570 | h'686176656E20746F6B656E' |
| 6571 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6572 | */ |
| 6573 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6574 | 0x84, |
| 6575 | 0x17, |
| 6576 | 0x19, 0x17, 0x70, |
| 6577 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 6578 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6579 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6580 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6581 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 6582 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6583 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6584 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 6585 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6586 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6587 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6588 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6589 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6590 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6591 | /* |
| 6592 | { |
| 6593 | 0: [], |
| 6594 | 9: [ |
| 6595 | [], |
| 6596 | [] |
| 6597 | ], |
| 6598 | 8: { |
| 6599 | 1: [], |
| 6600 | 2: {}, |
| 6601 | 3: [] |
| 6602 | }, |
| 6603 | 4: {}, |
| 6604 | 5: [], |
| 6605 | 6: [ |
| 6606 | [], |
| 6607 | [] |
| 6608 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6609 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6610 | */ |
| 6611 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6612 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 6613 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 6614 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 6615 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6616 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6617 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6618 | |
| 6619 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6620 | /* |
| 6621 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6622 | Duplicate label |
| 6623 | Integer overflow |
| 6624 | Date overflow |
| 6625 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6626 | { |
| 6627 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6628 | 3: -18446744073709551616, |
| 6629 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6630 | 5: 0, |
| 6631 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6632 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6633 | */ |
| 6634 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6635 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6636 | 0xa5, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6637 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6638 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 6639 | #else |
| 6640 | 0xa4, |
| 6641 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6642 | 0x05, 0x00, |
| 6643 | 0x05, 0x00, |
| 6644 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6645 | }; |
| 6646 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6647 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6648 | static const uint8_t spUnRecoverableMapError1[] = { |
| 6649 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6650 | }; |
| 6651 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6652 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6653 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6654 | static const uint8_t spUnRecoverableMapError2[] = { |
| 6655 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6656 | }; |
| 6657 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6658 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6659 | static const uint8_t spUnRecoverableMapError3[] = { |
| 6660 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 6661 | }; |
| 6662 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6663 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6664 | static const uint8_t spUnRecoverableMapError4[] = { |
| 6665 | 0xbf, |
| 6666 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6667 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6668 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6669 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6670 | 0xff |
| 6671 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6672 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6673 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6674 | const unsigned char not_well_formed_submod_section[] = { |
| 6675 | 0xa1, 0x14, 0x1f, |
| 6676 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6677 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6678 | |
| 6679 | /* Array of length 3, but only two items. */ |
| 6680 | const unsigned char spBadConsumeInput[] = { |
| 6681 | 0x83, 0x00, 0x00 |
| 6682 | }; |
| 6683 | |
| 6684 | /* Tag nesting too deep. */ |
| 6685 | const unsigned char spBadConsumeInput2[] = { |
| 6686 | 0x81, |
| 6687 | 0xD8, 0x37, |
| 6688 | 0xD8, 0x2C, |
| 6689 | 0xD8, 0x21, |
| 6690 | 0xD6, |
| 6691 | 0xCB, |
| 6692 | 00 |
| 6693 | }; |
| 6694 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6695 | |
| 6696 | const unsigned char spBadConsumeInput4[] = { |
| 6697 | 0x81, 0x9f, 0x00, 0xff |
| 6698 | }; |
| 6699 | |
| 6700 | const unsigned char spBadConsumeInput5[] = { |
| 6701 | 0xa1, 0x80, 0x00 |
| 6702 | }; |
| 6703 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6704 | /* |
| 6705 | Lots of nesting for various nesting tests. |
| 6706 | { 1:1, |
| 6707 | 2:{ |
| 6708 | 21:21, |
| 6709 | 22:{ |
| 6710 | 221:[2111, 2112, 2113], |
| 6711 | 222:222, |
| 6712 | 223: {} |
| 6713 | }, |
| 6714 | 23: 23 |
| 6715 | }, |
| 6716 | 3:3, |
| 6717 | 4: [ {} ] |
| 6718 | } |
| 6719 | */ |
| 6720 | static const uint8_t spNested[] = { |
| 6721 | 0xA4, /* Map of 4 */ |
| 6722 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 6723 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 6724 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 6725 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 6726 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 6727 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 6728 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 6729 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 6730 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 6731 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 6732 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 6733 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 6734 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 6735 | 0xA0 /* Array entry {}, an empty map */ |
| 6736 | }; |
| 6737 | |
| 6738 | |
| 6739 | static int32_t EnterMapCursorTest(void) |
| 6740 | { |
| 6741 | QCBORDecodeContext DCtx; |
| 6742 | QCBORItem Item1; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6743 | int64_t nInt; |
| 6744 | QCBORError uErr; |
| 6745 | |
| 6746 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6747 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6748 | QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt); |
| 6749 | uErr = QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 6750 | if(uErr != QCBOR_SUCCESS) { |
| 6751 | return 701; |
| 6752 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6753 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6754 | return 700; |
| 6755 | } |
| 6756 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6757 | |
| 6758 | int i; |
| 6759 | for(i = 0; i < 13; i++) { |
| 6760 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6761 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6762 | int j; |
| 6763 | /* Move travesal cursor */ |
| 6764 | for(j = 0; j < i; j++) { |
| 6765 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6766 | } |
| 6767 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6768 | QCBORDecode_ExitMap(&DCtx); |
| 6769 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6770 | if(Item1.label.int64 != 3) { |
| 6771 | return 8000; |
| 6772 | } |
| 6773 | } |
| 6774 | |
| 6775 | for(i = 0; i < 13; i++) { |
| 6776 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6777 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6778 | int j; |
| 6779 | /* Move travesal cursor */ |
| 6780 | for(j = 0; j < i; j++) { |
| 6781 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6782 | } |
| 6783 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6784 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6785 | QCBORDecode_ExitMap(&DCtx); |
| 6786 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6787 | if(Item1.label.int64 != 23) { |
| 6788 | return 8000; |
| 6789 | } |
| 6790 | } |
| 6791 | |
| 6792 | for(i = 0; i < 13; i++) { |
| 6793 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6794 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6795 | int j; |
| 6796 | /* Move travesal cursor */ |
| 6797 | for(j = 0; j < i; j++) { |
| 6798 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6799 | } |
| 6800 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6801 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6802 | for(j = 0; j < i; j++) { |
| 6803 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6804 | } |
| 6805 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 6806 | QCBORDecode_ExitArray(&DCtx); |
| 6807 | QCBORDecode_ExitMap(&DCtx); |
| 6808 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6809 | if(Item1.label.int64 != 23) { |
| 6810 | return 8000; |
| 6811 | } |
| 6812 | QCBORDecode_ExitMap(&DCtx); |
| 6813 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6814 | if(Item1.label.int64 != 3) { |
| 6815 | return 8000; |
| 6816 | } |
| 6817 | } |
| 6818 | |
| 6819 | return 0; |
| 6820 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6821 | |
| 6822 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6823 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6824 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6825 | QCBORItem Item1; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6826 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6827 | int32_t nReturn; |
| 6828 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6829 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6830 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6831 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6832 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6833 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6834 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6835 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6836 | QCBORDecode_ExitArray(&DCtx); |
| 6837 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6838 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 6839 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6840 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6841 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6842 | QCBORDecode_ExitArray(&DCtx); |
| 6843 | QCBORDecode_ExitArray(&DCtx); |
| 6844 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6845 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 6846 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6847 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6848 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6849 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6850 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6851 | QCBORDecode_ExitArray(&DCtx); |
| 6852 | QCBORDecode_ExitMap(&DCtx); |
| 6853 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6854 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6855 | QCBORDecode_ExitMap(&DCtx); |
| 6856 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6857 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6858 | QCBORDecode_ExitArray(&DCtx); |
| 6859 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6860 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 6861 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6862 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6863 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6864 | QCBORDecode_ExitArray(&DCtx); |
| 6865 | QCBORDecode_ExitArray(&DCtx); |
| 6866 | |
| 6867 | QCBORDecode_ExitMap(&DCtx); |
| 6868 | |
| 6869 | uErr = QCBORDecode_Finish(&DCtx); |
| 6870 | if(uErr != QCBOR_SUCCESS){ |
| 6871 | return 3011; |
| 6872 | } |
| 6873 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6874 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6875 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6876 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6877 | if(nReturn) { |
| 6878 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6879 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6880 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 6881 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6882 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6883 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 6884 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6885 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6886 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6887 | if(nReturn) { |
| 6888 | return nReturn; |
| 6889 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6890 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6891 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6892 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 6893 | // These tests confirm the cursor is at the right place after entering |
| 6894 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6895 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6896 | |
| 6897 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6898 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6899 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6900 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6901 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6902 | return 2001; |
| 6903 | } |
| 6904 | |
| 6905 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6906 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 6907 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6908 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6909 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 6910 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6911 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6912 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 6913 | return 2051; |
| 6914 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6915 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6916 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 6917 | return 2002; |
| 6918 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6919 | QCBORDecode_ExitArray(&DCtx); |
| 6920 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 6921 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6922 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6923 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 6924 | return 2052; |
| 6925 | } |
| 6926 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6927 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6928 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6929 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6930 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6931 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6932 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6933 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6934 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6935 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 6936 | return 2003; |
| 6937 | } |
| 6938 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6939 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6940 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6941 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6942 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6943 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6944 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6945 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6946 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6947 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6948 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6949 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6950 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6951 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6952 | } |
| 6953 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6954 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6955 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 6956 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6957 | QCBORDecode_ExitArray(&DCtx); |
| 6958 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6959 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 6960 | return 2006; |
| 6961 | } |
| 6962 | QCBORDecode_ExitMap(&DCtx); |
| 6963 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6964 | return 2007; |
| 6965 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6966 | #endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 6967 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6968 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6969 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6970 | int64_t nDecodedInt2; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6971 | |
| 6972 | UsefulBufC String; |
| 6973 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
| 6974 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6975 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
| 6976 | return 2009; |
| 6977 | } |
| 6978 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6979 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6980 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6981 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6982 | return 2008; |
| 6983 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6984 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6985 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6986 | |
| 6987 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6988 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6989 | // This will fail because the map is empty. |
| 6990 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6991 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6992 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6993 | return 2010; |
| 6994 | } |
| 6995 | QCBORDecode_ExitMap(&DCtx); |
| 6996 | uErr = QCBORDecode_Finish(&DCtx); |
| 6997 | if(uErr != QCBOR_SUCCESS){ |
| 6998 | return 2011; |
| 6999 | } |
| 7000 | |
| 7001 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7002 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7003 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7004 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7005 | // This will fail because the map is empty. |
| 7006 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 7007 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7008 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 7009 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7010 | } |
| 7011 | QCBORDecode_ExitMap(&DCtx); |
| 7012 | uErr = QCBORDecode_Finish(&DCtx); |
| 7013 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 7014 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7015 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7016 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7017 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7018 | |
| 7019 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7020 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 7021 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7022 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7023 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7024 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7025 | QCBORDecode_ExitArray(&DCtx); |
| 7026 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 7027 | QCBORDecode_ExitArray(&DCtx); |
| 7028 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7029 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7030 | return 2014; |
| 7031 | } |
| 7032 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7033 | int64_t nInt; |
| 7034 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7035 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7036 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7037 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7038 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7039 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 7040 | return 2021; |
| 7041 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7042 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 7043 | return 2121; |
| 7044 | } |
| 7045 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7046 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7047 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7048 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7049 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7050 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 7051 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7052 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7053 | return 2024; |
| 7054 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7055 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7056 | |
| 7057 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 7058 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7059 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 7060 | return 2025; |
| 7061 | } |
| 7062 | |
| 7063 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 7064 | |
| 7065 | QCBORDecode_ExitMap(&DCtx); |
| 7066 | uErr = QCBORDecode_Finish(&DCtx); |
| 7067 | if(uErr != QCBOR_SUCCESS) { |
| 7068 | return 2026; |
| 7069 | } |
| 7070 | |
| 7071 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7072 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7073 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7074 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7075 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 7076 | return 2030; |
| 7077 | } |
| 7078 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7079 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7080 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7081 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7082 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7083 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7084 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7085 | return 2031; |
| 7086 | } |
| 7087 | |
| 7088 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7089 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7090 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7091 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7092 | if(uErr != QCBOR_ERR_HIT_END) { |
| 7093 | return 2032; |
| 7094 | } |
| 7095 | |
| 7096 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7097 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7098 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7099 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7100 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 7101 | return 2033; |
| 7102 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7103 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7104 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7105 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 7106 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7107 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7108 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 7109 | return 2401; |
| 7110 | } |
| 7111 | if(QCBORDecode_GetError(&DCtx)) { |
| 7112 | return 2402; |
| 7113 | } |
| 7114 | |
| 7115 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7116 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 7117 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7118 | Item1.val.uCount != 3 || |
| 7119 | Item1.uNextNestLevel != 1) { |
| 7120 | return 2403; |
| 7121 | } |
| 7122 | if(QCBORDecode_GetError(&DCtx)) { |
| 7123 | return 2404; |
| 7124 | } |
| 7125 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7126 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 7127 | Item1.uNextNestLevel != 1 || |
| 7128 | Item1.val.int64 != 42) { |
| 7129 | return 2405; |
| 7130 | } |
| 7131 | if(QCBORDecode_GetError(&DCtx)) { |
| 7132 | return 2406; |
| 7133 | } |
| 7134 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7135 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 7136 | Item1.uNestingLevel != 1 || |
| 7137 | Item1.uNextNestLevel != 1 || |
| 7138 | Item1.val.uCount != 2) { |
| 7139 | return 2407; |
| 7140 | } |
| 7141 | if(QCBORDecode_GetError(&DCtx)) { |
| 7142 | return 2408; |
| 7143 | } |
| 7144 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7145 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7146 | Item1.uNestingLevel != 1 || |
| 7147 | Item1.uNextNestLevel != 0 || |
| 7148 | Item1.val.uCount != 4) { |
| 7149 | return 2409; |
| 7150 | } |
| 7151 | if(QCBORDecode_GetError(&DCtx)) { |
| 7152 | return 2410; |
| 7153 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7154 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7155 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7156 | nReturn = DecodeNestedIterate(); |
| 7157 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 7158 | |
| 7159 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 7160 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7161 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 7162 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 7163 | return 2500; |
| 7164 | } |
| 7165 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7166 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 7167 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7168 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7169 | return 2600; |
| 7170 | } |
| 7171 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7172 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7173 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 7174 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7175 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7176 | return 2700; |
| 7177 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7178 | #endif |
| 7179 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7180 | |
| 7181 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 7182 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7183 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7184 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7185 | return 2900; |
| 7186 | } |
| 7187 | #else |
| 7188 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7189 | return 2901; |
| 7190 | } |
| 7191 | #endif |
| 7192 | |
| 7193 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 7194 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7195 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 7196 | return 3000; |
| 7197 | } |
| 7198 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 7199 | nReturn = EnterMapCursorTest(); |
| 7200 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7201 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7202 | } |
| 7203 | |
| 7204 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7205 | struct NumberConversion { |
| 7206 | char *szDescription; |
| 7207 | UsefulBufC CBOR; |
| 7208 | int64_t nConvertedToInt64; |
| 7209 | QCBORError uErrorInt64; |
| 7210 | uint64_t uConvertToUInt64; |
| 7211 | QCBORError uErrorUint64; |
| 7212 | double dConvertToDouble; |
| 7213 | QCBORError uErrorDouble; |
| 7214 | }; |
| 7215 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7216 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 7217 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 7218 | #else |
| 7219 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 7220 | #endif |
| 7221 | |
| 7222 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7223 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7224 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7225 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 7226 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 7227 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 7228 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 7229 | }, 15}, |
| 7230 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 7231 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 7232 | 0, |
| 7233 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7234 | -4.6116860184273879E+18, |
| 7235 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 7236 | }, |
| 7237 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7238 | "too large to fit into int64_t", |
| 7239 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 7240 | 0, |
| 7241 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7242 | 0, |
| 7243 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7244 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7245 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7246 | }, |
| 7247 | { |
| 7248 | "largest negative int that fits in int64_t", |
| 7249 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 7250 | INT64_MIN, |
| 7251 | QCBOR_SUCCESS, |
| 7252 | 0, |
| 7253 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7254 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7255 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7256 | }, |
| 7257 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7258 | "negative bignum -1", |
| 7259 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 7260 | -1, |
| 7261 | QCBOR_SUCCESS, |
| 7262 | 0, |
| 7263 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7264 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7265 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7266 | }, |
| 7267 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7268 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7269 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7270 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7271 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7272 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7273 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7274 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7275 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7276 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7277 | }, |
| 7278 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7279 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7280 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7281 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7282 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7283 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7284 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7285 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7286 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7287 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7288 | }, |
| 7289 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7290 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7291 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7292 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7293 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7294 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7295 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7296 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7297 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7298 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7299 | }, |
| 7300 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7301 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7302 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 7303 | 0, |
| 7304 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7305 | 0, |
| 7306 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7307 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7308 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7309 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7310 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7311 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7312 | "Positive bignum 0x01020304 indefinite length string", |
| 7313 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 7314 | 0x01020304, |
| 7315 | QCBOR_SUCCESS, |
| 7316 | 0x01020304, |
| 7317 | QCBOR_SUCCESS, |
| 7318 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7319 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7320 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7321 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7322 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7323 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7324 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 7325 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 7326 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7327 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7328 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7329 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7330 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7331 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7332 | }, |
| 7333 | { |
| 7334 | "big float [9223372036854775806, 9223372036854775806]", |
| 7335 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7336 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 7337 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7338 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7339 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7340 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7341 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7342 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7343 | }, |
| 7344 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7345 | "Big float 3 * 2^^2", |
| 7346 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 7347 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7348 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7349 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7350 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7351 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7352 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7353 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7354 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7355 | "Decimal fraction 3/10", |
| 7356 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 7357 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7358 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7359 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7360 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7361 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7362 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 7363 | }, |
| 7364 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7365 | "extreme pos bignum", |
| 7366 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 7367 | // 50 rows of 8 is 400 digits. |
| 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, |
| 7389 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7390 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7391 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7392 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7393 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7394 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7395 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7396 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7397 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7398 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7399 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7400 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7401 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7402 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7403 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7404 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7405 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7406 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7407 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7408 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7409 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7410 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7411 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7412 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7413 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7414 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7415 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7416 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7417 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7418 | 404}, |
| 7419 | 0, |
| 7420 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7421 | 0, |
| 7422 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7423 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7424 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7425 | }, |
| 7426 | |
| 7427 | { |
| 7428 | "extreme neg bignum", |
| 7429 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 7430 | // 50 rows of 8 is 400 digits. |
| 7431 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7432 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7433 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7434 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7435 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7436 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7437 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7438 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7439 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7440 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7441 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7442 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7443 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7444 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7445 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7446 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7447 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7448 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7449 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7450 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7451 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7452 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7453 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7454 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7455 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7456 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7457 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7458 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7459 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7460 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7461 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7462 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7463 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7464 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7465 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7466 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7467 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7468 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7469 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7470 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7471 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7472 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7473 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7474 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7475 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7476 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7477 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7478 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7479 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7480 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7481 | 404}, |
| 7482 | 0, |
| 7483 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7484 | 0, |
| 7485 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7486 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7487 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7488 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7489 | |
| 7490 | { |
| 7491 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 7492 | {(uint8_t[]){ |
| 7493 | 0xC5, 0x82, |
| 7494 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7495 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7496 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7497 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7498 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7499 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7500 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7501 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7502 | }, |
| 7503 | |
| 7504 | { |
| 7505 | "bigfloat that evaluates to -INFINITY", |
| 7506 | {(uint8_t[]){ |
| 7507 | 0xC5, 0x82, |
| 7508 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7509 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7510 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7511 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7512 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7513 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7514 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7515 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7516 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7517 | { |
| 7518 | "Positive bignum 0xffff", |
| 7519 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 7520 | 65536-1, |
| 7521 | QCBOR_SUCCESS, |
| 7522 | 0xffff, |
| 7523 | QCBOR_SUCCESS, |
| 7524 | 65535.0, |
| 7525 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7526 | }, |
| 7527 | #endif /* QCBOR_DISABLE_TAGS */ |
| 7528 | { |
| 7529 | "Positive integer 18446744073709551615", |
| 7530 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 7531 | 0, |
| 7532 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7533 | 18446744073709551615ULL, |
| 7534 | QCBOR_SUCCESS, |
| 7535 | 18446744073709551615.0, |
| 7536 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7537 | }, |
| 7538 | |
| 7539 | { |
| 7540 | "Postive integer 0", |
| 7541 | {(uint8_t[]){0x0}, 1}, |
| 7542 | 0LL, |
| 7543 | QCBOR_SUCCESS, |
| 7544 | 0ULL, |
| 7545 | QCBOR_SUCCESS, |
| 7546 | 0.0, |
| 7547 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7548 | }, |
| 7549 | { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 7550 | "Negative integer -9223372036854775808", |
| 7551 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7552 | -9223372036854775807-1, // INT64_MIN |
| 7553 | QCBOR_SUCCESS, |
| 7554 | 0ULL, |
| 7555 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7556 | -9223372036854775808.0, |
| 7557 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7558 | }, |
| 7559 | { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 7560 | "Negative integer -18446744073709551616", |
| 7561 | {(uint8_t[]){0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 7562 | 0ULL, |
| 7563 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7564 | 0ULL, |
| 7565 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7566 | -18446744073709551616.0, |
| 7567 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7568 | }, |
| 7569 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7570 | "Double Floating point value 100.3", |
| 7571 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 7572 | 100L, |
| 7573 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7574 | 100ULL, |
| 7575 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7576 | 100.3, |
| 7577 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 7578 | }, |
| 7579 | { |
| 7580 | "Floating point value NaN 0xfa7fc00000", |
| 7581 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 7582 | 0, |
| 7583 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7584 | 0, |
| 7585 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7586 | NAN, |
| 7587 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7588 | }, |
| 7589 | { |
| 7590 | "half-precision Floating point value -4", |
| 7591 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 7592 | // Normal case with all enabled. |
| 7593 | -4, |
| 7594 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7595 | 0, |
| 7596 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7597 | -4.0, |
| 7598 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 7599 | }, |
| 7600 | { |
| 7601 | "+inifinity single precision", |
| 7602 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 7603 | 0, |
| 7604 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7605 | 0, |
| 7606 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 7607 | INFINITY, |
| 7608 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7609 | }, |
| 7610 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7611 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7612 | |
| 7613 | |
| 7614 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7615 | |
| 7616 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 7617 | { |
| 7618 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 7619 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7620 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 7621 | return 1; |
| 7622 | } |
| 7623 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7624 | (void)Pool; |
| 7625 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7626 | return 0; |
| 7627 | } |
| 7628 | |
| 7629 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7630 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7631 | { |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 7632 | uint64_t uInt; |
| 7633 | |
| 7634 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7635 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 7636 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7637 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7638 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 7639 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7640 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7641 | // Set up the decoding context including a memory pool so that |
| 7642 | // indefinite length items can be checked |
| 7643 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7644 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7645 | |
| 7646 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7647 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7648 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7649 | } |
| 7650 | |
| 7651 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7652 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7653 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7654 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7655 | } |
| 7656 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7657 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7658 | } |
| 7659 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7660 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7661 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7662 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7663 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7664 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7665 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7666 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7667 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7668 | } |
| 7669 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7670 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7671 | } |
| 7672 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7673 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7674 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7675 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7676 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7677 | |
| 7678 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7679 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7680 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7681 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7682 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7683 | } |
| 7684 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 7685 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7686 | // NaN's can't be compared for equality. A NaN is |
| 7687 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7688 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7689 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7690 | } |
| 7691 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7692 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7693 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7694 | } |
| 7695 | } |
| 7696 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7697 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7698 | } |
| 7699 | |
| 7700 | return 0; |
| 7701 | } |
| 7702 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7703 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7704 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7705 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7706 | { |
| 7707 | QCBORDecodeContext DCtx; |
| 7708 | QCBORItem Item; |
| 7709 | QCBORError uCBORError; |
| 7710 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7711 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7712 | QCBORDecode_Init(&DCtx, |
| 7713 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 7714 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7715 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7716 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 7717 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7718 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7719 | do { |
| 7720 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7721 | } while (QCBOR_SUCCESS == uCBORError); |
| 7722 | |
| 7723 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7724 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 7725 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7726 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7727 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7728 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7729 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7730 | |
| 7731 | |
| 7732 | static const uint8_t spSequenceTestInput[] = { |
| 7733 | /* 1. The valid date string "1985-04-12" */ |
| 7734 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 7735 | |
| 7736 | /* 2. */ |
| 7737 | 0x00, |
| 7738 | |
| 7739 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 7740 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 7741 | |
| 7742 | /* 4. */ |
| 7743 | 0x62, 'h', 'i', |
| 7744 | }; |
| 7745 | |
| 7746 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7747 | int32_t CBORSequenceDecodeTests(void) |
| 7748 | { |
| 7749 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7750 | QCBORItem Item; |
| 7751 | QCBORError uCBORError; |
| 7752 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7753 | |
| 7754 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7755 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7756 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7757 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7758 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7759 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7760 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7761 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7762 | if(uCBORError != QCBOR_SUCCESS) { |
| 7763 | return 1; |
| 7764 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7765 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7766 | return 2; |
| 7767 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7768 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7769 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7770 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7771 | uConsumed != 11) { |
| 7772 | return 102; |
| 7773 | } |
| 7774 | |
| 7775 | // Get 2. |
| 7776 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7777 | if(uCBORError != QCBOR_SUCCESS) { |
| 7778 | return 66; |
| 7779 | } |
| 7780 | |
| 7781 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7782 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7783 | uConsumed != 12) { |
| 7784 | return 102; |
| 7785 | } |
| 7786 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7787 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7788 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7789 | if(uCBORError != QCBOR_SUCCESS) { |
| 7790 | return 2; |
| 7791 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7792 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7793 | return 3; |
| 7794 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7795 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7796 | // A sequence can have stuff at the end that may |
| 7797 | // or may not be valid CBOR. The protocol decoder knows |
| 7798 | // when to stop by definition of the protocol, not |
| 7799 | // when the top-level map or array is ended. |
| 7800 | // Finish still has to be called to know that |
| 7801 | // maps and arrays (if there were any) were closed |
| 7802 | // off correctly. When called like this it |
| 7803 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 7804 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7805 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 7806 | return 4; |
| 7807 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7808 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7809 | // --- Test an empty input ---- |
| 7810 | uint8_t empty[1]; |
| 7811 | UsefulBufC Empty = {empty, 0}; |
| 7812 | QCBORDecode_Init(&DCtx, |
| 7813 | Empty, |
| 7814 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7815 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7816 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7817 | if(uCBORError != QCBOR_SUCCESS) { |
| 7818 | return 5; |
| 7819 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7820 | |
| 7821 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7822 | // --- Sequence with unclosed indefinite length array --- |
| 7823 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7824 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7825 | QCBORDecode_Init(&DCtx, |
| 7826 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 7827 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7828 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7829 | // Get the first item |
| 7830 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7831 | if(uCBORError != QCBOR_SUCCESS) { |
| 7832 | return 7; |
| 7833 | } |
| 7834 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7835 | return 8; |
| 7836 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7837 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7838 | // Get a second item |
| 7839 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7840 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7841 | if(uCBORError != QCBOR_SUCCESS) { |
| 7842 | return 9; |
| 7843 | } |
| 7844 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7845 | return 10; |
| 7846 | } |
| 7847 | |
| 7848 | // Try to finish before consuming all bytes to confirm |
| 7849 | // that the still-open error is returned. |
| 7850 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7851 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7852 | return 11; |
| 7853 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7854 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7855 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7856 | return 20; |
| 7857 | } |
| 7858 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7859 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7860 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7861 | // --- Sequence with a closed indefinite length array --- |
| 7862 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7863 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7864 | QCBORDecode_Init(&DCtx, |
| 7865 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 7866 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7867 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7868 | // Get the first item |
| 7869 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7870 | if(uCBORError != QCBOR_SUCCESS) { |
| 7871 | return 12; |
| 7872 | } |
| 7873 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7874 | return 13; |
| 7875 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7876 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7877 | // Get a second item |
| 7878 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7879 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7880 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7881 | if(uCBORError != QCBOR_SUCCESS) { |
| 7882 | return 14; |
| 7883 | } |
| 7884 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7885 | return 15; |
| 7886 | } |
| 7887 | |
| 7888 | // Try to finish before consuming all bytes to confirm |
| 7889 | // that the still-open error is returned. |
| 7890 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7891 | if(uCBORError != QCBOR_SUCCESS) { |
| 7892 | return 16; |
| 7893 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7894 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7895 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7896 | return 20; |
| 7897 | } |
| 7898 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7899 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7900 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7901 | return 0; |
| 7902 | } |
| 7903 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7904 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 7905 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7906 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7907 | { |
| 7908 | int nErrCode; |
| 7909 | int32_t n32; |
| 7910 | int16_t n16; |
| 7911 | int8_t n8; |
| 7912 | uint32_t u32; |
| 7913 | uint16_t u16; |
| 7914 | uint8_t u8; |
| 7915 | uint64_t u64; |
| 7916 | |
| 7917 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 7918 | if(nErrCode == -1 || n32 != 1) { |
| 7919 | return 1; |
| 7920 | } |
| 7921 | |
| 7922 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 7923 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 7924 | return 2; |
| 7925 | } |
| 7926 | |
| 7927 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 7928 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 7929 | return 3; |
| 7930 | } |
| 7931 | |
| 7932 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 7933 | if(nErrCode != -1) { |
| 7934 | return 4; |
| 7935 | } |
| 7936 | |
| 7937 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 7938 | if(nErrCode != -1) { |
| 7939 | return 5; |
| 7940 | } |
| 7941 | |
| 7942 | |
| 7943 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 7944 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 7945 | return 6; |
| 7946 | } |
| 7947 | |
| 7948 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 7949 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 7950 | return 7; |
| 7951 | } |
| 7952 | |
| 7953 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 7954 | if(nErrCode == -1 || n16 != 1) { |
| 7955 | return 8; |
| 7956 | } |
| 7957 | |
| 7958 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 7959 | if(nErrCode != -1) { |
| 7960 | return 9; |
| 7961 | } |
| 7962 | |
| 7963 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 7964 | if(nErrCode != -1) { |
| 7965 | return 10; |
| 7966 | } |
| 7967 | |
| 7968 | |
| 7969 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 7970 | if(nErrCode == -1 || n8 != 1) { |
| 7971 | return 11; |
| 7972 | } |
| 7973 | |
| 7974 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 7975 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 7976 | return 12; |
| 7977 | } |
| 7978 | |
| 7979 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 7980 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 7981 | return 13; |
| 7982 | } |
| 7983 | |
| 7984 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 7985 | if(nErrCode != -1) { |
| 7986 | return 14; |
| 7987 | } |
| 7988 | |
| 7989 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 7990 | if(nErrCode != -1) { |
| 7991 | return 15; |
| 7992 | } |
| 7993 | |
| 7994 | |
| 7995 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 7996 | if(nErrCode == -1 || u32 != 1) { |
| 7997 | return 16; |
| 7998 | } |
| 7999 | |
| 8000 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 8001 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 8002 | return 17; |
| 8003 | } |
| 8004 | |
| 8005 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 8006 | if(nErrCode == -1 || u32 != 0) { |
| 8007 | return 18; |
| 8008 | } |
| 8009 | |
| 8010 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 8011 | if(nErrCode != -1) { |
| 8012 | return 19; |
| 8013 | } |
| 8014 | |
| 8015 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 8016 | if(nErrCode != -1) { |
| 8017 | return 20; |
| 8018 | } |
| 8019 | |
| 8020 | |
| 8021 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 8022 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 8023 | return 21; |
| 8024 | } |
| 8025 | |
| 8026 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 8027 | if(nErrCode == -1 || u16 != 0) { |
| 8028 | return 22; |
| 8029 | } |
| 8030 | |
| 8031 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 8032 | if(nErrCode == -1 || u16 != 1) { |
| 8033 | return 23; |
| 8034 | } |
| 8035 | |
| 8036 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 8037 | if(nErrCode != -1) { |
| 8038 | return 24; |
| 8039 | } |
| 8040 | |
| 8041 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 8042 | if(nErrCode != -1) { |
| 8043 | return 25; |
| 8044 | } |
| 8045 | |
| 8046 | |
| 8047 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 8048 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 8049 | return 26; |
| 8050 | } |
| 8051 | |
| 8052 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 8053 | if(nErrCode == -1 || u8 != 0) { |
| 8054 | return 27; |
| 8055 | } |
| 8056 | |
| 8057 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 8058 | if(nErrCode == -1 || u8 != 1) { |
| 8059 | return 28; |
| 8060 | } |
| 8061 | |
| 8062 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 8063 | if(nErrCode != -1) { |
| 8064 | return 29; |
| 8065 | } |
| 8066 | |
| 8067 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 8068 | if(nErrCode != -1) { |
| 8069 | return 30; |
| 8070 | } |
| 8071 | |
| 8072 | |
| 8073 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 8074 | if(nErrCode == -1 || u64 != 1) { |
| 8075 | return 31; |
| 8076 | } |
| 8077 | |
| 8078 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 8079 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 8080 | return 32; |
| 8081 | } |
| 8082 | |
| 8083 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 8084 | if(nErrCode == -1 || u64 != 0) { |
| 8085 | return 33; |
| 8086 | } |
| 8087 | |
| 8088 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 8089 | if(nErrCode != -1) { |
| 8090 | return 34; |
| 8091 | } |
| 8092 | |
| 8093 | return 0; |
| 8094 | } |
| 8095 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8096 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8097 | |
| 8098 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8099 | /* |
| 8100 | A sequence with |
| 8101 | A wrapping bstr |
| 8102 | containing a map |
| 8103 | 1 |
| 8104 | 2 |
| 8105 | A wrapping bstr |
| 8106 | containing an array |
| 8107 | 3 |
| 8108 | wrapping bstr |
| 8109 | 4 |
| 8110 | 5 |
| 8111 | 6 |
| 8112 | array |
| 8113 | 7 |
| 8114 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8115 | */ |
| 8116 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8117 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8118 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8119 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8120 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8121 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8122 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8123 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8124 | |
| 8125 | QCBOREncode_BstrWrap(&EC); |
| 8126 | QCBOREncode_OpenMap(&EC); |
| 8127 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 8128 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 8129 | QCBOREncode_CloseMap(&EC); |
| 8130 | QCBOREncode_BstrWrap(&EC); |
| 8131 | QCBOREncode_OpenArray(&EC); |
| 8132 | QCBOREncode_AddInt64(&EC, 3); |
| 8133 | QCBOREncode_BstrWrap(&EC); |
| 8134 | QCBOREncode_AddInt64(&EC, 4); |
| 8135 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8136 | QCBOREncode_AddInt64(&EC, 5); |
| 8137 | QCBOREncode_CloseArray(&EC); |
| 8138 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8139 | QCBOREncode_AddInt64(&EC, 6); |
| 8140 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8141 | QCBOREncode_OpenArray(&EC); |
| 8142 | QCBOREncode_AddInt64(&EC, 7); |
| 8143 | QCBOREncode_AddInt64(&EC, 8); |
| 8144 | QCBOREncode_CloseArray(&EC); |
| 8145 | |
| 8146 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 8147 | if(uErr) { |
| 8148 | Encoded = NULLUsefulBufC; |
| 8149 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8150 | |
| 8151 | return Encoded; |
| 8152 | } |
| 8153 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8154 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8155 | static const uint8_t spBreakInByteString[] = { |
| 8156 | 0x41, 0xff |
| 8157 | }; |
| 8158 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8159 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8160 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8161 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8162 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8163 | |
| 8164 | QCBORDecodeContext DC; |
| 8165 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8166 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8167 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8168 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8169 | |
| 8170 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8171 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8172 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8173 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 8174 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8175 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8176 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8177 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8178 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8179 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8180 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8181 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8182 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8183 | QCBORDecode_ExitArray(&DC); |
| 8184 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8185 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8186 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8187 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8188 | QCBORDecode_GetInt64(&DC, &n7); |
| 8189 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8190 | QCBORDecode_ExitArray(&DC); |
| 8191 | |
| 8192 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8193 | if(uErr) { |
| 8194 | return (int32_t)uErr; |
| 8195 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8196 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8197 | |
| 8198 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 8199 | * Successful because no items are fetched from byte string. |
| 8200 | */ |
| 8201 | QCBORDecode_Init(&DC, |
| 8202 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8203 | 0); |
| 8204 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8205 | uErr = QCBORDecode_GetError(&DC); |
| 8206 | if(uErr) { |
| 8207 | return 100 + (int32_t)uErr; |
| 8208 | } |
| 8209 | |
| 8210 | QCBORDecode_ExitBstrWrapped(&DC); |
| 8211 | uErr = QCBORDecode_GetError(&DC); |
| 8212 | if(uErr) { |
| 8213 | return 200 + (int32_t)uErr; |
| 8214 | } |
| 8215 | |
| 8216 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 8217 | * It fails because there should be no break. |
| 8218 | */ |
| 8219 | QCBORDecode_Init(&DC, |
| 8220 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8221 | 0); |
| 8222 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8223 | QCBORItem Item; |
| 8224 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 8225 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 8226 | return 300 + (int32_t)uErr; |
| 8227 | } |
| 8228 | |
| 8229 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8230 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8231 | |
| 8232 | |
| 8233 | |
| 8234 | |
| 8235 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8236 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8237 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8238 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8239 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8240 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 8241 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 8242 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8243 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8244 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8245 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 8246 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 8247 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8248 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8249 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8250 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8251 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8252 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8253 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8254 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8255 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8256 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8257 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8258 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8259 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8260 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 8261 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8262 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8263 | 21, |
| 8264 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 8265 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 8266 | |
| 8267 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8268 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8269 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8270 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8271 | |
| 8272 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8273 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8274 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8275 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8276 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8277 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8278 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8279 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8280 | |
| 8281 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8282 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8283 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8284 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8285 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8286 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8287 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 8288 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8289 | |
| 8290 | 0x18, 0x33, |
| 8291 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 8292 | |
| 8293 | // MIME |
| 8294 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8295 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 8296 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 8297 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8298 | |
| 8299 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8300 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8301 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8302 | |
| 8303 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8304 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 8305 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 8306 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8307 | |
| 8308 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8309 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8310 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8311 | |
| 8312 | // UUID |
| 8313 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8314 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 8315 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8316 | |
| 8317 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8318 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 8319 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8320 | }; |
| 8321 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8322 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8323 | { |
| 8324 | QCBORDecodeContext DC; |
| 8325 | QCBORError uErr; |
| 8326 | |
| 8327 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 8328 | |
| 8329 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8330 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8331 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8332 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8333 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8334 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8335 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 8336 | return 1; |
| 8337 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8338 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8339 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8340 | return 2; |
| 8341 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8342 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8343 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8344 | return 3; |
| 8345 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8346 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8347 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8348 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8349 | return 4; |
| 8350 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8351 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8352 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8353 | return 5; |
| 8354 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8355 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8356 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8357 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8358 | bNeg != false) { |
| 8359 | return 10; |
| 8360 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8361 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8362 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8363 | bNeg != true) { |
| 8364 | return 11; |
| 8365 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8366 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8367 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8368 | return 12; |
| 8369 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8370 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8371 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8372 | return 13; |
| 8373 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8374 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8375 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8376 | return 14; |
| 8377 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8378 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8379 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8380 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8381 | return 20; |
| 8382 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8383 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8384 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8385 | return 21; |
| 8386 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8387 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8388 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8389 | return 22; |
| 8390 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8391 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8392 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8393 | return 23; |
| 8394 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8395 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8396 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8397 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8398 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8399 | return 30; |
| 8400 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8401 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8402 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8403 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8404 | return 31; |
| 8405 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8406 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8407 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8408 | return 32; |
| 8409 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8410 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8411 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8412 | return 33; |
| 8413 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8414 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8415 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8416 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8417 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8418 | return 40; |
| 8419 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8420 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8421 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8422 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8423 | return 41; |
| 8424 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8425 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8426 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8427 | return 42; |
| 8428 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8429 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8430 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8431 | return 43; |
| 8432 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8433 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8434 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8435 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8436 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8437 | return 50; |
| 8438 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8439 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8440 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8441 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8442 | return 51; |
| 8443 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8444 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8445 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8446 | return 52; |
| 8447 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8448 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8449 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8450 | return 53; |
| 8451 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8452 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8453 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8454 | // MIME |
| 8455 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8456 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8457 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8458 | bIsNot7Bit == true) { |
| 8459 | return 60; |
| 8460 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8461 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8462 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8463 | bIsNot7Bit == true) { |
| 8464 | return 61; |
| 8465 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8466 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8467 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8468 | bIsNot7Bit == false) { |
| 8469 | return 62; |
| 8470 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8471 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8472 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8473 | bIsNot7Bit == false) { |
| 8474 | return 63; |
| 8475 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8476 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8477 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8478 | return 64; |
| 8479 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8480 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8481 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8482 | return 65; |
| 8483 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8484 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8485 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8486 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8487 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8488 | return 70; |
| 8489 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8490 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 8491 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8492 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8493 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8494 | return 71; |
| 8495 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8496 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8497 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8498 | return 72; |
| 8499 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8500 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8501 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8502 | return 73; |
| 8503 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8504 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8505 | // Improvement: add some more error test cases |
| 8506 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8507 | QCBORDecode_ExitMap(&DC); |
| 8508 | |
| 8509 | uErr = QCBORDecode_Finish(&DC); |
| 8510 | if(uErr != QCBOR_SUCCESS) { |
| 8511 | return 100; |
| 8512 | } |
| 8513 | |
| 8514 | return 0; |
| 8515 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8516 | |
| 8517 | |
| 8518 | |
| 8519 | |
| 8520 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8521 | [ |
| 8522 | "aaaaaaaaaa", |
| 8523 | {} |
| 8524 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8525 | */ |
| 8526 | static const uint8_t spTooLarge1[] = { |
| 8527 | 0x9f, |
| 8528 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8529 | 0xa0, |
| 8530 | 0xff |
| 8531 | }; |
| 8532 | |
| 8533 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8534 | [ |
| 8535 | { |
| 8536 | 0: "aaaaaaaaaa" |
| 8537 | } |
| 8538 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8539 | */ |
| 8540 | static const uint8_t spTooLarge2[] = { |
| 8541 | 0x9f, |
| 8542 | 0xa1, |
| 8543 | 0x00, |
| 8544 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8545 | 0xff |
| 8546 | }; |
| 8547 | |
| 8548 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8549 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8550 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8551 | { |
| 8552 | 0: "aaaaaaaaaa" |
| 8553 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8554 | */ |
| 8555 | static const uint8_t spTooLarge3[] = { |
| 8556 | 0x4d, |
| 8557 | 0xa1, |
| 8558 | 0x00, |
| 8559 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8560 | }; |
| 8561 | |
| 8562 | int32_t TooLargeInputTest(void) |
| 8563 | { |
| 8564 | QCBORDecodeContext DC; |
| 8565 | QCBORError uErr; |
| 8566 | UsefulBufC String; |
| 8567 | |
| 8568 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 8569 | // to 10 There's not really any way to test this error |
| 8570 | // condition. The error condition is not complex, so setting |
| 8571 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 8572 | |
| 8573 | // The input CBOR is only too large because the |
| 8574 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 8575 | // |
| 8576 | // This test is disabled for the normal test runs because of the |
| 8577 | // special build requirement. |
| 8578 | |
| 8579 | |
| 8580 | // Tests the start of a map being too large |
| 8581 | 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] | 8582 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8583 | QCBORDecode_GetTextString(&DC, &String); |
| 8584 | uErr = QCBORDecode_GetError(&DC); |
| 8585 | if(uErr != QCBOR_SUCCESS) { |
| 8586 | return 1; |
| 8587 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8588 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8589 | uErr = QCBORDecode_GetError(&DC); |
| 8590 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8591 | return 2; |
| 8592 | } |
| 8593 | |
| 8594 | // Tests the end of a map being too large |
| 8595 | 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] | 8596 | QCBORDecode_EnterArray(&DC, NULL); |
| 8597 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8598 | uErr = QCBORDecode_GetError(&DC); |
| 8599 | if(uErr != QCBOR_SUCCESS) { |
| 8600 | return 3; |
| 8601 | } |
| 8602 | QCBORDecode_ExitMap(&DC); |
| 8603 | uErr = QCBORDecode_GetError(&DC); |
| 8604 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8605 | return 4; |
| 8606 | } |
| 8607 | |
| 8608 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 8609 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 8610 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8611 | uErr = QCBORDecode_GetError(&DC); |
| 8612 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8613 | return 5; |
| 8614 | } |
| 8615 | |
| 8616 | return 0; |
| 8617 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8618 | |
| 8619 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8620 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 8621 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8622 | /* |
| 8623 | An array of three map entries |
| 8624 | 1) Indefinite length string label for indefinite lenght byte string |
| 8625 | 2) Indefinite length string label for an integer |
| 8626 | 3) Indefinite length string label for an indefinite-length negative big num |
| 8627 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8628 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8629 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8630 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 8631 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 8632 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 8633 | 0x03, |
| 8634 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 8635 | 0xc3, |
| 8636 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8637 | }; |
| 8638 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8639 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8640 | { |
| 8641 | QCBORDecodeContext DCtx; |
| 8642 | |
| 8643 | QCBORDecode_Init(&DCtx, |
| 8644 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 8645 | QCBOR_DECODE_MODE_NORMAL); |
| 8646 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8647 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8648 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 8649 | |
| 8650 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8651 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8652 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8653 | |
| 8654 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8655 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8656 | return 1; |
| 8657 | } |
| 8658 | |
| 8659 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 8660 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 8661 | return 2; |
| 8662 | } |
| 8663 | |
| 8664 | uint64_t uInt; |
| 8665 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 8666 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8667 | return 3; |
| 8668 | } |
| 8669 | if(uInt != 3) { |
| 8670 | return 4; |
| 8671 | } |
| 8672 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8673 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8674 | double uDouble; |
| 8675 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 8676 | "label2", |
| 8677 | 0xff, |
| 8678 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8679 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8680 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8681 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8682 | return 5; |
| 8683 | } |
| 8684 | if(uDouble != -16777474) { |
| 8685 | return 6; |
| 8686 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8687 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8688 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 8689 | return 7; |
| 8690 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8691 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8692 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8693 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8694 | QCBORDecode_ExitMap(&DCtx); |
| 8695 | |
| 8696 | if(QCBORDecode_Finish(&DCtx)) { |
| 8697 | return 99; |
| 8698 | } |
| 8699 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8700 | #else /* QCBOR_DISABLE_TAGS */ |
| 8701 | /* The big num in the input is a CBOR tag and you can't do |
| 8702 | * map lookups in a map with a tag so this test does very little |
| 8703 | * when tags are disabled. That is OK, the test coverage is still |
| 8704 | * good when they are not. |
| 8705 | */ |
| 8706 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 8707 | return 1002; |
| 8708 | } |
| 8709 | #endif /*QCBOR_DISABLE_TAGS */ |
| 8710 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8711 | return 0; |
| 8712 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8713 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8714 | |
| 8715 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8716 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8717 | /* |
| 8718 | * An array of an integer and an array. The second array contains |
| 8719 | * a bstr-wrapped map. |
| 8720 | * |
| 8721 | * [7, [h'A36D6669... (see next lines) 73']] |
| 8722 | * |
| 8723 | * {"first integer": 42, |
| 8724 | * "an array of two strings": ["string1", "string2"], |
| 8725 | * "map in a map": |
| 8726 | * { "bytes 1": h'78787878', |
| 8727 | * "bytes 2": h'79797979', |
| 8728 | * "another int": 98, |
| 8729 | * "text 2": "lies, damn lies and statistics" |
| 8730 | * } |
| 8731 | * } |
| 8732 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8733 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8734 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 8735 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 8736 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8737 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8738 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8739 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8740 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8741 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8742 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8743 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8744 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8745 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8746 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8747 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8748 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8749 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8750 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8751 | 0x73 |
| 8752 | }; |
| 8753 | |
| 8754 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8755 | |
| 8756 | /* As above, but the arrays are indefinite length */ |
| 8757 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 8758 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 8759 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8760 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8761 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8762 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8763 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8764 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8765 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8766 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8767 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8768 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8769 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8770 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8771 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8772 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8773 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8774 | 0x73, |
| 8775 | 0xff, 0xff |
| 8776 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8777 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8778 | |
| 8779 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 8780 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8781 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8782 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8783 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 8784 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8785 | |
| 8786 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8787 | /* |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 8788 | * An array of one that contains a byte string that is an indefinite |
| 8789 | * length string that CBOR wraps an array of three numbers [42, 43, |
| 8790 | * 44]. The byte string is an implicit tag 24. |
| 8791 | * |
| 8792 | * [ |
| 8793 | * (_ h'83', h'18', h'2A182B', h'182C') |
| 8794 | * ] |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8795 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8796 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 8797 | 0x81, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8798 | 0x5f, |
| 8799 | 0x41, 0x83, |
| 8800 | 0x41, 0x18, |
| 8801 | 0x43, 0x2A, 0x18, 0x2B, |
| 8802 | 0x42, 0x18, 0x2C, |
| 8803 | 0xff |
| 8804 | }; |
| 8805 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8806 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8807 | #endif |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8808 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8809 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8810 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8811 | { |
| 8812 | QCBORItem Item; |
| 8813 | QCBORError nCBORError; |
| 8814 | QCBORDecodeContext DCtx; |
| 8815 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8816 | // Improvement: rework this test to use only integer labels. |
| 8817 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8818 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8819 | |
| 8820 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8821 | return 100+(int32_t)nCBORError; |
| 8822 | } |
| 8823 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8824 | return 200; |
| 8825 | } |
| 8826 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8827 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8828 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 8829 | return 150+(int32_t)nCBORError; |
| 8830 | } |
| 8831 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8832 | return 250; |
| 8833 | } |
| 8834 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8835 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8836 | return (int32_t)nCBORError; |
| 8837 | } |
| 8838 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8839 | return 300; |
| 8840 | } |
| 8841 | |
| 8842 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8843 | return 400 + (int32_t)nCBORError; |
| 8844 | } |
| 8845 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8846 | return 500; |
| 8847 | } |
| 8848 | |
| 8849 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8850 | return (int32_t)nCBORError; |
| 8851 | } |
| 8852 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8853 | return 600; |
| 8854 | } |
| 8855 | |
| 8856 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8857 | return 900 + (int32_t)nCBORError; |
| 8858 | } |
| 8859 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8860 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8861 | Item.val.int64 != 42 || |
| 8862 | Item.uDataAlloc || |
| 8863 | Item.uLabelAlloc || |
| 8864 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8865 | return 1000; |
| 8866 | } |
| 8867 | |
| 8868 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8869 | return 1100 + (int32_t)nCBORError; |
| 8870 | } |
| 8871 | |
| 8872 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8873 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8874 | Item.val.int64 != 42 || |
| 8875 | Item.uDataAlloc || |
| 8876 | Item.uLabelAlloc || |
| 8877 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8878 | return 1200; |
| 8879 | } |
| 8880 | |
| 8881 | |
| 8882 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8883 | return 1300 + (int32_t)nCBORError; |
| 8884 | } |
| 8885 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8886 | Item.uDataAlloc || |
| 8887 | Item.uLabelAlloc || |
| 8888 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8889 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8890 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8891 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8892 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8893 | |
| 8894 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8895 | return 1500 + (int32_t)nCBORError; |
| 8896 | } |
| 8897 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8898 | Item.uDataAlloc || |
| 8899 | Item.uLabelAlloc || |
| 8900 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8901 | return 1600; |
| 8902 | } |
| 8903 | |
| 8904 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8905 | return 1700 + (int32_t)nCBORError; |
| 8906 | } |
| 8907 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8908 | Item.uDataAlloc || |
| 8909 | Item.uLabelAlloc || |
| 8910 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8911 | return 1800; |
| 8912 | } |
| 8913 | |
| 8914 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8915 | return (int32_t)nCBORError; |
| 8916 | } |
| 8917 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8918 | Item.uDataAlloc || |
| 8919 | Item.uLabelAlloc || |
| 8920 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8921 | return 1900; |
| 8922 | } |
| 8923 | |
| 8924 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8925 | return (int32_t)nCBORError; |
| 8926 | } |
| 8927 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8928 | Item.uDataAlloc || |
| 8929 | Item.uLabelAlloc || |
| 8930 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8931 | return 2000; |
| 8932 | } |
| 8933 | |
| 8934 | |
| 8935 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8936 | return 2100 + (int32_t)nCBORError; |
| 8937 | } |
| 8938 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8939 | Item.uDataAlloc || |
| 8940 | Item.uLabelAlloc || |
| 8941 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 8942 | Item.uDataType != QCBOR_TYPE_MAP || |
| 8943 | Item.val.uCount != 4) { |
| 8944 | return 2100; |
| 8945 | } |
| 8946 | |
| 8947 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8948 | return 2200 + (int32_t)nCBORError; |
| 8949 | } |
| 8950 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8951 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 8952 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8953 | Item.uDataAlloc || |
| 8954 | Item.uLabelAlloc || |
| 8955 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 8956 | return 2300; |
| 8957 | } |
| 8958 | |
| 8959 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8960 | return 2400 + (int32_t)nCBORError; |
| 8961 | } |
| 8962 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8963 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8964 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8965 | Item.uDataAlloc || |
| 8966 | Item.uLabelAlloc || |
| 8967 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8968 | return 2500; |
| 8969 | } |
| 8970 | |
| 8971 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8972 | return 2600 + (int32_t)nCBORError; |
| 8973 | } |
| 8974 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8975 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8976 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8977 | Item.uDataAlloc || |
| 8978 | Item.uLabelAlloc || |
| 8979 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8980 | return 2700; |
| 8981 | } |
| 8982 | |
| 8983 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8984 | return 2800 + (int32_t)nCBORError; |
| 8985 | } |
| 8986 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8987 | Item.uDataAlloc || |
| 8988 | Item.uLabelAlloc || |
| 8989 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 8990 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8991 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8992 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8993 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8994 | |
| 8995 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8996 | return 3000 + (int32_t)nCBORError; |
| 8997 | } |
| 8998 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8999 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 9000 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9001 | Item.uDataAlloc || |
| 9002 | Item.uLabelAlloc || |
| 9003 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 9004 | return 3100; |
| 9005 | } |
| 9006 | |
| 9007 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9008 | return 3200 + (int32_t)nCBORError; |
| 9009 | } |
| 9010 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9011 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 9012 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9013 | Item.uDataAlloc || |
| 9014 | Item.uLabelAlloc || |
| 9015 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 9016 | return 3300; |
| 9017 | } |
| 9018 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 9019 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 9020 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9021 | return 3300 + (int32_t)nCBORError; |
| 9022 | } |
| 9023 | |
| 9024 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 9025 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 9026 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9027 | return 3400 + (int32_t)nCBORError; |
| 9028 | } |
| 9029 | |
| 9030 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 9031 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 9032 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9033 | return 3500 + (int32_t)nCBORError; |
| 9034 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9035 | |
| 9036 | |
| 9037 | // Rewind to top level after entering several maps |
| 9038 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9039 | |
| 9040 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9041 | return (int32_t)nCBORError; |
| 9042 | } |
| 9043 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 9044 | Item.val.uCount != 3) { |
| 9045 | return 400; |
| 9046 | } |
| 9047 | |
| 9048 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9049 | return 4000+(int32_t)nCBORError; |
| 9050 | } |
| 9051 | |
| 9052 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9053 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9054 | Item.val.int64 != 42 || |
| 9055 | Item.uDataAlloc || |
| 9056 | Item.uLabelAlloc || |
| 9057 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9058 | return 4100; |
| 9059 | } |
| 9060 | |
| 9061 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9062 | return 4100+(int32_t)nCBORError; |
| 9063 | } |
| 9064 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9065 | Item.uDataAlloc || |
| 9066 | Item.uLabelAlloc || |
| 9067 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9068 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9069 | Item.val.uCount != 2) { |
| 9070 | return 4200; |
| 9071 | } |
| 9072 | |
| 9073 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9074 | return 4200+(int32_t)nCBORError; |
| 9075 | } |
| 9076 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9077 | Item.uDataAlloc || |
| 9078 | Item.uLabelAlloc || |
| 9079 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9080 | return 4300; |
| 9081 | } |
| 9082 | |
| 9083 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9084 | return 4300+(int32_t)nCBORError; |
| 9085 | } |
| 9086 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9087 | Item.uDataAlloc || |
| 9088 | Item.uLabelAlloc || |
| 9089 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9090 | return 4400; |
| 9091 | } |
| 9092 | |
| 9093 | QCBORDecode_Rewind(&DCtx); |
| 9094 | |
| 9095 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9096 | return 4400+(int32_t)nCBORError; |
| 9097 | } |
| 9098 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 9099 | Item.val.uCount != 3) { |
| 9100 | return 4500; |
| 9101 | } |
| 9102 | |
| 9103 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9104 | return (int32_t)nCBORError; |
| 9105 | } |
| 9106 | |
| 9107 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9108 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9109 | Item.val.int64 != 42 || |
| 9110 | Item.uDataAlloc || |
| 9111 | Item.uLabelAlloc || |
| 9112 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9113 | return 4600; |
| 9114 | } |
| 9115 | |
| 9116 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9117 | return (int32_t)nCBORError; |
| 9118 | } |
| 9119 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9120 | Item.uDataAlloc || |
| 9121 | Item.uLabelAlloc || |
| 9122 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9123 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9124 | Item.val.uCount != 2) { |
| 9125 | return 4700; |
| 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, "string1")) { |
| 9135 | return 4800; |
| 9136 | } |
| 9137 | |
| 9138 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9139 | return 4900+(int32_t)nCBORError; |
| 9140 | } |
| 9141 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9142 | Item.uDataAlloc || |
| 9143 | Item.uLabelAlloc || |
| 9144 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9145 | return 5000; |
| 9146 | } |
| 9147 | |
| 9148 | |
| 9149 | // Rewind an entered map |
| 9150 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9151 | |
| 9152 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9153 | |
| 9154 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9155 | return 5100+(int32_t)nCBORError; |
| 9156 | } |
| 9157 | |
| 9158 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9159 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9160 | Item.val.int64 != 42 || |
| 9161 | Item.uDataAlloc || |
| 9162 | Item.uLabelAlloc || |
| 9163 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9164 | return 5200; |
| 9165 | } |
| 9166 | |
| 9167 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9168 | return 5200+(int32_t)nCBORError; |
| 9169 | } |
| 9170 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9171 | Item.uDataAlloc || |
| 9172 | Item.uLabelAlloc || |
| 9173 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9174 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9175 | Item.val.uCount != 2) { |
| 9176 | return -5300; |
| 9177 | } |
| 9178 | |
| 9179 | QCBORDecode_Rewind(&DCtx); |
| 9180 | |
| 9181 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9182 | return 5300+(int32_t)nCBORError; |
| 9183 | } |
| 9184 | |
| 9185 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9186 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9187 | Item.val.int64 != 42 || |
| 9188 | Item.uDataAlloc || |
| 9189 | Item.uLabelAlloc || |
| 9190 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9191 | return 5400; |
| 9192 | } |
| 9193 | |
| 9194 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9195 | return 5400+(int32_t)nCBORError; |
| 9196 | } |
| 9197 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9198 | Item.uDataAlloc || |
| 9199 | Item.uLabelAlloc || |
| 9200 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9201 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9202 | Item.val.uCount != 2) { |
| 9203 | return 5500; |
| 9204 | } |
| 9205 | |
| 9206 | |
| 9207 | // Rewind and entered array inside an entered map |
| 9208 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9209 | |
| 9210 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9211 | |
| 9212 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 9213 | |
| 9214 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9215 | return 5600+(int32_t)nCBORError; |
| 9216 | } |
| 9217 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9218 | Item.uDataAlloc || |
| 9219 | Item.uLabelAlloc || |
| 9220 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9221 | return 5700; |
| 9222 | } |
| 9223 | |
| 9224 | QCBORDecode_Rewind(&DCtx); |
| 9225 | |
| 9226 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9227 | return 5700+(int32_t)nCBORError; |
| 9228 | } |
| 9229 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9230 | Item.uDataAlloc || |
| 9231 | Item.uLabelAlloc || |
| 9232 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9233 | return 5800; |
| 9234 | } |
| 9235 | |
| 9236 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9237 | return (int32_t)nCBORError; |
| 9238 | } |
| 9239 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9240 | Item.uDataAlloc || |
| 9241 | Item.uLabelAlloc || |
| 9242 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9243 | return 5900; |
| 9244 | } |
| 9245 | |
| 9246 | QCBORDecode_Rewind(&DCtx); |
| 9247 | |
| 9248 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9249 | return 5900+(int32_t)nCBORError; |
| 9250 | } |
| 9251 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9252 | Item.uDataAlloc || |
| 9253 | Item.uLabelAlloc || |
| 9254 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9255 | return 6000; |
| 9256 | } |
| 9257 | |
| 9258 | |
| 9259 | // Rewind a byte string inside an array inside an array |
| 9260 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 9261 | |
| 9262 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9263 | |
| 9264 | uint64_t i; |
| 9265 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9266 | |
| 9267 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9268 | |
| 9269 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9270 | if(QCBORDecode_GetError(&DCtx)) { |
| 9271 | return 6100; |
| 9272 | } |
| 9273 | |
| 9274 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9275 | return (int32_t)nCBORError; |
| 9276 | } |
| 9277 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9278 | return 6200; |
| 9279 | } |
| 9280 | |
| 9281 | QCBORDecode_Rewind(&DCtx); |
| 9282 | |
| 9283 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9284 | return 6300+(int32_t)nCBORError; |
| 9285 | } |
| 9286 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9287 | return 6400; |
| 9288 | } |
| 9289 | |
| 9290 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9291 | // Rewind a byte string inside an indefinite-length array inside |
| 9292 | // indefinite-length array |
| 9293 | |
| 9294 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 9295 | |
| 9296 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9297 | |
| 9298 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9299 | |
| 9300 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9301 | |
| 9302 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9303 | if(QCBORDecode_GetError(&DCtx)) { |
| 9304 | return 6500; |
| 9305 | } |
| 9306 | |
| 9307 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9308 | return 6600+(int32_t)nCBORError; |
| 9309 | } |
| 9310 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9311 | return 6700; |
| 9312 | } |
| 9313 | |
| 9314 | QCBORDecode_Rewind(&DCtx); |
| 9315 | |
| 9316 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9317 | return 6800+(int32_t)nCBORError; |
| 9318 | } |
| 9319 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9320 | return 6900; |
| 9321 | } |
| 9322 | #endif |
| 9323 | |
| 9324 | // Rewind an empty map |
| 9325 | // [100, {}] |
| 9326 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 9327 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9328 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9329 | if(i != 100) { |
| 9330 | return 7010; |
| 9331 | } |
| 9332 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9333 | |
| 9334 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9335 | for(int n = 0; n < 5; n++) { |
| 9336 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9337 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9338 | return 7000 + n; |
| 9339 | } |
| 9340 | QCBORDecode_Rewind(&DCtx); |
| 9341 | } |
| 9342 | QCBORDecode_ExitMap(&DCtx); |
| 9343 | QCBORDecode_Rewind(&DCtx); |
| 9344 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9345 | if(i != 100) { |
| 9346 | return 7010; |
| 9347 | } |
| 9348 | QCBORDecode_ExitArray(&DCtx); |
| 9349 | QCBORDecode_Rewind(&DCtx); |
| 9350 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9351 | i = 9; |
| 9352 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9353 | if(i != 100) { |
| 9354 | return 7020; |
| 9355 | } |
| 9356 | if(QCBORDecode_GetError(&DCtx)){ |
| 9357 | return 7030; |
| 9358 | } |
| 9359 | |
| 9360 | // Rewind an empty indefinite length map |
| 9361 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9362 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 9363 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9364 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9365 | if(i != 100) { |
| 9366 | return 7810; |
| 9367 | } |
| 9368 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9369 | |
| 9370 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9371 | for(int n = 0; n < 5; n++) { |
| 9372 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9373 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9374 | return 7800 + n; |
| 9375 | } |
| 9376 | QCBORDecode_Rewind(&DCtx); |
| 9377 | } |
| 9378 | QCBORDecode_ExitMap(&DCtx); |
| 9379 | QCBORDecode_Rewind(&DCtx); |
| 9380 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9381 | if(i != 100) { |
| 9382 | return 7810; |
| 9383 | } |
| 9384 | QCBORDecode_ExitArray(&DCtx); |
| 9385 | QCBORDecode_Rewind(&DCtx); |
| 9386 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9387 | i = 9; |
| 9388 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9389 | if(i != 100) { |
| 9390 | return 7820; |
| 9391 | } |
| 9392 | if(QCBORDecode_GetError(&DCtx)){ |
| 9393 | return 7830; |
| 9394 | } |
| 9395 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9396 | |
| 9397 | // Rewind an indefnite length byte-string wrapped sequence |
| 9398 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 9399 | QCBORDecode_Init(&DCtx, |
| 9400 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 9401 | 0); |
| 9402 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 9403 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 9404 | |
| 9405 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9406 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9407 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9408 | return 7300; |
| 9409 | } |
| 9410 | |
| 9411 | /* |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9412 | Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on |
| 9413 | allocated strings. This is a fairly big job because of all the |
| 9414 | UsefulBuf internal book keeping that needs tweaking. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9415 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9416 | if(i != 42) { |
| 9417 | return 7110; |
| 9418 | } |
| 9419 | QCBORDecode_Rewind(&DCtx); |
| 9420 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9421 | if(i != 42) { |
| 9422 | return 7220; |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9423 | } |
| 9424 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 9425 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9426 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9427 | |
| 9428 | |
| 9429 | // Rewind an indefnite length byte-string wrapped sequence |
| 9430 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9431 | return 0; |
| 9432 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9433 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9434 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9435 | |
| 9436 | |
| 9437 | static const uint8_t spBooleansInMap[] = |
| 9438 | { |
| 9439 | 0xa1, 0x08, 0xf5 |
| 9440 | }; |
| 9441 | |
| 9442 | static const uint8_t spBooleansInMapWrongType[] = |
| 9443 | { |
| 9444 | 0xa1, 0x08, 0xf6 |
| 9445 | }; |
| 9446 | |
| 9447 | static const uint8_t spBooleansInMapNWF[] = |
| 9448 | { |
| 9449 | 0xa1, 0x08, 0x1a |
| 9450 | }; |
| 9451 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9452 | static const uint8_t spNullInMap[] = |
| 9453 | { |
| 9454 | 0xa1, 0x08, 0xf6 |
| 9455 | }; |
| 9456 | |
| 9457 | static const uint8_t spUndefinedInMap[] = |
| 9458 | { |
| 9459 | 0xa1, 0x08, 0xf7 |
| 9460 | }; |
| 9461 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9462 | |
| 9463 | int32_t BoolTest(void) |
| 9464 | { |
| 9465 | QCBORDecodeContext DCtx; |
| 9466 | bool b; |
| 9467 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9468 | QCBORDecode_Init(&DCtx, |
| 9469 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9470 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9471 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9472 | QCBORDecode_GetBool(&DCtx, &b); |
| 9473 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9474 | return 1; |
| 9475 | } |
| 9476 | |
| 9477 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 9478 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9479 | return 2; |
| 9480 | } |
| 9481 | |
| 9482 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 9483 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9484 | return 3; |
| 9485 | } |
| 9486 | |
| 9487 | |
| 9488 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 9489 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9490 | return 4; |
| 9491 | } |
| 9492 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9493 | QCBORDecode_Init(&DCtx, |
| 9494 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 9495 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9496 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9497 | QCBORDecode_GetBool(&DCtx, &b); |
| 9498 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9499 | return 5; |
| 9500 | } |
| 9501 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9502 | QCBORDecode_Init(&DCtx, |
| 9503 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9504 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9505 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9506 | QCBORDecode_GetBool(&DCtx, &b); |
| 9507 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9508 | return 6; |
| 9509 | } |
| 9510 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9511 | |
| 9512 | QCBORDecode_Init(&DCtx, |
| 9513 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9514 | 0); |
| 9515 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9516 | QCBORDecode_GetNull(&DCtx); |
| 9517 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9518 | return 7; |
| 9519 | } |
| 9520 | |
| 9521 | QCBORDecode_Init(&DCtx, |
| 9522 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9523 | 0); |
| 9524 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9525 | QCBORDecode_GetNull(&DCtx); |
| 9526 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9527 | return 8; |
| 9528 | } |
| 9529 | |
| 9530 | QCBORDecode_Init(&DCtx, |
| 9531 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9532 | 0); |
| 9533 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9534 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9535 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9536 | return 9; |
| 9537 | } |
| 9538 | |
| 9539 | QCBORDecode_Init(&DCtx, |
| 9540 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9541 | 0); |
| 9542 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9543 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9544 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9545 | return 10; |
| 9546 | } |
| 9547 | |
| 9548 | QCBORDecode_Init(&DCtx, |
| 9549 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9550 | 0); |
| 9551 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9552 | QCBORDecode_GetUndefined(&DCtx); |
| 9553 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9554 | return 11; |
| 9555 | } |
| 9556 | |
| 9557 | QCBORDecode_Init(&DCtx, |
| 9558 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9559 | 0); |
| 9560 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9561 | QCBORDecode_GetUndefined(&DCtx); |
| 9562 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9563 | return 12; |
| 9564 | } |
| 9565 | |
| 9566 | QCBORDecode_Init(&DCtx, |
| 9567 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9568 | 0); |
| 9569 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9570 | QCBORDecode_GetUndefined(&DCtx); |
| 9571 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9572 | return 13; |
| 9573 | } |
| 9574 | |
| 9575 | QCBORDecode_Init(&DCtx, |
| 9576 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9577 | 0); |
| 9578 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9579 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9580 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9581 | return 14; |
| 9582 | } |
| 9583 | |
| 9584 | QCBORDecode_Init(&DCtx, |
| 9585 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9586 | 0); |
| 9587 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9588 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9589 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9590 | return 15; |
| 9591 | } |
| 9592 | |
| 9593 | QCBORDecode_Init(&DCtx, |
| 9594 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9595 | 0); |
| 9596 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9597 | QCBORDecode_GetUndefined(&DCtx); |
| 9598 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9599 | return 15; |
| 9600 | } |
| 9601 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9602 | return 0; |
| 9603 | } |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9604 | |
| 9605 | |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 9606 | #if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT) |
| 9607 | |
| 9608 | struct PreciseNumberConversion { |
| 9609 | char *szDescription; |
| 9610 | UsefulBufC CBOR; |
| 9611 | QCBORError uError; |
| 9612 | uint8_t qcborType; |
| 9613 | struct { |
| 9614 | int64_t int64; |
| 9615 | uint64_t uint64; |
| 9616 | double d; |
| 9617 | } number; |
| 9618 | }; |
| 9619 | |
| 9620 | |
| 9621 | static const struct PreciseNumberConversion PreciseNumberConversions[] = { |
| 9622 | { |
| 9623 | "-0.00", |
| 9624 | {"\xf9\x80\x00", 3}, |
| 9625 | QCBOR_SUCCESS, |
| 9626 | QCBOR_TYPE_INT64, |
| 9627 | {0, 0, 0} |
| 9628 | }, |
| 9629 | { |
| 9630 | "NaN", |
| 9631 | {"\xf9\x7e\x00", 3}, |
| 9632 | QCBOR_SUCCESS, |
| 9633 | QCBOR_TYPE_DOUBLE, |
| 9634 | {0, 0, NAN} |
| 9635 | }, |
| 9636 | { |
| 9637 | "NaN payload", |
| 9638 | {"\xFB\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9}, |
| 9639 | QCBOR_SUCCESS, |
| 9640 | QCBOR_TYPE_DOUBLE, |
| 9641 | {0, 0, NAN} |
| 9642 | }, |
| 9643 | { |
| 9644 | "65536.0 single", |
| 9645 | {"\xFA\x47\x80\x00\x00", 5}, |
| 9646 | QCBOR_SUCCESS, |
| 9647 | QCBOR_TYPE_INT64, |
| 9648 | {65536, 0, 0} |
| 9649 | }, |
| 9650 | { |
| 9651 | "Infinity", |
| 9652 | {"\xf9\x7c\x00", 3}, |
| 9653 | QCBOR_SUCCESS, |
| 9654 | QCBOR_TYPE_DOUBLE, |
| 9655 | {0, 0, INFINITY} |
| 9656 | }, |
| 9657 | { |
| 9658 | "1.0", |
| 9659 | {"\xf9\x3c\x00", 3}, |
| 9660 | QCBOR_SUCCESS, |
| 9661 | QCBOR_TYPE_INT64, |
| 9662 | {1, 0, 0} |
| 9663 | }, |
| 9664 | { |
| 9665 | "UINT64_MAX", |
| 9666 | {"\x1B\xff\xff\xff\xff\xff\xff\xff\xff", 9}, |
| 9667 | QCBOR_SUCCESS, |
| 9668 | QCBOR_TYPE_UINT64, |
| 9669 | {0, UINT64_MAX, 0} |
| 9670 | }, |
| 9671 | { |
| 9672 | "INT64_MIN", |
| 9673 | {"\x3B\x7f\xff\xff\xff\xff\xff\xff\xff", 9}, |
| 9674 | QCBOR_SUCCESS, |
| 9675 | QCBOR_TYPE_INT64, |
| 9676 | {INT64_MIN, 0, 0} |
| 9677 | }, |
| 9678 | { |
| 9679 | "18446742974197923840", |
| 9680 | {"\xFB\x43\xEF\xFF\xFF\xE0\x00\x00\x00", 9}, |
| 9681 | QCBOR_SUCCESS, |
| 9682 | QCBOR_TYPE_UINT64, |
| 9683 | {0, 18446742974197923840ULL, 0} |
| 9684 | }, |
| 9685 | { |
| 9686 | "65-bit neg, too much precision", |
| 9687 | {"\x3B\x80\x00\x00\x00\x00\x00\x00\x01", 9}, |
| 9688 | QCBOR_SUCCESS, |
| 9689 | QCBOR_TYPE_65BIT_NEG_INT, |
| 9690 | {0, 0x8000000000000001, 0} |
| 9691 | }, |
| 9692 | { |
| 9693 | "65-bit neg lots of precision", |
| 9694 | {"\x3B\xff\xff\xff\xff\xff\xff\xf0\x00", 9}, |
| 9695 | QCBOR_SUCCESS, |
| 9696 | QCBOR_TYPE_DOUBLE, |
| 9697 | {0, 0, -18446744073709547521.0} |
| 9698 | }, |
| 9699 | { |
| 9700 | "65-bit neg very precise", |
| 9701 | {"\x3B\xff\xff\xff\xff\xff\xff\xf8\x00", 9}, |
| 9702 | QCBOR_SUCCESS, |
| 9703 | QCBOR_TYPE_DOUBLE, |
| 9704 | {0, 0, -18446744073709549569.0} |
| 9705 | }, |
| 9706 | { |
| 9707 | "65-bit neg too precise", |
| 9708 | {"\x3B\xff\xff\xff\xff\xff\xff\xfc\x00", 9}, |
| 9709 | QCBOR_SUCCESS, |
| 9710 | QCBOR_TYPE_65BIT_NEG_INT, |
| 9711 | {0, 18446744073709550592ULL, 0.0} |
| 9712 | }, |
| 9713 | { |
| 9714 | "65-bit neg, power of two", |
| 9715 | {"\x3B\x80\x00\x00\x00\x00\x00\x00\x00", 9}, |
| 9716 | QCBOR_SUCCESS, |
| 9717 | QCBOR_TYPE_DOUBLE, |
| 9718 | {0, 0, -9223372036854775809.0} |
| 9719 | }, |
| 9720 | { |
| 9721 | "Zero", |
| 9722 | {"\x00", 1}, |
| 9723 | QCBOR_SUCCESS, |
| 9724 | QCBOR_TYPE_INT64, |
| 9725 | {0, 0, 0} |
| 9726 | }, |
| 9727 | { |
| 9728 | "Pi", |
| 9729 | {"\xFB\x40\x09\x2A\xDB\x40\x2D\x16\xB9", 9}, |
| 9730 | QCBOR_SUCCESS, |
| 9731 | QCBOR_TYPE_DOUBLE, |
| 9732 | {0, 0, 3.145926} |
| 9733 | }, |
| 9734 | { |
| 9735 | "String", |
| 9736 | {"\x60", 1}, |
| 9737 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 9738 | QCBOR_TYPE_NONE, |
| 9739 | {0, 0, 0} |
| 9740 | } |
| 9741 | }; |
| 9742 | |
| 9743 | |
| 9744 | int32_t |
| 9745 | PreciseNumbersTest(void) |
| 9746 | { |
| 9747 | int i; |
| 9748 | QCBORError uErr; |
| 9749 | QCBORItem Item; |
| 9750 | QCBORDecodeContext DCtx; |
| 9751 | const struct PreciseNumberConversion *pTest; |
| 9752 | |
| 9753 | const int count = (int)C_ARRAY_COUNT(PreciseNumberConversions, struct PreciseNumberConversion); |
| 9754 | |
| 9755 | for(i = 0; i < count; i++) { |
| 9756 | pTest = &PreciseNumberConversions[i]; |
| 9757 | |
| 9758 | if(i == 11) { |
| 9759 | uErr = 99; // For break point only |
| 9760 | } |
| 9761 | |
| 9762 | QCBORDecode_Init(&DCtx, pTest->CBOR, 0); |
| 9763 | |
| 9764 | QCBORDecode_GetNumberConvertPrecisely(&DCtx, &Item); |
| 9765 | |
| 9766 | uErr = QCBORDecode_GetError(&DCtx); |
| 9767 | |
| 9768 | if(uErr != pTest->uError) { |
| 9769 | return i * 1000 + (int)uErr; |
| 9770 | } |
| 9771 | |
| 9772 | if(pTest->qcborType != Item.uDataType) { |
| 9773 | return i * 1000 + 200; |
| 9774 | } |
| 9775 | |
| 9776 | if(pTest->qcborType == QCBOR_TYPE_NONE) { |
| 9777 | continue; |
| 9778 | } |
| 9779 | |
| 9780 | switch(pTest->qcborType) { |
| 9781 | case QCBOR_TYPE_INT64: |
| 9782 | if(Item.val.int64 != pTest->number.int64) { |
| 9783 | return i * 1000 + 300; |
| 9784 | } |
| 9785 | break; |
| 9786 | |
| 9787 | case QCBOR_TYPE_UINT64: |
| 9788 | case QCBOR_TYPE_65BIT_NEG_INT: |
| 9789 | if(Item.val.uint64 != pTest->number.uint64) { |
| 9790 | return i * 1000 + 400; |
| 9791 | } |
| 9792 | break; |
| 9793 | |
| 9794 | case QCBOR_TYPE_DOUBLE: |
| 9795 | if(isnan(pTest->number.d)) { |
| 9796 | if(!isnan(Item.val.dfnum)) { |
| 9797 | return i * 1000 + 600; |
| 9798 | } |
| 9799 | } else { |
| 9800 | if(Item.val.dfnum != pTest->number.d) { |
| 9801 | return i * 1000 + 500; |
| 9802 | } |
| 9803 | } |
| 9804 | break; |
| 9805 | } |
| 9806 | } |
| 9807 | return 0; |
| 9808 | } |
| 9809 | |
| 9810 | #endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 9811 | |
| 9812 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9813 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9814 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9815 | static const uint8_t spExpectedArray2s[] = { |
| 9816 | 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9817 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9818 | 0x32}; |
| 9819 | |
| 9820 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9821 | static const uint8_t spExpectedArray2sIndef[] = { |
| 9822 | 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9823 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9824 | 0x32, 0xff}; |
| 9825 | #endif |
| 9826 | |
| 9827 | static const uint8_t spExpectedMap4[] = { |
| 9828 | 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9829 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9830 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9831 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9832 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9833 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9834 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9835 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9836 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9837 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73}; |
| 9838 | |
| 9839 | |
| 9840 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9841 | |
| 9842 | static const uint8_t spExpectedMap4Indef[] = { |
| 9843 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9844 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9845 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9846 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9847 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9848 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9849 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9850 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9851 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9852 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 9853 | 0xff}; |
| 9854 | |
| 9855 | /* |
| 9856 | * [[[[[0, []]]]], 0] |
| 9857 | */ |
| 9858 | static const uint8_t spDefAndIndef[] = { |
| 9859 | 0x82, |
| 9860 | 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00 |
| 9861 | }; |
| 9862 | #endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9863 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9864 | #ifndef QCBOR_DISABLE_TAGS |
| 9865 | /* An exp / mant tag in two nested arrays */ |
| 9866 | static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03}; |
| 9867 | #endif /* !QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9868 | #endif |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9869 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9870 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9871 | int32_t GetMapAndArrayTest(void) |
| 9872 | { |
| 9873 | QCBORDecodeContext DCtx; |
| 9874 | size_t uPosition ; |
| 9875 | QCBORItem Item; |
| 9876 | UsefulBufC ReturnedEncodedCBOR; |
| 9877 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9878 | // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9879 | QCBORDecode_Init(&DCtx, |
| 9880 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9881 | 0); |
| 9882 | |
| 9883 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9884 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9885 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9886 | if(QCBORDecode_GetError(&DCtx)) { |
| 9887 | return 1; |
| 9888 | } |
| 9889 | if(Item.val.uCount != 2) { |
| 9890 | return 2; |
| 9891 | } |
| 9892 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9893 | return 3; |
| 9894 | } |
| 9895 | |
| 9896 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9897 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9898 | return 4; |
| 9899 | } |
| 9900 | |
| 9901 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9902 | |
| 9903 | |
| 9904 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9905 | if(QCBORDecode_GetError(&DCtx)) { |
| 9906 | return 10; |
| 9907 | } |
| 9908 | if(Item.val.uCount != 4) { |
| 9909 | return 11; |
| 9910 | } |
| 9911 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9912 | return 12; |
| 9913 | } |
| 9914 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9915 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 9916 | "an array of two strings", |
| 9917 | &Item, |
| 9918 | &ReturnedEncodedCBOR); |
| 9919 | if(QCBORDecode_GetError(&DCtx)) { |
| 9920 | return 20; |
| 9921 | } |
| 9922 | if(Item.val.uCount != 2) { |
| 9923 | return 21; |
| 9924 | } |
| 9925 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9926 | return 22; |
| 9927 | } |
| 9928 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9929 | return 23; |
| 9930 | } |
| 9931 | |
| 9932 | QCBORDecode_Rewind(&DCtx); |
| 9933 | |
| 9934 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9935 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9936 | if(QCBORDecode_GetError(&DCtx)) { |
| 9937 | return 30; |
| 9938 | } |
| 9939 | if(Item.val.uCount != 4) { |
| 9940 | return 31; |
| 9941 | } |
| 9942 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9943 | return 32; |
| 9944 | } |
| 9945 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9946 | return 33; |
| 9947 | } |
| 9948 | |
| 9949 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9950 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9951 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9952 | return 40; |
| 9953 | } |
| 9954 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 9955 | return 41; |
| 9956 | } |
| 9957 | QCBORDecode_GetAndResetError(&DCtx); |
| 9958 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9959 | return 42; |
| 9960 | } |
| 9961 | |
| 9962 | |
| 9963 | #ifndef QCBOR_DISABLE_TAGS |
| 9964 | UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant); |
| 9965 | QCBORDecode_Init(&DCtx, ExpMant, 0); |
| 9966 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9967 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9968 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9969 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9970 | return 200; |
| 9971 | } |
| 9972 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 9973 | return 201; |
| 9974 | } |
| 9975 | if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) { |
| 9976 | return 202; |
| 9977 | } |
| 9978 | if(Item.val.uCount != 2) { |
| 9979 | return 201; |
| 9980 | } |
| 9981 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) { |
| 9982 | return 205; |
| 9983 | } |
| 9984 | #endif /* !QCBOR_DISABLE_TAGS */ |
| 9985 | |
| 9986 | |
| 9987 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9988 | |
| 9989 | UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef); |
| 9990 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9991 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9992 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9993 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9994 | return 50; |
| 9995 | } |
| 9996 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) { |
| 9997 | return 51; |
| 9998 | } |
| 9999 | |
| 10000 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 10001 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10002 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10003 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10004 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10005 | return 52; |
| 10006 | } |
| 10007 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) { |
| 10008 | return 53; |
| 10009 | } |
| 10010 | |
| 10011 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 10012 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10013 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10014 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10015 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10016 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10017 | return 54; |
| 10018 | } |
| 10019 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) { |
| 10020 | return 55; |
| 10021 | } |
| 10022 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 10023 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10024 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10025 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10026 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10027 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10028 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10029 | return 56; |
| 10030 | } |
| 10031 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) { |
| 10032 | return 57; |
| 10033 | } |
| 10034 | |
| 10035 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 10036 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10037 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10038 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10039 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10040 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10041 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 10042 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10043 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10044 | return 58; |
| 10045 | } |
| 10046 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) { |
| 10047 | return 59; |
| 10048 | } |
| 10049 | |
| 10050 | |
| 10051 | QCBORDecode_Init(&DCtx, |
| 10052 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10053 | 0); |
| 10054 | |
| 10055 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 10056 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 10057 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10058 | if(QCBORDecode_GetError(&DCtx)) { |
| 10059 | return 60; |
| 10060 | } |
| 10061 | if(Item.val.uCount != UINT16_MAX) { |
| 10062 | return 61; |
| 10063 | } |
| 10064 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 10065 | return 62; |
| 10066 | } |
| 10067 | |
| 10068 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 10069 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 10070 | return 63; |
| 10071 | } |
| 10072 | |
| 10073 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10074 | |
| 10075 | |
| 10076 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10077 | if(QCBORDecode_GetError(&DCtx)) { |
| 10078 | return 70; |
| 10079 | } |
| 10080 | if(Item.val.uCount != UINT16_MAX) { |
| 10081 | return 71; |
| 10082 | } |
| 10083 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 10084 | return 72; |
| 10085 | } |
| 10086 | |
| 10087 | |
| 10088 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10089 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 10090 | "an array of two strings", |
| 10091 | &Item, |
| 10092 | &ReturnedEncodedCBOR); |
| 10093 | if(QCBORDecode_GetError(&DCtx)) { |
| 10094 | return 80; |
| 10095 | } |
| 10096 | if(Item.val.uCount != UINT16_MAX) { |
| 10097 | return 81; |
| 10098 | } |
| 10099 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 10100 | return 82; |
| 10101 | } |
| 10102 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10103 | return 83; |
| 10104 | } |
| 10105 | |
| 10106 | QCBORDecode_Rewind(&DCtx); |
| 10107 | |
| 10108 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10109 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 10110 | if(QCBORDecode_GetError(&DCtx)) { |
| 10111 | return 90; |
| 10112 | } |
| 10113 | if(Item.val.uCount != UINT16_MAX) { |
| 10114 | return 91; |
| 10115 | } |
| 10116 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 10117 | return 92; |
| 10118 | } |
| 10119 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10120 | return 93; |
| 10121 | } |
| 10122 | |
| 10123 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10124 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 10125 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10126 | return 100; |
| 10127 | } |
| 10128 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 10129 | return 101; |
| 10130 | } |
| 10131 | QCBORDecode_GetAndResetError(&DCtx); |
| 10132 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10133 | return 102; |
| 10134 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10135 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 10136 | |
| 10137 | return 0; |
| 10138 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10139 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 10140 | |
| 10141 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 10142 | int32_t |
| 10143 | ErrorHandlingTests(void) |
| 10144 | { |
| 10145 | QCBORDecodeContext DCtx; |
| 10146 | QCBORItem Item; |
| 10147 | QCBORError uError; |
| 10148 | int64_t integer; |
| 10149 | |
| 10150 | /* Test QCBORDecode_SetError() */ |
| 10151 | QCBORDecode_Init(&DCtx, |
| 10152 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10153 | QCBOR_DECODE_MODE_NORMAL); |
| 10154 | |
| 10155 | QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED); |
| 10156 | |
| 10157 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10158 | |
| 10159 | uError = QCBORDecode_GetError(&DCtx); |
| 10160 | |
| 10161 | if(uError != QCBOR_ERR_FIRST_USER_DEFINED) { |
| 10162 | return -1; |
| 10163 | } |
| 10164 | |
| 10165 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 10166 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 10167 | return -2; |
| 10168 | } |
| 10169 | |
| 10170 | |
| 10171 | /* Test data type returned from previous error */ |
| 10172 | QCBORDecode_Init(&DCtx, |
| 10173 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10174 | QCBOR_DECODE_MODE_NORMAL); |
| 10175 | QCBORDecode_GetInt64(&DCtx, &integer); |
| 10176 | uError = QCBORDecode_GetError(&DCtx); |
| 10177 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10178 | return -3; |
| 10179 | } |
| 10180 | |
| 10181 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10182 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 10183 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 10184 | return -2; |
| 10185 | } |
| 10186 | uError = QCBORDecode_GetError(&DCtx); |
| 10187 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10188 | return -3; |
| 10189 | } |
| 10190 | |
| 10191 | |
| 10192 | /* Test error classification functions */ |
| 10193 | |
| 10194 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 10195 | return -10; |
| 10196 | } |
| 10197 | if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) { |
| 10198 | return -11; |
| 10199 | } |
| 10200 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 10201 | return -12; |
| 10202 | } |
| 10203 | if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) { |
| 10204 | return -13; |
| 10205 | } |
| 10206 | |
| 10207 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) { |
| 10208 | return -20; |
| 10209 | } |
| 10210 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) { |
| 10211 | return -21; |
| 10212 | } |
| 10213 | if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) { |
| 10214 | return -22; |
| 10215 | } |
| 10216 | if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) { |
| 10217 | return -23; |
| 10218 | } |
| 10219 | |
Laurence Lundblade | cd34e58 | 2024-06-06 10:55:14 -0700 | [diff] [blame] | 10220 | /* Test error strings */ |
| 10221 | const char *szErrString; |
| 10222 | |
| 10223 | szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG); |
| 10224 | if(szErrString == NULL) { |
| 10225 | return -100; |
| 10226 | } |
| 10227 | if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) { |
| 10228 | return -101; |
| 10229 | } |
| 10230 | |
| 10231 | szErrString = qcbor_err_to_str(QCBOR_SUCCESS); |
| 10232 | if(szErrString == NULL) { |
| 10233 | return -102; |
| 10234 | } |
| 10235 | if(strcmp(szErrString, "QCBOR_SUCCESS")) { |
| 10236 | return -103; |
| 10237 | } |
| 10238 | |
| 10239 | szErrString = qcbor_err_to_str(100); |
| 10240 | if(szErrString == NULL) { |
| 10241 | return -104; |
| 10242 | } |
| 10243 | if(strcmp(szErrString, "Unidentified QCBOR error")) { |
| 10244 | return -105; |
| 10245 | } |
| 10246 | |
| 10247 | szErrString = qcbor_err_to_str(200); |
| 10248 | if(szErrString == NULL) { |
| 10249 | return -106; |
| 10250 | } |
| 10251 | if(strcmp(szErrString, "USER_DEFINED_200")) { |
| 10252 | return -107; |
| 10253 | } |
| 10254 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 10255 | return 0; |
| 10256 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10257 | |
| 10258 | |
| 10259 | int32_t TellTests(void) |
| 10260 | { |
| 10261 | QCBORDecodeContext DCtx; |
| 10262 | QCBORItem Item; |
| 10263 | uint32_t uPosition; |
| 10264 | int nIndex; |
| 10265 | int64_t nDecodedInt; |
| 10266 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10267 | // Improvement: rewrite so this can run with only integer labels |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10268 | static const uint32_t aPos[] = |
| 10269 | {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, UINT32_MAX}; |
| 10270 | QCBORDecode_Init(&DCtx, |
| 10271 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10272 | 0); |
| 10273 | for(nIndex = 0; ; nIndex++) { |
| 10274 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10275 | if(uPosition != aPos[nIndex]) { |
| 10276 | return nIndex; |
| 10277 | } |
| 10278 | |
| 10279 | if(uPosition == UINT32_MAX) { |
| 10280 | break; |
| 10281 | } |
| 10282 | |
| 10283 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10284 | } |
| 10285 | |
| 10286 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10287 | static const uint32_t aPosIndef[] = |
| 10288 | {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, UINT32_MAX}; |
| 10289 | QCBORDecode_Init(&DCtx, |
| 10290 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10291 | 0); |
| 10292 | |
| 10293 | for(nIndex = 0; ; nIndex++) { |
| 10294 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10295 | if(uPosition != aPosIndef[nIndex]) { |
| 10296 | return nIndex + 100; |
| 10297 | } |
| 10298 | |
| 10299 | if(uPosition == UINT32_MAX) { |
| 10300 | break; |
| 10301 | } |
| 10302 | |
| 10303 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10304 | } |
| 10305 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10306 | |
| 10307 | /* Next, some tests with entered maps and arrays */ |
| 10308 | QCBORDecode_Init(&DCtx, |
| 10309 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10310 | 0); |
| 10311 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10312 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10313 | return 1001; |
| 10314 | } |
| 10315 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10316 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10317 | return 1002; |
| 10318 | } |
| 10319 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10320 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10321 | return 1003; |
| 10322 | } |
| 10323 | |
| 10324 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10325 | if(nDecodedInt != 98) { |
| 10326 | return 1004; |
| 10327 | } |
| 10328 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10329 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10330 | return 1005; |
| 10331 | } |
| 10332 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10333 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10334 | return 1006; |
| 10335 | } |
| 10336 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10337 | if(nDecodedInt != 98) { |
| 10338 | return 1007; |
| 10339 | } |
| 10340 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10341 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10342 | return 1008; |
| 10343 | } |
| 10344 | |
| 10345 | QCBORDecode_ExitMap(&DCtx); |
| 10346 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10347 | return 1009; |
| 10348 | } |
| 10349 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10350 | return 1010; |
| 10351 | } |
| 10352 | |
| 10353 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10354 | /* Next, some tests with entered maps and arrays */ |
| 10355 | QCBORDecode_Init(&DCtx, |
| 10356 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10357 | 0); |
| 10358 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10359 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10360 | return 2000; |
| 10361 | } |
| 10362 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10363 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10364 | return 2001; |
| 10365 | } |
| 10366 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10367 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10368 | return 2002; |
| 10369 | } |
| 10370 | |
| 10371 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10372 | if(nDecodedInt != 98) { |
| 10373 | return 2003; |
| 10374 | } |
| 10375 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10376 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10377 | return 2004; |
| 10378 | } |
| 10379 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10380 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10381 | return 2005; |
| 10382 | } |
| 10383 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10384 | if(nDecodedInt != 98) { |
| 10385 | return 2006; |
| 10386 | } |
| 10387 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10388 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10389 | return 2007; |
| 10390 | } |
| 10391 | |
| 10392 | QCBORDecode_ExitMap(&DCtx); |
| 10393 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10394 | return 2008; |
| 10395 | } |
| 10396 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10397 | return 2010; |
| 10398 | } |
| 10399 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10400 | |
| 10401 | |
| 10402 | |
| 10403 | /* Error state test */ |
| 10404 | QCBORDecode_Init(&DCtx, |
| 10405 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10406 | 0); |
| 10407 | /* Cause an error */ |
| 10408 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10409 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10410 | return 3000; |
| 10411 | } |
| 10412 | |
| 10413 | /* Empties tests */ |
| 10414 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 10415 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 10416 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10417 | return 4000; |
| 10418 | } |
| 10419 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10420 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10421 | return 4001; |
| 10422 | } |
| 10423 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10424 | return 4002; |
| 10425 | } |
| 10426 | QCBORDecode_ExitMap(&DCtx); |
| 10427 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10428 | return 4001; |
| 10429 | } |
| 10430 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10431 | return 4002; |
| 10432 | } |
| 10433 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10434 | return 4010; |
| 10435 | } |
| 10436 | |
| 10437 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10438 | const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map |
| 10439 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0); |
| 10440 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10441 | return 4100; |
| 10442 | } |
| 10443 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10444 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10445 | return 4101; |
| 10446 | } |
| 10447 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10448 | return 4102; |
| 10449 | } |
| 10450 | QCBORDecode_ExitMap(&DCtx); |
| 10451 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10452 | return 4101; |
| 10453 | } |
| 10454 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10455 | return 4102; |
| 10456 | } |
| 10457 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10458 | return 4110; |
| 10459 | } |
| 10460 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10461 | |
| 10462 | /* Test on a CBOR sequence */ |
| 10463 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0); |
| 10464 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10465 | return 5000; |
| 10466 | } |
| 10467 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10468 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10469 | return 5001; |
| 10470 | } |
| 10471 | if(QCBORDecode_Tell(&DCtx) != 11) { |
| 10472 | return 5002; |
| 10473 | } |
| 10474 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10475 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10476 | return 5003; |
| 10477 | } |
| 10478 | if(QCBORDecode_Tell(&DCtx) != 12) { |
| 10479 | return 5004; |
| 10480 | } |
| 10481 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10482 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10483 | return 5005; |
| 10484 | } |
| 10485 | if(QCBORDecode_Tell(&DCtx) != 17) { |
| 10486 | return 5006; |
| 10487 | } |
| 10488 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10489 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10490 | return 5007; |
| 10491 | } |
| 10492 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10493 | return 5008; |
| 10494 | } |
| 10495 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10496 | return 5010; |
| 10497 | } |
| 10498 | |
| 10499 | |
| 10500 | QCBORDecode_Init(&DCtx, |
| 10501 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10502 | 0); |
| 10503 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10504 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 10505 | if(QCBORDecode_Tell(&DCtx) != 42) { |
| 10506 | return 6001; |
| 10507 | } |
| 10508 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10509 | if(QCBORDecode_Tell(&DCtx) != 50) { |
| 10510 | return 6002; |
| 10511 | } |
| 10512 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10513 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10514 | return 6008; |
| 10515 | } |
| 10516 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10517 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 10518 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10519 | return 6003; |
| 10520 | } |
| 10521 | QCBORDecode_ExitArray(&DCtx); |
| 10522 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10523 | return 6004; |
| 10524 | } |
| 10525 | |
| 10526 | static const uint32_t aEmptiesPos[] = |
| 10527 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, UINT32_MAX}; |
| 10528 | QCBORDecode_Init(&DCtx, |
| 10529 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 10530 | 0); |
| 10531 | for(nIndex = 0; ; nIndex++) { |
| 10532 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10533 | if(uPosition != aEmptiesPos[nIndex]) { |
| 10534 | return nIndex + 200; |
| 10535 | } |
| 10536 | |
| 10537 | if(uPosition == UINT32_MAX) { |
| 10538 | break; |
| 10539 | } |
| 10540 | |
| 10541 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10542 | } |
| 10543 | |
| 10544 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10545 | static const uint32_t aIndefEmptiesPos[] = |
| 10546 | {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, UINT32_MAX}; |
| 10547 | QCBORDecode_Init(&DCtx, |
| 10548 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 10549 | 0); |
| 10550 | for(nIndex = 0; ; nIndex++) { |
| 10551 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10552 | if(uPosition != aIndefEmptiesPos[nIndex]) { |
| 10553 | return nIndex + 300; |
| 10554 | } |
| 10555 | |
| 10556 | if(uPosition == UINT32_MAX) { |
| 10557 | break; |
| 10558 | } |
| 10559 | |
| 10560 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10561 | } |
| 10562 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10563 | |
| 10564 | |
| 10565 | return 0; |
| 10566 | } |