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 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2337 | int32_t ParseSimpleTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2338 | { |
| 2339 | QCBORDecodeContext DCtx; |
| 2340 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2341 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2342 | |
| 2343 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2344 | QCBORDecode_Init(&DCtx, |
| 2345 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2346 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2347 | |
| 2348 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2349 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2350 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2351 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2352 | Item.val.uCount != 10) |
| 2353 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2354 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2355 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2356 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2357 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 2358 | return -1; |
| 2359 | |
| 2360 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2361 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2362 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 2363 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2364 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2365 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2366 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2367 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 2368 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2369 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2370 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2371 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2372 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 2373 | return -1; |
| 2374 | |
| 2375 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 2376 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2377 | return -1; |
| 2378 | |
| 2379 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2380 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2381 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 2382 | return -1; |
| 2383 | |
| 2384 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2385 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2386 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 2387 | return -1; |
| 2388 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2389 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2390 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2391 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2392 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2393 | return -1; |
| 2394 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 2395 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2396 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2397 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2398 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2399 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2400 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 2401 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2402 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2403 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2404 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2405 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 2406 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2407 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2408 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2409 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2413 | int32_t NotWellFormedTests(void) |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2414 | { |
| 2415 | // Loop over all the not-well-formed instance of CBOR |
| 2416 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2417 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 2418 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2419 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 2420 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 2421 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 2422 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2423 | // Set up decoder context. String allocator needed for indefinite |
| 2424 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2425 | QCBORDecodeContext DCtx; |
| 2426 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2427 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2428 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 2429 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2430 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2431 | |
| 2432 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2433 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2434 | do { |
| 2435 | QCBORItem Item; |
| 2436 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2437 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2438 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2439 | |
| 2440 | // Every test vector must fail with |
| 2441 | // a not-well-formed error. If not |
| 2442 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2443 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2444 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2445 | /* Return index of failure and QCBOR error in the result */ |
| 2446 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
| 2452 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2453 | struct DecodeFailTestInput { |
| 2454 | const char *szDescription; /* Description of the test */ |
| 2455 | QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */ |
| 2456 | UsefulBufC Input; /* Chunk of CBOR that cases error */ |
| 2457 | QCBORError nError; /* The expected error */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2458 | }; |
| 2459 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2460 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2461 | static int32_t |
| 2462 | ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2463 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2464 | int nIndex; |
| 2465 | QCBORDecodeContext DCtx; |
| 2466 | QCBORError uCBORError; |
| 2467 | QCBORItem Item; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2468 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2469 | for(nIndex = 0; nIndex < nNumFails; nIndex++) { |
| 2470 | const struct DecodeFailTestInput *pF = &pFailInputs[nIndex]; |
| 2471 | |
| 2472 | QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2473 | |
| 2474 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2475 | /* Set up the decoding context including a memory pool so that |
| 2476 | * indefinite length items can be checked. |
| 2477 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2478 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2479 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2480 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2481 | if(uCBORError != QCBOR_SUCCESS) { |
| 2482 | return -1; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2483 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2484 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2485 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2486 | if(nIndex == 8) { |
| 2487 | uCBORError = 9; /* For setting break points */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2490 | /* Iterate until there is an error of some sort of error */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2491 | do { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2492 | /* Set to something none-zero, something other than QCBOR_TYPE_NONE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2493 | memset(&Item, 0x33, sizeof(Item)); |
| 2494 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2495 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2496 | } while(uCBORError == QCBOR_SUCCESS); |
| 2497 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2498 | /* Must get the expected error or the this test fails |
| 2499 | * The data and label type must also be QCBOR_TYPE_NONE. |
| 2500 | */ |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2501 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2502 | Item.uDataType != QCBOR_TYPE_NONE || |
| 2503 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2504 | return (int32_t)(nIndex * 1000 + (int)uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | return 0; |
| 2509 | } |
| 2510 | |
| 2511 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2512 | static const struct DecodeFailTestInput Failures[] = { |
| 2513 | /* Most of this is copied from not_well_formed.h. Here the error |
| 2514 | * code returned is also checked. |
| 2515 | */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2516 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2517 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2518 | /* Indefinite length strings must be closed off */ |
| 2519 | { "An indefinite length byte string not closed off", |
| 2520 | QCBOR_DECODE_MODE_NORMAL, |
| 2521 | {"0x5f\x41\x00", 3}, |
| 2522 | QCBOR_ERR_HIT_END |
| 2523 | }, |
| 2524 | { "An indefinite length text string not closed off", |
| 2525 | QCBOR_DECODE_MODE_NORMAL, |
| 2526 | {"\x7f\x61\x00", 3}, |
| 2527 | QCBOR_ERR_HIT_END |
| 2528 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2529 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2530 | /* All the chunks in an indefinite length string must be of the |
| 2531 | * type of indefinite length string |
| 2532 | */ |
| 2533 | { "Indefinite length byte string with text string chunk", |
| 2534 | QCBOR_DECODE_MODE_NORMAL, |
| 2535 | {"\x5f\x61\x00\xff", 4}, |
| 2536 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2537 | }, |
| 2538 | { "Indefinite length text string with a byte string chunk", |
| 2539 | QCBOR_DECODE_MODE_NORMAL, |
| 2540 | {"\x7f\x41\x00\xff", 4}, |
| 2541 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2542 | }, |
| 2543 | { "Indefinite length byte string with a positive integer chunk", |
| 2544 | QCBOR_DECODE_MODE_NORMAL, |
| 2545 | {"\x5f\x00\xff", 3}, |
| 2546 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2547 | }, |
| 2548 | { "Indefinite length byte string with an negative integer chunk", |
| 2549 | QCBOR_DECODE_MODE_NORMAL, |
| 2550 | {"\x5f\x21\xff", 3}, |
| 2551 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2552 | }, |
| 2553 | { "Indefinite length byte string with an array chunk", |
| 2554 | QCBOR_DECODE_MODE_NORMAL, |
| 2555 | {"\x5f\x80\xff", 3}, |
| 2556 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2557 | }, |
| 2558 | { "Indefinite length byte string with an map chunk", |
| 2559 | QCBOR_DECODE_MODE_NORMAL, |
| 2560 | {"\x5f\xa0\xff", 3}, |
| 2561 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2562 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2563 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2564 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2565 | { "Indefinite length byte string with tagged integer chunk", |
| 2566 | QCBOR_DECODE_MODE_NORMAL, |
| 2567 | {"\x5f\xc0\x00\xff", 4}, |
| 2568 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2569 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2570 | #else |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2571 | { "Indefinite length byte string with tagged integer chunk", |
| 2572 | QCBOR_DECODE_MODE_NORMAL, |
| 2573 | {"\x5f\xc0\x00\xff", 4}, |
| 2574 | QCBOR_ERR_TAGS_DISABLED |
| 2575 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2576 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2577 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2578 | { "Indefinite length byte string with an simple type chunk", |
| 2579 | QCBOR_DECODE_MODE_NORMAL, |
| 2580 | {"\x5f\xe0\xff", 3}, |
| 2581 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2582 | }, |
| 2583 | { "???", |
| 2584 | QCBOR_DECODE_MODE_NORMAL, |
| 2585 | {"\x5f\x5f\x41\x00\xff\xff", 6}, |
| 2586 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2587 | }, |
| 2588 | { "indefinite length text string with indefinite string inside", |
| 2589 | QCBOR_DECODE_MODE_NORMAL, |
| 2590 | {"\x7f\x7f\x61\x00\xff\xff", 6}, |
| 2591 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 2592 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2593 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2594 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2595 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 2596 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2597 | /* Definte length maps and arrays must be closed by having the right number of items */ |
| 2598 | { "A definte length array that is supposed to have 1 item, but has none", |
| 2599 | QCBOR_DECODE_MODE_NORMAL, |
| 2600 | {"\x81", 1}, |
| 2601 | QCBOR_ERR_NO_MORE_ITEMS |
| 2602 | }, |
| 2603 | { "A definte length array that is supposed to have 2 items, but has only 1", |
| 2604 | QCBOR_DECODE_MODE_NORMAL, |
| 2605 | {"\x82\x00", 2}, |
| 2606 | QCBOR_ERR_NO_MORE_ITEMS |
| 2607 | }, |
| 2608 | { "A definte length array that is supposed to have 511 items, but has only 1", |
| 2609 | QCBOR_DECODE_MODE_NORMAL, |
| 2610 | {"\x9a\x01\xff\x00", 4}, |
| 2611 | QCBOR_ERR_HIT_END |
| 2612 | }, |
| 2613 | { "A definte length map that is supposed to have 1 item, but has none", |
| 2614 | QCBOR_DECODE_MODE_NORMAL, |
| 2615 | {"\xa1", 1}, |
| 2616 | QCBOR_ERR_NO_MORE_ITEMS |
| 2617 | }, |
| 2618 | { "A definte length map that is supposed to have s item, but has only 1", |
| 2619 | QCBOR_DECODE_MODE_NORMAL, |
| 2620 | {"\xa2\x01\x02", 3}, |
| 2621 | QCBOR_ERR_NO_MORE_ITEMS |
| 2622 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2623 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2624 | /* Indefinte length maps and arrays must be ended by a break */ |
| 2625 | { "Indefinite length array with zero items and no break", |
| 2626 | QCBOR_DECODE_MODE_NORMAL, |
| 2627 | {"\x9f", 1}, |
| 2628 | QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2629 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2630 | { "Indefinite length array with two items and no break", |
| 2631 | QCBOR_DECODE_MODE_NORMAL, |
| 2632 | {"\x9\x01\x02", 3}, |
| 2633 | QCBOR_ERR_NO_MORE_ITEMS |
| 2634 | }, |
| 2635 | { "Indefinite length map with zero items and no break", |
| 2636 | QCBOR_DECODE_MODE_NORMAL, |
| 2637 | {"\xbf", 1}, |
| 2638 | QCBOR_ERR_NO_MORE_ITEMS |
| 2639 | }, |
| 2640 | { "Indefinite length map with two items and no break", |
| 2641 | QCBOR_DECODE_MODE_NORMAL, |
| 2642 | {"\xbf\x01\x02\x01\x02", 5}, |
| 2643 | QCBOR_ERR_NO_MORE_ITEMS |
| 2644 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2645 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2646 | /* Nested maps and arrays must be closed off (some extra nested test vectors) */ |
| 2647 | { "Unclosed indefinite array containing a closed definite length array", |
| 2648 | QCBOR_DECODE_MODE_NORMAL, |
| 2649 | {"\x9f\x80\x00", 3}, |
| 2650 | QCBOR_ERR_NO_MORE_ITEMS |
| 2651 | }, |
| 2652 | |
| 2653 | { "Definite length array containing an unclosed indefinite length array", |
| 2654 | QCBOR_DECODE_MODE_NORMAL, |
| 2655 | {"\x81\x9f", 2}, |
| 2656 | QCBOR_ERR_NO_MORE_ITEMS |
| 2657 | }, |
| 2658 | { "Unclosed indefinite map containing a closed definite length array", |
| 2659 | QCBOR_DECODE_MODE_NORMAL, |
| 2660 | {"\xbf\x01\x80\x00\xa0", 5}, |
| 2661 | QCBOR_ERR_NO_MORE_ITEMS |
| 2662 | }, |
| 2663 | { "Definite length map containing an unclosed indefinite length array", |
| 2664 | QCBOR_DECODE_MODE_NORMAL, |
| 2665 | {"\xa1\x02\x9f", 3}, |
| 2666 | QCBOR_ERR_NO_MORE_ITEMS |
| 2667 | }, |
| 2668 | { "Deeply nested definite length arrays with deepest one unclosed", |
| 2669 | QCBOR_DECODE_MODE_NORMAL, |
| 2670 | {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9}, |
| 2671 | QCBOR_ERR_NO_MORE_ITEMS |
| 2672 | }, |
| 2673 | { "Deeply nested indefinite length arrays with deepest one unclosed", |
| 2674 | QCBOR_DECODE_MODE_NORMAL, |
| 2675 | {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9}, |
| 2676 | QCBOR_ERR_NO_MORE_ITEMS |
| 2677 | }, |
| 2678 | { "Mixed nesting with indefinite unclosed", |
| 2679 | QCBOR_DECODE_MODE_NORMAL, |
| 2680 | {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9}, |
| 2681 | QCBOR_ERR_NO_MORE_ITEMS }, |
| 2682 | { "Mixed nesting with definite unclosed", |
| 2683 | QCBOR_DECODE_MODE_NORMAL, |
| 2684 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2685 | QCBOR_ERR_BAD_BREAK |
| 2686 | }, |
| 2687 | { "Unclosed indefinite length map in definite length maps", |
| 2688 | QCBOR_DECODE_MODE_NORMAL, |
| 2689 | {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8}, |
| 2690 | QCBOR_ERR_NO_MORE_ITEMS |
| 2691 | }, |
| 2692 | { "Unclosed definite length map in indefinite length maps", |
| 2693 | QCBOR_DECODE_MODE_NORMAL, |
| 2694 | {"\xbf\x01\xbf\x02\xa1", 5}, |
| 2695 | QCBOR_ERR_NO_MORE_ITEMS |
| 2696 | }, |
| 2697 | { "Unclosed indefinite length array in definite length maps", |
| 2698 | QCBOR_DECODE_MODE_NORMAL, |
| 2699 | {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8}, |
| 2700 | QCBOR_ERR_NO_MORE_ITEMS |
| 2701 | }, |
| 2702 | { "Unclosed definite length array in indefinite length maps", |
| 2703 | QCBOR_DECODE_MODE_NORMAL, |
| 2704 | {"\xbf\x01\xbf\x02\x81", 5}, |
| 2705 | QCBOR_ERR_NO_MORE_ITEMS |
| 2706 | }, |
| 2707 | { "Unclosed indefinite length map in definite length arrays", |
| 2708 | QCBOR_DECODE_MODE_NORMAL, |
| 2709 | {"\x81\x82\xbf\xff\xbf", 5}, |
| 2710 | QCBOR_ERR_NO_MORE_ITEMS |
| 2711 | }, |
| 2712 | { "Unclosed definite length map in indefinite length arrays", |
| 2713 | QCBOR_DECODE_MODE_NORMAL, |
| 2714 | {"\x9f\x9f\xa1", 3}, |
| 2715 | QCBOR_ERR_NO_MORE_ITEMS |
| 2716 | }, |
| 2717 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2718 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2719 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2720 | /* The "argument" for the data item is incomplete */ |
| 2721 | { "Positive integer missing 1 byte argument", |
| 2722 | QCBOR_DECODE_MODE_NORMAL, |
| 2723 | {"\x18", 1}, |
| 2724 | QCBOR_ERR_HIT_END |
| 2725 | }, |
| 2726 | { "Positive integer missing 2 byte argument", |
| 2727 | QCBOR_DECODE_MODE_NORMAL, |
| 2728 | {"\x19", 1}, |
| 2729 | QCBOR_ERR_HIT_END |
| 2730 | }, |
| 2731 | { "Positive integer missing 4 byte argument", |
| 2732 | QCBOR_DECODE_MODE_NORMAL, |
| 2733 | {"\x1a", 1}, |
| 2734 | QCBOR_ERR_HIT_END |
| 2735 | }, |
| 2736 | { "Positive integer missing 8 byte argument", |
| 2737 | QCBOR_DECODE_MODE_NORMAL, |
| 2738 | {"\x1b", 1}, |
| 2739 | QCBOR_ERR_HIT_END |
| 2740 | }, |
| 2741 | { "Positive integer missing 1 byte of 2 byte argument", |
| 2742 | QCBOR_DECODE_MODE_NORMAL, |
| 2743 | {"\x19\x01", 2}, |
| 2744 | QCBOR_ERR_HIT_END |
| 2745 | }, |
| 2746 | { "Positive integer missing 2 bytes of 4 byte argument", |
| 2747 | QCBOR_DECODE_MODE_NORMAL, |
| 2748 | {"\x1a\x01\x02", 3}, |
| 2749 | QCBOR_ERR_HIT_END |
| 2750 | }, |
| 2751 | { "Positive integer missing 1 bytes of 7 byte argument", |
| 2752 | QCBOR_DECODE_MODE_NORMAL, |
| 2753 | {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8}, |
| 2754 | QCBOR_ERR_HIT_END |
| 2755 | }, |
| 2756 | { "Negative integer missing 1 byte argument", |
| 2757 | QCBOR_DECODE_MODE_NORMAL, |
| 2758 | {"\x38", 1}, |
| 2759 | QCBOR_ERR_HIT_END |
| 2760 | }, |
| 2761 | { "Binary string missing 1 byte argument", |
| 2762 | QCBOR_DECODE_MODE_NORMAL, |
| 2763 | {"\x58", 1}, |
| 2764 | QCBOR_ERR_HIT_END |
| 2765 | }, |
| 2766 | { "Text string missing 1 byte argument", |
| 2767 | QCBOR_DECODE_MODE_NORMAL, |
| 2768 | {"\x78", 1}, |
| 2769 | QCBOR_ERR_HIT_END |
| 2770 | }, |
| 2771 | { "Array missing 1 byte argument", |
| 2772 | QCBOR_DECODE_MODE_NORMAL, |
| 2773 | {"\x98", 1}, |
| 2774 | QCBOR_ERR_HIT_END |
| 2775 | }, |
| 2776 | { "Map missing 1 byte argument", |
| 2777 | QCBOR_DECODE_MODE_NORMAL, |
| 2778 | {"\xb8", 1}, |
| 2779 | QCBOR_ERR_HIT_END |
| 2780 | }, |
| 2781 | { "Tag missing 1 byte argument", |
| 2782 | QCBOR_DECODE_MODE_NORMAL, |
| 2783 | {"\xd8", 1}, |
| 2784 | QCBOR_ERR_HIT_END |
| 2785 | }, |
| 2786 | { "Simple missing 1 byte argument", |
| 2787 | QCBOR_DECODE_MODE_NORMAL, |
| 2788 | {"\xf8", 1}, |
| 2789 | QCBOR_ERR_HIT_END |
| 2790 | }, |
| 2791 | { "half-precision with 1 byte argument", |
| 2792 | QCBOR_DECODE_MODE_NORMAL, |
| 2793 | {"\xf9\x00", 2}, |
| 2794 | QCBOR_ERR_HIT_END |
| 2795 | }, |
| 2796 | { "single-precision with 2 byte argument", |
| 2797 | QCBOR_DECODE_MODE_NORMAL, |
| 2798 | {"\0xfa\x00\x00", 3}, |
| 2799 | QCBOR_ERR_HIT_END |
| 2800 | }, |
| 2801 | { "double-precision with 3 byte argument", |
| 2802 | QCBOR_DECODE_MODE_NORMAL, |
| 2803 | {"\xfb\x00\x00\x00", 4}, |
| 2804 | QCBOR_ERR_HIT_END |
| 2805 | }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2806 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2807 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2808 | { "Tag with no content", |
| 2809 | QCBOR_DECODE_MODE_NORMAL, |
| 2810 | {"\xc0", 1}, |
| 2811 | QCBOR_ERR_HIT_END |
| 2812 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2813 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2814 | { "Tag with no content", |
| 2815 | QCBOR_DECODE_MODE_NORMAL, |
| 2816 | {"\xc0", 1}, |
| 2817 | QCBOR_ERR_TAGS_DISABLED |
| 2818 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2819 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2820 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2821 | /* Breaks must not occur in definite length arrays and maps */ |
| 2822 | { "Array of length 1 with sole member replaced by a break", |
| 2823 | QCBOR_DECODE_MODE_NORMAL, |
| 2824 | {"\x81\xff", 2}, |
| 2825 | QCBOR_ERR_BAD_BREAK |
| 2826 | }, |
| 2827 | { "Array of length 2 with 2nd member replaced by a break", |
| 2828 | QCBOR_DECODE_MODE_NORMAL, |
| 2829 | {"\x82\x00\xff", 3}, |
| 2830 | QCBOR_ERR_BAD_BREAK |
| 2831 | }, |
| 2832 | { "Map of length 1 with sole member label replaced by a break", |
| 2833 | QCBOR_DECODE_MODE_NORMAL, |
| 2834 | {"\xa1\xff", 2}, |
| 2835 | QCBOR_ERR_BAD_BREAK |
| 2836 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2837 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2838 | /* Map of length 1 with sole member label replaced by break */ |
| 2839 | { "Alternate representation that some decoders handle differently", |
| 2840 | QCBOR_DECODE_MODE_NORMAL, |
| 2841 | {"\xa1\xff\x00", 3}, |
| 2842 | QCBOR_ERR_BAD_BREAK |
| 2843 | }, |
| 2844 | { "Array of length 1 with 2nd member value replaced by a break", |
| 2845 | QCBOR_DECODE_MODE_NORMAL, |
| 2846 | {"\xa1\x00\xff", 3}, |
| 2847 | QCBOR_ERR_BAD_BREAK |
| 2848 | }, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2849 | { "Map of length 2 with 2nd entry label replaced by a break", |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2850 | QCBOR_DECODE_MODE_NORMAL, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2851 | {"\xa2\x00\x00\xff\x00", 5}, |
| 2852 | QCBOR_ERR_BAD_BREAK |
| 2853 | }, |
| 2854 | { "Map of length 2 with 2nd entry value replaced by a break", |
| 2855 | QCBOR_DECODE_MODE_NORMAL, |
| 2856 | {"\xa2\x00\x00\x01\xff", 5}, |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2857 | QCBOR_ERR_BAD_BREAK |
| 2858 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2859 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2860 | /* Breaks must not occur on their own out of an indefinite length data item */ |
| 2861 | { "A bare break is not well formed", |
| 2862 | QCBOR_DECODE_MODE_NORMAL, |
| 2863 | {"\xff", 1}, |
| 2864 | QCBOR_ERR_BAD_BREAK |
| 2865 | }, |
| 2866 | { "A bare break after a zero length definite length array", |
| 2867 | QCBOR_DECODE_MODE_NORMAL, |
| 2868 | {"\x80\xff", 2}, |
| 2869 | QCBOR_ERR_BAD_BREAK |
| 2870 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2871 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2872 | { "A bare break after a zero length indefinite length map", |
| 2873 | QCBOR_DECODE_MODE_NORMAL, |
| 2874 | {"\x9f\xff\xff", 3}, |
| 2875 | QCBOR_ERR_BAD_BREAK |
| 2876 | }, |
| 2877 | { "A break inside a definite length array inside an indefenite length array", |
| 2878 | QCBOR_DECODE_MODE_NORMAL, |
| 2879 | {"\x9f\x81\xff", 3}, |
| 2880 | QCBOR_ERR_BAD_BREAK |
| 2881 | }, |
| 2882 | { "Complicated mixed nesting with break outside indefinite length array", |
| 2883 | QCBOR_DECODE_MODE_NORMAL, |
| 2884 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2885 | QCBOR_ERR_BAD_BREAK }, |
| 2886 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 2887 | |
| 2888 | /* Forbidden two byte encodings of simple types */ |
| 2889 | { "Must use 0xe0 instead", |
| 2890 | QCBOR_DECODE_MODE_NORMAL, |
| 2891 | {"\xf8\x00", 2}, |
| 2892 | QCBOR_ERR_BAD_TYPE_7 |
| 2893 | }, |
| 2894 | { "Should use 0xe1 instead", |
| 2895 | QCBOR_DECODE_MODE_NORMAL, |
| 2896 | {"\xf8\x01", 2}, |
| 2897 | QCBOR_ERR_BAD_TYPE_7 |
| 2898 | }, |
| 2899 | { "Should use 0xe2 instead", |
| 2900 | QCBOR_DECODE_MODE_NORMAL, |
| 2901 | {"\xf8\x02", 2}, |
| 2902 | QCBOR_ERR_BAD_TYPE_7 |
| 2903 | }, { "Should use 0xe3 instead", |
| 2904 | QCBOR_DECODE_MODE_NORMAL, |
| 2905 | {"\xf8\x03", 2}, |
| 2906 | QCBOR_ERR_BAD_TYPE_7 |
| 2907 | }, |
| 2908 | { "Should use 0xe4 instead", |
| 2909 | QCBOR_DECODE_MODE_NORMAL, |
| 2910 | {"\xf8\x04", 2}, |
| 2911 | QCBOR_ERR_BAD_TYPE_7 |
| 2912 | }, |
| 2913 | { "Should use 0xe5 instead", |
| 2914 | QCBOR_DECODE_MODE_NORMAL, |
| 2915 | {"\xf8\x05", 2}, |
| 2916 | QCBOR_ERR_BAD_TYPE_7 |
| 2917 | }, |
| 2918 | { "Should use 0xe6 instead", |
| 2919 | QCBOR_DECODE_MODE_NORMAL, |
| 2920 | {"\xf8\x06", 2}, |
| 2921 | QCBOR_ERR_BAD_TYPE_7 |
| 2922 | }, |
| 2923 | { "Should use 0xe7 instead", |
| 2924 | QCBOR_DECODE_MODE_NORMAL, |
| 2925 | {"\xf8\x07", 2}, |
| 2926 | QCBOR_ERR_BAD_TYPE_7 |
| 2927 | }, |
| 2928 | { "Should use 0xe8 instead", |
| 2929 | QCBOR_DECODE_MODE_NORMAL, |
| 2930 | {"\xf8\x08", 2}, |
| 2931 | QCBOR_ERR_BAD_TYPE_7 |
| 2932 | }, |
| 2933 | { "Should use 0xe9 instead", |
| 2934 | QCBOR_DECODE_MODE_NORMAL, |
| 2935 | {"\xf8\x09", 2}, |
| 2936 | QCBOR_ERR_BAD_TYPE_7 |
| 2937 | }, |
| 2938 | { "Should use 0xea instead", |
| 2939 | QCBOR_DECODE_MODE_NORMAL, |
| 2940 | {"\xf8\x0a", 2}, |
| 2941 | QCBOR_ERR_BAD_TYPE_7 |
| 2942 | }, |
| 2943 | { "Should use 0xeb instead", |
| 2944 | QCBOR_DECODE_MODE_NORMAL, |
| 2945 | {"\xf8\x0b", 2}, |
| 2946 | QCBOR_ERR_BAD_TYPE_7 |
| 2947 | }, |
| 2948 | { "Should use 0xec instead", |
| 2949 | QCBOR_DECODE_MODE_NORMAL, |
| 2950 | {"\xf8\x0c", 2}, |
| 2951 | QCBOR_ERR_BAD_TYPE_7 |
| 2952 | }, |
| 2953 | { "Should use 0xed instead", |
| 2954 | QCBOR_DECODE_MODE_NORMAL, |
| 2955 | {"\xf8\x0d", 2}, |
| 2956 | QCBOR_ERR_BAD_TYPE_7 |
| 2957 | }, |
| 2958 | { "Should use 0xee instead", |
| 2959 | QCBOR_DECODE_MODE_NORMAL, |
| 2960 | {"\xf8\x0e", 2}, |
| 2961 | QCBOR_ERR_BAD_TYPE_7 |
| 2962 | }, |
| 2963 | { "Should use 0xef instead", |
| 2964 | QCBOR_DECODE_MODE_NORMAL, |
| 2965 | {"\xf8\x0f", 2}, |
| 2966 | QCBOR_ERR_BAD_TYPE_7 |
| 2967 | }, |
| 2968 | { "Should use 0xf0 instead", |
| 2969 | QCBOR_DECODE_MODE_NORMAL, |
| 2970 | {"\xf8\x10", 2}, |
| 2971 | QCBOR_ERR_BAD_TYPE_7 |
| 2972 | }, |
| 2973 | { "Should use 0xf1 instead", |
| 2974 | QCBOR_DECODE_MODE_NORMAL, |
| 2975 | {"\xf8\x11", 2}, |
| 2976 | QCBOR_ERR_BAD_TYPE_7 |
| 2977 | }, |
| 2978 | { "Should use 0xf2 instead", |
| 2979 | QCBOR_DECODE_MODE_NORMAL, |
| 2980 | {"\xf8\x12", 2}, |
| 2981 | QCBOR_ERR_BAD_TYPE_7 |
| 2982 | }, |
| 2983 | { "Should use 0xf3 instead", |
| 2984 | QCBOR_DECODE_MODE_NORMAL, |
| 2985 | {"\xf8\x13", 2}, |
| 2986 | QCBOR_ERR_BAD_TYPE_7 |
| 2987 | }, |
| 2988 | { "Should use 0xf4 instead", |
| 2989 | QCBOR_DECODE_MODE_NORMAL, |
| 2990 | {"\xf8\x14", 2}, |
| 2991 | QCBOR_ERR_BAD_TYPE_7 |
| 2992 | }, |
| 2993 | { "Should use 0xf5 instead", |
| 2994 | QCBOR_DECODE_MODE_NORMAL, |
| 2995 | {"\xf8\x15", 2}, |
| 2996 | QCBOR_ERR_BAD_TYPE_7 |
| 2997 | }, |
| 2998 | { "Should use 0xf6 instead", |
| 2999 | QCBOR_DECODE_MODE_NORMAL, |
| 3000 | {"\xf8\x16", 2}, |
| 3001 | QCBOR_ERR_BAD_TYPE_7 |
| 3002 | }, |
| 3003 | { "Should use 0xef7 instead", |
| 3004 | QCBOR_DECODE_MODE_NORMAL, |
| 3005 | {"\xf8\x17", 2}, |
| 3006 | QCBOR_ERR_BAD_TYPE_7 |
| 3007 | }, |
| 3008 | { "Should use 0xef8 instead", |
| 3009 | QCBOR_DECODE_MODE_NORMAL, |
| 3010 | {"\xf8\x18", 2}, |
| 3011 | QCBOR_ERR_BAD_TYPE_7 |
| 3012 | }, |
| 3013 | { "Reserved", |
| 3014 | QCBOR_DECODE_MODE_NORMAL, |
| 3015 | {"\xf8\x18", 2}, |
| 3016 | QCBOR_ERR_BAD_TYPE_7 |
| 3017 | }, |
| 3018 | |
| 3019 | /* Maps must have an even number of data items (key & value) */ |
| 3020 | { "Map with 1 item when it should have 2", |
| 3021 | QCBOR_DECODE_MODE_NORMAL, |
| 3022 | {"\xa1\x00", 2}, |
| 3023 | QCBOR_ERR_HIT_END |
| 3024 | }, |
| 3025 | { "Map with 3 item when it should have 4", |
| 3026 | QCBOR_DECODE_MODE_NORMAL, |
| 3027 | {"\xa2\x00\x00\x00", 2}, |
| 3028 | QCBOR_ERR_HIT_END |
| 3029 | }, |
| 3030 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 3031 | { "Map with 1 item when it should have 2", |
| 3032 | QCBOR_DECODE_MODE_NORMAL, |
| 3033 | {"\xbf\x00\xff", 3}, |
| 3034 | QCBOR_ERR_BAD_BREAK |
| 3035 | }, |
| 3036 | { "Map with 3 item when it should have 4", |
| 3037 | QCBOR_DECODE_MODE_NORMAL, |
| 3038 | {"\xbf\x00\x00\x00\xff", 5}, |
| 3039 | QCBOR_ERR_BAD_BREAK |
| 3040 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3041 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3042 | |
| 3043 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3044 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3045 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3046 | { "Text-based date, with an integer", |
| 3047 | QCBOR_DECODE_MODE_NORMAL, |
| 3048 | {"\xc0\x00", 2}, |
| 3049 | QCBOR_ERR_BAD_OPT_TAG |
| 3050 | }, |
| 3051 | { "Epoch date, with an byte string", |
| 3052 | QCBOR_DECODE_MODE_NORMAL, |
| 3053 | {"\xc1\x41\x33", 3}, |
| 3054 | QCBOR_ERR_BAD_OPT_TAG |
| 3055 | }, |
| 3056 | { "tagged as both epoch and string dates", |
| 3057 | QCBOR_DECODE_MODE_NORMAL, |
| 3058 | {"\xc1\xc0\x00", 3}, |
| 3059 | QCBOR_ERR_BAD_OPT_TAG |
| 3060 | }, |
| 3061 | { "big num tagged an int, not a byte string", |
| 3062 | QCBOR_DECODE_MODE_NORMAL, |
| 3063 | {"\xc2\x00", 2}, |
| 3064 | QCBOR_ERR_BAD_OPT_TAG |
| 3065 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3066 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3067 | /* In addition to not-well-formed, some invalid CBOR */ |
| 3068 | { "Text-based date, with an integer", |
| 3069 | QCBOR_DECODE_MODE_NORMAL, |
| 3070 | {"\xc0\x00", 2}, |
| 3071 | QCBOR_ERR_TAGS_DISABLED |
| 3072 | }, |
| 3073 | { "Epoch date, with an byte string", |
| 3074 | QCBOR_DECODE_MODE_NORMAL, |
| 3075 | {"\xc1\x41\x33", 3}, |
| 3076 | QCBOR_ERR_TAGS_DISABLED |
| 3077 | }, |
| 3078 | { "tagged as both epoch and string dates", |
| 3079 | QCBOR_DECODE_MODE_NORMAL, |
| 3080 | {"\xc1\xc0\x00", 3}, |
| 3081 | QCBOR_ERR_TAGS_DISABLED |
| 3082 | }, |
| 3083 | { "big num tagged an int, not a byte string", |
| 3084 | QCBOR_DECODE_MODE_NORMAL, |
| 3085 | {"\xc2\x00", 2}, |
| 3086 | QCBOR_ERR_TAGS_DISABLED |
| 3087 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3088 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3089 | }; |
| 3090 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3091 | |
| 3092 | |
| 3093 | int32_t |
| 3094 | DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3095 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3096 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 3097 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 3098 | nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3099 | if(nResult) { |
| 3100 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3101 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3102 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3103 | // Corrupt the UsefulInputBuf and see that |
| 3104 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3105 | QCBORDecodeContext DCtx; |
| 3106 | QCBORItem Item; |
| 3107 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3108 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3109 | QCBORDecode_Init(&DCtx, |
| 3110 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 3111 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3112 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3113 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3114 | return (int32_t)uQCBORError; |
| 3115 | } |
| 3116 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 3117 | // This wasn't supposed to happen |
| 3118 | return -1; |
| 3119 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3120 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3121 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3122 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3123 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3124 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 3125 | // Did not get back the error expected |
| 3126 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3127 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3128 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3129 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3130 | /* |
| 3131 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 3132 | tests here can be performed to see that the max length |
| 3133 | error check works correctly. See DecodeBytes(). If the max |
| 3134 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3135 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3136 | This test will automatocally adapt the all CPU sizes |
| 3137 | through the use of SIZE_MAX. |
| 3138 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3139 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 3140 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3141 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3142 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 3143 | // This makes a CBOR head with a text string that is very long |
| 3144 | // but doesn't fill in the bytes of the text string as that is |
| 3145 | // not needed to test this part of QCBOR. |
| 3146 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 3147 | |
| 3148 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 3149 | |
| 3150 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3151 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3152 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3153 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 3154 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3158 | /* Try all 256 values of the byte at nLen including recursing for |
| 3159 | each of the values to try values at nLen+1 ... up to nLenMax |
| 3160 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 3161 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3162 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3163 | if(nLen >= nLenMax) { |
| 3164 | return; |
| 3165 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3166 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3167 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 3168 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3169 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3170 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3171 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3172 | // Get ready to parse |
| 3173 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3174 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3175 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3176 | // Parse by getting the next item until an error occurs |
| 3177 | // Just about every possible decoder error can occur here |
| 3178 | // The goal of this test is not to check for the correct |
| 3179 | // error since that is not really possible. It is to |
| 3180 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3181 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3182 | QCBORItem Item; |
| 3183 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3184 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3185 | break; |
| 3186 | } |
| 3187 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3188 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3189 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3194 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3195 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3196 | // Size 2 tests 64K inputs and runs quickly |
| 3197 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3198 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3199 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3200 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3201 | return 0; |
| 3202 | } |
| 3203 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3204 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3205 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3206 | { |
| 3207 | // size 3 tests 16 million inputs and runs OK |
| 3208 | // in seconds on fast machines. Size 4 takes |
| 3209 | // 10+ minutes and 5 half a day on fast |
| 3210 | // machines. This test is kept separate from |
| 3211 | // the others so as to no slow down the use |
| 3212 | // of them as a very frequent regression. |
| 3213 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3214 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3215 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3216 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 3217 | return 0; |
| 3218 | } |
| 3219 | |
| 3220 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3221 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3222 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3223 | 0xc0, // tag for string date |
| 3224 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3225 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3226 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3227 | 0xc0, // tag for string date |
| 3228 | 0x00, // Wrong type for a string date |
| 3229 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3230 | /* 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] | 3231 | 0xc1, // tag for epoch date |
| 3232 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3233 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3234 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3235 | 0xc1, |
| 3236 | 0x62, 'h', 'i', // wrong type tagged |
| 3237 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3238 | /* 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] | 3239 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3240 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3241 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 3242 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3243 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3244 | 0xc1, // tag for epoch date |
| 3245 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3246 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3247 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3248 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 3249 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3250 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3251 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3252 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3253 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3254 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3255 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3256 | 0xc1, // tag for epoch date |
| 3257 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 3258 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 3259 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3260 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3261 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3262 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3263 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3264 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3265 | 0xc1, // tag for epoch date |
| 3266 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 3267 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3268 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3269 | 0xc1, |
| 3270 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 3271 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3272 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3273 | 0xc1, // tag for epoch date |
| 3274 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3275 | }; |
| 3276 | |
| 3277 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3278 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3279 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3280 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 3281 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3282 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3283 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3284 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3285 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3286 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3287 | return diff > 0.0000001; |
| 3288 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3289 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3290 | |
| 3291 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3292 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3293 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3294 | { |
| 3295 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3296 | QCBORItem Item; |
| 3297 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3298 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3299 | QCBORDecode_Init(&DCtx, |
| 3300 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 3301 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3302 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3303 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3304 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3305 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3306 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3307 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | ba58768 | 2024-02-07 16:46:43 -0800 | [diff] [blame] | 3308 | UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3309 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3310 | } |
| 3311 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3312 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3313 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3314 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 3315 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3316 | } |
| 3317 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3318 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 3319 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3320 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3321 | return -4; |
| 3322 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3323 | if(uError == QCBOR_SUCCESS) { |
| 3324 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3325 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3326 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3327 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3328 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3329 | ) { |
| 3330 | return -5; |
| 3331 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3332 | } |
| 3333 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3334 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3335 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3336 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3337 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3338 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3339 | /* 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] | 3340 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 3341 | // The date is decoded and the two tags are returned. This is to |
| 3342 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3343 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 3344 | return -7; |
| 3345 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3346 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3347 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3348 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3349 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 3350 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 3351 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3352 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3353 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3354 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3355 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3356 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3357 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3358 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3359 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3360 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 3361 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3362 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3363 | return -10; |
| 3364 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3365 | if(uError == QCBOR_SUCCESS) { |
| 3366 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3367 | Item.val.epochDate.nSeconds != 1 |
| 3368 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3369 | || 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] | 3370 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3371 | ) { |
| 3372 | return -11; |
| 3373 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3374 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3375 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3376 | /* 8. Epoch date with too-large single-precision float */ |
| 3377 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3378 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3379 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3380 | } |
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 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 3383 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3384 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3385 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 3386 | } |
| 3387 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3388 | /* 10. Epoch date with largest supported double-precision value */ |
| 3389 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3390 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3391 | return -14; |
| 3392 | } |
| 3393 | if(uError == QCBOR_SUCCESS) { |
| 3394 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 3395 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 3396 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 3397 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 3398 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 3399 | ) { |
| 3400 | return -14; |
| 3401 | } |
| 3402 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3403 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3404 | /* 11. Epoch date with single-precision NaN */ |
| 3405 | 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] | 3406 | return -15; |
| 3407 | } |
| 3408 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3409 | /* 12. Epoch date with double-precision plus infinity */ |
| 3410 | 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] | 3411 | return -16; |
| 3412 | } |
| 3413 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3414 | /* 13. Epoch date with half-precision negative infinity */ |
| 3415 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3416 | 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] | 3417 | return -17; |
| 3418 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3419 | |
| 3420 | return 0; |
| 3421 | } |
| 3422 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3423 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3424 | /* |
| 3425 | Test cases covered here. Some items cover more than one of these. |
| 3426 | positive integer (zero counts as a positive integer) |
| 3427 | negative integer |
| 3428 | half-precision float |
| 3429 | single-precision float |
| 3430 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3431 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3432 | float Overflow error |
| 3433 | Wrong type error for epoch |
| 3434 | Wrong type error for date string |
| 3435 | float disabled error |
| 3436 | half-precision disabled error |
| 3437 | -Infinity |
| 3438 | Slightly too large integer |
| 3439 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3440 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3441 | Get epoch by int |
| 3442 | Get string by int |
| 3443 | Get epoch by string |
| 3444 | Get string by string |
| 3445 | Fail to get epoch by wrong int label |
| 3446 | Fail to get string by wrong string label |
| 3447 | Fail to get epoch by string because it is invalid |
| 3448 | Fail to get epoch by int because it is invalid |
| 3449 | |
| 3450 | Untagged values |
| 3451 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3452 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3453 | 0x87, // array of 7 items |
| 3454 | |
| 3455 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 3456 | |
| 3457 | // Untagged integer 0 |
| 3458 | 0x08, |
| 3459 | 0x00, |
| 3460 | |
| 3461 | // Utagged date string with string label y |
| 3462 | 0x61, 0x79, |
| 3463 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 3464 | |
| 3465 | // Untagged single-precision float with value 3.14 with string label x |
| 3466 | 0x61, 0x78, |
| 3467 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 3468 | |
| 3469 | // Untagged half-precision float with value -2 |
| 3470 | 0x09, |
| 3471 | 0xF9, 0xC0, 0x00, |
| 3472 | |
| 3473 | /* Untagged date-only date string */ |
| 3474 | 0x18, 0x63, |
| 3475 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3476 | |
| 3477 | /* Untagged days-count epoch date */ |
| 3478 | 0x11, |
| 3479 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 3480 | |
| 3481 | // End of map, back to array |
| 3482 | |
| 3483 | 0xa7, // Open map of tagged items with labels |
| 3484 | |
| 3485 | 0x00, |
| 3486 | 0xc0, // tag for string date |
| 3487 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 3488 | |
| 3489 | |
| 3490 | 0x01, |
| 3491 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 3492 | 0xc1, // tag for epoch date |
| 3493 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 3494 | |
| 3495 | 0x05, |
| 3496 | 0xc1, |
| 3497 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 3498 | |
| 3499 | |
| 3500 | 0x07, |
| 3501 | 0xc1, // tag for epoch date |
| 3502 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 3503 | |
| 3504 | /* Tagged days-count epoch date */ |
| 3505 | 0x63, 0x53, 0x44, 0x45, |
| 3506 | 0xD8, 0x64, /* tag(100) */ |
| 3507 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 3508 | |
| 3509 | // Untagged -1000 with label z |
| 3510 | 0x61, 0x7a, |
| 3511 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 3512 | 0x39, 0x03, 0xe7, |
| 3513 | |
| 3514 | /* Tagged date-only date string */ |
| 3515 | 0x63, 0x53, 0x44, 0x53, |
| 3516 | 0xD9, 0x03, 0xEC, |
| 3517 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 3518 | |
| 3519 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3520 | |
| 3521 | 0xc1, |
| 3522 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 3523 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3524 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3525 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 3526 | |
| 3527 | 0xc1, // tag for epoch date |
| 3528 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 3529 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3530 | // These two at the end because they are unrecoverable errors |
| 3531 | 0xc1, // tag for epoch date |
| 3532 | 0x80, // Erroneous empty array as content for date |
| 3533 | |
| 3534 | 0xc0, // tag for string date |
| 3535 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3536 | }; |
| 3537 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3538 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3539 | { |
| 3540 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3541 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3542 | int64_t nEpochDate3, nEpochDate5, |
| 3543 | nEpochDate4, nEpochDate6, |
| 3544 | nEpochDays2; |
| 3545 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3546 | |
| 3547 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3548 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3549 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3550 | |
| 3551 | /* Items are in an array or map to test look up by label and other |
| 3552 | * that might not occur in isolated items. But it does make the |
| 3553 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3554 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3555 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3556 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3557 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3558 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3559 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3560 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3561 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3562 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3563 | return 104; |
| 3564 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3565 | if(uError == QCBOR_SUCCESS) { |
| 3566 | if(nEpochDate5 != 3) { |
| 3567 | return 103; |
| 3568 | } |
| 3569 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3570 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3571 | // A half-precision date with value -2 FFF |
| 3572 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3573 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3574 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3575 | 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] | 3576 | return 106; |
| 3577 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 3578 | if(uError == QCBOR_SUCCESS) { |
| 3579 | if(nEpochDate4 != -2) { |
| 3580 | return 105; |
| 3581 | } |
| 3582 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3583 | |
| 3584 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3585 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 3586 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3587 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3588 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3589 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3590 | return 107; |
| 3591 | } |
| 3592 | |
| 3593 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3594 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3595 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3596 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3597 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3598 | return 108; |
| 3599 | } |
| 3600 | |
| 3601 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3602 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 3603 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3604 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3605 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3606 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3607 | return 109; |
| 3608 | } |
| 3609 | |
| 3610 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3611 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3612 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3613 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3614 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3615 | return 110; |
| 3616 | } |
| 3617 | |
| 3618 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3619 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3620 | |
| 3621 | // Untagged integer 0 |
| 3622 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3623 | &nEpochDate3); |
| 3624 | // Untagged date string |
| 3625 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3626 | &StringDate2); |
| 3627 | |
| 3628 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3629 | &StringDays2); |
| 3630 | |
| 3631 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 3632 | &nEpochDays2); |
| 3633 | |
| 3634 | QCBORDecode_ExitMap(&DC); |
| 3635 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3636 | return 3001; |
| 3637 | } |
| 3638 | |
| 3639 | // The map of tagged items |
| 3640 | QCBORDecode_EnterMap(&DC, NULL); |
| 3641 | |
| 3642 | #ifndef QCBOR_DISABLE_TAGS |
| 3643 | int64_t nEpochDate2, |
| 3644 | nEpochDateFail, |
| 3645 | nEpochDate1400000000, nEpochDays1; |
| 3646 | UsefulBufC StringDays1; |
| 3647 | uint64_t uTag1, uTag2; |
| 3648 | |
| 3649 | // Tagged date string |
| 3650 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3651 | &StringDate1); |
| 3652 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3653 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3654 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3655 | 1, |
| 3656 | QCBOR_TAG_REQUIREMENT_TAG | |
| 3657 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3658 | &nEpochDate1400000000); |
| 3659 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3660 | |
| 3661 | // Get largest negative double precision epoch date allowed |
| 3662 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 3663 | 5, |
| 3664 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 3665 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3666 | &nEpochDate2); |
| 3667 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3668 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3669 | return 102; |
| 3670 | } |
| 3671 | if(uError == QCBOR_SUCCESS) { |
| 3672 | if(nEpochDate2 != -9223372036854773760LL) { |
| 3673 | return 101; |
| 3674 | } |
| 3675 | } |
| 3676 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3677 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3678 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 3679 | "z", |
| 3680 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 3681 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3682 | &nEpochDate6); |
| 3683 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3684 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3685 | |
| 3686 | // Get largest double precision epoch date allowed |
| 3687 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3688 | &nEpochDate2); |
| 3689 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3690 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3691 | return 112; |
| 3692 | } |
| 3693 | if(uError == QCBOR_SUCCESS) { |
| 3694 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 3695 | return 111; |
| 3696 | } |
| 3697 | } |
| 3698 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3699 | /* The days format is much simpler than the date format |
| 3700 | * because it can't be a floating point value. The test |
| 3701 | * of the spiffy decode functions sufficiently covers |
| 3702 | * the test of the non-spiffy decode days date decoding. |
| 3703 | * There is no full fan out of the error conditions |
| 3704 | * and decode options as that is implemented by code |
| 3705 | * that is tested well by the date testing above. |
| 3706 | */ |
| 3707 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 3708 | &StringDays1); |
| 3709 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3710 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 3711 | &nEpochDays1); |
| 3712 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3713 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3714 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3715 | return 3001; |
| 3716 | } |
| 3717 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3718 | // Too-negative float, -9.2233720368547748E+18 |
| 3719 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3720 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3721 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3722 | return 1111; |
| 3723 | } |
| 3724 | |
| 3725 | // Too-large integer |
| 3726 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3727 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3728 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 3729 | return 1; |
| 3730 | } |
| 3731 | |
| 3732 | // Half-precision minus infinity |
| 3733 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3734 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3735 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3736 | return 2; |
| 3737 | } |
| 3738 | |
| 3739 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3740 | // Bad content for epoch date |
| 3741 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3742 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3743 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3744 | return 3; |
| 3745 | } |
| 3746 | |
| 3747 | // Bad content for string date |
| 3748 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 3749 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3750 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3751 | return 4; |
| 3752 | } |
| 3753 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3754 | QCBORDecode_ExitArray(&DC); |
| 3755 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3756 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3757 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3758 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3759 | #else /* QCBOR_DISABLE_TAGS */ |
| 3760 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3761 | &StringDate1); |
| 3762 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3763 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 3764 | return 4; |
| 3765 | } |
| 3766 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3767 | |
| 3768 | |
| 3769 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3770 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3771 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3772 | return 200; |
| 3773 | } |
| 3774 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3775 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3776 | return 201; |
| 3777 | } |
| 3778 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3779 | if(nEpochDays1 != -10676) { |
| 3780 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3781 | } |
| 3782 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3783 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3784 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3785 | } |
| 3786 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3787 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3788 | return 204; |
| 3789 | } |
| 3790 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3791 | if(nEpochDate6 != -1000) { |
| 3792 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3793 | } |
| 3794 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3795 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3796 | return 205; |
| 3797 | } |
| 3798 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3799 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3800 | |
| 3801 | if(nEpochDate3 != 0) { |
| 3802 | return 202; |
| 3803 | } |
| 3804 | |
| 3805 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3806 | return 206; |
| 3807 | } |
| 3808 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3809 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 3810 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3814 | return 208; |
| 3815 | } |
| 3816 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3817 | return 0; |
| 3818 | } |
| 3819 | |
| 3820 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3821 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3822 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3823 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3824 | 0x81, // Array of one |
| 3825 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 3826 | 0x82, // Array of two that is the faction 1/3 |
| 3827 | 0x01, |
| 3828 | 0x03, |
| 3829 | |
| 3830 | /* |
| 3831 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3832 | */ |
| 3833 | 0xd8, 0xe1, |
| 3834 | 0xd8, 0xe2, |
| 3835 | 0xd8, 0xe3, |
| 3836 | 0xd8, 0xe4, |
| 3837 | 0xd8, 0xe5, |
| 3838 | 0x80, |
| 3839 | |
| 3840 | /* tag 10489608748473423768( |
| 3841 | 2442302356( |
| 3842 | 21590( |
| 3843 | 240( |
| 3844 | [])))) |
| 3845 | */ |
| 3846 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3847 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 3848 | 0xd9, 0x54, 0x56, |
| 3849 | 0xd8, 0xf0, |
| 3850 | 0x80, |
| 3851 | |
| 3852 | /* tag 21590( |
| 3853 | 10489608748473423768( |
| 3854 | 2442302357( |
| 3855 | 65534( |
| 3856 | [])))) |
| 3857 | */ |
| 3858 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 3859 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3860 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3861 | 0xd9, 0xff, 0xfe, |
| 3862 | 0x80, |
| 3863 | |
| 3864 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3865 | works in conjuntion with entries above. |
| 3866 | 269488144(269488145(269488146(269488147([])))) |
| 3867 | */ |
| 3868 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 3869 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 3870 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 3871 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 3872 | 0x80, |
| 3873 | |
| 3874 | /* An invalid decimal fraction with an additional tag */ |
| 3875 | 0xd9, 0xff, 0xfa, |
| 3876 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 3877 | 0x00, // the integer 0; should be a byte string |
| 3878 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3879 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3880 | /* |
| 3881 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3882 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3883 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3884 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3885 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3886 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3887 | /* |
| 3888 | DB 9192939495969798 # tag(10489608748473423768) |
| 3889 | D8 88 # tag(136) |
| 3890 | C6 # tag(6) |
| 3891 | C7 # tag(7) |
| 3892 | 80 # array(0) |
| 3893 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3894 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3895 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3896 | |
| 3897 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3898 | 55799(55799(55799({ |
| 3899 | 6(7(-23)): 5859837686836516696(7({ |
| 3900 | 7(-20): 11({ |
| 3901 | 17(-18): 17(17(17("Organization"))), |
| 3902 | 9(-17): 773("SSG"), |
| 3903 | -15: 16(17(6(7("Confusion")))), |
| 3904 | 17(-16): 17("San Diego"), |
| 3905 | 17(-14): 17("US") |
| 3906 | }), |
| 3907 | 23(-19): 19({ |
| 3908 | -11: 9({ |
| 3909 | -9: -7 |
| 3910 | }), |
| 3911 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 3912 | }) |
| 3913 | })), |
| 3914 | 16(-22): 23({ |
| 3915 | 11(8(7(-5))): 8(-3) |
| 3916 | }) |
| 3917 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3918 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3919 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3920 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 3921 | 0xc6, 0xc7, 0x36, |
| 3922 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 3923 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 3924 | 0xcb, 0xa5, |
| 3925 | 0xd1, 0x31, |
| 3926 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 3927 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 3928 | 0xc9, 0x30, |
| 3929 | 0xd9, 0x03, 0x05, 0x63, |
| 3930 | 0x53, 0x53, 0x47, |
| 3931 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3932 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 3933 | 0x69, |
| 3934 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3935 | 0xd1, 0x2f, |
| 3936 | 0xd1, 0x69, |
| 3937 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 3938 | 0xd1, 0x2d, |
| 3939 | 0xd1, 0x62, |
| 3940 | 0x55, 0x53, |
| 3941 | 0xd7, 0x32, |
| 3942 | 0xd3, 0xa2, |
| 3943 | 0x2a, |
| 3944 | 0xc9, 0xa1, |
| 3945 | 0x28, |
| 3946 | 0x26, |
| 3947 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 3948 | 0xcc, 0x4a, |
| 3949 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 3950 | 0xd0, 0x35, |
| 3951 | 0xd7, 0xa1, |
| 3952 | 0xcb, 0xc8, 0xc7, 0x24, |
| 3953 | 0xc8, 0x22}; |
| 3954 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3955 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3956 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3957 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3958 | |
| 3959 | 0xc0, // tag for string date |
| 3960 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3961 | |
| 3962 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3963 | |
| 3964 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 3965 | |
| 3966 | 0xd8, 0x23, // tag for regex |
| 3967 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3968 | |
| 3969 | 0xc0, // tag for string date |
| 3970 | 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] | 3971 | |
| 3972 | // This last case makes the array untraversable because it is |
| 3973 | // an uncrecoverable error. Make sure it stays last and is the only |
| 3974 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3975 | }; |
| 3976 | |
| 3977 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3978 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3979 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3980 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3981 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3982 | { |
| 3983 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3984 | QCBORItem Item; |
| 3985 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3986 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3987 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3988 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3989 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3990 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3991 | /* |
| 3992 | This test matches the magic number tag and the fraction tag |
| 3993 | 55799([...]) |
| 3994 | */ |
| 3995 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3996 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3997 | return -2; |
| 3998 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3999 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4000 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 4001 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4002 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4003 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4004 | /* |
| 4005 | 4([1,3]) |
| 4006 | */ |
| 4007 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4008 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4009 | if(uError != QCBOR_SUCCESS || |
| 4010 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4011 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 4012 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 4013 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4014 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4015 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4016 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 4017 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4018 | return -4; |
| 4019 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4020 | // consume the items in the array |
| 4021 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4022 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4023 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4024 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4025 | if(uError != QCBOR_SUCCESS || |
| 4026 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4027 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 4028 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 4029 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 4030 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 4031 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 4032 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4033 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4034 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4035 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4036 | /* |
| 4037 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 4038 | */ |
| 4039 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4040 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4041 | return -6; |
| 4042 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4043 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 4044 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 4045 | return -106; |
| 4046 | } |
| 4047 | |
| 4048 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4049 | /* tag 10489608748473423768( |
| 4050 | 2442302356( |
| 4051 | 21590( |
| 4052 | 240( |
| 4053 | [])))) |
| 4054 | */ |
| 4055 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4056 | if(uError != QCBOR_SUCCESS || |
| 4057 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4058 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 4059 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 4060 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 4061 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4062 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | /* tag 21590( |
| 4066 | 10489608748473423768( |
| 4067 | 2442302357( |
| 4068 | 21591( |
| 4069 | [])))) |
| 4070 | */ |
| 4071 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4072 | if(uError != QCBOR_SUCCESS || |
| 4073 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4074 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 4075 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 4076 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 4077 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 4078 | return -8; |
| 4079 | } |
| 4080 | |
| 4081 | /* Make sure to blow past the limit of tags that must be mapped. |
| 4082 | works in conjuntion with entries above. |
| 4083 | 269488144(269488145(269488146(269488147([])))) |
| 4084 | */ |
| 4085 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4086 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4087 | return -9; |
| 4088 | } |
| 4089 | |
| 4090 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4091 | if(uError == QCBOR_SUCCESS) { |
| 4092 | return -10; |
| 4093 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4094 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4095 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4096 | // 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] | 4097 | // tage and then matches it. Caller-config lists are no longer |
| 4098 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4099 | QCBORDecode_Init(&DCtx, |
| 4100 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4101 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4102 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 4103 | const QCBORTagListIn TL = {2, puList}; |
| 4104 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4105 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4106 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4107 | return -8; |
| 4108 | } |
| 4109 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4110 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 4111 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 4112 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 4113 | Item.val.uCount != 0) { |
| 4114 | return -9; |
| 4115 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4116 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4117 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4118 | // 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] | 4119 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4120 | const uint64_t puLongList[17] = {1,2,1}; |
| 4121 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4122 | QCBORDecode_Init(&DCtx, |
| 4123 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 4124 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4125 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 4126 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4127 | return -11; |
| 4128 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4129 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4130 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4131 | QCBORTagListOut Out = {0, 4, puTags}; |
| 4132 | |
| 4133 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4134 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4135 | QCBORDecode_Init(&DCtx, |
| 4136 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4137 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4138 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4139 | return -12; |
| 4140 | } |
| 4141 | if(puTags[0] != 0x9192939495969798 || |
| 4142 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4143 | puTags[2] != 0x06 || |
| 4144 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4145 | return -13; |
| 4146 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4147 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4148 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4149 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4150 | QCBORDecode_Init(&DCtx, |
| 4151 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 4152 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4153 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 4154 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 4155 | return -14; |
| 4156 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4157 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4158 | |
| 4159 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4160 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4161 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 4162 | // It is a bit of a messy test and maybe could be improved, but |
| 4163 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4164 | QCBORDecode_Init(&DCtx, |
| 4165 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4166 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4167 | int n = CheckCSRMaps(&DCtx); |
| 4168 | if(n) { |
| 4169 | return n-2000; |
| 4170 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4171 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4172 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4173 | QCBORDecode_Init(&DCtx, |
| 4174 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 4175 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4176 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4177 | /* With the spiffy decode revision, this tag list is not used. |
| 4178 | It doesn't matter if a tag is in this list or not so some |
| 4179 | tests that couldn't process a tag because it isn't in this list |
| 4180 | now can process these unlisted tags. The tests have been |
| 4181 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4182 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 4183 | const QCBORTagListIn TagList = {3, puTagList}; |
| 4184 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4185 | |
| 4186 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4187 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4188 | return -100; |
| 4189 | } |
| 4190 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4191 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4192 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 4193 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 4194 | Item.val.uCount != 2 || |
| 4195 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 4196 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 4197 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 4198 | Out.uNumUsed != 3) { |
| 4199 | return -101; |
| 4200 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4201 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4202 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4203 | return -102; |
| 4204 | } |
| 4205 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4206 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 4207 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4208 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4209 | Item.val.uCount != 2 || |
| 4210 | puTags[0] != 5859837686836516696 || |
| 4211 | puTags[1] != 7 || |
| 4212 | Out.uNumUsed != 2) { |
| 4213 | return -103; |
| 4214 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4215 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4216 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4217 | return -104; |
| 4218 | } |
| 4219 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4220 | Item.val.uCount != 5 || |
| 4221 | puTags[0] != 0x0b || |
| 4222 | Out.uNumUsed != 1) { |
| 4223 | return -105; |
| 4224 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4225 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4226 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4227 | return -106; |
| 4228 | } |
| 4229 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4230 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 4231 | Item.val.string.len != 12 || |
| 4232 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 4233 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 4234 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 4235 | Out.uNumUsed != 3) { |
| 4236 | return -105; |
| 4237 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4238 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4239 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4240 | return -107; |
| 4241 | } |
| 4242 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4243 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 4244 | Item.val.string.len != 3 || |
| 4245 | puTags[0] != 773 || |
| 4246 | Out.uNumUsed != 1) { |
| 4247 | return -108; |
| 4248 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4249 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4250 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4251 | return -109; |
| 4252 | } |
| 4253 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4254 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4255 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4256 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4257 | puTags[3] != 7 || |
| 4258 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4259 | return -110; |
| 4260 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4261 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4262 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4263 | return -111; |
| 4264 | } |
| 4265 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4266 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4267 | Item.val.string.len != 9 || |
| 4268 | puTags[0] != 17 || |
| 4269 | Out.uNumUsed != 1) { |
| 4270 | return -112; |
| 4271 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4272 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4273 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4274 | return -111; |
| 4275 | } |
| 4276 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4277 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 4278 | Item.val.string.len != 2 || |
| 4279 | puTags[0] != 17 || |
| 4280 | Out.uNumUsed != 1) { |
| 4281 | return -112; |
| 4282 | } |
| 4283 | |
| 4284 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4285 | return -113; |
| 4286 | } |
| 4287 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4288 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4289 | Item.val.uCount != 2 || |
| 4290 | puTags[0] != 19 || |
| 4291 | Out.uNumUsed != 1) { |
| 4292 | return -114; |
| 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 -115; |
| 4297 | } |
| 4298 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4299 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4300 | Item.val.uCount != 1 || |
| 4301 | puTags[0] != 9 || |
| 4302 | Out.uNumUsed != 1) { |
| 4303 | return -116; |
| 4304 | } |
| 4305 | |
| 4306 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4307 | return -116; |
| 4308 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4309 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4310 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4311 | Out.uNumUsed != 0) { |
| 4312 | return -117; |
| 4313 | } |
| 4314 | |
| 4315 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4316 | return -118; |
| 4317 | } |
| 4318 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 4319 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4320 | puTags[0] != 12 || |
| 4321 | Out.uNumUsed != 1) { |
| 4322 | return -119; |
| 4323 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4324 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4325 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4326 | return -120; |
| 4327 | } |
| 4328 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 4329 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 4330 | Item.val.uCount != 1 || |
| 4331 | puTags[0] != 0x17 || |
| 4332 | Out.uNumUsed != 1) { |
| 4333 | return -121; |
| 4334 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4335 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4336 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 4337 | return -122; |
| 4338 | } |
| 4339 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4340 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4341 | Item.val.int64 != -3 || |
| 4342 | puTags[0] != 8 || |
| 4343 | Out.uNumUsed != 1) { |
| 4344 | return -123; |
| 4345 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4346 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 4347 | if(QCBORDecode_Finish(&DCtx)) { |
| 4348 | return -124; |
| 4349 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4350 | |
| 4351 | UsefulBufC DateString; |
| 4352 | QCBORDecode_Init(&DCtx, |
| 4353 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4354 | QCBOR_DECODE_MODE_NORMAL); |
| 4355 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4356 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4357 | // tagged date string |
| 4358 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4359 | // untagged date string |
| 4360 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4361 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4362 | return 100; |
| 4363 | } |
| 4364 | // untagged byte string |
| 4365 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4366 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4367 | return 101; |
| 4368 | } |
| 4369 | // tagged regex |
| 4370 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4371 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4372 | return 102; |
| 4373 | } |
| 4374 | // tagged date string with a byte string |
| 4375 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4376 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4377 | return 103; |
| 4378 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4379 | // The exit errors out because the last item, the date string with |
| 4380 | // bad content makes the array untraversable (the bad date string |
| 4381 | // could have tag content of an array or such that is not consumed |
| 4382 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4383 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4384 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4385 | return 104; |
| 4386 | } |
| 4387 | |
| 4388 | |
| 4389 | QCBORDecode_Init(&DCtx, |
| 4390 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4391 | QCBOR_DECODE_MODE_NORMAL); |
| 4392 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4393 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4394 | // tagged date string |
| 4395 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4396 | // untagged date string |
| 4397 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4398 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 4399 | return 200; |
| 4400 | } |
| 4401 | // untagged byte string |
| 4402 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4403 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4404 | return 201; |
| 4405 | } |
| 4406 | // tagged regex |
| 4407 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 4408 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4409 | return 202; |
| 4410 | } |
| 4411 | // tagged date string with a byte string |
| 4412 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4413 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4414 | return 203; |
| 4415 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4416 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4417 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4418 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4419 | return 204; |
| 4420 | } |
| 4421 | |
| 4422 | QCBORDecode_Init(&DCtx, |
| 4423 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 4424 | QCBOR_DECODE_MODE_NORMAL); |
| 4425 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4426 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4427 | // tagged date string |
| 4428 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4429 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4430 | return 300; |
| 4431 | } |
| 4432 | // untagged date string |
| 4433 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4434 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4435 | return 301; |
| 4436 | } |
| 4437 | // untagged byte string |
| 4438 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 4439 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4440 | return 302; |
| 4441 | } |
| 4442 | // tagged regex |
| 4443 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 4444 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4445 | return 303; |
| 4446 | } |
| 4447 | // tagged date string with a byte string |
| 4448 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4449 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4450 | return 304; |
| 4451 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4452 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4453 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 4454 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4455 | return 305; |
| 4456 | } |
| 4457 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4458 | return 0; |
| 4459 | } |
| 4460 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4461 | /* |
| 4462 | * These are showing the big numbers converted to integers. |
| 4463 | * The tag numbers are not shown. |
| 4464 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4465 | * [ |
| 4466 | * 18446744073709551616, |
| 4467 | * -18446744073709551617, |
| 4468 | * { |
| 4469 | * -64: -18446744073709551617, |
| 4470 | * 64: 18446744073709551616, |
| 4471 | * "BN+": 18446744073709551616, |
| 4472 | * "BN-": -18446744073709551617 |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4473 | * } |
| 4474 | * ] |
| 4475 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4476 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4477 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4478 | 0x83, |
| 4479 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4480 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4481 | 0xA4, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4482 | 0x38, 0x3F, |
| 4483 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4484 | 0x18, 0x40, |
| 4485 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4486 | 0x63, 0x42, 0x4E, 0x2B, |
| 4487 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4488 | 0x63, 0x42, 0x4E, 0x2D, |
| 4489 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4490 | }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4491 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4492 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4493 | /* The expected big num */ |
| 4494 | static const uint8_t spBigNum[] = { |
| 4495 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4496 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4497 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4498 | |
| 4499 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4500 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4501 | { |
| 4502 | QCBORDecodeContext DCtx; |
| 4503 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4504 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4505 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4506 | QCBORDecode_Init(&DCtx, |
| 4507 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 4508 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4509 | |
| 4510 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4511 | // |
| 4512 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4513 | return -1; |
| 4514 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4515 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4516 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4517 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4518 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4519 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4520 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4521 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4522 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4523 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4524 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | // |
| 4528 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4529 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4530 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4531 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4532 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4533 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4534 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4535 | // |
| 4536 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4537 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4538 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4539 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4540 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4541 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4542 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4543 | return -15; |
| 4544 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4545 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4546 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4547 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4548 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4552 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4553 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4554 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 4555 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4556 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4557 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4558 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4559 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4560 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 4561 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 4562 | return -9; |
| 4563 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 4564 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4565 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
| 4566 | return -10; |
| 4567 | } |
| 4568 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4569 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4570 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4571 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 4572 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4573 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 4574 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4575 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4576 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4577 | |
| 4578 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4579 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4580 | #else |
| 4581 | |
| 4582 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 4583 | return -100; |
| 4584 | } |
| 4585 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4586 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4587 | return 0; |
| 4588 | } |
| 4589 | |
| 4590 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4591 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4592 | uint8_t uDataType, |
| 4593 | uint8_t uNestingLevel, |
| 4594 | uint8_t uNextNest, |
| 4595 | int64_t nLabel, |
| 4596 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4597 | { |
| 4598 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4599 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4600 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4601 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 4602 | if(Item.uDataType != uDataType) return -1; |
| 4603 | if(uNestingLevel > 0) { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4604 | if(Item.uLabelType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4605 | return -1; |
| 4606 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4607 | if(Item.label.int64 != nLabel) { |
| 4608 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4609 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4610 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4611 | } |
| 4612 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4613 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4614 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4615 | if(pItem) { |
| 4616 | *pItem = Item; |
| 4617 | } |
| 4618 | return 0; |
| 4619 | } |
| 4620 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4621 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4622 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4623 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4624 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4625 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4626 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4627 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4628 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4629 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4630 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 4631 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 4632 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 4633 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 4634 | 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] | 4635 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4636 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 4637 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4638 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4639 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 4640 | 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] | 4641 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4642 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 4643 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4644 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4645 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4646 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4647 | return 0; |
| 4648 | } |
| 4649 | |
| 4650 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4651 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4652 | { |
| 4653 | -23: { |
| 4654 | -20: { |
| 4655 | -18: "Organization", |
| 4656 | -17: "SSG", |
| 4657 | -15: "Confusion", |
| 4658 | -16: "San Diego", |
| 4659 | -14: "US" |
| 4660 | }, |
| 4661 | -19: { |
| 4662 | -11: { |
| 4663 | -9: -7 |
| 4664 | }, |
| 4665 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 4666 | } |
| 4667 | }, |
| 4668 | -22: { |
| 4669 | -5: -3 |
| 4670 | } |
| 4671 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4672 | */ |
| 4673 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4674 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 4675 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4676 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4677 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4678 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4679 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4680 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 4681 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 4682 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 4683 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4684 | // Same map as above, but using indefinite lengths |
| 4685 | static const uint8_t spCSRInputIndefLen[] = { |
| 4686 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 4687 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4688 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4689 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4690 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4691 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4692 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 4693 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 4694 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 4695 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 4696 | |
| 4697 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4698 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4699 | { |
| 4700 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4701 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4702 | QCBORDecode_Init(&DCtx, |
| 4703 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4704 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4705 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4706 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4707 | } |
| 4708 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4709 | |
| 4710 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4711 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4712 | { |
| 4713 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4714 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4715 | QCBORDecode_Init(&DCtx, |
| 4716 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4717 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4718 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4719 | QCBORItem Item; |
| 4720 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4721 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4722 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4723 | return -1; |
| 4724 | } |
| 4725 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4726 | return -2; |
| 4727 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4728 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4729 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4730 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 4731 | return -3; |
| 4732 | } |
| 4733 | |
| 4734 | return 0; |
| 4735 | } |
| 4736 | |
| 4737 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4738 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4739 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4740 | { |
| 4741 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4742 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4743 | QCBORDecode_Init(&DCtx, |
| 4744 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 4745 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4746 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4747 | return CheckCSRMaps(&DCtx); |
| 4748 | } |
| 4749 | |
| 4750 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4751 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4752 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 4753 | { |
| 4754 | UsefulOutBuf UOB; |
| 4755 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4756 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4757 | int i; |
| 4758 | for(i = 0; i < n; i++) { |
| 4759 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 4760 | } |
| 4761 | |
| 4762 | for(i = 0; i < n; i++) { |
| 4763 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4764 | } |
| 4765 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4766 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4767 | |
| 4768 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4769 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4770 | { |
| 4771 | QCBORDecodeContext DC; |
| 4772 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4773 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4774 | int j; |
| 4775 | for(j = 0; j < nNestLevel; j++) { |
| 4776 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4777 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4778 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 4779 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4780 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4781 | return -4; |
| 4782 | } else { |
| 4783 | return 0; // Decoding doesn't recover after an error |
| 4784 | } |
| 4785 | } else { |
| 4786 | // Should be no error |
| 4787 | if(nReturn) { |
| 4788 | return -9; // Should not have got an error |
| 4789 | } |
| 4790 | } |
| 4791 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4792 | return -7; |
| 4793 | } |
| 4794 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4795 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4796 | if(nReturn) { |
| 4797 | return -3; |
| 4798 | } |
| 4799 | return 0; |
| 4800 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4801 | |
| 4802 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4803 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4804 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4805 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4806 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4807 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4808 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4809 | int nReturn = parse_indeflen_nested(Nested, i); |
| 4810 | if(nReturn) { |
| 4811 | return nReturn; |
| 4812 | } |
| 4813 | } |
| 4814 | return 0; |
| 4815 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4816 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4817 | // [1, [2, 3]] |
| 4818 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 4819 | // No closing break |
| 4820 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 4821 | // Not enough closing breaks |
| 4822 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 4823 | // Too many closing breaks |
| 4824 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 4825 | // Unclosed indeflen inside def len |
| 4826 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 4827 | // confused tag |
| 4828 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4829 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4830 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4831 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4832 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4833 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4834 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4835 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4836 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4837 | QCBORDecodeContext DC; |
| 4838 | QCBORItem Item; |
| 4839 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4840 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4841 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4842 | |
| 4843 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4844 | Item.uNestingLevel != 0 || |
| 4845 | Item.uNextNestLevel != 1) { |
| 4846 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4847 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4848 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4849 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4850 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 4851 | Item.uNestingLevel != 1 || |
| 4852 | Item.uNextNestLevel != 1) { |
| 4853 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4854 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4855 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4856 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4857 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4858 | Item.uNestingLevel != 1 || |
| 4859 | Item.uNextNestLevel != 2) { |
| 4860 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4861 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4862 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4863 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4864 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4865 | Item.uNestingLevel != 2 || |
| 4866 | Item.uNextNestLevel != 2) { |
| 4867 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4868 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4869 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4870 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4871 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4872 | Item.uNestingLevel != 2 || |
| 4873 | Item.uNextNestLevel != 0) { |
| 4874 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4875 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4876 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4877 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4878 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4879 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4880 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4881 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4882 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4883 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4884 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4885 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4886 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4887 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4888 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4889 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4890 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4891 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4892 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4893 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4894 | } |
| 4895 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4896 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4897 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4898 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4899 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4900 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4901 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4902 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4903 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4904 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4908 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4909 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4910 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4911 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4912 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4913 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4914 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4915 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4916 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4917 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4918 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4919 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4920 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4921 | |
| 4922 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4923 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4924 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4925 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4926 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4927 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4928 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4929 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4930 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4931 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4932 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4933 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4934 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4935 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4936 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4937 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4938 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4939 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4940 | return -140; |
| 4941 | } |
| 4942 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4943 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4944 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4945 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4946 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4947 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4948 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4949 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4950 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4951 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4952 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4953 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4954 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4955 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4956 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4957 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4958 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4959 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4960 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4961 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4962 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4963 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4964 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4965 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4966 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4967 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4968 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4969 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4970 | |
| 4971 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4972 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4973 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4974 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4975 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4976 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4977 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4978 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4979 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4980 | #else /* QCBOR_DISABLE_TAGS */ |
| 4981 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 4982 | return -20; |
| 4983 | } |
| 4984 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4985 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4986 | return 0; |
| 4987 | } |
| 4988 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4989 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4990 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 4991 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4992 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4993 | 0x81, // Array of length one |
| 4994 | 0x7f, // text string marked with indefinite length |
| 4995 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4996 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4997 | 0xff // ending break |
| 4998 | }; |
| 4999 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5000 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5001 | 0x81, // Array of length one |
| 5002 | 0x7f, // text string marked with indefinite length |
| 5003 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5004 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 5005 | 0xff // ending break |
| 5006 | }; |
| 5007 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5008 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5009 | 0x81, // Array of length one |
| 5010 | 0x7f, // text string marked with indefinite length |
| 5011 | 0x01, 0x02, // Not a string |
| 5012 | 0xff // ending break |
| 5013 | }; |
| 5014 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5015 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5016 | 0x81, // Array of length one |
| 5017 | 0x7f, // text string marked with indefinite length |
| 5018 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 5019 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5020 | // missing end of string |
| 5021 | }; |
| 5022 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5023 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5024 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5025 | 0xa1, // Array of length one |
| 5026 | 0x7f, // text string marked with indefinite length |
| 5027 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 5028 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 5029 | 0xff, // ending break |
| 5030 | 0x01 // integer being labeled. |
| 5031 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5032 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5033 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5034 | /** |
| 5035 | Make an indefinite length string |
| 5036 | |
| 5037 | @param Storage Storage for string, must be 144 bytes in size |
| 5038 | @return The indefinite length string |
| 5039 | |
| 5040 | This makes an array with one indefinite length string that has 7 chunks |
| 5041 | from size of 1 byte up to 64 bytes. |
| 5042 | */ |
| 5043 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5044 | { |
| 5045 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5046 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5047 | UsefulOutBuf_Init(&UOB, Storage); |
| 5048 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 5049 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5050 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5051 | uint8_t uStringByte = 0; |
| 5052 | // Use of type int is intentional |
| 5053 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 5054 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5055 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5056 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 5057 | for(int j = 0; j < uChunkSize; j++) { |
| 5058 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 5059 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5060 | } |
| 5061 | } |
| 5062 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 5063 | |
| 5064 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5065 | } |
| 5066 | |
| 5067 | static int CheckBigString(UsefulBufC BigString) |
| 5068 | { |
| 5069 | if(BigString.len != 255) { |
| 5070 | return 1; |
| 5071 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5072 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5073 | for(uint8_t i = 0; i < 255; i++){ |
| 5074 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 5075 | return 1; |
| 5076 | } |
| 5077 | } |
| 5078 | return 0; |
| 5079 | } |
| 5080 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5081 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5082 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5083 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5084 | QCBORDecodeContext DC; |
| 5085 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5086 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5087 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5088 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5089 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5090 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5091 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5092 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5093 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5094 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5095 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5096 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5097 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5098 | return -2; |
| 5099 | } |
| 5100 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 5101 | return -3; |
| 5102 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5103 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5104 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5105 | return -4; |
| 5106 | } |
| 5107 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 5108 | return -5; |
| 5109 | } |
| 5110 | if(QCBORDecode_Finish(&DC)) { |
| 5111 | return -6; |
| 5112 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5113 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5114 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5115 | QCBORDecode_Init(&DC, |
| 5116 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 5117 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5118 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5119 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5120 | return -7; |
| 5121 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5122 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5123 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5124 | return -8; |
| 5125 | } |
| 5126 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5127 | return -9; |
| 5128 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5129 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5130 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5131 | return -10; |
| 5132 | } |
| 5133 | |
| 5134 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5135 | QCBORDecode_Init(&DC, |
| 5136 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 5137 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5138 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5139 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5140 | return -11; |
| 5141 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5142 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5143 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5144 | return -12; |
| 5145 | } |
| 5146 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5147 | return -13; |
| 5148 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5149 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5150 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5151 | return -14; |
| 5152 | } |
| 5153 | |
| 5154 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5155 | QCBORDecode_Init(&DC, |
| 5156 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 5157 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5158 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5159 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5160 | return -15; |
| 5161 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5162 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5163 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5164 | return -16; |
| 5165 | } |
| 5166 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 5167 | return -17; |
| 5168 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5169 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5170 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 5171 | return -18; |
| 5172 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5173 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5174 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5175 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5176 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5177 | QCBORDecode_GetNext(&DC, &Item); |
| 5178 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5179 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5180 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5181 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5182 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5183 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5184 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5185 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5186 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5187 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5188 | |
| 5189 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 5190 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5191 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5192 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5193 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5194 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 5195 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 5196 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5197 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5198 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 5199 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5200 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5201 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5202 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5203 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5204 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5205 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5206 | QCBORDecode_GetNext(&DC, &Item); |
| 5207 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5208 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5209 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5210 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5211 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 5212 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5213 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5214 | // ---- big bstr ----- |
| 5215 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5216 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5217 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5218 | return -25; |
| 5219 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5220 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5221 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5222 | return -26; |
| 5223 | } |
| 5224 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5225 | return -26; |
| 5226 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5227 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5228 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 5229 | return -27; |
| 5230 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 5231 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5232 | return -28; |
| 5233 | } |
| 5234 | if(CheckBigString(Item.val.string)) { |
| 5235 | return -3; |
| 5236 | } |
| 5237 | if(QCBORDecode_Finish(&DC)) { |
| 5238 | return -29; |
| 5239 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5240 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5241 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5242 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 5243 | 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] | 5244 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5245 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 5246 | return -30; |
| 5247 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5248 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5249 | QCBORDecode_GetNext(&DC, &Item); |
| 5250 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 5251 | return -31; |
| 5252 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5253 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5254 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 5255 | return -32; |
| 5256 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5257 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5258 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5259 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 5260 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 5261 | return -33; |
| 5262 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5263 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 5264 | if(QCBORDecode_Finish(&DC)) { |
| 5265 | return -34; |
| 5266 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5267 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5268 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5269 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5270 | } |
| 5271 | |
| 5272 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5273 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5274 | { |
| 5275 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5276 | QCBORError nCBORError; |
| 5277 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5278 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5279 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5280 | QCBORDecode_Init(&DC, |
| 5281 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 5282 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5283 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5284 | 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] | 5285 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5286 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 5287 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5288 | return -1; |
| 5289 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5290 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5291 | if(CheckCSRMaps(&DC)) { |
| 5292 | return -2; |
| 5293 | } |
| 5294 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5295 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5296 | // Next parse, save pointers to a few strings, destroy original and |
| 5297 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5298 | 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] | 5299 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 5300 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5301 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 5302 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5303 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5304 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5305 | QCBORItem Item1, Item2, Item3, Item4; |
| 5306 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5307 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5308 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5309 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5310 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5311 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5312 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5313 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5314 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5315 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5316 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5317 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 5318 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5319 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 5320 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5321 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5322 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5323 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 5324 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5325 | Item1.uDataAlloc != 0 || |
| 5326 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5327 | UsefulBufCompareToSZ(Item1.label.string, "first integer") || |
| 5328 | Item1.label.string.ptr < Pool.ptr || |
| 5329 | 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] | 5330 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5331 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5332 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5333 | |
| 5334 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5335 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5336 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5337 | Item2.uDataAlloc != 0 || |
| 5338 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5339 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5340 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5341 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5342 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5343 | Item3.uDataAlloc == 0 || |
| 5344 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5345 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5346 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5347 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5348 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5349 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5350 | Item4.uDataAlloc == 0 || |
| 5351 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5352 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5353 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5354 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5355 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5356 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5357 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 5358 | QCBORDecode_Init(&DC, |
| 5359 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 5360 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5361 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 5362 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5363 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5364 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5365 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5366 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 5367 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5368 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 5369 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 5370 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 5371 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 5372 | } |
| 5373 | } |
| 5374 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 5375 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5376 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5377 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 5378 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 5379 | |
| 5380 | return 0; |
| 5381 | } |
| 5382 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5383 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5384 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 5385 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5386 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5387 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5388 | QCBORDecodeContext DC; |
| 5389 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 5390 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5391 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5392 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5393 | // Then fish into the internals of the decode context |
| 5394 | // to get the allocator function so it can be called directly. |
| 5395 | // Also figure out how much pool is available for use |
| 5396 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5397 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 5398 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 5399 | if(nError) { |
| 5400 | return -9; |
| 5401 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5402 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 5403 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5404 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5405 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5406 | // First test -- ask for one more byte than available and see failure |
| 5407 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5408 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 5409 | return -1; |
| 5410 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5411 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5412 | // Re do the set up for the next test that will do a successful alloc, |
| 5413 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5414 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5415 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5416 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 5417 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5418 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5419 | // Allocate one byte less than available and see success |
| 5420 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5421 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5422 | return -2; |
| 5423 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5424 | // Ask for some more and see failure |
| 5425 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5426 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 5427 | return -3; |
| 5428 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5429 | // Free the first allocate, retry the second and see success |
| 5430 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 5431 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5432 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 5433 | return -4; |
| 5434 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5435 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5436 | // Re do set up for next test that involves a successful alloc, |
| 5437 | // and a successful realloc and a failed realloc |
| 5438 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5439 | pAlloc = DC.StringAllocator.pfAllocator; |
| 5440 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5441 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5442 | // Allocate half the pool and see success |
| 5443 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5444 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 5445 | return -5; |
| 5446 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5447 | // Reallocate to take up the whole pool and see success |
| 5448 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5449 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 5450 | return -6; |
| 5451 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5452 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5453 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 5454 | return -7; |
| 5455 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5456 | // Try to allocate more to be sure there is failure after a realloc |
| 5457 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 5458 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5459 | return -8; |
| 5460 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5461 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 5462 | return 0; |
| 5463 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 5464 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5465 | |
| 5466 | /* Just enough of an allocator to test configuration of one */ |
| 5467 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 5468 | { |
| 5469 | (void)pOldMem; // unused variable |
| 5470 | |
| 5471 | if(uNewSize) { |
| 5472 | // Assumes the context pointer is the buffer and |
| 5473 | // nothing too big will ever be asked for. |
| 5474 | // This is only good for this basic test! |
| 5475 | return (UsefulBuf) {pCtx, uNewSize}; |
| 5476 | } else { |
| 5477 | return NULLUsefulBuf; |
| 5478 | } |
| 5479 | } |
| 5480 | |
| 5481 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 5482 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5483 | { |
| 5484 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 5485 | // nothing can be done with it unless that is set up. |
| 5486 | QCBORDecodeContext DC; |
| 5487 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 5488 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 5489 | |
| 5490 | uint8_t pAllocatorBuffer[50]; |
| 5491 | |
| 5492 | // This is really just to test that this call works. |
| 5493 | // The full functionality of string allocators is tested |
| 5494 | // elsewhere with the MemPool internal allocator. |
| 5495 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 5496 | |
| 5497 | QCBORItem Item; |
| 5498 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 5499 | return -1; |
| 5500 | } |
| 5501 | |
| 5502 | if(Item.uDataAlloc == 0 || |
| 5503 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 5504 | Item.val.string.ptr != pAllocatorBuffer) { |
| 5505 | return -2; |
| 5506 | } |
| 5507 | |
| 5508 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 5509 | return -3; |
| 5510 | } |
| 5511 | |
| 5512 | return 0; |
| 5513 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5514 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5515 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 5516 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5517 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5518 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5519 | struct EaMTest { |
| 5520 | const char *szName; |
| 5521 | UsefulBufC Input; |
| 5522 | uint8_t uTagRequirement; |
| 5523 | bool bHasTags; |
| 5524 | |
| 5525 | /* Expected values for GetNext */ |
| 5526 | QCBORError uExpectedErrorGN; |
| 5527 | uint8_t uQCBORTypeGN; |
| 5528 | int64_t nExponentGN; |
| 5529 | int64_t nMantissaGN; |
| 5530 | UsefulBufC MantissaGN; |
| 5531 | |
| 5532 | /* Expected values for GetDecimalFraction */ |
| 5533 | QCBORError uExpectedErrorGDF; |
| 5534 | int64_t nExponentGDF; |
| 5535 | int64_t nMantissaGDF; |
| 5536 | |
| 5537 | /* Expected values for GetDecimalFractionBig */ |
| 5538 | QCBORError uExpectedErrorGDFB; |
| 5539 | int64_t nExponentGDFB; |
| 5540 | UsefulBufC MantissaGDFB; |
| 5541 | bool IsNegativeGDFB; |
| 5542 | |
| 5543 | /* Expected values for GetBigFloat */ |
| 5544 | QCBORError uExpectedErrorGBF; |
| 5545 | int64_t nExponentGBF; |
| 5546 | int64_t nMantissaGBF; |
| 5547 | |
| 5548 | /* Expected values for GetBigFloatBig */ |
| 5549 | QCBORError uExpectedErrorGBFB; |
| 5550 | int64_t nExponentGBFB; |
| 5551 | UsefulBufC MantissaGBFB; |
| 5552 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5553 | }; |
| 5554 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5555 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5556 | |
| 5557 | static const struct EaMTest pEaMTests[] = { |
| 5558 | { |
| 5559 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 5560 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5561 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 5562 | false, |
| 5563 | |
| 5564 | QCBOR_SUCCESS, /* for GetNext */ |
| 5565 | QCBOR_TYPE_ARRAY, |
| 5566 | 0, |
| 5567 | 0, |
| 5568 | {(const uint8_t []){0x00}, 1}, |
| 5569 | |
| 5570 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 5571 | -1, |
| 5572 | 3, |
| 5573 | |
| 5574 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5575 | -1, |
| 5576 | {(const uint8_t []){0x03}, 1}, |
| 5577 | false, |
| 5578 | |
| 5579 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5580 | -1, |
| 5581 | 3, |
| 5582 | |
| 5583 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5584 | -1, |
| 5585 | {(const uint8_t []){0x03}, 1}, |
| 5586 | false |
| 5587 | }, |
| 5588 | |
| 5589 | { |
| 5590 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 5591 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 5592 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5593 | false, |
| 5594 | |
| 5595 | QCBOR_SUCCESS, /* for GetNext */ |
| 5596 | QCBOR_TYPE_ARRAY, |
| 5597 | 0, |
| 5598 | 0, |
| 5599 | {(const uint8_t []){0x00}, 1}, |
| 5600 | |
| 5601 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5602 | 0, |
| 5603 | 0, |
| 5604 | |
| 5605 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5606 | 0, |
| 5607 | {(const uint8_t []){0x00}, 1}, |
| 5608 | false, |
| 5609 | |
| 5610 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5611 | 0, |
| 5612 | 0, |
| 5613 | |
| 5614 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5615 | 0, |
| 5616 | {(const uint8_t []){0x00}, 1}, |
| 5617 | false |
| 5618 | |
| 5619 | }, |
| 5620 | |
| 5621 | { |
| 5622 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 5623 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 5624 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5625 | true, |
| 5626 | |
| 5627 | QCBOR_SUCCESS, /* for GetNext */ |
| 5628 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 5629 | -1, |
| 5630 | 3, |
| 5631 | {(const uint8_t []){0x00}, 1}, |
| 5632 | |
| 5633 | |
| 5634 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 5635 | -1, |
| 5636 | 3, |
| 5637 | |
| 5638 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5639 | -1, |
| 5640 | {(const uint8_t []){0x03}, 1}, |
| 5641 | false, |
| 5642 | |
| 5643 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5644 | 0, |
| 5645 | 0, |
| 5646 | |
| 5647 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5648 | 0, |
| 5649 | {(const uint8_t []){0x00}, 1}, |
| 5650 | false |
| 5651 | }, |
| 5652 | { |
| 5653 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 5654 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 5655 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5656 | true, |
| 5657 | |
| 5658 | QCBOR_SUCCESS, /* for GetNext */ |
| 5659 | QCBOR_TYPE_BIGFLOAT, |
| 5660 | 300, |
| 5661 | 100, |
| 5662 | {(const uint8_t []){0x00}, 1}, |
| 5663 | |
| 5664 | |
| 5665 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5666 | 0, |
| 5667 | 0, |
| 5668 | |
| 5669 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5670 | 0, |
| 5671 | {(const uint8_t []){0x03}, 1}, |
| 5672 | false, |
| 5673 | |
| 5674 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5675 | 300, |
| 5676 | 100, |
| 5677 | |
| 5678 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5679 | 300, |
| 5680 | {(const uint8_t []){0x64}, 1}, |
| 5681 | false |
| 5682 | }, |
| 5683 | |
| 5684 | { |
| 5685 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 5686 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5687 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 5688 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5689 | true, |
| 5690 | |
| 5691 | QCBOR_SUCCESS, /* for GetNext */ |
| 5692 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5693 | -20, |
| 5694 | 0, |
| 5695 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5696 | |
| 5697 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 5698 | 0, |
| 5699 | 0, |
| 5700 | |
| 5701 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5702 | -20, |
| 5703 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5704 | false, |
| 5705 | |
| 5706 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5707 | 0, |
| 5708 | 0, |
| 5709 | |
| 5710 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5711 | 0, |
| 5712 | {(const uint8_t []){0x00}, 0}, |
| 5713 | false |
| 5714 | }, |
| 5715 | |
| 5716 | { |
| 5717 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 5718 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5719 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 5720 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5721 | true, |
| 5722 | |
| 5723 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 5724 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5725 | 0, |
| 5726 | 0, |
| 5727 | {(const uint8_t []){0x00}, 0}, |
| 5728 | |
| 5729 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 5730 | 0, |
| 5731 | 0, |
| 5732 | |
| 5733 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 5734 | 0, |
| 5735 | {(const uint8_t []){0x00}, 0}, |
| 5736 | false, |
| 5737 | |
| 5738 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 5739 | 0, |
| 5740 | 0, |
| 5741 | |
| 5742 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 5743 | 0, |
| 5744 | {(const uint8_t []){0x00}, 0}, |
| 5745 | false |
| 5746 | }, |
| 5747 | { |
| 5748 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 5749 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 5750 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5751 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5752 | true, |
| 5753 | |
| 5754 | QCBOR_SUCCESS, /* for GetNext */ |
| 5755 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5756 | -20, |
| 5757 | 0, |
| 5758 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5759 | |
| 5760 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5761 | 0, |
| 5762 | 0, |
| 5763 | |
| 5764 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5765 | -20, |
| 5766 | {(const uint8_t []){0x00}, 1}, |
| 5767 | false, |
| 5768 | |
| 5769 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5770 | -20, |
| 5771 | 4294967295, |
| 5772 | |
| 5773 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5774 | -20, |
| 5775 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5776 | false |
| 5777 | }, |
| 5778 | |
| 5779 | { |
| 5780 | /* Special case for test 8. Don't renumber it. */ |
| 5781 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 5782 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 5783 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5784 | true, |
| 5785 | |
| 5786 | QCBOR_SUCCESS, /* for GetNext */ |
| 5787 | QCBOR_TYPE_ARRAY, |
| 5788 | 0, |
| 5789 | 0, |
| 5790 | {(const uint8_t []){0x00}, 1}, |
| 5791 | |
| 5792 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5793 | 0, |
| 5794 | 0, |
| 5795 | |
| 5796 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5797 | -20, |
| 5798 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5799 | false, |
| 5800 | |
| 5801 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 5802 | 0, |
| 5803 | 0, |
| 5804 | |
| 5805 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5806 | -20, |
| 5807 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5808 | false |
| 5809 | }, |
| 5810 | |
| 5811 | { |
| 5812 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 5813 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5814 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 5815 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5816 | true, |
| 5817 | |
| 5818 | QCBOR_SUCCESS, /* for GetNext */ |
| 5819 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 5820 | 9223372036854775807, |
| 5821 | 0, |
| 5822 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5823 | |
| 5824 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5825 | 0, |
| 5826 | 0, |
| 5827 | |
| 5828 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5829 | 9223372036854775807, |
| 5830 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5831 | true, |
| 5832 | |
| 5833 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5834 | 0, |
| 5835 | 0, |
| 5836 | |
| 5837 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5838 | 0, |
| 5839 | {(const uint8_t []){0x00}, 1}, |
| 5840 | false |
| 5841 | }, |
| 5842 | }; |
| 5843 | |
| 5844 | |
| 5845 | |
| 5846 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5847 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5848 | size_t uIndex; |
| 5849 | QCBORDecodeContext DCtx; |
| 5850 | QCBORItem Item; |
| 5851 | QCBORError uError; |
| 5852 | int64_t nMantissa, nExponent; |
| 5853 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 5854 | UsefulBufC Mantissa; |
| 5855 | bool bMantissaIsNegative; |
| 5856 | |
| 5857 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 5858 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 5859 | /* Decode with GetNext */ |
| 5860 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5861 | |
| 5862 | if(uIndex + 1 == 9) { |
| 5863 | nExponent = 99; // just to set a break point |
| 5864 | } |
| 5865 | |
| 5866 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 5867 | #ifdef QCBOR_DISABLE_TAGS |
| 5868 | /* Test 8 is a special case when tags are disabled */ |
| 5869 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 5870 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5871 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 5872 | } |
| 5873 | } else { |
| 5874 | #endif |
| 5875 | /* Now check return code, data type, mantissa and exponent */ |
| 5876 | if(pT->uExpectedErrorGN != uError) { |
| 5877 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 5878 | } |
| 5879 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 5880 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 5881 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 5882 | } |
| 5883 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 5884 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 5885 | } |
| 5886 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 5887 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 5888 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 5889 | } |
| 5890 | } else { |
| 5891 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 5892 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 5893 | } |
| 5894 | } |
| 5895 | } |
| 5896 | #ifdef QCBOR_DISABLE_TAGS |
| 5897 | } |
| 5898 | #endif |
| 5899 | |
| 5900 | /* Decode with GetDecimalFraction */ |
| 5901 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5902 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 5903 | pT->uTagRequirement, |
| 5904 | &nMantissa, |
| 5905 | &nExponent); |
| 5906 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5907 | #ifdef QCBOR_DISABLE_TAGS |
| 5908 | if(pT->bHasTags) { |
| 5909 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5910 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 5911 | } |
| 5912 | } else { |
| 5913 | #endif |
| 5914 | /* Now check return code, mantissa and exponent */ |
| 5915 | if(pT->uExpectedErrorGDF != uError) { |
| 5916 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 5917 | } |
| 5918 | if(uError == QCBOR_SUCCESS) { |
| 5919 | if(pT->nExponentGDF != nExponent) { |
| 5920 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 5921 | } |
| 5922 | if(pT->nMantissaGDF != nMantissa) { |
| 5923 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 5924 | } |
| 5925 | } |
| 5926 | #ifdef QCBOR_DISABLE_TAGS |
| 5927 | } |
| 5928 | #endif |
| 5929 | |
| 5930 | /* Decode with GetDecimalFractionBig */ |
| 5931 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5932 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 5933 | pT->uTagRequirement, |
| 5934 | MantissaBuf, |
| 5935 | &Mantissa, |
| 5936 | &bMantissaIsNegative, |
| 5937 | &nExponent); |
| 5938 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5939 | #ifdef QCBOR_DISABLE_TAGS |
| 5940 | if(pT->bHasTags) { |
| 5941 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5942 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 5943 | } |
| 5944 | } else { |
| 5945 | #endif |
| 5946 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5947 | if(pT->uExpectedErrorGDFB != uError) { |
| 5948 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 5949 | } |
| 5950 | if(uError == QCBOR_SUCCESS) { |
| 5951 | if(pT->nExponentGDFB != nExponent) { |
| 5952 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 5953 | } |
| 5954 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 5955 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 5956 | } |
| 5957 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 5958 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 5959 | } |
| 5960 | } |
| 5961 | #ifdef QCBOR_DISABLE_TAGS |
| 5962 | } |
| 5963 | #endif |
| 5964 | |
| 5965 | /* Decode with GetBigFloat */ |
| 5966 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5967 | QCBORDecode_GetBigFloat(&DCtx, |
| 5968 | pT->uTagRequirement, |
| 5969 | &nMantissa, |
| 5970 | &nExponent); |
| 5971 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5972 | #ifdef QCBOR_DISABLE_TAGS |
| 5973 | if(pT->bHasTags) { |
| 5974 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5975 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 5976 | } |
| 5977 | } else { |
| 5978 | #endif |
| 5979 | /* Now check return code, mantissa and exponent */ |
| 5980 | if(pT->uExpectedErrorGBF != uError) { |
| 5981 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 5982 | } |
| 5983 | if(uError == QCBOR_SUCCESS) { |
| 5984 | if(pT->nExponentGBF != nExponent) { |
| 5985 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 5986 | } |
| 5987 | if(pT->nMantissaGBF != nMantissa) { |
| 5988 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 5989 | } |
| 5990 | } |
| 5991 | #ifdef QCBOR_DISABLE_TAGS |
| 5992 | } |
| 5993 | #endif |
| 5994 | |
| 5995 | /* Decode with GetBigFloatBig */ |
| 5996 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5997 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 5998 | pT->uTagRequirement, |
| 5999 | MantissaBuf, |
| 6000 | &Mantissa, |
| 6001 | &bMantissaIsNegative, |
| 6002 | &nExponent); |
| 6003 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 6004 | #ifdef QCBOR_DISABLE_TAGS |
| 6005 | if(pT->bHasTags) { |
| 6006 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 6007 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 6008 | } |
| 6009 | } else { |
| 6010 | #endif |
| 6011 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 6012 | if(pT->uExpectedErrorGBFB != uError) { |
| 6013 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 6014 | } |
| 6015 | if(uError == QCBOR_SUCCESS) { |
| 6016 | if(pT->nExponentGBFB != nExponent) { |
| 6017 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 6018 | } |
| 6019 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 6020 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 6021 | } |
| 6022 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 6023 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 6024 | } |
| 6025 | } |
| 6026 | #ifdef QCBOR_DISABLE_TAGS |
| 6027 | } |
| 6028 | #endif |
| 6029 | } |
| 6030 | |
| 6031 | return 0; |
| 6032 | } |
| 6033 | |
| 6034 | |
| 6035 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 6036 | { |
| 6037 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6038 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6039 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6040 | QCBORItem item; |
| 6041 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 6042 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 6043 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 6044 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6045 | |
| 6046 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6047 | |
| 6048 | /* Now encode some stuff and then decode it */ |
| 6049 | uint8_t pBuf[40]; |
| 6050 | QCBOREncodeContext EC; |
| 6051 | UsefulBufC Encoded; |
| 6052 | |
| 6053 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 6054 | QCBOREncode_OpenArray(&EC); |
| 6055 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 6056 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 6057 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 6058 | QCBOREncode_CloseArray(&EC); |
| 6059 | QCBOREncode_Finish(&EC, &Encoded); |
| 6060 | |
| 6061 | |
| 6062 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6063 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6064 | if(uErr != QCBOR_SUCCESS) { |
| 6065 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6066 | } |
| 6067 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6068 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6069 | if(uErr != QCBOR_SUCCESS) { |
| 6070 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6071 | } |
| 6072 | |
| 6073 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 6074 | item.val.expAndMantissa.nExponent != 1000 || |
| 6075 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6076 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6077 | } |
| 6078 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6079 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6080 | if(uErr != QCBOR_SUCCESS) { |
| 6081 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6082 | } |
| 6083 | |
| 6084 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 6085 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 6086 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6087 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6088 | } |
| 6089 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6090 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 6091 | if(uErr != QCBOR_SUCCESS) { |
| 6092 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6093 | } |
| 6094 | |
| 6095 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 6096 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 6097 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6098 | return 106; |
| 6099 | } |
| 6100 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6101 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6102 | |
| 6103 | return 0; |
| 6104 | } |
| 6105 | |
| 6106 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6107 | int32_t ExponentAndMantissaDecodeTests(void) |
| 6108 | { |
| 6109 | int32_t rv = ProcessEaMTests(); |
| 6110 | if(rv) { |
| 6111 | return rv; |
| 6112 | } |
| 6113 | |
| 6114 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 6115 | } |
| 6116 | |
| 6117 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6118 | static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = { |
| 6119 | { "Exponent > INT64_MAX", |
| 6120 | QCBOR_DECODE_MODE_NORMAL, |
| 6121 | {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20}, |
| 6122 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6123 | }, |
| 6124 | { "Mantissa > INT64_MAX", |
| 6125 | QCBOR_DECODE_MODE_NORMAL, |
| 6126 | {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23}, |
| 6127 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6128 | }, |
| 6129 | { |
| 6130 | "End of input", |
| 6131 | QCBOR_DECODE_MODE_NORMAL, |
| 6132 | {"\xC4\x82", 2}, |
| 6133 | QCBOR_ERR_NO_MORE_ITEMS |
| 6134 | }, |
| 6135 | {"bad content for big num", |
| 6136 | QCBOR_DECODE_MODE_NORMAL, |
| 6137 | {"\xC4\x82\x01\xc3\x01", 5}, |
| 6138 | QCBOR_ERR_BAD_OPT_TAG |
| 6139 | }, |
| 6140 | {"bad content for big num", |
| 6141 | QCBOR_DECODE_MODE_NORMAL, |
| 6142 | {"\xC4\x82\xc2\x01\x1f", 5}, |
| 6143 | QCBOR_ERR_BAD_INT |
| 6144 | }, |
| 6145 | {"Bad integer for exponent", |
| 6146 | QCBOR_DECODE_MODE_NORMAL, |
| 6147 | {"\xC4\x82\x01\x1f", 4}, |
| 6148 | QCBOR_ERR_BAD_INT |
| 6149 | }, |
| 6150 | {"Bad integer for mantissa", |
| 6151 | QCBOR_DECODE_MODE_NORMAL, |
| 6152 | {"\xC4\x82\x1f\x01", 4}, |
| 6153 | QCBOR_ERR_BAD_INT |
| 6154 | }, |
| 6155 | {"3 items in array", |
| 6156 | QCBOR_DECODE_MODE_NORMAL, |
| 6157 | {"\xC4\x83\x03\x01\x02", 5}, |
| 6158 | QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6159 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6160 | {"unterminated indefinite length array", |
| 6161 | QCBOR_DECODE_MODE_NORMAL, |
| 6162 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6163 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6164 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6165 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6166 | {"unterminated indefinite length array", |
| 6167 | QCBOR_DECODE_MODE_NORMAL, |
| 6168 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 6169 | QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 6170 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6171 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6172 | {"Empty array", |
| 6173 | QCBOR_DECODE_MODE_NORMAL, |
| 6174 | {"\xC4\x80", 2}, |
| 6175 | QCBOR_ERR_NO_MORE_ITEMS |
| 6176 | }, |
| 6177 | {"Second is not an integer", |
| 6178 | QCBOR_DECODE_MODE_NORMAL, |
| 6179 | {"\xC4\x82\x03\x40", 4}, |
| 6180 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6181 | }, |
| 6182 | {"First is not an integer", |
| 6183 | QCBOR_DECODE_MODE_NORMAL, |
| 6184 | {"\xC4\x82\x40", 3}, |
| 6185 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6186 | }, |
| 6187 | {"Not an array", |
| 6188 | QCBOR_DECODE_MODE_NORMAL, |
| 6189 | {"\xC4\xA2", 2}, |
| 6190 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 6191 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6192 | }; |
| 6193 | |
| 6194 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6195 | int32_t |
| 6196 | ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6197 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 6198 | return ProcessDecodeFailures(ExponentAndMantissaFailures, |
| 6199 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 6200 | struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 6201 | } |
| 6202 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6203 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6204 | |
| 6205 | |
| 6206 | |
| 6207 | /* |
| 6208 | Some basic CBOR with map and array used in a lot of tests. |
| 6209 | The map labels are all strings |
| 6210 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6211 | { |
| 6212 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6213 | "an array of two strings": [ |
| 6214 | "string1", "string2" |
| 6215 | ], |
| 6216 | "map in a map": { |
| 6217 | "bytes 1": h'78787878', |
| 6218 | "bytes 2": h'79797979', |
| 6219 | "another int": 98, |
| 6220 | "text 2": "lies, damn lies and statistics" |
| 6221 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6222 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6223 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6224 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6225 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6226 | { |
| 6227 | QCBORItem Item1, Item2, Item3; |
| 6228 | int64_t nDecodedInt1, nDecodedInt2; |
| 6229 | UsefulBufC B1, B2, S1, S2, S3; |
| 6230 | |
| 6231 | QCBORDecodeContext DCtx; |
| 6232 | QCBORError nCBORError; |
| 6233 | |
| 6234 | QCBORDecode_Init(&DCtx, input, 0); |
| 6235 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6236 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6237 | |
| 6238 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 6239 | |
| 6240 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6241 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6242 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 6243 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 6244 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6245 | QCBORDecode_ExitMap(&DCtx); |
| 6246 | |
| 6247 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6248 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6249 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 6250 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6251 | return -400; |
| 6252 | } |
| 6253 | QCBORDecode_ExitArray(&DCtx); |
| 6254 | |
| 6255 | // Parse the same array again using GetText() instead of GetItem() |
| 6256 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6257 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 6258 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6259 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6260 | return 5000; |
| 6261 | } |
| 6262 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 6263 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6264 | return 5001; |
| 6265 | } */ |
| 6266 | |
| 6267 | QCBORDecode_ExitArray(&DCtx); |
| 6268 | |
| 6269 | QCBORDecode_ExitMap(&DCtx); |
| 6270 | |
| 6271 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 6272 | |
| 6273 | if(nCBORError) { |
| 6274 | return (int32_t)nCBORError; |
| 6275 | } |
| 6276 | |
| 6277 | if(nDecodedInt1 != 42) { |
| 6278 | return 1001; |
| 6279 | } |
| 6280 | |
| 6281 | if(nDecodedInt2 != 98) { |
| 6282 | return 1002; |
| 6283 | } |
| 6284 | |
| 6285 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6286 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6287 | return 1003; |
| 6288 | } |
| 6289 | |
| 6290 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6291 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6292 | return 1004; |
| 6293 | } |
| 6294 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6295 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6296 | return 1005; |
| 6297 | } |
| 6298 | |
| 6299 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 6300 | return 1006; |
| 6301 | } |
| 6302 | |
| 6303 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 6304 | return 1007; |
| 6305 | } |
| 6306 | |
| 6307 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 6308 | return 1008; |
| 6309 | } |
| 6310 | |
| 6311 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 6312 | return 1009; |
| 6313 | } |
| 6314 | |
| 6315 | return 0; |
| 6316 | } |
| 6317 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6318 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6319 | { |
| 6320 | -75008: h'05083399', |
| 6321 | 88: [], |
| 6322 | 100100: { |
| 6323 | "sub1": { |
| 6324 | 10: [ |
| 6325 | 0 |
| 6326 | ], |
| 6327 | -75009: h'A46823990001', |
| 6328 | 100100: { |
| 6329 | "json": "{ \"ueid\", \"xyz\"}", |
| 6330 | "subsub": { |
| 6331 | 100002: h'141813191001' |
| 6332 | } |
| 6333 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6334 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6335 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6336 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6337 | */ |
| 6338 | |
| 6339 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6340 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 6341 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 6342 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 6343 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 6344 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 6345 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 6346 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 6347 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 6348 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 6349 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 6350 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 6351 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6352 | }; |
| 6353 | |
| 6354 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 6355 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 6356 | { |
| 6357 | UsefulBufC String; |
| 6358 | |
| 6359 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 6360 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 6361 | |
| 6362 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6363 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 6364 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6365 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 6366 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 6367 | if(QCBORDecode_GetError(pDCtx)) { |
| 6368 | return 4001; |
| 6369 | } |
| 6370 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 6371 | return 4002; |
| 6372 | } |
| 6373 | QCBORDecode_ExitMap(pDCtx); |
| 6374 | QCBORDecode_ExitMap(pDCtx); |
| 6375 | QCBORDecode_ExitMap(pDCtx); |
| 6376 | QCBORDecode_ExitMap(pDCtx); |
| 6377 | |
| 6378 | return 0; |
| 6379 | } |
| 6380 | |
| 6381 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 6382 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 6383 | { |
| 6384 | QCBORItem Item; |
| 6385 | QCBORError uErr; |
| 6386 | |
| 6387 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 6388 | for(int x = 0; x < 20; x++) { |
| 6389 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6390 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6391 | return 4100; |
| 6392 | |
| 6393 | } |
| 6394 | } |
| 6395 | QCBORDecode_ExitArray(pDCtx); |
| 6396 | if(QCBORDecode_GetError(pDCtx)) { |
| 6397 | return 4101; |
| 6398 | } |
| 6399 | |
| 6400 | return 0; |
| 6401 | } |
| 6402 | |
| 6403 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 6404 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 6405 | { |
| 6406 | QCBORError uErr; |
| 6407 | |
| 6408 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 6409 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 6410 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6411 | int64_t nInt = 99; |
| 6412 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 6413 | if(nInt != 0) { |
| 6414 | return 4200; |
| 6415 | } |
| 6416 | for(int x = 0; x < 20; x++) { |
| 6417 | QCBORItem Item; |
| 6418 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 6419 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6420 | return 4201; |
| 6421 | |
| 6422 | } |
| 6423 | } |
| 6424 | QCBORDecode_ExitArray(pDCtx); |
| 6425 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 6426 | return 4202; |
| 6427 | } |
| 6428 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 6429 | UsefulBufC dD; |
| 6430 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6431 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6432 | return 4203; |
| 6433 | } |
| 6434 | for(int x = 0; x < 20; x++) { |
| 6435 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 6436 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 6437 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6438 | return 4204; |
| 6439 | } |
| 6440 | } |
| 6441 | QCBORDecode_ExitArray(pDCtx); |
| 6442 | QCBORDecode_ExitMap(pDCtx); |
| 6443 | QCBORDecode_ExitMap(pDCtx); |
| 6444 | |
| 6445 | return 0; |
| 6446 | } |
| 6447 | |
| 6448 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 6449 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 6450 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6451 | { |
| 6452 | QCBORDecodeContext DCtx; |
| 6453 | int32_t nReturn; |
| 6454 | QCBORError uErr; |
| 6455 | |
| 6456 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 6457 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6458 | |
| 6459 | for(int j = 0; j < 5; j++) { |
| 6460 | for(int i = 0; i < 20; i++) { |
| 6461 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 6462 | if(nReturn) { |
| 6463 | return nReturn; |
| 6464 | } |
| 6465 | } |
| 6466 | |
| 6467 | for(int i = 0; i < 20; i++) { |
| 6468 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 6469 | if(nReturn ) { |
| 6470 | return nReturn; |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | for(int i = 0; i < 20; i++) { |
| 6475 | nReturn = DecodeNestedGetZero(&DCtx); |
| 6476 | if(nReturn ) { |
| 6477 | return nReturn; |
| 6478 | } |
| 6479 | } |
| 6480 | } |
| 6481 | |
| 6482 | QCBORDecode_ExitMap(&DCtx); |
| 6483 | uErr = QCBORDecode_Finish(&DCtx); |
| 6484 | if(uErr) { |
| 6485 | return (int32_t)uErr + 4100; |
| 6486 | } |
| 6487 | |
| 6488 | return 0; |
| 6489 | } |
| 6490 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6491 | |
| 6492 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6493 | [ |
| 6494 | 23, |
| 6495 | 6000, |
| 6496 | h'67616C6163746963', |
| 6497 | h'686176656E20746F6B656E' |
| 6498 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6499 | */ |
| 6500 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6501 | 0x84, |
| 6502 | 0x17, |
| 6503 | 0x19, 0x17, 0x70, |
| 6504 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 6505 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6506 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6507 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6508 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 6509 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6510 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6511 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 6512 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6513 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6514 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6515 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6516 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6517 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6518 | /* |
| 6519 | { |
| 6520 | 0: [], |
| 6521 | 9: [ |
| 6522 | [], |
| 6523 | [] |
| 6524 | ], |
| 6525 | 8: { |
| 6526 | 1: [], |
| 6527 | 2: {}, |
| 6528 | 3: [] |
| 6529 | }, |
| 6530 | 4: {}, |
| 6531 | 5: [], |
| 6532 | 6: [ |
| 6533 | [], |
| 6534 | [] |
| 6535 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6536 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6537 | */ |
| 6538 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6539 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 6540 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 6541 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 6542 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6543 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6544 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6545 | |
| 6546 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6547 | /* |
| 6548 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6549 | Duplicate label |
| 6550 | Integer overflow |
| 6551 | Date overflow |
| 6552 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6553 | { |
| 6554 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6555 | 3: -18446744073709551616, |
| 6556 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6557 | 5: 0, |
| 6558 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6559 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6560 | */ |
| 6561 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6562 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6563 | 0xa6, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6564 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6565 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 6566 | #else |
| 6567 | 0xa4, |
| 6568 | #endif |
| 6569 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6570 | 0x05, 0x00, |
| 6571 | 0x05, 0x00, |
| 6572 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6573 | }; |
| 6574 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6575 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6576 | static const uint8_t spUnRecoverableMapError1[] = { |
| 6577 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6578 | }; |
| 6579 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6580 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6581 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6582 | static const uint8_t spUnRecoverableMapError2[] = { |
| 6583 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 6584 | }; |
| 6585 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6586 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6587 | static const uint8_t spUnRecoverableMapError3[] = { |
| 6588 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 6589 | }; |
| 6590 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6591 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6592 | static const uint8_t spUnRecoverableMapError4[] = { |
| 6593 | 0xbf, |
| 6594 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6595 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 6596 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6597 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 6598 | 0xff |
| 6599 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6600 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6601 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6602 | const unsigned char not_well_formed_submod_section[] = { |
| 6603 | 0xa1, 0x14, 0x1f, |
| 6604 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6605 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6606 | |
| 6607 | /* Array of length 3, but only two items. */ |
| 6608 | const unsigned char spBadConsumeInput[] = { |
| 6609 | 0x83, 0x00, 0x00 |
| 6610 | }; |
| 6611 | |
| 6612 | /* Tag nesting too deep. */ |
| 6613 | const unsigned char spBadConsumeInput2[] = { |
| 6614 | 0x81, |
| 6615 | 0xD8, 0x37, |
| 6616 | 0xD8, 0x2C, |
| 6617 | 0xD8, 0x21, |
| 6618 | 0xD6, |
| 6619 | 0xCB, |
| 6620 | 00 |
| 6621 | }; |
| 6622 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6623 | |
| 6624 | const unsigned char spBadConsumeInput4[] = { |
| 6625 | 0x81, 0x9f, 0x00, 0xff |
| 6626 | }; |
| 6627 | |
| 6628 | const unsigned char spBadConsumeInput5[] = { |
| 6629 | 0xa1, 0x80, 0x00 |
| 6630 | }; |
| 6631 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6632 | /* |
| 6633 | Lots of nesting for various nesting tests. |
| 6634 | { 1:1, |
| 6635 | 2:{ |
| 6636 | 21:21, |
| 6637 | 22:{ |
| 6638 | 221:[2111, 2112, 2113], |
| 6639 | 222:222, |
| 6640 | 223: {} |
| 6641 | }, |
| 6642 | 23: 23 |
| 6643 | }, |
| 6644 | 3:3, |
| 6645 | 4: [ {} ] |
| 6646 | } |
| 6647 | */ |
| 6648 | static const uint8_t spNested[] = { |
| 6649 | 0xA4, /* Map of 4 */ |
| 6650 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 6651 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 6652 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 6653 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 6654 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 6655 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 6656 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 6657 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 6658 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 6659 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 6660 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 6661 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 6662 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 6663 | 0xA0 /* Array entry {}, an empty map */ |
| 6664 | }; |
| 6665 | |
| 6666 | |
| 6667 | static int32_t EnterMapCursorTest(void) |
| 6668 | { |
| 6669 | QCBORDecodeContext DCtx; |
| 6670 | QCBORItem Item1; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6671 | int64_t nInt; |
| 6672 | QCBORError uErr; |
| 6673 | |
| 6674 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6675 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6676 | QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt); |
| 6677 | uErr = QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 6678 | if(uErr != QCBOR_SUCCESS) { |
| 6679 | return 701; |
| 6680 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 6681 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6682 | return 700; |
| 6683 | } |
| 6684 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6685 | |
| 6686 | int i; |
| 6687 | for(i = 0; i < 13; i++) { |
| 6688 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6689 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6690 | int j; |
| 6691 | /* Move travesal cursor */ |
| 6692 | for(j = 0; j < i; j++) { |
| 6693 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6694 | } |
| 6695 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6696 | QCBORDecode_ExitMap(&DCtx); |
| 6697 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6698 | if(Item1.label.int64 != 3) { |
| 6699 | return 8000; |
| 6700 | } |
| 6701 | } |
| 6702 | |
| 6703 | for(i = 0; i < 13; i++) { |
| 6704 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6705 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6706 | int j; |
| 6707 | /* Move travesal cursor */ |
| 6708 | for(j = 0; j < i; j++) { |
| 6709 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6710 | } |
| 6711 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6712 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6713 | QCBORDecode_ExitMap(&DCtx); |
| 6714 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6715 | if(Item1.label.int64 != 23) { |
| 6716 | return 8000; |
| 6717 | } |
| 6718 | } |
| 6719 | |
| 6720 | for(i = 0; i < 13; i++) { |
| 6721 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6722 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6723 | int j; |
| 6724 | /* Move travesal cursor */ |
| 6725 | for(j = 0; j < i; j++) { |
| 6726 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6727 | } |
| 6728 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6729 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6730 | for(j = 0; j < i; j++) { |
| 6731 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6732 | } |
| 6733 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 6734 | QCBORDecode_ExitArray(&DCtx); |
| 6735 | QCBORDecode_ExitMap(&DCtx); |
| 6736 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6737 | if(Item1.label.int64 != 23) { |
| 6738 | return 8000; |
| 6739 | } |
| 6740 | QCBORDecode_ExitMap(&DCtx); |
| 6741 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6742 | if(Item1.label.int64 != 3) { |
| 6743 | return 8000; |
| 6744 | } |
| 6745 | } |
| 6746 | |
| 6747 | return 0; |
| 6748 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6749 | |
| 6750 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6751 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6752 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6753 | QCBORItem Item1; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6754 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6755 | int32_t nReturn; |
| 6756 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6757 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6758 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6759 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6760 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6761 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6762 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6763 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6764 | QCBORDecode_ExitArray(&DCtx); |
| 6765 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6766 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 6767 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6768 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6769 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6770 | QCBORDecode_ExitArray(&DCtx); |
| 6771 | QCBORDecode_ExitArray(&DCtx); |
| 6772 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6773 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 6774 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6775 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6776 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6777 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6778 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6779 | QCBORDecode_ExitArray(&DCtx); |
| 6780 | QCBORDecode_ExitMap(&DCtx); |
| 6781 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6782 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6783 | QCBORDecode_ExitMap(&DCtx); |
| 6784 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6785 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6786 | QCBORDecode_ExitArray(&DCtx); |
| 6787 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6788 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 6789 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6790 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6791 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6792 | QCBORDecode_ExitArray(&DCtx); |
| 6793 | QCBORDecode_ExitArray(&DCtx); |
| 6794 | |
| 6795 | QCBORDecode_ExitMap(&DCtx); |
| 6796 | |
| 6797 | uErr = QCBORDecode_Finish(&DCtx); |
| 6798 | if(uErr != QCBOR_SUCCESS){ |
| 6799 | return 3011; |
| 6800 | } |
| 6801 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6802 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6803 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6804 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6805 | if(nReturn) { |
| 6806 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6807 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6808 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 6809 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6810 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6811 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 6812 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6813 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6814 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6815 | if(nReturn) { |
| 6816 | return nReturn; |
| 6817 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6818 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6819 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6820 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 6821 | // These tests confirm the cursor is at the right place after entering |
| 6822 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6823 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6824 | |
| 6825 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6826 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6827 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6828 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6829 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6830 | return 2001; |
| 6831 | } |
| 6832 | |
| 6833 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6834 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 6835 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6836 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6837 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 6838 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6839 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6840 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 6841 | return 2051; |
| 6842 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6843 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6844 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 6845 | return 2002; |
| 6846 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6847 | QCBORDecode_ExitArray(&DCtx); |
| 6848 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 6849 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6850 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6851 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 6852 | return 2052; |
| 6853 | } |
| 6854 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6855 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6856 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6857 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6858 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6859 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6860 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6861 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6862 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6863 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 6864 | return 2003; |
| 6865 | } |
| 6866 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6867 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6868 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6869 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6870 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6871 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6872 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6873 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6874 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6875 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6876 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6877 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6878 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6879 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6880 | } |
| 6881 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6882 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6883 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 6884 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6885 | QCBORDecode_ExitArray(&DCtx); |
| 6886 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6887 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 6888 | return 2006; |
| 6889 | } |
| 6890 | QCBORDecode_ExitMap(&DCtx); |
| 6891 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6892 | return 2007; |
| 6893 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6894 | #endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 6895 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6896 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6897 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6898 | int64_t nDecodedInt2; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6899 | |
| 6900 | UsefulBufC String; |
| 6901 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
| 6902 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6903 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
| 6904 | return 2009; |
| 6905 | } |
| 6906 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6907 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6908 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6909 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6910 | return 2008; |
| 6911 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 6912 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6913 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6914 | |
| 6915 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6916 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6917 | // This will fail because the map is empty. |
| 6918 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6919 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6920 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6921 | return 2010; |
| 6922 | } |
| 6923 | QCBORDecode_ExitMap(&DCtx); |
| 6924 | uErr = QCBORDecode_Finish(&DCtx); |
| 6925 | if(uErr != QCBOR_SUCCESS){ |
| 6926 | return 2011; |
| 6927 | } |
| 6928 | |
| 6929 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6930 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6931 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6932 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6933 | // This will fail because the map is empty. |
| 6934 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6935 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6936 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6937 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6938 | } |
| 6939 | QCBORDecode_ExitMap(&DCtx); |
| 6940 | uErr = QCBORDecode_Finish(&DCtx); |
| 6941 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6942 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6943 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6944 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6945 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6946 | |
| 6947 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6948 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6949 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6950 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6951 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6952 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6953 | QCBORDecode_ExitArray(&DCtx); |
| 6954 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 6955 | QCBORDecode_ExitArray(&DCtx); |
| 6956 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6957 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6958 | return 2014; |
| 6959 | } |
| 6960 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6961 | int64_t nInt; |
| 6962 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6963 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6964 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6965 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6966 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6967 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 6968 | return 2021; |
| 6969 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6970 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 6971 | return 2121; |
| 6972 | } |
| 6973 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6974 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6975 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6976 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6977 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 6978 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6979 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 6980 | return 2023; |
| 6981 | } |
| 6982 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6983 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6984 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 6985 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6986 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6987 | return 2024; |
| 6988 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6989 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6990 | |
| 6991 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 6992 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6993 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 6994 | return 2025; |
| 6995 | } |
| 6996 | |
| 6997 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 6998 | |
| 6999 | QCBORDecode_ExitMap(&DCtx); |
| 7000 | uErr = QCBORDecode_Finish(&DCtx); |
| 7001 | if(uErr != QCBOR_SUCCESS) { |
| 7002 | return 2026; |
| 7003 | } |
| 7004 | |
| 7005 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7006 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7007 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7008 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7009 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 7010 | return 2030; |
| 7011 | } |
| 7012 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7013 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7014 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7015 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7016 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7017 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7018 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7019 | return 2031; |
| 7020 | } |
| 7021 | |
| 7022 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7023 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7024 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7025 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7026 | if(uErr != QCBOR_ERR_HIT_END) { |
| 7027 | return 2032; |
| 7028 | } |
| 7029 | |
| 7030 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7031 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 7032 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 7033 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 7034 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 7035 | return 2033; |
| 7036 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7037 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7038 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7039 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 7040 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7041 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7042 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 7043 | return 2401; |
| 7044 | } |
| 7045 | if(QCBORDecode_GetError(&DCtx)) { |
| 7046 | return 2402; |
| 7047 | } |
| 7048 | |
| 7049 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7050 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 7051 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7052 | Item1.val.uCount != 3 || |
| 7053 | Item1.uNextNestLevel != 1) { |
| 7054 | return 2403; |
| 7055 | } |
| 7056 | if(QCBORDecode_GetError(&DCtx)) { |
| 7057 | return 2404; |
| 7058 | } |
| 7059 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7060 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 7061 | Item1.uNextNestLevel != 1 || |
| 7062 | Item1.val.int64 != 42) { |
| 7063 | return 2405; |
| 7064 | } |
| 7065 | if(QCBORDecode_GetError(&DCtx)) { |
| 7066 | return 2406; |
| 7067 | } |
| 7068 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7069 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 7070 | Item1.uNestingLevel != 1 || |
| 7071 | Item1.uNextNestLevel != 1 || |
| 7072 | Item1.val.uCount != 2) { |
| 7073 | return 2407; |
| 7074 | } |
| 7075 | if(QCBORDecode_GetError(&DCtx)) { |
| 7076 | return 2408; |
| 7077 | } |
| 7078 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7079 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 7080 | Item1.uNestingLevel != 1 || |
| 7081 | Item1.uNextNestLevel != 0 || |
| 7082 | Item1.val.uCount != 4) { |
| 7083 | return 2409; |
| 7084 | } |
| 7085 | if(QCBORDecode_GetError(&DCtx)) { |
| 7086 | return 2410; |
| 7087 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 7088 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7089 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7090 | nReturn = DecodeNestedIterate(); |
| 7091 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 7092 | |
| 7093 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 7094 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7095 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 7096 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 7097 | return 2500; |
| 7098 | } |
| 7099 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7100 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 7101 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7102 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7103 | return 2600; |
| 7104 | } |
| 7105 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7106 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7107 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 7108 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7109 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7110 | return 2700; |
| 7111 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7112 | #endif |
| 7113 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 7114 | |
| 7115 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 7116 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7117 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7118 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 7119 | return 2900; |
| 7120 | } |
| 7121 | #else |
| 7122 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7123 | return 2901; |
| 7124 | } |
| 7125 | #endif |
| 7126 | |
| 7127 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 7128 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 7129 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 7130 | return 3000; |
| 7131 | } |
| 7132 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 7133 | nReturn = EnterMapCursorTest(); |
| 7134 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 7135 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7136 | } |
| 7137 | |
| 7138 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7139 | struct NumberConversion { |
| 7140 | char *szDescription; |
| 7141 | UsefulBufC CBOR; |
| 7142 | int64_t nConvertedToInt64; |
| 7143 | QCBORError uErrorInt64; |
| 7144 | uint64_t uConvertToUInt64; |
| 7145 | QCBORError uErrorUint64; |
| 7146 | double dConvertToDouble; |
| 7147 | QCBORError uErrorDouble; |
| 7148 | }; |
| 7149 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7150 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 7151 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 7152 | #else |
| 7153 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 7154 | #endif |
| 7155 | |
| 7156 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7157 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7158 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7159 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 7160 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 7161 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 7162 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 7163 | }, 15}, |
| 7164 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 7165 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 7166 | 0, |
| 7167 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7168 | -4.6116860184273879E+18, |
| 7169 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 7170 | }, |
| 7171 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7172 | "too large to fit into int64_t", |
| 7173 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 7174 | 0, |
| 7175 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7176 | 0, |
| 7177 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7178 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7179 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7180 | }, |
| 7181 | { |
| 7182 | "largest negative int that fits in int64_t", |
| 7183 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 7184 | INT64_MIN, |
| 7185 | QCBOR_SUCCESS, |
| 7186 | 0, |
| 7187 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7188 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7189 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 7190 | }, |
| 7191 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7192 | "negative bignum -1", |
| 7193 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 7194 | -1, |
| 7195 | QCBOR_SUCCESS, |
| 7196 | 0, |
| 7197 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7198 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7199 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7200 | }, |
| 7201 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7202 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7203 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7204 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7205 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7206 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7207 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7208 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7209 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7210 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7211 | }, |
| 7212 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7213 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7214 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7215 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7216 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7217 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7218 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7219 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7220 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7221 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7222 | }, |
| 7223 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7224 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7225 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7226 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 7227 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7228 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7229 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7230 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7231 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7232 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7233 | }, |
| 7234 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 7235 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7236 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 7237 | 0, |
| 7238 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7239 | 0, |
| 7240 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7241 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7242 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7243 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7244 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7245 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7246 | "Positive bignum 0x01020304 indefinite length string", |
| 7247 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 7248 | 0x01020304, |
| 7249 | QCBOR_SUCCESS, |
| 7250 | 0x01020304, |
| 7251 | QCBOR_SUCCESS, |
| 7252 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7253 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7254 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7255 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7256 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 7257 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7258 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 7259 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 7260 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7261 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7262 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7263 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7264 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7265 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7266 | }, |
| 7267 | { |
| 7268 | "big float [9223372036854775806, 9223372036854775806]", |
| 7269 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7270 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 7271 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7272 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7273 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7274 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7275 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7276 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 7277 | }, |
| 7278 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7279 | "Big float 3 * 2^^2", |
| 7280 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 7281 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7282 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7283 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7284 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7285 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7286 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7287 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7288 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7289 | "Decimal fraction 3/10", |
| 7290 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 7291 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7292 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7293 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7294 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7295 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7296 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 7297 | }, |
| 7298 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7299 | "extreme pos bignum", |
| 7300 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 7301 | // 50 rows of 8 is 400 digits. |
| 7302 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7303 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7304 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7305 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7306 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7307 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7308 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7309 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7310 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7311 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7312 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7313 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7314 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7315 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7316 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7317 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7318 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7319 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7320 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7321 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7322 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7323 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7324 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7325 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7326 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7327 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7328 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7329 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7330 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7331 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7332 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7333 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7334 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7335 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7336 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7337 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7338 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7339 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7340 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7341 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7342 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7343 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7344 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7345 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7346 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7347 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7348 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7349 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7350 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7351 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7352 | 404}, |
| 7353 | 0, |
| 7354 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7355 | 0, |
| 7356 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7357 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7358 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7359 | }, |
| 7360 | |
| 7361 | { |
| 7362 | "extreme neg bignum", |
| 7363 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 7364 | // 50 rows of 8 is 400 digits. |
| 7365 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7366 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7367 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7368 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7369 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7370 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7371 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7372 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7373 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7374 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7375 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7376 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7377 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7378 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7379 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7380 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7381 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7382 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7383 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7384 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7385 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7386 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7387 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7388 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7389 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7390 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7391 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7392 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7393 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7394 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7395 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7396 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7397 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7398 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7399 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7400 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7401 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7402 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7403 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7404 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7405 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7406 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7407 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7408 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7409 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7410 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7411 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7412 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 7413 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 7414 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7415 | 404}, |
| 7416 | 0, |
| 7417 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7418 | 0, |
| 7419 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7420 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7421 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 7422 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7423 | |
| 7424 | { |
| 7425 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 7426 | {(uint8_t[]){ |
| 7427 | 0xC5, 0x82, |
| 7428 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 7429 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 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 | 51722fd | 2020-09-02 13:01:33 -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 | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7434 | 0, |
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 | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7436 | }, |
| 7437 | |
| 7438 | { |
| 7439 | "bigfloat that evaluates to -INFINITY", |
| 7440 | {(uint8_t[]){ |
| 7441 | 0xC5, 0x82, |
| 7442 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 7443 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7444 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7445 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7446 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7447 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7448 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7449 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 7450 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7451 | { |
| 7452 | "Positive bignum 0xffff", |
| 7453 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 7454 | 65536-1, |
| 7455 | QCBOR_SUCCESS, |
| 7456 | 0xffff, |
| 7457 | QCBOR_SUCCESS, |
| 7458 | 65535.0, |
| 7459 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7460 | }, |
| 7461 | #endif /* QCBOR_DISABLE_TAGS */ |
| 7462 | { |
| 7463 | "Positive integer 18446744073709551615", |
| 7464 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 7465 | 0, |
| 7466 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 7467 | 18446744073709551615ULL, |
| 7468 | QCBOR_SUCCESS, |
| 7469 | 18446744073709551615.0, |
| 7470 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7471 | }, |
| 7472 | |
| 7473 | { |
| 7474 | "Postive integer 0", |
| 7475 | {(uint8_t[]){0x0}, 1}, |
| 7476 | 0LL, |
| 7477 | QCBOR_SUCCESS, |
| 7478 | 0ULL, |
| 7479 | QCBOR_SUCCESS, |
| 7480 | 0.0, |
| 7481 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7482 | }, |
| 7483 | { |
| 7484 | "Negative integer -18446744073709551616", |
| 7485 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 7486 | -9223372036854775807-1, // INT64_MIN |
| 7487 | QCBOR_SUCCESS, |
| 7488 | 0ULL, |
| 7489 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 7490 | -9223372036854775808.0, |
| 7491 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7492 | }, |
| 7493 | { |
| 7494 | "Double Floating point value 100.3", |
| 7495 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 7496 | 100L, |
| 7497 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7498 | 100ULL, |
| 7499 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7500 | 100.3, |
| 7501 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 7502 | }, |
| 7503 | { |
| 7504 | "Floating point value NaN 0xfa7fc00000", |
| 7505 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 7506 | 0, |
| 7507 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7508 | 0, |
| 7509 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7510 | NAN, |
| 7511 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7512 | }, |
| 7513 | { |
| 7514 | "half-precision Floating point value -4", |
| 7515 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 7516 | // Normal case with all enabled. |
| 7517 | -4, |
| 7518 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 7519 | 0, |
| 7520 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 7521 | -4.0, |
| 7522 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 7523 | }, |
| 7524 | { |
| 7525 | "+inifinity single precision", |
| 7526 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 7527 | 0, |
| 7528 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 7529 | 0, |
| 7530 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 7531 | INFINITY, |
| 7532 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 7533 | }, |
| 7534 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7535 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7536 | |
| 7537 | |
| 7538 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7539 | |
| 7540 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 7541 | { |
| 7542 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 7543 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7544 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 7545 | return 1; |
| 7546 | } |
| 7547 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7548 | (void)Pool; |
| 7549 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7550 | return 0; |
| 7551 | } |
| 7552 | |
| 7553 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7554 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7555 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7556 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 7557 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7558 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 7559 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 7560 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7561 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7562 | // Set up the decoding context including a memory pool so that |
| 7563 | // indefinite length items can be checked |
| 7564 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7565 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7566 | |
| 7567 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7568 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7569 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7570 | } |
| 7571 | |
| 7572 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7573 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7574 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7575 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7576 | } |
| 7577 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7578 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7579 | } |
| 7580 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7581 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7582 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7583 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7584 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7585 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7586 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7587 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7588 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7589 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7590 | } |
| 7591 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7592 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7593 | } |
| 7594 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7595 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7596 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 7597 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7598 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7599 | |
| 7600 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7601 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7602 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 7603 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7604 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7605 | } |
| 7606 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 7607 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 7608 | // NaN's can't be compared for equality. A NaN is |
| 7609 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7610 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7611 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7612 | } |
| 7613 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7614 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 7615 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7616 | } |
| 7617 | } |
| 7618 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 7619 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 7620 | } |
| 7621 | |
| 7622 | return 0; |
| 7623 | } |
| 7624 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7625 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7626 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7627 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7628 | { |
| 7629 | QCBORDecodeContext DCtx; |
| 7630 | QCBORItem Item; |
| 7631 | QCBORError uCBORError; |
| 7632 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 7633 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7634 | QCBORDecode_Init(&DCtx, |
| 7635 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 7636 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7637 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7638 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 7639 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7640 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7641 | do { |
| 7642 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7643 | } while (QCBOR_SUCCESS == uCBORError); |
| 7644 | |
| 7645 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7646 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 7647 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 7648 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 7649 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 7650 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7651 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7652 | |
| 7653 | |
| 7654 | static const uint8_t spSequenceTestInput[] = { |
| 7655 | /* 1. The valid date string "1985-04-12" */ |
| 7656 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 7657 | |
| 7658 | /* 2. */ |
| 7659 | 0x00, |
| 7660 | |
| 7661 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 7662 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 7663 | |
| 7664 | /* 4. */ |
| 7665 | 0x62, 'h', 'i', |
| 7666 | }; |
| 7667 | |
| 7668 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7669 | int32_t CBORSequenceDecodeTests(void) |
| 7670 | { |
| 7671 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7672 | QCBORItem Item; |
| 7673 | QCBORError uCBORError; |
| 7674 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7675 | |
| 7676 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7677 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7678 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7679 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7680 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7681 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7682 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7683 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7684 | if(uCBORError != QCBOR_SUCCESS) { |
| 7685 | return 1; |
| 7686 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7687 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7688 | return 2; |
| 7689 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7690 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7691 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7692 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7693 | uConsumed != 11) { |
| 7694 | return 102; |
| 7695 | } |
| 7696 | |
| 7697 | // Get 2. |
| 7698 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7699 | if(uCBORError != QCBOR_SUCCESS) { |
| 7700 | return 66; |
| 7701 | } |
| 7702 | |
| 7703 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7704 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7705 | uConsumed != 12) { |
| 7706 | return 102; |
| 7707 | } |
| 7708 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7709 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7710 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7711 | if(uCBORError != QCBOR_SUCCESS) { |
| 7712 | return 2; |
| 7713 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7714 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7715 | return 3; |
| 7716 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7717 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7718 | // A sequence can have stuff at the end that may |
| 7719 | // or may not be valid CBOR. The protocol decoder knows |
| 7720 | // when to stop by definition of the protocol, not |
| 7721 | // when the top-level map or array is ended. |
| 7722 | // Finish still has to be called to know that |
| 7723 | // maps and arrays (if there were any) were closed |
| 7724 | // off correctly. When called like this it |
| 7725 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 7726 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7727 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 7728 | return 4; |
| 7729 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7730 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7731 | // --- Test an empty input ---- |
| 7732 | uint8_t empty[1]; |
| 7733 | UsefulBufC Empty = {empty, 0}; |
| 7734 | QCBORDecode_Init(&DCtx, |
| 7735 | Empty, |
| 7736 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7737 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7738 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7739 | if(uCBORError != QCBOR_SUCCESS) { |
| 7740 | return 5; |
| 7741 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7742 | |
| 7743 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7744 | // --- Sequence with unclosed indefinite length array --- |
| 7745 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7746 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7747 | QCBORDecode_Init(&DCtx, |
| 7748 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 7749 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7750 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7751 | // Get the first item |
| 7752 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7753 | if(uCBORError != QCBOR_SUCCESS) { |
| 7754 | return 7; |
| 7755 | } |
| 7756 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7757 | return 8; |
| 7758 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7759 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7760 | // Get a second item |
| 7761 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7762 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7763 | if(uCBORError != QCBOR_SUCCESS) { |
| 7764 | return 9; |
| 7765 | } |
| 7766 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7767 | return 10; |
| 7768 | } |
| 7769 | |
| 7770 | // Try to finish before consuming all bytes to confirm |
| 7771 | // that the still-open error is returned. |
| 7772 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7773 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7774 | return 11; |
| 7775 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7776 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7777 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7778 | return 20; |
| 7779 | } |
| 7780 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7781 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7782 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7783 | // --- Sequence with a closed indefinite length array --- |
| 7784 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7785 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7786 | QCBORDecode_Init(&DCtx, |
| 7787 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 7788 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7789 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7790 | // Get the first item |
| 7791 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7792 | if(uCBORError != QCBOR_SUCCESS) { |
| 7793 | return 12; |
| 7794 | } |
| 7795 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7796 | return 13; |
| 7797 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7798 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7799 | // Get a second item |
| 7800 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7801 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7802 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7803 | if(uCBORError != QCBOR_SUCCESS) { |
| 7804 | return 14; |
| 7805 | } |
| 7806 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7807 | return 15; |
| 7808 | } |
| 7809 | |
| 7810 | // Try to finish before consuming all bytes to confirm |
| 7811 | // that the still-open error is returned. |
| 7812 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7813 | if(uCBORError != QCBOR_SUCCESS) { |
| 7814 | return 16; |
| 7815 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7816 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7817 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7818 | return 20; |
| 7819 | } |
| 7820 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7821 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7822 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7823 | return 0; |
| 7824 | } |
| 7825 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7826 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 7827 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7828 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7829 | { |
| 7830 | int nErrCode; |
| 7831 | int32_t n32; |
| 7832 | int16_t n16; |
| 7833 | int8_t n8; |
| 7834 | uint32_t u32; |
| 7835 | uint16_t u16; |
| 7836 | uint8_t u8; |
| 7837 | uint64_t u64; |
| 7838 | |
| 7839 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 7840 | if(nErrCode == -1 || n32 != 1) { |
| 7841 | return 1; |
| 7842 | } |
| 7843 | |
| 7844 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 7845 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 7846 | return 2; |
| 7847 | } |
| 7848 | |
| 7849 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 7850 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 7851 | return 3; |
| 7852 | } |
| 7853 | |
| 7854 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 7855 | if(nErrCode != -1) { |
| 7856 | return 4; |
| 7857 | } |
| 7858 | |
| 7859 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 7860 | if(nErrCode != -1) { |
| 7861 | return 5; |
| 7862 | } |
| 7863 | |
| 7864 | |
| 7865 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 7866 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 7867 | return 6; |
| 7868 | } |
| 7869 | |
| 7870 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 7871 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 7872 | return 7; |
| 7873 | } |
| 7874 | |
| 7875 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 7876 | if(nErrCode == -1 || n16 != 1) { |
| 7877 | return 8; |
| 7878 | } |
| 7879 | |
| 7880 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 7881 | if(nErrCode != -1) { |
| 7882 | return 9; |
| 7883 | } |
| 7884 | |
| 7885 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 7886 | if(nErrCode != -1) { |
| 7887 | return 10; |
| 7888 | } |
| 7889 | |
| 7890 | |
| 7891 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 7892 | if(nErrCode == -1 || n8 != 1) { |
| 7893 | return 11; |
| 7894 | } |
| 7895 | |
| 7896 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 7897 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 7898 | return 12; |
| 7899 | } |
| 7900 | |
| 7901 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 7902 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 7903 | return 13; |
| 7904 | } |
| 7905 | |
| 7906 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 7907 | if(nErrCode != -1) { |
| 7908 | return 14; |
| 7909 | } |
| 7910 | |
| 7911 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 7912 | if(nErrCode != -1) { |
| 7913 | return 15; |
| 7914 | } |
| 7915 | |
| 7916 | |
| 7917 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 7918 | if(nErrCode == -1 || u32 != 1) { |
| 7919 | return 16; |
| 7920 | } |
| 7921 | |
| 7922 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 7923 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 7924 | return 17; |
| 7925 | } |
| 7926 | |
| 7927 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 7928 | if(nErrCode == -1 || u32 != 0) { |
| 7929 | return 18; |
| 7930 | } |
| 7931 | |
| 7932 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 7933 | if(nErrCode != -1) { |
| 7934 | return 19; |
| 7935 | } |
| 7936 | |
| 7937 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 7938 | if(nErrCode != -1) { |
| 7939 | return 20; |
| 7940 | } |
| 7941 | |
| 7942 | |
| 7943 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 7944 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 7945 | return 21; |
| 7946 | } |
| 7947 | |
| 7948 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 7949 | if(nErrCode == -1 || u16 != 0) { |
| 7950 | return 22; |
| 7951 | } |
| 7952 | |
| 7953 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 7954 | if(nErrCode == -1 || u16 != 1) { |
| 7955 | return 23; |
| 7956 | } |
| 7957 | |
| 7958 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 7959 | if(nErrCode != -1) { |
| 7960 | return 24; |
| 7961 | } |
| 7962 | |
| 7963 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 7964 | if(nErrCode != -1) { |
| 7965 | return 25; |
| 7966 | } |
| 7967 | |
| 7968 | |
| 7969 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 7970 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 7971 | return 26; |
| 7972 | } |
| 7973 | |
| 7974 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 7975 | if(nErrCode == -1 || u8 != 0) { |
| 7976 | return 27; |
| 7977 | } |
| 7978 | |
| 7979 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 7980 | if(nErrCode == -1 || u8 != 1) { |
| 7981 | return 28; |
| 7982 | } |
| 7983 | |
| 7984 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 7985 | if(nErrCode != -1) { |
| 7986 | return 29; |
| 7987 | } |
| 7988 | |
| 7989 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 7990 | if(nErrCode != -1) { |
| 7991 | return 30; |
| 7992 | } |
| 7993 | |
| 7994 | |
| 7995 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 7996 | if(nErrCode == -1 || u64 != 1) { |
| 7997 | return 31; |
| 7998 | } |
| 7999 | |
| 8000 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 8001 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 8002 | return 32; |
| 8003 | } |
| 8004 | |
| 8005 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 8006 | if(nErrCode == -1 || u64 != 0) { |
| 8007 | return 33; |
| 8008 | } |
| 8009 | |
| 8010 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 8011 | if(nErrCode != -1) { |
| 8012 | return 34; |
| 8013 | } |
| 8014 | |
| 8015 | return 0; |
| 8016 | } |
| 8017 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8018 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8019 | |
| 8020 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8021 | /* |
| 8022 | A sequence with |
| 8023 | A wrapping bstr |
| 8024 | containing a map |
| 8025 | 1 |
| 8026 | 2 |
| 8027 | A wrapping bstr |
| 8028 | containing an array |
| 8029 | 3 |
| 8030 | wrapping bstr |
| 8031 | 4 |
| 8032 | 5 |
| 8033 | 6 |
| 8034 | array |
| 8035 | 7 |
| 8036 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8037 | */ |
| 8038 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8039 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8040 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8041 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8042 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8043 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8044 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8045 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8046 | |
| 8047 | QCBOREncode_BstrWrap(&EC); |
| 8048 | QCBOREncode_OpenMap(&EC); |
| 8049 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 8050 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 8051 | QCBOREncode_CloseMap(&EC); |
| 8052 | QCBOREncode_BstrWrap(&EC); |
| 8053 | QCBOREncode_OpenArray(&EC); |
| 8054 | QCBOREncode_AddInt64(&EC, 3); |
| 8055 | QCBOREncode_BstrWrap(&EC); |
| 8056 | QCBOREncode_AddInt64(&EC, 4); |
| 8057 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8058 | QCBOREncode_AddInt64(&EC, 5); |
| 8059 | QCBOREncode_CloseArray(&EC); |
| 8060 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8061 | QCBOREncode_AddInt64(&EC, 6); |
| 8062 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 8063 | QCBOREncode_OpenArray(&EC); |
| 8064 | QCBOREncode_AddInt64(&EC, 7); |
| 8065 | QCBOREncode_AddInt64(&EC, 8); |
| 8066 | QCBOREncode_CloseArray(&EC); |
| 8067 | |
| 8068 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 8069 | if(uErr) { |
| 8070 | Encoded = NULLUsefulBufC; |
| 8071 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8072 | |
| 8073 | return Encoded; |
| 8074 | } |
| 8075 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8076 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8077 | static const uint8_t spBreakInByteString[] = { |
| 8078 | 0x41, 0xff |
| 8079 | }; |
| 8080 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8081 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8082 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8083 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8084 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8085 | |
| 8086 | QCBORDecodeContext DC; |
| 8087 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8088 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8089 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8090 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8091 | |
| 8092 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8093 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8094 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8095 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 8096 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8097 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8098 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8099 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8100 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8101 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8102 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8103 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8104 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8105 | QCBORDecode_ExitArray(&DC); |
| 8106 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8107 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8108 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8109 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 8110 | QCBORDecode_GetInt64(&DC, &n7); |
| 8111 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8112 | QCBORDecode_ExitArray(&DC); |
| 8113 | |
| 8114 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8115 | if(uErr) { |
| 8116 | return (int32_t)uErr; |
| 8117 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8118 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8119 | |
| 8120 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 8121 | * Successful because no items are fetched from byte string. |
| 8122 | */ |
| 8123 | QCBORDecode_Init(&DC, |
| 8124 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8125 | 0); |
| 8126 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8127 | uErr = QCBORDecode_GetError(&DC); |
| 8128 | if(uErr) { |
| 8129 | return 100 + (int32_t)uErr; |
| 8130 | } |
| 8131 | |
| 8132 | QCBORDecode_ExitBstrWrapped(&DC); |
| 8133 | uErr = QCBORDecode_GetError(&DC); |
| 8134 | if(uErr) { |
| 8135 | return 200 + (int32_t)uErr; |
| 8136 | } |
| 8137 | |
| 8138 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 8139 | * It fails because there should be no break. |
| 8140 | */ |
| 8141 | QCBORDecode_Init(&DC, |
| 8142 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 8143 | 0); |
| 8144 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8145 | QCBORItem Item; |
| 8146 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 8147 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 8148 | return 300 + (int32_t)uErr; |
| 8149 | } |
| 8150 | |
| 8151 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 8152 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8153 | |
| 8154 | |
| 8155 | |
| 8156 | |
| 8157 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8158 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8159 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8160 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8161 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8162 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 8163 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 8164 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8165 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8166 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8167 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 8168 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 8169 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8170 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8171 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8172 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8173 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8174 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8175 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8176 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8177 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 8178 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8179 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8180 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8181 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8182 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 8183 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8184 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8185 | 21, |
| 8186 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 8187 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 8188 | |
| 8189 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8190 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8191 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8192 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8193 | |
| 8194 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8195 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8196 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8197 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8198 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8199 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8200 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 8201 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8202 | |
| 8203 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8204 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 8205 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8206 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8207 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8208 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8209 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 8210 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8211 | |
| 8212 | 0x18, 0x33, |
| 8213 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 8214 | |
| 8215 | // MIME |
| 8216 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8217 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 8218 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 8219 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8220 | |
| 8221 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8222 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8223 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8224 | |
| 8225 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8226 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 8227 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 8228 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8229 | |
| 8230 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8231 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 8232 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8233 | |
| 8234 | // UUID |
| 8235 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8236 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 8237 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8238 | |
| 8239 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8240 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 8241 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8242 | }; |
| 8243 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8244 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8245 | { |
| 8246 | QCBORDecodeContext DC; |
| 8247 | QCBORError uErr; |
| 8248 | |
| 8249 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 8250 | |
| 8251 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8252 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8253 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8254 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8255 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8256 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8257 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 8258 | return 1; |
| 8259 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8260 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8261 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8262 | return 2; |
| 8263 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8264 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8265 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8266 | return 3; |
| 8267 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8268 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8269 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8270 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8271 | return 4; |
| 8272 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8273 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8274 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8275 | return 5; |
| 8276 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8277 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8278 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8279 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8280 | bNeg != false) { |
| 8281 | return 10; |
| 8282 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8283 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8284 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8285 | bNeg != true) { |
| 8286 | return 11; |
| 8287 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8288 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8289 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8290 | return 12; |
| 8291 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8292 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8293 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8294 | return 13; |
| 8295 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8296 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8297 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8298 | return 14; |
| 8299 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8300 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8301 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8302 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8303 | return 20; |
| 8304 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8305 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8306 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8307 | return 21; |
| 8308 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8309 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8310 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8311 | return 22; |
| 8312 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8313 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8314 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8315 | return 23; |
| 8316 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8317 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8318 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8319 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8320 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8321 | return 30; |
| 8322 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8323 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8324 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8325 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8326 | return 31; |
| 8327 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8328 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8329 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8330 | return 32; |
| 8331 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8332 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8333 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8334 | return 33; |
| 8335 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8336 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8337 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8338 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8339 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8340 | return 40; |
| 8341 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8342 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8343 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8344 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8345 | return 41; |
| 8346 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8347 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8348 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8349 | return 42; |
| 8350 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8351 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8352 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8353 | return 43; |
| 8354 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8355 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8356 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8357 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8358 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8359 | return 50; |
| 8360 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8361 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8362 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8363 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8364 | return 51; |
| 8365 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8366 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8367 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8368 | return 52; |
| 8369 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8370 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8371 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8372 | return 53; |
| 8373 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8374 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8375 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8376 | // MIME |
| 8377 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8378 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8379 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8380 | bIsNot7Bit == true) { |
| 8381 | return 60; |
| 8382 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8383 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8384 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8385 | bIsNot7Bit == true) { |
| 8386 | return 61; |
| 8387 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8388 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8389 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8390 | bIsNot7Bit == false) { |
| 8391 | return 62; |
| 8392 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8393 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8394 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 8395 | bIsNot7Bit == false) { |
| 8396 | return 63; |
| 8397 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8398 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8399 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8400 | return 64; |
| 8401 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8402 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8403 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8404 | return 65; |
| 8405 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8406 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8407 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8408 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8409 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8410 | return 70; |
| 8411 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 8412 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 8413 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8414 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8415 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 8416 | return 71; |
| 8417 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8418 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8419 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8420 | return 72; |
| 8421 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 8422 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 8423 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 8424 | return 73; |
| 8425 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8426 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 8427 | // Improvement: add some more error test cases |
| 8428 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 8429 | QCBORDecode_ExitMap(&DC); |
| 8430 | |
| 8431 | uErr = QCBORDecode_Finish(&DC); |
| 8432 | if(uErr != QCBOR_SUCCESS) { |
| 8433 | return 100; |
| 8434 | } |
| 8435 | |
| 8436 | return 0; |
| 8437 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8438 | |
| 8439 | |
| 8440 | |
| 8441 | |
| 8442 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8443 | [ |
| 8444 | "aaaaaaaaaa", |
| 8445 | {} |
| 8446 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8447 | */ |
| 8448 | static const uint8_t spTooLarge1[] = { |
| 8449 | 0x9f, |
| 8450 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8451 | 0xa0, |
| 8452 | 0xff |
| 8453 | }; |
| 8454 | |
| 8455 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8456 | [ |
| 8457 | { |
| 8458 | 0: "aaaaaaaaaa" |
| 8459 | } |
| 8460 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8461 | */ |
| 8462 | static const uint8_t spTooLarge2[] = { |
| 8463 | 0x9f, |
| 8464 | 0xa1, |
| 8465 | 0x00, |
| 8466 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8467 | 0xff |
| 8468 | }; |
| 8469 | |
| 8470 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8471 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8472 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 8473 | { |
| 8474 | 0: "aaaaaaaaaa" |
| 8475 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8476 | */ |
| 8477 | static const uint8_t spTooLarge3[] = { |
| 8478 | 0x4d, |
| 8479 | 0xa1, |
| 8480 | 0x00, |
| 8481 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 8482 | }; |
| 8483 | |
| 8484 | int32_t TooLargeInputTest(void) |
| 8485 | { |
| 8486 | QCBORDecodeContext DC; |
| 8487 | QCBORError uErr; |
| 8488 | UsefulBufC String; |
| 8489 | |
| 8490 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 8491 | // to 10 There's not really any way to test this error |
| 8492 | // condition. The error condition is not complex, so setting |
| 8493 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 8494 | |
| 8495 | // The input CBOR is only too large because the |
| 8496 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 8497 | // |
| 8498 | // This test is disabled for the normal test runs because of the |
| 8499 | // special build requirement. |
| 8500 | |
| 8501 | |
| 8502 | // Tests the start of a map being too large |
| 8503 | 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] | 8504 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8505 | QCBORDecode_GetTextString(&DC, &String); |
| 8506 | uErr = QCBORDecode_GetError(&DC); |
| 8507 | if(uErr != QCBOR_SUCCESS) { |
| 8508 | return 1; |
| 8509 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8510 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8511 | uErr = QCBORDecode_GetError(&DC); |
| 8512 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8513 | return 2; |
| 8514 | } |
| 8515 | |
| 8516 | // Tests the end of a map being too large |
| 8517 | 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] | 8518 | QCBORDecode_EnterArray(&DC, NULL); |
| 8519 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 8520 | uErr = QCBORDecode_GetError(&DC); |
| 8521 | if(uErr != QCBOR_SUCCESS) { |
| 8522 | return 3; |
| 8523 | } |
| 8524 | QCBORDecode_ExitMap(&DC); |
| 8525 | uErr = QCBORDecode_GetError(&DC); |
| 8526 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8527 | return 4; |
| 8528 | } |
| 8529 | |
| 8530 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 8531 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 8532 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8533 | uErr = QCBORDecode_GetError(&DC); |
| 8534 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 8535 | return 5; |
| 8536 | } |
| 8537 | |
| 8538 | return 0; |
| 8539 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8540 | |
| 8541 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8542 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 8543 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8544 | /* |
| 8545 | An array of three map entries |
| 8546 | 1) Indefinite length string label for indefinite lenght byte string |
| 8547 | 2) Indefinite length string label for an integer |
| 8548 | 3) Indefinite length string label for an indefinite-length negative big num |
| 8549 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8550 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8551 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8552 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 8553 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 8554 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 8555 | 0x03, |
| 8556 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 8557 | 0xc3, |
| 8558 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8559 | }; |
| 8560 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8561 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8562 | { |
| 8563 | QCBORDecodeContext DCtx; |
| 8564 | |
| 8565 | QCBORDecode_Init(&DCtx, |
| 8566 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 8567 | QCBOR_DECODE_MODE_NORMAL); |
| 8568 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 8569 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8570 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 8571 | |
| 8572 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 8573 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8574 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8575 | |
| 8576 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8577 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8578 | return 1; |
| 8579 | } |
| 8580 | |
| 8581 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 8582 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 8583 | return 2; |
| 8584 | } |
| 8585 | |
| 8586 | uint64_t uInt; |
| 8587 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 8588 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8589 | return 3; |
| 8590 | } |
| 8591 | if(uInt != 3) { |
| 8592 | return 4; |
| 8593 | } |
| 8594 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8595 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8596 | double uDouble; |
| 8597 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 8598 | "label2", |
| 8599 | 0xff, |
| 8600 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8601 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8602 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8603 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8604 | return 5; |
| 8605 | } |
| 8606 | if(uDouble != -16777474) { |
| 8607 | return 6; |
| 8608 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8609 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8610 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 8611 | return 7; |
| 8612 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 8613 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 8614 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 8615 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8616 | QCBORDecode_ExitMap(&DCtx); |
| 8617 | |
| 8618 | if(QCBORDecode_Finish(&DCtx)) { |
| 8619 | return 99; |
| 8620 | } |
| 8621 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8622 | #else /* QCBOR_DISABLE_TAGS */ |
| 8623 | /* The big num in the input is a CBOR tag and you can't do |
| 8624 | * map lookups in a map with a tag so this test does very little |
| 8625 | * when tags are disabled. That is OK, the test coverage is still |
| 8626 | * good when they are not. |
| 8627 | */ |
| 8628 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 8629 | return 1002; |
| 8630 | } |
| 8631 | #endif /*QCBOR_DISABLE_TAGS */ |
| 8632 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 8633 | return 0; |
| 8634 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 8635 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8636 | |
| 8637 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8638 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8639 | /* |
| 8640 | * An array of an integer and an array. The second array contains |
| 8641 | * a bstr-wrapped map. |
| 8642 | * |
| 8643 | * [7, [h'A36D6669... (see next lines) 73']] |
| 8644 | * |
| 8645 | * {"first integer": 42, |
| 8646 | * "an array of two strings": ["string1", "string2"], |
| 8647 | * "map in a map": |
| 8648 | * { "bytes 1": h'78787878', |
| 8649 | * "bytes 2": h'79797979', |
| 8650 | * "another int": 98, |
| 8651 | * "text 2": "lies, damn lies and statistics" |
| 8652 | * } |
| 8653 | * } |
| 8654 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8655 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8656 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 8657 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 8658 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8659 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8660 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8661 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8662 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8663 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8664 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8665 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8666 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8667 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8668 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8669 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8670 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8671 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8672 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8673 | 0x73 |
| 8674 | }; |
| 8675 | |
| 8676 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8677 | |
| 8678 | /* As above, but the arrays are indefinite length */ |
| 8679 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 8680 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 8681 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 8682 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 8683 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 8684 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 8685 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 8686 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 8687 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 8688 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 8689 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8690 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8691 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8692 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8693 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8694 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8695 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8696 | 0x73, |
| 8697 | 0xff, 0xff |
| 8698 | }; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8699 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8700 | |
| 8701 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 8702 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8703 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8704 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8705 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 8706 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8707 | |
| 8708 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8709 | /* |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 8710 | * An array of one that contains a byte string that is an indefinite |
| 8711 | * length string that CBOR wraps an array of three numbers [42, 43, |
| 8712 | * 44]. The byte string is an implicit tag 24. |
| 8713 | * |
| 8714 | * [ |
| 8715 | * (_ h'83', h'18', h'2A182B', h'182C') |
| 8716 | * ] |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8717 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8718 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 8719 | 0x81, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8720 | 0x5f, |
| 8721 | 0x41, 0x83, |
| 8722 | 0x41, 0x18, |
| 8723 | 0x43, 0x2A, 0x18, 0x2B, |
| 8724 | 0x42, 0x18, 0x2C, |
| 8725 | 0xff |
| 8726 | }; |
| 8727 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8728 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8729 | #endif |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8730 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8731 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8732 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8733 | { |
| 8734 | QCBORItem Item; |
| 8735 | QCBORError nCBORError; |
| 8736 | QCBORDecodeContext DCtx; |
| 8737 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 8738 | // Improvement: rework this test to use only integer labels. |
| 8739 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8740 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8741 | |
| 8742 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8743 | return 100+(int32_t)nCBORError; |
| 8744 | } |
| 8745 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8746 | return 200; |
| 8747 | } |
| 8748 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8749 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8750 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 8751 | return 150+(int32_t)nCBORError; |
| 8752 | } |
| 8753 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8754 | return 250; |
| 8755 | } |
| 8756 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8757 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8758 | return (int32_t)nCBORError; |
| 8759 | } |
| 8760 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8761 | return 300; |
| 8762 | } |
| 8763 | |
| 8764 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8765 | return 400 + (int32_t)nCBORError; |
| 8766 | } |
| 8767 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8768 | return 500; |
| 8769 | } |
| 8770 | |
| 8771 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8772 | return (int32_t)nCBORError; |
| 8773 | } |
| 8774 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8775 | return 600; |
| 8776 | } |
| 8777 | |
| 8778 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8779 | return 900 + (int32_t)nCBORError; |
| 8780 | } |
| 8781 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8782 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8783 | Item.val.int64 != 42 || |
| 8784 | Item.uDataAlloc || |
| 8785 | Item.uLabelAlloc || |
| 8786 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8787 | return 1000; |
| 8788 | } |
| 8789 | |
| 8790 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8791 | return 1100 + (int32_t)nCBORError; |
| 8792 | } |
| 8793 | |
| 8794 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8795 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8796 | Item.val.int64 != 42 || |
| 8797 | Item.uDataAlloc || |
| 8798 | Item.uLabelAlloc || |
| 8799 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8800 | return 1200; |
| 8801 | } |
| 8802 | |
| 8803 | |
| 8804 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8805 | return 1300 + (int32_t)nCBORError; |
| 8806 | } |
| 8807 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8808 | Item.uDataAlloc || |
| 8809 | Item.uLabelAlloc || |
| 8810 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8811 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8812 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8813 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8814 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8815 | |
| 8816 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8817 | return 1500 + (int32_t)nCBORError; |
| 8818 | } |
| 8819 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8820 | Item.uDataAlloc || |
| 8821 | Item.uLabelAlloc || |
| 8822 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8823 | return 1600; |
| 8824 | } |
| 8825 | |
| 8826 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8827 | return 1700 + (int32_t)nCBORError; |
| 8828 | } |
| 8829 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8830 | Item.uDataAlloc || |
| 8831 | Item.uLabelAlloc || |
| 8832 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8833 | return 1800; |
| 8834 | } |
| 8835 | |
| 8836 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8837 | return (int32_t)nCBORError; |
| 8838 | } |
| 8839 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8840 | Item.uDataAlloc || |
| 8841 | Item.uLabelAlloc || |
| 8842 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8843 | return 1900; |
| 8844 | } |
| 8845 | |
| 8846 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8847 | return (int32_t)nCBORError; |
| 8848 | } |
| 8849 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8850 | Item.uDataAlloc || |
| 8851 | Item.uLabelAlloc || |
| 8852 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8853 | return 2000; |
| 8854 | } |
| 8855 | |
| 8856 | |
| 8857 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8858 | return 2100 + (int32_t)nCBORError; |
| 8859 | } |
| 8860 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8861 | Item.uDataAlloc || |
| 8862 | Item.uLabelAlloc || |
| 8863 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 8864 | Item.uDataType != QCBOR_TYPE_MAP || |
| 8865 | Item.val.uCount != 4) { |
| 8866 | return 2100; |
| 8867 | } |
| 8868 | |
| 8869 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8870 | return 2200 + (int32_t)nCBORError; |
| 8871 | } |
| 8872 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8873 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 8874 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8875 | Item.uDataAlloc || |
| 8876 | Item.uLabelAlloc || |
| 8877 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 8878 | return 2300; |
| 8879 | } |
| 8880 | |
| 8881 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8882 | return 2400 + (int32_t)nCBORError; |
| 8883 | } |
| 8884 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8885 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8886 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8887 | Item.uDataAlloc || |
| 8888 | Item.uLabelAlloc || |
| 8889 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8890 | return 2500; |
| 8891 | } |
| 8892 | |
| 8893 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8894 | return 2600 + (int32_t)nCBORError; |
| 8895 | } |
| 8896 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8897 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8898 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8899 | Item.uDataAlloc || |
| 8900 | Item.uLabelAlloc || |
| 8901 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8902 | return 2700; |
| 8903 | } |
| 8904 | |
| 8905 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8906 | return 2800 + (int32_t)nCBORError; |
| 8907 | } |
| 8908 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8909 | Item.uDataAlloc || |
| 8910 | Item.uLabelAlloc || |
| 8911 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 8912 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8913 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8914 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8915 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8916 | |
| 8917 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8918 | return 3000 + (int32_t)nCBORError; |
| 8919 | } |
| 8920 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8921 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8922 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8923 | Item.uDataAlloc || |
| 8924 | Item.uLabelAlloc || |
| 8925 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8926 | return 3100; |
| 8927 | } |
| 8928 | |
| 8929 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8930 | return 3200 + (int32_t)nCBORError; |
| 8931 | } |
| 8932 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8933 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8934 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8935 | Item.uDataAlloc || |
| 8936 | Item.uLabelAlloc || |
| 8937 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8938 | return 3300; |
| 8939 | } |
| 8940 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8941 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 8942 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8943 | return 3300 + (int32_t)nCBORError; |
| 8944 | } |
| 8945 | |
| 8946 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8947 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8948 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8949 | return 3400 + (int32_t)nCBORError; |
| 8950 | } |
| 8951 | |
| 8952 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8953 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8954 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8955 | return 3500 + (int32_t)nCBORError; |
| 8956 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8957 | |
| 8958 | |
| 8959 | // Rewind to top level after entering several maps |
| 8960 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8961 | |
| 8962 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8963 | return (int32_t)nCBORError; |
| 8964 | } |
| 8965 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8966 | Item.val.uCount != 3) { |
| 8967 | return 400; |
| 8968 | } |
| 8969 | |
| 8970 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8971 | return 4000+(int32_t)nCBORError; |
| 8972 | } |
| 8973 | |
| 8974 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8975 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8976 | Item.val.int64 != 42 || |
| 8977 | Item.uDataAlloc || |
| 8978 | Item.uLabelAlloc || |
| 8979 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8980 | return 4100; |
| 8981 | } |
| 8982 | |
| 8983 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8984 | return 4100+(int32_t)nCBORError; |
| 8985 | } |
| 8986 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8987 | Item.uDataAlloc || |
| 8988 | Item.uLabelAlloc || |
| 8989 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8990 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8991 | Item.val.uCount != 2) { |
| 8992 | return 4200; |
| 8993 | } |
| 8994 | |
| 8995 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8996 | return 4200+(int32_t)nCBORError; |
| 8997 | } |
| 8998 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8999 | Item.uDataAlloc || |
| 9000 | Item.uLabelAlloc || |
| 9001 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9002 | return 4300; |
| 9003 | } |
| 9004 | |
| 9005 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9006 | return 4300+(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 4400; |
| 9013 | } |
| 9014 | |
| 9015 | QCBORDecode_Rewind(&DCtx); |
| 9016 | |
| 9017 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9018 | return 4400+(int32_t)nCBORError; |
| 9019 | } |
| 9020 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 9021 | Item.val.uCount != 3) { |
| 9022 | return 4500; |
| 9023 | } |
| 9024 | |
| 9025 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9026 | return (int32_t)nCBORError; |
| 9027 | } |
| 9028 | |
| 9029 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9030 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9031 | Item.val.int64 != 42 || |
| 9032 | Item.uDataAlloc || |
| 9033 | Item.uLabelAlloc || |
| 9034 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9035 | return 4600; |
| 9036 | } |
| 9037 | |
| 9038 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9039 | return (int32_t)nCBORError; |
| 9040 | } |
| 9041 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9042 | Item.uDataAlloc || |
| 9043 | Item.uLabelAlloc || |
| 9044 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9045 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9046 | Item.val.uCount != 2) { |
| 9047 | return 4700; |
| 9048 | } |
| 9049 | |
| 9050 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9051 | return (int32_t)nCBORError; |
| 9052 | } |
| 9053 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9054 | Item.uDataAlloc || |
| 9055 | Item.uLabelAlloc || |
| 9056 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9057 | return 4800; |
| 9058 | } |
| 9059 | |
| 9060 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9061 | return 4900+(int32_t)nCBORError; |
| 9062 | } |
| 9063 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9064 | Item.uDataAlloc || |
| 9065 | Item.uLabelAlloc || |
| 9066 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9067 | return 5000; |
| 9068 | } |
| 9069 | |
| 9070 | |
| 9071 | // Rewind an entered map |
| 9072 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9073 | |
| 9074 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9075 | |
| 9076 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9077 | return 5100+(int32_t)nCBORError; |
| 9078 | } |
| 9079 | |
| 9080 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9081 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9082 | Item.val.int64 != 42 || |
| 9083 | Item.uDataAlloc || |
| 9084 | Item.uLabelAlloc || |
| 9085 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9086 | return 5200; |
| 9087 | } |
| 9088 | |
| 9089 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9090 | return 5200+(int32_t)nCBORError; |
| 9091 | } |
| 9092 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9093 | Item.uDataAlloc || |
| 9094 | Item.uLabelAlloc || |
| 9095 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9096 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9097 | Item.val.uCount != 2) { |
| 9098 | return -5300; |
| 9099 | } |
| 9100 | |
| 9101 | QCBORDecode_Rewind(&DCtx); |
| 9102 | |
| 9103 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9104 | return 5300+(int32_t)nCBORError; |
| 9105 | } |
| 9106 | |
| 9107 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9108 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 9109 | Item.val.int64 != 42 || |
| 9110 | Item.uDataAlloc || |
| 9111 | Item.uLabelAlloc || |
| 9112 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 9113 | return 5400; |
| 9114 | } |
| 9115 | |
| 9116 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9117 | return 5400+(int32_t)nCBORError; |
| 9118 | } |
| 9119 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9120 | Item.uDataAlloc || |
| 9121 | Item.uLabelAlloc || |
| 9122 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 9123 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 9124 | Item.val.uCount != 2) { |
| 9125 | return 5500; |
| 9126 | } |
| 9127 | |
| 9128 | |
| 9129 | // Rewind and entered array inside an entered map |
| 9130 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 9131 | |
| 9132 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9133 | |
| 9134 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 9135 | |
| 9136 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9137 | return 5600+(int32_t)nCBORError; |
| 9138 | } |
| 9139 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9140 | Item.uDataAlloc || |
| 9141 | Item.uLabelAlloc || |
| 9142 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9143 | return 5700; |
| 9144 | } |
| 9145 | |
| 9146 | QCBORDecode_Rewind(&DCtx); |
| 9147 | |
| 9148 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9149 | return 5700+(int32_t)nCBORError; |
| 9150 | } |
| 9151 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9152 | Item.uDataAlloc || |
| 9153 | Item.uLabelAlloc || |
| 9154 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9155 | return 5800; |
| 9156 | } |
| 9157 | |
| 9158 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9159 | return (int32_t)nCBORError; |
| 9160 | } |
| 9161 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9162 | Item.uDataAlloc || |
| 9163 | Item.uLabelAlloc || |
| 9164 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 9165 | return 5900; |
| 9166 | } |
| 9167 | |
| 9168 | QCBORDecode_Rewind(&DCtx); |
| 9169 | |
| 9170 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9171 | return 5900+(int32_t)nCBORError; |
| 9172 | } |
| 9173 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 9174 | Item.uDataAlloc || |
| 9175 | Item.uLabelAlloc || |
| 9176 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 9177 | return 6000; |
| 9178 | } |
| 9179 | |
| 9180 | |
| 9181 | // Rewind a byte string inside an array inside an array |
| 9182 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 9183 | |
| 9184 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9185 | |
| 9186 | uint64_t i; |
| 9187 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9188 | |
| 9189 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9190 | |
| 9191 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9192 | if(QCBORDecode_GetError(&DCtx)) { |
| 9193 | return 6100; |
| 9194 | } |
| 9195 | |
| 9196 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9197 | return (int32_t)nCBORError; |
| 9198 | } |
| 9199 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9200 | return 6200; |
| 9201 | } |
| 9202 | |
| 9203 | QCBORDecode_Rewind(&DCtx); |
| 9204 | |
| 9205 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9206 | return 6300+(int32_t)nCBORError; |
| 9207 | } |
| 9208 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9209 | return 6400; |
| 9210 | } |
| 9211 | |
| 9212 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9213 | // Rewind a byte string inside an indefinite-length array inside |
| 9214 | // indefinite-length array |
| 9215 | |
| 9216 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 9217 | |
| 9218 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9219 | |
| 9220 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9221 | |
| 9222 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9223 | |
| 9224 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 9225 | if(QCBORDecode_GetError(&DCtx)) { |
| 9226 | return 6500; |
| 9227 | } |
| 9228 | |
| 9229 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9230 | return 6600+(int32_t)nCBORError; |
| 9231 | } |
| 9232 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9233 | return 6700; |
| 9234 | } |
| 9235 | |
| 9236 | QCBORDecode_Rewind(&DCtx); |
| 9237 | |
| 9238 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 9239 | return 6800+(int32_t)nCBORError; |
| 9240 | } |
| 9241 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 9242 | return 6900; |
| 9243 | } |
| 9244 | #endif |
| 9245 | |
| 9246 | // Rewind an empty map |
| 9247 | // [100, {}] |
| 9248 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 9249 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9250 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9251 | if(i != 100) { |
| 9252 | return 7010; |
| 9253 | } |
| 9254 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9255 | |
| 9256 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9257 | for(int n = 0; n < 5; n++) { |
| 9258 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9259 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9260 | return 7000 + n; |
| 9261 | } |
| 9262 | QCBORDecode_Rewind(&DCtx); |
| 9263 | } |
| 9264 | QCBORDecode_ExitMap(&DCtx); |
| 9265 | QCBORDecode_Rewind(&DCtx); |
| 9266 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9267 | if(i != 100) { |
| 9268 | return 7010; |
| 9269 | } |
| 9270 | QCBORDecode_ExitArray(&DCtx); |
| 9271 | QCBORDecode_Rewind(&DCtx); |
| 9272 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9273 | i = 9; |
| 9274 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9275 | if(i != 100) { |
| 9276 | return 7020; |
| 9277 | } |
| 9278 | if(QCBORDecode_GetError(&DCtx)){ |
| 9279 | return 7030; |
| 9280 | } |
| 9281 | |
| 9282 | // Rewind an empty indefinite length map |
| 9283 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9284 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 9285 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9286 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9287 | if(i != 100) { |
| 9288 | return 7810; |
| 9289 | } |
| 9290 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9291 | |
| 9292 | /* Do it 5 times to be sure multiple rewinds work */ |
| 9293 | for(int n = 0; n < 5; n++) { |
| 9294 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 9295 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 9296 | return 7800 + n; |
| 9297 | } |
| 9298 | QCBORDecode_Rewind(&DCtx); |
| 9299 | } |
| 9300 | QCBORDecode_ExitMap(&DCtx); |
| 9301 | QCBORDecode_Rewind(&DCtx); |
| 9302 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9303 | if(i != 100) { |
| 9304 | return 7810; |
| 9305 | } |
| 9306 | QCBORDecode_ExitArray(&DCtx); |
| 9307 | QCBORDecode_Rewind(&DCtx); |
| 9308 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9309 | i = 9; |
| 9310 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9311 | if(i != 100) { |
| 9312 | return 7820; |
| 9313 | } |
| 9314 | if(QCBORDecode_GetError(&DCtx)){ |
| 9315 | return 7830; |
| 9316 | } |
| 9317 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9318 | |
| 9319 | // Rewind an indefnite length byte-string wrapped sequence |
| 9320 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 9321 | QCBORDecode_Init(&DCtx, |
| 9322 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 9323 | 0); |
| 9324 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 9325 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 9326 | |
| 9327 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9328 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9329 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9330 | return 7300; |
| 9331 | } |
| 9332 | |
| 9333 | /* |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9334 | Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on |
| 9335 | allocated strings. This is a fairly big job because of all the |
| 9336 | UsefulBuf internal book keeping that needs tweaking. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9337 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9338 | if(i != 42) { |
| 9339 | return 7110; |
| 9340 | } |
| 9341 | QCBORDecode_Rewind(&DCtx); |
| 9342 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 9343 | if(i != 42) { |
| 9344 | return 7220; |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 9345 | } |
| 9346 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 9347 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9348 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 9349 | |
| 9350 | |
| 9351 | // Rewind an indefnite length byte-string wrapped sequence |
| 9352 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 9353 | return 0; |
| 9354 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9355 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9356 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9357 | |
| 9358 | |
| 9359 | static const uint8_t spBooleansInMap[] = |
| 9360 | { |
| 9361 | 0xa1, 0x08, 0xf5 |
| 9362 | }; |
| 9363 | |
| 9364 | static const uint8_t spBooleansInMapWrongType[] = |
| 9365 | { |
| 9366 | 0xa1, 0x08, 0xf6 |
| 9367 | }; |
| 9368 | |
| 9369 | static const uint8_t spBooleansInMapNWF[] = |
| 9370 | { |
| 9371 | 0xa1, 0x08, 0x1a |
| 9372 | }; |
| 9373 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9374 | static const uint8_t spNullInMap[] = |
| 9375 | { |
| 9376 | 0xa1, 0x08, 0xf6 |
| 9377 | }; |
| 9378 | |
| 9379 | static const uint8_t spUndefinedInMap[] = |
| 9380 | { |
| 9381 | 0xa1, 0x08, 0xf7 |
| 9382 | }; |
| 9383 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9384 | |
| 9385 | int32_t BoolTest(void) |
| 9386 | { |
| 9387 | QCBORDecodeContext DCtx; |
| 9388 | bool b; |
| 9389 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9390 | QCBORDecode_Init(&DCtx, |
| 9391 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9392 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9393 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9394 | QCBORDecode_GetBool(&DCtx, &b); |
| 9395 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9396 | return 1; |
| 9397 | } |
| 9398 | |
| 9399 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 9400 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9401 | return 2; |
| 9402 | } |
| 9403 | |
| 9404 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 9405 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 9406 | return 3; |
| 9407 | } |
| 9408 | |
| 9409 | |
| 9410 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 9411 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 9412 | return 4; |
| 9413 | } |
| 9414 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9415 | QCBORDecode_Init(&DCtx, |
| 9416 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 9417 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9418 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9419 | QCBORDecode_GetBool(&DCtx, &b); |
| 9420 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9421 | return 5; |
| 9422 | } |
| 9423 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9424 | QCBORDecode_Init(&DCtx, |
| 9425 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9426 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9427 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9428 | QCBORDecode_GetBool(&DCtx, &b); |
| 9429 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9430 | return 6; |
| 9431 | } |
| 9432 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 9433 | |
| 9434 | QCBORDecode_Init(&DCtx, |
| 9435 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9436 | 0); |
| 9437 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9438 | QCBORDecode_GetNull(&DCtx); |
| 9439 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9440 | return 7; |
| 9441 | } |
| 9442 | |
| 9443 | QCBORDecode_Init(&DCtx, |
| 9444 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9445 | 0); |
| 9446 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9447 | QCBORDecode_GetNull(&DCtx); |
| 9448 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9449 | return 8; |
| 9450 | } |
| 9451 | |
| 9452 | QCBORDecode_Init(&DCtx, |
| 9453 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 9454 | 0); |
| 9455 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9456 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9457 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9458 | return 9; |
| 9459 | } |
| 9460 | |
| 9461 | QCBORDecode_Init(&DCtx, |
| 9462 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9463 | 0); |
| 9464 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9465 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 9466 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9467 | return 10; |
| 9468 | } |
| 9469 | |
| 9470 | QCBORDecode_Init(&DCtx, |
| 9471 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9472 | 0); |
| 9473 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9474 | QCBORDecode_GetUndefined(&DCtx); |
| 9475 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9476 | return 11; |
| 9477 | } |
| 9478 | |
| 9479 | QCBORDecode_Init(&DCtx, |
| 9480 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9481 | 0); |
| 9482 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9483 | QCBORDecode_GetUndefined(&DCtx); |
| 9484 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9485 | return 12; |
| 9486 | } |
| 9487 | |
| 9488 | QCBORDecode_Init(&DCtx, |
| 9489 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9490 | 0); |
| 9491 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9492 | QCBORDecode_GetUndefined(&DCtx); |
| 9493 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9494 | return 13; |
| 9495 | } |
| 9496 | |
| 9497 | QCBORDecode_Init(&DCtx, |
| 9498 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 9499 | 0); |
| 9500 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9501 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9502 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 9503 | return 14; |
| 9504 | } |
| 9505 | |
| 9506 | QCBORDecode_Init(&DCtx, |
| 9507 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 9508 | 0); |
| 9509 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9510 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 9511 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9512 | return 15; |
| 9513 | } |
| 9514 | |
| 9515 | QCBORDecode_Init(&DCtx, |
| 9516 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 9517 | 0); |
| 9518 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9519 | QCBORDecode_GetUndefined(&DCtx); |
| 9520 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 9521 | return 15; |
| 9522 | } |
| 9523 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 9524 | return 0; |
| 9525 | } |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9526 | |
| 9527 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9528 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9529 | static const uint8_t spExpectedArray2s[] = { |
| 9530 | 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9531 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9532 | 0x32}; |
| 9533 | |
| 9534 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9535 | static const uint8_t spExpectedArray2sIndef[] = { |
| 9536 | 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9537 | 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 9538 | 0x32, 0xff}; |
| 9539 | #endif |
| 9540 | |
| 9541 | static const uint8_t spExpectedMap4[] = { |
| 9542 | 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9543 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9544 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9545 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9546 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9547 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9548 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9549 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9550 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9551 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73}; |
| 9552 | |
| 9553 | |
| 9554 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9555 | |
| 9556 | static const uint8_t spExpectedMap4Indef[] = { |
| 9557 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 9558 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 9559 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 9560 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 9561 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 9562 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 9563 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 9564 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 9565 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 9566 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 9567 | 0xff}; |
| 9568 | |
| 9569 | /* |
| 9570 | * [[[[[0, []]]]], 0] |
| 9571 | */ |
| 9572 | static const uint8_t spDefAndIndef[] = { |
| 9573 | 0x82, |
| 9574 | 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00 |
| 9575 | }; |
| 9576 | #endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 9577 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9578 | #ifndef QCBOR_DISABLE_TAGS |
| 9579 | /* An exp / mant tag in two nested arrays */ |
| 9580 | static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03}; |
| 9581 | #endif /* !QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9582 | #endif |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9583 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9584 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9585 | int32_t GetMapAndArrayTest(void) |
| 9586 | { |
| 9587 | QCBORDecodeContext DCtx; |
| 9588 | size_t uPosition ; |
| 9589 | QCBORItem Item; |
| 9590 | UsefulBufC ReturnedEncodedCBOR; |
| 9591 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9592 | // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9593 | QCBORDecode_Init(&DCtx, |
| 9594 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9595 | 0); |
| 9596 | |
| 9597 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9598 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9599 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9600 | if(QCBORDecode_GetError(&DCtx)) { |
| 9601 | return 1; |
| 9602 | } |
| 9603 | if(Item.val.uCount != 2) { |
| 9604 | return 2; |
| 9605 | } |
| 9606 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9607 | return 3; |
| 9608 | } |
| 9609 | |
| 9610 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9611 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9612 | return 4; |
| 9613 | } |
| 9614 | |
| 9615 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9616 | |
| 9617 | |
| 9618 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9619 | if(QCBORDecode_GetError(&DCtx)) { |
| 9620 | return 10; |
| 9621 | } |
| 9622 | if(Item.val.uCount != 4) { |
| 9623 | return 11; |
| 9624 | } |
| 9625 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9626 | return 12; |
| 9627 | } |
| 9628 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9629 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 9630 | "an array of two strings", |
| 9631 | &Item, |
| 9632 | &ReturnedEncodedCBOR); |
| 9633 | if(QCBORDecode_GetError(&DCtx)) { |
| 9634 | return 20; |
| 9635 | } |
| 9636 | if(Item.val.uCount != 2) { |
| 9637 | return 21; |
| 9638 | } |
| 9639 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) { |
| 9640 | return 22; |
| 9641 | } |
| 9642 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9643 | return 23; |
| 9644 | } |
| 9645 | |
| 9646 | QCBORDecode_Rewind(&DCtx); |
| 9647 | |
| 9648 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9649 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9650 | if(QCBORDecode_GetError(&DCtx)) { |
| 9651 | return 30; |
| 9652 | } |
| 9653 | if(Item.val.uCount != 4) { |
| 9654 | return 31; |
| 9655 | } |
| 9656 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) { |
| 9657 | return 32; |
| 9658 | } |
| 9659 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9660 | return 33; |
| 9661 | } |
| 9662 | |
| 9663 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9664 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9665 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9666 | return 40; |
| 9667 | } |
| 9668 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 9669 | return 41; |
| 9670 | } |
| 9671 | QCBORDecode_GetAndResetError(&DCtx); |
| 9672 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9673 | return 42; |
| 9674 | } |
| 9675 | |
| 9676 | |
| 9677 | #ifndef QCBOR_DISABLE_TAGS |
| 9678 | UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant); |
| 9679 | QCBORDecode_Init(&DCtx, ExpMant, 0); |
| 9680 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9681 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9682 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9683 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9684 | return 200; |
| 9685 | } |
| 9686 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 9687 | return 201; |
| 9688 | } |
| 9689 | if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) { |
| 9690 | return 202; |
| 9691 | } |
| 9692 | if(Item.val.uCount != 2) { |
| 9693 | return 201; |
| 9694 | } |
| 9695 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) { |
| 9696 | return 205; |
| 9697 | } |
| 9698 | #endif /* !QCBOR_DISABLE_TAGS */ |
| 9699 | |
| 9700 | |
| 9701 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 9702 | |
| 9703 | UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef); |
| 9704 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9705 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9706 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9707 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9708 | return 50; |
| 9709 | } |
| 9710 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) { |
| 9711 | return 51; |
| 9712 | } |
| 9713 | |
| 9714 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9715 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9716 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9717 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9718 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9719 | return 52; |
| 9720 | } |
| 9721 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) { |
| 9722 | return 53; |
| 9723 | } |
| 9724 | |
| 9725 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9726 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9727 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9728 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9729 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9730 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9731 | return 54; |
| 9732 | } |
| 9733 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) { |
| 9734 | return 55; |
| 9735 | } |
| 9736 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9737 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9738 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9739 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9740 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9741 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9742 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9743 | return 56; |
| 9744 | } |
| 9745 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) { |
| 9746 | return 57; |
| 9747 | } |
| 9748 | |
| 9749 | QCBORDecode_Init(&DCtx, DefAndIndef, 0); |
| 9750 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9751 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9752 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9753 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9754 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 9755 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9756 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9757 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 9758 | return 58; |
| 9759 | } |
| 9760 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) { |
| 9761 | return 59; |
| 9762 | } |
| 9763 | |
| 9764 | |
| 9765 | QCBORDecode_Init(&DCtx, |
| 9766 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 9767 | 0); |
| 9768 | |
| 9769 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 9770 | QCBORDecode_VGetNextConsume(&DCtx, &Item); |
| 9771 | QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9772 | if(QCBORDecode_GetError(&DCtx)) { |
| 9773 | return 60; |
| 9774 | } |
| 9775 | if(Item.val.uCount != UINT16_MAX) { |
| 9776 | return 61; |
| 9777 | } |
| 9778 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 9779 | return 62; |
| 9780 | } |
| 9781 | |
| 9782 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 9783 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 9784 | return 63; |
| 9785 | } |
| 9786 | |
| 9787 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9788 | |
| 9789 | |
| 9790 | QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR); |
| 9791 | if(QCBORDecode_GetError(&DCtx)) { |
| 9792 | return 70; |
| 9793 | } |
| 9794 | if(Item.val.uCount != UINT16_MAX) { |
| 9795 | return 71; |
| 9796 | } |
| 9797 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 9798 | return 72; |
| 9799 | } |
| 9800 | |
| 9801 | |
| 9802 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9803 | QCBORDecode_GetArrayFromMapSZ(&DCtx, |
| 9804 | "an array of two strings", |
| 9805 | &Item, |
| 9806 | &ReturnedEncodedCBOR); |
| 9807 | if(QCBORDecode_GetError(&DCtx)) { |
| 9808 | return 80; |
| 9809 | } |
| 9810 | if(Item.val.uCount != UINT16_MAX) { |
| 9811 | return 81; |
| 9812 | } |
| 9813 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) { |
| 9814 | return 82; |
| 9815 | } |
| 9816 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9817 | return 83; |
| 9818 | } |
| 9819 | |
| 9820 | QCBORDecode_Rewind(&DCtx); |
| 9821 | |
| 9822 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9823 | QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9824 | if(QCBORDecode_GetError(&DCtx)) { |
| 9825 | return 90; |
| 9826 | } |
| 9827 | if(Item.val.uCount != UINT16_MAX) { |
| 9828 | return 91; |
| 9829 | } |
| 9830 | if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) { |
| 9831 | return 92; |
| 9832 | } |
| 9833 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9834 | return 93; |
| 9835 | } |
| 9836 | |
| 9837 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9838 | QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR); |
| 9839 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9840 | return 100; |
| 9841 | } |
| 9842 | if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) { |
| 9843 | return 101; |
| 9844 | } |
| 9845 | QCBORDecode_GetAndResetError(&DCtx); |
| 9846 | if(uPosition != QCBORDecode_Tell(&DCtx)) { |
| 9847 | return 102; |
| 9848 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9849 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9850 | |
| 9851 | return 0; |
| 9852 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9853 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 9854 | |
| 9855 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9856 | int32_t |
| 9857 | ErrorHandlingTests(void) |
| 9858 | { |
| 9859 | QCBORDecodeContext DCtx; |
| 9860 | QCBORItem Item; |
| 9861 | QCBORError uError; |
| 9862 | int64_t integer; |
| 9863 | |
| 9864 | /* Test QCBORDecode_SetError() */ |
| 9865 | QCBORDecode_Init(&DCtx, |
| 9866 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9867 | QCBOR_DECODE_MODE_NORMAL); |
| 9868 | |
| 9869 | QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED); |
| 9870 | |
| 9871 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9872 | |
| 9873 | uError = QCBORDecode_GetError(&DCtx); |
| 9874 | |
| 9875 | if(uError != QCBOR_ERR_FIRST_USER_DEFINED) { |
| 9876 | return -1; |
| 9877 | } |
| 9878 | |
| 9879 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 9880 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 9881 | return -2; |
| 9882 | } |
| 9883 | |
| 9884 | |
| 9885 | /* Test data type returned from previous error */ |
| 9886 | QCBORDecode_Init(&DCtx, |
| 9887 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9888 | QCBOR_DECODE_MODE_NORMAL); |
| 9889 | QCBORDecode_GetInt64(&DCtx, &integer); |
| 9890 | uError = QCBORDecode_GetError(&DCtx); |
| 9891 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9892 | return -3; |
| 9893 | } |
| 9894 | |
| 9895 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9896 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 9897 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 9898 | return -2; |
| 9899 | } |
| 9900 | uError = QCBORDecode_GetError(&DCtx); |
| 9901 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9902 | return -3; |
| 9903 | } |
| 9904 | |
| 9905 | |
| 9906 | /* Test error classification functions */ |
| 9907 | |
| 9908 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 9909 | return -10; |
| 9910 | } |
| 9911 | if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) { |
| 9912 | return -11; |
| 9913 | } |
| 9914 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 9915 | return -12; |
| 9916 | } |
| 9917 | if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) { |
| 9918 | return -13; |
| 9919 | } |
| 9920 | |
| 9921 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) { |
| 9922 | return -20; |
| 9923 | } |
| 9924 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) { |
| 9925 | return -21; |
| 9926 | } |
| 9927 | if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) { |
| 9928 | return -22; |
| 9929 | } |
| 9930 | if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) { |
| 9931 | return -23; |
| 9932 | } |
| 9933 | |
Laurence Lundblade | cd34e58 | 2024-06-06 10:55:14 -0700 | [diff] [blame] | 9934 | /* Test error strings */ |
| 9935 | const char *szErrString; |
| 9936 | |
| 9937 | szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG); |
| 9938 | if(szErrString == NULL) { |
| 9939 | return -100; |
| 9940 | } |
| 9941 | if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) { |
| 9942 | return -101; |
| 9943 | } |
| 9944 | |
| 9945 | szErrString = qcbor_err_to_str(QCBOR_SUCCESS); |
| 9946 | if(szErrString == NULL) { |
| 9947 | return -102; |
| 9948 | } |
| 9949 | if(strcmp(szErrString, "QCBOR_SUCCESS")) { |
| 9950 | return -103; |
| 9951 | } |
| 9952 | |
| 9953 | szErrString = qcbor_err_to_str(100); |
| 9954 | if(szErrString == NULL) { |
| 9955 | return -104; |
| 9956 | } |
| 9957 | if(strcmp(szErrString, "Unidentified QCBOR error")) { |
| 9958 | return -105; |
| 9959 | } |
| 9960 | |
| 9961 | szErrString = qcbor_err_to_str(200); |
| 9962 | if(szErrString == NULL) { |
| 9963 | return -106; |
| 9964 | } |
| 9965 | if(strcmp(szErrString, "USER_DEFINED_200")) { |
| 9966 | return -107; |
| 9967 | } |
| 9968 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9969 | return 0; |
| 9970 | } |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 9971 | |
| 9972 | |
| 9973 | int32_t TellTests(void) |
| 9974 | { |
| 9975 | QCBORDecodeContext DCtx; |
| 9976 | QCBORItem Item; |
| 9977 | uint32_t uPosition; |
| 9978 | int nIndex; |
| 9979 | int64_t nDecodedInt; |
| 9980 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 9981 | // Improvement: rewrite so this can run with only integer labels |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 9982 | static const uint32_t aPos[] = |
| 9983 | {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, UINT32_MAX}; |
| 9984 | QCBORDecode_Init(&DCtx, |
| 9985 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9986 | 0); |
| 9987 | for(nIndex = 0; ; nIndex++) { |
| 9988 | uPosition = QCBORDecode_Tell(&DCtx); |
| 9989 | if(uPosition != aPos[nIndex]) { |
| 9990 | return nIndex; |
| 9991 | } |
| 9992 | |
| 9993 | if(uPosition == UINT32_MAX) { |
| 9994 | break; |
| 9995 | } |
| 9996 | |
| 9997 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9998 | } |
| 9999 | |
| 10000 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10001 | static const uint32_t aPosIndef[] = |
| 10002 | {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, UINT32_MAX}; |
| 10003 | QCBORDecode_Init(&DCtx, |
| 10004 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10005 | 0); |
| 10006 | |
| 10007 | for(nIndex = 0; ; nIndex++) { |
| 10008 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10009 | if(uPosition != aPosIndef[nIndex]) { |
| 10010 | return nIndex + 100; |
| 10011 | } |
| 10012 | |
| 10013 | if(uPosition == UINT32_MAX) { |
| 10014 | break; |
| 10015 | } |
| 10016 | |
| 10017 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10018 | } |
| 10019 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10020 | |
| 10021 | /* Next, some tests with entered maps and arrays */ |
| 10022 | QCBORDecode_Init(&DCtx, |
| 10023 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10024 | 0); |
| 10025 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10026 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10027 | return 1001; |
| 10028 | } |
| 10029 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10030 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10031 | return 1002; |
| 10032 | } |
| 10033 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10034 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10035 | return 1003; |
| 10036 | } |
| 10037 | |
| 10038 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10039 | if(nDecodedInt != 98) { |
| 10040 | return 1004; |
| 10041 | } |
| 10042 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10043 | if(QCBORDecode_Tell(&DCtx) != 72) { |
| 10044 | return 1005; |
| 10045 | } |
| 10046 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10047 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10048 | return 1006; |
| 10049 | } |
| 10050 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10051 | if(nDecodedInt != 98) { |
| 10052 | return 1007; |
| 10053 | } |
| 10054 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10055 | if(QCBORDecode_Tell(&DCtx) != 85) { |
| 10056 | return 1008; |
| 10057 | } |
| 10058 | |
| 10059 | QCBORDecode_ExitMap(&DCtx); |
| 10060 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10061 | return 1009; |
| 10062 | } |
| 10063 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10064 | return 1010; |
| 10065 | } |
| 10066 | |
| 10067 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10068 | /* Next, some tests with entered maps and arrays */ |
| 10069 | QCBORDecode_Init(&DCtx, |
| 10070 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded), |
| 10071 | 0); |
| 10072 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10073 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10074 | return 2000; |
| 10075 | } |
| 10076 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt); |
| 10077 | if(QCBORDecode_Tell(&DCtx) != 1) { |
| 10078 | return 2001; |
| 10079 | } |
| 10080 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 10081 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10082 | return 2002; |
| 10083 | } |
| 10084 | |
| 10085 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10086 | if(nDecodedInt != 98) { |
| 10087 | return 2003; |
| 10088 | } |
| 10089 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10090 | if(QCBORDecode_Tell(&DCtx) != 73) { |
| 10091 | return 2004; |
| 10092 | } |
| 10093 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10094 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10095 | return 2005; |
| 10096 | } |
| 10097 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10098 | if(nDecodedInt != 98) { |
| 10099 | return 2006; |
| 10100 | } |
| 10101 | /* Getting non-aggregate types doesn't affect cursor position. */ |
| 10102 | if(QCBORDecode_Tell(&DCtx) != 86) { |
| 10103 | return 2007; |
| 10104 | } |
| 10105 | |
| 10106 | QCBORDecode_ExitMap(&DCtx); |
| 10107 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10108 | return 2008; |
| 10109 | } |
| 10110 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10111 | return 2010; |
| 10112 | } |
| 10113 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10114 | |
| 10115 | |
| 10116 | |
| 10117 | /* Error state test */ |
| 10118 | QCBORDecode_Init(&DCtx, |
| 10119 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10120 | 0); |
| 10121 | /* Cause an error */ |
| 10122 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt); |
| 10123 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10124 | return 3000; |
| 10125 | } |
| 10126 | |
| 10127 | /* Empties tests */ |
| 10128 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 10129 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 10130 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10131 | return 4000; |
| 10132 | } |
| 10133 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10134 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10135 | return 4001; |
| 10136 | } |
| 10137 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10138 | return 4002; |
| 10139 | } |
| 10140 | QCBORDecode_ExitMap(&DCtx); |
| 10141 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10142 | return 4001; |
| 10143 | } |
| 10144 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10145 | return 4002; |
| 10146 | } |
| 10147 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10148 | return 4010; |
| 10149 | } |
| 10150 | |
| 10151 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10152 | const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map |
| 10153 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0); |
| 10154 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10155 | return 4100; |
| 10156 | } |
| 10157 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10158 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10159 | return 4101; |
| 10160 | } |
| 10161 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10162 | return 4102; |
| 10163 | } |
| 10164 | QCBORDecode_ExitMap(&DCtx); |
| 10165 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10166 | return 4101; |
| 10167 | } |
| 10168 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10169 | return 4102; |
| 10170 | } |
| 10171 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10172 | return 4110; |
| 10173 | } |
| 10174 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10175 | |
| 10176 | /* Test on a CBOR sequence */ |
| 10177 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0); |
| 10178 | if(QCBORDecode_Tell(&DCtx) != 0) { |
| 10179 | return 5000; |
| 10180 | } |
| 10181 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10182 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10183 | return 5001; |
| 10184 | } |
| 10185 | if(QCBORDecode_Tell(&DCtx) != 11) { |
| 10186 | return 5002; |
| 10187 | } |
| 10188 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10189 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10190 | return 5003; |
| 10191 | } |
| 10192 | if(QCBORDecode_Tell(&DCtx) != 12) { |
| 10193 | return 5004; |
| 10194 | } |
| 10195 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10196 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10197 | return 5005; |
| 10198 | } |
| 10199 | if(QCBORDecode_Tell(&DCtx) != 17) { |
| 10200 | return 5006; |
| 10201 | } |
| 10202 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10203 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 10204 | return 5007; |
| 10205 | } |
| 10206 | if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) { |
| 10207 | return 5008; |
| 10208 | } |
| 10209 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 10210 | return 5010; |
| 10211 | } |
| 10212 | |
| 10213 | |
| 10214 | QCBORDecode_Init(&DCtx, |
| 10215 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 10216 | 0); |
| 10217 | QCBORDecode_EnterMap(&DCtx, &Item); |
| 10218 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 10219 | if(QCBORDecode_Tell(&DCtx) != 42) { |
| 10220 | return 6001; |
| 10221 | } |
| 10222 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10223 | if(QCBORDecode_Tell(&DCtx) != 50) { |
| 10224 | return 6002; |
| 10225 | } |
| 10226 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10227 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10228 | return 6008; |
| 10229 | } |
| 10230 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10231 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 10232 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10233 | return 6003; |
| 10234 | } |
| 10235 | QCBORDecode_ExitArray(&DCtx); |
| 10236 | if(QCBORDecode_Tell(&DCtx) != 58) { |
| 10237 | return 6004; |
| 10238 | } |
| 10239 | |
| 10240 | static const uint32_t aEmptiesPos[] = |
| 10241 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, UINT32_MAX}; |
| 10242 | QCBORDecode_Init(&DCtx, |
| 10243 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 10244 | 0); |
| 10245 | for(nIndex = 0; ; nIndex++) { |
| 10246 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10247 | if(uPosition != aEmptiesPos[nIndex]) { |
| 10248 | return nIndex + 200; |
| 10249 | } |
| 10250 | |
| 10251 | if(uPosition == UINT32_MAX) { |
| 10252 | break; |
| 10253 | } |
| 10254 | |
| 10255 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10256 | } |
| 10257 | |
| 10258 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 10259 | static const uint32_t aIndefEmptiesPos[] = |
| 10260 | {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, UINT32_MAX}; |
| 10261 | QCBORDecode_Init(&DCtx, |
| 10262 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 10263 | 0); |
| 10264 | for(nIndex = 0; ; nIndex++) { |
| 10265 | uPosition = QCBORDecode_Tell(&DCtx); |
| 10266 | if(uPosition != aIndefEmptiesPos[nIndex]) { |
| 10267 | return nIndex + 300; |
| 10268 | } |
| 10269 | |
| 10270 | if(uPosition == UINT32_MAX) { |
| 10271 | break; |
| 10272 | } |
| 10273 | |
| 10274 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 10275 | } |
| 10276 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 10277 | |
| 10278 | |
| 10279 | return 0; |
| 10280 | } |