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 | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 119 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 120 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 121 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 122 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 123 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 124 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 125 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 126 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 127 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 128 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 129 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 130 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 131 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 132 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 133 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 134 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 135 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 136 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 137 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 138 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 139 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 140 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 141 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 142 | 0xff, 0xff}; |
| 143 | |
| 144 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 145 | // return CBOR error or -1 if type of value doesn't match |
| 146 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 147 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 148 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 149 | QCBORItem Item; |
| 150 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 151 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 152 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 153 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 154 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 155 | return -1; |
| 156 | |
| 157 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 158 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 159 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 160 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 161 | return -1; |
| 162 | |
| 163 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 164 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 165 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 166 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 167 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 168 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 169 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 170 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 171 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 172 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 173 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 174 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 175 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 176 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 177 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 178 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 179 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 180 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 181 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 182 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 183 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 184 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 185 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 186 | |
| 187 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 188 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 189 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 190 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 191 | Item.val.int64 != -2147483648) |
| 192 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 193 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 194 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 195 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 196 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 197 | Item.val.int64 != -2147483647) |
| 198 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 199 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 200 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 201 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 202 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 203 | Item.val.int64 != -65538) |
| 204 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 205 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 206 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 207 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 208 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 209 | Item.val.int64 != -65537) |
| 210 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 211 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 212 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 213 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 214 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 215 | Item.val.int64 != -65536) |
| 216 | return -1; |
| 217 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 218 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 219 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 220 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 221 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 222 | Item.val.int64 != -65535) |
| 223 | return -1; |
| 224 | |
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 != -65534) |
| 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 != -257) |
| 237 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 238 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 239 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 240 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 241 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 242 | Item.val.int64 != -256) |
| 243 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 244 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 245 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 246 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 247 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 248 | Item.val.int64 != -255) |
| 249 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 250 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 251 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 252 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 253 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 254 | Item.val.int64 != -254) |
| 255 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 256 | |
| 257 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 258 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 259 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 260 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 261 | Item.val.int64 != -25) |
| 262 | return -1; |
| 263 | |
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 != -24) |
| 269 | return -1; |
| 270 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 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 != -23) |
| 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 != -1) |
| 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 != 0) |
| 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 != 0) |
| 297 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 298 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 299 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 300 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 301 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 302 | Item.val.int64 != 1) |
| 303 | return -1; |
| 304 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 305 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 306 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 307 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 308 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 309 | Item.val.int64 != 22) |
| 310 | return -1; |
| 311 | |
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 != 23) |
| 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 != 24) |
| 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 != 25) |
| 331 | return -1; |
| 332 | |
| 333 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 334 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 335 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 336 | Item.val.int64 != 26) |
| 337 | return -1; |
| 338 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 339 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 340 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 341 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 342 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 343 | Item.val.int64 != 254) |
| 344 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 345 | |
| 346 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 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 != 255) |
| 351 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 352 | |
| 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 != 256) |
| 358 | return -1; |
| 359 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 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 != 257) |
| 365 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 366 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 367 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 368 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 369 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 370 | Item.val.int64 != 65534) |
| 371 | return -1; |
| 372 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 373 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 374 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 375 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 376 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 377 | Item.val.int64 != 65535) |
| 378 | return -1; |
| 379 | |
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 != 65536) |
| 385 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 386 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 387 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 388 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 389 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 390 | Item.val.int64 != 65537) |
| 391 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 392 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 393 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 394 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 395 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 396 | Item.val.int64 != 65538) |
| 397 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 398 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 399 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 400 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 401 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 402 | Item.val.int64 != 2147483647) |
| 403 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 404 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 405 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 406 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 407 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 408 | Item.val.int64 != 2147483647) |
| 409 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 410 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 411 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 412 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 413 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 414 | Item.val.int64 != 2147483648) |
| 415 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 416 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 417 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 418 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 419 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 420 | Item.val.int64 != 2147483649) |
| 421 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 422 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 423 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 424 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 425 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 426 | Item.val.int64 != 4294967294) |
| 427 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 428 | |
| 429 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 430 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 431 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 432 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 433 | Item.val.int64 != 4294967295) |
| 434 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 435 | |
| 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 != 4294967296) |
| 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 != 4294967297) |
| 448 | return -1; |
| 449 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 450 | |
| 451 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 452 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 453 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 454 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 455 | Item.val.int64 != 9223372036854775807LL) |
| 456 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 457 | |
| 458 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 459 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 460 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 461 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 462 | Item.val.uint64 != 18446744073709551615ULL) |
| 463 | return -1; |
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(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 467 | return -1; |
| 468 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 469 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 474 | /* One less than the smallest negative integer allowed in C. Decoding |
| 475 | this should fail. |
| 476 | -9223372036854775809 |
| 477 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 478 | static const uint8_t spTooSmallNegative[] = { |
| 479 | 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 483 | /* |
| 484 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 485 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 486 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 487 | int32_t IntegerValuesParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 488 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 489 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 490 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 491 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 492 | QCBORDecode_Init(&DCtx, |
| 493 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 494 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 495 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 496 | // The really big test of all successes |
| 497 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 498 | if(nReturn) { |
| 499 | return nReturn; |
| 500 | } |
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 one large negative integer that can be parsed |
| 503 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 504 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative), |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 505 | QCBOR_DECODE_MODE_NORMAL); |
| 506 | |
| 507 | QCBORItem item; |
| 508 | if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) { |
| 509 | nReturn = -4000; |
| 510 | } |
| 511 | |
| 512 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | |
| 516 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 517 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 518 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 519 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 520 | 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] | 521 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 522 | */ |
| 523 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 524 | static uint8_t spSimpleArrayBuffer[50]; |
| 525 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 526 | 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] | 527 | { |
| 528 | QCBOREncodeContext ECtx; |
| 529 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 530 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 531 | *pEncoded = NULL; |
| 532 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 533 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 534 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 535 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 536 | // and last with the buffer to do the actual encoding |
| 537 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 538 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 539 | QCBOREncode_OpenArray(&ECtx); |
| 540 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 541 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 542 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 543 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 544 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 545 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 546 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 547 | goto Done; |
| 548 | |
| 549 | if(*pEncoded != NULL) { |
| 550 | nReturn = 0; |
| 551 | goto Done; |
| 552 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 553 | |
| 554 | // Use static buffer to avoid dependency on malloc() |
| 555 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 556 | goto Done; |
| 557 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 558 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 559 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 560 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 561 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 562 | Done: |
| 563 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 567 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 568 | Some basic CBOR with map and array used in a lot of tests. |
| 569 | The map labels are all strings |
| 570 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 571 | { |
| 572 | "first integer": 42, |
| 573 | "an array of two strings": [ |
| 574 | "string1", "string2" |
| 575 | ], |
| 576 | "map in a map": { |
| 577 | "bytes 1": h'78787878', |
| 578 | "bytes 2": h'79797979', |
| 579 | "another int": 98, |
| 580 | "text 2": "lies, damn lies and statistics" |
| 581 | } |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 582 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 583 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 584 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 585 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 586 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 587 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 588 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 589 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 590 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 591 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 592 | 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, |
| 593 | 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4, |
| 594 | 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 595 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, |
| 596 | 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, |
| 597 | 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68, |
| 598 | 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, |
| 599 | 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 600 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, |
| 601 | 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, |
| 602 | 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, |
| 603 | 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 604 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 605 | |
| 606 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 607 | // Same as above, but with indefinite lengths. |
| 608 | static const uint8_t pValidMapIndefEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 609 | 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 610 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 611 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 612 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 613 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 614 | 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 615 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 616 | 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20, |
| 617 | 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, |
| 618 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 619 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 620 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 621 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 622 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 623 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 624 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 625 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 626 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 627 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 628 | 0xff, 0xff}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 629 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 630 | |
| 631 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 632 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 633 | size_t nLen, |
| 634 | int64_t *pInt1, |
| 635 | int64_t *pInt2, |
| 636 | const uint8_t **pBuf3, |
| 637 | size_t *pBuf3Len, |
| 638 | const uint8_t **pBuf4, |
| 639 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 640 | { |
| 641 | QCBORDecodeContext DCtx; |
| 642 | QCBORItem Item; |
| 643 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 644 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 645 | QCBORDecode_Init(&DCtx, |
| 646 | (UsefulBufC){pEncoded, nLen}, |
| 647 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 648 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 649 | // Make sure the first thing is a map |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 650 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 651 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 652 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 653 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 654 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 655 | // First integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 656 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 657 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 658 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 659 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 660 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 661 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 662 | // Second integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 663 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 664 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 665 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 666 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 667 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 668 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 669 | // First string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 670 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 671 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 672 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 673 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 674 | *pBuf3 = Item.val.string.ptr; |
| 675 | *pBuf3Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 676 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 677 | // Second string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 678 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 679 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 680 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 681 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 682 | *pBuf4 = Item.val.string.ptr; |
| 683 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 684 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 685 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 686 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 687 | Done: |
| 688 | return(nReturn); |
| 689 | } |
| 690 | |
| 691 | |
| 692 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 693 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 694 | int32_t SimpleArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 695 | { |
| 696 | uint8_t *pEncoded; |
| 697 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 698 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 699 | int64_t i1=0, i2=0; |
| 700 | size_t i3=0, i4=0; |
| 701 | const uint8_t *s3= (uint8_t *)""; |
| 702 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 703 | |
| 704 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 705 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 706 | return(-1); |
| 707 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 708 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 709 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 710 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 711 | if(i1 != 23 || |
| 712 | i2 != 6000 || |
| 713 | i3 != 8 || |
| 714 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 715 | memcmp("galactic", s3, 8) !=0 || |
| 716 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 717 | return(-1); |
| 718 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 719 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 720 | return(0); |
| 721 | } |
| 722 | |
| 723 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 724 | /* |
| 725 | [ |
| 726 | 0, |
| 727 | [], |
| 728 | [ |
| 729 | [], |
| 730 | [ |
| 731 | 0 |
| 732 | ], |
| 733 | {}, |
| 734 | { |
| 735 | 1: {}, |
| 736 | 2: {}, |
| 737 | 3: [] |
| 738 | } |
| 739 | ] |
| 740 | ] |
| 741 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 742 | static uint8_t sEmpties[] = { |
| 743 | 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 744 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 745 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 746 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 747 | /* Same as above, but with indefinte lengths */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 748 | static const uint8_t sEmptiesIndef[] = { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 749 | 0x9F, |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 750 | 0x00, |
| 751 | 0x9F, |
| 752 | 0xFF, |
| 753 | 0x9F, |
| 754 | 0x9F, |
| 755 | 0xFF, |
| 756 | 0x9F, |
| 757 | 0x00, |
| 758 | 0xFF, |
| 759 | 0xBF, |
| 760 | 0xFF, |
| 761 | 0xBF, |
| 762 | 0x01, |
| 763 | 0xBF, |
| 764 | 0xFF, |
| 765 | 0x02, |
| 766 | 0xBF, |
| 767 | 0xFF, |
| 768 | 0x03, |
| 769 | 0x9F, |
| 770 | 0xFF, |
| 771 | 0xFF, |
| 772 | 0xFF, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 773 | 0xFF}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 774 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 775 | |
| 776 | |
| 777 | static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts) |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 778 | { |
| 779 | QCBORDecodeContext DCtx; |
| 780 | QCBORItem Item; |
| 781 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 782 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 783 | input, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 784 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 785 | |
| 786 | // Array with 3 items |
| 787 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 788 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 789 | Item.uNestingLevel != 0 || |
| 790 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 791 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 792 | return -1; |
| 793 | } |
| 794 | |
| 795 | // An integer 0 |
| 796 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 797 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 798 | Item.uNestingLevel != 1 || |
| 799 | Item.uNextNestLevel != 1 || |
| 800 | Item.val.uint64 != 0) { |
| 801 | return -2; |
| 802 | } |
| 803 | |
| 804 | // An empty array |
| 805 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 806 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 807 | Item.uNestingLevel != 1 || |
| 808 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 809 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 810 | return -3; |
| 811 | } |
| 812 | |
| 813 | // An array with 4 items |
| 814 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 815 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 816 | Item.uNestingLevel != 1 || |
| 817 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 818 | (bCheckCounts && Item.val.uCount != 4)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 819 | return -4; |
| 820 | } |
| 821 | |
| 822 | // An empty array |
| 823 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 824 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 825 | Item.uNestingLevel != 2 || |
| 826 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 827 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 828 | return -5; |
| 829 | } |
| 830 | |
| 831 | // An array with 1 item |
| 832 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 833 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 834 | Item.uNestingLevel != 2 || |
| 835 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 836 | (bCheckCounts && Item.val.uCount != 1)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 837 | return -6; |
| 838 | } |
| 839 | |
| 840 | // An integer 0 |
| 841 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 842 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 843 | Item.uNestingLevel != 3 || |
| 844 | Item.uNextNestLevel != 2 || |
| 845 | Item.val.uint64 != 0) { |
| 846 | return -7; |
| 847 | } |
| 848 | |
| 849 | // An empty map |
| 850 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 851 | Item.uDataType != QCBOR_TYPE_MAP || |
| 852 | Item.uNestingLevel != 2 || |
| 853 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 854 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 855 | return -8; |
| 856 | } |
| 857 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 858 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 859 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 860 | Item.uDataType != QCBOR_TYPE_MAP || |
| 861 | Item.uNestingLevel != 2 || |
| 862 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 863 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 864 | return -9; |
| 865 | } |
| 866 | |
| 867 | // An empty map |
| 868 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 869 | Item.uDataType != QCBOR_TYPE_MAP || |
| 870 | Item.uNestingLevel != 3 || |
| 871 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 872 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 873 | return -10; |
| 874 | } |
| 875 | |
| 876 | // An empty map |
| 877 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 878 | Item.uDataType != QCBOR_TYPE_MAP || |
| 879 | Item.uNestingLevel != 3 || |
| 880 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 881 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 882 | return -11; |
| 883 | } |
| 884 | |
| 885 | // An empty array |
| 886 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 887 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 888 | Item.uNestingLevel != 3 || |
| 889 | Item.uNextNestLevel != 0 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 890 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 891 | return -12; |
| 892 | } |
| 893 | |
| 894 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 895 | return -13; |
| 896 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 901 | int32_t EmptyMapsAndArraysTest(void) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 902 | { |
| 903 | int nResult; |
| 904 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 905 | true); |
| 906 | if(nResult) { |
| 907 | return nResult; |
| 908 | } |
| 909 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 910 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 911 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 912 | false); |
| 913 | |
| 914 | if(nResult) { |
| 915 | return nResult -100; |
| 916 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 917 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 922 | |
| 923 | static const uint8_t sEmptyMap[] = { |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 924 | 0xA1, //# map(1) |
| 925 | 0x02, //# unsigned(2) |
| 926 | 0xA0, //# map(0) |
| 927 | }; |
| 928 | |
| 929 | int32_t ParseEmptyMapInMapTest(void) |
| 930 | { |
| 931 | QCBORDecodeContext DCtx; |
| 932 | QCBORItem Item; |
| 933 | int nReturn = 0; |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 934 | QCBORError uErr; |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 935 | |
| 936 | QCBORDecode_Init(&DCtx, |
| 937 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap), |
| 938 | QCBOR_DECODE_MODE_NORMAL); |
| 939 | |
| 940 | /* now open the first Map */ |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 941 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 942 | if(uErr != QCBOR_SUCCESS || |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 943 | Item.uDataType != QCBOR_TYPE_MAP) { |
| 944 | nReturn = -3; |
| 945 | goto done; |
| 946 | } |
| 947 | |
| 948 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0) { |
| 949 | nReturn = -1; |
| 950 | goto done; |
| 951 | } |
| 952 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 953 | Item.uNestingLevel != 1 || |
| 954 | Item.label.int64 != 2) { |
| 955 | nReturn = -2; |
| 956 | goto done; |
| 957 | } |
| 958 | |
| 959 | done: |
| 960 | return(nReturn); |
| 961 | } |
| 962 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 963 | |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 964 | /* [[[[[[[[[[]]]]]]]]]] */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 965 | static const uint8_t spDeepArrays[] = { |
| 966 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 967 | 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 968 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 969 | int32_t ParseDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 970 | { |
| 971 | QCBORDecodeContext DCtx; |
| 972 | int nReturn = 0; |
| 973 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 974 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 975 | QCBORDecode_Init(&DCtx, |
| 976 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 977 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 978 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 979 | for(i = 0; i < 10; i++) { |
| 980 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 981 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 982 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 983 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 984 | Item.uNestingLevel != i) { |
| 985 | nReturn = -1; |
| 986 | break; |
| 987 | } |
| 988 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 989 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 990 | return(nReturn); |
| 991 | } |
| 992 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 993 | /* Big enough to test nesting to the depth of 24 |
| 994 | [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]] |
| 995 | */ |
| 996 | static const uint8_t spTooDeepArrays[] = { |
| 997 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 998 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 999 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 1000 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1001 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1002 | int32_t ParseTooDeepArrayTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1003 | { |
| 1004 | QCBORDecodeContext DCtx; |
| 1005 | int nReturn = 0; |
| 1006 | int i; |
| 1007 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1008 | |
| 1009 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1010 | QCBORDecode_Init(&DCtx, |
| 1011 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 1012 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1013 | |
Laurence Lundblade | 3e0b230 | 2023-12-04 14:02:38 -0700 | [diff] [blame] | 1014 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1015 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1016 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 1017 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1018 | Item.uNestingLevel != i) { |
| 1019 | nReturn = -1; |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1023 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1024 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1025 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1026 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1027 | return(nReturn); |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1033 | int32_t ShortBufferParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1034 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1035 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1036 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1037 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1038 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1039 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1040 | QCBORDecode_Init(&DCtx, |
| 1041 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 1042 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1043 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1044 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1045 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1046 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1047 | nResult = -1; |
| 1048 | goto Done; |
| 1049 | } |
| 1050 | } |
| 1051 | Done: |
| 1052 | return nResult; |
| 1053 | } |
| 1054 | |
| 1055 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1056 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1057 | int32_t ShortBufferParseTest2(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1058 | { |
| 1059 | uint8_t *pEncoded; |
| 1060 | int nReturn; |
| 1061 | size_t nEncodedLen; |
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 | int64_t i1, i2; |
| 1064 | size_t i3, i4; |
| 1065 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1066 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1067 | nReturn = 0; |
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 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 1070 | return(-1); |
| 1071 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1072 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1073 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1074 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 1075 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1076 | if(nResult == 0) { |
| 1077 | nReturn = -1; |
| 1078 | } |
| 1079 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1080 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1081 | return(nReturn); |
| 1082 | } |
| 1083 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1084 | /* |
| 1085 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1086 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 1087 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1088 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1089 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1090 | { |
| 1091 | QCBORDecodeContext DCtx; |
| 1092 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1093 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1094 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1095 | QCBORDecode_Init(&DCtx, |
| 1096 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1097 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1098 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1099 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1100 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1101 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1102 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1103 | Item.val.uCount != 3) |
| 1104 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1105 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1106 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1107 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1108 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1109 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1110 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1111 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1112 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1113 | Item.uDataAlloc || |
| 1114 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1115 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1116 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1117 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1118 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1119 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1120 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1121 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1122 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1123 | Item.uDataAlloc || |
| 1124 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1125 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1126 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1127 | Item.val.uCount != 2) |
| 1128 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1129 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1130 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1131 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1132 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1133 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1134 | Item.uDataAlloc || |
| 1135 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1136 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1137 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1138 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1139 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1140 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1141 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1142 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1143 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1144 | Item.uDataAlloc || |
| 1145 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1146 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1147 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1148 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1149 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1150 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1151 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1152 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1153 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1154 | Item.uDataAlloc || |
| 1155 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1156 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1157 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1158 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1159 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1160 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1161 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1162 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1163 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1164 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1165 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1166 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1167 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1168 | Item.uDataAlloc || |
| 1169 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1170 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1171 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1172 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1173 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1174 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1175 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1176 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1177 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1178 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1179 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1180 | Item.uDataAlloc || |
| 1181 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1182 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1183 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1184 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1185 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1186 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1187 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1188 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1189 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1190 | Item.uDataAlloc || |
| 1191 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1192 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1193 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1194 | Item.val.int64 != 98) |
| 1195 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1196 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1197 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1198 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1199 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1200 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1201 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1202 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1203 | Item.uDataAlloc || |
| 1204 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1205 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1206 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1207 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1208 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1213 | /* |
| 1214 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1215 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1216 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1217 | int32_t ParseMapAsArrayTest(void) |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1218 | { |
| 1219 | QCBORDecodeContext DCtx; |
| 1220 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1221 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1222 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1223 | QCBORDecode_Init(&DCtx, |
| 1224 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1225 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1226 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1227 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1228 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1229 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1230 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1231 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1232 | return -1; |
| 1233 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1234 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1235 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1236 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1237 | } |
| 1238 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1239 | Item.uDataAlloc || |
| 1240 | Item.uLabelAlloc || |
| 1241 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1242 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1243 | return -2; |
| 1244 | } |
| 1245 | |
| 1246 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1247 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1248 | } |
| 1249 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1250 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1251 | Item.val.int64 != 42 || |
| 1252 | Item.uDataAlloc || |
| 1253 | Item.uLabelAlloc) { |
| 1254 | return -3; |
| 1255 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1256 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1257 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1258 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1259 | } |
| 1260 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1261 | Item.uDataAlloc || |
| 1262 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1263 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1264 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1265 | return -4; |
| 1266 | } |
| 1267 | |
| 1268 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1269 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1270 | } |
| 1271 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1272 | Item.uDataAlloc || |
| 1273 | Item.uLabelAlloc || |
| 1274 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1275 | Item.val.uCount != 2) { |
| 1276 | return -5; |
| 1277 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1278 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1279 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1280 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1281 | } |
| 1282 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1283 | Item.val.string.len != 7 || |
| 1284 | Item.uDataAlloc || |
| 1285 | Item.uLabelAlloc || |
| 1286 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1287 | return -6; |
| 1288 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1289 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1290 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1291 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1292 | } |
| 1293 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1294 | Item.uDataAlloc || |
| 1295 | Item.uLabelAlloc || |
| 1296 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1297 | return -7; |
| 1298 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1299 | |
| 1300 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1301 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1302 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1303 | } |
| 1304 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1305 | Item.uDataAlloc || |
| 1306 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1307 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1308 | return -8; |
| 1309 | } |
| 1310 | |
| 1311 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1312 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1313 | } |
| 1314 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1315 | Item.uDataAlloc || |
| 1316 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1317 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1318 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1319 | return -9; |
| 1320 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1321 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1322 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1323 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1324 | } |
| 1325 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1326 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1327 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1328 | Item.uDataAlloc || |
| 1329 | Item.uLabelAlloc) { |
| 1330 | return -10; |
| 1331 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1332 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1333 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1334 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1335 | } |
| 1336 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1337 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1338 | Item.uDataAlloc || |
| 1339 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1340 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1341 | return -11; |
| 1342 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1343 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1344 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1345 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1346 | } |
| 1347 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1348 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1349 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1350 | Item.uDataAlloc || |
| 1351 | Item.uLabelAlloc) { |
| 1352 | return -12; |
| 1353 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1354 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1355 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1356 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1357 | } |
| 1358 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1359 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1360 | Item.uDataAlloc || |
| 1361 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1362 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1363 | return -13; |
| 1364 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1365 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1366 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1367 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1368 | } |
| 1369 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1370 | Item.uDataAlloc || |
| 1371 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1372 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1373 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1374 | return -14; |
| 1375 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1376 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1377 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1378 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1379 | } |
| 1380 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1381 | Item.uDataAlloc || |
| 1382 | Item.uLabelAlloc || |
| 1383 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1384 | Item.val.int64 != 98) { |
| 1385 | return -15; |
| 1386 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1387 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1388 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1389 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1390 | } |
| 1391 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1392 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1393 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1394 | Item.uDataAlloc || |
| 1395 | Item.uLabelAlloc) { |
| 1396 | return -16; |
| 1397 | } |
| 1398 | |
| 1399 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1400 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1401 | } |
| 1402 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1403 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1404 | Item.uDataAlloc || |
| 1405 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1406 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1407 | return -17; |
| 1408 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1409 | |
| 1410 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1411 | /* |
| 1412 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1413 | map that when interpreted as an array will be too many. Test |
| 1414 | data just has the start of the map, not all the items in the map. |
| 1415 | */ |
| 1416 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1417 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1418 | QCBORDecode_Init(&DCtx, |
| 1419 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1420 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1421 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1422 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1423 | return -50; |
| 1424 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1425 | |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1426 | // TODO: test decoding of labels that are arrays or such |
| 1427 | // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY |
| 1428 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1429 | return 0; |
| 1430 | } |
| 1431 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1432 | |
| 1433 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1434 | Fully or partially decode pValidMapEncoded. When |
| 1435 | partially decoding check for the right error code. |
| 1436 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1437 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1438 | The partial decodes test error conditions of |
| 1439 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1440 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1441 | This could be combined with the above test |
| 1442 | and made prettier and maybe a little more |
| 1443 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1444 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1445 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1446 | { |
| 1447 | QCBORDecodeContext DCtx; |
| 1448 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1449 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1450 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1451 | QCBORDecode_Init(&DCtx, |
| 1452 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1453 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1454 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1455 | if(nLevel < 1) { |
| 1456 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1457 | return -1; |
| 1458 | } else { |
| 1459 | return 0; |
| 1460 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1461 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1462 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1463 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1464 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1465 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1466 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1467 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1468 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1469 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1470 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1471 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1472 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1473 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1474 | } else { |
| 1475 | return 0; |
| 1476 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1477 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1478 | |
| 1479 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1480 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1481 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1482 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1483 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1484 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1485 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1486 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1487 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1488 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1489 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1490 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1491 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1492 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1493 | } else { |
| 1494 | return 0; |
| 1495 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1496 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1497 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1498 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1499 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1500 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1501 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1502 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1503 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1504 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1505 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1506 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1507 | |
| 1508 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1509 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1510 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1511 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1512 | } else { |
| 1513 | return 0; |
| 1514 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1515 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1516 | |
| 1517 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1518 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1519 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1520 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1521 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1522 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1523 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1524 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1525 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1526 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1527 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1528 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1529 | } else { |
| 1530 | return 0; |
| 1531 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1532 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1533 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1534 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1535 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1536 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1537 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1538 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1539 | return -10; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1540 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1541 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1542 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1543 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1544 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1545 | } else { |
| 1546 | return 0; |
| 1547 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1548 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1549 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1550 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1551 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1552 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1553 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1554 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1555 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1556 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1557 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1558 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1559 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1560 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1561 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1562 | } else { |
| 1563 | return 0; |
| 1564 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1565 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1566 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1567 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1568 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1569 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1570 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1571 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1572 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1573 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1574 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1575 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1576 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1577 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1578 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1579 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1580 | } else { |
| 1581 | return 0; |
| 1582 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1583 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1584 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1585 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1586 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1587 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1588 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1589 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1590 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1591 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1592 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1593 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1594 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1595 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1596 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1597 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1598 | } else { |
| 1599 | return 0; |
| 1600 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1601 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1602 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1603 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1604 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1605 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1606 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1607 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1608 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1609 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1610 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1611 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1612 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1613 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1614 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1615 | } else { |
| 1616 | return 0; |
| 1617 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1618 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1619 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1620 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1621 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1622 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1623 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1624 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1625 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1626 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1627 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1628 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1629 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1630 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1631 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1632 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1633 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1639 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1640 | int32_t ParseMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1641 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1642 | // Parse a moderatly complex map structure very thoroughly |
| 1643 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 1644 | if(nResult) { |
| 1645 | return nResult; |
| 1646 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1647 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1648 | // Again, but in strings-only mode. It should succeed since the input |
| 1649 | // map has only string labels. |
| 1650 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 1651 | if(nResult) { |
| 1652 | return nResult; |
| 1653 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1654 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1655 | // Again, but try to finish the decoding before the end of the |
| 1656 | // input at 10 different place and see that the right error code |
| 1657 | // is returned. |
| 1658 | for(int i = 0; i < 10; i++) { |
| 1659 | nResult = ExtraBytesTest(i); |
| 1660 | if(nResult) { |
| 1661 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1662 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1663 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1664 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1665 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1669 | /* The simple-values including some not well formed */ |
| 1670 | static const uint8_t spSimpleValues[] = { |
| 1671 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 1672 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 1673 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1674 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1675 | int32_t ParseSimpleTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1676 | { |
| 1677 | QCBORDecodeContext DCtx; |
| 1678 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1679 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1680 | |
| 1681 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1682 | QCBORDecode_Init(&DCtx, |
| 1683 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1684 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1685 | |
| 1686 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1687 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1688 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1689 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1690 | Item.val.uCount != 10) |
| 1691 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1692 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1693 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1694 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1695 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1696 | return -1; |
| 1697 | |
| 1698 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1699 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1700 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1701 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1702 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1703 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1704 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1705 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1706 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1707 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1708 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1709 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1710 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1711 | return -1; |
| 1712 | |
| 1713 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1714 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1715 | return -1; |
| 1716 | |
| 1717 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1718 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1719 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1720 | return -1; |
| 1721 | |
| 1722 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1723 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1724 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1725 | return -1; |
| 1726 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1727 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1728 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1729 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1730 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1731 | return -1; |
| 1732 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1733 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1734 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1735 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1736 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1737 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1738 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1739 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1740 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1741 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1742 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1743 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1744 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1745 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1746 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1747 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1751 | int32_t NotWellFormedTests(void) |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1752 | { |
| 1753 | // Loop over all the not-well-formed instance of CBOR |
| 1754 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1755 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 1756 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1757 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 1758 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 1759 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 1760 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1761 | if(nIterate == 86) { |
| 1762 | nIterate = 86; |
| 1763 | } |
| 1764 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1765 | // Set up decoder context. String allocator needed for indefinite |
| 1766 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1767 | QCBORDecodeContext DCtx; |
| 1768 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1769 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1770 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1771 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1772 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1773 | |
| 1774 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1775 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1776 | do { |
| 1777 | QCBORItem Item; |
| 1778 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1779 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1780 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1781 | |
| 1782 | // Every test vector must fail with |
| 1783 | // a not-well-formed error. If not |
| 1784 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1785 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1786 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1787 | /* Return index of failure and QCBOR error in the result */ |
| 1788 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1795 | // 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] | 1796 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1797 | UsefulBufC Input; |
| 1798 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1799 | }; |
| 1800 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1801 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1802 | static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1803 | { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1804 | for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1805 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1806 | QCBORError uCBORError; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1807 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1808 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1809 | |
| 1810 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1811 | // Set up the decoding context including a memory pool so that |
| 1812 | // indefinite length items can be checked |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1813 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1814 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1815 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1816 | if(uCBORError) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1817 | return -9; |
| 1818 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1819 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1820 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1821 | const size_t nIndexx = (size_t)(pF - pFailInputs); |
| 1822 | if(nIndexx == 8) { |
| 1823 | uCBORError = 9; |
| 1824 | } |
| 1825 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 1826 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1827 | // Iterate until there is an error of some sort error |
| 1828 | QCBORItem Item; |
| 1829 | do { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1830 | // Set to something none-zero, something other than QCBOR_TYPE_NONE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1831 | memset(&Item, 0x33, sizeof(Item)); |
| 1832 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1833 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1834 | } while(uCBORError == QCBOR_SUCCESS); |
| 1835 | |
| 1836 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1837 | |
| 1838 | // Must get the expected error or the this test fails |
| 1839 | // The data and label type must also be QCBOR_TYPE_NONE |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1840 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1841 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1842 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1843 | // return index of CBOR + 100 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1844 | const size_t nIndex = (size_t)(pF - pFailInputs); |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1845 | return (int32_t)(nIndex * 100 + uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1853 | static const struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1854 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1855 | // returned is also checked. |
| 1856 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1857 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1858 | // Indefinite length strings must be closed off |
| 1859 | // An indefinite length byte string not closed off |
| 1860 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1861 | // An indefinite length text string not closed off |
| 1862 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1863 | |
| 1864 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1865 | // All the chunks in an indefinite length string must be of the type of |
| 1866 | // indefinite length string |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1867 | // indefinite length byte string with text string chunk |
| 1868 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1869 | // indefinite length text string with a byte string chunk |
| 1870 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1871 | // indefinite length byte string with an positive integer chunk |
| 1872 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1873 | // indefinite length byte string with an negative integer chunk |
| 1874 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1875 | // indefinite length byte string with an array chunk |
| 1876 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1877 | // indefinite length byte string with an map chunk |
| 1878 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1879 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1880 | // indefinite length byte string with tagged integer chunk |
| 1881 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1882 | #else |
| 1883 | // indefinite length byte string with tagged integer chunk |
| 1884 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_TAGS_DISABLED }, |
| 1885 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1886 | // indefinite length byte string with an simple type chunk |
| 1887 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1888 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1889 | // indefinite length text string with indefinite string inside |
| 1890 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1891 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1892 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1893 | |
| 1894 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1895 | // An indefinite length text string not closed off |
| 1896 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1897 | |
| 1898 | |
| 1899 | // All the chunks in an indefinite length string must be of the type of |
| 1900 | // indefinite length string |
| 1901 | // indefinite length byte string with text string chunk |
| 1902 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1903 | // indefinite length text string with a byte string chunk |
| 1904 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1905 | // indefinite length byte string with an positive integer chunk |
| 1906 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1907 | // indefinite length byte string with an negative integer chunk |
| 1908 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1909 | // indefinite length byte string with an array chunk |
| 1910 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1911 | // indefinite length byte string with an map chunk |
| 1912 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1913 | // indefinite length byte string with tagged integer chunk |
| 1914 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1915 | // indefinite length byte string with an simple type chunk |
| 1916 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1917 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1918 | // indefinite length text string with indefinite string inside |
| 1919 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1920 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1921 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1922 | |
| 1923 | // Definte length maps and arrays must be closed by having the right number of items |
| 1924 | // 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] | 1925 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1926 | // 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] | 1927 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1928 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1929 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1930 | // 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] | 1931 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1932 | // 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] | 1933 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1934 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1935 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1936 | // Indefinte length maps and arrays must be ended by a break |
| 1937 | // Indefinite length array with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1938 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1939 | // Indefinite length array with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1940 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1941 | // Indefinite length map with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1942 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1943 | // Indefinite length map with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1944 | { {(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] | 1945 | |
| 1946 | |
| 1947 | // 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] | 1948 | // Unclosed indefinite array containing a closed definite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1949 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1950 | // Definite length array containing an unclosed indefinite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1951 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1952 | // Unclosed indefinite map containing a closed definite length array |
| 1953 | { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
| 1954 | // Definite length map containing an unclosed indefinite length array |
| 1955 | { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1956 | // Deeply nested definite length arrays with deepest one unclosed |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1957 | { {(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] | 1958 | // Deeply nested indefinite length arrays with deepest one unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1959 | { {(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] | 1960 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1961 | { {(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] | 1962 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1963 | { {(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] | 1964 | // Unclosed indefinite length map in definite length maps |
| 1965 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8}, |
| 1966 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1967 | // Unclosed definite length map in indefinite length maps |
| 1968 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1969 | // Unclosed indefinite length array in definite length maps |
| 1970 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8}, |
| 1971 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1972 | // Unclosed definite length array in indefinite length maps |
| 1973 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1974 | // Unclosed indefinite length map in definite length arrays |
| 1975 | { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1976 | // Unclosed definite length map in indefinite length arrays |
| 1977 | { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1978 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1979 | |
| 1980 | // The "argument" for the data item is incomplete |
| 1981 | // Positive integer missing 1 byte argument |
| 1982 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1983 | // Positive integer missing 2 byte argument |
| 1984 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1985 | // Positive integer missing 4 byte argument |
| 1986 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1987 | // Positive integer missing 8 byte argument |
| 1988 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1989 | // Positive integer missing 1 byte of 2 byte argument |
| 1990 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1991 | // Positive integer missing 2 bytes of 4 byte argument |
| 1992 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1993 | // Positive integer missing 1 bytes of 7 byte argument |
| 1994 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1995 | // Negative integer missing 1 byte argument |
| 1996 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1997 | // Binary string missing 1 byte argument |
| 1998 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1999 | // Text string missing 1 byte argument |
| 2000 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 2001 | // Array missing 1 byte argument |
| 2002 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 2003 | // Map missing 1 byte argument |
| 2004 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 2005 | // Tag missing 1 byte argument |
| 2006 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 2007 | // Simple missing 1 byte argument |
| 2008 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2009 | // half-precision with 1 byte argument |
| 2010 | { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2011 | // single-precision with 2 byte argument |
| 2012 | { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 2013 | // double-precision with 3 byte argument |
| 2014 | { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 2015 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2016 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2017 | // Tag with no content |
| 2018 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2019 | #else /* QCBOR_DISABLE_TAGS */ |
| 2020 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_TAGS_DISABLED }, |
| 2021 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2022 | |
| 2023 | // Breaks must not occur in definite length arrays and maps |
| 2024 | // Array of length 1 with sole member replaced by a break |
| 2025 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2026 | // Array of length 2 with 2nd member replaced by a break |
| 2027 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2028 | // Map of length 1 with sole member label replaced by a break |
| 2029 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2030 | // Map of length 1 with sole member label replaced by break |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2031 | // Alternate representation that some decoders handle differently |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2032 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2033 | // Array of length 1 with 2nd member value replaced by a break |
| 2034 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2035 | // Map of length 2 with 2nd member replaced by a break |
| 2036 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 2037 | |
| 2038 | |
| 2039 | // Breaks must not occur on their own out of an indefinite length data item |
| 2040 | // A bare break is not well formed |
| 2041 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 2042 | // A bare break after a zero length definite length array |
| 2043 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2044 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2045 | // A bare break after a zero length indefinite length map |
| 2046 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2047 | // A break inside a definite length array inside an indefenite length array |
| 2048 | { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2049 | // Complicated mixed nesting with break outside indefinite length array |
| 2050 | { {(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] | 2051 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2052 | |
| 2053 | |
| 2054 | // Forbidden two byte encodings of simple types |
| 2055 | // Must use 0xe0 instead |
| 2056 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2057 | // Should use 0xe1 instead |
| 2058 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2059 | // Should use 0xe2 instead |
| 2060 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2061 | // Should use 0xe3 instead |
| 2062 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2063 | // Should use 0xe4 instead |
| 2064 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2065 | // Should use 0xe5 instead |
| 2066 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2067 | // Should use 0xe6 instead |
| 2068 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2069 | // Should use 0xe7 instead |
| 2070 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2071 | // Should use 0xe8 instead |
| 2072 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2073 | // Should use 0xe9 instead |
| 2074 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2075 | // Should use 0xea instead |
| 2076 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2077 | // Should use 0xeb instead |
| 2078 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2079 | // Should use 0xec instead |
| 2080 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2081 | // Should use 0xed instead |
| 2082 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2083 | // Should use 0xee instead |
| 2084 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2085 | // Should use 0xef instead |
| 2086 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2087 | // Should use 0xf0 instead |
| 2088 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2089 | // Should use 0xf1 instead |
| 2090 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2091 | // Should use 0xf2 instead |
| 2092 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2093 | // Must use 0xf3 instead |
| 2094 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2095 | // Must use 0xf4 instead |
| 2096 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2097 | // Must use 0xf5 instead |
| 2098 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2099 | // Must use 0xf6 instead |
| 2100 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2101 | // Must use 0xf7 instead |
| 2102 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2103 | // Must use 0xf8 instead |
| 2104 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2105 | // Reserved |
| 2106 | { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2107 | |
| 2108 | // Integers with additional info indefinite length |
| 2109 | // Positive integer with additional info indefinite length |
| 2110 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2111 | // Negative integer with additional info indefinite length |
| 2112 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2113 | |
| 2114 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2115 | // CBOR tag with "argument" an indefinite length |
| 2116 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 2117 | // CBOR tag with "argument" an indefinite length alternate vector |
| 2118 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2119 | #else /* QCBOR_DISABLE_TAGS */ |
| 2120 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED }, |
| 2121 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_TAGS_DISABLED }, |
| 2122 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2123 | |
| 2124 | // Missing bytes from a deterministic length string |
| 2125 | // A byte string is of length 1 without the 1 byte |
| 2126 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 2127 | // A text string is of length 1 without the 1 byte |
| 2128 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2129 | |
| 2130 | #if SIZE_MAX > 2147483647 |
Laurence Lundblade | 42272e4 | 2020-01-31 07:50:53 -0800 | [diff] [blame] | 2131 | // Byte string should have 2^32-15 bytes, but has one |
| 2132 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2133 | // Byte string should have 2^32-15 bytes, but has one |
| 2134 | { {(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] | 2135 | // Byte string should have 2^64 bytes, but has 3 |
| 2136 | { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2137 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2138 | // Text string should have 2^64 bytes, but has 3 |
| 2139 | { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2140 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2141 | #else |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2142 | // Byte string should have 2^32-15 bytes, but has one |
| 2143 | { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2144 | // Byte string should have 2^32-15 bytes, but has one |
| 2145 | { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2146 | // Byte string should have 2^16 bytes, but has 3 |
| 2147 | { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2148 | // Text string should have 2^64 bytes, but has 3 |
| 2149 | { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2150 | #endif |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2151 | |
| 2152 | // Use of unassigned additional information values |
| 2153 | // Major type positive integer with reserved value 28 |
| 2154 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2155 | // Major type positive integer with reserved value 29 |
| 2156 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2157 | // Major type positive integer with reserved value 30 |
| 2158 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2159 | // Major type negative integer with reserved value 28 |
| 2160 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2161 | // Major type negative integer with reserved value 29 |
| 2162 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2163 | // Major type negative integer with reserved value 30 |
| 2164 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2165 | // Major type byte string with reserved value 28 length |
| 2166 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2167 | // Major type byte string with reserved value 29 length |
| 2168 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2169 | // Major type byte string with reserved value 30 length |
| 2170 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2171 | // Major type text string with reserved value 28 length |
| 2172 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2173 | // Major type text string with reserved value 29 length |
| 2174 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2175 | // Major type text string with reserved value 30 length |
| 2176 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2177 | // Major type array with reserved value 28 length |
| 2178 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2179 | // Major type array with reserved value 29 length |
| 2180 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2181 | // Major type array with reserved value 30 length |
| 2182 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2183 | // Major type map with reserved value 28 length |
| 2184 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2185 | // Major type map with reserved value 29 length |
| 2186 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2187 | // Major type map with reserved value 30 length |
| 2188 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2189 | // Major type tag with reserved value 28 length |
| 2190 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2191 | // Major type tag with reserved value 29 length |
| 2192 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2193 | // Major type tag with reserved value 30 length |
| 2194 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2195 | // Major type simple with reserved value 28 length |
| 2196 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2197 | // Major type simple with reserved value 29 length |
| 2198 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2199 | // Major type simple with reserved value 30 length |
| 2200 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2201 | |
| 2202 | |
| 2203 | // Maps must have an even number of data items (key & value) |
| 2204 | // Map with 1 item when it should have 2 |
| 2205 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2206 | // Map with 3 item when it should have 4 |
| 2207 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2208 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2209 | // Map with 1 item when it should have 2 |
| 2210 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2211 | // Map with 3 item when it should have 4 |
| 2212 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2213 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2214 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2215 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2216 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2217 | // Text-based date, with an integer |
| 2218 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2219 | // Epoch date, with an byte string |
| 2220 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2221 | // tagged as both epoch and string dates |
| 2222 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2223 | // big num tagged an int, not a byte string |
| 2224 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2225 | #else /* QCBOR_DISABLE_TAGS */ |
| 2226 | // Text-based date, with an integer |
| 2227 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED }, |
| 2228 | // Epoch date, with an byte string |
| 2229 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_TAGS_DISABLED }, |
| 2230 | // tagged as both epoch and string dates |
| 2231 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_TAGS_DISABLED }, |
| 2232 | // big num tagged an int, not a byte string |
| 2233 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED }, |
| 2234 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2235 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2236 | }; |
| 2237 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2238 | int32_t DecodeFailureTests(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2239 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2240 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2241 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2242 | nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2243 | if(nResult) { |
| 2244 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2245 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2246 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2247 | // Corrupt the UsefulInputBuf and see that |
| 2248 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2249 | QCBORDecodeContext DCtx; |
| 2250 | QCBORItem Item; |
| 2251 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2252 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2253 | QCBORDecode_Init(&DCtx, |
| 2254 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2255 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2256 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2257 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2258 | return (int32_t)uQCBORError; |
| 2259 | } |
| 2260 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 2261 | // This wasn't supposed to happen |
| 2262 | return -1; |
| 2263 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2264 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2265 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2266 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2267 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2268 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2269 | // Did not get back the error expected |
| 2270 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2271 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2272 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2273 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2274 | /* |
| 2275 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 2276 | tests here can be performed to see that the max length |
| 2277 | error check works correctly. See DecodeBytes(). If the max |
| 2278 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2279 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2280 | This test will automatocally adapt the all CPU sizes |
| 2281 | through the use of SIZE_MAX. |
| 2282 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2283 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2284 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2285 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2286 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2287 | // This makes a CBOR head with a text string that is very long |
| 2288 | // but doesn't fill in the bytes of the text string as that is |
| 2289 | // not needed to test this part of QCBOR. |
| 2290 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 2291 | |
| 2292 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 2293 | |
| 2294 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2295 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2296 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2297 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2298 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2302 | /* Try all 256 values of the byte at nLen including recursing for |
| 2303 | each of the values to try values at nLen+1 ... up to nLenMax |
| 2304 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2305 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2306 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2307 | if(nLen >= nLenMax) { |
| 2308 | return; |
| 2309 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2310 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2311 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 2312 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2313 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2314 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2315 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2316 | // Get ready to parse |
| 2317 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2318 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2319 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2320 | // Parse by getting the next item until an error occurs |
| 2321 | // Just about every possible decoder error can occur here |
| 2322 | // The goal of this test is not to check for the correct |
| 2323 | // error since that is not really possible. It is to |
| 2324 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2325 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2326 | QCBORItem Item; |
| 2327 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2328 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2329 | break; |
| 2330 | } |
| 2331 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2332 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2333 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2338 | int32_t ComprehensiveInputTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2339 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2340 | // Size 2 tests 64K inputs and runs quickly |
| 2341 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2342 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2343 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2344 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2345 | return 0; |
| 2346 | } |
| 2347 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2348 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2349 | int32_t BigComprehensiveInputTest(void) |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2350 | { |
| 2351 | // size 3 tests 16 million inputs and runs OK |
| 2352 | // in seconds on fast machines. Size 4 takes |
| 2353 | // 10+ minutes and 5 half a day on fast |
| 2354 | // machines. This test is kept separate from |
| 2355 | // the others so as to no slow down the use |
| 2356 | // of them as a very frequent regression. |
| 2357 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2358 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2359 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2360 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2361 | return 0; |
| 2362 | } |
| 2363 | |
| 2364 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2365 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2366 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2367 | 0xc0, // tag for string date |
| 2368 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2369 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2370 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2371 | 0xc0, // tag for string date |
| 2372 | 0x00, // Wrong type for a string date |
| 2373 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2374 | /* 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] | 2375 | 0xc1, // tag for epoch date |
| 2376 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2377 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2378 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2379 | 0xc1, |
| 2380 | 0x62, 'h', 'i', // wrong type tagged |
| 2381 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2382 | /* 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] | 2383 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2384 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2385 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2386 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2387 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2388 | 0xc1, // tag for epoch date |
| 2389 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
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 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2392 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2393 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2394 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2395 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2396 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2397 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2398 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2399 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2400 | 0xc1, // tag for epoch date |
| 2401 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2402 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2403 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2404 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2405 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2406 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2407 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2408 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2409 | 0xc1, // tag for epoch date |
| 2410 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 2411 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2412 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2413 | 0xc1, |
| 2414 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 2415 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2416 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2417 | 0xc1, // tag for epoch date |
| 2418 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2419 | }; |
| 2420 | |
| 2421 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2422 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2423 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2424 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 2425 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
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 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2428 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2429 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2430 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2431 | return diff > 0.0000001; |
| 2432 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2433 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2434 | |
| 2435 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2436 | /* Test date decoding using GetNext() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2437 | int32_t DateParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2438 | { |
| 2439 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2440 | QCBORItem Item; |
| 2441 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2442 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2443 | QCBORDecode_Init(&DCtx, |
| 2444 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2445 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2446 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2447 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2448 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2449 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2450 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2451 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2452 | UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2453 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2454 | } |
| 2455 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2456 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2457 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2458 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2459 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2462 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 2463 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2464 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2465 | return -4; |
| 2466 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2467 | if(uError == QCBOR_SUCCESS) { |
| 2468 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2469 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2470 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2471 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2472 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2473 | ) { |
| 2474 | return -5; |
| 2475 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2476 | } |
| 2477 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2478 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2479 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 2480 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2481 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2482 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2483 | /* 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] | 2484 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 2485 | // The date is decoded and the two tags are returned. This is to |
| 2486 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2487 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2488 | return -7; |
| 2489 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2490 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2491 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2492 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2493 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2494 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2495 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2496 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2497 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2498 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2499 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2500 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2501 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2502 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2503 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2504 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 2505 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2506 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2507 | return -10; |
| 2508 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2509 | if(uError == QCBOR_SUCCESS) { |
| 2510 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2511 | Item.val.epochDate.nSeconds != 1 |
| 2512 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2513 | || 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] | 2514 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2515 | ) { |
| 2516 | return -11; |
| 2517 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2518 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2519 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2520 | /* 8. Epoch date with too-large single-precision float */ |
| 2521 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2522 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2523 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2524 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2525 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2526 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 2527 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2528 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2529 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2530 | } |
| 2531 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2532 | /* 10. Epoch date with largest supported double-precision value */ |
| 2533 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2534 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2535 | return -14; |
| 2536 | } |
| 2537 | if(uError == QCBOR_SUCCESS) { |
| 2538 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2539 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 2540 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2541 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 2542 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2543 | ) { |
| 2544 | return -14; |
| 2545 | } |
| 2546 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2547 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2548 | /* 11. Epoch date with single-precision NaN */ |
| 2549 | 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] | 2550 | return -15; |
| 2551 | } |
| 2552 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2553 | /* 12. Epoch date with double-precision plus infinity */ |
| 2554 | 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] | 2555 | return -16; |
| 2556 | } |
| 2557 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2558 | /* 13. Epoch date with half-precision negative infinity */ |
| 2559 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2560 | 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] | 2561 | return -17; |
| 2562 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2563 | |
| 2564 | return 0; |
| 2565 | } |
| 2566 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2567 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2568 | /* |
| 2569 | Test cases covered here. Some items cover more than one of these. |
| 2570 | positive integer (zero counts as a positive integer) |
| 2571 | negative integer |
| 2572 | half-precision float |
| 2573 | single-precision float |
| 2574 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2575 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2576 | float Overflow error |
| 2577 | Wrong type error for epoch |
| 2578 | Wrong type error for date string |
| 2579 | float disabled error |
| 2580 | half-precision disabled error |
| 2581 | -Infinity |
| 2582 | Slightly too large integer |
| 2583 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2584 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2585 | Get epoch by int |
| 2586 | Get string by int |
| 2587 | Get epoch by string |
| 2588 | Get string by string |
| 2589 | Fail to get epoch by wrong int label |
| 2590 | Fail to get string by wrong string label |
| 2591 | Fail to get epoch by string because it is invalid |
| 2592 | Fail to get epoch by int because it is invalid |
| 2593 | |
| 2594 | Untagged values |
| 2595 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2596 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2597 | 0x87, // array of 7 items |
| 2598 | |
| 2599 | 0xa6, // Open a map for tests involving untagged items with labels. |
| 2600 | |
| 2601 | // Untagged integer 0 |
| 2602 | 0x08, |
| 2603 | 0x00, |
| 2604 | |
| 2605 | // Utagged date string with string label y |
| 2606 | 0x61, 0x79, |
| 2607 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
| 2608 | |
| 2609 | // Untagged single-precision float with value 3.14 with string label x |
| 2610 | 0x61, 0x78, |
| 2611 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 2612 | |
| 2613 | // Untagged half-precision float with value -2 |
| 2614 | 0x09, |
| 2615 | 0xF9, 0xC0, 0x00, |
| 2616 | |
| 2617 | /* Untagged date-only date string */ |
| 2618 | 0x18, 0x63, |
| 2619 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2620 | |
| 2621 | /* Untagged days-count epoch date */ |
| 2622 | 0x11, |
| 2623 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 2624 | |
| 2625 | // End of map, back to array |
| 2626 | |
| 2627 | 0xa7, // Open map of tagged items with labels |
| 2628 | |
| 2629 | 0x00, |
| 2630 | 0xc0, // tag for string date |
| 2631 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
| 2632 | |
| 2633 | |
| 2634 | 0x01, |
| 2635 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
| 2636 | 0xc1, // tag for epoch date |
| 2637 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2638 | |
| 2639 | 0x05, |
| 2640 | 0xc1, |
| 2641 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 2642 | |
| 2643 | |
| 2644 | 0x07, |
| 2645 | 0xc1, // tag for epoch date |
| 2646 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2647 | |
| 2648 | /* Tagged days-count epoch date */ |
| 2649 | 0x63, 0x53, 0x44, 0x45, |
| 2650 | 0xD8, 0x64, /* tag(100) */ |
| 2651 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 2652 | |
| 2653 | // Untagged -1000 with label z |
| 2654 | 0x61, 0x7a, |
| 2655 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
| 2656 | 0x39, 0x03, 0xe7, |
| 2657 | |
| 2658 | /* Tagged date-only date string */ |
| 2659 | 0x63, 0x53, 0x44, 0x53, |
| 2660 | 0xD9, 0x03, 0xEC, |
| 2661 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2662 | |
| 2663 | // End of map of tagged items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2664 | |
| 2665 | 0xc1, |
| 2666 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 2667 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2668 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2669 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 2670 | |
| 2671 | 0xc1, // tag for epoch date |
| 2672 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 2673 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2674 | // These two at the end because they are unrecoverable errors |
| 2675 | 0xc1, // tag for epoch date |
| 2676 | 0x80, // Erroneous empty array as content for date |
| 2677 | |
| 2678 | 0xc0, // tag for string date |
| 2679 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2680 | }; |
| 2681 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2682 | int32_t SpiffyDateDecodeTest(void) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2683 | { |
| 2684 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2685 | QCBORError uError; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2686 | int64_t nEpochDate3, nEpochDate5, |
| 2687 | nEpochDate4, nEpochDate6, |
| 2688 | nEpochDays2; |
| 2689 | UsefulBufC StringDate1, StringDate2, StringDays2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2690 | |
| 2691 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2692 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2693 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2694 | |
| 2695 | /* Items are in an array or map to test look up by label and other |
| 2696 | * that might not occur in isolated items. But it does make the |
| 2697 | * test a bit messy. */ |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2698 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2699 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2700 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2701 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2702 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2703 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2704 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2705 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2706 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2707 | return 104; |
| 2708 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2709 | if(uError == QCBOR_SUCCESS) { |
| 2710 | if(nEpochDate5 != 3) { |
| 2711 | return 103; |
| 2712 | } |
| 2713 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2714 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2715 | // A half-precision date with value -2 FFF |
| 2716 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2717 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2718 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2719 | 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] | 2720 | return 106; |
| 2721 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2722 | if(uError == QCBOR_SUCCESS) { |
| 2723 | if(nEpochDate4 != -2) { |
| 2724 | return 105; |
| 2725 | } |
| 2726 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2727 | |
| 2728 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2729 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 2730 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2731 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2732 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2733 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2734 | return 107; |
| 2735 | } |
| 2736 | |
| 2737 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2738 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2739 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2740 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2741 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2742 | return 108; |
| 2743 | } |
| 2744 | |
| 2745 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2746 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 2747 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2748 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2749 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2750 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2751 | return 109; |
| 2752 | } |
| 2753 | |
| 2754 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2755 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2756 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2757 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2758 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2759 | return 110; |
| 2760 | } |
| 2761 | |
| 2762 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2763 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2764 | |
| 2765 | // Untagged integer 0 |
| 2766 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2767 | &nEpochDate3); |
| 2768 | // Untagged date string |
| 2769 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2770 | &StringDate2); |
| 2771 | |
| 2772 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2773 | &StringDays2); |
| 2774 | |
| 2775 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2776 | &nEpochDays2); |
| 2777 | |
| 2778 | QCBORDecode_ExitMap(&DC); |
| 2779 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 2780 | return 3001; |
| 2781 | } |
| 2782 | |
| 2783 | // The map of tagged items |
| 2784 | QCBORDecode_EnterMap(&DC, NULL); |
| 2785 | |
| 2786 | #ifndef QCBOR_DISABLE_TAGS |
| 2787 | int64_t nEpochDate2, |
| 2788 | nEpochDateFail, |
| 2789 | nEpochDate1400000000, nEpochDays1; |
| 2790 | UsefulBufC StringDays1; |
| 2791 | uint64_t uTag1, uTag2; |
| 2792 | |
| 2793 | // Tagged date string |
| 2794 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2795 | &StringDate1); |
| 2796 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2797 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2798 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2799 | 1, |
| 2800 | QCBOR_TAG_REQUIREMENT_TAG | |
| 2801 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2802 | &nEpochDate1400000000); |
| 2803 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2804 | |
| 2805 | // Get largest negative double precision epoch date allowed |
| 2806 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2807 | 5, |
| 2808 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 2809 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2810 | &nEpochDate2); |
| 2811 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2812 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2813 | return 102; |
| 2814 | } |
| 2815 | if(uError == QCBOR_SUCCESS) { |
| 2816 | if(nEpochDate2 != -9223372036854773760LL) { |
| 2817 | return 101; |
| 2818 | } |
| 2819 | } |
| 2820 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2821 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2822 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 2823 | "z", |
| 2824 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 2825 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2826 | &nEpochDate6); |
| 2827 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2828 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2829 | |
| 2830 | // Get largest double precision epoch date allowed |
| 2831 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2832 | &nEpochDate2); |
| 2833 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2834 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2835 | return 112; |
| 2836 | } |
| 2837 | if(uError == QCBOR_SUCCESS) { |
| 2838 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 2839 | return 111; |
| 2840 | } |
| 2841 | } |
| 2842 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2843 | /* The days format is much simpler than the date format |
| 2844 | * because it can't be a floating point value. The test |
| 2845 | * of the spiffy decode functions sufficiently covers |
| 2846 | * the test of the non-spiffy decode days date decoding. |
| 2847 | * There is no full fan out of the error conditions |
| 2848 | * and decode options as that is implemented by code |
| 2849 | * that is tested well by the date testing above. |
| 2850 | */ |
| 2851 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 2852 | &StringDays1); |
| 2853 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2854 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 2855 | &nEpochDays1); |
| 2856 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2857 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2858 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 2859 | return 3001; |
| 2860 | } |
| 2861 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2862 | // Too-negative float, -9.2233720368547748E+18 |
| 2863 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2864 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2865 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 2866 | return 1111; |
| 2867 | } |
| 2868 | |
| 2869 | // Too-large integer |
| 2870 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2871 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2872 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 2873 | return 1; |
| 2874 | } |
| 2875 | |
| 2876 | // Half-precision minus infinity |
| 2877 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2878 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2879 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
| 2880 | return 2; |
| 2881 | } |
| 2882 | |
| 2883 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2884 | // Bad content for epoch date |
| 2885 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2886 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2887 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 2888 | return 3; |
| 2889 | } |
| 2890 | |
| 2891 | // Bad content for string date |
| 2892 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 2893 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2894 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 2895 | return 4; |
| 2896 | } |
| 2897 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2898 | QCBORDecode_ExitArray(&DC); |
| 2899 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2900 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2901 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2902 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2903 | #else /* QCBOR_DISABLE_TAGS */ |
| 2904 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2905 | &StringDate1); |
| 2906 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2907 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 2908 | return 4; |
| 2909 | } |
| 2910 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2911 | |
| 2912 | |
| 2913 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2914 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2915 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2916 | return 200; |
| 2917 | } |
| 2918 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2919 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2920 | return 201; |
| 2921 | } |
| 2922 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2923 | if(nEpochDays1 != -10676) { |
| 2924 | return 205; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2925 | } |
| 2926 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2927 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2928 | return 207; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2931 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2932 | return 204; |
| 2933 | } |
| 2934 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2935 | if(nEpochDate6 != -1000) { |
| 2936 | return 203; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2939 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2940 | return 205; |
| 2941 | } |
| 2942 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2943 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2944 | |
| 2945 | if(nEpochDate3 != 0) { |
| 2946 | return 202; |
| 2947 | } |
| 2948 | |
| 2949 | if(nEpochDays2 != 3994) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2950 | return 206; |
| 2951 | } |
| 2952 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2953 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 2954 | return 206; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2958 | return 208; |
| 2959 | } |
| 2960 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2961 | return 0; |
| 2962 | } |
| 2963 | |
| 2964 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2965 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2966 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2967 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2968 | 0x81, // Array of one |
| 2969 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 2970 | 0x82, // Array of two that is the faction 1/3 |
| 2971 | 0x01, |
| 2972 | 0x03, |
| 2973 | |
| 2974 | /* |
| 2975 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 2976 | */ |
| 2977 | 0xd8, 0xe1, |
| 2978 | 0xd8, 0xe2, |
| 2979 | 0xd8, 0xe3, |
| 2980 | 0xd8, 0xe4, |
| 2981 | 0xd8, 0xe5, |
| 2982 | 0x80, |
| 2983 | |
| 2984 | /* tag 10489608748473423768( |
| 2985 | 2442302356( |
| 2986 | 21590( |
| 2987 | 240( |
| 2988 | [])))) |
| 2989 | */ |
| 2990 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2991 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 2992 | 0xd9, 0x54, 0x56, |
| 2993 | 0xd8, 0xf0, |
| 2994 | 0x80, |
| 2995 | |
| 2996 | /* tag 21590( |
| 2997 | 10489608748473423768( |
| 2998 | 2442302357( |
| 2999 | 65534( |
| 3000 | [])))) |
| 3001 | */ |
| 3002 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 3003 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 3004 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 3005 | 0xd9, 0xff, 0xfe, |
| 3006 | 0x80, |
| 3007 | |
| 3008 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3009 | works in conjuntion with entries above. |
| 3010 | 269488144(269488145(269488146(269488147([])))) |
| 3011 | */ |
| 3012 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 3013 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 3014 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 3015 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 3016 | 0x80, |
| 3017 | |
| 3018 | /* An invalid decimal fraction with an additional tag */ |
| 3019 | 0xd9, 0xff, 0xfa, |
| 3020 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 3021 | 0x00, // the integer 0; should be a byte string |
| 3022 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3023 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3024 | /* |
| 3025 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3026 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3027 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3028 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3029 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3030 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3031 | /* |
| 3032 | DB 9192939495969798 # tag(10489608748473423768) |
| 3033 | D8 88 # tag(136) |
| 3034 | C6 # tag(6) |
| 3035 | C7 # tag(7) |
| 3036 | 80 # array(0) |
| 3037 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3038 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3039 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3040 | |
| 3041 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3042 | 55799(55799(55799({ |
| 3043 | 6(7(-23)): 5859837686836516696(7({ |
| 3044 | 7(-20): 11({ |
| 3045 | 17(-18): 17(17(17("Organization"))), |
| 3046 | 9(-17): 773("SSG"), |
| 3047 | -15: 16(17(6(7("Confusion")))), |
| 3048 | 17(-16): 17("San Diego"), |
| 3049 | 17(-14): 17("US") |
| 3050 | }), |
| 3051 | 23(-19): 19({ |
| 3052 | -11: 9({ |
| 3053 | -9: -7 |
| 3054 | }), |
| 3055 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 3056 | }) |
| 3057 | })), |
| 3058 | 16(-22): 23({ |
| 3059 | 11(8(7(-5))): 8(-3) |
| 3060 | }) |
| 3061 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3062 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3063 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3064 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 3065 | 0xc6, 0xc7, 0x36, |
| 3066 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 3067 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 3068 | 0xcb, 0xa5, |
| 3069 | 0xd1, 0x31, |
| 3070 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 3071 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 3072 | 0xc9, 0x30, |
| 3073 | 0xd9, 0x03, 0x05, 0x63, |
| 3074 | 0x53, 0x53, 0x47, |
| 3075 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3076 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 3077 | 0x69, |
| 3078 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3079 | 0xd1, 0x2f, |
| 3080 | 0xd1, 0x69, |
| 3081 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 3082 | 0xd1, 0x2d, |
| 3083 | 0xd1, 0x62, |
| 3084 | 0x55, 0x53, |
| 3085 | 0xd7, 0x32, |
| 3086 | 0xd3, 0xa2, |
| 3087 | 0x2a, |
| 3088 | 0xc9, 0xa1, |
| 3089 | 0x28, |
| 3090 | 0x26, |
| 3091 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 3092 | 0xcc, 0x4a, |
| 3093 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 3094 | 0xd0, 0x35, |
| 3095 | 0xd7, 0xa1, |
| 3096 | 0xcb, 0xc8, 0xc7, 0x24, |
| 3097 | 0xc8, 0x22}; |
| 3098 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3099 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3100 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3101 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3102 | |
| 3103 | 0xc0, // tag for string date |
| 3104 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3105 | |
| 3106 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3107 | |
| 3108 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 3109 | |
| 3110 | 0xd8, 0x23, // tag for regex |
| 3111 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3112 | |
| 3113 | 0xc0, // tag for string date |
| 3114 | 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] | 3115 | |
| 3116 | // This last case makes the array untraversable because it is |
| 3117 | // an uncrecoverable error. Make sure it stays last and is the only |
| 3118 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3119 | }; |
| 3120 | |
| 3121 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3122 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3123 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3124 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3125 | int32_t OptTagParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3126 | { |
| 3127 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3128 | QCBORItem Item; |
| 3129 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3130 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3131 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3132 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3133 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3134 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3135 | /* |
| 3136 | This test matches the magic number tag and the fraction tag |
| 3137 | 55799([...]) |
| 3138 | */ |
| 3139 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3140 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3141 | return -2; |
| 3142 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3143 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3144 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 3145 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3146 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3147 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3148 | /* |
| 3149 | 4([1,3]) |
| 3150 | */ |
| 3151 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3152 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3153 | if(uError != QCBOR_SUCCESS || |
| 3154 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3155 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 3156 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 3157 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3158 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3159 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3160 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 3161 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3162 | return -4; |
| 3163 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3164 | // consume the items in the array |
| 3165 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3166 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3167 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3168 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3169 | if(uError != QCBOR_SUCCESS || |
| 3170 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3171 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 3172 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3173 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3174 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3175 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 3176 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3177 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3178 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3179 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3180 | /* |
| 3181 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3182 | */ |
| 3183 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3184 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3185 | return -6; |
| 3186 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3187 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 3188 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 3189 | return -106; |
| 3190 | } |
| 3191 | |
| 3192 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3193 | /* tag 10489608748473423768( |
| 3194 | 2442302356( |
| 3195 | 21590( |
| 3196 | 240( |
| 3197 | [])))) |
| 3198 | */ |
| 3199 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3200 | if(uError != QCBOR_SUCCESS || |
| 3201 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3202 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 3203 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 3204 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 3205 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3206 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | /* tag 21590( |
| 3210 | 10489608748473423768( |
| 3211 | 2442302357( |
| 3212 | 21591( |
| 3213 | [])))) |
| 3214 | */ |
| 3215 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3216 | if(uError != QCBOR_SUCCESS || |
| 3217 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3218 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 3219 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 3220 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 3221 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 3222 | return -8; |
| 3223 | } |
| 3224 | |
| 3225 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3226 | works in conjuntion with entries above. |
| 3227 | 269488144(269488145(269488146(269488147([])))) |
| 3228 | */ |
| 3229 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3230 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3231 | return -9; |
| 3232 | } |
| 3233 | |
| 3234 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3235 | if(uError == QCBOR_SUCCESS) { |
| 3236 | return -10; |
| 3237 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3238 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3239 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3240 | // 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] | 3241 | // tage and then matches it. Caller-config lists are no longer |
| 3242 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3243 | QCBORDecode_Init(&DCtx, |
| 3244 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3245 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3246 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 3247 | const QCBORTagListIn TL = {2, puList}; |
| 3248 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3249 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3250 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3251 | return -8; |
| 3252 | } |
| 3253 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3254 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 3255 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 3256 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 3257 | Item.val.uCount != 0) { |
| 3258 | return -9; |
| 3259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3260 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3261 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3262 | // 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] | 3263 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3264 | const uint64_t puLongList[17] = {1,2,1}; |
| 3265 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3266 | QCBORDecode_Init(&DCtx, |
| 3267 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3268 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3269 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 3270 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3271 | return -11; |
| 3272 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3273 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3274 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3275 | QCBORTagListOut Out = {0, 4, puTags}; |
| 3276 | |
| 3277 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3278 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3279 | QCBORDecode_Init(&DCtx, |
| 3280 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3281 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3282 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3283 | return -12; |
| 3284 | } |
| 3285 | if(puTags[0] != 0x9192939495969798 || |
| 3286 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3287 | puTags[2] != 0x06 || |
| 3288 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3289 | return -13; |
| 3290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3291 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3292 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3293 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3294 | QCBORDecode_Init(&DCtx, |
| 3295 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3296 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3297 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 3298 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3299 | return -14; |
| 3300 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3301 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3302 | |
| 3303 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3304 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3305 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 3306 | // It is a bit of a messy test and maybe could be improved, but |
| 3307 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3308 | QCBORDecode_Init(&DCtx, |
| 3309 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3310 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3311 | int n = CheckCSRMaps(&DCtx); |
| 3312 | if(n) { |
| 3313 | return n-2000; |
| 3314 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3315 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3316 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3317 | QCBORDecode_Init(&DCtx, |
| 3318 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3319 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3320 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3321 | /* With the spiffy decode revision, this tag list is not used. |
| 3322 | It doesn't matter if a tag is in this list or not so some |
| 3323 | tests that couldn't process a tag because it isn't in this list |
| 3324 | now can process these unlisted tags. The tests have been |
| 3325 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3326 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 3327 | const QCBORTagListIn TagList = {3, puTagList}; |
| 3328 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3329 | |
| 3330 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3331 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3332 | return -100; |
| 3333 | } |
| 3334 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3335 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3336 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 3337 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 3338 | Item.val.uCount != 2 || |
| 3339 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 3340 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 3341 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 3342 | Out.uNumUsed != 3) { |
| 3343 | return -101; |
| 3344 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3345 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3346 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3347 | return -102; |
| 3348 | } |
| 3349 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3350 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3351 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3352 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3353 | Item.val.uCount != 2 || |
| 3354 | puTags[0] != 5859837686836516696 || |
| 3355 | puTags[1] != 7 || |
| 3356 | Out.uNumUsed != 2) { |
| 3357 | return -103; |
| 3358 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3359 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3360 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3361 | return -104; |
| 3362 | } |
| 3363 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3364 | Item.val.uCount != 5 || |
| 3365 | puTags[0] != 0x0b || |
| 3366 | Out.uNumUsed != 1) { |
| 3367 | return -105; |
| 3368 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3369 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3370 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3371 | return -106; |
| 3372 | } |
| 3373 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3374 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 3375 | Item.val.string.len != 12 || |
| 3376 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 3377 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 3378 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 3379 | Out.uNumUsed != 3) { |
| 3380 | return -105; |
| 3381 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3382 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3383 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3384 | return -107; |
| 3385 | } |
| 3386 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3387 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 3388 | Item.val.string.len != 3 || |
| 3389 | puTags[0] != 773 || |
| 3390 | Out.uNumUsed != 1) { |
| 3391 | return -108; |
| 3392 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3393 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3394 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3395 | return -109; |
| 3396 | } |
| 3397 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3398 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3399 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3400 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3401 | puTags[3] != 7 || |
| 3402 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3403 | return -110; |
| 3404 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3405 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3406 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3407 | return -111; |
| 3408 | } |
| 3409 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3410 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3411 | Item.val.string.len != 9 || |
| 3412 | puTags[0] != 17 || |
| 3413 | Out.uNumUsed != 1) { |
| 3414 | return -112; |
| 3415 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3416 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3417 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3418 | return -111; |
| 3419 | } |
| 3420 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3421 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3422 | Item.val.string.len != 2 || |
| 3423 | puTags[0] != 17 || |
| 3424 | Out.uNumUsed != 1) { |
| 3425 | return -112; |
| 3426 | } |
| 3427 | |
| 3428 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3429 | return -113; |
| 3430 | } |
| 3431 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3432 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3433 | Item.val.uCount != 2 || |
| 3434 | puTags[0] != 19 || |
| 3435 | Out.uNumUsed != 1) { |
| 3436 | return -114; |
| 3437 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3438 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3439 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3440 | return -115; |
| 3441 | } |
| 3442 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3443 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3444 | Item.val.uCount != 1 || |
| 3445 | puTags[0] != 9 || |
| 3446 | Out.uNumUsed != 1) { |
| 3447 | return -116; |
| 3448 | } |
| 3449 | |
| 3450 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3451 | return -116; |
| 3452 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3453 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3454 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3455 | Out.uNumUsed != 0) { |
| 3456 | return -117; |
| 3457 | } |
| 3458 | |
| 3459 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3460 | return -118; |
| 3461 | } |
| 3462 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 3463 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3464 | puTags[0] != 12 || |
| 3465 | Out.uNumUsed != 1) { |
| 3466 | return -119; |
| 3467 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3468 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3469 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3470 | return -120; |
| 3471 | } |
| 3472 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3473 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 3474 | Item.val.uCount != 1 || |
| 3475 | puTags[0] != 0x17 || |
| 3476 | Out.uNumUsed != 1) { |
| 3477 | return -121; |
| 3478 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3479 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3480 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3481 | return -122; |
| 3482 | } |
| 3483 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3484 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3485 | Item.val.int64 != -3 || |
| 3486 | puTags[0] != 8 || |
| 3487 | Out.uNumUsed != 1) { |
| 3488 | return -123; |
| 3489 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3490 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3491 | if(QCBORDecode_Finish(&DCtx)) { |
| 3492 | return -124; |
| 3493 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3494 | |
| 3495 | UsefulBufC DateString; |
| 3496 | QCBORDecode_Init(&DCtx, |
| 3497 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3498 | QCBOR_DECODE_MODE_NORMAL); |
| 3499 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3500 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3501 | // tagged date string |
| 3502 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3503 | // untagged date string |
| 3504 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3505 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3506 | return 100; |
| 3507 | } |
| 3508 | // untagged byte string |
| 3509 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3510 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3511 | return 101; |
| 3512 | } |
| 3513 | // tagged regex |
| 3514 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3515 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3516 | return 102; |
| 3517 | } |
| 3518 | // tagged date string with a byte string |
| 3519 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3520 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3521 | return 103; |
| 3522 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3523 | // The exit errors out because the last item, the date string with |
| 3524 | // bad content makes the array untraversable (the bad date string |
| 3525 | // could have tag content of an array or such that is not consumed |
| 3526 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3527 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3528 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3529 | return 104; |
| 3530 | } |
| 3531 | |
| 3532 | |
| 3533 | QCBORDecode_Init(&DCtx, |
| 3534 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3535 | QCBOR_DECODE_MODE_NORMAL); |
| 3536 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3537 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3538 | // tagged date string |
| 3539 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3540 | // untagged date string |
| 3541 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3542 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3543 | return 200; |
| 3544 | } |
| 3545 | // untagged byte string |
| 3546 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3547 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3548 | return 201; |
| 3549 | } |
| 3550 | // tagged regex |
| 3551 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3552 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3553 | return 202; |
| 3554 | } |
| 3555 | // tagged date string with a byte string |
| 3556 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3557 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3558 | return 203; |
| 3559 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3560 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3561 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3562 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3563 | return 204; |
| 3564 | } |
| 3565 | |
| 3566 | QCBORDecode_Init(&DCtx, |
| 3567 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3568 | QCBOR_DECODE_MODE_NORMAL); |
| 3569 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3570 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3571 | // tagged date string |
| 3572 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3573 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3574 | return 300; |
| 3575 | } |
| 3576 | // untagged date string |
| 3577 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3578 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3579 | return 301; |
| 3580 | } |
| 3581 | // untagged byte string |
| 3582 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3583 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3584 | return 302; |
| 3585 | } |
| 3586 | // tagged regex |
| 3587 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3588 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3589 | return 303; |
| 3590 | } |
| 3591 | // tagged date string with a byte string |
| 3592 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3593 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3594 | return 304; |
| 3595 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3596 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3597 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3598 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3599 | return 305; |
| 3600 | } |
| 3601 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3602 | return 0; |
| 3603 | } |
| 3604 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3605 | /* |
| 3606 | * These are showing the big numbers converted to integers. |
| 3607 | * The tag numbers are not shown. |
| 3608 | * |
| 3609 | * [ 18446744073709551616, |
| 3610 | * -18446744073709551617, |
| 3611 | * {"BN+": 18446744073709551616, |
| 3612 | * 64: 18446744073709551616, |
| 3613 | * "BN-": -18446744073709551617, |
| 3614 | * -64: -18446744073709551617 |
| 3615 | * } |
| 3616 | * ] |
| 3617 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3618 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3619 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3620 | 0x83, |
| 3621 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3622 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3623 | 0xA4, |
| 3624 | 0x63, 0x42, 0x4E, 0x2B, |
| 3625 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3626 | 0x18, 0x40, |
| 3627 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3628 | 0x63, 0x42, 0x4E, 0x2D, |
| 3629 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3630 | 0x38, 0x3F, |
| 3631 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 3632 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3633 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3634 | /* The expected big num */ |
| 3635 | static const uint8_t spBigNum[] = { |
| 3636 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3637 | 0x00}; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3638 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3639 | |
| 3640 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3641 | int32_t BignumParseTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3642 | { |
| 3643 | QCBORDecodeContext DCtx; |
| 3644 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3645 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3646 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3647 | QCBORDecode_Init(&DCtx, |
| 3648 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 3649 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3650 | |
| 3651 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3652 | // |
| 3653 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 3654 | return -1; |
| 3655 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3656 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3657 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3658 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3659 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3660 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3661 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3662 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3663 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3664 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3665 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | // |
| 3669 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3670 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3671 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3672 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3673 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3674 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3675 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3676 | // |
| 3677 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3678 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3679 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3680 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3681 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3682 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3683 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3684 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3685 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3686 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3687 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3688 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3692 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3693 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3694 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3695 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3696 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3697 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3698 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3699 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3700 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3701 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3702 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3703 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3704 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3705 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3706 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3707 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3708 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3709 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3710 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3711 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3712 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3713 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3714 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3715 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3716 | #else |
| 3717 | |
| 3718 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) { |
| 3719 | return -100; |
| 3720 | } |
| 3721 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3722 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3723 | return 0; |
| 3724 | } |
| 3725 | |
| 3726 | |
| 3727 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3728 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3729 | uint8_t uDataType, |
| 3730 | uint8_t uNestingLevel, |
| 3731 | uint8_t uNextNest, |
| 3732 | int64_t nLabel, |
| 3733 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3734 | { |
| 3735 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3736 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3737 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3738 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 3739 | if(Item.uDataType != uDataType) return -1; |
| 3740 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3741 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 3742 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 3743 | return -1; |
| 3744 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3745 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3746 | if(Item.label.int64 != nLabel) return -1; |
| 3747 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3748 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3749 | } |
| 3750 | } |
| 3751 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3752 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3753 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3754 | if(pItem) { |
| 3755 | *pItem = Item; |
| 3756 | } |
| 3757 | return 0; |
| 3758 | } |
| 3759 | |
| 3760 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3761 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3762 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3763 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3764 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
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_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3767 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3768 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3769 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3770 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 3771 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 3772 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 3773 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 3774 | 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] | 3775 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3776 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 3777 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3778 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3779 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 3780 | 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] | 3781 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3782 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 3783 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3784 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3785 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3786 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3787 | return 0; |
| 3788 | } |
| 3789 | |
| 3790 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3791 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3792 | { |
| 3793 | -23: { |
| 3794 | -20: { |
| 3795 | -18: "Organization", |
| 3796 | -17: "SSG", |
| 3797 | -15: "Confusion", |
| 3798 | -16: "San Diego", |
| 3799 | -14: "US" |
| 3800 | }, |
| 3801 | -19: { |
| 3802 | -11: { |
| 3803 | -9: -7 |
| 3804 | }, |
| 3805 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 3806 | } |
| 3807 | }, |
| 3808 | -22: { |
| 3809 | -5: -3 |
| 3810 | } |
| 3811 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3812 | */ |
| 3813 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3814 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 3815 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3816 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3817 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3818 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3819 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3820 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 3821 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 3822 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 3823 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3824 | // Same map as above, but using indefinite lengths |
| 3825 | static const uint8_t spCSRInputIndefLen[] = { |
| 3826 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 3827 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3828 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3829 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3830 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3831 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3832 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 3833 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 3834 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 3835 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 3836 | |
| 3837 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3838 | int32_t NestedMapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3839 | { |
| 3840 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3841 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3842 | QCBORDecode_Init(&DCtx, |
| 3843 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3844 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3845 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3846 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3847 | } |
| 3848 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3849 | |
| 3850 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3851 | int32_t StringDecoderModeFailTest(void) |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3852 | { |
| 3853 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3854 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3855 | QCBORDecode_Init(&DCtx, |
| 3856 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3857 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3858 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3859 | QCBORItem Item; |
| 3860 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3861 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3862 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3863 | return -1; |
| 3864 | } |
| 3865 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3866 | return -2; |
| 3867 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3868 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3869 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3870 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 3871 | return -3; |
| 3872 | } |
| 3873 | |
| 3874 | return 0; |
| 3875 | } |
| 3876 | |
| 3877 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3878 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3879 | int32_t NestedMapTestIndefLen(void) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3880 | { |
| 3881 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3882 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3883 | QCBORDecode_Init(&DCtx, |
| 3884 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 3885 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3886 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3887 | return CheckCSRMaps(&DCtx); |
| 3888 | } |
| 3889 | |
| 3890 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3891 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3892 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 3893 | { |
| 3894 | UsefulOutBuf UOB; |
| 3895 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3896 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3897 | int i; |
| 3898 | for(i = 0; i < n; i++) { |
| 3899 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 3900 | } |
| 3901 | |
| 3902 | for(i = 0; i < n; i++) { |
| 3903 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3904 | } |
| 3905 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3906 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3907 | |
| 3908 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3909 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3910 | { |
| 3911 | QCBORDecodeContext DC; |
| 3912 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3913 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3914 | int j; |
| 3915 | for(j = 0; j < nNestLevel; j++) { |
| 3916 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3917 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3918 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 3919 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3920 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3921 | return -4; |
| 3922 | } else { |
| 3923 | return 0; // Decoding doesn't recover after an error |
| 3924 | } |
| 3925 | } else { |
| 3926 | // Should be no error |
| 3927 | if(nReturn) { |
| 3928 | return -9; // Should not have got an error |
| 3929 | } |
| 3930 | } |
| 3931 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3932 | return -7; |
| 3933 | } |
| 3934 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3935 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3936 | if(nReturn) { |
| 3937 | return -3; |
| 3938 | } |
| 3939 | return 0; |
| 3940 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3941 | |
| 3942 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3943 | int32_t IndefiniteLengthNestTest(void) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3944 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3945 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3946 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3947 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3948 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3949 | int nReturn = parse_indeflen_nested(Nested, i); |
| 3950 | if(nReturn) { |
| 3951 | return nReturn; |
| 3952 | } |
| 3953 | } |
| 3954 | return 0; |
| 3955 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3956 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3957 | // [1, [2, 3]] |
| 3958 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 3959 | // No closing break |
| 3960 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 3961 | // Not enough closing breaks |
| 3962 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 3963 | // Too many closing breaks |
| 3964 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 3965 | // Unclosed indeflen inside def len |
| 3966 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 3967 | // confused tag |
| 3968 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3969 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 3970 | int32_t IndefiniteLengthArrayMapTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3971 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3972 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3973 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3974 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3975 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3976 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3977 | QCBORDecodeContext DC; |
| 3978 | QCBORItem Item; |
| 3979 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3980 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3981 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3982 | |
| 3983 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3984 | Item.uNestingLevel != 0 || |
| 3985 | Item.uNextNestLevel != 1) { |
| 3986 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3987 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3988 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3989 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3990 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 3991 | Item.uNestingLevel != 1 || |
| 3992 | Item.uNextNestLevel != 1) { |
| 3993 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3994 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3995 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3996 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3997 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3998 | Item.uNestingLevel != 1 || |
| 3999 | Item.uNextNestLevel != 2) { |
| 4000 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4001 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4002 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4003 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4004 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4005 | Item.uNestingLevel != 2 || |
| 4006 | Item.uNextNestLevel != 2) { |
| 4007 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4008 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4009 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4010 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 4011 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4012 | Item.uNestingLevel != 2 || |
| 4013 | Item.uNextNestLevel != 0) { |
| 4014 | return -5; |
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 | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4017 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4018 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4019 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4020 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4021 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4022 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4023 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4024 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4025 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4026 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4027 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4028 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4029 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4030 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4031 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4032 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4033 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4034 | } |
| 4035 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4036 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4037 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4038 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4039 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4040 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4041 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4042 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4043 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4044 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4048 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4049 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4050 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4051 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4052 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4053 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4054 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4055 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4056 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4057 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4058 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4059 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4060 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4061 | |
| 4062 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4063 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4064 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4065 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4066 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4067 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4068 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4069 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4070 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4071 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4072 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4073 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4074 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4075 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 4076 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4077 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 4078 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4079 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4080 | return -140; |
| 4081 | } |
| 4082 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4083 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4084 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4085 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4086 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4087 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4088 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4089 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4090 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4091 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4092 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4093 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4094 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4095 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4096 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4097 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4098 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4099 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4100 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4101 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4102 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4103 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4104 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4105 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4106 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4107 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4108 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4109 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4110 | |
| 4111 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4112 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4113 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4114 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4115 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4116 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4117 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4118 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4119 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4120 | #else /* QCBOR_DISABLE_TAGS */ |
| 4121 | if(nResult != QCBOR_ERR_TAGS_DISABLED) { |
| 4122 | return -20; |
| 4123 | } |
| 4124 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4125 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4126 | return 0; |
| 4127 | } |
| 4128 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4129 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4130 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 4131 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4132 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4133 | 0x81, // Array of length one |
| 4134 | 0x7f, // text string marked with indefinite length |
| 4135 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4136 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4137 | 0xff // ending break |
| 4138 | }; |
| 4139 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4140 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4141 | 0x81, // Array of length one |
| 4142 | 0x7f, // text string marked with indefinite length |
| 4143 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4144 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 4145 | 0xff // ending break |
| 4146 | }; |
| 4147 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4148 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4149 | 0x81, // Array of length one |
| 4150 | 0x7f, // text string marked with indefinite length |
| 4151 | 0x01, 0x02, // Not a string |
| 4152 | 0xff // ending break |
| 4153 | }; |
| 4154 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4155 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4156 | 0x81, // Array of length one |
| 4157 | 0x7f, // text string marked with indefinite length |
| 4158 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4159 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4160 | // missing end of string |
| 4161 | }; |
| 4162 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4163 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4164 | 0xa1, // Array of length one |
| 4165 | 0x7f, // text string marked with indefinite length |
| 4166 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 4167 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4168 | 0xff, // ending break |
| 4169 | 0x01 // integer being labeled. |
| 4170 | }; |
| 4171 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4172 | /** |
| 4173 | Make an indefinite length string |
| 4174 | |
| 4175 | @param Storage Storage for string, must be 144 bytes in size |
| 4176 | @return The indefinite length string |
| 4177 | |
| 4178 | This makes an array with one indefinite length string that has 7 chunks |
| 4179 | from size of 1 byte up to 64 bytes. |
| 4180 | */ |
| 4181 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4182 | { |
| 4183 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4184 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4185 | UsefulOutBuf_Init(&UOB, Storage); |
| 4186 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 4187 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4188 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4189 | uint8_t uStringByte = 0; |
| 4190 | // Use of type int is intentional |
| 4191 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 4192 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4193 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4194 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 4195 | for(int j = 0; j < uChunkSize; j++) { |
| 4196 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 4197 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4198 | } |
| 4199 | } |
| 4200 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4201 | |
| 4202 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4203 | } |
| 4204 | |
| 4205 | static int CheckBigString(UsefulBufC BigString) |
| 4206 | { |
| 4207 | if(BigString.len != 255) { |
| 4208 | return 1; |
| 4209 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4210 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4211 | for(uint8_t i = 0; i < 255; i++){ |
| 4212 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 4213 | return 1; |
| 4214 | } |
| 4215 | } |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4219 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4220 | int32_t IndefiniteLengthStringTest(void) |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4221 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4222 | QCBORDecodeContext DC; |
| 4223 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4224 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4225 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4226 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4227 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4228 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4229 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4230 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4231 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4232 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4233 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4234 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4235 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4236 | return -2; |
| 4237 | } |
| 4238 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 4239 | return -3; |
| 4240 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4241 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4242 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4243 | return -4; |
| 4244 | } |
| 4245 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 4246 | return -5; |
| 4247 | } |
| 4248 | if(QCBORDecode_Finish(&DC)) { |
| 4249 | return -6; |
| 4250 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4251 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4252 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4253 | QCBORDecode_Init(&DC, |
| 4254 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 4255 | QCBOR_DECODE_MODE_NORMAL); |
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_SetMemPool(&DC, MemPool, false)) { |
| 4258 | return -7; |
| 4259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4260 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4261 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4262 | return -8; |
| 4263 | } |
| 4264 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4265 | return -9; |
| 4266 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4267 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4268 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4269 | return -10; |
| 4270 | } |
| 4271 | |
| 4272 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4273 | QCBORDecode_Init(&DC, |
| 4274 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 4275 | QCBOR_DECODE_MODE_NORMAL); |
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_SetMemPool(&DC, MemPool, false)) { |
| 4278 | return -11; |
| 4279 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4280 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4281 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4282 | return -12; |
| 4283 | } |
| 4284 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4285 | return -13; |
| 4286 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4287 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4288 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4289 | return -14; |
| 4290 | } |
| 4291 | |
| 4292 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4293 | QCBORDecode_Init(&DC, |
| 4294 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 4295 | QCBOR_DECODE_MODE_NORMAL); |
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_SetMemPool(&DC, MemPool, false)) { |
| 4298 | return -15; |
| 4299 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4300 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4301 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4302 | return -16; |
| 4303 | } |
| 4304 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4305 | return -17; |
| 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 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 4309 | return -18; |
| 4310 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4311 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4312 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4313 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4314 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4315 | QCBORDecode_GetNext(&DC, &Item); |
| 4316 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4317 | return -19; |
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 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4321 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4322 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4323 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4324 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4325 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4326 | |
| 4327 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 4328 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4329 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4330 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4331 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4332 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4333 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4334 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4335 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4336 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 4337 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4338 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4339 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4340 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4341 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4342 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4343 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4344 | QCBORDecode_GetNext(&DC, &Item); |
| 4345 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4346 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4347 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4348 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4349 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4350 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4351 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4352 | // ---- big bstr ----- |
| 4353 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4354 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4355 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4356 | return -25; |
| 4357 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4358 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4359 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4360 | return -26; |
| 4361 | } |
| 4362 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4363 | return -26; |
| 4364 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4365 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4366 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4367 | return -27; |
| 4368 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4369 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4370 | return -28; |
| 4371 | } |
| 4372 | if(CheckBigString(Item.val.string)) { |
| 4373 | return -3; |
| 4374 | } |
| 4375 | if(QCBORDecode_Finish(&DC)) { |
| 4376 | return -29; |
| 4377 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4378 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4379 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4380 | 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] | 4381 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4382 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4383 | return -30; |
| 4384 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4385 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4386 | QCBORDecode_GetNext(&DC, &Item); |
| 4387 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4388 | return -31; |
| 4389 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4390 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4391 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 4392 | return -32; |
| 4393 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4394 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4395 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4396 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 4397 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 4398 | return -33; |
| 4399 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4400 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4401 | if(QCBORDecode_Finish(&DC)) { |
| 4402 | return -34; |
| 4403 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4404 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4405 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4406 | } |
| 4407 | |
| 4408 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 4409 | int32_t AllocAllStringsTest(void) |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4410 | { |
| 4411 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4412 | QCBORError nCBORError; |
| 4413 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4414 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4415 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4416 | QCBORDecode_Init(&DC, |
| 4417 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4418 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4419 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4420 | 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] | 4421 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4422 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 4423 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4424 | return -1; |
| 4425 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4426 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4427 | if(CheckCSRMaps(&DC)) { |
| 4428 | return -2; |
| 4429 | } |
| 4430 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 4431 | // Next parse, save pointers to a few strings, destroy original and |
| 4432 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4433 | 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] | 4434 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4435 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4436 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 4437 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4438 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4439 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4440 | QCBORItem Item1, Item2, Item3, Item4; |
| 4441 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4442 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4443 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 4444 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4445 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4446 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
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, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4449 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4450 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4451 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4452 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4453 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4454 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 4455 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4456 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4457 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4458 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 4459 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4460 | Item1.uDataAlloc != 0 || |
| 4461 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4462 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4463 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4464 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4465 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4466 | |
| 4467 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4468 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4469 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4470 | Item2.uDataAlloc != 0 || |
| 4471 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4472 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4473 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4474 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4475 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4476 | Item3.uDataAlloc == 0 || |
| 4477 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4478 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4479 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4480 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4481 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4482 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4483 | Item4.uDataAlloc == 0 || |
| 4484 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4485 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4486 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4487 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4488 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4489 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4490 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4491 | QCBORDecode_Init(&DC, |
| 4492 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 4493 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4494 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 4495 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4496 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4497 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4498 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4499 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4500 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4501 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 4502 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 4503 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 4504 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 4505 | } |
| 4506 | } |
| 4507 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4508 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4509 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | return 0; |
| 4513 | } |
| 4514 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4515 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4516 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 4517 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4518 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4519 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4520 | QCBORDecodeContext DC; |
| 4521 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 4522 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4523 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4524 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4525 | // Then fish into the internals of the decode context |
| 4526 | // to get the allocator function so it can be called directly. |
| 4527 | // Also figure out how much pool is available for use |
| 4528 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4529 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4530 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 4531 | if(nError) { |
| 4532 | return -9; |
| 4533 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4534 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 4535 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4536 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4537 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4538 | // First test -- ask for one more byte than available and see failure |
| 4539 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4540 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 4541 | return -1; |
| 4542 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4543 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4544 | // Re do the set up for the next test that will do a successful alloc, |
| 4545 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4546 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4547 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4548 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4549 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4550 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4551 | // Allocate one byte less than available and see success |
| 4552 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4553 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4554 | return -2; |
| 4555 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4556 | // Ask for some more and see failure |
| 4557 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4558 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 4559 | return -3; |
| 4560 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4561 | // Free the first allocate, retry the second and see success |
| 4562 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 4563 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4564 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 4565 | return -4; |
| 4566 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4567 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4568 | // Re do set up for next test that involves a successful alloc, |
| 4569 | // and a successful realloc and a failed realloc |
| 4570 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4571 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4572 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4573 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4574 | // Allocate half the pool and see success |
| 4575 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4576 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4577 | return -5; |
| 4578 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4579 | // Reallocate to take up the whole pool and see success |
| 4580 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4581 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 4582 | return -6; |
| 4583 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4584 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4585 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 4586 | return -7; |
| 4587 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4588 | // Try to allocate more to be sure there is failure after a realloc |
| 4589 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 4590 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4591 | return -8; |
| 4592 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4593 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4594 | return 0; |
| 4595 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4596 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4597 | |
| 4598 | /* Just enough of an allocator to test configuration of one */ |
| 4599 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 4600 | { |
| 4601 | (void)pOldMem; // unused variable |
| 4602 | |
| 4603 | if(uNewSize) { |
| 4604 | // Assumes the context pointer is the buffer and |
| 4605 | // nothing too big will ever be asked for. |
| 4606 | // This is only good for this basic test! |
| 4607 | return (UsefulBuf) {pCtx, uNewSize}; |
| 4608 | } else { |
| 4609 | return NULLUsefulBuf; |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4614 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4615 | { |
| 4616 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4617 | // nothing can be done with it unless that is set up. |
| 4618 | QCBORDecodeContext DC; |
| 4619 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 4620 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 4621 | |
| 4622 | uint8_t pAllocatorBuffer[50]; |
| 4623 | |
| 4624 | // This is really just to test that this call works. |
| 4625 | // The full functionality of string allocators is tested |
| 4626 | // elsewhere with the MemPool internal allocator. |
| 4627 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 4628 | |
| 4629 | QCBORItem Item; |
| 4630 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 4631 | return -1; |
| 4632 | } |
| 4633 | |
| 4634 | if(Item.uDataAlloc == 0 || |
| 4635 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4636 | Item.val.string.ptr != pAllocatorBuffer) { |
| 4637 | return -2; |
| 4638 | } |
| 4639 | |
| 4640 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 4641 | return -3; |
| 4642 | } |
| 4643 | |
| 4644 | return 0; |
| 4645 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4646 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4647 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4648 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4649 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4650 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4651 | struct EaMTest { |
| 4652 | const char *szName; |
| 4653 | UsefulBufC Input; |
| 4654 | uint8_t uTagRequirement; |
| 4655 | bool bHasTags; |
| 4656 | |
| 4657 | /* Expected values for GetNext */ |
| 4658 | QCBORError uExpectedErrorGN; |
| 4659 | uint8_t uQCBORTypeGN; |
| 4660 | int64_t nExponentGN; |
| 4661 | int64_t nMantissaGN; |
| 4662 | UsefulBufC MantissaGN; |
| 4663 | |
| 4664 | /* Expected values for GetDecimalFraction */ |
| 4665 | QCBORError uExpectedErrorGDF; |
| 4666 | int64_t nExponentGDF; |
| 4667 | int64_t nMantissaGDF; |
| 4668 | |
| 4669 | /* Expected values for GetDecimalFractionBig */ |
| 4670 | QCBORError uExpectedErrorGDFB; |
| 4671 | int64_t nExponentGDFB; |
| 4672 | UsefulBufC MantissaGDFB; |
| 4673 | bool IsNegativeGDFB; |
| 4674 | |
| 4675 | /* Expected values for GetBigFloat */ |
| 4676 | QCBORError uExpectedErrorGBF; |
| 4677 | int64_t nExponentGBF; |
| 4678 | int64_t nMantissaGBF; |
| 4679 | |
| 4680 | /* Expected values for GetBigFloatBig */ |
| 4681 | QCBORError uExpectedErrorGBFB; |
| 4682 | int64_t nExponentGBFB; |
| 4683 | UsefulBufC MantissaGBFB; |
| 4684 | bool IsNegativeGBFB; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4685 | }; |
| 4686 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4687 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4688 | |
| 4689 | static const struct EaMTest pEaMTests[] = { |
| 4690 | { |
| 4691 | "1. Untagged pair (big float or decimal fraction), no tag required", |
| 4692 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 4693 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 4694 | false, |
| 4695 | |
| 4696 | QCBOR_SUCCESS, /* for GetNext */ |
| 4697 | QCBOR_TYPE_ARRAY, |
| 4698 | 0, |
| 4699 | 0, |
| 4700 | {(const uint8_t []){0x00}, 1}, |
| 4701 | |
| 4702 | QCBOR_SUCCESS, /* GetDecimalFraction */ |
| 4703 | -1, |
| 4704 | 3, |
| 4705 | |
| 4706 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4707 | -1, |
| 4708 | {(const uint8_t []){0x03}, 1}, |
| 4709 | false, |
| 4710 | |
| 4711 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4712 | -1, |
| 4713 | 3, |
| 4714 | |
| 4715 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4716 | -1, |
| 4717 | {(const uint8_t []){0x03}, 1}, |
| 4718 | false |
| 4719 | }, |
| 4720 | |
| 4721 | { |
| 4722 | "2. Untagged pair (big float or decimal fraction), tag required", |
| 4723 | {(const uint8_t []){0x82, 0x20, 0x03}, 3}, |
| 4724 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4725 | false, |
| 4726 | |
| 4727 | QCBOR_SUCCESS, /* for GetNext */ |
| 4728 | QCBOR_TYPE_ARRAY, |
| 4729 | 0, |
| 4730 | 0, |
| 4731 | {(const uint8_t []){0x00}, 1}, |
| 4732 | |
| 4733 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4734 | 0, |
| 4735 | 0, |
| 4736 | |
| 4737 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4738 | 0, |
| 4739 | {(const uint8_t []){0x00}, 1}, |
| 4740 | false, |
| 4741 | |
| 4742 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4743 | 0, |
| 4744 | 0, |
| 4745 | |
| 4746 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4747 | 0, |
| 4748 | {(const uint8_t []){0x00}, 1}, |
| 4749 | false |
| 4750 | |
| 4751 | }, |
| 4752 | |
| 4753 | { |
| 4754 | "3. Tagged 1.5 decimal fraction, tag 4 optional", |
| 4755 | {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 4756 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4757 | true, |
| 4758 | |
| 4759 | QCBOR_SUCCESS, /* for GetNext */ |
| 4760 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 4761 | -1, |
| 4762 | 3, |
| 4763 | {(const uint8_t []){0x00}, 1}, |
| 4764 | |
| 4765 | |
| 4766 | QCBOR_SUCCESS, /* for GetDecimalFraction */ |
| 4767 | -1, |
| 4768 | 3, |
| 4769 | |
| 4770 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4771 | -1, |
| 4772 | {(const uint8_t []){0x03}, 1}, |
| 4773 | false, |
| 4774 | |
| 4775 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4776 | 0, |
| 4777 | 0, |
| 4778 | |
| 4779 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4780 | 0, |
| 4781 | {(const uint8_t []){0x00}, 1}, |
| 4782 | false |
| 4783 | }, |
| 4784 | { |
| 4785 | "4. Tagged 100 * 2^300 big float, tag 5 optional", |
| 4786 | {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7}, |
| 4787 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4788 | true, |
| 4789 | |
| 4790 | QCBOR_SUCCESS, /* for GetNext */ |
| 4791 | QCBOR_TYPE_BIGFLOAT, |
| 4792 | 300, |
| 4793 | 100, |
| 4794 | {(const uint8_t []){0x00}, 1}, |
| 4795 | |
| 4796 | |
| 4797 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4798 | 0, |
| 4799 | 0, |
| 4800 | |
| 4801 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4802 | 0, |
| 4803 | {(const uint8_t []){0x03}, 1}, |
| 4804 | false, |
| 4805 | |
| 4806 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4807 | 300, |
| 4808 | 100, |
| 4809 | |
| 4810 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4811 | 300, |
| 4812 | {(const uint8_t []){0x64}, 1}, |
| 4813 | false |
| 4814 | }, |
| 4815 | |
| 4816 | { |
| 4817 | "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required", |
| 4818 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 4819 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15}, |
| 4820 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4821 | true, |
| 4822 | |
| 4823 | QCBOR_SUCCESS, /* for GetNext */ |
| 4824 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 4825 | -20, |
| 4826 | 0, |
| 4827 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4828 | |
| 4829 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */ |
| 4830 | 0, |
| 4831 | 0, |
| 4832 | |
| 4833 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4834 | -20, |
| 4835 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4836 | false, |
| 4837 | |
| 4838 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4839 | 0, |
| 4840 | 0, |
| 4841 | |
| 4842 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4843 | 0, |
| 4844 | {(const uint8_t []){0x00}, 0}, |
| 4845 | false |
| 4846 | }, |
| 4847 | |
| 4848 | { |
| 4849 | "6. Error: Mantissa and exponent inside a Mantissa and exponent", |
| 4850 | {(const uint8_t []){0xC4, 0x82, 0x33, |
| 4851 | 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10}, |
| 4852 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4853 | true, |
| 4854 | |
| 4855 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */ |
| 4856 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 4857 | 0, |
| 4858 | 0, |
| 4859 | {(const uint8_t []){0x00}, 0}, |
| 4860 | |
| 4861 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ |
| 4862 | 0, |
| 4863 | 0, |
| 4864 | |
| 4865 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */ |
| 4866 | 0, |
| 4867 | {(const uint8_t []){0x00}, 0}, |
| 4868 | false, |
| 4869 | |
| 4870 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */ |
| 4871 | 0, |
| 4872 | 0, |
| 4873 | |
| 4874 | QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ |
| 4875 | 0, |
| 4876 | {(const uint8_t []){0x00}, 0}, |
| 4877 | false |
| 4878 | }, |
| 4879 | { |
| 4880 | "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required", |
| 4881 | {(const uint8_t []){0xC5, 0x82, 0x33, |
| 4882 | 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 4883 | QCBOR_TAG_REQUIREMENT_TAG, |
| 4884 | true, |
| 4885 | |
| 4886 | QCBOR_SUCCESS, /* for GetNext */ |
| 4887 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 4888 | -20, |
| 4889 | 0, |
| 4890 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 4891 | |
| 4892 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */ |
| 4893 | 0, |
| 4894 | 0, |
| 4895 | |
| 4896 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */ |
| 4897 | -20, |
| 4898 | {(const uint8_t []){0x00}, 1}, |
| 4899 | false, |
| 4900 | |
| 4901 | QCBOR_SUCCESS, /* for GetBigFloat */ |
| 4902 | -20, |
| 4903 | 4294967295, |
| 4904 | |
| 4905 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4906 | -20, |
| 4907 | {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4}, |
| 4908 | false |
| 4909 | }, |
| 4910 | |
| 4911 | { |
| 4912 | /* Special case for test 8. Don't renumber it. */ |
| 4913 | "8. Untagged pair with big num (big float or decimal fraction), tag optional", |
| 4914 | {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14}, |
| 4915 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4916 | true, |
| 4917 | |
| 4918 | QCBOR_SUCCESS, /* for GetNext */ |
| 4919 | QCBOR_TYPE_ARRAY, |
| 4920 | 0, |
| 4921 | 0, |
| 4922 | {(const uint8_t []){0x00}, 1}, |
| 4923 | |
| 4924 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 4925 | 0, |
| 4926 | 0, |
| 4927 | |
| 4928 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4929 | -20, |
| 4930 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4931 | false, |
| 4932 | |
| 4933 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */ |
| 4934 | 0, |
| 4935 | 0, |
| 4936 | |
| 4937 | QCBOR_SUCCESS, /* for GetBigFloatBig */ |
| 4938 | -20, |
| 4939 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4940 | false |
| 4941 | }, |
| 4942 | |
| 4943 | { |
| 4944 | "9. decimal fraction with large exponent and negative big num mantissa", |
| 4945 | {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4946 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, |
| 4947 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4948 | true, |
| 4949 | |
| 4950 | QCBOR_SUCCESS, /* for GetNext */ |
| 4951 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
| 4952 | 9223372036854775807, |
| 4953 | 0, |
| 4954 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4955 | |
| 4956 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */ |
| 4957 | 0, |
| 4958 | 0, |
| 4959 | |
| 4960 | QCBOR_SUCCESS, /* for GetDecimalFractionBig */ |
| 4961 | 9223372036854775807, |
| 4962 | {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10}, |
| 4963 | true, |
| 4964 | |
| 4965 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */ |
| 4966 | 0, |
| 4967 | 0, |
| 4968 | |
| 4969 | QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */ |
| 4970 | 0, |
| 4971 | {(const uint8_t []){0x00}, 1}, |
| 4972 | false |
| 4973 | }, |
| 4974 | }; |
| 4975 | |
| 4976 | |
| 4977 | |
| 4978 | int32_t ProcessEaMTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4979 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4980 | size_t uIndex; |
| 4981 | QCBORDecodeContext DCtx; |
| 4982 | QCBORItem Item; |
| 4983 | QCBORError uError; |
| 4984 | int64_t nMantissa, nExponent; |
| 4985 | MakeUsefulBufOnStack( MantissaBuf, 200); |
| 4986 | UsefulBufC Mantissa; |
| 4987 | bool bMantissaIsNegative; |
| 4988 | |
| 4989 | for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) { |
| 4990 | const struct EaMTest *pT = &pEaMTests[uIndex]; |
| 4991 | /* Decode with GetNext */ |
| 4992 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 4993 | |
| 4994 | if(uIndex + 1 == 9) { |
| 4995 | nExponent = 99; // just to set a break point |
| 4996 | } |
| 4997 | |
| 4998 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4999 | #ifdef QCBOR_DISABLE_TAGS |
| 5000 | /* Test 8 is a special case when tags are disabled */ |
| 5001 | if(pT->bHasTags && uIndex + 1 != 8) { |
| 5002 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5003 | return (int32_t)(1+uIndex) * 1000 + 9; |
| 5004 | } |
| 5005 | } else { |
| 5006 | #endif |
| 5007 | /* Now check return code, data type, mantissa and exponent */ |
| 5008 | if(pT->uExpectedErrorGN != uError) { |
| 5009 | return (int32_t)(1+uIndex) * 1000 + 1; |
| 5010 | } |
| 5011 | if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) { |
| 5012 | if(pT->uQCBORTypeGN != Item.uDataType) { |
| 5013 | return (int32_t)(1+uIndex) * 1000 + 2; |
| 5014 | } |
| 5015 | if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) { |
| 5016 | return (int32_t)(1+uIndex) * 1000 + 3; |
| 5017 | } |
| 5018 | if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) { |
| 5019 | if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) { |
| 5020 | return (int32_t)(1+uIndex) * 1000 + 4; |
| 5021 | } |
| 5022 | } else { |
| 5023 | if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) { |
| 5024 | return (int32_t)(1+uIndex) * 1000 + 5; |
| 5025 | } |
| 5026 | } |
| 5027 | } |
| 5028 | #ifdef QCBOR_DISABLE_TAGS |
| 5029 | } |
| 5030 | #endif |
| 5031 | |
| 5032 | /* Decode with GetDecimalFraction */ |
| 5033 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5034 | QCBORDecode_GetDecimalFraction(&DCtx, |
| 5035 | pT->uTagRequirement, |
| 5036 | &nMantissa, |
| 5037 | &nExponent); |
| 5038 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5039 | #ifdef QCBOR_DISABLE_TAGS |
| 5040 | if(pT->bHasTags) { |
| 5041 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5042 | return (int32_t)(1+uIndex) * 1000 + 39; |
| 5043 | } |
| 5044 | } else { |
| 5045 | #endif |
| 5046 | /* Now check return code, mantissa and exponent */ |
| 5047 | if(pT->uExpectedErrorGDF != uError) { |
| 5048 | return (int32_t)(1+uIndex) * 1000 + 31; |
| 5049 | } |
| 5050 | if(uError == QCBOR_SUCCESS) { |
| 5051 | if(pT->nExponentGDF != nExponent) { |
| 5052 | return (int32_t)(1+uIndex) * 1000 + 32; |
| 5053 | } |
| 5054 | if(pT->nMantissaGDF != nMantissa) { |
| 5055 | return (int32_t)(1+uIndex) * 1000 + 33; |
| 5056 | } |
| 5057 | } |
| 5058 | #ifdef QCBOR_DISABLE_TAGS |
| 5059 | } |
| 5060 | #endif |
| 5061 | |
| 5062 | /* Decode with GetDecimalFractionBig */ |
| 5063 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5064 | QCBORDecode_GetDecimalFractionBig(&DCtx, |
| 5065 | pT->uTagRequirement, |
| 5066 | MantissaBuf, |
| 5067 | &Mantissa, |
| 5068 | &bMantissaIsNegative, |
| 5069 | &nExponent); |
| 5070 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5071 | #ifdef QCBOR_DISABLE_TAGS |
| 5072 | if(pT->bHasTags) { |
| 5073 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5074 | return (int32_t)(1+uIndex) * 1000 + 49; |
| 5075 | } |
| 5076 | } else { |
| 5077 | #endif |
| 5078 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5079 | if(pT->uExpectedErrorGDFB != uError) { |
| 5080 | return (int32_t)(1+uIndex) * 1000 + 41; |
| 5081 | } |
| 5082 | if(uError == QCBOR_SUCCESS) { |
| 5083 | if(pT->nExponentGDFB != nExponent) { |
| 5084 | return (int32_t)(1+uIndex) * 1000 + 42; |
| 5085 | } |
| 5086 | if(pT->IsNegativeGDFB != bMantissaIsNegative) { |
| 5087 | return (int32_t)(1+uIndex) * 1000 + 43; |
| 5088 | } |
| 5089 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) { |
| 5090 | return (int32_t)(1+uIndex) * 1000 + 44; |
| 5091 | } |
| 5092 | } |
| 5093 | #ifdef QCBOR_DISABLE_TAGS |
| 5094 | } |
| 5095 | #endif |
| 5096 | |
| 5097 | /* Decode with GetBigFloat */ |
| 5098 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5099 | QCBORDecode_GetBigFloat(&DCtx, |
| 5100 | pT->uTagRequirement, |
| 5101 | &nMantissa, |
| 5102 | &nExponent); |
| 5103 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5104 | #ifdef QCBOR_DISABLE_TAGS |
| 5105 | if(pT->bHasTags) { |
| 5106 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5107 | return (int32_t)(1+uIndex) * 1000 + 19; |
| 5108 | } |
| 5109 | } else { |
| 5110 | #endif |
| 5111 | /* Now check return code, mantissa and exponent */ |
| 5112 | if(pT->uExpectedErrorGBF != uError) { |
| 5113 | return (int32_t)(1+uIndex) * 1000 + 11; |
| 5114 | } |
| 5115 | if(uError == QCBOR_SUCCESS) { |
| 5116 | if(pT->nExponentGBF != nExponent) { |
| 5117 | return (int32_t)(1+uIndex) * 1000 + 12; |
| 5118 | } |
| 5119 | if(pT->nMantissaGBF != nMantissa) { |
| 5120 | return (int32_t)(1+uIndex) * 1000 + 13; |
| 5121 | } |
| 5122 | } |
| 5123 | #ifdef QCBOR_DISABLE_TAGS |
| 5124 | } |
| 5125 | #endif |
| 5126 | |
| 5127 | /* Decode with GetBigFloatBig */ |
| 5128 | QCBORDecode_Init(&DCtx, pT->Input, 0); |
| 5129 | QCBORDecode_GetBigFloatBig(&DCtx, |
| 5130 | pT->uTagRequirement, |
| 5131 | MantissaBuf, |
| 5132 | &Mantissa, |
| 5133 | &bMantissaIsNegative, |
| 5134 | &nExponent); |
| 5135 | uError = QCBORDecode_GetAndResetError(&DCtx); |
| 5136 | #ifdef QCBOR_DISABLE_TAGS |
| 5137 | if(pT->bHasTags) { |
| 5138 | if(uError != QCBOR_ERR_TAGS_DISABLED) { |
| 5139 | return (int32_t)(1+uIndex) * 1000 + 29; |
| 5140 | } |
| 5141 | } else { |
| 5142 | #endif |
| 5143 | /* Now check return code, mantissa (bytes and sign) and exponent */ |
| 5144 | if(pT->uExpectedErrorGBFB != uError) { |
| 5145 | return (int32_t)(1+uIndex) * 1000 + 21; |
| 5146 | } |
| 5147 | if(uError == QCBOR_SUCCESS) { |
| 5148 | if(pT->nExponentGBFB != nExponent) { |
| 5149 | return (int32_t)(1+uIndex) * 1000 + 22; |
| 5150 | } |
| 5151 | if(pT->IsNegativeGBFB != bMantissaIsNegative) { |
| 5152 | return (int32_t)(1+uIndex) * 1000 + 23; |
| 5153 | } |
| 5154 | if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) { |
| 5155 | return (int32_t)(1+uIndex) * 1000 + 24; |
| 5156 | } |
| 5157 | } |
| 5158 | #ifdef QCBOR_DISABLE_TAGS |
| 5159 | } |
| 5160 | #endif |
| 5161 | } |
| 5162 | |
| 5163 | return 0; |
| 5164 | } |
| 5165 | |
| 5166 | |
| 5167 | int32_t ExponentAndMantissaDecodeTestsSecondary(void) |
| 5168 | { |
| 5169 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5170 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5171 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5172 | QCBORItem item; |
| 5173 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 5174 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 5175 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 5176 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5177 | |
| 5178 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5179 | |
| 5180 | /* Now encode some stuff and then decode it */ |
| 5181 | uint8_t pBuf[40]; |
| 5182 | QCBOREncodeContext EC; |
| 5183 | UsefulBufC Encoded; |
| 5184 | |
| 5185 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 5186 | QCBOREncode_OpenArray(&EC); |
| 5187 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 5188 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 5189 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 5190 | QCBOREncode_CloseArray(&EC); |
| 5191 | QCBOREncode_Finish(&EC, &Encoded); |
| 5192 | |
| 5193 | |
| 5194 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5195 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5196 | if(uErr != QCBOR_SUCCESS) { |
| 5197 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5198 | } |
| 5199 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5200 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5201 | if(uErr != QCBOR_SUCCESS) { |
| 5202 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 5206 | item.val.expAndMantissa.nExponent != 1000 || |
| 5207 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5208 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5209 | } |
| 5210 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5211 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5212 | if(uErr != QCBOR_SUCCESS) { |
| 5213 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 5217 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 5218 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5219 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5220 | } |
| 5221 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5222 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 5223 | if(uErr != QCBOR_SUCCESS) { |
| 5224 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 5228 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 5229 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5230 | return 106; |
| 5231 | } |
| 5232 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5233 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5234 | |
| 5235 | return 0; |
| 5236 | } |
| 5237 | |
| 5238 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5239 | int32_t ExponentAndMantissaDecodeTests(void) |
| 5240 | { |
| 5241 | int32_t rv = ProcessEaMTests(); |
| 5242 | if(rv) { |
| 5243 | return rv; |
| 5244 | } |
| 5245 | |
| 5246 | return ExponentAndMantissaDecodeTestsSecondary(); |
| 5247 | } |
| 5248 | |
| 5249 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5250 | static const struct FailInput ExponentAndMantissaFailures[] = { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5251 | // Exponent > INT64_MAX |
| 5252 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5253 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5254 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5255 | // Mantissa > INT64_MAX |
| 5256 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5257 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 5258 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5259 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5260 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5261 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5262 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5263 | // bad content for big num |
| 5264 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 5265 | // bad content for big num |
| 5266 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 5267 | // Bad integer for exponent |
| 5268 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 5269 | // Bad integer for mantissa |
| 5270 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 5271 | // 3 items in array |
| 5272 | { {(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] | 5273 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5274 | // unterminated indefinite length array |
| 5275 | { {(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] | 5276 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5277 | // unterminated indefinite length array |
| 5278 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED}, |
| 5279 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5280 | // Empty array |
| 5281 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 5282 | // Second is not an integer |
| 5283 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5284 | // First is not an integer |
| 5285 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 5286 | // Not an array |
| 5287 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 5288 | }; |
| 5289 | |
| 5290 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5291 | int32_t ExponentAndMantissaDecodeFailTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5292 | { |
| 5293 | return ProcessFailures(ExponentAndMantissaFailures, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5294 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 5295 | struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 5296 | } |
| 5297 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5298 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5299 | |
| 5300 | |
| 5301 | |
| 5302 | /* |
| 5303 | Some basic CBOR with map and array used in a lot of tests. |
| 5304 | The map labels are all strings |
| 5305 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5306 | { |
| 5307 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5308 | "an array of two strings": [ |
| 5309 | "string1", "string2" |
| 5310 | ], |
| 5311 | "map in a map": { |
| 5312 | "bytes 1": h'78787878', |
| 5313 | "bytes 2": h'79797979', |
| 5314 | "another int": 98, |
| 5315 | "text 2": "lies, damn lies and statistics" |
| 5316 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5317 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5318 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5319 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5320 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5321 | { |
| 5322 | QCBORItem Item1, Item2, Item3; |
| 5323 | int64_t nDecodedInt1, nDecodedInt2; |
| 5324 | UsefulBufC B1, B2, S1, S2, S3; |
| 5325 | |
| 5326 | QCBORDecodeContext DCtx; |
| 5327 | QCBORError nCBORError; |
| 5328 | |
| 5329 | QCBORDecode_Init(&DCtx, input, 0); |
| 5330 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5331 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5332 | |
| 5333 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 5334 | |
| 5335 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5336 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5337 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 5338 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 5339 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5340 | QCBORDecode_ExitMap(&DCtx); |
| 5341 | |
| 5342 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5343 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5344 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 5345 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5346 | return -400; |
| 5347 | } |
| 5348 | QCBORDecode_ExitArray(&DCtx); |
| 5349 | |
| 5350 | // Parse the same array again using GetText() instead of GetItem() |
| 5351 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5352 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 5353 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5354 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 5355 | return 5000; |
| 5356 | } |
| 5357 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 5358 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5359 | return 5001; |
| 5360 | } */ |
| 5361 | |
| 5362 | QCBORDecode_ExitArray(&DCtx); |
| 5363 | |
| 5364 | QCBORDecode_ExitMap(&DCtx); |
| 5365 | |
| 5366 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 5367 | |
| 5368 | if(nCBORError) { |
| 5369 | return (int32_t)nCBORError; |
| 5370 | } |
| 5371 | |
| 5372 | if(nDecodedInt1 != 42) { |
| 5373 | return 1001; |
| 5374 | } |
| 5375 | |
| 5376 | if(nDecodedInt2 != 98) { |
| 5377 | return 1002; |
| 5378 | } |
| 5379 | |
| 5380 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5381 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5382 | return 1003; |
| 5383 | } |
| 5384 | |
| 5385 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5386 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5387 | return 1004; |
| 5388 | } |
| 5389 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5390 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5391 | return 1005; |
| 5392 | } |
| 5393 | |
| 5394 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 5395 | return 1006; |
| 5396 | } |
| 5397 | |
| 5398 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 5399 | return 1007; |
| 5400 | } |
| 5401 | |
| 5402 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 5403 | return 1008; |
| 5404 | } |
| 5405 | |
| 5406 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 5407 | return 1009; |
| 5408 | } |
| 5409 | |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5413 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5414 | { |
| 5415 | -75008: h'05083399', |
| 5416 | 88: [], |
| 5417 | 100100: { |
| 5418 | "sub1": { |
| 5419 | 10: [ |
| 5420 | 0 |
| 5421 | ], |
| 5422 | -75009: h'A46823990001', |
| 5423 | 100100: { |
| 5424 | "json": "{ \"ueid\", \"xyz\"}", |
| 5425 | "subsub": { |
| 5426 | 100002: h'141813191001' |
| 5427 | } |
| 5428 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5429 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5430 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5431 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5432 | */ |
| 5433 | |
| 5434 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5435 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 5436 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 5437 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 5438 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 5439 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 5440 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 5441 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 5442 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 5443 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 5444 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 5445 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 5446 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5447 | }; |
| 5448 | |
| 5449 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 5450 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 5451 | { |
| 5452 | UsefulBufC String; |
| 5453 | |
| 5454 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 5455 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 5456 | |
| 5457 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5458 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 5459 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5460 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 5461 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 5462 | if(QCBORDecode_GetError(pDCtx)) { |
| 5463 | return 4001; |
| 5464 | } |
| 5465 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 5466 | return 4002; |
| 5467 | } |
| 5468 | QCBORDecode_ExitMap(pDCtx); |
| 5469 | QCBORDecode_ExitMap(pDCtx); |
| 5470 | QCBORDecode_ExitMap(pDCtx); |
| 5471 | QCBORDecode_ExitMap(pDCtx); |
| 5472 | |
| 5473 | return 0; |
| 5474 | } |
| 5475 | |
| 5476 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 5477 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 5478 | { |
| 5479 | QCBORItem Item; |
| 5480 | QCBORError uErr; |
| 5481 | |
| 5482 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 5483 | for(int x = 0; x < 20; x++) { |
| 5484 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5485 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5486 | return 4100; |
| 5487 | |
| 5488 | } |
| 5489 | } |
| 5490 | QCBORDecode_ExitArray(pDCtx); |
| 5491 | if(QCBORDecode_GetError(pDCtx)) { |
| 5492 | return 4101; |
| 5493 | } |
| 5494 | |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
| 5498 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 5499 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 5500 | { |
| 5501 | QCBORError uErr; |
| 5502 | |
| 5503 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5504 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 5505 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5506 | int64_t nInt = 99; |
| 5507 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 5508 | if(nInt != 0) { |
| 5509 | return 4200; |
| 5510 | } |
| 5511 | for(int x = 0; x < 20; x++) { |
| 5512 | QCBORItem Item; |
| 5513 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5514 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5515 | return 4201; |
| 5516 | |
| 5517 | } |
| 5518 | } |
| 5519 | QCBORDecode_ExitArray(pDCtx); |
| 5520 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 5521 | return 4202; |
| 5522 | } |
| 5523 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5524 | UsefulBufC dD; |
| 5525 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5526 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5527 | return 4203; |
| 5528 | } |
| 5529 | for(int x = 0; x < 20; x++) { |
| 5530 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5531 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 5532 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5533 | return 4204; |
| 5534 | } |
| 5535 | } |
| 5536 | QCBORDecode_ExitArray(pDCtx); |
| 5537 | QCBORDecode_ExitMap(pDCtx); |
| 5538 | QCBORDecode_ExitMap(pDCtx); |
| 5539 | |
| 5540 | return 0; |
| 5541 | } |
| 5542 | |
| 5543 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 5544 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 5545 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5546 | { |
| 5547 | QCBORDecodeContext DCtx; |
| 5548 | int32_t nReturn; |
| 5549 | QCBORError uErr; |
| 5550 | |
| 5551 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 5552 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5553 | |
| 5554 | for(int j = 0; j < 5; j++) { |
| 5555 | for(int i = 0; i < 20; i++) { |
| 5556 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 5557 | if(nReturn) { |
| 5558 | return nReturn; |
| 5559 | } |
| 5560 | } |
| 5561 | |
| 5562 | for(int i = 0; i < 20; i++) { |
| 5563 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 5564 | if(nReturn ) { |
| 5565 | return nReturn; |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | for(int i = 0; i < 20; i++) { |
| 5570 | nReturn = DecodeNestedGetZero(&DCtx); |
| 5571 | if(nReturn ) { |
| 5572 | return nReturn; |
| 5573 | } |
| 5574 | } |
| 5575 | } |
| 5576 | |
| 5577 | QCBORDecode_ExitMap(&DCtx); |
| 5578 | uErr = QCBORDecode_Finish(&DCtx); |
| 5579 | if(uErr) { |
| 5580 | return (int32_t)uErr + 4100; |
| 5581 | } |
| 5582 | |
| 5583 | return 0; |
| 5584 | } |
| 5585 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5586 | |
| 5587 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5588 | [ |
| 5589 | 23, |
| 5590 | 6000, |
| 5591 | h'67616C6163746963', |
| 5592 | h'686176656E20746F6B656E' |
| 5593 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5594 | */ |
| 5595 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5596 | 0x84, |
| 5597 | 0x17, |
| 5598 | 0x19, 0x17, 0x70, |
| 5599 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 5600 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5601 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5602 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5603 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 5604 | |
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 spEmptyMap[] = {0xa0}; |
| 5607 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5608 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5609 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5610 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5611 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5612 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5613 | /* |
| 5614 | { |
| 5615 | 0: [], |
| 5616 | 9: [ |
| 5617 | [], |
| 5618 | [] |
| 5619 | ], |
| 5620 | 8: { |
| 5621 | 1: [], |
| 5622 | 2: {}, |
| 5623 | 3: [] |
| 5624 | }, |
| 5625 | 4: {}, |
| 5626 | 5: [], |
| 5627 | 6: [ |
| 5628 | [], |
| 5629 | [] |
| 5630 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5631 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5632 | */ |
| 5633 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5634 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 5635 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 5636 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 5637 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5638 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5639 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5640 | |
| 5641 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5642 | /* |
| 5643 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5644 | Duplicate label |
| 5645 | Integer overflow |
| 5646 | Date overflow |
| 5647 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5648 | { |
| 5649 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5650 | 3: -18446744073709551616, |
| 5651 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5652 | 5: 0, |
| 5653 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5654 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5655 | */ |
| 5656 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5657 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5658 | 0xa6, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5659 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5660 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 5661 | #else |
| 5662 | 0xa4, |
| 5663 | #endif |
| 5664 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5665 | 0x05, 0x00, |
| 5666 | 0x05, 0x00, |
| 5667 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5668 | }; |
| 5669 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5670 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5671 | static const uint8_t spUnRecoverableMapError1[] = { |
| 5672 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5673 | }; |
| 5674 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5675 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5676 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5677 | static const uint8_t spUnRecoverableMapError2[] = { |
| 5678 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 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 spUnRecoverableMapError3[] = { |
| 5683 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 5684 | }; |
| 5685 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5686 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5687 | static const uint8_t spUnRecoverableMapError4[] = { |
| 5688 | 0xbf, |
| 5689 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5690 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5691 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5692 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5693 | 0xff |
| 5694 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5695 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5696 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 5697 | const unsigned char not_well_formed_submod_section[] = { |
| 5698 | 0xa1, 0x14, 0x1f, |
| 5699 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5700 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5701 | |
| 5702 | /* Array of length 3, but only two items. */ |
| 5703 | const unsigned char spBadConsumeInput[] = { |
| 5704 | 0x83, 0x00, 0x00 |
| 5705 | }; |
| 5706 | |
| 5707 | /* Tag nesting too deep. */ |
| 5708 | const unsigned char spBadConsumeInput2[] = { |
| 5709 | 0x81, |
| 5710 | 0xD8, 0x37, |
| 5711 | 0xD8, 0x2C, |
| 5712 | 0xD8, 0x21, |
| 5713 | 0xD6, |
| 5714 | 0xCB, |
| 5715 | 00 |
| 5716 | }; |
| 5717 | |
| 5718 | const unsigned char spBadConsumeInput3[] = { |
| 5719 | 0x81, 0xc0, 0x81, 0x00 |
| 5720 | }; |
| 5721 | |
| 5722 | const unsigned char spBadConsumeInput4[] = { |
| 5723 | 0x81, 0x9f, 0x00, 0xff |
| 5724 | }; |
| 5725 | |
| 5726 | const unsigned char spBadConsumeInput5[] = { |
| 5727 | 0xa1, 0x80, 0x00 |
| 5728 | }; |
| 5729 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame^] | 5730 | /* |
| 5731 | Lots of nesting for various nesting tests. |
| 5732 | { 1:1, |
| 5733 | 2:{ |
| 5734 | 21:21, |
| 5735 | 22:{ |
| 5736 | 221:[2111, 2112, 2113], |
| 5737 | 222:222, |
| 5738 | 223: {} |
| 5739 | }, |
| 5740 | 23: 23 |
| 5741 | }, |
| 5742 | 3:3, |
| 5743 | 4: [ {} ] |
| 5744 | } |
| 5745 | */ |
| 5746 | static const uint8_t spNested[] = { |
| 5747 | 0xA4, /* Map of 4 */ |
| 5748 | 0x01, 0x01, /* Map entry 1 : 1 */ |
| 5749 | 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */ |
| 5750 | 0x15, 0x15, /* Map entry 21 : 21 */ |
| 5751 | 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */ |
| 5752 | 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */ |
| 5753 | 0x19, 0x08, 0x3F, /* Array item 2111 */ |
| 5754 | 0x19, 0x08, 0x40, /* Array item 2112 */ |
| 5755 | 0x19, 0x08, 0x41, /* Array item 2113 */ |
| 5756 | 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */ |
| 5757 | 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */ |
| 5758 | 0x17, 0x17, /* Map entry 23 : 23 */ |
| 5759 | 0x03, 0x03, /* Map entry 3 : 3 */ |
| 5760 | 0x04, 0x81, /* Map entry 4: [, an array of 1 */ |
| 5761 | 0xA0 /* Array entry {}, an empty map */ |
| 5762 | }; |
| 5763 | |
| 5764 | |
| 5765 | static int32_t EnterMapCursorTest(void) |
| 5766 | { |
| 5767 | QCBORDecodeContext DCtx; |
| 5768 | QCBORItem Item1; |
| 5769 | |
| 5770 | int i; |
| 5771 | for(i = 0; i < 13; i++) { |
| 5772 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 5773 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5774 | int j; |
| 5775 | /* Move travesal cursor */ |
| 5776 | for(j = 0; j < i; j++) { |
| 5777 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5778 | } |
| 5779 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 5780 | QCBORDecode_ExitMap(&DCtx); |
| 5781 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5782 | if(Item1.label.int64 != 3) { |
| 5783 | return 8000; |
| 5784 | } |
| 5785 | } |
| 5786 | |
| 5787 | for(i = 0; i < 13; i++) { |
| 5788 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 5789 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5790 | int j; |
| 5791 | /* Move travesal cursor */ |
| 5792 | for(j = 0; j < i; j++) { |
| 5793 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5794 | } |
| 5795 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 5796 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 5797 | QCBORDecode_ExitMap(&DCtx); |
| 5798 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5799 | if(Item1.label.int64 != 23) { |
| 5800 | return 8000; |
| 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | for(i = 0; i < 13; i++) { |
| 5805 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0); |
| 5806 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5807 | int j; |
| 5808 | /* Move travesal cursor */ |
| 5809 | for(j = 0; j < i; j++) { |
| 5810 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5811 | } |
| 5812 | QCBORDecode_EnterMapFromMapN(&DCtx, 2); |
| 5813 | QCBORDecode_EnterMapFromMapN(&DCtx, 22); |
| 5814 | for(j = 0; j < i; j++) { |
| 5815 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5816 | } |
| 5817 | QCBORDecode_EnterArrayFromMapN(&DCtx, 221); |
| 5818 | QCBORDecode_ExitArray(&DCtx); |
| 5819 | QCBORDecode_ExitMap(&DCtx); |
| 5820 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5821 | if(Item1.label.int64 != 23) { |
| 5822 | return 8000; |
| 5823 | } |
| 5824 | QCBORDecode_ExitMap(&DCtx); |
| 5825 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5826 | if(Item1.label.int64 != 3) { |
| 5827 | return 8000; |
| 5828 | } |
| 5829 | } |
| 5830 | |
| 5831 | return 0; |
| 5832 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 5833 | |
| 5834 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 5835 | int32_t EnterMapTest(void) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5836 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5837 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5838 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5839 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5840 | int32_t nReturn; |
| 5841 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5842 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5843 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5844 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5845 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5846 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5847 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5848 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5849 | QCBORDecode_ExitArray(&DCtx); |
| 5850 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5851 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 5852 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5853 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5854 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5855 | QCBORDecode_ExitArray(&DCtx); |
| 5856 | QCBORDecode_ExitArray(&DCtx); |
| 5857 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5858 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 5859 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5860 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5861 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5862 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5863 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5864 | QCBORDecode_ExitArray(&DCtx); |
| 5865 | QCBORDecode_ExitMap(&DCtx); |
| 5866 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5867 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5868 | QCBORDecode_ExitMap(&DCtx); |
| 5869 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5870 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5871 | QCBORDecode_ExitArray(&DCtx); |
| 5872 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5873 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 5874 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5875 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5876 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5877 | QCBORDecode_ExitArray(&DCtx); |
| 5878 | QCBORDecode_ExitArray(&DCtx); |
| 5879 | |
| 5880 | QCBORDecode_ExitMap(&DCtx); |
| 5881 | |
| 5882 | uErr = QCBORDecode_Finish(&DCtx); |
| 5883 | if(uErr != QCBOR_SUCCESS){ |
| 5884 | return 3011; |
| 5885 | } |
| 5886 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5887 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5888 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5889 | if(nReturn) { |
| 5890 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5891 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5892 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5893 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5894 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5895 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5896 | if(nReturn) { |
| 5897 | return nReturn; |
| 5898 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5899 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5900 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5901 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5902 | // These tests confirm the cursor is at the right place after entering |
| 5903 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5904 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5905 | |
| 5906 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5907 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5908 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5909 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5910 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 5911 | return 2001; |
| 5912 | } |
| 5913 | |
| 5914 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5915 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5916 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5917 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5918 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 5919 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5920 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5921 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 5922 | return 2051; |
| 5923 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5924 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5925 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 5926 | return 2002; |
| 5927 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5928 | QCBORDecode_ExitArray(&DCtx); |
| 5929 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 5930 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5931 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5932 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 5933 | return 2052; |
| 5934 | } |
| 5935 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5936 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5937 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5938 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5939 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5940 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5941 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5942 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5943 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5944 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 5945 | return 2003; |
| 5946 | } |
| 5947 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5948 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5949 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5950 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5951 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5952 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5953 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5954 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5955 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5956 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5957 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5958 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5959 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5960 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5961 | } |
| 5962 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5963 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5964 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 5965 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5966 | QCBORDecode_ExitArray(&DCtx); |
| 5967 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5968 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 5969 | return 2006; |
| 5970 | } |
| 5971 | QCBORDecode_ExitMap(&DCtx); |
| 5972 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5973 | return 2007; |
| 5974 | } |
| 5975 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5976 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5977 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5978 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5979 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5980 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5981 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5982 | return 2008; |
| 5983 | } |
| 5984 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5985 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5986 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5987 | return 2009; |
| 5988 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5989 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5990 | |
| 5991 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5992 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5993 | // This will fail because the map is empty. |
| 5994 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5995 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5996 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5997 | return 2010; |
| 5998 | } |
| 5999 | QCBORDecode_ExitMap(&DCtx); |
| 6000 | uErr = QCBORDecode_Finish(&DCtx); |
| 6001 | if(uErr != QCBOR_SUCCESS){ |
| 6002 | return 2011; |
| 6003 | } |
| 6004 | |
| 6005 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6006 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6007 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6008 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6009 | // This will fail because the map is empty. |
| 6010 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 6011 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6012 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6013 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6014 | } |
| 6015 | QCBORDecode_ExitMap(&DCtx); |
| 6016 | uErr = QCBORDecode_Finish(&DCtx); |
| 6017 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 6018 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6019 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6020 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 6021 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6022 | |
| 6023 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6024 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 6025 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6026 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6027 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6028 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6029 | QCBORDecode_ExitArray(&DCtx); |
| 6030 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 6031 | QCBORDecode_ExitArray(&DCtx); |
| 6032 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6033 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 6034 | return 2014; |
| 6035 | } |
| 6036 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6037 | int64_t nInt; |
| 6038 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6039 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6040 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6041 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6042 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6043 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 6044 | return 2021; |
| 6045 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6046 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 6047 | return 2121; |
| 6048 | } |
| 6049 | (void)QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6050 | #endif |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 6051 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6052 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6053 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 6054 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6055 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 6056 | return 2023; |
| 6057 | } |
| 6058 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6059 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6060 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 6061 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6062 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6063 | return 2024; |
| 6064 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6065 | #endif |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6066 | |
| 6067 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 6068 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6069 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 6070 | return 2025; |
| 6071 | } |
| 6072 | |
| 6073 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 6074 | |
| 6075 | QCBORDecode_ExitMap(&DCtx); |
| 6076 | uErr = QCBORDecode_Finish(&DCtx); |
| 6077 | if(uErr != QCBOR_SUCCESS) { |
| 6078 | return 2026; |
| 6079 | } |
| 6080 | |
| 6081 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6082 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6083 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6084 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6085 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 6086 | return 2030; |
| 6087 | } |
| 6088 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6089 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6090 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6091 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6092 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6093 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6094 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6095 | return 2031; |
| 6096 | } |
| 6097 | |
| 6098 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6099 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6100 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6101 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6102 | if(uErr != QCBOR_ERR_HIT_END) { |
| 6103 | return 2032; |
| 6104 | } |
| 6105 | |
| 6106 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6107 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 6108 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 6109 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 6110 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 6111 | return 2033; |
| 6112 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6113 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 6114 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 6115 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6116 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6117 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 6118 | return 2401; |
| 6119 | } |
| 6120 | if(QCBORDecode_GetError(&DCtx)) { |
| 6121 | return 2402; |
| 6122 | } |
| 6123 | |
| 6124 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6125 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 6126 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 6127 | Item1.val.uCount != 3 || |
| 6128 | Item1.uNextNestLevel != 1) { |
| 6129 | return 2403; |
| 6130 | } |
| 6131 | if(QCBORDecode_GetError(&DCtx)) { |
| 6132 | return 2404; |
| 6133 | } |
| 6134 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6135 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 6136 | Item1.uNextNestLevel != 1 || |
| 6137 | Item1.val.int64 != 42) { |
| 6138 | return 2405; |
| 6139 | } |
| 6140 | if(QCBORDecode_GetError(&DCtx)) { |
| 6141 | return 2406; |
| 6142 | } |
| 6143 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6144 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 6145 | Item1.uNestingLevel != 1 || |
| 6146 | Item1.uNextNestLevel != 1 || |
| 6147 | Item1.val.uCount != 2) { |
| 6148 | return 2407; |
| 6149 | } |
| 6150 | if(QCBORDecode_GetError(&DCtx)) { |
| 6151 | return 2408; |
| 6152 | } |
| 6153 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6154 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 6155 | Item1.uNestingLevel != 1 || |
| 6156 | Item1.uNextNestLevel != 0 || |
| 6157 | Item1.val.uCount != 4) { |
| 6158 | return 2409; |
| 6159 | } |
| 6160 | if(QCBORDecode_GetError(&DCtx)) { |
| 6161 | return 2410; |
| 6162 | } |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6163 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6164 | nReturn = DecodeNestedIterate(); |
| 6165 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 6166 | |
| 6167 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 6168 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 6169 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 6170 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 6171 | return 2500; |
| 6172 | } |
| 6173 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6174 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 6175 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6176 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 6177 | return 2600; |
| 6178 | } |
| 6179 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6180 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6181 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 6182 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6183 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6184 | return 2700; |
| 6185 | } |
| 6186 | |
| 6187 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0); |
| 6188 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6189 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 6190 | return 2800; |
| 6191 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6192 | #endif |
| 6193 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 6194 | |
| 6195 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 6196 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6197 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6198 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 6199 | return 2900; |
| 6200 | } |
| 6201 | #else |
| 6202 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6203 | return 2901; |
| 6204 | } |
| 6205 | #endif |
| 6206 | |
| 6207 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 6208 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 6209 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 6210 | return 3000; |
| 6211 | } |
| 6212 | |
Laurence Lundblade | c5f45e4 | 2023-12-18 09:23:20 -0700 | [diff] [blame^] | 6213 | nReturn = EnterMapCursorTest(); |
| 6214 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 6215 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6219 | struct NumberConversion { |
| 6220 | char *szDescription; |
| 6221 | UsefulBufC CBOR; |
| 6222 | int64_t nConvertedToInt64; |
| 6223 | QCBORError uErrorInt64; |
| 6224 | uint64_t uConvertToUInt64; |
| 6225 | QCBORError uErrorUint64; |
| 6226 | double dConvertToDouble; |
| 6227 | QCBORError uErrorDouble; |
| 6228 | }; |
| 6229 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6230 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 6231 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 6232 | #else |
| 6233 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 6234 | #endif |
| 6235 | |
| 6236 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6237 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6238 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6239 | { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6240 | "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN", |
| 6241 | {(uint8_t[]){0xC5, 0x82, 0x20, |
| 6242 | 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff, |
| 6243 | }, 15}, |
| 6244 | -4611686018427387904, /* INT64_MIN / 2 */ |
| 6245 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
| 6246 | 0, |
| 6247 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 6248 | -4.6116860184273879E+18, |
| 6249 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
| 6250 | }, |
| 6251 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6252 | "too large to fit into int64_t", |
| 6253 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 6254 | 0, |
| 6255 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6256 | 0, |
| 6257 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6258 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6259 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6260 | }, |
| 6261 | { |
| 6262 | "largest negative int that fits in int64_t", |
| 6263 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 6264 | INT64_MIN, |
| 6265 | QCBOR_SUCCESS, |
| 6266 | 0, |
| 6267 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6268 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6269 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6270 | }, |
| 6271 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6272 | "negative bignum -1", |
| 6273 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 6274 | -1, |
| 6275 | QCBOR_SUCCESS, |
| 6276 | 0, |
| 6277 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6278 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6279 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6280 | }, |
| 6281 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6282 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6283 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6284 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 6285 | 257000, |
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 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6288 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6289 | 257000.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 | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6293 | "bigfloat with negative bignum -258 * 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 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6296 | -2064, |
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 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6299 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6300 | -2064.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 | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6304 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6305 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6306 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 6307 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6308 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6309 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6310 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6311 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6312 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6313 | }, |
| 6314 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 6315 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6316 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 6317 | 0, |
| 6318 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6319 | 0, |
| 6320 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6321 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6322 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6323 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6324 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6325 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6326 | "Positive bignum 0x01020304 indefinite length string", |
| 6327 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 6328 | 0x01020304, |
| 6329 | QCBOR_SUCCESS, |
| 6330 | 0x01020304, |
| 6331 | QCBOR_SUCCESS, |
| 6332 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6333 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6334 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6335 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6336 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 6337 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6338 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 6339 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 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_NUMBER_SIGN_CONVERSION), |
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 | { |
| 6348 | "big float [9223372036854775806, 9223372036854775806]", |
| 6349 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6350 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 6351 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6352 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6353 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6354 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6355 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6356 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6357 | }, |
| 6358 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6359 | "Big float 3 * 2^^2", |
| 6360 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 6361 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6362 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6363 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6364 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6365 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6366 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6367 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6368 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6369 | "Decimal fraction 3/10", |
| 6370 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 6371 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6372 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6373 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6374 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6375 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6376 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6377 | }, |
| 6378 | { |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6379 | "extreme pos bignum", |
| 6380 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 6381 | // 50 rows of 8 is 400 digits. |
| 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, |
| 6420 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6421 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6422 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6423 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6424 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6425 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6426 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6427 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6428 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6429 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6430 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 6431 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6432 | 404}, |
| 6433 | 0, |
| 6434 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6435 | 0, |
| 6436 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6437 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6438 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6439 | }, |
| 6440 | |
| 6441 | { |
| 6442 | "extreme neg bignum", |
| 6443 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 6444 | // 50 rows of 8 is 400 digits. |
| 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, |
| 6483 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6484 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6485 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6486 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6487 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6488 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6489 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6490 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6491 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6492 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6493 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
gtravisb | 787e82a | 2023-11-30 18:38:21 -0800 | [diff] [blame] | 6494 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6495 | 404}, |
| 6496 | 0, |
| 6497 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6498 | 0, |
| 6499 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6500 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6501 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6502 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6503 | |
| 6504 | { |
| 6505 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 6506 | {(uint8_t[]){ |
| 6507 | 0xC5, 0x82, |
| 6508 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6509 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6510 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6511 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6512 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6513 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6514 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6515 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6516 | }, |
| 6517 | |
| 6518 | { |
| 6519 | "bigfloat that evaluates to -INFINITY", |
| 6520 | {(uint8_t[]){ |
| 6521 | 0xC5, 0x82, |
| 6522 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6523 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6524 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6525 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6526 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6527 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6528 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6529 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6530 | }, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6531 | { |
| 6532 | "Positive bignum 0xffff", |
| 6533 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 6534 | 65536-1, |
| 6535 | QCBOR_SUCCESS, |
| 6536 | 0xffff, |
| 6537 | QCBOR_SUCCESS, |
| 6538 | 65535.0, |
| 6539 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6540 | }, |
| 6541 | #endif /* QCBOR_DISABLE_TAGS */ |
| 6542 | { |
| 6543 | "Positive integer 18446744073709551615", |
| 6544 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 6545 | 0, |
| 6546 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6547 | 18446744073709551615ULL, |
| 6548 | QCBOR_SUCCESS, |
| 6549 | 18446744073709551615.0, |
| 6550 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6551 | }, |
| 6552 | |
| 6553 | { |
| 6554 | "Postive integer 0", |
| 6555 | {(uint8_t[]){0x0}, 1}, |
| 6556 | 0LL, |
| 6557 | QCBOR_SUCCESS, |
| 6558 | 0ULL, |
| 6559 | QCBOR_SUCCESS, |
| 6560 | 0.0, |
| 6561 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6562 | }, |
| 6563 | { |
| 6564 | "Negative integer -18446744073709551616", |
| 6565 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 6566 | -9223372036854775807-1, // INT64_MIN |
| 6567 | QCBOR_SUCCESS, |
| 6568 | 0ULL, |
| 6569 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6570 | -9223372036854775808.0, |
| 6571 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6572 | }, |
| 6573 | { |
| 6574 | "Double Floating point value 100.3", |
| 6575 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 6576 | 100L, |
| 6577 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6578 | 100ULL, |
| 6579 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6580 | 100.3, |
| 6581 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
| 6582 | }, |
| 6583 | { |
| 6584 | "Floating point value NaN 0xfa7fc00000", |
| 6585 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 6586 | 0, |
| 6587 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6588 | 0, |
| 6589 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6590 | NAN, |
| 6591 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6592 | }, |
| 6593 | { |
| 6594 | "half-precision Floating point value -4", |
| 6595 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 6596 | // Normal case with all enabled. |
| 6597 | -4, |
| 6598 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
| 6599 | 0, |
| 6600 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
| 6601 | -4.0, |
| 6602 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 6603 | }, |
| 6604 | { |
| 6605 | "+inifinity single precision", |
| 6606 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 6607 | 0, |
| 6608 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
| 6609 | 0, |
| 6610 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
| 6611 | INFINITY, |
| 6612 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 6613 | }, |
| 6614 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6615 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6616 | |
| 6617 | |
| 6618 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6619 | |
| 6620 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 6621 | { |
| 6622 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 6623 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6624 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 6625 | return 1; |
| 6626 | } |
| 6627 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6628 | (void)Pool; |
| 6629 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6630 | return 0; |
| 6631 | } |
| 6632 | |
| 6633 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6634 | int32_t IntegerConvertTest(void) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6635 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6636 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 6637 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6638 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6639 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 6640 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6641 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6642 | // Set up the decoding context including a memory pool so that |
| 6643 | // indefinite length items can be checked |
| 6644 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6645 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6646 | |
| 6647 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6648 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6649 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6653 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6654 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6655 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6656 | } |
| 6657 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6658 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6659 | } |
| 6660 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6661 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6662 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6663 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6664 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6665 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6666 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6667 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6668 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6669 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6670 | } |
| 6671 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6672 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6673 | } |
| 6674 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6675 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6676 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6677 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6678 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6679 | |
| 6680 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6681 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6682 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6683 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6684 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6685 | } |
| 6686 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 6687 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6688 | // NaN's can't be compared for equality. A NaN is |
| 6689 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6690 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6691 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6692 | } |
| 6693 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6694 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6695 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6696 | } |
| 6697 | } |
| 6698 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6699 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6700 | } |
| 6701 | |
| 6702 | return 0; |
| 6703 | } |
| 6704 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6705 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6706 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6707 | int32_t CBORTestIssue134(void) |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6708 | { |
| 6709 | QCBORDecodeContext DCtx; |
| 6710 | QCBORItem Item; |
| 6711 | QCBORError uCBORError; |
| 6712 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6713 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6714 | QCBORDecode_Init(&DCtx, |
| 6715 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 6716 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6717 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6718 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 6719 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6720 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6721 | do { |
| 6722 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6723 | } while (QCBOR_SUCCESS == uCBORError); |
| 6724 | |
| 6725 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6726 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 6727 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6728 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6729 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6730 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6731 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6732 | |
| 6733 | |
| 6734 | static const uint8_t spSequenceTestInput[] = { |
| 6735 | /* 1. The valid date string "1985-04-12" */ |
| 6736 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 6737 | |
| 6738 | /* 2. */ |
| 6739 | 0x00, |
| 6740 | |
| 6741 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 6742 | 0x1a, 0x53, 0x72, 0x4E, 0x00, |
| 6743 | |
| 6744 | /* 4. */ |
| 6745 | 0x62, 'h', 'i', |
| 6746 | }; |
| 6747 | |
| 6748 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6749 | int32_t CBORSequenceDecodeTests(void) |
| 6750 | { |
| 6751 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6752 | QCBORItem Item; |
| 6753 | QCBORError uCBORError; |
| 6754 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6755 | |
| 6756 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6757 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6758 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6759 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput), |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6760 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6761 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6762 | // Get 1. |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6763 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6764 | if(uCBORError != QCBOR_SUCCESS) { |
| 6765 | return 1; |
| 6766 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6767 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6768 | return 2; |
| 6769 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6770 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6771 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6772 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6773 | uConsumed != 11) { |
| 6774 | return 102; |
| 6775 | } |
| 6776 | |
| 6777 | // Get 2. |
| 6778 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6779 | if(uCBORError != QCBOR_SUCCESS) { |
| 6780 | return 66; |
| 6781 | } |
| 6782 | |
| 6783 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6784 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6785 | uConsumed != 12) { |
| 6786 | return 102; |
| 6787 | } |
| 6788 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6789 | // Get 3. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6790 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6791 | if(uCBORError != QCBOR_SUCCESS) { |
| 6792 | return 2; |
| 6793 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6794 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6795 | return 3; |
| 6796 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6797 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6798 | // A sequence can have stuff at the end that may |
| 6799 | // or may not be valid CBOR. The protocol decoder knows |
| 6800 | // when to stop by definition of the protocol, not |
| 6801 | // when the top-level map or array is ended. |
| 6802 | // Finish still has to be called to know that |
| 6803 | // maps and arrays (if there were any) were closed |
| 6804 | // off correctly. When called like this it |
| 6805 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 6806 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6807 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 6808 | return 4; |
| 6809 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6810 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6811 | // --- Test an empty input ---- |
| 6812 | uint8_t empty[1]; |
| 6813 | UsefulBufC Empty = {empty, 0}; |
| 6814 | QCBORDecode_Init(&DCtx, |
| 6815 | Empty, |
| 6816 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6817 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6818 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6819 | if(uCBORError != QCBOR_SUCCESS) { |
| 6820 | return 5; |
| 6821 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6822 | |
| 6823 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6824 | // --- Sequence with unclosed indefinite length array --- |
| 6825 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6826 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6827 | QCBORDecode_Init(&DCtx, |
| 6828 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 6829 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6830 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6831 | // Get the first item |
| 6832 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6833 | if(uCBORError != QCBOR_SUCCESS) { |
| 6834 | return 7; |
| 6835 | } |
| 6836 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6837 | return 8; |
| 6838 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6839 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6840 | // Get a second item |
| 6841 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6842 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6843 | if(uCBORError != QCBOR_SUCCESS) { |
| 6844 | return 9; |
| 6845 | } |
| 6846 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6847 | return 10; |
| 6848 | } |
| 6849 | |
| 6850 | // Try to finish before consuming all bytes to confirm |
| 6851 | // that the still-open error is returned. |
| 6852 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6853 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6854 | return 11; |
| 6855 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6856 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6857 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6858 | return 20; |
| 6859 | } |
| 6860 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6861 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6862 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6863 | // --- Sequence with a closed indefinite length array --- |
| 6864 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6865 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6866 | QCBORDecode_Init(&DCtx, |
| 6867 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 6868 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6869 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6870 | // Get the first item |
| 6871 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6872 | if(uCBORError != QCBOR_SUCCESS) { |
| 6873 | return 12; |
| 6874 | } |
| 6875 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6876 | return 13; |
| 6877 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6878 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6879 | // Get a second item |
| 6880 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6881 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6882 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6883 | if(uCBORError != QCBOR_SUCCESS) { |
| 6884 | return 14; |
| 6885 | } |
| 6886 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6887 | return 15; |
| 6888 | } |
| 6889 | |
| 6890 | // Try to finish before consuming all bytes to confirm |
| 6891 | // that the still-open error is returned. |
| 6892 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6893 | if(uCBORError != QCBOR_SUCCESS) { |
| 6894 | return 16; |
| 6895 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6896 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6897 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6898 | return 20; |
| 6899 | } |
| 6900 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6901 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6902 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6903 | return 0; |
| 6904 | } |
| 6905 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6906 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 6907 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 6908 | int32_t IntToTests(void) |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6909 | { |
| 6910 | int nErrCode; |
| 6911 | int32_t n32; |
| 6912 | int16_t n16; |
| 6913 | int8_t n8; |
| 6914 | uint32_t u32; |
| 6915 | uint16_t u16; |
| 6916 | uint8_t u8; |
| 6917 | uint64_t u64; |
| 6918 | |
| 6919 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 6920 | if(nErrCode == -1 || n32 != 1) { |
| 6921 | return 1; |
| 6922 | } |
| 6923 | |
| 6924 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 6925 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 6926 | return 2; |
| 6927 | } |
| 6928 | |
| 6929 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 6930 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 6931 | return 3; |
| 6932 | } |
| 6933 | |
| 6934 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 6935 | if(nErrCode != -1) { |
| 6936 | return 4; |
| 6937 | } |
| 6938 | |
| 6939 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 6940 | if(nErrCode != -1) { |
| 6941 | return 5; |
| 6942 | } |
| 6943 | |
| 6944 | |
| 6945 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 6946 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 6947 | return 6; |
| 6948 | } |
| 6949 | |
| 6950 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 6951 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 6952 | return 7; |
| 6953 | } |
| 6954 | |
| 6955 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 6956 | if(nErrCode == -1 || n16 != 1) { |
| 6957 | return 8; |
| 6958 | } |
| 6959 | |
| 6960 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 6961 | if(nErrCode != -1) { |
| 6962 | return 9; |
| 6963 | } |
| 6964 | |
| 6965 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 6966 | if(nErrCode != -1) { |
| 6967 | return 10; |
| 6968 | } |
| 6969 | |
| 6970 | |
| 6971 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 6972 | if(nErrCode == -1 || n8 != 1) { |
| 6973 | return 11; |
| 6974 | } |
| 6975 | |
| 6976 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 6977 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 6978 | return 12; |
| 6979 | } |
| 6980 | |
| 6981 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 6982 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 6983 | return 13; |
| 6984 | } |
| 6985 | |
| 6986 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 6987 | if(nErrCode != -1) { |
| 6988 | return 14; |
| 6989 | } |
| 6990 | |
| 6991 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 6992 | if(nErrCode != -1) { |
| 6993 | return 15; |
| 6994 | } |
| 6995 | |
| 6996 | |
| 6997 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 6998 | if(nErrCode == -1 || u32 != 1) { |
| 6999 | return 16; |
| 7000 | } |
| 7001 | |
| 7002 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 7003 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 7004 | return 17; |
| 7005 | } |
| 7006 | |
| 7007 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 7008 | if(nErrCode == -1 || u32 != 0) { |
| 7009 | return 18; |
| 7010 | } |
| 7011 | |
| 7012 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 7013 | if(nErrCode != -1) { |
| 7014 | return 19; |
| 7015 | } |
| 7016 | |
| 7017 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 7018 | if(nErrCode != -1) { |
| 7019 | return 20; |
| 7020 | } |
| 7021 | |
| 7022 | |
| 7023 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 7024 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 7025 | return 21; |
| 7026 | } |
| 7027 | |
| 7028 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 7029 | if(nErrCode == -1 || u16 != 0) { |
| 7030 | return 22; |
| 7031 | } |
| 7032 | |
| 7033 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 7034 | if(nErrCode == -1 || u16 != 1) { |
| 7035 | return 23; |
| 7036 | } |
| 7037 | |
| 7038 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 7039 | if(nErrCode != -1) { |
| 7040 | return 24; |
| 7041 | } |
| 7042 | |
| 7043 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 7044 | if(nErrCode != -1) { |
| 7045 | return 25; |
| 7046 | } |
| 7047 | |
| 7048 | |
| 7049 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 7050 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 7051 | return 26; |
| 7052 | } |
| 7053 | |
| 7054 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 7055 | if(nErrCode == -1 || u8 != 0) { |
| 7056 | return 27; |
| 7057 | } |
| 7058 | |
| 7059 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 7060 | if(nErrCode == -1 || u8 != 1) { |
| 7061 | return 28; |
| 7062 | } |
| 7063 | |
| 7064 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 7065 | if(nErrCode != -1) { |
| 7066 | return 29; |
| 7067 | } |
| 7068 | |
| 7069 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 7070 | if(nErrCode != -1) { |
| 7071 | return 30; |
| 7072 | } |
| 7073 | |
| 7074 | |
| 7075 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 7076 | if(nErrCode == -1 || u64 != 1) { |
| 7077 | return 31; |
| 7078 | } |
| 7079 | |
| 7080 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 7081 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 7082 | return 32; |
| 7083 | } |
| 7084 | |
| 7085 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 7086 | if(nErrCode == -1 || u64 != 0) { |
| 7087 | return 33; |
| 7088 | } |
| 7089 | |
| 7090 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 7091 | if(nErrCode != -1) { |
| 7092 | return 34; |
| 7093 | } |
| 7094 | |
| 7095 | return 0; |
| 7096 | } |
| 7097 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7098 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7099 | |
| 7100 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7101 | /* |
| 7102 | A sequence with |
| 7103 | A wrapping bstr |
| 7104 | containing a map |
| 7105 | 1 |
| 7106 | 2 |
| 7107 | A wrapping bstr |
| 7108 | containing an array |
| 7109 | 3 |
| 7110 | wrapping bstr |
| 7111 | 4 |
| 7112 | 5 |
| 7113 | 6 |
| 7114 | array |
| 7115 | 7 |
| 7116 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7117 | */ |
| 7118 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7119 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7120 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7121 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7122 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7123 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7124 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7125 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7126 | |
| 7127 | QCBOREncode_BstrWrap(&EC); |
| 7128 | QCBOREncode_OpenMap(&EC); |
| 7129 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 7130 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 7131 | QCBOREncode_CloseMap(&EC); |
| 7132 | QCBOREncode_BstrWrap(&EC); |
| 7133 | QCBOREncode_OpenArray(&EC); |
| 7134 | QCBOREncode_AddInt64(&EC, 3); |
| 7135 | QCBOREncode_BstrWrap(&EC); |
| 7136 | QCBOREncode_AddInt64(&EC, 4); |
| 7137 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7138 | QCBOREncode_AddInt64(&EC, 5); |
| 7139 | QCBOREncode_CloseArray(&EC); |
| 7140 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7141 | QCBOREncode_AddInt64(&EC, 6); |
| 7142 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 7143 | QCBOREncode_OpenArray(&EC); |
| 7144 | QCBOREncode_AddInt64(&EC, 7); |
| 7145 | QCBOREncode_AddInt64(&EC, 8); |
| 7146 | QCBOREncode_CloseArray(&EC); |
| 7147 | |
| 7148 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 7149 | if(uErr) { |
| 7150 | Encoded = NULLUsefulBufC; |
| 7151 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7152 | |
| 7153 | return Encoded; |
| 7154 | } |
| 7155 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7156 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7157 | static const uint8_t spBreakInByteString[] = { |
| 7158 | 0x41, 0xff |
| 7159 | }; |
| 7160 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7161 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7162 | int32_t EnterBstrTest(void) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7163 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7164 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7165 | |
| 7166 | QCBORDecodeContext DC; |
| 7167 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7168 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7169 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7170 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7171 | |
| 7172 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7173 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7174 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7175 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 7176 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7177 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7178 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7179 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7180 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7181 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7182 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7183 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7184 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7185 | QCBORDecode_ExitArray(&DC); |
| 7186 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7187 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7188 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7189 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 7190 | QCBORDecode_GetInt64(&DC, &n7); |
| 7191 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7192 | QCBORDecode_ExitArray(&DC); |
| 7193 | |
| 7194 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7195 | if(uErr) { |
| 7196 | return (int32_t)uErr; |
| 7197 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7198 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7199 | |
| 7200 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 7201 | * Successful because no items are fetched from byte string. |
| 7202 | */ |
| 7203 | QCBORDecode_Init(&DC, |
| 7204 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 7205 | 0); |
| 7206 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7207 | uErr = QCBORDecode_GetError(&DC); |
| 7208 | if(uErr) { |
| 7209 | return 100 + (int32_t)uErr; |
| 7210 | } |
| 7211 | |
| 7212 | QCBORDecode_ExitBstrWrapped(&DC); |
| 7213 | uErr = QCBORDecode_GetError(&DC); |
| 7214 | if(uErr) { |
| 7215 | return 200 + (int32_t)uErr; |
| 7216 | } |
| 7217 | |
| 7218 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 7219 | * It fails because there should be no break. |
| 7220 | */ |
| 7221 | QCBORDecode_Init(&DC, |
| 7222 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 7223 | 0); |
| 7224 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7225 | QCBORItem Item; |
| 7226 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 7227 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 7228 | return 300 + (int32_t)uErr; |
| 7229 | } |
| 7230 | |
| 7231 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 7232 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7233 | |
| 7234 | |
| 7235 | |
| 7236 | |
| 7237 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7238 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7239 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7240 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7241 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7242 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 7243 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 7244 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7245 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7246 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7247 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 7248 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 7249 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7250 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7251 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7252 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7253 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 7254 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7255 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7256 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7257 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 7258 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7259 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7260 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7261 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7262 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 7263 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7264 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7265 | 21, |
| 7266 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 7267 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 7268 | |
| 7269 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7270 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7271 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 7272 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7273 | |
| 7274 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7275 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 7276 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7277 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7278 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7279 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7280 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 7281 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7282 | |
| 7283 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7284 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 7285 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7286 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7287 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7288 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7289 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 7290 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7291 | |
| 7292 | 0x18, 0x33, |
| 7293 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 7294 | |
| 7295 | // MIME |
| 7296 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7297 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 7298 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 7299 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7300 | |
| 7301 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7302 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 7303 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7304 | |
| 7305 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7306 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 7307 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 7308 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7309 | |
| 7310 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7311 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 7312 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7313 | |
| 7314 | // UUID |
| 7315 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7316 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 7317 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7318 | |
| 7319 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7320 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 7321 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7322 | }; |
| 7323 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7324 | int32_t DecodeTaggedTypeTests(void) |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7325 | { |
| 7326 | QCBORDecodeContext DC; |
| 7327 | QCBORError uErr; |
| 7328 | |
| 7329 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 7330 | |
| 7331 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7332 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7333 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7334 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7335 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7336 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7337 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 7338 | return 1; |
| 7339 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7340 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7341 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7342 | return 2; |
| 7343 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7344 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7345 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7346 | return 3; |
| 7347 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7348 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7349 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7350 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7351 | return 4; |
| 7352 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7353 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7354 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7355 | return 5; |
| 7356 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7357 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7358 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7359 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7360 | bNeg != false) { |
| 7361 | return 10; |
| 7362 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7363 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7364 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7365 | bNeg != true) { |
| 7366 | return 11; |
| 7367 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7368 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7369 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7370 | return 12; |
| 7371 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7372 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7373 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7374 | return 13; |
| 7375 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7376 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7377 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7378 | return 14; |
| 7379 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7380 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7381 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7382 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7383 | return 20; |
| 7384 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7385 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7386 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7387 | return 21; |
| 7388 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7389 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7390 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7391 | return 22; |
| 7392 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7393 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7394 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7395 | return 23; |
| 7396 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7397 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7398 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7399 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7400 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7401 | return 30; |
| 7402 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7403 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7404 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7405 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7406 | return 31; |
| 7407 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7408 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7409 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7410 | return 32; |
| 7411 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7412 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7413 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7414 | return 33; |
| 7415 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7416 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7417 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7418 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7419 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7420 | return 40; |
| 7421 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7422 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7423 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7424 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7425 | return 41; |
| 7426 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7427 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7428 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7429 | return 42; |
| 7430 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7431 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7432 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7433 | return 43; |
| 7434 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7435 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7436 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7437 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7438 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7439 | return 50; |
| 7440 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7441 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7442 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7443 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7444 | return 51; |
| 7445 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7446 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7447 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7448 | return 52; |
| 7449 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7450 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7451 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7452 | return 53; |
| 7453 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7454 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7455 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7456 | // MIME |
| 7457 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7458 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7459 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7460 | bIsNot7Bit == true) { |
| 7461 | return 60; |
| 7462 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7463 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7464 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7465 | bIsNot7Bit == true) { |
| 7466 | return 61; |
| 7467 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7468 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7469 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7470 | bIsNot7Bit == false) { |
| 7471 | return 62; |
| 7472 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7473 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7474 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 7475 | bIsNot7Bit == false) { |
| 7476 | return 63; |
| 7477 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7478 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7479 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7480 | return 64; |
| 7481 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7482 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7483 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7484 | return 65; |
| 7485 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7486 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7487 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7488 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7489 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7490 | return 70; |
| 7491 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 7492 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 7493 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7494 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7495 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 7496 | return 71; |
| 7497 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7498 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7499 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7500 | return 72; |
| 7501 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7502 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 7503 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 7504 | return 73; |
| 7505 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7506 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 7507 | // Improvement: add some more error test cases |
| 7508 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 7509 | QCBORDecode_ExitMap(&DC); |
| 7510 | |
| 7511 | uErr = QCBORDecode_Finish(&DC); |
| 7512 | if(uErr != QCBOR_SUCCESS) { |
| 7513 | return 100; |
| 7514 | } |
| 7515 | |
| 7516 | return 0; |
| 7517 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7518 | |
| 7519 | |
| 7520 | |
| 7521 | |
| 7522 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7523 | [ |
| 7524 | "aaaaaaaaaa", |
| 7525 | {} |
| 7526 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7527 | */ |
| 7528 | static const uint8_t spTooLarge1[] = { |
| 7529 | 0x9f, |
| 7530 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7531 | 0xa0, |
| 7532 | 0xff |
| 7533 | }; |
| 7534 | |
| 7535 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7536 | [ |
| 7537 | { |
| 7538 | 0: "aaaaaaaaaa" |
| 7539 | } |
| 7540 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7541 | */ |
| 7542 | static const uint8_t spTooLarge2[] = { |
| 7543 | 0x9f, |
| 7544 | 0xa1, |
| 7545 | 0x00, |
| 7546 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7547 | 0xff |
| 7548 | }; |
| 7549 | |
| 7550 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7551 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7552 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 7553 | { |
| 7554 | 0: "aaaaaaaaaa" |
| 7555 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7556 | */ |
| 7557 | static const uint8_t spTooLarge3[] = { |
| 7558 | 0x4d, |
| 7559 | 0xa1, |
| 7560 | 0x00, |
| 7561 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 7562 | }; |
| 7563 | |
| 7564 | int32_t TooLargeInputTest(void) |
| 7565 | { |
| 7566 | QCBORDecodeContext DC; |
| 7567 | QCBORError uErr; |
| 7568 | UsefulBufC String; |
| 7569 | |
| 7570 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 7571 | // to 10 There's not really any way to test this error |
| 7572 | // condition. The error condition is not complex, so setting |
| 7573 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 7574 | |
| 7575 | // The input CBOR is only too large because the |
| 7576 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 7577 | // |
| 7578 | // This test is disabled for the normal test runs because of the |
| 7579 | // special build requirement. |
| 7580 | |
| 7581 | |
| 7582 | // Tests the start of a map being too large |
| 7583 | 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] | 7584 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7585 | QCBORDecode_GetTextString(&DC, &String); |
| 7586 | uErr = QCBORDecode_GetError(&DC); |
| 7587 | if(uErr != QCBOR_SUCCESS) { |
| 7588 | return 1; |
| 7589 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7590 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7591 | uErr = QCBORDecode_GetError(&DC); |
| 7592 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7593 | return 2; |
| 7594 | } |
| 7595 | |
| 7596 | // Tests the end of a map being too large |
| 7597 | 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] | 7598 | QCBORDecode_EnterArray(&DC, NULL); |
| 7599 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7600 | uErr = QCBORDecode_GetError(&DC); |
| 7601 | if(uErr != QCBOR_SUCCESS) { |
| 7602 | return 3; |
| 7603 | } |
| 7604 | QCBORDecode_ExitMap(&DC); |
| 7605 | uErr = QCBORDecode_GetError(&DC); |
| 7606 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7607 | return 4; |
| 7608 | } |
| 7609 | |
| 7610 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 7611 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 7612 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7613 | uErr = QCBORDecode_GetError(&DC); |
| 7614 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7615 | return 5; |
| 7616 | } |
| 7617 | |
| 7618 | return 0; |
| 7619 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7620 | |
| 7621 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7622 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7623 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7624 | /* |
| 7625 | An array of three map entries |
| 7626 | 1) Indefinite length string label for indefinite lenght byte string |
| 7627 | 2) Indefinite length string label for an integer |
| 7628 | 3) Indefinite length string label for an indefinite-length negative big num |
| 7629 | */ |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7630 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7631 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7632 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 7633 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 7634 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 7635 | 0x03, |
| 7636 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 7637 | 0xc3, |
| 7638 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7639 | }; |
| 7640 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7641 | int32_t SpiffyIndefiniteLengthStringsTests(void) |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7642 | { |
| 7643 | QCBORDecodeContext DCtx; |
| 7644 | |
| 7645 | QCBORDecode_Init(&DCtx, |
| 7646 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 7647 | QCBOR_DECODE_MODE_NORMAL); |
| 7648 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7649 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7650 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 7651 | |
| 7652 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7653 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7654 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7655 | |
| 7656 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7657 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7658 | return 1; |
| 7659 | } |
| 7660 | |
| 7661 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 7662 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 7663 | return 2; |
| 7664 | } |
| 7665 | |
| 7666 | uint64_t uInt; |
| 7667 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 7668 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7669 | return 3; |
| 7670 | } |
| 7671 | if(uInt != 3) { |
| 7672 | return 4; |
| 7673 | } |
| 7674 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7675 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7676 | double uDouble; |
| 7677 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 7678 | "label2", |
| 7679 | 0xff, |
| 7680 | &uDouble); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7681 | |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7682 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7683 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7684 | return 5; |
| 7685 | } |
| 7686 | if(uDouble != -16777474) { |
| 7687 | return 6; |
| 7688 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7689 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7690 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 7691 | return 7; |
| 7692 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7693 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7694 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7695 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7696 | QCBORDecode_ExitMap(&DCtx); |
| 7697 | |
| 7698 | if(QCBORDecode_Finish(&DCtx)) { |
| 7699 | return 99; |
| 7700 | } |
| 7701 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7702 | #else /* QCBOR_DISABLE_TAGS */ |
| 7703 | /* The big num in the input is a CBOR tag and you can't do |
| 7704 | * map lookups in a map with a tag so this test does very little |
| 7705 | * when tags are disabled. That is OK, the test coverage is still |
| 7706 | * good when they are not. |
| 7707 | */ |
| 7708 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 7709 | return 1002; |
| 7710 | } |
| 7711 | #endif /*QCBOR_DISABLE_TAGS */ |
| 7712 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7713 | return 0; |
| 7714 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7715 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7716 | |
| 7717 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7718 | /* |
| 7719 | * An array of an integer and an array. The second array contains |
| 7720 | * a bstr-wrapped map. |
| 7721 | * |
| 7722 | * [7, [h'A36D6669... (see next lines) 73']] |
| 7723 | * |
| 7724 | * {"first integer": 42, |
| 7725 | * "an array of two strings": ["string1", "string2"], |
| 7726 | * "map in a map": |
| 7727 | * { "bytes 1": h'78787878', |
| 7728 | * "bytes 2": h'79797979', |
| 7729 | * "another int": 98, |
| 7730 | * "text 2": "lies, damn lies and statistics" |
| 7731 | * } |
| 7732 | * } |
| 7733 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7734 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7735 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 7736 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 7737 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7738 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7739 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7740 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7741 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7742 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7743 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7744 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7745 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7746 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7747 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7748 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7749 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7750 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7751 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7752 | 0x73 |
| 7753 | }; |
| 7754 | |
| 7755 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7756 | |
| 7757 | /* As above, but the arrays are indefinite length */ |
| 7758 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 7759 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 7760 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7761 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7762 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7763 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7764 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7765 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7766 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7767 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7768 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7769 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7770 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7771 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7772 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7773 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7774 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7775 | 0x73, |
| 7776 | 0xff, 0xff |
| 7777 | }; |
| 7778 | #endif |
| 7779 | |
| 7780 | |
| 7781 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 7782 | |
| 7783 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7784 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 7785 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7786 | |
| 7787 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7788 | |
| 7789 | /* |
| 7790 | An array of one that contains |
| 7791 | a byte string that is tagged 24 which means CBOR-encoded data |
| 7792 | the byte string is an indefinite length string |
| 7793 | the wrapped byte string is an array of three numbers |
| 7794 | [42, 43, 44] |
| 7795 | |
| 7796 | [ |
| 7797 | 24( |
| 7798 | (_ h'83', h'18', h'2A182B', h'182C') |
| 7799 | ) |
| 7800 | ] |
| 7801 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7802 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 7803 | 0x81, |
| 7804 | 0xd8, 0x18, |
| 7805 | 0x5f, |
| 7806 | 0x41, 0x83, |
| 7807 | 0x41, 0x18, |
| 7808 | 0x43, 0x2A, 0x18, 0x2B, |
| 7809 | 0x42, 0x18, 0x2C, |
| 7810 | 0xff |
| 7811 | }; |
| 7812 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7813 | |
| 7814 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 7815 | int32_t PeekAndRewindTest(void) |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7816 | { |
| 7817 | QCBORItem Item; |
| 7818 | QCBORError nCBORError; |
| 7819 | QCBORDecodeContext DCtx; |
| 7820 | |
| 7821 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7822 | |
| 7823 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7824 | return 100+(int32_t)nCBORError; |
| 7825 | } |
| 7826 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7827 | return 200; |
| 7828 | } |
| 7829 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 7830 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 7831 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 7832 | return 150+(int32_t)nCBORError; |
| 7833 | } |
| 7834 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7835 | return 250; |
| 7836 | } |
| 7837 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7838 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7839 | return (int32_t)nCBORError; |
| 7840 | } |
| 7841 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7842 | return 300; |
| 7843 | } |
| 7844 | |
| 7845 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7846 | return 400 + (int32_t)nCBORError; |
| 7847 | } |
| 7848 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7849 | return 500; |
| 7850 | } |
| 7851 | |
| 7852 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7853 | return (int32_t)nCBORError; |
| 7854 | } |
| 7855 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7856 | return 600; |
| 7857 | } |
| 7858 | |
| 7859 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7860 | return 900 + (int32_t)nCBORError; |
| 7861 | } |
| 7862 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7863 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7864 | Item.val.int64 != 42 || |
| 7865 | Item.uDataAlloc || |
| 7866 | Item.uLabelAlloc || |
| 7867 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7868 | return 1000; |
| 7869 | } |
| 7870 | |
| 7871 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7872 | return 1100 + (int32_t)nCBORError; |
| 7873 | } |
| 7874 | |
| 7875 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7876 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7877 | Item.val.int64 != 42 || |
| 7878 | Item.uDataAlloc || |
| 7879 | Item.uLabelAlloc || |
| 7880 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7881 | return 1200; |
| 7882 | } |
| 7883 | |
| 7884 | |
| 7885 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7886 | return 1300 + (int32_t)nCBORError; |
| 7887 | } |
| 7888 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7889 | Item.uDataAlloc || |
| 7890 | Item.uLabelAlloc || |
| 7891 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7892 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7893 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7894 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7895 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7896 | |
| 7897 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7898 | return 1500 + (int32_t)nCBORError; |
| 7899 | } |
| 7900 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7901 | Item.uDataAlloc || |
| 7902 | Item.uLabelAlloc || |
| 7903 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7904 | return 1600; |
| 7905 | } |
| 7906 | |
| 7907 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7908 | return 1700 + (int32_t)nCBORError; |
| 7909 | } |
| 7910 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7911 | Item.uDataAlloc || |
| 7912 | Item.uLabelAlloc || |
| 7913 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7914 | return 1800; |
| 7915 | } |
| 7916 | |
| 7917 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7918 | return (int32_t)nCBORError; |
| 7919 | } |
| 7920 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7921 | Item.uDataAlloc || |
| 7922 | Item.uLabelAlloc || |
| 7923 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7924 | return 1900; |
| 7925 | } |
| 7926 | |
| 7927 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7928 | return (int32_t)nCBORError; |
| 7929 | } |
| 7930 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7931 | Item.uDataAlloc || |
| 7932 | Item.uLabelAlloc || |
| 7933 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7934 | return 2000; |
| 7935 | } |
| 7936 | |
| 7937 | |
| 7938 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7939 | return 2100 + (int32_t)nCBORError; |
| 7940 | } |
| 7941 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7942 | Item.uDataAlloc || |
| 7943 | Item.uLabelAlloc || |
| 7944 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 7945 | Item.uDataType != QCBOR_TYPE_MAP || |
| 7946 | Item.val.uCount != 4) { |
| 7947 | return 2100; |
| 7948 | } |
| 7949 | |
| 7950 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7951 | return 2200 + (int32_t)nCBORError; |
| 7952 | } |
| 7953 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7954 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 7955 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7956 | Item.uDataAlloc || |
| 7957 | Item.uLabelAlloc || |
| 7958 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 7959 | return 2300; |
| 7960 | } |
| 7961 | |
| 7962 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7963 | return 2400 + (int32_t)nCBORError; |
| 7964 | } |
| 7965 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7966 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7967 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7968 | Item.uDataAlloc || |
| 7969 | Item.uLabelAlloc || |
| 7970 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7971 | return 2500; |
| 7972 | } |
| 7973 | |
| 7974 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7975 | return 2600 + (int32_t)nCBORError; |
| 7976 | } |
| 7977 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7978 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7979 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7980 | Item.uDataAlloc || |
| 7981 | Item.uLabelAlloc || |
| 7982 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7983 | return 2700; |
| 7984 | } |
| 7985 | |
| 7986 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7987 | return 2800 + (int32_t)nCBORError; |
| 7988 | } |
| 7989 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7990 | Item.uDataAlloc || |
| 7991 | Item.uLabelAlloc || |
| 7992 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 7993 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7994 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7995 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7996 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7997 | |
| 7998 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7999 | return 3000 + (int32_t)nCBORError; |
| 8000 | } |
| 8001 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8002 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8003 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8004 | Item.uDataAlloc || |
| 8005 | Item.uLabelAlloc || |
| 8006 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8007 | return 3100; |
| 8008 | } |
| 8009 | |
| 8010 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8011 | return 3200 + (int32_t)nCBORError; |
| 8012 | } |
| 8013 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8014 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 8015 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8016 | Item.uDataAlloc || |
| 8017 | Item.uLabelAlloc || |
| 8018 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 8019 | return 3300; |
| 8020 | } |
| 8021 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 8022 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 8023 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8024 | return 3300 + (int32_t)nCBORError; |
| 8025 | } |
| 8026 | |
| 8027 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8028 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8029 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8030 | return 3400 + (int32_t)nCBORError; |
| 8031 | } |
| 8032 | |
| 8033 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 8034 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 8035 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8036 | return 3500 + (int32_t)nCBORError; |
| 8037 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8038 | |
| 8039 | |
| 8040 | // Rewind to top level after entering several maps |
| 8041 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8042 | |
| 8043 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8044 | return (int32_t)nCBORError; |
| 8045 | } |
| 8046 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8047 | Item.val.uCount != 3) { |
| 8048 | return 400; |
| 8049 | } |
| 8050 | |
| 8051 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8052 | return 4000+(int32_t)nCBORError; |
| 8053 | } |
| 8054 | |
| 8055 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8056 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8057 | Item.val.int64 != 42 || |
| 8058 | Item.uDataAlloc || |
| 8059 | Item.uLabelAlloc || |
| 8060 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8061 | return 4100; |
| 8062 | } |
| 8063 | |
| 8064 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8065 | return 4100+(int32_t)nCBORError; |
| 8066 | } |
| 8067 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8068 | Item.uDataAlloc || |
| 8069 | Item.uLabelAlloc || |
| 8070 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8071 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8072 | Item.val.uCount != 2) { |
| 8073 | return 4200; |
| 8074 | } |
| 8075 | |
| 8076 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8077 | return 4200+(int32_t)nCBORError; |
| 8078 | } |
| 8079 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8080 | Item.uDataAlloc || |
| 8081 | Item.uLabelAlloc || |
| 8082 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8083 | return 4300; |
| 8084 | } |
| 8085 | |
| 8086 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8087 | return 4300+(int32_t)nCBORError; |
| 8088 | } |
| 8089 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8090 | Item.uDataAlloc || |
| 8091 | Item.uLabelAlloc || |
| 8092 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8093 | return 4400; |
| 8094 | } |
| 8095 | |
| 8096 | QCBORDecode_Rewind(&DCtx); |
| 8097 | |
| 8098 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8099 | return 4400+(int32_t)nCBORError; |
| 8100 | } |
| 8101 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 8102 | Item.val.uCount != 3) { |
| 8103 | return 4500; |
| 8104 | } |
| 8105 | |
| 8106 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8107 | return (int32_t)nCBORError; |
| 8108 | } |
| 8109 | |
| 8110 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8111 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8112 | Item.val.int64 != 42 || |
| 8113 | Item.uDataAlloc || |
| 8114 | Item.uLabelAlloc || |
| 8115 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8116 | return 4600; |
| 8117 | } |
| 8118 | |
| 8119 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8120 | return (int32_t)nCBORError; |
| 8121 | } |
| 8122 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8123 | Item.uDataAlloc || |
| 8124 | Item.uLabelAlloc || |
| 8125 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8126 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8127 | Item.val.uCount != 2) { |
| 8128 | return 4700; |
| 8129 | } |
| 8130 | |
| 8131 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8132 | return (int32_t)nCBORError; |
| 8133 | } |
| 8134 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8135 | Item.uDataAlloc || |
| 8136 | Item.uLabelAlloc || |
| 8137 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8138 | return 4800; |
| 8139 | } |
| 8140 | |
| 8141 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8142 | return 4900+(int32_t)nCBORError; |
| 8143 | } |
| 8144 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8145 | Item.uDataAlloc || |
| 8146 | Item.uLabelAlloc || |
| 8147 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8148 | return 5000; |
| 8149 | } |
| 8150 | |
| 8151 | |
| 8152 | // Rewind an entered map |
| 8153 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8154 | |
| 8155 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8156 | |
| 8157 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8158 | return 5100+(int32_t)nCBORError; |
| 8159 | } |
| 8160 | |
| 8161 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8162 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8163 | Item.val.int64 != 42 || |
| 8164 | Item.uDataAlloc || |
| 8165 | Item.uLabelAlloc || |
| 8166 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8167 | return 5200; |
| 8168 | } |
| 8169 | |
| 8170 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8171 | return 5200+(int32_t)nCBORError; |
| 8172 | } |
| 8173 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8174 | Item.uDataAlloc || |
| 8175 | Item.uLabelAlloc || |
| 8176 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8177 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8178 | Item.val.uCount != 2) { |
| 8179 | return -5300; |
| 8180 | } |
| 8181 | |
| 8182 | QCBORDecode_Rewind(&DCtx); |
| 8183 | |
| 8184 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8185 | return 5300+(int32_t)nCBORError; |
| 8186 | } |
| 8187 | |
| 8188 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8189 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 8190 | Item.val.int64 != 42 || |
| 8191 | Item.uDataAlloc || |
| 8192 | Item.uLabelAlloc || |
| 8193 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 8194 | return 5400; |
| 8195 | } |
| 8196 | |
| 8197 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8198 | return 5400+(int32_t)nCBORError; |
| 8199 | } |
| 8200 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 8201 | Item.uDataAlloc || |
| 8202 | Item.uLabelAlloc || |
| 8203 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 8204 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 8205 | Item.val.uCount != 2) { |
| 8206 | return 5500; |
| 8207 | } |
| 8208 | |
| 8209 | |
| 8210 | // Rewind and entered array inside an entered map |
| 8211 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 8212 | |
| 8213 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8214 | |
| 8215 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 8216 | |
| 8217 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8218 | return 5600+(int32_t)nCBORError; |
| 8219 | } |
| 8220 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8221 | Item.uDataAlloc || |
| 8222 | Item.uLabelAlloc || |
| 8223 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8224 | return 5700; |
| 8225 | } |
| 8226 | |
| 8227 | QCBORDecode_Rewind(&DCtx); |
| 8228 | |
| 8229 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8230 | return 5700+(int32_t)nCBORError; |
| 8231 | } |
| 8232 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8233 | Item.uDataAlloc || |
| 8234 | Item.uLabelAlloc || |
| 8235 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8236 | return 5800; |
| 8237 | } |
| 8238 | |
| 8239 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8240 | return (int32_t)nCBORError; |
| 8241 | } |
| 8242 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8243 | Item.uDataAlloc || |
| 8244 | Item.uLabelAlloc || |
| 8245 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 8246 | return 5900; |
| 8247 | } |
| 8248 | |
| 8249 | QCBORDecode_Rewind(&DCtx); |
| 8250 | |
| 8251 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8252 | return 5900+(int32_t)nCBORError; |
| 8253 | } |
| 8254 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 8255 | Item.uDataAlloc || |
| 8256 | Item.uLabelAlloc || |
| 8257 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 8258 | return 6000; |
| 8259 | } |
| 8260 | |
| 8261 | |
| 8262 | // Rewind a byte string inside an array inside an array |
| 8263 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 8264 | |
| 8265 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8266 | |
| 8267 | uint64_t i; |
| 8268 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8269 | |
| 8270 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8271 | |
| 8272 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8273 | if(QCBORDecode_GetError(&DCtx)) { |
| 8274 | return 6100; |
| 8275 | } |
| 8276 | |
| 8277 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8278 | return (int32_t)nCBORError; |
| 8279 | } |
| 8280 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8281 | return 6200; |
| 8282 | } |
| 8283 | |
| 8284 | QCBORDecode_Rewind(&DCtx); |
| 8285 | |
| 8286 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8287 | return 6300+(int32_t)nCBORError; |
| 8288 | } |
| 8289 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8290 | return 6400; |
| 8291 | } |
| 8292 | |
| 8293 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8294 | // Rewind a byte string inside an indefinite-length array inside |
| 8295 | // indefinite-length array |
| 8296 | |
| 8297 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 8298 | |
| 8299 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8300 | |
| 8301 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8302 | |
| 8303 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8304 | |
| 8305 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 8306 | if(QCBORDecode_GetError(&DCtx)) { |
| 8307 | return 6500; |
| 8308 | } |
| 8309 | |
| 8310 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8311 | return 6600+(int32_t)nCBORError; |
| 8312 | } |
| 8313 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8314 | return 6700; |
| 8315 | } |
| 8316 | |
| 8317 | QCBORDecode_Rewind(&DCtx); |
| 8318 | |
| 8319 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 8320 | return 6800+(int32_t)nCBORError; |
| 8321 | } |
| 8322 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 8323 | return 6900; |
| 8324 | } |
| 8325 | #endif |
| 8326 | |
| 8327 | // Rewind an empty map |
| 8328 | // [100, {}] |
| 8329 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 8330 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8331 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8332 | if(i != 100) { |
| 8333 | return 7010; |
| 8334 | } |
| 8335 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8336 | |
| 8337 | /* Do it 5 times to be sure multiple rewinds work */ |
| 8338 | for(int n = 0; n < 5; n++) { |
| 8339 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 8340 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8341 | return 7000 + n; |
| 8342 | } |
| 8343 | QCBORDecode_Rewind(&DCtx); |
| 8344 | } |
| 8345 | QCBORDecode_ExitMap(&DCtx); |
| 8346 | QCBORDecode_Rewind(&DCtx); |
| 8347 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8348 | if(i != 100) { |
| 8349 | return 7010; |
| 8350 | } |
| 8351 | QCBORDecode_ExitArray(&DCtx); |
| 8352 | QCBORDecode_Rewind(&DCtx); |
| 8353 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8354 | i = 9; |
| 8355 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8356 | if(i != 100) { |
| 8357 | return 7020; |
| 8358 | } |
| 8359 | if(QCBORDecode_GetError(&DCtx)){ |
| 8360 | return 7030; |
| 8361 | } |
| 8362 | |
| 8363 | // Rewind an empty indefinite length map |
| 8364 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 8365 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 8366 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8367 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8368 | if(i != 100) { |
| 8369 | return 7810; |
| 8370 | } |
| 8371 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8372 | |
| 8373 | /* Do it 5 times to be sure multiple rewinds work */ |
| 8374 | for(int n = 0; n < 5; n++) { |
| 8375 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 8376 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 8377 | return 7800 + n; |
| 8378 | } |
| 8379 | QCBORDecode_Rewind(&DCtx); |
| 8380 | } |
| 8381 | QCBORDecode_ExitMap(&DCtx); |
| 8382 | QCBORDecode_Rewind(&DCtx); |
| 8383 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8384 | if(i != 100) { |
| 8385 | return 7810; |
| 8386 | } |
| 8387 | QCBORDecode_ExitArray(&DCtx); |
| 8388 | QCBORDecode_Rewind(&DCtx); |
| 8389 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8390 | i = 9; |
| 8391 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8392 | if(i != 100) { |
| 8393 | return 7820; |
| 8394 | } |
| 8395 | if(QCBORDecode_GetError(&DCtx)){ |
| 8396 | return 7830; |
| 8397 | } |
| 8398 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 8399 | |
| 8400 | // Rewind an indefnite length byte-string wrapped sequence |
| 8401 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8402 | // TODO: rewrite this test to not use tags |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8403 | QCBORDecode_Init(&DCtx, |
| 8404 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 8405 | 0); |
| 8406 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 8407 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 8408 | |
| 8409 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 8410 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8411 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8412 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8413 | /* TODO: This is what happens when trying to enter |
| 8414 | * indefinite-length byte string wrapped CBOR. Tolerate for |
| 8415 | * now. Eventually it needs to be fixed so this works, but that |
| 8416 | * is not simple. |
| 8417 | */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8418 | return 7300; |
| 8419 | } |
| 8420 | |
| 8421 | /* |
| 8422 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8423 | if(i != 42) { |
| 8424 | return 7110; |
| 8425 | } |
| 8426 | QCBORDecode_Rewind(&DCtx); |
| 8427 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 8428 | if(i != 42) { |
| 8429 | return 7220; |
| 8430 | }*/ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 8431 | |
| 8432 | #else /* QCBOR_DISABLE_TAGS */ |
| 8433 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) { |
| 8434 | return 7301; |
| 8435 | } |
| 8436 | #endif /* QCBOR_DISABLE_TAGS */ |
| 8437 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 8438 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 8439 | |
| 8440 | |
| 8441 | // Rewind an indefnite length byte-string wrapped sequence |
| 8442 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 8443 | return 0; |
| 8444 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8445 | |
| 8446 | |
| 8447 | |
| 8448 | |
| 8449 | static const uint8_t spBooleansInMap[] = |
| 8450 | { |
| 8451 | 0xa1, 0x08, 0xf5 |
| 8452 | }; |
| 8453 | |
| 8454 | static const uint8_t spBooleansInMapWrongType[] = |
| 8455 | { |
| 8456 | 0xa1, 0x08, 0xf6 |
| 8457 | }; |
| 8458 | |
| 8459 | static const uint8_t spBooleansInMapNWF[] = |
| 8460 | { |
| 8461 | 0xa1, 0x08, 0x1a |
| 8462 | }; |
| 8463 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8464 | static const uint8_t spNullInMap[] = |
| 8465 | { |
| 8466 | 0xa1, 0x08, 0xf6 |
| 8467 | }; |
| 8468 | |
| 8469 | static const uint8_t spUndefinedInMap[] = |
| 8470 | { |
| 8471 | 0xa1, 0x08, 0xf7 |
| 8472 | }; |
| 8473 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8474 | |
| 8475 | int32_t BoolTest(void) |
| 8476 | { |
| 8477 | QCBORDecodeContext DCtx; |
| 8478 | bool b; |
| 8479 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8480 | QCBORDecode_Init(&DCtx, |
| 8481 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8482 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8483 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8484 | QCBORDecode_GetBool(&DCtx, &b); |
| 8485 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 8486 | return 1; |
| 8487 | } |
| 8488 | |
| 8489 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 8490 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 8491 | return 2; |
| 8492 | } |
| 8493 | |
| 8494 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 8495 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 8496 | return 3; |
| 8497 | } |
| 8498 | |
| 8499 | |
| 8500 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 8501 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 8502 | return 4; |
| 8503 | } |
| 8504 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8505 | QCBORDecode_Init(&DCtx, |
| 8506 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 8507 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8508 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8509 | QCBORDecode_GetBool(&DCtx, &b); |
| 8510 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8511 | return 5; |
| 8512 | } |
| 8513 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8514 | QCBORDecode_Init(&DCtx, |
| 8515 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8516 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8517 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8518 | QCBORDecode_GetBool(&DCtx, &b); |
| 8519 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8520 | return 6; |
| 8521 | } |
| 8522 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 8523 | |
| 8524 | QCBORDecode_Init(&DCtx, |
| 8525 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 8526 | 0); |
| 8527 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8528 | QCBORDecode_GetNull(&DCtx); |
| 8529 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8530 | return 7; |
| 8531 | } |
| 8532 | |
| 8533 | QCBORDecode_Init(&DCtx, |
| 8534 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8535 | 0); |
| 8536 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8537 | QCBORDecode_GetNull(&DCtx); |
| 8538 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8539 | return 8; |
| 8540 | } |
| 8541 | |
| 8542 | QCBORDecode_Init(&DCtx, |
| 8543 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 8544 | 0); |
| 8545 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8546 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 8547 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8548 | return 9; |
| 8549 | } |
| 8550 | |
| 8551 | QCBORDecode_Init(&DCtx, |
| 8552 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8553 | 0); |
| 8554 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8555 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 8556 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8557 | return 10; |
| 8558 | } |
| 8559 | |
| 8560 | QCBORDecode_Init(&DCtx, |
| 8561 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8562 | 0); |
| 8563 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8564 | QCBORDecode_GetUndefined(&DCtx); |
| 8565 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8566 | return 11; |
| 8567 | } |
| 8568 | |
| 8569 | QCBORDecode_Init(&DCtx, |
| 8570 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 8571 | 0); |
| 8572 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8573 | QCBORDecode_GetUndefined(&DCtx); |
| 8574 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8575 | return 12; |
| 8576 | } |
| 8577 | |
| 8578 | QCBORDecode_Init(&DCtx, |
| 8579 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8580 | 0); |
| 8581 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8582 | QCBORDecode_GetUndefined(&DCtx); |
| 8583 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8584 | return 13; |
| 8585 | } |
| 8586 | |
| 8587 | QCBORDecode_Init(&DCtx, |
| 8588 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 8589 | 0); |
| 8590 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8591 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 8592 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 8593 | return 14; |
| 8594 | } |
| 8595 | |
| 8596 | QCBORDecode_Init(&DCtx, |
| 8597 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 8598 | 0); |
| 8599 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8600 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 8601 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 8602 | return 15; |
| 8603 | } |
| 8604 | |
| 8605 | QCBORDecode_Init(&DCtx, |
| 8606 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 8607 | 0); |
| 8608 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 8609 | QCBORDecode_GetUndefined(&DCtx); |
| 8610 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 8611 | return 15; |
| 8612 | } |
| 8613 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 8614 | return 0; |
| 8615 | } |