Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3 | Copyright (c) 2018-2022, Laurence Lundblade. |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 4 | Copyright (c) 2021, Arm Limited. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 5 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 6 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are |
| 9 | met: |
| 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 | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1791 | // TODO: add a test index and report it so it is eaier to figure out which test failed. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1792 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1793 | UsefulBufC Input; |
| 1794 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1795 | }; |
| 1796 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1797 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1798 | static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1799 | { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1800 | for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1801 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1802 | QCBORError uCBORError; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1803 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1804 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1805 | |
| 1806 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1807 | // Set up the decoding context including a memory pool so that |
| 1808 | // indefinite length items can be checked |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1809 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1810 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1811 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1812 | if(uCBORError) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1813 | return -9; |
| 1814 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1815 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1816 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1817 | const size_t nIndexx = (size_t)(pF - pFailInputs); |
| 1818 | if(nIndexx == 8) { |
| 1819 | uCBORError = 9; |
| 1820 | } |
| 1821 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 1822 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1823 | // Iterate until there is an error of some sort error |
| 1824 | QCBORItem Item; |
| 1825 | do { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1826 | // Set to something none-zero, something other than QCBOR_TYPE_NONE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1827 | memset(&Item, 0x33, sizeof(Item)); |
| 1828 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1829 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1830 | } while(uCBORError == QCBOR_SUCCESS); |
| 1831 | |
| 1832 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1833 | |
| 1834 | // Must get the expected error or the this test fails |
| 1835 | // The data and label type must also be QCBOR_TYPE_NONE |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1836 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1837 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1838 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1839 | // return index of CBOR + 100 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1840 | const size_t nIndex = (size_t)(pF - pFailInputs); |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1841 | return (int32_t)(nIndex * 100 + uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1849 | static const struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1850 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1851 | // returned is also checked. |
| 1852 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1853 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1854 | // Indefinite length strings must be closed off |
| 1855 | // An indefinite length byte string not closed off |
| 1856 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1857 | // An indefinite length text string not closed off |
| 1858 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1859 | |
| 1860 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1861 | // All the chunks in an indefinite length string must be of the type of |
| 1862 | // indefinite length string |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1863 | // indefinite length byte string with text string chunk |
| 1864 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1865 | // indefinite length text string with a byte string chunk |
| 1866 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1867 | // indefinite length byte string with an positive integer chunk |
| 1868 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1869 | // indefinite length byte string with an negative integer chunk |
| 1870 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1871 | // indefinite length byte string with an array chunk |
| 1872 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1873 | // indefinite length byte string with an map chunk |
| 1874 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1875 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1876 | // indefinite length byte string with tagged integer chunk |
| 1877 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1878 | #else |
| 1879 | // indefinite length byte string with tagged integer chunk |
| 1880 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_TAGS_DISABLED }, |
| 1881 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1882 | // indefinite length byte string with an simple type chunk |
| 1883 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1884 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1885 | // indefinite length text string with indefinite string inside |
| 1886 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1887 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1888 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1889 | |
| 1890 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1891 | // An indefinite length text string not closed off |
| 1892 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1893 | |
| 1894 | |
| 1895 | // All the chunks in an indefinite length string must be of the type of |
| 1896 | // indefinite length string |
| 1897 | // indefinite length byte string with text string chunk |
| 1898 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1899 | // indefinite length text string with a byte string chunk |
| 1900 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1901 | // indefinite length byte string with an positive integer chunk |
| 1902 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1903 | // indefinite length byte string with an negative integer chunk |
| 1904 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1905 | // indefinite length byte string with an array chunk |
| 1906 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1907 | // indefinite length byte string with an map chunk |
| 1908 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1909 | // indefinite length byte string with tagged integer chunk |
| 1910 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1911 | // indefinite length byte string with an simple type chunk |
| 1912 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1913 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1914 | // indefinite length text string with indefinite string inside |
| 1915 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1916 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1917 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1918 | |
| 1919 | // Definte length maps and arrays must be closed by having the right number of items |
| 1920 | // A definte length array that is supposed to have 1 item, but has none |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1921 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1922 | // A definte length array that is supposed to have 2 items, but has only 1 |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1923 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1924 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1925 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1926 | // A definte length map that is supposed to have 1 item, but has none |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1927 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1928 | // A definte length map that is supposed to have s item, but has only 1 |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1929 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1930 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1931 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1932 | // Indefinte length maps and arrays must be ended by a break |
| 1933 | // Indefinite length array with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1934 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1935 | // Indefinite length array with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1936 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1937 | // Indefinite length map with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1938 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1939 | // Indefinite length map with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1940 | { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1941 | |
| 1942 | |
| 1943 | // Nested maps and arrays must be closed off (some extra nested test vectors) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1944 | // Unclosed indefinite array containing a closed definite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1945 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1946 | // Definite length array containing an unclosed indefinite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1947 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1948 | // Unclosed indefinite map containing a closed definite length array |
| 1949 | { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
| 1950 | // Definite length map containing an unclosed indefinite length array |
| 1951 | { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1952 | // Deeply nested definite length arrays with deepest one unclosed |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1953 | { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1954 | // Deeply nested indefinite length arrays with deepest one unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1955 | { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1956 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1957 | { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1958 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1959 | { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1960 | // Unclosed indefinite length map in definite length maps |
| 1961 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8}, |
| 1962 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1963 | // Unclosed definite length map in indefinite length maps |
| 1964 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1965 | // Unclosed indefinite length array in definite length maps |
| 1966 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8}, |
| 1967 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1968 | // Unclosed definite length array in indefinite length maps |
| 1969 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1970 | // Unclosed indefinite length map in definite length arrays |
| 1971 | { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1972 | // Unclosed definite length map in indefinite length arrays |
| 1973 | { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1974 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1975 | |
| 1976 | // The "argument" for the data item is incomplete |
| 1977 | // Positive integer missing 1 byte argument |
| 1978 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1979 | // Positive integer missing 2 byte argument |
| 1980 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1981 | // Positive integer missing 4 byte argument |
| 1982 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1983 | // Positive integer missing 8 byte argument |
| 1984 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1985 | // Positive integer missing 1 byte of 2 byte argument |
| 1986 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1987 | // Positive integer missing 2 bytes of 4 byte argument |
| 1988 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1989 | // Positive integer missing 1 bytes of 7 byte argument |
| 1990 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1991 | // Negative integer missing 1 byte argument |
| 1992 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1993 | // Binary string missing 1 byte argument |
| 1994 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1995 | // Text string missing 1 byte argument |
| 1996 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 1997 | // Array missing 1 byte argument |
| 1998 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 1999 | // Map missing 1 byte argument |
| 2000 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 2001 | // Tag missing 1 byte argument |
| 2002 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 2003 | // Simple missing 1 byte argument |
| 2004 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2005 | // half-precision with 1 byte argument |
| 2006 | { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2007 | // single-precision with 2 byte argument |
| 2008 | { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 2009 | // double-precision with 3 byte argument |
| 2010 | { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 2011 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2012 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2013 | // Tag with no content |
| 2014 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2015 | #else /* QCBOR_DISABLE_TAGS */ |
| 2016 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_TAGS_DISABLED }, |
| 2017 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2018 | |
| 2019 | // Breaks must not occur in definite length arrays and maps |
| 2020 | // Array of length 1 with sole member replaced by a break |
| 2021 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2022 | // Array of length 2 with 2nd member replaced by a break |
| 2023 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2024 | // Map of length 1 with sole member label replaced by a break |
| 2025 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2026 | // Map of length 1 with sole member label replaced by break |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2027 | // Alternate representation that some decoders handle differently |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2028 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2029 | // Array of length 1 with 2nd member value replaced by a break |
| 2030 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2031 | // Map of length 2 with 2nd member replaced by a break |
| 2032 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 2033 | |
| 2034 | |
| 2035 | // Breaks must not occur on their own out of an indefinite length data item |
| 2036 | // A bare break is not well formed |
| 2037 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 2038 | // A bare break after a zero length definite length array |
| 2039 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2040 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2041 | // A bare break after a zero length indefinite length map |
| 2042 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2043 | // A break inside a definite length array inside an indefenite length array |
| 2044 | { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2045 | // Complicated mixed nesting with break outside indefinite length array |
| 2046 | { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2047 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2048 | |
| 2049 | |
| 2050 | // Forbidden two byte encodings of simple types |
| 2051 | // Must use 0xe0 instead |
| 2052 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2053 | // Should use 0xe1 instead |
| 2054 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2055 | // Should use 0xe2 instead |
| 2056 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2057 | // Should use 0xe3 instead |
| 2058 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2059 | // Should use 0xe4 instead |
| 2060 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2061 | // Should use 0xe5 instead |
| 2062 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2063 | // Should use 0xe6 instead |
| 2064 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2065 | // Should use 0xe7 instead |
| 2066 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2067 | // Should use 0xe8 instead |
| 2068 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2069 | // Should use 0xe9 instead |
| 2070 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2071 | // Should use 0xea instead |
| 2072 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2073 | // Should use 0xeb instead |
| 2074 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2075 | // Should use 0xec instead |
| 2076 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2077 | // Should use 0xed instead |
| 2078 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2079 | // Should use 0xee instead |
| 2080 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2081 | // Should use 0xef instead |
| 2082 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2083 | // Should use 0xf0 instead |
| 2084 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2085 | // Should use 0xf1 instead |
| 2086 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2087 | // Should use 0xf2 instead |
| 2088 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2089 | // Must use 0xf3 instead |
| 2090 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2091 | // Must use 0xf4 instead |
| 2092 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2093 | // Must use 0xf5 instead |
| 2094 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2095 | // Must use 0xf6 instead |
| 2096 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2097 | // Must use 0xf7 instead |
| 2098 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2099 | // Must use 0xf8 instead |
| 2100 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2101 | // Reserved |
| 2102 | { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2103 | |
| 2104 | // Integers with additional info indefinite length |
| 2105 | // Positive integer with additional info indefinite length |
| 2106 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2107 | // Negative integer with additional info indefinite length |
| 2108 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2109 | |
| 2110 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2111 | // CBOR tag with "argument" an indefinite length |
| 2112 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 2113 | // CBOR tag with "argument" an indefinite length alternate vector |
| 2114 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2115 | #else /* QCBOR_DISABLE_TAGS */ |
| 2116 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED }, |
| 2117 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_TAGS_DISABLED }, |
| 2118 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2119 | |
| 2120 | // Missing bytes from a deterministic length string |
| 2121 | // A byte string is of length 1 without the 1 byte |
| 2122 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 2123 | // A text string is of length 1 without the 1 byte |
| 2124 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2125 | |
| 2126 | #if SIZE_MAX > 2147483647 |
Laurence Lundblade | 42272e4 | 2020-01-31 07:50:53 -0800 | [diff] [blame] | 2127 | // Byte string should have 2^32-15 bytes, but has one |
| 2128 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2129 | // Byte string should have 2^32-15 bytes, but has one |
| 2130 | { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2131 | // Byte string should have 2^64 bytes, but has 3 |
| 2132 | { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2133 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2134 | // Text string should have 2^64 bytes, but has 3 |
| 2135 | { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2136 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2137 | #else |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2138 | // Byte string should have 2^32-15 bytes, but has one |
| 2139 | { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2140 | // Byte string should have 2^32-15 bytes, but has one |
| 2141 | { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2142 | // Byte string should have 2^16 bytes, but has 3 |
| 2143 | { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2144 | // Text string should have 2^64 bytes, but has 3 |
| 2145 | { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2146 | #endif |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2147 | |
| 2148 | // Use of unassigned additional information values |
| 2149 | // Major type positive integer with reserved value 28 |
| 2150 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2151 | // Major type positive integer with reserved value 29 |
| 2152 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2153 | // Major type positive integer with reserved value 30 |
| 2154 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2155 | // Major type negative integer with reserved value 28 |
| 2156 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2157 | // Major type negative integer with reserved value 29 |
| 2158 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2159 | // Major type negative integer with reserved value 30 |
| 2160 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2161 | // Major type byte string with reserved value 28 length |
| 2162 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2163 | // Major type byte string with reserved value 29 length |
| 2164 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2165 | // Major type byte string with reserved value 30 length |
| 2166 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2167 | // Major type text string with reserved value 28 length |
| 2168 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2169 | // Major type text string with reserved value 29 length |
| 2170 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2171 | // Major type text string with reserved value 30 length |
| 2172 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2173 | // Major type array with reserved value 28 length |
| 2174 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2175 | // Major type array with reserved value 29 length |
| 2176 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2177 | // Major type array with reserved value 30 length |
| 2178 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2179 | // Major type map with reserved value 28 length |
| 2180 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2181 | // Major type map with reserved value 29 length |
| 2182 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2183 | // Major type map with reserved value 30 length |
| 2184 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2185 | // Major type tag with reserved value 28 length |
| 2186 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2187 | // Major type tag with reserved value 29 length |
| 2188 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2189 | // Major type tag with reserved value 30 length |
| 2190 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2191 | // Major type simple with reserved value 28 length |
| 2192 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2193 | // Major type simple with reserved value 29 length |
| 2194 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2195 | // Major type simple with reserved value 30 length |
| 2196 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2197 | |
| 2198 | |
| 2199 | // Maps must have an even number of data items (key & value) |
| 2200 | // Map with 1 item when it should have 2 |
| 2201 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2202 | // Map with 3 item when it should have 4 |
| 2203 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2204 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2205 | // Map with 1 item when it should have 2 |
| 2206 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2207 | // Map with 3 item when it should have 4 |
| 2208 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2209 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2210 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2211 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2212 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2213 | // Text-based date, with an integer |
| 2214 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2215 | // Epoch date, with an byte string |
| 2216 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2217 | // tagged as both epoch and string dates |
| 2218 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2219 | // big num tagged an int, not a byte string |
| 2220 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2221 | #else /* QCBOR_DISABLE_TAGS */ |
| 2222 | // Text-based date, with an integer |
| 2223 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED }, |
| 2224 | // Epoch date, with an byte string |
| 2225 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_TAGS_DISABLED }, |
| 2226 | // tagged as both epoch and string dates |
| 2227 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_TAGS_DISABLED }, |
| 2228 | // big num tagged an int, not a byte string |
| 2229 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED }, |
| 2230 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2231 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2232 | }; |
| 2233 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2234 | int32_t DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2235 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2236 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2237 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2238 | nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2239 | if(nResult) { |
| 2240 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2241 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2242 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2243 | // Corrupt the UsefulInputBuf and see that |
| 2244 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2245 | QCBORDecodeContext DCtx; |
| 2246 | QCBORItem Item; |
| 2247 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2248 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2249 | QCBORDecode_Init(&DCtx, |
| 2250 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2251 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2252 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2253 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2254 | return (int32_t)uQCBORError; |
| 2255 | } |
| 2256 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 2257 | // This wasn't supposed to happen |
| 2258 | return -1; |
| 2259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2260 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2261 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2262 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2263 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2264 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2265 | // Did not get back the error expected |
| 2266 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2267 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2268 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2269 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2270 | /* |
| 2271 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 2272 | tests here can be performed to see that the max length |
| 2273 | error check works correctly. See DecodeBytes(). If the max |
| 2274 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2275 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2276 | This test will automatocally adapt the all CPU sizes |
| 2277 | through the use of SIZE_MAX. |
| 2278 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2279 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2280 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2281 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2282 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2283 | // This makes a CBOR head with a text string that is very long |
| 2284 | // but doesn't fill in the bytes of the text string as that is |
| 2285 | // not needed to test this part of QCBOR. |
| 2286 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 2287 | |
| 2288 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 2289 | |
| 2290 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2291 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2292 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2293 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2294 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2298 | /* Try all 256 values of the byte at nLen including recursing for |
| 2299 | each of the values to try values at nLen+1 ... up to nLenMax |
| 2300 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2301 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2302 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2303 | if(nLen >= nLenMax) { |
| 2304 | return; |
| 2305 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2306 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2307 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 2308 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2309 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2310 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2311 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2312 | // Get ready to parse |
| 2313 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2314 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2315 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2316 | // Parse by getting the next item until an error occurs |
| 2317 | // Just about every possible decoder error can occur here |
| 2318 | // The goal of this test is not to check for the correct |
| 2319 | // error since that is not really possible. It is to |
| 2320 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2321 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2322 | QCBORItem Item; |
| 2323 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2324 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2325 | break; |
| 2326 | } |
| 2327 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2328 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2329 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2334 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2335 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2336 | // Size 2 tests 64K inputs and runs quickly |
| 2337 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2338 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2339 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2340 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2341 | return 0; |
| 2342 | } |
| 2343 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2344 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2345 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2346 | { |
| 2347 | // size 3 tests 16 million inputs and runs OK |
| 2348 | // in seconds on fast machines. Size 4 takes |
| 2349 | // 10+ minutes and 5 half a day on fast |
| 2350 | // machines. This test is kept separate from |
| 2351 | // the others so as to no slow down the use |
| 2352 | // of them as a very frequent regression. |
| 2353 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2354 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2355 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2356 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2357 | return 0; |
| 2358 | } |
| 2359 | |
| 2360 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2361 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2362 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2363 | 0xc0, // tag for string date |
| 2364 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2365 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2366 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2367 | 0xc0, // tag for string date |
| 2368 | 0x00, // Wrong type for a string date |
| 2369 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2370 | /* 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] | 2371 | 0xc1, // tag for epoch date |
| 2372 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2373 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2374 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2375 | 0xc1, |
| 2376 | 0x62, 'h', 'i', // wrong type tagged |
| 2377 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2378 | /* 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] | 2379 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2380 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2381 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2382 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2383 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2384 | 0xc1, // tag for epoch date |
| 2385 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2386 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2387 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2388 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2389 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2390 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2391 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2392 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2393 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2394 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2395 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2396 | 0xc1, // tag for epoch date |
| 2397 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2398 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2399 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2400 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2401 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2402 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2403 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2404 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2405 | 0xc1, // tag for epoch date |
| 2406 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 2407 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2408 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2409 | 0xc1, |
| 2410 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 2411 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2412 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2413 | 0xc1, // tag for epoch date |
| 2414 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2415 | }; |
| 2416 | |
| 2417 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2418 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2419 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2420 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 2421 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2422 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2423 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2424 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2425 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2426 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2427 | return diff > 0.0000001; |
| 2428 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2429 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2430 | |
| 2431 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2432 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2433 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2434 | { |
| 2435 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2436 | QCBORItem Item; |
| 2437 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2438 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2439 | QCBORDecode_Init(&DCtx, |
| 2440 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2441 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2442 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2443 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2444 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2445 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2446 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2447 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2448 | UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2449 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2450 | } |
| 2451 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2452 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2453 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2454 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2455 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2458 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 2459 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2460 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2461 | return -4; |
| 2462 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2463 | if(uError == QCBOR_SUCCESS) { |
| 2464 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2465 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2466 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2467 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2468 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2469 | ) { |
| 2470 | return -5; |
| 2471 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2474 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2475 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 2476 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2477 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2478 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2479 | /* 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] | 2480 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 2481 | // The date is decoded and the two tags are returned. This is to |
| 2482 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2483 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2484 | return -7; |
| 2485 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2486 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2487 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2488 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2489 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2490 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2491 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2492 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2493 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2494 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2495 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2496 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2497 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2498 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2499 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2500 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 2501 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2502 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2503 | return -10; |
| 2504 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2505 | if(uError == QCBOR_SUCCESS) { |
| 2506 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2507 | Item.val.epochDate.nSeconds != 1 |
| 2508 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2509 | || 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] | 2510 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2511 | ) { |
| 2512 | return -11; |
| 2513 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2514 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2515 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2516 | /* 8. Epoch date with too-large single-precision float */ |
| 2517 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2518 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2519 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2520 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2521 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2522 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 2523 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2524 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2525 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2526 | } |
| 2527 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2528 | /* 10. Epoch date with largest supported double-precision value */ |
| 2529 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2530 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2531 | return -14; |
| 2532 | } |
| 2533 | if(uError == QCBOR_SUCCESS) { |
| 2534 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2535 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 2536 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2537 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 2538 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2539 | ) { |
| 2540 | return -14; |
| 2541 | } |
| 2542 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2543 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2544 | /* 11. Epoch date with single-precision NaN */ |
| 2545 | 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] | 2546 | return -15; |
| 2547 | } |
| 2548 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2549 | /* 12. Epoch date with double-precision plus infinity */ |
| 2550 | 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] | 2551 | return -16; |
| 2552 | } |
| 2553 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2554 | /* 13. Epoch date with half-precision negative infinity */ |
| 2555 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2556 | 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] | 2557 | return -17; |
| 2558 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2559 | |
| 2560 | return 0; |
| 2561 | } |
| 2562 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2563 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2564 | /* |
| 2565 | Test cases covered here. Some items cover more than one of these. |
| 2566 | positive integer (zero counts as a positive integer) |
| 2567 | negative integer |
| 2568 | half-precision float |
| 2569 | single-precision float |
| 2570 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2571 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2572 | float Overflow error |
| 2573 | Wrong type error for epoch |
| 2574 | Wrong type error for date string |
| 2575 | float disabled error |
| 2576 | half-precision disabled error |
| 2577 | -Infinity |
| 2578 | Slightly too large integer |
| 2579 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2580 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2581 | Get epoch by int |
| 2582 | Get string by int |
| 2583 | Get epoch by string |
| 2584 | Get string by string |
| 2585 | Fail to get epoch by wrong int label |
| 2586 | Fail to get string by wrong string label |
| 2587 | Fail to get epoch by string because it is invalid |
| 2588 | Fail to get epoch by int because it is invalid |
| 2589 | |
| 2590 | Untagged values |
| 2591 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2592 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2593 | 0x87, // array of 7 items |
| 2594 | |
| 2595 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 2596 | |
| 2597 | // Untagged integer 0 |
| 2598 | 0x08, |
| 2599 | 0x00, |
| 2600 | |
| 2601 | // Utagged date string with string label y |
| 2602 | 0x61, 0x79, |
| 2603 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 2604 | |
| 2605 | // Untagged single-precision float with value 3.14 with string label x |
| 2606 | 0x61, 0x78, |
| 2607 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 2608 | |
| 2609 | // Untagged half-precision float with value -2 |
| 2610 | 0x09, |
| 2611 | 0xF9, 0xC0, 0x00, |
| 2612 | |
| 2613 | /* Untagged date-only date string */ |
| 2614 | 0x18, 0x63, |
| 2615 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2616 | |
| 2617 | /* Untagged days-count epoch date */ |
| 2618 | 0x11, |
| 2619 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 2620 | |
| 2621 | // End of map, back to array |
| 2622 | |
| 2623 | 0xa7, // Open map of tagged items with labels |
| 2624 | |
| 2625 | 0x00, |
| 2626 | 0xc0, // tag for string date |
| 2627 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 2628 | |
| 2629 | |
| 2630 | 0x01, |
| 2631 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 2632 | 0xc1, // tag for epoch date |
| 2633 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2634 | |
| 2635 | 0x05, |
| 2636 | 0xc1, |
| 2637 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 2638 | |
| 2639 | |
| 2640 | 0x07, |
| 2641 | 0xc1, // tag for epoch date |
| 2642 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2643 | |
| 2644 | /* Tagged days-count epoch date */ |
| 2645 | 0x63, 0x53, 0x44, 0x45, |
| 2646 | 0xD8, 0x64, /* tag(100) */ |
| 2647 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 2648 | |
| 2649 | // Untagged -1000 with label z |
| 2650 | 0x61, 0x7a, |
| 2651 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 2652 | 0x39, 0x03, 0xe7, |
| 2653 | |
| 2654 | /* Tagged date-only date string */ |
| 2655 | 0x63, 0x53, 0x44, 0x53, |
| 2656 | 0xD9, 0x03, 0xEC, |
| 2657 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2658 | |
| 2659 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2660 | |
| 2661 | 0xc1, |
| 2662 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 2663 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2664 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2665 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 2666 | |
| 2667 | 0xc1, // tag for epoch date |
| 2668 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 2669 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2670 | // These two at the end because they are unrecoverable errors |
| 2671 | 0xc1, // tag for epoch date |
| 2672 | 0x80, // Erroneous empty array as content for date |
| 2673 | |
| 2674 | 0xc0, // tag for string date |
| 2675 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2676 | }; |
| 2677 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2678 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2679 | { |
| 2680 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2681 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2682 | int64_t nEpochDate3, nEpochDate5, |
| 2683 | nEpochDate4, nEpochDate6, |
| 2684 | nEpochDays2; |
| 2685 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2686 | |
| 2687 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2688 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2689 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2690 | |
| 2691 | /* Items are in an array or map to test look up by label and other |
| 2692 | * that might not occur in isolated items. But it does make the |
| 2693 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2694 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2695 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2696 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2697 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2698 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2699 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2700 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2701 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2702 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2703 | return 104; |
| 2704 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2705 | if(uError == QCBOR_SUCCESS) { |
| 2706 | if(nEpochDate5 != 3) { |
| 2707 | return 103; |
| 2708 | } |
| 2709 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2710 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2711 | // A half-precision date with value -2 FFF |
| 2712 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2713 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2714 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2715 | 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] | 2716 | return 106; |
| 2717 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2718 | if(uError == QCBOR_SUCCESS) { |
| 2719 | if(nEpochDate4 != -2) { |
| 2720 | return 105; |
| 2721 | } |
| 2722 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2723 | |
| 2724 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2725 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 2726 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2727 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2728 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2729 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2730 | return 107; |
| 2731 | } |
| 2732 | |
| 2733 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2734 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2735 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2736 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2737 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2738 | return 108; |
| 2739 | } |
| 2740 | |
| 2741 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2742 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 2743 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2744 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2745 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2746 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2747 | return 109; |
| 2748 | } |
| 2749 | |
| 2750 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2751 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2752 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2753 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2754 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2755 | return 110; |
| 2756 | } |
| 2757 | |
| 2758 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2759 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2760 | |
| 2761 | // Untagged integer 0 |
| 2762 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2763 | &nEpochDate3); |
| 2764 | // Untagged date string |
| 2765 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2766 | &StringDate2); |
| 2767 | |
| 2768 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2769 | &StringDays2); |
| 2770 | |
| 2771 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2772 | &nEpochDays2); |
| 2773 | |
| 2774 | QCBORDecode_ExitMap(&DC); |
| 2775 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 2776 | return 3001; |
| 2777 | } |
| 2778 | |
| 2779 | // The map of tagged items |
| 2780 | QCBORDecode_EnterMap(&DC, NULL); |
| 2781 | |
| 2782 | #ifndef QCBOR_DISABLE_TAGS |
| 2783 | int64_t nEpochDate2, |
| 2784 | nEpochDateFail, |
| 2785 | nEpochDate1400000000, nEpochDays1; |
| 2786 | UsefulBufC StringDays1; |
| 2787 | uint64_t uTag1, uTag2; |
| 2788 | |
| 2789 | // Tagged date string |
| 2790 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2791 | &StringDate1); |
| 2792 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2793 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2794 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2795 | 1, |
| 2796 | QCBOR_TAG_REQUIREMENT_TAG | |
| 2797 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2798 | &nEpochDate1400000000); |
| 2799 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2800 | |
| 2801 | // Get largest negative double precision epoch date allowed |
| 2802 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2803 | 5, |
| 2804 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 2805 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2806 | &nEpochDate2); |
| 2807 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2808 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2809 | return 102; |
| 2810 | } |
| 2811 | if(uError == QCBOR_SUCCESS) { |
| 2812 | if(nEpochDate2 != -9223372036854773760LL) { |
| 2813 | return 101; |
| 2814 | } |
| 2815 | } |
| 2816 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2817 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2818 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 2819 | "z", |
| 2820 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 2821 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2822 | &nEpochDate6); |
| 2823 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2824 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2825 | |
| 2826 | // Get largest double precision epoch date allowed |
| 2827 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2828 | &nEpochDate2); |
| 2829 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2830 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2831 | return 112; |
| 2832 | } |
| 2833 | if(uError == QCBOR_SUCCESS) { |
| 2834 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 2835 | return 111; |
| 2836 | } |
| 2837 | } |
| 2838 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2839 | /* The days format is much simpler than the date format |
| 2840 | * because it can't be a floating point value. The test |
| 2841 | * of the spiffy decode functions sufficiently covers |
| 2842 | * the test of the non-spiffy decode days date decoding. |
| 2843 | * There is no full fan out of the error conditions |
| 2844 | * and decode options as that is implemented by code |
| 2845 | * that is tested well by the date testing above. |
| 2846 | */ |
| 2847 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 2848 | &StringDays1); |
| 2849 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2850 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 2851 | &nEpochDays1); |
| 2852 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2853 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2854 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 2855 | return 3001; |
| 2856 | } |
| 2857 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2858 | // Too-negative float, -9.2233720368547748E+18 |
| 2859 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2860 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2861 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 2862 | return 1111; |
| 2863 | } |
| 2864 | |
| 2865 | // Too-large integer |
| 2866 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2867 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2868 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 2869 | return 1; |
| 2870 | } |
| 2871 | |
| 2872 | // Half-precision minus infinity |
| 2873 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2874 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2875 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 2876 | return 2; |
| 2877 | } |
| 2878 | |
| 2879 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2880 | // Bad content for epoch date |
| 2881 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2882 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2883 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 2884 | return 3; |
| 2885 | } |
| 2886 | |
| 2887 | // Bad content for string date |
| 2888 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 2889 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2890 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 2891 | return 4; |
| 2892 | } |
| 2893 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2894 | QCBORDecode_ExitArray(&DC); |
| 2895 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2896 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2897 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2898 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2899 | #else /* QCBOR_DISABLE_TAGS */ |
| 2900 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2901 | &StringDate1); |
| 2902 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2903 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 2904 | return 4; |
| 2905 | } |
| 2906 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2907 | |
| 2908 | |
| 2909 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2910 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2911 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2912 | return 200; |
| 2913 | } |
| 2914 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2915 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2916 | return 201; |
| 2917 | } |
| 2918 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2919 | if(nEpochDays1 != -10676) { |
| 2920 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2921 | } |
| 2922 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2923 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2924 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2925 | } |
| 2926 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2927 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2928 | return 204; |
| 2929 | } |
| 2930 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2931 | if(nEpochDate6 != -1000) { |
| 2932 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2935 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2936 | return 205; |
| 2937 | } |
| 2938 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2939 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2940 | |
| 2941 | if(nEpochDate3 != 0) { |
| 2942 | return 202; |
| 2943 | } |
| 2944 | |
| 2945 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2946 | return 206; |
| 2947 | } |
| 2948 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2949 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 2950 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2954 | return 208; |
| 2955 | } |
| 2956 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2957 | return 0; |
| 2958 | } |
| 2959 | |
| 2960 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2961 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2962 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2963 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2964 | 0x81, // Array of one |
| 2965 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 2966 | 0x82, // Array of two that is the faction 1/3 |
| 2967 | 0x01, |
| 2968 | 0x03, |
| 2969 | |
| 2970 | /* |
| 2971 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 2972 | */ |
| 2973 | 0xd8, 0xe1, |
| 2974 | 0xd8, 0xe2, |
| 2975 | 0xd8, 0xe3, |
| 2976 | 0xd8, 0xe4, |
| 2977 | 0xd8, 0xe5, |
| 2978 | 0x80, |
| 2979 | |
| 2980 | /* tag 10489608748473423768( |
| 2981 | 2442302356( |
| 2982 | 21590( |
| 2983 | 240( |
| 2984 | [])))) |
| 2985 | */ |
| 2986 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2987 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 2988 | 0xd9, 0x54, 0x56, |
| 2989 | 0xd8, 0xf0, |
| 2990 | 0x80, |
| 2991 | |
| 2992 | /* tag 21590( |
| 2993 | 10489608748473423768( |
| 2994 | 2442302357( |
| 2995 | 65534( |
| 2996 | [])))) |
| 2997 | */ |
| 2998 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 2999 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3000 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3001 | 0xd9, 0xff, 0xfe, |
| 3002 | 0x80, |
| 3003 | |
| 3004 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3005 | works in conjuntion with entries above. |
| 3006 | 269488144(269488145(269488146(269488147([])))) |
| 3007 | */ |
| 3008 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 3009 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 3010 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 3011 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 3012 | 0x80, |
| 3013 | |
| 3014 | /* An invalid decimal fraction with an additional tag */ |
| 3015 | 0xd9, 0xff, 0xfa, |
| 3016 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 3017 | 0x00, // the integer 0; should be a byte string |
| 3018 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3019 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3020 | /* |
| 3021 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3022 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3023 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3024 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3025 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3026 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3027 | /* |
| 3028 | DB 9192939495969798 # tag(10489608748473423768) |
| 3029 | D8 88 # tag(136) |
| 3030 | C6 # tag(6) |
| 3031 | C7 # tag(7) |
| 3032 | 80 # array(0) |
| 3033 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3034 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3035 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3036 | |
| 3037 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3038 | 55799(55799(55799({ |
| 3039 | 6(7(-23)): 5859837686836516696(7({ |
| 3040 | 7(-20): 11({ |
| 3041 | 17(-18): 17(17(17("Organization"))), |
| 3042 | 9(-17): 773("SSG"), |
| 3043 | -15: 16(17(6(7("Confusion")))), |
| 3044 | 17(-16): 17("San Diego"), |
| 3045 | 17(-14): 17("US") |
| 3046 | }), |
| 3047 | 23(-19): 19({ |
| 3048 | -11: 9({ |
| 3049 | -9: -7 |
| 3050 | }), |
| 3051 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 3052 | }) |
| 3053 | })), |
| 3054 | 16(-22): 23({ |
| 3055 | 11(8(7(-5))): 8(-3) |
| 3056 | }) |
| 3057 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3058 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3059 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3060 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 3061 | 0xc6, 0xc7, 0x36, |
| 3062 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 3063 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 3064 | 0xcb, 0xa5, |
| 3065 | 0xd1, 0x31, |
| 3066 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 3067 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 3068 | 0xc9, 0x30, |
| 3069 | 0xd9, 0x03, 0x05, 0x63, |
| 3070 | 0x53, 0x53, 0x47, |
| 3071 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3072 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 3073 | 0x69, |
| 3074 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3075 | 0xd1, 0x2f, |
| 3076 | 0xd1, 0x69, |
| 3077 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 3078 | 0xd1, 0x2d, |
| 3079 | 0xd1, 0x62, |
| 3080 | 0x55, 0x53, |
| 3081 | 0xd7, 0x32, |
| 3082 | 0xd3, 0xa2, |
| 3083 | 0x2a, |
| 3084 | 0xc9, 0xa1, |
| 3085 | 0x28, |
| 3086 | 0x26, |
| 3087 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 3088 | 0xcc, 0x4a, |
| 3089 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 3090 | 0xd0, 0x35, |
| 3091 | 0xd7, 0xa1, |
| 3092 | 0xcb, 0xc8, 0xc7, 0x24, |
| 3093 | 0xc8, 0x22}; |
| 3094 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3095 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3096 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3097 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3098 | |
| 3099 | 0xc0, // tag for string date |
| 3100 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3101 | |
| 3102 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3103 | |
| 3104 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 3105 | |
| 3106 | 0xd8, 0x23, // tag for regex |
| 3107 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3108 | |
| 3109 | 0xc0, // tag for string date |
| 3110 | 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] | 3111 | |
| 3112 | // This last case makes the array untraversable because it is |
| 3113 | // an uncrecoverable error. Make sure it stays last and is the only |
| 3114 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3115 | }; |
| 3116 | |
| 3117 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3118 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3119 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3120 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3121 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3122 | { |
| 3123 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3124 | QCBORItem Item; |
| 3125 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3126 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3127 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3128 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3129 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3130 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3131 | /* |
| 3132 | This test matches the magic number tag and the fraction tag |
| 3133 | 55799([...]) |
| 3134 | */ |
| 3135 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3136 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3137 | return -2; |
| 3138 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3139 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3140 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 3141 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3142 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3143 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3144 | /* |
| 3145 | 4([1,3]) |
| 3146 | */ |
| 3147 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3148 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3149 | if(uError != QCBOR_SUCCESS || |
| 3150 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3151 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 3152 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 3153 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3154 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3155 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3156 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 3157 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3158 | return -4; |
| 3159 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3160 | // consume the items in the array |
| 3161 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3162 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3163 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3164 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3165 | if(uError != QCBOR_SUCCESS || |
| 3166 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3167 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 3168 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3169 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3170 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3171 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 3172 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3173 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3174 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3175 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3176 | /* |
| 3177 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3178 | */ |
| 3179 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3180 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3181 | return -6; |
| 3182 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3183 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 3184 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 3185 | return -106; |
| 3186 | } |
| 3187 | |
| 3188 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3189 | /* tag 10489608748473423768( |
| 3190 | 2442302356( |
| 3191 | 21590( |
| 3192 | 240( |
| 3193 | [])))) |
| 3194 | */ |
| 3195 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3196 | if(uError != QCBOR_SUCCESS || |
| 3197 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3198 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 3199 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 3200 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 3201 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3202 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /* tag 21590( |
| 3206 | 10489608748473423768( |
| 3207 | 2442302357( |
| 3208 | 21591( |
| 3209 | [])))) |
| 3210 | */ |
| 3211 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3212 | if(uError != QCBOR_SUCCESS || |
| 3213 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3214 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 3215 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 3216 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 3217 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 3218 | return -8; |
| 3219 | } |
| 3220 | |
| 3221 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3222 | works in conjuntion with entries above. |
| 3223 | 269488144(269488145(269488146(269488147([])))) |
| 3224 | */ |
| 3225 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3226 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3227 | return -9; |
| 3228 | } |
| 3229 | |
| 3230 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3231 | if(uError == QCBOR_SUCCESS) { |
| 3232 | return -10; |
| 3233 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3234 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3235 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3236 | // 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] | 3237 | // tage and then matches it. Caller-config lists are no longer |
| 3238 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3239 | QCBORDecode_Init(&DCtx, |
| 3240 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3241 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3242 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 3243 | const QCBORTagListIn TL = {2, puList}; |
| 3244 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3245 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3246 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3247 | return -8; |
| 3248 | } |
| 3249 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3250 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 3251 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 3252 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 3253 | Item.val.uCount != 0) { |
| 3254 | return -9; |
| 3255 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3256 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3257 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3258 | // 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] | 3259 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3260 | const uint64_t puLongList[17] = {1,2,1}; |
| 3261 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3262 | QCBORDecode_Init(&DCtx, |
| 3263 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3264 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3265 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 3266 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3267 | return -11; |
| 3268 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3269 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3270 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3271 | QCBORTagListOut Out = {0, 4, puTags}; |
| 3272 | |
| 3273 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3274 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3275 | QCBORDecode_Init(&DCtx, |
| 3276 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3277 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3278 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3279 | return -12; |
| 3280 | } |
| 3281 | if(puTags[0] != 0x9192939495969798 || |
| 3282 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3283 | puTags[2] != 0x06 || |
| 3284 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3285 | return -13; |
| 3286 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3287 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3288 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3289 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3290 | QCBORDecode_Init(&DCtx, |
| 3291 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3292 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3293 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 3294 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3295 | return -14; |
| 3296 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3297 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3298 | |
| 3299 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3300 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3301 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 3302 | // It is a bit of a messy test and maybe could be improved, but |
| 3303 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3304 | QCBORDecode_Init(&DCtx, |
| 3305 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3306 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3307 | int n = CheckCSRMaps(&DCtx); |
| 3308 | if(n) { |
| 3309 | return n-2000; |
| 3310 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3311 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3312 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3313 | QCBORDecode_Init(&DCtx, |
| 3314 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3315 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3316 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3317 | /* With the spiffy decode revision, this tag list is not used. |
| 3318 | It doesn't matter if a tag is in this list or not so some |
| 3319 | tests that couldn't process a tag because it isn't in this list |
| 3320 | now can process these unlisted tags. The tests have been |
| 3321 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3322 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 3323 | const QCBORTagListIn TagList = {3, puTagList}; |
| 3324 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3325 | |
| 3326 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3327 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3328 | return -100; |
| 3329 | } |
| 3330 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3331 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3332 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 3333 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 3334 | Item.val.uCount != 2 || |
| 3335 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 3336 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 3337 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 3338 | Out.uNumUsed != 3) { |
| 3339 | return -101; |
| 3340 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3341 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3342 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3343 | return -102; |
| 3344 | } |
| 3345 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3346 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3347 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3348 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3349 | Item.val.uCount != 2 || |
| 3350 | puTags[0] != 5859837686836516696 || |
| 3351 | puTags[1] != 7 || |
| 3352 | Out.uNumUsed != 2) { |
| 3353 | return -103; |
| 3354 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3355 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3356 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3357 | return -104; |
| 3358 | } |
| 3359 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3360 | Item.val.uCount != 5 || |
| 3361 | puTags[0] != 0x0b || |
| 3362 | Out.uNumUsed != 1) { |
| 3363 | return -105; |
| 3364 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3365 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3366 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3367 | return -106; |
| 3368 | } |
| 3369 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3370 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 3371 | Item.val.string.len != 12 || |
| 3372 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 3373 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 3374 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 3375 | Out.uNumUsed != 3) { |
| 3376 | return -105; |
| 3377 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3378 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3379 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3380 | return -107; |
| 3381 | } |
| 3382 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3383 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 3384 | Item.val.string.len != 3 || |
| 3385 | puTags[0] != 773 || |
| 3386 | Out.uNumUsed != 1) { |
| 3387 | return -108; |
| 3388 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3389 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3390 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3391 | return -109; |
| 3392 | } |
| 3393 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3394 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3395 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3396 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3397 | puTags[3] != 7 || |
| 3398 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3399 | return -110; |
| 3400 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3401 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3402 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3403 | return -111; |
| 3404 | } |
| 3405 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3406 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3407 | Item.val.string.len != 9 || |
| 3408 | puTags[0] != 17 || |
| 3409 | Out.uNumUsed != 1) { |
| 3410 | return -112; |
| 3411 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3412 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3413 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3414 | return -111; |
| 3415 | } |
| 3416 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3417 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3418 | Item.val.string.len != 2 || |
| 3419 | puTags[0] != 17 || |
| 3420 | Out.uNumUsed != 1) { |
| 3421 | return -112; |
| 3422 | } |
| 3423 | |
| 3424 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3425 | return -113; |
| 3426 | } |
| 3427 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3428 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3429 | Item.val.uCount != 2 || |
| 3430 | puTags[0] != 19 || |
| 3431 | Out.uNumUsed != 1) { |
| 3432 | return -114; |
| 3433 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3434 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3435 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3436 | return -115; |
| 3437 | } |
| 3438 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3439 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3440 | Item.val.uCount != 1 || |
| 3441 | puTags[0] != 9 || |
| 3442 | Out.uNumUsed != 1) { |
| 3443 | return -116; |
| 3444 | } |
| 3445 | |
| 3446 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3447 | return -116; |
| 3448 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3449 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3450 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3451 | Out.uNumUsed != 0) { |
| 3452 | return -117; |
| 3453 | } |
| 3454 | |
| 3455 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3456 | return -118; |
| 3457 | } |
| 3458 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 3459 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3460 | puTags[0] != 12 || |
| 3461 | Out.uNumUsed != 1) { |
| 3462 | return -119; |
| 3463 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3464 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3465 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3466 | return -120; |
| 3467 | } |
| 3468 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3469 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 3470 | Item.val.uCount != 1 || |
| 3471 | puTags[0] != 0x17 || |
| 3472 | Out.uNumUsed != 1) { |
| 3473 | return -121; |
| 3474 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3475 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3476 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3477 | return -122; |
| 3478 | } |
| 3479 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3480 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3481 | Item.val.int64 != -3 || |
| 3482 | puTags[0] != 8 || |
| 3483 | Out.uNumUsed != 1) { |
| 3484 | return -123; |
| 3485 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3486 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3487 | if(QCBORDecode_Finish(&DCtx)) { |
| 3488 | return -124; |
| 3489 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3490 | |
| 3491 | UsefulBufC DateString; |
| 3492 | QCBORDecode_Init(&DCtx, |
| 3493 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3494 | QCBOR_DECODE_MODE_NORMAL); |
| 3495 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3496 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3497 | // tagged date string |
| 3498 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3499 | // untagged date string |
| 3500 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3501 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3502 | return 100; |
| 3503 | } |
| 3504 | // untagged byte string |
| 3505 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3506 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3507 | return 101; |
| 3508 | } |
| 3509 | // tagged regex |
| 3510 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3511 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3512 | return 102; |
| 3513 | } |
| 3514 | // tagged date string with a byte string |
| 3515 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3516 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3517 | return 103; |
| 3518 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3519 | // The exit errors out because the last item, the date string with |
| 3520 | // bad content makes the array untraversable (the bad date string |
| 3521 | // could have tag content of an array or such that is not consumed |
| 3522 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3523 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3524 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3525 | return 104; |
| 3526 | } |
| 3527 | |
| 3528 | |
| 3529 | QCBORDecode_Init(&DCtx, |
| 3530 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3531 | QCBOR_DECODE_MODE_NORMAL); |
| 3532 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3533 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3534 | // tagged date string |
| 3535 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3536 | // untagged date string |
| 3537 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3538 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3539 | return 200; |
| 3540 | } |
| 3541 | // untagged byte string |
| 3542 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3543 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3544 | return 201; |
| 3545 | } |
| 3546 | // tagged regex |
| 3547 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3548 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3549 | return 202; |
| 3550 | } |
| 3551 | // tagged date string with a byte string |
| 3552 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3553 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3554 | return 203; |
| 3555 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3556 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3557 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3558 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3559 | return 204; |
| 3560 | } |
| 3561 | |
| 3562 | QCBORDecode_Init(&DCtx, |
| 3563 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3564 | QCBOR_DECODE_MODE_NORMAL); |
| 3565 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3566 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3567 | // tagged date string |
| 3568 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3569 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3570 | return 300; |
| 3571 | } |
| 3572 | // untagged date string |
| 3573 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3574 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3575 | return 301; |
| 3576 | } |
| 3577 | // untagged byte string |
| 3578 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3579 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3580 | return 302; |
| 3581 | } |
| 3582 | // tagged regex |
| 3583 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3584 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3585 | return 303; |
| 3586 | } |
| 3587 | // tagged date string with a byte string |
| 3588 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3589 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3590 | return 304; |
| 3591 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3592 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3593 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3594 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3595 | return 305; |
| 3596 | } |
| 3597 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3598 | return 0; |
| 3599 | } |
| 3600 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3601 | /* |
| 3602 | * These are showing the big numbers converted to integers. |
| 3603 | * The tag numbers are not shown. |
| 3604 | * |
| 3605 | * [ 18446744073709551616, |
| 3606 | * -18446744073709551617, |
| 3607 | * {"BN+": 18446744073709551616, |
| 3608 | * 64: 18446744073709551616, |
| 3609 | * "BN-": -18446744073709551617, |
| 3610 | * -64: -18446744073709551617 |
| 3611 | * } |
| 3612 | * ] |
| 3613 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3614 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3615 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3616 | 0x83, |
| 3617 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3618 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3619 | 0xA4, |
| 3620 | 0x63, 0x42, 0x4E, 0x2B, |
| 3621 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3622 | 0x18, 0x40, |
| 3623 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3624 | 0x63, 0x42, 0x4E, 0x2D, |
| 3625 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3626 | 0x38, 0x3F, |
| 3627 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 3628 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3629 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3630 | /* The expected big num */ |
| 3631 | static const uint8_t spBigNum[] = { |
| 3632 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3633 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3634 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3635 | |
| 3636 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3637 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3638 | { |
| 3639 | QCBORDecodeContext DCtx; |
| 3640 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3641 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3642 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3643 | QCBORDecode_Init(&DCtx, |
| 3644 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 3645 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3646 | |
| 3647 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3648 | // |
| 3649 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 3650 | return -1; |
| 3651 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3652 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3653 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3654 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3655 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3656 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3657 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3658 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3659 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3660 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3661 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | // |
| 3665 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3666 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3667 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3668 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3669 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3670 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3671 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3672 | // |
| 3673 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3674 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3675 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3676 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3677 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3678 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3679 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3680 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3681 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3682 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3683 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3684 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3688 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3689 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3690 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3691 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3692 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3693 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3694 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3695 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3696 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3697 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3698 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3699 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3700 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3701 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3702 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3703 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3704 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3705 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3706 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3707 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3708 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3709 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3710 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3711 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3712 | #else |
| 3713 | |
| 3714 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 3715 | return -100; |
| 3716 | } |
| 3717 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3718 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3719 | return 0; |
| 3720 | } |
| 3721 | |
| 3722 | |
| 3723 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3724 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3725 | uint8_t uDataType, |
| 3726 | uint8_t uNestingLevel, |
| 3727 | uint8_t uNextNest, |
| 3728 | int64_t nLabel, |
| 3729 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3730 | { |
| 3731 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3732 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3733 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3734 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 3735 | if(Item.uDataType != uDataType) return -1; |
| 3736 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3737 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 3738 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 3739 | return -1; |
| 3740 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3741 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3742 | if(Item.label.int64 != nLabel) return -1; |
| 3743 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3744 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3745 | } |
| 3746 | } |
| 3747 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3748 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3749 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3750 | if(pItem) { |
| 3751 | *pItem = Item; |
| 3752 | } |
| 3753 | return 0; |
| 3754 | } |
| 3755 | |
| 3756 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3757 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3758 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3759 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3760 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3761 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3762 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3763 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3764 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3765 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3766 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 3767 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 3768 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 3769 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 3770 | 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] | 3771 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3772 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 3773 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3774 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3775 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 3776 | 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] | 3777 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3778 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 3779 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3780 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3781 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3782 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3783 | return 0; |
| 3784 | } |
| 3785 | |
| 3786 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3787 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3788 | { |
| 3789 | -23: { |
| 3790 | -20: { |
| 3791 | -18: "Organization", |
| 3792 | -17: "SSG", |
| 3793 | -15: "Confusion", |
| 3794 | -16: "San Diego", |
| 3795 | -14: "US" |
| 3796 | }, |
| 3797 | -19: { |
| 3798 | -11: { |
| 3799 | -9: -7 |
| 3800 | }, |
| 3801 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 3802 | } |
| 3803 | }, |
| 3804 | -22: { |
| 3805 | -5: -3 |
| 3806 | } |
| 3807 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3808 | */ |
| 3809 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3810 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 3811 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3812 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3813 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3814 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3815 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3816 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 3817 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 3818 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 3819 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3820 | // Same map as above, but using indefinite lengths |
| 3821 | static const uint8_t spCSRInputIndefLen[] = { |
| 3822 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 3823 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3824 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3825 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3826 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3827 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3828 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 3829 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 3830 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 3831 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 3832 | |
| 3833 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3834 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3835 | { |
| 3836 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3837 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3838 | QCBORDecode_Init(&DCtx, |
| 3839 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3840 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3841 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3842 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3843 | } |
| 3844 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3845 | |
| 3846 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3847 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3848 | { |
| 3849 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3850 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3851 | QCBORDecode_Init(&DCtx, |
| 3852 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3853 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3854 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3855 | QCBORItem Item; |
| 3856 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3857 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3858 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3859 | return -1; |
| 3860 | } |
| 3861 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3862 | return -2; |
| 3863 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3864 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3865 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3866 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 3867 | return -3; |
| 3868 | } |
| 3869 | |
| 3870 | return 0; |
| 3871 | } |
| 3872 | |
| 3873 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3874 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3875 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3876 | { |
| 3877 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3878 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3879 | QCBORDecode_Init(&DCtx, |
| 3880 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 3881 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3882 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3883 | return CheckCSRMaps(&DCtx); |
| 3884 | } |
| 3885 | |
| 3886 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3887 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3888 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 3889 | { |
| 3890 | UsefulOutBuf UOB; |
| 3891 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3892 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3893 | int i; |
| 3894 | for(i = 0; i < n; i++) { |
| 3895 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 3896 | } |
| 3897 | |
| 3898 | for(i = 0; i < n; i++) { |
| 3899 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3900 | } |
| 3901 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3902 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3903 | |
| 3904 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3905 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3906 | { |
| 3907 | QCBORDecodeContext DC; |
| 3908 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3909 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3910 | int j; |
| 3911 | for(j = 0; j < nNestLevel; j++) { |
| 3912 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3913 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3914 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 3915 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3916 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3917 | return -4; |
| 3918 | } else { |
| 3919 | return 0; // Decoding doesn't recover after an error |
| 3920 | } |
| 3921 | } else { |
| 3922 | // Should be no error |
| 3923 | if(nReturn) { |
| 3924 | return -9; // Should not have got an error |
| 3925 | } |
| 3926 | } |
| 3927 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3928 | return -7; |
| 3929 | } |
| 3930 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3931 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3932 | if(nReturn) { |
| 3933 | return -3; |
| 3934 | } |
| 3935 | return 0; |
| 3936 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3937 | |
| 3938 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3939 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3940 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3941 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3942 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3943 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3944 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3945 | int nReturn = parse_indeflen_nested(Nested, i); |
| 3946 | if(nReturn) { |
| 3947 | return nReturn; |
| 3948 | } |
| 3949 | } |
| 3950 | return 0; |
| 3951 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3952 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3953 | // [1, [2, 3]] |
| 3954 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 3955 | // No closing break |
| 3956 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 3957 | // Not enough closing breaks |
| 3958 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 3959 | // Too many closing breaks |
| 3960 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 3961 | // Unclosed indeflen inside def len |
| 3962 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 3963 | // confused tag |
| 3964 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3965 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3966 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3967 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3968 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3969 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3970 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3971 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3972 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3973 | QCBORDecodeContext DC; |
| 3974 | QCBORItem Item; |
| 3975 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3976 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3977 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3978 | |
| 3979 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3980 | Item.uNestingLevel != 0 || |
| 3981 | Item.uNextNestLevel != 1) { |
| 3982 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3983 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3984 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3985 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3986 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 3987 | Item.uNestingLevel != 1 || |
| 3988 | Item.uNextNestLevel != 1) { |
| 3989 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3990 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3991 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3992 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3993 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3994 | Item.uNestingLevel != 1 || |
| 3995 | Item.uNextNestLevel != 2) { |
| 3996 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3997 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3998 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3999 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4000 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4001 | Item.uNestingLevel != 2 || |
| 4002 | Item.uNextNestLevel != 2) { |
| 4003 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4004 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4005 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4006 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4007 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4008 | Item.uNestingLevel != 2 || |
| 4009 | Item.uNextNestLevel != 0) { |
| 4010 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4011 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4012 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4013 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4014 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4015 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4016 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4017 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4018 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4019 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4020 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4021 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4022 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4023 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4024 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4025 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4026 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4027 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4028 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4029 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4030 | } |
| 4031 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4032 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4033 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4034 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4035 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4036 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4037 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4038 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4039 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4040 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4044 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4045 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4046 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4047 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4048 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4049 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4050 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4051 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4052 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4053 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4054 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4055 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4056 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4057 | |
| 4058 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4059 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4060 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4061 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4062 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4063 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4064 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4065 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4066 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4067 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4068 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4069 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4070 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4071 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4072 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4073 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4074 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4075 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4076 | return -140; |
| 4077 | } |
| 4078 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4079 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4080 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4081 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4082 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4083 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4084 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4085 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4086 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4087 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4088 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4089 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4090 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4091 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4092 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4093 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4094 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4095 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4096 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4097 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4098 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4099 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4100 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4101 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4102 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4103 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4104 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4105 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4106 | |
| 4107 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4108 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4109 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4110 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4111 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4112 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4113 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4114 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4115 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4116 | #else /* QCBOR_DISABLE_TAGS */ |
| 4117 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 4118 | return -20; |
| 4119 | } |
| 4120 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4121 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4122 | return 0; |
| 4123 | } |
| 4124 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4125 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4126 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 4127 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4128 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4129 | 0x81, // Array of length one |
| 4130 | 0x7f, // text string marked with indefinite length |
| 4131 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4132 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4133 | 0xff // ending break |
| 4134 | }; |
| 4135 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4136 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4137 | 0x81, // Array of length one |
| 4138 | 0x7f, // text string marked with indefinite length |
| 4139 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4140 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 4141 | 0xff // ending break |
| 4142 | }; |
| 4143 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4144 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4145 | 0x81, // Array of length one |
| 4146 | 0x7f, // text string marked with indefinite length |
| 4147 | 0x01, 0x02, // Not a string |
| 4148 | 0xff // ending break |
| 4149 | }; |
| 4150 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4151 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4152 | 0x81, // Array of length one |
| 4153 | 0x7f, // text string marked with indefinite length |
| 4154 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4155 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4156 | // missing end of string |
| 4157 | }; |
| 4158 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4159 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4160 | 0xa1, // Array of length one |
| 4161 | 0x7f, // text string marked with indefinite length |
| 4162 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 4163 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4164 | 0xff, // ending break |
| 4165 | 0x01 // integer being labeled. |
| 4166 | }; |
| 4167 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4168 | /** |
| 4169 | Make an indefinite length string |
| 4170 | |
| 4171 | @param Storage Storage for string, must be 144 bytes in size |
| 4172 | @return The indefinite length string |
| 4173 | |
| 4174 | This makes an array with one indefinite length string that has 7 chunks |
| 4175 | from size of 1 byte up to 64 bytes. |
| 4176 | */ |
| 4177 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4178 | { |
| 4179 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4180 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4181 | UsefulOutBuf_Init(&UOB, Storage); |
| 4182 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 4183 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4184 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4185 | uint8_t uStringByte = 0; |
| 4186 | // Use of type int is intentional |
| 4187 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 4188 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4189 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4190 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 4191 | for(int j = 0; j < uChunkSize; j++) { |
| 4192 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 4193 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4194 | } |
| 4195 | } |
| 4196 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4197 | |
| 4198 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4199 | } |
| 4200 | |
| 4201 | static int CheckBigString(UsefulBufC BigString) |
| 4202 | { |
| 4203 | if(BigString.len != 255) { |
| 4204 | return 1; |
| 4205 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4206 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4207 | for(uint8_t i = 0; i < 255; i++){ |
| 4208 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 4209 | return 1; |
| 4210 | } |
| 4211 | } |
| 4212 | return 0; |
| 4213 | } |
| 4214 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4215 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4216 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4217 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4218 | QCBORDecodeContext DC; |
| 4219 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4220 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4221 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4222 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4223 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4224 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4225 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4226 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4227 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4228 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4229 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4230 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4231 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4232 | return -2; |
| 4233 | } |
| 4234 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 4235 | return -3; |
| 4236 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4237 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4238 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4239 | return -4; |
| 4240 | } |
| 4241 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 4242 | return -5; |
| 4243 | } |
| 4244 | if(QCBORDecode_Finish(&DC)) { |
| 4245 | return -6; |
| 4246 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4247 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4248 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4249 | QCBORDecode_Init(&DC, |
| 4250 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 4251 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4252 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4253 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4254 | return -7; |
| 4255 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4256 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4257 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4258 | return -8; |
| 4259 | } |
| 4260 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4261 | return -9; |
| 4262 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4263 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4264 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4265 | return -10; |
| 4266 | } |
| 4267 | |
| 4268 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4269 | QCBORDecode_Init(&DC, |
| 4270 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 4271 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4272 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4273 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4274 | return -11; |
| 4275 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4276 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4277 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4278 | return -12; |
| 4279 | } |
| 4280 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4281 | return -13; |
| 4282 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4283 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4284 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4285 | return -14; |
| 4286 | } |
| 4287 | |
| 4288 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4289 | QCBORDecode_Init(&DC, |
| 4290 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 4291 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4292 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4293 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4294 | return -15; |
| 4295 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4296 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4297 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4298 | return -16; |
| 4299 | } |
| 4300 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4301 | return -17; |
| 4302 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4303 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4304 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 4305 | return -18; |
| 4306 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4307 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4308 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4309 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4310 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4311 | QCBORDecode_GetNext(&DC, &Item); |
| 4312 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4313 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4314 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4315 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4316 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4317 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4318 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4319 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4320 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4321 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4322 | |
| 4323 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 4324 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4325 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4326 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4327 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4328 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4329 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4330 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4331 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4332 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 4333 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4334 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4335 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4336 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4337 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4338 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4339 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4340 | QCBORDecode_GetNext(&DC, &Item); |
| 4341 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4342 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4343 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4344 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4345 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4346 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4347 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4348 | // ---- big bstr ----- |
| 4349 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4350 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4351 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4352 | return -25; |
| 4353 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4354 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4355 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4356 | return -26; |
| 4357 | } |
| 4358 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4359 | return -26; |
| 4360 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4361 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4362 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4363 | return -27; |
| 4364 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4365 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4366 | return -28; |
| 4367 | } |
| 4368 | if(CheckBigString(Item.val.string)) { |
| 4369 | return -3; |
| 4370 | } |
| 4371 | if(QCBORDecode_Finish(&DC)) { |
| 4372 | return -29; |
| 4373 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4374 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4375 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4376 | 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] | 4377 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4378 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4379 | return -30; |
| 4380 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4381 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4382 | QCBORDecode_GetNext(&DC, &Item); |
| 4383 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4384 | return -31; |
| 4385 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4386 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4387 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 4388 | return -32; |
| 4389 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4390 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4391 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4392 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 4393 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 4394 | return -33; |
| 4395 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4396 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4397 | if(QCBORDecode_Finish(&DC)) { |
| 4398 | return -34; |
| 4399 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4400 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4401 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4405 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4406 | { |
| 4407 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4408 | QCBORError nCBORError; |
| 4409 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4410 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4411 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4412 | QCBORDecode_Init(&DC, |
| 4413 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4414 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4415 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4416 | 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] | 4417 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4418 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 4419 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4420 | return -1; |
| 4421 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4422 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4423 | if(CheckCSRMaps(&DC)) { |
| 4424 | return -2; |
| 4425 | } |
| 4426 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 4427 | // Next parse, save pointers to a few strings, destroy original and |
| 4428 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4429 | 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] | 4430 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4431 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4432 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 4433 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4434 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4435 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4436 | QCBORItem Item1, Item2, Item3, Item4; |
| 4437 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4438 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4439 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 4440 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4441 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4442 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4443 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4444 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4445 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4446 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4447 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4448 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4449 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4450 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 4451 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4452 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4453 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4454 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 4455 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4456 | Item1.uDataAlloc != 0 || |
| 4457 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4458 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4459 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4460 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4461 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4462 | |
| 4463 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4464 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4465 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4466 | Item2.uDataAlloc != 0 || |
| 4467 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4468 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4469 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4470 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4471 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4472 | Item3.uDataAlloc == 0 || |
| 4473 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4474 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4475 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4476 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4477 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4478 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4479 | Item4.uDataAlloc == 0 || |
| 4480 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4481 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4482 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4483 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4484 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4485 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4486 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4487 | QCBORDecode_Init(&DC, |
| 4488 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 4489 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4490 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 4491 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4492 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4493 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4494 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4495 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4496 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4497 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 4498 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 4499 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 4500 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 4501 | } |
| 4502 | } |
| 4503 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4504 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4505 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | return 0; |
| 4509 | } |
| 4510 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4511 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4512 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 4513 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4514 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4515 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4516 | QCBORDecodeContext DC; |
| 4517 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 4518 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4519 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4520 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4521 | // Then fish into the internals of the decode context |
| 4522 | // to get the allocator function so it can be called directly. |
| 4523 | // Also figure out how much pool is available for use |
| 4524 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4525 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4526 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 4527 | if(nError) { |
| 4528 | return -9; |
| 4529 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4530 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 4531 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4532 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4533 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4534 | // First test -- ask for one more byte than available and see failure |
| 4535 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4536 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 4537 | return -1; |
| 4538 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4539 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4540 | // Re do the set up for the next test that will do a successful alloc, |
| 4541 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4542 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4543 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4544 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4545 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4546 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4547 | // Allocate one byte less than available and see success |
| 4548 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4549 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4550 | return -2; |
| 4551 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4552 | // Ask for some more and see failure |
| 4553 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4554 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 4555 | return -3; |
| 4556 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4557 | // Free the first allocate, retry the second and see success |
| 4558 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 4559 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4560 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 4561 | return -4; |
| 4562 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4563 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4564 | // Re do set up for next test that involves a successful alloc, |
| 4565 | // and a successful realloc and a failed realloc |
| 4566 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4567 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4568 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4569 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4570 | // Allocate half the pool and see success |
| 4571 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4572 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4573 | return -5; |
| 4574 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4575 | // Reallocate to take up the whole pool and see success |
| 4576 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4577 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 4578 | return -6; |
| 4579 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4580 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4581 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 4582 | return -7; |
| 4583 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4584 | // Try to allocate more to be sure there is failure after a realloc |
| 4585 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 4586 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4587 | return -8; |
| 4588 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4589 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4590 | return 0; |
| 4591 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4592 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4593 | |
| 4594 | /* Just enough of an allocator to test configuration of one */ |
| 4595 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 4596 | { |
| 4597 | (void)pOldMem; // unused variable |
| 4598 | |
| 4599 | if(uNewSize) { |
| 4600 | // Assumes the context pointer is the buffer and |
| 4601 | // nothing too big will ever be asked for. |
| 4602 | // This is only good for this basic test! |
| 4603 | return (UsefulBuf) {pCtx, uNewSize}; |
| 4604 | } else { |
| 4605 | return NULLUsefulBuf; |
| 4606 | } |
| 4607 | } |
| 4608 | |
| 4609 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4610 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4611 | { |
| 4612 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4613 | // nothing can be done with it unless that is set up. |
| 4614 | QCBORDecodeContext DC; |
| 4615 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 4616 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 4617 | |
| 4618 | uint8_t pAllocatorBuffer[50]; |
| 4619 | |
| 4620 | // This is really just to test that this call works. |
| 4621 | // The full functionality of string allocators is tested |
| 4622 | // elsewhere with the MemPool internal allocator. |
| 4623 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 4624 | |
| 4625 | QCBORItem Item; |
| 4626 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 4627 | return -1; |
| 4628 | } |
| 4629 | |
| 4630 | if(Item.uDataAlloc == 0 || |
| 4631 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4632 | Item.val.string.ptr != pAllocatorBuffer) { |
| 4633 | return -2; |
| 4634 | } |
| 4635 | |
| 4636 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 4637 | return -3; |
| 4638 | } |
| 4639 | |
| 4640 | return 0; |
| 4641 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4642 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4643 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4644 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4645 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4646 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4647 | struct EaMTest { |
| 4648 | const char *szName; |
| 4649 | UsefulBufC Input; |
| 4650 | uint8_t uTagRequirement; |
| 4651 | bool bHasTags; |
| 4652 | |
| 4653 | /* Expected values for GetNext */ |
| 4654 | QCBORError uExpectedErrorGN; |
| 4655 | uint8_t uQCBORTypeGN; |
| 4656 | int64_t nExponentGN; |
| 4657 | int64_t nMantissaGN; |
| 4658 | UsefulBufC MantissaGN; |
| 4659 | |
| 4660 | /* Expected values for GetDecimalFraction */ |
| 4661 | QCBORError uExpectedErrorGDF; |
| 4662 | int64_t nExponentGDF; |
| 4663 | int64_t nMantissaGDF; |
| 4664 | |
| 4665 | /* Expected values for GetDecimalFractionBig */ |
| 4666 | QCBORError uExpectedErrorGDFB; |
| 4667 | int64_t nExponentGDFB; |
| 4668 | UsefulBufC MantissaGDFB; |
| 4669 | bool IsNegativeGDFB; |
| 4670 | |
| 4671 | /* Expected values for GetBigFloat */ |
| 4672 | QCBORError uExpectedErrorGBF; |
| 4673 | int64_t nExponentGBF; |
| 4674 | int64_t nMantissaGBF; |
| 4675 | |
| 4676 | /* Expected values for GetBigFloatBig */ |
| 4677 | QCBORError uExpectedErrorGBFB; |
| 4678 | int64_t nExponentGBFB; |
| 4679 | UsefulBufC MantissaGBFB; |
| 4680 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4681 | }; |
| 4682 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4683 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4684 | |
| 4685 | static const struct EaMTest pEaMTests[] = { |
| 4686 | { |
| 4687 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 4688 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 4689 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 4690 | false, |
| 4691 | |
| 4692 | QCBOR_SUCCESS, /* for GetNext */ |
| 4693 | QCBOR_TYPE_ARRAY, |
| 4694 | 0, |
| 4695 | 0, |
| 4696 | {(const uint8_t []){0x00}, 1}, |
| 4697 | |
| 4698 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 4699 | -1, |
| 4700 | 3, |
| 4701 | |
| 4702 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4703 | -1, |
| 4704 | {(const uint8_t []){0x03}, 1}, |
| 4705 | false, |
| 4706 | |
| 4707 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4708 | -1, |
| 4709 | 3, |
| 4710 | |
| 4711 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4712 | -1, |
| 4713 | {(const uint8_t []){0x03}, 1}, |
| 4714 | false |
| 4715 | }, |
| 4716 | |
| 4717 | { |
| 4718 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 4719 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 4720 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4721 | false, |
| 4722 | |
| 4723 | QCBOR_SUCCESS, /* for GetNext */ |
| 4724 | QCBOR_TYPE_ARRAY, |
| 4725 | 0, |
| 4726 | 0, |
| 4727 | {(const uint8_t []){0x00}, 1}, |
| 4728 | |
| 4729 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4730 | 0, |
| 4731 | 0, |
| 4732 | |
| 4733 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4734 | 0, |
| 4735 | {(const uint8_t []){0x00}, 1}, |
| 4736 | false, |
| 4737 | |
| 4738 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4739 | 0, |
| 4740 | 0, |
| 4741 | |
| 4742 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4743 | 0, |
| 4744 | {(const uint8_t []){0x00}, 1}, |
| 4745 | false |
| 4746 | |
| 4747 | }, |
| 4748 | |
| 4749 | { |
| 4750 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 4751 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 4752 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4753 | true, |
| 4754 | |
| 4755 | QCBOR_SUCCESS, /* for GetNext */ |
| 4756 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 4757 | -1, |
| 4758 | 3, |
| 4759 | {(const uint8_t []){0x00}, 1}, |
| 4760 | |
| 4761 | |
| 4762 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 4763 | -1, |
| 4764 | 3, |
| 4765 | |
| 4766 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4767 | -1, |
| 4768 | {(const uint8_t []){0x03}, 1}, |
| 4769 | false, |
| 4770 | |
| 4771 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4772 | 0, |
| 4773 | 0, |
| 4774 | |
| 4775 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4776 | 0, |
| 4777 | {(const uint8_t []){0x00}, 1}, |
| 4778 | false |
| 4779 | }, |
| 4780 | { |
| 4781 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 4782 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 4783 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4784 | true, |
| 4785 | |
| 4786 | QCBOR_SUCCESS, /* for GetNext */ |
| 4787 | QCBOR_TYPE_BIGFLOAT, |
| 4788 | 300, |
| 4789 | 100, |
| 4790 | {(const uint8_t []){0x00}, 1}, |
| 4791 | |
| 4792 | |
| 4793 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4794 | 0, |
| 4795 | 0, |
| 4796 | |
| 4797 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4798 | 0, |
| 4799 | {(const uint8_t []){0x03}, 1}, |
| 4800 | false, |
| 4801 | |
| 4802 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4803 | 300, |
| 4804 | 100, |
| 4805 | |
| 4806 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4807 | 300, |
| 4808 | {(const uint8_t []){0x64}, 1}, |
| 4809 | false |
| 4810 | }, |
| 4811 | |
| 4812 | { |
| 4813 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 4814 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 4815 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 4816 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4817 | true, |
| 4818 | |
| 4819 | QCBOR_SUCCESS, /* for GetNext */ |
| 4820 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 4821 | -20, |
| 4822 | 0, |
| 4823 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4824 | |
| 4825 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 4826 | 0, |
| 4827 | 0, |
| 4828 | |
| 4829 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4830 | -20, |
| 4831 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4832 | false, |
| 4833 | |
| 4834 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4835 | 0, |
| 4836 | 0, |
| 4837 | |
| 4838 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4839 | 0, |
| 4840 | {(const uint8_t []){0x00}, 0}, |
| 4841 | false |
| 4842 | }, |
| 4843 | |
| 4844 | { |
| 4845 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 4846 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 4847 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 4848 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4849 | true, |
| 4850 | |
| 4851 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 4852 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 4853 | 0, |
| 4854 | 0, |
| 4855 | {(const uint8_t []){0x00}, 0}, |
| 4856 | |
| 4857 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 4858 | 0, |
| 4859 | 0, |
| 4860 | |
| 4861 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 4862 | 0, |
| 4863 | {(const uint8_t []){0x00}, 0}, |
| 4864 | false, |
| 4865 | |
| 4866 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 4867 | 0, |
| 4868 | 0, |
| 4869 | |
| 4870 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 4871 | 0, |
| 4872 | {(const uint8_t []){0x00}, 0}, |
| 4873 | false |
| 4874 | }, |
| 4875 | { |
| 4876 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 4877 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 4878 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 4879 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4880 | true, |
| 4881 | |
| 4882 | QCBOR_SUCCESS, /* for GetNext */ |
| 4883 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 4884 | -20, |
| 4885 | 0, |
| 4886 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 4887 | |
| 4888 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4889 | 0, |
| 4890 | 0, |
| 4891 | |
| 4892 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4893 | -20, |
| 4894 | {(const uint8_t []){0x00}, 1}, |
| 4895 | false, |
| 4896 | |
| 4897 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4898 | -20, |
| 4899 | 4294967295, |
| 4900 | |
| 4901 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4902 | -20, |
| 4903 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 4904 | false |
| 4905 | }, |
| 4906 | |
| 4907 | { |
| 4908 | /* Special case for test 8. Don't renumber it. */ |
| 4909 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 4910 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 4911 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4912 | true, |
| 4913 | |
| 4914 | QCBOR_SUCCESS, /* for GetNext */ |
| 4915 | QCBOR_TYPE_ARRAY, |
| 4916 | 0, |
| 4917 | 0, |
| 4918 | {(const uint8_t []){0x00}, 1}, |
| 4919 | |
| 4920 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 4921 | 0, |
| 4922 | 0, |
| 4923 | |
| 4924 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4925 | -20, |
| 4926 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4927 | false, |
| 4928 | |
| 4929 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 4930 | 0, |
| 4931 | 0, |
| 4932 | |
| 4933 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4934 | -20, |
| 4935 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4936 | false |
| 4937 | }, |
| 4938 | |
| 4939 | { |
| 4940 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 4941 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4942 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 4943 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4944 | true, |
| 4945 | |
| 4946 | QCBOR_SUCCESS, /* for GetNext */ |
| 4947 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 4948 | 9223372036854775807, |
| 4949 | 0, |
| 4950 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4951 | |
| 4952 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 4953 | 0, |
| 4954 | 0, |
| 4955 | |
| 4956 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4957 | 9223372036854775807, |
| 4958 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4959 | true, |
| 4960 | |
| 4961 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4962 | 0, |
| 4963 | 0, |
| 4964 | |
| 4965 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4966 | 0, |
| 4967 | {(const uint8_t []){0x00}, 1}, |
| 4968 | false |
| 4969 | }, |
| 4970 | }; |
| 4971 | |
| 4972 | |
| 4973 | |
| 4974 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4975 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4976 | size_t uIndex; |
| 4977 | QCBORDecodeContext DCtx; |
| 4978 | QCBORItem Item; |
| 4979 | QCBORError uError; |
| 4980 | int64_t nMantissa, nExponent; |
| 4981 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 4982 | UsefulBufC Mantissa; |
| 4983 | bool bMantissaIsNegative; |
| 4984 | |
| 4985 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 4986 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 4987 | /* Decode with GetNext */ |
| 4988 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 4989 | |
| 4990 | if(uIndex + 1 == 9) { |
| 4991 | nExponent = 99; // just to set a break point |
| 4992 | } |
| 4993 | |
| 4994 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4995 | #ifdef QCBOR_DISABLE_TAGS |
| 4996 | /* Test 8 is a special case when tags are disabled */ |
| 4997 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 4998 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 4999 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 5000 | } |
| 5001 | } else { |
| 5002 | #endif |
| 5003 | /* Now check return code, data type, mantissa and exponent */ |
| 5004 | if(pT->uExpectedErrorGN != uError) { |
| 5005 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 5006 | } |
| 5007 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 5008 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 5009 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 5010 | } |
| 5011 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 5012 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 5013 | } |
| 5014 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 5015 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 5016 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 5017 | } |
| 5018 | } else { |
| 5019 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 5020 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 5021 | } |
| 5022 | } |
| 5023 | } |
| 5024 | #ifdef QCBOR_DISABLE_TAGS |
| 5025 | } |
| 5026 | #endif |
| 5027 | |
| 5028 | /* Decode with GetDecimalFraction */ |
| 5029 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5030 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 5031 | pT->uTagRequirement, |
| 5032 | &nMantissa, |
| 5033 | &nExponent); |
| 5034 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5035 | #ifdef QCBOR_DISABLE_TAGS |
| 5036 | if(pT->bHasTags) { |
| 5037 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5038 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 5039 | } |
| 5040 | } else { |
| 5041 | #endif |
| 5042 | /* Now check return code, mantissa and exponent */ |
| 5043 | if(pT->uExpectedErrorGDF != uError) { |
| 5044 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 5045 | } |
| 5046 | if(uError == QCBOR_SUCCESS) { |
| 5047 | if(pT->nExponentGDF != nExponent) { |
| 5048 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 5049 | } |
| 5050 | if(pT->nMantissaGDF != nMantissa) { |
| 5051 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 5052 | } |
| 5053 | } |
| 5054 | #ifdef QCBOR_DISABLE_TAGS |
| 5055 | } |
| 5056 | #endif |
| 5057 | |
| 5058 | /* Decode with GetDecimalFractionBig */ |
| 5059 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5060 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 5061 | pT->uTagRequirement, |
| 5062 | MantissaBuf, |
| 5063 | &Mantissa, |
| 5064 | &bMantissaIsNegative, |
| 5065 | &nExponent); |
| 5066 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5067 | #ifdef QCBOR_DISABLE_TAGS |
| 5068 | if(pT->bHasTags) { |
| 5069 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5070 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 5071 | } |
| 5072 | } else { |
| 5073 | #endif |
| 5074 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5075 | if(pT->uExpectedErrorGDFB != uError) { |
| 5076 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 5077 | } |
| 5078 | if(uError == QCBOR_SUCCESS) { |
| 5079 | if(pT->nExponentGDFB != nExponent) { |
| 5080 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 5081 | } |
| 5082 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 5083 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 5084 | } |
| 5085 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 5086 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 5087 | } |
| 5088 | } |
| 5089 | #ifdef QCBOR_DISABLE_TAGS |
| 5090 | } |
| 5091 | #endif |
| 5092 | |
| 5093 | /* Decode with GetBigFloat */ |
| 5094 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5095 | QCBORDecode_GetBigFloat(&DCtx, |
| 5096 | pT->uTagRequirement, |
| 5097 | &nMantissa, |
| 5098 | &nExponent); |
| 5099 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5100 | #ifdef QCBOR_DISABLE_TAGS |
| 5101 | if(pT->bHasTags) { |
| 5102 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5103 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 5104 | } |
| 5105 | } else { |
| 5106 | #endif |
| 5107 | /* Now check return code, mantissa and exponent */ |
| 5108 | if(pT->uExpectedErrorGBF != uError) { |
| 5109 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 5110 | } |
| 5111 | if(uError == QCBOR_SUCCESS) { |
| 5112 | if(pT->nExponentGBF != nExponent) { |
| 5113 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 5114 | } |
| 5115 | if(pT->nMantissaGBF != nMantissa) { |
| 5116 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 5117 | } |
| 5118 | } |
| 5119 | #ifdef QCBOR_DISABLE_TAGS |
| 5120 | } |
| 5121 | #endif |
| 5122 | |
| 5123 | /* Decode with GetBigFloatBig */ |
| 5124 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5125 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 5126 | pT->uTagRequirement, |
| 5127 | MantissaBuf, |
| 5128 | &Mantissa, |
| 5129 | &bMantissaIsNegative, |
| 5130 | &nExponent); |
| 5131 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5132 | #ifdef QCBOR_DISABLE_TAGS |
| 5133 | if(pT->bHasTags) { |
| 5134 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5135 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 5136 | } |
| 5137 | } else { |
| 5138 | #endif |
| 5139 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5140 | if(pT->uExpectedErrorGBFB != uError) { |
| 5141 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 5142 | } |
| 5143 | if(uError == QCBOR_SUCCESS) { |
| 5144 | if(pT->nExponentGBFB != nExponent) { |
| 5145 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 5146 | } |
| 5147 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 5148 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 5149 | } |
| 5150 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 5151 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 5152 | } |
| 5153 | } |
| 5154 | #ifdef QCBOR_DISABLE_TAGS |
| 5155 | } |
| 5156 | #endif |
| 5157 | } |
| 5158 | |
| 5159 | return 0; |
| 5160 | } |
| 5161 | |
| 5162 | |
| 5163 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 5164 | { |
| 5165 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5166 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5167 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5168 | QCBORItem item; |
| 5169 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 5170 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 5171 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 5172 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5173 | |
| 5174 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5175 | |
| 5176 | /* Now encode some stuff and then decode it */ |
| 5177 | uint8_t pBuf[40]; |
| 5178 | QCBOREncodeContext EC; |
| 5179 | UsefulBufC Encoded; |
| 5180 | |
| 5181 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 5182 | QCBOREncode_OpenArray(&EC); |
| 5183 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 5184 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 5185 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 5186 | QCBOREncode_CloseArray(&EC); |
| 5187 | QCBOREncode_Finish(&EC, &Encoded); |
| 5188 | |
| 5189 | |
| 5190 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5191 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5192 | if(uErr != QCBOR_SUCCESS) { |
| 5193 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5194 | } |
| 5195 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5196 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5197 | if(uErr != QCBOR_SUCCESS) { |
| 5198 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 5202 | item.val.expAndMantissa.nExponent != 1000 || |
| 5203 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5204 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5205 | } |
| 5206 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5207 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5208 | if(uErr != QCBOR_SUCCESS) { |
| 5209 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 5213 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 5214 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5215 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5216 | } |
| 5217 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5218 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5219 | if(uErr != QCBOR_SUCCESS) { |
| 5220 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 5224 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 5225 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5226 | return 106; |
| 5227 | } |
| 5228 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5229 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5230 | |
| 5231 | return 0; |
| 5232 | } |
| 5233 | |
| 5234 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5235 | int32_t ExponentAndMantissaDecodeTests(void) |
| 5236 | { |
| 5237 | int32_t rv = ProcessEaMTests(); |
| 5238 | if(rv) { |
| 5239 | return rv; |
| 5240 | } |
| 5241 | |
| 5242 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 5243 | } |
| 5244 | |
| 5245 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5246 | static const struct FailInput ExponentAndMantissaFailures[] = { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5247 | // Exponent > INT64_MAX |
| 5248 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5249 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5250 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5251 | // Mantissa > INT64_MAX |
| 5252 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5253 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 5254 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5255 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5256 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5257 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5258 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5259 | // bad content for big num |
| 5260 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 5261 | // bad content for big num |
| 5262 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 5263 | // Bad integer for exponent |
| 5264 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 5265 | // Bad integer for mantissa |
| 5266 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 5267 | // 3 items in array |
| 5268 | { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5269 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5270 | // unterminated indefinite length array |
| 5271 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5272 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5273 | // unterminated indefinite length array |
| 5274 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED}, |
| 5275 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5276 | // Empty array |
| 5277 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 5278 | // Second is not an integer |
| 5279 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5280 | // First is not an integer |
| 5281 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5282 | // Not an array |
| 5283 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 5284 | }; |
| 5285 | |
| 5286 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5287 | int32_t ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5288 | { |
| 5289 | return ProcessFailures(ExponentAndMantissaFailures, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5290 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 5291 | struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5292 | } |
| 5293 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5294 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5295 | |
| 5296 | |
| 5297 | |
| 5298 | /* |
| 5299 | Some basic CBOR with map and array used in a lot of tests. |
| 5300 | The map labels are all strings |
| 5301 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5302 | { |
| 5303 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5304 | "an array of two strings": [ |
| 5305 | "string1", "string2" |
| 5306 | ], |
| 5307 | "map in a map": { |
| 5308 | "bytes 1": h'78787878', |
| 5309 | "bytes 2": h'79797979', |
| 5310 | "another int": 98, |
| 5311 | "text 2": "lies, damn lies and statistics" |
| 5312 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5313 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5314 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5315 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5316 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5317 | { |
| 5318 | QCBORItem Item1, Item2, Item3; |
| 5319 | int64_t nDecodedInt1, nDecodedInt2; |
| 5320 | UsefulBufC B1, B2, S1, S2, S3; |
| 5321 | |
| 5322 | QCBORDecodeContext DCtx; |
| 5323 | QCBORError nCBORError; |
| 5324 | |
| 5325 | QCBORDecode_Init(&DCtx, input, 0); |
| 5326 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5327 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5328 | |
| 5329 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 5330 | |
| 5331 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5332 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5333 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 5334 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 5335 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5336 | QCBORDecode_ExitMap(&DCtx); |
| 5337 | |
| 5338 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5339 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5340 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 5341 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5342 | return -400; |
| 5343 | } |
| 5344 | QCBORDecode_ExitArray(&DCtx); |
| 5345 | |
| 5346 | // Parse the same array again using GetText() instead of GetItem() |
| 5347 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5348 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 5349 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5350 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 5351 | return 5000; |
| 5352 | } |
| 5353 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 5354 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5355 | return 5001; |
| 5356 | } */ |
| 5357 | |
| 5358 | QCBORDecode_ExitArray(&DCtx); |
| 5359 | |
| 5360 | QCBORDecode_ExitMap(&DCtx); |
| 5361 | |
| 5362 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 5363 | |
| 5364 | if(nCBORError) { |
| 5365 | return (int32_t)nCBORError; |
| 5366 | } |
| 5367 | |
| 5368 | if(nDecodedInt1 != 42) { |
| 5369 | return 1001; |
| 5370 | } |
| 5371 | |
| 5372 | if(nDecodedInt2 != 98) { |
| 5373 | return 1002; |
| 5374 | } |
| 5375 | |
| 5376 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5377 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5378 | return 1003; |
| 5379 | } |
| 5380 | |
| 5381 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5382 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5383 | return 1004; |
| 5384 | } |
| 5385 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5386 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5387 | return 1005; |
| 5388 | } |
| 5389 | |
| 5390 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 5391 | return 1006; |
| 5392 | } |
| 5393 | |
| 5394 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 5395 | return 1007; |
| 5396 | } |
| 5397 | |
| 5398 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 5399 | return 1008; |
| 5400 | } |
| 5401 | |
| 5402 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 5403 | return 1009; |
| 5404 | } |
| 5405 | |
| 5406 | return 0; |
| 5407 | } |
| 5408 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5409 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5410 | { |
| 5411 | -75008: h'05083399', |
| 5412 | 88: [], |
| 5413 | 100100: { |
| 5414 | "sub1": { |
| 5415 | 10: [ |
| 5416 | 0 |
| 5417 | ], |
| 5418 | -75009: h'A46823990001', |
| 5419 | 100100: { |
| 5420 | "json": "{ \"ueid\", \"xyz\"}", |
| 5421 | "subsub": { |
| 5422 | 100002: h'141813191001' |
| 5423 | } |
| 5424 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5425 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5426 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5427 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5428 | */ |
| 5429 | |
| 5430 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5431 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 5432 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 5433 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 5434 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 5435 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 5436 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 5437 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 5438 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 5439 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 5440 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 5441 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 5442 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5443 | }; |
| 5444 | |
| 5445 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 5446 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 5447 | { |
| 5448 | UsefulBufC String; |
| 5449 | |
| 5450 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 5451 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 5452 | |
| 5453 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5454 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 5455 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5456 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 5457 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 5458 | if(QCBORDecode_GetError(pDCtx)) { |
| 5459 | return 4001; |
| 5460 | } |
| 5461 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 5462 | return 4002; |
| 5463 | } |
| 5464 | QCBORDecode_ExitMap(pDCtx); |
| 5465 | QCBORDecode_ExitMap(pDCtx); |
| 5466 | QCBORDecode_ExitMap(pDCtx); |
| 5467 | QCBORDecode_ExitMap(pDCtx); |
| 5468 | |
| 5469 | return 0; |
| 5470 | } |
| 5471 | |
| 5472 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 5473 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 5474 | { |
| 5475 | QCBORItem Item; |
| 5476 | QCBORError uErr; |
| 5477 | |
| 5478 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 5479 | for(int x = 0; x < 20; x++) { |
| 5480 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5481 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5482 | return 4100; |
| 5483 | |
| 5484 | } |
| 5485 | } |
| 5486 | QCBORDecode_ExitArray(pDCtx); |
| 5487 | if(QCBORDecode_GetError(pDCtx)) { |
| 5488 | return 4101; |
| 5489 | } |
| 5490 | |
| 5491 | return 0; |
| 5492 | } |
| 5493 | |
| 5494 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 5495 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 5496 | { |
| 5497 | QCBORError uErr; |
| 5498 | |
| 5499 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5500 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 5501 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5502 | int64_t nInt = 99; |
| 5503 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 5504 | if(nInt != 0) { |
| 5505 | return 4200; |
| 5506 | } |
| 5507 | for(int x = 0; x < 20; x++) { |
| 5508 | QCBORItem Item; |
| 5509 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5510 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5511 | return 4201; |
| 5512 | |
| 5513 | } |
| 5514 | } |
| 5515 | QCBORDecode_ExitArray(pDCtx); |
| 5516 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 5517 | return 4202; |
| 5518 | } |
| 5519 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5520 | UsefulBufC dD; |
| 5521 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5522 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5523 | return 4203; |
| 5524 | } |
| 5525 | for(int x = 0; x < 20; x++) { |
| 5526 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5527 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 5528 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5529 | return 4204; |
| 5530 | } |
| 5531 | } |
| 5532 | QCBORDecode_ExitArray(pDCtx); |
| 5533 | QCBORDecode_ExitMap(pDCtx); |
| 5534 | QCBORDecode_ExitMap(pDCtx); |
| 5535 | |
| 5536 | return 0; |
| 5537 | } |
| 5538 | |
| 5539 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 5540 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 5541 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5542 | { |
| 5543 | QCBORDecodeContext DCtx; |
| 5544 | int32_t nReturn; |
| 5545 | QCBORError uErr; |
| 5546 | |
| 5547 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 5548 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5549 | |
| 5550 | for(int j = 0; j < 5; j++) { |
| 5551 | for(int i = 0; i < 20; i++) { |
| 5552 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 5553 | if(nReturn) { |
| 5554 | return nReturn; |
| 5555 | } |
| 5556 | } |
| 5557 | |
| 5558 | for(int i = 0; i < 20; i++) { |
| 5559 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 5560 | if(nReturn ) { |
| 5561 | return nReturn; |
| 5562 | } |
| 5563 | } |
| 5564 | |
| 5565 | for(int i = 0; i < 20; i++) { |
| 5566 | nReturn = DecodeNestedGetZero(&DCtx); |
| 5567 | if(nReturn ) { |
| 5568 | return nReturn; |
| 5569 | } |
| 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | QCBORDecode_ExitMap(&DCtx); |
| 5574 | uErr = QCBORDecode_Finish(&DCtx); |
| 5575 | if(uErr) { |
| 5576 | return (int32_t)uErr + 4100; |
| 5577 | } |
| 5578 | |
| 5579 | return 0; |
| 5580 | } |
| 5581 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5582 | |
| 5583 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5584 | [ |
| 5585 | 23, |
| 5586 | 6000, |
| 5587 | h'67616C6163746963', |
| 5588 | h'686176656E20746F6B656E' |
| 5589 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5590 | */ |
| 5591 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5592 | 0x84, |
| 5593 | 0x17, |
| 5594 | 0x19, 0x17, 0x70, |
| 5595 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 5596 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5597 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5598 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5599 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 5600 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5601 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5602 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 5603 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5604 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5605 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5606 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5607 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5608 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5609 | /* |
| 5610 | { |
| 5611 | 0: [], |
| 5612 | 9: [ |
| 5613 | [], |
| 5614 | [] |
| 5615 | ], |
| 5616 | 8: { |
| 5617 | 1: [], |
| 5618 | 2: {}, |
| 5619 | 3: [] |
| 5620 | }, |
| 5621 | 4: {}, |
| 5622 | 5: [], |
| 5623 | 6: [ |
| 5624 | [], |
| 5625 | [] |
| 5626 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5627 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5628 | */ |
| 5629 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5630 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 5631 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 5632 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 5633 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5634 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5635 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5636 | |
| 5637 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5638 | /* |
| 5639 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5640 | Duplicate label |
| 5641 | Integer overflow |
| 5642 | Date overflow |
| 5643 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5644 | { |
| 5645 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5646 | 3: -18446744073709551616, |
| 5647 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5648 | 5: 0, |
| 5649 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5650 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5651 | */ |
| 5652 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5653 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 5654 | 0xa5, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5655 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5656 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 5657 | #else |
| 5658 | 0xa4, |
| 5659 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5660 | 0x05, 0x00, |
| 5661 | 0x05, 0x00, |
| 5662 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5663 | }; |
| 5664 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5665 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5666 | static const uint8_t spUnRecoverableMapError1[] = { |
| 5667 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5668 | }; |
| 5669 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5670 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5671 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5672 | static const uint8_t spUnRecoverableMapError2[] = { |
| 5673 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5674 | }; |
| 5675 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5676 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5677 | static const uint8_t spUnRecoverableMapError3[] = { |
| 5678 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 5679 | }; |
| 5680 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5681 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5682 | static const uint8_t spUnRecoverableMapError4[] = { |
| 5683 | 0xbf, |
| 5684 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5685 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5686 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5687 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5688 | 0xff |
| 5689 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5690 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5691 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 5692 | const unsigned char not_well_formed_submod_section[] = { |
| 5693 | 0xa1, 0x14, 0x1f, |
| 5694 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5695 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5696 | |
| 5697 | /* Array of length 3, but only two items. */ |
| 5698 | const unsigned char spBadConsumeInput[] = { |
| 5699 | 0x83, 0x00, 0x00 |
| 5700 | }; |
| 5701 | |
| 5702 | /* Tag nesting too deep. */ |
| 5703 | const unsigned char spBadConsumeInput2[] = { |
| 5704 | 0x81, |
| 5705 | 0xD8, 0x37, |
| 5706 | 0xD8, 0x2C, |
| 5707 | 0xD8, 0x21, |
| 5708 | 0xD6, |
| 5709 | 0xCB, |
| 5710 | 00 |
| 5711 | }; |
| 5712 | |
| 5713 | const unsigned char spBadConsumeInput3[] = { |
| 5714 | 0x81, 0xc0, 0x81, 0x00 |
| 5715 | }; |
| 5716 | |
| 5717 | const unsigned char spBadConsumeInput4[] = { |
| 5718 | 0x81, 0x9f, 0x00, 0xff |
| 5719 | }; |
| 5720 | |
| 5721 | const unsigned char spBadConsumeInput5[] = { |
| 5722 | 0xa1, 0x80, 0x00 |
| 5723 | }; |
| 5724 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 5725 | /* |
| 5726 | Lots of nesting for various nesting tests. |
| 5727 | { 1:1, |
| 5728 | 2:{ |
| 5729 | 21:21, |
| 5730 | 22:{ |
| 5731 | 221:[2111, 2112, 2113], |
| 5732 | 222:222, |
| 5733 | 223: {} |
| 5734 | }, |
| 5735 | 23: 23 |
| 5736 | }, |
| 5737 | 3:3, |
| 5738 | 4: [ {} ] |
| 5739 | } |
| 5740 | */ |
| 5741 | static const uint8_t spNested[] = { |
| 5742 | 0xA4, /* Map of 4 */ |
| 5743 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 5744 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 5745 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 5746 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 5747 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 5748 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 5749 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 5750 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 5751 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 5752 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 5753 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 5754 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 5755 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 5756 | 0xA0 /* Array entry {}, an empty map */ |
| 5757 | }; |
| 5758 | |
| 5759 | |
| 5760 | static int32_t EnterMapCursorTest(void) |
| 5761 | { |
| 5762 | QCBORDecodeContext DCtx; |
| 5763 | QCBORItem Item1; |
| 5764 | |
| 5765 | int i; |
| 5766 | for(i = 0; i < 13; i++) { |
| 5767 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 5768 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5769 | int j; |
| 5770 | /* Move travesal cursor */ |
| 5771 | for(j = 0; j < i; j++) { |
| 5772 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5773 | } |
| 5774 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 5775 | QCBORDecode_ExitMap(&DCtx); |
| 5776 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5777 | if(Item1.label.int64 != 3) { |
| 5778 | return 8000; |
| 5779 | } |
| 5780 | } |
| 5781 | |
| 5782 | for(i = 0; i < 13; i++) { |
| 5783 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 5784 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5785 | int j; |
| 5786 | /* Move travesal cursor */ |
| 5787 | for(j = 0; j < i; j++) { |
| 5788 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5789 | } |
| 5790 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 5791 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 5792 | QCBORDecode_ExitMap(&DCtx); |
| 5793 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5794 | if(Item1.label.int64 != 23) { |
| 5795 | return 8000; |
| 5796 | } |
| 5797 | } |
| 5798 | |
| 5799 | for(i = 0; i < 13; i++) { |
| 5800 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 5801 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5802 | int j; |
| 5803 | /* Move travesal cursor */ |
| 5804 | for(j = 0; j < i; j++) { |
| 5805 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5806 | } |
| 5807 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 5808 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 5809 | for(j = 0; j < i; j++) { |
| 5810 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5811 | } |
| 5812 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 5813 | QCBORDecode_ExitArray(&DCtx); |
| 5814 | QCBORDecode_ExitMap(&DCtx); |
| 5815 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5816 | if(Item1.label.int64 != 23) { |
| 5817 | return 8000; |
| 5818 | } |
| 5819 | QCBORDecode_ExitMap(&DCtx); |
| 5820 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5821 | if(Item1.label.int64 != 3) { |
| 5822 | return 8000; |
| 5823 | } |
| 5824 | } |
| 5825 | |
| 5826 | return 0; |
| 5827 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5828 | |
| 5829 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5830 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5831 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5832 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5833 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5834 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5835 | int32_t nReturn; |
| 5836 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5837 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5838 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5839 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5840 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5841 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5842 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5843 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5844 | QCBORDecode_ExitArray(&DCtx); |
| 5845 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5846 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 5847 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5848 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5849 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5850 | QCBORDecode_ExitArray(&DCtx); |
| 5851 | QCBORDecode_ExitArray(&DCtx); |
| 5852 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5853 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 5854 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5855 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5856 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5857 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5858 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5859 | QCBORDecode_ExitArray(&DCtx); |
| 5860 | QCBORDecode_ExitMap(&DCtx); |
| 5861 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5862 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5863 | QCBORDecode_ExitMap(&DCtx); |
| 5864 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5865 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5866 | QCBORDecode_ExitArray(&DCtx); |
| 5867 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5868 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 5869 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5870 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5871 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5872 | QCBORDecode_ExitArray(&DCtx); |
| 5873 | QCBORDecode_ExitArray(&DCtx); |
| 5874 | |
| 5875 | QCBORDecode_ExitMap(&DCtx); |
| 5876 | |
| 5877 | uErr = QCBORDecode_Finish(&DCtx); |
| 5878 | if(uErr != QCBOR_SUCCESS){ |
| 5879 | return 3011; |
| 5880 | } |
| 5881 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5882 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5883 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5884 | if(nReturn) { |
| 5885 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5886 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5887 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5888 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5889 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5890 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5891 | if(nReturn) { |
| 5892 | return nReturn; |
| 5893 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5894 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5895 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5896 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5897 | // These tests confirm the cursor is at the right place after entering |
| 5898 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5899 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5900 | |
| 5901 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5902 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5903 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5904 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5905 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 5906 | return 2001; |
| 5907 | } |
| 5908 | |
| 5909 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5910 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5911 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5912 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5913 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 5914 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5915 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5916 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 5917 | return 2051; |
| 5918 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5919 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5920 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 5921 | return 2002; |
| 5922 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5923 | QCBORDecode_ExitArray(&DCtx); |
| 5924 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 5925 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5926 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5927 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 5928 | return 2052; |
| 5929 | } |
| 5930 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5931 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5932 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5933 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5934 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5935 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5936 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5937 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5938 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5939 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 5940 | return 2003; |
| 5941 | } |
| 5942 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5943 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5944 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5945 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5946 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5947 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5948 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5949 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5950 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5951 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5952 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5953 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5954 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5955 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5956 | } |
| 5957 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5958 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5959 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 5960 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5961 | QCBORDecode_ExitArray(&DCtx); |
| 5962 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5963 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 5964 | return 2006; |
| 5965 | } |
| 5966 | QCBORDecode_ExitMap(&DCtx); |
| 5967 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5968 | return 2007; |
| 5969 | } |
| 5970 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5971 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5972 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5973 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5974 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5975 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5976 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5977 | return 2008; |
| 5978 | } |
| 5979 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5980 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5981 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5982 | return 2009; |
| 5983 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5984 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5985 | |
| 5986 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5987 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5988 | // This will fail because the map is empty. |
| 5989 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5990 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5991 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5992 | return 2010; |
| 5993 | } |
| 5994 | QCBORDecode_ExitMap(&DCtx); |
| 5995 | uErr = QCBORDecode_Finish(&DCtx); |
| 5996 | if(uErr != QCBOR_SUCCESS){ |
| 5997 | return 2011; |
| 5998 | } |
| 5999 | |
| 6000 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6001 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6002 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6003 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6004 | // This will fail because the map is empty. |
| 6005 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6006 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6007 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6008 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6009 | } |
| 6010 | QCBORDecode_ExitMap(&DCtx); |
| 6011 | uErr = QCBORDecode_Finish(&DCtx); |
| 6012 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6013 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6014 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6015 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6016 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6017 | |
| 6018 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6019 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6020 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6021 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6022 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6023 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6024 | QCBORDecode_ExitArray(&DCtx); |
| 6025 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 6026 | QCBORDecode_ExitArray(&DCtx); |
| 6027 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6028 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6029 | return 2014; |
| 6030 | } |
| 6031 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6032 | int64_t nInt; |
| 6033 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6034 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6035 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6036 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6037 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6038 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 6039 | return 2021; |
| 6040 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6041 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 6042 | return 2121; |
| 6043 | } |
| 6044 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6045 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6046 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6047 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6048 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6049 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 6050 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6051 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6052 | return 2024; |
| 6053 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6054 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6055 | |
| 6056 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 6057 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6058 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 6059 | return 2025; |
| 6060 | } |
| 6061 | |
| 6062 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 6063 | |
| 6064 | QCBORDecode_ExitMap(&DCtx); |
| 6065 | uErr = QCBORDecode_Finish(&DCtx); |
| 6066 | if(uErr != QCBOR_SUCCESS) { |
| 6067 | return 2026; |
| 6068 | } |
| 6069 | |
| 6070 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6071 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6072 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6073 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6074 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 6075 | return 2030; |
| 6076 | } |
| 6077 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6078 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6079 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6080 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6081 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6082 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6083 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6084 | return 2031; |
| 6085 | } |
| 6086 | |
| 6087 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6088 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6089 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6090 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6091 | if(uErr != QCBOR_ERR_HIT_END) { |
| 6092 | return 2032; |
| 6093 | } |
| 6094 | |
| 6095 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6096 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6097 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6098 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6099 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 6100 | return 2033; |
| 6101 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6102 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6103 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 6104 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6105 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6106 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 6107 | return 2401; |
| 6108 | } |
| 6109 | if(QCBORDecode_GetError(&DCtx)) { |
| 6110 | return 2402; |
| 6111 | } |
| 6112 | |
| 6113 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6114 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6115 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 6116 | Item1.val.uCount != 3 || |
| 6117 | Item1.uNextNestLevel != 1) { |
| 6118 | return 2403; |
| 6119 | } |
| 6120 | if(QCBORDecode_GetError(&DCtx)) { |
| 6121 | return 2404; |
| 6122 | } |
| 6123 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6124 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 6125 | Item1.uNextNestLevel != 1 || |
| 6126 | Item1.val.int64 != 42) { |
| 6127 | return 2405; |
| 6128 | } |
| 6129 | if(QCBORDecode_GetError(&DCtx)) { |
| 6130 | return 2406; |
| 6131 | } |
| 6132 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6133 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 6134 | Item1.uNestingLevel != 1 || |
| 6135 | Item1.uNextNestLevel != 1 || |
| 6136 | Item1.val.uCount != 2) { |
| 6137 | return 2407; |
| 6138 | } |
| 6139 | if(QCBORDecode_GetError(&DCtx)) { |
| 6140 | return 2408; |
| 6141 | } |
| 6142 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6143 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 6144 | Item1.uNestingLevel != 1 || |
| 6145 | Item1.uNextNestLevel != 0 || |
| 6146 | Item1.val.uCount != 4) { |
| 6147 | return 2409; |
| 6148 | } |
| 6149 | if(QCBORDecode_GetError(&DCtx)) { |
| 6150 | return 2410; |
| 6151 | } |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6152 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6153 | nReturn = DecodeNestedIterate(); |
| 6154 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6155 | |
| 6156 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 6157 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6158 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 6159 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 6160 | return 2500; |
| 6161 | } |
| 6162 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6163 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 6164 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6165 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6166 | return 2600; |
| 6167 | } |
| 6168 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6169 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6170 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 6171 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6172 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6173 | return 2700; |
| 6174 | } |
| 6175 | |
| 6176 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0); |
| 6177 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6178 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 6179 | return 2800; |
| 6180 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6181 | #endif |
| 6182 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6183 | |
| 6184 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 6185 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6186 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6187 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6188 | return 2900; |
| 6189 | } |
| 6190 | #else |
| 6191 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6192 | return 2901; |
| 6193 | } |
| 6194 | #endif |
| 6195 | |
| 6196 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 6197 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6198 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 6199 | return 3000; |
| 6200 | } |
| 6201 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame] | 6202 | nReturn = EnterMapCursorTest(); |
| 6203 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6204 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6208 | struct NumberConversion { |
| 6209 | char *szDescription; |
| 6210 | UsefulBufC CBOR; |
| 6211 | int64_t nConvertedToInt64; |
| 6212 | QCBORError uErrorInt64; |
| 6213 | uint64_t uConvertToUInt64; |
| 6214 | QCBORError uErrorUint64; |
| 6215 | double dConvertToDouble; |
| 6216 | QCBORError uErrorDouble; |
| 6217 | }; |
| 6218 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6219 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 6220 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 6221 | #else |
| 6222 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 6223 | #endif |
| 6224 | |
| 6225 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6226 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6227 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6228 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6229 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 6230 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 6231 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 6232 | }, 15}, |
| 6233 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 6234 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 6235 | 0, |
| 6236 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 6237 | -4.6116860184273879E+18, |
| 6238 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 6239 | }, |
| 6240 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6241 | "too large to fit into int64_t", |
| 6242 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 6243 | 0, |
| 6244 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6245 | 0, |
| 6246 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6247 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6248 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6249 | }, |
| 6250 | { |
| 6251 | "largest negative int that fits in int64_t", |
| 6252 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 6253 | INT64_MIN, |
| 6254 | QCBOR_SUCCESS, |
| 6255 | 0, |
| 6256 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6257 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6258 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6259 | }, |
| 6260 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6261 | "negative bignum -1", |
| 6262 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 6263 | -1, |
| 6264 | QCBOR_SUCCESS, |
| 6265 | 0, |
| 6266 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6267 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6268 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6269 | }, |
| 6270 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6271 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6272 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6273 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 6274 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6275 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6276 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6277 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6278 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6279 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6280 | }, |
| 6281 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6282 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6283 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6284 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6285 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6286 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6287 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6288 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6289 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6290 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6291 | }, |
| 6292 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6293 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6294 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6295 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 6296 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6297 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6298 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6299 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6300 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6301 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6302 | }, |
| 6303 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6304 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6305 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 6306 | 0, |
| 6307 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6308 | 0, |
| 6309 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6310 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6311 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6312 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6313 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6314 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6315 | "Positive bignum 0x01020304 indefinite length string", |
| 6316 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 6317 | 0x01020304, |
| 6318 | QCBOR_SUCCESS, |
| 6319 | 0x01020304, |
| 6320 | QCBOR_SUCCESS, |
| 6321 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6322 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6323 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6324 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6325 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6326 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6327 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 6328 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 6329 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6330 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6331 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6332 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6333 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6334 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6335 | }, |
| 6336 | { |
| 6337 | "big float [9223372036854775806, 9223372036854775806]", |
| 6338 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6339 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 6340 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6341 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6342 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6343 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6344 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6345 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6346 | }, |
| 6347 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6348 | "Big float 3 * 2^^2", |
| 6349 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 6350 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6351 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6352 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6353 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6354 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6355 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6356 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6357 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6358 | "Decimal fraction 3/10", |
| 6359 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 6360 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6361 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6362 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6363 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6364 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6365 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6366 | }, |
| 6367 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6368 | "extreme pos bignum", |
| 6369 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 6370 | // 50 rows of 8 is 400 digits. |
| 6371 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6372 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6373 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6374 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6375 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6376 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6377 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6378 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6379 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6380 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6381 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6382 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6383 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6384 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6385 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6386 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6387 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6388 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6389 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6390 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6391 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6392 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6393 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6394 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6395 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6396 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6397 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6398 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6399 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6400 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6401 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6402 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6403 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6404 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6405 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6406 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6407 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6408 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6409 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6410 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6411 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6412 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6413 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6414 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6415 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6416 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6417 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6418 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6419 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 6420 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6421 | 404}, |
| 6422 | 0, |
| 6423 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6424 | 0, |
| 6425 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6426 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6427 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6428 | }, |
| 6429 | |
| 6430 | { |
| 6431 | "extreme neg bignum", |
| 6432 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 6433 | // 50 rows of 8 is 400 digits. |
| 6434 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6435 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6436 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6437 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6438 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6439 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6440 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6441 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6442 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6443 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6444 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6445 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6446 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6447 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6448 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6449 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6450 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6451 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6452 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6453 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6454 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6455 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6456 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6457 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6458 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6459 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6460 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6461 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6462 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6463 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6464 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6465 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6466 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6467 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6468 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6469 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6470 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6471 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6472 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6473 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6474 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6475 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6476 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6477 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6478 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6479 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6480 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6481 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6482 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 6483 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6484 | 404}, |
| 6485 | 0, |
| 6486 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6487 | 0, |
| 6488 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6489 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6490 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6491 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6492 | |
| 6493 | { |
| 6494 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 6495 | {(uint8_t[]){ |
| 6496 | 0xC5, 0x82, |
| 6497 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6498 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6499 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6500 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6501 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6502 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6503 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6504 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6505 | }, |
| 6506 | |
| 6507 | { |
| 6508 | "bigfloat that evaluates to -INFINITY", |
| 6509 | {(uint8_t[]){ |
| 6510 | 0xC5, 0x82, |
| 6511 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6512 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6513 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6514 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6515 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6516 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6517 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6518 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6519 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6520 | { |
| 6521 | "Positive bignum 0xffff", |
| 6522 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 6523 | 65536-1, |
| 6524 | QCBOR_SUCCESS, |
| 6525 | 0xffff, |
| 6526 | QCBOR_SUCCESS, |
| 6527 | 65535.0, |
| 6528 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6529 | }, |
| 6530 | #endif /* QCBOR_DISABLE_TAGS */ |
| 6531 | { |
| 6532 | "Positive integer 18446744073709551615", |
| 6533 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 6534 | 0, |
| 6535 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6536 | 18446744073709551615ULL, |
| 6537 | QCBOR_SUCCESS, |
| 6538 | 18446744073709551615.0, |
| 6539 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6540 | }, |
| 6541 | |
| 6542 | { |
| 6543 | "Postive integer 0", |
| 6544 | {(uint8_t[]){0x0}, 1}, |
| 6545 | 0LL, |
| 6546 | QCBOR_SUCCESS, |
| 6547 | 0ULL, |
| 6548 | QCBOR_SUCCESS, |
| 6549 | 0.0, |
| 6550 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6551 | }, |
| 6552 | { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 6553 | "Negative integer -9223372036854775808", |
| 6554 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6555 | -9223372036854775807-1, // INT64_MIN |
| 6556 | QCBOR_SUCCESS, |
| 6557 | 0ULL, |
| 6558 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6559 | -9223372036854775808.0, |
| 6560 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6561 | }, |
| 6562 | { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 6563 | "Negative integer -18446744073709551616", |
| 6564 | {(uint8_t[]){0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 6565 | 0ULL, |
| 6566 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6567 | 0ULL, |
| 6568 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6569 | -18446744073709551616.0, |
| 6570 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6571 | }, |
| 6572 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6573 | "Double Floating point value 100.3", |
| 6574 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 6575 | 100L, |
| 6576 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6577 | 100ULL, |
| 6578 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6579 | 100.3, |
| 6580 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 6581 | }, |
| 6582 | { |
| 6583 | "Floating point value NaN 0xfa7fc00000", |
| 6584 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 6585 | 0, |
| 6586 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6587 | 0, |
| 6588 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6589 | NAN, |
| 6590 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6591 | }, |
| 6592 | { |
| 6593 | "half-precision Floating point value -4", |
| 6594 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 6595 | // Normal case with all enabled. |
| 6596 | -4, |
| 6597 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6598 | 0, |
| 6599 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 6600 | -4.0, |
| 6601 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 6602 | }, |
| 6603 | { |
| 6604 | "+inifinity single precision", |
| 6605 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 6606 | 0, |
| 6607 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6608 | 0, |
| 6609 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 6610 | INFINITY, |
| 6611 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6612 | }, |
| 6613 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6614 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6615 | |
| 6616 | |
| 6617 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6618 | |
| 6619 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 6620 | { |
| 6621 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 6622 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6623 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 6624 | return 1; |
| 6625 | } |
| 6626 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6627 | (void)Pool; |
| 6628 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6629 | return 0; |
| 6630 | } |
| 6631 | |
| 6632 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6633 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6634 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6635 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 6636 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6637 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6638 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 6639 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6640 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6641 | // Set up the decoding context including a memory pool so that |
| 6642 | // indefinite length items can be checked |
| 6643 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6644 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6645 | |
| 6646 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6647 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6648 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6649 | } |
| 6650 | |
| 6651 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6652 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6653 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6654 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6655 | } |
| 6656 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6657 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6658 | } |
| 6659 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6660 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6661 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6662 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6663 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6664 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6665 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6666 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6667 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6668 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6669 | } |
| 6670 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6671 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6672 | } |
| 6673 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6674 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6675 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6676 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6677 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6678 | |
| 6679 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6680 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6681 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6682 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6683 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6684 | } |
| 6685 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 6686 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6687 | // NaN's can't be compared for equality. A NaN is |
| 6688 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6689 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6690 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6691 | } |
| 6692 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6693 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6694 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6695 | } |
| 6696 | } |
| 6697 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6698 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | return 0; |
| 6702 | } |
| 6703 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6704 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6705 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6706 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6707 | { |
| 6708 | QCBORDecodeContext DCtx; |
| 6709 | QCBORItem Item; |
| 6710 | QCBORError uCBORError; |
| 6711 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6712 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6713 | QCBORDecode_Init(&DCtx, |
| 6714 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 6715 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6716 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6717 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 6718 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6719 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6720 | do { |
| 6721 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6722 | } while (QCBOR_SUCCESS == uCBORError); |
| 6723 | |
| 6724 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6725 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 6726 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6727 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6728 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6729 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6730 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6731 | |
| 6732 | |
| 6733 | static const uint8_t spSequenceTestInput[] = { |
| 6734 | /* 1. The valid date string "1985-04-12" */ |
| 6735 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 6736 | |
| 6737 | /* 2. */ |
| 6738 | 0x00, |
| 6739 | |
| 6740 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 6741 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 6742 | |
| 6743 | /* 4. */ |
| 6744 | 0x62, 'h', 'i', |
| 6745 | }; |
| 6746 | |
| 6747 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6748 | int32_t CBORSequenceDecodeTests(void) |
| 6749 | { |
| 6750 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6751 | QCBORItem Item; |
| 6752 | QCBORError uCBORError; |
| 6753 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6754 | |
| 6755 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6756 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6757 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6758 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6759 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6760 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6761 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6762 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6763 | if(uCBORError != QCBOR_SUCCESS) { |
| 6764 | return 1; |
| 6765 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6766 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6767 | return 2; |
| 6768 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6769 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6770 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6771 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6772 | uConsumed != 11) { |
| 6773 | return 102; |
| 6774 | } |
| 6775 | |
| 6776 | // Get 2. |
| 6777 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6778 | if(uCBORError != QCBOR_SUCCESS) { |
| 6779 | return 66; |
| 6780 | } |
| 6781 | |
| 6782 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6783 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6784 | uConsumed != 12) { |
| 6785 | return 102; |
| 6786 | } |
| 6787 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6788 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6789 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6790 | if(uCBORError != QCBOR_SUCCESS) { |
| 6791 | return 2; |
| 6792 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6793 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6794 | return 3; |
| 6795 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6796 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6797 | // A sequence can have stuff at the end that may |
| 6798 | // or may not be valid CBOR. The protocol decoder knows |
| 6799 | // when to stop by definition of the protocol, not |
| 6800 | // when the top-level map or array is ended. |
| 6801 | // Finish still has to be called to know that |
| 6802 | // maps and arrays (if there were any) were closed |
| 6803 | // off correctly. When called like this it |
| 6804 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 6805 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6806 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 6807 | return 4; |
| 6808 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6809 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6810 | // --- Test an empty input ---- |
| 6811 | uint8_t empty[1]; |
| 6812 | UsefulBufC Empty = {empty, 0}; |
| 6813 | QCBORDecode_Init(&DCtx, |
| 6814 | Empty, |
| 6815 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6816 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6817 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6818 | if(uCBORError != QCBOR_SUCCESS) { |
| 6819 | return 5; |
| 6820 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6821 | |
| 6822 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6823 | // --- Sequence with unclosed indefinite length array --- |
| 6824 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6825 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6826 | QCBORDecode_Init(&DCtx, |
| 6827 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 6828 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6829 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6830 | // Get the first item |
| 6831 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6832 | if(uCBORError != QCBOR_SUCCESS) { |
| 6833 | return 7; |
| 6834 | } |
| 6835 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6836 | return 8; |
| 6837 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6838 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6839 | // Get a second item |
| 6840 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6841 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6842 | if(uCBORError != QCBOR_SUCCESS) { |
| 6843 | return 9; |
| 6844 | } |
| 6845 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6846 | return 10; |
| 6847 | } |
| 6848 | |
| 6849 | // Try to finish before consuming all bytes to confirm |
| 6850 | // that the still-open error is returned. |
| 6851 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6852 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6853 | return 11; |
| 6854 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6855 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6856 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6857 | return 20; |
| 6858 | } |
| 6859 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6860 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6861 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6862 | // --- Sequence with a closed indefinite length array --- |
| 6863 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6864 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6865 | QCBORDecode_Init(&DCtx, |
| 6866 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 6867 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6868 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6869 | // Get the first item |
| 6870 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6871 | if(uCBORError != QCBOR_SUCCESS) { |
| 6872 | return 12; |
| 6873 | } |
| 6874 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6875 | return 13; |
| 6876 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6877 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6878 | // Get a second item |
| 6879 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6880 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6881 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6882 | if(uCBORError != QCBOR_SUCCESS) { |
| 6883 | return 14; |
| 6884 | } |
| 6885 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6886 | return 15; |
| 6887 | } |
| 6888 | |
| 6889 | // Try to finish before consuming all bytes to confirm |
| 6890 | // that the still-open error is returned. |
| 6891 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6892 | if(uCBORError != QCBOR_SUCCESS) { |
| 6893 | return 16; |
| 6894 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6895 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6896 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6897 | return 20; |
| 6898 | } |
| 6899 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6900 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6901 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6902 | return 0; |
| 6903 | } |
| 6904 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6905 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 6906 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6907 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6908 | { |
| 6909 | int nErrCode; |
| 6910 | int32_t n32; |
| 6911 | int16_t n16; |
| 6912 | int8_t n8; |
| 6913 | uint32_t u32; |
| 6914 | uint16_t u16; |
| 6915 | uint8_t u8; |
| 6916 | uint64_t u64; |
| 6917 | |
| 6918 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 6919 | if(nErrCode == -1 || n32 != 1) { |
| 6920 | return 1; |
| 6921 | } |
| 6922 | |
| 6923 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 6924 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 6925 | return 2; |
| 6926 | } |
| 6927 | |
| 6928 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 6929 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 6930 | return 3; |
| 6931 | } |
| 6932 | |
| 6933 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 6934 | if(nErrCode != -1) { |
| 6935 | return 4; |
| 6936 | } |
| 6937 | |
| 6938 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 6939 | if(nErrCode != -1) { |
| 6940 | return 5; |
| 6941 | } |
| 6942 | |
| 6943 | |
| 6944 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 6945 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 6946 | return 6; |
| 6947 | } |
| 6948 | |
| 6949 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 6950 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 6951 | return 7; |
| 6952 | } |
| 6953 | |
| 6954 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 6955 | if(nErrCode == -1 || n16 != 1) { |
| 6956 | return 8; |
| 6957 | } |
| 6958 | |
| 6959 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 6960 | if(nErrCode != -1) { |
| 6961 | return 9; |
| 6962 | } |
| 6963 | |
| 6964 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 6965 | if(nErrCode != -1) { |
| 6966 | return 10; |
| 6967 | } |
| 6968 | |
| 6969 | |
| 6970 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 6971 | if(nErrCode == -1 || n8 != 1) { |
| 6972 | return 11; |
| 6973 | } |
| 6974 | |
| 6975 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 6976 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 6977 | return 12; |
| 6978 | } |
| 6979 | |
| 6980 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 6981 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 6982 | return 13; |
| 6983 | } |
| 6984 | |
| 6985 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 6986 | if(nErrCode != -1) { |
| 6987 | return 14; |
| 6988 | } |
| 6989 | |
| 6990 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 6991 | if(nErrCode != -1) { |
| 6992 | return 15; |
| 6993 | } |
| 6994 | |
| 6995 | |
| 6996 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 6997 | if(nErrCode == -1 || u32 != 1) { |
| 6998 | return 16; |
| 6999 | } |
| 7000 | |
| 7001 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 7002 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 7003 | return 17; |
| 7004 | } |
| 7005 | |
| 7006 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 7007 | if(nErrCode == -1 || u32 != 0) { |
| 7008 | return 18; |
| 7009 | } |
| 7010 | |
| 7011 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 7012 | if(nErrCode != -1) { |
| 7013 | return 19; |
| 7014 | } |
| 7015 | |
| 7016 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 7017 | if(nErrCode != -1) { |
| 7018 | return 20; |
| 7019 | } |
| 7020 | |
| 7021 | |
| 7022 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 7023 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 7024 | return 21; |
| 7025 | } |
| 7026 | |
| 7027 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 7028 | if(nErrCode == -1 || u16 != 0) { |
| 7029 | return 22; |
| 7030 | } |
| 7031 | |
| 7032 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 7033 | if(nErrCode == -1 || u16 != 1) { |
| 7034 | return 23; |
| 7035 | } |
| 7036 | |
| 7037 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 7038 | if(nErrCode != -1) { |
| 7039 | return 24; |
| 7040 | } |
| 7041 | |
| 7042 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 7043 | if(nErrCode != -1) { |
| 7044 | return 25; |
| 7045 | } |
| 7046 | |
| 7047 | |
| 7048 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 7049 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 7050 | return 26; |
| 7051 | } |
| 7052 | |
| 7053 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 7054 | if(nErrCode == -1 || u8 != 0) { |
| 7055 | return 27; |
| 7056 | } |
| 7057 | |
| 7058 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 7059 | if(nErrCode == -1 || u8 != 1) { |
| 7060 | return 28; |
| 7061 | } |
| 7062 | |
| 7063 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 7064 | if(nErrCode != -1) { |
| 7065 | return 29; |
| 7066 | } |
| 7067 | |
| 7068 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 7069 | if(nErrCode != -1) { |
| 7070 | return 30; |
| 7071 | } |
| 7072 | |
| 7073 | |
| 7074 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 7075 | if(nErrCode == -1 || u64 != 1) { |
| 7076 | return 31; |
| 7077 | } |
| 7078 | |
| 7079 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 7080 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 7081 | return 32; |
| 7082 | } |
| 7083 | |
| 7084 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 7085 | if(nErrCode == -1 || u64 != 0) { |
| 7086 | return 33; |
| 7087 | } |
| 7088 | |
| 7089 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 7090 | if(nErrCode != -1) { |
| 7091 | return 34; |
| 7092 | } |
| 7093 | |
| 7094 | return 0; |
| 7095 | } |
| 7096 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7097 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7098 | |
| 7099 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7100 | /* |
| 7101 | A sequence with |
| 7102 | A wrapping bstr |
| 7103 | containing a map |
| 7104 | 1 |
| 7105 | 2 |
| 7106 | A wrapping bstr |
| 7107 | containing an array |
| 7108 | 3 |
| 7109 | wrapping bstr |
| 7110 | 4 |
| 7111 | 5 |
| 7112 | 6 |
| 7113 | array |
| 7114 | 7 |
| 7115 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7116 | */ |
| 7117 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7118 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7119 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7120 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7121 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7122 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7123 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7124 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7125 | |
| 7126 | QCBOREncode_BstrWrap(&EC); |
| 7127 | QCBOREncode_OpenMap(&EC); |
| 7128 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 7129 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 7130 | QCBOREncode_CloseMap(&EC); |
| 7131 | QCBOREncode_BstrWrap(&EC); |
| 7132 | QCBOREncode_OpenArray(&EC); |
| 7133 | QCBOREncode_AddInt64(&EC, 3); |
| 7134 | QCBOREncode_BstrWrap(&EC); |
| 7135 | QCBOREncode_AddInt64(&EC, 4); |
| 7136 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7137 | QCBOREncode_AddInt64(&EC, 5); |
| 7138 | QCBOREncode_CloseArray(&EC); |
| 7139 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7140 | QCBOREncode_AddInt64(&EC, 6); |
| 7141 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7142 | QCBOREncode_OpenArray(&EC); |
| 7143 | QCBOREncode_AddInt64(&EC, 7); |
| 7144 | QCBOREncode_AddInt64(&EC, 8); |
| 7145 | QCBOREncode_CloseArray(&EC); |
| 7146 | |
| 7147 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 7148 | if(uErr) { |
| 7149 | Encoded = NULLUsefulBufC; |
| 7150 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7151 | |
| 7152 | return Encoded; |
| 7153 | } |
| 7154 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7155 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7156 | static const uint8_t spBreakInByteString[] = { |
| 7157 | 0x41, 0xff |
| 7158 | }; |
| 7159 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7160 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7161 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7162 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7163 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7164 | |
| 7165 | QCBORDecodeContext DC; |
| 7166 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7167 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7168 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7169 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7170 | |
| 7171 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7172 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7173 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7174 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 7175 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7176 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7177 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7178 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7179 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7180 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7181 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7182 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7183 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7184 | QCBORDecode_ExitArray(&DC); |
| 7185 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7186 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7187 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7188 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7189 | QCBORDecode_GetInt64(&DC, &n7); |
| 7190 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7191 | QCBORDecode_ExitArray(&DC); |
| 7192 | |
| 7193 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7194 | if(uErr) { |
| 7195 | return (int32_t)uErr; |
| 7196 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7197 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7198 | |
| 7199 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 7200 | * Successful because no items are fetched from byte string. |
| 7201 | */ |
| 7202 | QCBORDecode_Init(&DC, |
| 7203 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 7204 | 0); |
| 7205 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7206 | uErr = QCBORDecode_GetError(&DC); |
| 7207 | if(uErr) { |
| 7208 | return 100 + (int32_t)uErr; |
| 7209 | } |
| 7210 | |
| 7211 | QCBORDecode_ExitBstrWrapped(&DC); |
| 7212 | uErr = QCBORDecode_GetError(&DC); |
| 7213 | if(uErr) { |
| 7214 | return 200 + (int32_t)uErr; |
| 7215 | } |
| 7216 | |
| 7217 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 7218 | * It fails because there should be no break. |
| 7219 | */ |
| 7220 | QCBORDecode_Init(&DC, |
| 7221 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 7222 | 0); |
| 7223 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7224 | QCBORItem Item; |
| 7225 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 7226 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 7227 | return 300 + (int32_t)uErr; |
| 7228 | } |
| 7229 | |
| 7230 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7231 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7232 | |
| 7233 | |
| 7234 | |
| 7235 | |
| 7236 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7237 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7238 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7239 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7240 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7241 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 7242 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 7243 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7244 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7245 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7246 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 7247 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 7248 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7249 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7250 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7251 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7252 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 7253 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7254 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7255 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7256 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 7257 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7258 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7259 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7260 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7261 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 7262 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7263 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7264 | 21, |
| 7265 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 7266 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 7267 | |
| 7268 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7269 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7270 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 7271 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7272 | |
| 7273 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7274 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 7275 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7276 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7277 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7278 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7279 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 7280 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7281 | |
| 7282 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7283 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 7284 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7285 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7286 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7287 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7288 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 7289 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7290 | |
| 7291 | 0x18, 0x33, |
| 7292 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 7293 | |
| 7294 | // MIME |
| 7295 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7296 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 7297 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 7298 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7299 | |
| 7300 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7301 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 7302 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7303 | |
| 7304 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7305 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 7306 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 7307 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7308 | |
| 7309 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7310 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 7311 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7312 | |
| 7313 | // UUID |
| 7314 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7315 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 7316 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7317 | |
| 7318 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7319 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 7320 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7321 | }; |
| 7322 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7323 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7324 | { |
| 7325 | QCBORDecodeContext DC; |
| 7326 | QCBORError uErr; |
| 7327 | |
| 7328 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 7329 | |
| 7330 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7331 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7332 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7333 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7334 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7335 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7336 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 7337 | return 1; |
| 7338 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7339 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7340 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7341 | return 2; |
| 7342 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7343 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7344 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7345 | return 3; |
| 7346 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7347 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7348 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7349 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7350 | return 4; |
| 7351 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7352 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7353 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7354 | return 5; |
| 7355 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7356 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7357 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7358 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7359 | bNeg != false) { |
| 7360 | return 10; |
| 7361 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7362 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7363 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7364 | bNeg != true) { |
| 7365 | return 11; |
| 7366 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7367 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7368 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7369 | return 12; |
| 7370 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7371 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7372 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7373 | return 13; |
| 7374 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7375 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7376 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7377 | return 14; |
| 7378 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7379 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7380 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7381 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7382 | return 20; |
| 7383 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7384 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7385 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7386 | return 21; |
| 7387 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7388 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7389 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7390 | return 22; |
| 7391 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7392 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7393 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7394 | return 23; |
| 7395 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7396 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7397 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7398 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7399 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7400 | return 30; |
| 7401 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7402 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7403 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7404 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7405 | return 31; |
| 7406 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7407 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7408 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7409 | return 32; |
| 7410 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7411 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7412 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7413 | return 33; |
| 7414 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7415 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7416 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7417 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7418 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7419 | return 40; |
| 7420 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7421 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7422 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7423 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7424 | return 41; |
| 7425 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7426 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7427 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7428 | return 42; |
| 7429 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7430 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7431 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7432 | return 43; |
| 7433 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7434 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7435 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7436 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7437 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7438 | return 50; |
| 7439 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7440 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7441 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7442 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7443 | return 51; |
| 7444 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7445 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7446 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7447 | return 52; |
| 7448 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7449 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7450 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7451 | return 53; |
| 7452 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7453 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7454 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7455 | // MIME |
| 7456 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7457 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7458 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7459 | bIsNot7Bit == true) { |
| 7460 | return 60; |
| 7461 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7462 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7463 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7464 | bIsNot7Bit == true) { |
| 7465 | return 61; |
| 7466 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7467 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7468 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7469 | bIsNot7Bit == false) { |
| 7470 | return 62; |
| 7471 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7472 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7473 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7474 | bIsNot7Bit == false) { |
| 7475 | return 63; |
| 7476 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7477 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7478 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7479 | return 64; |
| 7480 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7481 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7482 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7483 | return 65; |
| 7484 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7485 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7486 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7487 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7488 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7489 | return 70; |
| 7490 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7491 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 7492 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7493 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7494 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7495 | return 71; |
| 7496 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7497 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7498 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7499 | return 72; |
| 7500 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7501 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7502 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7503 | return 73; |
| 7504 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7505 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7506 | // Improvement: add some more error test cases |
| 7507 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7508 | QCBORDecode_ExitMap(&DC); |
| 7509 | |
| 7510 | uErr = QCBORDecode_Finish(&DC); |
| 7511 | if(uErr != QCBOR_SUCCESS) { |
| 7512 | return 100; |
| 7513 | } |
| 7514 | |
| 7515 | return 0; |
| 7516 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7517 | |
| 7518 | |
| 7519 | |
| 7520 | |
| 7521 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7522 | [ |
| 7523 | "aaaaaaaaaa", |
| 7524 | {} |
| 7525 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7526 | */ |
| 7527 | static const uint8_t spTooLarge1[] = { |
| 7528 | 0x9f, |
| 7529 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7530 | 0xa0, |
| 7531 | 0xff |
| 7532 | }; |
| 7533 | |
| 7534 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7535 | [ |
| 7536 | { |
| 7537 | 0: "aaaaaaaaaa" |
| 7538 | } |
| 7539 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7540 | */ |
| 7541 | static const uint8_t spTooLarge2[] = { |
| 7542 | 0x9f, |
| 7543 | 0xa1, |
| 7544 | 0x00, |
| 7545 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7546 | 0xff |
| 7547 | }; |
| 7548 | |
| 7549 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7550 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7551 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7552 | { |
| 7553 | 0: "aaaaaaaaaa" |
| 7554 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7555 | */ |
| 7556 | static const uint8_t spTooLarge3[] = { |
| 7557 | 0x4d, |
| 7558 | 0xa1, |
| 7559 | 0x00, |
| 7560 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7561 | }; |
| 7562 | |
| 7563 | int32_t TooLargeInputTest(void) |
| 7564 | { |
| 7565 | QCBORDecodeContext DC; |
| 7566 | QCBORError uErr; |
| 7567 | UsefulBufC String; |
| 7568 | |
| 7569 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 7570 | // to 10 There's not really any way to test this error |
| 7571 | // condition. The error condition is not complex, so setting |
| 7572 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 7573 | |
| 7574 | // The input CBOR is only too large because the |
| 7575 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 7576 | // |
| 7577 | // This test is disabled for the normal test runs because of the |
| 7578 | // special build requirement. |
| 7579 | |
| 7580 | |
| 7581 | // Tests the start of a map being too large |
| 7582 | 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] | 7583 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7584 | QCBORDecode_GetTextString(&DC, &String); |
| 7585 | uErr = QCBORDecode_GetError(&DC); |
| 7586 | if(uErr != QCBOR_SUCCESS) { |
| 7587 | return 1; |
| 7588 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7589 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7590 | uErr = QCBORDecode_GetError(&DC); |
| 7591 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7592 | return 2; |
| 7593 | } |
| 7594 | |
| 7595 | // Tests the end of a map being too large |
| 7596 | 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] | 7597 | QCBORDecode_EnterArray(&DC, NULL); |
| 7598 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7599 | uErr = QCBORDecode_GetError(&DC); |
| 7600 | if(uErr != QCBOR_SUCCESS) { |
| 7601 | return 3; |
| 7602 | } |
| 7603 | QCBORDecode_ExitMap(&DC); |
| 7604 | uErr = QCBORDecode_GetError(&DC); |
| 7605 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7606 | return 4; |
| 7607 | } |
| 7608 | |
| 7609 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 7610 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 7611 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7612 | uErr = QCBORDecode_GetError(&DC); |
| 7613 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7614 | return 5; |
| 7615 | } |
| 7616 | |
| 7617 | return 0; |
| 7618 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7619 | |
| 7620 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7621 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7622 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7623 | /* |
| 7624 | An array of three map entries |
| 7625 | 1) Indefinite length string label for indefinite lenght byte string |
| 7626 | 2) Indefinite length string label for an integer |
| 7627 | 3) Indefinite length string label for an indefinite-length negative big num |
| 7628 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7629 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7630 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7631 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 7632 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 7633 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 7634 | 0x03, |
| 7635 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 7636 | 0xc3, |
| 7637 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7638 | }; |
| 7639 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7640 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7641 | { |
| 7642 | QCBORDecodeContext DCtx; |
| 7643 | |
| 7644 | QCBORDecode_Init(&DCtx, |
| 7645 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 7646 | QCBOR_DECODE_MODE_NORMAL); |
| 7647 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7648 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7649 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 7650 | |
| 7651 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7652 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7653 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7654 | |
| 7655 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7656 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7657 | return 1; |
| 7658 | } |
| 7659 | |
| 7660 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 7661 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 7662 | return 2; |
| 7663 | } |
| 7664 | |
| 7665 | uint64_t uInt; |
| 7666 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 7667 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7668 | return 3; |
| 7669 | } |
| 7670 | if(uInt != 3) { |
| 7671 | return 4; |
| 7672 | } |
| 7673 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7674 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7675 | double uDouble; |
| 7676 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 7677 | "label2", |
| 7678 | 0xff, |
| 7679 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7680 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7681 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7682 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7683 | return 5; |
| 7684 | } |
| 7685 | if(uDouble != -16777474) { |
| 7686 | return 6; |
| 7687 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7688 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7689 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 7690 | return 7; |
| 7691 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7692 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7693 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7694 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7695 | QCBORDecode_ExitMap(&DCtx); |
| 7696 | |
| 7697 | if(QCBORDecode_Finish(&DCtx)) { |
| 7698 | return 99; |
| 7699 | } |
| 7700 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7701 | #else /* QCBOR_DISABLE_TAGS */ |
| 7702 | /* The big num in the input is a CBOR tag and you can't do |
| 7703 | * map lookups in a map with a tag so this test does very little |
| 7704 | * when tags are disabled. That is OK, the test coverage is still |
| 7705 | * good when they are not. |
| 7706 | */ |
| 7707 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 7708 | return 1002; |
| 7709 | } |
| 7710 | #endif /*QCBOR_DISABLE_TAGS */ |
| 7711 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7712 | return 0; |
| 7713 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7714 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7715 | |
| 7716 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7717 | /* |
| 7718 | * An array of an integer and an array. The second array contains |
| 7719 | * a bstr-wrapped map. |
| 7720 | * |
| 7721 | * [7, [h'A36D6669... (see next lines) 73']] |
| 7722 | * |
| 7723 | * {"first integer": 42, |
| 7724 | * "an array of two strings": ["string1", "string2"], |
| 7725 | * "map in a map": |
| 7726 | * { "bytes 1": h'78787878', |
| 7727 | * "bytes 2": h'79797979', |
| 7728 | * "another int": 98, |
| 7729 | * "text 2": "lies, damn lies and statistics" |
| 7730 | * } |
| 7731 | * } |
| 7732 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7733 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7734 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 7735 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 7736 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7737 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7738 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7739 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7740 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7741 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7742 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7743 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7744 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7745 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7746 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7747 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7748 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7749 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7750 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7751 | 0x73 |
| 7752 | }; |
| 7753 | |
| 7754 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7755 | |
| 7756 | /* As above, but the arrays are indefinite length */ |
| 7757 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 7758 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 7759 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7760 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7761 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7762 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7763 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7764 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7765 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7766 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7767 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7768 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7769 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7770 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7771 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7772 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7773 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7774 | 0x73, |
| 7775 | 0xff, 0xff |
| 7776 | }; |
| 7777 | #endif |
| 7778 | |
| 7779 | |
| 7780 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 7781 | |
| 7782 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7783 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 7784 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7785 | |
| 7786 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7787 | |
| 7788 | /* |
| 7789 | An array of one that contains |
| 7790 | a byte string that is tagged 24 which means CBOR-encoded data |
| 7791 | the byte string is an indefinite length string |
| 7792 | the wrapped byte string is an array of three numbers |
| 7793 | [42, 43, 44] |
| 7794 | |
| 7795 | [ |
| 7796 | 24( |
| 7797 | (_ h'83', h'18', h'2A182B', h'182C') |
| 7798 | ) |
| 7799 | ] |
| 7800 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7801 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 7802 | 0x81, |
| 7803 | 0xd8, 0x18, |
| 7804 | 0x5f, |
| 7805 | 0x41, 0x83, |
| 7806 | 0x41, 0x18, |
| 7807 | 0x43, 0x2A, 0x18, 0x2B, |
| 7808 | 0x42, 0x18, 0x2C, |
| 7809 | 0xff |
| 7810 | }; |
| 7811 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7812 | |
| 7813 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7814 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7815 | { |
| 7816 | QCBORItem Item; |
| 7817 | QCBORError nCBORError; |
| 7818 | QCBORDecodeContext DCtx; |
| 7819 | |
| 7820 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7821 | |
| 7822 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7823 | return 100+(int32_t)nCBORError; |
| 7824 | } |
| 7825 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7826 | return 200; |
| 7827 | } |
| 7828 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 7829 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 7830 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 7831 | return 150+(int32_t)nCBORError; |
| 7832 | } |
| 7833 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7834 | return 250; |
| 7835 | } |
| 7836 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7837 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7838 | return (int32_t)nCBORError; |
| 7839 | } |
| 7840 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7841 | return 300; |
| 7842 | } |
| 7843 | |
| 7844 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7845 | return 400 + (int32_t)nCBORError; |
| 7846 | } |
| 7847 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7848 | return 500; |
| 7849 | } |
| 7850 | |
| 7851 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7852 | return (int32_t)nCBORError; |
| 7853 | } |
| 7854 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7855 | return 600; |
| 7856 | } |
| 7857 | |
| 7858 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7859 | return 900 + (int32_t)nCBORError; |
| 7860 | } |
| 7861 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7862 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7863 | Item.val.int64 != 42 || |
| 7864 | Item.uDataAlloc || |
| 7865 | Item.uLabelAlloc || |
| 7866 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7867 | return 1000; |
| 7868 | } |
| 7869 | |
| 7870 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7871 | return 1100 + (int32_t)nCBORError; |
| 7872 | } |
| 7873 | |
| 7874 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7875 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7876 | Item.val.int64 != 42 || |
| 7877 | Item.uDataAlloc || |
| 7878 | Item.uLabelAlloc || |
| 7879 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7880 | return 1200; |
| 7881 | } |
| 7882 | |
| 7883 | |
| 7884 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7885 | return 1300 + (int32_t)nCBORError; |
| 7886 | } |
| 7887 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7888 | Item.uDataAlloc || |
| 7889 | Item.uLabelAlloc || |
| 7890 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7891 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7892 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7893 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7894 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7895 | |
| 7896 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7897 | return 1500 + (int32_t)nCBORError; |
| 7898 | } |
| 7899 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7900 | Item.uDataAlloc || |
| 7901 | Item.uLabelAlloc || |
| 7902 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7903 | return 1600; |
| 7904 | } |
| 7905 | |
| 7906 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7907 | return 1700 + (int32_t)nCBORError; |
| 7908 | } |
| 7909 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7910 | Item.uDataAlloc || |
| 7911 | Item.uLabelAlloc || |
| 7912 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7913 | return 1800; |
| 7914 | } |
| 7915 | |
| 7916 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7917 | return (int32_t)nCBORError; |
| 7918 | } |
| 7919 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7920 | Item.uDataAlloc || |
| 7921 | Item.uLabelAlloc || |
| 7922 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7923 | return 1900; |
| 7924 | } |
| 7925 | |
| 7926 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7927 | return (int32_t)nCBORError; |
| 7928 | } |
| 7929 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7930 | Item.uDataAlloc || |
| 7931 | Item.uLabelAlloc || |
| 7932 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7933 | return 2000; |
| 7934 | } |
| 7935 | |
| 7936 | |
| 7937 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7938 | return 2100 + (int32_t)nCBORError; |
| 7939 | } |
| 7940 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7941 | Item.uDataAlloc || |
| 7942 | Item.uLabelAlloc || |
| 7943 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 7944 | Item.uDataType != QCBOR_TYPE_MAP || |
| 7945 | Item.val.uCount != 4) { |
| 7946 | return 2100; |
| 7947 | } |
| 7948 | |
| 7949 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7950 | return 2200 + (int32_t)nCBORError; |
| 7951 | } |
| 7952 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7953 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 7954 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7955 | Item.uDataAlloc || |
| 7956 | Item.uLabelAlloc || |
| 7957 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 7958 | return 2300; |
| 7959 | } |
| 7960 | |
| 7961 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7962 | return 2400 + (int32_t)nCBORError; |
| 7963 | } |
| 7964 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7965 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7966 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7967 | Item.uDataAlloc || |
| 7968 | Item.uLabelAlloc || |
| 7969 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7970 | return 2500; |
| 7971 | } |
| 7972 | |
| 7973 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7974 | return 2600 + (int32_t)nCBORError; |
| 7975 | } |
| 7976 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7977 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7978 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7979 | Item.uDataAlloc || |
| 7980 | Item.uLabelAlloc || |
| 7981 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7982 | return 2700; |
| 7983 | } |
| 7984 | |
| 7985 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7986 | return 2800 + (int32_t)nCBORError; |
| 7987 | } |
| 7988 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7989 | Item.uDataAlloc || |
| 7990 | Item.uLabelAlloc || |
| 7991 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 7992 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7993 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7994 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7995 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7996 | |
| 7997 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7998 | return 3000 + (int32_t)nCBORError; |
| 7999 | } |
| 8000 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8001 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8002 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8003 | Item.uDataAlloc || |
| 8004 | Item.uLabelAlloc || |
| 8005 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8006 | return 3100; |
| 8007 | } |
| 8008 | |
| 8009 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8010 | return 3200 + (int32_t)nCBORError; |
| 8011 | } |
| 8012 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8013 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8014 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8015 | Item.uDataAlloc || |
| 8016 | Item.uLabelAlloc || |
| 8017 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8018 | return 3300; |
| 8019 | } |
| 8020 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8021 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 8022 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8023 | return 3300 + (int32_t)nCBORError; |
| 8024 | } |
| 8025 | |
| 8026 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8027 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8028 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8029 | return 3400 + (int32_t)nCBORError; |
| 8030 | } |
| 8031 | |
| 8032 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8033 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8034 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8035 | return 3500 + (int32_t)nCBORError; |
| 8036 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8037 | |
| 8038 | |
| 8039 | // Rewind to top level after entering several maps |
| 8040 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8041 | |
| 8042 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8043 | return (int32_t)nCBORError; |
| 8044 | } |
| 8045 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8046 | Item.val.uCount != 3) { |
| 8047 | return 400; |
| 8048 | } |
| 8049 | |
| 8050 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8051 | return 4000+(int32_t)nCBORError; |
| 8052 | } |
| 8053 | |
| 8054 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8055 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8056 | Item.val.int64 != 42 || |
| 8057 | Item.uDataAlloc || |
| 8058 | Item.uLabelAlloc || |
| 8059 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8060 | return 4100; |
| 8061 | } |
| 8062 | |
| 8063 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8064 | return 4100+(int32_t)nCBORError; |
| 8065 | } |
| 8066 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8067 | Item.uDataAlloc || |
| 8068 | Item.uLabelAlloc || |
| 8069 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8070 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8071 | Item.val.uCount != 2) { |
| 8072 | return 4200; |
| 8073 | } |
| 8074 | |
| 8075 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8076 | return 4200+(int32_t)nCBORError; |
| 8077 | } |
| 8078 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8079 | Item.uDataAlloc || |
| 8080 | Item.uLabelAlloc || |
| 8081 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8082 | return 4300; |
| 8083 | } |
| 8084 | |
| 8085 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8086 | return 4300+(int32_t)nCBORError; |
| 8087 | } |
| 8088 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8089 | Item.uDataAlloc || |
| 8090 | Item.uLabelAlloc || |
| 8091 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8092 | return 4400; |
| 8093 | } |
| 8094 | |
| 8095 | QCBORDecode_Rewind(&DCtx); |
| 8096 | |
| 8097 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8098 | return 4400+(int32_t)nCBORError; |
| 8099 | } |
| 8100 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8101 | Item.val.uCount != 3) { |
| 8102 | return 4500; |
| 8103 | } |
| 8104 | |
| 8105 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8106 | return (int32_t)nCBORError; |
| 8107 | } |
| 8108 | |
| 8109 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8110 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8111 | Item.val.int64 != 42 || |
| 8112 | Item.uDataAlloc || |
| 8113 | Item.uLabelAlloc || |
| 8114 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8115 | return 4600; |
| 8116 | } |
| 8117 | |
| 8118 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8119 | return (int32_t)nCBORError; |
| 8120 | } |
| 8121 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8122 | Item.uDataAlloc || |
| 8123 | Item.uLabelAlloc || |
| 8124 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8125 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8126 | Item.val.uCount != 2) { |
| 8127 | return 4700; |
| 8128 | } |
| 8129 | |
| 8130 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8131 | return (int32_t)nCBORError; |
| 8132 | } |
| 8133 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8134 | Item.uDataAlloc || |
| 8135 | Item.uLabelAlloc || |
| 8136 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8137 | return 4800; |
| 8138 | } |
| 8139 | |
| 8140 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8141 | return 4900+(int32_t)nCBORError; |
| 8142 | } |
| 8143 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8144 | Item.uDataAlloc || |
| 8145 | Item.uLabelAlloc || |
| 8146 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8147 | return 5000; |
| 8148 | } |
| 8149 | |
| 8150 | |
| 8151 | // Rewind an entered map |
| 8152 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8153 | |
| 8154 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8155 | |
| 8156 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8157 | return 5100+(int32_t)nCBORError; |
| 8158 | } |
| 8159 | |
| 8160 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8161 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8162 | Item.val.int64 != 42 || |
| 8163 | Item.uDataAlloc || |
| 8164 | Item.uLabelAlloc || |
| 8165 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8166 | return 5200; |
| 8167 | } |
| 8168 | |
| 8169 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8170 | return 5200+(int32_t)nCBORError; |
| 8171 | } |
| 8172 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8173 | Item.uDataAlloc || |
| 8174 | Item.uLabelAlloc || |
| 8175 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8176 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8177 | Item.val.uCount != 2) { |
| 8178 | return -5300; |
| 8179 | } |
| 8180 | |
| 8181 | QCBORDecode_Rewind(&DCtx); |
| 8182 | |
| 8183 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8184 | return 5300+(int32_t)nCBORError; |
| 8185 | } |
| 8186 | |
| 8187 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8188 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8189 | Item.val.int64 != 42 || |
| 8190 | Item.uDataAlloc || |
| 8191 | Item.uLabelAlloc || |
| 8192 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8193 | return 5400; |
| 8194 | } |
| 8195 | |
| 8196 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8197 | return 5400+(int32_t)nCBORError; |
| 8198 | } |
| 8199 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8200 | Item.uDataAlloc || |
| 8201 | Item.uLabelAlloc || |
| 8202 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8203 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8204 | Item.val.uCount != 2) { |
| 8205 | return 5500; |
| 8206 | } |
| 8207 | |
| 8208 | |
| 8209 | // Rewind and entered array inside an entered map |
| 8210 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8211 | |
| 8212 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8213 | |
| 8214 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 8215 | |
| 8216 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8217 | return 5600+(int32_t)nCBORError; |
| 8218 | } |
| 8219 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8220 | Item.uDataAlloc || |
| 8221 | Item.uLabelAlloc || |
| 8222 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8223 | return 5700; |
| 8224 | } |
| 8225 | |
| 8226 | QCBORDecode_Rewind(&DCtx); |
| 8227 | |
| 8228 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8229 | return 5700+(int32_t)nCBORError; |
| 8230 | } |
| 8231 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8232 | Item.uDataAlloc || |
| 8233 | Item.uLabelAlloc || |
| 8234 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8235 | return 5800; |
| 8236 | } |
| 8237 | |
| 8238 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8239 | return (int32_t)nCBORError; |
| 8240 | } |
| 8241 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8242 | Item.uDataAlloc || |
| 8243 | Item.uLabelAlloc || |
| 8244 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8245 | return 5900; |
| 8246 | } |
| 8247 | |
| 8248 | QCBORDecode_Rewind(&DCtx); |
| 8249 | |
| 8250 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8251 | return 5900+(int32_t)nCBORError; |
| 8252 | } |
| 8253 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8254 | Item.uDataAlloc || |
| 8255 | Item.uLabelAlloc || |
| 8256 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8257 | return 6000; |
| 8258 | } |
| 8259 | |
| 8260 | |
| 8261 | // Rewind a byte string inside an array inside an array |
| 8262 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 8263 | |
| 8264 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8265 | |
| 8266 | uint64_t i; |
| 8267 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8268 | |
| 8269 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8270 | |
| 8271 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8272 | if(QCBORDecode_GetError(&DCtx)) { |
| 8273 | return 6100; |
| 8274 | } |
| 8275 | |
| 8276 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8277 | return (int32_t)nCBORError; |
| 8278 | } |
| 8279 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8280 | return 6200; |
| 8281 | } |
| 8282 | |
| 8283 | QCBORDecode_Rewind(&DCtx); |
| 8284 | |
| 8285 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8286 | return 6300+(int32_t)nCBORError; |
| 8287 | } |
| 8288 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8289 | return 6400; |
| 8290 | } |
| 8291 | |
| 8292 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8293 | // Rewind a byte string inside an indefinite-length array inside |
| 8294 | // indefinite-length array |
| 8295 | |
| 8296 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 8297 | |
| 8298 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8299 | |
| 8300 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8301 | |
| 8302 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8303 | |
| 8304 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8305 | if(QCBORDecode_GetError(&DCtx)) { |
| 8306 | return 6500; |
| 8307 | } |
| 8308 | |
| 8309 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8310 | return 6600+(int32_t)nCBORError; |
| 8311 | } |
| 8312 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8313 | return 6700; |
| 8314 | } |
| 8315 | |
| 8316 | QCBORDecode_Rewind(&DCtx); |
| 8317 | |
| 8318 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8319 | return 6800+(int32_t)nCBORError; |
| 8320 | } |
| 8321 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8322 | return 6900; |
| 8323 | } |
| 8324 | #endif |
| 8325 | |
| 8326 | // Rewind an empty map |
| 8327 | // [100, {}] |
| 8328 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 8329 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8330 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8331 | if(i != 100) { |
| 8332 | return 7010; |
| 8333 | } |
| 8334 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8335 | |
| 8336 | /* Do it 5 times to be sure multiple rewinds work */ |
| 8337 | for(int n = 0; n < 5; n++) { |
| 8338 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 8339 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8340 | return 7000 + n; |
| 8341 | } |
| 8342 | QCBORDecode_Rewind(&DCtx); |
| 8343 | } |
| 8344 | QCBORDecode_ExitMap(&DCtx); |
| 8345 | QCBORDecode_Rewind(&DCtx); |
| 8346 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8347 | if(i != 100) { |
| 8348 | return 7010; |
| 8349 | } |
| 8350 | QCBORDecode_ExitArray(&DCtx); |
| 8351 | QCBORDecode_Rewind(&DCtx); |
| 8352 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8353 | i = 9; |
| 8354 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8355 | if(i != 100) { |
| 8356 | return 7020; |
| 8357 | } |
| 8358 | if(QCBORDecode_GetError(&DCtx)){ |
| 8359 | return 7030; |
| 8360 | } |
| 8361 | |
| 8362 | // Rewind an empty indefinite length map |
| 8363 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8364 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 8365 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8366 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8367 | if(i != 100) { |
| 8368 | return 7810; |
| 8369 | } |
| 8370 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8371 | |
| 8372 | /* Do it 5 times to be sure multiple rewinds work */ |
| 8373 | for(int n = 0; n < 5; n++) { |
| 8374 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 8375 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8376 | return 7800 + n; |
| 8377 | } |
| 8378 | QCBORDecode_Rewind(&DCtx); |
| 8379 | } |
| 8380 | QCBORDecode_ExitMap(&DCtx); |
| 8381 | QCBORDecode_Rewind(&DCtx); |
| 8382 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8383 | if(i != 100) { |
| 8384 | return 7810; |
| 8385 | } |
| 8386 | QCBORDecode_ExitArray(&DCtx); |
| 8387 | QCBORDecode_Rewind(&DCtx); |
| 8388 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8389 | i = 9; |
| 8390 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8391 | if(i != 100) { |
| 8392 | return 7820; |
| 8393 | } |
| 8394 | if(QCBORDecode_GetError(&DCtx)){ |
| 8395 | return 7830; |
| 8396 | } |
| 8397 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8398 | |
| 8399 | // Rewind an indefnite length byte-string wrapped sequence |
| 8400 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8401 | // TODO: rewrite this test to not use tags |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8402 | QCBORDecode_Init(&DCtx, |
| 8403 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 8404 | 0); |
| 8405 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 8406 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 8407 | |
| 8408 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8409 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8410 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8411 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8412 | /* TODO: This is what happens when trying to enter |
| 8413 | * indefinite-length byte string wrapped CBOR. Tolerate for |
| 8414 | * now. Eventually it needs to be fixed so this works, but that |
| 8415 | * is not simple. |
| 8416 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8417 | return 7300; |
| 8418 | } |
| 8419 | |
| 8420 | /* |
| 8421 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8422 | if(i != 42) { |
| 8423 | return 7110; |
| 8424 | } |
| 8425 | QCBORDecode_Rewind(&DCtx); |
| 8426 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8427 | if(i != 42) { |
| 8428 | return 7220; |
| 8429 | }*/ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8430 | |
| 8431 | #else /* QCBOR_DISABLE_TAGS */ |
| 8432 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 8433 | return 7301; |
| 8434 | } |
| 8435 | #endif /* QCBOR_DISABLE_TAGS */ |
| 8436 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8437 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8438 | |
| 8439 | |
| 8440 | // Rewind an indefnite length byte-string wrapped sequence |
| 8441 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8442 | return 0; |
| 8443 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8444 | |
| 8445 | |
| 8446 | |
| 8447 | |
| 8448 | static const uint8_t spBooleansInMap[] = |
| 8449 | { |
| 8450 | 0xa1, 0x08, 0xf5 |
| 8451 | }; |
| 8452 | |
| 8453 | static const uint8_t spBooleansInMapWrongType[] = |
| 8454 | { |
| 8455 | 0xa1, 0x08, 0xf6 |
| 8456 | }; |
| 8457 | |
| 8458 | static const uint8_t spBooleansInMapNWF[] = |
| 8459 | { |
| 8460 | 0xa1, 0x08, 0x1a |
| 8461 | }; |
| 8462 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8463 | static const uint8_t spNullInMap[] = |
| 8464 | { |
| 8465 | 0xa1, 0x08, 0xf6 |
| 8466 | }; |
| 8467 | |
| 8468 | static const uint8_t spUndefinedInMap[] = |
| 8469 | { |
| 8470 | 0xa1, 0x08, 0xf7 |
| 8471 | }; |
| 8472 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8473 | |
| 8474 | int32_t BoolTest(void) |
| 8475 | { |
| 8476 | QCBORDecodeContext DCtx; |
| 8477 | bool b; |
| 8478 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8479 | QCBORDecode_Init(&DCtx, |
| 8480 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8481 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8482 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8483 | QCBORDecode_GetBool(&DCtx, &b); |
| 8484 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 8485 | return 1; |
| 8486 | } |
| 8487 | |
| 8488 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 8489 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 8490 | return 2; |
| 8491 | } |
| 8492 | |
| 8493 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 8494 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 8495 | return 3; |
| 8496 | } |
| 8497 | |
| 8498 | |
| 8499 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 8500 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 8501 | return 4; |
| 8502 | } |
| 8503 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8504 | QCBORDecode_Init(&DCtx, |
| 8505 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 8506 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8507 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8508 | QCBORDecode_GetBool(&DCtx, &b); |
| 8509 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8510 | return 5; |
| 8511 | } |
| 8512 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8513 | QCBORDecode_Init(&DCtx, |
| 8514 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8515 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8516 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8517 | QCBORDecode_GetBool(&DCtx, &b); |
| 8518 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8519 | return 6; |
| 8520 | } |
| 8521 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8522 | |
| 8523 | QCBORDecode_Init(&DCtx, |
| 8524 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 8525 | 0); |
| 8526 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8527 | QCBORDecode_GetNull(&DCtx); |
| 8528 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8529 | return 7; |
| 8530 | } |
| 8531 | |
| 8532 | QCBORDecode_Init(&DCtx, |
| 8533 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8534 | 0); |
| 8535 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8536 | QCBORDecode_GetNull(&DCtx); |
| 8537 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8538 | return 8; |
| 8539 | } |
| 8540 | |
| 8541 | QCBORDecode_Init(&DCtx, |
| 8542 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 8543 | 0); |
| 8544 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8545 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 8546 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8547 | return 9; |
| 8548 | } |
| 8549 | |
| 8550 | QCBORDecode_Init(&DCtx, |
| 8551 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8552 | 0); |
| 8553 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8554 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 8555 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8556 | return 10; |
| 8557 | } |
| 8558 | |
| 8559 | QCBORDecode_Init(&DCtx, |
| 8560 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8561 | 0); |
| 8562 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8563 | QCBORDecode_GetUndefined(&DCtx); |
| 8564 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8565 | return 11; |
| 8566 | } |
| 8567 | |
| 8568 | QCBORDecode_Init(&DCtx, |
| 8569 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 8570 | 0); |
| 8571 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8572 | QCBORDecode_GetUndefined(&DCtx); |
| 8573 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8574 | return 12; |
| 8575 | } |
| 8576 | |
| 8577 | QCBORDecode_Init(&DCtx, |
| 8578 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8579 | 0); |
| 8580 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8581 | QCBORDecode_GetUndefined(&DCtx); |
| 8582 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8583 | return 13; |
| 8584 | } |
| 8585 | |
| 8586 | QCBORDecode_Init(&DCtx, |
| 8587 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 8588 | 0); |
| 8589 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8590 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 8591 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8592 | return 14; |
| 8593 | } |
| 8594 | |
| 8595 | QCBORDecode_Init(&DCtx, |
| 8596 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8597 | 0); |
| 8598 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8599 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 8600 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8601 | return 15; |
| 8602 | } |
| 8603 | |
| 8604 | QCBORDecode_Init(&DCtx, |
| 8605 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8606 | 0); |
| 8607 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8608 | QCBORDecode_GetUndefined(&DCtx); |
| 8609 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8610 | return 15; |
| 8611 | } |
| 8612 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8613 | return 0; |
| 8614 | } |