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 | |
| 2300 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2301 | int32_t ParseMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2302 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2303 | // Parse a moderatly complex map structure very thoroughly |
| 2304 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 2305 | if(nResult) { |
| 2306 | return nResult; |
| 2307 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2308 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2309 | // Again, but in strings-only mode. It should succeed since the input |
| 2310 | // map has only string labels. |
| 2311 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 2312 | if(nResult) { |
| 2313 | return nResult; |
| 2314 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2315 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2316 | // Again, but try to finish the decoding before the end of the |
| 2317 | // input at 10 different place and see that the right error code |
| 2318 | // is returned. |
| 2319 | for(int i = 0; i < 10; i++) { |
| 2320 | nResult = ExtraBytesTest(i); |
| 2321 | if(nResult) { |
| 2322 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 2323 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2324 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2325 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2326 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2327 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2328 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2329 | |
| 2330 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2331 | /* The simple-values including some not well formed */ |
| 2332 | static const uint8_t spSimpleValues[] = { |
| 2333 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 2334 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 2335 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2336 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2337 | /* A map of good simple values, plus one well-formed integer */ |
| 2338 | static const uint8_t spGoodSimpleValues[] = { |
| 2339 | 0xa9, 0x01, 0xf4, 0x02, 0xf5, 0x03, 0xf6, 0x04, 0xf7, |
| 2340 | 0x05, 0xe0, 0x06, 0xf3, 0x07, 0xf8, 0x20, 0x61, 0x40, |
| 2341 | 0xf8, 0xff, 0x0f, 0x0f}; |
| 2342 | |
| 2343 | int32_t SimpleValueDecodeTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2344 | { |
| 2345 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2346 | QCBORItem Item; |
| 2347 | QCBORError uErr; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2348 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2349 | QCBORDecode_Init(&DCtx, |
| 2350 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2351 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2352 | |
| 2353 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2354 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2355 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2356 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2357 | Item.val.uCount != 10) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2358 | return 1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2359 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2360 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2361 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2362 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2363 | return 2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2364 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2365 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2366 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2367 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2368 | return 3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2369 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2370 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2371 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2372 | if(Item.uDataType != QCBOR_TYPE_NULL) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2373 | return 4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2374 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2375 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2376 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2377 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2378 | return 5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2379 | |
| 2380 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 2381 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2382 | return 6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2383 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2384 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2385 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2386 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2387 | return 7; |
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 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2390 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2391 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2392 | return 8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2393 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2394 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2395 | return 9; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2396 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2397 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2398 | return 10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2399 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2400 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2401 | return 11; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2402 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2403 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2404 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2405 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2406 | return 12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2407 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2408 | if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2409 | return (int32_t)uErr; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2410 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2411 | return 13; |
| 2412 | |
| 2413 | |
| 2414 | QCBORDecode_Init(&DCtx, |
| 2415 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues), |
| 2416 | QCBOR_DECODE_MODE_NORMAL); |
| 2417 | |
| 2418 | uint8_t uSimple; |
| 2419 | |
| 2420 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 2421 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2422 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) { |
| 2423 | return 20; |
| 2424 | } |
| 2425 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2426 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) { |
| 2427 | return 21; |
| 2428 | } |
| 2429 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2430 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) { |
| 2431 | return 22; |
| 2432 | } |
| 2433 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2434 | if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) { |
| 2435 | return 23; |
| 2436 | } |
| 2437 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2438 | if(QCBORDecode_GetError(&DCtx) || uSimple != 0) { |
| 2439 | return 24; |
| 2440 | } |
| 2441 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2442 | if(QCBORDecode_GetError(&DCtx) || uSimple != 19) { |
| 2443 | return 25; |
| 2444 | } |
| 2445 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2446 | if(QCBORDecode_GetError(&DCtx) || uSimple != 32) { |
| 2447 | return 26; |
| 2448 | } |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2449 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 2450 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2451 | if(QCBORDecode_GetError(&DCtx) || uSimple != 255) { |
| 2452 | return 27; |
| 2453 | } |
| 2454 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 2455 | QCBORDecode_GetSimple(&DCtx, &uSimple); |
| 2456 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2457 | return 28; |
| 2458 | } |
| 2459 | |
| 2460 | QCBORDecode_Rewind(&DCtx); |
| 2461 | |
| 2462 | QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple); |
| 2463 | if(QCBORDecode_GetError(&DCtx) || uSimple != 19) { |
| 2464 | return 30; |
| 2465 | } |
| 2466 | |
| 2467 | QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple); |
| 2468 | if(QCBORDecode_GetError(&DCtx) || uSimple != 255) { |
| 2469 | return 31; |
| 2470 | } |
| 2471 | |
| 2472 | QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple); |
| 2473 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 2474 | return 32; |
| 2475 | } |
| 2476 | |
| 2477 | QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple); |
| 2478 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 2479 | return 33; |
| 2480 | } |
| 2481 | |
| 2482 | QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple); |
| 2483 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 2484 | return 34; |
| 2485 | } |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2486 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2487 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2488 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2492 | int32_t NotWellFormedTests(void) |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2493 | { |
| 2494 | // Loop over all the not-well-formed instance of CBOR |
| 2495 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2496 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 2497 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2498 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 2499 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 2500 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 2501 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2502 | // Set up decoder context. String allocator needed for indefinite |
| 2503 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2504 | QCBORDecodeContext DCtx; |
| 2505 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2506 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2507 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 2508 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2509 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2510 | |
| 2511 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2512 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2513 | do { |
| 2514 | QCBORItem Item; |
| 2515 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2516 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2517 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2518 | |
| 2519 | // Every test vector must fail with |
| 2520 | // a not-well-formed error. If not |
| 2521 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2522 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2523 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2524 | /* Return index of failure and QCBOR error in the result */ |
| 2525 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2526 | } |
| 2527 | } |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
| 2531 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2532 | struct DecodeFailTestInput { |
| 2533 | const char *szDescription; /* Description of the test */ |
| 2534 | QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */ |
| 2535 | UsefulBufC Input; /* Chunk of CBOR that cases error */ |
| 2536 | QCBORError nError; /* The expected error */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2537 | }; |
| 2538 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2539 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2540 | static int32_t |
| 2541 | ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2542 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2543 | int nIndex; |
| 2544 | QCBORDecodeContext DCtx; |
| 2545 | QCBORError uCBORError; |
| 2546 | QCBORItem Item; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2547 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2548 | for(nIndex = 0; nIndex < nNumFails; nIndex++) { |
| 2549 | const struct DecodeFailTestInput *pF = &pFailInputs[nIndex]; |
| 2550 | |
| 2551 | QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2552 | |
| 2553 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2554 | /* Set up the decoding context including a memory pool so that |
| 2555 | * indefinite length items can be checked. |
| 2556 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2557 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2558 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2559 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2560 | if(uCBORError != QCBOR_SUCCESS) { |
| 2561 | return -1; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2562 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2563 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2564 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2565 | if(nIndex == 4) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2566 | uCBORError = 9; /* For setting break points */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2569 | /* Iterate until there is an error of some sort */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2570 | do { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2571 | /* Set to something non-zero, something other than QCBOR_TYPE_NONE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2572 | memset(&Item, 0x33, sizeof(Item)); |
| 2573 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2574 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2575 | } while(uCBORError == QCBOR_SUCCESS); |
| 2576 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2577 | /* Must get the expected error or the this test fails. |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2578 | * The data and label type must also be QCBOR_TYPE_NONE. |
| 2579 | */ |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2580 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2581 | Item.uDataType != QCBOR_TYPE_NONE || |
| 2582 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2583 | return (int32_t)(nIndex * 1000 + (int)uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2591 | static const struct DecodeFailTestInput Failures[] = { |
| 2592 | /* Most of this is copied from not_well_formed.h. Here the error |
| 2593 | * code returned is also checked. |
| 2594 | */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2595 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2596 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2597 | /* Indefinite length strings must be closed off */ |
| 2598 | { "An indefinite length byte string not closed off", |
| 2599 | QCBOR_DECODE_MODE_NORMAL, |
| 2600 | {"0x5f\x41\x00", 3}, |
| 2601 | QCBOR_ERR_HIT_END |
| 2602 | }, |
| 2603 | { "An indefinite length text string not closed off", |
| 2604 | QCBOR_DECODE_MODE_NORMAL, |
| 2605 | {"\x7f\x61\x00", 3}, |
| 2606 | QCBOR_ERR_HIT_END |
| 2607 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2608 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2609 | /* All the chunks in an indefinite length string must be of the |
| 2610 | * type of indefinite length string |
| 2611 | */ |
| 2612 | { "Indefinite length byte string with text string chunk", |
| 2613 | QCBOR_DECODE_MODE_NORMAL, |
| 2614 | {"\x5f\x61\x00\xff", 4}, |
| 2615 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2616 | }, |
| 2617 | { "Indefinite length text string with a byte string chunk", |
| 2618 | QCBOR_DECODE_MODE_NORMAL, |
| 2619 | {"\x7f\x41\x00\xff", 4}, |
| 2620 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2621 | }, |
| 2622 | { "Indefinite length byte string with a positive integer chunk", |
| 2623 | QCBOR_DECODE_MODE_NORMAL, |
| 2624 | {"\x5f\x00\xff", 3}, |
| 2625 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2626 | }, |
| 2627 | { "Indefinite length byte string with an negative integer chunk", |
| 2628 | QCBOR_DECODE_MODE_NORMAL, |
| 2629 | {"\x5f\x21\xff", 3}, |
| 2630 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2631 | }, |
| 2632 | { "Indefinite length byte string with an array chunk", |
| 2633 | QCBOR_DECODE_MODE_NORMAL, |
| 2634 | {"\x5f\x80\xff", 3}, |
| 2635 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2636 | }, |
| 2637 | { "Indefinite length byte string with an map chunk", |
| 2638 | QCBOR_DECODE_MODE_NORMAL, |
| 2639 | {"\x5f\xa0\xff", 3}, |
| 2640 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2641 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2642 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2643 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2644 | { "Indefinite length byte string with tagged integer chunk", |
| 2645 | QCBOR_DECODE_MODE_NORMAL, |
| 2646 | {"\x5f\xc0\x00\xff", 4}, |
| 2647 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2648 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2649 | #else |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2650 | { "Indefinite length byte string with tagged integer chunk", |
| 2651 | QCBOR_DECODE_MODE_NORMAL, |
| 2652 | {"\x5f\xc0\x00\xff", 4}, |
| 2653 | QCBOR_ERR_TAGS_DISABLED |
| 2654 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2655 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2656 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2657 | { "Indefinite length byte string with an simple type chunk", |
| 2658 | QCBOR_DECODE_MODE_NORMAL, |
| 2659 | {"\x5f\xe0\xff", 3}, |
| 2660 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2661 | }, |
| 2662 | { "???", |
| 2663 | QCBOR_DECODE_MODE_NORMAL, |
| 2664 | {"\x5f\x5f\x41\x00\xff\xff", 6}, |
| 2665 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2666 | }, |
| 2667 | { "indefinite length text string with indefinite string inside", |
| 2668 | QCBOR_DECODE_MODE_NORMAL, |
| 2669 | {"\x7f\x7f\x61\x00\xff\xff", 6}, |
| 2670 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2671 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2672 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2673 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2674 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2675 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2676 | /* Definte length maps and arrays must be closed by having the right number of items */ |
| 2677 | { "A definte length array that is supposed to have 1 item, but has none", |
| 2678 | QCBOR_DECODE_MODE_NORMAL, |
| 2679 | {"\x81", 1}, |
| 2680 | QCBOR_ERR_NO_MORE_ITEMS |
| 2681 | }, |
| 2682 | { "A definte length array that is supposed to have 2 items, but has only 1", |
| 2683 | QCBOR_DECODE_MODE_NORMAL, |
| 2684 | {"\x82\x00", 2}, |
| 2685 | QCBOR_ERR_NO_MORE_ITEMS |
| 2686 | }, |
| 2687 | { "A definte length array that is supposed to have 511 items, but has only 1", |
| 2688 | QCBOR_DECODE_MODE_NORMAL, |
| 2689 | {"\x9a\x01\xff\x00", 4}, |
| 2690 | QCBOR_ERR_HIT_END |
| 2691 | }, |
| 2692 | { "A definte length map that is supposed to have 1 item, but has none", |
| 2693 | QCBOR_DECODE_MODE_NORMAL, |
| 2694 | {"\xa1", 1}, |
| 2695 | QCBOR_ERR_NO_MORE_ITEMS |
| 2696 | }, |
| 2697 | { "A definte length map that is supposed to have s item, but has only 1", |
| 2698 | QCBOR_DECODE_MODE_NORMAL, |
| 2699 | {"\xa2\x01\x02", 3}, |
| 2700 | QCBOR_ERR_NO_MORE_ITEMS |
| 2701 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2702 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2703 | /* Indefinte length maps and arrays must be ended by a break */ |
| 2704 | { "Indefinite length array with zero items and no break", |
| 2705 | QCBOR_DECODE_MODE_NORMAL, |
| 2706 | {"\x9f", 1}, |
| 2707 | QCBOR_ERR_NO_MORE_ITEMS }, |
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 array with two items and no break", |
| 2710 | QCBOR_DECODE_MODE_NORMAL, |
| 2711 | {"\x9\x01\x02", 3}, |
| 2712 | QCBOR_ERR_NO_MORE_ITEMS |
| 2713 | }, |
| 2714 | { "Indefinite length map with zero items and no break", |
| 2715 | QCBOR_DECODE_MODE_NORMAL, |
| 2716 | {"\xbf", 1}, |
| 2717 | QCBOR_ERR_NO_MORE_ITEMS |
| 2718 | }, |
| 2719 | { "Indefinite length map with two items and no break", |
| 2720 | QCBOR_DECODE_MODE_NORMAL, |
| 2721 | {"\xbf\x01\x02\x01\x02", 5}, |
| 2722 | QCBOR_ERR_NO_MORE_ITEMS |
| 2723 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2724 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2725 | /* Nested maps and arrays must be closed off (some extra nested test vectors) */ |
| 2726 | { "Unclosed indefinite array containing a closed definite length array", |
| 2727 | QCBOR_DECODE_MODE_NORMAL, |
| 2728 | {"\x9f\x80\x00", 3}, |
| 2729 | QCBOR_ERR_NO_MORE_ITEMS |
| 2730 | }, |
| 2731 | |
| 2732 | { "Definite length array containing an unclosed indefinite length array", |
| 2733 | QCBOR_DECODE_MODE_NORMAL, |
| 2734 | {"\x81\x9f", 2}, |
| 2735 | QCBOR_ERR_NO_MORE_ITEMS |
| 2736 | }, |
| 2737 | { "Unclosed indefinite map containing a closed definite length array", |
| 2738 | QCBOR_DECODE_MODE_NORMAL, |
| 2739 | {"\xbf\x01\x80\x00\xa0", 5}, |
| 2740 | QCBOR_ERR_NO_MORE_ITEMS |
| 2741 | }, |
| 2742 | { "Definite length map containing an unclosed indefinite length array", |
| 2743 | QCBOR_DECODE_MODE_NORMAL, |
| 2744 | {"\xa1\x02\x9f", 3}, |
| 2745 | QCBOR_ERR_NO_MORE_ITEMS |
| 2746 | }, |
| 2747 | { "Deeply nested definite length arrays with deepest one unclosed", |
| 2748 | QCBOR_DECODE_MODE_NORMAL, |
| 2749 | {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9}, |
| 2750 | QCBOR_ERR_NO_MORE_ITEMS |
| 2751 | }, |
| 2752 | { "Deeply nested indefinite length arrays with deepest one unclosed", |
| 2753 | QCBOR_DECODE_MODE_NORMAL, |
| 2754 | {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9}, |
| 2755 | QCBOR_ERR_NO_MORE_ITEMS |
| 2756 | }, |
| 2757 | { "Mixed nesting with indefinite unclosed", |
| 2758 | QCBOR_DECODE_MODE_NORMAL, |
| 2759 | {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9}, |
| 2760 | QCBOR_ERR_NO_MORE_ITEMS }, |
| 2761 | { "Mixed nesting with definite unclosed", |
| 2762 | QCBOR_DECODE_MODE_NORMAL, |
| 2763 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2764 | QCBOR_ERR_BAD_BREAK |
| 2765 | }, |
| 2766 | { "Unclosed indefinite length map in definite length maps", |
| 2767 | QCBOR_DECODE_MODE_NORMAL, |
| 2768 | {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8}, |
| 2769 | QCBOR_ERR_NO_MORE_ITEMS |
| 2770 | }, |
| 2771 | { "Unclosed definite length map in indefinite length maps", |
| 2772 | QCBOR_DECODE_MODE_NORMAL, |
| 2773 | {"\xbf\x01\xbf\x02\xa1", 5}, |
| 2774 | QCBOR_ERR_NO_MORE_ITEMS |
| 2775 | }, |
| 2776 | { "Unclosed indefinite length array in definite length maps", |
| 2777 | QCBOR_DECODE_MODE_NORMAL, |
| 2778 | {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8}, |
| 2779 | QCBOR_ERR_NO_MORE_ITEMS |
| 2780 | }, |
| 2781 | { "Unclosed definite length array in indefinite length maps", |
| 2782 | QCBOR_DECODE_MODE_NORMAL, |
| 2783 | {"\xbf\x01\xbf\x02\x81", 5}, |
| 2784 | QCBOR_ERR_NO_MORE_ITEMS |
| 2785 | }, |
| 2786 | { "Unclosed indefinite length map in definite length arrays", |
| 2787 | QCBOR_DECODE_MODE_NORMAL, |
| 2788 | {"\x81\x82\xbf\xff\xbf", 5}, |
| 2789 | QCBOR_ERR_NO_MORE_ITEMS |
| 2790 | }, |
| 2791 | { "Unclosed definite length map in indefinite length arrays", |
| 2792 | QCBOR_DECODE_MODE_NORMAL, |
| 2793 | {"\x9f\x9f\xa1", 3}, |
| 2794 | QCBOR_ERR_NO_MORE_ITEMS |
| 2795 | }, |
| 2796 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2797 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2798 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2799 | /* The "argument" for the data item is incomplete */ |
| 2800 | { "Positive integer missing 1 byte argument", |
| 2801 | QCBOR_DECODE_MODE_NORMAL, |
| 2802 | {"\x18", 1}, |
| 2803 | QCBOR_ERR_HIT_END |
| 2804 | }, |
| 2805 | { "Positive integer missing 2 byte argument", |
| 2806 | QCBOR_DECODE_MODE_NORMAL, |
| 2807 | {"\x19", 1}, |
| 2808 | QCBOR_ERR_HIT_END |
| 2809 | }, |
| 2810 | { "Positive integer missing 4 byte argument", |
| 2811 | QCBOR_DECODE_MODE_NORMAL, |
| 2812 | {"\x1a", 1}, |
| 2813 | QCBOR_ERR_HIT_END |
| 2814 | }, |
| 2815 | { "Positive integer missing 8 byte argument", |
| 2816 | QCBOR_DECODE_MODE_NORMAL, |
| 2817 | {"\x1b", 1}, |
| 2818 | QCBOR_ERR_HIT_END |
| 2819 | }, |
| 2820 | { "Positive integer missing 1 byte of 2 byte argument", |
| 2821 | QCBOR_DECODE_MODE_NORMAL, |
| 2822 | {"\x19\x01", 2}, |
| 2823 | QCBOR_ERR_HIT_END |
| 2824 | }, |
| 2825 | { "Positive integer missing 2 bytes of 4 byte argument", |
| 2826 | QCBOR_DECODE_MODE_NORMAL, |
| 2827 | {"\x1a\x01\x02", 3}, |
| 2828 | QCBOR_ERR_HIT_END |
| 2829 | }, |
| 2830 | { "Positive integer missing 1 bytes of 7 byte argument", |
| 2831 | QCBOR_DECODE_MODE_NORMAL, |
| 2832 | {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8}, |
| 2833 | QCBOR_ERR_HIT_END |
| 2834 | }, |
| 2835 | { "Negative integer missing 1 byte argument", |
| 2836 | QCBOR_DECODE_MODE_NORMAL, |
| 2837 | {"\x38", 1}, |
| 2838 | QCBOR_ERR_HIT_END |
| 2839 | }, |
| 2840 | { "Binary string missing 1 byte argument", |
| 2841 | QCBOR_DECODE_MODE_NORMAL, |
| 2842 | {"\x58", 1}, |
| 2843 | QCBOR_ERR_HIT_END |
| 2844 | }, |
| 2845 | { "Text string missing 1 byte argument", |
| 2846 | QCBOR_DECODE_MODE_NORMAL, |
| 2847 | {"\x78", 1}, |
| 2848 | QCBOR_ERR_HIT_END |
| 2849 | }, |
| 2850 | { "Array missing 1 byte argument", |
| 2851 | QCBOR_DECODE_MODE_NORMAL, |
| 2852 | {"\x98", 1}, |
| 2853 | QCBOR_ERR_HIT_END |
| 2854 | }, |
| 2855 | { "Map missing 1 byte argument", |
| 2856 | QCBOR_DECODE_MODE_NORMAL, |
| 2857 | {"\xb8", 1}, |
| 2858 | QCBOR_ERR_HIT_END |
| 2859 | }, |
| 2860 | { "Tag missing 1 byte argument", |
| 2861 | QCBOR_DECODE_MODE_NORMAL, |
| 2862 | {"\xd8", 1}, |
| 2863 | QCBOR_ERR_HIT_END |
| 2864 | }, |
| 2865 | { "Simple missing 1 byte argument", |
| 2866 | QCBOR_DECODE_MODE_NORMAL, |
| 2867 | {"\xf8", 1}, |
| 2868 | QCBOR_ERR_HIT_END |
| 2869 | }, |
| 2870 | { "half-precision with 1 byte argument", |
| 2871 | QCBOR_DECODE_MODE_NORMAL, |
| 2872 | {"\xf9\x00", 2}, |
| 2873 | QCBOR_ERR_HIT_END |
| 2874 | }, |
| 2875 | { "single-precision with 2 byte argument", |
| 2876 | QCBOR_DECODE_MODE_NORMAL, |
| 2877 | {"\0xfa\x00\x00", 3}, |
| 2878 | QCBOR_ERR_HIT_END |
| 2879 | }, |
| 2880 | { "double-precision with 3 byte argument", |
| 2881 | QCBOR_DECODE_MODE_NORMAL, |
| 2882 | {"\xfb\x00\x00\x00", 4}, |
| 2883 | QCBOR_ERR_HIT_END |
| 2884 | }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2885 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2886 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2887 | { "Tag with no content", |
| 2888 | QCBOR_DECODE_MODE_NORMAL, |
| 2889 | {"\xc0", 1}, |
| 2890 | QCBOR_ERR_HIT_END |
| 2891 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2892 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2893 | { "Tag with no content", |
| 2894 | QCBOR_DECODE_MODE_NORMAL, |
| 2895 | {"\xc0", 1}, |
| 2896 | QCBOR_ERR_TAGS_DISABLED |
| 2897 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2898 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2899 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2900 | /* Breaks must not occur in definite length arrays and maps */ |
| 2901 | { "Array of length 1 with sole member replaced by a break", |
| 2902 | QCBOR_DECODE_MODE_NORMAL, |
| 2903 | {"\x81\xff", 2}, |
| 2904 | QCBOR_ERR_BAD_BREAK |
| 2905 | }, |
| 2906 | { "Array of length 2 with 2nd member replaced by a break", |
| 2907 | QCBOR_DECODE_MODE_NORMAL, |
| 2908 | {"\x82\x00\xff", 3}, |
| 2909 | QCBOR_ERR_BAD_BREAK |
| 2910 | }, |
| 2911 | { "Map of length 1 with sole member label replaced by a break", |
| 2912 | QCBOR_DECODE_MODE_NORMAL, |
| 2913 | {"\xa1\xff", 2}, |
| 2914 | QCBOR_ERR_BAD_BREAK |
| 2915 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2916 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2917 | /* Map of length 1 with sole member label replaced by break */ |
| 2918 | { "Alternate representation that some decoders handle differently", |
| 2919 | QCBOR_DECODE_MODE_NORMAL, |
| 2920 | {"\xa1\xff\x00", 3}, |
| 2921 | QCBOR_ERR_BAD_BREAK |
| 2922 | }, |
| 2923 | { "Array of length 1 with 2nd member value replaced by a break", |
| 2924 | QCBOR_DECODE_MODE_NORMAL, |
| 2925 | {"\xa1\x00\xff", 3}, |
| 2926 | QCBOR_ERR_BAD_BREAK |
| 2927 | }, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2928 | { "Map of length 2 with 2nd entry label replaced by a break", |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2929 | QCBOR_DECODE_MODE_NORMAL, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2930 | {"\xa2\x00\x00\xff\x00", 5}, |
| 2931 | QCBOR_ERR_BAD_BREAK |
| 2932 | }, |
| 2933 | { "Map of length 2 with 2nd entry value replaced by a break", |
| 2934 | QCBOR_DECODE_MODE_NORMAL, |
| 2935 | {"\xa2\x00\x00\x01\xff", 5}, |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2936 | QCBOR_ERR_BAD_BREAK |
| 2937 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2938 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2939 | /* Breaks must not occur on their own out of an indefinite length data item */ |
| 2940 | { "A bare break is not well formed", |
| 2941 | QCBOR_DECODE_MODE_NORMAL, |
| 2942 | {"\xff", 1}, |
| 2943 | QCBOR_ERR_BAD_BREAK |
| 2944 | }, |
| 2945 | { "A bare break after a zero length definite length array", |
| 2946 | QCBOR_DECODE_MODE_NORMAL, |
| 2947 | {"\x80\xff", 2}, |
| 2948 | QCBOR_ERR_BAD_BREAK |
| 2949 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2950 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2951 | { "A bare break after a zero length indefinite length map", |
| 2952 | QCBOR_DECODE_MODE_NORMAL, |
| 2953 | {"\x9f\xff\xff", 3}, |
| 2954 | QCBOR_ERR_BAD_BREAK |
| 2955 | }, |
| 2956 | { "A break inside a definite length array inside an indefenite length array", |
| 2957 | QCBOR_DECODE_MODE_NORMAL, |
| 2958 | {"\x9f\x81\xff", 3}, |
| 2959 | QCBOR_ERR_BAD_BREAK |
| 2960 | }, |
| 2961 | { "Complicated mixed nesting with break outside indefinite length array", |
| 2962 | QCBOR_DECODE_MODE_NORMAL, |
| 2963 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2964 | QCBOR_ERR_BAD_BREAK }, |
| 2965 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 2966 | |
| 2967 | /* Forbidden two byte encodings of simple types */ |
| 2968 | { "Must use 0xe0 instead", |
| 2969 | QCBOR_DECODE_MODE_NORMAL, |
| 2970 | {"\xf8\x00", 2}, |
| 2971 | QCBOR_ERR_BAD_TYPE_7 |
| 2972 | }, |
| 2973 | { "Should use 0xe1 instead", |
| 2974 | QCBOR_DECODE_MODE_NORMAL, |
| 2975 | {"\xf8\x01", 2}, |
| 2976 | QCBOR_ERR_BAD_TYPE_7 |
| 2977 | }, |
| 2978 | { "Should use 0xe2 instead", |
| 2979 | QCBOR_DECODE_MODE_NORMAL, |
| 2980 | {"\xf8\x02", 2}, |
| 2981 | QCBOR_ERR_BAD_TYPE_7 |
| 2982 | }, { "Should use 0xe3 instead", |
| 2983 | QCBOR_DECODE_MODE_NORMAL, |
| 2984 | {"\xf8\x03", 2}, |
| 2985 | QCBOR_ERR_BAD_TYPE_7 |
| 2986 | }, |
| 2987 | { "Should use 0xe4 instead", |
| 2988 | QCBOR_DECODE_MODE_NORMAL, |
| 2989 | {"\xf8\x04", 2}, |
| 2990 | QCBOR_ERR_BAD_TYPE_7 |
| 2991 | }, |
| 2992 | { "Should use 0xe5 instead", |
| 2993 | QCBOR_DECODE_MODE_NORMAL, |
| 2994 | {"\xf8\x05", 2}, |
| 2995 | QCBOR_ERR_BAD_TYPE_7 |
| 2996 | }, |
| 2997 | { "Should use 0xe6 instead", |
| 2998 | QCBOR_DECODE_MODE_NORMAL, |
| 2999 | {"\xf8\x06", 2}, |
| 3000 | QCBOR_ERR_BAD_TYPE_7 |
| 3001 | }, |
| 3002 | { "Should use 0xe7 instead", |
| 3003 | QCBOR_DECODE_MODE_NORMAL, |
| 3004 | {"\xf8\x07", 2}, |
| 3005 | QCBOR_ERR_BAD_TYPE_7 |
| 3006 | }, |
| 3007 | { "Should use 0xe8 instead", |
| 3008 | QCBOR_DECODE_MODE_NORMAL, |
| 3009 | {"\xf8\x08", 2}, |
| 3010 | QCBOR_ERR_BAD_TYPE_7 |
| 3011 | }, |
| 3012 | { "Should use 0xe9 instead", |
| 3013 | QCBOR_DECODE_MODE_NORMAL, |
| 3014 | {"\xf8\x09", 2}, |
| 3015 | QCBOR_ERR_BAD_TYPE_7 |
| 3016 | }, |
| 3017 | { "Should use 0xea instead", |
| 3018 | QCBOR_DECODE_MODE_NORMAL, |
| 3019 | {"\xf8\x0a", 2}, |
| 3020 | QCBOR_ERR_BAD_TYPE_7 |
| 3021 | }, |
| 3022 | { "Should use 0xeb instead", |
| 3023 | QCBOR_DECODE_MODE_NORMAL, |
| 3024 | {"\xf8\x0b", 2}, |
| 3025 | QCBOR_ERR_BAD_TYPE_7 |
| 3026 | }, |
| 3027 | { "Should use 0xec instead", |
| 3028 | QCBOR_DECODE_MODE_NORMAL, |
| 3029 | {"\xf8\x0c", 2}, |
| 3030 | QCBOR_ERR_BAD_TYPE_7 |
| 3031 | }, |
| 3032 | { "Should use 0xed instead", |
| 3033 | QCBOR_DECODE_MODE_NORMAL, |
| 3034 | {"\xf8\x0d", 2}, |
| 3035 | QCBOR_ERR_BAD_TYPE_7 |
| 3036 | }, |
| 3037 | { "Should use 0xee instead", |
| 3038 | QCBOR_DECODE_MODE_NORMAL, |
| 3039 | {"\xf8\x0e", 2}, |
| 3040 | QCBOR_ERR_BAD_TYPE_7 |
| 3041 | }, |
| 3042 | { "Should use 0xef instead", |
| 3043 | QCBOR_DECODE_MODE_NORMAL, |
| 3044 | {"\xf8\x0f", 2}, |
| 3045 | QCBOR_ERR_BAD_TYPE_7 |
| 3046 | }, |
| 3047 | { "Should use 0xf0 instead", |
| 3048 | QCBOR_DECODE_MODE_NORMAL, |
| 3049 | {"\xf8\x10", 2}, |
| 3050 | QCBOR_ERR_BAD_TYPE_7 |
| 3051 | }, |
| 3052 | { "Should use 0xf1 instead", |
| 3053 | QCBOR_DECODE_MODE_NORMAL, |
| 3054 | {"\xf8\x11", 2}, |
| 3055 | QCBOR_ERR_BAD_TYPE_7 |
| 3056 | }, |
| 3057 | { "Should use 0xf2 instead", |
| 3058 | QCBOR_DECODE_MODE_NORMAL, |
| 3059 | {"\xf8\x12", 2}, |
| 3060 | QCBOR_ERR_BAD_TYPE_7 |
| 3061 | }, |
| 3062 | { "Should use 0xf3 instead", |
| 3063 | QCBOR_DECODE_MODE_NORMAL, |
| 3064 | {"\xf8\x13", 2}, |
| 3065 | QCBOR_ERR_BAD_TYPE_7 |
| 3066 | }, |
| 3067 | { "Should use 0xf4 instead", |
| 3068 | QCBOR_DECODE_MODE_NORMAL, |
| 3069 | {"\xf8\x14", 2}, |
| 3070 | QCBOR_ERR_BAD_TYPE_7 |
| 3071 | }, |
| 3072 | { "Should use 0xf5 instead", |
| 3073 | QCBOR_DECODE_MODE_NORMAL, |
| 3074 | {"\xf8\x15", 2}, |
| 3075 | QCBOR_ERR_BAD_TYPE_7 |
| 3076 | }, |
| 3077 | { "Should use 0xf6 instead", |
| 3078 | QCBOR_DECODE_MODE_NORMAL, |
| 3079 | {"\xf8\x16", 2}, |
| 3080 | QCBOR_ERR_BAD_TYPE_7 |
| 3081 | }, |
| 3082 | { "Should use 0xef7 instead", |
| 3083 | QCBOR_DECODE_MODE_NORMAL, |
| 3084 | {"\xf8\x17", 2}, |
| 3085 | QCBOR_ERR_BAD_TYPE_7 |
| 3086 | }, |
| 3087 | { "Should use 0xef8 instead", |
| 3088 | QCBOR_DECODE_MODE_NORMAL, |
| 3089 | {"\xf8\x18", 2}, |
| 3090 | QCBOR_ERR_BAD_TYPE_7 |
| 3091 | }, |
| 3092 | { "Reserved", |
| 3093 | QCBOR_DECODE_MODE_NORMAL, |
| 3094 | {"\xf8\x18", 2}, |
| 3095 | QCBOR_ERR_BAD_TYPE_7 |
| 3096 | }, |
| 3097 | |
| 3098 | /* Maps must have an even number of data items (key & value) */ |
| 3099 | { "Map with 1 item when it should have 2", |
| 3100 | QCBOR_DECODE_MODE_NORMAL, |
| 3101 | {"\xa1\x00", 2}, |
| 3102 | QCBOR_ERR_HIT_END |
| 3103 | }, |
| 3104 | { "Map with 3 item when it should have 4", |
| 3105 | QCBOR_DECODE_MODE_NORMAL, |
| 3106 | {"\xa2\x00\x00\x00", 2}, |
| 3107 | QCBOR_ERR_HIT_END |
| 3108 | }, |
| 3109 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 3110 | { "Map with 1 item when it should have 2", |
| 3111 | QCBOR_DECODE_MODE_NORMAL, |
| 3112 | {"\xbf\x00\xff", 3}, |
| 3113 | QCBOR_ERR_BAD_BREAK |
| 3114 | }, |
| 3115 | { "Map with 3 item when it should have 4", |
| 3116 | QCBOR_DECODE_MODE_NORMAL, |
| 3117 | {"\xbf\x00\x00\x00\xff", 5}, |
| 3118 | QCBOR_ERR_BAD_BREAK |
| 3119 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3120 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3121 | |
| 3122 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3123 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3124 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3125 | { "Text-based date, with an integer", |
| 3126 | QCBOR_DECODE_MODE_NORMAL, |
| 3127 | {"\xc0\x00", 2}, |
| 3128 | QCBOR_ERR_BAD_OPT_TAG |
| 3129 | }, |
| 3130 | { "Epoch date, with an byte string", |
| 3131 | QCBOR_DECODE_MODE_NORMAL, |
| 3132 | {"\xc1\x41\x33", 3}, |
| 3133 | QCBOR_ERR_BAD_OPT_TAG |
| 3134 | }, |
| 3135 | { "tagged as both epoch and string dates", |
| 3136 | QCBOR_DECODE_MODE_NORMAL, |
| 3137 | {"\xc1\xc0\x00", 3}, |
| 3138 | QCBOR_ERR_BAD_OPT_TAG |
| 3139 | }, |
| 3140 | { "big num tagged an int, not a byte string", |
| 3141 | QCBOR_DECODE_MODE_NORMAL, |
| 3142 | {"\xc2\x00", 2}, |
| 3143 | QCBOR_ERR_BAD_OPT_TAG |
| 3144 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3145 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3146 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3147 | { "Text-based date, with an integer", |
| 3148 | QCBOR_DECODE_MODE_NORMAL, |
| 3149 | {"\xc0\x00", 2}, |
| 3150 | QCBOR_ERR_TAGS_DISABLED |
| 3151 | }, |
| 3152 | { "Epoch date, with an byte string", |
| 3153 | QCBOR_DECODE_MODE_NORMAL, |
| 3154 | {"\xc1\x41\x33", 3}, |
| 3155 | QCBOR_ERR_TAGS_DISABLED |
| 3156 | }, |
| 3157 | { "tagged as both epoch and string dates", |
| 3158 | QCBOR_DECODE_MODE_NORMAL, |
| 3159 | {"\xc1\xc0\x00", 3}, |
| 3160 | QCBOR_ERR_TAGS_DISABLED |
| 3161 | }, |
| 3162 | { "big num tagged an int, not a byte string", |
| 3163 | QCBOR_DECODE_MODE_NORMAL, |
| 3164 | {"\xc2\x00", 2}, |
| 3165 | QCBOR_ERR_TAGS_DISABLED |
| 3166 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3167 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3168 | }; |
| 3169 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3170 | |
| 3171 | |
| 3172 | int32_t |
| 3173 | DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3174 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3175 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3176 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3177 | nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3178 | if(nResult) { |
| 3179 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3180 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3181 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3182 | // Corrupt the UsefulInputBuf and see that |
| 3183 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3184 | QCBORDecodeContext DCtx; |
| 3185 | QCBORItem Item; |
| 3186 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3187 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3188 | QCBORDecode_Init(&DCtx, |
| 3189 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 3190 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3191 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3192 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3193 | return (int32_t)uQCBORError; |
| 3194 | } |
| 3195 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 3196 | // This wasn't supposed to happen |
| 3197 | return -1; |
| 3198 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3199 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3200 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3201 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3202 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3203 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 3204 | // Did not get back the error expected |
| 3205 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3206 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3207 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3208 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3209 | /* |
| 3210 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 3211 | tests here can be performed to see that the max length |
| 3212 | error check works correctly. See DecodeBytes(). If the max |
| 3213 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3214 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3215 | This test will automatocally adapt the all CPU sizes |
| 3216 | through the use of SIZE_MAX. |
| 3217 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3218 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 3219 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3220 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3221 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3222 | // This makes a CBOR head with a text string that is very long |
| 3223 | // but doesn't fill in the bytes of the text string as that is |
| 3224 | // not needed to test this part of QCBOR. |
| 3225 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 3226 | |
| 3227 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 3228 | |
| 3229 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3230 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3231 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3232 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3233 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3237 | /* Try all 256 values of the byte at nLen including recursing for |
| 3238 | each of the values to try values at nLen+1 ... up to nLenMax |
| 3239 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 3240 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3241 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3242 | if(nLen >= nLenMax) { |
| 3243 | return; |
| 3244 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3245 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3246 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 3247 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3248 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3249 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3250 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3251 | // Get ready to parse |
| 3252 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3253 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3254 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3255 | // Parse by getting the next item until an error occurs |
| 3256 | // Just about every possible decoder error can occur here |
| 3257 | // The goal of this test is not to check for the correct |
| 3258 | // error since that is not really possible. It is to |
| 3259 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3260 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3261 | QCBORItem Item; |
| 3262 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3263 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3264 | break; |
| 3265 | } |
| 3266 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3267 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3268 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3273 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3274 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3275 | // Size 2 tests 64K inputs and runs quickly |
| 3276 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3277 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3278 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3279 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3280 | return 0; |
| 3281 | } |
| 3282 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3283 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3284 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3285 | { |
| 3286 | // size 3 tests 16 million inputs and runs OK |
| 3287 | // in seconds on fast machines. Size 4 takes |
| 3288 | // 10+ minutes and 5 half a day on fast |
| 3289 | // machines. This test is kept separate from |
| 3290 | // the others so as to no slow down the use |
| 3291 | // of them as a very frequent regression. |
| 3292 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3293 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3294 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3295 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3296 | return 0; |
| 3297 | } |
| 3298 | |
| 3299 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3300 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3301 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3302 | 0xc0, // tag for string date |
| 3303 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3304 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3305 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3306 | 0xc0, // tag for string date |
| 3307 | 0x00, // Wrong type for a string date |
| 3308 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3309 | /* 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] | 3310 | 0xc1, // tag for epoch date |
| 3311 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3312 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3313 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3314 | 0xc1, |
| 3315 | 0x62, 'h', 'i', // wrong type tagged |
| 3316 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3317 | /* 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] | 3318 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3319 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3320 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 3321 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3322 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3323 | 0xc1, // tag for epoch date |
| 3324 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3325 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3326 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3327 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 3328 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3329 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3330 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3331 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3332 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3333 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3334 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3335 | 0xc1, // tag for epoch date |
| 3336 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 3337 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 3338 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3339 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3340 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3341 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3342 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3343 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3344 | 0xc1, // tag for epoch date |
| 3345 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 3346 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3347 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3348 | 0xc1, |
| 3349 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 3350 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3351 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3352 | 0xc1, // tag for epoch date |
| 3353 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3354 | }; |
| 3355 | |
| 3356 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3357 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3358 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3359 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 3360 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3361 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3362 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3363 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3364 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3365 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3366 | return diff > 0.0000001; |
| 3367 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3368 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3369 | |
| 3370 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3371 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3372 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3373 | { |
| 3374 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3375 | QCBORItem Item; |
| 3376 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3377 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3378 | QCBORDecode_Init(&DCtx, |
| 3379 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 3380 | QCBOR_DECODE_MODE_NORMAL); |
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 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3383 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3384 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3385 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3386 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | ba58768 | 2024-02-07 16:46:43 -0800 | [diff] [blame] | 3387 | UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3388 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3389 | } |
| 3390 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3391 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3392 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3393 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3394 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3395 | } |
| 3396 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3397 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 3398 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3399 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3400 | return -4; |
| 3401 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3402 | if(uError == QCBOR_SUCCESS) { |
| 3403 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3404 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3405 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3406 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3407 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3408 | ) { |
| 3409 | return -5; |
| 3410 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3413 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3414 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3415 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3416 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3417 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3418 | /* 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] | 3419 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 3420 | // The date is decoded and the two tags are returned. This is to |
| 3421 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3422 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3423 | return -7; |
| 3424 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3425 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3426 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3427 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3428 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3429 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 3430 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3431 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3432 | } |
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 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3435 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3436 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3437 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3438 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3439 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 3440 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3441 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3442 | return -10; |
| 3443 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3444 | if(uError == QCBOR_SUCCESS) { |
| 3445 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3446 | Item.val.epochDate.nSeconds != 1 |
| 3447 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3448 | || 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] | 3449 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3450 | ) { |
| 3451 | return -11; |
| 3452 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3453 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3454 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3455 | /* 8. Epoch date with too-large single-precision float */ |
| 3456 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3457 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3458 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3459 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3460 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3461 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 3462 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3463 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3464 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3465 | } |
| 3466 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3467 | /* 10. Epoch date with largest supported double-precision value */ |
| 3468 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3469 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3470 | return -14; |
| 3471 | } |
| 3472 | if(uError == QCBOR_SUCCESS) { |
| 3473 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3474 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 3475 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3476 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 3477 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 3478 | ) { |
| 3479 | return -14; |
| 3480 | } |
| 3481 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3482 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3483 | /* 11. Epoch date with single-precision NaN */ |
| 3484 | 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] | 3485 | return -15; |
| 3486 | } |
| 3487 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3488 | /* 12. Epoch date with double-precision plus infinity */ |
| 3489 | 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] | 3490 | return -16; |
| 3491 | } |
| 3492 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3493 | /* 13. Epoch date with half-precision negative infinity */ |
| 3494 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3495 | 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] | 3496 | return -17; |
| 3497 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3498 | |
| 3499 | return 0; |
| 3500 | } |
| 3501 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3502 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3503 | /* |
| 3504 | Test cases covered here. Some items cover more than one of these. |
| 3505 | positive integer (zero counts as a positive integer) |
| 3506 | negative integer |
| 3507 | half-precision float |
| 3508 | single-precision float |
| 3509 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3510 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3511 | float Overflow error |
| 3512 | Wrong type error for epoch |
| 3513 | Wrong type error for date string |
| 3514 | float disabled error |
| 3515 | half-precision disabled error |
| 3516 | -Infinity |
| 3517 | Slightly too large integer |
| 3518 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3519 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3520 | Get epoch by int |
| 3521 | Get string by int |
| 3522 | Get epoch by string |
| 3523 | Get string by string |
| 3524 | Fail to get epoch by wrong int label |
| 3525 | Fail to get string by wrong string label |
| 3526 | Fail to get epoch by string because it is invalid |
| 3527 | Fail to get epoch by int because it is invalid |
| 3528 | |
| 3529 | Untagged values |
| 3530 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3531 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3532 | 0x87, // array of 7 items |
| 3533 | |
| 3534 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 3535 | |
| 3536 | // Untagged integer 0 |
| 3537 | 0x08, |
| 3538 | 0x00, |
| 3539 | |
| 3540 | // Utagged date string with string label y |
| 3541 | 0x61, 0x79, |
| 3542 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 3543 | |
| 3544 | // Untagged single-precision float with value 3.14 with string label x |
| 3545 | 0x61, 0x78, |
| 3546 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 3547 | |
| 3548 | // Untagged half-precision float with value -2 |
| 3549 | 0x09, |
| 3550 | 0xF9, 0xC0, 0x00, |
| 3551 | |
| 3552 | /* Untagged date-only date string */ |
| 3553 | 0x18, 0x63, |
| 3554 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3555 | |
| 3556 | /* Untagged days-count epoch date */ |
| 3557 | 0x11, |
| 3558 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 3559 | |
| 3560 | // End of map, back to array |
| 3561 | |
| 3562 | 0xa7, // Open map of tagged items with labels |
| 3563 | |
| 3564 | 0x00, |
| 3565 | 0xc0, // tag for string date |
| 3566 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 3567 | |
| 3568 | |
| 3569 | 0x01, |
| 3570 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 3571 | 0xc1, // tag for epoch date |
| 3572 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3573 | |
| 3574 | 0x05, |
| 3575 | 0xc1, |
| 3576 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 3577 | |
| 3578 | |
| 3579 | 0x07, |
| 3580 | 0xc1, // tag for epoch date |
| 3581 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3582 | |
| 3583 | /* Tagged days-count epoch date */ |
| 3584 | 0x63, 0x53, 0x44, 0x45, |
| 3585 | 0xD8, 0x64, /* tag(100) */ |
| 3586 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 3587 | |
| 3588 | // Untagged -1000 with label z |
| 3589 | 0x61, 0x7a, |
| 3590 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 3591 | 0x39, 0x03, 0xe7, |
| 3592 | |
| 3593 | /* Tagged date-only date string */ |
| 3594 | 0x63, 0x53, 0x44, 0x53, |
| 3595 | 0xD9, 0x03, 0xEC, |
| 3596 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3597 | |
| 3598 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3599 | |
| 3600 | 0xc1, |
| 3601 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 3602 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3603 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3604 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 3605 | |
| 3606 | 0xc1, // tag for epoch date |
| 3607 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 3608 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3609 | // These two at the end because they are unrecoverable errors |
| 3610 | 0xc1, // tag for epoch date |
| 3611 | 0x80, // Erroneous empty array as content for date |
| 3612 | |
| 3613 | 0xc0, // tag for string date |
| 3614 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3615 | }; |
| 3616 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3617 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3618 | { |
| 3619 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3620 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3621 | int64_t nEpochDate3, nEpochDate5, |
| 3622 | nEpochDate4, nEpochDate6, |
| 3623 | nEpochDays2; |
| 3624 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3625 | |
| 3626 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3627 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3628 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3629 | |
| 3630 | /* Items are in an array or map to test look up by label and other |
| 3631 | * that might not occur in isolated items. But it does make the |
| 3632 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3633 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3634 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3635 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3636 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3637 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3638 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3639 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3640 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3641 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3642 | return 104; |
| 3643 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3644 | if(uError == QCBOR_SUCCESS) { |
| 3645 | if(nEpochDate5 != 3) { |
| 3646 | return 103; |
| 3647 | } |
| 3648 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3649 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3650 | // A half-precision date with value -2 FFF |
| 3651 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3652 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3653 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3654 | 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] | 3655 | return 106; |
| 3656 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3657 | if(uError == QCBOR_SUCCESS) { |
| 3658 | if(nEpochDate4 != -2) { |
| 3659 | return 105; |
| 3660 | } |
| 3661 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3662 | |
| 3663 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3664 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 3665 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3666 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3667 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3668 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3669 | return 107; |
| 3670 | } |
| 3671 | |
| 3672 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3673 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3674 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3675 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3676 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3677 | return 108; |
| 3678 | } |
| 3679 | |
| 3680 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3681 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 3682 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3683 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3684 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3685 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3686 | return 109; |
| 3687 | } |
| 3688 | |
| 3689 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3690 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3691 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3692 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3693 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3694 | return 110; |
| 3695 | } |
| 3696 | |
| 3697 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3698 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3699 | |
| 3700 | // Untagged integer 0 |
| 3701 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3702 | &nEpochDate3); |
| 3703 | // Untagged date string |
| 3704 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3705 | &StringDate2); |
| 3706 | |
| 3707 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3708 | &StringDays2); |
| 3709 | |
| 3710 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3711 | &nEpochDays2); |
| 3712 | |
| 3713 | QCBORDecode_ExitMap(&DC); |
| 3714 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3715 | return 3001; |
| 3716 | } |
| 3717 | |
| 3718 | // The map of tagged items |
| 3719 | QCBORDecode_EnterMap(&DC, NULL); |
| 3720 | |
| 3721 | #ifndef QCBOR_DISABLE_TAGS |
| 3722 | int64_t nEpochDate2, |
| 3723 | nEpochDateFail, |
| 3724 | nEpochDate1400000000, nEpochDays1; |
| 3725 | UsefulBufC StringDays1; |
| 3726 | uint64_t uTag1, uTag2; |
| 3727 | |
| 3728 | // Tagged date string |
| 3729 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3730 | &StringDate1); |
| 3731 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3732 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3733 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3734 | 1, |
| 3735 | QCBOR_TAG_REQUIREMENT_TAG | |
| 3736 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3737 | &nEpochDate1400000000); |
| 3738 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3739 | |
| 3740 | // Get largest negative double precision epoch date allowed |
| 3741 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3742 | 5, |
| 3743 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 3744 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3745 | &nEpochDate2); |
| 3746 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3747 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3748 | return 102; |
| 3749 | } |
| 3750 | if(uError == QCBOR_SUCCESS) { |
| 3751 | if(nEpochDate2 != -9223372036854773760LL) { |
| 3752 | return 101; |
| 3753 | } |
| 3754 | } |
| 3755 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3756 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3757 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 3758 | "z", |
| 3759 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 3760 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3761 | &nEpochDate6); |
| 3762 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3763 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3764 | |
| 3765 | // Get largest double precision epoch date allowed |
| 3766 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3767 | &nEpochDate2); |
| 3768 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3769 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3770 | return 112; |
| 3771 | } |
| 3772 | if(uError == QCBOR_SUCCESS) { |
| 3773 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 3774 | return 111; |
| 3775 | } |
| 3776 | } |
| 3777 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3778 | /* The days format is much simpler than the date format |
| 3779 | * because it can't be a floating point value. The test |
| 3780 | * of the spiffy decode functions sufficiently covers |
| 3781 | * the test of the non-spiffy decode days date decoding. |
| 3782 | * There is no full fan out of the error conditions |
| 3783 | * and decode options as that is implemented by code |
| 3784 | * that is tested well by the date testing above. |
| 3785 | */ |
| 3786 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 3787 | &StringDays1); |
| 3788 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3789 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 3790 | &nEpochDays1); |
| 3791 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3792 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3793 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3794 | return 3001; |
| 3795 | } |
| 3796 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3797 | // Too-negative float, -9.2233720368547748E+18 |
| 3798 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3799 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3800 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3801 | return 1111; |
| 3802 | } |
| 3803 | |
| 3804 | // Too-large integer |
| 3805 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3806 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3807 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 3808 | return 1; |
| 3809 | } |
| 3810 | |
| 3811 | // Half-precision minus infinity |
| 3812 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3813 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3814 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3815 | return 2; |
| 3816 | } |
| 3817 | |
| 3818 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3819 | // Bad content for epoch date |
| 3820 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3821 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3822 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3823 | return 3; |
| 3824 | } |
| 3825 | |
| 3826 | // Bad content for string date |
| 3827 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 3828 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3829 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3830 | return 4; |
| 3831 | } |
| 3832 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3833 | QCBORDecode_ExitArray(&DC); |
| 3834 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3835 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3836 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3837 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3838 | #else /* QCBOR_DISABLE_TAGS */ |
| 3839 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3840 | &StringDate1); |
| 3841 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3842 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 3843 | return 4; |
| 3844 | } |
| 3845 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3846 | |
| 3847 | |
| 3848 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3849 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3850 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3851 | return 200; |
| 3852 | } |
| 3853 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3854 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3855 | return 201; |
| 3856 | } |
| 3857 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3858 | if(nEpochDays1 != -10676) { |
| 3859 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3860 | } |
| 3861 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3862 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3863 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3864 | } |
| 3865 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3866 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3867 | return 204; |
| 3868 | } |
| 3869 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3870 | if(nEpochDate6 != -1000) { |
| 3871 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3872 | } |
| 3873 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3874 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3875 | return 205; |
| 3876 | } |
| 3877 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3878 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3879 | |
| 3880 | if(nEpochDate3 != 0) { |
| 3881 | return 202; |
| 3882 | } |
| 3883 | |
| 3884 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3885 | return 206; |
| 3886 | } |
| 3887 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3888 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 3889 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3893 | return 208; |
| 3894 | } |
| 3895 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3896 | return 0; |
| 3897 | } |
| 3898 | |
| 3899 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3900 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3901 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3902 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3903 | 0x81, // Array of one |
| 3904 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 3905 | 0x82, // Array of two that is the faction 1/3 |
| 3906 | 0x01, |
| 3907 | 0x03, |
| 3908 | |
| 3909 | /* |
| 3910 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3911 | */ |
| 3912 | 0xd8, 0xe1, |
| 3913 | 0xd8, 0xe2, |
| 3914 | 0xd8, 0xe3, |
| 3915 | 0xd8, 0xe4, |
| 3916 | 0xd8, 0xe5, |
| 3917 | 0x80, |
| 3918 | |
| 3919 | /* tag 10489608748473423768( |
| 3920 | 2442302356( |
| 3921 | 21590( |
| 3922 | 240( |
| 3923 | [])))) |
| 3924 | */ |
| 3925 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3926 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 3927 | 0xd9, 0x54, 0x56, |
| 3928 | 0xd8, 0xf0, |
| 3929 | 0x80, |
| 3930 | |
| 3931 | /* tag 21590( |
| 3932 | 10489608748473423768( |
| 3933 | 2442302357( |
| 3934 | 65534( |
| 3935 | [])))) |
| 3936 | */ |
| 3937 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 3938 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3939 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3940 | 0xd9, 0xff, 0xfe, |
| 3941 | 0x80, |
| 3942 | |
| 3943 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3944 | works in conjuntion with entries above. |
| 3945 | 269488144(269488145(269488146(269488147([])))) |
| 3946 | */ |
| 3947 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 3948 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 3949 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 3950 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 3951 | 0x80, |
| 3952 | |
| 3953 | /* An invalid decimal fraction with an additional tag */ |
| 3954 | 0xd9, 0xff, 0xfa, |
| 3955 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 3956 | 0x00, // the integer 0; should be a byte string |
| 3957 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3958 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3959 | /* |
| 3960 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3961 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3962 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3963 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3964 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3965 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3966 | /* |
| 3967 | DB 9192939495969798 # tag(10489608748473423768) |
| 3968 | D8 88 # tag(136) |
| 3969 | C6 # tag(6) |
| 3970 | C7 # tag(7) |
| 3971 | 80 # array(0) |
| 3972 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3973 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3974 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3975 | |
| 3976 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3977 | 55799(55799(55799({ |
| 3978 | 6(7(-23)): 5859837686836516696(7({ |
| 3979 | 7(-20): 11({ |
| 3980 | 17(-18): 17(17(17("Organization"))), |
| 3981 | 9(-17): 773("SSG"), |
| 3982 | -15: 16(17(6(7("Confusion")))), |
| 3983 | 17(-16): 17("San Diego"), |
| 3984 | 17(-14): 17("US") |
| 3985 | }), |
| 3986 | 23(-19): 19({ |
| 3987 | -11: 9({ |
| 3988 | -9: -7 |
| 3989 | }), |
| 3990 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 3991 | }) |
| 3992 | })), |
| 3993 | 16(-22): 23({ |
| 3994 | 11(8(7(-5))): 8(-3) |
| 3995 | }) |
| 3996 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3997 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3998 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3999 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 4000 | 0xc6, 0xc7, 0x36, |
| 4001 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 4002 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 4003 | 0xcb, 0xa5, |
| 4004 | 0xd1, 0x31, |
| 4005 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 4006 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 4007 | 0xc9, 0x30, |
| 4008 | 0xd9, 0x03, 0x05, 0x63, |
| 4009 | 0x53, 0x53, 0x47, |
| 4010 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4011 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 4012 | 0x69, |
| 4013 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4014 | 0xd1, 0x2f, |
| 4015 | 0xd1, 0x69, |
| 4016 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 4017 | 0xd1, 0x2d, |
| 4018 | 0xd1, 0x62, |
| 4019 | 0x55, 0x53, |
| 4020 | 0xd7, 0x32, |
| 4021 | 0xd3, 0xa2, |
| 4022 | 0x2a, |
| 4023 | 0xc9, 0xa1, |
| 4024 | 0x28, |
| 4025 | 0x26, |
| 4026 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 4027 | 0xcc, 0x4a, |
| 4028 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 4029 | 0xd0, 0x35, |
| 4030 | 0xd7, 0xa1, |
| 4031 | 0xcb, 0xc8, 0xc7, 0x24, |
| 4032 | 0xc8, 0x22}; |
| 4033 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4034 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4035 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4036 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4037 | |
| 4038 | 0xc0, // tag for string date |
| 4039 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4040 | |
| 4041 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4042 | |
| 4043 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 4044 | |
| 4045 | 0xd8, 0x23, // tag for regex |
| 4046 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 4047 | |
| 4048 | 0xc0, // tag for string date |
| 4049 | 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] | 4050 | |
| 4051 | // This last case makes the array untraversable because it is |
| 4052 | // an uncrecoverable error. Make sure it stays last and is the only |
| 4053 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4054 | }; |
| 4055 | |
| 4056 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4057 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4058 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4059 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4060 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4061 | { |
| 4062 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4063 | QCBORItem Item; |
| 4064 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4065 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4066 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4067 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4068 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4069 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4070 | /* |
| 4071 | This test matches the magic number tag and the fraction tag |
| 4072 | 55799([...]) |
| 4073 | */ |
| 4074 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4075 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4076 | return -2; |
| 4077 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4078 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4079 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 4080 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4081 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4082 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4083 | /* |
| 4084 | 4([1,3]) |
| 4085 | */ |
| 4086 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4087 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4088 | if(uError != QCBOR_SUCCESS || |
| 4089 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4090 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 4091 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 4092 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4093 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4094 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4095 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 4096 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4097 | return -4; |
| 4098 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4099 | // consume the items in the array |
| 4100 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4101 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4102 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4103 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4104 | if(uError != QCBOR_SUCCESS || |
| 4105 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4106 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 4107 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4108 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4109 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4110 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 4111 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4112 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4113 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4114 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4115 | /* |
| 4116 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 4117 | */ |
| 4118 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4119 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4120 | return -6; |
| 4121 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4122 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 4123 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 4124 | return -106; |
| 4125 | } |
| 4126 | |
| 4127 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4128 | /* tag 10489608748473423768( |
| 4129 | 2442302356( |
| 4130 | 21590( |
| 4131 | 240( |
| 4132 | [])))) |
| 4133 | */ |
| 4134 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4135 | if(uError != QCBOR_SUCCESS || |
| 4136 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4137 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 4138 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 4139 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 4140 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4141 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /* tag 21590( |
| 4145 | 10489608748473423768( |
| 4146 | 2442302357( |
| 4147 | 21591( |
| 4148 | [])))) |
| 4149 | */ |
| 4150 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4151 | if(uError != QCBOR_SUCCESS || |
| 4152 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4153 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 4154 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 4155 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 4156 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 4157 | return -8; |
| 4158 | } |
| 4159 | |
| 4160 | /* Make sure to blow past the limit of tags that must be mapped. |
| 4161 | works in conjuntion with entries above. |
| 4162 | 269488144(269488145(269488146(269488147([])))) |
| 4163 | */ |
| 4164 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4165 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4166 | return -9; |
| 4167 | } |
| 4168 | |
| 4169 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4170 | if(uError == QCBOR_SUCCESS) { |
| 4171 | return -10; |
| 4172 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4173 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4174 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4175 | // 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] | 4176 | // tage and then matches it. Caller-config lists are no longer |
| 4177 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4178 | QCBORDecode_Init(&DCtx, |
| 4179 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4180 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4181 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 4182 | const QCBORTagListIn TL = {2, puList}; |
| 4183 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4184 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4185 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4186 | return -8; |
| 4187 | } |
| 4188 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4189 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 4190 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 4191 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 4192 | Item.val.uCount != 0) { |
| 4193 | return -9; |
| 4194 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4195 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4196 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4197 | // 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] | 4198 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4199 | const uint64_t puLongList[17] = {1,2,1}; |
| 4200 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4201 | QCBORDecode_Init(&DCtx, |
| 4202 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4203 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4204 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 4205 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4206 | return -11; |
| 4207 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4208 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4209 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4210 | QCBORTagListOut Out = {0, 4, puTags}; |
| 4211 | |
| 4212 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4213 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4214 | QCBORDecode_Init(&DCtx, |
| 4215 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4216 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4217 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4218 | return -12; |
| 4219 | } |
| 4220 | if(puTags[0] != 0x9192939495969798 || |
| 4221 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4222 | puTags[2] != 0x06 || |
| 4223 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4224 | return -13; |
| 4225 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4226 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4227 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4228 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4229 | QCBORDecode_Init(&DCtx, |
| 4230 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4231 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4232 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 4233 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4234 | return -14; |
| 4235 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4236 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4237 | |
| 4238 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4239 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4240 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 4241 | // It is a bit of a messy test and maybe could be improved, but |
| 4242 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4243 | QCBORDecode_Init(&DCtx, |
| 4244 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4245 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4246 | int n = CheckCSRMaps(&DCtx); |
| 4247 | if(n) { |
| 4248 | return n-2000; |
| 4249 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4250 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4251 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4252 | QCBORDecode_Init(&DCtx, |
| 4253 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4254 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4255 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4256 | /* With the spiffy decode revision, this tag list is not used. |
| 4257 | It doesn't matter if a tag is in this list or not so some |
| 4258 | tests that couldn't process a tag because it isn't in this list |
| 4259 | now can process these unlisted tags. The tests have been |
| 4260 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4261 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 4262 | const QCBORTagListIn TagList = {3, puTagList}; |
| 4263 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4264 | |
| 4265 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4266 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4267 | return -100; |
| 4268 | } |
| 4269 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4270 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4271 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 4272 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 4273 | Item.val.uCount != 2 || |
| 4274 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 4275 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 4276 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 4277 | Out.uNumUsed != 3) { |
| 4278 | return -101; |
| 4279 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4280 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4281 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4282 | return -102; |
| 4283 | } |
| 4284 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4285 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4286 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4287 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4288 | Item.val.uCount != 2 || |
| 4289 | puTags[0] != 5859837686836516696 || |
| 4290 | puTags[1] != 7 || |
| 4291 | Out.uNumUsed != 2) { |
| 4292 | return -103; |
| 4293 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4294 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4295 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4296 | return -104; |
| 4297 | } |
| 4298 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4299 | Item.val.uCount != 5 || |
| 4300 | puTags[0] != 0x0b || |
| 4301 | Out.uNumUsed != 1) { |
| 4302 | return -105; |
| 4303 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4304 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4305 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4306 | return -106; |
| 4307 | } |
| 4308 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4309 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 4310 | Item.val.string.len != 12 || |
| 4311 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 4312 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 4313 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 4314 | Out.uNumUsed != 3) { |
| 4315 | return -105; |
| 4316 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4317 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4318 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4319 | return -107; |
| 4320 | } |
| 4321 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4322 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 4323 | Item.val.string.len != 3 || |
| 4324 | puTags[0] != 773 || |
| 4325 | Out.uNumUsed != 1) { |
| 4326 | return -108; |
| 4327 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4328 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4329 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4330 | return -109; |
| 4331 | } |
| 4332 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4333 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4334 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4335 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4336 | puTags[3] != 7 || |
| 4337 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4338 | return -110; |
| 4339 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4340 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4341 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4342 | return -111; |
| 4343 | } |
| 4344 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4345 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4346 | Item.val.string.len != 9 || |
| 4347 | puTags[0] != 17 || |
| 4348 | Out.uNumUsed != 1) { |
| 4349 | return -112; |
| 4350 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4351 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4352 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4353 | return -111; |
| 4354 | } |
| 4355 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4356 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4357 | Item.val.string.len != 2 || |
| 4358 | puTags[0] != 17 || |
| 4359 | Out.uNumUsed != 1) { |
| 4360 | return -112; |
| 4361 | } |
| 4362 | |
| 4363 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4364 | return -113; |
| 4365 | } |
| 4366 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4367 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4368 | Item.val.uCount != 2 || |
| 4369 | puTags[0] != 19 || |
| 4370 | Out.uNumUsed != 1) { |
| 4371 | return -114; |
| 4372 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4373 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4374 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4375 | return -115; |
| 4376 | } |
| 4377 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4378 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4379 | Item.val.uCount != 1 || |
| 4380 | puTags[0] != 9 || |
| 4381 | Out.uNumUsed != 1) { |
| 4382 | return -116; |
| 4383 | } |
| 4384 | |
| 4385 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4386 | return -116; |
| 4387 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4388 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4389 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4390 | Out.uNumUsed != 0) { |
| 4391 | return -117; |
| 4392 | } |
| 4393 | |
| 4394 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4395 | return -118; |
| 4396 | } |
| 4397 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 4398 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4399 | puTags[0] != 12 || |
| 4400 | Out.uNumUsed != 1) { |
| 4401 | return -119; |
| 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 -120; |
| 4406 | } |
| 4407 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4408 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 4409 | Item.val.uCount != 1 || |
| 4410 | puTags[0] != 0x17 || |
| 4411 | Out.uNumUsed != 1) { |
| 4412 | return -121; |
| 4413 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4414 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4415 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4416 | return -122; |
| 4417 | } |
| 4418 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4419 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4420 | Item.val.int64 != -3 || |
| 4421 | puTags[0] != 8 || |
| 4422 | Out.uNumUsed != 1) { |
| 4423 | return -123; |
| 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_Finish(&DCtx)) { |
| 4427 | return -124; |
| 4428 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4429 | |
| 4430 | UsefulBufC DateString; |
| 4431 | QCBORDecode_Init(&DCtx, |
| 4432 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4433 | QCBOR_DECODE_MODE_NORMAL); |
| 4434 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4435 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4436 | // tagged date string |
| 4437 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4438 | // untagged date string |
| 4439 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4440 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4441 | return 100; |
| 4442 | } |
| 4443 | // untagged byte string |
| 4444 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4445 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4446 | return 101; |
| 4447 | } |
| 4448 | // tagged regex |
| 4449 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4450 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4451 | return 102; |
| 4452 | } |
| 4453 | // tagged date string with a byte string |
| 4454 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4455 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4456 | return 103; |
| 4457 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4458 | // The exit errors out because the last item, the date string with |
| 4459 | // bad content makes the array untraversable (the bad date string |
| 4460 | // could have tag content of an array or such that is not consumed |
| 4461 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4462 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4463 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4464 | return 104; |
| 4465 | } |
| 4466 | |
| 4467 | |
| 4468 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4469 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4470 | QCBOR_DECODE_MODE_NORMAL); |
| 4471 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 4472 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) { |
| 4473 | return 200; |
| 4474 | } |
| 4475 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) { |
| 4476 | return 202; |
| 4477 | } |
| 4478 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) { |
| 4479 | return 203; |
| 4480 | } |
| 4481 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4482 | return 204; |
| 4483 | } |
| 4484 | |
| 4485 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 4486 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) { |
| 4487 | return 210; |
| 4488 | } |
| 4489 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) { |
| 4490 | return 212; |
| 4491 | } |
| 4492 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) { |
| 4493 | return 213; |
| 4494 | } |
| 4495 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4496 | return 214; |
| 4497 | } |
| 4498 | |
| 4499 | |
| 4500 | QCBORDecode_Init(&DCtx, |
| 4501 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4502 | QCBOR_DECODE_MODE_NORMAL); |
| 4503 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 4504 | QCBORDecode_EnterMapFromMapN(&DCtx, -23); |
| 4505 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) { |
| 4506 | return 220; |
| 4507 | } |
| 4508 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) { |
| 4509 | return 221; |
| 4510 | } |
| 4511 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) { |
| 4512 | return 222; |
| 4513 | } |
| 4514 | |
| 4515 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 4516 | QCBORDecode_Init(&DCtx, |
| 4517 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4518 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 4519 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 4520 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) { |
| 4521 | return 230; |
| 4522 | } |
| 4523 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) { |
| 4524 | return 231; |
| 4525 | } |
| 4526 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) { |
| 4527 | return 232; |
| 4528 | } |
| 4529 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4530 | return 234; |
| 4531 | } |
| 4532 | int64_t nInt; |
| 4533 | QCBORDecode_GetInt64(&DCtx, &nInt); |
| 4534 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) { |
| 4535 | return 240; |
| 4536 | } |
| 4537 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 6) { |
| 4538 | return 241; |
| 4539 | } |
| 4540 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) { |
| 4541 | return 242; |
| 4542 | } |
| 4543 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 4544 | return 243; |
| 4545 | } |
| 4546 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4547 | |
| 4548 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4549 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4550 | QCBOR_DECODE_MODE_NORMAL); |
| 4551 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4552 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4553 | // tagged date string |
| 4554 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4555 | // untagged date string |
| 4556 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4557 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4558 | return 250; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4559 | } |
| 4560 | // untagged byte string |
| 4561 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4562 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4563 | return 251; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4564 | } |
| 4565 | // tagged regex |
| 4566 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4567 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4568 | return 252; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4569 | } |
| 4570 | // tagged date string with a byte string |
| 4571 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4572 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4573 | return 253; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4574 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4575 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4576 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4577 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4578 | return 254; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | QCBORDecode_Init(&DCtx, |
| 4582 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4583 | QCBOR_DECODE_MODE_NORMAL); |
| 4584 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4585 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4586 | // tagged date string |
| 4587 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4588 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4589 | return 300; |
| 4590 | } |
| 4591 | // untagged date string |
| 4592 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4593 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4594 | return 301; |
| 4595 | } |
| 4596 | // untagged byte string |
| 4597 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4598 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4599 | return 302; |
| 4600 | } |
| 4601 | // tagged regex |
| 4602 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4603 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4604 | return 303; |
| 4605 | } |
| 4606 | // tagged date string with a byte string |
| 4607 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4608 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4609 | return 304; |
| 4610 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4611 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4612 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4613 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4614 | return 305; |
| 4615 | } |
| 4616 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4617 | return 0; |
| 4618 | } |
| 4619 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4620 | /* |
| 4621 | * These are showing the big numbers converted to integers. |
| 4622 | * The tag numbers are not shown. |
| 4623 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4624 | * [ |
| 4625 | * 18446744073709551616, |
| 4626 | * -18446744073709551617, |
| 4627 | * { |
| 4628 | * -64: -18446744073709551617, |
| 4629 | * 64: 18446744073709551616, |
| 4630 | * "BN+": 18446744073709551616, |
| 4631 | * "BN-": -18446744073709551617 |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4632 | * } |
| 4633 | * ] |
| 4634 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4635 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4636 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4637 | 0x83, |
| 4638 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4639 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4640 | 0xA4, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4641 | 0x38, 0x3F, |
| 4642 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4643 | 0x18, 0x40, |
| 4644 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4645 | 0x63, 0x42, 0x4E, 0x2B, |
| 4646 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4647 | 0x63, 0x42, 0x4E, 0x2D, |
| 4648 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4649 | }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4650 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4651 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4652 | /* The expected big num */ |
| 4653 | static const uint8_t spBigNum[] = { |
| 4654 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4655 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4656 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4657 | |
| 4658 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4659 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4660 | { |
| 4661 | QCBORDecodeContext DCtx; |
| 4662 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4663 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4664 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4665 | QCBORDecode_Init(&DCtx, |
| 4666 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 4667 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4668 | |
| 4669 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4670 | // |
| 4671 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4672 | return -1; |
| 4673 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4674 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4675 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4676 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4677 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4678 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4679 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4680 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4681 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4682 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4683 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | // |
| 4687 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4688 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4689 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4690 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4691 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4692 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4693 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4694 | // |
| 4695 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4696 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4697 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4698 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4699 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4700 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4701 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4702 | return -15; |
| 4703 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4704 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4705 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4706 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4707 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4711 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4712 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4713 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4714 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4715 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4716 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4717 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4718 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4719 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 4720 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4721 | return -9; |
| 4722 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4723 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4724 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
| 4725 | return -10; |
| 4726 | } |
| 4727 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4728 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4729 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4730 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4731 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4732 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4733 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4734 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4735 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4736 | |
| 4737 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4738 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4739 | #else |
| 4740 | |
| 4741 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 4742 | return -100; |
| 4743 | } |
| 4744 | #endif /* QCBOR_DISABLE_TAGS */ |
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 | return 0; |
| 4747 | } |
| 4748 | |
| 4749 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4750 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4751 | uint8_t uDataType, |
| 4752 | uint8_t uNestingLevel, |
| 4753 | uint8_t uNextNest, |
| 4754 | int64_t nLabel, |
| 4755 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4756 | { |
| 4757 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4758 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4759 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4760 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 4761 | if(Item.uDataType != uDataType) return -1; |
| 4762 | if(uNestingLevel > 0) { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4763 | if(Item.uLabelType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4764 | return -1; |
| 4765 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4766 | if(Item.label.int64 != nLabel) { |
| 4767 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4768 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4769 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4770 | } |
| 4771 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4772 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4773 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4774 | if(pItem) { |
| 4775 | *pItem = Item; |
| 4776 | } |
| 4777 | return 0; |
| 4778 | } |
| 4779 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4780 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4781 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4782 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4783 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4784 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4785 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4786 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4787 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4788 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4789 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 4790 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 4791 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 4792 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 4793 | 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] | 4794 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4795 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 4796 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4797 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4798 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 4799 | 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] | 4800 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4801 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 4802 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4803 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4804 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4805 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4806 | return 0; |
| 4807 | } |
| 4808 | |
| 4809 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4810 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4811 | { |
| 4812 | -23: { |
| 4813 | -20: { |
| 4814 | -18: "Organization", |
| 4815 | -17: "SSG", |
| 4816 | -15: "Confusion", |
| 4817 | -16: "San Diego", |
| 4818 | -14: "US" |
| 4819 | }, |
| 4820 | -19: { |
| 4821 | -11: { |
| 4822 | -9: -7 |
| 4823 | }, |
| 4824 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 4825 | } |
| 4826 | }, |
| 4827 | -22: { |
| 4828 | -5: -3 |
| 4829 | } |
| 4830 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4831 | */ |
| 4832 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4833 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 4834 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4835 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4836 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4837 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4838 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4839 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 4840 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 4841 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 4842 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4843 | // Same map as above, but using indefinite lengths |
| 4844 | static const uint8_t spCSRInputIndefLen[] = { |
| 4845 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 4846 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4847 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4848 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4849 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4850 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4851 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 4852 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 4853 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 4854 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 4855 | |
| 4856 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4857 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4858 | { |
| 4859 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4860 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4861 | QCBORDecode_Init(&DCtx, |
| 4862 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4863 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4864 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4865 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4866 | } |
| 4867 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4868 | |
| 4869 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4870 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4871 | { |
| 4872 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4873 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4874 | QCBORDecode_Init(&DCtx, |
| 4875 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4876 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4877 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4878 | QCBORItem Item; |
| 4879 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4880 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4881 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4882 | return -1; |
| 4883 | } |
| 4884 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4885 | return -2; |
| 4886 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4887 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4888 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4889 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 4890 | return -3; |
| 4891 | } |
| 4892 | |
| 4893 | return 0; |
| 4894 | } |
| 4895 | |
| 4896 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4897 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4898 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4899 | { |
| 4900 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4901 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4902 | QCBORDecode_Init(&DCtx, |
| 4903 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 4904 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4905 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4906 | return CheckCSRMaps(&DCtx); |
| 4907 | } |
| 4908 | |
| 4909 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4910 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4911 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 4912 | { |
| 4913 | UsefulOutBuf UOB; |
| 4914 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4915 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4916 | int i; |
| 4917 | for(i = 0; i < n; i++) { |
| 4918 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 4919 | } |
| 4920 | |
| 4921 | for(i = 0; i < n; i++) { |
| 4922 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4923 | } |
| 4924 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4925 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4926 | |
| 4927 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4928 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4929 | { |
| 4930 | QCBORDecodeContext DC; |
| 4931 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4932 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4933 | int j; |
| 4934 | for(j = 0; j < nNestLevel; j++) { |
| 4935 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4936 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4937 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 4938 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4939 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4940 | return -4; |
| 4941 | } else { |
| 4942 | return 0; // Decoding doesn't recover after an error |
| 4943 | } |
| 4944 | } else { |
| 4945 | // Should be no error |
| 4946 | if(nReturn) { |
| 4947 | return -9; // Should not have got an error |
| 4948 | } |
| 4949 | } |
| 4950 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4951 | return -7; |
| 4952 | } |
| 4953 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4954 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4955 | if(nReturn) { |
| 4956 | return -3; |
| 4957 | } |
| 4958 | return 0; |
| 4959 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4960 | |
| 4961 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4962 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4963 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4964 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4965 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4966 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4967 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4968 | int nReturn = parse_indeflen_nested(Nested, i); |
| 4969 | if(nReturn) { |
| 4970 | return nReturn; |
| 4971 | } |
| 4972 | } |
| 4973 | return 0; |
| 4974 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4975 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4976 | // [1, [2, 3]] |
| 4977 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 4978 | // No closing break |
| 4979 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 4980 | // Not enough closing breaks |
| 4981 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 4982 | // Too many closing breaks |
| 4983 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 4984 | // Unclosed indeflen inside def len |
| 4985 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 4986 | // confused tag |
| 4987 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4988 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4989 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4990 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4991 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4992 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4993 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4994 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4995 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4996 | QCBORDecodeContext DC; |
| 4997 | QCBORItem Item; |
| 4998 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4999 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5000 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5001 | |
| 5002 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 5003 | Item.uNestingLevel != 0 || |
| 5004 | Item.uNextNestLevel != 1) { |
| 5005 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5006 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5007 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5008 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5009 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 5010 | Item.uNestingLevel != 1 || |
| 5011 | Item.uNextNestLevel != 1) { |
| 5012 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5013 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5014 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5015 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5016 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 5017 | Item.uNestingLevel != 1 || |
| 5018 | Item.uNextNestLevel != 2) { |
| 5019 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5020 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5021 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5022 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 5023 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5024 | Item.uNestingLevel != 2 || |
| 5025 | Item.uNextNestLevel != 2) { |
| 5026 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5027 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5028 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5029 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 5030 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5031 | Item.uNestingLevel != 2 || |
| 5032 | Item.uNextNestLevel != 0) { |
| 5033 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5034 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5035 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5036 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5037 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5038 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5039 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5040 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5041 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5042 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5043 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5044 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5045 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5046 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5047 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5048 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5049 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5050 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5051 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5052 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5053 | } |
| 5054 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5055 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5056 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5057 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5058 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5059 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5060 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5061 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5062 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5063 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5067 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5068 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5069 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5070 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5071 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5072 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5073 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5074 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5075 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5076 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5077 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5078 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5079 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5080 | |
| 5081 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5082 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5083 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5084 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5085 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5086 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5087 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5088 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5089 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +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 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 5093 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5094 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 5095 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5096 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 5097 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5098 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 5099 | return -140; |
| 5100 | } |
| 5101 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5102 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5103 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5104 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5105 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5106 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5107 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5108 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 5109 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5110 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5111 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5112 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +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 -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5116 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5117 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5118 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5119 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5120 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 5121 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5122 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5123 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5124 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5125 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5126 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5127 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5128 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5129 | |
| 5130 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5131 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5132 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5133 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5134 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5135 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 5136 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 5137 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5138 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5139 | #else /* QCBOR_DISABLE_TAGS */ |
| 5140 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 5141 | return -20; |
| 5142 | } |
| 5143 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5144 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5145 | return 0; |
| 5146 | } |
| 5147 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5148 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5149 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 5150 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5151 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 5152 | 0x81, // Array of length one |
| 5153 | 0x7f, // text string marked with indefinite length |
| 5154 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5155 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5156 | 0xff // ending break |
| 5157 | }; |
| 5158 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5159 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5160 | 0x81, // Array of length one |
| 5161 | 0x7f, // text string marked with indefinite length |
| 5162 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5163 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 5164 | 0xff // ending break |
| 5165 | }; |
| 5166 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5167 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5168 | 0x81, // Array of length one |
| 5169 | 0x7f, // text string marked with indefinite length |
| 5170 | 0x01, 0x02, // Not a string |
| 5171 | 0xff // ending break |
| 5172 | }; |
| 5173 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5174 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5175 | 0x81, // Array of length one |
| 5176 | 0x7f, // text string marked with indefinite length |
| 5177 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5178 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5179 | // missing end of string |
| 5180 | }; |
| 5181 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5182 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5183 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5184 | 0xa1, // Array of length one |
| 5185 | 0x7f, // text string marked with indefinite length |
| 5186 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 5187 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5188 | 0xff, // ending break |
| 5189 | 0x01 // integer being labeled. |
| 5190 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5191 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5192 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5193 | /** |
| 5194 | Make an indefinite length string |
| 5195 | |
| 5196 | @param Storage Storage for string, must be 144 bytes in size |
| 5197 | @return The indefinite length string |
| 5198 | |
| 5199 | This makes an array with one indefinite length string that has 7 chunks |
| 5200 | from size of 1 byte up to 64 bytes. |
| 5201 | */ |
| 5202 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5203 | { |
| 5204 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5205 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5206 | UsefulOutBuf_Init(&UOB, Storage); |
| 5207 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 5208 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5209 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5210 | uint8_t uStringByte = 0; |
| 5211 | // Use of type int is intentional |
| 5212 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 5213 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5214 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5215 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 5216 | for(int j = 0; j < uChunkSize; j++) { |
| 5217 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 5218 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5219 | } |
| 5220 | } |
| 5221 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 5222 | |
| 5223 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5224 | } |
| 5225 | |
| 5226 | static int CheckBigString(UsefulBufC BigString) |
| 5227 | { |
| 5228 | if(BigString.len != 255) { |
| 5229 | return 1; |
| 5230 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5231 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5232 | for(uint8_t i = 0; i < 255; i++){ |
| 5233 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 5234 | return 1; |
| 5235 | } |
| 5236 | } |
| 5237 | return 0; |
| 5238 | } |
| 5239 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5240 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5241 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5242 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5243 | QCBORDecodeContext DC; |
| 5244 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5245 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5246 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5247 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5248 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5249 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5250 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5251 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5252 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5253 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5254 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5255 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5256 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5257 | return -2; |
| 5258 | } |
| 5259 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 5260 | return -3; |
| 5261 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5262 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5263 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5264 | return -4; |
| 5265 | } |
| 5266 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 5267 | return -5; |
| 5268 | } |
| 5269 | if(QCBORDecode_Finish(&DC)) { |
| 5270 | return -6; |
| 5271 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5272 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5273 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5274 | QCBORDecode_Init(&DC, |
| 5275 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 5276 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5277 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5278 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5279 | return -7; |
| 5280 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5281 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5282 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5283 | return -8; |
| 5284 | } |
| 5285 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5286 | return -9; |
| 5287 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5288 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5289 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5290 | return -10; |
| 5291 | } |
| 5292 | |
| 5293 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5294 | QCBORDecode_Init(&DC, |
| 5295 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 5296 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5297 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5298 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5299 | return -11; |
| 5300 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5301 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5302 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5303 | return -12; |
| 5304 | } |
| 5305 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5306 | return -13; |
| 5307 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5308 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5309 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5310 | return -14; |
| 5311 | } |
| 5312 | |
| 5313 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5314 | QCBORDecode_Init(&DC, |
| 5315 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 5316 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5317 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5318 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5319 | return -15; |
| 5320 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5321 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5322 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5323 | return -16; |
| 5324 | } |
| 5325 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5326 | return -17; |
| 5327 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5328 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5329 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 5330 | return -18; |
| 5331 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5332 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5333 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5334 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5335 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5336 | QCBORDecode_GetNext(&DC, &Item); |
| 5337 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5338 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5339 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5340 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5341 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5342 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5343 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5344 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5345 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5346 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5347 | |
| 5348 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 5349 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5350 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5351 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5352 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5353 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 5354 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 5355 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5356 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5357 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 5358 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5359 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5360 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5361 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5362 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5363 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5364 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5365 | QCBORDecode_GetNext(&DC, &Item); |
| 5366 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5367 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5368 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5369 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5370 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5371 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5372 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5373 | // ---- big bstr ----- |
| 5374 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5375 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5376 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5377 | return -25; |
| 5378 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5379 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5380 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5381 | return -26; |
| 5382 | } |
| 5383 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5384 | return -26; |
| 5385 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5386 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5387 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5388 | return -27; |
| 5389 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5390 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5391 | return -28; |
| 5392 | } |
| 5393 | if(CheckBigString(Item.val.string)) { |
| 5394 | return -3; |
| 5395 | } |
| 5396 | if(QCBORDecode_Finish(&DC)) { |
| 5397 | return -29; |
| 5398 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5399 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5400 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5401 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5402 | 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] | 5403 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5404 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5405 | return -30; |
| 5406 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5407 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5408 | QCBORDecode_GetNext(&DC, &Item); |
| 5409 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 5410 | return -31; |
| 5411 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5412 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5413 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 5414 | return -32; |
| 5415 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5416 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5417 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5418 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 5419 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 5420 | return -33; |
| 5421 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5422 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5423 | if(QCBORDecode_Finish(&DC)) { |
| 5424 | return -34; |
| 5425 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5426 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5427 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5428 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5429 | } |
| 5430 | |
| 5431 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5432 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5433 | { |
| 5434 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5435 | QCBORError nCBORError; |
| 5436 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5437 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5438 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5439 | QCBORDecode_Init(&DC, |
| 5440 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 5441 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5442 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5443 | 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] | 5444 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5445 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 5446 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5447 | return -1; |
| 5448 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5449 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5450 | if(CheckCSRMaps(&DC)) { |
| 5451 | return -2; |
| 5452 | } |
| 5453 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5454 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5455 | // Next parse, save pointers to a few strings, destroy original and |
| 5456 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5457 | 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] | 5458 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5459 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5460 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 5461 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5462 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5463 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5464 | QCBORItem Item1, Item2, Item3, Item4; |
| 5465 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5466 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5467 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5468 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5469 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5470 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5471 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5472 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5473 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5474 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5475 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5476 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5477 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5478 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 5479 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5480 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5481 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5482 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 5483 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5484 | Item1.uDataAlloc != 0 || |
| 5485 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5486 | UsefulBufCompareToSZ(Item1.label.string, "first integer") || |
| 5487 | Item1.label.string.ptr < Pool.ptr || |
| 5488 | 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] | 5489 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5490 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5491 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5492 | |
| 5493 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5494 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5495 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5496 | Item2.uDataAlloc != 0 || |
| 5497 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5498 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5499 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5500 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5501 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5502 | Item3.uDataAlloc == 0 || |
| 5503 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5504 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5505 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5506 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5507 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5508 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5509 | Item4.uDataAlloc == 0 || |
| 5510 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5511 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5512 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5513 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5514 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5515 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5516 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5517 | QCBORDecode_Init(&DC, |
| 5518 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 5519 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5520 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 5521 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5522 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5523 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5524 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5525 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5526 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5527 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 5528 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 5529 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 5530 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 5531 | } |
| 5532 | } |
| 5533 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5534 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5535 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5536 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5537 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5538 | |
| 5539 | return 0; |
| 5540 | } |
| 5541 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5542 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5543 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 5544 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5545 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5546 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5547 | QCBORDecodeContext DC; |
| 5548 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 5549 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5550 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5551 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5552 | // Then fish into the internals of the decode context |
| 5553 | // to get the allocator function so it can be called directly. |
| 5554 | // Also figure out how much pool is available for use |
| 5555 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5556 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5557 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 5558 | if(nError) { |
| 5559 | return -9; |
| 5560 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5561 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 5562 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5563 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5564 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5565 | // First test -- ask for one more byte than available and see failure |
| 5566 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5567 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 5568 | return -1; |
| 5569 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5570 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5571 | // Re do the set up for the next test that will do a successful alloc, |
| 5572 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5573 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5574 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5575 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5576 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5577 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5578 | // Allocate one byte less than available and see success |
| 5579 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5580 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5581 | return -2; |
| 5582 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5583 | // Ask for some more and see failure |
| 5584 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5585 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 5586 | return -3; |
| 5587 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5588 | // Free the first allocate, retry the second and see success |
| 5589 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 5590 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5591 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 5592 | return -4; |
| 5593 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5594 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5595 | // Re do set up for next test that involves a successful alloc, |
| 5596 | // and a successful realloc and a failed realloc |
| 5597 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5598 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5599 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5600 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5601 | // Allocate half the pool and see success |
| 5602 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5603 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5604 | return -5; |
| 5605 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5606 | // Reallocate to take up the whole pool and see success |
| 5607 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5608 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 5609 | return -6; |
| 5610 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5611 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5612 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 5613 | return -7; |
| 5614 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5615 | // Try to allocate more to be sure there is failure after a realloc |
| 5616 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 5617 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5618 | return -8; |
| 5619 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5620 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5621 | return 0; |
| 5622 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 5623 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5624 | |
| 5625 | /* Just enough of an allocator to test configuration of one */ |
| 5626 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 5627 | { |
| 5628 | (void)pOldMem; // unused variable |
| 5629 | |
| 5630 | if(uNewSize) { |
| 5631 | // Assumes the context pointer is the buffer and |
| 5632 | // nothing too big will ever be asked for. |
| 5633 | // This is only good for this basic test! |
| 5634 | return (UsefulBuf) {pCtx, uNewSize}; |
| 5635 | } else { |
| 5636 | return NULLUsefulBuf; |
| 5637 | } |
| 5638 | } |
| 5639 | |
| 5640 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5641 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5642 | { |
| 5643 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5644 | // nothing can be done with it unless that is set up. |
| 5645 | QCBORDecodeContext DC; |
| 5646 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 5647 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 5648 | |
| 5649 | uint8_t pAllocatorBuffer[50]; |
| 5650 | |
| 5651 | // This is really just to test that this call works. |
| 5652 | // The full functionality of string allocators is tested |
| 5653 | // elsewhere with the MemPool internal allocator. |
| 5654 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 5655 | |
| 5656 | QCBORItem Item; |
| 5657 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 5658 | return -1; |
| 5659 | } |
| 5660 | |
| 5661 | if(Item.uDataAlloc == 0 || |
| 5662 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 5663 | Item.val.string.ptr != pAllocatorBuffer) { |
| 5664 | return -2; |
| 5665 | } |
| 5666 | |
| 5667 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 5668 | return -3; |
| 5669 | } |
| 5670 | |
| 5671 | return 0; |
| 5672 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5673 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5674 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5675 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5676 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5677 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5678 | struct EaMTest { |
| 5679 | const char *szName; |
| 5680 | UsefulBufC Input; |
| 5681 | uint8_t uTagRequirement; |
| 5682 | bool bHasTags; |
| 5683 | |
| 5684 | /* Expected values for GetNext */ |
| 5685 | QCBORError uExpectedErrorGN; |
| 5686 | uint8_t uQCBORTypeGN; |
| 5687 | int64_t nExponentGN; |
| 5688 | int64_t nMantissaGN; |
| 5689 | UsefulBufC MantissaGN; |
| 5690 | |
| 5691 | /* Expected values for GetDecimalFraction */ |
| 5692 | QCBORError uExpectedErrorGDF; |
| 5693 | int64_t nExponentGDF; |
| 5694 | int64_t nMantissaGDF; |
| 5695 | |
| 5696 | /* Expected values for GetDecimalFractionBig */ |
| 5697 | QCBORError uExpectedErrorGDFB; |
| 5698 | int64_t nExponentGDFB; |
| 5699 | UsefulBufC MantissaGDFB; |
| 5700 | bool IsNegativeGDFB; |
| 5701 | |
| 5702 | /* Expected values for GetBigFloat */ |
| 5703 | QCBORError uExpectedErrorGBF; |
| 5704 | int64_t nExponentGBF; |
| 5705 | int64_t nMantissaGBF; |
| 5706 | |
| 5707 | /* Expected values for GetBigFloatBig */ |
| 5708 | QCBORError uExpectedErrorGBFB; |
| 5709 | int64_t nExponentGBFB; |
| 5710 | UsefulBufC MantissaGBFB; |
| 5711 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5712 | }; |
| 5713 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5714 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5715 | |
| 5716 | static const struct EaMTest pEaMTests[] = { |
| 5717 | { |
| 5718 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 5719 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5720 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 5721 | false, |
| 5722 | |
| 5723 | QCBOR_SUCCESS, /* for GetNext */ |
| 5724 | QCBOR_TYPE_ARRAY, |
| 5725 | 0, |
| 5726 | 0, |
| 5727 | {(const uint8_t []){0x00}, 1}, |
| 5728 | |
| 5729 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 5730 | -1, |
| 5731 | 3, |
| 5732 | |
| 5733 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5734 | -1, |
| 5735 | {(const uint8_t []){0x03}, 1}, |
| 5736 | false, |
| 5737 | |
| 5738 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5739 | -1, |
| 5740 | 3, |
| 5741 | |
| 5742 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5743 | -1, |
| 5744 | {(const uint8_t []){0x03}, 1}, |
| 5745 | false |
| 5746 | }, |
| 5747 | |
| 5748 | { |
| 5749 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 5750 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5751 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5752 | false, |
| 5753 | |
| 5754 | QCBOR_SUCCESS, /* for GetNext */ |
| 5755 | QCBOR_TYPE_ARRAY, |
| 5756 | 0, |
| 5757 | 0, |
| 5758 | {(const uint8_t []){0x00}, 1}, |
| 5759 | |
| 5760 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5761 | 0, |
| 5762 | 0, |
| 5763 | |
| 5764 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5765 | 0, |
| 5766 | {(const uint8_t []){0x00}, 1}, |
| 5767 | false, |
| 5768 | |
| 5769 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5770 | 0, |
| 5771 | 0, |
| 5772 | |
| 5773 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5774 | 0, |
| 5775 | {(const uint8_t []){0x00}, 1}, |
| 5776 | false |
| 5777 | |
| 5778 | }, |
| 5779 | |
| 5780 | { |
| 5781 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 5782 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 5783 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5784 | true, |
| 5785 | |
| 5786 | QCBOR_SUCCESS, /* for GetNext */ |
| 5787 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 5788 | -1, |
| 5789 | 3, |
| 5790 | {(const uint8_t []){0x00}, 1}, |
| 5791 | |
| 5792 | |
| 5793 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 5794 | -1, |
| 5795 | 3, |
| 5796 | |
| 5797 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5798 | -1, |
| 5799 | {(const uint8_t []){0x03}, 1}, |
| 5800 | false, |
| 5801 | |
| 5802 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5803 | 0, |
| 5804 | 0, |
| 5805 | |
| 5806 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5807 | 0, |
| 5808 | {(const uint8_t []){0x00}, 1}, |
| 5809 | false |
| 5810 | }, |
| 5811 | { |
| 5812 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 5813 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 5814 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5815 | true, |
| 5816 | |
| 5817 | QCBOR_SUCCESS, /* for GetNext */ |
| 5818 | QCBOR_TYPE_BIGFLOAT, |
| 5819 | 300, |
| 5820 | 100, |
| 5821 | {(const uint8_t []){0x00}, 1}, |
| 5822 | |
| 5823 | |
| 5824 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5825 | 0, |
| 5826 | 0, |
| 5827 | |
| 5828 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5829 | 0, |
| 5830 | {(const uint8_t []){0x03}, 1}, |
| 5831 | false, |
| 5832 | |
| 5833 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5834 | 300, |
| 5835 | 100, |
| 5836 | |
| 5837 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5838 | 300, |
| 5839 | {(const uint8_t []){0x64}, 1}, |
| 5840 | false |
| 5841 | }, |
| 5842 | |
| 5843 | { |
| 5844 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 5845 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5846 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 5847 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5848 | true, |
| 5849 | |
| 5850 | QCBOR_SUCCESS, /* for GetNext */ |
| 5851 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5852 | -20, |
| 5853 | 0, |
| 5854 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5855 | |
| 5856 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 5857 | 0, |
| 5858 | 0, |
| 5859 | |
| 5860 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5861 | -20, |
| 5862 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5863 | false, |
| 5864 | |
| 5865 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5866 | 0, |
| 5867 | 0, |
| 5868 | |
| 5869 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5870 | 0, |
| 5871 | {(const uint8_t []){0x00}, 0}, |
| 5872 | false |
| 5873 | }, |
| 5874 | |
| 5875 | { |
| 5876 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 5877 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5878 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 5879 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5880 | true, |
| 5881 | |
| 5882 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 5883 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5884 | 0, |
| 5885 | 0, |
| 5886 | {(const uint8_t []){0x00}, 0}, |
| 5887 | |
| 5888 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 5889 | 0, |
| 5890 | 0, |
| 5891 | |
| 5892 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 5893 | 0, |
| 5894 | {(const uint8_t []){0x00}, 0}, |
| 5895 | false, |
| 5896 | |
| 5897 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 5898 | 0, |
| 5899 | 0, |
| 5900 | |
| 5901 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 5902 | 0, |
| 5903 | {(const uint8_t []){0x00}, 0}, |
| 5904 | false |
| 5905 | }, |
| 5906 | { |
| 5907 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 5908 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 5909 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5910 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5911 | true, |
| 5912 | |
| 5913 | QCBOR_SUCCESS, /* for GetNext */ |
| 5914 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5915 | -20, |
| 5916 | 0, |
| 5917 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5918 | |
| 5919 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5920 | 0, |
| 5921 | 0, |
| 5922 | |
| 5923 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5924 | -20, |
| 5925 | {(const uint8_t []){0x00}, 1}, |
| 5926 | false, |
| 5927 | |
| 5928 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5929 | -20, |
| 5930 | 4294967295, |
| 5931 | |
| 5932 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5933 | -20, |
| 5934 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5935 | false |
| 5936 | }, |
| 5937 | |
| 5938 | { |
| 5939 | /* Special case for test 8. Don't renumber it. */ |
| 5940 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 5941 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 5942 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5943 | true, |
| 5944 | |
| 5945 | QCBOR_SUCCESS, /* for GetNext */ |
| 5946 | QCBOR_TYPE_ARRAY, |
| 5947 | 0, |
| 5948 | 0, |
| 5949 | {(const uint8_t []){0x00}, 1}, |
| 5950 | |
| 5951 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5952 | 0, |
| 5953 | 0, |
| 5954 | |
| 5955 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5956 | -20, |
| 5957 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5958 | false, |
| 5959 | |
| 5960 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 5961 | 0, |
| 5962 | 0, |
| 5963 | |
| 5964 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5965 | -20, |
| 5966 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5967 | false |
| 5968 | }, |
| 5969 | |
| 5970 | { |
| 5971 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 5972 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5973 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 5974 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5975 | true, |
| 5976 | |
| 5977 | QCBOR_SUCCESS, /* for GetNext */ |
| 5978 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 5979 | 9223372036854775807, |
| 5980 | 0, |
| 5981 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5982 | |
| 5983 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5984 | 0, |
| 5985 | 0, |
| 5986 | |
| 5987 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5988 | 9223372036854775807, |
| 5989 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5990 | true, |
| 5991 | |
| 5992 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5993 | 0, |
| 5994 | 0, |
| 5995 | |
| 5996 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5997 | 0, |
| 5998 | {(const uint8_t []){0x00}, 1}, |
| 5999 | false |
| 6000 | }, |
| 6001 | }; |
| 6002 | |
| 6003 | |
| 6004 | |
| 6005 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6006 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6007 | size_t uIndex; |
| 6008 | QCBORDecodeContext DCtx; |
| 6009 | QCBORItem Item; |
| 6010 | QCBORError uError; |
| 6011 | int64_t nMantissa, nExponent; |
| 6012 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 6013 | UsefulBufC Mantissa; |
| 6014 | bool bMantissaIsNegative; |
| 6015 | |
| 6016 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 6017 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 6018 | /* Decode with GetNext */ |
| 6019 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6020 | |
| 6021 | if(uIndex + 1 == 9) { |
| 6022 | nExponent = 99; // just to set a break point |
| 6023 | } |
| 6024 | |
| 6025 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6026 | #ifdef QCBOR_DISABLE_TAGS |
| 6027 | /* Test 8 is a special case when tags are disabled */ |
| 6028 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 6029 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6030 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 6031 | } |
| 6032 | } else { |
| 6033 | #endif |
| 6034 | /* Now check return code, data type, mantissa and exponent */ |
| 6035 | if(pT->uExpectedErrorGN != uError) { |
| 6036 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 6037 | } |
| 6038 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 6039 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 6040 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 6041 | } |
| 6042 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 6043 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 6044 | } |
| 6045 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 6046 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 6047 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 6048 | } |
| 6049 | } else { |
| 6050 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 6051 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 6052 | } |
| 6053 | } |
| 6054 | } |
| 6055 | #ifdef QCBOR_DISABLE_TAGS |
| 6056 | } |
| 6057 | #endif |
| 6058 | |
| 6059 | /* Decode with GetDecimalFraction */ |
| 6060 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6061 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 6062 | pT->uTagRequirement, |
| 6063 | &nMantissa, |
| 6064 | &nExponent); |
| 6065 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6066 | #ifdef QCBOR_DISABLE_TAGS |
| 6067 | if(pT->bHasTags) { |
| 6068 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6069 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 6070 | } |
| 6071 | } else { |
| 6072 | #endif |
| 6073 | /* Now check return code, mantissa and exponent */ |
| 6074 | if(pT->uExpectedErrorGDF != uError) { |
| 6075 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 6076 | } |
| 6077 | if(uError == QCBOR_SUCCESS) { |
| 6078 | if(pT->nExponentGDF != nExponent) { |
| 6079 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 6080 | } |
| 6081 | if(pT->nMantissaGDF != nMantissa) { |
| 6082 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 6083 | } |
| 6084 | } |
| 6085 | #ifdef QCBOR_DISABLE_TAGS |
| 6086 | } |
| 6087 | #endif |
| 6088 | |
| 6089 | /* Decode with GetDecimalFractionBig */ |
| 6090 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6091 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 6092 | pT->uTagRequirement, |
| 6093 | MantissaBuf, |
| 6094 | &Mantissa, |
| 6095 | &bMantissaIsNegative, |
| 6096 | &nExponent); |
| 6097 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6098 | #ifdef QCBOR_DISABLE_TAGS |
| 6099 | if(pT->bHasTags) { |
| 6100 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6101 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 6102 | } |
| 6103 | } else { |
| 6104 | #endif |
| 6105 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 6106 | if(pT->uExpectedErrorGDFB != uError) { |
| 6107 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 6108 | } |
| 6109 | if(uError == QCBOR_SUCCESS) { |
| 6110 | if(pT->nExponentGDFB != nExponent) { |
| 6111 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 6112 | } |
| 6113 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 6114 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 6115 | } |
| 6116 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 6117 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 6118 | } |
| 6119 | } |
| 6120 | #ifdef QCBOR_DISABLE_TAGS |
| 6121 | } |
| 6122 | #endif |
| 6123 | |
| 6124 | /* Decode with GetBigFloat */ |
| 6125 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6126 | QCBORDecode_GetBigFloat(&DCtx, |
| 6127 | pT->uTagRequirement, |
| 6128 | &nMantissa, |
| 6129 | &nExponent); |
| 6130 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6131 | #ifdef QCBOR_DISABLE_TAGS |
| 6132 | if(pT->bHasTags) { |
| 6133 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6134 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 6135 | } |
| 6136 | } else { |
| 6137 | #endif |
| 6138 | /* Now check return code, mantissa and exponent */ |
| 6139 | if(pT->uExpectedErrorGBF != uError) { |
| 6140 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 6141 | } |
| 6142 | if(uError == QCBOR_SUCCESS) { |
| 6143 | if(pT->nExponentGBF != nExponent) { |
| 6144 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 6145 | } |
| 6146 | if(pT->nMantissaGBF != nMantissa) { |
| 6147 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 6148 | } |
| 6149 | } |
| 6150 | #ifdef QCBOR_DISABLE_TAGS |
| 6151 | } |
| 6152 | #endif |
| 6153 | |
| 6154 | /* Decode with GetBigFloatBig */ |
| 6155 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 6156 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 6157 | pT->uTagRequirement, |
| 6158 | MantissaBuf, |
| 6159 | &Mantissa, |
| 6160 | &bMantissaIsNegative, |
| 6161 | &nExponent); |
| 6162 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6163 | #ifdef QCBOR_DISABLE_TAGS |
| 6164 | if(pT->bHasTags) { |
| 6165 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6166 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 6167 | } |
| 6168 | } else { |
| 6169 | #endif |
| 6170 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 6171 | if(pT->uExpectedErrorGBFB != uError) { |
| 6172 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 6173 | } |
| 6174 | if(uError == QCBOR_SUCCESS) { |
| 6175 | if(pT->nExponentGBFB != nExponent) { |
| 6176 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 6177 | } |
| 6178 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 6179 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 6180 | } |
| 6181 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 6182 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 6183 | } |
| 6184 | } |
| 6185 | #ifdef QCBOR_DISABLE_TAGS |
| 6186 | } |
| 6187 | #endif |
| 6188 | } |
| 6189 | |
| 6190 | return 0; |
| 6191 | } |
| 6192 | |
| 6193 | |
| 6194 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 6195 | { |
| 6196 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6197 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6198 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6199 | QCBORItem item; |
| 6200 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 6201 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 6202 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 6203 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6204 | |
| 6205 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6206 | |
| 6207 | /* Now encode some stuff and then decode it */ |
| 6208 | uint8_t pBuf[40]; |
| 6209 | QCBOREncodeContext EC; |
| 6210 | UsefulBufC Encoded; |
| 6211 | |
| 6212 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 6213 | QCBOREncode_OpenArray(&EC); |
| 6214 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 6215 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 6216 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 6217 | QCBOREncode_CloseArray(&EC); |
| 6218 | QCBOREncode_Finish(&EC, &Encoded); |
| 6219 | |
| 6220 | |
| 6221 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6222 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6223 | if(uErr != QCBOR_SUCCESS) { |
| 6224 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6225 | } |
| 6226 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6227 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6228 | if(uErr != QCBOR_SUCCESS) { |
| 6229 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 6233 | item.val.expAndMantissa.nExponent != 1000 || |
| 6234 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6235 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6236 | } |
| 6237 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6238 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6239 | if(uErr != QCBOR_SUCCESS) { |
| 6240 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6241 | } |
| 6242 | |
| 6243 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 6244 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 6245 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6246 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6247 | } |
| 6248 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6249 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6250 | if(uErr != QCBOR_SUCCESS) { |
| 6251 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6252 | } |
| 6253 | |
| 6254 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 6255 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 6256 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6257 | return 106; |
| 6258 | } |
| 6259 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6260 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6261 | |
| 6262 | return 0; |
| 6263 | } |
| 6264 | |
| 6265 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6266 | int32_t ExponentAndMantissaDecodeTests(void) |
| 6267 | { |
| 6268 | int32_t rv = ProcessEaMTests(); |
| 6269 | if(rv) { |
| 6270 | return rv; |
| 6271 | } |
| 6272 | |
| 6273 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 6274 | } |
| 6275 | |
| 6276 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6277 | static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = { |
| 6278 | { "Exponent > INT64_MAX", |
| 6279 | QCBOR_DECODE_MODE_NORMAL, |
| 6280 | {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20}, |
| 6281 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6282 | }, |
| 6283 | { "Mantissa > INT64_MAX", |
| 6284 | QCBOR_DECODE_MODE_NORMAL, |
| 6285 | {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23}, |
| 6286 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6287 | }, |
| 6288 | { |
| 6289 | "End of input", |
| 6290 | QCBOR_DECODE_MODE_NORMAL, |
| 6291 | {"\xC4\x82", 2}, |
| 6292 | QCBOR_ERR_NO_MORE_ITEMS |
| 6293 | }, |
| 6294 | {"bad content for big num", |
| 6295 | QCBOR_DECODE_MODE_NORMAL, |
| 6296 | {"\xC4\x82\x01\xc3\x01", 5}, |
| 6297 | QCBOR_ERR_BAD_OPT_TAG |
| 6298 | }, |
| 6299 | {"bad content for big num", |
| 6300 | QCBOR_DECODE_MODE_NORMAL, |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 6301 | {"\xC4\x82\xC2\x01\x1F", 5}, |
| 6302 | QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6303 | }, |
| 6304 | {"Bad integer for exponent", |
| 6305 | QCBOR_DECODE_MODE_NORMAL, |
| 6306 | {"\xC4\x82\x01\x1f", 4}, |
| 6307 | QCBOR_ERR_BAD_INT |
| 6308 | }, |
| 6309 | {"Bad integer for mantissa", |
| 6310 | QCBOR_DECODE_MODE_NORMAL, |
| 6311 | {"\xC4\x82\x1f\x01", 4}, |
| 6312 | QCBOR_ERR_BAD_INT |
| 6313 | }, |
| 6314 | {"3 items in array", |
| 6315 | QCBOR_DECODE_MODE_NORMAL, |
| 6316 | {"\xC4\x83\x03\x01\x02", 5}, |
| 6317 | QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6318 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6319 | {"unterminated indefinite length array", |
| 6320 | QCBOR_DECODE_MODE_NORMAL, |
| 6321 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6322 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6323 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6324 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6325 | {"unterminated indefinite length array", |
| 6326 | QCBOR_DECODE_MODE_NORMAL, |
| 6327 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6328 | QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 6329 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6330 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6331 | {"Empty array", |
| 6332 | QCBOR_DECODE_MODE_NORMAL, |
| 6333 | {"\xC4\x80", 2}, |
| 6334 | QCBOR_ERR_NO_MORE_ITEMS |
| 6335 | }, |
| 6336 | {"Second is not an integer", |
| 6337 | QCBOR_DECODE_MODE_NORMAL, |
| 6338 | {"\xC4\x82\x03\x40", 4}, |
| 6339 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6340 | }, |
| 6341 | {"First is not an integer", |
| 6342 | QCBOR_DECODE_MODE_NORMAL, |
| 6343 | {"\xC4\x82\x40", 3}, |
| 6344 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6345 | }, |
| 6346 | {"Not an array", |
| 6347 | QCBOR_DECODE_MODE_NORMAL, |
| 6348 | {"\xC4\xA2", 2}, |
| 6349 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6350 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6351 | }; |
| 6352 | |
| 6353 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6354 | int32_t |
| 6355 | ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6356 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6357 | return ProcessDecodeFailures(ExponentAndMantissaFailures, |
| 6358 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 6359 | struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6360 | } |
| 6361 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6362 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6363 | |
| 6364 | |
| 6365 | |
| 6366 | /* |
| 6367 | Some basic CBOR with map and array used in a lot of tests. |
| 6368 | The map labels are all strings |
| 6369 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6370 | { |
| 6371 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6372 | "an array of two strings": [ |
| 6373 | "string1", "string2" |
| 6374 | ], |
| 6375 | "map in a map": { |
| 6376 | "bytes 1": h'78787878', |
| 6377 | "bytes 2": h'79797979', |
| 6378 | "another int": 98, |
| 6379 | "text 2": "lies, damn lies and statistics" |
| 6380 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6381 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6382 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6383 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6384 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6385 | { |
| 6386 | QCBORItem Item1, Item2, Item3; |
| 6387 | int64_t nDecodedInt1, nDecodedInt2; |
| 6388 | UsefulBufC B1, B2, S1, S2, S3; |
| 6389 | |
| 6390 | QCBORDecodeContext DCtx; |
| 6391 | QCBORError nCBORError; |
| 6392 | |
| 6393 | QCBORDecode_Init(&DCtx, input, 0); |
| 6394 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6395 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6396 | |
| 6397 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 6398 | |
| 6399 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6400 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6401 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 6402 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 6403 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6404 | QCBORDecode_ExitMap(&DCtx); |
| 6405 | |
| 6406 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6407 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6408 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 6409 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6410 | return -400; |
| 6411 | } |
| 6412 | QCBORDecode_ExitArray(&DCtx); |
| 6413 | |
| 6414 | // Parse the same array again using GetText() instead of GetItem() |
| 6415 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6416 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 6417 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6418 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6419 | return 5000; |
| 6420 | } |
| 6421 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 6422 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6423 | return 5001; |
| 6424 | } */ |
| 6425 | |
| 6426 | QCBORDecode_ExitArray(&DCtx); |
| 6427 | |
| 6428 | QCBORDecode_ExitMap(&DCtx); |
| 6429 | |
| 6430 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 6431 | |
| 6432 | if(nCBORError) { |
| 6433 | return (int32_t)nCBORError; |
| 6434 | } |
| 6435 | |
| 6436 | if(nDecodedInt1 != 42) { |
| 6437 | return 1001; |
| 6438 | } |
| 6439 | |
| 6440 | if(nDecodedInt2 != 98) { |
| 6441 | return 1002; |
| 6442 | } |
| 6443 | |
| 6444 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6445 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6446 | return 1003; |
| 6447 | } |
| 6448 | |
| 6449 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6450 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6451 | return 1004; |
| 6452 | } |
| 6453 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6454 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6455 | return 1005; |
| 6456 | } |
| 6457 | |
| 6458 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 6459 | return 1006; |
| 6460 | } |
| 6461 | |
| 6462 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 6463 | return 1007; |
| 6464 | } |
| 6465 | |
| 6466 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 6467 | return 1008; |
| 6468 | } |
| 6469 | |
| 6470 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 6471 | return 1009; |
| 6472 | } |
| 6473 | |
| 6474 | return 0; |
| 6475 | } |
| 6476 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6477 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6478 | { |
| 6479 | -75008: h'05083399', |
| 6480 | 88: [], |
| 6481 | 100100: { |
| 6482 | "sub1": { |
| 6483 | 10: [ |
| 6484 | 0 |
| 6485 | ], |
| 6486 | -75009: h'A46823990001', |
| 6487 | 100100: { |
| 6488 | "json": "{ \"ueid\", \"xyz\"}", |
| 6489 | "subsub": { |
| 6490 | 100002: h'141813191001' |
| 6491 | } |
| 6492 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6493 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6494 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6495 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6496 | */ |
| 6497 | |
| 6498 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6499 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 6500 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 6501 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 6502 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 6503 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 6504 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 6505 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 6506 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 6507 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 6508 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 6509 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 6510 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6511 | }; |
| 6512 | |
| 6513 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 6514 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 6515 | { |
| 6516 | UsefulBufC String; |
| 6517 | |
| 6518 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 6519 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 6520 | |
| 6521 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6522 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 6523 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6524 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 6525 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 6526 | if(QCBORDecode_GetError(pDCtx)) { |
| 6527 | return 4001; |
| 6528 | } |
| 6529 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 6530 | return 4002; |
| 6531 | } |
| 6532 | QCBORDecode_ExitMap(pDCtx); |
| 6533 | QCBORDecode_ExitMap(pDCtx); |
| 6534 | QCBORDecode_ExitMap(pDCtx); |
| 6535 | QCBORDecode_ExitMap(pDCtx); |
| 6536 | |
| 6537 | return 0; |
| 6538 | } |
| 6539 | |
| 6540 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 6541 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 6542 | { |
| 6543 | QCBORItem Item; |
| 6544 | QCBORError uErr; |
| 6545 | |
| 6546 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 6547 | for(int x = 0; x < 20; x++) { |
| 6548 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6549 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6550 | return 4100; |
| 6551 | |
| 6552 | } |
| 6553 | } |
| 6554 | QCBORDecode_ExitArray(pDCtx); |
| 6555 | if(QCBORDecode_GetError(pDCtx)) { |
| 6556 | return 4101; |
| 6557 | } |
| 6558 | |
| 6559 | return 0; |
| 6560 | } |
| 6561 | |
| 6562 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 6563 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 6564 | { |
| 6565 | QCBORError uErr; |
| 6566 | |
| 6567 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6568 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 6569 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6570 | int64_t nInt = 99; |
| 6571 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 6572 | if(nInt != 0) { |
| 6573 | return 4200; |
| 6574 | } |
| 6575 | for(int x = 0; x < 20; x++) { |
| 6576 | QCBORItem Item; |
| 6577 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6578 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6579 | return 4201; |
| 6580 | |
| 6581 | } |
| 6582 | } |
| 6583 | QCBORDecode_ExitArray(pDCtx); |
| 6584 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 6585 | return 4202; |
| 6586 | } |
| 6587 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6588 | UsefulBufC dD; |
| 6589 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6590 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6591 | return 4203; |
| 6592 | } |
| 6593 | for(int x = 0; x < 20; x++) { |
| 6594 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6595 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 6596 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6597 | return 4204; |
| 6598 | } |
| 6599 | } |
| 6600 | QCBORDecode_ExitArray(pDCtx); |
| 6601 | QCBORDecode_ExitMap(pDCtx); |
| 6602 | QCBORDecode_ExitMap(pDCtx); |
| 6603 | |
| 6604 | return 0; |
| 6605 | } |
| 6606 | |
| 6607 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 6608 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 6609 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6610 | { |
| 6611 | QCBORDecodeContext DCtx; |
| 6612 | int32_t nReturn; |
| 6613 | QCBORError uErr; |
| 6614 | |
| 6615 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 6616 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6617 | |
| 6618 | for(int j = 0; j < 5; j++) { |
| 6619 | for(int i = 0; i < 20; i++) { |
| 6620 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 6621 | if(nReturn) { |
| 6622 | return nReturn; |
| 6623 | } |
| 6624 | } |
| 6625 | |
| 6626 | for(int i = 0; i < 20; i++) { |
| 6627 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 6628 | if(nReturn ) { |
| 6629 | return nReturn; |
| 6630 | } |
| 6631 | } |
| 6632 | |
| 6633 | for(int i = 0; i < 20; i++) { |
| 6634 | nReturn = DecodeNestedGetZero(&DCtx); |
| 6635 | if(nReturn ) { |
| 6636 | return nReturn; |
| 6637 | } |
| 6638 | } |
| 6639 | } |
| 6640 | |
| 6641 | QCBORDecode_ExitMap(&DCtx); |
| 6642 | uErr = QCBORDecode_Finish(&DCtx); |
| 6643 | if(uErr) { |
| 6644 | return (int32_t)uErr + 4100; |
| 6645 | } |
| 6646 | |
| 6647 | return 0; |
| 6648 | } |
| 6649 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6650 | |
| 6651 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6652 | [ |
| 6653 | 23, |
| 6654 | 6000, |
| 6655 | h'67616C6163746963', |
| 6656 | h'686176656E20746F6B656E' |
| 6657 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6658 | */ |
| 6659 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6660 | 0x84, |
| 6661 | 0x17, |
| 6662 | 0x19, 0x17, 0x70, |
| 6663 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 6664 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6665 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6666 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6667 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 6668 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6669 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6670 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 6671 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6672 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6673 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6674 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6675 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6676 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6677 | /* |
| 6678 | { |
| 6679 | 0: [], |
| 6680 | 9: [ |
| 6681 | [], |
| 6682 | [] |
| 6683 | ], |
| 6684 | 8: { |
| 6685 | 1: [], |
| 6686 | 2: {}, |
| 6687 | 3: [] |
| 6688 | }, |
| 6689 | 4: {}, |
| 6690 | 5: [], |
| 6691 | 6: [ |
| 6692 | [], |
| 6693 | [] |
| 6694 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6695 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6696 | */ |
| 6697 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6698 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 6699 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 6700 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 6701 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6702 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6703 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6704 | |
| 6705 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6706 | /* |
| 6707 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6708 | Duplicate label |
| 6709 | Integer overflow |
| 6710 | Date overflow |
| 6711 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6712 | { |
| 6713 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6714 | 3: -18446744073709551616, |
| 6715 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6716 | 5: 0, |
| 6717 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6718 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6719 | */ |
| 6720 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6721 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6722 | 0xa6, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6723 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6724 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 6725 | #else |
| 6726 | 0xa4, |
| 6727 | #endif |
| 6728 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6729 | 0x05, 0x00, |
| 6730 | 0x05, 0x00, |
| 6731 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6732 | }; |
| 6733 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6734 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6735 | static const uint8_t spUnRecoverableMapError1[] = { |
| 6736 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6737 | }; |
| 6738 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6739 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6740 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6741 | static const uint8_t spUnRecoverableMapError2[] = { |
| 6742 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6743 | }; |
| 6744 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6745 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6746 | static const uint8_t spUnRecoverableMapError3[] = { |
| 6747 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 6748 | }; |
| 6749 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6750 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6751 | static const uint8_t spUnRecoverableMapError4[] = { |
| 6752 | 0xbf, |
| 6753 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6754 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6755 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6756 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6757 | 0xff |
| 6758 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6759 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6760 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6761 | const unsigned char not_well_formed_submod_section[] = { |
| 6762 | 0xa1, 0x14, 0x1f, |
| 6763 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6764 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6765 | |
| 6766 | /* Array of length 3, but only two items. */ |
| 6767 | const unsigned char spBadConsumeInput[] = { |
| 6768 | 0x83, 0x00, 0x00 |
| 6769 | }; |
| 6770 | |
| 6771 | /* Tag nesting too deep. */ |
| 6772 | const unsigned char spBadConsumeInput2[] = { |
| 6773 | 0x81, |
| 6774 | 0xD8, 0x37, |
| 6775 | 0xD8, 0x2C, |
| 6776 | 0xD8, 0x21, |
| 6777 | 0xD6, |
| 6778 | 0xCB, |
| 6779 | 00 |
| 6780 | }; |
| 6781 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6782 | |
| 6783 | const unsigned char spBadConsumeInput4[] = { |
| 6784 | 0x81, 0x9f, 0x00, 0xff |
| 6785 | }; |
| 6786 | |
| 6787 | const unsigned char spBadConsumeInput5[] = { |
| 6788 | 0xa1, 0x80, 0x00 |
| 6789 | }; |
| 6790 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6791 | /* |
| 6792 | Lots of nesting for various nesting tests. |
| 6793 | { 1:1, |
| 6794 | 2:{ |
| 6795 | 21:21, |
| 6796 | 22:{ |
| 6797 | 221:[2111, 2112, 2113], |
| 6798 | 222:222, |
| 6799 | 223: {} |
| 6800 | }, |
| 6801 | 23: 23 |
| 6802 | }, |
| 6803 | 3:3, |
| 6804 | 4: [ {} ] |
| 6805 | } |
| 6806 | */ |
| 6807 | static const uint8_t spNested[] = { |
| 6808 | 0xA4, /* Map of 4 */ |
| 6809 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 6810 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 6811 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 6812 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 6813 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 6814 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 6815 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 6816 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 6817 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 6818 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 6819 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 6820 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 6821 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 6822 | 0xA0 /* Array entry {}, an empty map */ |
| 6823 | }; |
| 6824 | |
| 6825 | |
| 6826 | static int32_t EnterMapCursorTest(void) |
| 6827 | { |
| 6828 | QCBORDecodeContext DCtx; |
| 6829 | QCBORItem Item1; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6830 | int64_t nInt; |
| 6831 | QCBORError uErr; |
| 6832 | |
| 6833 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6834 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6835 | QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt); |
| 6836 | uErr = QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 6837 | if(uErr != QCBOR_SUCCESS) { |
| 6838 | return 701; |
| 6839 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6840 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6841 | return 700; |
| 6842 | } |
| 6843 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6844 | |
| 6845 | int i; |
| 6846 | for(i = 0; i < 13; i++) { |
| 6847 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6848 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6849 | int j; |
| 6850 | /* Move travesal cursor */ |
| 6851 | for(j = 0; j < i; j++) { |
| 6852 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6853 | } |
| 6854 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6855 | QCBORDecode_ExitMap(&DCtx); |
| 6856 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6857 | if(Item1.label.int64 != 3) { |
| 6858 | return 8000; |
| 6859 | } |
| 6860 | } |
| 6861 | |
| 6862 | for(i = 0; i < 13; i++) { |
| 6863 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6864 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6865 | int j; |
| 6866 | /* Move travesal cursor */ |
| 6867 | for(j = 0; j < i; j++) { |
| 6868 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6869 | } |
| 6870 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6871 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6872 | QCBORDecode_ExitMap(&DCtx); |
| 6873 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6874 | if(Item1.label.int64 != 23) { |
| 6875 | return 8000; |
| 6876 | } |
| 6877 | } |
| 6878 | |
| 6879 | for(i = 0; i < 13; i++) { |
| 6880 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6881 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6882 | int j; |
| 6883 | /* Move travesal cursor */ |
| 6884 | for(j = 0; j < i; j++) { |
| 6885 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6886 | } |
| 6887 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6888 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6889 | for(j = 0; j < i; j++) { |
| 6890 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6891 | } |
| 6892 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 6893 | QCBORDecode_ExitArray(&DCtx); |
| 6894 | QCBORDecode_ExitMap(&DCtx); |
| 6895 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6896 | if(Item1.label.int64 != 23) { |
| 6897 | return 8000; |
| 6898 | } |
| 6899 | QCBORDecode_ExitMap(&DCtx); |
| 6900 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6901 | if(Item1.label.int64 != 3) { |
| 6902 | return 8000; |
| 6903 | } |
| 6904 | } |
| 6905 | |
| 6906 | return 0; |
| 6907 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6908 | |
| 6909 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6910 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6911 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6912 | QCBORItem Item1; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6913 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6914 | int32_t nReturn; |
| 6915 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6916 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6917 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6918 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6919 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6920 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6921 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6922 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6923 | QCBORDecode_ExitArray(&DCtx); |
| 6924 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6925 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 6926 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6927 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6928 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6929 | QCBORDecode_ExitArray(&DCtx); |
| 6930 | QCBORDecode_ExitArray(&DCtx); |
| 6931 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6932 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 6933 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6934 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6935 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6936 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6937 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6938 | QCBORDecode_ExitArray(&DCtx); |
| 6939 | QCBORDecode_ExitMap(&DCtx); |
| 6940 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6941 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6942 | QCBORDecode_ExitMap(&DCtx); |
| 6943 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6944 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6945 | QCBORDecode_ExitArray(&DCtx); |
| 6946 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6947 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 6948 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6949 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6950 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6951 | QCBORDecode_ExitArray(&DCtx); |
| 6952 | QCBORDecode_ExitArray(&DCtx); |
| 6953 | |
| 6954 | QCBORDecode_ExitMap(&DCtx); |
| 6955 | |
| 6956 | uErr = QCBORDecode_Finish(&DCtx); |
| 6957 | if(uErr != QCBOR_SUCCESS){ |
| 6958 | return 3011; |
| 6959 | } |
| 6960 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6961 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6962 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6963 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6964 | if(nReturn) { |
| 6965 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6966 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6967 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 6968 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6969 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6970 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 6971 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6972 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6973 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6974 | if(nReturn) { |
| 6975 | return nReturn; |
| 6976 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6977 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6978 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6979 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 6980 | // These tests confirm the cursor is at the right place after entering |
| 6981 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6982 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6983 | |
| 6984 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6985 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6986 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6987 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6988 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6989 | return 2001; |
| 6990 | } |
| 6991 | |
| 6992 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6993 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 6994 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6995 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6996 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 6997 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6998 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6999 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 7000 | return 2051; |
| 7001 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7002 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7003 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 7004 | return 2002; |
| 7005 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7006 | QCBORDecode_ExitArray(&DCtx); |
| 7007 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 7008 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7009 | UsefulBuf_Compare(ArrayItem.label.string, |
| 7010 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 7011 | return 2052; |
| 7012 | } |
| 7013 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7014 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7015 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7016 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 7017 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7018 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7019 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7020 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 7021 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7022 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 7023 | return 2003; |
| 7024 | } |
| 7025 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7026 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7027 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7028 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7029 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7030 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7031 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7032 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7033 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7034 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7035 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 7036 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7037 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 7038 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7039 | } |
| 7040 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7041 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7042 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 7043 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 7044 | QCBORDecode_ExitArray(&DCtx); |
| 7045 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 7046 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 7047 | return 2006; |
| 7048 | } |
| 7049 | QCBORDecode_ExitMap(&DCtx); |
| 7050 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7051 | return 2007; |
| 7052 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7053 | #endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 7054 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 7055 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7056 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 7057 | int64_t nDecodedInt2; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7058 | |
| 7059 | UsefulBufC String; |
| 7060 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
| 7061 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7062 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
| 7063 | return 2009; |
| 7064 | } |
| 7065 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 7066 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 7067 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7068 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 7069 | return 2008; |
| 7070 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7071 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 7072 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7073 | |
| 7074 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7075 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7076 | // This will fail because the map is empty. |
| 7077 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 7078 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7079 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7080 | return 2010; |
| 7081 | } |
| 7082 | QCBORDecode_ExitMap(&DCtx); |
| 7083 | uErr = QCBORDecode_Finish(&DCtx); |
| 7084 | if(uErr != QCBOR_SUCCESS){ |
| 7085 | return 2011; |
| 7086 | } |
| 7087 | |
| 7088 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7089 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7090 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7091 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7092 | // This will fail because the map is empty. |
| 7093 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 7094 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7095 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 7096 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7097 | } |
| 7098 | QCBORDecode_ExitMap(&DCtx); |
| 7099 | uErr = QCBORDecode_Finish(&DCtx); |
| 7100 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 7101 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7102 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7103 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 7104 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7105 | |
| 7106 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7107 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 7108 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7109 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7110 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7111 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7112 | QCBORDecode_ExitArray(&DCtx); |
| 7113 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 7114 | QCBORDecode_ExitArray(&DCtx); |
| 7115 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7116 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 7117 | return 2014; |
| 7118 | } |
| 7119 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7120 | int64_t nInt; |
| 7121 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7122 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7123 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7124 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7125 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7126 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 7127 | return 2021; |
| 7128 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7129 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 7130 | return 2121; |
| 7131 | } |
| 7132 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7133 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 7134 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7135 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7136 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 7137 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7138 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 7139 | return 2023; |
| 7140 | } |
| 7141 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7142 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7143 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 7144 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7145 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7146 | return 2024; |
| 7147 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7148 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7149 | |
| 7150 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 7151 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7152 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 7153 | return 2025; |
| 7154 | } |
| 7155 | |
| 7156 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 7157 | |
| 7158 | QCBORDecode_ExitMap(&DCtx); |
| 7159 | uErr = QCBORDecode_Finish(&DCtx); |
| 7160 | if(uErr != QCBOR_SUCCESS) { |
| 7161 | return 2026; |
| 7162 | } |
| 7163 | |
| 7164 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7165 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7166 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7167 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7168 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 7169 | return 2030; |
| 7170 | } |
| 7171 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7172 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7173 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7174 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7175 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7176 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7177 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7178 | return 2031; |
| 7179 | } |
| 7180 | |
| 7181 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7182 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7183 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7184 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7185 | if(uErr != QCBOR_ERR_HIT_END) { |
| 7186 | return 2032; |
| 7187 | } |
| 7188 | |
| 7189 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7190 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7191 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7192 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7193 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 7194 | return 2033; |
| 7195 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7196 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7197 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7198 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 7199 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7200 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7201 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 7202 | return 2401; |
| 7203 | } |
| 7204 | if(QCBORDecode_GetError(&DCtx)) { |
| 7205 | return 2402; |
| 7206 | } |
| 7207 | |
| 7208 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7209 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 7210 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7211 | Item1.val.uCount != 3 || |
| 7212 | Item1.uNextNestLevel != 1) { |
| 7213 | return 2403; |
| 7214 | } |
| 7215 | if(QCBORDecode_GetError(&DCtx)) { |
| 7216 | return 2404; |
| 7217 | } |
| 7218 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7219 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 7220 | Item1.uNextNestLevel != 1 || |
| 7221 | Item1.val.int64 != 42) { |
| 7222 | return 2405; |
| 7223 | } |
| 7224 | if(QCBORDecode_GetError(&DCtx)) { |
| 7225 | return 2406; |
| 7226 | } |
| 7227 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7228 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 7229 | Item1.uNestingLevel != 1 || |
| 7230 | Item1.uNextNestLevel != 1 || |
| 7231 | Item1.val.uCount != 2) { |
| 7232 | return 2407; |
| 7233 | } |
| 7234 | if(QCBORDecode_GetError(&DCtx)) { |
| 7235 | return 2408; |
| 7236 | } |
| 7237 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7238 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7239 | Item1.uNestingLevel != 1 || |
| 7240 | Item1.uNextNestLevel != 0 || |
| 7241 | Item1.val.uCount != 4) { |
| 7242 | return 2409; |
| 7243 | } |
| 7244 | if(QCBORDecode_GetError(&DCtx)) { |
| 7245 | return 2410; |
| 7246 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7247 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7248 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7249 | nReturn = DecodeNestedIterate(); |
| 7250 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 7251 | |
| 7252 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 7253 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7254 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 7255 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 7256 | return 2500; |
| 7257 | } |
| 7258 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7259 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 7260 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7261 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7262 | return 2600; |
| 7263 | } |
| 7264 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7265 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7266 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 7267 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7268 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7269 | return 2700; |
| 7270 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7271 | #endif |
| 7272 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7273 | |
| 7274 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 7275 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7276 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7277 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7278 | return 2900; |
| 7279 | } |
| 7280 | #else |
| 7281 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7282 | return 2901; |
| 7283 | } |
| 7284 | #endif |
| 7285 | |
| 7286 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 7287 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7288 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 7289 | return 3000; |
| 7290 | } |
| 7291 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 7292 | nReturn = EnterMapCursorTest(); |
| 7293 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7294 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7295 | } |
| 7296 | |
| 7297 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7298 | struct NumberConversion { |
| 7299 | char *szDescription; |
| 7300 | UsefulBufC CBOR; |
| 7301 | int64_t nConvertedToInt64; |
| 7302 | QCBORError uErrorInt64; |
| 7303 | uint64_t uConvertToUInt64; |
| 7304 | QCBORError uErrorUint64; |
| 7305 | double dConvertToDouble; |
| 7306 | QCBORError uErrorDouble; |
| 7307 | }; |
| 7308 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7309 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 7310 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 7311 | #else |
| 7312 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 7313 | #endif |
| 7314 | |
| 7315 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7316 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7317 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7318 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 7319 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 7320 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 7321 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 7322 | }, 15}, |
| 7323 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 7324 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 7325 | 0, |
| 7326 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7327 | -4.6116860184273879E+18, |
| 7328 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 7329 | }, |
| 7330 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7331 | "too large to fit into int64_t", |
| 7332 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 7333 | 0, |
| 7334 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7335 | 0, |
| 7336 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7337 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7338 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7339 | }, |
| 7340 | { |
| 7341 | "largest negative int that fits in int64_t", |
| 7342 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 7343 | INT64_MIN, |
| 7344 | QCBOR_SUCCESS, |
| 7345 | 0, |
| 7346 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7347 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7348 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7349 | }, |
| 7350 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7351 | "negative bignum -1", |
| 7352 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 7353 | -1, |
| 7354 | QCBOR_SUCCESS, |
| 7355 | 0, |
| 7356 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7357 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7358 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7359 | }, |
| 7360 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7361 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7362 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7363 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7364 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7365 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7366 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7367 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7368 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7369 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7370 | }, |
| 7371 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7372 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7373 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7374 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7375 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7376 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7377 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7378 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7379 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7380 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7381 | }, |
| 7382 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7383 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7384 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7385 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7386 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7387 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7388 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7389 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7390 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7391 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7392 | }, |
| 7393 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7394 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7395 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 7396 | 0, |
| 7397 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7398 | 0, |
| 7399 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7400 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7401 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7402 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7403 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7404 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7405 | "Positive bignum 0x01020304 indefinite length string", |
| 7406 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 7407 | 0x01020304, |
| 7408 | QCBOR_SUCCESS, |
| 7409 | 0x01020304, |
| 7410 | QCBOR_SUCCESS, |
| 7411 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7412 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7413 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7414 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7415 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7416 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7417 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 7418 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 7419 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7420 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7421 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7422 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7423 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7424 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7425 | }, |
| 7426 | { |
| 7427 | "big float [9223372036854775806, 9223372036854775806]", |
| 7428 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7429 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 7430 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7431 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7432 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7433 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7434 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7435 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7436 | }, |
| 7437 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7438 | "Big float 3 * 2^^2", |
| 7439 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 7440 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7441 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7442 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7443 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7444 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7445 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7446 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7447 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7448 | "Decimal fraction 3/10", |
| 7449 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 7450 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7451 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7452 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7453 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7454 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7455 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 7456 | }, |
| 7457 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7458 | "extreme pos bignum", |
| 7459 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 7460 | // 50 rows of 8 is 400 digits. |
| 7461 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7462 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7463 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7464 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7465 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7466 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7467 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7468 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7469 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7470 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7471 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7472 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7473 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7474 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7475 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7476 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7477 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7478 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7479 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7480 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7481 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7482 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7483 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7484 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7485 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7486 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7487 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7488 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7489 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7490 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7491 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7492 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7493 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7494 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7495 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7496 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7497 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7498 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7499 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7500 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7501 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7502 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7503 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7504 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7505 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7506 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7507 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7508 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7509 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7510 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7511 | 404}, |
| 7512 | 0, |
| 7513 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7514 | 0, |
| 7515 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7516 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7517 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7518 | }, |
| 7519 | |
| 7520 | { |
| 7521 | "extreme neg bignum", |
| 7522 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 7523 | // 50 rows of 8 is 400 digits. |
| 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, |
| 7562 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7563 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7564 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7565 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7566 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7567 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7568 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7569 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7570 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7571 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7572 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7573 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7574 | 404}, |
| 7575 | 0, |
| 7576 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7577 | 0, |
| 7578 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7579 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7580 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7581 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7582 | |
| 7583 | { |
| 7584 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 7585 | {(uint8_t[]){ |
| 7586 | 0xC5, 0x82, |
| 7587 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7588 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7589 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7590 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7591 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7592 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7593 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7594 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7595 | }, |
| 7596 | |
| 7597 | { |
| 7598 | "bigfloat that evaluates to -INFINITY", |
| 7599 | {(uint8_t[]){ |
| 7600 | 0xC5, 0x82, |
| 7601 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7602 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7603 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7604 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7605 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7606 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7607 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7608 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7609 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7610 | { |
| 7611 | "Positive bignum 0xffff", |
| 7612 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 7613 | 65536-1, |
| 7614 | QCBOR_SUCCESS, |
| 7615 | 0xffff, |
| 7616 | QCBOR_SUCCESS, |
| 7617 | 65535.0, |
| 7618 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7619 | }, |
| 7620 | #endif /* QCBOR_DISABLE_TAGS */ |
| 7621 | { |
| 7622 | "Positive integer 18446744073709551615", |
| 7623 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 7624 | 0, |
| 7625 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7626 | 18446744073709551615ULL, |
| 7627 | QCBOR_SUCCESS, |
| 7628 | 18446744073709551615.0, |
| 7629 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7630 | }, |
| 7631 | |
| 7632 | { |
| 7633 | "Postive integer 0", |
| 7634 | {(uint8_t[]){0x0}, 1}, |
| 7635 | 0LL, |
| 7636 | QCBOR_SUCCESS, |
| 7637 | 0ULL, |
| 7638 | QCBOR_SUCCESS, |
| 7639 | 0.0, |
| 7640 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7641 | }, |
| 7642 | { |
| 7643 | "Negative integer -18446744073709551616", |
| 7644 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 7645 | -9223372036854775807-1, // INT64_MIN |
| 7646 | QCBOR_SUCCESS, |
| 7647 | 0ULL, |
| 7648 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7649 | -9223372036854775808.0, |
| 7650 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7651 | }, |
| 7652 | { |
| 7653 | "Double Floating point value 100.3", |
| 7654 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 7655 | 100L, |
| 7656 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7657 | 100ULL, |
| 7658 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7659 | 100.3, |
| 7660 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 7661 | }, |
| 7662 | { |
| 7663 | "Floating point value NaN 0xfa7fc00000", |
| 7664 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 7665 | 0, |
| 7666 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7667 | 0, |
| 7668 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7669 | NAN, |
| 7670 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7671 | }, |
| 7672 | { |
| 7673 | "half-precision Floating point value -4", |
| 7674 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 7675 | // Normal case with all enabled. |
| 7676 | -4, |
| 7677 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7678 | 0, |
| 7679 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7680 | -4.0, |
| 7681 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 7682 | }, |
| 7683 | { |
| 7684 | "+inifinity single precision", |
| 7685 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 7686 | 0, |
| 7687 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7688 | 0, |
| 7689 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 7690 | INFINITY, |
| 7691 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7692 | }, |
| 7693 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7694 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7695 | |
| 7696 | |
| 7697 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7698 | |
| 7699 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 7700 | { |
| 7701 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 7702 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7703 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 7704 | return 1; |
| 7705 | } |
| 7706 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7707 | (void)Pool; |
| 7708 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7709 | return 0; |
| 7710 | } |
| 7711 | |
| 7712 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7713 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7714 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7715 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 7716 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7717 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7718 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 7719 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7720 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7721 | // Set up the decoding context including a memory pool so that |
| 7722 | // indefinite length items can be checked |
| 7723 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7724 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7725 | |
| 7726 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7727 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7728 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7729 | } |
| 7730 | |
| 7731 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7732 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7733 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7734 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7735 | } |
| 7736 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7737 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7738 | } |
| 7739 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7740 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7741 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7742 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7743 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7744 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7745 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7746 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7747 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7748 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7749 | } |
| 7750 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7751 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7752 | } |
| 7753 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7754 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7755 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7756 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7757 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7758 | |
| 7759 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7760 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7761 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7762 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7763 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7764 | } |
| 7765 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 7766 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7767 | // NaN's can't be compared for equality. A NaN is |
| 7768 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7769 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7770 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7771 | } |
| 7772 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7773 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7774 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7775 | } |
| 7776 | } |
| 7777 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7778 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7779 | } |
| 7780 | |
| 7781 | return 0; |
| 7782 | } |
| 7783 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7784 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7785 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7786 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7787 | { |
| 7788 | QCBORDecodeContext DCtx; |
| 7789 | QCBORItem Item; |
| 7790 | QCBORError uCBORError; |
| 7791 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7792 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7793 | QCBORDecode_Init(&DCtx, |
| 7794 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 7795 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7796 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7797 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 7798 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7799 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7800 | do { |
| 7801 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7802 | } while (QCBOR_SUCCESS == uCBORError); |
| 7803 | |
| 7804 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7805 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 7806 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7807 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7808 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7809 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7810 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7811 | |
| 7812 | |
| 7813 | static const uint8_t spSequenceTestInput[] = { |
| 7814 | /* 1. The valid date string "1985-04-12" */ |
| 7815 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 7816 | |
| 7817 | /* 2. */ |
| 7818 | 0x00, |
| 7819 | |
| 7820 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 7821 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 7822 | |
| 7823 | /* 4. */ |
| 7824 | 0x62, 'h', 'i', |
| 7825 | }; |
| 7826 | |
| 7827 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7828 | int32_t CBORSequenceDecodeTests(void) |
| 7829 | { |
| 7830 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7831 | QCBORItem Item; |
| 7832 | QCBORError uCBORError; |
| 7833 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7834 | |
| 7835 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7836 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7837 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7838 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7839 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7840 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7841 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7842 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7843 | if(uCBORError != QCBOR_SUCCESS) { |
| 7844 | return 1; |
| 7845 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7846 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7847 | return 2; |
| 7848 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7849 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7850 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7851 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7852 | uConsumed != 11) { |
| 7853 | return 102; |
| 7854 | } |
| 7855 | |
| 7856 | // Get 2. |
| 7857 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7858 | if(uCBORError != QCBOR_SUCCESS) { |
| 7859 | return 66; |
| 7860 | } |
| 7861 | |
| 7862 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7863 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7864 | uConsumed != 12) { |
| 7865 | return 102; |
| 7866 | } |
| 7867 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7868 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7869 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7870 | if(uCBORError != QCBOR_SUCCESS) { |
| 7871 | return 2; |
| 7872 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7873 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7874 | return 3; |
| 7875 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7876 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7877 | // A sequence can have stuff at the end that may |
| 7878 | // or may not be valid CBOR. The protocol decoder knows |
| 7879 | // when to stop by definition of the protocol, not |
| 7880 | // when the top-level map or array is ended. |
| 7881 | // Finish still has to be called to know that |
| 7882 | // maps and arrays (if there were any) were closed |
| 7883 | // off correctly. When called like this it |
| 7884 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 7885 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7886 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 7887 | return 4; |
| 7888 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7889 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7890 | // --- Test an empty input ---- |
| 7891 | uint8_t empty[1]; |
| 7892 | UsefulBufC Empty = {empty, 0}; |
| 7893 | QCBORDecode_Init(&DCtx, |
| 7894 | Empty, |
| 7895 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7896 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7897 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7898 | if(uCBORError != QCBOR_SUCCESS) { |
| 7899 | return 5; |
| 7900 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7901 | |
| 7902 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7903 | // --- Sequence with unclosed indefinite length array --- |
| 7904 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7905 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7906 | QCBORDecode_Init(&DCtx, |
| 7907 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 7908 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7909 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7910 | // Get the first item |
| 7911 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7912 | if(uCBORError != QCBOR_SUCCESS) { |
| 7913 | return 7; |
| 7914 | } |
| 7915 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7916 | return 8; |
| 7917 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7918 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7919 | // Get a second item |
| 7920 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7921 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7922 | if(uCBORError != QCBOR_SUCCESS) { |
| 7923 | return 9; |
| 7924 | } |
| 7925 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7926 | return 10; |
| 7927 | } |
| 7928 | |
| 7929 | // Try to finish before consuming all bytes to confirm |
| 7930 | // that the still-open error is returned. |
| 7931 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7932 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7933 | return 11; |
| 7934 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7935 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7936 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7937 | return 20; |
| 7938 | } |
| 7939 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7940 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7941 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7942 | // --- Sequence with a closed indefinite length array --- |
| 7943 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7944 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7945 | QCBORDecode_Init(&DCtx, |
| 7946 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 7947 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7948 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7949 | // Get the first item |
| 7950 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7951 | if(uCBORError != QCBOR_SUCCESS) { |
| 7952 | return 12; |
| 7953 | } |
| 7954 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7955 | return 13; |
| 7956 | } |
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 | // Get a second item |
| 7959 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7960 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7961 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7962 | if(uCBORError != QCBOR_SUCCESS) { |
| 7963 | return 14; |
| 7964 | } |
| 7965 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7966 | return 15; |
| 7967 | } |
| 7968 | |
| 7969 | // Try to finish before consuming all bytes to confirm |
| 7970 | // that the still-open error is returned. |
| 7971 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7972 | if(uCBORError != QCBOR_SUCCESS) { |
| 7973 | return 16; |
| 7974 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7975 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7976 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7977 | return 20; |
| 7978 | } |
| 7979 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7980 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7981 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7982 | return 0; |
| 7983 | } |
| 7984 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7985 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 7986 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7987 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7988 | { |
| 7989 | int nErrCode; |
| 7990 | int32_t n32; |
| 7991 | int16_t n16; |
| 7992 | int8_t n8; |
| 7993 | uint32_t u32; |
| 7994 | uint16_t u16; |
| 7995 | uint8_t u8; |
| 7996 | uint64_t u64; |
| 7997 | |
| 7998 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 7999 | if(nErrCode == -1 || n32 != 1) { |
| 8000 | return 1; |
| 8001 | } |
| 8002 | |
| 8003 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 8004 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 8005 | return 2; |
| 8006 | } |
| 8007 | |
| 8008 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 8009 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 8010 | return 3; |
| 8011 | } |
| 8012 | |
| 8013 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 8014 | if(nErrCode != -1) { |
| 8015 | return 4; |
| 8016 | } |
| 8017 | |
| 8018 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 8019 | if(nErrCode != -1) { |
| 8020 | return 5; |
| 8021 | } |
| 8022 | |
| 8023 | |
| 8024 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 8025 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 8026 | return 6; |
| 8027 | } |
| 8028 | |
| 8029 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 8030 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 8031 | return 7; |
| 8032 | } |
| 8033 | |
| 8034 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 8035 | if(nErrCode == -1 || n16 != 1) { |
| 8036 | return 8; |
| 8037 | } |
| 8038 | |
| 8039 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 8040 | if(nErrCode != -1) { |
| 8041 | return 9; |
| 8042 | } |
| 8043 | |
| 8044 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 8045 | if(nErrCode != -1) { |
| 8046 | return 10; |
| 8047 | } |
| 8048 | |
| 8049 | |
| 8050 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 8051 | if(nErrCode == -1 || n8 != 1) { |
| 8052 | return 11; |
| 8053 | } |
| 8054 | |
| 8055 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 8056 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 8057 | return 12; |
| 8058 | } |
| 8059 | |
| 8060 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 8061 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 8062 | return 13; |
| 8063 | } |
| 8064 | |
| 8065 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 8066 | if(nErrCode != -1) { |
| 8067 | return 14; |
| 8068 | } |
| 8069 | |
| 8070 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 8071 | if(nErrCode != -1) { |
| 8072 | return 15; |
| 8073 | } |
| 8074 | |
| 8075 | |
| 8076 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 8077 | if(nErrCode == -1 || u32 != 1) { |
| 8078 | return 16; |
| 8079 | } |
| 8080 | |
| 8081 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 8082 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 8083 | return 17; |
| 8084 | } |
| 8085 | |
| 8086 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 8087 | if(nErrCode == -1 || u32 != 0) { |
| 8088 | return 18; |
| 8089 | } |
| 8090 | |
| 8091 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 8092 | if(nErrCode != -1) { |
| 8093 | return 19; |
| 8094 | } |
| 8095 | |
| 8096 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 8097 | if(nErrCode != -1) { |
| 8098 | return 20; |
| 8099 | } |
| 8100 | |
| 8101 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame^] | 8102 | nErrCode = QCBOR_Int64ToUInt16((int64_t)UINT16_MAX, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8103 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 8104 | return 21; |
| 8105 | } |
| 8106 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame^] | 8107 | nErrCode = QCBOR_Int64ToUInt16((int64_t)0, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8108 | if(nErrCode == -1 || u16 != 0) { |
| 8109 | return 22; |
| 8110 | } |
| 8111 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame^] | 8112 | nErrCode = QCBOR_Int64ToUInt16(1, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8113 | if(nErrCode == -1 || u16 != 1) { |
| 8114 | return 23; |
| 8115 | } |
| 8116 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame^] | 8117 | nErrCode = QCBOR_Int64ToUInt16(((int64_t)UINT16_MAX)+1, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8118 | if(nErrCode != -1) { |
| 8119 | return 24; |
| 8120 | } |
| 8121 | |
Sebastian Burck | 5a0ab73 | 2024-06-29 06:31:20 +0200 | [diff] [blame^] | 8122 | nErrCode = QCBOR_Int64ToUInt16((int64_t)-1, &u16); |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 8123 | if(nErrCode != -1) { |
| 8124 | return 25; |
| 8125 | } |
| 8126 | |
| 8127 | |
| 8128 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 8129 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 8130 | return 26; |
| 8131 | } |
| 8132 | |
| 8133 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 8134 | if(nErrCode == -1 || u8 != 0) { |
| 8135 | return 27; |
| 8136 | } |
| 8137 | |
| 8138 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 8139 | if(nErrCode == -1 || u8 != 1) { |
| 8140 | return 28; |
| 8141 | } |
| 8142 | |
| 8143 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 8144 | if(nErrCode != -1) { |
| 8145 | return 29; |
| 8146 | } |
| 8147 | |
| 8148 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 8149 | if(nErrCode != -1) { |
| 8150 | return 30; |
| 8151 | } |
| 8152 | |
| 8153 | |
| 8154 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 8155 | if(nErrCode == -1 || u64 != 1) { |
| 8156 | return 31; |
| 8157 | } |
| 8158 | |
| 8159 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 8160 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 8161 | return 32; |
| 8162 | } |
| 8163 | |
| 8164 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 8165 | if(nErrCode == -1 || u64 != 0) { |
| 8166 | return 33; |
| 8167 | } |
| 8168 | |
| 8169 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 8170 | if(nErrCode != -1) { |
| 8171 | return 34; |
| 8172 | } |
| 8173 | |
| 8174 | return 0; |
| 8175 | } |
| 8176 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8177 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8178 | |
| 8179 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8180 | /* |
| 8181 | A sequence with |
| 8182 | A wrapping bstr |
| 8183 | containing a map |
| 8184 | 1 |
| 8185 | 2 |
| 8186 | A wrapping bstr |
| 8187 | containing an array |
| 8188 | 3 |
| 8189 | wrapping bstr |
| 8190 | 4 |
| 8191 | 5 |
| 8192 | 6 |
| 8193 | array |
| 8194 | 7 |
| 8195 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8196 | */ |
| 8197 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8198 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8199 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8200 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8201 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8202 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8203 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8204 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8205 | |
| 8206 | QCBOREncode_BstrWrap(&EC); |
| 8207 | QCBOREncode_OpenMap(&EC); |
| 8208 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 8209 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 8210 | QCBOREncode_CloseMap(&EC); |
| 8211 | QCBOREncode_BstrWrap(&EC); |
| 8212 | QCBOREncode_OpenArray(&EC); |
| 8213 | QCBOREncode_AddInt64(&EC, 3); |
| 8214 | QCBOREncode_BstrWrap(&EC); |
| 8215 | QCBOREncode_AddInt64(&EC, 4); |
| 8216 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8217 | QCBOREncode_AddInt64(&EC, 5); |
| 8218 | QCBOREncode_CloseArray(&EC); |
| 8219 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8220 | QCBOREncode_AddInt64(&EC, 6); |
| 8221 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8222 | QCBOREncode_OpenArray(&EC); |
| 8223 | QCBOREncode_AddInt64(&EC, 7); |
| 8224 | QCBOREncode_AddInt64(&EC, 8); |
| 8225 | QCBOREncode_CloseArray(&EC); |
| 8226 | |
| 8227 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 8228 | if(uErr) { |
| 8229 | Encoded = NULLUsefulBufC; |
| 8230 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8231 | |
| 8232 | return Encoded; |
| 8233 | } |
| 8234 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8235 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8236 | static const uint8_t spBreakInByteString[] = { |
| 8237 | 0x41, 0xff |
| 8238 | }; |
| 8239 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8240 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8241 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8242 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8243 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8244 | |
| 8245 | QCBORDecodeContext DC; |
| 8246 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8247 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8248 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8249 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8250 | |
| 8251 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8252 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8253 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8254 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 8255 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8256 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8257 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8258 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8259 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8260 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8261 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8262 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8263 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8264 | QCBORDecode_ExitArray(&DC); |
| 8265 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8266 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8267 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8268 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8269 | QCBORDecode_GetInt64(&DC, &n7); |
| 8270 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8271 | QCBORDecode_ExitArray(&DC); |
| 8272 | |
| 8273 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8274 | if(uErr) { |
| 8275 | return (int32_t)uErr; |
| 8276 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8277 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8278 | |
| 8279 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 8280 | * Successful because no items are fetched from byte string. |
| 8281 | */ |
| 8282 | QCBORDecode_Init(&DC, |
| 8283 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8284 | 0); |
| 8285 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8286 | uErr = QCBORDecode_GetError(&DC); |
| 8287 | if(uErr) { |
| 8288 | return 100 + (int32_t)uErr; |
| 8289 | } |
| 8290 | |
| 8291 | QCBORDecode_ExitBstrWrapped(&DC); |
| 8292 | uErr = QCBORDecode_GetError(&DC); |
| 8293 | if(uErr) { |
| 8294 | return 200 + (int32_t)uErr; |
| 8295 | } |
| 8296 | |
| 8297 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 8298 | * It fails because there should be no break. |
| 8299 | */ |
| 8300 | QCBORDecode_Init(&DC, |
| 8301 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8302 | 0); |
| 8303 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8304 | QCBORItem Item; |
| 8305 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 8306 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 8307 | return 300 + (int32_t)uErr; |
| 8308 | } |
| 8309 | |
| 8310 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8311 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8312 | |
| 8313 | |
| 8314 | |
| 8315 | |
| 8316 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8317 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8318 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8319 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8320 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8321 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 8322 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 8323 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8324 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8325 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8326 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 8327 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 8328 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8329 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8330 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8331 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8332 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8333 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8334 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8335 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8336 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8337 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8338 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8339 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8340 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8341 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 8342 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8343 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8344 | 21, |
| 8345 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 8346 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 8347 | |
| 8348 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8349 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8350 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8351 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8352 | |
| 8353 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8354 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8355 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8356 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8357 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8358 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8359 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8360 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8361 | |
| 8362 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8363 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8364 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8365 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8366 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8367 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8368 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 8369 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8370 | |
| 8371 | 0x18, 0x33, |
| 8372 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 8373 | |
| 8374 | // MIME |
| 8375 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8376 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 8377 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 8378 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8379 | |
| 8380 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8381 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8382 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8383 | |
| 8384 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8385 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 8386 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 8387 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8388 | |
| 8389 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8390 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8391 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8392 | |
| 8393 | // UUID |
| 8394 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8395 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 8396 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8397 | |
| 8398 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8399 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 8400 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8401 | }; |
| 8402 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8403 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8404 | { |
| 8405 | QCBORDecodeContext DC; |
| 8406 | QCBORError uErr; |
| 8407 | |
| 8408 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 8409 | |
| 8410 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8411 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8412 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8413 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8414 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8415 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8416 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 8417 | return 1; |
| 8418 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8419 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8420 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8421 | return 2; |
| 8422 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8423 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8424 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8425 | return 3; |
| 8426 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8427 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8428 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8429 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8430 | return 4; |
| 8431 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8432 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8433 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8434 | return 5; |
| 8435 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8436 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8437 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8438 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8439 | bNeg != false) { |
| 8440 | return 10; |
| 8441 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8442 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8443 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8444 | bNeg != true) { |
| 8445 | return 11; |
| 8446 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8447 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8448 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8449 | return 12; |
| 8450 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8451 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8452 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8453 | return 13; |
| 8454 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8455 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8456 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8457 | return 14; |
| 8458 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8459 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8460 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8461 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8462 | return 20; |
| 8463 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8464 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8465 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8466 | return 21; |
| 8467 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8468 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8469 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8470 | return 22; |
| 8471 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8472 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8473 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8474 | return 23; |
| 8475 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8476 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8477 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8478 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8479 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8480 | return 30; |
| 8481 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8482 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8483 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8484 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8485 | return 31; |
| 8486 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8487 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8488 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8489 | return 32; |
| 8490 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8491 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8492 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8493 | return 33; |
| 8494 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8495 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8496 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8497 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8498 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8499 | return 40; |
| 8500 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8501 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8502 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8503 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8504 | return 41; |
| 8505 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8506 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8507 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8508 | return 42; |
| 8509 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8510 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8511 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8512 | return 43; |
| 8513 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8514 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8515 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8516 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8517 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8518 | return 50; |
| 8519 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8520 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8521 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8522 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8523 | return 51; |
| 8524 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8525 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8526 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8527 | return 52; |
| 8528 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8529 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8530 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8531 | return 53; |
| 8532 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8533 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8534 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8535 | // MIME |
| 8536 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8537 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8538 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8539 | bIsNot7Bit == true) { |
| 8540 | return 60; |
| 8541 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8542 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8543 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8544 | bIsNot7Bit == true) { |
| 8545 | return 61; |
| 8546 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8547 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8548 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8549 | bIsNot7Bit == false) { |
| 8550 | return 62; |
| 8551 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8552 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8553 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8554 | bIsNot7Bit == false) { |
| 8555 | return 63; |
| 8556 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8557 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8558 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8559 | return 64; |
| 8560 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8561 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8562 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8563 | return 65; |
| 8564 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8565 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8566 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8567 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8568 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8569 | return 70; |
| 8570 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8571 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 8572 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8573 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, 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 71; |
| 8576 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8577 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, 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 72; |
| 8580 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8581 | QCBORDecode_GetBinaryUUIDInMapSZ(&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 73; |
| 8584 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8585 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8586 | // Improvement: add some more error test cases |
| 8587 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8588 | QCBORDecode_ExitMap(&DC); |
| 8589 | |
| 8590 | uErr = QCBORDecode_Finish(&DC); |
| 8591 | if(uErr != QCBOR_SUCCESS) { |
| 8592 | return 100; |
| 8593 | } |
| 8594 | |
| 8595 | return 0; |
| 8596 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8597 | |
| 8598 | |
| 8599 | |
| 8600 | |
| 8601 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8602 | [ |
| 8603 | "aaaaaaaaaa", |
| 8604 | {} |
| 8605 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8606 | */ |
| 8607 | static const uint8_t spTooLarge1[] = { |
| 8608 | 0x9f, |
| 8609 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8610 | 0xa0, |
| 8611 | 0xff |
| 8612 | }; |
| 8613 | |
| 8614 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8615 | [ |
| 8616 | { |
| 8617 | 0: "aaaaaaaaaa" |
| 8618 | } |
| 8619 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8620 | */ |
| 8621 | static const uint8_t spTooLarge2[] = { |
| 8622 | 0x9f, |
| 8623 | 0xa1, |
| 8624 | 0x00, |
| 8625 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8626 | 0xff |
| 8627 | }; |
| 8628 | |
| 8629 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8630 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8631 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8632 | { |
| 8633 | 0: "aaaaaaaaaa" |
| 8634 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8635 | */ |
| 8636 | static const uint8_t spTooLarge3[] = { |
| 8637 | 0x4d, |
| 8638 | 0xa1, |
| 8639 | 0x00, |
| 8640 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8641 | }; |
| 8642 | |
| 8643 | int32_t TooLargeInputTest(void) |
| 8644 | { |
| 8645 | QCBORDecodeContext DC; |
| 8646 | QCBORError uErr; |
| 8647 | UsefulBufC String; |
| 8648 | |
| 8649 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 8650 | // to 10 There's not really any way to test this error |
| 8651 | // condition. The error condition is not complex, so setting |
| 8652 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 8653 | |
| 8654 | // The input CBOR is only too large because the |
| 8655 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 8656 | // |
| 8657 | // This test is disabled for the normal test runs because of the |
| 8658 | // special build requirement. |
| 8659 | |
| 8660 | |
| 8661 | // Tests the start of a map being too large |
| 8662 | 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] | 8663 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8664 | QCBORDecode_GetTextString(&DC, &String); |
| 8665 | uErr = QCBORDecode_GetError(&DC); |
| 8666 | if(uErr != QCBOR_SUCCESS) { |
| 8667 | return 1; |
| 8668 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8669 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8670 | uErr = QCBORDecode_GetError(&DC); |
| 8671 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8672 | return 2; |
| 8673 | } |
| 8674 | |
| 8675 | // Tests the end of a map being too large |
| 8676 | 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] | 8677 | QCBORDecode_EnterArray(&DC, NULL); |
| 8678 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8679 | uErr = QCBORDecode_GetError(&DC); |
| 8680 | if(uErr != QCBOR_SUCCESS) { |
| 8681 | return 3; |
| 8682 | } |
| 8683 | QCBORDecode_ExitMap(&DC); |
| 8684 | uErr = QCBORDecode_GetError(&DC); |
| 8685 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8686 | return 4; |
| 8687 | } |
| 8688 | |
| 8689 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 8690 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 8691 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8692 | uErr = QCBORDecode_GetError(&DC); |
| 8693 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8694 | return 5; |
| 8695 | } |
| 8696 | |
| 8697 | return 0; |
| 8698 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8699 | |
| 8700 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8701 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 8702 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8703 | /* |
| 8704 | An array of three map entries |
| 8705 | 1) Indefinite length string label for indefinite lenght byte string |
| 8706 | 2) Indefinite length string label for an integer |
| 8707 | 3) Indefinite length string label for an indefinite-length negative big num |
| 8708 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8709 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8710 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8711 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 8712 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 8713 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 8714 | 0x03, |
| 8715 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 8716 | 0xc3, |
| 8717 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8718 | }; |
| 8719 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8720 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8721 | { |
| 8722 | QCBORDecodeContext DCtx; |
| 8723 | |
| 8724 | QCBORDecode_Init(&DCtx, |
| 8725 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 8726 | QCBOR_DECODE_MODE_NORMAL); |
| 8727 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8728 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8729 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 8730 | |
| 8731 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8732 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8733 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8734 | |
| 8735 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8736 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8737 | return 1; |
| 8738 | } |
| 8739 | |
| 8740 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 8741 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 8742 | return 2; |
| 8743 | } |
| 8744 | |
| 8745 | uint64_t uInt; |
| 8746 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 8747 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8748 | return 3; |
| 8749 | } |
| 8750 | if(uInt != 3) { |
| 8751 | return 4; |
| 8752 | } |
| 8753 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8754 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8755 | double uDouble; |
| 8756 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 8757 | "label2", |
| 8758 | 0xff, |
| 8759 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8760 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8761 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8762 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8763 | return 5; |
| 8764 | } |
| 8765 | if(uDouble != -16777474) { |
| 8766 | return 6; |
| 8767 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8768 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8769 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 8770 | return 7; |
| 8771 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8772 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8773 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8774 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8775 | QCBORDecode_ExitMap(&DCtx); |
| 8776 | |
| 8777 | if(QCBORDecode_Finish(&DCtx)) { |
| 8778 | return 99; |
| 8779 | } |
| 8780 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8781 | #else /* QCBOR_DISABLE_TAGS */ |
| 8782 | /* The big num in the input is a CBOR tag and you can't do |
| 8783 | * map lookups in a map with a tag so this test does very little |
| 8784 | * when tags are disabled. That is OK, the test coverage is still |
| 8785 | * good when they are not. |
| 8786 | */ |
| 8787 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 8788 | return 1002; |
| 8789 | } |
| 8790 | #endif /*QCBOR_DISABLE_TAGS */ |
| 8791 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8792 | return 0; |
| 8793 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8794 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8795 | |
| 8796 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8797 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8798 | /* |
| 8799 | * An array of an integer and an array. The second array contains |
| 8800 | * a bstr-wrapped map. |
| 8801 | * |
| 8802 | * [7, [h'A36D6669... (see next lines) 73']] |
| 8803 | * |
| 8804 | * {"first integer": 42, |
| 8805 | * "an array of two strings": ["string1", "string2"], |
| 8806 | * "map in a map": |
| 8807 | * { "bytes 1": h'78787878', |
| 8808 | * "bytes 2": h'79797979', |
| 8809 | * "another int": 98, |
| 8810 | * "text 2": "lies, damn lies and statistics" |
| 8811 | * } |
| 8812 | * } |
| 8813 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8814 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8815 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 8816 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 8817 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8818 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8819 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8820 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8821 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8822 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8823 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8824 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8825 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8826 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8827 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8828 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8829 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8830 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8831 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8832 | 0x73 |
| 8833 | }; |
| 8834 | |
| 8835 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8836 | |
| 8837 | /* As above, but the arrays are indefinite length */ |
| 8838 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 8839 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 8840 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8841 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8842 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8843 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8844 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8845 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8846 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8847 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8848 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8849 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8850 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8851 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8852 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8853 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8854 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8855 | 0x73, |
| 8856 | 0xff, 0xff |
| 8857 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8858 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8859 | |
| 8860 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 8861 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8862 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8863 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8864 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 8865 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8866 | |
| 8867 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8868 | /* |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 8869 | * An array of one that contains a byte string that is an indefinite |
| 8870 | * length string that CBOR wraps an array of three numbers [42, 43, |
| 8871 | * 44]. The byte string is an implicit tag 24. |
| 8872 | * |
| 8873 | * [ |
| 8874 | * (_ h'83', h'18', h'2A182B', h'182C') |
| 8875 | * ] |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8876 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8877 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 8878 | 0x81, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8879 | 0x5f, |
| 8880 | 0x41, 0x83, |
| 8881 | 0x41, 0x18, |
| 8882 | 0x43, 0x2A, 0x18, 0x2B, |
| 8883 | 0x42, 0x18, 0x2C, |
| 8884 | 0xff |
| 8885 | }; |
| 8886 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8887 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8888 | #endif |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8889 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8890 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8891 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8892 | { |
| 8893 | QCBORItem Item; |
| 8894 | QCBORError nCBORError; |
| 8895 | QCBORDecodeContext DCtx; |
| 8896 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8897 | // Improvement: rework this test to use only integer labels. |
| 8898 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8899 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8900 | |
| 8901 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8902 | return 100+(int32_t)nCBORError; |
| 8903 | } |
| 8904 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8905 | return 200; |
| 8906 | } |
| 8907 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8908 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8909 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 8910 | return 150+(int32_t)nCBORError; |
| 8911 | } |
| 8912 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8913 | return 250; |
| 8914 | } |
| 8915 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8916 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8917 | return (int32_t)nCBORError; |
| 8918 | } |
| 8919 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8920 | return 300; |
| 8921 | } |
| 8922 | |
| 8923 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8924 | return 400 + (int32_t)nCBORError; |
| 8925 | } |
| 8926 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8927 | return 500; |
| 8928 | } |
| 8929 | |
| 8930 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8931 | return (int32_t)nCBORError; |
| 8932 | } |
| 8933 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8934 | return 600; |
| 8935 | } |
| 8936 | |
| 8937 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8938 | return 900 + (int32_t)nCBORError; |
| 8939 | } |
| 8940 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8941 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8942 | Item.val.int64 != 42 || |
| 8943 | Item.uDataAlloc || |
| 8944 | Item.uLabelAlloc || |
| 8945 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8946 | return 1000; |
| 8947 | } |
| 8948 | |
| 8949 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8950 | return 1100 + (int32_t)nCBORError; |
| 8951 | } |
| 8952 | |
| 8953 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8954 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8955 | Item.val.int64 != 42 || |
| 8956 | Item.uDataAlloc || |
| 8957 | Item.uLabelAlloc || |
| 8958 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8959 | return 1200; |
| 8960 | } |
| 8961 | |
| 8962 | |
| 8963 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8964 | return 1300 + (int32_t)nCBORError; |
| 8965 | } |
| 8966 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8967 | Item.uDataAlloc || |
| 8968 | Item.uLabelAlloc || |
| 8969 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8970 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8971 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8972 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8973 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8974 | |
| 8975 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8976 | return 1500 + (int32_t)nCBORError; |
| 8977 | } |
| 8978 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8979 | Item.uDataAlloc || |
| 8980 | Item.uLabelAlloc || |
| 8981 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8982 | return 1600; |
| 8983 | } |
| 8984 | |
| 8985 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8986 | return 1700 + (int32_t)nCBORError; |
| 8987 | } |
| 8988 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8989 | Item.uDataAlloc || |
| 8990 | Item.uLabelAlloc || |
| 8991 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8992 | return 1800; |
| 8993 | } |
| 8994 | |
| 8995 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8996 | return (int32_t)nCBORError; |
| 8997 | } |
| 8998 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8999 | Item.uDataAlloc || |
| 9000 | Item.uLabelAlloc || |
| 9001 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9002 | return 1900; |
| 9003 | } |
| 9004 | |
| 9005 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9006 | return (int32_t)nCBORError; |
| 9007 | } |
| 9008 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9009 | Item.uDataAlloc || |
| 9010 | Item.uLabelAlloc || |
| 9011 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9012 | return 2000; |
| 9013 | } |
| 9014 | |
| 9015 | |
| 9016 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9017 | return 2100 + (int32_t)nCBORError; |
| 9018 | } |
| 9019 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9020 | Item.uDataAlloc || |
| 9021 | Item.uLabelAlloc || |
| 9022 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 9023 | Item.uDataType != QCBOR_TYPE_MAP || |
| 9024 | Item.val.uCount != 4) { |
| 9025 | return 2100; |
| 9026 | } |
| 9027 | |
| 9028 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9029 | return 2200 + (int32_t)nCBORError; |
| 9030 | } |
| 9031 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9032 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 9033 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 9034 | Item.uDataAlloc || |
| 9035 | Item.uLabelAlloc || |
| 9036 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 9037 | return 2300; |
| 9038 | } |
| 9039 | |
| 9040 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 9041 | return 2400 + (int32_t)nCBORError; |
| 9042 | } |
| 9043 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9044 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 9045 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 9046 | Item.uDataAlloc || |
| 9047 | Item.uLabelAlloc || |
| 9048 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 9049 | return 2500; |
| 9050 | } |
| 9051 | |
| 9052 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9053 | return 2600 + (int32_t)nCBORError; |
| 9054 | } |
| 9055 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9056 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 9057 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 9058 | Item.uDataAlloc || |
| 9059 | Item.uLabelAlloc || |
| 9060 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 9061 | return 2700; |
| 9062 | } |
| 9063 | |
| 9064 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9065 | return 2800 + (int32_t)nCBORError; |
| 9066 | } |
| 9067 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9068 | Item.uDataAlloc || |
| 9069 | Item.uLabelAlloc || |
| 9070 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 9071 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9072 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9073 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9074 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9075 | |
| 9076 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 9077 | return 3000 + (int32_t)nCBORError; |
| 9078 | } |
| 9079 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9080 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 9081 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9082 | Item.uDataAlloc || |
| 9083 | Item.uLabelAlloc || |
| 9084 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 9085 | return 3100; |
| 9086 | } |
| 9087 | |
| 9088 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9089 | return 3200 + (int32_t)nCBORError; |
| 9090 | } |
| 9091 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9092 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 9093 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9094 | Item.uDataAlloc || |
| 9095 | Item.uLabelAlloc || |
| 9096 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 9097 | return 3300; |
| 9098 | } |
| 9099 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 9100 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 9101 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9102 | return 3300 + (int32_t)nCBORError; |
| 9103 | } |
| 9104 | |
| 9105 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 9106 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 9107 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9108 | return 3400 + (int32_t)nCBORError; |
| 9109 | } |
| 9110 | |
| 9111 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 9112 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 9113 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9114 | return 3500 + (int32_t)nCBORError; |
| 9115 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9116 | |
| 9117 | |
| 9118 | // Rewind to top level after entering several maps |
| 9119 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9120 | |
| 9121 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9122 | return (int32_t)nCBORError; |
| 9123 | } |
| 9124 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 9125 | Item.val.uCount != 3) { |
| 9126 | return 400; |
| 9127 | } |
| 9128 | |
| 9129 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9130 | return 4000+(int32_t)nCBORError; |
| 9131 | } |
| 9132 | |
| 9133 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9134 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9135 | Item.val.int64 != 42 || |
| 9136 | Item.uDataAlloc || |
| 9137 | Item.uLabelAlloc || |
| 9138 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9139 | return 4100; |
| 9140 | } |
| 9141 | |
| 9142 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9143 | return 4100+(int32_t)nCBORError; |
| 9144 | } |
| 9145 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9146 | Item.uDataAlloc || |
| 9147 | Item.uLabelAlloc || |
| 9148 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9149 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9150 | Item.val.uCount != 2) { |
| 9151 | return 4200; |
| 9152 | } |
| 9153 | |
| 9154 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9155 | return 4200+(int32_t)nCBORError; |
| 9156 | } |
| 9157 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9158 | Item.uDataAlloc || |
| 9159 | Item.uLabelAlloc || |
| 9160 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9161 | return 4300; |
| 9162 | } |
| 9163 | |
| 9164 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9165 | return 4300+(int32_t)nCBORError; |
| 9166 | } |
| 9167 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9168 | Item.uDataAlloc || |
| 9169 | Item.uLabelAlloc || |
| 9170 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9171 | return 4400; |
| 9172 | } |
| 9173 | |
| 9174 | QCBORDecode_Rewind(&DCtx); |
| 9175 | |
| 9176 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9177 | return 4400+(int32_t)nCBORError; |
| 9178 | } |
| 9179 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 9180 | Item.val.uCount != 3) { |
| 9181 | return 4500; |
| 9182 | } |
| 9183 | |
| 9184 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9185 | return (int32_t)nCBORError; |
| 9186 | } |
| 9187 | |
| 9188 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9189 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9190 | Item.val.int64 != 42 || |
| 9191 | Item.uDataAlloc || |
| 9192 | Item.uLabelAlloc || |
| 9193 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9194 | return 4600; |
| 9195 | } |
| 9196 | |
| 9197 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9198 | return (int32_t)nCBORError; |
| 9199 | } |
| 9200 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9201 | Item.uDataAlloc || |
| 9202 | Item.uLabelAlloc || |
| 9203 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9204 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9205 | Item.val.uCount != 2) { |
| 9206 | return 4700; |
| 9207 | } |
| 9208 | |
| 9209 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9210 | return (int32_t)nCBORError; |
| 9211 | } |
| 9212 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9213 | Item.uDataAlloc || |
| 9214 | Item.uLabelAlloc || |
| 9215 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9216 | return 4800; |
| 9217 | } |
| 9218 | |
| 9219 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9220 | return 4900+(int32_t)nCBORError; |
| 9221 | } |
| 9222 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9223 | Item.uDataAlloc || |
| 9224 | Item.uLabelAlloc || |
| 9225 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9226 | return 5000; |
| 9227 | } |
| 9228 | |
| 9229 | |
| 9230 | // Rewind an entered map |
| 9231 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9232 | |
| 9233 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9234 | |
| 9235 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9236 | return 5100+(int32_t)nCBORError; |
| 9237 | } |
| 9238 | |
| 9239 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9240 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9241 | Item.val.int64 != 42 || |
| 9242 | Item.uDataAlloc || |
| 9243 | Item.uLabelAlloc || |
| 9244 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9245 | return 5200; |
| 9246 | } |
| 9247 | |
| 9248 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9249 | return 5200+(int32_t)nCBORError; |
| 9250 | } |
| 9251 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9252 | Item.uDataAlloc || |
| 9253 | Item.uLabelAlloc || |
| 9254 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9255 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9256 | Item.val.uCount != 2) { |
| 9257 | return -5300; |
| 9258 | } |
| 9259 | |
| 9260 | QCBORDecode_Rewind(&DCtx); |
| 9261 | |
| 9262 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9263 | return 5300+(int32_t)nCBORError; |
| 9264 | } |
| 9265 | |
| 9266 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9267 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9268 | Item.val.int64 != 42 || |
| 9269 | Item.uDataAlloc || |
| 9270 | Item.uLabelAlloc || |
| 9271 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9272 | return 5400; |
| 9273 | } |
| 9274 | |
| 9275 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9276 | return 5400+(int32_t)nCBORError; |
| 9277 | } |
| 9278 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9279 | Item.uDataAlloc || |
| 9280 | Item.uLabelAlloc || |
| 9281 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9282 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9283 | Item.val.uCount != 2) { |
| 9284 | return 5500; |
| 9285 | } |
| 9286 | |
| 9287 | |
| 9288 | // Rewind and entered array inside an entered map |
| 9289 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9290 | |
| 9291 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9292 | |
| 9293 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 9294 | |
| 9295 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9296 | return 5600+(int32_t)nCBORError; |
| 9297 | } |
| 9298 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9299 | Item.uDataAlloc || |
| 9300 | Item.uLabelAlloc || |
| 9301 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9302 | return 5700; |
| 9303 | } |
| 9304 | |
| 9305 | QCBORDecode_Rewind(&DCtx); |
| 9306 | |
| 9307 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9308 | return 5700+(int32_t)nCBORError; |
| 9309 | } |
| 9310 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9311 | Item.uDataAlloc || |
| 9312 | Item.uLabelAlloc || |
| 9313 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9314 | return 5800; |
| 9315 | } |
| 9316 | |
| 9317 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9318 | return (int32_t)nCBORError; |
| 9319 | } |
| 9320 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9321 | Item.uDataAlloc || |
| 9322 | Item.uLabelAlloc || |
| 9323 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9324 | return 5900; |
| 9325 | } |
| 9326 | |
| 9327 | QCBORDecode_Rewind(&DCtx); |
| 9328 | |
| 9329 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9330 | return 5900+(int32_t)nCBORError; |
| 9331 | } |
| 9332 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9333 | Item.uDataAlloc || |
| 9334 | Item.uLabelAlloc || |
| 9335 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9336 | return 6000; |
| 9337 | } |
| 9338 | |
| 9339 | |
| 9340 | // Rewind a byte string inside an array inside an array |
| 9341 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 9342 | |
| 9343 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9344 | |
| 9345 | uint64_t i; |
| 9346 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9347 | |
| 9348 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9349 | |
| 9350 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9351 | if(QCBORDecode_GetError(&DCtx)) { |
| 9352 | return 6100; |
| 9353 | } |
| 9354 | |
| 9355 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9356 | return (int32_t)nCBORError; |
| 9357 | } |
| 9358 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9359 | return 6200; |
| 9360 | } |
| 9361 | |
| 9362 | QCBORDecode_Rewind(&DCtx); |
| 9363 | |
| 9364 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9365 | return 6300+(int32_t)nCBORError; |
| 9366 | } |
| 9367 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9368 | return 6400; |
| 9369 | } |
| 9370 | |
| 9371 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9372 | // Rewind a byte string inside an indefinite-length array inside |
| 9373 | // indefinite-length array |
| 9374 | |
| 9375 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 9376 | |
| 9377 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9378 | |
| 9379 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9380 | |
| 9381 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9382 | |
| 9383 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9384 | if(QCBORDecode_GetError(&DCtx)) { |
| 9385 | return 6500; |
| 9386 | } |
| 9387 | |
| 9388 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9389 | return 6600+(int32_t)nCBORError; |
| 9390 | } |
| 9391 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9392 | return 6700; |
| 9393 | } |
| 9394 | |
| 9395 | QCBORDecode_Rewind(&DCtx); |
| 9396 | |
| 9397 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9398 | return 6800+(int32_t)nCBORError; |
| 9399 | } |
| 9400 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9401 | return 6900; |
| 9402 | } |
| 9403 | #endif |
| 9404 | |
| 9405 | // Rewind an empty map |
| 9406 | // [100, {}] |
| 9407 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 9408 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9409 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9410 | if(i != 100) { |
| 9411 | return 7010; |
| 9412 | } |
| 9413 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9414 | |
| 9415 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9416 | for(int n = 0; n < 5; n++) { |
| 9417 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9418 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9419 | return 7000 + n; |
| 9420 | } |
| 9421 | QCBORDecode_Rewind(&DCtx); |
| 9422 | } |
| 9423 | QCBORDecode_ExitMap(&DCtx); |
| 9424 | QCBORDecode_Rewind(&DCtx); |
| 9425 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9426 | if(i != 100) { |
| 9427 | return 7010; |
| 9428 | } |
| 9429 | QCBORDecode_ExitArray(&DCtx); |
| 9430 | QCBORDecode_Rewind(&DCtx); |
| 9431 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9432 | i = 9; |
| 9433 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9434 | if(i != 100) { |
| 9435 | return 7020; |
| 9436 | } |
| 9437 | if(QCBORDecode_GetError(&DCtx)){ |
| 9438 | return 7030; |
| 9439 | } |
| 9440 | |
| 9441 | // Rewind an empty indefinite length map |
| 9442 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9443 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 9444 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9445 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9446 | if(i != 100) { |
| 9447 | return 7810; |
| 9448 | } |
| 9449 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9450 | |
| 9451 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9452 | for(int n = 0; n < 5; n++) { |
| 9453 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9454 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9455 | return 7800 + n; |
| 9456 | } |
| 9457 | QCBORDecode_Rewind(&DCtx); |
| 9458 | } |
| 9459 | QCBORDecode_ExitMap(&DCtx); |
| 9460 | QCBORDecode_Rewind(&DCtx); |
| 9461 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9462 | if(i != 100) { |
| 9463 | return 7810; |
| 9464 | } |
| 9465 | QCBORDecode_ExitArray(&DCtx); |
| 9466 | QCBORDecode_Rewind(&DCtx); |
| 9467 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9468 | i = 9; |
| 9469 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9470 | if(i != 100) { |
| 9471 | return 7820; |
| 9472 | } |
| 9473 | if(QCBORDecode_GetError(&DCtx)){ |
| 9474 | return 7830; |
| 9475 | } |
| 9476 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9477 | |
| 9478 | // Rewind an indefnite length byte-string wrapped sequence |
| 9479 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 9480 | QCBORDecode_Init(&DCtx, |
| 9481 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 9482 | 0); |
| 9483 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 9484 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 9485 | |
| 9486 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9487 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9488 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9489 | return 7300; |
| 9490 | } |
| 9491 | |
| 9492 | /* |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9493 | Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on |
| 9494 | allocated strings. This is a fairly big job because of all the |
| 9495 | UsefulBuf internal book keeping that needs tweaking. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9496 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9497 | if(i != 42) { |
| 9498 | return 7110; |
| 9499 | } |
| 9500 | QCBORDecode_Rewind(&DCtx); |
| 9501 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9502 | if(i != 42) { |
| 9503 | return 7220; |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9504 | } |
| 9505 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 9506 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9507 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9508 | |
| 9509 | |
| 9510 | // Rewind an indefnite length byte-string wrapped sequence |
| 9511 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9512 | return 0; |
| 9513 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9514 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9515 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9516 | |
| 9517 | |
| 9518 | static const uint8_t spBooleansInMap[] = |
| 9519 | { |
| 9520 | 0xa1, 0x08, 0xf5 |
| 9521 | }; |
| 9522 | |
| 9523 | static const uint8_t spBooleansInMapWrongType[] = |
| 9524 | { |
| 9525 | 0xa1, 0x08, 0xf6 |
| 9526 | }; |
| 9527 | |
| 9528 | static const uint8_t spBooleansInMapNWF[] = |
| 9529 | { |
| 9530 | 0xa1, 0x08, 0x1a |
| 9531 | }; |
| 9532 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9533 | static const uint8_t spNullInMap[] = |
| 9534 | { |
| 9535 | 0xa1, 0x08, 0xf6 |
| 9536 | }; |
| 9537 | |
| 9538 | static const uint8_t spUndefinedInMap[] = |
| 9539 | { |
| 9540 | 0xa1, 0x08, 0xf7 |
| 9541 | }; |
| 9542 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9543 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 9544 | #ifndef QCBOR_DISABLE_TAGS |
| 9545 | static const uint8_t spTaggedSimples[] = |
| 9546 | { |
| 9547 | 0xd8, 0x58, 0xd8, 0x2c, 0xd6, 0xf5, |
| 9548 | 0xd9, 0x0f, 0xA0, 0xf7 |
| 9549 | }; |
| 9550 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 9551 | |
| 9552 | |
| 9553 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9554 | int32_t BoolTest(void) |
| 9555 | { |
| 9556 | QCBORDecodeContext DCtx; |
| 9557 | bool b; |
| 9558 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9559 | QCBORDecode_Init(&DCtx, |
| 9560 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9561 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9562 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9563 | QCBORDecode_GetBool(&DCtx, &b); |
| 9564 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9565 | return 1; |
| 9566 | } |
| 9567 | |
| 9568 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 9569 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9570 | return 2; |
| 9571 | } |
| 9572 | |
| 9573 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 9574 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9575 | return 3; |
| 9576 | } |
| 9577 | |
| 9578 | |
| 9579 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 9580 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9581 | return 4; |
| 9582 | } |
| 9583 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9584 | QCBORDecode_Init(&DCtx, |
| 9585 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 9586 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9587 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9588 | QCBORDecode_GetBool(&DCtx, &b); |
| 9589 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9590 | return 5; |
| 9591 | } |
| 9592 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9593 | QCBORDecode_Init(&DCtx, |
| 9594 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9595 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9596 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9597 | QCBORDecode_GetBool(&DCtx, &b); |
| 9598 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9599 | return 6; |
| 9600 | } |
| 9601 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9602 | |
| 9603 | QCBORDecode_Init(&DCtx, |
| 9604 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9605 | 0); |
| 9606 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9607 | QCBORDecode_GetNull(&DCtx); |
| 9608 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9609 | return 7; |
| 9610 | } |
| 9611 | |
| 9612 | QCBORDecode_Init(&DCtx, |
| 9613 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9614 | 0); |
| 9615 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9616 | QCBORDecode_GetNull(&DCtx); |
| 9617 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9618 | return 8; |
| 9619 | } |
| 9620 | |
| 9621 | QCBORDecode_Init(&DCtx, |
| 9622 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9623 | 0); |
| 9624 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9625 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9626 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9627 | return 9; |
| 9628 | } |
| 9629 | |
| 9630 | QCBORDecode_Init(&DCtx, |
| 9631 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9632 | 0); |
| 9633 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9634 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9635 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9636 | return 10; |
| 9637 | } |
| 9638 | |
| 9639 | QCBORDecode_Init(&DCtx, |
| 9640 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9641 | 0); |
| 9642 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9643 | QCBORDecode_GetUndefined(&DCtx); |
| 9644 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9645 | return 11; |
| 9646 | } |
| 9647 | |
| 9648 | QCBORDecode_Init(&DCtx, |
| 9649 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9650 | 0); |
| 9651 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9652 | QCBORDecode_GetUndefined(&DCtx); |
| 9653 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9654 | return 12; |
| 9655 | } |
| 9656 | |
| 9657 | QCBORDecode_Init(&DCtx, |
| 9658 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9659 | 0); |
| 9660 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9661 | QCBORDecode_GetUndefined(&DCtx); |
| 9662 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9663 | return 13; |
| 9664 | } |
| 9665 | |
| 9666 | QCBORDecode_Init(&DCtx, |
| 9667 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9668 | 0); |
| 9669 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9670 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9671 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9672 | return 14; |
| 9673 | } |
| 9674 | |
| 9675 | QCBORDecode_Init(&DCtx, |
| 9676 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9677 | 0); |
| 9678 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9679 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9680 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9681 | return 15; |
| 9682 | } |
| 9683 | |
| 9684 | QCBORDecode_Init(&DCtx, |
| 9685 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9686 | 0); |
| 9687 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9688 | QCBORDecode_GetUndefined(&DCtx); |
| 9689 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9690 | return 15; |
| 9691 | } |
| 9692 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 9693 | #ifndef QCBOR_DISABLE_TAGS |
| 9694 | QCBORDecode_Init(&DCtx, |
| 9695 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedSimples), |
| 9696 | 0); |
| 9697 | QCBORDecode_GetBool(&DCtx, &b); |
| 9698 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 22) { |
| 9699 | return 401; |
| 9700 | } |
| 9701 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 44) { |
| 9702 | return 402; |
| 9703 | } |
| 9704 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 88) { |
| 9705 | return 403; |
| 9706 | } |
| 9707 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) { |
| 9708 | return 404; |
| 9709 | } |
| 9710 | QCBORDecode_GetUndefined(&DCtx); |
| 9711 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 4000) { |
| 9712 | return 405; |
| 9713 | } |
| 9714 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != CBOR_TAG_INVALID64) { |
| 9715 | return 406; |
| 9716 | } |
| 9717 | QCBORDecode_GetNull(&DCtx); /* Off the end */ |
| 9718 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 9719 | return 407; |
| 9720 | } |
| 9721 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 9722 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9723 | return 0; |
| 9724 | } |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9725 | |
| 9726 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9727 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9728 | static const uint8_t spExpectedArray2s[] = { |
| 9729 | 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9730 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9731 | 0x32}; |
| 9732 | |
| 9733 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9734 | static const uint8_t spExpectedArray2sIndef[] = { |
| 9735 | 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9736 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9737 | 0x32, 0xff}; |
| 9738 | #endif |
| 9739 | |
| 9740 | static const uint8_t spExpectedMap4[] = { |
| 9741 | 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9742 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9743 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9744 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9745 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9746 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9747 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9748 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9749 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9750 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73}; |
| 9751 | |
| 9752 | |
| 9753 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9754 | |
| 9755 | static const uint8_t spExpectedMap4Indef[] = { |
| 9756 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9757 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9758 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9759 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9760 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9761 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9762 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9763 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9764 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9765 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 9766 | 0xff}; |
| 9767 | |
| 9768 | /* |
| 9769 | * [[[[[0, []]]]], 0] |
| 9770 | */ |
| 9771 | static const uint8_t spDefAndIndef[] = { |
| 9772 | 0x82, |
| 9773 | 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00 |
| 9774 | }; |
| 9775 | #endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9776 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9777 | #ifndef QCBOR_DISABLE_TAGS |
| 9778 | /* An exp / mant tag in two nested arrays */ |
| 9779 | static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03}; |
| 9780 | #endif /* !QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9781 | #endif |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9782 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9783 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9784 | int32_t GetMapAndArrayTest(void) |
| 9785 | { |
| 9786 | QCBORDecodeContext DCtx; |
| 9787 | size_t uPosition ; |
| 9788 | QCBORItem Item; |
| 9789 | UsefulBufC ReturnedEncodedCBOR; |
| 9790 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9791 | // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9792 | QCBORDecode_Init(&DCtx, |
| 9793 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9794 | 0); |
| 9795 | |
| 9796 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9797 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9798 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9799 | if(QCBORDecode_GetError(&DCtx)) { |
| 9800 | return 1; |
| 9801 | } |
| 9802 | if(Item.val.uCount != 2) { |
| 9803 | return 2; |
| 9804 | } |
| 9805 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9806 | return 3; |
| 9807 | } |
| 9808 | |
| 9809 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9810 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9811 | return 4; |
| 9812 | } |
| 9813 | |
| 9814 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9815 | |
| 9816 | |
| 9817 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9818 | if(QCBORDecode_GetError(&DCtx)) { |
| 9819 | return 10; |
| 9820 | } |
| 9821 | if(Item.val.uCount != 4) { |
| 9822 | return 11; |
| 9823 | } |
| 9824 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9825 | return 12; |
| 9826 | } |
| 9827 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9828 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 9829 | "an array of two strings", |
| 9830 | &Item, |
| 9831 | &ReturnedEncodedCBOR); |
| 9832 | if(QCBORDecode_GetError(&DCtx)) { |
| 9833 | return 20; |
| 9834 | } |
| 9835 | if(Item.val.uCount != 2) { |
| 9836 | return 21; |
| 9837 | } |
| 9838 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9839 | return 22; |
| 9840 | } |
| 9841 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9842 | return 23; |
| 9843 | } |
| 9844 | |
| 9845 | QCBORDecode_Rewind(&DCtx); |
| 9846 | |
| 9847 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9848 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9849 | if(QCBORDecode_GetError(&DCtx)) { |
| 9850 | return 30; |
| 9851 | } |
| 9852 | if(Item.val.uCount != 4) { |
| 9853 | return 31; |
| 9854 | } |
| 9855 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9856 | return 32; |
| 9857 | } |
| 9858 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9859 | return 33; |
| 9860 | } |
| 9861 | |
| 9862 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9863 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9864 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9865 | return 40; |
| 9866 | } |
| 9867 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 9868 | return 41; |
| 9869 | } |
| 9870 | QCBORDecode_GetAndResetError(&DCtx); |
| 9871 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9872 | return 42; |
| 9873 | } |
| 9874 | |
| 9875 | |
| 9876 | #ifndef QCBOR_DISABLE_TAGS |
| 9877 | UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant); |
| 9878 | QCBORDecode_Init(&DCtx, ExpMant, 0); |
| 9879 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9880 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9881 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9882 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9883 | return 200; |
| 9884 | } |
| 9885 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 9886 | return 201; |
| 9887 | } |
| 9888 | if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) { |
| 9889 | return 202; |
| 9890 | } |
| 9891 | if(Item.val.uCount != 2) { |
| 9892 | return 201; |
| 9893 | } |
| 9894 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) { |
| 9895 | return 205; |
| 9896 | } |
| 9897 | #endif /* !QCBOR_DISABLE_TAGS */ |
| 9898 | |
| 9899 | |
| 9900 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9901 | |
| 9902 | UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef); |
| 9903 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9904 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9905 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9906 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9907 | return 50; |
| 9908 | } |
| 9909 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) { |
| 9910 | return 51; |
| 9911 | } |
| 9912 | |
| 9913 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9914 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9915 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9916 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9917 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9918 | return 52; |
| 9919 | } |
| 9920 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) { |
| 9921 | return 53; |
| 9922 | } |
| 9923 | |
| 9924 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9925 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9926 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9927 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9928 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9929 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9930 | return 54; |
| 9931 | } |
| 9932 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) { |
| 9933 | return 55; |
| 9934 | } |
| 9935 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9936 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9937 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9938 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9939 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9940 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9941 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9942 | return 56; |
| 9943 | } |
| 9944 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) { |
| 9945 | return 57; |
| 9946 | } |
| 9947 | |
| 9948 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9949 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9950 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9951 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9952 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9953 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9954 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9955 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9956 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9957 | return 58; |
| 9958 | } |
| 9959 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) { |
| 9960 | return 59; |
| 9961 | } |
| 9962 | |
| 9963 | |
| 9964 | QCBORDecode_Init(&DCtx, |
| 9965 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 9966 | 0); |
| 9967 | |
| 9968 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9969 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9970 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9971 | if(QCBORDecode_GetError(&DCtx)) { |
| 9972 | return 60; |
| 9973 | } |
| 9974 | if(Item.val.uCount != UINT16_MAX) { |
| 9975 | return 61; |
| 9976 | } |
| 9977 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 9978 | return 62; |
| 9979 | } |
| 9980 | |
| 9981 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9982 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9983 | return 63; |
| 9984 | } |
| 9985 | |
| 9986 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9987 | |
| 9988 | |
| 9989 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9990 | if(QCBORDecode_GetError(&DCtx)) { |
| 9991 | return 70; |
| 9992 | } |
| 9993 | if(Item.val.uCount != UINT16_MAX) { |
| 9994 | return 71; |
| 9995 | } |
| 9996 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 9997 | return 72; |
| 9998 | } |
| 9999 | |
| 10000 | |
| 10001 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10002 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 10003 | "an array of two strings", |
| 10004 | &Item, |
| 10005 | &ReturnedEncodedCBOR); |
| 10006 | if(QCBORDecode_GetError(&DCtx)) { |
| 10007 | return 80; |
| 10008 | } |
| 10009 | if(Item.val.uCount != UINT16_MAX) { |
| 10010 | return 81; |
| 10011 | } |
| 10012 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 10013 | return 82; |
| 10014 | } |
| 10015 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10016 | return 83; |
| 10017 | } |
| 10018 | |
| 10019 | QCBORDecode_Rewind(&DCtx); |
| 10020 | |
| 10021 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10022 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 10023 | if(QCBORDecode_GetError(&DCtx)) { |
| 10024 | return 90; |
| 10025 | } |
| 10026 | if(Item.val.uCount != UINT16_MAX) { |
| 10027 | return 91; |
| 10028 | } |
| 10029 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 10030 | return 92; |
| 10031 | } |
| 10032 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10033 | return 93; |
| 10034 | } |
| 10035 | |
| 10036 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10037 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 10038 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10039 | return 100; |
| 10040 | } |
| 10041 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 10042 | return 101; |
| 10043 | } |
| 10044 | QCBORDecode_GetAndResetError(&DCtx); |
| 10045 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 10046 | return 102; |
| 10047 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10048 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 10049 | |
| 10050 | return 0; |
| 10051 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10052 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 10053 | |
| 10054 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 10055 | int32_t |
| 10056 | ErrorHandlingTests(void) |
| 10057 | { |
| 10058 | QCBORDecodeContext DCtx; |
| 10059 | QCBORItem Item; |
| 10060 | QCBORError uError; |
| 10061 | int64_t integer; |
| 10062 | |
| 10063 | /* Test QCBORDecode_SetError() */ |
| 10064 | QCBORDecode_Init(&DCtx, |
| 10065 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10066 | QCBOR_DECODE_MODE_NORMAL); |
| 10067 | |
| 10068 | QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED); |
| 10069 | |
| 10070 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10071 | |
| 10072 | uError = QCBORDecode_GetError(&DCtx); |
| 10073 | |
| 10074 | if(uError != QCBOR_ERR_FIRST_USER_DEFINED) { |
| 10075 | return -1; |
| 10076 | } |
| 10077 | |
| 10078 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 10079 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 10080 | return -2; |
| 10081 | } |
| 10082 | |
| 10083 | |
| 10084 | /* Test data type returned from previous error */ |
| 10085 | QCBORDecode_Init(&DCtx, |
| 10086 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10087 | QCBOR_DECODE_MODE_NORMAL); |
| 10088 | QCBORDecode_GetInt64(&DCtx, &integer); |
| 10089 | uError = QCBORDecode_GetError(&DCtx); |
| 10090 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10091 | return -3; |
| 10092 | } |
| 10093 | |
| 10094 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10095 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 10096 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 10097 | return -2; |
| 10098 | } |
| 10099 | uError = QCBORDecode_GetError(&DCtx); |
| 10100 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 10101 | return -3; |
| 10102 | } |
| 10103 | |
| 10104 | |
| 10105 | /* Test error classification functions */ |
| 10106 | |
| 10107 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 10108 | return -10; |
| 10109 | } |
| 10110 | if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) { |
| 10111 | return -11; |
| 10112 | } |
| 10113 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 10114 | return -12; |
| 10115 | } |
| 10116 | if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) { |
| 10117 | return -13; |
| 10118 | } |
| 10119 | |
| 10120 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) { |
| 10121 | return -20; |
| 10122 | } |
| 10123 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) { |
| 10124 | return -21; |
| 10125 | } |
| 10126 | if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) { |
| 10127 | return -22; |
| 10128 | } |
| 10129 | if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) { |
| 10130 | return -23; |
| 10131 | } |
| 10132 | |
Laurence Lundblade | cd34e58 | 2024-06-06 10:55:14 -0700 | [diff] [blame] | 10133 | /* Test error strings */ |
| 10134 | const char *szErrString; |
| 10135 | |
| 10136 | szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG); |
| 10137 | if(szErrString == NULL) { |
| 10138 | return -100; |
| 10139 | } |
| 10140 | if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) { |
| 10141 | return -101; |
| 10142 | } |
| 10143 | |
| 10144 | szErrString = qcbor_err_to_str(QCBOR_SUCCESS); |
| 10145 | if(szErrString == NULL) { |
| 10146 | return -102; |
| 10147 | } |
| 10148 | if(strcmp(szErrString, "QCBOR_SUCCESS")) { |
| 10149 | return -103; |
| 10150 | } |
| 10151 | |
| 10152 | szErrString = qcbor_err_to_str(100); |
| 10153 | if(szErrString == NULL) { |
| 10154 | return -104; |
| 10155 | } |
| 10156 | if(strcmp(szErrString, "Unidentified QCBOR error")) { |
| 10157 | return -105; |
| 10158 | } |
| 10159 | |
| 10160 | szErrString = qcbor_err_to_str(200); |
| 10161 | if(szErrString == NULL) { |
| 10162 | return -106; |
| 10163 | } |
| 10164 | if(strcmp(szErrString, "USER_DEFINED_200")) { |
| 10165 | return -107; |
| 10166 | } |
| 10167 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 10168 | return 0; |
| 10169 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10170 | |
| 10171 | |
| 10172 | int32_t TellTests(void) |
| 10173 | { |
| 10174 | QCBORDecodeContext DCtx; |
| 10175 | QCBORItem Item; |
| 10176 | uint32_t uPosition; |
| 10177 | int nIndex; |
| 10178 | int64_t nDecodedInt; |
| 10179 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 10180 | // Improvement: rewrite so this can run with only integer labels |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10181 | static const uint32_t aPos[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10182 | {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, 151}; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10183 | QCBORDecode_Init(&DCtx, |
| 10184 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10185 | 0); |
| 10186 | for(nIndex = 0; ; nIndex++) { |
| 10187 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10188 | if(uPosition != aPos[nIndex]) { |
| 10189 | return nIndex; |
| 10190 | } |
| 10191 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10192 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10193 | break; |
| 10194 | } |
| 10195 | |
| 10196 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10197 | } |
| 10198 | |
| 10199 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10200 | static const uint32_t aPosIndef[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10201 | {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, 154}; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10202 | QCBORDecode_Init(&DCtx, |
| 10203 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10204 | 0); |
| 10205 | |
| 10206 | for(nIndex = 0; ; nIndex++) { |
| 10207 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10208 | if(uPosition != aPosIndef[nIndex]) { |
| 10209 | return nIndex + 100; |
| 10210 | } |
| 10211 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10212 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10213 | break; |
| 10214 | } |
| 10215 | |
| 10216 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10217 | } |
| 10218 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10219 | |
| 10220 | /* Next, some tests with entered maps and arrays */ |
| 10221 | QCBORDecode_Init(&DCtx, |
| 10222 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10223 | 0); |
| 10224 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10225 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10226 | return 1001; |
| 10227 | } |
| 10228 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10229 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10230 | return 1002; |
| 10231 | } |
| 10232 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10233 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10234 | return 1003; |
| 10235 | } |
| 10236 | |
| 10237 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10238 | if(nDecodedInt != 98) { |
| 10239 | return 1004; |
| 10240 | } |
| 10241 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10242 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10243 | return 1005; |
| 10244 | } |
| 10245 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10246 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10247 | return 1006; |
| 10248 | } |
| 10249 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10250 | if(nDecodedInt != 98) { |
| 10251 | return 1007; |
| 10252 | } |
| 10253 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10254 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10255 | return 1008; |
| 10256 | } |
| 10257 | |
| 10258 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10259 | if(QCBORDecode_Tell(&DCtx) != 151) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10260 | return 1009; |
| 10261 | } |
| 10262 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10263 | return 1010; |
| 10264 | } |
| 10265 | |
| 10266 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10267 | /* Next, some tests with entered maps and arrays */ |
| 10268 | QCBORDecode_Init(&DCtx, |
| 10269 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10270 | 0); |
| 10271 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10272 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10273 | return 2000; |
| 10274 | } |
| 10275 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10276 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10277 | return 2001; |
| 10278 | } |
| 10279 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10280 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10281 | return 2002; |
| 10282 | } |
| 10283 | |
| 10284 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10285 | if(nDecodedInt != 98) { |
| 10286 | return 2003; |
| 10287 | } |
| 10288 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10289 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10290 | return 2004; |
| 10291 | } |
| 10292 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10293 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10294 | return 2005; |
| 10295 | } |
| 10296 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10297 | if(nDecodedInt != 98) { |
| 10298 | return 2006; |
| 10299 | } |
| 10300 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10301 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10302 | return 2007; |
| 10303 | } |
| 10304 | |
| 10305 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10306 | if(QCBORDecode_Tell(&DCtx) != 154) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10307 | return 2008; |
| 10308 | } |
| 10309 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10310 | return 2010; |
| 10311 | } |
| 10312 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10313 | |
| 10314 | |
| 10315 | |
| 10316 | /* Error state test */ |
| 10317 | QCBORDecode_Init(&DCtx, |
| 10318 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10319 | 0); |
| 10320 | /* Cause an error */ |
| 10321 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10322 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10323 | return 3000; |
| 10324 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10325 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_MAP_NOT_ENTERED) { |
| 10326 | return 3001; |
| 10327 | } |
| 10328 | |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10329 | |
| 10330 | /* Empties tests */ |
| 10331 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 10332 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 10333 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10334 | return 4000; |
| 10335 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10336 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_SUCCESS) { |
| 10337 | return 4008; |
| 10338 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10339 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10340 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10341 | return 4001; |
| 10342 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10343 | if(QCBORDecode_Tell(&DCtx) != 1) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10344 | return 4002; |
| 10345 | } |
| 10346 | QCBORDecode_ExitMap(&DCtx); |
| 10347 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10348 | return 4003; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10349 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10350 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10351 | return 4004; |
| 10352 | } |
| 10353 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10354 | return 4005; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10355 | } |
| 10356 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10357 | return 4010; |
| 10358 | } |
| 10359 | |
| 10360 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10361 | const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map |
| 10362 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0); |
| 10363 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10364 | return 4100; |
| 10365 | } |
| 10366 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10367 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10368 | return 4101; |
| 10369 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10370 | if(QCBORDecode_Tell(&DCtx) != 2) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10371 | return 4102; |
| 10372 | } |
| 10373 | QCBORDecode_ExitMap(&DCtx); |
| 10374 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10375 | return 4103; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10376 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10377 | if(QCBORDecode_Tell(&DCtx) != 2) { |
| 10378 | return 4104; |
| 10379 | } |
| 10380 | if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10381 | return 4005; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10382 | } |
| 10383 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10384 | return 4110; |
| 10385 | } |
| 10386 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10387 | |
| 10388 | /* Test on a CBOR sequence */ |
| 10389 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0); |
| 10390 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10391 | return 5000; |
| 10392 | } |
| 10393 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10394 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10395 | return 5001; |
| 10396 | } |
| 10397 | if(QCBORDecode_Tell(&DCtx) != 11) { |
| 10398 | return 5002; |
| 10399 | } |
| 10400 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10401 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10402 | return 5003; |
| 10403 | } |
| 10404 | if(QCBORDecode_Tell(&DCtx) != 12) { |
| 10405 | return 5004; |
| 10406 | } |
| 10407 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10408 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10409 | return 5005; |
| 10410 | } |
| 10411 | if(QCBORDecode_Tell(&DCtx) != 17) { |
| 10412 | return 5006; |
| 10413 | } |
| 10414 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10415 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10416 | return 5007; |
| 10417 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10418 | if(QCBORDecode_Tell(&DCtx) != 20) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10419 | return 5008; |
| 10420 | } |
| 10421 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10422 | return 5010; |
| 10423 | } |
| 10424 | |
| 10425 | |
| 10426 | QCBORDecode_Init(&DCtx, |
| 10427 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10428 | 0); |
| 10429 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10430 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 10431 | if(QCBORDecode_Tell(&DCtx) != 42) { |
| 10432 | return 6001; |
| 10433 | } |
| 10434 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10435 | if(QCBORDecode_Tell(&DCtx) != 50) { |
| 10436 | return 6002; |
| 10437 | } |
| 10438 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10439 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10440 | return 6008; |
| 10441 | } |
| 10442 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10443 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 10444 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10445 | return 6003; |
| 10446 | } |
| 10447 | QCBORDecode_ExitArray(&DCtx); |
| 10448 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10449 | return 6004; |
| 10450 | } |
| 10451 | |
| 10452 | static const uint32_t aEmptiesPos[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10453 | {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] | 10454 | QCBORDecode_Init(&DCtx, |
| 10455 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 10456 | 0); |
| 10457 | for(nIndex = 0; ; nIndex++) { |
| 10458 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10459 | if(uPosition != aEmptiesPos[nIndex]) { |
| 10460 | return nIndex + 200; |
| 10461 | } |
| 10462 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10463 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10464 | break; |
| 10465 | } |
| 10466 | |
| 10467 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10468 | } |
| 10469 | |
| 10470 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10471 | static const uint32_t aIndefEmptiesPos[] = |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10472 | {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] | 10473 | QCBORDecode_Init(&DCtx, |
| 10474 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 10475 | 0); |
| 10476 | for(nIndex = 0; ; nIndex++) { |
| 10477 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10478 | if(uPosition != aIndefEmptiesPos[nIndex]) { |
| 10479 | return nIndex + 300; |
| 10480 | } |
| 10481 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 10482 | if(QCBORDecode_EndCheck(&DCtx)) { |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 10483 | break; |
| 10484 | } |
| 10485 | |
| 10486 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10487 | } |
| 10488 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10489 | |
| 10490 | |
| 10491 | return 0; |
| 10492 | } |