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 | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 66 | /* |
| 67 | [ |
| 68 | -9223372036854775808, |
| 69 | -4294967297, |
| 70 | -4294967296, |
| 71 | -4294967295, |
| 72 | -4294967294, |
| 73 | -2147483648, |
| 74 | -2147483647, |
| 75 | -65538, |
| 76 | -65537, |
| 77 | -65536, |
| 78 | -65535, |
| 79 | -65534, |
| 80 | -257, |
| 81 | -256, |
| 82 | -255, |
| 83 | -254, |
| 84 | -25, |
| 85 | -24, |
| 86 | -23, |
| 87 | -1, |
| 88 | 0, |
| 89 | 0, |
| 90 | 1, |
| 91 | 22, |
| 92 | 23, |
| 93 | 24, |
| 94 | 25, |
| 95 | 26, |
| 96 | 254, |
| 97 | 255, |
| 98 | 256, |
| 99 | 257, |
| 100 | 65534, |
| 101 | 65535, |
| 102 | 65536, |
| 103 | 65537, |
| 104 | 65538, |
| 105 | 2147483647, |
| 106 | 2147483647, |
| 107 | 2147483648, |
| 108 | 2147483649, |
| 109 | 4294967294, |
| 110 | 4294967295, |
| 111 | 4294967296, |
| 112 | 4294967297, |
| 113 | 9223372036854775807, |
| 114 | 18446744073709551615 |
| 115 | ] |
| 116 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 117 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 118 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 119 | 0x98, 0x31, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 120 | 0xff, 0xff, 0xff, 0x3b, 0xFf, 0xff, 0xff, 0xff, 0xff, |
| 121 | 0xff, 0xff, 0xfe, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 122 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 123 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 124 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 125 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 126 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 127 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 128 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 129 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 130 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 131 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 132 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 133 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 134 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 135 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 136 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 137 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 138 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 139 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 140 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 141 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 142 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 143 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 144 | 0xff, 0xff}; |
| 145 | |
| 146 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 147 | // return CBOR error or -1 if type of value doesn't match |
| 148 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 149 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 150 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 151 | QCBORItem Item; |
| 152 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 153 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 154 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 155 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 156 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 157 | return -1; |
| 158 | |
| 159 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 160 | return (int32_t)nCBORError; |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 161 | if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT || |
| 162 | Item.val.uint64 != 18446744073709551615ULL) |
| 163 | return -1; |
| 164 | |
| 165 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 166 | return (int32_t)nCBORError; |
| 167 | if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT || |
| 168 | Item.val.uint64 != 18446744073709551614ULL) |
| 169 | return -1; |
| 170 | |
| 171 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 172 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 173 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 174 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 175 | return -1; |
| 176 | |
| 177 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 178 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 179 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 180 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 181 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 182 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 183 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 184 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 185 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 186 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 187 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 188 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 189 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 190 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 191 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 192 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 193 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 194 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 195 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 196 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 197 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 198 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 199 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 200 | |
| 201 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 202 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 203 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 204 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 205 | Item.val.int64 != -2147483648) |
| 206 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 207 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 208 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 209 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 210 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 211 | Item.val.int64 != -2147483647) |
| 212 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 213 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 214 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 215 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 216 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 217 | Item.val.int64 != -65538) |
| 218 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 219 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 220 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 221 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 222 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 223 | Item.val.int64 != -65537) |
| 224 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 225 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 226 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 227 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 228 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 229 | Item.val.int64 != -65536) |
| 230 | return -1; |
| 231 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 232 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 233 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 234 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 235 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 236 | Item.val.int64 != -65535) |
| 237 | return -1; |
| 238 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 239 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 240 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 241 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 242 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 243 | Item.val.int64 != -65534) |
| 244 | return -1; |
| 245 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 246 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 247 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 248 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 249 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 250 | Item.val.int64 != -257) |
| 251 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 252 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 253 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 254 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 255 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 256 | Item.val.int64 != -256) |
| 257 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 258 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 259 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 260 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 261 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 262 | Item.val.int64 != -255) |
| 263 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 264 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 265 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 266 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 267 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 268 | Item.val.int64 != -254) |
| 269 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 270 | |
| 271 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 272 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 273 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 274 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 275 | Item.val.int64 != -25) |
| 276 | return -1; |
| 277 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 278 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 279 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 280 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 281 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 282 | Item.val.int64 != -24) |
| 283 | return -1; |
| 284 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 285 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 286 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 287 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 288 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 289 | Item.val.int64 != -23) |
| 290 | return -1; |
| 291 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 292 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 293 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 294 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 295 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 296 | Item.val.int64 != -1) |
| 297 | return -1; |
| 298 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 299 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 300 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 301 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 302 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 303 | Item.val.int64 != 0) |
| 304 | return -1; |
| 305 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 306 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 307 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 308 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 309 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 310 | Item.val.int64 != 0) |
| 311 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 312 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 313 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 314 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 315 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 316 | Item.val.int64 != 1) |
| 317 | return -1; |
| 318 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 319 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 320 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 321 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 322 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 323 | Item.val.int64 != 22) |
| 324 | return -1; |
| 325 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 326 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 327 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 328 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 329 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 330 | Item.val.int64 != 23) |
| 331 | return -1; |
| 332 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 333 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 334 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 335 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 336 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 337 | Item.val.int64 != 24) |
| 338 | return -1; |
| 339 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 340 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 341 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 342 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 343 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 344 | Item.val.int64 != 25) |
| 345 | return -1; |
| 346 | |
| 347 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 348 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 349 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 350 | Item.val.int64 != 26) |
| 351 | return -1; |
| 352 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 353 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 354 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 355 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 356 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 357 | Item.val.int64 != 254) |
| 358 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 359 | |
| 360 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 361 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 362 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 363 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 364 | Item.val.int64 != 255) |
| 365 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 366 | |
| 367 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 368 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 369 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 370 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 371 | Item.val.int64 != 256) |
| 372 | return -1; |
| 373 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 374 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 375 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 376 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 377 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 378 | Item.val.int64 != 257) |
| 379 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 380 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 381 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 382 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 383 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 384 | Item.val.int64 != 65534) |
| 385 | return -1; |
| 386 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 387 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 388 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 389 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 390 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 391 | Item.val.int64 != 65535) |
| 392 | return -1; |
| 393 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 394 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 395 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 396 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 397 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 398 | Item.val.int64 != 65536) |
| 399 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 400 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 401 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 402 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 403 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 404 | Item.val.int64 != 65537) |
| 405 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 406 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 407 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 408 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 409 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 410 | Item.val.int64 != 65538) |
| 411 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 412 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 413 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 414 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 415 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 416 | Item.val.int64 != 2147483647) |
| 417 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 418 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 419 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 420 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 421 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 422 | Item.val.int64 != 2147483647) |
| 423 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 424 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 425 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 426 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 427 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 428 | Item.val.int64 != 2147483648) |
| 429 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 430 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 431 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 432 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 433 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 434 | Item.val.int64 != 2147483649) |
| 435 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 436 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 437 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 438 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 439 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 440 | Item.val.int64 != 4294967294) |
| 441 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 442 | |
| 443 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 444 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 445 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 446 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 447 | Item.val.int64 != 4294967295) |
| 448 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 449 | |
| 450 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 451 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 452 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 453 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 454 | Item.val.int64 != 4294967296) |
| 455 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 456 | |
| 457 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 458 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 459 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 460 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 461 | Item.val.int64 != 4294967297) |
| 462 | return -1; |
| 463 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 464 | |
| 465 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 466 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 467 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 468 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 469 | Item.val.int64 != 9223372036854775807LL) |
| 470 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 471 | |
| 472 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 473 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 474 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 475 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 476 | Item.val.uint64 != 18446744073709551615ULL) |
| 477 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 478 | |
| 479 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 480 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 481 | return -1; |
| 482 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 483 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 488 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 489 | /* |
| 490 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 491 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 492 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 493 | int32_t IntegerValuesParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 494 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 495 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 496 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 497 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 498 | QCBORDecode_Init(&DCtx, |
| 499 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 500 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 501 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 502 | // The really big test of all successes |
| 503 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 504 | if(nReturn) { |
| 505 | return nReturn; |
| 506 | } |
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 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
| 512 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 513 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 514 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 515 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 516 | 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] | 517 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 518 | */ |
| 519 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 520 | static uint8_t spSimpleArrayBuffer[50]; |
| 521 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 522 | 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] | 523 | { |
| 524 | QCBOREncodeContext ECtx; |
| 525 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 526 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 527 | *pEncoded = NULL; |
| 528 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 529 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 530 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 531 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 532 | // and last with the buffer to do the actual encoding |
| 533 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 534 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 535 | QCBOREncode_OpenArray(&ECtx); |
| 536 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 537 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 538 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 539 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 540 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 541 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 542 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 543 | goto Done; |
| 544 | |
| 545 | if(*pEncoded != NULL) { |
| 546 | nReturn = 0; |
| 547 | goto Done; |
| 548 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 549 | |
| 550 | // Use static buffer to avoid dependency on malloc() |
| 551 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 552 | goto Done; |
| 553 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 554 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 555 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 556 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 557 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 558 | Done: |
| 559 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 563 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 564 | Some basic CBOR with map and array used in a lot of tests. |
| 565 | The map labels are all strings |
| 566 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 567 | { |
| 568 | "first integer": 42, |
| 569 | "an array of two strings": [ |
| 570 | "string1", "string2" |
| 571 | ], |
| 572 | "map in a map": { |
| 573 | "bytes 1": h'78787878', |
| 574 | "bytes 2": h'79797979', |
| 575 | "another int": 98, |
| 576 | "text 2": "lies, damn lies and statistics" |
| 577 | } |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 578 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 579 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 580 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 581 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 582 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 583 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 584 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 585 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 586 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 587 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 588 | 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, |
| 589 | 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4, |
| 590 | 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 591 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, |
| 592 | 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, |
| 593 | 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68, |
| 594 | 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, |
| 595 | 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 596 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, |
| 597 | 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, |
| 598 | 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, |
| 599 | 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 600 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 601 | |
| 602 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 603 | // Same as above, but with indefinite lengths. |
| 604 | static const uint8_t pValidMapIndefEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 605 | 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 606 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 607 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 608 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 609 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 610 | 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 611 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 612 | 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20, |
| 613 | 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, |
| 614 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 615 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 616 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 617 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 618 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 619 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 620 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 621 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 622 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 623 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 624 | 0xff, 0xff}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 625 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 626 | |
| 627 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 628 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 629 | size_t nLen, |
| 630 | int64_t *pInt1, |
| 631 | int64_t *pInt2, |
| 632 | const uint8_t **pBuf3, |
| 633 | size_t *pBuf3Len, |
| 634 | const uint8_t **pBuf4, |
| 635 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 636 | { |
| 637 | QCBORDecodeContext DCtx; |
| 638 | QCBORItem Item; |
| 639 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 640 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 641 | QCBORDecode_Init(&DCtx, |
| 642 | (UsefulBufC){pEncoded, nLen}, |
| 643 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 644 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 645 | // Make sure the first thing is a map |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 646 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 647 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 648 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 649 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 650 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 651 | // First integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 652 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 653 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 654 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 655 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 656 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 657 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 658 | // Second integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 659 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 660 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 661 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 662 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 663 | *pInt2 = Item.val.int64; |
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 | // First string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 666 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 667 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
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 | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 670 | *pBuf3 = Item.val.string.ptr; |
| 671 | *pBuf3Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 672 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 673 | // Second string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 674 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 675 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 676 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 677 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 678 | *pBuf4 = Item.val.string.ptr; |
| 679 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 680 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 681 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 682 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 683 | Done: |
| 684 | return(nReturn); |
| 685 | } |
| 686 | |
| 687 | |
| 688 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 689 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 690 | int32_t SimpleArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 691 | { |
| 692 | uint8_t *pEncoded; |
| 693 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 694 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 695 | int64_t i1=0, i2=0; |
| 696 | size_t i3=0, i4=0; |
| 697 | const uint8_t *s3= (uint8_t *)""; |
| 698 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 699 | |
| 700 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 701 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 702 | return(-1); |
| 703 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 704 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 705 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 706 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 707 | if(i1 != 23 || |
| 708 | i2 != 6000 || |
| 709 | i3 != 8 || |
| 710 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 711 | memcmp("galactic", s3, 8) !=0 || |
| 712 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 713 | return(-1); |
| 714 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 715 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 716 | return(0); |
| 717 | } |
| 718 | |
| 719 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 720 | /* |
| 721 | [ |
| 722 | 0, |
| 723 | [], |
| 724 | [ |
| 725 | [], |
| 726 | [ |
| 727 | 0 |
| 728 | ], |
| 729 | {}, |
| 730 | { |
| 731 | 1: {}, |
| 732 | 2: {}, |
| 733 | 3: [] |
| 734 | } |
| 735 | ] |
| 736 | ] |
| 737 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 738 | static uint8_t sEmpties[] = { |
| 739 | 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 740 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 741 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 742 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 743 | /* Same as above, but with indefinte lengths */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 744 | static const uint8_t sEmptiesIndef[] = { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 745 | 0x9F, |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 746 | 0x00, |
| 747 | 0x9F, |
| 748 | 0xFF, |
| 749 | 0x9F, |
| 750 | 0x9F, |
| 751 | 0xFF, |
| 752 | 0x9F, |
| 753 | 0x00, |
| 754 | 0xFF, |
| 755 | 0xBF, |
| 756 | 0xFF, |
| 757 | 0xBF, |
| 758 | 0x01, |
| 759 | 0xBF, |
| 760 | 0xFF, |
| 761 | 0x02, |
| 762 | 0xBF, |
| 763 | 0xFF, |
| 764 | 0x03, |
| 765 | 0x9F, |
| 766 | 0xFF, |
| 767 | 0xFF, |
| 768 | 0xFF, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 769 | 0xFF}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 770 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 771 | |
| 772 | |
| 773 | static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts) |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 774 | { |
| 775 | QCBORDecodeContext DCtx; |
| 776 | QCBORItem Item; |
| 777 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 778 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 779 | input, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 780 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 781 | |
| 782 | // Array with 3 items |
| 783 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 784 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 785 | Item.uNestingLevel != 0 || |
| 786 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 787 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 788 | return -1; |
| 789 | } |
| 790 | |
| 791 | // An integer 0 |
| 792 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 793 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 794 | Item.uNestingLevel != 1 || |
| 795 | Item.uNextNestLevel != 1 || |
| 796 | Item.val.uint64 != 0) { |
| 797 | return -2; |
| 798 | } |
| 799 | |
| 800 | // An empty array |
| 801 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 802 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 803 | Item.uNestingLevel != 1 || |
| 804 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 805 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 806 | return -3; |
| 807 | } |
| 808 | |
| 809 | // An array with 4 items |
| 810 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 811 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 812 | Item.uNestingLevel != 1 || |
| 813 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 814 | (bCheckCounts && Item.val.uCount != 4)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 815 | return -4; |
| 816 | } |
| 817 | |
| 818 | // An empty array |
| 819 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 820 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 821 | Item.uNestingLevel != 2 || |
| 822 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 823 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 824 | return -5; |
| 825 | } |
| 826 | |
| 827 | // An array with 1 item |
| 828 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 829 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 830 | Item.uNestingLevel != 2 || |
| 831 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 832 | (bCheckCounts && Item.val.uCount != 1)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 833 | return -6; |
| 834 | } |
| 835 | |
| 836 | // An integer 0 |
| 837 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 838 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 839 | Item.uNestingLevel != 3 || |
| 840 | Item.uNextNestLevel != 2 || |
| 841 | Item.val.uint64 != 0) { |
| 842 | return -7; |
| 843 | } |
| 844 | |
| 845 | // An empty map |
| 846 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 847 | Item.uDataType != QCBOR_TYPE_MAP || |
| 848 | Item.uNestingLevel != 2 || |
| 849 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 850 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 851 | return -8; |
| 852 | } |
| 853 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 854 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 855 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 856 | Item.uDataType != QCBOR_TYPE_MAP || |
| 857 | Item.uNestingLevel != 2 || |
| 858 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 859 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 860 | return -9; |
| 861 | } |
| 862 | |
| 863 | // An empty map |
| 864 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 865 | Item.uDataType != QCBOR_TYPE_MAP || |
| 866 | Item.uNestingLevel != 3 || |
| 867 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 868 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 869 | return -10; |
| 870 | } |
| 871 | |
| 872 | // An empty map |
| 873 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 874 | Item.uDataType != QCBOR_TYPE_MAP || |
| 875 | Item.uNestingLevel != 3 || |
| 876 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 877 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 878 | return -11; |
| 879 | } |
| 880 | |
| 881 | // An empty array |
| 882 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 883 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 884 | Item.uNestingLevel != 3 || |
| 885 | Item.uNextNestLevel != 0 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 886 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 887 | return -12; |
| 888 | } |
| 889 | |
| 890 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 891 | return -13; |
| 892 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 897 | int32_t EmptyMapsAndArraysTest(void) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 898 | { |
| 899 | int nResult; |
| 900 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 901 | true); |
| 902 | if(nResult) { |
| 903 | return nResult; |
| 904 | } |
| 905 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 906 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 907 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 908 | false); |
| 909 | |
| 910 | if(nResult) { |
| 911 | return nResult -100; |
| 912 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 913 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 918 | |
| 919 | static const uint8_t sEmptyMap[] = { |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 920 | 0xA1, //# map(1) |
| 921 | 0x02, //# unsigned(2) |
| 922 | 0xA0, //# map(0) |
| 923 | }; |
| 924 | |
| 925 | int32_t ParseEmptyMapInMapTest(void) |
| 926 | { |
| 927 | QCBORDecodeContext DCtx; |
| 928 | QCBORItem Item; |
| 929 | int nReturn = 0; |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 930 | QCBORError uErr; |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 931 | |
| 932 | QCBORDecode_Init(&DCtx, |
| 933 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap), |
| 934 | QCBOR_DECODE_MODE_NORMAL); |
| 935 | |
| 936 | /* now open the first Map */ |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 937 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 938 | if(uErr != QCBOR_SUCCESS || |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 939 | Item.uDataType != QCBOR_TYPE_MAP) { |
| 940 | nReturn = -3; |
| 941 | goto done; |
| 942 | } |
| 943 | |
| 944 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0) { |
| 945 | nReturn = -1; |
| 946 | goto done; |
| 947 | } |
| 948 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 949 | Item.uNestingLevel != 1 || |
| 950 | Item.label.int64 != 2) { |
| 951 | nReturn = -2; |
| 952 | goto done; |
| 953 | } |
| 954 | |
| 955 | done: |
| 956 | return(nReturn); |
| 957 | } |
| 958 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 959 | |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 960 | /* [[[[[[[[[[]]]]]]]]]] */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 961 | static const uint8_t spDeepArrays[] = { |
| 962 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 963 | 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 964 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 965 | int32_t ParseDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 966 | { |
| 967 | QCBORDecodeContext DCtx; |
| 968 | int nReturn = 0; |
| 969 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 970 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 971 | QCBORDecode_Init(&DCtx, |
| 972 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 973 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 974 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 975 | for(i = 0; i < 10; i++) { |
| 976 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 977 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 978 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 979 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 980 | Item.uNestingLevel != i) { |
| 981 | nReturn = -1; |
| 982 | break; |
| 983 | } |
| 984 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 985 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 986 | return(nReturn); |
| 987 | } |
| 988 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 989 | /* Big enough to test nesting to the depth of 24 |
| 990 | [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]] |
| 991 | */ |
| 992 | static const uint8_t spTooDeepArrays[] = { |
| 993 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 994 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 995 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 996 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 997 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 998 | int32_t ParseTooDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 999 | { |
| 1000 | QCBORDecodeContext DCtx; |
| 1001 | int nReturn = 0; |
| 1002 | int i; |
| 1003 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1004 | |
| 1005 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1006 | QCBORDecode_Init(&DCtx, |
| 1007 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 1008 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1009 | |
Laurence Lundblade | 3e0b230 | 2023-12-04 14:02:38 -0700 | [diff] [blame] | 1010 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1011 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1012 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 1013 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1014 | Item.uNestingLevel != i) { |
| 1015 | nReturn = -1; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1019 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1020 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1021 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1022 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1023 | return(nReturn); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1029 | int32_t ShortBufferParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1030 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1031 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1032 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1033 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1034 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1035 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1036 | QCBORDecode_Init(&DCtx, |
| 1037 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 1038 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1039 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1040 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1041 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1042 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1043 | nResult = -1; |
| 1044 | goto Done; |
| 1045 | } |
| 1046 | } |
| 1047 | Done: |
| 1048 | return nResult; |
| 1049 | } |
| 1050 | |
| 1051 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1052 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1053 | int32_t ShortBufferParseTest2(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1054 | { |
| 1055 | uint8_t *pEncoded; |
| 1056 | int nReturn; |
| 1057 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1058 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1059 | int64_t i1, i2; |
| 1060 | size_t i3, i4; |
| 1061 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1062 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1063 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1064 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1065 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 1066 | return(-1); |
| 1067 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1068 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1069 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1070 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 1071 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1072 | if(nResult == 0) { |
| 1073 | nReturn = -1; |
| 1074 | } |
| 1075 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1076 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1077 | return(nReturn); |
| 1078 | } |
| 1079 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1080 | /* |
| 1081 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1082 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 1083 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1084 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1085 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1086 | { |
| 1087 | QCBORDecodeContext DCtx; |
| 1088 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1089 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1090 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1091 | QCBORDecode_Init(&DCtx, |
| 1092 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1093 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1094 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1095 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1096 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1097 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1098 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1099 | Item.val.uCount != 3) |
| 1100 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1101 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1102 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1103 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1104 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1105 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1106 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1107 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1108 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1109 | Item.uDataAlloc || |
| 1110 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1111 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1112 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1113 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1114 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1115 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1116 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1117 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1118 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1119 | Item.uDataAlloc || |
| 1120 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1121 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1122 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1123 | Item.val.uCount != 2) |
| 1124 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1125 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1126 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1127 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1128 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1129 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1130 | Item.uDataAlloc || |
| 1131 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1132 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1133 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1134 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1135 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1136 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1137 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1138 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1139 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1140 | Item.uDataAlloc || |
| 1141 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1142 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1143 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1144 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1145 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1146 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1147 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1148 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1149 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1150 | Item.uDataAlloc || |
| 1151 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1152 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1153 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1154 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1155 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1156 | } |
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.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1162 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1163 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1164 | Item.uDataAlloc || |
| 1165 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1166 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1167 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1168 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1169 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1170 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1171 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1172 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1173 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1174 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1175 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1176 | Item.uDataAlloc || |
| 1177 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1178 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1179 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1180 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1181 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1182 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1183 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1184 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1185 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1186 | Item.uDataAlloc || |
| 1187 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1188 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1189 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1190 | Item.val.int64 != 98) |
| 1191 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1192 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1193 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1194 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1195 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1196 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1197 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1198 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1199 | Item.uDataAlloc || |
| 1200 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1201 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1202 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1203 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1204 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1209 | /* |
| 1210 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1211 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1212 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1213 | int32_t ParseMapAsArrayTest(void) |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1214 | { |
| 1215 | QCBORDecodeContext DCtx; |
| 1216 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1217 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1218 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1219 | QCBORDecode_Init(&DCtx, |
| 1220 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1221 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1222 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1223 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1224 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1225 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1226 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1227 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1228 | return -1; |
| 1229 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1230 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1231 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1232 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1233 | } |
| 1234 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1235 | Item.uDataAlloc || |
| 1236 | Item.uLabelAlloc || |
| 1237 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1238 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1239 | return -2; |
| 1240 | } |
| 1241 | |
| 1242 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1243 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1244 | } |
| 1245 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1246 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1247 | Item.val.int64 != 42 || |
| 1248 | Item.uDataAlloc || |
| 1249 | Item.uLabelAlloc) { |
| 1250 | return -3; |
| 1251 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1252 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1253 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1254 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1255 | } |
| 1256 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1257 | Item.uDataAlloc || |
| 1258 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1259 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1260 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1261 | return -4; |
| 1262 | } |
| 1263 | |
| 1264 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1265 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1266 | } |
| 1267 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1268 | Item.uDataAlloc || |
| 1269 | Item.uLabelAlloc || |
| 1270 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1271 | Item.val.uCount != 2) { |
| 1272 | return -5; |
| 1273 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1274 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1275 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1276 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1277 | } |
| 1278 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1279 | Item.val.string.len != 7 || |
| 1280 | Item.uDataAlloc || |
| 1281 | Item.uLabelAlloc || |
| 1282 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1283 | return -6; |
| 1284 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1285 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1286 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1287 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1288 | } |
| 1289 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1290 | Item.uDataAlloc || |
| 1291 | Item.uLabelAlloc || |
| 1292 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1293 | return -7; |
| 1294 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1295 | |
| 1296 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1297 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1298 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1299 | } |
| 1300 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1301 | Item.uDataAlloc || |
| 1302 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1303 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1304 | return -8; |
| 1305 | } |
| 1306 | |
| 1307 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1308 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1309 | } |
| 1310 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1311 | Item.uDataAlloc || |
| 1312 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1313 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1314 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1315 | return -9; |
| 1316 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1317 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1318 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1319 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1320 | } |
| 1321 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1322 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1323 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1324 | Item.uDataAlloc || |
| 1325 | Item.uLabelAlloc) { |
| 1326 | return -10; |
| 1327 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1328 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1329 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1330 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1331 | } |
| 1332 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1333 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1334 | Item.uDataAlloc || |
| 1335 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1336 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1337 | return -11; |
| 1338 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1339 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1340 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1341 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1342 | } |
| 1343 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1344 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1345 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1346 | Item.uDataAlloc || |
| 1347 | Item.uLabelAlloc) { |
| 1348 | return -12; |
| 1349 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1350 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1351 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1352 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1353 | } |
| 1354 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1355 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1356 | Item.uDataAlloc || |
| 1357 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1358 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1359 | return -13; |
| 1360 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1361 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1362 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1363 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1364 | } |
| 1365 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1366 | Item.uDataAlloc || |
| 1367 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1368 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1369 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1370 | return -14; |
| 1371 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1372 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1373 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1374 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1375 | } |
| 1376 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1377 | Item.uDataAlloc || |
| 1378 | Item.uLabelAlloc || |
| 1379 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1380 | Item.val.int64 != 98) { |
| 1381 | return -15; |
| 1382 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1383 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1384 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1385 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1386 | } |
| 1387 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1388 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1389 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1390 | Item.uDataAlloc || |
| 1391 | Item.uLabelAlloc) { |
| 1392 | return -16; |
| 1393 | } |
| 1394 | |
| 1395 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1396 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1397 | } |
| 1398 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1399 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1400 | Item.uDataAlloc || |
| 1401 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1402 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1403 | return -17; |
| 1404 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1405 | |
| 1406 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1407 | /* |
| 1408 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1409 | map that when interpreted as an array will be too many. Test |
| 1410 | data just has the start of the map, not all the items in the map. |
| 1411 | */ |
| 1412 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1413 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1414 | QCBORDecode_Init(&DCtx, |
| 1415 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1416 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1417 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1418 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1419 | return -50; |
| 1420 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1421 | |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1422 | // TODO: test decoding of labels that are arrays or such |
| 1423 | // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY |
| 1424 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1425 | return 0; |
| 1426 | } |
| 1427 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1428 | |
| 1429 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1430 | Fully or partially decode pValidMapEncoded. When |
| 1431 | partially decoding check for the right error code. |
| 1432 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1433 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1434 | The partial decodes test error conditions of |
| 1435 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1436 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1437 | This could be combined with the above test |
| 1438 | and made prettier and maybe a little more |
| 1439 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1440 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1441 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1442 | { |
| 1443 | QCBORDecodeContext DCtx; |
| 1444 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1445 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1446 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1447 | QCBORDecode_Init(&DCtx, |
| 1448 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1449 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1450 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1451 | if(nLevel < 1) { |
| 1452 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1453 | return -1; |
| 1454 | } else { |
| 1455 | return 0; |
| 1456 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1457 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1458 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1459 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1460 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1461 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1462 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1463 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1464 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1465 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1466 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1467 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1468 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1469 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1470 | } else { |
| 1471 | return 0; |
| 1472 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1473 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1474 | |
| 1475 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1476 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1477 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1478 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1479 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1480 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1481 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1482 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1483 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1484 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1485 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1486 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1487 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1488 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1489 | } else { |
| 1490 | return 0; |
| 1491 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1492 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1493 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1494 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1495 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1496 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1497 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1498 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1499 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1500 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1501 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1502 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1503 | |
| 1504 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1505 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1506 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1507 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1508 | } else { |
| 1509 | return 0; |
| 1510 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1511 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1512 | |
| 1513 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1514 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1515 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1516 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1517 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1518 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1519 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1520 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1521 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1522 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1523 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1524 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1525 | } else { |
| 1526 | return 0; |
| 1527 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1528 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1529 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1530 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1531 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1532 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1533 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1534 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1535 | return -10; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1536 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1537 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1538 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1539 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1540 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1541 | } else { |
| 1542 | return 0; |
| 1543 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1544 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1545 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1546 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1547 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1548 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1549 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1550 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1551 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1552 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1553 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1554 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1555 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1556 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1557 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1558 | } else { |
| 1559 | return 0; |
| 1560 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1561 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1562 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1563 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1564 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1565 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1566 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1567 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1568 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1569 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1570 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1571 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1572 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1573 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1574 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1575 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1576 | } else { |
| 1577 | return 0; |
| 1578 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1579 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1580 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1581 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1582 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1583 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1584 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1585 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1586 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1587 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1588 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1589 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1590 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1591 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1592 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1593 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1594 | } else { |
| 1595 | return 0; |
| 1596 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1597 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1598 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1599 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1600 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1601 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1602 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1603 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1604 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1605 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1606 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1607 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1608 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1609 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1610 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1611 | } else { |
| 1612 | return 0; |
| 1613 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1614 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1615 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1616 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1617 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1618 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1619 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1620 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1621 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1622 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1623 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1624 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1625 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1626 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1627 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1628 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1629 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1630 | return 0; |
| 1631 | } |
| 1632 | |
| 1633 | |
| 1634 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1635 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1636 | int32_t ParseMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1637 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1638 | // Parse a moderatly complex map structure very thoroughly |
| 1639 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 1640 | if(nResult) { |
| 1641 | return nResult; |
| 1642 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1643 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1644 | // Again, but in strings-only mode. It should succeed since the input |
| 1645 | // map has only string labels. |
| 1646 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 1647 | if(nResult) { |
| 1648 | return nResult; |
| 1649 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1650 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1651 | // Again, but try to finish the decoding before the end of the |
| 1652 | // input at 10 different place and see that the right error code |
| 1653 | // is returned. |
| 1654 | for(int i = 0; i < 10; i++) { |
| 1655 | nResult = ExtraBytesTest(i); |
| 1656 | if(nResult) { |
| 1657 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1658 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1659 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1660 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1661 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1665 | /* The simple-values including some not well formed */ |
| 1666 | static const uint8_t spSimpleValues[] = { |
| 1667 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 1668 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 1669 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1670 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1671 | int32_t ParseSimpleTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1672 | { |
| 1673 | QCBORDecodeContext DCtx; |
| 1674 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1675 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1676 | |
| 1677 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1678 | QCBORDecode_Init(&DCtx, |
| 1679 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1680 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1681 | |
| 1682 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1683 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1684 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1685 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1686 | Item.val.uCount != 10) |
| 1687 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1688 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1689 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1690 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1691 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1692 | return -1; |
| 1693 | |
| 1694 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1695 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1696 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1697 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1698 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1699 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1700 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1701 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1702 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1703 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1704 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1705 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1706 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1707 | return -1; |
| 1708 | |
| 1709 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1710 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1711 | return -1; |
| 1712 | |
| 1713 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1714 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1715 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1716 | return -1; |
| 1717 | |
| 1718 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1719 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1720 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1721 | return -1; |
| 1722 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1723 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1724 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1725 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1726 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1727 | return -1; |
| 1728 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1729 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1730 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1731 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1732 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1733 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1734 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1735 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1736 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1737 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1738 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1739 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1740 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1741 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1742 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1743 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1747 | int32_t NotWellFormedTests(void) |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1748 | { |
| 1749 | // Loop over all the not-well-formed instance of CBOR |
| 1750 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1751 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 1752 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1753 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 1754 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 1755 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 1756 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1757 | if(nIterate == 86) { |
| 1758 | nIterate = 86; |
| 1759 | } |
| 1760 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1761 | // Set up decoder context. String allocator needed for indefinite |
| 1762 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1763 | QCBORDecodeContext DCtx; |
| 1764 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1765 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1766 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1767 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1768 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1769 | |
| 1770 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1771 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1772 | do { |
| 1773 | QCBORItem Item; |
| 1774 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1775 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1776 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1777 | |
| 1778 | // Every test vector must fail with |
| 1779 | // a not-well-formed error. If not |
| 1780 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1781 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1782 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1783 | /* Return index of failure and QCBOR error in the result */ |
| 1784 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1791 | struct DecodeFailTestInput { |
| 1792 | const char *szDescription; /* Description of the test */ |
| 1793 | QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */ |
| 1794 | UsefulBufC Input; /* Chunk of CBOR that cases error */ |
| 1795 | QCBORError nError; /* The expected error */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1796 | }; |
| 1797 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1798 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1799 | static int32_t |
| 1800 | ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1801 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1802 | int nIndex; |
| 1803 | QCBORDecodeContext DCtx; |
| 1804 | QCBORError uCBORError; |
| 1805 | QCBORItem Item; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1806 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1807 | for(nIndex = 0; nIndex < nNumFails; nIndex++) { |
| 1808 | const struct DecodeFailTestInput *pF = &pFailInputs[nIndex]; |
| 1809 | |
| 1810 | QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1811 | |
| 1812 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1813 | /* Set up the decoding context including a memory pool so that |
| 1814 | * indefinite length items can be checked. |
| 1815 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1816 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1817 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1818 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1819 | if(uCBORError != QCBOR_SUCCESS) { |
| 1820 | return -1; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1821 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1822 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1823 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1824 | if(nIndex == 8) { |
| 1825 | uCBORError = 9; /* For setting break points */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1828 | /* Iterate until there is an error of some sort of error */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1829 | do { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1830 | /* Set to something none-zero, something other than QCBOR_TYPE_NONE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1831 | memset(&Item, 0x33, sizeof(Item)); |
| 1832 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1833 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1834 | } while(uCBORError == QCBOR_SUCCESS); |
| 1835 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1836 | /* Must get the expected error or the this test fails |
| 1837 | * The data and label type must also be QCBOR_TYPE_NONE. |
| 1838 | */ |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1839 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1840 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1841 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1842 | return (int32_t)(nIndex * 1000 + (int)uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
| 1849 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1850 | static const struct DecodeFailTestInput Failures[] = { |
| 1851 | /* Most of this is copied from not_well_formed.h. Here the error |
| 1852 | * code returned is also checked. |
| 1853 | */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1854 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1855 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1856 | /* Indefinite length strings must be closed off */ |
| 1857 | { "An indefinite length byte string not closed off", |
| 1858 | QCBOR_DECODE_MODE_NORMAL, |
| 1859 | {"0x5f\x41\x00", 3}, |
| 1860 | QCBOR_ERR_HIT_END |
| 1861 | }, |
| 1862 | { "An indefinite length text string not closed off", |
| 1863 | QCBOR_DECODE_MODE_NORMAL, |
| 1864 | {"\x7f\x61\x00", 3}, |
| 1865 | QCBOR_ERR_HIT_END |
| 1866 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1867 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1868 | /* All the chunks in an indefinite length string must be of the |
| 1869 | * type of indefinite length string |
| 1870 | */ |
| 1871 | { "Indefinite length byte string with text string chunk", |
| 1872 | QCBOR_DECODE_MODE_NORMAL, |
| 1873 | {"\x5f\x61\x00\xff", 4}, |
| 1874 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1875 | }, |
| 1876 | { "Indefinite length text string with a byte string chunk", |
| 1877 | QCBOR_DECODE_MODE_NORMAL, |
| 1878 | {"\x7f\x41\x00\xff", 4}, |
| 1879 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1880 | }, |
| 1881 | { "Indefinite length byte string with a positive integer chunk", |
| 1882 | QCBOR_DECODE_MODE_NORMAL, |
| 1883 | {"\x5f\x00\xff", 3}, |
| 1884 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1885 | }, |
| 1886 | { "Indefinite length byte string with an negative integer chunk", |
| 1887 | QCBOR_DECODE_MODE_NORMAL, |
| 1888 | {"\x5f\x21\xff", 3}, |
| 1889 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1890 | }, |
| 1891 | { "Indefinite length byte string with an array chunk", |
| 1892 | QCBOR_DECODE_MODE_NORMAL, |
| 1893 | {"\x5f\x80\xff", 3}, |
| 1894 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1895 | }, |
| 1896 | { "Indefinite length byte string with an map chunk", |
| 1897 | QCBOR_DECODE_MODE_NORMAL, |
| 1898 | {"\x5f\xa0\xff", 3}, |
| 1899 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1900 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1901 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1902 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1903 | { "Indefinite length byte string with tagged integer chunk", |
| 1904 | QCBOR_DECODE_MODE_NORMAL, |
| 1905 | {"\x5f\xc0\x00\xff", 4}, |
| 1906 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1907 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1908 | #else |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1909 | { "Indefinite length byte string with tagged integer chunk", |
| 1910 | QCBOR_DECODE_MODE_NORMAL, |
| 1911 | {"\x5f\xc0\x00\xff", 4}, |
| 1912 | QCBOR_ERR_TAGS_DISABLED |
| 1913 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1914 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1915 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1916 | { "Indefinite length byte string with an simple type chunk", |
| 1917 | QCBOR_DECODE_MODE_NORMAL, |
| 1918 | {"\x5f\xe0\xff", 3}, |
| 1919 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1920 | }, |
| 1921 | { "???", |
| 1922 | QCBOR_DECODE_MODE_NORMAL, |
| 1923 | {"\x5f\x5f\x41\x00\xff\xff", 6}, |
| 1924 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1925 | }, |
| 1926 | { "indefinite length text string with indefinite string inside", |
| 1927 | QCBOR_DECODE_MODE_NORMAL, |
| 1928 | {"\x7f\x7f\x61\x00\xff\xff", 6}, |
| 1929 | QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1930 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1931 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1932 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1933 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1934 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1935 | /* Definte length maps and arrays must be closed by having the right number of items */ |
| 1936 | { "A definte length array that is supposed to have 1 item, but has none", |
| 1937 | QCBOR_DECODE_MODE_NORMAL, |
| 1938 | {"\x81", 1}, |
| 1939 | QCBOR_ERR_NO_MORE_ITEMS |
| 1940 | }, |
| 1941 | { "A definte length array that is supposed to have 2 items, but has only 1", |
| 1942 | QCBOR_DECODE_MODE_NORMAL, |
| 1943 | {"\x82\x00", 2}, |
| 1944 | QCBOR_ERR_NO_MORE_ITEMS |
| 1945 | }, |
| 1946 | { "A definte length array that is supposed to have 511 items, but has only 1", |
| 1947 | QCBOR_DECODE_MODE_NORMAL, |
| 1948 | {"\x9a\x01\xff\x00", 4}, |
| 1949 | QCBOR_ERR_HIT_END |
| 1950 | }, |
| 1951 | { "A definte length map that is supposed to have 1 item, but has none", |
| 1952 | QCBOR_DECODE_MODE_NORMAL, |
| 1953 | {"\xa1", 1}, |
| 1954 | QCBOR_ERR_NO_MORE_ITEMS |
| 1955 | }, |
| 1956 | { "A definte length map that is supposed to have s item, but has only 1", |
| 1957 | QCBOR_DECODE_MODE_NORMAL, |
| 1958 | {"\xa2\x01\x02", 3}, |
| 1959 | QCBOR_ERR_NO_MORE_ITEMS |
| 1960 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1961 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1962 | /* Indefinte length maps and arrays must be ended by a break */ |
| 1963 | { "Indefinite length array with zero items and no break", |
| 1964 | QCBOR_DECODE_MODE_NORMAL, |
| 1965 | {"\x9f", 1}, |
| 1966 | QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1967 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1968 | { "Indefinite length array with two items and no break", |
| 1969 | QCBOR_DECODE_MODE_NORMAL, |
| 1970 | {"\x9\x01\x02", 3}, |
| 1971 | QCBOR_ERR_NO_MORE_ITEMS |
| 1972 | }, |
| 1973 | { "Indefinite length map with zero items and no break", |
| 1974 | QCBOR_DECODE_MODE_NORMAL, |
| 1975 | {"\xbf", 1}, |
| 1976 | QCBOR_ERR_NO_MORE_ITEMS |
| 1977 | }, |
| 1978 | { "Indefinite length map with two items and no break", |
| 1979 | QCBOR_DECODE_MODE_NORMAL, |
| 1980 | {"\xbf\x01\x02\x01\x02", 5}, |
| 1981 | QCBOR_ERR_NO_MORE_ITEMS |
| 1982 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1983 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 1984 | /* Nested maps and arrays must be closed off (some extra nested test vectors) */ |
| 1985 | { "Unclosed indefinite array containing a closed definite length array", |
| 1986 | QCBOR_DECODE_MODE_NORMAL, |
| 1987 | {"\x9f\x80\x00", 3}, |
| 1988 | QCBOR_ERR_NO_MORE_ITEMS |
| 1989 | }, |
| 1990 | |
| 1991 | { "Definite length array containing an unclosed indefinite length array", |
| 1992 | QCBOR_DECODE_MODE_NORMAL, |
| 1993 | {"\x81\x9f", 2}, |
| 1994 | QCBOR_ERR_NO_MORE_ITEMS |
| 1995 | }, |
| 1996 | { "Unclosed indefinite map containing a closed definite length array", |
| 1997 | QCBOR_DECODE_MODE_NORMAL, |
| 1998 | {"\xbf\x01\x80\x00\xa0", 5}, |
| 1999 | QCBOR_ERR_NO_MORE_ITEMS |
| 2000 | }, |
| 2001 | { "Definite length map containing an unclosed indefinite length array", |
| 2002 | QCBOR_DECODE_MODE_NORMAL, |
| 2003 | {"\xa1\x02\x9f", 3}, |
| 2004 | QCBOR_ERR_NO_MORE_ITEMS |
| 2005 | }, |
| 2006 | { "Deeply nested definite length arrays with deepest one unclosed", |
| 2007 | QCBOR_DECODE_MODE_NORMAL, |
| 2008 | {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9}, |
| 2009 | QCBOR_ERR_NO_MORE_ITEMS |
| 2010 | }, |
| 2011 | { "Deeply nested indefinite length arrays with deepest one unclosed", |
| 2012 | QCBOR_DECODE_MODE_NORMAL, |
| 2013 | {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9}, |
| 2014 | QCBOR_ERR_NO_MORE_ITEMS |
| 2015 | }, |
| 2016 | { "Mixed nesting with indefinite unclosed", |
| 2017 | QCBOR_DECODE_MODE_NORMAL, |
| 2018 | {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9}, |
| 2019 | QCBOR_ERR_NO_MORE_ITEMS }, |
| 2020 | { "Mixed nesting with definite unclosed", |
| 2021 | QCBOR_DECODE_MODE_NORMAL, |
| 2022 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2023 | QCBOR_ERR_BAD_BREAK |
| 2024 | }, |
| 2025 | { "Unclosed indefinite length map in definite length maps", |
| 2026 | QCBOR_DECODE_MODE_NORMAL, |
| 2027 | {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8}, |
| 2028 | QCBOR_ERR_NO_MORE_ITEMS |
| 2029 | }, |
| 2030 | { "Unclosed definite length map in indefinite length maps", |
| 2031 | QCBOR_DECODE_MODE_NORMAL, |
| 2032 | {"\xbf\x01\xbf\x02\xa1", 5}, |
| 2033 | QCBOR_ERR_NO_MORE_ITEMS |
| 2034 | }, |
| 2035 | { "Unclosed indefinite length array in definite length maps", |
| 2036 | QCBOR_DECODE_MODE_NORMAL, |
| 2037 | {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8}, |
| 2038 | QCBOR_ERR_NO_MORE_ITEMS |
| 2039 | }, |
| 2040 | { "Unclosed definite length array in indefinite length maps", |
| 2041 | QCBOR_DECODE_MODE_NORMAL, |
| 2042 | {"\xbf\x01\xbf\x02\x81", 5}, |
| 2043 | QCBOR_ERR_NO_MORE_ITEMS |
| 2044 | }, |
| 2045 | { "Unclosed indefinite length map in definite length arrays", |
| 2046 | QCBOR_DECODE_MODE_NORMAL, |
| 2047 | {"\x81\x82\xbf\xff\xbf", 5}, |
| 2048 | QCBOR_ERR_NO_MORE_ITEMS |
| 2049 | }, |
| 2050 | { "Unclosed definite length map in indefinite length arrays", |
| 2051 | QCBOR_DECODE_MODE_NORMAL, |
| 2052 | {"\x9f\x9f\xa1", 3}, |
| 2053 | QCBOR_ERR_NO_MORE_ITEMS |
| 2054 | }, |
| 2055 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2056 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2057 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2058 | /* The "argument" for the data item is incomplete */ |
| 2059 | { "Positive integer missing 1 byte argument", |
| 2060 | QCBOR_DECODE_MODE_NORMAL, |
| 2061 | {"\x18", 1}, |
| 2062 | QCBOR_ERR_HIT_END |
| 2063 | }, |
| 2064 | { "Positive integer missing 2 byte argument", |
| 2065 | QCBOR_DECODE_MODE_NORMAL, |
| 2066 | {"\x19", 1}, |
| 2067 | QCBOR_ERR_HIT_END |
| 2068 | }, |
| 2069 | { "Positive integer missing 4 byte argument", |
| 2070 | QCBOR_DECODE_MODE_NORMAL, |
| 2071 | {"\x1a", 1}, |
| 2072 | QCBOR_ERR_HIT_END |
| 2073 | }, |
| 2074 | { "Positive integer missing 8 byte argument", |
| 2075 | QCBOR_DECODE_MODE_NORMAL, |
| 2076 | {"\x1b", 1}, |
| 2077 | QCBOR_ERR_HIT_END |
| 2078 | }, |
| 2079 | { "Positive integer missing 1 byte of 2 byte argument", |
| 2080 | QCBOR_DECODE_MODE_NORMAL, |
| 2081 | {"\x19\x01", 2}, |
| 2082 | QCBOR_ERR_HIT_END |
| 2083 | }, |
| 2084 | { "Positive integer missing 2 bytes of 4 byte argument", |
| 2085 | QCBOR_DECODE_MODE_NORMAL, |
| 2086 | {"\x1a\x01\x02", 3}, |
| 2087 | QCBOR_ERR_HIT_END |
| 2088 | }, |
| 2089 | { "Positive integer missing 1 bytes of 7 byte argument", |
| 2090 | QCBOR_DECODE_MODE_NORMAL, |
| 2091 | {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8}, |
| 2092 | QCBOR_ERR_HIT_END |
| 2093 | }, |
| 2094 | { "Negative integer missing 1 byte argument", |
| 2095 | QCBOR_DECODE_MODE_NORMAL, |
| 2096 | {"\x38", 1}, |
| 2097 | QCBOR_ERR_HIT_END |
| 2098 | }, |
| 2099 | { "Binary string missing 1 byte argument", |
| 2100 | QCBOR_DECODE_MODE_NORMAL, |
| 2101 | {"\x58", 1}, |
| 2102 | QCBOR_ERR_HIT_END |
| 2103 | }, |
| 2104 | { "Text string missing 1 byte argument", |
| 2105 | QCBOR_DECODE_MODE_NORMAL, |
| 2106 | {"\x78", 1}, |
| 2107 | QCBOR_ERR_HIT_END |
| 2108 | }, |
| 2109 | { "Array missing 1 byte argument", |
| 2110 | QCBOR_DECODE_MODE_NORMAL, |
| 2111 | {"\x98", 1}, |
| 2112 | QCBOR_ERR_HIT_END |
| 2113 | }, |
| 2114 | { "Map missing 1 byte argument", |
| 2115 | QCBOR_DECODE_MODE_NORMAL, |
| 2116 | {"\xb8", 1}, |
| 2117 | QCBOR_ERR_HIT_END |
| 2118 | }, |
| 2119 | { "Tag missing 1 byte argument", |
| 2120 | QCBOR_DECODE_MODE_NORMAL, |
| 2121 | {"\xd8", 1}, |
| 2122 | QCBOR_ERR_HIT_END |
| 2123 | }, |
| 2124 | { "Simple missing 1 byte argument", |
| 2125 | QCBOR_DECODE_MODE_NORMAL, |
| 2126 | {"\xf8", 1}, |
| 2127 | QCBOR_ERR_HIT_END |
| 2128 | }, |
| 2129 | { "half-precision with 1 byte argument", |
| 2130 | QCBOR_DECODE_MODE_NORMAL, |
| 2131 | {"\xf9\x00", 2}, |
| 2132 | QCBOR_ERR_HIT_END |
| 2133 | }, |
| 2134 | { "single-precision with 2 byte argument", |
| 2135 | QCBOR_DECODE_MODE_NORMAL, |
| 2136 | {"\0xfa\x00\x00", 3}, |
| 2137 | QCBOR_ERR_HIT_END |
| 2138 | }, |
| 2139 | { "double-precision with 3 byte argument", |
| 2140 | QCBOR_DECODE_MODE_NORMAL, |
| 2141 | {"\xfb\x00\x00\x00", 4}, |
| 2142 | QCBOR_ERR_HIT_END |
| 2143 | }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2144 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2145 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2146 | { "Tag with no content", |
| 2147 | QCBOR_DECODE_MODE_NORMAL, |
| 2148 | {"\xc0", 1}, |
| 2149 | QCBOR_ERR_HIT_END |
| 2150 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2151 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2152 | { "Tag with no content", |
| 2153 | QCBOR_DECODE_MODE_NORMAL, |
| 2154 | {"\xc0", 1}, |
| 2155 | QCBOR_ERR_TAGS_DISABLED |
| 2156 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2157 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2158 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2159 | /* Breaks must not occur in definite length arrays and maps */ |
| 2160 | { "Array of length 1 with sole member replaced by a break", |
| 2161 | QCBOR_DECODE_MODE_NORMAL, |
| 2162 | {"\x81\xff", 2}, |
| 2163 | QCBOR_ERR_BAD_BREAK |
| 2164 | }, |
| 2165 | { "Array of length 2 with 2nd member replaced by a break", |
| 2166 | QCBOR_DECODE_MODE_NORMAL, |
| 2167 | {"\x82\x00\xff", 3}, |
| 2168 | QCBOR_ERR_BAD_BREAK |
| 2169 | }, |
| 2170 | { "Map of length 1 with sole member label replaced by a break", |
| 2171 | QCBOR_DECODE_MODE_NORMAL, |
| 2172 | {"\xa1\xff", 2}, |
| 2173 | QCBOR_ERR_BAD_BREAK |
| 2174 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2175 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2176 | /* Map of length 1 with sole member label replaced by break */ |
| 2177 | { "Alternate representation that some decoders handle differently", |
| 2178 | QCBOR_DECODE_MODE_NORMAL, |
| 2179 | {"\xa1\xff\x00", 3}, |
| 2180 | QCBOR_ERR_BAD_BREAK |
| 2181 | }, |
| 2182 | { "Array of length 1 with 2nd member value replaced by a break", |
| 2183 | QCBOR_DECODE_MODE_NORMAL, |
| 2184 | {"\xa1\x00\xff", 3}, |
| 2185 | QCBOR_ERR_BAD_BREAK |
| 2186 | }, |
| 2187 | { "Map of length 2 with 2nd member replaced by a break", |
| 2188 | QCBOR_DECODE_MODE_NORMAL, |
| 2189 | {"\xa2\x00\x00\xff", 4}, |
| 2190 | QCBOR_ERR_BAD_BREAK |
| 2191 | }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2192 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2193 | /* Breaks must not occur on their own out of an indefinite length data item */ |
| 2194 | { "A bare break is not well formed", |
| 2195 | QCBOR_DECODE_MODE_NORMAL, |
| 2196 | {"\xff", 1}, |
| 2197 | QCBOR_ERR_BAD_BREAK |
| 2198 | }, |
| 2199 | { "A bare break after a zero length definite length array", |
| 2200 | QCBOR_DECODE_MODE_NORMAL, |
| 2201 | {"\x80\xff", 2}, |
| 2202 | QCBOR_ERR_BAD_BREAK |
| 2203 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2204 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2205 | { "A bare break after a zero length indefinite length map", |
| 2206 | QCBOR_DECODE_MODE_NORMAL, |
| 2207 | {"\x9f\xff\xff", 3}, |
| 2208 | QCBOR_ERR_BAD_BREAK |
| 2209 | }, |
| 2210 | { "A break inside a definite length array inside an indefenite length array", |
| 2211 | QCBOR_DECODE_MODE_NORMAL, |
| 2212 | {"\x9f\x81\xff", 3}, |
| 2213 | QCBOR_ERR_BAD_BREAK |
| 2214 | }, |
| 2215 | { "Complicated mixed nesting with break outside indefinite length array", |
| 2216 | QCBOR_DECODE_MODE_NORMAL, |
| 2217 | {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10}, |
| 2218 | QCBOR_ERR_BAD_BREAK }, |
| 2219 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 2220 | |
| 2221 | /* Forbidden two byte encodings of simple types */ |
| 2222 | { "Must use 0xe0 instead", |
| 2223 | QCBOR_DECODE_MODE_NORMAL, |
| 2224 | {"\xf8\x00", 2}, |
| 2225 | QCBOR_ERR_BAD_TYPE_7 |
| 2226 | }, |
| 2227 | { "Should use 0xe1 instead", |
| 2228 | QCBOR_DECODE_MODE_NORMAL, |
| 2229 | {"\xf8\x01", 2}, |
| 2230 | QCBOR_ERR_BAD_TYPE_7 |
| 2231 | }, |
| 2232 | { "Should use 0xe2 instead", |
| 2233 | QCBOR_DECODE_MODE_NORMAL, |
| 2234 | {"\xf8\x02", 2}, |
| 2235 | QCBOR_ERR_BAD_TYPE_7 |
| 2236 | }, { "Should use 0xe3 instead", |
| 2237 | QCBOR_DECODE_MODE_NORMAL, |
| 2238 | {"\xf8\x03", 2}, |
| 2239 | QCBOR_ERR_BAD_TYPE_7 |
| 2240 | }, |
| 2241 | { "Should use 0xe4 instead", |
| 2242 | QCBOR_DECODE_MODE_NORMAL, |
| 2243 | {"\xf8\x04", 2}, |
| 2244 | QCBOR_ERR_BAD_TYPE_7 |
| 2245 | }, |
| 2246 | { "Should use 0xe5 instead", |
| 2247 | QCBOR_DECODE_MODE_NORMAL, |
| 2248 | {"\xf8\x05", 2}, |
| 2249 | QCBOR_ERR_BAD_TYPE_7 |
| 2250 | }, |
| 2251 | { "Should use 0xe6 instead", |
| 2252 | QCBOR_DECODE_MODE_NORMAL, |
| 2253 | {"\xf8\x06", 2}, |
| 2254 | QCBOR_ERR_BAD_TYPE_7 |
| 2255 | }, |
| 2256 | { "Should use 0xe7 instead", |
| 2257 | QCBOR_DECODE_MODE_NORMAL, |
| 2258 | {"\xf8\x07", 2}, |
| 2259 | QCBOR_ERR_BAD_TYPE_7 |
| 2260 | }, |
| 2261 | { "Should use 0xe8 instead", |
| 2262 | QCBOR_DECODE_MODE_NORMAL, |
| 2263 | {"\xf8\x08", 2}, |
| 2264 | QCBOR_ERR_BAD_TYPE_7 |
| 2265 | }, |
| 2266 | { "Should use 0xe9 instead", |
| 2267 | QCBOR_DECODE_MODE_NORMAL, |
| 2268 | {"\xf8\x09", 2}, |
| 2269 | QCBOR_ERR_BAD_TYPE_7 |
| 2270 | }, |
| 2271 | { "Should use 0xea instead", |
| 2272 | QCBOR_DECODE_MODE_NORMAL, |
| 2273 | {"\xf8\x0a", 2}, |
| 2274 | QCBOR_ERR_BAD_TYPE_7 |
| 2275 | }, |
| 2276 | { "Should use 0xeb instead", |
| 2277 | QCBOR_DECODE_MODE_NORMAL, |
| 2278 | {"\xf8\x0b", 2}, |
| 2279 | QCBOR_ERR_BAD_TYPE_7 |
| 2280 | }, |
| 2281 | { "Should use 0xec instead", |
| 2282 | QCBOR_DECODE_MODE_NORMAL, |
| 2283 | {"\xf8\x0c", 2}, |
| 2284 | QCBOR_ERR_BAD_TYPE_7 |
| 2285 | }, |
| 2286 | { "Should use 0xed instead", |
| 2287 | QCBOR_DECODE_MODE_NORMAL, |
| 2288 | {"\xf8\x0d", 2}, |
| 2289 | QCBOR_ERR_BAD_TYPE_7 |
| 2290 | }, |
| 2291 | { "Should use 0xee instead", |
| 2292 | QCBOR_DECODE_MODE_NORMAL, |
| 2293 | {"\xf8\x0e", 2}, |
| 2294 | QCBOR_ERR_BAD_TYPE_7 |
| 2295 | }, |
| 2296 | { "Should use 0xef instead", |
| 2297 | QCBOR_DECODE_MODE_NORMAL, |
| 2298 | {"\xf8\x0f", 2}, |
| 2299 | QCBOR_ERR_BAD_TYPE_7 |
| 2300 | }, |
| 2301 | { "Should use 0xf0 instead", |
| 2302 | QCBOR_DECODE_MODE_NORMAL, |
| 2303 | {"\xf8\x10", 2}, |
| 2304 | QCBOR_ERR_BAD_TYPE_7 |
| 2305 | }, |
| 2306 | { "Should use 0xf1 instead", |
| 2307 | QCBOR_DECODE_MODE_NORMAL, |
| 2308 | {"\xf8\x11", 2}, |
| 2309 | QCBOR_ERR_BAD_TYPE_7 |
| 2310 | }, |
| 2311 | { "Should use 0xf2 instead", |
| 2312 | QCBOR_DECODE_MODE_NORMAL, |
| 2313 | {"\xf8\x12", 2}, |
| 2314 | QCBOR_ERR_BAD_TYPE_7 |
| 2315 | }, |
| 2316 | { "Should use 0xf3 instead", |
| 2317 | QCBOR_DECODE_MODE_NORMAL, |
| 2318 | {"\xf8\x13", 2}, |
| 2319 | QCBOR_ERR_BAD_TYPE_7 |
| 2320 | }, |
| 2321 | { "Should use 0xf4 instead", |
| 2322 | QCBOR_DECODE_MODE_NORMAL, |
| 2323 | {"\xf8\x14", 2}, |
| 2324 | QCBOR_ERR_BAD_TYPE_7 |
| 2325 | }, |
| 2326 | { "Should use 0xf5 instead", |
| 2327 | QCBOR_DECODE_MODE_NORMAL, |
| 2328 | {"\xf8\x15", 2}, |
| 2329 | QCBOR_ERR_BAD_TYPE_7 |
| 2330 | }, |
| 2331 | { "Should use 0xf6 instead", |
| 2332 | QCBOR_DECODE_MODE_NORMAL, |
| 2333 | {"\xf8\x16", 2}, |
| 2334 | QCBOR_ERR_BAD_TYPE_7 |
| 2335 | }, |
| 2336 | { "Should use 0xef7 instead", |
| 2337 | QCBOR_DECODE_MODE_NORMAL, |
| 2338 | {"\xf8\x17", 2}, |
| 2339 | QCBOR_ERR_BAD_TYPE_7 |
| 2340 | }, |
| 2341 | { "Should use 0xef8 instead", |
| 2342 | QCBOR_DECODE_MODE_NORMAL, |
| 2343 | {"\xf8\x18", 2}, |
| 2344 | QCBOR_ERR_BAD_TYPE_7 |
| 2345 | }, |
| 2346 | { "Reserved", |
| 2347 | QCBOR_DECODE_MODE_NORMAL, |
| 2348 | {"\xf8\x18", 2}, |
| 2349 | QCBOR_ERR_BAD_TYPE_7 |
| 2350 | }, |
| 2351 | |
| 2352 | /* Maps must have an even number of data items (key & value) */ |
| 2353 | { "Map with 1 item when it should have 2", |
| 2354 | QCBOR_DECODE_MODE_NORMAL, |
| 2355 | {"\xa1\x00", 2}, |
| 2356 | QCBOR_ERR_HIT_END |
| 2357 | }, |
| 2358 | { "Map with 3 item when it should have 4", |
| 2359 | QCBOR_DECODE_MODE_NORMAL, |
| 2360 | {"\xa2\x00\x00\x00", 2}, |
| 2361 | QCBOR_ERR_HIT_END |
| 2362 | }, |
| 2363 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 2364 | { "Map with 1 item when it should have 2", |
| 2365 | QCBOR_DECODE_MODE_NORMAL, |
| 2366 | {"\xbf\x00\xff", 3}, |
| 2367 | QCBOR_ERR_BAD_BREAK |
| 2368 | }, |
| 2369 | { "Map with 3 item when it should have 4", |
| 2370 | QCBOR_DECODE_MODE_NORMAL, |
| 2371 | {"\xbf\x00\x00\x00\xff", 5}, |
| 2372 | QCBOR_ERR_BAD_BREAK |
| 2373 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2374 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2375 | |
| 2376 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2377 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2378 | /* In addition to not-well-formed, some invalid CBOR */ |
| 2379 | { "Text-based date, with an integer", |
| 2380 | QCBOR_DECODE_MODE_NORMAL, |
| 2381 | {"\xc0\x00", 2}, |
| 2382 | QCBOR_ERR_BAD_OPT_TAG |
| 2383 | }, |
| 2384 | { "Epoch date, with an byte string", |
| 2385 | QCBOR_DECODE_MODE_NORMAL, |
| 2386 | {"\xc1\x41\x33", 3}, |
| 2387 | QCBOR_ERR_BAD_OPT_TAG |
| 2388 | }, |
| 2389 | { "tagged as both epoch and string dates", |
| 2390 | QCBOR_DECODE_MODE_NORMAL, |
| 2391 | {"\xc1\xc0\x00", 3}, |
| 2392 | QCBOR_ERR_BAD_OPT_TAG |
| 2393 | }, |
| 2394 | { "big num tagged an int, not a byte string", |
| 2395 | QCBOR_DECODE_MODE_NORMAL, |
| 2396 | {"\xc2\x00", 2}, |
| 2397 | QCBOR_ERR_BAD_OPT_TAG |
| 2398 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2399 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2400 | /* In addition to not-well-formed, some invalid CBOR */ |
| 2401 | { "Text-based date, with an integer", |
| 2402 | QCBOR_DECODE_MODE_NORMAL, |
| 2403 | {"\xc0\x00", 2}, |
| 2404 | QCBOR_ERR_TAGS_DISABLED |
| 2405 | }, |
| 2406 | { "Epoch date, with an byte string", |
| 2407 | QCBOR_DECODE_MODE_NORMAL, |
| 2408 | {"\xc1\x41\x33", 3}, |
| 2409 | QCBOR_ERR_TAGS_DISABLED |
| 2410 | }, |
| 2411 | { "tagged as both epoch and string dates", |
| 2412 | QCBOR_DECODE_MODE_NORMAL, |
| 2413 | {"\xc1\xc0\x00", 3}, |
| 2414 | QCBOR_ERR_TAGS_DISABLED |
| 2415 | }, |
| 2416 | { "big num tagged an int, not a byte string", |
| 2417 | QCBOR_DECODE_MODE_NORMAL, |
| 2418 | {"\xc2\x00", 2}, |
| 2419 | QCBOR_ERR_TAGS_DISABLED |
| 2420 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2421 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2422 | }; |
| 2423 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2424 | |
| 2425 | |
| 2426 | int32_t |
| 2427 | DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2428 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2429 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2430 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 2431 | nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2432 | if(nResult) { |
| 2433 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2434 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2435 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2436 | // Corrupt the UsefulInputBuf and see that |
| 2437 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2438 | QCBORDecodeContext DCtx; |
| 2439 | QCBORItem Item; |
| 2440 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2441 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2442 | QCBORDecode_Init(&DCtx, |
| 2443 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2444 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2445 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2446 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2447 | return (int32_t)uQCBORError; |
| 2448 | } |
| 2449 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 2450 | // This wasn't supposed to happen |
| 2451 | return -1; |
| 2452 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2453 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2454 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2455 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2456 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2457 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2458 | // Did not get back the error expected |
| 2459 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2460 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2461 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2462 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2463 | /* |
| 2464 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 2465 | tests here can be performed to see that the max length |
| 2466 | error check works correctly. See DecodeBytes(). If the max |
| 2467 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2468 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2469 | This test will automatocally adapt the all CPU sizes |
| 2470 | through the use of SIZE_MAX. |
| 2471 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2472 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2473 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2474 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2475 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2476 | // This makes a CBOR head with a text string that is very long |
| 2477 | // but doesn't fill in the bytes of the text string as that is |
| 2478 | // not needed to test this part of QCBOR. |
| 2479 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 2480 | |
| 2481 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 2482 | |
| 2483 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2484 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2485 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2486 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2487 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2491 | /* Try all 256 values of the byte at nLen including recursing for |
| 2492 | each of the values to try values at nLen+1 ... up to nLenMax |
| 2493 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2494 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2495 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2496 | if(nLen >= nLenMax) { |
| 2497 | return; |
| 2498 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2499 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2500 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 2501 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2502 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2503 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2504 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2505 | // Get ready to parse |
| 2506 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2507 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2508 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2509 | // Parse by getting the next item until an error occurs |
| 2510 | // Just about every possible decoder error can occur here |
| 2511 | // The goal of this test is not to check for the correct |
| 2512 | // error since that is not really possible. It is to |
| 2513 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2514 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2515 | QCBORItem Item; |
| 2516 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2517 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2518 | break; |
| 2519 | } |
| 2520 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2521 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2522 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2527 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2528 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2529 | // Size 2 tests 64K inputs and runs quickly |
| 2530 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2531 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2532 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2533 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2534 | return 0; |
| 2535 | } |
| 2536 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2537 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2538 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2539 | { |
| 2540 | // size 3 tests 16 million inputs and runs OK |
| 2541 | // in seconds on fast machines. Size 4 takes |
| 2542 | // 10+ minutes and 5 half a day on fast |
| 2543 | // machines. This test is kept separate from |
| 2544 | // the others so as to no slow down the use |
| 2545 | // of them as a very frequent regression. |
| 2546 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2547 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2548 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2549 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2550 | return 0; |
| 2551 | } |
| 2552 | |
| 2553 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2554 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2555 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2556 | 0xc0, // tag for string date |
| 2557 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2558 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2559 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2560 | 0xc0, // tag for string date |
| 2561 | 0x00, // Wrong type for a string date |
| 2562 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2563 | /* 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] | 2564 | 0xc1, // tag for epoch date |
| 2565 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2566 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2567 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2568 | 0xc1, |
| 2569 | 0x62, 'h', 'i', // wrong type tagged |
| 2570 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2571 | /* 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] | 2572 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2573 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2574 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2575 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2576 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2577 | 0xc1, // tag for epoch date |
| 2578 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2579 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2580 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2581 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2582 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2583 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2584 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2585 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2586 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2587 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2588 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2589 | 0xc1, // tag for epoch date |
| 2590 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2591 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2592 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2593 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2594 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2595 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2596 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2597 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2598 | 0xc1, // tag for epoch date |
| 2599 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 2600 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2601 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2602 | 0xc1, |
| 2603 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 2604 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2605 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2606 | 0xc1, // tag for epoch date |
| 2607 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2608 | }; |
| 2609 | |
| 2610 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2611 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2612 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2613 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 2614 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2615 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2616 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2617 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2618 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2619 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2620 | return diff > 0.0000001; |
| 2621 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2622 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2623 | |
| 2624 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2625 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2626 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2627 | { |
| 2628 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2629 | QCBORItem Item; |
| 2630 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2631 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2632 | QCBORDecode_Init(&DCtx, |
| 2633 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2634 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2635 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2636 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2637 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2638 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2639 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2640 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | ba58768 | 2024-02-07 16:46:43 -0800 | [diff] [blame] | 2641 | UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2642 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2643 | } |
| 2644 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2645 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2646 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2647 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2648 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2649 | } |
| 2650 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2651 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 2652 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2653 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2654 | return -4; |
| 2655 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2656 | if(uError == QCBOR_SUCCESS) { |
| 2657 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2658 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2659 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2660 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2661 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2662 | ) { |
| 2663 | return -5; |
| 2664 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2667 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2668 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 2669 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2670 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2671 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2672 | /* 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] | 2673 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 2674 | // The date is decoded and the two tags are returned. This is to |
| 2675 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2676 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2677 | return -7; |
| 2678 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2679 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2680 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2681 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2682 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2683 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2684 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2685 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2686 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2687 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2688 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2689 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2690 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2691 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2692 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2693 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 2694 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2695 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2696 | return -10; |
| 2697 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2698 | if(uError == QCBOR_SUCCESS) { |
| 2699 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2700 | Item.val.epochDate.nSeconds != 1 |
| 2701 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2702 | || 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] | 2703 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2704 | ) { |
| 2705 | return -11; |
| 2706 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2707 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2708 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2709 | /* 8. Epoch date with too-large single-precision float */ |
| 2710 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2711 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2712 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2713 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2714 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2715 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 2716 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2717 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2718 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2721 | /* 10. Epoch date with largest supported double-precision value */ |
| 2722 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2723 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2724 | return -14; |
| 2725 | } |
| 2726 | if(uError == QCBOR_SUCCESS) { |
| 2727 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2728 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 2729 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2730 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 2731 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2732 | ) { |
| 2733 | return -14; |
| 2734 | } |
| 2735 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2736 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2737 | /* 11. Epoch date with single-precision NaN */ |
| 2738 | 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] | 2739 | return -15; |
| 2740 | } |
| 2741 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2742 | /* 12. Epoch date with double-precision plus infinity */ |
| 2743 | 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] | 2744 | return -16; |
| 2745 | } |
| 2746 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2747 | /* 13. Epoch date with half-precision negative infinity */ |
| 2748 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2749 | 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] | 2750 | return -17; |
| 2751 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2752 | |
| 2753 | return 0; |
| 2754 | } |
| 2755 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2756 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2757 | /* |
| 2758 | Test cases covered here. Some items cover more than one of these. |
| 2759 | positive integer (zero counts as a positive integer) |
| 2760 | negative integer |
| 2761 | half-precision float |
| 2762 | single-precision float |
| 2763 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2764 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2765 | float Overflow error |
| 2766 | Wrong type error for epoch |
| 2767 | Wrong type error for date string |
| 2768 | float disabled error |
| 2769 | half-precision disabled error |
| 2770 | -Infinity |
| 2771 | Slightly too large integer |
| 2772 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2773 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2774 | Get epoch by int |
| 2775 | Get string by int |
| 2776 | Get epoch by string |
| 2777 | Get string by string |
| 2778 | Fail to get epoch by wrong int label |
| 2779 | Fail to get string by wrong string label |
| 2780 | Fail to get epoch by string because it is invalid |
| 2781 | Fail to get epoch by int because it is invalid |
| 2782 | |
| 2783 | Untagged values |
| 2784 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2785 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2786 | 0x87, // array of 7 items |
| 2787 | |
| 2788 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 2789 | |
| 2790 | // Untagged integer 0 |
| 2791 | 0x08, |
| 2792 | 0x00, |
| 2793 | |
| 2794 | // Utagged date string with string label y |
| 2795 | 0x61, 0x79, |
| 2796 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 2797 | |
| 2798 | // Untagged single-precision float with value 3.14 with string label x |
| 2799 | 0x61, 0x78, |
| 2800 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 2801 | |
| 2802 | // Untagged half-precision float with value -2 |
| 2803 | 0x09, |
| 2804 | 0xF9, 0xC0, 0x00, |
| 2805 | |
| 2806 | /* Untagged date-only date string */ |
| 2807 | 0x18, 0x63, |
| 2808 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2809 | |
| 2810 | /* Untagged days-count epoch date */ |
| 2811 | 0x11, |
| 2812 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 2813 | |
| 2814 | // End of map, back to array |
| 2815 | |
| 2816 | 0xa7, // Open map of tagged items with labels |
| 2817 | |
| 2818 | 0x00, |
| 2819 | 0xc0, // tag for string date |
| 2820 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 2821 | |
| 2822 | |
| 2823 | 0x01, |
| 2824 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 2825 | 0xc1, // tag for epoch date |
| 2826 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2827 | |
| 2828 | 0x05, |
| 2829 | 0xc1, |
| 2830 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 2831 | |
| 2832 | |
| 2833 | 0x07, |
| 2834 | 0xc1, // tag for epoch date |
| 2835 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2836 | |
| 2837 | /* Tagged days-count epoch date */ |
| 2838 | 0x63, 0x53, 0x44, 0x45, |
| 2839 | 0xD8, 0x64, /* tag(100) */ |
| 2840 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 2841 | |
| 2842 | // Untagged -1000 with label z |
| 2843 | 0x61, 0x7a, |
| 2844 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 2845 | 0x39, 0x03, 0xe7, |
| 2846 | |
| 2847 | /* Tagged date-only date string */ |
| 2848 | 0x63, 0x53, 0x44, 0x53, |
| 2849 | 0xD9, 0x03, 0xEC, |
| 2850 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2851 | |
| 2852 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2853 | |
| 2854 | 0xc1, |
| 2855 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 2856 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2857 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2858 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 2859 | |
| 2860 | 0xc1, // tag for epoch date |
| 2861 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 2862 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2863 | // These two at the end because they are unrecoverable errors |
| 2864 | 0xc1, // tag for epoch date |
| 2865 | 0x80, // Erroneous empty array as content for date |
| 2866 | |
| 2867 | 0xc0, // tag for string date |
| 2868 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2869 | }; |
| 2870 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2871 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2872 | { |
| 2873 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2874 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2875 | int64_t nEpochDate3, nEpochDate5, |
| 2876 | nEpochDate4, nEpochDate6, |
| 2877 | nEpochDays2; |
| 2878 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2879 | |
| 2880 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2881 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2882 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2883 | |
| 2884 | /* Items are in an array or map to test look up by label and other |
| 2885 | * that might not occur in isolated items. But it does make the |
| 2886 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2887 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2888 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2889 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2890 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2891 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2892 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2893 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2894 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2895 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2896 | return 104; |
| 2897 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2898 | if(uError == QCBOR_SUCCESS) { |
| 2899 | if(nEpochDate5 != 3) { |
| 2900 | return 103; |
| 2901 | } |
| 2902 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2903 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2904 | // A half-precision date with value -2 FFF |
| 2905 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2906 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2907 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2908 | 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] | 2909 | return 106; |
| 2910 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2911 | if(uError == QCBOR_SUCCESS) { |
| 2912 | if(nEpochDate4 != -2) { |
| 2913 | return 105; |
| 2914 | } |
| 2915 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2916 | |
| 2917 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2918 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 2919 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2920 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2921 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2922 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2923 | return 107; |
| 2924 | } |
| 2925 | |
| 2926 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2927 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2928 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2929 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2930 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2931 | return 108; |
| 2932 | } |
| 2933 | |
| 2934 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2935 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 2936 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2937 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2938 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2939 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2940 | return 109; |
| 2941 | } |
| 2942 | |
| 2943 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2944 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2945 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2946 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2947 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2948 | return 110; |
| 2949 | } |
| 2950 | |
| 2951 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2952 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2953 | |
| 2954 | // Untagged integer 0 |
| 2955 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2956 | &nEpochDate3); |
| 2957 | // Untagged date string |
| 2958 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2959 | &StringDate2); |
| 2960 | |
| 2961 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2962 | &StringDays2); |
| 2963 | |
| 2964 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2965 | &nEpochDays2); |
| 2966 | |
| 2967 | QCBORDecode_ExitMap(&DC); |
| 2968 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 2969 | return 3001; |
| 2970 | } |
| 2971 | |
| 2972 | // The map of tagged items |
| 2973 | QCBORDecode_EnterMap(&DC, NULL); |
| 2974 | |
| 2975 | #ifndef QCBOR_DISABLE_TAGS |
| 2976 | int64_t nEpochDate2, |
| 2977 | nEpochDateFail, |
| 2978 | nEpochDate1400000000, nEpochDays1; |
| 2979 | UsefulBufC StringDays1; |
| 2980 | uint64_t uTag1, uTag2; |
| 2981 | |
| 2982 | // Tagged date string |
| 2983 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2984 | &StringDate1); |
| 2985 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2986 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2987 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2988 | 1, |
| 2989 | QCBOR_TAG_REQUIREMENT_TAG | |
| 2990 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2991 | &nEpochDate1400000000); |
| 2992 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2993 | |
| 2994 | // Get largest negative double precision epoch date allowed |
| 2995 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2996 | 5, |
| 2997 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 2998 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2999 | &nEpochDate2); |
| 3000 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3001 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3002 | return 102; |
| 3003 | } |
| 3004 | if(uError == QCBOR_SUCCESS) { |
| 3005 | if(nEpochDate2 != -9223372036854773760LL) { |
| 3006 | return 101; |
| 3007 | } |
| 3008 | } |
| 3009 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3010 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3011 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 3012 | "z", |
| 3013 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 3014 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 3015 | &nEpochDate6); |
| 3016 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3017 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3018 | |
| 3019 | // Get largest double precision epoch date allowed |
| 3020 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3021 | &nEpochDate2); |
| 3022 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3023 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 3024 | return 112; |
| 3025 | } |
| 3026 | if(uError == QCBOR_SUCCESS) { |
| 3027 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 3028 | return 111; |
| 3029 | } |
| 3030 | } |
| 3031 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3032 | /* The days format is much simpler than the date format |
| 3033 | * because it can't be a floating point value. The test |
| 3034 | * of the spiffy decode functions sufficiently covers |
| 3035 | * the test of the non-spiffy decode days date decoding. |
| 3036 | * There is no full fan out of the error conditions |
| 3037 | * and decode options as that is implemented by code |
| 3038 | * that is tested well by the date testing above. |
| 3039 | */ |
| 3040 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 3041 | &StringDays1); |
| 3042 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3043 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 3044 | &nEpochDays1); |
| 3045 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3046 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3047 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 3048 | return 3001; |
| 3049 | } |
| 3050 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3051 | // Too-negative float, -9.2233720368547748E+18 |
| 3052 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3053 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3054 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3055 | return 1111; |
| 3056 | } |
| 3057 | |
| 3058 | // Too-large integer |
| 3059 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3060 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3061 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 3062 | return 1; |
| 3063 | } |
| 3064 | |
| 3065 | // Half-precision minus infinity |
| 3066 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3067 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3068 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 3069 | return 2; |
| 3070 | } |
| 3071 | |
| 3072 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3073 | // Bad content for epoch date |
| 3074 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 3075 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3076 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3077 | return 3; |
| 3078 | } |
| 3079 | |
| 3080 | // Bad content for string date |
| 3081 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 3082 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3083 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 3084 | return 4; |
| 3085 | } |
| 3086 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3087 | QCBORDecode_ExitArray(&DC); |
| 3088 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3089 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3090 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3091 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3092 | #else /* QCBOR_DISABLE_TAGS */ |
| 3093 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 3094 | &StringDate1); |
| 3095 | uError = QCBORDecode_GetAndResetError(&DC); |
| 3096 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 3097 | return 4; |
| 3098 | } |
| 3099 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3100 | |
| 3101 | |
| 3102 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3103 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3104 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3105 | return 200; |
| 3106 | } |
| 3107 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3108 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3109 | return 201; |
| 3110 | } |
| 3111 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3112 | if(nEpochDays1 != -10676) { |
| 3113 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3114 | } |
| 3115 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3116 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3117 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3120 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3121 | return 204; |
| 3122 | } |
| 3123 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3124 | if(nEpochDate6 != -1000) { |
| 3125 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3126 | } |
| 3127 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3128 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3129 | return 205; |
| 3130 | } |
| 3131 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3132 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3133 | |
| 3134 | if(nEpochDate3 != 0) { |
| 3135 | return 202; |
| 3136 | } |
| 3137 | |
| 3138 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3139 | return 206; |
| 3140 | } |
| 3141 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3142 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 3143 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 3147 | return 208; |
| 3148 | } |
| 3149 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3150 | return 0; |
| 3151 | } |
| 3152 | |
| 3153 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3154 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3155 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3156 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3157 | 0x81, // Array of one |
| 3158 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 3159 | 0x82, // Array of two that is the faction 1/3 |
| 3160 | 0x01, |
| 3161 | 0x03, |
| 3162 | |
| 3163 | /* |
| 3164 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3165 | */ |
| 3166 | 0xd8, 0xe1, |
| 3167 | 0xd8, 0xe2, |
| 3168 | 0xd8, 0xe3, |
| 3169 | 0xd8, 0xe4, |
| 3170 | 0xd8, 0xe5, |
| 3171 | 0x80, |
| 3172 | |
| 3173 | /* tag 10489608748473423768( |
| 3174 | 2442302356( |
| 3175 | 21590( |
| 3176 | 240( |
| 3177 | [])))) |
| 3178 | */ |
| 3179 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3180 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 3181 | 0xd9, 0x54, 0x56, |
| 3182 | 0xd8, 0xf0, |
| 3183 | 0x80, |
| 3184 | |
| 3185 | /* tag 21590( |
| 3186 | 10489608748473423768( |
| 3187 | 2442302357( |
| 3188 | 65534( |
| 3189 | [])))) |
| 3190 | */ |
| 3191 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 3192 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3193 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3194 | 0xd9, 0xff, 0xfe, |
| 3195 | 0x80, |
| 3196 | |
| 3197 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3198 | works in conjuntion with entries above. |
| 3199 | 269488144(269488145(269488146(269488147([])))) |
| 3200 | */ |
| 3201 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 3202 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 3203 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 3204 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 3205 | 0x80, |
| 3206 | |
| 3207 | /* An invalid decimal fraction with an additional tag */ |
| 3208 | 0xd9, 0xff, 0xfa, |
| 3209 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 3210 | 0x00, // the integer 0; should be a byte string |
| 3211 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3212 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3213 | /* |
| 3214 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3215 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3216 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3217 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3218 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3219 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3220 | /* |
| 3221 | DB 9192939495969798 # tag(10489608748473423768) |
| 3222 | D8 88 # tag(136) |
| 3223 | C6 # tag(6) |
| 3224 | C7 # tag(7) |
| 3225 | 80 # array(0) |
| 3226 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3227 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3228 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3229 | |
| 3230 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3231 | 55799(55799(55799({ |
| 3232 | 6(7(-23)): 5859837686836516696(7({ |
| 3233 | 7(-20): 11({ |
| 3234 | 17(-18): 17(17(17("Organization"))), |
| 3235 | 9(-17): 773("SSG"), |
| 3236 | -15: 16(17(6(7("Confusion")))), |
| 3237 | 17(-16): 17("San Diego"), |
| 3238 | 17(-14): 17("US") |
| 3239 | }), |
| 3240 | 23(-19): 19({ |
| 3241 | -11: 9({ |
| 3242 | -9: -7 |
| 3243 | }), |
| 3244 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 3245 | }) |
| 3246 | })), |
| 3247 | 16(-22): 23({ |
| 3248 | 11(8(7(-5))): 8(-3) |
| 3249 | }) |
| 3250 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3251 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3252 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3253 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 3254 | 0xc6, 0xc7, 0x36, |
| 3255 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 3256 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 3257 | 0xcb, 0xa5, |
| 3258 | 0xd1, 0x31, |
| 3259 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 3260 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 3261 | 0xc9, 0x30, |
| 3262 | 0xd9, 0x03, 0x05, 0x63, |
| 3263 | 0x53, 0x53, 0x47, |
| 3264 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3265 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 3266 | 0x69, |
| 3267 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3268 | 0xd1, 0x2f, |
| 3269 | 0xd1, 0x69, |
| 3270 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 3271 | 0xd1, 0x2d, |
| 3272 | 0xd1, 0x62, |
| 3273 | 0x55, 0x53, |
| 3274 | 0xd7, 0x32, |
| 3275 | 0xd3, 0xa2, |
| 3276 | 0x2a, |
| 3277 | 0xc9, 0xa1, |
| 3278 | 0x28, |
| 3279 | 0x26, |
| 3280 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 3281 | 0xcc, 0x4a, |
| 3282 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 3283 | 0xd0, 0x35, |
| 3284 | 0xd7, 0xa1, |
| 3285 | 0xcb, 0xc8, 0xc7, 0x24, |
| 3286 | 0xc8, 0x22}; |
| 3287 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3288 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3289 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3290 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3291 | |
| 3292 | 0xc0, // tag for string date |
| 3293 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3294 | |
| 3295 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3296 | |
| 3297 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 3298 | |
| 3299 | 0xd8, 0x23, // tag for regex |
| 3300 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3301 | |
| 3302 | 0xc0, // tag for string date |
| 3303 | 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] | 3304 | |
| 3305 | // This last case makes the array untraversable because it is |
| 3306 | // an uncrecoverable error. Make sure it stays last and is the only |
| 3307 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3308 | }; |
| 3309 | |
| 3310 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3311 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3312 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3313 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3314 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3315 | { |
| 3316 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3317 | QCBORItem Item; |
| 3318 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3319 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3320 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3321 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3322 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3323 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3324 | /* |
| 3325 | This test matches the magic number tag and the fraction tag |
| 3326 | 55799([...]) |
| 3327 | */ |
| 3328 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3329 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3330 | return -2; |
| 3331 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3332 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3333 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 3334 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3335 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3336 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3337 | /* |
| 3338 | 4([1,3]) |
| 3339 | */ |
| 3340 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3341 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3342 | if(uError != QCBOR_SUCCESS || |
| 3343 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3344 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 3345 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 3346 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3347 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3348 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3349 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 3350 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3351 | return -4; |
| 3352 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3353 | // consume the items in the array |
| 3354 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3355 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3356 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3357 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3358 | if(uError != QCBOR_SUCCESS || |
| 3359 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3360 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 3361 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3362 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3363 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3364 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 3365 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3366 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3367 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3368 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3369 | /* |
| 3370 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3371 | */ |
| 3372 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3373 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3374 | return -6; |
| 3375 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3376 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 3377 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 3378 | return -106; |
| 3379 | } |
| 3380 | |
| 3381 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3382 | /* tag 10489608748473423768( |
| 3383 | 2442302356( |
| 3384 | 21590( |
| 3385 | 240( |
| 3386 | [])))) |
| 3387 | */ |
| 3388 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3389 | if(uError != QCBOR_SUCCESS || |
| 3390 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3391 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 3392 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 3393 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 3394 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3395 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | /* tag 21590( |
| 3399 | 10489608748473423768( |
| 3400 | 2442302357( |
| 3401 | 21591( |
| 3402 | [])))) |
| 3403 | */ |
| 3404 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3405 | if(uError != QCBOR_SUCCESS || |
| 3406 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3407 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 3408 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 3409 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 3410 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 3411 | return -8; |
| 3412 | } |
| 3413 | |
| 3414 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3415 | works in conjuntion with entries above. |
| 3416 | 269488144(269488145(269488146(269488147([])))) |
| 3417 | */ |
| 3418 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3419 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3420 | return -9; |
| 3421 | } |
| 3422 | |
| 3423 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3424 | if(uError == QCBOR_SUCCESS) { |
| 3425 | return -10; |
| 3426 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3427 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3428 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3429 | // 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] | 3430 | // tage and then matches it. Caller-config lists are no longer |
| 3431 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3432 | QCBORDecode_Init(&DCtx, |
| 3433 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3434 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3435 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 3436 | const QCBORTagListIn TL = {2, puList}; |
| 3437 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3438 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3439 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3440 | return -8; |
| 3441 | } |
| 3442 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3443 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 3444 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 3445 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 3446 | Item.val.uCount != 0) { |
| 3447 | return -9; |
| 3448 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3449 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3450 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3451 | // 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] | 3452 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3453 | const uint64_t puLongList[17] = {1,2,1}; |
| 3454 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3455 | QCBORDecode_Init(&DCtx, |
| 3456 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3457 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3458 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 3459 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3460 | return -11; |
| 3461 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3462 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3463 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3464 | QCBORTagListOut Out = {0, 4, puTags}; |
| 3465 | |
| 3466 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3467 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3468 | QCBORDecode_Init(&DCtx, |
| 3469 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3470 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3471 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3472 | return -12; |
| 3473 | } |
| 3474 | if(puTags[0] != 0x9192939495969798 || |
| 3475 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3476 | puTags[2] != 0x06 || |
| 3477 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3478 | return -13; |
| 3479 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3480 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3481 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3482 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3483 | QCBORDecode_Init(&DCtx, |
| 3484 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3485 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3486 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 3487 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3488 | return -14; |
| 3489 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3490 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3491 | |
| 3492 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3493 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3494 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 3495 | // It is a bit of a messy test and maybe could be improved, but |
| 3496 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3497 | QCBORDecode_Init(&DCtx, |
| 3498 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3499 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3500 | int n = CheckCSRMaps(&DCtx); |
| 3501 | if(n) { |
| 3502 | return n-2000; |
| 3503 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3504 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3505 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3506 | QCBORDecode_Init(&DCtx, |
| 3507 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3508 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3509 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3510 | /* With the spiffy decode revision, this tag list is not used. |
| 3511 | It doesn't matter if a tag is in this list or not so some |
| 3512 | tests that couldn't process a tag because it isn't in this list |
| 3513 | now can process these unlisted tags. The tests have been |
| 3514 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3515 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 3516 | const QCBORTagListIn TagList = {3, puTagList}; |
| 3517 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3518 | |
| 3519 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3520 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3521 | return -100; |
| 3522 | } |
| 3523 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3524 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3525 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 3526 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 3527 | Item.val.uCount != 2 || |
| 3528 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 3529 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 3530 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 3531 | Out.uNumUsed != 3) { |
| 3532 | return -101; |
| 3533 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3534 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3535 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3536 | return -102; |
| 3537 | } |
| 3538 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3539 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3540 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3541 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3542 | Item.val.uCount != 2 || |
| 3543 | puTags[0] != 5859837686836516696 || |
| 3544 | puTags[1] != 7 || |
| 3545 | Out.uNumUsed != 2) { |
| 3546 | return -103; |
| 3547 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3548 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3549 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3550 | return -104; |
| 3551 | } |
| 3552 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3553 | Item.val.uCount != 5 || |
| 3554 | puTags[0] != 0x0b || |
| 3555 | Out.uNumUsed != 1) { |
| 3556 | return -105; |
| 3557 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3558 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3559 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3560 | return -106; |
| 3561 | } |
| 3562 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3563 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 3564 | Item.val.string.len != 12 || |
| 3565 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 3566 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 3567 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 3568 | Out.uNumUsed != 3) { |
| 3569 | return -105; |
| 3570 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3571 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3572 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3573 | return -107; |
| 3574 | } |
| 3575 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3576 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 3577 | Item.val.string.len != 3 || |
| 3578 | puTags[0] != 773 || |
| 3579 | Out.uNumUsed != 1) { |
| 3580 | return -108; |
| 3581 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3582 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3583 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3584 | return -109; |
| 3585 | } |
| 3586 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3587 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3588 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3589 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3590 | puTags[3] != 7 || |
| 3591 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3592 | return -110; |
| 3593 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3594 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3595 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3596 | return -111; |
| 3597 | } |
| 3598 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3599 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3600 | Item.val.string.len != 9 || |
| 3601 | puTags[0] != 17 || |
| 3602 | Out.uNumUsed != 1) { |
| 3603 | return -112; |
| 3604 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3605 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3606 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3607 | return -111; |
| 3608 | } |
| 3609 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3610 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3611 | Item.val.string.len != 2 || |
| 3612 | puTags[0] != 17 || |
| 3613 | Out.uNumUsed != 1) { |
| 3614 | return -112; |
| 3615 | } |
| 3616 | |
| 3617 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3618 | return -113; |
| 3619 | } |
| 3620 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3621 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3622 | Item.val.uCount != 2 || |
| 3623 | puTags[0] != 19 || |
| 3624 | Out.uNumUsed != 1) { |
| 3625 | return -114; |
| 3626 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3627 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3628 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3629 | return -115; |
| 3630 | } |
| 3631 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3632 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3633 | Item.val.uCount != 1 || |
| 3634 | puTags[0] != 9 || |
| 3635 | Out.uNumUsed != 1) { |
| 3636 | return -116; |
| 3637 | } |
| 3638 | |
| 3639 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3640 | return -116; |
| 3641 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3642 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3643 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3644 | Out.uNumUsed != 0) { |
| 3645 | return -117; |
| 3646 | } |
| 3647 | |
| 3648 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3649 | return -118; |
| 3650 | } |
| 3651 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 3652 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3653 | puTags[0] != 12 || |
| 3654 | Out.uNumUsed != 1) { |
| 3655 | return -119; |
| 3656 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3657 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3658 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3659 | return -120; |
| 3660 | } |
| 3661 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3662 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 3663 | Item.val.uCount != 1 || |
| 3664 | puTags[0] != 0x17 || |
| 3665 | Out.uNumUsed != 1) { |
| 3666 | return -121; |
| 3667 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3668 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3669 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3670 | return -122; |
| 3671 | } |
| 3672 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3673 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3674 | Item.val.int64 != -3 || |
| 3675 | puTags[0] != 8 || |
| 3676 | Out.uNumUsed != 1) { |
| 3677 | return -123; |
| 3678 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3679 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3680 | if(QCBORDecode_Finish(&DCtx)) { |
| 3681 | return -124; |
| 3682 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3683 | |
| 3684 | UsefulBufC DateString; |
| 3685 | QCBORDecode_Init(&DCtx, |
| 3686 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3687 | QCBOR_DECODE_MODE_NORMAL); |
| 3688 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3689 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3690 | // tagged date string |
| 3691 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3692 | // untagged date string |
| 3693 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3694 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3695 | return 100; |
| 3696 | } |
| 3697 | // untagged byte string |
| 3698 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3699 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3700 | return 101; |
| 3701 | } |
| 3702 | // tagged regex |
| 3703 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3704 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3705 | return 102; |
| 3706 | } |
| 3707 | // tagged date string with a byte string |
| 3708 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3709 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3710 | return 103; |
| 3711 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3712 | // The exit errors out because the last item, the date string with |
| 3713 | // bad content makes the array untraversable (the bad date string |
| 3714 | // could have tag content of an array or such that is not consumed |
| 3715 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3716 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3717 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3718 | return 104; |
| 3719 | } |
| 3720 | |
| 3721 | |
| 3722 | QCBORDecode_Init(&DCtx, |
| 3723 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3724 | QCBOR_DECODE_MODE_NORMAL); |
| 3725 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3726 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3727 | // tagged date string |
| 3728 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3729 | // untagged date string |
| 3730 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3731 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3732 | return 200; |
| 3733 | } |
| 3734 | // untagged byte string |
| 3735 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3736 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3737 | return 201; |
| 3738 | } |
| 3739 | // tagged regex |
| 3740 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3741 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3742 | return 202; |
| 3743 | } |
| 3744 | // tagged date string with a byte string |
| 3745 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3746 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3747 | return 203; |
| 3748 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3749 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3750 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3751 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3752 | return 204; |
| 3753 | } |
| 3754 | |
| 3755 | QCBORDecode_Init(&DCtx, |
| 3756 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3757 | QCBOR_DECODE_MODE_NORMAL); |
| 3758 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3759 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3760 | // tagged date string |
| 3761 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3762 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3763 | return 300; |
| 3764 | } |
| 3765 | // untagged date string |
| 3766 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3767 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3768 | return 301; |
| 3769 | } |
| 3770 | // untagged byte string |
| 3771 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3772 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3773 | return 302; |
| 3774 | } |
| 3775 | // tagged regex |
| 3776 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3777 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3778 | return 303; |
| 3779 | } |
| 3780 | // tagged date string with a byte string |
| 3781 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3782 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3783 | return 304; |
| 3784 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3785 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3786 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3787 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3788 | return 305; |
| 3789 | } |
| 3790 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3791 | return 0; |
| 3792 | } |
| 3793 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3794 | /* |
| 3795 | * These are showing the big numbers converted to integers. |
| 3796 | * The tag numbers are not shown. |
| 3797 | * |
| 3798 | * [ 18446744073709551616, |
| 3799 | * -18446744073709551617, |
| 3800 | * {"BN+": 18446744073709551616, |
| 3801 | * 64: 18446744073709551616, |
| 3802 | * "BN-": -18446744073709551617, |
| 3803 | * -64: -18446744073709551617 |
| 3804 | * } |
| 3805 | * ] |
| 3806 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3807 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3808 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3809 | 0x83, |
| 3810 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3811 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3812 | 0xA4, |
| 3813 | 0x63, 0x42, 0x4E, 0x2B, |
| 3814 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3815 | 0x18, 0x40, |
| 3816 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3817 | 0x63, 0x42, 0x4E, 0x2D, |
| 3818 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3819 | 0x38, 0x3F, |
| 3820 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 3821 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3822 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3823 | /* The expected big num */ |
| 3824 | static const uint8_t spBigNum[] = { |
| 3825 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3826 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3827 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3828 | |
| 3829 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3830 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3831 | { |
| 3832 | QCBORDecodeContext DCtx; |
| 3833 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3834 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3835 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3836 | QCBORDecode_Init(&DCtx, |
| 3837 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 3838 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3839 | |
| 3840 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3841 | // |
| 3842 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 3843 | return -1; |
| 3844 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3845 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3846 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3847 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3848 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3849 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3850 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3851 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3852 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3853 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3854 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | // |
| 3858 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3859 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3860 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3861 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3862 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3863 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3864 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3865 | // |
| 3866 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3867 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3868 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3869 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3870 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3871 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3872 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3873 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3874 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3875 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3876 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3877 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3881 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3882 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3883 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3884 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3885 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3886 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3887 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3888 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3889 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3890 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3891 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3892 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3893 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3894 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3895 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3896 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3897 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3898 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3899 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3900 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3901 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3902 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3903 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3904 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3905 | #else |
| 3906 | |
| 3907 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 3908 | return -100; |
| 3909 | } |
| 3910 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3911 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3912 | return 0; |
| 3913 | } |
| 3914 | |
| 3915 | |
| 3916 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3917 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3918 | uint8_t uDataType, |
| 3919 | uint8_t uNestingLevel, |
| 3920 | uint8_t uNextNest, |
| 3921 | int64_t nLabel, |
| 3922 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3923 | { |
| 3924 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3925 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3926 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3927 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 3928 | if(Item.uDataType != uDataType) return -1; |
| 3929 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3930 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 3931 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 3932 | return -1; |
| 3933 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3934 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3935 | if(Item.label.int64 != nLabel) return -1; |
| 3936 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3937 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3938 | } |
| 3939 | } |
| 3940 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3941 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3942 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3943 | if(pItem) { |
| 3944 | *pItem = Item; |
| 3945 | } |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
| 3949 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3950 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3951 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3952 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3953 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3954 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3955 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3956 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3957 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3958 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3959 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 3960 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 3961 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 3962 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 3963 | 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] | 3964 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3965 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 3966 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3967 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3968 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 3969 | 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] | 3970 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3971 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 3972 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3973 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3974 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3975 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3976 | return 0; |
| 3977 | } |
| 3978 | |
| 3979 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3980 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3981 | { |
| 3982 | -23: { |
| 3983 | -20: { |
| 3984 | -18: "Organization", |
| 3985 | -17: "SSG", |
| 3986 | -15: "Confusion", |
| 3987 | -16: "San Diego", |
| 3988 | -14: "US" |
| 3989 | }, |
| 3990 | -19: { |
| 3991 | -11: { |
| 3992 | -9: -7 |
| 3993 | }, |
| 3994 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 3995 | } |
| 3996 | }, |
| 3997 | -22: { |
| 3998 | -5: -3 |
| 3999 | } |
| 4000 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4001 | */ |
| 4002 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4003 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 4004 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4005 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4006 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4007 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4008 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4009 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 4010 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 4011 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 4012 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4013 | // Same map as above, but using indefinite lengths |
| 4014 | static const uint8_t spCSRInputIndefLen[] = { |
| 4015 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 4016 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 4017 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 4018 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 4019 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 4020 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 4021 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 4022 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 4023 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 4024 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 4025 | |
| 4026 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4027 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4028 | { |
| 4029 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4030 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4031 | QCBORDecode_Init(&DCtx, |
| 4032 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4033 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4034 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4035 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4036 | } |
| 4037 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4038 | |
| 4039 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4040 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4041 | { |
| 4042 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4043 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4044 | QCBORDecode_Init(&DCtx, |
| 4045 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4046 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4047 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4048 | QCBORItem Item; |
| 4049 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4050 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4051 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 4052 | return -1; |
| 4053 | } |
| 4054 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4055 | return -2; |
| 4056 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4057 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 4058 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4059 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 4060 | return -3; |
| 4061 | } |
| 4062 | |
| 4063 | return 0; |
| 4064 | } |
| 4065 | |
| 4066 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4067 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4068 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4069 | { |
| 4070 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4071 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4072 | QCBORDecode_Init(&DCtx, |
| 4073 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 4074 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4075 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 4076 | return CheckCSRMaps(&DCtx); |
| 4077 | } |
| 4078 | |
| 4079 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4080 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4081 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 4082 | { |
| 4083 | UsefulOutBuf UOB; |
| 4084 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4085 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4086 | int i; |
| 4087 | for(i = 0; i < n; i++) { |
| 4088 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 4089 | } |
| 4090 | |
| 4091 | for(i = 0; i < n; i++) { |
| 4092 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4093 | } |
| 4094 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4095 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4096 | |
| 4097 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4098 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4099 | { |
| 4100 | QCBORDecodeContext DC; |
| 4101 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4102 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4103 | int j; |
| 4104 | for(j = 0; j < nNestLevel; j++) { |
| 4105 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4106 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4107 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 4108 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4109 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4110 | return -4; |
| 4111 | } else { |
| 4112 | return 0; // Decoding doesn't recover after an error |
| 4113 | } |
| 4114 | } else { |
| 4115 | // Should be no error |
| 4116 | if(nReturn) { |
| 4117 | return -9; // Should not have got an error |
| 4118 | } |
| 4119 | } |
| 4120 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4121 | return -7; |
| 4122 | } |
| 4123 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4124 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4125 | if(nReturn) { |
| 4126 | return -3; |
| 4127 | } |
| 4128 | return 0; |
| 4129 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4130 | |
| 4131 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4132 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4133 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4134 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4135 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4136 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4137 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4138 | int nReturn = parse_indeflen_nested(Nested, i); |
| 4139 | if(nReturn) { |
| 4140 | return nReturn; |
| 4141 | } |
| 4142 | } |
| 4143 | return 0; |
| 4144 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4145 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4146 | // [1, [2, 3]] |
| 4147 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 4148 | // No closing break |
| 4149 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 4150 | // Not enough closing breaks |
| 4151 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 4152 | // Too many closing breaks |
| 4153 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 4154 | // Unclosed indeflen inside def len |
| 4155 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 4156 | // confused tag |
| 4157 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4158 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4159 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4160 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4161 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4162 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4163 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4164 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4165 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4166 | QCBORDecodeContext DC; |
| 4167 | QCBORItem Item; |
| 4168 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4169 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4170 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4171 | |
| 4172 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4173 | Item.uNestingLevel != 0 || |
| 4174 | Item.uNextNestLevel != 1) { |
| 4175 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4176 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4177 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4178 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4179 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 4180 | Item.uNestingLevel != 1 || |
| 4181 | Item.uNextNestLevel != 1) { |
| 4182 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4183 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4184 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4185 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4186 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 4187 | Item.uNestingLevel != 1 || |
| 4188 | Item.uNextNestLevel != 2) { |
| 4189 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4190 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4191 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4192 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4193 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4194 | Item.uNestingLevel != 2 || |
| 4195 | Item.uNextNestLevel != 2) { |
| 4196 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4197 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4198 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4199 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4200 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4201 | Item.uNestingLevel != 2 || |
| 4202 | Item.uNextNestLevel != 0) { |
| 4203 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4204 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4205 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4206 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4207 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4208 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4209 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4210 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4211 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4212 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4213 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4214 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4215 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4216 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4217 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4218 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4219 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4220 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4221 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4222 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4223 | } |
| 4224 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4225 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4226 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4227 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4228 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4229 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4230 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4231 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4232 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4233 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4237 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4238 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4239 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4240 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4241 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4242 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4243 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4244 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4245 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4246 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4247 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4248 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4249 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4250 | |
| 4251 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4252 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4253 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4254 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4255 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4256 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4257 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4258 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4259 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4260 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4261 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4262 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4263 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4264 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4265 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4266 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4267 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4268 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4269 | return -140; |
| 4270 | } |
| 4271 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4272 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4273 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4274 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4275 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4276 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4277 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4278 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4279 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4280 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4281 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4282 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4283 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4284 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4285 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4286 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4287 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4288 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4289 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4290 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4291 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4292 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4293 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4294 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4295 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4296 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4297 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4298 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4299 | |
| 4300 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4301 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4302 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4303 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4304 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4305 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4306 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4307 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4308 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4309 | #else /* QCBOR_DISABLE_TAGS */ |
| 4310 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 4311 | return -20; |
| 4312 | } |
| 4313 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4314 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4315 | return 0; |
| 4316 | } |
| 4317 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4318 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4319 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 4320 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4321 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4322 | 0x81, // Array of length one |
| 4323 | 0x7f, // text string marked with indefinite length |
| 4324 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4325 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4326 | 0xff // ending break |
| 4327 | }; |
| 4328 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4329 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4330 | 0x81, // Array of length one |
| 4331 | 0x7f, // text string marked with indefinite length |
| 4332 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4333 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 4334 | 0xff // ending break |
| 4335 | }; |
| 4336 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4337 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4338 | 0x81, // Array of length one |
| 4339 | 0x7f, // text string marked with indefinite length |
| 4340 | 0x01, 0x02, // Not a string |
| 4341 | 0xff // ending break |
| 4342 | }; |
| 4343 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4344 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4345 | 0x81, // Array of length one |
| 4346 | 0x7f, // text string marked with indefinite length |
| 4347 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4348 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4349 | // missing end of string |
| 4350 | }; |
| 4351 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4352 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4353 | 0xa1, // Array of length one |
| 4354 | 0x7f, // text string marked with indefinite length |
| 4355 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 4356 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4357 | 0xff, // ending break |
| 4358 | 0x01 // integer being labeled. |
| 4359 | }; |
| 4360 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4361 | /** |
| 4362 | Make an indefinite length string |
| 4363 | |
| 4364 | @param Storage Storage for string, must be 144 bytes in size |
| 4365 | @return The indefinite length string |
| 4366 | |
| 4367 | This makes an array with one indefinite length string that has 7 chunks |
| 4368 | from size of 1 byte up to 64 bytes. |
| 4369 | */ |
| 4370 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4371 | { |
| 4372 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4373 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4374 | UsefulOutBuf_Init(&UOB, Storage); |
| 4375 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 4376 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4377 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4378 | uint8_t uStringByte = 0; |
| 4379 | // Use of type int is intentional |
| 4380 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 4381 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4382 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4383 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 4384 | for(int j = 0; j < uChunkSize; j++) { |
| 4385 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 4386 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4387 | } |
| 4388 | } |
| 4389 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4390 | |
| 4391 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4392 | } |
| 4393 | |
| 4394 | static int CheckBigString(UsefulBufC BigString) |
| 4395 | { |
| 4396 | if(BigString.len != 255) { |
| 4397 | return 1; |
| 4398 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4399 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4400 | for(uint8_t i = 0; i < 255; i++){ |
| 4401 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 4402 | return 1; |
| 4403 | } |
| 4404 | } |
| 4405 | return 0; |
| 4406 | } |
| 4407 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4408 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4409 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4410 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4411 | QCBORDecodeContext DC; |
| 4412 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4413 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4414 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4415 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4416 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4417 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4418 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4419 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4420 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4421 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4422 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4423 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4424 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4425 | return -2; |
| 4426 | } |
| 4427 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 4428 | return -3; |
| 4429 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4430 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4431 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4432 | return -4; |
| 4433 | } |
| 4434 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 4435 | return -5; |
| 4436 | } |
| 4437 | if(QCBORDecode_Finish(&DC)) { |
| 4438 | return -6; |
| 4439 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4440 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4441 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4442 | QCBORDecode_Init(&DC, |
| 4443 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 4444 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4445 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4446 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4447 | return -7; |
| 4448 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4449 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4450 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4451 | return -8; |
| 4452 | } |
| 4453 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4454 | return -9; |
| 4455 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4456 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4457 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4458 | return -10; |
| 4459 | } |
| 4460 | |
| 4461 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4462 | QCBORDecode_Init(&DC, |
| 4463 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 4464 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4465 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4466 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4467 | return -11; |
| 4468 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4469 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4470 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4471 | return -12; |
| 4472 | } |
| 4473 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4474 | return -13; |
| 4475 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4476 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4477 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4478 | return -14; |
| 4479 | } |
| 4480 | |
| 4481 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4482 | QCBORDecode_Init(&DC, |
| 4483 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 4484 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4485 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4486 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4487 | return -15; |
| 4488 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4489 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4490 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4491 | return -16; |
| 4492 | } |
| 4493 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4494 | return -17; |
| 4495 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4496 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4497 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 4498 | return -18; |
| 4499 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4500 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4501 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4502 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4503 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4504 | QCBORDecode_GetNext(&DC, &Item); |
| 4505 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4506 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4507 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4508 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4509 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4510 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4511 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4512 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4513 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4514 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4515 | |
| 4516 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 4517 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4518 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4519 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4520 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4521 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4522 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4523 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4524 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4525 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 4526 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4527 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4528 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4529 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4530 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4531 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4532 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4533 | QCBORDecode_GetNext(&DC, &Item); |
| 4534 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4535 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4536 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4537 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4538 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4539 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4540 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4541 | // ---- big bstr ----- |
| 4542 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4543 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4544 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4545 | return -25; |
| 4546 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4547 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4548 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4549 | return -26; |
| 4550 | } |
| 4551 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4552 | return -26; |
| 4553 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4554 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4555 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4556 | return -27; |
| 4557 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4558 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4559 | return -28; |
| 4560 | } |
| 4561 | if(CheckBigString(Item.val.string)) { |
| 4562 | return -3; |
| 4563 | } |
| 4564 | if(QCBORDecode_Finish(&DC)) { |
| 4565 | return -29; |
| 4566 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4567 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4568 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4569 | 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] | 4570 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4571 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4572 | return -30; |
| 4573 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4574 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4575 | QCBORDecode_GetNext(&DC, &Item); |
| 4576 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4577 | return -31; |
| 4578 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4579 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4580 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 4581 | return -32; |
| 4582 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4583 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4584 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4585 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 4586 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 4587 | return -33; |
| 4588 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4589 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4590 | if(QCBORDecode_Finish(&DC)) { |
| 4591 | return -34; |
| 4592 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4593 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4594 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4598 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4599 | { |
| 4600 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4601 | QCBORError nCBORError; |
| 4602 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4603 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4604 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4605 | QCBORDecode_Init(&DC, |
| 4606 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4607 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4608 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4609 | 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] | 4610 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4611 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 4612 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4613 | return -1; |
| 4614 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4615 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4616 | if(CheckCSRMaps(&DC)) { |
| 4617 | return -2; |
| 4618 | } |
| 4619 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 4620 | // Next parse, save pointers to a few strings, destroy original and |
| 4621 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4622 | 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] | 4623 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4624 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4625 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 4626 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4627 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4628 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4629 | QCBORItem Item1, Item2, Item3, Item4; |
| 4630 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4631 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4632 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 4633 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4634 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4635 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4636 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4637 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4638 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4639 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4640 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4641 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4642 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4643 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 4644 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4645 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4646 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4647 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 4648 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4649 | Item1.uDataAlloc != 0 || |
| 4650 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4651 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4652 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4653 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4654 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4655 | |
| 4656 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4657 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4658 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4659 | Item2.uDataAlloc != 0 || |
| 4660 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4661 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4662 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4663 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4664 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4665 | Item3.uDataAlloc == 0 || |
| 4666 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4667 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4668 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4669 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4670 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4671 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4672 | Item4.uDataAlloc == 0 || |
| 4673 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4674 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4675 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4676 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4677 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4678 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4679 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4680 | QCBORDecode_Init(&DC, |
| 4681 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 4682 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4683 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 4684 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4685 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4686 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4687 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4688 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4689 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4690 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 4691 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 4692 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 4693 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 4694 | } |
| 4695 | } |
| 4696 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4697 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4698 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | return 0; |
| 4702 | } |
| 4703 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4704 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4705 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 4706 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4707 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4708 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4709 | QCBORDecodeContext DC; |
| 4710 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 4711 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4712 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4713 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4714 | // Then fish into the internals of the decode context |
| 4715 | // to get the allocator function so it can be called directly. |
| 4716 | // Also figure out how much pool is available for use |
| 4717 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4718 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4719 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 4720 | if(nError) { |
| 4721 | return -9; |
| 4722 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4723 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 4724 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4725 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4726 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4727 | // First test -- ask for one more byte than available and see failure |
| 4728 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4729 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 4730 | return -1; |
| 4731 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4732 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4733 | // Re do the set up for the next test that will do a successful alloc, |
| 4734 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4735 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4736 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4737 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4738 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4739 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4740 | // Allocate one byte less than available and see success |
| 4741 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4742 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4743 | return -2; |
| 4744 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4745 | // Ask for some more and see failure |
| 4746 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4747 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 4748 | return -3; |
| 4749 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4750 | // Free the first allocate, retry the second and see success |
| 4751 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 4752 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4753 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 4754 | return -4; |
| 4755 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4756 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4757 | // Re do set up for next test that involves a successful alloc, |
| 4758 | // and a successful realloc and a failed realloc |
| 4759 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4760 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4761 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4762 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4763 | // Allocate half the pool and see success |
| 4764 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4765 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4766 | return -5; |
| 4767 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4768 | // Reallocate to take up the whole pool and see success |
| 4769 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4770 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 4771 | return -6; |
| 4772 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4773 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4774 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 4775 | return -7; |
| 4776 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4777 | // Try to allocate more to be sure there is failure after a realloc |
| 4778 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 4779 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4780 | return -8; |
| 4781 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4782 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4783 | return 0; |
| 4784 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4785 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4786 | |
| 4787 | /* Just enough of an allocator to test configuration of one */ |
| 4788 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 4789 | { |
| 4790 | (void)pOldMem; // unused variable |
| 4791 | |
| 4792 | if(uNewSize) { |
| 4793 | // Assumes the context pointer is the buffer and |
| 4794 | // nothing too big will ever be asked for. |
| 4795 | // This is only good for this basic test! |
| 4796 | return (UsefulBuf) {pCtx, uNewSize}; |
| 4797 | } else { |
| 4798 | return NULLUsefulBuf; |
| 4799 | } |
| 4800 | } |
| 4801 | |
| 4802 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4803 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4804 | { |
| 4805 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4806 | // nothing can be done with it unless that is set up. |
| 4807 | QCBORDecodeContext DC; |
| 4808 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 4809 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 4810 | |
| 4811 | uint8_t pAllocatorBuffer[50]; |
| 4812 | |
| 4813 | // This is really just to test that this call works. |
| 4814 | // The full functionality of string allocators is tested |
| 4815 | // elsewhere with the MemPool internal allocator. |
| 4816 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 4817 | |
| 4818 | QCBORItem Item; |
| 4819 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 4820 | return -1; |
| 4821 | } |
| 4822 | |
| 4823 | if(Item.uDataAlloc == 0 || |
| 4824 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4825 | Item.val.string.ptr != pAllocatorBuffer) { |
| 4826 | return -2; |
| 4827 | } |
| 4828 | |
| 4829 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 4830 | return -3; |
| 4831 | } |
| 4832 | |
| 4833 | return 0; |
| 4834 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4835 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4836 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4837 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4838 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4839 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4840 | struct EaMTest { |
| 4841 | const char *szName; |
| 4842 | UsefulBufC Input; |
| 4843 | uint8_t uTagRequirement; |
| 4844 | bool bHasTags; |
| 4845 | |
| 4846 | /* Expected values for GetNext */ |
| 4847 | QCBORError uExpectedErrorGN; |
| 4848 | uint8_t uQCBORTypeGN; |
| 4849 | int64_t nExponentGN; |
| 4850 | int64_t nMantissaGN; |
| 4851 | UsefulBufC MantissaGN; |
| 4852 | |
| 4853 | /* Expected values for GetDecimalFraction */ |
| 4854 | QCBORError uExpectedErrorGDF; |
| 4855 | int64_t nExponentGDF; |
| 4856 | int64_t nMantissaGDF; |
| 4857 | |
| 4858 | /* Expected values for GetDecimalFractionBig */ |
| 4859 | QCBORError uExpectedErrorGDFB; |
| 4860 | int64_t nExponentGDFB; |
| 4861 | UsefulBufC MantissaGDFB; |
| 4862 | bool IsNegativeGDFB; |
| 4863 | |
| 4864 | /* Expected values for GetBigFloat */ |
| 4865 | QCBORError uExpectedErrorGBF; |
| 4866 | int64_t nExponentGBF; |
| 4867 | int64_t nMantissaGBF; |
| 4868 | |
| 4869 | /* Expected values for GetBigFloatBig */ |
| 4870 | QCBORError uExpectedErrorGBFB; |
| 4871 | int64_t nExponentGBFB; |
| 4872 | UsefulBufC MantissaGBFB; |
| 4873 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4874 | }; |
| 4875 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4876 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4877 | |
| 4878 | static const struct EaMTest pEaMTests[] = { |
| 4879 | { |
| 4880 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 4881 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 4882 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 4883 | false, |
| 4884 | |
| 4885 | QCBOR_SUCCESS, /* for GetNext */ |
| 4886 | QCBOR_TYPE_ARRAY, |
| 4887 | 0, |
| 4888 | 0, |
| 4889 | {(const uint8_t []){0x00}, 1}, |
| 4890 | |
| 4891 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 4892 | -1, |
| 4893 | 3, |
| 4894 | |
| 4895 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4896 | -1, |
| 4897 | {(const uint8_t []){0x03}, 1}, |
| 4898 | false, |
| 4899 | |
| 4900 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4901 | -1, |
| 4902 | 3, |
| 4903 | |
| 4904 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4905 | -1, |
| 4906 | {(const uint8_t []){0x03}, 1}, |
| 4907 | false |
| 4908 | }, |
| 4909 | |
| 4910 | { |
| 4911 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 4912 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 4913 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4914 | false, |
| 4915 | |
| 4916 | QCBOR_SUCCESS, /* for GetNext */ |
| 4917 | QCBOR_TYPE_ARRAY, |
| 4918 | 0, |
| 4919 | 0, |
| 4920 | {(const uint8_t []){0x00}, 1}, |
| 4921 | |
| 4922 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4923 | 0, |
| 4924 | 0, |
| 4925 | |
| 4926 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4927 | 0, |
| 4928 | {(const uint8_t []){0x00}, 1}, |
| 4929 | false, |
| 4930 | |
| 4931 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4932 | 0, |
| 4933 | 0, |
| 4934 | |
| 4935 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4936 | 0, |
| 4937 | {(const uint8_t []){0x00}, 1}, |
| 4938 | false |
| 4939 | |
| 4940 | }, |
| 4941 | |
| 4942 | { |
| 4943 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 4944 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 4945 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4946 | true, |
| 4947 | |
| 4948 | QCBOR_SUCCESS, /* for GetNext */ |
| 4949 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 4950 | -1, |
| 4951 | 3, |
| 4952 | {(const uint8_t []){0x00}, 1}, |
| 4953 | |
| 4954 | |
| 4955 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 4956 | -1, |
| 4957 | 3, |
| 4958 | |
| 4959 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4960 | -1, |
| 4961 | {(const uint8_t []){0x03}, 1}, |
| 4962 | false, |
| 4963 | |
| 4964 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4965 | 0, |
| 4966 | 0, |
| 4967 | |
| 4968 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4969 | 0, |
| 4970 | {(const uint8_t []){0x00}, 1}, |
| 4971 | false |
| 4972 | }, |
| 4973 | { |
| 4974 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 4975 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 4976 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4977 | true, |
| 4978 | |
| 4979 | QCBOR_SUCCESS, /* for GetNext */ |
| 4980 | QCBOR_TYPE_BIGFLOAT, |
| 4981 | 300, |
| 4982 | 100, |
| 4983 | {(const uint8_t []){0x00}, 1}, |
| 4984 | |
| 4985 | |
| 4986 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4987 | 0, |
| 4988 | 0, |
| 4989 | |
| 4990 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4991 | 0, |
| 4992 | {(const uint8_t []){0x03}, 1}, |
| 4993 | false, |
| 4994 | |
| 4995 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4996 | 300, |
| 4997 | 100, |
| 4998 | |
| 4999 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5000 | 300, |
| 5001 | {(const uint8_t []){0x64}, 1}, |
| 5002 | false |
| 5003 | }, |
| 5004 | |
| 5005 | { |
| 5006 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 5007 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5008 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 5009 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5010 | true, |
| 5011 | |
| 5012 | QCBOR_SUCCESS, /* for GetNext */ |
| 5013 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5014 | -20, |
| 5015 | 0, |
| 5016 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5017 | |
| 5018 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 5019 | 0, |
| 5020 | 0, |
| 5021 | |
| 5022 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5023 | -20, |
| 5024 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5025 | false, |
| 5026 | |
| 5027 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5028 | 0, |
| 5029 | 0, |
| 5030 | |
| 5031 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5032 | 0, |
| 5033 | {(const uint8_t []){0x00}, 0}, |
| 5034 | false |
| 5035 | }, |
| 5036 | |
| 5037 | { |
| 5038 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 5039 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 5040 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 5041 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5042 | true, |
| 5043 | |
| 5044 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 5045 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5046 | 0, |
| 5047 | 0, |
| 5048 | {(const uint8_t []){0x00}, 0}, |
| 5049 | |
| 5050 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 5051 | 0, |
| 5052 | 0, |
| 5053 | |
| 5054 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 5055 | 0, |
| 5056 | {(const uint8_t []){0x00}, 0}, |
| 5057 | false, |
| 5058 | |
| 5059 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 5060 | 0, |
| 5061 | 0, |
| 5062 | |
| 5063 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 5064 | 0, |
| 5065 | {(const uint8_t []){0x00}, 0}, |
| 5066 | false |
| 5067 | }, |
| 5068 | { |
| 5069 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 5070 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 5071 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5072 | QCBOR_TAG_REQUIREMENT_TAG, |
| 5073 | true, |
| 5074 | |
| 5075 | QCBOR_SUCCESS, /* for GetNext */ |
| 5076 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5077 | -20, |
| 5078 | 0, |
| 5079 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5080 | |
| 5081 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 5082 | 0, |
| 5083 | 0, |
| 5084 | |
| 5085 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 5086 | -20, |
| 5087 | {(const uint8_t []){0x00}, 1}, |
| 5088 | false, |
| 5089 | |
| 5090 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 5091 | -20, |
| 5092 | 4294967295, |
| 5093 | |
| 5094 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5095 | -20, |
| 5096 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 5097 | false |
| 5098 | }, |
| 5099 | |
| 5100 | { |
| 5101 | /* Special case for test 8. Don't renumber it. */ |
| 5102 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 5103 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 5104 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5105 | true, |
| 5106 | |
| 5107 | QCBOR_SUCCESS, /* for GetNext */ |
| 5108 | QCBOR_TYPE_ARRAY, |
| 5109 | 0, |
| 5110 | 0, |
| 5111 | {(const uint8_t []){0x00}, 1}, |
| 5112 | |
| 5113 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5114 | 0, |
| 5115 | 0, |
| 5116 | |
| 5117 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5118 | -20, |
| 5119 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5120 | false, |
| 5121 | |
| 5122 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 5123 | 0, |
| 5124 | 0, |
| 5125 | |
| 5126 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 5127 | -20, |
| 5128 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5129 | false |
| 5130 | }, |
| 5131 | |
| 5132 | { |
| 5133 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 5134 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5135 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 5136 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 5137 | true, |
| 5138 | |
| 5139 | QCBOR_SUCCESS, /* for GetNext */ |
| 5140 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 5141 | 9223372036854775807, |
| 5142 | 0, |
| 5143 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5144 | |
| 5145 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 5146 | 0, |
| 5147 | 0, |
| 5148 | |
| 5149 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 5150 | 9223372036854775807, |
| 5151 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 5152 | true, |
| 5153 | |
| 5154 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 5155 | 0, |
| 5156 | 0, |
| 5157 | |
| 5158 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 5159 | 0, |
| 5160 | {(const uint8_t []){0x00}, 1}, |
| 5161 | false |
| 5162 | }, |
| 5163 | }; |
| 5164 | |
| 5165 | |
| 5166 | |
| 5167 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5168 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5169 | size_t uIndex; |
| 5170 | QCBORDecodeContext DCtx; |
| 5171 | QCBORItem Item; |
| 5172 | QCBORError uError; |
| 5173 | int64_t nMantissa, nExponent; |
| 5174 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 5175 | UsefulBufC Mantissa; |
| 5176 | bool bMantissaIsNegative; |
| 5177 | |
| 5178 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 5179 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 5180 | /* Decode with GetNext */ |
| 5181 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5182 | |
| 5183 | if(uIndex + 1 == 9) { |
| 5184 | nExponent = 99; // just to set a break point |
| 5185 | } |
| 5186 | |
| 5187 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 5188 | #ifdef QCBOR_DISABLE_TAGS |
| 5189 | /* Test 8 is a special case when tags are disabled */ |
| 5190 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 5191 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5192 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 5193 | } |
| 5194 | } else { |
| 5195 | #endif |
| 5196 | /* Now check return code, data type, mantissa and exponent */ |
| 5197 | if(pT->uExpectedErrorGN != uError) { |
| 5198 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 5199 | } |
| 5200 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 5201 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 5202 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 5203 | } |
| 5204 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 5205 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 5206 | } |
| 5207 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 5208 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 5209 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 5210 | } |
| 5211 | } else { |
| 5212 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 5213 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 5214 | } |
| 5215 | } |
| 5216 | } |
| 5217 | #ifdef QCBOR_DISABLE_TAGS |
| 5218 | } |
| 5219 | #endif |
| 5220 | |
| 5221 | /* Decode with GetDecimalFraction */ |
| 5222 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5223 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 5224 | pT->uTagRequirement, |
| 5225 | &nMantissa, |
| 5226 | &nExponent); |
| 5227 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5228 | #ifdef QCBOR_DISABLE_TAGS |
| 5229 | if(pT->bHasTags) { |
| 5230 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5231 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 5232 | } |
| 5233 | } else { |
| 5234 | #endif |
| 5235 | /* Now check return code, mantissa and exponent */ |
| 5236 | if(pT->uExpectedErrorGDF != uError) { |
| 5237 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 5238 | } |
| 5239 | if(uError == QCBOR_SUCCESS) { |
| 5240 | if(pT->nExponentGDF != nExponent) { |
| 5241 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 5242 | } |
| 5243 | if(pT->nMantissaGDF != nMantissa) { |
| 5244 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 5245 | } |
| 5246 | } |
| 5247 | #ifdef QCBOR_DISABLE_TAGS |
| 5248 | } |
| 5249 | #endif |
| 5250 | |
| 5251 | /* Decode with GetDecimalFractionBig */ |
| 5252 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5253 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 5254 | pT->uTagRequirement, |
| 5255 | MantissaBuf, |
| 5256 | &Mantissa, |
| 5257 | &bMantissaIsNegative, |
| 5258 | &nExponent); |
| 5259 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5260 | #ifdef QCBOR_DISABLE_TAGS |
| 5261 | if(pT->bHasTags) { |
| 5262 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5263 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 5264 | } |
| 5265 | } else { |
| 5266 | #endif |
| 5267 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5268 | if(pT->uExpectedErrorGDFB != uError) { |
| 5269 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 5270 | } |
| 5271 | if(uError == QCBOR_SUCCESS) { |
| 5272 | if(pT->nExponentGDFB != nExponent) { |
| 5273 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 5274 | } |
| 5275 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 5276 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 5277 | } |
| 5278 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 5279 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 5280 | } |
| 5281 | } |
| 5282 | #ifdef QCBOR_DISABLE_TAGS |
| 5283 | } |
| 5284 | #endif |
| 5285 | |
| 5286 | /* Decode with GetBigFloat */ |
| 5287 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5288 | QCBORDecode_GetBigFloat(&DCtx, |
| 5289 | pT->uTagRequirement, |
| 5290 | &nMantissa, |
| 5291 | &nExponent); |
| 5292 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5293 | #ifdef QCBOR_DISABLE_TAGS |
| 5294 | if(pT->bHasTags) { |
| 5295 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5296 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 5297 | } |
| 5298 | } else { |
| 5299 | #endif |
| 5300 | /* Now check return code, mantissa and exponent */ |
| 5301 | if(pT->uExpectedErrorGBF != uError) { |
| 5302 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 5303 | } |
| 5304 | if(uError == QCBOR_SUCCESS) { |
| 5305 | if(pT->nExponentGBF != nExponent) { |
| 5306 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 5307 | } |
| 5308 | if(pT->nMantissaGBF != nMantissa) { |
| 5309 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 5310 | } |
| 5311 | } |
| 5312 | #ifdef QCBOR_DISABLE_TAGS |
| 5313 | } |
| 5314 | #endif |
| 5315 | |
| 5316 | /* Decode with GetBigFloatBig */ |
| 5317 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5318 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 5319 | pT->uTagRequirement, |
| 5320 | MantissaBuf, |
| 5321 | &Mantissa, |
| 5322 | &bMantissaIsNegative, |
| 5323 | &nExponent); |
| 5324 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5325 | #ifdef QCBOR_DISABLE_TAGS |
| 5326 | if(pT->bHasTags) { |
| 5327 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5328 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 5329 | } |
| 5330 | } else { |
| 5331 | #endif |
| 5332 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5333 | if(pT->uExpectedErrorGBFB != uError) { |
| 5334 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 5335 | } |
| 5336 | if(uError == QCBOR_SUCCESS) { |
| 5337 | if(pT->nExponentGBFB != nExponent) { |
| 5338 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 5339 | } |
| 5340 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 5341 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 5342 | } |
| 5343 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 5344 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 5345 | } |
| 5346 | } |
| 5347 | #ifdef QCBOR_DISABLE_TAGS |
| 5348 | } |
| 5349 | #endif |
| 5350 | } |
| 5351 | |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
| 5355 | |
| 5356 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 5357 | { |
| 5358 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5359 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5360 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5361 | QCBORItem item; |
| 5362 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 5363 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 5364 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 5365 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5366 | |
| 5367 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5368 | |
| 5369 | /* Now encode some stuff and then decode it */ |
| 5370 | uint8_t pBuf[40]; |
| 5371 | QCBOREncodeContext EC; |
| 5372 | UsefulBufC Encoded; |
| 5373 | |
| 5374 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 5375 | QCBOREncode_OpenArray(&EC); |
| 5376 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 5377 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 5378 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 5379 | QCBOREncode_CloseArray(&EC); |
| 5380 | QCBOREncode_Finish(&EC, &Encoded); |
| 5381 | |
| 5382 | |
| 5383 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5384 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5385 | if(uErr != QCBOR_SUCCESS) { |
| 5386 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5389 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5390 | if(uErr != QCBOR_SUCCESS) { |
| 5391 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 5395 | item.val.expAndMantissa.nExponent != 1000 || |
| 5396 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5397 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5398 | } |
| 5399 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5400 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5401 | if(uErr != QCBOR_SUCCESS) { |
| 5402 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 5406 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 5407 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5408 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5409 | } |
| 5410 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5411 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5412 | if(uErr != QCBOR_SUCCESS) { |
| 5413 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 5417 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 5418 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5419 | return 106; |
| 5420 | } |
| 5421 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5422 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5423 | |
| 5424 | return 0; |
| 5425 | } |
| 5426 | |
| 5427 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5428 | int32_t ExponentAndMantissaDecodeTests(void) |
| 5429 | { |
| 5430 | int32_t rv = ProcessEaMTests(); |
| 5431 | if(rv) { |
| 5432 | return rv; |
| 5433 | } |
| 5434 | |
| 5435 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 5436 | } |
| 5437 | |
| 5438 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 5439 | static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = { |
| 5440 | { "Exponent > INT64_MAX", |
| 5441 | QCBOR_DECODE_MODE_NORMAL, |
| 5442 | {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20}, |
| 5443 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 5444 | }, |
| 5445 | { "Mantissa > INT64_MAX", |
| 5446 | QCBOR_DECODE_MODE_NORMAL, |
| 5447 | {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23}, |
| 5448 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 5449 | }, |
| 5450 | { |
| 5451 | "End of input", |
| 5452 | QCBOR_DECODE_MODE_NORMAL, |
| 5453 | {"\xC4\x82", 2}, |
| 5454 | QCBOR_ERR_NO_MORE_ITEMS |
| 5455 | }, |
| 5456 | {"bad content for big num", |
| 5457 | QCBOR_DECODE_MODE_NORMAL, |
| 5458 | {"\xC4\x82\x01\xc3\x01", 5}, |
| 5459 | QCBOR_ERR_BAD_OPT_TAG |
| 5460 | }, |
| 5461 | {"bad content for big num", |
| 5462 | QCBOR_DECODE_MODE_NORMAL, |
| 5463 | {"\xC4\x82\xc2\x01\x1f", 5}, |
| 5464 | QCBOR_ERR_BAD_INT |
| 5465 | }, |
| 5466 | {"Bad integer for exponent", |
| 5467 | QCBOR_DECODE_MODE_NORMAL, |
| 5468 | {"\xC4\x82\x01\x1f", 4}, |
| 5469 | QCBOR_ERR_BAD_INT |
| 5470 | }, |
| 5471 | {"Bad integer for mantissa", |
| 5472 | QCBOR_DECODE_MODE_NORMAL, |
| 5473 | {"\xC4\x82\x1f\x01", 4}, |
| 5474 | QCBOR_ERR_BAD_INT |
| 5475 | }, |
| 5476 | {"3 items in array", |
| 5477 | QCBOR_DECODE_MODE_NORMAL, |
| 5478 | {"\xC4\x83\x03\x01\x02", 5}, |
| 5479 | QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5480 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 5481 | {"unterminated indefinite length array", |
| 5482 | QCBOR_DECODE_MODE_NORMAL, |
| 5483 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 5484 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 5485 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5486 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 5487 | {"unterminated indefinite length array", |
| 5488 | QCBOR_DECODE_MODE_NORMAL, |
| 5489 | {"\xC4\x9f\x03\x01\x02", 5}, |
| 5490 | QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 5491 | }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5492 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 5493 | {"Empty array", |
| 5494 | QCBOR_DECODE_MODE_NORMAL, |
| 5495 | {"\xC4\x80", 2}, |
| 5496 | QCBOR_ERR_NO_MORE_ITEMS |
| 5497 | }, |
| 5498 | {"Second is not an integer", |
| 5499 | QCBOR_DECODE_MODE_NORMAL, |
| 5500 | {"\xC4\x82\x03\x40", 4}, |
| 5501 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 5502 | }, |
| 5503 | {"First is not an integer", |
| 5504 | QCBOR_DECODE_MODE_NORMAL, |
| 5505 | {"\xC4\x82\x40", 3}, |
| 5506 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 5507 | }, |
| 5508 | {"Not an array", |
| 5509 | QCBOR_DECODE_MODE_NORMAL, |
| 5510 | {"\xC4\xA2", 2}, |
| 5511 | QCBOR_ERR_BAD_EXP_AND_MANTISSA |
| 5512 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5513 | }; |
| 5514 | |
| 5515 | |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 5516 | int32_t |
| 5517 | ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5518 | { |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 5519 | return ProcessDecodeFailures(ExponentAndMantissaFailures, |
| 5520 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 5521 | struct DecodeFailTestInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5522 | } |
| 5523 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5524 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5525 | |
| 5526 | |
| 5527 | |
| 5528 | /* |
| 5529 | Some basic CBOR with map and array used in a lot of tests. |
| 5530 | The map labels are all strings |
| 5531 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5532 | { |
| 5533 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5534 | "an array of two strings": [ |
| 5535 | "string1", "string2" |
| 5536 | ], |
| 5537 | "map in a map": { |
| 5538 | "bytes 1": h'78787878', |
| 5539 | "bytes 2": h'79797979', |
| 5540 | "another int": 98, |
| 5541 | "text 2": "lies, damn lies and statistics" |
| 5542 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5543 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5544 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5545 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5546 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5547 | { |
| 5548 | QCBORItem Item1, Item2, Item3; |
| 5549 | int64_t nDecodedInt1, nDecodedInt2; |
| 5550 | UsefulBufC B1, B2, S1, S2, S3; |
| 5551 | |
| 5552 | QCBORDecodeContext DCtx; |
| 5553 | QCBORError nCBORError; |
| 5554 | |
| 5555 | QCBORDecode_Init(&DCtx, input, 0); |
| 5556 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5557 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5558 | |
| 5559 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 5560 | |
| 5561 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5562 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5563 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 5564 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 5565 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5566 | QCBORDecode_ExitMap(&DCtx); |
| 5567 | |
| 5568 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5569 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5570 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 5571 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5572 | return -400; |
| 5573 | } |
| 5574 | QCBORDecode_ExitArray(&DCtx); |
| 5575 | |
| 5576 | // Parse the same array again using GetText() instead of GetItem() |
| 5577 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5578 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 5579 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5580 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 5581 | return 5000; |
| 5582 | } |
| 5583 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 5584 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5585 | return 5001; |
| 5586 | } */ |
| 5587 | |
| 5588 | QCBORDecode_ExitArray(&DCtx); |
| 5589 | |
| 5590 | QCBORDecode_ExitMap(&DCtx); |
| 5591 | |
| 5592 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 5593 | |
| 5594 | if(nCBORError) { |
| 5595 | return (int32_t)nCBORError; |
| 5596 | } |
| 5597 | |
| 5598 | if(nDecodedInt1 != 42) { |
| 5599 | return 1001; |
| 5600 | } |
| 5601 | |
| 5602 | if(nDecodedInt2 != 98) { |
| 5603 | return 1002; |
| 5604 | } |
| 5605 | |
| 5606 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5607 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5608 | return 1003; |
| 5609 | } |
| 5610 | |
| 5611 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5612 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5613 | return 1004; |
| 5614 | } |
| 5615 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5616 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5617 | return 1005; |
| 5618 | } |
| 5619 | |
| 5620 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 5621 | return 1006; |
| 5622 | } |
| 5623 | |
| 5624 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 5625 | return 1007; |
| 5626 | } |
| 5627 | |
| 5628 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 5629 | return 1008; |
| 5630 | } |
| 5631 | |
| 5632 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 5633 | return 1009; |
| 5634 | } |
| 5635 | |
| 5636 | return 0; |
| 5637 | } |
| 5638 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5639 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5640 | { |
| 5641 | -75008: h'05083399', |
| 5642 | 88: [], |
| 5643 | 100100: { |
| 5644 | "sub1": { |
| 5645 | 10: [ |
| 5646 | 0 |
| 5647 | ], |
| 5648 | -75009: h'A46823990001', |
| 5649 | 100100: { |
| 5650 | "json": "{ \"ueid\", \"xyz\"}", |
| 5651 | "subsub": { |
| 5652 | 100002: h'141813191001' |
| 5653 | } |
| 5654 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5655 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5656 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5657 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5658 | */ |
| 5659 | |
| 5660 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5661 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 5662 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 5663 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 5664 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 5665 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 5666 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 5667 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 5668 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 5669 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 5670 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 5671 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 5672 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5673 | }; |
| 5674 | |
| 5675 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 5676 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 5677 | { |
| 5678 | UsefulBufC String; |
| 5679 | |
| 5680 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 5681 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 5682 | |
| 5683 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5684 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 5685 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5686 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 5687 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 5688 | if(QCBORDecode_GetError(pDCtx)) { |
| 5689 | return 4001; |
| 5690 | } |
| 5691 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 5692 | return 4002; |
| 5693 | } |
| 5694 | QCBORDecode_ExitMap(pDCtx); |
| 5695 | QCBORDecode_ExitMap(pDCtx); |
| 5696 | QCBORDecode_ExitMap(pDCtx); |
| 5697 | QCBORDecode_ExitMap(pDCtx); |
| 5698 | |
| 5699 | return 0; |
| 5700 | } |
| 5701 | |
| 5702 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 5703 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 5704 | { |
| 5705 | QCBORItem Item; |
| 5706 | QCBORError uErr; |
| 5707 | |
| 5708 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 5709 | for(int x = 0; x < 20; x++) { |
| 5710 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5711 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5712 | return 4100; |
| 5713 | |
| 5714 | } |
| 5715 | } |
| 5716 | QCBORDecode_ExitArray(pDCtx); |
| 5717 | if(QCBORDecode_GetError(pDCtx)) { |
| 5718 | return 4101; |
| 5719 | } |
| 5720 | |
| 5721 | return 0; |
| 5722 | } |
| 5723 | |
| 5724 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 5725 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 5726 | { |
| 5727 | QCBORError uErr; |
| 5728 | |
| 5729 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5730 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 5731 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5732 | int64_t nInt = 99; |
| 5733 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 5734 | if(nInt != 0) { |
| 5735 | return 4200; |
| 5736 | } |
| 5737 | for(int x = 0; x < 20; x++) { |
| 5738 | QCBORItem Item; |
| 5739 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5740 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5741 | return 4201; |
| 5742 | |
| 5743 | } |
| 5744 | } |
| 5745 | QCBORDecode_ExitArray(pDCtx); |
| 5746 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 5747 | return 4202; |
| 5748 | } |
| 5749 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5750 | UsefulBufC dD; |
| 5751 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5752 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5753 | return 4203; |
| 5754 | } |
| 5755 | for(int x = 0; x < 20; x++) { |
| 5756 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5757 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 5758 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5759 | return 4204; |
| 5760 | } |
| 5761 | } |
| 5762 | QCBORDecode_ExitArray(pDCtx); |
| 5763 | QCBORDecode_ExitMap(pDCtx); |
| 5764 | QCBORDecode_ExitMap(pDCtx); |
| 5765 | |
| 5766 | return 0; |
| 5767 | } |
| 5768 | |
| 5769 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 5770 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 5771 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5772 | { |
| 5773 | QCBORDecodeContext DCtx; |
| 5774 | int32_t nReturn; |
| 5775 | QCBORError uErr; |
| 5776 | |
| 5777 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 5778 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5779 | |
| 5780 | for(int j = 0; j < 5; j++) { |
| 5781 | for(int i = 0; i < 20; i++) { |
| 5782 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 5783 | if(nReturn) { |
| 5784 | return nReturn; |
| 5785 | } |
| 5786 | } |
| 5787 | |
| 5788 | for(int i = 0; i < 20; i++) { |
| 5789 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 5790 | if(nReturn ) { |
| 5791 | return nReturn; |
| 5792 | } |
| 5793 | } |
| 5794 | |
| 5795 | for(int i = 0; i < 20; i++) { |
| 5796 | nReturn = DecodeNestedGetZero(&DCtx); |
| 5797 | if(nReturn ) { |
| 5798 | return nReturn; |
| 5799 | } |
| 5800 | } |
| 5801 | } |
| 5802 | |
| 5803 | QCBORDecode_ExitMap(&DCtx); |
| 5804 | uErr = QCBORDecode_Finish(&DCtx); |
| 5805 | if(uErr) { |
| 5806 | return (int32_t)uErr + 4100; |
| 5807 | } |
| 5808 | |
| 5809 | return 0; |
| 5810 | } |
| 5811 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5812 | |
| 5813 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5814 | [ |
| 5815 | 23, |
| 5816 | 6000, |
| 5817 | h'67616C6163746963', |
| 5818 | h'686176656E20746F6B656E' |
| 5819 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5820 | */ |
| 5821 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5822 | 0x84, |
| 5823 | 0x17, |
| 5824 | 0x19, 0x17, 0x70, |
| 5825 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 5826 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5827 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5828 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5829 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 5830 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5831 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5832 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 5833 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5834 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5835 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5836 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5837 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5838 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5839 | /* |
| 5840 | { |
| 5841 | 0: [], |
| 5842 | 9: [ |
| 5843 | [], |
| 5844 | [] |
| 5845 | ], |
| 5846 | 8: { |
| 5847 | 1: [], |
| 5848 | 2: {}, |
| 5849 | 3: [] |
| 5850 | }, |
| 5851 | 4: {}, |
| 5852 | 5: [], |
| 5853 | 6: [ |
| 5854 | [], |
| 5855 | [] |
| 5856 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5857 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5858 | */ |
| 5859 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5860 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 5861 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 5862 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 5863 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5864 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5865 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5866 | |
| 5867 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5868 | /* |
| 5869 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5870 | Duplicate label |
| 5871 | Integer overflow |
| 5872 | Date overflow |
| 5873 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5874 | { |
| 5875 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5876 | 3: -18446744073709551616, |
| 5877 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5878 | 5: 0, |
| 5879 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5880 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5881 | */ |
| 5882 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5883 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 5884 | 0xa5, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5885 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5886 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 5887 | #else |
| 5888 | 0xa4, |
| 5889 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5890 | 0x05, 0x00, |
| 5891 | 0x05, 0x00, |
| 5892 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5893 | }; |
| 5894 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5895 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5896 | static const uint8_t spUnRecoverableMapError1[] = { |
| 5897 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5898 | }; |
| 5899 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5900 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5901 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5902 | static const uint8_t spUnRecoverableMapError2[] = { |
| 5903 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5904 | }; |
| 5905 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5906 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5907 | static const uint8_t spUnRecoverableMapError3[] = { |
| 5908 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 5909 | }; |
| 5910 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5911 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5912 | static const uint8_t spUnRecoverableMapError4[] = { |
| 5913 | 0xbf, |
| 5914 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5915 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5916 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5917 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5918 | 0xff |
| 5919 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5920 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5921 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 5922 | const unsigned char not_well_formed_submod_section[] = { |
| 5923 | 0xa1, 0x14, 0x1f, |
| 5924 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5925 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5926 | |
| 5927 | /* Array of length 3, but only two items. */ |
| 5928 | const unsigned char spBadConsumeInput[] = { |
| 5929 | 0x83, 0x00, 0x00 |
| 5930 | }; |
| 5931 | |
| 5932 | /* Tag nesting too deep. */ |
| 5933 | const unsigned char spBadConsumeInput2[] = { |
| 5934 | 0x81, |
| 5935 | 0xD8, 0x37, |
| 5936 | 0xD8, 0x2C, |
| 5937 | 0xD8, 0x21, |
| 5938 | 0xD6, |
| 5939 | 0xCB, |
| 5940 | 00 |
| 5941 | }; |
| 5942 | |
| 5943 | const unsigned char spBadConsumeInput3[] = { |
| 5944 | 0x81, 0xc0, 0x81, 0x00 |
| 5945 | }; |
| 5946 | |
| 5947 | const unsigned char spBadConsumeInput4[] = { |
| 5948 | 0x81, 0x9f, 0x00, 0xff |
| 5949 | }; |
| 5950 | |
| 5951 | const unsigned char spBadConsumeInput5[] = { |
| 5952 | 0xa1, 0x80, 0x00 |
| 5953 | }; |
| 5954 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 5955 | /* |
| 5956 | Lots of nesting for various nesting tests. |
| 5957 | { 1:1, |
| 5958 | 2:{ |
| 5959 | 21:21, |
| 5960 | 22:{ |
| 5961 | 221:[2111, 2112, 2113], |
| 5962 | 222:222, |
| 5963 | 223: {} |
| 5964 | }, |
| 5965 | 23: 23 |
| 5966 | }, |
| 5967 | 3:3, |
| 5968 | 4: [ {} ] |
| 5969 | } |
| 5970 | */ |
| 5971 | static const uint8_t spNested[] = { |
| 5972 | 0xA4, /* Map of 4 */ |
| 5973 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 5974 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 5975 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 5976 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 5977 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 5978 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 5979 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 5980 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 5981 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 5982 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 5983 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 5984 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 5985 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 5986 | 0xA0 /* Array entry {}, an empty map */ |
| 5987 | }; |
| 5988 | |
| 5989 | |
| 5990 | static int32_t EnterMapCursorTest(void) |
| 5991 | { |
| 5992 | QCBORDecodeContext DCtx; |
| 5993 | QCBORItem Item1; |
| 5994 | |
| 5995 | int i; |
| 5996 | for(i = 0; i < 13; i++) { |
| 5997 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 5998 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5999 | int j; |
| 6000 | /* Move travesal cursor */ |
| 6001 | for(j = 0; j < i; j++) { |
| 6002 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6003 | } |
| 6004 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6005 | QCBORDecode_ExitMap(&DCtx); |
| 6006 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6007 | if(Item1.label.int64 != 3) { |
| 6008 | return 8000; |
| 6009 | } |
| 6010 | } |
| 6011 | |
| 6012 | for(i = 0; i < 13; i++) { |
| 6013 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6014 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6015 | int j; |
| 6016 | /* Move travesal cursor */ |
| 6017 | for(j = 0; j < i; j++) { |
| 6018 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6019 | } |
| 6020 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6021 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6022 | QCBORDecode_ExitMap(&DCtx); |
| 6023 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6024 | if(Item1.label.int64 != 23) { |
| 6025 | return 8000; |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | for(i = 0; i < 13; i++) { |
| 6030 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 6031 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6032 | int j; |
| 6033 | /* Move travesal cursor */ |
| 6034 | for(j = 0; j < i; j++) { |
| 6035 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6036 | } |
| 6037 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 6038 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 6039 | for(j = 0; j < i; j++) { |
| 6040 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6041 | } |
| 6042 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 6043 | QCBORDecode_ExitArray(&DCtx); |
| 6044 | QCBORDecode_ExitMap(&DCtx); |
| 6045 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6046 | if(Item1.label.int64 != 23) { |
| 6047 | return 8000; |
| 6048 | } |
| 6049 | QCBORDecode_ExitMap(&DCtx); |
| 6050 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6051 | if(Item1.label.int64 != 3) { |
| 6052 | return 8000; |
| 6053 | } |
| 6054 | } |
| 6055 | |
| 6056 | return 0; |
| 6057 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6058 | |
| 6059 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6060 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6061 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6062 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6063 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6064 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6065 | int32_t nReturn; |
| 6066 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6067 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6068 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6069 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6070 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6071 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 6072 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6073 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6074 | QCBORDecode_ExitArray(&DCtx); |
| 6075 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6076 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 6077 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6078 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6079 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6080 | QCBORDecode_ExitArray(&DCtx); |
| 6081 | QCBORDecode_ExitArray(&DCtx); |
| 6082 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6083 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 6084 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6085 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6086 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6087 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6088 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6089 | QCBORDecode_ExitArray(&DCtx); |
| 6090 | QCBORDecode_ExitMap(&DCtx); |
| 6091 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6092 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6093 | QCBORDecode_ExitMap(&DCtx); |
| 6094 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6095 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6096 | QCBORDecode_ExitArray(&DCtx); |
| 6097 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6098 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 6099 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6100 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6101 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6102 | QCBORDecode_ExitArray(&DCtx); |
| 6103 | QCBORDecode_ExitArray(&DCtx); |
| 6104 | |
| 6105 | QCBORDecode_ExitMap(&DCtx); |
| 6106 | |
| 6107 | uErr = QCBORDecode_Finish(&DCtx); |
| 6108 | if(uErr != QCBOR_SUCCESS){ |
| 6109 | return 3011; |
| 6110 | } |
| 6111 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6112 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6113 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 6114 | if(nReturn) { |
| 6115 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6116 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6117 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6118 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6119 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6120 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6121 | if(nReturn) { |
| 6122 | return nReturn; |
| 6123 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 6124 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 6125 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6126 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 6127 | // These tests confirm the cursor is at the right place after entering |
| 6128 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6129 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6130 | |
| 6131 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6132 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6133 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6134 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6135 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 6136 | return 2001; |
| 6137 | } |
| 6138 | |
| 6139 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6140 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 6141 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6142 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6143 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 6144 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6145 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6146 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 6147 | return 2051; |
| 6148 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6149 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6150 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 6151 | return 2002; |
| 6152 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6153 | QCBORDecode_ExitArray(&DCtx); |
| 6154 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 6155 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6156 | UsefulBuf_Compare(ArrayItem.label.string, |
| 6157 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 6158 | return 2052; |
| 6159 | } |
| 6160 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6161 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6162 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6163 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6164 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6165 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6166 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6167 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 6168 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6169 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 6170 | return 2003; |
| 6171 | } |
| 6172 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6173 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6174 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6175 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6176 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6177 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6178 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6179 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6180 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6181 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6182 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6183 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6184 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 6185 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6186 | } |
| 6187 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6188 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6189 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 6190 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 6191 | QCBORDecode_ExitArray(&DCtx); |
| 6192 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 6193 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 6194 | return 2006; |
| 6195 | } |
| 6196 | QCBORDecode_ExitMap(&DCtx); |
| 6197 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6198 | return 2007; |
| 6199 | } |
| 6200 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6201 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6202 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6203 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6204 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6205 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6206 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6207 | return 2008; |
| 6208 | } |
| 6209 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6210 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 6211 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6212 | return 2009; |
| 6213 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 6214 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6215 | |
| 6216 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6217 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6218 | // This will fail because the map is empty. |
| 6219 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6220 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6221 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6222 | return 2010; |
| 6223 | } |
| 6224 | QCBORDecode_ExitMap(&DCtx); |
| 6225 | uErr = QCBORDecode_Finish(&DCtx); |
| 6226 | if(uErr != QCBOR_SUCCESS){ |
| 6227 | return 2011; |
| 6228 | } |
| 6229 | |
| 6230 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6231 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6232 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6233 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6234 | // This will fail because the map is empty. |
| 6235 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6236 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6237 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6238 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6239 | } |
| 6240 | QCBORDecode_ExitMap(&DCtx); |
| 6241 | uErr = QCBORDecode_Finish(&DCtx); |
| 6242 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6243 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6244 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6245 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6246 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6247 | |
| 6248 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6249 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6250 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6251 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6252 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6253 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6254 | QCBORDecode_ExitArray(&DCtx); |
| 6255 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 6256 | QCBORDecode_ExitArray(&DCtx); |
| 6257 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6258 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6259 | return 2014; |
| 6260 | } |
| 6261 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6262 | int64_t nInt; |
| 6263 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6264 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6265 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6266 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6267 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6268 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 6269 | return 2021; |
| 6270 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6271 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 6272 | return 2121; |
| 6273 | } |
| 6274 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6275 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6276 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6277 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6278 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6279 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 6280 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6281 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6282 | return 2024; |
| 6283 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6284 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6285 | |
| 6286 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 6287 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6288 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 6289 | return 2025; |
| 6290 | } |
| 6291 | |
| 6292 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 6293 | |
| 6294 | QCBORDecode_ExitMap(&DCtx); |
| 6295 | uErr = QCBORDecode_Finish(&DCtx); |
| 6296 | if(uErr != QCBOR_SUCCESS) { |
| 6297 | return 2026; |
| 6298 | } |
| 6299 | |
| 6300 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6301 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6302 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6303 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6304 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 6305 | return 2030; |
| 6306 | } |
| 6307 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6308 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6309 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6310 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6311 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6312 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6313 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6314 | return 2031; |
| 6315 | } |
| 6316 | |
| 6317 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6318 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6319 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6320 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6321 | if(uErr != QCBOR_ERR_HIT_END) { |
| 6322 | return 2032; |
| 6323 | } |
| 6324 | |
| 6325 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6326 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6327 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6328 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6329 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 6330 | return 2033; |
| 6331 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6332 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6333 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 6334 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6335 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6336 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 6337 | return 2401; |
| 6338 | } |
| 6339 | if(QCBORDecode_GetError(&DCtx)) { |
| 6340 | return 2402; |
| 6341 | } |
| 6342 | |
| 6343 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6344 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6345 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 6346 | Item1.val.uCount != 3 || |
| 6347 | Item1.uNextNestLevel != 1) { |
| 6348 | return 2403; |
| 6349 | } |
| 6350 | if(QCBORDecode_GetError(&DCtx)) { |
| 6351 | return 2404; |
| 6352 | } |
| 6353 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6354 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 6355 | Item1.uNextNestLevel != 1 || |
| 6356 | Item1.val.int64 != 42) { |
| 6357 | return 2405; |
| 6358 | } |
| 6359 | if(QCBORDecode_GetError(&DCtx)) { |
| 6360 | return 2406; |
| 6361 | } |
| 6362 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6363 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 6364 | Item1.uNestingLevel != 1 || |
| 6365 | Item1.uNextNestLevel != 1 || |
| 6366 | Item1.val.uCount != 2) { |
| 6367 | return 2407; |
| 6368 | } |
| 6369 | if(QCBORDecode_GetError(&DCtx)) { |
| 6370 | return 2408; |
| 6371 | } |
| 6372 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6373 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 6374 | Item1.uNestingLevel != 1 || |
| 6375 | Item1.uNextNestLevel != 0 || |
| 6376 | Item1.val.uCount != 4) { |
| 6377 | return 2409; |
| 6378 | } |
| 6379 | if(QCBORDecode_GetError(&DCtx)) { |
| 6380 | return 2410; |
| 6381 | } |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6382 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6383 | nReturn = DecodeNestedIterate(); |
| 6384 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6385 | |
| 6386 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 6387 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6388 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 6389 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 6390 | return 2500; |
| 6391 | } |
| 6392 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6393 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 6394 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6395 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6396 | return 2600; |
| 6397 | } |
| 6398 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6399 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6400 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 6401 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6402 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6403 | return 2700; |
| 6404 | } |
| 6405 | |
| 6406 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0); |
| 6407 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6408 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 6409 | return 2800; |
| 6410 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6411 | #endif |
| 6412 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6413 | |
| 6414 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 6415 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6416 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6417 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6418 | return 2900; |
| 6419 | } |
| 6420 | #else |
| 6421 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6422 | return 2901; |
| 6423 | } |
| 6424 | #endif |
| 6425 | |
| 6426 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 6427 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6428 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 6429 | return 3000; |
| 6430 | } |
| 6431 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6432 | nReturn = EnterMapCursorTest(); |
| 6433 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6434 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6438 | struct NumberConversion { |
| 6439 | char *szDescription; |
| 6440 | UsefulBufC CBOR; |
| 6441 | int64_t nConvertedToInt64; |
| 6442 | QCBORError uErrorInt64; |
| 6443 | uint64_t uConvertToUInt64; |
| 6444 | QCBORError uErrorUint64; |
| 6445 | double dConvertToDouble; |
| 6446 | QCBORError uErrorDouble; |
| 6447 | }; |
| 6448 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6449 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 6450 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 6451 | #else |
| 6452 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 6453 | #endif |
| 6454 | |
| 6455 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6456 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6457 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6458 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6459 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 6460 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 6461 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 6462 | }, 15}, |
| 6463 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 6464 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 6465 | 0, |
| 6466 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 6467 | -4.6116860184273879E+18, |
| 6468 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 6469 | }, |
| 6470 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6471 | "too large to fit into int64_t", |
| 6472 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 6473 | 0, |
| 6474 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6475 | 0, |
| 6476 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6477 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6478 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6479 | }, |
| 6480 | { |
| 6481 | "largest negative int that fits in int64_t", |
| 6482 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 6483 | INT64_MIN, |
| 6484 | QCBOR_SUCCESS, |
| 6485 | 0, |
| 6486 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6487 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6488 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6489 | }, |
| 6490 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6491 | "negative bignum -1", |
| 6492 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 6493 | -1, |
| 6494 | QCBOR_SUCCESS, |
| 6495 | 0, |
| 6496 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6497 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6498 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6499 | }, |
| 6500 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6501 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6502 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6503 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 6504 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6505 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6506 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6507 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6508 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6509 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6510 | }, |
| 6511 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6512 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6513 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6514 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6515 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6516 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6517 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6518 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6519 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6520 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6521 | }, |
| 6522 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6523 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6524 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6525 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 6526 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6527 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6528 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6529 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6530 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6531 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6532 | }, |
| 6533 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6534 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6535 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 6536 | 0, |
| 6537 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6538 | 0, |
| 6539 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6540 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6541 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6542 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6543 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6544 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6545 | "Positive bignum 0x01020304 indefinite length string", |
| 6546 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 6547 | 0x01020304, |
| 6548 | QCBOR_SUCCESS, |
| 6549 | 0x01020304, |
| 6550 | QCBOR_SUCCESS, |
| 6551 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6552 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6553 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6554 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6555 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6556 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6557 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 6558 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 6559 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6560 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6561 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6562 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6563 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6564 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6565 | }, |
| 6566 | { |
| 6567 | "big float [9223372036854775806, 9223372036854775806]", |
| 6568 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6569 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 6570 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6571 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6572 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6573 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6574 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6575 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6576 | }, |
| 6577 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6578 | "Big float 3 * 2^^2", |
| 6579 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 6580 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6581 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6582 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6583 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6584 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6585 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6586 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6587 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6588 | "Decimal fraction 3/10", |
| 6589 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 6590 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6591 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6592 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6593 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6594 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6595 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6596 | }, |
| 6597 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6598 | "extreme pos bignum", |
| 6599 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 6600 | // 50 rows of 8 is 400 digits. |
| 6601 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6602 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6603 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6604 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6605 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6606 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6607 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6608 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6609 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6610 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6611 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6612 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6613 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6614 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6615 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6616 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6617 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6618 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6619 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6620 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6621 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6622 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6623 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6624 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6625 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6626 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6627 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6628 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6629 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6630 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6631 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6632 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6633 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6634 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6635 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6636 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6637 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6638 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6639 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6640 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6641 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6642 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6643 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6644 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6645 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6646 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6647 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6648 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6649 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 6650 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6651 | 404}, |
| 6652 | 0, |
| 6653 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6654 | 0, |
| 6655 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6656 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6657 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6658 | }, |
| 6659 | |
| 6660 | { |
| 6661 | "extreme neg bignum", |
| 6662 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 6663 | // 50 rows of 8 is 400 digits. |
| 6664 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6665 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6666 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6667 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6668 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6669 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6670 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6671 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6672 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6673 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6674 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6675 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6676 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6677 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6678 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6679 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6680 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6681 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6682 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6683 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6684 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6685 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6686 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6687 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6688 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6689 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6690 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6691 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6692 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6693 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6694 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6695 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6696 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6697 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6698 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6699 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6700 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6701 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6702 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6703 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6704 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6705 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6706 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6707 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6708 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6709 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6710 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6711 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6712 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 6713 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6714 | 404}, |
| 6715 | 0, |
| 6716 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6717 | 0, |
| 6718 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6719 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6720 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6721 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6722 | |
| 6723 | { |
| 6724 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 6725 | {(uint8_t[]){ |
| 6726 | 0xC5, 0x82, |
| 6727 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6728 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6729 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6730 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6731 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6732 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6733 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6734 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6735 | }, |
| 6736 | |
| 6737 | { |
| 6738 | "bigfloat that evaluates to -INFINITY", |
| 6739 | {(uint8_t[]){ |
| 6740 | 0xC5, 0x82, |
| 6741 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6742 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6743 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6744 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6745 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6746 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6747 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6748 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6749 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6750 | { |
| 6751 | "Positive bignum 0xffff", |
| 6752 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 6753 | 65536-1, |
| 6754 | QCBOR_SUCCESS, |
| 6755 | 0xffff, |
| 6756 | QCBOR_SUCCESS, |
| 6757 | 65535.0, |
| 6758 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6759 | }, |
| 6760 | #endif /* QCBOR_DISABLE_TAGS */ |
| 6761 | { |
| 6762 | "Positive integer 18446744073709551615", |
| 6763 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 6764 | 0, |
| 6765 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6766 | 18446744073709551615ULL, |
| 6767 | QCBOR_SUCCESS, |
| 6768 | 18446744073709551615.0, |
| 6769 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6770 | }, |
| 6771 | |
| 6772 | { |
| 6773 | "Postive integer 0", |
| 6774 | {(uint8_t[]){0x0}, 1}, |
| 6775 | 0LL, |
| 6776 | QCBOR_SUCCESS, |
| 6777 | 0ULL, |
| 6778 | QCBOR_SUCCESS, |
| 6779 | 0.0, |
| 6780 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6781 | }, |
| 6782 | { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6783 | "Negative integer -9223372036854775808", |
| 6784 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6785 | -9223372036854775807-1, // INT64_MIN |
| 6786 | QCBOR_SUCCESS, |
| 6787 | 0ULL, |
| 6788 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6789 | -9223372036854775808.0, |
| 6790 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6791 | }, |
| 6792 | { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6793 | "Negative integer -18446744073709551616", |
| 6794 | {(uint8_t[]){0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 6795 | 0ULL, |
| 6796 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6797 | 0ULL, |
| 6798 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6799 | -18446744073709551616.0, |
| 6800 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6801 | }, |
| 6802 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6803 | "Double Floating point value 100.3", |
| 6804 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 6805 | 100L, |
| 6806 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6807 | 100ULL, |
| 6808 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6809 | 100.3, |
| 6810 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 6811 | }, |
| 6812 | { |
| 6813 | "Floating point value NaN 0xfa7fc00000", |
| 6814 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 6815 | 0, |
| 6816 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6817 | 0, |
| 6818 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6819 | NAN, |
| 6820 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6821 | }, |
| 6822 | { |
| 6823 | "half-precision Floating point value -4", |
| 6824 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 6825 | // Normal case with all enabled. |
| 6826 | -4, |
| 6827 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6828 | 0, |
| 6829 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 6830 | -4.0, |
| 6831 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 6832 | }, |
| 6833 | { |
| 6834 | "+inifinity single precision", |
| 6835 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 6836 | 0, |
| 6837 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6838 | 0, |
| 6839 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 6840 | INFINITY, |
| 6841 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6842 | }, |
| 6843 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6844 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6845 | |
| 6846 | |
| 6847 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6848 | |
| 6849 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 6850 | { |
| 6851 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 6852 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6853 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 6854 | return 1; |
| 6855 | } |
| 6856 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6857 | (void)Pool; |
| 6858 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6859 | return 0; |
| 6860 | } |
| 6861 | |
| 6862 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6863 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6864 | { |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 6865 | uint64_t uInt; |
| 6866 | |
| 6867 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6868 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 6869 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6870 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6871 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 6872 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6873 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6874 | // Set up the decoding context including a memory pool so that |
| 6875 | // indefinite length items can be checked |
| 6876 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6877 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6878 | |
| 6879 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6880 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6881 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6885 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6886 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6887 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6888 | } |
| 6889 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6890 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6891 | } |
| 6892 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6893 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6894 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6895 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6896 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6897 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6898 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6899 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6900 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6901 | } |
| 6902 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6903 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6906 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6907 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6908 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6909 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6910 | |
| 6911 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6912 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6913 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6914 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6915 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6916 | } |
| 6917 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 6918 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6919 | // NaN's can't be compared for equality. A NaN is |
| 6920 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6921 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6922 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6923 | } |
| 6924 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6925 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6926 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6927 | } |
| 6928 | } |
| 6929 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6930 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6931 | } |
| 6932 | |
| 6933 | return 0; |
| 6934 | } |
| 6935 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6936 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6937 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6938 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6939 | { |
| 6940 | QCBORDecodeContext DCtx; |
| 6941 | QCBORItem Item; |
| 6942 | QCBORError uCBORError; |
| 6943 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6944 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6945 | QCBORDecode_Init(&DCtx, |
| 6946 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 6947 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6948 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6949 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 6950 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6951 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6952 | do { |
| 6953 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6954 | } while (QCBOR_SUCCESS == uCBORError); |
| 6955 | |
| 6956 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6957 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 6958 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6959 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6960 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6961 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6962 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6963 | |
| 6964 | |
| 6965 | static const uint8_t spSequenceTestInput[] = { |
| 6966 | /* 1. The valid date string "1985-04-12" */ |
| 6967 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 6968 | |
| 6969 | /* 2. */ |
| 6970 | 0x00, |
| 6971 | |
| 6972 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 6973 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 6974 | |
| 6975 | /* 4. */ |
| 6976 | 0x62, 'h', 'i', |
| 6977 | }; |
| 6978 | |
| 6979 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6980 | int32_t CBORSequenceDecodeTests(void) |
| 6981 | { |
| 6982 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6983 | QCBORItem Item; |
| 6984 | QCBORError uCBORError; |
| 6985 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6986 | |
| 6987 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6988 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6989 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6990 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6991 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6992 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6993 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6994 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6995 | if(uCBORError != QCBOR_SUCCESS) { |
| 6996 | return 1; |
| 6997 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6998 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6999 | return 2; |
| 7000 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7001 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7002 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7003 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7004 | uConsumed != 11) { |
| 7005 | return 102; |
| 7006 | } |
| 7007 | |
| 7008 | // Get 2. |
| 7009 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7010 | if(uCBORError != QCBOR_SUCCESS) { |
| 7011 | return 66; |
| 7012 | } |
| 7013 | |
| 7014 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 7015 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 7016 | uConsumed != 12) { |
| 7017 | return 102; |
| 7018 | } |
| 7019 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7020 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 7021 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7022 | if(uCBORError != QCBOR_SUCCESS) { |
| 7023 | return 2; |
| 7024 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7025 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7026 | return 3; |
| 7027 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7028 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7029 | // A sequence can have stuff at the end that may |
| 7030 | // or may not be valid CBOR. The protocol decoder knows |
| 7031 | // when to stop by definition of the protocol, not |
| 7032 | // when the top-level map or array is ended. |
| 7033 | // Finish still has to be called to know that |
| 7034 | // maps and arrays (if there were any) were closed |
| 7035 | // off correctly. When called like this it |
| 7036 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 7037 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7038 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 7039 | return 4; |
| 7040 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7041 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7042 | // --- Test an empty input ---- |
| 7043 | uint8_t empty[1]; |
| 7044 | UsefulBufC Empty = {empty, 0}; |
| 7045 | QCBORDecode_Init(&DCtx, |
| 7046 | Empty, |
| 7047 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7048 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7049 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7050 | if(uCBORError != QCBOR_SUCCESS) { |
| 7051 | return 5; |
| 7052 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7053 | |
| 7054 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7055 | // --- Sequence with unclosed indefinite length array --- |
| 7056 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7057 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7058 | QCBORDecode_Init(&DCtx, |
| 7059 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 7060 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7061 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7062 | // Get the first item |
| 7063 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7064 | if(uCBORError != QCBOR_SUCCESS) { |
| 7065 | return 7; |
| 7066 | } |
| 7067 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7068 | return 8; |
| 7069 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7070 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7071 | // Get a second item |
| 7072 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7073 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7074 | if(uCBORError != QCBOR_SUCCESS) { |
| 7075 | return 9; |
| 7076 | } |
| 7077 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7078 | return 10; |
| 7079 | } |
| 7080 | |
| 7081 | // Try to finish before consuming all bytes to confirm |
| 7082 | // that the still-open error is returned. |
| 7083 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7084 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7085 | return 11; |
| 7086 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7087 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7088 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7089 | return 20; |
| 7090 | } |
| 7091 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7092 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7093 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7094 | // --- Sequence with a closed indefinite length array --- |
| 7095 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7096 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7097 | QCBORDecode_Init(&DCtx, |
| 7098 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 7099 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7100 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7101 | // Get the first item |
| 7102 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7103 | if(uCBORError != QCBOR_SUCCESS) { |
| 7104 | return 12; |
| 7105 | } |
| 7106 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 7107 | return 13; |
| 7108 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7109 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7110 | // Get a second item |
| 7111 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7112 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7113 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7114 | if(uCBORError != QCBOR_SUCCESS) { |
| 7115 | return 14; |
| 7116 | } |
| 7117 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 7118 | return 15; |
| 7119 | } |
| 7120 | |
| 7121 | // Try to finish before consuming all bytes to confirm |
| 7122 | // that the still-open error is returned. |
| 7123 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 7124 | if(uCBORError != QCBOR_SUCCESS) { |
| 7125 | return 16; |
| 7126 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7127 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7128 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 7129 | return 20; |
| 7130 | } |
| 7131 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7132 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 7133 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 7134 | return 0; |
| 7135 | } |
| 7136 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7137 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 7138 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7139 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 7140 | { |
| 7141 | int nErrCode; |
| 7142 | int32_t n32; |
| 7143 | int16_t n16; |
| 7144 | int8_t n8; |
| 7145 | uint32_t u32; |
| 7146 | uint16_t u16; |
| 7147 | uint8_t u8; |
| 7148 | uint64_t u64; |
| 7149 | |
| 7150 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 7151 | if(nErrCode == -1 || n32 != 1) { |
| 7152 | return 1; |
| 7153 | } |
| 7154 | |
| 7155 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 7156 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 7157 | return 2; |
| 7158 | } |
| 7159 | |
| 7160 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 7161 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 7162 | return 3; |
| 7163 | } |
| 7164 | |
| 7165 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 7166 | if(nErrCode != -1) { |
| 7167 | return 4; |
| 7168 | } |
| 7169 | |
| 7170 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 7171 | if(nErrCode != -1) { |
| 7172 | return 5; |
| 7173 | } |
| 7174 | |
| 7175 | |
| 7176 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 7177 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 7178 | return 6; |
| 7179 | } |
| 7180 | |
| 7181 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 7182 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 7183 | return 7; |
| 7184 | } |
| 7185 | |
| 7186 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 7187 | if(nErrCode == -1 || n16 != 1) { |
| 7188 | return 8; |
| 7189 | } |
| 7190 | |
| 7191 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 7192 | if(nErrCode != -1) { |
| 7193 | return 9; |
| 7194 | } |
| 7195 | |
| 7196 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 7197 | if(nErrCode != -1) { |
| 7198 | return 10; |
| 7199 | } |
| 7200 | |
| 7201 | |
| 7202 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 7203 | if(nErrCode == -1 || n8 != 1) { |
| 7204 | return 11; |
| 7205 | } |
| 7206 | |
| 7207 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 7208 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 7209 | return 12; |
| 7210 | } |
| 7211 | |
| 7212 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 7213 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 7214 | return 13; |
| 7215 | } |
| 7216 | |
| 7217 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 7218 | if(nErrCode != -1) { |
| 7219 | return 14; |
| 7220 | } |
| 7221 | |
| 7222 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 7223 | if(nErrCode != -1) { |
| 7224 | return 15; |
| 7225 | } |
| 7226 | |
| 7227 | |
| 7228 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 7229 | if(nErrCode == -1 || u32 != 1) { |
| 7230 | return 16; |
| 7231 | } |
| 7232 | |
| 7233 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 7234 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 7235 | return 17; |
| 7236 | } |
| 7237 | |
| 7238 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 7239 | if(nErrCode == -1 || u32 != 0) { |
| 7240 | return 18; |
| 7241 | } |
| 7242 | |
| 7243 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 7244 | if(nErrCode != -1) { |
| 7245 | return 19; |
| 7246 | } |
| 7247 | |
| 7248 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 7249 | if(nErrCode != -1) { |
| 7250 | return 20; |
| 7251 | } |
| 7252 | |
| 7253 | |
| 7254 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 7255 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 7256 | return 21; |
| 7257 | } |
| 7258 | |
| 7259 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 7260 | if(nErrCode == -1 || u16 != 0) { |
| 7261 | return 22; |
| 7262 | } |
| 7263 | |
| 7264 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 7265 | if(nErrCode == -1 || u16 != 1) { |
| 7266 | return 23; |
| 7267 | } |
| 7268 | |
| 7269 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 7270 | if(nErrCode != -1) { |
| 7271 | return 24; |
| 7272 | } |
| 7273 | |
| 7274 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 7275 | if(nErrCode != -1) { |
| 7276 | return 25; |
| 7277 | } |
| 7278 | |
| 7279 | |
| 7280 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 7281 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 7282 | return 26; |
| 7283 | } |
| 7284 | |
| 7285 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 7286 | if(nErrCode == -1 || u8 != 0) { |
| 7287 | return 27; |
| 7288 | } |
| 7289 | |
| 7290 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 7291 | if(nErrCode == -1 || u8 != 1) { |
| 7292 | return 28; |
| 7293 | } |
| 7294 | |
| 7295 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 7296 | if(nErrCode != -1) { |
| 7297 | return 29; |
| 7298 | } |
| 7299 | |
| 7300 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 7301 | if(nErrCode != -1) { |
| 7302 | return 30; |
| 7303 | } |
| 7304 | |
| 7305 | |
| 7306 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 7307 | if(nErrCode == -1 || u64 != 1) { |
| 7308 | return 31; |
| 7309 | } |
| 7310 | |
| 7311 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 7312 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 7313 | return 32; |
| 7314 | } |
| 7315 | |
| 7316 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 7317 | if(nErrCode == -1 || u64 != 0) { |
| 7318 | return 33; |
| 7319 | } |
| 7320 | |
| 7321 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 7322 | if(nErrCode != -1) { |
| 7323 | return 34; |
| 7324 | } |
| 7325 | |
| 7326 | return 0; |
| 7327 | } |
| 7328 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7329 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7330 | |
| 7331 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7332 | /* |
| 7333 | A sequence with |
| 7334 | A wrapping bstr |
| 7335 | containing a map |
| 7336 | 1 |
| 7337 | 2 |
| 7338 | A wrapping bstr |
| 7339 | containing an array |
| 7340 | 3 |
| 7341 | wrapping bstr |
| 7342 | 4 |
| 7343 | 5 |
| 7344 | 6 |
| 7345 | array |
| 7346 | 7 |
| 7347 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7348 | */ |
| 7349 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7350 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7351 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7352 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7353 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7354 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7355 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7356 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7357 | |
| 7358 | QCBOREncode_BstrWrap(&EC); |
| 7359 | QCBOREncode_OpenMap(&EC); |
| 7360 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 7361 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 7362 | QCBOREncode_CloseMap(&EC); |
| 7363 | QCBOREncode_BstrWrap(&EC); |
| 7364 | QCBOREncode_OpenArray(&EC); |
| 7365 | QCBOREncode_AddInt64(&EC, 3); |
| 7366 | QCBOREncode_BstrWrap(&EC); |
| 7367 | QCBOREncode_AddInt64(&EC, 4); |
| 7368 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7369 | QCBOREncode_AddInt64(&EC, 5); |
| 7370 | QCBOREncode_CloseArray(&EC); |
| 7371 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7372 | QCBOREncode_AddInt64(&EC, 6); |
| 7373 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7374 | QCBOREncode_OpenArray(&EC); |
| 7375 | QCBOREncode_AddInt64(&EC, 7); |
| 7376 | QCBOREncode_AddInt64(&EC, 8); |
| 7377 | QCBOREncode_CloseArray(&EC); |
| 7378 | |
| 7379 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 7380 | if(uErr) { |
| 7381 | Encoded = NULLUsefulBufC; |
| 7382 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7383 | |
| 7384 | return Encoded; |
| 7385 | } |
| 7386 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7387 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7388 | static const uint8_t spBreakInByteString[] = { |
| 7389 | 0x41, 0xff |
| 7390 | }; |
| 7391 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7392 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7393 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7394 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7395 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7396 | |
| 7397 | QCBORDecodeContext DC; |
| 7398 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7399 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7400 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7401 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7402 | |
| 7403 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7404 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7405 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7406 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 7407 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7408 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7409 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7410 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7411 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7412 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7413 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7414 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7415 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7416 | QCBORDecode_ExitArray(&DC); |
| 7417 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7418 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7419 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7420 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7421 | QCBORDecode_GetInt64(&DC, &n7); |
| 7422 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7423 | QCBORDecode_ExitArray(&DC); |
| 7424 | |
| 7425 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7426 | if(uErr) { |
| 7427 | return (int32_t)uErr; |
| 7428 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7429 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7430 | |
| 7431 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 7432 | * Successful because no items are fetched from byte string. |
| 7433 | */ |
| 7434 | QCBORDecode_Init(&DC, |
| 7435 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 7436 | 0); |
| 7437 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7438 | uErr = QCBORDecode_GetError(&DC); |
| 7439 | if(uErr) { |
| 7440 | return 100 + (int32_t)uErr; |
| 7441 | } |
| 7442 | |
| 7443 | QCBORDecode_ExitBstrWrapped(&DC); |
| 7444 | uErr = QCBORDecode_GetError(&DC); |
| 7445 | if(uErr) { |
| 7446 | return 200 + (int32_t)uErr; |
| 7447 | } |
| 7448 | |
| 7449 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 7450 | * It fails because there should be no break. |
| 7451 | */ |
| 7452 | QCBORDecode_Init(&DC, |
| 7453 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 7454 | 0); |
| 7455 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7456 | QCBORItem Item; |
| 7457 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 7458 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 7459 | return 300 + (int32_t)uErr; |
| 7460 | } |
| 7461 | |
| 7462 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7463 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7464 | |
| 7465 | |
| 7466 | |
| 7467 | |
| 7468 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7469 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7470 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7471 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7472 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7473 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 7474 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 7475 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7476 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7477 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7478 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 7479 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 7480 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7481 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7482 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7483 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7484 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 7485 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7486 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7487 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7488 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 7489 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7490 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7491 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7492 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7493 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 7494 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7495 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7496 | 21, |
| 7497 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 7498 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 7499 | |
| 7500 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7501 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7502 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 7503 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7504 | |
| 7505 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7506 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 7507 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7508 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7509 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7510 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7511 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 7512 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7513 | |
| 7514 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7515 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 7516 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7517 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7518 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7519 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7520 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 7521 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7522 | |
| 7523 | 0x18, 0x33, |
| 7524 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 7525 | |
| 7526 | // MIME |
| 7527 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7528 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 7529 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 7530 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7531 | |
| 7532 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7533 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 7534 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7535 | |
| 7536 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7537 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 7538 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 7539 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7540 | |
| 7541 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7542 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 7543 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7544 | |
| 7545 | // UUID |
| 7546 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7547 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 7548 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7549 | |
| 7550 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7551 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 7552 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7553 | }; |
| 7554 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7555 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7556 | { |
| 7557 | QCBORDecodeContext DC; |
| 7558 | QCBORError uErr; |
| 7559 | |
| 7560 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 7561 | |
| 7562 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7563 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7564 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7565 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7566 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7567 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7568 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 7569 | return 1; |
| 7570 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7571 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7572 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7573 | return 2; |
| 7574 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7575 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7576 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7577 | return 3; |
| 7578 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7579 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7580 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7581 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7582 | return 4; |
| 7583 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7584 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7585 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7586 | return 5; |
| 7587 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7588 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7589 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7590 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7591 | bNeg != false) { |
| 7592 | return 10; |
| 7593 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7594 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7595 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7596 | bNeg != true) { |
| 7597 | return 11; |
| 7598 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7599 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7600 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7601 | return 12; |
| 7602 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7603 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7604 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7605 | return 13; |
| 7606 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7607 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7608 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7609 | return 14; |
| 7610 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7611 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7612 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7613 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7614 | return 20; |
| 7615 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7616 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7617 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7618 | return 21; |
| 7619 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7620 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7621 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7622 | return 22; |
| 7623 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7624 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7625 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7626 | return 23; |
| 7627 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7628 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7629 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7630 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7631 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7632 | return 30; |
| 7633 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7634 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7635 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7636 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7637 | return 31; |
| 7638 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7639 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7640 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7641 | return 32; |
| 7642 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7643 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7644 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7645 | return 33; |
| 7646 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7647 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7648 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7649 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7650 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7651 | return 40; |
| 7652 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7653 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7654 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7655 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7656 | return 41; |
| 7657 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7658 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7659 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7660 | return 42; |
| 7661 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7662 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7663 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7664 | return 43; |
| 7665 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7666 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7667 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7668 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7669 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7670 | return 50; |
| 7671 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7672 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7673 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7674 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7675 | return 51; |
| 7676 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7677 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7678 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7679 | return 52; |
| 7680 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7681 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7682 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7683 | return 53; |
| 7684 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7685 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7686 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7687 | // MIME |
| 7688 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7689 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7690 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7691 | bIsNot7Bit == true) { |
| 7692 | return 60; |
| 7693 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7694 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7695 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7696 | bIsNot7Bit == true) { |
| 7697 | return 61; |
| 7698 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7699 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7700 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7701 | bIsNot7Bit == false) { |
| 7702 | return 62; |
| 7703 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7704 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7705 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7706 | bIsNot7Bit == false) { |
| 7707 | return 63; |
| 7708 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7709 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7710 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7711 | return 64; |
| 7712 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7713 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7714 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7715 | return 65; |
| 7716 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7717 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7718 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7719 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7720 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7721 | return 70; |
| 7722 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7723 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 7724 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7725 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7726 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7727 | return 71; |
| 7728 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7729 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7730 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7731 | return 72; |
| 7732 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7733 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7734 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7735 | return 73; |
| 7736 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7737 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7738 | // Improvement: add some more error test cases |
| 7739 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7740 | QCBORDecode_ExitMap(&DC); |
| 7741 | |
| 7742 | uErr = QCBORDecode_Finish(&DC); |
| 7743 | if(uErr != QCBOR_SUCCESS) { |
| 7744 | return 100; |
| 7745 | } |
| 7746 | |
| 7747 | return 0; |
| 7748 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7749 | |
| 7750 | |
| 7751 | |
| 7752 | |
| 7753 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7754 | [ |
| 7755 | "aaaaaaaaaa", |
| 7756 | {} |
| 7757 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7758 | */ |
| 7759 | static const uint8_t spTooLarge1[] = { |
| 7760 | 0x9f, |
| 7761 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7762 | 0xa0, |
| 7763 | 0xff |
| 7764 | }; |
| 7765 | |
| 7766 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7767 | [ |
| 7768 | { |
| 7769 | 0: "aaaaaaaaaa" |
| 7770 | } |
| 7771 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7772 | */ |
| 7773 | static const uint8_t spTooLarge2[] = { |
| 7774 | 0x9f, |
| 7775 | 0xa1, |
| 7776 | 0x00, |
| 7777 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7778 | 0xff |
| 7779 | }; |
| 7780 | |
| 7781 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7782 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7783 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7784 | { |
| 7785 | 0: "aaaaaaaaaa" |
| 7786 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7787 | */ |
| 7788 | static const uint8_t spTooLarge3[] = { |
| 7789 | 0x4d, |
| 7790 | 0xa1, |
| 7791 | 0x00, |
| 7792 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7793 | }; |
| 7794 | |
| 7795 | int32_t TooLargeInputTest(void) |
| 7796 | { |
| 7797 | QCBORDecodeContext DC; |
| 7798 | QCBORError uErr; |
| 7799 | UsefulBufC String; |
| 7800 | |
| 7801 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 7802 | // to 10 There's not really any way to test this error |
| 7803 | // condition. The error condition is not complex, so setting |
| 7804 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 7805 | |
| 7806 | // The input CBOR is only too large because the |
| 7807 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 7808 | // |
| 7809 | // This test is disabled for the normal test runs because of the |
| 7810 | // special build requirement. |
| 7811 | |
| 7812 | |
| 7813 | // Tests the start of a map being too large |
| 7814 | 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] | 7815 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7816 | QCBORDecode_GetTextString(&DC, &String); |
| 7817 | uErr = QCBORDecode_GetError(&DC); |
| 7818 | if(uErr != QCBOR_SUCCESS) { |
| 7819 | return 1; |
| 7820 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7821 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7822 | uErr = QCBORDecode_GetError(&DC); |
| 7823 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7824 | return 2; |
| 7825 | } |
| 7826 | |
| 7827 | // Tests the end of a map being too large |
| 7828 | 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] | 7829 | QCBORDecode_EnterArray(&DC, NULL); |
| 7830 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7831 | uErr = QCBORDecode_GetError(&DC); |
| 7832 | if(uErr != QCBOR_SUCCESS) { |
| 7833 | return 3; |
| 7834 | } |
| 7835 | QCBORDecode_ExitMap(&DC); |
| 7836 | uErr = QCBORDecode_GetError(&DC); |
| 7837 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7838 | return 4; |
| 7839 | } |
| 7840 | |
| 7841 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 7842 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 7843 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7844 | uErr = QCBORDecode_GetError(&DC); |
| 7845 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7846 | return 5; |
| 7847 | } |
| 7848 | |
| 7849 | return 0; |
| 7850 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7851 | |
| 7852 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7853 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7854 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7855 | /* |
| 7856 | An array of three map entries |
| 7857 | 1) Indefinite length string label for indefinite lenght byte string |
| 7858 | 2) Indefinite length string label for an integer |
| 7859 | 3) Indefinite length string label for an indefinite-length negative big num |
| 7860 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7861 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7862 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7863 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 7864 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 7865 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 7866 | 0x03, |
| 7867 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 7868 | 0xc3, |
| 7869 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7870 | }; |
| 7871 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7872 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7873 | { |
| 7874 | QCBORDecodeContext DCtx; |
| 7875 | |
| 7876 | QCBORDecode_Init(&DCtx, |
| 7877 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 7878 | QCBOR_DECODE_MODE_NORMAL); |
| 7879 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7880 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7881 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 7882 | |
| 7883 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7884 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7885 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7886 | |
| 7887 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7888 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7889 | return 1; |
| 7890 | } |
| 7891 | |
| 7892 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 7893 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 7894 | return 2; |
| 7895 | } |
| 7896 | |
| 7897 | uint64_t uInt; |
| 7898 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 7899 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7900 | return 3; |
| 7901 | } |
| 7902 | if(uInt != 3) { |
| 7903 | return 4; |
| 7904 | } |
| 7905 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7906 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7907 | double uDouble; |
| 7908 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 7909 | "label2", |
| 7910 | 0xff, |
| 7911 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7912 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7913 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7914 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7915 | return 5; |
| 7916 | } |
| 7917 | if(uDouble != -16777474) { |
| 7918 | return 6; |
| 7919 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7920 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7921 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 7922 | return 7; |
| 7923 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7924 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7925 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7926 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7927 | QCBORDecode_ExitMap(&DCtx); |
| 7928 | |
| 7929 | if(QCBORDecode_Finish(&DCtx)) { |
| 7930 | return 99; |
| 7931 | } |
| 7932 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7933 | #else /* QCBOR_DISABLE_TAGS */ |
| 7934 | /* The big num in the input is a CBOR tag and you can't do |
| 7935 | * map lookups in a map with a tag so this test does very little |
| 7936 | * when tags are disabled. That is OK, the test coverage is still |
| 7937 | * good when they are not. |
| 7938 | */ |
| 7939 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 7940 | return 1002; |
| 7941 | } |
| 7942 | #endif /*QCBOR_DISABLE_TAGS */ |
| 7943 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7944 | return 0; |
| 7945 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7946 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7947 | |
| 7948 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7949 | /* |
| 7950 | * An array of an integer and an array. The second array contains |
| 7951 | * a bstr-wrapped map. |
| 7952 | * |
| 7953 | * [7, [h'A36D6669... (see next lines) 73']] |
| 7954 | * |
| 7955 | * {"first integer": 42, |
| 7956 | * "an array of two strings": ["string1", "string2"], |
| 7957 | * "map in a map": |
| 7958 | * { "bytes 1": h'78787878', |
| 7959 | * "bytes 2": h'79797979', |
| 7960 | * "another int": 98, |
| 7961 | * "text 2": "lies, damn lies and statistics" |
| 7962 | * } |
| 7963 | * } |
| 7964 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7965 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7966 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 7967 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 7968 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7969 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7970 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7971 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7972 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7973 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7974 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7975 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7976 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7977 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7978 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7979 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7980 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7981 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7982 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7983 | 0x73 |
| 7984 | }; |
| 7985 | |
| 7986 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7987 | |
| 7988 | /* As above, but the arrays are indefinite length */ |
| 7989 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 7990 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 7991 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7992 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7993 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7994 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7995 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7996 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7997 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7998 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7999 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 8000 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 8001 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 8002 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 8003 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 8004 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 8005 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 8006 | 0x73, |
| 8007 | 0xff, 0xff |
| 8008 | }; |
| 8009 | #endif |
| 8010 | |
| 8011 | |
| 8012 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 8013 | |
| 8014 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8015 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 8016 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8017 | |
| 8018 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8019 | /* |
Laurence Lundblade | d6b33f5 | 2024-03-06 12:51:15 -0800 | [diff] [blame] | 8020 | * An array of one that contains a byte string that is an indefinite |
| 8021 | * length string that CBOR wraps an array of three numbers [42, 43, |
| 8022 | * 44]. The byte string is an implicit tag 24. |
| 8023 | * |
| 8024 | * [ |
| 8025 | * (_ h'83', h'18', h'2A182B', h'182C') |
| 8026 | * ] |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8027 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8028 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 8029 | 0x81, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8030 | 0x5f, |
| 8031 | 0x41, 0x83, |
| 8032 | 0x41, 0x18, |
| 8033 | 0x43, 0x2A, 0x18, 0x2B, |
| 8034 | 0x42, 0x18, 0x2C, |
| 8035 | 0xff |
| 8036 | }; |
| 8037 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8038 | |
| 8039 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 8040 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8041 | { |
| 8042 | QCBORItem Item; |
| 8043 | QCBORError nCBORError; |
| 8044 | QCBORDecodeContext DCtx; |
| 8045 | |
| 8046 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8047 | |
| 8048 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8049 | return 100+(int32_t)nCBORError; |
| 8050 | } |
| 8051 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8052 | return 200; |
| 8053 | } |
| 8054 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8055 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8056 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 8057 | return 150+(int32_t)nCBORError; |
| 8058 | } |
| 8059 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8060 | return 250; |
| 8061 | } |
| 8062 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8063 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8064 | return (int32_t)nCBORError; |
| 8065 | } |
| 8066 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8067 | return 300; |
| 8068 | } |
| 8069 | |
| 8070 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8071 | return 400 + (int32_t)nCBORError; |
| 8072 | } |
| 8073 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8074 | return 500; |
| 8075 | } |
| 8076 | |
| 8077 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8078 | return (int32_t)nCBORError; |
| 8079 | } |
| 8080 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8081 | return 600; |
| 8082 | } |
| 8083 | |
| 8084 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8085 | return 900 + (int32_t)nCBORError; |
| 8086 | } |
| 8087 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8088 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8089 | Item.val.int64 != 42 || |
| 8090 | Item.uDataAlloc || |
| 8091 | Item.uLabelAlloc || |
| 8092 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8093 | return 1000; |
| 8094 | } |
| 8095 | |
| 8096 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8097 | return 1100 + (int32_t)nCBORError; |
| 8098 | } |
| 8099 | |
| 8100 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8101 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8102 | Item.val.int64 != 42 || |
| 8103 | Item.uDataAlloc || |
| 8104 | Item.uLabelAlloc || |
| 8105 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8106 | return 1200; |
| 8107 | } |
| 8108 | |
| 8109 | |
| 8110 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8111 | return 1300 + (int32_t)nCBORError; |
| 8112 | } |
| 8113 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8114 | Item.uDataAlloc || |
| 8115 | Item.uLabelAlloc || |
| 8116 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8117 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8118 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8119 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8120 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8121 | |
| 8122 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8123 | return 1500 + (int32_t)nCBORError; |
| 8124 | } |
| 8125 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8126 | Item.uDataAlloc || |
| 8127 | Item.uLabelAlloc || |
| 8128 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8129 | return 1600; |
| 8130 | } |
| 8131 | |
| 8132 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8133 | return 1700 + (int32_t)nCBORError; |
| 8134 | } |
| 8135 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8136 | Item.uDataAlloc || |
| 8137 | Item.uLabelAlloc || |
| 8138 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8139 | return 1800; |
| 8140 | } |
| 8141 | |
| 8142 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8143 | return (int32_t)nCBORError; |
| 8144 | } |
| 8145 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8146 | Item.uDataAlloc || |
| 8147 | Item.uLabelAlloc || |
| 8148 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8149 | return 1900; |
| 8150 | } |
| 8151 | |
| 8152 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8153 | return (int32_t)nCBORError; |
| 8154 | } |
| 8155 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8156 | Item.uDataAlloc || |
| 8157 | Item.uLabelAlloc || |
| 8158 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8159 | return 2000; |
| 8160 | } |
| 8161 | |
| 8162 | |
| 8163 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8164 | return 2100 + (int32_t)nCBORError; |
| 8165 | } |
| 8166 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8167 | Item.uDataAlloc || |
| 8168 | Item.uLabelAlloc || |
| 8169 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 8170 | Item.uDataType != QCBOR_TYPE_MAP || |
| 8171 | Item.val.uCount != 4) { |
| 8172 | return 2100; |
| 8173 | } |
| 8174 | |
| 8175 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8176 | return 2200 + (int32_t)nCBORError; |
| 8177 | } |
| 8178 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8179 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 8180 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8181 | Item.uDataAlloc || |
| 8182 | Item.uLabelAlloc || |
| 8183 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 8184 | return 2300; |
| 8185 | } |
| 8186 | |
| 8187 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8188 | return 2400 + (int32_t)nCBORError; |
| 8189 | } |
| 8190 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8191 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8192 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8193 | Item.uDataAlloc || |
| 8194 | Item.uLabelAlloc || |
| 8195 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8196 | return 2500; |
| 8197 | } |
| 8198 | |
| 8199 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8200 | return 2600 + (int32_t)nCBORError; |
| 8201 | } |
| 8202 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8203 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 8204 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 8205 | Item.uDataAlloc || |
| 8206 | Item.uLabelAlloc || |
| 8207 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 8208 | return 2700; |
| 8209 | } |
| 8210 | |
| 8211 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8212 | return 2800 + (int32_t)nCBORError; |
| 8213 | } |
| 8214 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8215 | Item.uDataAlloc || |
| 8216 | Item.uLabelAlloc || |
| 8217 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 8218 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8219 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8220 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8221 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8222 | |
| 8223 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 8224 | return 3000 + (int32_t)nCBORError; |
| 8225 | } |
| 8226 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8227 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8228 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8229 | Item.uDataAlloc || |
| 8230 | Item.uLabelAlloc || |
| 8231 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8232 | return 3100; |
| 8233 | } |
| 8234 | |
| 8235 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8236 | return 3200 + (int32_t)nCBORError; |
| 8237 | } |
| 8238 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8239 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8240 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8241 | Item.uDataAlloc || |
| 8242 | Item.uLabelAlloc || |
| 8243 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8244 | return 3300; |
| 8245 | } |
| 8246 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8247 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 8248 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8249 | return 3300 + (int32_t)nCBORError; |
| 8250 | } |
| 8251 | |
| 8252 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8253 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8254 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8255 | return 3400 + (int32_t)nCBORError; |
| 8256 | } |
| 8257 | |
| 8258 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8259 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8260 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8261 | return 3500 + (int32_t)nCBORError; |
| 8262 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8263 | |
| 8264 | |
| 8265 | // Rewind to top level after entering several maps |
| 8266 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8267 | |
| 8268 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8269 | return (int32_t)nCBORError; |
| 8270 | } |
| 8271 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8272 | Item.val.uCount != 3) { |
| 8273 | return 400; |
| 8274 | } |
| 8275 | |
| 8276 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8277 | return 4000+(int32_t)nCBORError; |
| 8278 | } |
| 8279 | |
| 8280 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8281 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8282 | Item.val.int64 != 42 || |
| 8283 | Item.uDataAlloc || |
| 8284 | Item.uLabelAlloc || |
| 8285 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8286 | return 4100; |
| 8287 | } |
| 8288 | |
| 8289 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8290 | return 4100+(int32_t)nCBORError; |
| 8291 | } |
| 8292 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8293 | Item.uDataAlloc || |
| 8294 | Item.uLabelAlloc || |
| 8295 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8296 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8297 | Item.val.uCount != 2) { |
| 8298 | return 4200; |
| 8299 | } |
| 8300 | |
| 8301 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8302 | return 4200+(int32_t)nCBORError; |
| 8303 | } |
| 8304 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8305 | Item.uDataAlloc || |
| 8306 | Item.uLabelAlloc || |
| 8307 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8308 | return 4300; |
| 8309 | } |
| 8310 | |
| 8311 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8312 | return 4300+(int32_t)nCBORError; |
| 8313 | } |
| 8314 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8315 | Item.uDataAlloc || |
| 8316 | Item.uLabelAlloc || |
| 8317 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8318 | return 4400; |
| 8319 | } |
| 8320 | |
| 8321 | QCBORDecode_Rewind(&DCtx); |
| 8322 | |
| 8323 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8324 | return 4400+(int32_t)nCBORError; |
| 8325 | } |
| 8326 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8327 | Item.val.uCount != 3) { |
| 8328 | return 4500; |
| 8329 | } |
| 8330 | |
| 8331 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8332 | return (int32_t)nCBORError; |
| 8333 | } |
| 8334 | |
| 8335 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8336 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8337 | Item.val.int64 != 42 || |
| 8338 | Item.uDataAlloc || |
| 8339 | Item.uLabelAlloc || |
| 8340 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8341 | return 4600; |
| 8342 | } |
| 8343 | |
| 8344 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8345 | return (int32_t)nCBORError; |
| 8346 | } |
| 8347 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8348 | Item.uDataAlloc || |
| 8349 | Item.uLabelAlloc || |
| 8350 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8351 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8352 | Item.val.uCount != 2) { |
| 8353 | return 4700; |
| 8354 | } |
| 8355 | |
| 8356 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8357 | return (int32_t)nCBORError; |
| 8358 | } |
| 8359 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8360 | Item.uDataAlloc || |
| 8361 | Item.uLabelAlloc || |
| 8362 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8363 | return 4800; |
| 8364 | } |
| 8365 | |
| 8366 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8367 | return 4900+(int32_t)nCBORError; |
| 8368 | } |
| 8369 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8370 | Item.uDataAlloc || |
| 8371 | Item.uLabelAlloc || |
| 8372 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8373 | return 5000; |
| 8374 | } |
| 8375 | |
| 8376 | |
| 8377 | // Rewind an entered map |
| 8378 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8379 | |
| 8380 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8381 | |
| 8382 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8383 | return 5100+(int32_t)nCBORError; |
| 8384 | } |
| 8385 | |
| 8386 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8387 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8388 | Item.val.int64 != 42 || |
| 8389 | Item.uDataAlloc || |
| 8390 | Item.uLabelAlloc || |
| 8391 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8392 | return 5200; |
| 8393 | } |
| 8394 | |
| 8395 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8396 | return 5200+(int32_t)nCBORError; |
| 8397 | } |
| 8398 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8399 | Item.uDataAlloc || |
| 8400 | Item.uLabelAlloc || |
| 8401 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8402 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8403 | Item.val.uCount != 2) { |
| 8404 | return -5300; |
| 8405 | } |
| 8406 | |
| 8407 | QCBORDecode_Rewind(&DCtx); |
| 8408 | |
| 8409 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8410 | return 5300+(int32_t)nCBORError; |
| 8411 | } |
| 8412 | |
| 8413 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8414 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8415 | Item.val.int64 != 42 || |
| 8416 | Item.uDataAlloc || |
| 8417 | Item.uLabelAlloc || |
| 8418 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8419 | return 5400; |
| 8420 | } |
| 8421 | |
| 8422 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8423 | return 5400+(int32_t)nCBORError; |
| 8424 | } |
| 8425 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8426 | Item.uDataAlloc || |
| 8427 | Item.uLabelAlloc || |
| 8428 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8429 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8430 | Item.val.uCount != 2) { |
| 8431 | return 5500; |
| 8432 | } |
| 8433 | |
| 8434 | |
| 8435 | // Rewind and entered array inside an entered map |
| 8436 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8437 | |
| 8438 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8439 | |
| 8440 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 8441 | |
| 8442 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8443 | return 5600+(int32_t)nCBORError; |
| 8444 | } |
| 8445 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8446 | Item.uDataAlloc || |
| 8447 | Item.uLabelAlloc || |
| 8448 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8449 | return 5700; |
| 8450 | } |
| 8451 | |
| 8452 | QCBORDecode_Rewind(&DCtx); |
| 8453 | |
| 8454 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8455 | return 5700+(int32_t)nCBORError; |
| 8456 | } |
| 8457 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8458 | Item.uDataAlloc || |
| 8459 | Item.uLabelAlloc || |
| 8460 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8461 | return 5800; |
| 8462 | } |
| 8463 | |
| 8464 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8465 | return (int32_t)nCBORError; |
| 8466 | } |
| 8467 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8468 | Item.uDataAlloc || |
| 8469 | Item.uLabelAlloc || |
| 8470 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8471 | return 5900; |
| 8472 | } |
| 8473 | |
| 8474 | QCBORDecode_Rewind(&DCtx); |
| 8475 | |
| 8476 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8477 | return 5900+(int32_t)nCBORError; |
| 8478 | } |
| 8479 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8480 | Item.uDataAlloc || |
| 8481 | Item.uLabelAlloc || |
| 8482 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8483 | return 6000; |
| 8484 | } |
| 8485 | |
| 8486 | |
| 8487 | // Rewind a byte string inside an array inside an array |
| 8488 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 8489 | |
| 8490 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8491 | |
| 8492 | uint64_t i; |
| 8493 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8494 | |
| 8495 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8496 | |
| 8497 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8498 | if(QCBORDecode_GetError(&DCtx)) { |
| 8499 | return 6100; |
| 8500 | } |
| 8501 | |
| 8502 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8503 | return (int32_t)nCBORError; |
| 8504 | } |
| 8505 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8506 | return 6200; |
| 8507 | } |
| 8508 | |
| 8509 | QCBORDecode_Rewind(&DCtx); |
| 8510 | |
| 8511 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8512 | return 6300+(int32_t)nCBORError; |
| 8513 | } |
| 8514 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8515 | return 6400; |
| 8516 | } |
| 8517 | |
| 8518 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8519 | // Rewind a byte string inside an indefinite-length array inside |
| 8520 | // indefinite-length array |
| 8521 | |
| 8522 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 8523 | |
| 8524 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8525 | |
| 8526 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8527 | |
| 8528 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8529 | |
| 8530 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8531 | if(QCBORDecode_GetError(&DCtx)) { |
| 8532 | return 6500; |
| 8533 | } |
| 8534 | |
| 8535 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8536 | return 6600+(int32_t)nCBORError; |
| 8537 | } |
| 8538 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8539 | return 6700; |
| 8540 | } |
| 8541 | |
| 8542 | QCBORDecode_Rewind(&DCtx); |
| 8543 | |
| 8544 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8545 | return 6800+(int32_t)nCBORError; |
| 8546 | } |
| 8547 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8548 | return 6900; |
| 8549 | } |
| 8550 | #endif |
| 8551 | |
| 8552 | // Rewind an empty map |
| 8553 | // [100, {}] |
| 8554 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 8555 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8556 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8557 | if(i != 100) { |
| 8558 | return 7010; |
| 8559 | } |
| 8560 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8561 | |
| 8562 | /* Do it 5 times to be sure multiple rewinds work */ |
| 8563 | for(int n = 0; n < 5; n++) { |
| 8564 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 8565 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8566 | return 7000 + n; |
| 8567 | } |
| 8568 | QCBORDecode_Rewind(&DCtx); |
| 8569 | } |
| 8570 | QCBORDecode_ExitMap(&DCtx); |
| 8571 | QCBORDecode_Rewind(&DCtx); |
| 8572 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8573 | if(i != 100) { |
| 8574 | return 7010; |
| 8575 | } |
| 8576 | QCBORDecode_ExitArray(&DCtx); |
| 8577 | QCBORDecode_Rewind(&DCtx); |
| 8578 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8579 | i = 9; |
| 8580 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8581 | if(i != 100) { |
| 8582 | return 7020; |
| 8583 | } |
| 8584 | if(QCBORDecode_GetError(&DCtx)){ |
| 8585 | return 7030; |
| 8586 | } |
| 8587 | |
| 8588 | // Rewind an empty indefinite length map |
| 8589 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8590 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 8591 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8592 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8593 | if(i != 100) { |
| 8594 | return 7810; |
| 8595 | } |
| 8596 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8597 | |
| 8598 | /* Do it 5 times to be sure multiple rewinds work */ |
| 8599 | for(int n = 0; n < 5; n++) { |
| 8600 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 8601 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8602 | return 7800 + n; |
| 8603 | } |
| 8604 | QCBORDecode_Rewind(&DCtx); |
| 8605 | } |
| 8606 | QCBORDecode_ExitMap(&DCtx); |
| 8607 | QCBORDecode_Rewind(&DCtx); |
| 8608 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8609 | if(i != 100) { |
| 8610 | return 7810; |
| 8611 | } |
| 8612 | QCBORDecode_ExitArray(&DCtx); |
| 8613 | QCBORDecode_Rewind(&DCtx); |
| 8614 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8615 | i = 9; |
| 8616 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8617 | if(i != 100) { |
| 8618 | return 7820; |
| 8619 | } |
| 8620 | if(QCBORDecode_GetError(&DCtx)){ |
| 8621 | return 7830; |
| 8622 | } |
| 8623 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8624 | |
| 8625 | // Rewind an indefnite length byte-string wrapped sequence |
| 8626 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 8627 | QCBORDecode_Init(&DCtx, |
| 8628 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 8629 | 0); |
| 8630 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 8631 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 8632 | |
| 8633 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8634 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
| 8635 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8636 | /* TODO: This is what happens when trying to enter |
| 8637 | * indefinite-length byte string wrapped CBOR. Tolerate for |
| 8638 | * now. Eventually it needs to be fixed so this works, but that |
| 8639 | * is not simple. |
| 8640 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8641 | return 7300; |
| 8642 | } |
| 8643 | |
| 8644 | /* |
| 8645 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8646 | if(i != 42) { |
| 8647 | return 7110; |
| 8648 | } |
| 8649 | QCBORDecode_Rewind(&DCtx); |
| 8650 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8651 | if(i != 42) { |
| 8652 | return 7220; |
| 8653 | }*/ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8654 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8655 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8656 | |
| 8657 | |
| 8658 | // Rewind an indefnite length byte-string wrapped sequence |
| 8659 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8660 | return 0; |
| 8661 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8662 | |
| 8663 | |
| 8664 | |
| 8665 | |
| 8666 | static const uint8_t spBooleansInMap[] = |
| 8667 | { |
| 8668 | 0xa1, 0x08, 0xf5 |
| 8669 | }; |
| 8670 | |
| 8671 | static const uint8_t spBooleansInMapWrongType[] = |
| 8672 | { |
| 8673 | 0xa1, 0x08, 0xf6 |
| 8674 | }; |
| 8675 | |
| 8676 | static const uint8_t spBooleansInMapNWF[] = |
| 8677 | { |
| 8678 | 0xa1, 0x08, 0x1a |
| 8679 | }; |
| 8680 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8681 | static const uint8_t spNullInMap[] = |
| 8682 | { |
| 8683 | 0xa1, 0x08, 0xf6 |
| 8684 | }; |
| 8685 | |
| 8686 | static const uint8_t spUndefinedInMap[] = |
| 8687 | { |
| 8688 | 0xa1, 0x08, 0xf7 |
| 8689 | }; |
| 8690 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8691 | |
| 8692 | int32_t BoolTest(void) |
| 8693 | { |
| 8694 | QCBORDecodeContext DCtx; |
| 8695 | bool b; |
| 8696 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8697 | QCBORDecode_Init(&DCtx, |
| 8698 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8699 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8700 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8701 | QCBORDecode_GetBool(&DCtx, &b); |
| 8702 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 8703 | return 1; |
| 8704 | } |
| 8705 | |
| 8706 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 8707 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 8708 | return 2; |
| 8709 | } |
| 8710 | |
| 8711 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 8712 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 8713 | return 3; |
| 8714 | } |
| 8715 | |
| 8716 | |
| 8717 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 8718 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 8719 | return 4; |
| 8720 | } |
| 8721 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8722 | QCBORDecode_Init(&DCtx, |
| 8723 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 8724 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8725 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8726 | QCBORDecode_GetBool(&DCtx, &b); |
| 8727 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8728 | return 5; |
| 8729 | } |
| 8730 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8731 | QCBORDecode_Init(&DCtx, |
| 8732 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8733 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8734 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8735 | QCBORDecode_GetBool(&DCtx, &b); |
| 8736 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8737 | return 6; |
| 8738 | } |
| 8739 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8740 | |
| 8741 | QCBORDecode_Init(&DCtx, |
| 8742 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 8743 | 0); |
| 8744 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8745 | QCBORDecode_GetNull(&DCtx); |
| 8746 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8747 | return 7; |
| 8748 | } |
| 8749 | |
| 8750 | QCBORDecode_Init(&DCtx, |
| 8751 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8752 | 0); |
| 8753 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8754 | QCBORDecode_GetNull(&DCtx); |
| 8755 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8756 | return 8; |
| 8757 | } |
| 8758 | |
| 8759 | QCBORDecode_Init(&DCtx, |
| 8760 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 8761 | 0); |
| 8762 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8763 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 8764 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8765 | return 9; |
| 8766 | } |
| 8767 | |
| 8768 | QCBORDecode_Init(&DCtx, |
| 8769 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8770 | 0); |
| 8771 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8772 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 8773 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8774 | return 10; |
| 8775 | } |
| 8776 | |
| 8777 | QCBORDecode_Init(&DCtx, |
| 8778 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8779 | 0); |
| 8780 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8781 | QCBORDecode_GetUndefined(&DCtx); |
| 8782 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8783 | return 11; |
| 8784 | } |
| 8785 | |
| 8786 | QCBORDecode_Init(&DCtx, |
| 8787 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 8788 | 0); |
| 8789 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8790 | QCBORDecode_GetUndefined(&DCtx); |
| 8791 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8792 | return 12; |
| 8793 | } |
| 8794 | |
| 8795 | QCBORDecode_Init(&DCtx, |
| 8796 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8797 | 0); |
| 8798 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8799 | QCBORDecode_GetUndefined(&DCtx); |
| 8800 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8801 | return 13; |
| 8802 | } |
| 8803 | |
| 8804 | QCBORDecode_Init(&DCtx, |
| 8805 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 8806 | 0); |
| 8807 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8808 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 8809 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8810 | return 14; |
| 8811 | } |
| 8812 | |
| 8813 | QCBORDecode_Init(&DCtx, |
| 8814 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8815 | 0); |
| 8816 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8817 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 8818 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8819 | return 15; |
| 8820 | } |
| 8821 | |
| 8822 | QCBORDecode_Init(&DCtx, |
| 8823 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8824 | 0); |
| 8825 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8826 | QCBORDecode_GetUndefined(&DCtx); |
| 8827 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8828 | return 15; |
| 8829 | } |
| 8830 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8831 | return 0; |
| 8832 | } |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 8833 | |
| 8834 | |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame^] | 8835 | #if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT) |
| 8836 | |
| 8837 | struct PreciseNumberConversion { |
| 8838 | char *szDescription; |
| 8839 | UsefulBufC CBOR; |
| 8840 | QCBORError uError; |
| 8841 | uint8_t qcborType; |
| 8842 | struct { |
| 8843 | int64_t int64; |
| 8844 | uint64_t uint64; |
| 8845 | double d; |
| 8846 | } number; |
| 8847 | }; |
| 8848 | |
| 8849 | |
| 8850 | static const struct PreciseNumberConversion PreciseNumberConversions[] = { |
| 8851 | { |
| 8852 | "-0.00", |
| 8853 | {"\xf9\x80\x00", 3}, |
| 8854 | QCBOR_SUCCESS, |
| 8855 | QCBOR_TYPE_INT64, |
| 8856 | {0, 0, 0} |
| 8857 | }, |
| 8858 | { |
| 8859 | "NaN", |
| 8860 | {"\xf9\x7e\x00", 3}, |
| 8861 | QCBOR_SUCCESS, |
| 8862 | QCBOR_TYPE_DOUBLE, |
| 8863 | {0, 0, NAN} |
| 8864 | }, |
| 8865 | { |
| 8866 | "NaN payload", |
| 8867 | {"\xFB\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9}, |
| 8868 | QCBOR_SUCCESS, |
| 8869 | QCBOR_TYPE_DOUBLE, |
| 8870 | {0, 0, NAN} |
| 8871 | }, |
| 8872 | { |
| 8873 | "65536.0 single", |
| 8874 | {"\xFA\x47\x80\x00\x00", 5}, |
| 8875 | QCBOR_SUCCESS, |
| 8876 | QCBOR_TYPE_INT64, |
| 8877 | {65536, 0, 0} |
| 8878 | }, |
| 8879 | { |
| 8880 | "Infinity", |
| 8881 | {"\xf9\x7c\x00", 3}, |
| 8882 | QCBOR_SUCCESS, |
| 8883 | QCBOR_TYPE_DOUBLE, |
| 8884 | {0, 0, INFINITY} |
| 8885 | }, |
| 8886 | { |
| 8887 | "1.0", |
| 8888 | {"\xf9\x3c\x00", 3}, |
| 8889 | QCBOR_SUCCESS, |
| 8890 | QCBOR_TYPE_INT64, |
| 8891 | {1, 0, 0} |
| 8892 | }, |
| 8893 | { |
| 8894 | "UINT64_MAX", |
| 8895 | {"\x1B\xff\xff\xff\xff\xff\xff\xff\xff", 9}, |
| 8896 | QCBOR_SUCCESS, |
| 8897 | QCBOR_TYPE_UINT64, |
| 8898 | {0, UINT64_MAX, 0} |
| 8899 | }, |
| 8900 | { |
| 8901 | "INT64_MIN", |
| 8902 | {"\x3B\x7f\xff\xff\xff\xff\xff\xff\xff", 9}, |
| 8903 | QCBOR_SUCCESS, |
| 8904 | QCBOR_TYPE_INT64, |
| 8905 | {INT64_MIN, 0, 0} |
| 8906 | }, |
| 8907 | { |
| 8908 | "18446742974197923840", |
| 8909 | {"\xFB\x43\xEF\xFF\xFF\xE0\x00\x00\x00", 9}, |
| 8910 | QCBOR_SUCCESS, |
| 8911 | QCBOR_TYPE_UINT64, |
| 8912 | {0, 18446742974197923840ULL, 0} |
| 8913 | }, |
| 8914 | { |
| 8915 | "65-bit neg, too much precision", |
| 8916 | {"\x3B\x80\x00\x00\x00\x00\x00\x00\x01", 9}, |
| 8917 | QCBOR_SUCCESS, |
| 8918 | QCBOR_TYPE_65BIT_NEG_INT, |
| 8919 | {0, 0x8000000000000001, 0} |
| 8920 | }, |
| 8921 | { |
| 8922 | "65-bit neg lots of precision", |
| 8923 | {"\x3B\xff\xff\xff\xff\xff\xff\xf0\x00", 9}, |
| 8924 | QCBOR_SUCCESS, |
| 8925 | QCBOR_TYPE_DOUBLE, |
| 8926 | {0, 0, -18446744073709547521.0} |
| 8927 | }, |
| 8928 | { |
| 8929 | "65-bit neg very precise", |
| 8930 | {"\x3B\xff\xff\xff\xff\xff\xff\xf8\x00", 9}, |
| 8931 | QCBOR_SUCCESS, |
| 8932 | QCBOR_TYPE_DOUBLE, |
| 8933 | {0, 0, -18446744073709549569.0} |
| 8934 | }, |
| 8935 | { |
| 8936 | "65-bit neg too precise", |
| 8937 | {"\x3B\xff\xff\xff\xff\xff\xff\xfc\x00", 9}, |
| 8938 | QCBOR_SUCCESS, |
| 8939 | QCBOR_TYPE_65BIT_NEG_INT, |
| 8940 | {0, 18446744073709550592ULL, 0.0} |
| 8941 | }, |
| 8942 | { |
| 8943 | "65-bit neg, power of two", |
| 8944 | {"\x3B\x80\x00\x00\x00\x00\x00\x00\x00", 9}, |
| 8945 | QCBOR_SUCCESS, |
| 8946 | QCBOR_TYPE_DOUBLE, |
| 8947 | {0, 0, -9223372036854775809.0} |
| 8948 | }, |
| 8949 | { |
| 8950 | "Zero", |
| 8951 | {"\x00", 1}, |
| 8952 | QCBOR_SUCCESS, |
| 8953 | QCBOR_TYPE_INT64, |
| 8954 | {0, 0, 0} |
| 8955 | }, |
| 8956 | { |
| 8957 | "Pi", |
| 8958 | {"\xFB\x40\x09\x2A\xDB\x40\x2D\x16\xB9", 9}, |
| 8959 | QCBOR_SUCCESS, |
| 8960 | QCBOR_TYPE_DOUBLE, |
| 8961 | {0, 0, 3.145926} |
| 8962 | }, |
| 8963 | { |
| 8964 | "String", |
| 8965 | {"\x60", 1}, |
| 8966 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 8967 | QCBOR_TYPE_NONE, |
| 8968 | {0, 0, 0} |
| 8969 | } |
| 8970 | }; |
| 8971 | |
| 8972 | |
| 8973 | int32_t |
| 8974 | PreciseNumbersTest(void) |
| 8975 | { |
| 8976 | int i; |
| 8977 | QCBORError uErr; |
| 8978 | QCBORItem Item; |
| 8979 | QCBORDecodeContext DCtx; |
| 8980 | const struct PreciseNumberConversion *pTest; |
| 8981 | |
| 8982 | const int count = (int)C_ARRAY_COUNT(PreciseNumberConversions, struct PreciseNumberConversion); |
| 8983 | |
| 8984 | for(i = 0; i < count; i++) { |
| 8985 | pTest = &PreciseNumberConversions[i]; |
| 8986 | |
| 8987 | if(i == 11) { |
| 8988 | uErr = 99; // For break point only |
| 8989 | } |
| 8990 | |
| 8991 | QCBORDecode_Init(&DCtx, pTest->CBOR, 0); |
| 8992 | |
| 8993 | QCBORDecode_GetNumberConvertPrecisely(&DCtx, &Item); |
| 8994 | |
| 8995 | uErr = QCBORDecode_GetError(&DCtx); |
| 8996 | |
| 8997 | if(uErr != pTest->uError) { |
| 8998 | return i * 1000 + (int)uErr; |
| 8999 | } |
| 9000 | |
| 9001 | if(pTest->qcborType != Item.uDataType) { |
| 9002 | return i * 1000 + 200; |
| 9003 | } |
| 9004 | |
| 9005 | if(pTest->qcborType == QCBOR_TYPE_NONE) { |
| 9006 | continue; |
| 9007 | } |
| 9008 | |
| 9009 | switch(pTest->qcborType) { |
| 9010 | case QCBOR_TYPE_INT64: |
| 9011 | if(Item.val.int64 != pTest->number.int64) { |
| 9012 | return i * 1000 + 300; |
| 9013 | } |
| 9014 | break; |
| 9015 | |
| 9016 | case QCBOR_TYPE_UINT64: |
| 9017 | case QCBOR_TYPE_65BIT_NEG_INT: |
| 9018 | if(Item.val.uint64 != pTest->number.uint64) { |
| 9019 | return i * 1000 + 400; |
| 9020 | } |
| 9021 | break; |
| 9022 | |
| 9023 | case QCBOR_TYPE_DOUBLE: |
| 9024 | if(isnan(pTest->number.d)) { |
| 9025 | if(!isnan(Item.val.dfnum)) { |
| 9026 | return i * 1000 + 600; |
| 9027 | } |
| 9028 | } else { |
| 9029 | if(Item.val.dfnum != pTest->number.d) { |
| 9030 | return i * 1000 + 500; |
| 9031 | } |
| 9032 | } |
| 9033 | break; |
| 9034 | } |
| 9035 | } |
| 9036 | return 0; |
| 9037 | } |
| 9038 | |
| 9039 | #endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 9040 | |
| 9041 | |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 9042 | int32_t |
| 9043 | ErrorHandlingTests(void) |
| 9044 | { |
| 9045 | QCBORDecodeContext DCtx; |
| 9046 | QCBORItem Item; |
| 9047 | QCBORError uError; |
| 9048 | int64_t integer; |
| 9049 | |
| 9050 | /* Test QCBORDecode_SetError() */ |
| 9051 | QCBORDecode_Init(&DCtx, |
| 9052 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9053 | QCBOR_DECODE_MODE_NORMAL); |
| 9054 | |
| 9055 | QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED); |
| 9056 | |
| 9057 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9058 | |
| 9059 | uError = QCBORDecode_GetError(&DCtx); |
| 9060 | |
| 9061 | if(uError != QCBOR_ERR_FIRST_USER_DEFINED) { |
| 9062 | return -1; |
| 9063 | } |
| 9064 | |
| 9065 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 9066 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 9067 | return -2; |
| 9068 | } |
| 9069 | |
| 9070 | |
| 9071 | /* Test data type returned from previous error */ |
| 9072 | QCBORDecode_Init(&DCtx, |
| 9073 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 9074 | QCBOR_DECODE_MODE_NORMAL); |
| 9075 | QCBORDecode_GetInt64(&DCtx, &integer); |
| 9076 | uError = QCBORDecode_GetError(&DCtx); |
| 9077 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9078 | return -3; |
| 9079 | } |
| 9080 | |
| 9081 | QCBORDecode_VGetNext(&DCtx, &Item); |
| 9082 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 9083 | Item.uDataType != QCBOR_TYPE_NONE) { |
| 9084 | return -2; |
| 9085 | } |
| 9086 | uError = QCBORDecode_GetError(&DCtx); |
| 9087 | if(uError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 9088 | return -3; |
| 9089 | } |
| 9090 | |
| 9091 | |
| 9092 | /* Test error classification functions */ |
| 9093 | |
| 9094 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 9095 | return -10; |
| 9096 | } |
| 9097 | if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) { |
| 9098 | return -11; |
| 9099 | } |
| 9100 | if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) { |
| 9101 | return -12; |
| 9102 | } |
| 9103 | if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) { |
| 9104 | return -13; |
| 9105 | } |
| 9106 | |
| 9107 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) { |
| 9108 | return -20; |
| 9109 | } |
| 9110 | if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) { |
| 9111 | return -21; |
| 9112 | } |
| 9113 | if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) { |
| 9114 | return -22; |
| 9115 | } |
| 9116 | if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) { |
| 9117 | return -23; |
| 9118 | } |
| 9119 | |
| 9120 | return 0; |
| 9121 | } |