Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, The Linux Foundation. |
| 3 | * Copyright (c) 2018-2024, Laurence Lundblade. |
| 4 | * Copyright (c) 2021, Arm Limited. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 10 | * Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above |
| 13 | copyright notice, this list of conditions and the following |
| 14 | disclaimer in the documentation and/or other materials provided |
| 15 | with the distribution. |
| 16 | * Neither the name of The Linux Foundation nor the names of its |
| 17 | contributors, nor the name "Laurence Lundblade" may be used to |
| 18 | endorse or promote products derived from this software without |
| 19 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 20 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 21 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 25 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 30 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 32 | =============================================================================*/ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 33 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 34 | #include "qcbor_decode_tests.h" |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 35 | #include "qcbor/qcbor_encode.h" |
| 36 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 37 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | d4728fd | 2018-12-17 15:15:56 -0800 | [diff] [blame] | 38 | #include <string.h> |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 39 | #include <math.h> // for fabs() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 40 | #include "not_well_formed_cbor.h" |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 41 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 42 | // Handy macro to compare a UsefulBuf to a C string |
| 43 | #define UsefulBufCompareToSZ(x, y) \ |
| 44 | UsefulBuf_Compare(x, UsefulBuf_FromSZ(y)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 45 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 46 | #ifdef PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 47 | #include <stdio.h> |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 48 | |
| 49 | static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 50 | { |
| 51 | if(szLabel) { |
| 52 | printf("%s ", szLabel); |
| 53 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 54 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 55 | size_t i; |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 56 | for(i = 0; i < Buf.len; i++) { |
| 57 | uint8_t Z = ((uint8_t *)Buf.ptr)[i]; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 58 | printf("%02x ", Z); |
| 59 | } |
| 60 | printf("\n"); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 61 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 62 | fflush(stdout); |
| 63 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 64 | #endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 65 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 66 | |
| 67 | /* Make a test results code that includes three components. Return code |
| 68 | * is xxxyyyzzz where zz is the error code, yy is the test number and |
| 69 | * zz is check being performed |
| 70 | */ |
| 71 | static int32_t |
| 72 | MakeTestResultCode(uint32_t uTestCase, |
| 73 | uint32_t uTestNumber, |
| 74 | QCBORError uErrorCode) |
| 75 | { |
| 76 | uint32_t uCode = (uTestCase * 1000000) + |
| 77 | (uTestNumber * 1000) + |
| 78 | (uint32_t)uErrorCode; |
| 79 | return (int32_t)uCode; |
| 80 | } |
| 81 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 82 | /* |
| 83 | [ |
| 84 | -9223372036854775808, |
| 85 | -4294967297, |
| 86 | -4294967296, |
| 87 | -4294967295, |
| 88 | -4294967294, |
| 89 | -2147483648, |
| 90 | -2147483647, |
| 91 | -65538, |
| 92 | -65537, |
| 93 | -65536, |
| 94 | -65535, |
| 95 | -65534, |
| 96 | -257, |
| 97 | -256, |
| 98 | -255, |
| 99 | -254, |
| 100 | -25, |
| 101 | -24, |
| 102 | -23, |
| 103 | -1, |
| 104 | 0, |
| 105 | 0, |
| 106 | 1, |
| 107 | 22, |
| 108 | 23, |
| 109 | 24, |
| 110 | 25, |
| 111 | 26, |
| 112 | 254, |
| 113 | 255, |
| 114 | 256, |
| 115 | 257, |
| 116 | 65534, |
| 117 | 65535, |
| 118 | 65536, |
| 119 | 65537, |
| 120 | 65538, |
| 121 | 2147483647, |
| 122 | 2147483647, |
| 123 | 2147483648, |
| 124 | 2147483649, |
| 125 | 4294967294, |
| 126 | 4294967295, |
| 127 | 4294967296, |
| 128 | 4294967297, |
| 129 | 9223372036854775807, |
| 130 | 18446744073709551615 |
| 131 | ] |
| 132 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 133 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 134 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 135 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 136 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 137 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 138 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 139 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 140 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 141 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 142 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 143 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 144 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 145 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 146 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 147 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 148 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 149 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 150 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 151 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 152 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 153 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 154 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 155 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 156 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 157 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 158 | 0xff, 0xff}; |
| 159 | |
| 160 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 161 | // return CBOR error or -1 if type of value doesn't match |
| 162 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 163 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 164 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 165 | QCBORItem Item; |
| 166 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 167 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 168 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 169 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 170 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 171 | return -1; |
| 172 | |
| 173 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 174 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 175 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 176 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 177 | return -1; |
| 178 | |
| 179 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 180 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 181 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 182 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 183 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 184 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 185 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 186 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 187 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 188 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 189 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 190 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 191 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 192 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 193 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 194 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 195 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 196 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 197 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 198 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 199 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 200 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 202 | |
| 203 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 204 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 205 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 206 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 207 | Item.val.int64 != -2147483648) |
| 208 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 209 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 210 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 211 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 212 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 213 | Item.val.int64 != -2147483647) |
| 214 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 215 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 216 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 217 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 218 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 219 | Item.val.int64 != -65538) |
| 220 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 221 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 222 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 223 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 224 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 225 | Item.val.int64 != -65537) |
| 226 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 227 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 228 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 229 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 230 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 231 | Item.val.int64 != -65536) |
| 232 | return -1; |
| 233 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 234 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 235 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 236 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 237 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 238 | Item.val.int64 != -65535) |
| 239 | return -1; |
| 240 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 241 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 242 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 243 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 244 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 245 | Item.val.int64 != -65534) |
| 246 | return -1; |
| 247 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 248 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 249 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 250 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 251 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 252 | Item.val.int64 != -257) |
| 253 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 254 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 255 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 256 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 257 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 258 | Item.val.int64 != -256) |
| 259 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 260 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 261 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 262 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 263 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 264 | Item.val.int64 != -255) |
| 265 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 266 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 267 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 268 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 269 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 270 | Item.val.int64 != -254) |
| 271 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 272 | |
| 273 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 274 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 275 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 276 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 277 | Item.val.int64 != -25) |
| 278 | return -1; |
| 279 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 280 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 281 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 282 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 283 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 284 | Item.val.int64 != -24) |
| 285 | return -1; |
| 286 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 287 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 288 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 289 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 290 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 291 | Item.val.int64 != -23) |
| 292 | return -1; |
| 293 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 294 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 295 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 296 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 297 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 298 | Item.val.int64 != -1) |
| 299 | return -1; |
| 300 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 301 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 302 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 303 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 304 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 305 | Item.val.int64 != 0) |
| 306 | return -1; |
| 307 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 308 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 309 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 310 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 311 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 312 | Item.val.int64 != 0) |
| 313 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 314 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 315 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 316 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 317 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 318 | Item.val.int64 != 1) |
| 319 | return -1; |
| 320 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 321 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 322 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 323 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 324 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 325 | Item.val.int64 != 22) |
| 326 | return -1; |
| 327 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 328 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 329 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 330 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 331 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 332 | Item.val.int64 != 23) |
| 333 | return -1; |
| 334 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 335 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 336 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 337 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 338 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 339 | Item.val.int64 != 24) |
| 340 | return -1; |
| 341 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 342 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 343 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 344 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 345 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 346 | Item.val.int64 != 25) |
| 347 | return -1; |
| 348 | |
| 349 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 350 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 351 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 352 | Item.val.int64 != 26) |
| 353 | return -1; |
| 354 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 355 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 356 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 357 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 358 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 359 | Item.val.int64 != 254) |
| 360 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 361 | |
| 362 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 363 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 364 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 365 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 366 | Item.val.int64 != 255) |
| 367 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 368 | |
| 369 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 370 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 371 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 372 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 373 | Item.val.int64 != 256) |
| 374 | return -1; |
| 375 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 376 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 377 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 378 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 379 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 380 | Item.val.int64 != 257) |
| 381 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 382 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 383 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 384 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 385 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 386 | Item.val.int64 != 65534) |
| 387 | return -1; |
| 388 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 389 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 390 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 391 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 392 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 393 | Item.val.int64 != 65535) |
| 394 | return -1; |
| 395 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 396 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 397 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 398 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 399 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 400 | Item.val.int64 != 65536) |
| 401 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 402 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 403 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 404 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 405 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 406 | Item.val.int64 != 65537) |
| 407 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 408 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 409 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 410 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 411 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 412 | Item.val.int64 != 65538) |
| 413 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 414 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 415 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 416 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 417 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 418 | Item.val.int64 != 2147483647) |
| 419 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 420 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 421 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 422 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 423 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 424 | Item.val.int64 != 2147483647) |
| 425 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 426 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 427 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 428 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 429 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 430 | Item.val.int64 != 2147483648) |
| 431 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 432 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 433 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 434 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 435 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 436 | Item.val.int64 != 2147483649) |
| 437 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 438 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 439 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 440 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 441 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 442 | Item.val.int64 != 4294967294) |
| 443 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 444 | |
| 445 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 446 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 447 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 448 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 449 | Item.val.int64 != 4294967295) |
| 450 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 451 | |
| 452 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 453 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 454 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 455 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 456 | Item.val.int64 != 4294967296) |
| 457 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 458 | |
| 459 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 460 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 461 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 462 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 463 | Item.val.int64 != 4294967297) |
| 464 | return -1; |
| 465 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 466 | |
| 467 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 468 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 469 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 470 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 471 | Item.val.int64 != 9223372036854775807LL) |
| 472 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 473 | |
| 474 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 475 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 476 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 477 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 478 | Item.val.uint64 != 18446744073709551615ULL) |
| 479 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 480 | |
| 481 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 482 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 483 | return -1; |
| 484 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 485 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 490 | /* One less than the smallest negative integer allowed in C. Decoding |
| 491 | this should fail. |
| 492 | -9223372036854775809 |
| 493 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 494 | static const uint8_t spTooSmallNegative[] = { |
| 495 | 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 499 | /* |
| 500 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 501 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 502 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 503 | int32_t IntegerValuesParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 504 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 505 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 506 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 507 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 508 | QCBORDecode_Init(&DCtx, |
| 509 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 510 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 511 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 512 | // The really big test of all successes |
| 513 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 514 | if(nReturn) { |
| 515 | return nReturn; |
| 516 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 517 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 518 | // The one large negative integer that can be parsed |
| 519 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 520 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative), |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 521 | QCBOR_DECODE_MODE_NORMAL); |
| 522 | |
| 523 | QCBORItem item; |
| 524 | if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) { |
| 525 | nReturn = -4000; |
| 526 | } |
| 527 | |
| 528 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 533 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 534 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 535 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 536 | Two of the inputs can be set. Two other items in the array are fixed. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 537 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 538 | */ |
| 539 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 540 | static uint8_t spSimpleArrayBuffer[50]; |
| 541 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 542 | static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 543 | { |
| 544 | QCBOREncodeContext ECtx; |
| 545 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 546 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 547 | *pEncoded = NULL; |
| 548 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 549 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 550 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 551 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 552 | // and last with the buffer to do the actual encoding |
| 553 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 554 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 555 | QCBOREncode_OpenArray(&ECtx); |
| 556 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 557 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 558 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 559 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 560 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 561 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 562 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 563 | goto Done; |
| 564 | |
| 565 | if(*pEncoded != NULL) { |
| 566 | nReturn = 0; |
| 567 | goto Done; |
| 568 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 569 | |
| 570 | // Use static buffer to avoid dependency on malloc() |
| 571 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 572 | goto Done; |
| 573 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 574 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 575 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 576 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 577 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 578 | Done: |
| 579 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 583 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 584 | Some basic CBOR with map and array used in a lot of tests. |
| 585 | The map labels are all strings |
| 586 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 587 | { |
| 588 | "first integer": 42, |
| 589 | "an array of two strings": [ |
| 590 | "string1", "string2" |
| 591 | ], |
| 592 | "map in a map": { |
| 593 | "bytes 1": h'78787878', |
| 594 | "bytes 2": h'79797979', |
| 595 | "another int": 98, |
| 596 | "text 2": "lies, damn lies and statistics" |
| 597 | } |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 598 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 599 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 600 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 601 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 602 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 603 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 604 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 605 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 606 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 607 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 608 | 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, |
| 609 | 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4, |
| 610 | 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 611 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, |
| 612 | 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, |
| 613 | 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68, |
| 614 | 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, |
| 615 | 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 616 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, |
| 617 | 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, |
| 618 | 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, |
| 619 | 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 620 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 621 | |
| 622 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 623 | // Same as above, but with indefinite lengths. |
| 624 | static const uint8_t pValidMapIndefEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 625 | 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 626 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 627 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 628 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 629 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 630 | 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 631 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 632 | 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20, |
| 633 | 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, |
| 634 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 635 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 636 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 637 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 638 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 639 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 640 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 641 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 642 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 643 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 644 | 0xff, 0xff}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 645 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 646 | |
| 647 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 648 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 649 | size_t nLen, |
| 650 | int64_t *pInt1, |
| 651 | int64_t *pInt2, |
| 652 | const uint8_t **pBuf3, |
| 653 | size_t *pBuf3Len, |
| 654 | const uint8_t **pBuf4, |
| 655 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 656 | { |
| 657 | QCBORDecodeContext DCtx; |
| 658 | QCBORItem Item; |
| 659 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 660 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 661 | QCBORDecode_Init(&DCtx, |
| 662 | (UsefulBufC){pEncoded, nLen}, |
| 663 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 664 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 665 | // Make sure the first thing is a map |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 666 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 667 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 668 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 669 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 670 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 671 | // First integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 672 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 673 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 674 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 675 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 676 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 677 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 678 | // Second integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 679 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 680 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 681 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 682 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 683 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 684 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 685 | // First string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 686 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 687 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 688 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 689 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 690 | *pBuf3 = Item.val.string.ptr; |
| 691 | *pBuf3Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 692 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 693 | // Second string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 694 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 695 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 696 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 697 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 698 | *pBuf4 = Item.val.string.ptr; |
| 699 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 700 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 701 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 702 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 703 | Done: |
| 704 | return(nReturn); |
| 705 | } |
| 706 | |
| 707 | |
| 708 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 709 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 710 | int32_t SimpleArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 711 | { |
| 712 | uint8_t *pEncoded; |
| 713 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 714 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 715 | int64_t i1=0, i2=0; |
| 716 | size_t i3=0, i4=0; |
| 717 | const uint8_t *s3= (uint8_t *)""; |
| 718 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 719 | |
| 720 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 721 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 722 | return(-1); |
| 723 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 724 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 725 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 726 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 727 | if(i1 != 23 || |
| 728 | i2 != 6000 || |
| 729 | i3 != 8 || |
| 730 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 731 | memcmp("galactic", s3, 8) !=0 || |
| 732 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 733 | return(-1); |
| 734 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 735 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 736 | return(0); |
| 737 | } |
| 738 | |
| 739 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 740 | /* |
| 741 | [ |
| 742 | 0, |
| 743 | [], |
| 744 | [ |
| 745 | [], |
| 746 | [ |
| 747 | 0 |
| 748 | ], |
| 749 | {}, |
| 750 | { |
| 751 | 1: {}, |
| 752 | 2: {}, |
| 753 | 3: [] |
| 754 | } |
| 755 | ] |
| 756 | ] |
| 757 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 758 | static uint8_t sEmpties[] = { |
| 759 | 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 760 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 761 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 762 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 763 | /* Same as above, but with indefinte lengths */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 764 | static const uint8_t sEmptiesIndef[] = { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 765 | 0x9F, |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 766 | 0x00, |
| 767 | 0x9F, |
| 768 | 0xFF, |
| 769 | 0x9F, |
| 770 | 0x9F, |
| 771 | 0xFF, |
| 772 | 0x9F, |
| 773 | 0x00, |
| 774 | 0xFF, |
| 775 | 0xBF, |
| 776 | 0xFF, |
| 777 | 0xBF, |
| 778 | 0x01, |
| 779 | 0xBF, |
| 780 | 0xFF, |
| 781 | 0x02, |
| 782 | 0xBF, |
| 783 | 0xFF, |
| 784 | 0x03, |
| 785 | 0x9F, |
| 786 | 0xFF, |
| 787 | 0xFF, |
| 788 | 0xFF, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 789 | 0xFF}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 790 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 791 | |
| 792 | |
| 793 | static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts) |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 794 | { |
| 795 | QCBORDecodeContext DCtx; |
| 796 | QCBORItem Item; |
| 797 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 798 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 799 | input, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 800 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 801 | |
| 802 | // Array with 3 items |
| 803 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 804 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 805 | Item.uNestingLevel != 0 || |
| 806 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 807 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 808 | return -1; |
| 809 | } |
| 810 | |
| 811 | // An integer 0 |
| 812 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 813 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 814 | Item.uNestingLevel != 1 || |
| 815 | Item.uNextNestLevel != 1 || |
| 816 | Item.val.uint64 != 0) { |
| 817 | return -2; |
| 818 | } |
| 819 | |
| 820 | // An empty array |
| 821 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 822 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 823 | Item.uNestingLevel != 1 || |
| 824 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 825 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 826 | return -3; |
| 827 | } |
| 828 | |
| 829 | // An array with 4 items |
| 830 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 831 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 832 | Item.uNestingLevel != 1 || |
| 833 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 834 | (bCheckCounts && Item.val.uCount != 4)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 835 | return -4; |
| 836 | } |
| 837 | |
| 838 | // An empty array |
| 839 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 840 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 841 | Item.uNestingLevel != 2 || |
| 842 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 843 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 844 | return -5; |
| 845 | } |
| 846 | |
| 847 | // An array with 1 item |
| 848 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 849 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 850 | Item.uNestingLevel != 2 || |
| 851 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 852 | (bCheckCounts && Item.val.uCount != 1)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 853 | return -6; |
| 854 | } |
| 855 | |
| 856 | // An integer 0 |
| 857 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 858 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 859 | Item.uNestingLevel != 3 || |
| 860 | Item.uNextNestLevel != 2 || |
| 861 | Item.val.uint64 != 0) { |
| 862 | return -7; |
| 863 | } |
| 864 | |
| 865 | // An empty map |
| 866 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 867 | Item.uDataType != QCBOR_TYPE_MAP || |
| 868 | Item.uNestingLevel != 2 || |
| 869 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 870 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 871 | return -8; |
| 872 | } |
| 873 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 874 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 875 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 876 | Item.uDataType != QCBOR_TYPE_MAP || |
| 877 | Item.uNestingLevel != 2 || |
| 878 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 879 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 880 | return -9; |
| 881 | } |
| 882 | |
| 883 | // An empty map |
| 884 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 885 | Item.uDataType != QCBOR_TYPE_MAP || |
| 886 | Item.uNestingLevel != 3 || |
| 887 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 888 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 889 | return -10; |
| 890 | } |
| 891 | |
| 892 | // An empty map |
| 893 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 894 | Item.uDataType != QCBOR_TYPE_MAP || |
| 895 | Item.uNestingLevel != 3 || |
| 896 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 897 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 898 | return -11; |
| 899 | } |
| 900 | |
| 901 | // An empty array |
| 902 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 903 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 904 | Item.uNestingLevel != 3 || |
| 905 | Item.uNextNestLevel != 0 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 906 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 907 | return -12; |
| 908 | } |
| 909 | |
| 910 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 911 | return -13; |
| 912 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 917 | int32_t EmptyMapsAndArraysTest(void) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 918 | { |
| 919 | int nResult; |
| 920 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 921 | true); |
| 922 | if(nResult) { |
| 923 | return nResult; |
| 924 | } |
| 925 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 926 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 927 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 928 | false); |
| 929 | |
| 930 | if(nResult) { |
| 931 | return nResult -100; |
| 932 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 933 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 938 | |
| 939 | static const uint8_t sEmptyMap[] = { |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 940 | 0xA1, //# map(1) |
| 941 | 0x02, //# unsigned(2) |
| 942 | 0xA0, //# map(0) |
| 943 | }; |
| 944 | |
| 945 | int32_t ParseEmptyMapInMapTest(void) |
| 946 | { |
| 947 | QCBORDecodeContext DCtx; |
| 948 | QCBORItem Item; |
| 949 | int nReturn = 0; |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 950 | QCBORError uErr; |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 951 | |
| 952 | QCBORDecode_Init(&DCtx, |
| 953 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap), |
| 954 | QCBOR_DECODE_MODE_NORMAL); |
| 955 | |
| 956 | /* now open the first Map */ |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 957 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 958 | if(uErr != QCBOR_SUCCESS || |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 959 | Item.uDataType != QCBOR_TYPE_MAP) { |
| 960 | nReturn = -3; |
| 961 | goto done; |
| 962 | } |
| 963 | |
| 964 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0) { |
| 965 | nReturn = -1; |
| 966 | goto done; |
| 967 | } |
| 968 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 969 | Item.uNestingLevel != 1 || |
| 970 | Item.label.int64 != 2) { |
| 971 | nReturn = -2; |
| 972 | goto done; |
| 973 | } |
| 974 | |
| 975 | done: |
| 976 | return(nReturn); |
| 977 | } |
| 978 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 979 | |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 980 | /* [[[[[[[[[[]]]]]]]]]] */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 981 | static const uint8_t spDeepArrays[] = { |
| 982 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 983 | 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 984 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 985 | int32_t ParseDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 986 | { |
| 987 | QCBORDecodeContext DCtx; |
| 988 | int nReturn = 0; |
| 989 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 990 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 991 | QCBORDecode_Init(&DCtx, |
| 992 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 993 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 994 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 995 | for(i = 0; i < 10; i++) { |
| 996 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 997 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 998 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 999 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1000 | Item.uNestingLevel != i) { |
| 1001 | nReturn = -1; |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1005 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1006 | return(nReturn); |
| 1007 | } |
| 1008 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1009 | /* Big enough to test nesting to the depth of 24 |
| 1010 | [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]] |
| 1011 | */ |
| 1012 | static const uint8_t spTooDeepArrays[] = { |
| 1013 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1014 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1015 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1016 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1017 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1018 | int32_t ParseTooDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1019 | { |
| 1020 | QCBORDecodeContext DCtx; |
| 1021 | int nReturn = 0; |
| 1022 | int i; |
| 1023 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1024 | |
| 1025 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1026 | QCBORDecode_Init(&DCtx, |
| 1027 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 1028 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1029 | |
Laurence Lundblade | 3e0b230 | 2023-12-04 14:02:38 -0700 | [diff] [blame] | 1030 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1031 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1032 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 1033 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1034 | Item.uNestingLevel != i) { |
| 1035 | nReturn = -1; |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1039 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1040 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1041 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1042 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1043 | return(nReturn); |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1049 | int32_t ShortBufferParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1050 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1051 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1052 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1053 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1054 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1056 | QCBORDecode_Init(&DCtx, |
| 1057 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 1058 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1059 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1060 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1061 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1062 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1063 | nResult = -1; |
| 1064 | goto Done; |
| 1065 | } |
| 1066 | } |
| 1067 | Done: |
| 1068 | return nResult; |
| 1069 | } |
| 1070 | |
| 1071 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1072 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1073 | int32_t ShortBufferParseTest2(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1074 | { |
| 1075 | uint8_t *pEncoded; |
| 1076 | int nReturn; |
| 1077 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1078 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1079 | int64_t i1, i2; |
| 1080 | size_t i3, i4; |
| 1081 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1082 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1083 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1084 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1085 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 1086 | return(-1); |
| 1087 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1088 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1089 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1090 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 1091 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1092 | if(nResult == 0) { |
| 1093 | nReturn = -1; |
| 1094 | } |
| 1095 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1096 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1097 | return(nReturn); |
| 1098 | } |
| 1099 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1100 | |
| 1101 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1102 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1103 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1104 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1105 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1106 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1107 | |
| 1108 | static const uint8_t pPerverseLabels[] = { |
| 1109 | 0xae, |
| 1110 | |
| 1111 | 0xf5, 0x61, 0x61, |
| 1112 | |
| 1113 | 0xf6, 0x61, 0x62, |
| 1114 | |
| 1115 | 0xf8, 0xff, 0x61, 0x63, |
| 1116 | |
| 1117 | 0xf9, 0x7e, 0x00, 0x61, 0x64, |
| 1118 | |
| 1119 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0x61, 0x65, |
| 1120 | |
| 1121 | 0xfb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x66, |
| 1122 | |
| 1123 | 0xa1, 0x19, 0x03, 0xe8, 0x10, 0x61, 0x67, |
| 1124 | |
| 1125 | 0x81, 0x81, 0x81, 0x80, 0x61, 0x68, |
| 1126 | |
| 1127 | 0xc1, 0x09, 0x61, 0x69, |
| 1128 | |
| 1129 | 0x82, 0x05, 0xa2, 0x01, 0x02, 0x03, 0x04, 0x61, 0x6a, |
| 1130 | |
| 1131 | 0xbf, 0xff, 0x61, 0x6b, |
| 1132 | |
| 1133 | 0x9f, 0x11, 0x12, 0x13, 0xff, 0x61, 0x6c, |
| 1134 | |
| 1135 | 0x7f, 0x62, 0x41, 0x42, 0x62, 0x43, 0x44, 0xff, 0x61, 0x6d, |
| 1136 | |
| 1137 | 0xd9, 0x01, 0x02, 0xbf, 0x7f, 0x61, 0x4a, 0x61, 0x4b, 0xff, 0x00, 0xf4, 0xd7, 0x80 ,0xff, 0x61, 0x6e |
| 1138 | }; |
| 1139 | #endif |
| 1140 | |
| 1141 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1142 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1143 | /* |
| 1144 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1145 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 1146 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1147 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1148 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1149 | { |
| 1150 | QCBORDecodeContext DCtx; |
| 1151 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1152 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1153 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1154 | QCBORDecode_Init(&DCtx, |
| 1155 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1156 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1157 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1158 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1159 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1160 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1161 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1162 | Item.val.uCount != 3) |
| 1163 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1164 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1165 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1166 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1167 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1168 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1169 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1170 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1171 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1172 | Item.uDataAlloc || |
| 1173 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1174 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1175 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1176 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1177 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1178 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1179 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1180 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1181 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1182 | Item.uDataAlloc || |
| 1183 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1184 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1185 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1186 | Item.val.uCount != 2) |
| 1187 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1188 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1189 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1190 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1191 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1192 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1193 | Item.uDataAlloc || |
| 1194 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1195 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1196 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1197 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1198 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1199 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1200 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1201 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1202 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1203 | Item.uDataAlloc || |
| 1204 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1205 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1206 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1207 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1208 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1209 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1210 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1211 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1212 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1213 | Item.uDataAlloc || |
| 1214 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1215 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1216 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1217 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1218 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1219 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1220 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1221 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1222 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1223 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1224 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1225 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1226 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1227 | Item.uDataAlloc || |
| 1228 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1229 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1230 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1231 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1232 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1233 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1234 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1235 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1236 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1237 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1238 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1239 | Item.uDataAlloc || |
| 1240 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1241 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1242 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1243 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1244 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1245 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1246 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1247 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1248 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1249 | Item.uDataAlloc || |
| 1250 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1251 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1252 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1253 | Item.val.int64 != 98) |
| 1254 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1255 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1256 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1257 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1258 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1259 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1260 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1261 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1262 | Item.uDataAlloc || |
| 1263 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1264 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1265 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1266 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1267 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1268 | return 0; |
| 1269 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1270 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1271 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1272 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1273 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1274 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1275 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1276 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1277 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1278 | |
| 1279 | /* Utility to decode a one byte string and match to letter. */ |
| 1280 | static QCBORError |
| 1281 | CheckOneLetterString(QCBORDecodeContext *pDecode, uint8_t letter) |
| 1282 | { |
| 1283 | UsefulBufC Text; |
| 1284 | QCBORError uErr; |
| 1285 | |
| 1286 | QCBORDecode_GetTextString(pDecode, &Text); |
| 1287 | uErr = QCBORDecode_GetError(pDecode); |
| 1288 | if(uErr) { |
| 1289 | return uErr; |
| 1290 | } |
| 1291 | |
| 1292 | if(Text.len != 1) { |
| 1293 | return QCBOR_ERR_FIRST_USER_DEFINED; |
| 1294 | } |
| 1295 | |
| 1296 | if(*(const uint8_t *)Text.ptr != letter) { |
| 1297 | return QCBOR_ERR_FIRST_USER_DEFINED; |
| 1298 | } |
| 1299 | |
| 1300 | return QCBOR_SUCCESS; |
| 1301 | } |
| 1302 | #endif |
| 1303 | |
| 1304 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1305 | /* |
| 1306 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1307 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1308 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1309 | int32_t ParseMapAsArrayTest(void) |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1310 | { |
| 1311 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1312 | QCBORItem Item; |
| 1313 | QCBORError uErr; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1314 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1315 | QCBORDecode_Init(&DCtx, |
| 1316 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1317 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1318 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1319 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1320 | return MakeTestResultCode(1, 1, uErr); |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1321 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1322 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1323 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1324 | return -1; |
| 1325 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1326 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1327 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1328 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1329 | } |
| 1330 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1331 | Item.uDataAlloc || |
| 1332 | Item.uLabelAlloc || |
| 1333 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1334 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1335 | return -2; |
| 1336 | } |
| 1337 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1338 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1339 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1340 | } |
| 1341 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1342 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1343 | Item.val.int64 != 42 || |
| 1344 | Item.uDataAlloc || |
| 1345 | Item.uLabelAlloc) { |
| 1346 | return -3; |
| 1347 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1348 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1349 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1350 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1351 | } |
| 1352 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1353 | Item.uDataAlloc || |
| 1354 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1355 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1356 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1357 | return -4; |
| 1358 | } |
| 1359 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1360 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1361 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1362 | } |
| 1363 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1364 | Item.uDataAlloc || |
| 1365 | Item.uLabelAlloc || |
| 1366 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1367 | Item.val.uCount != 2) { |
| 1368 | return -5; |
| 1369 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1370 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1371 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1372 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1373 | } |
| 1374 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1375 | Item.val.string.len != 7 || |
| 1376 | Item.uDataAlloc || |
| 1377 | Item.uLabelAlloc || |
| 1378 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1379 | return -6; |
| 1380 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1381 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1382 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1383 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1384 | } |
| 1385 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1386 | Item.uDataAlloc || |
| 1387 | Item.uLabelAlloc || |
| 1388 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1389 | return -7; |
| 1390 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1391 | |
| 1392 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1393 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1394 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1395 | } |
| 1396 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1397 | Item.uDataAlloc || |
| 1398 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1399 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1400 | return -8; |
| 1401 | } |
| 1402 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1403 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1404 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1405 | } |
| 1406 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1407 | Item.uDataAlloc || |
| 1408 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1409 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1410 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1411 | return -9; |
| 1412 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1413 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1414 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1415 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1416 | } |
| 1417 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1418 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1419 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1420 | Item.uDataAlloc || |
| 1421 | Item.uLabelAlloc) { |
| 1422 | return -10; |
| 1423 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1424 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1425 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1426 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1427 | } |
| 1428 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1429 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1430 | Item.uDataAlloc || |
| 1431 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1432 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1433 | return -11; |
| 1434 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1435 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1436 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1437 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1438 | } |
| 1439 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1440 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1441 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1442 | Item.uDataAlloc || |
| 1443 | Item.uLabelAlloc) { |
| 1444 | return -12; |
| 1445 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1446 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1447 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1448 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1449 | } |
| 1450 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1451 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1452 | Item.uDataAlloc || |
| 1453 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1454 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1455 | return -13; |
| 1456 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1457 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1458 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1459 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1460 | } |
| 1461 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1462 | Item.uDataAlloc || |
| 1463 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1464 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1465 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1466 | return -14; |
| 1467 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1468 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1469 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1470 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1471 | } |
| 1472 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1473 | Item.uDataAlloc || |
| 1474 | Item.uLabelAlloc || |
| 1475 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1476 | Item.val.int64 != 98) { |
| 1477 | return -15; |
| 1478 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1479 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1480 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1481 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1482 | } |
| 1483 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1484 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1485 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1486 | Item.uDataAlloc || |
| 1487 | Item.uLabelAlloc) { |
| 1488 | return -16; |
| 1489 | } |
| 1490 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1491 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1492 | return (int32_t)uErr; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1493 | } |
| 1494 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1495 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1496 | Item.uDataAlloc || |
| 1497 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1498 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1499 | return -17; |
| 1500 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1501 | |
| 1502 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1503 | /* |
| 1504 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1505 | map that when interpreted as an array will be too many. Test |
| 1506 | data just has the start of the map, not all the items in the map. |
| 1507 | */ |
| 1508 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1509 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1510 | QCBORDecode_Init(&DCtx, |
| 1511 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1512 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1513 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1514 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1515 | return -50; |
| 1516 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1517 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 1518 | /* This test requires indef strings, HW float and preferred float,... */ |
| 1519 | #if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \ |
| 1520 | !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \ |
| 1521 | !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \ |
| 1522 | !defined(QCBOR_DISABLE_TAGS) && \ |
| 1523 | !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS) |
| 1524 | |
| 1525 | UsefulBufC Encoded; |
| 1526 | |
| 1527 | /* Big decode of a map with a wide variety or labels */ |
| 1528 | QCBORDecode_Init(&DCtx, |
| 1529 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels), |
| 1530 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 1531 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1532 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1533 | |
| 1534 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1535 | if(uErr) { |
| 1536 | return MakeTestResultCode(10, 1, uErr); |
| 1537 | } |
| 1538 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1539 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) { |
| 1540 | return MakeTestResultCode(10, 2, 0); |
| 1541 | } |
| 1542 | |
| 1543 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1544 | if(uErr) { |
| 1545 | return MakeTestResultCode(10, 3, uErr); |
| 1546 | } |
| 1547 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1548 | Item.uDataType != QCBOR_TYPE_TRUE) { |
| 1549 | return MakeTestResultCode(10, 4, 0); |
| 1550 | } |
| 1551 | |
| 1552 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1553 | if(uErr) { |
| 1554 | return MakeTestResultCode(10, 5, uErr); |
| 1555 | } |
| 1556 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1557 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1558 | ((const char *)Item.val.string.ptr)[0] != 'a') { |
| 1559 | return MakeTestResultCode(10, 6, 0); |
| 1560 | } |
| 1561 | |
| 1562 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1563 | if(uErr) { |
| 1564 | return MakeTestResultCode(10, 7, uErr); |
| 1565 | } |
| 1566 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1567 | Item.uDataType != QCBOR_TYPE_NULL) { |
| 1568 | return MakeTestResultCode(10, 8, 0); |
| 1569 | } |
| 1570 | |
| 1571 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1572 | if(uErr) { |
| 1573 | return MakeTestResultCode(10, 9, uErr); |
| 1574 | } |
| 1575 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1576 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1577 | ((const char *)Item.val.string.ptr)[0] != 'b') { |
| 1578 | return MakeTestResultCode(10, 10, 0); |
| 1579 | } |
| 1580 | |
| 1581 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1582 | if(uErr) { |
| 1583 | return MakeTestResultCode(10, 11, uErr); |
| 1584 | } |
| 1585 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1586 | Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || |
| 1587 | Item.val.int64 != 255) { |
| 1588 | return MakeTestResultCode(10, 12, 0); |
| 1589 | } |
| 1590 | |
| 1591 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1592 | if(uErr) { |
| 1593 | return MakeTestResultCode(10, 13, uErr); |
| 1594 | } |
| 1595 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1596 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1597 | ((const char *)Item.val.string.ptr)[0] != 'c') { |
| 1598 | return MakeTestResultCode(10, 14, 0); |
| 1599 | } |
| 1600 | |
| 1601 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1602 | if(uErr) { |
| 1603 | return MakeTestResultCode(10, 15, uErr); |
| 1604 | } |
| 1605 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1606 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1607 | !isnan(Item.val.dfnum)) { |
| 1608 | return MakeTestResultCode(10, 16, 0); |
| 1609 | } |
| 1610 | |
| 1611 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1612 | if(uErr) { |
| 1613 | return MakeTestResultCode(10, 17, uErr); |
| 1614 | } |
| 1615 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1616 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1617 | ((const char *)Item.val.string.ptr)[0] != 'd') { |
| 1618 | return MakeTestResultCode(10, 18, 0); |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1623 | if(uErr) { |
| 1624 | return MakeTestResultCode(10, 19, uErr); |
| 1625 | } |
| 1626 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1627 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1628 | Item.val.dfnum != 3.4028234663852886E+38) { |
| 1629 | return MakeTestResultCode(10, 20, 0); |
| 1630 | } |
| 1631 | |
| 1632 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1633 | if(uErr) { |
| 1634 | return MakeTestResultCode(10, 21, uErr); |
| 1635 | } |
| 1636 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1637 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1638 | ((const char *)Item.val.string.ptr)[0] != 'e') { |
| 1639 | return MakeTestResultCode(10, 22, 0); |
| 1640 | } |
| 1641 | |
| 1642 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1643 | if(uErr) { |
| 1644 | return MakeTestResultCode(10, 23, uErr); |
| 1645 | } |
| 1646 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1647 | Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 1648 | Item.val.dfnum != -INFINITY) { |
| 1649 | return MakeTestResultCode(10, 24, 0); |
| 1650 | } |
| 1651 | |
| 1652 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1653 | if(uErr) { |
| 1654 | return MakeTestResultCode(10, 25, uErr); |
| 1655 | } |
| 1656 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1657 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1658 | ((const char *)Item.val.string.ptr)[0] != 'f') { |
| 1659 | return MakeTestResultCode(10, 26, 0); |
| 1660 | } |
| 1661 | |
| 1662 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1663 | if(uErr) { |
| 1664 | return MakeTestResultCode(10, 26, uErr); |
| 1665 | } |
| 1666 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1667 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1668 | Item.val.uCount != 2) { |
| 1669 | return MakeTestResultCode(10, 27, 0); |
| 1670 | } |
| 1671 | |
| 1672 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1673 | if(uErr) { |
| 1674 | return MakeTestResultCode(10, 28, uErr); |
| 1675 | } |
| 1676 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1677 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1678 | Item.val.int64 != 1000) { |
| 1679 | return MakeTestResultCode(10, 29, 0); |
| 1680 | } |
| 1681 | |
| 1682 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1683 | if(uErr) { |
| 1684 | return MakeTestResultCode(10, 30, uErr); |
| 1685 | } |
| 1686 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1687 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1688 | Item.val.int64 != 16) { |
| 1689 | return MakeTestResultCode(10, 31, 0); |
| 1690 | } |
| 1691 | |
| 1692 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1693 | if(uErr) { |
| 1694 | return MakeTestResultCode(10, 32, uErr); |
| 1695 | } |
| 1696 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1697 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1698 | ((const char *)Item.val.string.ptr)[0] != 'g') { |
| 1699 | return MakeTestResultCode(10, 33, 0); |
| 1700 | } |
| 1701 | |
| 1702 | for(int i = 0 ; i < 4; i++) { |
| 1703 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1704 | if(uErr) { |
| 1705 | return MakeTestResultCode(10, 34, uErr); |
| 1706 | } |
| 1707 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1708 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 1709 | return MakeTestResultCode(10, 35, 0); |
| 1710 | } |
| 1711 | if(i != 3) { |
| 1712 | if(Item.val.uCount != 1) { |
| 1713 | return MakeTestResultCode(10, 35, 0); |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1718 | if(uErr) { |
| 1719 | return MakeTestResultCode(10, 36, uErr); |
| 1720 | } |
| 1721 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1722 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1723 | ((const char *)Item.val.string.ptr)[0] != 'h') { |
| 1724 | return MakeTestResultCode(10, 37, 0); |
| 1725 | } |
| 1726 | |
| 1727 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1728 | if(uErr) { |
| 1729 | return MakeTestResultCode(10, 38, uErr); |
| 1730 | } |
| 1731 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1732 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 1733 | return MakeTestResultCode(10, 39, 0); |
| 1734 | } |
| 1735 | |
| 1736 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1737 | if(uErr) { |
| 1738 | return MakeTestResultCode(10, 40, uErr); |
| 1739 | } |
| 1740 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1741 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1742 | ((const char *)Item.val.string.ptr)[0] != 'i') { |
| 1743 | return MakeTestResultCode(10, 41, 0); |
| 1744 | } |
| 1745 | |
| 1746 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1747 | if(uErr) { |
| 1748 | return MakeTestResultCode(10, 42, uErr); |
| 1749 | } |
| 1750 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1751 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1752 | Item.val.uCount != 2) { |
| 1753 | return MakeTestResultCode(10, 31, 0); |
| 1754 | } |
| 1755 | |
| 1756 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1757 | if(uErr) { |
| 1758 | return MakeTestResultCode(10, 43, uErr); |
| 1759 | } |
| 1760 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1761 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1762 | return MakeTestResultCode(10, 31, 0); |
| 1763 | } |
| 1764 | |
| 1765 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1766 | if(uErr) { |
| 1767 | return MakeTestResultCode(10, 44, uErr); |
| 1768 | } |
| 1769 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1770 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1771 | Item.val.uCount != 4) { |
| 1772 | return MakeTestResultCode(10, 45, 0); |
| 1773 | } |
| 1774 | |
| 1775 | for(int i = 0 ; i < 4; i++) { |
| 1776 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1777 | if(uErr) { |
| 1778 | return MakeTestResultCode(10, 46, uErr); |
| 1779 | } |
| 1780 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1781 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1782 | return MakeTestResultCode(10, 47, 0); |
| 1783 | } |
| 1784 | } |
| 1785 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1786 | if(uErr) { |
| 1787 | return MakeTestResultCode(10, 48, uErr); |
| 1788 | } |
| 1789 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1790 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1791 | ((const char *)Item.val.string.ptr)[0] != 'j') { |
| 1792 | return MakeTestResultCode(10, 49, 0); |
| 1793 | } |
| 1794 | |
| 1795 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1796 | if(uErr) { |
| 1797 | return MakeTestResultCode(10, 50, uErr); |
| 1798 | } |
| 1799 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1800 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1801 | Item.val.uCount != UINT16_MAX) { |
| 1802 | return MakeTestResultCode(10, 51, 0); |
| 1803 | } |
| 1804 | |
| 1805 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1806 | if(uErr) { |
| 1807 | return MakeTestResultCode(10, 52, uErr); |
| 1808 | } |
| 1809 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1810 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1811 | ((const char *)Item.val.string.ptr)[0] != 'k') { |
| 1812 | return MakeTestResultCode(10, 53, 0); |
| 1813 | } |
| 1814 | |
| 1815 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1816 | if(uErr) { |
| 1817 | return MakeTestResultCode(10, 54, uErr); |
| 1818 | } |
| 1819 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1820 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1821 | Item.val.uCount != UINT16_MAX) { |
| 1822 | return MakeTestResultCode(10, 55, 0); |
| 1823 | } |
| 1824 | |
| 1825 | for(int i = 0 ; i < 3; i++) { |
| 1826 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1827 | if(uErr) { |
| 1828 | return MakeTestResultCode(10, 56, uErr); |
| 1829 | } |
| 1830 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1831 | Item.uDataType != QCBOR_TYPE_INT64) { |
| 1832 | return MakeTestResultCode(10, 57, 0); |
| 1833 | } |
| 1834 | } |
| 1835 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1836 | if(uErr) { |
| 1837 | return MakeTestResultCode(10, 58, uErr); |
| 1838 | } |
| 1839 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1840 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1841 | ((const char *)Item.val.string.ptr)[0] != 'l') { |
| 1842 | return MakeTestResultCode(10, 59, 0); |
| 1843 | } |
| 1844 | |
| 1845 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1846 | if(uErr) { |
| 1847 | return MakeTestResultCode(10, 60, uErr); |
| 1848 | } |
| 1849 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1850 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1851 | Item.val.string.len != 4) { |
| 1852 | return MakeTestResultCode(10, 61, 0); |
| 1853 | } |
| 1854 | |
| 1855 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1856 | if(uErr) { |
| 1857 | return MakeTestResultCode(10, 62, uErr); |
| 1858 | } |
| 1859 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1860 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1861 | ((const char *)Item.val.string.ptr)[0] != 'm') { |
| 1862 | return MakeTestResultCode(10, 63, 0); |
| 1863 | } |
| 1864 | |
| 1865 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1866 | if(uErr) { |
| 1867 | return MakeTestResultCode(10, 64, uErr); |
| 1868 | } |
| 1869 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1870 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1871 | !QCBORDecode_IsTagged(&DCtx, &Item, 258) || |
| 1872 | Item.val.uCount != UINT16_MAX) { |
| 1873 | return MakeTestResultCode(10, 65, 0); |
| 1874 | } |
| 1875 | |
| 1876 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1877 | if(uErr) { |
| 1878 | return MakeTestResultCode(10, 66, uErr); |
| 1879 | } |
| 1880 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1881 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1882 | Item.val.string.len != 2) { |
| 1883 | return MakeTestResultCode(10, 67, 0); |
| 1884 | } |
| 1885 | |
| 1886 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1887 | if(uErr) { |
| 1888 | return MakeTestResultCode(10, 68, uErr); |
| 1889 | } |
| 1890 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1891 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1892 | Item.val.int64 != 0) { |
| 1893 | return MakeTestResultCode(10, 69, 0); |
| 1894 | } |
| 1895 | |
| 1896 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1897 | if(uErr) { |
| 1898 | return MakeTestResultCode(10, 70, uErr); |
| 1899 | } |
| 1900 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1901 | Item.uDataType != QCBOR_TYPE_FALSE) { |
| 1902 | return MakeTestResultCode(10, 71, 0); |
| 1903 | } |
| 1904 | |
| 1905 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1906 | if(uErr) { |
| 1907 | return MakeTestResultCode(10, 72, uErr); |
| 1908 | } |
| 1909 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1910 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1911 | !QCBORDecode_IsTagged(&DCtx, &Item, 23) || |
| 1912 | Item.val.uCount != 0) { |
| 1913 | return MakeTestResultCode(10, 73, 0); |
| 1914 | } |
| 1915 | |
| 1916 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 1917 | if(uErr) { |
| 1918 | return MakeTestResultCode(10, 74, uErr); |
| 1919 | } |
| 1920 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1921 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1922 | ((const char *)Item.val.string.ptr)[0] != 'n') { |
| 1923 | return MakeTestResultCode(10, 75, 0); |
| 1924 | } |
| 1925 | |
| 1926 | |
| 1927 | /* Big decode of a map with a wide variety or labels */ |
| 1928 | QCBORDecode_Init(&DCtx, |
| 1929 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels), |
| 1930 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 1931 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1932 | |
| 1933 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1934 | bool b; |
| 1935 | QCBORDecode_GetBool(&DCtx, &b); |
| 1936 | |
| 1937 | uErr = CheckOneLetterString(&DCtx, 'a'); |
| 1938 | if(uErr) { |
| 1939 | return MakeTestResultCode(11, 1, uErr); |
| 1940 | } |
| 1941 | |
| 1942 | QCBORDecode_GetNull(&DCtx); |
| 1943 | uErr = CheckOneLetterString(&DCtx, 'b'); |
| 1944 | if(uErr) { |
| 1945 | return MakeTestResultCode(11, 2, uErr); |
| 1946 | } |
| 1947 | |
| 1948 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 1949 | uErr = CheckOneLetterString(&DCtx, 'c'); |
| 1950 | if(uErr) { |
| 1951 | return MakeTestResultCode(11, 3, uErr); |
| 1952 | } |
| 1953 | |
| 1954 | double dNum; |
| 1955 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1956 | if(!isnan(dNum)) { |
| 1957 | return MakeTestResultCode(11, 4, 0); |
| 1958 | } |
| 1959 | uErr = CheckOneLetterString(&DCtx, 'd'); |
| 1960 | if(uErr) { |
| 1961 | return MakeTestResultCode(11, 5, uErr); |
| 1962 | } |
| 1963 | |
| 1964 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1965 | if( dNum != 3.4028234663852886E+38 ) { |
| 1966 | return MakeTestResultCode(11, 6, 0); |
| 1967 | } |
| 1968 | uErr = CheckOneLetterString(&DCtx, 'e'); |
| 1969 | if(uErr) { |
| 1970 | return MakeTestResultCode(11, 7, uErr); |
| 1971 | } |
| 1972 | |
| 1973 | QCBORDecode_GetDouble(&DCtx, &dNum); |
| 1974 | if(dNum != -INFINITY) { |
| 1975 | return MakeTestResultCode(11, 8, 0); |
| 1976 | } |
| 1977 | uErr = CheckOneLetterString(&DCtx, 'f'); |
| 1978 | if(uErr) { |
| 1979 | return MakeTestResultCode(11, 9, uErr); |
| 1980 | } |
| 1981 | |
| 1982 | int64_t nInt; |
| 1983 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1984 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 1985 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 1986 | QCBORDecode_ExitArray(&DCtx); |
| 1987 | uErr = CheckOneLetterString(&DCtx, 'g'); |
| 1988 | if(uErr) { |
| 1989 | return MakeTestResultCode(11, 10, uErr); |
| 1990 | } |
| 1991 | |
| 1992 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1993 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1994 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1995 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 1996 | QCBORDecode_ExitArray(&DCtx); |
| 1997 | QCBORDecode_ExitArray(&DCtx); |
| 1998 | QCBORDecode_ExitArray(&DCtx); |
| 1999 | QCBORDecode_ExitArray(&DCtx); |
| 2000 | uErr = CheckOneLetterString(&DCtx, 'h'); |
| 2001 | if(uErr) { |
| 2002 | return MakeTestResultCode(11, 11, uErr); |
| 2003 | } |
| 2004 | QCBORDecode_GetEpochDate(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 2005 | uErr = CheckOneLetterString(&DCtx, 'i'); |
| 2006 | if(uErr) { |
| 2007 | return MakeTestResultCode(11, 12, uErr); |
| 2008 | } |
| 2009 | |
| 2010 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2011 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2012 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2013 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2014 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2015 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2016 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2017 | QCBORDecode_ExitArray(&DCtx); |
| 2018 | QCBORDecode_ExitArray(&DCtx); |
| 2019 | uErr = CheckOneLetterString(&DCtx, 'j'); |
| 2020 | if(uErr) { |
| 2021 | return MakeTestResultCode(11, 13, uErr); |
| 2022 | } |
| 2023 | |
| 2024 | QCBORDecode_GetArray(&DCtx, &Item, &Encoded); |
| 2025 | uErr = CheckOneLetterString(&DCtx, 'k'); |
| 2026 | if(uErr) { |
| 2027 | return MakeTestResultCode(11, 14, uErr); |
| 2028 | } |
| 2029 | |
| 2030 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2031 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2032 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2033 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2034 | QCBORDecode_ExitArray(&DCtx); |
| 2035 | uErr = CheckOneLetterString(&DCtx, 'l'); |
| 2036 | if(uErr) { |
| 2037 | return MakeTestResultCode(11, 15, uErr); |
| 2038 | } |
| 2039 | |
| 2040 | QCBORDecode_GetTextString(&DCtx, &Encoded); |
| 2041 | uErr = CheckOneLetterString(&DCtx, 'm'); |
| 2042 | if(uErr) { |
| 2043 | return MakeTestResultCode(11, 16, uErr); |
| 2044 | } |
| 2045 | |
| 2046 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2047 | if(!QCBORDecode_IsTagged(&DCtx, &Item, 258)) { |
| 2048 | return MakeTestResultCode(11, 17, 0); |
| 2049 | } |
| 2050 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) { |
| 2051 | return MakeTestResultCode(11, 18, 0); |
| 2052 | } |
| 2053 | if(Item.val.uCount != UINT16_MAX) { |
| 2054 | return MakeTestResultCode(11, 19, 0); |
| 2055 | } |
| 2056 | QCBORDecode_GetTextString(&DCtx, &Encoded); |
| 2057 | if(Encoded.len != 2) { |
| 2058 | return MakeTestResultCode(11, 20, 0); |
| 2059 | } QCBORDecode_GetInt64(&DCtx, &nInt); |
| 2060 | QCBORDecode_GetBool(&DCtx, &b); |
| 2061 | if(b != false) { |
| 2062 | return MakeTestResultCode(11, 21, 0); |
| 2063 | } |
| 2064 | QCBORDecode_EnterArray(&DCtx, &Item); |
| 2065 | if(!QCBORDecode_IsTagged(&DCtx, &Item, 23)) { |
| 2066 | return MakeTestResultCode(11, 22, 0); |
| 2067 | } |
| 2068 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2069 | return MakeTestResultCode(11, 23, 0); |
| 2070 | } |
| 2071 | if(Item.val.uCount != 0) { |
| 2072 | return MakeTestResultCode(11, 24, 0); |
| 2073 | } |
| 2074 | QCBORDecode_ExitArray(&DCtx); |
| 2075 | QCBORDecode_ExitArray(&DCtx); |
| 2076 | uErr = CheckOneLetterString(&DCtx, 'n'); |
| 2077 | if(uErr) { |
| 2078 | return MakeTestResultCode(11, 25, uErr); |
| 2079 | } |
| 2080 | |
| 2081 | QCBORDecode_ExitArray(&DCtx); |
| 2082 | uErr = QCBORDecode_Finish(&DCtx); |
| 2083 | if(uErr) { |
| 2084 | return MakeTestResultCode(11, 26, uErr); |
| 2085 | } |
| 2086 | #endif /* QCBOR_DISABLE_... */ |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 2087 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2088 | return 0; |
| 2089 | } |
| 2090 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2091 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 2092 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2093 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 2094 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2095 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2096 | Fully or partially decode pValidMapEncoded. When |
| 2097 | partially decoding check for the right error code. |
| 2098 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2099 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2100 | The partial decodes test error conditions of |
| 2101 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2102 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2103 | This could be combined with the above test |
| 2104 | and made prettier and maybe a little more |
| 2105 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2106 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2107 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2108 | { |
| 2109 | QCBORDecodeContext DCtx; |
| 2110 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2111 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2112 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2113 | QCBORDecode_Init(&DCtx, |
| 2114 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 2115 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2116 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2117 | if(nLevel < 1) { |
| 2118 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 2119 | return -1; |
| 2120 | } else { |
| 2121 | return 0; |
| 2122 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2123 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2124 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2125 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2126 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2127 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2128 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2129 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2130 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2131 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2132 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2133 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2134 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2135 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2136 | } else { |
| 2137 | return 0; |
| 2138 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2139 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2140 | |
| 2141 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2142 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2143 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2144 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2145 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2146 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 2147 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2148 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2149 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2150 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2151 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2152 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2153 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2154 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2155 | } else { |
| 2156 | return 0; |
| 2157 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2158 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2159 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2160 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2161 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2162 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2163 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2164 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2165 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2166 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2167 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2168 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2169 | |
| 2170 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2171 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2172 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2173 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2174 | } else { |
| 2175 | return 0; |
| 2176 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2177 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2178 | |
| 2179 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2180 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2181 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2182 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2183 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2184 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2185 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2186 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2187 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2188 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2189 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2190 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2191 | } else { |
| 2192 | return 0; |
| 2193 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2194 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2195 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2196 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2197 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2198 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2199 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2200 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2201 | return -10; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2202 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2203 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2204 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2205 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2206 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2207 | } else { |
| 2208 | return 0; |
| 2209 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2210 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2211 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2212 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2213 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2214 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2215 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2216 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2217 | Item.uDataType != QCBOR_TYPE_MAP || |
| 2218 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2219 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2220 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2221 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2222 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2223 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2224 | } else { |
| 2225 | return 0; |
| 2226 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2227 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2228 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2229 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2230 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2231 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2232 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2233 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2234 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2235 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2236 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2237 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2238 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2239 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2240 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2241 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2242 | } else { |
| 2243 | return 0; |
| 2244 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2245 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2246 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2247 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2248 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2249 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2250 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2251 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2252 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2253 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2254 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2255 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2256 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2257 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2258 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2259 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2260 | } else { |
| 2261 | return 0; |
| 2262 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2263 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2264 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2265 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2266 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2267 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2268 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2269 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2270 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 2271 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2272 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2273 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2274 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2275 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 2276 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2277 | } else { |
| 2278 | return 0; |
| 2279 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2280 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2281 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2282 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2283 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2284 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2285 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2286 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2287 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2288 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2289 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2291 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 2292 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2293 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2294 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2295 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | |
| 2299 | |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame^] | 2300 | /* These are just the item that open large maps and arrays, not |
| 2301 | * the items in the array. This is sufficient to test the |
| 2302 | * boundary condition. */ |
| 2303 | static const uint8_t spLargeArrayFake[] = { |
| 2304 | 0x99, 0xff, 0xfe}; |
| 2305 | |
| 2306 | static const uint8_t spTooLargeArrayFake[] = { |
| 2307 | 0x99, 0xff, 0xff}; |
| 2308 | |
| 2309 | static const uint8_t spLargeMapFake[] = { |
| 2310 | 0xb9, 0x7f, 0xff}; |
| 2311 | |
| 2312 | static const uint8_t spTooLargeMapFake[] = { |
| 2313 | 0xba, 0x00, 0x00, 0x80, 0x00}; |
| 2314 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2315 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2316 | int32_t ParseMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2317 | { |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame^] | 2318 | QCBORDecodeContext DCtx; |
| 2319 | QCBORItem Item; |
| 2320 | QCBORError uErr; |
| 2321 | |
| 2322 | QCBORDecode_Init(&DCtx, |
| 2323 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLargeArrayFake), |
| 2324 | QCBOR_DECODE_MODE_NORMAL); |
| 2325 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 2326 | if(uErr != QCBOR_SUCCESS || Item.val.uCount != QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 2327 | return -100; |
| 2328 | } |
| 2329 | |
| 2330 | QCBORDecode_Init(&DCtx, |
| 2331 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLargeArrayFake), |
| 2332 | QCBOR_DECODE_MODE_NORMAL); |
| 2333 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 2334 | if(uErr != QCBOR_ERR_ARRAY_DECODE_TOO_LONG) { |
| 2335 | return -101; |
| 2336 | } |
| 2337 | |
| 2338 | QCBORDecode_Init(&DCtx, |
| 2339 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLargeMapFake), |
| 2340 | QCBOR_DECODE_MODE_NORMAL); |
| 2341 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 2342 | if(uErr != QCBOR_SUCCESS || Item.val.uCount != QCBOR_MAX_ITEMS_IN_MAP) { |
| 2343 | return -110; |
| 2344 | } |
| 2345 | |
| 2346 | QCBORDecode_Init(&DCtx, |
| 2347 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLargeMapFake), |
| 2348 | QCBOR_DECODE_MODE_NORMAL); |
| 2349 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 2350 | if(uErr != QCBOR_ERR_ARRAY_DECODE_TOO_LONG) { |
| 2351 | return -111; |
| 2352 | } |
| 2353 | |
| 2354 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2355 | // Parse a moderatly complex map structure very thoroughly |
| 2356 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 2357 | if(nResult) { |
| 2358 | return nResult; |
| 2359 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2360 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2361 | // Again, but in strings-only mode. It should succeed since the input |
| 2362 | // map has only string labels. |
| 2363 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 2364 | if(nResult) { |
| 2365 | return nResult; |
| 2366 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2367 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2368 | // Again, but try to finish the decoding before the end of the |
| 2369 | // input at 10 different place and see that the right error code |
| 2370 | // is returned. |
| 2371 | for(int i = 0; i < 10; i++) { |
| 2372 | nResult = ExtraBytesTest(i); |
| 2373 | if(nResult) { |
| 2374 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2375 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2376 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2377 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2378 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2379 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2380 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2381 | |
| 2382 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2383 | /* The simple-values including some not well formed */ |
| 2384 | static const uint8_t spSimpleValues[] = { |
| 2385 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 2386 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 2387 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2388 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2389 | /* A map of good simple values, plus one well-formed integer */ |
| 2390 | static const uint8_t spGoodSimpleValues[] = { |
| 2391 | 0xa9, 0x01, 0xf4, 0x02, 0xf5, 0x03, 0xf6, 0x04, 0xf7, |
| 2392 | 0x05, 0xe0, 0x06, 0xf3, 0x07, 0xf8, 0x20, 0x61, 0x40, |
| 2393 | 0xf8, 0xff, 0x0f, 0x0f}; |
| 2394 | |
| 2395 | int32_t SimpleValueDecodeTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2396 | { |
| 2397 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2398 | QCBORItem Item; |
| 2399 | QCBORError uErr; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2400 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2401 | QCBORDecode_Init(&DCtx, |
| 2402 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2403 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2404 | |
| 2405 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2406 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2407 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2408 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2409 | Item.val.uCount != 10) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2410 | return 1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2411 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2412 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2413 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2414 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2415 | return 2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2416 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2417 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2418 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2419 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2420 | return 3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2421 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2422 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2423 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2424 | if(Item.uDataType != QCBOR_TYPE_NULL) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2425 | return 4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2426 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2427 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2428 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2429 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2430 | return 5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2431 | |
| 2432 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 2433 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2434 | return 6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2435 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2436 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2437 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2438 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2439 | return 7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2440 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2441 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2442 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2443 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2444 | return 8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2445 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2446 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2447 | return 9; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2448 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2449 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2450 | return 10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2451 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2452 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2453 | return 11; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2454 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2455 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2456 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2457 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2458 | return 12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2459 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2460 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2461 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2462 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2463 | return 13; |
| 2464 | |
| 2465 | |
| 2466 | QCBORDecode_Init(&DCtx, |
| 2467 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues), |
| 2468 | QCBOR_DECODE_MODE_NORMAL); |
| 2469 | |
| 2470 | uint8_t uSimple; |
| 2471 | |
| 2472 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 2473 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2474 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) { |
| 2475 | return 20; |
| 2476 | } |
| 2477 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2478 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) { |
| 2479 | return 21; |
| 2480 | } |
| 2481 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2482 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) { |
| 2483 | return 22; |
| 2484 | } |
| 2485 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2486 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) { |
| 2487 | return 23; |
| 2488 | } |
| 2489 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2490 | if(QCBORDecode_GetError(&DCtx) || uSimple != 0) { |
| 2491 | return 24; |
| 2492 | } |
| 2493 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2494 | if(QCBORDecode_GetError(&DCtx) || uSimple != 19) { |
| 2495 | return 25; |
| 2496 | } |
| 2497 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2498 | if(QCBORDecode_GetError(&DCtx) || uSimple != 32) { |
| 2499 | return 26; |
| 2500 | } |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2501 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2502 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2503 | if(QCBORDecode_GetError(&DCtx) || uSimple != 255) { |
| 2504 | return 27; |
| 2505 | } |
| 2506 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 2507 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2508 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2509 | return 28; |
| 2510 | } |
| 2511 | |
| 2512 | QCBORDecode_Rewind(&DCtx); |
| 2513 | |
| 2514 | QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple); |
| 2515 | if(QCBORDecode_GetError(&DCtx) || uSimple != 19) { |
| 2516 | return 30; |
| 2517 | } |
| 2518 | |
| 2519 | QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple); |
| 2520 | if(QCBORDecode_GetError(&DCtx) || uSimple != 255) { |
| 2521 | return 31; |
| 2522 | } |
| 2523 | |
| 2524 | QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple); |
| 2525 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 2526 | return 32; |
| 2527 | } |
| 2528 | |
| 2529 | QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple); |
| 2530 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 2531 | return 33; |
| 2532 | } |
| 2533 | |
| 2534 | QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple); |
| 2535 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 2536 | return 34; |
| 2537 | } |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2538 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2539 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2540 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2544 | int32_t NotWellFormedTests(void) |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2545 | { |
| 2546 | // Loop over all the not-well-formed instance of CBOR |
| 2547 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2548 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 2549 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2550 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 2551 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 2552 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 2553 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2554 | // Set up decoder context. String allocator needed for indefinite |
| 2555 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2556 | QCBORDecodeContext DCtx; |
| 2557 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2558 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2559 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 2560 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2561 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2562 | |
| 2563 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2564 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2565 | do { |
| 2566 | QCBORItem Item; |
| 2567 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2568 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2569 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2570 | |
| 2571 | // Every test vector must fail with |
| 2572 | // a not-well-formed error. If not |
| 2573 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2574 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2575 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2576 | /* Return index of failure and QCBOR error in the result */ |
| 2577 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | return 0; |
| 2581 | } |
| 2582 | |
| 2583 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2584 | struct DecodeFailTestInput { |
| 2585 | const char *szDescription; /* Description of the test */ |
| 2586 | QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */ |
| 2587 | UsefulBufC Input; /* Chunk of CBOR that cases error */ |
| 2588 | QCBORError nError; /* The expected error */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2589 | }; |
| 2590 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2591 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2592 | static int32_t |
| 2593 | ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2594 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2595 | int nIndex; |
| 2596 | QCBORDecodeContext DCtx; |
| 2597 | QCBORError uCBORError; |
| 2598 | QCBORItem Item; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2599 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2600 | for(nIndex = 0; nIndex < nNumFails; nIndex++) { |
| 2601 | const struct DecodeFailTestInput *pF = &pFailInputs[nIndex]; |
| 2602 | |
| 2603 | QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2604 | |
| 2605 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2606 | /* Set up the decoding context including a memory pool so that |
| 2607 | * indefinite length items can be checked. |
| 2608 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2609 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2610 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2611 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2612 | if(uCBORError != QCBOR_SUCCESS) { |
| 2613 | return -1; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2614 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2615 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2616 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2617 | if(nIndex == 4) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2618 | uCBORError = 9; /* For setting break points */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2621 | /* Iterate until there is an error of some sort */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2622 | do { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2623 | /* Set to something non-zero, something other than QCBOR_TYPE_NONE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2624 | memset(&Item, 0x33, sizeof(Item)); |
| 2625 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2626 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2627 | } while(uCBORError == QCBOR_SUCCESS); |
| 2628 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2629 | /* Must get the expected error or the this test fails. |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2630 | * The data and label type must also be QCBOR_TYPE_NONE. |
| 2631 | */ |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2632 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2633 | Item.uDataType != QCBOR_TYPE_NONE || |
| 2634 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2635 | return (int32_t)(nIndex * 1000 + (int)uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | return 0; |
| 2640 | } |
| 2641 | |
| 2642 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2643 | static const struct DecodeFailTestInput Failures[] = { |
| 2644 | /* Most of this is copied from not_well_formed.h. Here the error |
| 2645 | * code returned is also checked. |
| 2646 | */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2647 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2648 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2649 | /* Indefinite length strings must be closed off */ |
| 2650 | { "An indefinite length byte string not closed off", |
| 2651 | QCBOR_DECODE_MODE_NORMAL, |
| 2652 | {"0x5f\x41\x00", 3}, |
| 2653 | QCBOR_ERR_HIT_END |
| 2654 | }, |
| 2655 | { "An indefinite length text string not closed off", |
| 2656 | QCBOR_DECODE_MODE_NORMAL, |
| 2657 | {"\x7f\x61\x00", 3}, |
| 2658 | QCBOR_ERR_HIT_END |
| 2659 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2660 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2661 | /* All the chunks in an indefinite length string must be of the |
| 2662 | * type of indefinite length string |
| 2663 | */ |
| 2664 | { "Indefinite length byte string with text string chunk", |
| 2665 | QCBOR_DECODE_MODE_NORMAL, |
| 2666 | {"\x5f\x61\x00\xff", 4}, |
| 2667 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2668 | }, |
| 2669 | { "Indefinite length text string with a byte string chunk", |
| 2670 | QCBOR_DECODE_MODE_NORMAL, |
| 2671 | {"\x7f\x41\x00\xff", 4}, |
| 2672 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2673 | }, |
| 2674 | { "Indefinite length byte string with a positive integer chunk", |
| 2675 | QCBOR_DECODE_MODE_NORMAL, |
| 2676 | {"\x5f\x00\xff", 3}, |
| 2677 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2678 | }, |
| 2679 | { "Indefinite length byte string with an negative integer chunk", |
| 2680 | QCBOR_DECODE_MODE_NORMAL, |
| 2681 | {"\x5f\x21\xff", 3}, |
| 2682 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2683 | }, |
| 2684 | { "Indefinite length byte string with an array chunk", |
| 2685 | QCBOR_DECODE_MODE_NORMAL, |
| 2686 | {"\x5f\x80\xff", 3}, |
| 2687 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2688 | }, |
| 2689 | { "Indefinite length byte string with an map chunk", |
| 2690 | QCBOR_DECODE_MODE_NORMAL, |
| 2691 | {"\x5f\xa0\xff", 3}, |
| 2692 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2693 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2694 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2695 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2696 | { "Indefinite length byte string with tagged integer chunk", |
| 2697 | QCBOR_DECODE_MODE_NORMAL, |
| 2698 | {"\x5f\xc0\x00\xff", 4}, |
| 2699 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2700 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2701 | #else |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2702 | { "Indefinite length byte string with tagged integer chunk", |
| 2703 | QCBOR_DECODE_MODE_NORMAL, |
| 2704 | {"\x5f\xc0\x00\xff", 4}, |
| 2705 | QCBOR_ERR_TAGS_DISABLED |
| 2706 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2707 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2708 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2709 | { "Indefinite length byte string with an simple type chunk", |
| 2710 | QCBOR_DECODE_MODE_NORMAL, |
| 2711 | {"\x5f\xe0\xff", 3}, |
| 2712 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2713 | }, |
| 2714 | { "???", |
| 2715 | QCBOR_DECODE_MODE_NORMAL, |
| 2716 | {"\x5f\x5f\x41\x00\xff\xff", 6}, |
| 2717 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2718 | }, |
| 2719 | { "indefinite length text string with indefinite string inside", |
| 2720 | QCBOR_DECODE_MODE_NORMAL, |
| 2721 | {"\x7f\x7f\x61\x00\xff\xff", 6}, |
| 2722 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2723 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2724 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2725 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2726 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2727 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2728 | /* Definte length maps and arrays must be closed by having the right number of items */ |
| 2729 | { "A definte length array that is supposed to have 1 item, but has none", |
| 2730 | QCBOR_DECODE_MODE_NORMAL, |
| 2731 | {"\x81", 1}, |
| 2732 | QCBOR_ERR_NO_MORE_ITEMS |
| 2733 | }, |
| 2734 | { "A definte length array that is supposed to have 2 items, but has only 1", |
| 2735 | QCBOR_DECODE_MODE_NORMAL, |
| 2736 | {"\x82\x00", 2}, |
| 2737 | QCBOR_ERR_NO_MORE_ITEMS |
| 2738 | }, |
| 2739 | { "A definte length array that is supposed to have 511 items, but has only 1", |
| 2740 | QCBOR_DECODE_MODE_NORMAL, |
| 2741 | {"\x9a\x01\xff\x00", 4}, |
| 2742 | QCBOR_ERR_HIT_END |
| 2743 | }, |
| 2744 | { "A definte length map that is supposed to have 1 item, but has none", |
| 2745 | QCBOR_DECODE_MODE_NORMAL, |
| 2746 | {"\xa1", 1}, |
| 2747 | QCBOR_ERR_NO_MORE_ITEMS |
| 2748 | }, |
| 2749 | { "A definte length map that is supposed to have s item, but has only 1", |
| 2750 | QCBOR_DECODE_MODE_NORMAL, |
| 2751 | {"\xa2\x01\x02", 3}, |
| 2752 | QCBOR_ERR_NO_MORE_ITEMS |
| 2753 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2754 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2755 | /* Indefinte length maps and arrays must be ended by a break */ |
| 2756 | { "Indefinite length array with zero items and no break", |
| 2757 | QCBOR_DECODE_MODE_NORMAL, |
| 2758 | {"\x9f", 1}, |
| 2759 | QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2760 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2761 | { "Indefinite length array with two items and no break", |
| 2762 | QCBOR_DECODE_MODE_NORMAL, |
| 2763 | {"\x9\x01\x02", 3}, |
| 2764 | QCBOR_ERR_NO_MORE_ITEMS |
| 2765 | }, |
| 2766 | { "Indefinite length map with zero items and no break", |
| 2767 | QCBOR_DECODE_MODE_NORMAL, |
| 2768 | {"\xbf", 1}, |
| 2769 | QCBOR_ERR_NO_MORE_ITEMS |
| 2770 | }, |
| 2771 | { "Indefinite length map with two items and no break", |
| 2772 | QCBOR_DECODE_MODE_NORMAL, |
| 2773 | {"\xbf\x01\x02\x01\x02", 5}, |
| 2774 | QCBOR_ERR_NO_MORE_ITEMS |
| 2775 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2776 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2777 | /* Nested maps and arrays must be closed off (some extra nested test vectors) */ |
| 2778 | { "Unclosed indefinite array containing a closed definite length array", |
| 2779 | QCBOR_DECODE_MODE_NORMAL, |
| 2780 | {"\x9f\x80\x00", 3}, |
| 2781 | QCBOR_ERR_NO_MORE_ITEMS |
| 2782 | }, |
| 2783 | |
| 2784 | { "Definite length array containing an unclosed indefinite length array", |
| 2785 | QCBOR_DECODE_MODE_NORMAL, |
| 2786 | {"\x81\x9f", 2}, |
| 2787 | QCBOR_ERR_NO_MORE_ITEMS |
| 2788 | }, |
| 2789 | { "Unclosed indefinite map containing a closed definite length array", |
| 2790 | QCBOR_DECODE_MODE_NORMAL, |
| 2791 | {"\xbf\x01\x80\x00\xa0", 5}, |
| 2792 | QCBOR_ERR_NO_MORE_ITEMS |
| 2793 | }, |
| 2794 | { "Definite length map containing an unclosed indefinite length array", |
| 2795 | QCBOR_DECODE_MODE_NORMAL, |
| 2796 | {"\xa1\x02\x9f", 3}, |
| 2797 | QCBOR_ERR_NO_MORE_ITEMS |
| 2798 | }, |
| 2799 | { "Deeply nested definite length arrays with deepest one unclosed", |
| 2800 | QCBOR_DECODE_MODE_NORMAL, |
| 2801 | {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9}, |
| 2802 | QCBOR_ERR_NO_MORE_ITEMS |
| 2803 | }, |
| 2804 | { "Deeply nested indefinite length arrays with deepest one unclosed", |
| 2805 | QCBOR_DECODE_MODE_NORMAL, |
| 2806 | {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9}, |
| 2807 | QCBOR_ERR_NO_MORE_ITEMS |
| 2808 | }, |
| 2809 | { "Mixed nesting with indefinite unclosed", |
| 2810 | QCBOR_DECODE_MODE_NORMAL, |
| 2811 | {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9}, |
| 2812 | QCBOR_ERR_NO_MORE_ITEMS }, |
| 2813 | { "Mixed nesting with definite unclosed", |
| 2814 | QCBOR_DECODE_MODE_NORMAL, |
| 2815 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2816 | QCBOR_ERR_BAD_BREAK |
| 2817 | }, |
| 2818 | { "Unclosed indefinite length map in definite length maps", |
| 2819 | QCBOR_DECODE_MODE_NORMAL, |
| 2820 | {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8}, |
| 2821 | QCBOR_ERR_NO_MORE_ITEMS |
| 2822 | }, |
| 2823 | { "Unclosed definite length map in indefinite length maps", |
| 2824 | QCBOR_DECODE_MODE_NORMAL, |
| 2825 | {"\xbf\x01\xbf\x02\xa1", 5}, |
| 2826 | QCBOR_ERR_NO_MORE_ITEMS |
| 2827 | }, |
| 2828 | { "Unclosed indefinite length array in definite length maps", |
| 2829 | QCBOR_DECODE_MODE_NORMAL, |
| 2830 | {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8}, |
| 2831 | QCBOR_ERR_NO_MORE_ITEMS |
| 2832 | }, |
| 2833 | { "Unclosed definite length array in indefinite length maps", |
| 2834 | QCBOR_DECODE_MODE_NORMAL, |
| 2835 | {"\xbf\x01\xbf\x02\x81", 5}, |
| 2836 | QCBOR_ERR_NO_MORE_ITEMS |
| 2837 | }, |
| 2838 | { "Unclosed indefinite length map in definite length arrays", |
| 2839 | QCBOR_DECODE_MODE_NORMAL, |
| 2840 | {"\x81\x82\xbf\xff\xbf", 5}, |
| 2841 | QCBOR_ERR_NO_MORE_ITEMS |
| 2842 | }, |
| 2843 | { "Unclosed definite length map in indefinite length arrays", |
| 2844 | QCBOR_DECODE_MODE_NORMAL, |
| 2845 | {"\x9f\x9f\xa1", 3}, |
| 2846 | QCBOR_ERR_NO_MORE_ITEMS |
| 2847 | }, |
| 2848 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2849 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2850 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2851 | /* The "argument" for the data item is incomplete */ |
| 2852 | { "Positive integer missing 1 byte argument", |
| 2853 | QCBOR_DECODE_MODE_NORMAL, |
| 2854 | {"\x18", 1}, |
| 2855 | QCBOR_ERR_HIT_END |
| 2856 | }, |
| 2857 | { "Positive integer missing 2 byte argument", |
| 2858 | QCBOR_DECODE_MODE_NORMAL, |
| 2859 | {"\x19", 1}, |
| 2860 | QCBOR_ERR_HIT_END |
| 2861 | }, |
| 2862 | { "Positive integer missing 4 byte argument", |
| 2863 | QCBOR_DECODE_MODE_NORMAL, |
| 2864 | {"\x1a", 1}, |
| 2865 | QCBOR_ERR_HIT_END |
| 2866 | }, |
| 2867 | { "Positive integer missing 8 byte argument", |
| 2868 | QCBOR_DECODE_MODE_NORMAL, |
| 2869 | {"\x1b", 1}, |
| 2870 | QCBOR_ERR_HIT_END |
| 2871 | }, |
| 2872 | { "Positive integer missing 1 byte of 2 byte argument", |
| 2873 | QCBOR_DECODE_MODE_NORMAL, |
| 2874 | {"\x19\x01", 2}, |
| 2875 | QCBOR_ERR_HIT_END |
| 2876 | }, |
| 2877 | { "Positive integer missing 2 bytes of 4 byte argument", |
| 2878 | QCBOR_DECODE_MODE_NORMAL, |
| 2879 | {"\x1a\x01\x02", 3}, |
| 2880 | QCBOR_ERR_HIT_END |
| 2881 | }, |
| 2882 | { "Positive integer missing 1 bytes of 7 byte argument", |
| 2883 | QCBOR_DECODE_MODE_NORMAL, |
| 2884 | {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8}, |
| 2885 | QCBOR_ERR_HIT_END |
| 2886 | }, |
| 2887 | { "Negative integer missing 1 byte argument", |
| 2888 | QCBOR_DECODE_MODE_NORMAL, |
| 2889 | {"\x38", 1}, |
| 2890 | QCBOR_ERR_HIT_END |
| 2891 | }, |
| 2892 | { "Binary string missing 1 byte argument", |
| 2893 | QCBOR_DECODE_MODE_NORMAL, |
| 2894 | {"\x58", 1}, |
| 2895 | QCBOR_ERR_HIT_END |
| 2896 | }, |
| 2897 | { "Text string missing 1 byte argument", |
| 2898 | QCBOR_DECODE_MODE_NORMAL, |
| 2899 | {"\x78", 1}, |
| 2900 | QCBOR_ERR_HIT_END |
| 2901 | }, |
| 2902 | { "Array missing 1 byte argument", |
| 2903 | QCBOR_DECODE_MODE_NORMAL, |
| 2904 | {"\x98", 1}, |
| 2905 | QCBOR_ERR_HIT_END |
| 2906 | }, |
| 2907 | { "Map missing 1 byte argument", |
| 2908 | QCBOR_DECODE_MODE_NORMAL, |
| 2909 | {"\xb8", 1}, |
| 2910 | QCBOR_ERR_HIT_END |
| 2911 | }, |
| 2912 | { "Tag missing 1 byte argument", |
| 2913 | QCBOR_DECODE_MODE_NORMAL, |
| 2914 | {"\xd8", 1}, |
| 2915 | QCBOR_ERR_HIT_END |
| 2916 | }, |
| 2917 | { "Simple missing 1 byte argument", |
| 2918 | QCBOR_DECODE_MODE_NORMAL, |
| 2919 | {"\xf8", 1}, |
| 2920 | QCBOR_ERR_HIT_END |
| 2921 | }, |
| 2922 | { "half-precision with 1 byte argument", |
| 2923 | QCBOR_DECODE_MODE_NORMAL, |
| 2924 | {"\xf9\x00", 2}, |
| 2925 | QCBOR_ERR_HIT_END |
| 2926 | }, |
| 2927 | { "single-precision with 2 byte argument", |
| 2928 | QCBOR_DECODE_MODE_NORMAL, |
| 2929 | {"\0xfa\x00\x00", 3}, |
| 2930 | QCBOR_ERR_HIT_END |
| 2931 | }, |
| 2932 | { "double-precision with 3 byte argument", |
| 2933 | QCBOR_DECODE_MODE_NORMAL, |
| 2934 | {"\xfb\x00\x00\x00", 4}, |
| 2935 | QCBOR_ERR_HIT_END |
| 2936 | }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2937 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2938 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2939 | { "Tag with no content", |
| 2940 | QCBOR_DECODE_MODE_NORMAL, |
| 2941 | {"\xc0", 1}, |
| 2942 | QCBOR_ERR_HIT_END |
| 2943 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2944 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2945 | { "Tag with no content", |
| 2946 | QCBOR_DECODE_MODE_NORMAL, |
| 2947 | {"\xc0", 1}, |
| 2948 | QCBOR_ERR_TAGS_DISABLED |
| 2949 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2950 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2951 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2952 | /* Breaks must not occur in definite length arrays and maps */ |
| 2953 | { "Array of length 1 with sole member replaced by a break", |
| 2954 | QCBOR_DECODE_MODE_NORMAL, |
| 2955 | {"\x81\xff", 2}, |
| 2956 | QCBOR_ERR_BAD_BREAK |
| 2957 | }, |
| 2958 | { "Array of length 2 with 2nd member replaced by a break", |
| 2959 | QCBOR_DECODE_MODE_NORMAL, |
| 2960 | {"\x82\x00\xff", 3}, |
| 2961 | QCBOR_ERR_BAD_BREAK |
| 2962 | }, |
| 2963 | { "Map of length 1 with sole member label replaced by a break", |
| 2964 | QCBOR_DECODE_MODE_NORMAL, |
| 2965 | {"\xa1\xff", 2}, |
| 2966 | QCBOR_ERR_BAD_BREAK |
| 2967 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2968 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2969 | /* Map of length 1 with sole member label replaced by break */ |
| 2970 | { "Alternate representation that some decoders handle differently", |
| 2971 | QCBOR_DECODE_MODE_NORMAL, |
| 2972 | {"\xa1\xff\x00", 3}, |
| 2973 | QCBOR_ERR_BAD_BREAK |
| 2974 | }, |
| 2975 | { "Array of length 1 with 2nd member value replaced by a break", |
| 2976 | QCBOR_DECODE_MODE_NORMAL, |
| 2977 | {"\xa1\x00\xff", 3}, |
| 2978 | QCBOR_ERR_BAD_BREAK |
| 2979 | }, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2980 | { "Map of length 2 with 2nd entry label replaced by a break", |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2981 | QCBOR_DECODE_MODE_NORMAL, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2982 | {"\xa2\x00\x00\xff\x00", 5}, |
| 2983 | QCBOR_ERR_BAD_BREAK |
| 2984 | }, |
| 2985 | { "Map of length 2 with 2nd entry value replaced by a break", |
| 2986 | QCBOR_DECODE_MODE_NORMAL, |
| 2987 | {"\xa2\x00\x00\x01\xff", 5}, |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2988 | QCBOR_ERR_BAD_BREAK |
| 2989 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2990 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2991 | /* Breaks must not occur on their own out of an indefinite length data item */ |
| 2992 | { "A bare break is not well formed", |
| 2993 | QCBOR_DECODE_MODE_NORMAL, |
| 2994 | {"\xff", 1}, |
| 2995 | QCBOR_ERR_BAD_BREAK |
| 2996 | }, |
| 2997 | { "A bare break after a zero length definite length array", |
| 2998 | QCBOR_DECODE_MODE_NORMAL, |
| 2999 | {"\x80\xff", 2}, |
| 3000 | QCBOR_ERR_BAD_BREAK |
| 3001 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3002 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3003 | { "A bare break after a zero length indefinite length map", |
| 3004 | QCBOR_DECODE_MODE_NORMAL, |
| 3005 | {"\x9f\xff\xff", 3}, |
| 3006 | QCBOR_ERR_BAD_BREAK |
| 3007 | }, |
| 3008 | { "A break inside a definite length array inside an indefenite length array", |
| 3009 | QCBOR_DECODE_MODE_NORMAL, |
| 3010 | {"\x9f\x81\xff", 3}, |
| 3011 | QCBOR_ERR_BAD_BREAK |
| 3012 | }, |
| 3013 | { "Complicated mixed nesting with break outside indefinite length array", |
| 3014 | QCBOR_DECODE_MODE_NORMAL, |
| 3015 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 3016 | QCBOR_ERR_BAD_BREAK }, |
| 3017 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 3018 | |
| 3019 | /* Forbidden two byte encodings of simple types */ |
| 3020 | { "Must use 0xe0 instead", |
| 3021 | QCBOR_DECODE_MODE_NORMAL, |
| 3022 | {"\xf8\x00", 2}, |
| 3023 | QCBOR_ERR_BAD_TYPE_7 |
| 3024 | }, |
| 3025 | { "Should use 0xe1 instead", |
| 3026 | QCBOR_DECODE_MODE_NORMAL, |
| 3027 | {"\xf8\x01", 2}, |
| 3028 | QCBOR_ERR_BAD_TYPE_7 |
| 3029 | }, |
| 3030 | { "Should use 0xe2 instead", |
| 3031 | QCBOR_DECODE_MODE_NORMAL, |
| 3032 | {"\xf8\x02", 2}, |
| 3033 | QCBOR_ERR_BAD_TYPE_7 |
| 3034 | }, { "Should use 0xe3 instead", |
| 3035 | QCBOR_DECODE_MODE_NORMAL, |
| 3036 | {"\xf8\x03", 2}, |
| 3037 | QCBOR_ERR_BAD_TYPE_7 |
| 3038 | }, |
| 3039 | { "Should use 0xe4 instead", |
| 3040 | QCBOR_DECODE_MODE_NORMAL, |
| 3041 | {"\xf8\x04", 2}, |
| 3042 | QCBOR_ERR_BAD_TYPE_7 |
| 3043 | }, |
| 3044 | { "Should use 0xe5 instead", |
| 3045 | QCBOR_DECODE_MODE_NORMAL, |
| 3046 | {"\xf8\x05", 2}, |
| 3047 | QCBOR_ERR_BAD_TYPE_7 |
| 3048 | }, |
| 3049 | { "Should use 0xe6 instead", |
| 3050 | QCBOR_DECODE_MODE_NORMAL, |
| 3051 | {"\xf8\x06", 2}, |
| 3052 | QCBOR_ERR_BAD_TYPE_7 |
| 3053 | }, |
| 3054 | { "Should use 0xe7 instead", |
| 3055 | QCBOR_DECODE_MODE_NORMAL, |
| 3056 | {"\xf8\x07", 2}, |
| 3057 | QCBOR_ERR_BAD_TYPE_7 |
| 3058 | }, |
| 3059 | { "Should use 0xe8 instead", |
| 3060 | QCBOR_DECODE_MODE_NORMAL, |
| 3061 | {"\xf8\x08", 2}, |
| 3062 | QCBOR_ERR_BAD_TYPE_7 |
| 3063 | }, |
| 3064 | { "Should use 0xe9 instead", |
| 3065 | QCBOR_DECODE_MODE_NORMAL, |
| 3066 | {"\xf8\x09", 2}, |
| 3067 | QCBOR_ERR_BAD_TYPE_7 |
| 3068 | }, |
| 3069 | { "Should use 0xea instead", |
| 3070 | QCBOR_DECODE_MODE_NORMAL, |
| 3071 | {"\xf8\x0a", 2}, |
| 3072 | QCBOR_ERR_BAD_TYPE_7 |
| 3073 | }, |
| 3074 | { "Should use 0xeb instead", |
| 3075 | QCBOR_DECODE_MODE_NORMAL, |
| 3076 | {"\xf8\x0b", 2}, |
| 3077 | QCBOR_ERR_BAD_TYPE_7 |
| 3078 | }, |
| 3079 | { "Should use 0xec instead", |
| 3080 | QCBOR_DECODE_MODE_NORMAL, |
| 3081 | {"\xf8\x0c", 2}, |
| 3082 | QCBOR_ERR_BAD_TYPE_7 |
| 3083 | }, |
| 3084 | { "Should use 0xed instead", |
| 3085 | QCBOR_DECODE_MODE_NORMAL, |
| 3086 | {"\xf8\x0d", 2}, |
| 3087 | QCBOR_ERR_BAD_TYPE_7 |
| 3088 | }, |
| 3089 | { "Should use 0xee instead", |
| 3090 | QCBOR_DECODE_MODE_NORMAL, |
| 3091 | {"\xf8\x0e", 2}, |
| 3092 | QCBOR_ERR_BAD_TYPE_7 |
| 3093 | }, |
| 3094 | { "Should use 0xef instead", |
| 3095 | QCBOR_DECODE_MODE_NORMAL, |
| 3096 | {"\xf8\x0f", 2}, |
| 3097 | QCBOR_ERR_BAD_TYPE_7 |
| 3098 | }, |
| 3099 | { "Should use 0xf0 instead", |
| 3100 | QCBOR_DECODE_MODE_NORMAL, |
| 3101 | {"\xf8\x10", 2}, |
| 3102 | QCBOR_ERR_BAD_TYPE_7 |
| 3103 | }, |
| 3104 | { "Should use 0xf1 instead", |
| 3105 | QCBOR_DECODE_MODE_NORMAL, |
| 3106 | {"\xf8\x11", 2}, |
| 3107 | QCBOR_ERR_BAD_TYPE_7 |
| 3108 | }, |
| 3109 | { "Should use 0xf2 instead", |
| 3110 | QCBOR_DECODE_MODE_NORMAL, |
| 3111 | {"\xf8\x12", 2}, |
| 3112 | QCBOR_ERR_BAD_TYPE_7 |
| 3113 | }, |
| 3114 | { "Should use 0xf3 instead", |
| 3115 | QCBOR_DECODE_MODE_NORMAL, |
| 3116 | {"\xf8\x13", 2}, |
| 3117 | QCBOR_ERR_BAD_TYPE_7 |
| 3118 | }, |
| 3119 | { "Should use 0xf4 instead", |
| 3120 | QCBOR_DECODE_MODE_NORMAL, |
| 3121 | {"\xf8\x14", 2}, |
| 3122 | QCBOR_ERR_BAD_TYPE_7 |
| 3123 | }, |
| 3124 | { "Should use 0xf5 instead", |
| 3125 | QCBOR_DECODE_MODE_NORMAL, |
| 3126 | {"\xf8\x15", 2}, |
| 3127 | QCBOR_ERR_BAD_TYPE_7 |
| 3128 | }, |
| 3129 | { "Should use 0xf6 instead", |
| 3130 | QCBOR_DECODE_MODE_NORMAL, |
| 3131 | {"\xf8\x16", 2}, |
| 3132 | QCBOR_ERR_BAD_TYPE_7 |
| 3133 | }, |
| 3134 | { "Should use 0xef7 instead", |
| 3135 | QCBOR_DECODE_MODE_NORMAL, |
| 3136 | {"\xf8\x17", 2}, |
| 3137 | QCBOR_ERR_BAD_TYPE_7 |
| 3138 | }, |
| 3139 | { "Should use 0xef8 instead", |
| 3140 | QCBOR_DECODE_MODE_NORMAL, |
| 3141 | {"\xf8\x18", 2}, |
| 3142 | QCBOR_ERR_BAD_TYPE_7 |
| 3143 | }, |
| 3144 | { "Reserved", |
| 3145 | QCBOR_DECODE_MODE_NORMAL, |
| 3146 | {"\xf8\x18", 2}, |
| 3147 | QCBOR_ERR_BAD_TYPE_7 |
| 3148 | }, |
| 3149 | |
| 3150 | /* Maps must have an even number of data items (key & value) */ |
| 3151 | { "Map with 1 item when it should have 2", |
| 3152 | QCBOR_DECODE_MODE_NORMAL, |
| 3153 | {"\xa1\x00", 2}, |
| 3154 | QCBOR_ERR_HIT_END |
| 3155 | }, |
| 3156 | { "Map with 3 item when it should have 4", |
| 3157 | QCBOR_DECODE_MODE_NORMAL, |
| 3158 | {"\xa2\x00\x00\x00", 2}, |
| 3159 | QCBOR_ERR_HIT_END |
| 3160 | }, |
| 3161 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 3162 | { "Map with 1 item when it should have 2", |
| 3163 | QCBOR_DECODE_MODE_NORMAL, |
| 3164 | {"\xbf\x00\xff", 3}, |
| 3165 | QCBOR_ERR_BAD_BREAK |
| 3166 | }, |
| 3167 | { "Map with 3 item when it should have 4", |
| 3168 | QCBOR_DECODE_MODE_NORMAL, |
| 3169 | {"\xbf\x00\x00\x00\xff", 5}, |
| 3170 | QCBOR_ERR_BAD_BREAK |
| 3171 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3172 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3173 | |
| 3174 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3175 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3176 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3177 | { "Text-based date, with an integer", |
| 3178 | QCBOR_DECODE_MODE_NORMAL, |
| 3179 | {"\xc0\x00", 2}, |
| 3180 | QCBOR_ERR_BAD_OPT_TAG |
| 3181 | }, |
| 3182 | { "Epoch date, with an byte string", |
| 3183 | QCBOR_DECODE_MODE_NORMAL, |
| 3184 | {"\xc1\x41\x33", 3}, |
| 3185 | QCBOR_ERR_BAD_OPT_TAG |
| 3186 | }, |
| 3187 | { "tagged as both epoch and string dates", |
| 3188 | QCBOR_DECODE_MODE_NORMAL, |
| 3189 | {"\xc1\xc0\x00", 3}, |
| 3190 | QCBOR_ERR_BAD_OPT_TAG |
| 3191 | }, |
| 3192 | { "big num tagged an int, not a byte string", |
| 3193 | QCBOR_DECODE_MODE_NORMAL, |
| 3194 | {"\xc2\x00", 2}, |
| 3195 | QCBOR_ERR_BAD_OPT_TAG |
| 3196 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3197 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3198 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3199 | { "Text-based date, with an integer", |
| 3200 | QCBOR_DECODE_MODE_NORMAL, |
| 3201 | {"\xc0\x00", 2}, |
| 3202 | QCBOR_ERR_TAGS_DISABLED |
| 3203 | }, |
| 3204 | { "Epoch date, with an byte string", |
| 3205 | QCBOR_DECODE_MODE_NORMAL, |
| 3206 | {"\xc1\x41\x33", 3}, |
| 3207 | QCBOR_ERR_TAGS_DISABLED |
| 3208 | }, |
| 3209 | { "tagged as both epoch and string dates", |
| 3210 | QCBOR_DECODE_MODE_NORMAL, |
| 3211 | {"\xc1\xc0\x00", 3}, |
| 3212 | QCBOR_ERR_TAGS_DISABLED |
| 3213 | }, |
| 3214 | { "big num tagged an int, not a byte string", |
| 3215 | QCBOR_DECODE_MODE_NORMAL, |
| 3216 | {"\xc2\x00", 2}, |
| 3217 | QCBOR_ERR_TAGS_DISABLED |
| 3218 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3219 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3220 | }; |
| 3221 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3222 | |
| 3223 | |
| 3224 | int32_t |
| 3225 | DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3226 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3227 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3228 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3229 | nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3230 | if(nResult) { |
| 3231 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3232 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3233 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3234 | // Corrupt the UsefulInputBuf and see that |
| 3235 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3236 | QCBORDecodeContext DCtx; |
| 3237 | QCBORItem Item; |
| 3238 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3239 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3240 | QCBORDecode_Init(&DCtx, |
| 3241 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 3242 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3243 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3244 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3245 | return (int32_t)uQCBORError; |
| 3246 | } |
| 3247 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 3248 | // This wasn't supposed to happen |
| 3249 | return -1; |
| 3250 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3251 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3252 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3253 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3254 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3255 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 3256 | // Did not get back the error expected |
| 3257 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3258 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3259 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3260 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3261 | /* |
| 3262 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 3263 | tests here can be performed to see that the max length |
| 3264 | error check works correctly. See DecodeBytes(). If the max |
| 3265 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3266 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3267 | This test will automatocally adapt the all CPU sizes |
| 3268 | through the use of SIZE_MAX. |
| 3269 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3270 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 3271 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3272 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3273 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3274 | // This makes a CBOR head with a text string that is very long |
| 3275 | // but doesn't fill in the bytes of the text string as that is |
| 3276 | // not needed to test this part of QCBOR. |
| 3277 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 3278 | |
| 3279 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 3280 | |
| 3281 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3282 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3283 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3284 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3285 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3289 | /* Try all 256 values of the byte at nLen including recursing for |
| 3290 | each of the values to try values at nLen+1 ... up to nLenMax |
| 3291 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 3292 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3293 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3294 | if(nLen >= nLenMax) { |
| 3295 | return; |
| 3296 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3297 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3298 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 3299 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3300 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3301 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3302 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3303 | // Get ready to parse |
| 3304 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3305 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3306 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3307 | // Parse by getting the next item until an error occurs |
| 3308 | // Just about every possible decoder error can occur here |
| 3309 | // The goal of this test is not to check for the correct |
| 3310 | // error since that is not really possible. It is to |
| 3311 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3312 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3313 | QCBORItem Item; |
| 3314 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3315 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3316 | break; |
| 3317 | } |
| 3318 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3319 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3320 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3325 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3326 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3327 | // Size 2 tests 64K inputs and runs quickly |
| 3328 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3329 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3330 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3331 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3332 | return 0; |
| 3333 | } |
| 3334 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3335 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3336 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3337 | { |
| 3338 | // size 3 tests 16 million inputs and runs OK |
| 3339 | // in seconds on fast machines. Size 4 takes |
| 3340 | // 10+ minutes and 5 half a day on fast |
| 3341 | // machines. This test is kept separate from |
| 3342 | // the others so as to no slow down the use |
| 3343 | // of them as a very frequent regression. |
| 3344 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3345 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3346 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3347 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3348 | return 0; |
| 3349 | } |
| 3350 | |
| 3351 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3352 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3353 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3354 | 0xc0, // tag for string date |
| 3355 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3356 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3357 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3358 | 0xc0, // tag for string date |
| 3359 | 0x00, // Wrong type for a string date |
| 3360 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3361 | /* 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] | 3362 | 0xc1, // tag for epoch date |
| 3363 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3364 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3365 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3366 | 0xc1, |
| 3367 | 0x62, 'h', 'i', // wrong type tagged |
| 3368 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3369 | /* 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] | 3370 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3371 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3372 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 3373 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3374 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3375 | 0xc1, // tag for epoch date |
| 3376 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3377 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3378 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3379 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 3380 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3381 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3382 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3383 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3384 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3385 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3386 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3387 | 0xc1, // tag for epoch date |
| 3388 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 3389 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 3390 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3391 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3392 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3393 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3394 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3395 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3396 | 0xc1, // tag for epoch date |
| 3397 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 3398 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3399 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3400 | 0xc1, |
| 3401 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 3402 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3403 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3404 | 0xc1, // tag for epoch date |
| 3405 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3406 | }; |
| 3407 | |
| 3408 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3409 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3410 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3411 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 3412 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3413 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3414 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3415 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3416 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3417 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3418 | return diff > 0.0000001; |
| 3419 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3420 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3421 | |
| 3422 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3423 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3424 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3425 | { |
| 3426 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3427 | QCBORItem Item; |
| 3428 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3429 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3430 | QCBORDecode_Init(&DCtx, |
| 3431 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 3432 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3433 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3434 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3435 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3436 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3437 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3438 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | ba58768 | 2024-02-07 16:46:43 -0800 | [diff] [blame] | 3439 | UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3440 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3441 | } |
| 3442 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3443 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3444 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3445 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3446 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3447 | } |
| 3448 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3449 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 3450 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3451 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3452 | return -4; |
| 3453 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3454 | if(uError == QCBOR_SUCCESS) { |
| 3455 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3456 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3457 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3458 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3459 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3460 | ) { |
| 3461 | return -5; |
| 3462 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3463 | } |
| 3464 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3465 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3466 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3467 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3468 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3469 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3470 | /* 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] | 3471 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 3472 | // The date is decoded and the two tags are returned. This is to |
| 3473 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3474 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3475 | return -7; |
| 3476 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3477 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3478 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3479 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3480 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3481 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 3482 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3483 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3484 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3485 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3486 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3487 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3488 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3489 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3490 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3491 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 3492 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3493 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3494 | return -10; |
| 3495 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3496 | if(uError == QCBOR_SUCCESS) { |
| 3497 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3498 | Item.val.epochDate.nSeconds != 1 |
| 3499 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3500 | || 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] | 3501 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3502 | ) { |
| 3503 | return -11; |
| 3504 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3505 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3506 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3507 | /* 8. Epoch date with too-large single-precision float */ |
| 3508 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3509 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3510 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3511 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3512 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3513 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 3514 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3515 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3516 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3517 | } |
| 3518 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3519 | /* 10. Epoch date with largest supported double-precision value */ |
| 3520 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3521 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3522 | return -14; |
| 3523 | } |
| 3524 | if(uError == QCBOR_SUCCESS) { |
| 3525 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3526 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 3527 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3528 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 3529 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 3530 | ) { |
| 3531 | return -14; |
| 3532 | } |
| 3533 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3534 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3535 | /* 11. Epoch date with single-precision NaN */ |
| 3536 | 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] | 3537 | return -15; |
| 3538 | } |
| 3539 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3540 | /* 12. Epoch date with double-precision plus infinity */ |
| 3541 | 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] | 3542 | return -16; |
| 3543 | } |
| 3544 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3545 | /* 13. Epoch date with half-precision negative infinity */ |
| 3546 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3547 | 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] | 3548 | return -17; |
| 3549 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3550 | |
| 3551 | return 0; |
| 3552 | } |
| 3553 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3554 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3555 | /* |
| 3556 | Test cases covered here. Some items cover more than one of these. |
| 3557 | positive integer (zero counts as a positive integer) |
| 3558 | negative integer |
| 3559 | half-precision float |
| 3560 | single-precision float |
| 3561 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3562 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3563 | float Overflow error |
| 3564 | Wrong type error for epoch |
| 3565 | Wrong type error for date string |
| 3566 | float disabled error |
| 3567 | half-precision disabled error |
| 3568 | -Infinity |
| 3569 | Slightly too large integer |
| 3570 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3571 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3572 | Get epoch by int |
| 3573 | Get string by int |
| 3574 | Get epoch by string |
| 3575 | Get string by string |
| 3576 | Fail to get epoch by wrong int label |
| 3577 | Fail to get string by wrong string label |
| 3578 | Fail to get epoch by string because it is invalid |
| 3579 | Fail to get epoch by int because it is invalid |
| 3580 | |
| 3581 | Untagged values |
| 3582 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3583 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3584 | 0x87, // array of 7 items |
| 3585 | |
| 3586 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 3587 | |
| 3588 | // Untagged integer 0 |
| 3589 | 0x08, |
| 3590 | 0x00, |
| 3591 | |
| 3592 | // Utagged date string with string label y |
| 3593 | 0x61, 0x79, |
| 3594 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 3595 | |
| 3596 | // Untagged single-precision float with value 3.14 with string label x |
| 3597 | 0x61, 0x78, |
| 3598 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 3599 | |
| 3600 | // Untagged half-precision float with value -2 |
| 3601 | 0x09, |
| 3602 | 0xF9, 0xC0, 0x00, |
| 3603 | |
| 3604 | /* Untagged date-only date string */ |
| 3605 | 0x18, 0x63, |
| 3606 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3607 | |
| 3608 | /* Untagged days-count epoch date */ |
| 3609 | 0x11, |
| 3610 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 3611 | |
| 3612 | // End of map, back to array |
| 3613 | |
| 3614 | 0xa7, // Open map of tagged items with labels |
| 3615 | |
| 3616 | 0x00, |
| 3617 | 0xc0, // tag for string date |
| 3618 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 3619 | |
| 3620 | |
| 3621 | 0x01, |
| 3622 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 3623 | 0xc1, // tag for epoch date |
| 3624 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3625 | |
| 3626 | 0x05, |
| 3627 | 0xc1, |
| 3628 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 3629 | |
| 3630 | |
| 3631 | 0x07, |
| 3632 | 0xc1, // tag for epoch date |
| 3633 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3634 | |
| 3635 | /* Tagged days-count epoch date */ |
| 3636 | 0x63, 0x53, 0x44, 0x45, |
| 3637 | 0xD8, 0x64, /* tag(100) */ |
| 3638 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 3639 | |
| 3640 | // Untagged -1000 with label z |
| 3641 | 0x61, 0x7a, |
| 3642 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 3643 | 0x39, 0x03, 0xe7, |
| 3644 | |
| 3645 | /* Tagged date-only date string */ |
| 3646 | 0x63, 0x53, 0x44, 0x53, |
| 3647 | 0xD9, 0x03, 0xEC, |
| 3648 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3649 | |
| 3650 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3651 | |
| 3652 | 0xc1, |
| 3653 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 3654 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3655 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3656 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 3657 | |
| 3658 | 0xc1, // tag for epoch date |
| 3659 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 3660 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3661 | // These two at the end because they are unrecoverable errors |
| 3662 | 0xc1, // tag for epoch date |
| 3663 | 0x80, // Erroneous empty array as content for date |
| 3664 | |
| 3665 | 0xc0, // tag for string date |
| 3666 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3667 | }; |
| 3668 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3669 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3670 | { |
| 3671 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3672 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3673 | int64_t nEpochDate3, nEpochDate5, |
| 3674 | nEpochDate4, nEpochDate6, |
| 3675 | nEpochDays2; |
| 3676 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3677 | |
| 3678 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3679 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3680 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3681 | |
| 3682 | /* Items are in an array or map to test look up by label and other |
| 3683 | * that might not occur in isolated items. But it does make the |
| 3684 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3685 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3686 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3687 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3688 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3689 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3690 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3691 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3692 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3693 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3694 | return 104; |
| 3695 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3696 | if(uError == QCBOR_SUCCESS) { |
| 3697 | if(nEpochDate5 != 3) { |
| 3698 | return 103; |
| 3699 | } |
| 3700 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3701 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3702 | // A half-precision date with value -2 FFF |
| 3703 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3704 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3705 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3706 | 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] | 3707 | return 106; |
| 3708 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3709 | if(uError == QCBOR_SUCCESS) { |
| 3710 | if(nEpochDate4 != -2) { |
| 3711 | return 105; |
| 3712 | } |
| 3713 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3714 | |
| 3715 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3716 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 3717 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3718 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3719 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3720 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3721 | return 107; |
| 3722 | } |
| 3723 | |
| 3724 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3725 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3726 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3727 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3728 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3729 | return 108; |
| 3730 | } |
| 3731 | |
| 3732 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3733 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 3734 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3735 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3736 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3737 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3738 | return 109; |
| 3739 | } |
| 3740 | |
| 3741 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3742 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3743 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3744 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3745 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3746 | return 110; |
| 3747 | } |
| 3748 | |
| 3749 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3750 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3751 | |
| 3752 | // Untagged integer 0 |
| 3753 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3754 | &nEpochDate3); |
| 3755 | // Untagged date string |
| 3756 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3757 | &StringDate2); |
| 3758 | |
| 3759 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3760 | &StringDays2); |
| 3761 | |
| 3762 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3763 | &nEpochDays2); |
| 3764 | |
| 3765 | QCBORDecode_ExitMap(&DC); |
| 3766 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3767 | return 3001; |
| 3768 | } |
| 3769 | |
| 3770 | // The map of tagged items |
| 3771 | QCBORDecode_EnterMap(&DC, NULL); |
| 3772 | |
| 3773 | #ifndef QCBOR_DISABLE_TAGS |
| 3774 | int64_t nEpochDate2, |
| 3775 | nEpochDateFail, |
| 3776 | nEpochDate1400000000, nEpochDays1; |
| 3777 | UsefulBufC StringDays1; |
| 3778 | uint64_t uTag1, uTag2; |
| 3779 | |
| 3780 | // Tagged date string |
| 3781 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3782 | &StringDate1); |
| 3783 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3784 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3785 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3786 | 1, |
| 3787 | QCBOR_TAG_REQUIREMENT_TAG | |
| 3788 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3789 | &nEpochDate1400000000); |
| 3790 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3791 | |
| 3792 | // Get largest negative double precision epoch date allowed |
| 3793 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3794 | 5, |
| 3795 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 3796 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3797 | &nEpochDate2); |
| 3798 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3799 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3800 | return 102; |
| 3801 | } |
| 3802 | if(uError == QCBOR_SUCCESS) { |
| 3803 | if(nEpochDate2 != -9223372036854773760LL) { |
| 3804 | return 101; |
| 3805 | } |
| 3806 | } |
| 3807 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3808 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3809 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 3810 | "z", |
| 3811 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 3812 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3813 | &nEpochDate6); |
| 3814 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3815 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3816 | |
| 3817 | // Get largest double precision epoch date allowed |
| 3818 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3819 | &nEpochDate2); |
| 3820 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3821 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3822 | return 112; |
| 3823 | } |
| 3824 | if(uError == QCBOR_SUCCESS) { |
| 3825 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 3826 | return 111; |
| 3827 | } |
| 3828 | } |
| 3829 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3830 | /* The days format is much simpler than the date format |
| 3831 | * because it can't be a floating point value. The test |
| 3832 | * of the spiffy decode functions sufficiently covers |
| 3833 | * the test of the non-spiffy decode days date decoding. |
| 3834 | * There is no full fan out of the error conditions |
| 3835 | * and decode options as that is implemented by code |
| 3836 | * that is tested well by the date testing above. |
| 3837 | */ |
| 3838 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 3839 | &StringDays1); |
| 3840 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3841 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 3842 | &nEpochDays1); |
| 3843 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3844 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3845 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3846 | return 3001; |
| 3847 | } |
| 3848 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3849 | // Too-negative float, -9.2233720368547748E+18 |
| 3850 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3851 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3852 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3853 | return 1111; |
| 3854 | } |
| 3855 | |
| 3856 | // Too-large integer |
| 3857 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3858 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3859 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 3860 | return 1; |
| 3861 | } |
| 3862 | |
| 3863 | // Half-precision minus infinity |
| 3864 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3865 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3866 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3867 | return 2; |
| 3868 | } |
| 3869 | |
| 3870 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3871 | // Bad content for epoch date |
| 3872 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3873 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3874 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3875 | return 3; |
| 3876 | } |
| 3877 | |
| 3878 | // Bad content for string date |
| 3879 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 3880 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3881 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3882 | return 4; |
| 3883 | } |
| 3884 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3885 | QCBORDecode_ExitArray(&DC); |
| 3886 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3887 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3888 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3889 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3890 | #else /* QCBOR_DISABLE_TAGS */ |
| 3891 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3892 | &StringDate1); |
| 3893 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3894 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 3895 | return 4; |
| 3896 | } |
| 3897 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3898 | |
| 3899 | |
| 3900 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3901 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3902 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3903 | return 200; |
| 3904 | } |
| 3905 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3906 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3907 | return 201; |
| 3908 | } |
| 3909 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3910 | if(nEpochDays1 != -10676) { |
| 3911 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3912 | } |
| 3913 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3914 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3915 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3918 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3919 | return 204; |
| 3920 | } |
| 3921 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3922 | if(nEpochDate6 != -1000) { |
| 3923 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3924 | } |
| 3925 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3926 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3927 | return 205; |
| 3928 | } |
| 3929 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3930 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3931 | |
| 3932 | if(nEpochDate3 != 0) { |
| 3933 | return 202; |
| 3934 | } |
| 3935 | |
| 3936 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3937 | return 206; |
| 3938 | } |
| 3939 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3940 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 3941 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3945 | return 208; |
| 3946 | } |
| 3947 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3948 | return 0; |
| 3949 | } |
| 3950 | |
| 3951 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3952 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3953 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3954 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3955 | 0x81, // Array of one |
| 3956 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 3957 | 0x82, // Array of two that is the faction 1/3 |
| 3958 | 0x01, |
| 3959 | 0x03, |
| 3960 | |
| 3961 | /* |
| 3962 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3963 | */ |
| 3964 | 0xd8, 0xe1, |
| 3965 | 0xd8, 0xe2, |
| 3966 | 0xd8, 0xe3, |
| 3967 | 0xd8, 0xe4, |
| 3968 | 0xd8, 0xe5, |
| 3969 | 0x80, |
| 3970 | |
| 3971 | /* tag 10489608748473423768( |
| 3972 | 2442302356( |
| 3973 | 21590( |
| 3974 | 240( |
| 3975 | [])))) |
| 3976 | */ |
| 3977 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3978 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 3979 | 0xd9, 0x54, 0x56, |
| 3980 | 0xd8, 0xf0, |
| 3981 | 0x80, |
| 3982 | |
| 3983 | /* tag 21590( |
| 3984 | 10489608748473423768( |
| 3985 | 2442302357( |
| 3986 | 65534( |
| 3987 | [])))) |
| 3988 | */ |
| 3989 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 3990 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3991 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3992 | 0xd9, 0xff, 0xfe, |
| 3993 | 0x80, |
| 3994 | |
| 3995 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3996 | works in conjuntion with entries above. |
| 3997 | 269488144(269488145(269488146(269488147([])))) |
| 3998 | */ |
| 3999 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 4000 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 4001 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 4002 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 4003 | 0x80, |
| 4004 | |
| 4005 | /* An invalid decimal fraction with an additional tag */ |
| 4006 | 0xd9, 0xff, 0xfa, |
| 4007 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 4008 | 0x00, // the integer 0; should be a byte string |
| 4009 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4010 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4011 | /* |
| 4012 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4013 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4014 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4015 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4016 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4017 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4018 | /* |
| 4019 | DB 9192939495969798 # tag(10489608748473423768) |
| 4020 | D8 88 # tag(136) |
| 4021 | C6 # tag(6) |
| 4022 | C7 # tag(7) |
| 4023 | 80 # array(0) |
| 4024 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4025 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4026 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4027 | |
| 4028 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4029 | 55799(55799(55799({ |
| 4030 | 6(7(-23)): 5859837686836516696(7({ |
| 4031 | 7(-20): 11({ |
| 4032 | 17(-18): 17(17(17("Organization"))), |
| 4033 | 9(-17): 773("SSG"), |
| 4034 | -15: 16(17(6(7("Confusion")))), |
| 4035 | 17(-16): 17("San Diego"), |
| 4036 | 17(-14): 17("US") |
| 4037 | }), |
| 4038 | 23(-19): 19({ |
| 4039 | -11: 9({ |
| 4040 | -9: -7 |
| 4041 | }), |
| 4042 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 4043 | }) |
| 4044 | })), |
| 4045 | 16(-22): 23({ |
| 4046 | 11(8(7(-5))): 8(-3) |
| 4047 | }) |
| 4048 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4049 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4050 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4051 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 4052 | 0xc6, 0xc7, 0x36, |
| 4053 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 4054 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 4055 | 0xcb, 0xa5, |
| 4056 | 0xd1, 0x31, |
| 4057 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 4058 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 4059 | 0xc9, 0x30, |
| 4060 | 0xd9, 0x03, 0x05, 0x63, |
| 4061 | 0x53, 0x53, 0x47, |
| 4062 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4063 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 4064 | 0x69, |
| 4065 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4066 | 0xd1, 0x2f, |
| 4067 | 0xd1, 0x69, |
| 4068 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 4069 | 0xd1, 0x2d, |
| 4070 | 0xd1, 0x62, |
| 4071 | 0x55, 0x53, |
| 4072 | 0xd7, 0x32, |
| 4073 | 0xd3, 0xa2, |
| 4074 | 0x2a, |
| 4075 | 0xc9, 0xa1, |
| 4076 | 0x28, |
| 4077 | 0x26, |
| 4078 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 4079 | 0xcc, 0x4a, |
| 4080 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 4081 | 0xd0, 0x35, |
| 4082 | 0xd7, 0xa1, |
| 4083 | 0xcb, 0xc8, 0xc7, 0x24, |
| 4084 | 0xc8, 0x22}; |
| 4085 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4086 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4087 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4088 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4089 | |
| 4090 | 0xc0, // tag for string date |
| 4091 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4092 | |
| 4093 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4094 | |
| 4095 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 4096 | |
| 4097 | 0xd8, 0x23, // tag for regex |
| 4098 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4099 | |
| 4100 | 0xc0, // tag for string date |
| 4101 | 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] | 4102 | |
| 4103 | // This last case makes the array untraversable because it is |
| 4104 | // an uncrecoverable error. Make sure it stays last and is the only |
| 4105 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4106 | }; |
| 4107 | |
| 4108 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4109 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4110 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4111 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4112 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4113 | { |
| 4114 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4115 | QCBORItem Item; |
| 4116 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4117 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4118 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4119 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4120 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4121 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4122 | /* |
| 4123 | This test matches the magic number tag and the fraction tag |
| 4124 | 55799([...]) |
| 4125 | */ |
| 4126 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4127 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4128 | return -2; |
| 4129 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4130 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4131 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 4132 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4133 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4134 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4135 | /* |
| 4136 | 4([1,3]) |
| 4137 | */ |
| 4138 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4139 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4140 | if(uError != QCBOR_SUCCESS || |
| 4141 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4142 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 4143 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 4144 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4145 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4146 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4147 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 4148 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4149 | return -4; |
| 4150 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4151 | // consume the items in the array |
| 4152 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4153 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4154 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4155 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4156 | if(uError != QCBOR_SUCCESS || |
| 4157 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4158 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 4159 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4160 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4161 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4162 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 4163 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4164 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4165 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4166 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4167 | /* |
| 4168 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 4169 | */ |
| 4170 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4171 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4172 | return -6; |
| 4173 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4174 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 4175 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 4176 | return -106; |
| 4177 | } |
| 4178 | |
| 4179 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4180 | /* tag 10489608748473423768( |
| 4181 | 2442302356( |
| 4182 | 21590( |
| 4183 | 240( |
| 4184 | [])))) |
| 4185 | */ |
| 4186 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4187 | if(uError != QCBOR_SUCCESS || |
| 4188 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4189 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 4190 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 4191 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 4192 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4193 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | /* tag 21590( |
| 4197 | 10489608748473423768( |
| 4198 | 2442302357( |
| 4199 | 21591( |
| 4200 | [])))) |
| 4201 | */ |
| 4202 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4203 | if(uError != QCBOR_SUCCESS || |
| 4204 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4205 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 4206 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 4207 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 4208 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 4209 | return -8; |
| 4210 | } |
| 4211 | |
| 4212 | /* Make sure to blow past the limit of tags that must be mapped. |
| 4213 | works in conjuntion with entries above. |
| 4214 | 269488144(269488145(269488146(269488147([])))) |
| 4215 | */ |
| 4216 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4217 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4218 | return -9; |
| 4219 | } |
| 4220 | |
| 4221 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4222 | if(uError == QCBOR_SUCCESS) { |
| 4223 | return -10; |
| 4224 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4225 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4226 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4227 | // 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] | 4228 | // tage and then matches it. Caller-config lists are no longer |
| 4229 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4230 | QCBORDecode_Init(&DCtx, |
| 4231 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4232 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4233 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 4234 | const QCBORTagListIn TL = {2, puList}; |
| 4235 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4236 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4237 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4238 | return -8; |
| 4239 | } |
| 4240 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4241 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 4242 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 4243 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 4244 | Item.val.uCount != 0) { |
| 4245 | return -9; |
| 4246 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4247 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4248 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4249 | // 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] | 4250 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4251 | const uint64_t puLongList[17] = {1,2,1}; |
| 4252 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4253 | QCBORDecode_Init(&DCtx, |
| 4254 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4255 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4256 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 4257 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4258 | return -11; |
| 4259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4260 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4261 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4262 | QCBORTagListOut Out = {0, 4, puTags}; |
| 4263 | |
| 4264 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4265 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4266 | QCBORDecode_Init(&DCtx, |
| 4267 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4268 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4269 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4270 | return -12; |
| 4271 | } |
| 4272 | if(puTags[0] != 0x9192939495969798 || |
| 4273 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4274 | puTags[2] != 0x06 || |
| 4275 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4276 | return -13; |
| 4277 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4278 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4279 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4280 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4281 | QCBORDecode_Init(&DCtx, |
| 4282 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4283 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4284 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 4285 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4286 | return -14; |
| 4287 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4288 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4289 | |
| 4290 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4291 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4292 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 4293 | // It is a bit of a messy test and maybe could be improved, but |
| 4294 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4295 | QCBORDecode_Init(&DCtx, |
| 4296 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4297 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4298 | int n = CheckCSRMaps(&DCtx); |
| 4299 | if(n) { |
| 4300 | return n-2000; |
| 4301 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4302 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4303 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4304 | QCBORDecode_Init(&DCtx, |
| 4305 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4306 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4307 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4308 | /* With the spiffy decode revision, this tag list is not used. |
| 4309 | It doesn't matter if a tag is in this list or not so some |
| 4310 | tests that couldn't process a tag because it isn't in this list |
| 4311 | now can process these unlisted tags. The tests have been |
| 4312 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4313 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 4314 | const QCBORTagListIn TagList = {3, puTagList}; |
| 4315 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4316 | |
| 4317 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4318 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4319 | return -100; |
| 4320 | } |
| 4321 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4322 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4323 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 4324 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 4325 | Item.val.uCount != 2 || |
| 4326 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 4327 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 4328 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 4329 | Out.uNumUsed != 3) { |
| 4330 | return -101; |
| 4331 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4332 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4333 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4334 | return -102; |
| 4335 | } |
| 4336 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4337 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4338 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4339 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4340 | Item.val.uCount != 2 || |
| 4341 | puTags[0] != 5859837686836516696 || |
| 4342 | puTags[1] != 7 || |
| 4343 | Out.uNumUsed != 2) { |
| 4344 | return -103; |
| 4345 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4346 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4347 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4348 | return -104; |
| 4349 | } |
| 4350 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4351 | Item.val.uCount != 5 || |
| 4352 | puTags[0] != 0x0b || |
| 4353 | Out.uNumUsed != 1) { |
| 4354 | return -105; |
| 4355 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4356 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4357 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4358 | return -106; |
| 4359 | } |
| 4360 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4361 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 4362 | Item.val.string.len != 12 || |
| 4363 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 4364 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 4365 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 4366 | Out.uNumUsed != 3) { |
| 4367 | return -105; |
| 4368 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4369 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4370 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4371 | return -107; |
| 4372 | } |
| 4373 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4374 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 4375 | Item.val.string.len != 3 || |
| 4376 | puTags[0] != 773 || |
| 4377 | Out.uNumUsed != 1) { |
| 4378 | return -108; |
| 4379 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4380 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4381 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4382 | return -109; |
| 4383 | } |
| 4384 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4385 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4386 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4387 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4388 | puTags[3] != 7 || |
| 4389 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4390 | return -110; |
| 4391 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4392 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4393 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4394 | return -111; |
| 4395 | } |
| 4396 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4397 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4398 | Item.val.string.len != 9 || |
| 4399 | puTags[0] != 17 || |
| 4400 | Out.uNumUsed != 1) { |
| 4401 | return -112; |
| 4402 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4403 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4404 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4405 | return -111; |
| 4406 | } |
| 4407 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4408 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4409 | Item.val.string.len != 2 || |
| 4410 | puTags[0] != 17 || |
| 4411 | Out.uNumUsed != 1) { |
| 4412 | return -112; |
| 4413 | } |
| 4414 | |
| 4415 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4416 | return -113; |
| 4417 | } |
| 4418 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4419 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4420 | Item.val.uCount != 2 || |
| 4421 | puTags[0] != 19 || |
| 4422 | Out.uNumUsed != 1) { |
| 4423 | return -114; |
| 4424 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4425 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4426 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4427 | return -115; |
| 4428 | } |
| 4429 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4430 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4431 | Item.val.uCount != 1 || |
| 4432 | puTags[0] != 9 || |
| 4433 | Out.uNumUsed != 1) { |
| 4434 | return -116; |
| 4435 | } |
| 4436 | |
| 4437 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4438 | return -116; |
| 4439 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4440 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4441 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4442 | Out.uNumUsed != 0) { |
| 4443 | return -117; |
| 4444 | } |
| 4445 | |
| 4446 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4447 | return -118; |
| 4448 | } |
| 4449 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 4450 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4451 | puTags[0] != 12 || |
| 4452 | Out.uNumUsed != 1) { |
| 4453 | return -119; |
| 4454 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4455 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4456 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4457 | return -120; |
| 4458 | } |
| 4459 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4460 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 4461 | Item.val.uCount != 1 || |
| 4462 | puTags[0] != 0x17 || |
| 4463 | Out.uNumUsed != 1) { |
| 4464 | return -121; |
| 4465 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4466 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4467 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4468 | return -122; |
| 4469 | } |
| 4470 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4471 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4472 | Item.val.int64 != -3 || |
| 4473 | puTags[0] != 8 || |
| 4474 | Out.uNumUsed != 1) { |
| 4475 | return -123; |
| 4476 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4477 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4478 | if(QCBORDecode_Finish(&DCtx)) { |
| 4479 | return -124; |
| 4480 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4481 | |
| 4482 | UsefulBufC DateString; |
| 4483 | QCBORDecode_Init(&DCtx, |
| 4484 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4485 | QCBOR_DECODE_MODE_NORMAL); |
| 4486 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4487 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4488 | // tagged date string |
| 4489 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4490 | // untagged date string |
| 4491 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4492 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4493 | return 100; |
| 4494 | } |
| 4495 | // untagged byte string |
| 4496 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4497 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4498 | return 101; |
| 4499 | } |
| 4500 | // tagged regex |
| 4501 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4502 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4503 | return 102; |
| 4504 | } |
| 4505 | // tagged date string with a byte string |
| 4506 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4507 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4508 | return 103; |
| 4509 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4510 | // The exit errors out because the last item, the date string with |
| 4511 | // bad content makes the array untraversable (the bad date string |
| 4512 | // could have tag content of an array or such that is not consumed |
| 4513 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4514 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4515 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4516 | return 104; |
| 4517 | } |
| 4518 | |
| 4519 | |
| 4520 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4521 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4522 | QCBOR_DECODE_MODE_NORMAL); |
| 4523 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 4524 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) { |
| 4525 | return 200; |
| 4526 | } |
| 4527 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) { |
| 4528 | return 202; |
| 4529 | } |
| 4530 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) { |
| 4531 | return 203; |
| 4532 | } |
| 4533 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4534 | return 204; |
| 4535 | } |
| 4536 | |
| 4537 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 4538 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) { |
| 4539 | return 210; |
| 4540 | } |
| 4541 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) { |
| 4542 | return 212; |
| 4543 | } |
| 4544 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) { |
| 4545 | return 213; |
| 4546 | } |
| 4547 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4548 | return 214; |
| 4549 | } |
| 4550 | |
| 4551 | |
| 4552 | QCBORDecode_Init(&DCtx, |
| 4553 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4554 | QCBOR_DECODE_MODE_NORMAL); |
| 4555 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 4556 | QCBORDecode_EnterMapFromMapN(&DCtx, -23); |
| 4557 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) { |
| 4558 | return 220; |
| 4559 | } |
| 4560 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) { |
| 4561 | return 221; |
| 4562 | } |
| 4563 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) { |
| 4564 | return 222; |
| 4565 | } |
| 4566 | |
| 4567 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 4568 | QCBORDecode_Init(&DCtx, |
| 4569 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4570 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 4571 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 4572 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) { |
| 4573 | return 230; |
| 4574 | } |
| 4575 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) { |
| 4576 | return 231; |
| 4577 | } |
| 4578 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) { |
| 4579 | return 232; |
| 4580 | } |
| 4581 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4582 | return 234; |
| 4583 | } |
| 4584 | int64_t nInt; |
| 4585 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 4586 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) { |
| 4587 | return 240; |
| 4588 | } |
| 4589 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 6) { |
| 4590 | return 241; |
| 4591 | } |
| 4592 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) { |
| 4593 | return 242; |
| 4594 | } |
| 4595 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4596 | return 243; |
| 4597 | } |
| 4598 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4599 | |
| 4600 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4601 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4602 | QCBOR_DECODE_MODE_NORMAL); |
| 4603 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4604 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4605 | // tagged date string |
| 4606 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4607 | // untagged date string |
| 4608 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4609 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4610 | return 250; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4611 | } |
| 4612 | // untagged byte string |
| 4613 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4614 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4615 | return 251; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4616 | } |
| 4617 | // tagged regex |
| 4618 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4619 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4620 | return 252; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4621 | } |
| 4622 | // tagged date string with a byte string |
| 4623 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4624 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4625 | return 253; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4626 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4627 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4628 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4629 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4630 | return 254; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
| 4633 | QCBORDecode_Init(&DCtx, |
| 4634 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4635 | QCBOR_DECODE_MODE_NORMAL); |
| 4636 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4637 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4638 | // tagged date string |
| 4639 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4640 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4641 | return 300; |
| 4642 | } |
| 4643 | // untagged date string |
| 4644 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4645 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4646 | return 301; |
| 4647 | } |
| 4648 | // untagged byte string |
| 4649 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4650 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4651 | return 302; |
| 4652 | } |
| 4653 | // tagged regex |
| 4654 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4655 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4656 | return 303; |
| 4657 | } |
| 4658 | // tagged date string with a byte string |
| 4659 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4660 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4661 | return 304; |
| 4662 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4663 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4664 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4665 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4666 | return 305; |
| 4667 | } |
| 4668 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4669 | return 0; |
| 4670 | } |
| 4671 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4672 | /* |
| 4673 | * These are showing the big numbers converted to integers. |
| 4674 | * The tag numbers are not shown. |
| 4675 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4676 | * [ |
| 4677 | * 18446744073709551616, |
| 4678 | * -18446744073709551617, |
| 4679 | * { |
| 4680 | * -64: -18446744073709551617, |
| 4681 | * 64: 18446744073709551616, |
| 4682 | * "BN+": 18446744073709551616, |
| 4683 | * "BN-": -18446744073709551617 |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4684 | * } |
| 4685 | * ] |
| 4686 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4687 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4688 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4689 | 0x83, |
| 4690 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4691 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4692 | 0xA4, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4693 | 0x38, 0x3F, |
| 4694 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4695 | 0x18, 0x40, |
| 4696 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4697 | 0x63, 0x42, 0x4E, 0x2B, |
| 4698 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4699 | 0x63, 0x42, 0x4E, 0x2D, |
| 4700 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4701 | }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4702 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4703 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4704 | /* The expected big num */ |
| 4705 | static const uint8_t spBigNum[] = { |
| 4706 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4707 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4708 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4709 | |
| 4710 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4711 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4712 | { |
| 4713 | QCBORDecodeContext DCtx; |
| 4714 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4715 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4716 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4717 | QCBORDecode_Init(&DCtx, |
| 4718 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 4719 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4720 | |
| 4721 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4722 | // |
| 4723 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4724 | return -1; |
| 4725 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4726 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4727 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4728 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4729 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4730 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4731 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4732 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4733 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4734 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4735 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | // |
| 4739 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4740 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4741 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4742 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4743 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4744 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4745 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4746 | // |
| 4747 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4748 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4749 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4750 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4751 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4752 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4753 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4754 | return -15; |
| 4755 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4756 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4757 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4758 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4759 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4763 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4764 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4765 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4766 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4767 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4768 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4769 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4770 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4771 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 4772 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4773 | return -9; |
| 4774 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4775 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4776 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
| 4777 | return -10; |
| 4778 | } |
| 4779 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4780 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4781 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4782 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4783 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4784 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4785 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4786 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4787 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4788 | |
| 4789 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4790 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4791 | #else |
| 4792 | |
| 4793 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 4794 | return -100; |
| 4795 | } |
| 4796 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4797 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4798 | return 0; |
| 4799 | } |
| 4800 | |
| 4801 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4802 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4803 | uint8_t uDataType, |
| 4804 | uint8_t uNestingLevel, |
| 4805 | uint8_t uNextNest, |
| 4806 | int64_t nLabel, |
| 4807 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4808 | { |
| 4809 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4810 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4811 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4812 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 4813 | if(Item.uDataType != uDataType) return -1; |
| 4814 | if(uNestingLevel > 0) { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4815 | if(Item.uLabelType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4816 | return -1; |
| 4817 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4818 | if(Item.label.int64 != nLabel) { |
| 4819 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4820 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4821 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4822 | } |
| 4823 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4824 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4825 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4826 | if(pItem) { |
| 4827 | *pItem = Item; |
| 4828 | } |
| 4829 | return 0; |
| 4830 | } |
| 4831 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4832 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4833 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4834 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4835 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4836 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4837 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4838 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4839 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4840 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4841 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 4842 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 4843 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 4844 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 4845 | 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] | 4846 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4847 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 4848 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4849 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4850 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 4851 | 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] | 4852 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4853 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 4854 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4855 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4856 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4857 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4858 | return 0; |
| 4859 | } |
| 4860 | |
| 4861 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4862 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4863 | { |
| 4864 | -23: { |
| 4865 | -20: { |
| 4866 | -18: "Organization", |
| 4867 | -17: "SSG", |
| 4868 | -15: "Confusion", |
| 4869 | -16: "San Diego", |
| 4870 | -14: "US" |
| 4871 | }, |
| 4872 | -19: { |
| 4873 | -11: { |
| 4874 | -9: -7 |
| 4875 | }, |
| 4876 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 4877 | } |
| 4878 | }, |
| 4879 | -22: { |
| 4880 | -5: -3 |
| 4881 | } |
| 4882 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4883 | */ |
| 4884 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4885 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 4886 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4887 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4888 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4889 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4890 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4891 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 4892 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 4893 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 4894 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4895 | // Same map as above, but using indefinite lengths |
| 4896 | static const uint8_t spCSRInputIndefLen[] = { |
| 4897 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 4898 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4899 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4900 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4901 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4902 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4903 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 4904 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 4905 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 4906 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 4907 | |
| 4908 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4909 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4910 | { |
| 4911 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4912 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4913 | QCBORDecode_Init(&DCtx, |
| 4914 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4915 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4916 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4917 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4918 | } |
| 4919 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4920 | |
| 4921 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4922 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4923 | { |
| 4924 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4925 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4926 | QCBORDecode_Init(&DCtx, |
| 4927 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4928 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4929 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4930 | QCBORItem Item; |
| 4931 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4932 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4933 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4934 | return -1; |
| 4935 | } |
| 4936 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4937 | return -2; |
| 4938 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4939 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4940 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4941 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 4942 | return -3; |
| 4943 | } |
| 4944 | |
| 4945 | return 0; |
| 4946 | } |
| 4947 | |
| 4948 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4949 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4950 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4951 | { |
| 4952 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4953 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4954 | QCBORDecode_Init(&DCtx, |
| 4955 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 4956 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4957 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4958 | return CheckCSRMaps(&DCtx); |
| 4959 | } |
| 4960 | |
| 4961 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4962 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4963 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 4964 | { |
| 4965 | UsefulOutBuf UOB; |
| 4966 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4967 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4968 | int i; |
| 4969 | for(i = 0; i < n; i++) { |
| 4970 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 4971 | } |
| 4972 | |
| 4973 | for(i = 0; i < n; i++) { |
| 4974 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4975 | } |
| 4976 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4977 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4978 | |
| 4979 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4980 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4981 | { |
| 4982 | QCBORDecodeContext DC; |
| 4983 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4984 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4985 | int j; |
| 4986 | for(j = 0; j < nNestLevel; j++) { |
| 4987 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4988 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4989 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 4990 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4991 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4992 | return -4; |
| 4993 | } else { |
| 4994 | return 0; // Decoding doesn't recover after an error |
| 4995 | } |
| 4996 | } else { |
| 4997 | // Should be no error |
| 4998 | if(nReturn) { |
| 4999 | return -9; // Should not have got an error |
| 5000 | } |
| 5001 | } |
| 5002 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5003 | return -7; |
| 5004 | } |
| 5005 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5006 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5007 | if(nReturn) { |
| 5008 | return -3; |
| 5009 | } |
| 5010 | return 0; |
| 5011 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5012 | |
| 5013 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5014 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5015 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 5016 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5017 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5018 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 5019 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5020 | int nReturn = parse_indeflen_nested(Nested, i); |
| 5021 | if(nReturn) { |
| 5022 | return nReturn; |
| 5023 | } |
| 5024 | } |
| 5025 | return 0; |
| 5026 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5027 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5028 | // [1, [2, 3]] |
| 5029 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 5030 | // No closing break |
| 5031 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 5032 | // Not enough closing breaks |
| 5033 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 5034 | // Too many closing breaks |
| 5035 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 5036 | // Unclosed indeflen inside def len |
| 5037 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 5038 | // confused tag |
| 5039 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5040 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5041 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5042 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5043 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5044 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5045 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5046 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5047 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5048 | QCBORDecodeContext DC; |
| 5049 | QCBORItem Item; |
| 5050 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5051 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5052 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5053 | |
| 5054 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 5055 | Item.uNestingLevel != 0 || |
| 5056 | Item.uNextNestLevel != 1) { |
| 5057 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5058 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5059 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5060 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5061 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 5062 | Item.uNestingLevel != 1 || |
| 5063 | Item.uNextNestLevel != 1) { |
| 5064 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5065 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5066 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5067 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5068 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 5069 | Item.uNestingLevel != 1 || |
| 5070 | Item.uNextNestLevel != 2) { |
| 5071 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5072 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5073 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5074 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 5075 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5076 | Item.uNestingLevel != 2 || |
| 5077 | Item.uNextNestLevel != 2) { |
| 5078 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5079 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5080 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5081 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 5082 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5083 | Item.uNestingLevel != 2 || |
| 5084 | Item.uNextNestLevel != 0) { |
| 5085 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5086 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5087 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5088 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5089 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5090 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5091 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5092 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5093 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5094 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5095 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5096 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5097 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5098 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5099 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5100 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5101 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5102 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5103 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5104 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5105 | } |
| 5106 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5107 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5108 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5109 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5110 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5111 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5112 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5113 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5114 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5115 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5119 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5120 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5121 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5122 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5123 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5124 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5125 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5126 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5127 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5128 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5129 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5130 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5131 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5132 | |
| 5133 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5134 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5135 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5136 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5137 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5138 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5139 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5140 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5141 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5142 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5143 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5144 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 5145 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5146 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5147 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5148 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 5149 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5150 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 5151 | return -140; |
| 5152 | } |
| 5153 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5154 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5155 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5156 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5157 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5158 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5159 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5160 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5161 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5162 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5163 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5164 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5165 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5166 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5167 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5168 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5169 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5170 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5171 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5172 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5173 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5174 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5175 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5176 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5177 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5178 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5179 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5180 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5181 | |
| 5182 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5183 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5184 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5185 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5186 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5187 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5188 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 5189 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5190 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5191 | #else /* QCBOR_DISABLE_TAGS */ |
| 5192 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 5193 | return -20; |
| 5194 | } |
| 5195 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5196 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5197 | return 0; |
| 5198 | } |
| 5199 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5200 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5201 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 5202 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5203 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5204 | 0x81, // Array of length one |
| 5205 | 0x7f, // text string marked with indefinite length |
| 5206 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5207 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5208 | 0xff // ending break |
| 5209 | }; |
| 5210 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5211 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5212 | 0x81, // Array of length one |
| 5213 | 0x7f, // text string marked with indefinite length |
| 5214 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5215 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 5216 | 0xff // ending break |
| 5217 | }; |
| 5218 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5219 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5220 | 0x81, // Array of length one |
| 5221 | 0x7f, // text string marked with indefinite length |
| 5222 | 0x01, 0x02, // Not a string |
| 5223 | 0xff // ending break |
| 5224 | }; |
| 5225 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5226 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5227 | 0x81, // Array of length one |
| 5228 | 0x7f, // text string marked with indefinite length |
| 5229 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5230 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5231 | // missing end of string |
| 5232 | }; |
| 5233 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5234 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5235 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5236 | 0xa1, // Array of length one |
| 5237 | 0x7f, // text string marked with indefinite length |
| 5238 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 5239 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5240 | 0xff, // ending break |
| 5241 | 0x01 // integer being labeled. |
| 5242 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5243 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5244 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5245 | /** |
| 5246 | Make an indefinite length string |
| 5247 | |
| 5248 | @param Storage Storage for string, must be 144 bytes in size |
| 5249 | @return The indefinite length string |
| 5250 | |
| 5251 | This makes an array with one indefinite length string that has 7 chunks |
| 5252 | from size of 1 byte up to 64 bytes. |
| 5253 | */ |
| 5254 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5255 | { |
| 5256 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5257 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5258 | UsefulOutBuf_Init(&UOB, Storage); |
| 5259 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 5260 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5261 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5262 | uint8_t uStringByte = 0; |
| 5263 | // Use of type int is intentional |
| 5264 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 5265 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5266 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5267 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 5268 | for(int j = 0; j < uChunkSize; j++) { |
| 5269 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 5270 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5271 | } |
| 5272 | } |
| 5273 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 5274 | |
| 5275 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5276 | } |
| 5277 | |
| 5278 | static int CheckBigString(UsefulBufC BigString) |
| 5279 | { |
| 5280 | if(BigString.len != 255) { |
| 5281 | return 1; |
| 5282 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5283 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5284 | for(uint8_t i = 0; i < 255; i++){ |
| 5285 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 5286 | return 1; |
| 5287 | } |
| 5288 | } |
| 5289 | return 0; |
| 5290 | } |
| 5291 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5292 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5293 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5294 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5295 | QCBORDecodeContext DC; |
| 5296 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5297 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5298 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5299 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5300 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5301 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5302 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5303 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5304 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5305 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5306 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5307 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5308 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5309 | return -2; |
| 5310 | } |
| 5311 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 5312 | return -3; |
| 5313 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5314 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5315 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5316 | return -4; |
| 5317 | } |
| 5318 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 5319 | return -5; |
| 5320 | } |
| 5321 | if(QCBORDecode_Finish(&DC)) { |
| 5322 | return -6; |
| 5323 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5324 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5325 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5326 | QCBORDecode_Init(&DC, |
| 5327 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 5328 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5329 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5330 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5331 | return -7; |
| 5332 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5333 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5334 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5335 | return -8; |
| 5336 | } |
| 5337 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5338 | return -9; |
| 5339 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5340 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5341 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5342 | return -10; |
| 5343 | } |
| 5344 | |
| 5345 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5346 | QCBORDecode_Init(&DC, |
| 5347 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 5348 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5349 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5350 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5351 | return -11; |
| 5352 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5353 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5354 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5355 | return -12; |
| 5356 | } |
| 5357 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5358 | return -13; |
| 5359 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5360 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5361 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5362 | return -14; |
| 5363 | } |
| 5364 | |
| 5365 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5366 | QCBORDecode_Init(&DC, |
| 5367 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 5368 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5369 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5370 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5371 | return -15; |
| 5372 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5373 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5374 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5375 | return -16; |
| 5376 | } |
| 5377 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5378 | return -17; |
| 5379 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5380 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5381 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 5382 | return -18; |
| 5383 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5384 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5385 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5386 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5387 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5388 | QCBORDecode_GetNext(&DC, &Item); |
| 5389 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5390 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5391 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5392 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5393 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5394 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5395 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5396 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5397 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5398 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5399 | |
| 5400 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 5401 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5402 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5403 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5404 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5405 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 5406 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 5407 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5408 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5409 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 5410 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5411 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5412 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5413 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5414 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5415 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5416 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5417 | QCBORDecode_GetNext(&DC, &Item); |
| 5418 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5419 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5420 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5421 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5422 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5423 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5424 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5425 | // ---- big bstr ----- |
| 5426 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5427 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5428 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5429 | return -25; |
| 5430 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5431 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5432 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5433 | return -26; |
| 5434 | } |
| 5435 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5436 | return -26; |
| 5437 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5438 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5439 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5440 | return -27; |
| 5441 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5442 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5443 | return -28; |
| 5444 | } |
| 5445 | if(CheckBigString(Item.val.string)) { |
| 5446 | return -3; |
| 5447 | } |
| 5448 | if(QCBORDecode_Finish(&DC)) { |
| 5449 | return -29; |
| 5450 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5451 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5452 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5453 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5454 | 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] | 5455 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5456 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5457 | return -30; |
| 5458 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5459 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5460 | QCBORDecode_GetNext(&DC, &Item); |
| 5461 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 5462 | return -31; |
| 5463 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5464 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5465 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 5466 | return -32; |
| 5467 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5468 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5469 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5470 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 5471 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 5472 | return -33; |
| 5473 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5474 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5475 | if(QCBORDecode_Finish(&DC)) { |
| 5476 | return -34; |
| 5477 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5478 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5479 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5480 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5481 | } |
| 5482 | |
| 5483 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5484 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5485 | { |
| 5486 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5487 | QCBORError nCBORError; |
| 5488 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5489 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5490 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5491 | QCBORDecode_Init(&DC, |
| 5492 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 5493 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5494 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5495 | 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] | 5496 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5497 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 5498 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5499 | return -1; |
| 5500 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5501 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5502 | if(CheckCSRMaps(&DC)) { |
| 5503 | return -2; |
| 5504 | } |
| 5505 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5506 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5507 | // Next parse, save pointers to a few strings, destroy original and |
| 5508 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5509 | 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] | 5510 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5511 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5512 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 5513 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5514 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5515 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5516 | QCBORItem Item1, Item2, Item3, Item4; |
| 5517 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5518 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5519 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5520 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5521 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5522 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5523 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5524 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5525 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5526 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5527 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5528 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5529 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5530 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 5531 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5532 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5533 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5534 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 5535 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5536 | Item1.uDataAlloc != 0 || |
| 5537 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5538 | UsefulBufCompareToSZ(Item1.label.string, "first integer") || |
| 5539 | Item1.label.string.ptr < Pool.ptr || |
| 5540 | 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] | 5541 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5542 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5543 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5544 | |
| 5545 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5546 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5547 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5548 | Item2.uDataAlloc != 0 || |
| 5549 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5550 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5551 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5552 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5553 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5554 | Item3.uDataAlloc == 0 || |
| 5555 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5556 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5557 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5558 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5559 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5560 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5561 | Item4.uDataAlloc == 0 || |
| 5562 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5563 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5564 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5565 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5566 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5567 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5568 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5569 | QCBORDecode_Init(&DC, |
| 5570 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 5571 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5572 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 5573 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5574 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5575 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5576 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5577 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5578 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5579 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 5580 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 5581 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 5582 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 5583 | } |
| 5584 | } |
| 5585 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5586 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5587 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5588 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5589 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5590 | |
| 5591 | return 0; |
| 5592 | } |
| 5593 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5594 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5595 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 5596 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5597 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5598 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5599 | QCBORDecodeContext DC; |
| 5600 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 5601 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5602 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5603 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5604 | // Then fish into the internals of the decode context |
| 5605 | // to get the allocator function so it can be called directly. |
| 5606 | // Also figure out how much pool is available for use |
| 5607 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5608 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5609 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 5610 | if(nError) { |
| 5611 | return -9; |
| 5612 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5613 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 5614 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5615 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5616 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5617 | // First test -- ask for one more byte than available and see failure |
| 5618 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5619 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 5620 | return -1; |
| 5621 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5622 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5623 | // Re do the set up for the next test that will do a successful alloc, |
| 5624 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5625 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5626 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5627 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5628 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5629 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5630 | // Allocate one byte less than available and see success |
| 5631 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5632 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5633 | return -2; |
| 5634 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5635 | // Ask for some more and see failure |
| 5636 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5637 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 5638 | return -3; |
| 5639 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5640 | // Free the first allocate, retry the second and see success |
| 5641 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 5642 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5643 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 5644 | return -4; |
| 5645 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5646 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5647 | // Re do set up for next test that involves a successful alloc, |
| 5648 | // and a successful realloc and a failed realloc |
| 5649 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5650 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5651 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5652 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5653 | // Allocate half the pool and see success |
| 5654 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5655 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5656 | return -5; |
| 5657 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5658 | // Reallocate to take up the whole pool and see success |
| 5659 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5660 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 5661 | return -6; |
| 5662 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5663 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5664 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 5665 | return -7; |
| 5666 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5667 | // Try to allocate more to be sure there is failure after a realloc |
| 5668 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 5669 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5670 | return -8; |
| 5671 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5672 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5673 | return 0; |
| 5674 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 5675 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5676 | |
| 5677 | /* Just enough of an allocator to test configuration of one */ |
| 5678 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 5679 | { |
| 5680 | (void)pOldMem; // unused variable |
| 5681 | |
| 5682 | if(uNewSize) { |
| 5683 | // Assumes the context pointer is the buffer and |
| 5684 | // nothing too big will ever be asked for. |
| 5685 | // This is only good for this basic test! |
| 5686 | return (UsefulBuf) {pCtx, uNewSize}; |
| 5687 | } else { |
| 5688 | return NULLUsefulBuf; |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5693 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5694 | { |
| 5695 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5696 | // nothing can be done with it unless that is set up. |
| 5697 | QCBORDecodeContext DC; |
| 5698 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 5699 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 5700 | |
| 5701 | uint8_t pAllocatorBuffer[50]; |
| 5702 | |
| 5703 | // This is really just to test that this call works. |
| 5704 | // The full functionality of string allocators is tested |
| 5705 | // elsewhere with the MemPool internal allocator. |
| 5706 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 5707 | |
| 5708 | QCBORItem Item; |
| 5709 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 5710 | return -1; |
| 5711 | } |
| 5712 | |
| 5713 | if(Item.uDataAlloc == 0 || |
| 5714 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 5715 | Item.val.string.ptr != pAllocatorBuffer) { |
| 5716 | return -2; |
| 5717 | } |
| 5718 | |
| 5719 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 5720 | return -3; |
| 5721 | } |
| 5722 | |
| 5723 | return 0; |
| 5724 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5725 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5726 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5727 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5728 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5729 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5730 | struct EaMTest { |
| 5731 | const char *szName; |
| 5732 | UsefulBufC Input; |
| 5733 | uint8_t uTagRequirement; |
| 5734 | bool bHasTags; |
| 5735 | |
| 5736 | /* Expected values for GetNext */ |
| 5737 | QCBORError uExpectedErrorGN; |
| 5738 | uint8_t uQCBORTypeGN; |
| 5739 | int64_t nExponentGN; |
| 5740 | int64_t nMantissaGN; |
| 5741 | UsefulBufC MantissaGN; |
| 5742 | |
| 5743 | /* Expected values for GetDecimalFraction */ |
| 5744 | QCBORError uExpectedErrorGDF; |
| 5745 | int64_t nExponentGDF; |
| 5746 | int64_t nMantissaGDF; |
| 5747 | |
| 5748 | /* Expected values for GetDecimalFractionBig */ |
| 5749 | QCBORError uExpectedErrorGDFB; |
| 5750 | int64_t nExponentGDFB; |
| 5751 | UsefulBufC MantissaGDFB; |
| 5752 | bool IsNegativeGDFB; |
| 5753 | |
| 5754 | /* Expected values for GetBigFloat */ |
| 5755 | QCBORError uExpectedErrorGBF; |
| 5756 | int64_t nExponentGBF; |
| 5757 | int64_t nMantissaGBF; |
| 5758 | |
| 5759 | /* Expected values for GetBigFloatBig */ |
| 5760 | QCBORError uExpectedErrorGBFB; |
| 5761 | int64_t nExponentGBFB; |
| 5762 | UsefulBufC MantissaGBFB; |
| 5763 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5764 | }; |
| 5765 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5766 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5767 | |
| 5768 | static const struct EaMTest pEaMTests[] = { |
| 5769 | { |
| 5770 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 5771 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5772 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 5773 | false, |
| 5774 | |
| 5775 | QCBOR_SUCCESS, /* for GetNext */ |
| 5776 | QCBOR_TYPE_ARRAY, |
| 5777 | 0, |
| 5778 | 0, |
| 5779 | {(const uint8_t []){0x00}, 1}, |
| 5780 | |
| 5781 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 5782 | -1, |
| 5783 | 3, |
| 5784 | |
| 5785 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5786 | -1, |
| 5787 | {(const uint8_t []){0x03}, 1}, |
| 5788 | false, |
| 5789 | |
| 5790 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5791 | -1, |
| 5792 | 3, |
| 5793 | |
| 5794 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5795 | -1, |
| 5796 | {(const uint8_t []){0x03}, 1}, |
| 5797 | false |
| 5798 | }, |
| 5799 | |
| 5800 | { |
| 5801 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 5802 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5803 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5804 | false, |
| 5805 | |
| 5806 | QCBOR_SUCCESS, /* for GetNext */ |
| 5807 | QCBOR_TYPE_ARRAY, |
| 5808 | 0, |
| 5809 | 0, |
| 5810 | {(const uint8_t []){0x00}, 1}, |
| 5811 | |
| 5812 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5813 | 0, |
| 5814 | 0, |
| 5815 | |
| 5816 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5817 | 0, |
| 5818 | {(const uint8_t []){0x00}, 1}, |
| 5819 | false, |
| 5820 | |
| 5821 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5822 | 0, |
| 5823 | 0, |
| 5824 | |
| 5825 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5826 | 0, |
| 5827 | {(const uint8_t []){0x00}, 1}, |
| 5828 | false |
| 5829 | |
| 5830 | }, |
| 5831 | |
| 5832 | { |
| 5833 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 5834 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 5835 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5836 | true, |
| 5837 | |
| 5838 | QCBOR_SUCCESS, /* for GetNext */ |
| 5839 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 5840 | -1, |
| 5841 | 3, |
| 5842 | {(const uint8_t []){0x00}, 1}, |
| 5843 | |
| 5844 | |
| 5845 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 5846 | -1, |
| 5847 | 3, |
| 5848 | |
| 5849 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5850 | -1, |
| 5851 | {(const uint8_t []){0x03}, 1}, |
| 5852 | false, |
| 5853 | |
| 5854 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5855 | 0, |
| 5856 | 0, |
| 5857 | |
| 5858 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5859 | 0, |
| 5860 | {(const uint8_t []){0x00}, 1}, |
| 5861 | false |
| 5862 | }, |
| 5863 | { |
| 5864 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 5865 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 5866 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5867 | true, |
| 5868 | |
| 5869 | QCBOR_SUCCESS, /* for GetNext */ |
| 5870 | QCBOR_TYPE_BIGFLOAT, |
| 5871 | 300, |
| 5872 | 100, |
| 5873 | {(const uint8_t []){0x00}, 1}, |
| 5874 | |
| 5875 | |
| 5876 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5877 | 0, |
| 5878 | 0, |
| 5879 | |
| 5880 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5881 | 0, |
| 5882 | {(const uint8_t []){0x03}, 1}, |
| 5883 | false, |
| 5884 | |
| 5885 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5886 | 300, |
| 5887 | 100, |
| 5888 | |
| 5889 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5890 | 300, |
| 5891 | {(const uint8_t []){0x64}, 1}, |
| 5892 | false |
| 5893 | }, |
| 5894 | |
| 5895 | { |
| 5896 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 5897 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5898 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 5899 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5900 | true, |
| 5901 | |
| 5902 | QCBOR_SUCCESS, /* for GetNext */ |
| 5903 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5904 | -20, |
| 5905 | 0, |
| 5906 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5907 | |
| 5908 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 5909 | 0, |
| 5910 | 0, |
| 5911 | |
| 5912 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5913 | -20, |
| 5914 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5915 | false, |
| 5916 | |
| 5917 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5918 | 0, |
| 5919 | 0, |
| 5920 | |
| 5921 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5922 | 0, |
| 5923 | {(const uint8_t []){0x00}, 0}, |
| 5924 | false |
| 5925 | }, |
| 5926 | |
| 5927 | { |
| 5928 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 5929 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5930 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 5931 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5932 | true, |
| 5933 | |
| 5934 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 5935 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5936 | 0, |
| 5937 | 0, |
| 5938 | {(const uint8_t []){0x00}, 0}, |
| 5939 | |
| 5940 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 5941 | 0, |
| 5942 | 0, |
| 5943 | |
| 5944 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 5945 | 0, |
| 5946 | {(const uint8_t []){0x00}, 0}, |
| 5947 | false, |
| 5948 | |
| 5949 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 5950 | 0, |
| 5951 | 0, |
| 5952 | |
| 5953 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 5954 | 0, |
| 5955 | {(const uint8_t []){0x00}, 0}, |
| 5956 | false |
| 5957 | }, |
| 5958 | { |
| 5959 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 5960 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 5961 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5962 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5963 | true, |
| 5964 | |
| 5965 | QCBOR_SUCCESS, /* for GetNext */ |
| 5966 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5967 | -20, |
| 5968 | 0, |
| 5969 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5970 | |
| 5971 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5972 | 0, |
| 5973 | 0, |
| 5974 | |
| 5975 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5976 | -20, |
| 5977 | {(const uint8_t []){0x00}, 1}, |
| 5978 | false, |
| 5979 | |
| 5980 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5981 | -20, |
| 5982 | 4294967295, |
| 5983 | |
| 5984 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5985 | -20, |
| 5986 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5987 | false |
| 5988 | }, |
| 5989 | |
| 5990 | { |
| 5991 | /* Special case for test 8. Don't renumber it. */ |
| 5992 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 5993 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 5994 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5995 | true, |
| 5996 | |
| 5997 | QCBOR_SUCCESS, /* for GetNext */ |
| 5998 | QCBOR_TYPE_ARRAY, |
| 5999 | 0, |
| 6000 | 0, |
| 6001 | {(const uint8_t []){0x00}, 1}, |
| 6002 | |
| 6003 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 6004 | 0, |
| 6005 | 0, |
| 6006 | |
| 6007 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 6008 | -20, |
| 6009 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 6010 | false, |
| 6011 | |
| 6012 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 6013 | 0, |
| 6014 | 0, |
| 6015 | |
| 6016 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 6017 | -20, |
| 6018 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 6019 | false |
| 6020 | }, |
| 6021 | |
| 6022 | { |
| 6023 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 6024 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 6025 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 6026 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 6027 | true, |
| 6028 | |
| 6029 | QCBOR_SUCCESS, /* for GetNext */ |
| 6030 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 6031 | 9223372036854775807, |
| 6032 | 0, |
| 6033 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 6034 | |
| 6035 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 6036 | 0, |
| 6037 | 0, |
| 6038 | |
| 6039 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 6040 | 9223372036854775807, |
| 6041 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 6042 | true, |
| 6043 | |
| 6044 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 6045 | 0, |
| 6046 | 0, |
| 6047 | |
| 6048 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 6049 | 0, |
| 6050 | {(const uint8_t []){0x00}, 1}, |
| 6051 | false |
| 6052 | }, |
| 6053 | }; |
| 6054 | |
| 6055 | |
| 6056 | |
| 6057 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6058 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6059 | size_t uIndex; |
| 6060 | QCBORDecodeContext DCtx; |
| 6061 | QCBORItem Item; |
| 6062 | QCBORError uError; |
| 6063 | int64_t nMantissa, nExponent; |
| 6064 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 6065 | UsefulBufC Mantissa; |
| 6066 | bool bMantissaIsNegative; |
| 6067 | |
| 6068 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 6069 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 6070 | /* Decode with GetNext */ |
| 6071 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6072 | |
| 6073 | if(uIndex + 1 == 9) { |
| 6074 | nExponent = 99; // just to set a break point |
| 6075 | } |
| 6076 | |
| 6077 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6078 | #ifdef QCBOR_DISABLE_TAGS |
| 6079 | /* Test 8 is a special case when tags are disabled */ |
| 6080 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 6081 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6082 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 6083 | } |
| 6084 | } else { |
| 6085 | #endif |
| 6086 | /* Now check return code, data type, mantissa and exponent */ |
| 6087 | if(pT->uExpectedErrorGN != uError) { |
| 6088 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 6089 | } |
| 6090 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 6091 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 6092 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 6093 | } |
| 6094 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 6095 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 6096 | } |
| 6097 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 6098 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 6099 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 6100 | } |
| 6101 | } else { |
| 6102 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 6103 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 6104 | } |
| 6105 | } |
| 6106 | } |
| 6107 | #ifdef QCBOR_DISABLE_TAGS |
| 6108 | } |
| 6109 | #endif |
| 6110 | |
| 6111 | /* Decode with GetDecimalFraction */ |
| 6112 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6113 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 6114 | pT->uTagRequirement, |
| 6115 | &nMantissa, |
| 6116 | &nExponent); |
| 6117 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6118 | #ifdef QCBOR_DISABLE_TAGS |
| 6119 | if(pT->bHasTags) { |
| 6120 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6121 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 6122 | } |
| 6123 | } else { |
| 6124 | #endif |
| 6125 | /* Now check return code, mantissa and exponent */ |
| 6126 | if(pT->uExpectedErrorGDF != uError) { |
| 6127 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 6128 | } |
| 6129 | if(uError == QCBOR_SUCCESS) { |
| 6130 | if(pT->nExponentGDF != nExponent) { |
| 6131 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 6132 | } |
| 6133 | if(pT->nMantissaGDF != nMantissa) { |
| 6134 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 6135 | } |
| 6136 | } |
| 6137 | #ifdef QCBOR_DISABLE_TAGS |
| 6138 | } |
| 6139 | #endif |
| 6140 | |
| 6141 | /* Decode with GetDecimalFractionBig */ |
| 6142 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6143 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 6144 | pT->uTagRequirement, |
| 6145 | MantissaBuf, |
| 6146 | &Mantissa, |
| 6147 | &bMantissaIsNegative, |
| 6148 | &nExponent); |
| 6149 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6150 | #ifdef QCBOR_DISABLE_TAGS |
| 6151 | if(pT->bHasTags) { |
| 6152 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6153 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 6154 | } |
| 6155 | } else { |
| 6156 | #endif |
| 6157 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 6158 | if(pT->uExpectedErrorGDFB != uError) { |
| 6159 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 6160 | } |
| 6161 | if(uError == QCBOR_SUCCESS) { |
| 6162 | if(pT->nExponentGDFB != nExponent) { |
| 6163 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 6164 | } |
| 6165 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 6166 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 6167 | } |
| 6168 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 6169 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 6170 | } |
| 6171 | } |
| 6172 | #ifdef QCBOR_DISABLE_TAGS |
| 6173 | } |
| 6174 | #endif |
| 6175 | |
| 6176 | /* Decode with GetBigFloat */ |
| 6177 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6178 | QCBORDecode_GetBigFloat(&DCtx, |
| 6179 | pT->uTagRequirement, |
| 6180 | &nMantissa, |
| 6181 | &nExponent); |
| 6182 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6183 | #ifdef QCBOR_DISABLE_TAGS |
| 6184 | if(pT->bHasTags) { |
| 6185 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6186 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 6187 | } |
| 6188 | } else { |
| 6189 | #endif |
| 6190 | /* Now check return code, mantissa and exponent */ |
| 6191 | if(pT->uExpectedErrorGBF != uError) { |
| 6192 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 6193 | } |
| 6194 | if(uError == QCBOR_SUCCESS) { |
| 6195 | if(pT->nExponentGBF != nExponent) { |
| 6196 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 6197 | } |
| 6198 | if(pT->nMantissaGBF != nMantissa) { |
| 6199 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 6200 | } |
| 6201 | } |
| 6202 | #ifdef QCBOR_DISABLE_TAGS |
| 6203 | } |
| 6204 | #endif |
| 6205 | |
| 6206 | /* Decode with GetBigFloatBig */ |
| 6207 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6208 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 6209 | pT->uTagRequirement, |
| 6210 | MantissaBuf, |
| 6211 | &Mantissa, |
| 6212 | &bMantissaIsNegative, |
| 6213 | &nExponent); |
| 6214 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6215 | #ifdef QCBOR_DISABLE_TAGS |
| 6216 | if(pT->bHasTags) { |
| 6217 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6218 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 6219 | } |
| 6220 | } else { |
| 6221 | #endif |
| 6222 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 6223 | if(pT->uExpectedErrorGBFB != uError) { |
| 6224 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 6225 | } |
| 6226 | if(uError == QCBOR_SUCCESS) { |
| 6227 | if(pT->nExponentGBFB != nExponent) { |
| 6228 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 6229 | } |
| 6230 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 6231 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 6232 | } |
| 6233 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 6234 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 6235 | } |
| 6236 | } |
| 6237 | #ifdef QCBOR_DISABLE_TAGS |
| 6238 | } |
| 6239 | #endif |
| 6240 | } |
| 6241 | |
| 6242 | return 0; |
| 6243 | } |
| 6244 | |
| 6245 | |
| 6246 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 6247 | { |
| 6248 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6249 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6250 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6251 | QCBORItem item; |
| 6252 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 6253 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 6254 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 6255 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6256 | |
| 6257 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6258 | |
| 6259 | /* Now encode some stuff and then decode it */ |
| 6260 | uint8_t pBuf[40]; |
| 6261 | QCBOREncodeContext EC; |
| 6262 | UsefulBufC Encoded; |
| 6263 | |
| 6264 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 6265 | QCBOREncode_OpenArray(&EC); |
| 6266 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 6267 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 6268 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 6269 | QCBOREncode_CloseArray(&EC); |
| 6270 | QCBOREncode_Finish(&EC, &Encoded); |
| 6271 | |
| 6272 | |
| 6273 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6274 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6275 | if(uErr != QCBOR_SUCCESS) { |
| 6276 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6277 | } |
| 6278 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6279 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6280 | if(uErr != QCBOR_SUCCESS) { |
| 6281 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 6285 | item.val.expAndMantissa.nExponent != 1000 || |
| 6286 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6287 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6288 | } |
| 6289 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6290 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6291 | if(uErr != QCBOR_SUCCESS) { |
| 6292 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6293 | } |
| 6294 | |
| 6295 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 6296 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 6297 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6298 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6299 | } |
| 6300 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6301 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6302 | if(uErr != QCBOR_SUCCESS) { |
| 6303 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6304 | } |
| 6305 | |
| 6306 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 6307 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 6308 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6309 | return 106; |
| 6310 | } |
| 6311 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6312 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6313 | |
| 6314 | return 0; |
| 6315 | } |
| 6316 | |
| 6317 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6318 | int32_t ExponentAndMantissaDecodeTests(void) |
| 6319 | { |
| 6320 | int32_t rv = ProcessEaMTests(); |
| 6321 | if(rv) { |
| 6322 | return rv; |
| 6323 | } |
| 6324 | |
| 6325 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 6326 | } |
| 6327 | |
| 6328 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6329 | static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = { |
| 6330 | { "Exponent > INT64_MAX", |
| 6331 | QCBOR_DECODE_MODE_NORMAL, |
| 6332 | {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20}, |
| 6333 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6334 | }, |
| 6335 | { "Mantissa > INT64_MAX", |
| 6336 | QCBOR_DECODE_MODE_NORMAL, |
| 6337 | {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23}, |
| 6338 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6339 | }, |
| 6340 | { |
| 6341 | "End of input", |
| 6342 | QCBOR_DECODE_MODE_NORMAL, |
| 6343 | {"\xC4\x82", 2}, |
| 6344 | QCBOR_ERR_NO_MORE_ITEMS |
| 6345 | }, |
| 6346 | {"bad content for big num", |
| 6347 | QCBOR_DECODE_MODE_NORMAL, |
| 6348 | {"\xC4\x82\x01\xc3\x01", 5}, |
| 6349 | QCBOR_ERR_BAD_OPT_TAG |
| 6350 | }, |
| 6351 | {"bad content for big num", |
| 6352 | QCBOR_DECODE_MODE_NORMAL, |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 6353 | {"\xC4\x82\xC2\x01\x1F", 5}, |
| 6354 | QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6355 | }, |
| 6356 | {"Bad integer for exponent", |
| 6357 | QCBOR_DECODE_MODE_NORMAL, |
| 6358 | {"\xC4\x82\x01\x1f", 4}, |
| 6359 | QCBOR_ERR_BAD_INT |
| 6360 | }, |
| 6361 | {"Bad integer for mantissa", |
| 6362 | QCBOR_DECODE_MODE_NORMAL, |
| 6363 | {"\xC4\x82\x1f\x01", 4}, |
| 6364 | QCBOR_ERR_BAD_INT |
| 6365 | }, |
| 6366 | {"3 items in array", |
| 6367 | QCBOR_DECODE_MODE_NORMAL, |
| 6368 | {"\xC4\x83\x03\x01\x02", 5}, |
| 6369 | QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6370 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6371 | {"unterminated indefinite length array", |
| 6372 | QCBOR_DECODE_MODE_NORMAL, |
| 6373 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6374 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6375 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6376 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6377 | {"unterminated indefinite length array", |
| 6378 | QCBOR_DECODE_MODE_NORMAL, |
| 6379 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6380 | QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 6381 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6382 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6383 | {"Empty array", |
| 6384 | QCBOR_DECODE_MODE_NORMAL, |
| 6385 | {"\xC4\x80", 2}, |
| 6386 | QCBOR_ERR_NO_MORE_ITEMS |
| 6387 | }, |
| 6388 | {"Second is not an integer", |
| 6389 | QCBOR_DECODE_MODE_NORMAL, |
| 6390 | {"\xC4\x82\x03\x40", 4}, |
| 6391 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6392 | }, |
| 6393 | {"First is not an integer", |
| 6394 | QCBOR_DECODE_MODE_NORMAL, |
| 6395 | {"\xC4\x82\x40", 3}, |
| 6396 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6397 | }, |
| 6398 | {"Not an array", |
| 6399 | QCBOR_DECODE_MODE_NORMAL, |
| 6400 | {"\xC4\xA2", 2}, |
| 6401 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6402 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6403 | }; |
| 6404 | |
| 6405 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6406 | int32_t |
| 6407 | ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6408 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6409 | return ProcessDecodeFailures(ExponentAndMantissaFailures, |
| 6410 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 6411 | struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6412 | } |
| 6413 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6414 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6415 | |
| 6416 | |
| 6417 | |
| 6418 | /* |
| 6419 | Some basic CBOR with map and array used in a lot of tests. |
| 6420 | The map labels are all strings |
| 6421 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6422 | { |
| 6423 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6424 | "an array of two strings": [ |
| 6425 | "string1", "string2" |
| 6426 | ], |
| 6427 | "map in a map": { |
| 6428 | "bytes 1": h'78787878', |
| 6429 | "bytes 2": h'79797979', |
| 6430 | "another int": 98, |
| 6431 | "text 2": "lies, damn lies and statistics" |
| 6432 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6433 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6434 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6435 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6436 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6437 | { |
| 6438 | QCBORItem Item1, Item2, Item3; |
| 6439 | int64_t nDecodedInt1, nDecodedInt2; |
| 6440 | UsefulBufC B1, B2, S1, S2, S3; |
| 6441 | |
| 6442 | QCBORDecodeContext DCtx; |
| 6443 | QCBORError nCBORError; |
| 6444 | |
| 6445 | QCBORDecode_Init(&DCtx, input, 0); |
| 6446 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6447 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6448 | |
| 6449 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 6450 | |
| 6451 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6452 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6453 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 6454 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 6455 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6456 | QCBORDecode_ExitMap(&DCtx); |
| 6457 | |
| 6458 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6459 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6460 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 6461 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6462 | return -400; |
| 6463 | } |
| 6464 | QCBORDecode_ExitArray(&DCtx); |
| 6465 | |
| 6466 | // Parse the same array again using GetText() instead of GetItem() |
| 6467 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6468 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 6469 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6470 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6471 | return 5000; |
| 6472 | } |
| 6473 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 6474 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6475 | return 5001; |
| 6476 | } */ |
| 6477 | |
| 6478 | QCBORDecode_ExitArray(&DCtx); |
| 6479 | |
| 6480 | QCBORDecode_ExitMap(&DCtx); |
| 6481 | |
| 6482 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 6483 | |
| 6484 | if(nCBORError) { |
| 6485 | return (int32_t)nCBORError; |
| 6486 | } |
| 6487 | |
| 6488 | if(nDecodedInt1 != 42) { |
| 6489 | return 1001; |
| 6490 | } |
| 6491 | |
| 6492 | if(nDecodedInt2 != 98) { |
| 6493 | return 1002; |
| 6494 | } |
| 6495 | |
| 6496 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6497 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6498 | return 1003; |
| 6499 | } |
| 6500 | |
| 6501 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6502 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6503 | return 1004; |
| 6504 | } |
| 6505 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6506 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6507 | return 1005; |
| 6508 | } |
| 6509 | |
| 6510 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 6511 | return 1006; |
| 6512 | } |
| 6513 | |
| 6514 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 6515 | return 1007; |
| 6516 | } |
| 6517 | |
| 6518 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 6519 | return 1008; |
| 6520 | } |
| 6521 | |
| 6522 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 6523 | return 1009; |
| 6524 | } |
| 6525 | |
| 6526 | return 0; |
| 6527 | } |
| 6528 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6529 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6530 | { |
| 6531 | -75008: h'05083399', |
| 6532 | 88: [], |
| 6533 | 100100: { |
| 6534 | "sub1": { |
| 6535 | 10: [ |
| 6536 | 0 |
| 6537 | ], |
| 6538 | -75009: h'A46823990001', |
| 6539 | 100100: { |
| 6540 | "json": "{ \"ueid\", \"xyz\"}", |
| 6541 | "subsub": { |
| 6542 | 100002: h'141813191001' |
| 6543 | } |
| 6544 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6545 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6546 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6547 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6548 | */ |
| 6549 | |
| 6550 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6551 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 6552 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 6553 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 6554 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 6555 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 6556 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 6557 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 6558 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 6559 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 6560 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 6561 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 6562 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6563 | }; |
| 6564 | |
| 6565 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 6566 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 6567 | { |
| 6568 | UsefulBufC String; |
| 6569 | |
| 6570 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 6571 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 6572 | |
| 6573 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6574 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 6575 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6576 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 6577 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 6578 | if(QCBORDecode_GetError(pDCtx)) { |
| 6579 | return 4001; |
| 6580 | } |
| 6581 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 6582 | return 4002; |
| 6583 | } |
| 6584 | QCBORDecode_ExitMap(pDCtx); |
| 6585 | QCBORDecode_ExitMap(pDCtx); |
| 6586 | QCBORDecode_ExitMap(pDCtx); |
| 6587 | QCBORDecode_ExitMap(pDCtx); |
| 6588 | |
| 6589 | return 0; |
| 6590 | } |
| 6591 | |
| 6592 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 6593 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 6594 | { |
| 6595 | QCBORItem Item; |
| 6596 | QCBORError uErr; |
| 6597 | |
| 6598 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 6599 | for(int x = 0; x < 20; x++) { |
| 6600 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6601 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6602 | return 4100; |
| 6603 | |
| 6604 | } |
| 6605 | } |
| 6606 | QCBORDecode_ExitArray(pDCtx); |
| 6607 | if(QCBORDecode_GetError(pDCtx)) { |
| 6608 | return 4101; |
| 6609 | } |
| 6610 | |
| 6611 | return 0; |
| 6612 | } |
| 6613 | |
| 6614 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 6615 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 6616 | { |
| 6617 | QCBORError uErr; |
| 6618 | |
| 6619 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6620 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 6621 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6622 | int64_t nInt = 99; |
| 6623 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 6624 | if(nInt != 0) { |
| 6625 | return 4200; |
| 6626 | } |
| 6627 | for(int x = 0; x < 20; x++) { |
| 6628 | QCBORItem Item; |
| 6629 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6630 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6631 | return 4201; |
| 6632 | |
| 6633 | } |
| 6634 | } |
| 6635 | QCBORDecode_ExitArray(pDCtx); |
| 6636 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 6637 | return 4202; |
| 6638 | } |
| 6639 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6640 | UsefulBufC dD; |
| 6641 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6642 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6643 | return 4203; |
| 6644 | } |
| 6645 | for(int x = 0; x < 20; x++) { |
| 6646 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6647 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 6648 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6649 | return 4204; |
| 6650 | } |
| 6651 | } |
| 6652 | QCBORDecode_ExitArray(pDCtx); |
| 6653 | QCBORDecode_ExitMap(pDCtx); |
| 6654 | QCBORDecode_ExitMap(pDCtx); |
| 6655 | |
| 6656 | return 0; |
| 6657 | } |
| 6658 | |
| 6659 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 6660 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 6661 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6662 | { |
| 6663 | QCBORDecodeContext DCtx; |
| 6664 | int32_t nReturn; |
| 6665 | QCBORError uErr; |
| 6666 | |
| 6667 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 6668 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6669 | |
| 6670 | for(int j = 0; j < 5; j++) { |
| 6671 | for(int i = 0; i < 20; i++) { |
| 6672 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 6673 | if(nReturn) { |
| 6674 | return nReturn; |
| 6675 | } |
| 6676 | } |
| 6677 | |
| 6678 | for(int i = 0; i < 20; i++) { |
| 6679 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 6680 | if(nReturn ) { |
| 6681 | return nReturn; |
| 6682 | } |
| 6683 | } |
| 6684 | |
| 6685 | for(int i = 0; i < 20; i++) { |
| 6686 | nReturn = DecodeNestedGetZero(&DCtx); |
| 6687 | if(nReturn ) { |
| 6688 | return nReturn; |
| 6689 | } |
| 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | QCBORDecode_ExitMap(&DCtx); |
| 6694 | uErr = QCBORDecode_Finish(&DCtx); |
| 6695 | if(uErr) { |
| 6696 | return (int32_t)uErr + 4100; |
| 6697 | } |
| 6698 | |
| 6699 | return 0; |
| 6700 | } |
| 6701 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6702 | |
| 6703 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6704 | [ |
| 6705 | 23, |
| 6706 | 6000, |
| 6707 | h'67616C6163746963', |
| 6708 | h'686176656E20746F6B656E' |
| 6709 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6710 | */ |
| 6711 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6712 | 0x84, |
| 6713 | 0x17, |
| 6714 | 0x19, 0x17, 0x70, |
| 6715 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 6716 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6717 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6718 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6719 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 6720 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6721 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6722 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 6723 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6724 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6725 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6726 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6727 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6728 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6729 | /* |
| 6730 | { |
| 6731 | 0: [], |
| 6732 | 9: [ |
| 6733 | [], |
| 6734 | [] |
| 6735 | ], |
| 6736 | 8: { |
| 6737 | 1: [], |
| 6738 | 2: {}, |
| 6739 | 3: [] |
| 6740 | }, |
| 6741 | 4: {}, |
| 6742 | 5: [], |
| 6743 | 6: [ |
| 6744 | [], |
| 6745 | [] |
| 6746 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6747 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6748 | */ |
| 6749 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6750 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 6751 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 6752 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 6753 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6754 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6755 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6756 | |
| 6757 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6758 | /* |
| 6759 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6760 | Duplicate label |
| 6761 | Integer overflow |
| 6762 | Date overflow |
| 6763 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6764 | { |
| 6765 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6766 | 3: -18446744073709551616, |
| 6767 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6768 | 5: 0, |
| 6769 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6770 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6771 | */ |
| 6772 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6773 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6774 | 0xa6, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6775 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6776 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 6777 | #else |
| 6778 | 0xa4, |
| 6779 | #endif |
| 6780 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6781 | 0x05, 0x00, |
| 6782 | 0x05, 0x00, |
| 6783 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6784 | }; |
| 6785 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6786 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6787 | static const uint8_t spUnRecoverableMapError1[] = { |
| 6788 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6789 | }; |
| 6790 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6791 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6792 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6793 | static const uint8_t spUnRecoverableMapError2[] = { |
| 6794 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6795 | }; |
| 6796 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6797 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6798 | static const uint8_t spUnRecoverableMapError3[] = { |
| 6799 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 6800 | }; |
| 6801 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6802 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6803 | static const uint8_t spUnRecoverableMapError4[] = { |
| 6804 | 0xbf, |
| 6805 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6806 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6807 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6808 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6809 | 0xff |
| 6810 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6811 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6812 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6813 | const unsigned char not_well_formed_submod_section[] = { |
| 6814 | 0xa1, 0x14, 0x1f, |
| 6815 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6816 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6817 | |
| 6818 | /* Array of length 3, but only two items. */ |
| 6819 | const unsigned char spBadConsumeInput[] = { |
| 6820 | 0x83, 0x00, 0x00 |
| 6821 | }; |
| 6822 | |
| 6823 | /* Tag nesting too deep. */ |
| 6824 | const unsigned char spBadConsumeInput2[] = { |
| 6825 | 0x81, |
| 6826 | 0xD8, 0x37, |
| 6827 | 0xD8, 0x2C, |
| 6828 | 0xD8, 0x21, |
| 6829 | 0xD6, |
| 6830 | 0xCB, |
| 6831 | 00 |
| 6832 | }; |
| 6833 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6834 | |
| 6835 | const unsigned char spBadConsumeInput4[] = { |
| 6836 | 0x81, 0x9f, 0x00, 0xff |
| 6837 | }; |
| 6838 | |
| 6839 | const unsigned char spBadConsumeInput5[] = { |
| 6840 | 0xa1, 0x80, 0x00 |
| 6841 | }; |
| 6842 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6843 | /* |
| 6844 | Lots of nesting for various nesting tests. |
| 6845 | { 1:1, |
| 6846 | 2:{ |
| 6847 | 21:21, |
| 6848 | 22:{ |
| 6849 | 221:[2111, 2112, 2113], |
| 6850 | 222:222, |
| 6851 | 223: {} |
| 6852 | }, |
| 6853 | 23: 23 |
| 6854 | }, |
| 6855 | 3:3, |
| 6856 | 4: [ {} ] |
| 6857 | } |
| 6858 | */ |
| 6859 | static const uint8_t spNested[] = { |
| 6860 | 0xA4, /* Map of 4 */ |
| 6861 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 6862 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 6863 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 6864 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 6865 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 6866 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 6867 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 6868 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 6869 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 6870 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 6871 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 6872 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 6873 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 6874 | 0xA0 /* Array entry {}, an empty map */ |
| 6875 | }; |
| 6876 | |
| 6877 | |
| 6878 | static int32_t EnterMapCursorTest(void) |
| 6879 | { |
| 6880 | QCBORDecodeContext DCtx; |
| 6881 | QCBORItem Item1; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6882 | int64_t nInt; |
| 6883 | QCBORError uErr; |
| 6884 | |
| 6885 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6886 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6887 | QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt); |
| 6888 | uErr = QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 6889 | if(uErr != QCBOR_SUCCESS) { |
| 6890 | return 701; |
| 6891 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6892 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6893 | return 700; |
| 6894 | } |
| 6895 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6896 | |
| 6897 | int i; |
| 6898 | for(i = 0; i < 13; i++) { |
| 6899 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6900 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6901 | int j; |
| 6902 | /* Move travesal cursor */ |
| 6903 | for(j = 0; j < i; j++) { |
| 6904 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6905 | } |
| 6906 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6907 | QCBORDecode_ExitMap(&DCtx); |
| 6908 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6909 | if(Item1.label.int64 != 3) { |
| 6910 | return 8000; |
| 6911 | } |
| 6912 | } |
| 6913 | |
| 6914 | for(i = 0; i < 13; i++) { |
| 6915 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6916 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6917 | int j; |
| 6918 | /* Move travesal cursor */ |
| 6919 | for(j = 0; j < i; j++) { |
| 6920 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6921 | } |
| 6922 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6923 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6924 | QCBORDecode_ExitMap(&DCtx); |
| 6925 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6926 | if(Item1.label.int64 != 23) { |
| 6927 | return 8000; |
| 6928 | } |
| 6929 | } |
| 6930 | |
| 6931 | for(i = 0; i < 13; i++) { |
| 6932 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6933 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6934 | int j; |
| 6935 | /* Move travesal cursor */ |
| 6936 | for(j = 0; j < i; j++) { |
| 6937 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6938 | } |
| 6939 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6940 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6941 | for(j = 0; j < i; j++) { |
| 6942 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6943 | } |
| 6944 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 6945 | QCBORDecode_ExitArray(&DCtx); |
| 6946 | QCBORDecode_ExitMap(&DCtx); |
| 6947 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6948 | if(Item1.label.int64 != 23) { |
| 6949 | return 8000; |
| 6950 | } |
| 6951 | QCBORDecode_ExitMap(&DCtx); |
| 6952 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6953 | if(Item1.label.int64 != 3) { |
| 6954 | return 8000; |
| 6955 | } |
| 6956 | } |
| 6957 | |
| 6958 | return 0; |
| 6959 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6960 | |
| 6961 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6962 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6963 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6964 | QCBORItem Item1; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6965 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6966 | int32_t nReturn; |
| 6967 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6968 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6969 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6970 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6971 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6972 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6973 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6974 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6975 | QCBORDecode_ExitArray(&DCtx); |
| 6976 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6977 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 6978 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6979 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6980 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6981 | QCBORDecode_ExitArray(&DCtx); |
| 6982 | QCBORDecode_ExitArray(&DCtx); |
| 6983 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6984 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 6985 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6986 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6987 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6988 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6989 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6990 | QCBORDecode_ExitArray(&DCtx); |
| 6991 | QCBORDecode_ExitMap(&DCtx); |
| 6992 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6993 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6994 | QCBORDecode_ExitMap(&DCtx); |
| 6995 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6996 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6997 | QCBORDecode_ExitArray(&DCtx); |
| 6998 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6999 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 7000 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 7001 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7002 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 7003 | QCBORDecode_ExitArray(&DCtx); |
| 7004 | QCBORDecode_ExitArray(&DCtx); |
| 7005 | |
| 7006 | QCBORDecode_ExitMap(&DCtx); |
| 7007 | |
| 7008 | uErr = QCBORDecode_Finish(&DCtx); |
| 7009 | if(uErr != QCBOR_SUCCESS){ |
| 7010 | return 3011; |
| 7011 | } |
| 7012 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7013 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 7014 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 7015 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 7016 | if(nReturn) { |
| 7017 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 7018 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7019 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 7020 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7021 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7022 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 7023 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 7024 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 7025 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7026 | if(nReturn) { |
| 7027 | return nReturn; |
| 7028 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 7029 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 7030 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7031 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 7032 | // These tests confirm the cursor is at the right place after entering |
| 7033 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7034 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7035 | |
| 7036 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7037 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7038 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7039 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7040 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 7041 | return 2001; |
| 7042 | } |
| 7043 | |
| 7044 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7045 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 7046 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 7047 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7048 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 7049 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7050 | UsefulBuf_Compare(ArrayItem.label.string, |
| 7051 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 7052 | return 2051; |
| 7053 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7054 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7055 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 7056 | return 2002; |
| 7057 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7058 | QCBORDecode_ExitArray(&DCtx); |
| 7059 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 7060 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7061 | UsefulBuf_Compare(ArrayItem.label.string, |
| 7062 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 7063 | return 2052; |
| 7064 | } |
| 7065 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7066 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7067 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7068 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 7069 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7070 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7071 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7072 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 7073 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7074 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 7075 | return 2003; |
| 7076 | } |
| 7077 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7078 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7079 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7080 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7081 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7082 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7083 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7084 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7085 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7086 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7087 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 7088 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7089 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 7090 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7091 | } |
| 7092 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7093 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7094 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 7095 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 7096 | QCBORDecode_ExitArray(&DCtx); |
| 7097 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7098 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 7099 | return 2006; |
| 7100 | } |
| 7101 | QCBORDecode_ExitMap(&DCtx); |
| 7102 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7103 | return 2007; |
| 7104 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7105 | #endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 7106 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 7107 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7108 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 7109 | int64_t nDecodedInt2; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7110 | |
| 7111 | UsefulBufC String; |
| 7112 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
| 7113 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7114 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
| 7115 | return 2009; |
| 7116 | } |
| 7117 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 7118 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 7119 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7120 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 7121 | return 2008; |
| 7122 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7123 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7124 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7125 | |
| 7126 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7127 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7128 | // This will fail because the map is empty. |
| 7129 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 7130 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7131 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7132 | return 2010; |
| 7133 | } |
| 7134 | QCBORDecode_ExitMap(&DCtx); |
| 7135 | uErr = QCBORDecode_Finish(&DCtx); |
| 7136 | if(uErr != QCBOR_SUCCESS){ |
| 7137 | return 2011; |
| 7138 | } |
| 7139 | |
| 7140 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7141 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7142 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7143 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7144 | // This will fail because the map is empty. |
| 7145 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 7146 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7147 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 7148 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7149 | } |
| 7150 | QCBORDecode_ExitMap(&DCtx); |
| 7151 | uErr = QCBORDecode_Finish(&DCtx); |
| 7152 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 7153 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7154 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7155 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7156 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7157 | |
| 7158 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7159 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 7160 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7161 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7162 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7163 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7164 | QCBORDecode_ExitArray(&DCtx); |
| 7165 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 7166 | QCBORDecode_ExitArray(&DCtx); |
| 7167 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7168 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7169 | return 2014; |
| 7170 | } |
| 7171 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7172 | int64_t nInt; |
| 7173 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7174 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7175 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7176 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7177 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7178 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 7179 | return 2021; |
| 7180 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7181 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 7182 | return 2121; |
| 7183 | } |
| 7184 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7185 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7186 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7187 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7188 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 7189 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7190 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 7191 | return 2023; |
| 7192 | } |
| 7193 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7194 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7195 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 7196 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7197 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7198 | return 2024; |
| 7199 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7200 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7201 | |
| 7202 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 7203 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7204 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 7205 | return 2025; |
| 7206 | } |
| 7207 | |
| 7208 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 7209 | |
| 7210 | QCBORDecode_ExitMap(&DCtx); |
| 7211 | uErr = QCBORDecode_Finish(&DCtx); |
| 7212 | if(uErr != QCBOR_SUCCESS) { |
| 7213 | return 2026; |
| 7214 | } |
| 7215 | |
| 7216 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7217 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7218 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7219 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7220 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 7221 | return 2030; |
| 7222 | } |
| 7223 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7224 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7225 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7226 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7227 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7228 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7229 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7230 | return 2031; |
| 7231 | } |
| 7232 | |
| 7233 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7234 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7235 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7236 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7237 | if(uErr != QCBOR_ERR_HIT_END) { |
| 7238 | return 2032; |
| 7239 | } |
| 7240 | |
| 7241 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7242 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7243 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7244 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7245 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 7246 | return 2033; |
| 7247 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7248 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7249 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7250 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 7251 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7252 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7253 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 7254 | return 2401; |
| 7255 | } |
| 7256 | if(QCBORDecode_GetError(&DCtx)) { |
| 7257 | return 2402; |
| 7258 | } |
| 7259 | |
| 7260 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7261 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 7262 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7263 | Item1.val.uCount != 3 || |
| 7264 | Item1.uNextNestLevel != 1) { |
| 7265 | return 2403; |
| 7266 | } |
| 7267 | if(QCBORDecode_GetError(&DCtx)) { |
| 7268 | return 2404; |
| 7269 | } |
| 7270 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7271 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 7272 | Item1.uNextNestLevel != 1 || |
| 7273 | Item1.val.int64 != 42) { |
| 7274 | return 2405; |
| 7275 | } |
| 7276 | if(QCBORDecode_GetError(&DCtx)) { |
| 7277 | return 2406; |
| 7278 | } |
| 7279 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7280 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 7281 | Item1.uNestingLevel != 1 || |
| 7282 | Item1.uNextNestLevel != 1 || |
| 7283 | Item1.val.uCount != 2) { |
| 7284 | return 2407; |
| 7285 | } |
| 7286 | if(QCBORDecode_GetError(&DCtx)) { |
| 7287 | return 2408; |
| 7288 | } |
| 7289 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7290 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7291 | Item1.uNestingLevel != 1 || |
| 7292 | Item1.uNextNestLevel != 0 || |
| 7293 | Item1.val.uCount != 4) { |
| 7294 | return 2409; |
| 7295 | } |
| 7296 | if(QCBORDecode_GetError(&DCtx)) { |
| 7297 | return 2410; |
| 7298 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7299 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7300 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7301 | nReturn = DecodeNestedIterate(); |
| 7302 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 7303 | |
| 7304 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 7305 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7306 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 7307 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 7308 | return 2500; |
| 7309 | } |
| 7310 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7311 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 7312 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7313 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7314 | return 2600; |
| 7315 | } |
| 7316 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7317 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7318 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 7319 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7320 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7321 | return 2700; |
| 7322 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7323 | #endif |
| 7324 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7325 | |
| 7326 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 7327 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7328 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7329 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7330 | return 2900; |
| 7331 | } |
| 7332 | #else |
| 7333 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7334 | return 2901; |
| 7335 | } |
| 7336 | #endif |
| 7337 | |
| 7338 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 7339 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7340 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 7341 | return 3000; |
| 7342 | } |
| 7343 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 7344 | nReturn = EnterMapCursorTest(); |
| 7345 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7346 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7347 | } |
| 7348 | |
| 7349 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7350 | struct NumberConversion { |
| 7351 | char *szDescription; |
| 7352 | UsefulBufC CBOR; |
| 7353 | int64_t nConvertedToInt64; |
| 7354 | QCBORError uErrorInt64; |
| 7355 | uint64_t uConvertToUInt64; |
| 7356 | QCBORError uErrorUint64; |
| 7357 | double dConvertToDouble; |
| 7358 | QCBORError uErrorDouble; |
| 7359 | }; |
| 7360 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7361 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 7362 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 7363 | #else |
| 7364 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 7365 | #endif |
| 7366 | |
| 7367 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7368 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7369 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7370 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 7371 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 7372 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 7373 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 7374 | }, 15}, |
| 7375 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 7376 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 7377 | 0, |
| 7378 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7379 | -4.6116860184273879E+18, |
| 7380 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 7381 | }, |
| 7382 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7383 | "too large to fit into int64_t", |
| 7384 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 7385 | 0, |
| 7386 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7387 | 0, |
| 7388 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7389 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7390 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7391 | }, |
| 7392 | { |
| 7393 | "largest negative int that fits in int64_t", |
| 7394 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 7395 | INT64_MIN, |
| 7396 | QCBOR_SUCCESS, |
| 7397 | 0, |
| 7398 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7399 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7400 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7401 | }, |
| 7402 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7403 | "negative bignum -1", |
| 7404 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 7405 | -1, |
| 7406 | QCBOR_SUCCESS, |
| 7407 | 0, |
| 7408 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7409 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7410 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7411 | }, |
| 7412 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7413 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7414 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7415 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7416 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7417 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7418 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7419 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7420 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7421 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7422 | }, |
| 7423 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7424 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7425 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7426 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7427 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7428 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7429 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7430 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7431 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7432 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7433 | }, |
| 7434 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7435 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7436 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7437 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7438 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7439 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7440 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7441 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7442 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7443 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7444 | }, |
| 7445 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7446 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7447 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 7448 | 0, |
| 7449 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7450 | 0, |
| 7451 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7452 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7453 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7454 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7455 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7456 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7457 | "Positive bignum 0x01020304 indefinite length string", |
| 7458 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 7459 | 0x01020304, |
| 7460 | QCBOR_SUCCESS, |
| 7461 | 0x01020304, |
| 7462 | QCBOR_SUCCESS, |
| 7463 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7464 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7465 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7466 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7467 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7468 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7469 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 7470 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 7471 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7472 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7473 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7474 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7475 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7476 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7477 | }, |
| 7478 | { |
| 7479 | "big float [9223372036854775806, 9223372036854775806]", |
| 7480 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7481 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 7482 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7483 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7484 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7485 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7486 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7487 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7488 | }, |
| 7489 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7490 | "Big float 3 * 2^^2", |
| 7491 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 7492 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7493 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7494 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7495 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7496 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7497 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7498 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7499 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7500 | "Decimal fraction 3/10", |
| 7501 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 7502 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7503 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7504 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7505 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7506 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7507 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 7508 | }, |
| 7509 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7510 | "extreme pos bignum", |
| 7511 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 7512 | // 50 rows of 8 is 400 digits. |
| 7513 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7514 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7515 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7516 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7517 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7518 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7519 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7520 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7521 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7522 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7523 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7524 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7525 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7526 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7527 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7528 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7529 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7530 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7531 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7532 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7533 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7534 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7535 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7536 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7537 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7538 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7539 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7540 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7541 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7542 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7543 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7544 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7545 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7546 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7547 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7548 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7549 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7550 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7551 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7552 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7553 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7554 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7555 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7556 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7557 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7558 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7559 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7560 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7561 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7562 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7563 | 404}, |
| 7564 | 0, |
| 7565 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7566 | 0, |
| 7567 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7568 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7569 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7570 | }, |
| 7571 | |
| 7572 | { |
| 7573 | "extreme neg bignum", |
| 7574 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 7575 | // 50 rows of 8 is 400 digits. |
| 7576 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7577 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7578 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7579 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7580 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7581 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7582 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7583 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7584 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7585 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7586 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7587 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7588 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7589 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7590 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7591 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7592 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7593 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7594 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7595 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7596 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7597 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7598 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7599 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7600 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7601 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7602 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7603 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7604 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7605 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7606 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7607 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7608 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7609 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7610 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7611 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7612 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7613 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7614 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7615 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7616 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7617 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7618 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7619 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7620 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7621 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7622 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7623 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7624 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7625 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7626 | 404}, |
| 7627 | 0, |
| 7628 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7629 | 0, |
| 7630 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7631 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7632 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7633 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7634 | |
| 7635 | { |
| 7636 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 7637 | {(uint8_t[]){ |
| 7638 | 0xC5, 0x82, |
| 7639 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7640 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7641 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7642 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7643 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7644 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7645 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7646 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7647 | }, |
| 7648 | |
| 7649 | { |
| 7650 | "bigfloat that evaluates to -INFINITY", |
| 7651 | {(uint8_t[]){ |
| 7652 | 0xC5, 0x82, |
| 7653 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7654 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7655 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7656 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7657 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7658 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7659 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7660 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7661 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7662 | { |
| 7663 | "Positive bignum 0xffff", |
| 7664 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 7665 | 65536-1, |
| 7666 | QCBOR_SUCCESS, |
| 7667 | 0xffff, |
| 7668 | QCBOR_SUCCESS, |
| 7669 | 65535.0, |
| 7670 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7671 | }, |
| 7672 | #endif /* QCBOR_DISABLE_TAGS */ |
| 7673 | { |
| 7674 | "Positive integer 18446744073709551615", |
| 7675 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 7676 | 0, |
| 7677 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7678 | 18446744073709551615ULL, |
| 7679 | QCBOR_SUCCESS, |
| 7680 | 18446744073709551615.0, |
| 7681 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7682 | }, |
| 7683 | |
| 7684 | { |
| 7685 | "Postive integer 0", |
| 7686 | {(uint8_t[]){0x0}, 1}, |
| 7687 | 0LL, |
| 7688 | QCBOR_SUCCESS, |
| 7689 | 0ULL, |
| 7690 | QCBOR_SUCCESS, |
| 7691 | 0.0, |
| 7692 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7693 | }, |
| 7694 | { |
| 7695 | "Negative integer -18446744073709551616", |
| 7696 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 7697 | -9223372036854775807-1, // INT64_MIN |
| 7698 | QCBOR_SUCCESS, |
| 7699 | 0ULL, |
| 7700 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7701 | -9223372036854775808.0, |
| 7702 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7703 | }, |
| 7704 | { |
| 7705 | "Double Floating point value 100.3", |
| 7706 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 7707 | 100L, |
| 7708 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7709 | 100ULL, |
| 7710 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7711 | 100.3, |
| 7712 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 7713 | }, |
| 7714 | { |
| 7715 | "Floating point value NaN 0xfa7fc00000", |
| 7716 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 7717 | 0, |
| 7718 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7719 | 0, |
| 7720 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7721 | NAN, |
| 7722 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7723 | }, |
| 7724 | { |
| 7725 | "half-precision Floating point value -4", |
| 7726 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 7727 | // Normal case with all enabled. |
| 7728 | -4, |
| 7729 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7730 | 0, |
| 7731 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7732 | -4.0, |
| 7733 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 7734 | }, |
| 7735 | { |
| 7736 | "+inifinity single precision", |
| 7737 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 7738 | 0, |
| 7739 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7740 | 0, |
| 7741 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 7742 | INFINITY, |
| 7743 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7744 | }, |
| 7745 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7746 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7747 | |
| 7748 | |
| 7749 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7750 | |
| 7751 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 7752 | { |
| 7753 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 7754 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7755 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 7756 | return 1; |
| 7757 | } |
| 7758 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7759 | (void)Pool; |
| 7760 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7761 | return 0; |
| 7762 | } |
| 7763 | |
| 7764 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7765 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7766 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7767 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 7768 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7769 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7770 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 7771 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7772 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7773 | // Set up the decoding context including a memory pool so that |
| 7774 | // indefinite length items can be checked |
| 7775 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7776 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7777 | |
| 7778 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7779 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7780 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7781 | } |
| 7782 | |
| 7783 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7784 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7785 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7786 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7787 | } |
| 7788 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7789 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7790 | } |
| 7791 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7792 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7793 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7794 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7795 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7796 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7797 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7798 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7799 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7800 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7801 | } |
| 7802 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7803 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7804 | } |
| 7805 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7806 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7807 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7808 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7809 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7810 | |
| 7811 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7812 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7813 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7814 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7815 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7816 | } |
| 7817 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 7818 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7819 | // NaN's can't be compared for equality. A NaN is |
| 7820 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7821 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7822 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7823 | } |
| 7824 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7825 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7826 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7827 | } |
| 7828 | } |
| 7829 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7830 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7831 | } |
| 7832 | |
| 7833 | return 0; |
| 7834 | } |
| 7835 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7836 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7837 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7838 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7839 | { |
| 7840 | QCBORDecodeContext DCtx; |
| 7841 | QCBORItem Item; |
| 7842 | QCBORError uCBORError; |
| 7843 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7844 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7845 | QCBORDecode_Init(&DCtx, |
| 7846 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 7847 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7848 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7849 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 7850 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7851 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7852 | do { |
| 7853 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7854 | } while (QCBOR_SUCCESS == uCBORError); |
| 7855 | |
| 7856 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7857 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 7858 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7859 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7860 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7861 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7862 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7863 | |
| 7864 | |
| 7865 | static const uint8_t spSequenceTestInput[] = { |
| 7866 | /* 1. The valid date string "1985-04-12" */ |
| 7867 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 7868 | |
| 7869 | /* 2. */ |
| 7870 | 0x00, |
| 7871 | |
| 7872 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 7873 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 7874 | |
| 7875 | /* 4. */ |
| 7876 | 0x62, 'h', 'i', |
| 7877 | }; |
| 7878 | |
| 7879 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7880 | int32_t CBORSequenceDecodeTests(void) |
| 7881 | { |
| 7882 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7883 | QCBORItem Item; |
| 7884 | QCBORError uCBORError; |
| 7885 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7886 | |
| 7887 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7888 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7889 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7890 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7891 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7892 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7893 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7894 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7895 | if(uCBORError != QCBOR_SUCCESS) { |
| 7896 | return 1; |
| 7897 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7898 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7899 | return 2; |
| 7900 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7901 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7902 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7903 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7904 | uConsumed != 11) { |
| 7905 | return 102; |
| 7906 | } |
| 7907 | |
| 7908 | // Get 2. |
| 7909 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7910 | if(uCBORError != QCBOR_SUCCESS) { |
| 7911 | return 66; |
| 7912 | } |
| 7913 | |
| 7914 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7915 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7916 | uConsumed != 12) { |
| 7917 | return 102; |
| 7918 | } |
| 7919 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7920 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7921 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7922 | if(uCBORError != QCBOR_SUCCESS) { |
| 7923 | return 2; |
| 7924 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7925 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7926 | return 3; |
| 7927 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7928 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7929 | // A sequence can have stuff at the end that may |
| 7930 | // or may not be valid CBOR. The protocol decoder knows |
| 7931 | // when to stop by definition of the protocol, not |
| 7932 | // when the top-level map or array is ended. |
| 7933 | // Finish still has to be called to know that |
| 7934 | // maps and arrays (if there were any) were closed |
| 7935 | // off correctly. When called like this it |
| 7936 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 7937 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7938 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 7939 | return 4; |
| 7940 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7941 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7942 | // --- Test an empty input ---- |
| 7943 | uint8_t empty[1]; |
| 7944 | UsefulBufC Empty = {empty, 0}; |
| 7945 | QCBORDecode_Init(&DCtx, |
| 7946 | Empty, |
| 7947 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7948 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7949 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7950 | if(uCBORError != QCBOR_SUCCESS) { |
| 7951 | return 5; |
| 7952 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7953 | |
| 7954 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7955 | // --- Sequence with unclosed indefinite length array --- |
| 7956 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7957 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7958 | QCBORDecode_Init(&DCtx, |
| 7959 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 7960 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7961 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7962 | // Get the first item |
| 7963 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7964 | if(uCBORError != QCBOR_SUCCESS) { |
| 7965 | return 7; |
| 7966 | } |
| 7967 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7968 | return 8; |
| 7969 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7970 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7971 | // Get a second item |
| 7972 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7973 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7974 | if(uCBORError != QCBOR_SUCCESS) { |
| 7975 | return 9; |
| 7976 | } |
| 7977 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7978 | return 10; |
| 7979 | } |
| 7980 | |
| 7981 | // Try to finish before consuming all bytes to confirm |
| 7982 | // that the still-open error is returned. |
| 7983 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7984 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7985 | return 11; |
| 7986 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7987 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7988 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7989 | return 20; |
| 7990 | } |
| 7991 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7992 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7993 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7994 | // --- Sequence with a closed indefinite length array --- |
| 7995 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7996 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7997 | QCBORDecode_Init(&DCtx, |
| 7998 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 7999 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 8000 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 8001 | // Get the first item |
| 8002 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 8003 | if(uCBORError != QCBOR_SUCCESS) { |
| 8004 | return 12; |
| 8005 | } |
| 8006 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 8007 | return 13; |
| 8008 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 8009 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 8010 | // Get a second item |
| 8011 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8012 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8013 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 8014 | if(uCBORError != QCBOR_SUCCESS) { |
| 8015 | return 14; |
| 8016 | } |
| 8017 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 8018 | return 15; |
| 8019 | } |
| 8020 | |
| 8021 | // Try to finish before consuming all bytes to confirm |
| 8022 | // that the still-open error is returned. |
| 8023 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 8024 | if(uCBORError != QCBOR_SUCCESS) { |
| 8025 | return 16; |
| 8026 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8027 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8028 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 8029 | return 20; |
| 8030 | } |
| 8031 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 8032 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 8033 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 8034 | return 0; |
| 8035 | } |
| 8036 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8037 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 8038 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8039 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8040 | { |
| 8041 | int nErrCode; |
| 8042 | int32_t n32; |
| 8043 | int16_t n16; |
| 8044 | int8_t n8; |
| 8045 | uint32_t u32; |
| 8046 | uint16_t u16; |
| 8047 | uint8_t u8; |
| 8048 | uint64_t u64; |
| 8049 | |
| 8050 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 8051 | if(nErrCode == -1 || n32 != 1) { |
| 8052 | return 1; |
| 8053 | } |
| 8054 | |
| 8055 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 8056 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 8057 | return 2; |
| 8058 | } |
| 8059 | |
| 8060 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 8061 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 8062 | return 3; |
| 8063 | } |
| 8064 | |
| 8065 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 8066 | if(nErrCode != -1) { |
| 8067 | return 4; |
| 8068 | } |
| 8069 | |
| 8070 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 8071 | if(nErrCode != -1) { |
| 8072 | return 5; |
| 8073 | } |
| 8074 | |
| 8075 | |
| 8076 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 8077 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 8078 | return 6; |
| 8079 | } |
| 8080 | |
| 8081 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 8082 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 8083 | return 7; |
| 8084 | } |
| 8085 | |
| 8086 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 8087 | if(nErrCode == -1 || n16 != 1) { |
| 8088 | return 8; |
| 8089 | } |
| 8090 | |
| 8091 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 8092 | if(nErrCode != -1) { |
| 8093 | return 9; |
| 8094 | } |
| 8095 | |
| 8096 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 8097 | if(nErrCode != -1) { |
| 8098 | return 10; |
| 8099 | } |
| 8100 | |
| 8101 | |
| 8102 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 8103 | if(nErrCode == -1 || n8 != 1) { |
| 8104 | return 11; |
| 8105 | } |
| 8106 | |
| 8107 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 8108 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 8109 | return 12; |
| 8110 | } |
| 8111 | |
| 8112 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 8113 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 8114 | return 13; |
| 8115 | } |
| 8116 | |
| 8117 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 8118 | if(nErrCode != -1) { |
| 8119 | return 14; |
| 8120 | } |
| 8121 | |
| 8122 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 8123 | if(nErrCode != -1) { |
| 8124 | return 15; |
| 8125 | } |
| 8126 | |
| 8127 | |
| 8128 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 8129 | if(nErrCode == -1 || u32 != 1) { |
| 8130 | return 16; |
| 8131 | } |
| 8132 | |
| 8133 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 8134 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 8135 | return 17; |
| 8136 | } |
| 8137 | |
| 8138 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 8139 | if(nErrCode == -1 || u32 != 0) { |
| 8140 | return 18; |
| 8141 | } |
| 8142 | |
| 8143 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 8144 | if(nErrCode != -1) { |
| 8145 | return 19; |
| 8146 | } |
| 8147 | |
| 8148 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 8149 | if(nErrCode != -1) { |
| 8150 | return 20; |
| 8151 | } |
| 8152 | |
| 8153 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame] | 8154 | nErrCode = QCBOR_Int64ToUInt16((int64_t)UINT16_MAX, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8155 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 8156 | return 21; |
| 8157 | } |
| 8158 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame] | 8159 | nErrCode = QCBOR_Int64ToUInt16((int64_t)0, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8160 | if(nErrCode == -1 || u16 != 0) { |
| 8161 | return 22; |
| 8162 | } |
| 8163 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame] | 8164 | nErrCode = QCBOR_Int64ToUInt16(1, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8165 | if(nErrCode == -1 || u16 != 1) { |
| 8166 | return 23; |
| 8167 | } |
| 8168 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame] | 8169 | nErrCode = QCBOR_Int64ToUInt16(((int64_t)UINT16_MAX)+1, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8170 | if(nErrCode != -1) { |
| 8171 | return 24; |
| 8172 | } |
| 8173 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame] | 8174 | nErrCode = QCBOR_Int64ToUInt16((int64_t)-1, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8175 | if(nErrCode != -1) { |
| 8176 | return 25; |
| 8177 | } |
| 8178 | |
| 8179 | |
| 8180 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 8181 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 8182 | return 26; |
| 8183 | } |
| 8184 | |
| 8185 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 8186 | if(nErrCode == -1 || u8 != 0) { |
| 8187 | return 27; |
| 8188 | } |
| 8189 | |
| 8190 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 8191 | if(nErrCode == -1 || u8 != 1) { |
| 8192 | return 28; |
| 8193 | } |
| 8194 | |
| 8195 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 8196 | if(nErrCode != -1) { |
| 8197 | return 29; |
| 8198 | } |
| 8199 | |
| 8200 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 8201 | if(nErrCode != -1) { |
| 8202 | return 30; |
| 8203 | } |
| 8204 | |
| 8205 | |
| 8206 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 8207 | if(nErrCode == -1 || u64 != 1) { |
| 8208 | return 31; |
| 8209 | } |
| 8210 | |
| 8211 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 8212 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 8213 | return 32; |
| 8214 | } |
| 8215 | |
| 8216 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 8217 | if(nErrCode == -1 || u64 != 0) { |
| 8218 | return 33; |
| 8219 | } |
| 8220 | |
| 8221 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 8222 | if(nErrCode != -1) { |
| 8223 | return 34; |
| 8224 | } |
| 8225 | |
| 8226 | return 0; |
| 8227 | } |
| 8228 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8229 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8230 | |
| 8231 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8232 | /* |
| 8233 | A sequence with |
| 8234 | A wrapping bstr |
| 8235 | containing a map |
| 8236 | 1 |
| 8237 | 2 |
| 8238 | A wrapping bstr |
| 8239 | containing an array |
| 8240 | 3 |
| 8241 | wrapping bstr |
| 8242 | 4 |
| 8243 | 5 |
| 8244 | 6 |
| 8245 | array |
| 8246 | 7 |
| 8247 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8248 | */ |
| 8249 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8250 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8251 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8252 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8253 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8254 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8255 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8256 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8257 | |
| 8258 | QCBOREncode_BstrWrap(&EC); |
| 8259 | QCBOREncode_OpenMap(&EC); |
| 8260 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 8261 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 8262 | QCBOREncode_CloseMap(&EC); |
| 8263 | QCBOREncode_BstrWrap(&EC); |
| 8264 | QCBOREncode_OpenArray(&EC); |
| 8265 | QCBOREncode_AddInt64(&EC, 3); |
| 8266 | QCBOREncode_BstrWrap(&EC); |
| 8267 | QCBOREncode_AddInt64(&EC, 4); |
| 8268 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8269 | QCBOREncode_AddInt64(&EC, 5); |
| 8270 | QCBOREncode_CloseArray(&EC); |
| 8271 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8272 | QCBOREncode_AddInt64(&EC, 6); |
| 8273 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8274 | QCBOREncode_OpenArray(&EC); |
| 8275 | QCBOREncode_AddInt64(&EC, 7); |
| 8276 | QCBOREncode_AddInt64(&EC, 8); |
| 8277 | QCBOREncode_CloseArray(&EC); |
| 8278 | |
| 8279 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 8280 | if(uErr) { |
| 8281 | Encoded = NULLUsefulBufC; |
| 8282 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8283 | |
| 8284 | return Encoded; |
| 8285 | } |
| 8286 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8287 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8288 | static const uint8_t spBreakInByteString[] = { |
| 8289 | 0x41, 0xff |
| 8290 | }; |
| 8291 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8292 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8293 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8294 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8295 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8296 | |
| 8297 | QCBORDecodeContext DC; |
| 8298 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8299 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8300 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8301 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8302 | |
| 8303 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8304 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8305 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8306 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 8307 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8308 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8309 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8310 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8311 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8312 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8313 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8314 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8315 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8316 | QCBORDecode_ExitArray(&DC); |
| 8317 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8318 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8319 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8320 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8321 | QCBORDecode_GetInt64(&DC, &n7); |
| 8322 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8323 | QCBORDecode_ExitArray(&DC); |
| 8324 | |
| 8325 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8326 | if(uErr) { |
| 8327 | return (int32_t)uErr; |
| 8328 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8329 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8330 | |
| 8331 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 8332 | * Successful because no items are fetched from byte string. |
| 8333 | */ |
| 8334 | QCBORDecode_Init(&DC, |
| 8335 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8336 | 0); |
| 8337 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8338 | uErr = QCBORDecode_GetError(&DC); |
| 8339 | if(uErr) { |
| 8340 | return 100 + (int32_t)uErr; |
| 8341 | } |
| 8342 | |
| 8343 | QCBORDecode_ExitBstrWrapped(&DC); |
| 8344 | uErr = QCBORDecode_GetError(&DC); |
| 8345 | if(uErr) { |
| 8346 | return 200 + (int32_t)uErr; |
| 8347 | } |
| 8348 | |
| 8349 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 8350 | * It fails because there should be no break. |
| 8351 | */ |
| 8352 | QCBORDecode_Init(&DC, |
| 8353 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8354 | 0); |
| 8355 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8356 | QCBORItem Item; |
| 8357 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 8358 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 8359 | return 300 + (int32_t)uErr; |
| 8360 | } |
| 8361 | |
| 8362 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8363 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8364 | |
| 8365 | |
| 8366 | |
| 8367 | |
| 8368 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8369 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8370 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8371 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8372 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8373 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 8374 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 8375 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8376 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8377 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8378 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 8379 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 8380 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8381 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8382 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8383 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8384 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8385 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8386 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8387 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8388 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8389 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8390 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8391 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8392 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8393 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 8394 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8395 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8396 | 21, |
| 8397 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 8398 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 8399 | |
| 8400 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8401 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8402 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8403 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8404 | |
| 8405 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8406 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8407 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8408 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8409 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8410 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8411 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8412 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8413 | |
| 8414 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8415 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8416 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8417 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8418 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8419 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8420 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 8421 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8422 | |
| 8423 | 0x18, 0x33, |
| 8424 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 8425 | |
| 8426 | // MIME |
| 8427 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8428 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 8429 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 8430 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8431 | |
| 8432 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8433 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8434 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8435 | |
| 8436 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8437 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 8438 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 8439 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8440 | |
| 8441 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8442 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8443 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8444 | |
| 8445 | // UUID |
| 8446 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8447 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 8448 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8449 | |
| 8450 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8451 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 8452 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8453 | }; |
| 8454 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8455 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8456 | { |
| 8457 | QCBORDecodeContext DC; |
| 8458 | QCBORError uErr; |
| 8459 | |
| 8460 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 8461 | |
| 8462 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8463 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8464 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8465 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8466 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8467 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8468 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 8469 | return 1; |
| 8470 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8471 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8472 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8473 | return 2; |
| 8474 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8475 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8476 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8477 | return 3; |
| 8478 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8479 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8480 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8481 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8482 | return 4; |
| 8483 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8484 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8485 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8486 | return 5; |
| 8487 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8488 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8489 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8490 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8491 | bNeg != false) { |
| 8492 | return 10; |
| 8493 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8494 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8495 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8496 | bNeg != true) { |
| 8497 | return 11; |
| 8498 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8499 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8500 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8501 | return 12; |
| 8502 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8503 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8504 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8505 | return 13; |
| 8506 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8507 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8508 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8509 | return 14; |
| 8510 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8511 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8512 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8513 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8514 | return 20; |
| 8515 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8516 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8517 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8518 | return 21; |
| 8519 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8520 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8521 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8522 | return 22; |
| 8523 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8524 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8525 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8526 | return 23; |
| 8527 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8528 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8529 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8530 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8531 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8532 | return 30; |
| 8533 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8534 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8535 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8536 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8537 | return 31; |
| 8538 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8539 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8540 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8541 | return 32; |
| 8542 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8543 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8544 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8545 | return 33; |
| 8546 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8547 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8548 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8549 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8550 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8551 | return 40; |
| 8552 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8553 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8554 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8555 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8556 | return 41; |
| 8557 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8558 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8559 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8560 | return 42; |
| 8561 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8562 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8563 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8564 | return 43; |
| 8565 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8566 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8567 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8568 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8569 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8570 | return 50; |
| 8571 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8572 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8573 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8574 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8575 | return 51; |
| 8576 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8577 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8578 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8579 | return 52; |
| 8580 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8581 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8582 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8583 | return 53; |
| 8584 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8585 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8586 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8587 | // MIME |
| 8588 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8589 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8590 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8591 | bIsNot7Bit == true) { |
| 8592 | return 60; |
| 8593 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8594 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8595 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8596 | bIsNot7Bit == true) { |
| 8597 | return 61; |
| 8598 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8599 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8600 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8601 | bIsNot7Bit == false) { |
| 8602 | return 62; |
| 8603 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8604 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8605 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8606 | bIsNot7Bit == false) { |
| 8607 | return 63; |
| 8608 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8609 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8610 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8611 | return 64; |
| 8612 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8613 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8614 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8615 | return 65; |
| 8616 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8617 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8618 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8619 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8620 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8621 | return 70; |
| 8622 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8623 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 8624 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8625 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8626 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8627 | return 71; |
| 8628 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8629 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8630 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8631 | return 72; |
| 8632 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8633 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8634 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8635 | return 73; |
| 8636 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8637 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8638 | // Improvement: add some more error test cases |
| 8639 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8640 | QCBORDecode_ExitMap(&DC); |
| 8641 | |
| 8642 | uErr = QCBORDecode_Finish(&DC); |
| 8643 | if(uErr != QCBOR_SUCCESS) { |
| 8644 | return 100; |
| 8645 | } |
| 8646 | |
| 8647 | return 0; |
| 8648 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8649 | |
| 8650 | |
| 8651 | |
| 8652 | |
| 8653 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8654 | [ |
| 8655 | "aaaaaaaaaa", |
| 8656 | {} |
| 8657 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8658 | */ |
| 8659 | static const uint8_t spTooLarge1[] = { |
| 8660 | 0x9f, |
| 8661 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8662 | 0xa0, |
| 8663 | 0xff |
| 8664 | }; |
| 8665 | |
| 8666 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8667 | [ |
| 8668 | { |
| 8669 | 0: "aaaaaaaaaa" |
| 8670 | } |
| 8671 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8672 | */ |
| 8673 | static const uint8_t spTooLarge2[] = { |
| 8674 | 0x9f, |
| 8675 | 0xa1, |
| 8676 | 0x00, |
| 8677 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8678 | 0xff |
| 8679 | }; |
| 8680 | |
| 8681 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8682 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8683 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8684 | { |
| 8685 | 0: "aaaaaaaaaa" |
| 8686 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8687 | */ |
| 8688 | static const uint8_t spTooLarge3[] = { |
| 8689 | 0x4d, |
| 8690 | 0xa1, |
| 8691 | 0x00, |
| 8692 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8693 | }; |
| 8694 | |
| 8695 | int32_t TooLargeInputTest(void) |
| 8696 | { |
| 8697 | QCBORDecodeContext DC; |
| 8698 | QCBORError uErr; |
| 8699 | UsefulBufC String; |
| 8700 | |
| 8701 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 8702 | // to 10 There's not really any way to test this error |
| 8703 | // condition. The error condition is not complex, so setting |
| 8704 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 8705 | |
| 8706 | // The input CBOR is only too large because the |
| 8707 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 8708 | // |
| 8709 | // This test is disabled for the normal test runs because of the |
| 8710 | // special build requirement. |
| 8711 | |
| 8712 | |
| 8713 | // Tests the start of a map being too large |
| 8714 | 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] | 8715 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8716 | QCBORDecode_GetTextString(&DC, &String); |
| 8717 | uErr = QCBORDecode_GetError(&DC); |
| 8718 | if(uErr != QCBOR_SUCCESS) { |
| 8719 | return 1; |
| 8720 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8721 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8722 | uErr = QCBORDecode_GetError(&DC); |
| 8723 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8724 | return 2; |
| 8725 | } |
| 8726 | |
| 8727 | // Tests the end of a map being too large |
| 8728 | 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] | 8729 | QCBORDecode_EnterArray(&DC, NULL); |
| 8730 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8731 | uErr = QCBORDecode_GetError(&DC); |
| 8732 | if(uErr != QCBOR_SUCCESS) { |
| 8733 | return 3; |
| 8734 | } |
| 8735 | QCBORDecode_ExitMap(&DC); |
| 8736 | uErr = QCBORDecode_GetError(&DC); |
| 8737 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8738 | return 4; |
| 8739 | } |
| 8740 | |
| 8741 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 8742 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 8743 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8744 | uErr = QCBORDecode_GetError(&DC); |
| 8745 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8746 | return 5; |
| 8747 | } |
| 8748 | |
| 8749 | return 0; |
| 8750 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8751 | |
| 8752 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8753 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 8754 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8755 | /* |
| 8756 | An array of three map entries |
| 8757 | 1) Indefinite length string label for indefinite lenght byte string |
| 8758 | 2) Indefinite length string label for an integer |
| 8759 | 3) Indefinite length string label for an indefinite-length negative big num |
| 8760 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8761 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8762 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8763 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 8764 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 8765 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 8766 | 0x03, |
| 8767 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 8768 | 0xc3, |
| 8769 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8770 | }; |
| 8771 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8772 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8773 | { |
| 8774 | QCBORDecodeContext DCtx; |
| 8775 | |
| 8776 | QCBORDecode_Init(&DCtx, |
| 8777 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 8778 | QCBOR_DECODE_MODE_NORMAL); |
| 8779 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8780 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8781 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 8782 | |
| 8783 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8784 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8785 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8786 | |
| 8787 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8788 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8789 | return 1; |
| 8790 | } |
| 8791 | |
| 8792 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 8793 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 8794 | return 2; |
| 8795 | } |
| 8796 | |
| 8797 | uint64_t uInt; |
| 8798 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 8799 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8800 | return 3; |
| 8801 | } |
| 8802 | if(uInt != 3) { |
| 8803 | return 4; |
| 8804 | } |
| 8805 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8806 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8807 | double uDouble; |
| 8808 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 8809 | "label2", |
| 8810 | 0xff, |
| 8811 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8812 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8813 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8814 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8815 | return 5; |
| 8816 | } |
| 8817 | if(uDouble != -16777474) { |
| 8818 | return 6; |
| 8819 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8820 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8821 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 8822 | return 7; |
| 8823 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8824 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8825 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8826 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8827 | QCBORDecode_ExitMap(&DCtx); |
| 8828 | |
| 8829 | if(QCBORDecode_Finish(&DCtx)) { |
| 8830 | return 99; |
| 8831 | } |
| 8832 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8833 | #else /* QCBOR_DISABLE_TAGS */ |
| 8834 | /* The big num in the input is a CBOR tag and you can't do |
| 8835 | * map lookups in a map with a tag so this test does very little |
| 8836 | * when tags are disabled. That is OK, the test coverage is still |
| 8837 | * good when they are not. |
| 8838 | */ |
| 8839 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 8840 | return 1002; |
| 8841 | } |
| 8842 | #endif /*QCBOR_DISABLE_TAGS */ |
| 8843 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8844 | return 0; |
| 8845 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8846 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8847 | |
| 8848 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8849 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8850 | /* |
| 8851 | * An array of an integer and an array. The second array contains |
| 8852 | * a bstr-wrapped map. |
| 8853 | * |
| 8854 | * [7, [h'A36D6669... (see next lines) 73']] |
| 8855 | * |
| 8856 | * {"first integer": 42, |
| 8857 | * "an array of two strings": ["string1", "string2"], |
| 8858 | * "map in a map": |
| 8859 | * { "bytes 1": h'78787878', |
| 8860 | * "bytes 2": h'79797979', |
| 8861 | * "another int": 98, |
| 8862 | * "text 2": "lies, damn lies and statistics" |
| 8863 | * } |
| 8864 | * } |
| 8865 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8866 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8867 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 8868 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 8869 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8870 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8871 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8872 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8873 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8874 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8875 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8876 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8877 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8878 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8879 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8880 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8881 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8882 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8883 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8884 | 0x73 |
| 8885 | }; |
| 8886 | |
| 8887 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8888 | |
| 8889 | /* As above, but the arrays are indefinite length */ |
| 8890 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 8891 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 8892 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8893 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8894 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8895 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8896 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8897 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8898 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8899 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8900 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8901 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8902 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8903 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8904 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8905 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8906 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8907 | 0x73, |
| 8908 | 0xff, 0xff |
| 8909 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8910 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8911 | |
| 8912 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 8913 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8914 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8915 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8916 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 8917 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8918 | |
| 8919 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8920 | /* |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 8921 | * An array of one that contains a byte string that is an indefinite |
| 8922 | * length string that CBOR wraps an array of three numbers [42, 43, |
| 8923 | * 44]. The byte string is an implicit tag 24. |
| 8924 | * |
| 8925 | * [ |
| 8926 | * (_ h'83', h'18', h'2A182B', h'182C') |
| 8927 | * ] |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8928 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8929 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 8930 | 0x81, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8931 | 0x5f, |
| 8932 | 0x41, 0x83, |
| 8933 | 0x41, 0x18, |
| 8934 | 0x43, 0x2A, 0x18, 0x2B, |
| 8935 | 0x42, 0x18, 0x2C, |
| 8936 | 0xff |
| 8937 | }; |
| 8938 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8939 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8940 | #endif |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8941 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8942 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8943 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8944 | { |
| 8945 | QCBORItem Item; |
| 8946 | QCBORError nCBORError; |
| 8947 | QCBORDecodeContext DCtx; |
| 8948 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8949 | // Improvement: rework this test to use only integer labels. |
| 8950 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8951 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8952 | |
| 8953 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8954 | return 100+(int32_t)nCBORError; |
| 8955 | } |
| 8956 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8957 | return 200; |
| 8958 | } |
| 8959 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8960 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8961 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 8962 | return 150+(int32_t)nCBORError; |
| 8963 | } |
| 8964 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8965 | return 250; |
| 8966 | } |
| 8967 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8968 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8969 | return (int32_t)nCBORError; |
| 8970 | } |
| 8971 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8972 | return 300; |
| 8973 | } |
| 8974 | |
| 8975 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8976 | return 400 + (int32_t)nCBORError; |
| 8977 | } |
| 8978 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8979 | return 500; |
| 8980 | } |
| 8981 | |
| 8982 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8983 | return (int32_t)nCBORError; |
| 8984 | } |
| 8985 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8986 | return 600; |
| 8987 | } |
| 8988 | |
| 8989 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8990 | return 900 + (int32_t)nCBORError; |
| 8991 | } |
| 8992 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8993 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8994 | Item.val.int64 != 42 || |
| 8995 | Item.uDataAlloc || |
| 8996 | Item.uLabelAlloc || |
| 8997 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8998 | return 1000; |
| 8999 | } |
| 9000 | |
| 9001 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9002 | return 1100 + (int32_t)nCBORError; |
| 9003 | } |
| 9004 | |
| 9005 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9006 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9007 | Item.val.int64 != 42 || |
| 9008 | Item.uDataAlloc || |
| 9009 | Item.uLabelAlloc || |
| 9010 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9011 | return 1200; |
| 9012 | } |
| 9013 | |
| 9014 | |
| 9015 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9016 | return 1300 + (int32_t)nCBORError; |
| 9017 | } |
| 9018 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9019 | Item.uDataAlloc || |
| 9020 | Item.uLabelAlloc || |
| 9021 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9022 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9023 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9024 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9025 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9026 | |
| 9027 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9028 | return 1500 + (int32_t)nCBORError; |
| 9029 | } |
| 9030 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9031 | Item.uDataAlloc || |
| 9032 | Item.uLabelAlloc || |
| 9033 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9034 | return 1600; |
| 9035 | } |
| 9036 | |
| 9037 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 9038 | return 1700 + (int32_t)nCBORError; |
| 9039 | } |
| 9040 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9041 | Item.uDataAlloc || |
| 9042 | Item.uLabelAlloc || |
| 9043 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9044 | return 1800; |
| 9045 | } |
| 9046 | |
| 9047 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 9048 | return (int32_t)nCBORError; |
| 9049 | } |
| 9050 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9051 | Item.uDataAlloc || |
| 9052 | Item.uLabelAlloc || |
| 9053 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9054 | return 1900; |
| 9055 | } |
| 9056 | |
| 9057 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9058 | return (int32_t)nCBORError; |
| 9059 | } |
| 9060 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9061 | Item.uDataAlloc || |
| 9062 | Item.uLabelAlloc || |
| 9063 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9064 | return 2000; |
| 9065 | } |
| 9066 | |
| 9067 | |
| 9068 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9069 | return 2100 + (int32_t)nCBORError; |
| 9070 | } |
| 9071 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9072 | Item.uDataAlloc || |
| 9073 | Item.uLabelAlloc || |
| 9074 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 9075 | Item.uDataType != QCBOR_TYPE_MAP || |
| 9076 | Item.val.uCount != 4) { |
| 9077 | return 2100; |
| 9078 | } |
| 9079 | |
| 9080 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9081 | return 2200 + (int32_t)nCBORError; |
| 9082 | } |
| 9083 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9084 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 9085 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 9086 | Item.uDataAlloc || |
| 9087 | Item.uLabelAlloc || |
| 9088 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 9089 | return 2300; |
| 9090 | } |
| 9091 | |
| 9092 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 9093 | return 2400 + (int32_t)nCBORError; |
| 9094 | } |
| 9095 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9096 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 9097 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 9098 | Item.uDataAlloc || |
| 9099 | Item.uLabelAlloc || |
| 9100 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 9101 | return 2500; |
| 9102 | } |
| 9103 | |
| 9104 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9105 | return 2600 + (int32_t)nCBORError; |
| 9106 | } |
| 9107 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9108 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 9109 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 9110 | Item.uDataAlloc || |
| 9111 | Item.uLabelAlloc || |
| 9112 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 9113 | return 2700; |
| 9114 | } |
| 9115 | |
| 9116 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9117 | return 2800 + (int32_t)nCBORError; |
| 9118 | } |
| 9119 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9120 | Item.uDataAlloc || |
| 9121 | Item.uLabelAlloc || |
| 9122 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 9123 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9124 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9125 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9126 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9127 | |
| 9128 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 9129 | return 3000 + (int32_t)nCBORError; |
| 9130 | } |
| 9131 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9132 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 9133 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9134 | Item.uDataAlloc || |
| 9135 | Item.uLabelAlloc || |
| 9136 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 9137 | return 3100; |
| 9138 | } |
| 9139 | |
| 9140 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9141 | return 3200 + (int32_t)nCBORError; |
| 9142 | } |
| 9143 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9144 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 9145 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9146 | Item.uDataAlloc || |
| 9147 | Item.uLabelAlloc || |
| 9148 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 9149 | return 3300; |
| 9150 | } |
| 9151 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 9152 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 9153 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9154 | return 3300 + (int32_t)nCBORError; |
| 9155 | } |
| 9156 | |
| 9157 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 9158 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 9159 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9160 | return 3400 + (int32_t)nCBORError; |
| 9161 | } |
| 9162 | |
| 9163 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 9164 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 9165 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9166 | return 3500 + (int32_t)nCBORError; |
| 9167 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9168 | |
| 9169 | |
| 9170 | // Rewind to top level after entering several maps |
| 9171 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9172 | |
| 9173 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9174 | return (int32_t)nCBORError; |
| 9175 | } |
| 9176 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 9177 | Item.val.uCount != 3) { |
| 9178 | return 400; |
| 9179 | } |
| 9180 | |
| 9181 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9182 | return 4000+(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 4100; |
| 9192 | } |
| 9193 | |
| 9194 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9195 | return 4100+(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 4200; |
| 9204 | } |
| 9205 | |
| 9206 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9207 | return 4200+(int32_t)nCBORError; |
| 9208 | } |
| 9209 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9210 | Item.uDataAlloc || |
| 9211 | Item.uLabelAlloc || |
| 9212 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9213 | return 4300; |
| 9214 | } |
| 9215 | |
| 9216 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9217 | return 4300+(int32_t)nCBORError; |
| 9218 | } |
| 9219 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9220 | Item.uDataAlloc || |
| 9221 | Item.uLabelAlloc || |
| 9222 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9223 | return 4400; |
| 9224 | } |
| 9225 | |
| 9226 | QCBORDecode_Rewind(&DCtx); |
| 9227 | |
| 9228 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9229 | return 4400+(int32_t)nCBORError; |
| 9230 | } |
| 9231 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 9232 | Item.val.uCount != 3) { |
| 9233 | return 4500; |
| 9234 | } |
| 9235 | |
| 9236 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9237 | return (int32_t)nCBORError; |
| 9238 | } |
| 9239 | |
| 9240 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9241 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9242 | Item.val.int64 != 42 || |
| 9243 | Item.uDataAlloc || |
| 9244 | Item.uLabelAlloc || |
| 9245 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9246 | return 4600; |
| 9247 | } |
| 9248 | |
| 9249 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9250 | return (int32_t)nCBORError; |
| 9251 | } |
| 9252 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9253 | Item.uDataAlloc || |
| 9254 | Item.uLabelAlloc || |
| 9255 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9256 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9257 | Item.val.uCount != 2) { |
| 9258 | return 4700; |
| 9259 | } |
| 9260 | |
| 9261 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9262 | return (int32_t)nCBORError; |
| 9263 | } |
| 9264 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9265 | Item.uDataAlloc || |
| 9266 | Item.uLabelAlloc || |
| 9267 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9268 | return 4800; |
| 9269 | } |
| 9270 | |
| 9271 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9272 | return 4900+(int32_t)nCBORError; |
| 9273 | } |
| 9274 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9275 | Item.uDataAlloc || |
| 9276 | Item.uLabelAlloc || |
| 9277 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9278 | return 5000; |
| 9279 | } |
| 9280 | |
| 9281 | |
| 9282 | // Rewind an entered map |
| 9283 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9284 | |
| 9285 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9286 | |
| 9287 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9288 | return 5100+(int32_t)nCBORError; |
| 9289 | } |
| 9290 | |
| 9291 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9292 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9293 | Item.val.int64 != 42 || |
| 9294 | Item.uDataAlloc || |
| 9295 | Item.uLabelAlloc || |
| 9296 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9297 | return 5200; |
| 9298 | } |
| 9299 | |
| 9300 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9301 | return 5200+(int32_t)nCBORError; |
| 9302 | } |
| 9303 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9304 | Item.uDataAlloc || |
| 9305 | Item.uLabelAlloc || |
| 9306 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9307 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9308 | Item.val.uCount != 2) { |
| 9309 | return -5300; |
| 9310 | } |
| 9311 | |
| 9312 | QCBORDecode_Rewind(&DCtx); |
| 9313 | |
| 9314 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9315 | return 5300+(int32_t)nCBORError; |
| 9316 | } |
| 9317 | |
| 9318 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9319 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9320 | Item.val.int64 != 42 || |
| 9321 | Item.uDataAlloc || |
| 9322 | Item.uLabelAlloc || |
| 9323 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9324 | return 5400; |
| 9325 | } |
| 9326 | |
| 9327 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9328 | return 5400+(int32_t)nCBORError; |
| 9329 | } |
| 9330 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9331 | Item.uDataAlloc || |
| 9332 | Item.uLabelAlloc || |
| 9333 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9334 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9335 | Item.val.uCount != 2) { |
| 9336 | return 5500; |
| 9337 | } |
| 9338 | |
| 9339 | |
| 9340 | // Rewind and entered array inside an entered map |
| 9341 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9342 | |
| 9343 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9344 | |
| 9345 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 9346 | |
| 9347 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9348 | return 5600+(int32_t)nCBORError; |
| 9349 | } |
| 9350 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9351 | Item.uDataAlloc || |
| 9352 | Item.uLabelAlloc || |
| 9353 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9354 | return 5700; |
| 9355 | } |
| 9356 | |
| 9357 | QCBORDecode_Rewind(&DCtx); |
| 9358 | |
| 9359 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9360 | return 5700+(int32_t)nCBORError; |
| 9361 | } |
| 9362 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9363 | Item.uDataAlloc || |
| 9364 | Item.uLabelAlloc || |
| 9365 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9366 | return 5800; |
| 9367 | } |
| 9368 | |
| 9369 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9370 | return (int32_t)nCBORError; |
| 9371 | } |
| 9372 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9373 | Item.uDataAlloc || |
| 9374 | Item.uLabelAlloc || |
| 9375 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9376 | return 5900; |
| 9377 | } |
| 9378 | |
| 9379 | QCBORDecode_Rewind(&DCtx); |
| 9380 | |
| 9381 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9382 | return 5900+(int32_t)nCBORError; |
| 9383 | } |
| 9384 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9385 | Item.uDataAlloc || |
| 9386 | Item.uLabelAlloc || |
| 9387 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9388 | return 6000; |
| 9389 | } |
| 9390 | |
| 9391 | |
| 9392 | // Rewind a byte string inside an array inside an array |
| 9393 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 9394 | |
| 9395 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9396 | |
| 9397 | uint64_t i; |
| 9398 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9399 | |
| 9400 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9401 | |
| 9402 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9403 | if(QCBORDecode_GetError(&DCtx)) { |
| 9404 | return 6100; |
| 9405 | } |
| 9406 | |
| 9407 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9408 | return (int32_t)nCBORError; |
| 9409 | } |
| 9410 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9411 | return 6200; |
| 9412 | } |
| 9413 | |
| 9414 | QCBORDecode_Rewind(&DCtx); |
| 9415 | |
| 9416 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9417 | return 6300+(int32_t)nCBORError; |
| 9418 | } |
| 9419 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9420 | return 6400; |
| 9421 | } |
| 9422 | |
| 9423 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9424 | // Rewind a byte string inside an indefinite-length array inside |
| 9425 | // indefinite-length array |
| 9426 | |
| 9427 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 9428 | |
| 9429 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9430 | |
| 9431 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9432 | |
| 9433 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9434 | |
| 9435 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9436 | if(QCBORDecode_GetError(&DCtx)) { |
| 9437 | return 6500; |
| 9438 | } |
| 9439 | |
| 9440 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9441 | return 6600+(int32_t)nCBORError; |
| 9442 | } |
| 9443 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9444 | return 6700; |
| 9445 | } |
| 9446 | |
| 9447 | QCBORDecode_Rewind(&DCtx); |
| 9448 | |
| 9449 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9450 | return 6800+(int32_t)nCBORError; |
| 9451 | } |
| 9452 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9453 | return 6900; |
| 9454 | } |
| 9455 | #endif |
| 9456 | |
| 9457 | // Rewind an empty map |
| 9458 | // [100, {}] |
| 9459 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 9460 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9461 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9462 | if(i != 100) { |
| 9463 | return 7010; |
| 9464 | } |
| 9465 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9466 | |
| 9467 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9468 | for(int n = 0; n < 5; n++) { |
| 9469 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9470 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9471 | return 7000 + n; |
| 9472 | } |
| 9473 | QCBORDecode_Rewind(&DCtx); |
| 9474 | } |
| 9475 | QCBORDecode_ExitMap(&DCtx); |
| 9476 | QCBORDecode_Rewind(&DCtx); |
| 9477 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9478 | if(i != 100) { |
| 9479 | return 7010; |
| 9480 | } |
| 9481 | QCBORDecode_ExitArray(&DCtx); |
| 9482 | QCBORDecode_Rewind(&DCtx); |
| 9483 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9484 | i = 9; |
| 9485 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9486 | if(i != 100) { |
| 9487 | return 7020; |
| 9488 | } |
| 9489 | if(QCBORDecode_GetError(&DCtx)){ |
| 9490 | return 7030; |
| 9491 | } |
| 9492 | |
| 9493 | // Rewind an empty indefinite length map |
| 9494 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9495 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 9496 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9497 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9498 | if(i != 100) { |
| 9499 | return 7810; |
| 9500 | } |
| 9501 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9502 | |
| 9503 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9504 | for(int n = 0; n < 5; n++) { |
| 9505 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9506 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9507 | return 7800 + n; |
| 9508 | } |
| 9509 | QCBORDecode_Rewind(&DCtx); |
| 9510 | } |
| 9511 | QCBORDecode_ExitMap(&DCtx); |
| 9512 | QCBORDecode_Rewind(&DCtx); |
| 9513 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9514 | if(i != 100) { |
| 9515 | return 7810; |
| 9516 | } |
| 9517 | QCBORDecode_ExitArray(&DCtx); |
| 9518 | QCBORDecode_Rewind(&DCtx); |
| 9519 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9520 | i = 9; |
| 9521 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9522 | if(i != 100) { |
| 9523 | return 7820; |
| 9524 | } |
| 9525 | if(QCBORDecode_GetError(&DCtx)){ |
| 9526 | return 7830; |
| 9527 | } |
| 9528 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9529 | |
| 9530 | // Rewind an indefnite length byte-string wrapped sequence |
| 9531 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 9532 | QCBORDecode_Init(&DCtx, |
| 9533 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 9534 | 0); |
| 9535 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 9536 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 9537 | |
| 9538 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9539 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9540 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9541 | return 7300; |
| 9542 | } |
| 9543 | |
| 9544 | /* |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9545 | Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on |
| 9546 | allocated strings. This is a fairly big job because of all the |
| 9547 | UsefulBuf internal book keeping that needs tweaking. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9548 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9549 | if(i != 42) { |
| 9550 | return 7110; |
| 9551 | } |
| 9552 | QCBORDecode_Rewind(&DCtx); |
| 9553 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9554 | if(i != 42) { |
| 9555 | return 7220; |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9556 | } |
| 9557 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 9558 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9559 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9560 | |
| 9561 | |
| 9562 | // Rewind an indefnite length byte-string wrapped sequence |
| 9563 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9564 | return 0; |
| 9565 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9566 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9567 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9568 | |
| 9569 | |
| 9570 | static const uint8_t spBooleansInMap[] = |
| 9571 | { |
| 9572 | 0xa1, 0x08, 0xf5 |
| 9573 | }; |
| 9574 | |
| 9575 | static const uint8_t spBooleansInMapWrongType[] = |
| 9576 | { |
| 9577 | 0xa1, 0x08, 0xf6 |
| 9578 | }; |
| 9579 | |
| 9580 | static const uint8_t spBooleansInMapNWF[] = |
| 9581 | { |
| 9582 | 0xa1, 0x08, 0x1a |
| 9583 | }; |
| 9584 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9585 | static const uint8_t spNullInMap[] = |
| 9586 | { |
| 9587 | 0xa1, 0x08, 0xf6 |
| 9588 | }; |
| 9589 | |
| 9590 | static const uint8_t spUndefinedInMap[] = |
| 9591 | { |
| 9592 | 0xa1, 0x08, 0xf7 |
| 9593 | }; |
| 9594 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9595 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 9596 | #ifndef QCBOR_DISABLE_TAGS |
| 9597 | static const uint8_t spTaggedSimples[] = |
| 9598 | { |
| 9599 | 0xd8, 0x58, 0xd8, 0x2c, 0xd6, 0xf5, |
| 9600 | 0xd9, 0x0f, 0xA0, 0xf7 |
| 9601 | }; |
| 9602 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 9603 | |
| 9604 | |
| 9605 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9606 | int32_t BoolTest(void) |
| 9607 | { |
| 9608 | QCBORDecodeContext DCtx; |
| 9609 | bool b; |
| 9610 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9611 | QCBORDecode_Init(&DCtx, |
| 9612 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9613 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9614 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9615 | QCBORDecode_GetBool(&DCtx, &b); |
| 9616 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9617 | return 1; |
| 9618 | } |
| 9619 | |
| 9620 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 9621 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9622 | return 2; |
| 9623 | } |
| 9624 | |
| 9625 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 9626 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9627 | return 3; |
| 9628 | } |
| 9629 | |
| 9630 | |
| 9631 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 9632 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9633 | return 4; |
| 9634 | } |
| 9635 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9636 | QCBORDecode_Init(&DCtx, |
| 9637 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 9638 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9639 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9640 | QCBORDecode_GetBool(&DCtx, &b); |
| 9641 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9642 | return 5; |
| 9643 | } |
| 9644 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9645 | QCBORDecode_Init(&DCtx, |
| 9646 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9647 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9648 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9649 | QCBORDecode_GetBool(&DCtx, &b); |
| 9650 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9651 | return 6; |
| 9652 | } |
| 9653 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9654 | |
| 9655 | QCBORDecode_Init(&DCtx, |
| 9656 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9657 | 0); |
| 9658 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9659 | QCBORDecode_GetNull(&DCtx); |
| 9660 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9661 | return 7; |
| 9662 | } |
| 9663 | |
| 9664 | QCBORDecode_Init(&DCtx, |
| 9665 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9666 | 0); |
| 9667 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9668 | QCBORDecode_GetNull(&DCtx); |
| 9669 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9670 | return 8; |
| 9671 | } |
| 9672 | |
| 9673 | QCBORDecode_Init(&DCtx, |
| 9674 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9675 | 0); |
| 9676 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9677 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9678 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9679 | return 9; |
| 9680 | } |
| 9681 | |
| 9682 | QCBORDecode_Init(&DCtx, |
| 9683 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9684 | 0); |
| 9685 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9686 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9687 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9688 | return 10; |
| 9689 | } |
| 9690 | |
| 9691 | QCBORDecode_Init(&DCtx, |
| 9692 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9693 | 0); |
| 9694 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9695 | QCBORDecode_GetUndefined(&DCtx); |
| 9696 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9697 | return 11; |
| 9698 | } |
| 9699 | |
| 9700 | QCBORDecode_Init(&DCtx, |
| 9701 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9702 | 0); |
| 9703 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9704 | QCBORDecode_GetUndefined(&DCtx); |
| 9705 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9706 | return 12; |
| 9707 | } |
| 9708 | |
| 9709 | QCBORDecode_Init(&DCtx, |
| 9710 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9711 | 0); |
| 9712 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9713 | QCBORDecode_GetUndefined(&DCtx); |
| 9714 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9715 | return 13; |
| 9716 | } |
| 9717 | |
| 9718 | QCBORDecode_Init(&DCtx, |
| 9719 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9720 | 0); |
| 9721 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9722 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9723 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9724 | return 14; |
| 9725 | } |
| 9726 | |
| 9727 | QCBORDecode_Init(&DCtx, |
| 9728 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9729 | 0); |
| 9730 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9731 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9732 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9733 | return 15; |
| 9734 | } |
| 9735 | |
| 9736 | QCBORDecode_Init(&DCtx, |
| 9737 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9738 | 0); |
| 9739 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9740 | QCBORDecode_GetUndefined(&DCtx); |
| 9741 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9742 | return 15; |
| 9743 | } |
| 9744 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 9745 | #ifndef QCBOR_DISABLE_TAGS |
| 9746 | QCBORDecode_Init(&DCtx, |
| 9747 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedSimples), |
| 9748 | 0); |
| 9749 | QCBORDecode_GetBool(&DCtx, &b); |
| 9750 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 22) { |
| 9751 | return 401; |
| 9752 | } |
| 9753 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 44) { |
| 9754 | return 402; |
| 9755 | } |
| 9756 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 88) { |
| 9757 | return 403; |
| 9758 | } |
| 9759 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 9760 | return 404; |
| 9761 | } |
| 9762 | QCBORDecode_GetUndefined(&DCtx); |
| 9763 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 4000) { |
| 9764 | return 405; |
| 9765 | } |
| 9766 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != CBOR_TAG_INVALID64) { |
| 9767 | return 406; |
| 9768 | } |
| 9769 | QCBORDecode_GetNull(&DCtx); /* Off the end */ |
| 9770 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 9771 | return 407; |
| 9772 | } |
| 9773 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 9774 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9775 | return 0; |
| 9776 | } |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9777 | |
| 9778 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9779 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9780 | static const uint8_t spExpectedArray2s[] = { |
| 9781 | 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9782 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9783 | 0x32}; |
| 9784 | |
| 9785 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9786 | static const uint8_t spExpectedArray2sIndef[] = { |
| 9787 | 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9788 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9789 | 0x32, 0xff}; |
| 9790 | #endif |
| 9791 | |
| 9792 | static const uint8_t spExpectedMap4[] = { |
| 9793 | 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9794 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9795 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9796 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9797 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9798 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9799 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9800 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9801 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9802 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73}; |
| 9803 | |
| 9804 | |
| 9805 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9806 | |
| 9807 | static const uint8_t spExpectedMap4Indef[] = { |
| 9808 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9809 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9810 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9811 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9812 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9813 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9814 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9815 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9816 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9817 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 9818 | 0xff}; |
| 9819 | |
| 9820 | /* |
| 9821 | * [[[[[0, []]]]], 0] |
| 9822 | */ |
| 9823 | static const uint8_t spDefAndIndef[] = { |
| 9824 | 0x82, |
| 9825 | 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00 |
| 9826 | }; |
| 9827 | #endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9828 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9829 | #ifndef QCBOR_DISABLE_TAGS |
| 9830 | /* An exp / mant tag in two nested arrays */ |
| 9831 | static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03}; |
| 9832 | #endif /* !QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9833 | #endif |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9834 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9835 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9836 | int32_t GetMapAndArrayTest(void) |
| 9837 | { |
| 9838 | QCBORDecodeContext DCtx; |
| 9839 | size_t uPosition ; |
| 9840 | QCBORItem Item; |
| 9841 | UsefulBufC ReturnedEncodedCBOR; |
| 9842 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9843 | // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9844 | QCBORDecode_Init(&DCtx, |
| 9845 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9846 | 0); |
| 9847 | |
| 9848 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9849 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9850 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9851 | if(QCBORDecode_GetError(&DCtx)) { |
| 9852 | return 1; |
| 9853 | } |
| 9854 | if(Item.val.uCount != 2) { |
| 9855 | return 2; |
| 9856 | } |
| 9857 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9858 | return 3; |
| 9859 | } |
| 9860 | |
| 9861 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9862 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9863 | return 4; |
| 9864 | } |
| 9865 | |
| 9866 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9867 | |
| 9868 | |
| 9869 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9870 | if(QCBORDecode_GetError(&DCtx)) { |
| 9871 | return 10; |
| 9872 | } |
| 9873 | if(Item.val.uCount != 4) { |
| 9874 | return 11; |
| 9875 | } |
| 9876 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9877 | return 12; |
| 9878 | } |
| 9879 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9880 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 9881 | "an array of two strings", |
| 9882 | &Item, |
| 9883 | &ReturnedEncodedCBOR); |
| 9884 | if(QCBORDecode_GetError(&DCtx)) { |
| 9885 | return 20; |
| 9886 | } |
| 9887 | if(Item.val.uCount != 2) { |
| 9888 | return 21; |
| 9889 | } |
| 9890 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9891 | return 22; |
| 9892 | } |
| 9893 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9894 | return 23; |
| 9895 | } |
| 9896 | |
| 9897 | QCBORDecode_Rewind(&DCtx); |
| 9898 | |
| 9899 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9900 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9901 | if(QCBORDecode_GetError(&DCtx)) { |
| 9902 | return 30; |
| 9903 | } |
| 9904 | if(Item.val.uCount != 4) { |
| 9905 | return 31; |
| 9906 | } |
| 9907 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9908 | return 32; |
| 9909 | } |
| 9910 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9911 | return 33; |
| 9912 | } |
| 9913 | |
| 9914 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9915 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9916 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9917 | return 40; |
| 9918 | } |
| 9919 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 9920 | return 41; |
| 9921 | } |
| 9922 | QCBORDecode_GetAndResetError(&DCtx); |
| 9923 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9924 | return 42; |
| 9925 | } |
| 9926 | |
| 9927 | |
| 9928 | #ifndef QCBOR_DISABLE_TAGS |
| 9929 | UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant); |
| 9930 | QCBORDecode_Init(&DCtx, ExpMant, 0); |
| 9931 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9932 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9933 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9934 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9935 | return 200; |
| 9936 | } |
| 9937 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 9938 | return 201; |
| 9939 | } |
| 9940 | if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) { |
| 9941 | return 202; |
| 9942 | } |
| 9943 | if(Item.val.uCount != 2) { |
| 9944 | return 201; |
| 9945 | } |
| 9946 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) { |
| 9947 | return 205; |
| 9948 | } |
| 9949 | #endif /* !QCBOR_DISABLE_TAGS */ |
| 9950 | |
| 9951 | |
| 9952 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9953 | |
| 9954 | UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef); |
| 9955 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9956 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9957 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9958 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9959 | return 50; |
| 9960 | } |
| 9961 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) { |
| 9962 | return 51; |
| 9963 | } |
| 9964 | |
| 9965 | QCBORDecode_Init(&DCtx, DefAndIndef, 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 52; |
| 9971 | } |
| 9972 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) { |
| 9973 | return 53; |
| 9974 | } |
| 9975 | |
| 9976 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9977 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9978 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9979 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9980 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9981 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9982 | return 54; |
| 9983 | } |
| 9984 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) { |
| 9985 | return 55; |
| 9986 | } |
| 9987 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9988 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9989 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9990 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9991 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9992 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9993 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9994 | return 56; |
| 9995 | } |
| 9996 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) { |
| 9997 | return 57; |
| 9998 | } |
| 9999 | |
| 10000 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 10001 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10002 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10003 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10004 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10005 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 10006 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 10007 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10008 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10009 | return 58; |
| 10010 | } |
| 10011 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) { |
| 10012 | return 59; |
| 10013 | } |
| 10014 | |
| 10015 | |
| 10016 | QCBORDecode_Init(&DCtx, |
| 10017 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10018 | 0); |
| 10019 | |
| 10020 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 10021 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 10022 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10023 | if(QCBORDecode_GetError(&DCtx)) { |
| 10024 | return 60; |
| 10025 | } |
| 10026 | if(Item.val.uCount != UINT16_MAX) { |
| 10027 | return 61; |
| 10028 | } |
| 10029 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 10030 | return 62; |
| 10031 | } |
| 10032 | |
| 10033 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 10034 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 10035 | return 63; |
| 10036 | } |
| 10037 | |
| 10038 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10039 | |
| 10040 | |
| 10041 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 10042 | if(QCBORDecode_GetError(&DCtx)) { |
| 10043 | return 70; |
| 10044 | } |
| 10045 | if(Item.val.uCount != UINT16_MAX) { |
| 10046 | return 71; |
| 10047 | } |
| 10048 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 10049 | return 72; |
| 10050 | } |
| 10051 | |
| 10052 | |
| 10053 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10054 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 10055 | "an array of two strings", |
| 10056 | &Item, |
| 10057 | &ReturnedEncodedCBOR); |
| 10058 | if(QCBORDecode_GetError(&DCtx)) { |
| 10059 | return 80; |
| 10060 | } |
| 10061 | if(Item.val.uCount != UINT16_MAX) { |
| 10062 | return 81; |
| 10063 | } |
| 10064 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 10065 | return 82; |
| 10066 | } |
| 10067 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10068 | return 83; |
| 10069 | } |
| 10070 | |
| 10071 | QCBORDecode_Rewind(&DCtx); |
| 10072 | |
| 10073 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10074 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 10075 | if(QCBORDecode_GetError(&DCtx)) { |
| 10076 | return 90; |
| 10077 | } |
| 10078 | if(Item.val.uCount != UINT16_MAX) { |
| 10079 | return 91; |
| 10080 | } |
| 10081 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 10082 | return 92; |
| 10083 | } |
| 10084 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10085 | return 93; |
| 10086 | } |
| 10087 | |
| 10088 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10089 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 10090 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10091 | return 100; |
| 10092 | } |
| 10093 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 10094 | return 101; |
| 10095 | } |
| 10096 | QCBORDecode_GetAndResetError(&DCtx); |
| 10097 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10098 | return 102; |
| 10099 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10100 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 10101 | |
| 10102 | return 0; |
| 10103 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10104 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 10105 | |
| 10106 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 10107 | int32_t |
| 10108 | ErrorHandlingTests(void) |
| 10109 | { |
| 10110 | QCBORDecodeContext DCtx; |
| 10111 | QCBORItem Item; |
| 10112 | QCBORError uError; |
| 10113 | int64_t integer; |
| 10114 | |
| 10115 | /* Test QCBORDecode_SetError() */ |
| 10116 | QCBORDecode_Init(&DCtx, |
| 10117 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10118 | QCBOR_DECODE_MODE_NORMAL); |
| 10119 | |
| 10120 | QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED); |
| 10121 | |
| 10122 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10123 | |
| 10124 | uError = QCBORDecode_GetError(&DCtx); |
| 10125 | |
| 10126 | if(uError != QCBOR_ERR_FIRST_USER_DEFINED) { |
| 10127 | return -1; |
| 10128 | } |
| 10129 | |
| 10130 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 10131 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 10132 | return -2; |
| 10133 | } |
| 10134 | |
| 10135 | |
| 10136 | /* Test data type returned from previous error */ |
| 10137 | QCBORDecode_Init(&DCtx, |
| 10138 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10139 | QCBOR_DECODE_MODE_NORMAL); |
| 10140 | QCBORDecode_GetInt64(&DCtx, &integer); |
| 10141 | uError = QCBORDecode_GetError(&DCtx); |
| 10142 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10143 | return -3; |
| 10144 | } |
| 10145 | |
| 10146 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10147 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 10148 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 10149 | return -2; |
| 10150 | } |
| 10151 | uError = QCBORDecode_GetError(&DCtx); |
| 10152 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10153 | return -3; |
| 10154 | } |
| 10155 | |
| 10156 | |
| 10157 | /* Test error classification functions */ |
| 10158 | |
| 10159 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 10160 | return -10; |
| 10161 | } |
| 10162 | if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) { |
| 10163 | return -11; |
| 10164 | } |
| 10165 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 10166 | return -12; |
| 10167 | } |
| 10168 | if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) { |
| 10169 | return -13; |
| 10170 | } |
| 10171 | |
| 10172 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) { |
| 10173 | return -20; |
| 10174 | } |
| 10175 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) { |
| 10176 | return -21; |
| 10177 | } |
| 10178 | if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) { |
| 10179 | return -22; |
| 10180 | } |
| 10181 | if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) { |
| 10182 | return -23; |
| 10183 | } |
| 10184 | |
Laurence Lundblade | cd34e58 | 2024-06-06 10:55:14 -0700 | [diff] [blame] | 10185 | /* Test error strings */ |
| 10186 | const char *szErrString; |
| 10187 | |
| 10188 | szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG); |
| 10189 | if(szErrString == NULL) { |
| 10190 | return -100; |
| 10191 | } |
| 10192 | if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) { |
| 10193 | return -101; |
| 10194 | } |
| 10195 | |
| 10196 | szErrString = qcbor_err_to_str(QCBOR_SUCCESS); |
| 10197 | if(szErrString == NULL) { |
| 10198 | return -102; |
| 10199 | } |
| 10200 | if(strcmp(szErrString, "QCBOR_SUCCESS")) { |
| 10201 | return -103; |
| 10202 | } |
| 10203 | |
| 10204 | szErrString = qcbor_err_to_str(100); |
| 10205 | if(szErrString == NULL) { |
| 10206 | return -104; |
| 10207 | } |
| 10208 | if(strcmp(szErrString, "Unidentified QCBOR error")) { |
| 10209 | return -105; |
| 10210 | } |
| 10211 | |
| 10212 | szErrString = qcbor_err_to_str(200); |
| 10213 | if(szErrString == NULL) { |
| 10214 | return -106; |
| 10215 | } |
| 10216 | if(strcmp(szErrString, "USER_DEFINED_200")) { |
| 10217 | return -107; |
| 10218 | } |
| 10219 | |
Laurence Lundblade | e222674 | 2024-08-16 10:50:23 -0700 | [diff] [blame] | 10220 | QCBORDecode_Init(&DCtx, |
| 10221 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10222 | QCBOR_DECODE_MODE_NORMAL); |
| 10223 | |
| 10224 | UsefulBufC Xx = QCBORDecode_RetrieveUndecodedInput(&DCtx); |
| 10225 | if(Xx.ptr != pValidMapEncoded) { |
| 10226 | return -200; |
| 10227 | } |
| 10228 | if(Xx.len != sizeof(pValidMapEncoded)) { |
| 10229 | return -201; |
| 10230 | } |
| 10231 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 10232 | return 0; |
| 10233 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10234 | |
| 10235 | |
| 10236 | int32_t TellTests(void) |
| 10237 | { |
| 10238 | QCBORDecodeContext DCtx; |
| 10239 | QCBORItem Item; |
| 10240 | uint32_t uPosition; |
| 10241 | int nIndex; |
| 10242 | int64_t nDecodedInt; |
| 10243 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10244 | // Improvement: rewrite so this can run with only integer labels |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10245 | static const uint32_t aPos[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10246 | {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, 151}; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10247 | QCBORDecode_Init(&DCtx, |
| 10248 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10249 | 0); |
| 10250 | for(nIndex = 0; ; nIndex++) { |
| 10251 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10252 | if(uPosition != aPos[nIndex]) { |
| 10253 | return nIndex; |
| 10254 | } |
| 10255 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10256 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10257 | break; |
| 10258 | } |
| 10259 | |
| 10260 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10261 | } |
| 10262 | |
| 10263 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10264 | static const uint32_t aPosIndef[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10265 | {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, 154}; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10266 | QCBORDecode_Init(&DCtx, |
| 10267 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10268 | 0); |
| 10269 | |
| 10270 | for(nIndex = 0; ; nIndex++) { |
| 10271 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10272 | if(uPosition != aPosIndef[nIndex]) { |
| 10273 | return nIndex + 100; |
| 10274 | } |
| 10275 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10276 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10277 | break; |
| 10278 | } |
| 10279 | |
| 10280 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10281 | } |
| 10282 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10283 | |
| 10284 | /* Next, some tests with entered maps and arrays */ |
| 10285 | QCBORDecode_Init(&DCtx, |
| 10286 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10287 | 0); |
| 10288 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10289 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10290 | return 1001; |
| 10291 | } |
| 10292 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10293 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10294 | return 1002; |
| 10295 | } |
| 10296 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10297 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10298 | return 1003; |
| 10299 | } |
| 10300 | |
| 10301 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10302 | if(nDecodedInt != 98) { |
| 10303 | return 1004; |
| 10304 | } |
| 10305 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10306 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10307 | return 1005; |
| 10308 | } |
| 10309 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10310 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10311 | return 1006; |
| 10312 | } |
| 10313 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10314 | if(nDecodedInt != 98) { |
| 10315 | return 1007; |
| 10316 | } |
| 10317 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10318 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10319 | return 1008; |
| 10320 | } |
| 10321 | |
| 10322 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10323 | if(QCBORDecode_Tell(&DCtx) != 151) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10324 | return 1009; |
| 10325 | } |
| 10326 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10327 | return 1010; |
| 10328 | } |
| 10329 | |
| 10330 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10331 | /* Next, some tests with entered maps and arrays */ |
| 10332 | QCBORDecode_Init(&DCtx, |
| 10333 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10334 | 0); |
| 10335 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10336 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10337 | return 2000; |
| 10338 | } |
| 10339 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10340 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10341 | return 2001; |
| 10342 | } |
| 10343 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10344 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10345 | return 2002; |
| 10346 | } |
| 10347 | |
| 10348 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10349 | if(nDecodedInt != 98) { |
| 10350 | return 2003; |
| 10351 | } |
| 10352 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10353 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10354 | return 2004; |
| 10355 | } |
| 10356 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10357 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10358 | return 2005; |
| 10359 | } |
| 10360 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10361 | if(nDecodedInt != 98) { |
| 10362 | return 2006; |
| 10363 | } |
| 10364 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10365 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10366 | return 2007; |
| 10367 | } |
| 10368 | |
| 10369 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10370 | if(QCBORDecode_Tell(&DCtx) != 154) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10371 | return 2008; |
| 10372 | } |
| 10373 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10374 | return 2010; |
| 10375 | } |
| 10376 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10377 | |
| 10378 | |
| 10379 | |
| 10380 | /* Error state test */ |
| 10381 | QCBORDecode_Init(&DCtx, |
| 10382 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10383 | 0); |
| 10384 | /* Cause an error */ |
| 10385 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10386 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10387 | return 3000; |
| 10388 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10389 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_MAP_NOT_ENTERED) { |
| 10390 | return 3001; |
| 10391 | } |
| 10392 | |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10393 | |
| 10394 | /* Empties tests */ |
| 10395 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 10396 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 10397 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10398 | return 4000; |
| 10399 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10400 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_SUCCESS) { |
| 10401 | return 4008; |
| 10402 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10403 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10404 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10405 | return 4001; |
| 10406 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10407 | if(QCBORDecode_Tell(&DCtx) != 1) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10408 | return 4002; |
| 10409 | } |
| 10410 | QCBORDecode_ExitMap(&DCtx); |
| 10411 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10412 | return 4003; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10413 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10414 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10415 | return 4004; |
| 10416 | } |
| 10417 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10418 | return 4005; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10419 | } |
| 10420 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10421 | return 4010; |
| 10422 | } |
| 10423 | |
| 10424 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10425 | const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map |
| 10426 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0); |
| 10427 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10428 | return 4100; |
| 10429 | } |
| 10430 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10431 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10432 | return 4101; |
| 10433 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10434 | if(QCBORDecode_Tell(&DCtx) != 2) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10435 | return 4102; |
| 10436 | } |
| 10437 | QCBORDecode_ExitMap(&DCtx); |
| 10438 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10439 | return 4103; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10440 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10441 | if(QCBORDecode_Tell(&DCtx) != 2) { |
| 10442 | return 4104; |
| 10443 | } |
| 10444 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10445 | return 4005; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10446 | } |
| 10447 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10448 | return 4110; |
| 10449 | } |
| 10450 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10451 | |
| 10452 | /* Test on a CBOR sequence */ |
| 10453 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0); |
| 10454 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10455 | return 5000; |
| 10456 | } |
| 10457 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10458 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10459 | return 5001; |
| 10460 | } |
| 10461 | if(QCBORDecode_Tell(&DCtx) != 11) { |
| 10462 | return 5002; |
| 10463 | } |
| 10464 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10465 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10466 | return 5003; |
| 10467 | } |
| 10468 | if(QCBORDecode_Tell(&DCtx) != 12) { |
| 10469 | return 5004; |
| 10470 | } |
| 10471 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10472 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10473 | return 5005; |
| 10474 | } |
| 10475 | if(QCBORDecode_Tell(&DCtx) != 17) { |
| 10476 | return 5006; |
| 10477 | } |
| 10478 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10479 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10480 | return 5007; |
| 10481 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10482 | if(QCBORDecode_Tell(&DCtx) != 20) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10483 | return 5008; |
| 10484 | } |
| 10485 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10486 | return 5010; |
| 10487 | } |
| 10488 | |
| 10489 | |
| 10490 | QCBORDecode_Init(&DCtx, |
| 10491 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10492 | 0); |
| 10493 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10494 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 10495 | if(QCBORDecode_Tell(&DCtx) != 42) { |
| 10496 | return 6001; |
| 10497 | } |
| 10498 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10499 | if(QCBORDecode_Tell(&DCtx) != 50) { |
| 10500 | return 6002; |
| 10501 | } |
| 10502 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10503 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10504 | return 6008; |
| 10505 | } |
| 10506 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10507 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 10508 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10509 | return 6003; |
| 10510 | } |
| 10511 | QCBORDecode_ExitArray(&DCtx); |
| 10512 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10513 | return 6004; |
| 10514 | } |
| 10515 | |
| 10516 | static const uint32_t aEmptiesPos[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10517 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15}; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10518 | QCBORDecode_Init(&DCtx, |
| 10519 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 10520 | 0); |
| 10521 | for(nIndex = 0; ; nIndex++) { |
| 10522 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10523 | if(uPosition != aEmptiesPos[nIndex]) { |
| 10524 | return nIndex + 200; |
| 10525 | } |
| 10526 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10527 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10528 | break; |
| 10529 | } |
| 10530 | |
| 10531 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10532 | } |
| 10533 | |
| 10534 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10535 | static const uint32_t aIndefEmptiesPos[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10536 | {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, 25}; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10537 | QCBORDecode_Init(&DCtx, |
| 10538 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 10539 | 0); |
| 10540 | for(nIndex = 0; ; nIndex++) { |
| 10541 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10542 | if(uPosition != aIndefEmptiesPos[nIndex]) { |
| 10543 | return nIndex + 300; |
| 10544 | } |
| 10545 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10546 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10547 | break; |
| 10548 | } |
| 10549 | |
| 10550 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10551 | } |
| 10552 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10553 | |
| 10554 | |
| 10555 | return 0; |
| 10556 | } |