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 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 487 | int32_t IntegerValuesParseTest() |
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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 694 | int32_t SimpleArrayTest() |
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 | |
| 901 | int32_t EmptyMapsAndArraysTest() |
| 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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 969 | int32_t ParseDeepArrayTest() |
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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1002 | int32_t ParseTooDeepArrayTest() |
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 | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 1014 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; 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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1033 | int32_t ShortBufferParseTest() |
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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1057 | int32_t ShortBufferParseTest2() |
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 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1217 | int32_t ParseMapAsArrayTest() |
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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1640 | int32_t ParseMapTest() |
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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1675 | int32_t ParseSimpleTest() |
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 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1751 | int32_t NotWellFormedTests() |
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 | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1761 | // Set up decoder context. String allocator needed for indefinite |
| 1762 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1763 | QCBORDecodeContext DCtx; |
| 1764 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1765 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1766 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1767 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1768 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1769 | |
| 1770 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1771 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1772 | do { |
| 1773 | QCBORItem Item; |
| 1774 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1775 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1776 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1777 | |
| 1778 | // Every test vector must fail with |
| 1779 | // a not-well-formed error. If not |
| 1780 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1781 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1782 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1783 | /* Return index of failure and QCBOR error in the result */ |
| 1784 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1791 | // TODO: add a test index and report it so it is eaier to figure out which test failed. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1792 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1793 | UsefulBufC Input; |
| 1794 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1795 | }; |
| 1796 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1797 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1798 | static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1799 | { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1800 | for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1801 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1802 | QCBORError uCBORError; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1803 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1804 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1805 | |
| 1806 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1807 | // Set up the decoding context including a memory pool so that |
| 1808 | // indefinite length items can be checked |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1809 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1810 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1811 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1812 | if(uCBORError) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1813 | return -9; |
| 1814 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1815 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1816 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 1817 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1818 | // Iterate until there is an error of some sort error |
| 1819 | QCBORItem Item; |
| 1820 | do { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1821 | // Set to something none-zero, something other than QCBOR_TYPE_NONE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1822 | memset(&Item, 0x33, sizeof(Item)); |
| 1823 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1824 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1825 | } while(uCBORError == QCBOR_SUCCESS); |
| 1826 | |
| 1827 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1828 | |
| 1829 | // Must get the expected error or the this test fails |
| 1830 | // The data and label type must also be QCBOR_TYPE_NONE |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1831 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1832 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1833 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1834 | // return index of CBOR + 100 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1835 | const size_t nIndex = (size_t)(pF - pFailInputs); |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1836 | return (int32_t)(nIndex * 100 + uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
| 1843 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1844 | static const struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1845 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1846 | // returned is also checked. |
| 1847 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1848 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1849 | // Indefinite length strings must be closed off |
| 1850 | // An indefinite length byte string not closed off |
| 1851 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1852 | // An indefinite length text string not closed off |
| 1853 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1854 | |
| 1855 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1856 | // All the chunks in an indefinite length string must be of the type of |
| 1857 | // indefinite length string |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1858 | // indefinite length byte string with text string chunk |
| 1859 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1860 | // indefinite length text string with a byte string chunk |
| 1861 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1862 | // indefinite length byte string with an positive integer chunk |
| 1863 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1864 | // indefinite length byte string with an negative integer chunk |
| 1865 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1866 | // indefinite length byte string with an array chunk |
| 1867 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1868 | // indefinite length byte string with an map chunk |
| 1869 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1870 | // indefinite length byte string with tagged integer chunk |
| 1871 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1872 | // indefinite length byte string with an simple type chunk |
| 1873 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1874 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1875 | // indefinite length text string with indefinite string inside |
| 1876 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1877 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1878 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1879 | |
| 1880 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1881 | // An indefinite length text string not closed off |
| 1882 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1883 | |
| 1884 | |
| 1885 | // All the chunks in an indefinite length string must be of the type of |
| 1886 | // indefinite length string |
| 1887 | // indefinite length byte string with text string chunk |
| 1888 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1889 | // indefinite length text string with a byte string chunk |
| 1890 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1891 | // indefinite length byte string with an positive integer chunk |
| 1892 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1893 | // indefinite length byte string with an negative integer chunk |
| 1894 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1895 | // indefinite length byte string with an array chunk |
| 1896 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1897 | // indefinite length byte string with an map chunk |
| 1898 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1899 | // indefinite length byte string with tagged integer chunk |
| 1900 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1901 | // indefinite length byte string with an simple type chunk |
| 1902 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1903 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1904 | // indefinite length text string with indefinite string inside |
| 1905 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1906 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1907 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1908 | |
| 1909 | // Definte length maps and arrays must be closed by having the right number of items |
| 1910 | // 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] | 1911 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1912 | // 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] | 1913 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1914 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1915 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1916 | // 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] | 1917 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1918 | // 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] | 1919 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1920 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1921 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1922 | // Indefinte length maps and arrays must be ended by a break |
| 1923 | // Indefinite length array with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1924 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1925 | // Indefinite length array with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1926 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1927 | // Indefinite length map with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1928 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1929 | // Indefinite length map with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1930 | { {(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] | 1931 | |
| 1932 | |
| 1933 | // 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] | 1934 | // Unclosed indefinite array containing a closed definite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1935 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1936 | // Definite length array containing an unclosed indefinite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1937 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1938 | // Unclosed indefinite map containing a closed definite length array |
| 1939 | { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
| 1940 | // Definite length map containing an unclosed indefinite length array |
| 1941 | { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1942 | // Deeply nested definite length arrays with deepest one unclosed |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1943 | { {(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] | 1944 | // Deeply nested indefinite length arrays with deepest one unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1945 | { {(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] | 1946 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1947 | { {(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] | 1948 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1949 | { {(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] | 1950 | // Unclosed indefinite length map in definite length maps |
| 1951 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8}, |
| 1952 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1953 | // Unclosed definite length map in indefinite length maps |
| 1954 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1955 | // Unclosed indefinite length array in definite length maps |
| 1956 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8}, |
| 1957 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1958 | // Unclosed definite length array in indefinite length maps |
| 1959 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1960 | // Unclosed indefinite length map in definite length arrays |
| 1961 | { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1962 | // Unclosed definite length map in indefinite length arrays |
| 1963 | { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1964 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1965 | |
| 1966 | // The "argument" for the data item is incomplete |
| 1967 | // Positive integer missing 1 byte argument |
| 1968 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1969 | // Positive integer missing 2 byte argument |
| 1970 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1971 | // Positive integer missing 4 byte argument |
| 1972 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1973 | // Positive integer missing 8 byte argument |
| 1974 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1975 | // Positive integer missing 1 byte of 2 byte argument |
| 1976 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1977 | // Positive integer missing 2 bytes of 4 byte argument |
| 1978 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1979 | // Positive integer missing 1 bytes of 7 byte argument |
| 1980 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1981 | // Negative integer missing 1 byte argument |
| 1982 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1983 | // Binary string missing 1 byte argument |
| 1984 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1985 | // Text string missing 1 byte argument |
| 1986 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 1987 | // Array missing 1 byte argument |
| 1988 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 1989 | // Map missing 1 byte argument |
| 1990 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 1991 | // Tag missing 1 byte argument |
| 1992 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 1993 | // Simple missing 1 byte argument |
| 1994 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1995 | // half-precision with 1 byte argument |
| 1996 | { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1997 | // single-precision with 2 byte argument |
| 1998 | { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1999 | // double-precision with 3 byte argument |
| 2000 | { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 2001 | |
| 2002 | |
| 2003 | // Tag with no content |
| 2004 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2005 | |
| 2006 | |
| 2007 | // Breaks must not occur in definite length arrays and maps |
| 2008 | // Array of length 1 with sole member replaced by a break |
| 2009 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2010 | // Array of length 2 with 2nd member replaced by a break |
| 2011 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2012 | // Map of length 1 with sole member label replaced by a break |
| 2013 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2014 | // Map of length 1 with sole member label replaced by break |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2015 | // Alternate representation that some decoders handle differently |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2016 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2017 | // Array of length 1 with 2nd member value replaced by a break |
| 2018 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2019 | // Map of length 2 with 2nd member replaced by a break |
| 2020 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 2021 | |
| 2022 | |
| 2023 | // Breaks must not occur on their own out of an indefinite length data item |
| 2024 | // A bare break is not well formed |
| 2025 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 2026 | // A bare break after a zero length definite length array |
| 2027 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2028 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2029 | // A bare break after a zero length indefinite length map |
| 2030 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2031 | // A break inside a definite length array inside an indefenite length array |
| 2032 | { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2033 | // Complicated mixed nesting with break outside indefinite length array |
| 2034 | { {(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] | 2035 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2036 | |
| 2037 | |
| 2038 | // Forbidden two byte encodings of simple types |
| 2039 | // Must use 0xe0 instead |
| 2040 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2041 | // Should use 0xe1 instead |
| 2042 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2043 | // Should use 0xe2 instead |
| 2044 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2045 | // Should use 0xe3 instead |
| 2046 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2047 | // Should use 0xe4 instead |
| 2048 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2049 | // Should use 0xe5 instead |
| 2050 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2051 | // Should use 0xe6 instead |
| 2052 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2053 | // Should use 0xe7 instead |
| 2054 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2055 | // Should use 0xe8 instead |
| 2056 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2057 | // Should use 0xe9 instead |
| 2058 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2059 | // Should use 0xea instead |
| 2060 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2061 | // Should use 0xeb instead |
| 2062 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2063 | // Should use 0xec instead |
| 2064 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2065 | // Should use 0xed instead |
| 2066 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2067 | // Should use 0xee instead |
| 2068 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2069 | // Should use 0xef instead |
| 2070 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2071 | // Should use 0xf0 instead |
| 2072 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2073 | // Should use 0xf1 instead |
| 2074 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2075 | // Should use 0xf2 instead |
| 2076 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2077 | // Must use 0xf3 instead |
| 2078 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2079 | // Must use 0xf4 instead |
| 2080 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2081 | // Must use 0xf5 instead |
| 2082 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2083 | // Must use 0xf6 instead |
| 2084 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2085 | // Must use 0xf7 instead |
| 2086 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2087 | // Must use 0xf8 instead |
| 2088 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2089 | // Reserved |
| 2090 | { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2091 | |
| 2092 | // Integers with additional info indefinite length |
| 2093 | // Positive integer with additional info indefinite length |
| 2094 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2095 | // Negative integer with additional info indefinite length |
| 2096 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2097 | // CBOR tag with "argument" an indefinite length |
| 2098 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 2099 | // CBOR tag with "argument" an indefinite length alternate vector |
| 2100 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
| 2101 | |
| 2102 | |
| 2103 | // Missing bytes from a deterministic length string |
| 2104 | // A byte string is of length 1 without the 1 byte |
| 2105 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 2106 | // A text string is of length 1 without the 1 byte |
| 2107 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2108 | |
| 2109 | #if SIZE_MAX > 2147483647 |
Laurence Lundblade | 42272e4 | 2020-01-31 07:50:53 -0800 | [diff] [blame] | 2110 | // Byte string should have 2^32-15 bytes, but has one |
| 2111 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2112 | // Byte string should have 2^32-15 bytes, but has one |
| 2113 | { {(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] | 2114 | // Byte string should have 2^64 bytes, but has 3 |
| 2115 | { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2116 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2117 | // Text string should have 2^64 bytes, but has 3 |
| 2118 | { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2119 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2120 | #else |
| 2121 | // Byte string should have 2^32-15 bytes, but has one |
| 2122 | { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2123 | // Byte string should have 2^32-15 bytes, but has one |
| 2124 | { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2125 | // Byte string should have 2^16 bytes, but has 3 |
| 2126 | { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2127 | // Text string should have 2^64 bytes, but has 3 |
| 2128 | { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2129 | #endif |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2130 | |
| 2131 | // Use of unassigned additional information values |
| 2132 | // Major type positive integer with reserved value 28 |
| 2133 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2134 | // Major type positive integer with reserved value 29 |
| 2135 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2136 | // Major type positive integer with reserved value 30 |
| 2137 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2138 | // Major type negative integer with reserved value 28 |
| 2139 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2140 | // Major type negative integer with reserved value 29 |
| 2141 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2142 | // Major type negative integer with reserved value 30 |
| 2143 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2144 | // Major type byte string with reserved value 28 length |
| 2145 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2146 | // Major type byte string with reserved value 29 length |
| 2147 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2148 | // Major type byte string with reserved value 30 length |
| 2149 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2150 | // Major type text string with reserved value 28 length |
| 2151 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2152 | // Major type text string with reserved value 29 length |
| 2153 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2154 | // Major type text string with reserved value 30 length |
| 2155 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2156 | // Major type array with reserved value 28 length |
| 2157 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2158 | // Major type array with reserved value 29 length |
| 2159 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2160 | // Major type array with reserved value 30 length |
| 2161 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2162 | // Major type map with reserved value 28 length |
| 2163 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2164 | // Major type map with reserved value 29 length |
| 2165 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2166 | // Major type map with reserved value 30 length |
| 2167 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2168 | // Major type tag with reserved value 28 length |
| 2169 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2170 | // Major type tag with reserved value 29 length |
| 2171 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2172 | // Major type tag with reserved value 30 length |
| 2173 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2174 | // Major type simple with reserved value 28 length |
| 2175 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2176 | // Major type simple with reserved value 29 length |
| 2177 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2178 | // Major type simple with reserved value 30 length |
| 2179 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2180 | |
| 2181 | |
| 2182 | // Maps must have an even number of data items (key & value) |
| 2183 | // Map with 1 item when it should have 2 |
| 2184 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2185 | // Map with 3 item when it should have 4 |
| 2186 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2187 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2188 | // Map with 1 item when it should have 2 |
| 2189 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2190 | // Map with 3 item when it should have 4 |
| 2191 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2192 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2193 | |
| 2194 | |
| 2195 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2196 | // Text-based date, with an integer |
| 2197 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2198 | // Epoch date, with an byte string |
| 2199 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2200 | // tagged as both epoch and string dates |
| 2201 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2202 | // big num tagged an int, not a byte string |
| 2203 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2204 | }; |
| 2205 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2206 | int32_t DecodeFailureTests() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2207 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2208 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2209 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2210 | nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2211 | if(nResult) { |
| 2212 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2213 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2214 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2215 | // Corrupt the UsefulInputBuf and see that |
| 2216 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2217 | QCBORDecodeContext DCtx; |
| 2218 | QCBORItem Item; |
| 2219 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2220 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2221 | QCBORDecode_Init(&DCtx, |
| 2222 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2223 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2224 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2225 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2226 | return (int32_t)uQCBORError; |
| 2227 | } |
| 2228 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 2229 | // This wasn't supposed to happen |
| 2230 | return -1; |
| 2231 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2232 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2233 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2234 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2235 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2236 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2237 | // Did not get back the error expected |
| 2238 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2239 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2240 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2241 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2242 | /* |
| 2243 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 2244 | tests here can be performed to see that the max length |
| 2245 | error check works correctly. See DecodeBytes(). If the max |
| 2246 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2247 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2248 | This test will automatocally adapt the all CPU sizes |
| 2249 | through the use of SIZE_MAX. |
| 2250 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2251 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2252 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2253 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2254 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2255 | // This makes a CBOR head with a text string that is very long |
| 2256 | // but doesn't fill in the bytes of the text string as that is |
| 2257 | // not needed to test this part of QCBOR. |
| 2258 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 2259 | |
| 2260 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 2261 | |
| 2262 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2263 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2264 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2265 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2266 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2270 | /* Try all 256 values of the byte at nLen including recursing for |
| 2271 | each of the values to try values at nLen+1 ... up to nLenMax |
| 2272 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2273 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2274 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2275 | if(nLen >= nLenMax) { |
| 2276 | return; |
| 2277 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2278 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2279 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 2280 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2281 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2282 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2283 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2284 | // Get ready to parse |
| 2285 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2286 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2287 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2288 | // Parse by getting the next item until an error occurs |
| 2289 | // Just about every possible decoder error can occur here |
| 2290 | // The goal of this test is not to check for the correct |
| 2291 | // error since that is not really possible. It is to |
| 2292 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2293 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2294 | QCBORItem Item; |
| 2295 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2296 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2297 | break; |
| 2298 | } |
| 2299 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2300 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2301 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2306 | int32_t ComprehensiveInputTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2307 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2308 | // Size 2 tests 64K inputs and runs quickly |
| 2309 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2310 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2311 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2312 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
| 2315 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2316 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2317 | int32_t BigComprehensiveInputTest() |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2318 | { |
| 2319 | // size 3 tests 16 million inputs and runs OK |
| 2320 | // in seconds on fast machines. Size 4 takes |
| 2321 | // 10+ minutes and 5 half a day on fast |
| 2322 | // machines. This test is kept separate from |
| 2323 | // the others so as to no slow down the use |
| 2324 | // of them as a very frequent regression. |
| 2325 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2326 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2327 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2328 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2329 | return 0; |
| 2330 | } |
| 2331 | |
| 2332 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2333 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2334 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2335 | 0xc0, // tag for string date |
| 2336 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2337 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2338 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2339 | 0xc0, // tag for string date |
| 2340 | 0x00, // Wrong type for a string date |
| 2341 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2342 | /* 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] | 2343 | 0xc1, // tag for epoch date |
| 2344 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2345 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2346 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2347 | 0xc1, |
| 2348 | 0x62, 'h', 'i', // wrong type tagged |
| 2349 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2350 | /* 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] | 2351 | // CBOR_TAG_ENC_AS_B64 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2352 | 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2353 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2354 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2355 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2356 | 0xc1, // tag for epoch date |
| 2357 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2358 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2359 | /* 7. Epoch date with single-precision value of 1.1. */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2360 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2361 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2362 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2363 | /* 8. Epoch date with too-large single precision float */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2364 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2365 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2366 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2367 | /* 9. Epoch date with slightly too-large double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2368 | 0xc1, // tag for epoch date |
| 2369 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2370 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2371 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2372 | /* 10. Epoch date with largest supported double precision value */ |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2373 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2374 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2375 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2376 | /* 11. Epoch date with single-precision NaN */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2377 | 0xc1, // tag for epoch date |
| 2378 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 2379 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2380 | /* 12. Epoch date with double precision plus infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2381 | 0xc1, |
| 2382 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 2383 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2384 | /* 13. Epoch date with half-precision negative infinity */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2385 | 0xc1, // tag for epoch date |
| 2386 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2387 | }; |
| 2388 | |
| 2389 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2390 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2391 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2392 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 2393 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2394 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2395 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2396 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2397 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2398 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2399 | return diff > 0.0000001; |
| 2400 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2401 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2402 | |
| 2403 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2404 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2405 | int32_t DateParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2406 | { |
| 2407 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2408 | QCBORItem Item; |
| 2409 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2410 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2411 | QCBORDecode_Init(&DCtx, |
| 2412 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2413 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2414 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2415 | /* 1. The valid date string "1985-04-12" */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2416 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2417 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2418 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2419 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2420 | UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2421 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2422 | } |
| 2423 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2424 | /* 2. An invalid date string due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2425 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2426 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2427 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2430 | /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */ |
| 2431 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2432 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2433 | return -4; |
| 2434 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2435 | if(uError == QCBOR_SUCCESS) { |
| 2436 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2437 | Item.val.epochDate.nSeconds != 1400000000 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2438 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2439 | || Item.val.epochDate.fSecondsFraction != 0 |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2440 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2441 | ) { |
| 2442 | return -5; |
| 2443 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2446 | /* 4. An invalid epoch date due to wrong tag content type */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2447 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 2448 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2449 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2450 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2451 | /* 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] | 2452 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 2453 | // The date is decoded and the two tags are returned. This is to |
| 2454 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2455 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2456 | return -7; |
| 2457 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2458 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2459 | Item.val.epochDate.nSeconds != 1400000001 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2460 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2461 | Item.val.epochDate.fSecondsFraction != 0 || |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2462 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2463 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2464 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2465 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2466 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2467 | /* 6. Epoch date with value to large to fit into int64 */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2468 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2469 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2470 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2471 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2472 | /* 7. Epoch date with single-precision value of 1.1. */ |
| 2473 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2474 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2475 | return -10; |
| 2476 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2477 | if(uError == QCBOR_SUCCESS) { |
| 2478 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2479 | Item.val.epochDate.nSeconds != 1 |
| 2480 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2481 | || 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] | 2482 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2483 | ) { |
| 2484 | return -11; |
| 2485 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2486 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2487 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2488 | /* 8. Epoch date with too-large single-precision float */ |
| 2489 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2490 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2491 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2492 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2493 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2494 | /* 9. Epoch date with slightly too-large double-precision value */ |
| 2495 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2496 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2497 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2498 | } |
| 2499 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2500 | /* 10. Epoch date with largest supported double-precision value */ |
| 2501 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2502 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
| 2503 | return -14; |
| 2504 | } |
| 2505 | if(uError == QCBOR_SUCCESS) { |
| 2506 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2507 | Item.val.epochDate.nSeconds != 9223372036854773760 |
| 2508 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2509 | || Item.val.epochDate.fSecondsFraction != 0.0 |
| 2510 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2511 | ) { |
| 2512 | return -14; |
| 2513 | } |
| 2514 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2515 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2516 | /* 11. Epoch date with single-precision NaN */ |
| 2517 | 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] | 2518 | return -15; |
| 2519 | } |
| 2520 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2521 | /* 12. Epoch date with double-precision plus infinity */ |
| 2522 | 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] | 2523 | return -16; |
| 2524 | } |
| 2525 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2526 | /* 13. Epoch date with half-precision negative infinity */ |
| 2527 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2528 | 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] | 2529 | return -17; |
| 2530 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2531 | |
| 2532 | return 0; |
| 2533 | } |
| 2534 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2535 | /* |
| 2536 | Test cases covered here. Some items cover more than one of these. |
| 2537 | positive integer (zero counts as a positive integer) |
| 2538 | negative integer |
| 2539 | half-precision float |
| 2540 | single-precision float |
| 2541 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2542 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2543 | float Overflow error |
| 2544 | Wrong type error for epoch |
| 2545 | Wrong type error for date string |
| 2546 | float disabled error |
| 2547 | half-precision disabled error |
| 2548 | -Infinity |
| 2549 | Slightly too large integer |
| 2550 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2551 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2552 | Get epoch by int |
| 2553 | Get string by int |
| 2554 | Get epoch by string |
| 2555 | Get string by string |
| 2556 | Fail to get epoch by wrong int label |
| 2557 | Fail to get string by wrong string label |
| 2558 | Fail to get epoch by string because it is invalid |
| 2559 | Fail to get epoch by int because it is invalid |
| 2560 | |
| 2561 | Untagged values |
| 2562 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2563 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 2564 | 0x86, // array of 6 items |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2565 | |
| 2566 | 0xc1, |
| 2567 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 2568 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2569 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2570 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 2571 | |
| 2572 | 0xc1, // tag for epoch date |
| 2573 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 2574 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2575 | 0xad, // Open a map for tests involving labels. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2576 | |
| 2577 | 0x00, |
| 2578 | 0xc0, // tag for string date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2579 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2580 | |
| 2581 | 0x01, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2582 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2583 | 0xc1, // tag for epoch date |
| 2584 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2585 | |
| 2586 | // Untagged integer 0 |
| 2587 | 0x08, |
| 2588 | 0x00, |
| 2589 | |
| 2590 | // Utagged date string with string label y |
| 2591 | 0x61, 0x79, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2592 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2593 | |
| 2594 | // Untagged -1000 with label z |
| 2595 | 0x61, 0x7a, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2596 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2597 | 0x39, 0x03, 0xe7, |
| 2598 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2599 | 0x07, |
| 2600 | 0xc1, // tag for epoch date |
| 2601 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2602 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2603 | 0x05, |
| 2604 | 0xc1, |
| 2605 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 2606 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2607 | // Untagged single-precision float with value 3.14 with string label x |
| 2608 | 0x61, 0x78, |
| 2609 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 2610 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2611 | // Untagged half-precision float with value -2 |
| 2612 | 0x09, |
| 2613 | 0xF9, 0xC0, 0x00, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2614 | |
| 2615 | /* Tagged date-only date string */ |
| 2616 | 0x63, 0x53, 0x44, 0x53, |
| 2617 | 0xD9, 0x03, 0xEC, |
| 2618 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2619 | |
| 2620 | /* Untagged date-only date string */ |
| 2621 | 0x18, 0x63, |
| 2622 | 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */ |
| 2623 | |
| 2624 | /* Tagged days-count epoch date */ |
| 2625 | 0x63, 0x53, 0x44, 0x45, |
| 2626 | 0xD8, 0x64, /* tag(100) */ |
| 2627 | 0x39, 0x29, 0xB3, /* -10676 */ |
| 2628 | |
| 2629 | /* Untagged days-count epoch date */ |
| 2630 | 0x11, |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 2631 | 0x19, 0x0F, 0x9A, /* 3994 */ |
| 2632 | |
| 2633 | // End of map, back to array |
| 2634 | |
| 2635 | // These two at the end because they are unrecoverable errors |
| 2636 | 0xc1, // tag for epoch date |
| 2637 | 0x80, // Erroneous empty array as content for date |
| 2638 | |
| 2639 | 0xc0, // tag for string date |
| 2640 | 0xa0 // Erroneous empty map as content for date |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2641 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2642 | }; |
| 2643 | |
| 2644 | int32_t SpiffyDateDecodeTest() |
| 2645 | { |
| 2646 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2647 | QCBORError uError; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2648 | int64_t nEpochDate2, nEpochDate3, nEpochDate5, |
| 2649 | nEpochDate4, nEpochDate6, nEpochDateFail, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2650 | nEpochDate1400000000, nEpochDays1, nEpochDays2; |
| 2651 | UsefulBufC StringDate1, StringDate2, StringDays1, StringDays2; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2652 | uint64_t uTag1, uTag2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2653 | |
| 2654 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2655 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2656 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2657 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2658 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2659 | // Too-negative float, -9.2233720368547748E+18 |
| 2660 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2661 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2662 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2663 | return 1111; |
| 2664 | } |
| 2665 | |
| 2666 | // Too-large integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2667 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2668 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2669 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2670 | return 1; |
| 2671 | } |
| 2672 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2673 | // Half-precision minus infinity |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2674 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2675 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2676 | if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2677 | return 2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2680 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2681 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2682 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2683 | |
| 2684 | // Get largest negative double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2685 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2686 | 5, |
| 2687 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 2688 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2689 | &nEpochDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2690 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2691 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2692 | return 102; |
| 2693 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2694 | if(uError == QCBOR_SUCCESS) { |
| 2695 | if(nEpochDate2 != -9223372036854773760LL) { |
| 2696 | return 101; |
| 2697 | } |
| 2698 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2699 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2700 | // Get largest double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2701 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2702 | &nEpochDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2703 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2704 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2705 | return 112; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2706 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2707 | if(uError == QCBOR_SUCCESS) { |
| 2708 | if(nEpochDate2 != 9223372036854773760ULL) { |
| 2709 | return 111; |
| 2710 | } |
| 2711 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2712 | |
| 2713 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2714 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2715 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2716 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2717 | if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2718 | return 104; |
| 2719 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2720 | if(uError == QCBOR_SUCCESS) { |
| 2721 | if(nEpochDate5 != 3) { |
| 2722 | return 103; |
| 2723 | } |
| 2724 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2725 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2726 | // A half-precision date with value -2 FFF |
| 2727 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2728 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2729 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2730 | 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] | 2731 | return 106; |
| 2732 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2733 | if(uError == QCBOR_SUCCESS) { |
| 2734 | if(nEpochDate4 != -2) { |
| 2735 | return 105; |
| 2736 | } |
| 2737 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2738 | |
| 2739 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2740 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 2741 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2742 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2743 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2744 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2745 | return 107; |
| 2746 | } |
| 2747 | |
| 2748 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2749 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2750 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2751 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2752 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2753 | return 108; |
| 2754 | } |
| 2755 | |
| 2756 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2757 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 2758 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2759 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2760 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2761 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2762 | return 109; |
| 2763 | } |
| 2764 | |
| 2765 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2766 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2767 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2768 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2769 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2770 | return 110; |
| 2771 | } |
| 2772 | |
| 2773 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2774 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2775 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2776 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2777 | 1, |
| 2778 | QCBOR_TAG_REQUIREMENT_TAG | |
| 2779 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2780 | &nEpochDate1400000000); |
| 2781 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2782 | // Tagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2783 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2784 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2785 | // Untagged integer 0 |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2786 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2787 | &nEpochDate3); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2788 | // Untagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2789 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2790 | &StringDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2791 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2792 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 2793 | "z", |
| 2794 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 2795 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2796 | &nEpochDate6); |
| 2797 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2798 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2799 | /* The days format is much simpler than the date format |
| 2800 | * because it can't be a floating point value. The test |
| 2801 | * of the spiffy decode functions sufficiently covers |
| 2802 | * the test of the non-spiffy decode days date decoding. |
| 2803 | * There is no full fan out of the error conditions |
| 2804 | * and decode options as that is implemented by code |
| 2805 | * that is tested well by the date testing above. |
| 2806 | */ |
| 2807 | QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG, |
| 2808 | &StringDays1); |
| 2809 | |
| 2810 | QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2811 | &StringDays2); |
| 2812 | |
| 2813 | QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG, |
| 2814 | &nEpochDays1); |
| 2815 | |
| 2816 | QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2817 | &nEpochDays2); |
| 2818 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2819 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 2820 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 2821 | return 3001; |
| 2822 | } |
| 2823 | |
| 2824 | // Bad content for epoch date |
| 2825 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
| 2826 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2827 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 2828 | return 3; |
| 2829 | } |
| 2830 | |
| 2831 | // Bad content for string date |
| 2832 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
| 2833 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2834 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 2835 | return 4; |
| 2836 | } |
| 2837 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2838 | QCBORDecode_ExitArray(&DC); |
| 2839 | uError = QCBORDecode_Finish(&DC); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 2840 | if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2841 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2844 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2845 | return 200; |
| 2846 | } |
| 2847 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2848 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2849 | return 201; |
| 2850 | } |
| 2851 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2852 | if(nEpochDate3 != 0) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2853 | return 202; |
| 2854 | } |
| 2855 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2856 | if(nEpochDate6 != -1000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2857 | return 203; |
| 2858 | } |
| 2859 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2860 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2861 | return 204; |
| 2862 | } |
| 2863 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2864 | if(nEpochDays1 != -10676) { |
| 2865 | return 205; |
| 2866 | } |
| 2867 | |
| 2868 | if(nEpochDays2 != 3994) { |
| 2869 | return 206; |
| 2870 | } |
| 2871 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2872 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2873 | return 205; |
| 2874 | } |
| 2875 | |
| 2876 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 2877 | return 206; |
| 2878 | } |
| 2879 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2880 | if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2881 | return 207; |
| 2882 | } |
| 2883 | |
| 2884 | if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2885 | return 208; |
| 2886 | } |
| 2887 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2888 | return 0; |
| 2889 | } |
| 2890 | |
| 2891 | |
| 2892 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2893 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2894 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2895 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2896 | 0x81, // Array of one |
| 2897 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 2898 | 0x82, // Array of two that is the faction 1/3 |
| 2899 | 0x01, |
| 2900 | 0x03, |
| 2901 | |
| 2902 | /* |
| 2903 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 2904 | */ |
| 2905 | 0xd8, 0xe1, |
| 2906 | 0xd8, 0xe2, |
| 2907 | 0xd8, 0xe3, |
| 2908 | 0xd8, 0xe4, |
| 2909 | 0xd8, 0xe5, |
| 2910 | 0x80, |
| 2911 | |
| 2912 | /* tag 10489608748473423768( |
| 2913 | 2442302356( |
| 2914 | 21590( |
| 2915 | 240( |
| 2916 | [])))) |
| 2917 | */ |
| 2918 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2919 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 2920 | 0xd9, 0x54, 0x56, |
| 2921 | 0xd8, 0xf0, |
| 2922 | 0x80, |
| 2923 | |
| 2924 | /* tag 21590( |
| 2925 | 10489608748473423768( |
| 2926 | 2442302357( |
| 2927 | 65534( |
| 2928 | [])))) |
| 2929 | */ |
| 2930 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 2931 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2932 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 2933 | 0xd9, 0xff, 0xfe, |
| 2934 | 0x80, |
| 2935 | |
| 2936 | /* Make sure to blow past the limit of tags that must be mapped. |
| 2937 | works in conjuntion with entries above. |
| 2938 | 269488144(269488145(269488146(269488147([])))) |
| 2939 | */ |
| 2940 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 2941 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 2942 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 2943 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 2944 | 0x80, |
| 2945 | |
| 2946 | /* An invalid decimal fraction with an additional tag */ |
| 2947 | 0xd9, 0xff, 0xfa, |
| 2948 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 2949 | 0x00, // the integer 0; should be a byte string |
| 2950 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2951 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2952 | /* |
| 2953 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2954 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2955 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2956 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2957 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2958 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2959 | /* |
| 2960 | DB 9192939495969798 # tag(10489608748473423768) |
| 2961 | D8 88 # tag(136) |
| 2962 | C6 # tag(6) |
| 2963 | C7 # tag(7) |
| 2964 | 80 # array(0) |
| 2965 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2966 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2967 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2968 | |
| 2969 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2970 | 55799(55799(55799({ |
| 2971 | 6(7(-23)): 5859837686836516696(7({ |
| 2972 | 7(-20): 11({ |
| 2973 | 17(-18): 17(17(17("Organization"))), |
| 2974 | 9(-17): 773("SSG"), |
| 2975 | -15: 16(17(6(7("Confusion")))), |
| 2976 | 17(-16): 17("San Diego"), |
| 2977 | 17(-14): 17("US") |
| 2978 | }), |
| 2979 | 23(-19): 19({ |
| 2980 | -11: 9({ |
| 2981 | -9: -7 |
| 2982 | }), |
| 2983 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 2984 | }) |
| 2985 | })), |
| 2986 | 16(-22): 23({ |
| 2987 | 11(8(7(-5))): 8(-3) |
| 2988 | }) |
| 2989 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2990 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2991 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2992 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 2993 | 0xc6, 0xc7, 0x36, |
| 2994 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 2995 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 2996 | 0xcb, 0xa5, |
| 2997 | 0xd1, 0x31, |
| 2998 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 2999 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 3000 | 0xc9, 0x30, |
| 3001 | 0xd9, 0x03, 0x05, 0x63, |
| 3002 | 0x53, 0x53, 0x47, |
| 3003 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3004 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 3005 | 0x69, |
| 3006 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3007 | 0xd1, 0x2f, |
| 3008 | 0xd1, 0x69, |
| 3009 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 3010 | 0xd1, 0x2d, |
| 3011 | 0xd1, 0x62, |
| 3012 | 0x55, 0x53, |
| 3013 | 0xd7, 0x32, |
| 3014 | 0xd3, 0xa2, |
| 3015 | 0x2a, |
| 3016 | 0xc9, 0xa1, |
| 3017 | 0x28, |
| 3018 | 0x26, |
| 3019 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 3020 | 0xcc, 0x4a, |
| 3021 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 3022 | 0xd0, 0x35, |
| 3023 | 0xd7, 0xa1, |
| 3024 | 0xcb, 0xc8, 0xc7, 0x24, |
| 3025 | 0xc8, 0x22}; |
| 3026 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3027 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3028 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3029 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3030 | |
| 3031 | 0xc0, // tag for string date |
| 3032 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3033 | |
| 3034 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3035 | |
| 3036 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 3037 | |
| 3038 | 0xd8, 0x23, // tag for regex |
| 3039 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 3040 | |
| 3041 | 0xc0, // tag for string date |
| 3042 | 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^] | 3043 | |
| 3044 | // This last case makes the array untraversable because it is |
| 3045 | // an uncrecoverable error. Make sure it stays last and is the only |
| 3046 | // instance so the other tests can work. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3047 | }; |
| 3048 | |
| 3049 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3050 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3051 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3052 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3053 | int32_t OptTagParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3054 | { |
| 3055 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3056 | QCBORItem Item; |
| 3057 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3058 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3059 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3060 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3061 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3062 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3063 | /* |
| 3064 | This test matches the magic number tag and the fraction tag |
| 3065 | 55799([...]) |
| 3066 | */ |
| 3067 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3068 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3069 | return -2; |
| 3070 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3071 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3072 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 3073 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3074 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3075 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3076 | /* |
| 3077 | 4([1,3]) |
| 3078 | */ |
| 3079 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3080 | #ifdef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3081 | if(uError != QCBOR_SUCCESS || |
| 3082 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3083 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 3084 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 3085 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3086 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3087 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3088 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 3089 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3090 | return -4; |
| 3091 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3092 | // consume the items in the array |
| 3093 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3094 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3095 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3096 | #else /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3097 | if(uError != QCBOR_SUCCESS || |
| 3098 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3099 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 3100 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3101 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3102 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3103 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 3104 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3105 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3106 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3107 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3108 | /* |
| 3109 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3110 | */ |
| 3111 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3112 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3113 | return -6; |
| 3114 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3115 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 3116 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 3117 | return -106; |
| 3118 | } |
| 3119 | |
| 3120 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3121 | /* tag 10489608748473423768( |
| 3122 | 2442302356( |
| 3123 | 21590( |
| 3124 | 240( |
| 3125 | [])))) |
| 3126 | */ |
| 3127 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3128 | if(uError != QCBOR_SUCCESS || |
| 3129 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3130 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 3131 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 3132 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 3133 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3134 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | /* tag 21590( |
| 3138 | 10489608748473423768( |
| 3139 | 2442302357( |
| 3140 | 21591( |
| 3141 | [])))) |
| 3142 | */ |
| 3143 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3144 | if(uError != QCBOR_SUCCESS || |
| 3145 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3146 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 3147 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 3148 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 3149 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 3150 | return -8; |
| 3151 | } |
| 3152 | |
| 3153 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3154 | works in conjuntion with entries above. |
| 3155 | 269488144(269488145(269488146(269488147([])))) |
| 3156 | */ |
| 3157 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3158 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3159 | return -9; |
| 3160 | } |
| 3161 | |
| 3162 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3163 | if(uError == QCBOR_SUCCESS) { |
| 3164 | return -10; |
| 3165 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3166 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3167 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3168 | // 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] | 3169 | // tage and then matches it. Caller-config lists are no longer |
| 3170 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3171 | QCBORDecode_Init(&DCtx, |
| 3172 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3173 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3174 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 3175 | const QCBORTagListIn TL = {2, puList}; |
| 3176 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3177 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3178 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3179 | return -8; |
| 3180 | } |
| 3181 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3182 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 3183 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 3184 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 3185 | Item.val.uCount != 0) { |
| 3186 | return -9; |
| 3187 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3188 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3189 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3190 | // 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] | 3191 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3192 | const uint64_t puLongList[17] = {1,2,1}; |
| 3193 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3194 | QCBORDecode_Init(&DCtx, |
| 3195 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3196 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3197 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 3198 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3199 | return -11; |
| 3200 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3201 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3202 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3203 | QCBORTagListOut Out = {0, 4, puTags}; |
| 3204 | |
| 3205 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3206 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3207 | QCBORDecode_Init(&DCtx, |
| 3208 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3209 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3210 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3211 | return -12; |
| 3212 | } |
| 3213 | if(puTags[0] != 0x9192939495969798 || |
| 3214 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3215 | puTags[2] != 0x06 || |
| 3216 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3217 | return -13; |
| 3218 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3219 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3220 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3221 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3222 | QCBORDecode_Init(&DCtx, |
| 3223 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3224 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3225 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 3226 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3227 | return -14; |
| 3228 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3229 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3230 | |
| 3231 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3232 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3233 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 3234 | // It is a bit of a messy test and maybe could be improved, but |
| 3235 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3236 | QCBORDecode_Init(&DCtx, |
| 3237 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3238 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3239 | int n = CheckCSRMaps(&DCtx); |
| 3240 | if(n) { |
| 3241 | return n-2000; |
| 3242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3243 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3244 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3245 | QCBORDecode_Init(&DCtx, |
| 3246 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3247 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3248 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3249 | /* With the spiffy decode revision, this tag list is not used. |
| 3250 | It doesn't matter if a tag is in this list or not so some |
| 3251 | tests that couldn't process a tag because it isn't in this list |
| 3252 | now can process these unlisted tags. The tests have been |
| 3253 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3254 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 3255 | const QCBORTagListIn TagList = {3, puTagList}; |
| 3256 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3257 | |
| 3258 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3259 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3260 | return -100; |
| 3261 | } |
| 3262 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3263 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3264 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 3265 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 3266 | Item.val.uCount != 2 || |
| 3267 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 3268 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 3269 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 3270 | Out.uNumUsed != 3) { |
| 3271 | return -101; |
| 3272 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3273 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3274 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3275 | return -102; |
| 3276 | } |
| 3277 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3278 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3279 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3280 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3281 | Item.val.uCount != 2 || |
| 3282 | puTags[0] != 5859837686836516696 || |
| 3283 | puTags[1] != 7 || |
| 3284 | Out.uNumUsed != 2) { |
| 3285 | return -103; |
| 3286 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3287 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3288 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3289 | return -104; |
| 3290 | } |
| 3291 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3292 | Item.val.uCount != 5 || |
| 3293 | puTags[0] != 0x0b || |
| 3294 | Out.uNumUsed != 1) { |
| 3295 | return -105; |
| 3296 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3297 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3298 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3299 | return -106; |
| 3300 | } |
| 3301 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3302 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 3303 | Item.val.string.len != 12 || |
| 3304 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 3305 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 3306 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 3307 | Out.uNumUsed != 3) { |
| 3308 | return -105; |
| 3309 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3310 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3311 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3312 | return -107; |
| 3313 | } |
| 3314 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3315 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 3316 | Item.val.string.len != 3 || |
| 3317 | puTags[0] != 773 || |
| 3318 | Out.uNumUsed != 1) { |
| 3319 | return -108; |
| 3320 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3321 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3322 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3323 | return -109; |
| 3324 | } |
| 3325 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3326 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3327 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3328 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3329 | puTags[3] != 7 || |
| 3330 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3331 | return -110; |
| 3332 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3333 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3334 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3335 | return -111; |
| 3336 | } |
| 3337 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3338 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3339 | Item.val.string.len != 9 || |
| 3340 | puTags[0] != 17 || |
| 3341 | Out.uNumUsed != 1) { |
| 3342 | return -112; |
| 3343 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3344 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3345 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3346 | return -111; |
| 3347 | } |
| 3348 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3349 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3350 | Item.val.string.len != 2 || |
| 3351 | puTags[0] != 17 || |
| 3352 | Out.uNumUsed != 1) { |
| 3353 | return -112; |
| 3354 | } |
| 3355 | |
| 3356 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3357 | return -113; |
| 3358 | } |
| 3359 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3360 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3361 | Item.val.uCount != 2 || |
| 3362 | puTags[0] != 19 || |
| 3363 | Out.uNumUsed != 1) { |
| 3364 | return -114; |
| 3365 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3366 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3367 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3368 | return -115; |
| 3369 | } |
| 3370 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3371 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3372 | Item.val.uCount != 1 || |
| 3373 | puTags[0] != 9 || |
| 3374 | Out.uNumUsed != 1) { |
| 3375 | return -116; |
| 3376 | } |
| 3377 | |
| 3378 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3379 | return -116; |
| 3380 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3381 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3382 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3383 | Out.uNumUsed != 0) { |
| 3384 | return -117; |
| 3385 | } |
| 3386 | |
| 3387 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3388 | return -118; |
| 3389 | } |
| 3390 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 3391 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3392 | puTags[0] != 12 || |
| 3393 | Out.uNumUsed != 1) { |
| 3394 | return -119; |
| 3395 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3396 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3397 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3398 | return -120; |
| 3399 | } |
| 3400 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3401 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 3402 | Item.val.uCount != 1 || |
| 3403 | puTags[0] != 0x17 || |
| 3404 | Out.uNumUsed != 1) { |
| 3405 | return -121; |
| 3406 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3407 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3408 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3409 | return -122; |
| 3410 | } |
| 3411 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3412 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3413 | Item.val.int64 != -3 || |
| 3414 | puTags[0] != 8 || |
| 3415 | Out.uNumUsed != 1) { |
| 3416 | return -123; |
| 3417 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3418 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3419 | if(QCBORDecode_Finish(&DCtx)) { |
| 3420 | return -124; |
| 3421 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3422 | |
| 3423 | UsefulBufC DateString; |
| 3424 | QCBORDecode_Init(&DCtx, |
| 3425 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3426 | QCBOR_DECODE_MODE_NORMAL); |
| 3427 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3428 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3429 | // tagged date string |
| 3430 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3431 | // untagged date string |
| 3432 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3433 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3434 | return 100; |
| 3435 | } |
| 3436 | // untagged byte string |
| 3437 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3438 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3439 | return 101; |
| 3440 | } |
| 3441 | // tagged regex |
| 3442 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3443 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3444 | return 102; |
| 3445 | } |
| 3446 | // tagged date string with a byte string |
| 3447 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3448 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3449 | return 103; |
| 3450 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3451 | // The exit errors out because the last item, the date string with |
| 3452 | // bad content makes the array untraversable (the bad date string |
| 3453 | // could have tag content of an array or such that is not consumed |
| 3454 | // by the date decoding). |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3455 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3456 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3457 | return 104; |
| 3458 | } |
| 3459 | |
| 3460 | |
| 3461 | QCBORDecode_Init(&DCtx, |
| 3462 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3463 | QCBOR_DECODE_MODE_NORMAL); |
| 3464 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3465 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3466 | // tagged date string |
| 3467 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3468 | // untagged date string |
| 3469 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3470 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3471 | return 200; |
| 3472 | } |
| 3473 | // untagged byte string |
| 3474 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3475 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3476 | return 201; |
| 3477 | } |
| 3478 | // tagged regex |
| 3479 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3480 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3481 | return 202; |
| 3482 | } |
| 3483 | // tagged date string with a byte string |
| 3484 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3485 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3486 | return 203; |
| 3487 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3488 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3489 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3490 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3491 | return 204; |
| 3492 | } |
| 3493 | |
| 3494 | QCBORDecode_Init(&DCtx, |
| 3495 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3496 | QCBOR_DECODE_MODE_NORMAL); |
| 3497 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3498 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3499 | // tagged date string |
| 3500 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3501 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3502 | return 300; |
| 3503 | } |
| 3504 | // untagged date string |
| 3505 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3506 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3507 | return 301; |
| 3508 | } |
| 3509 | // untagged byte string |
| 3510 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3511 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3512 | return 302; |
| 3513 | } |
| 3514 | // tagged regex |
| 3515 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3516 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3517 | return 303; |
| 3518 | } |
| 3519 | // tagged date string with a byte string |
| 3520 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3521 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3522 | return 304; |
| 3523 | } |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3524 | // See comments above |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3525 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 3526 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3527 | return 305; |
| 3528 | } |
| 3529 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3530 | return 0; |
| 3531 | } |
| 3532 | |
| 3533 | |
| 3534 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3535 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3536 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3537 | 0x83, |
| 3538 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3539 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3540 | 0xA4, |
| 3541 | 0x63, 0x42, 0x4E, 0x2B, |
| 3542 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3543 | 0x18, 0x40, |
| 3544 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3545 | 0x63, 0x42, 0x4E, 0x2D, |
| 3546 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3547 | 0x38, 0x3F, |
| 3548 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 3549 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3550 | /* The expected big num */ |
| 3551 | static const uint8_t spBigNum[] = { |
| 3552 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3553 | 0x00}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3554 | |
| 3555 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3556 | int32_t BignumParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3557 | { |
| 3558 | QCBORDecodeContext DCtx; |
| 3559 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3560 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3561 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3562 | QCBORDecode_Init(&DCtx, |
| 3563 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 3564 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3565 | |
| 3566 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3567 | // |
| 3568 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 3569 | return -1; |
| 3570 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3571 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3572 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3573 | |
| 3574 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3575 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3576 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3577 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3578 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3579 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | // |
| 3583 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3584 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3585 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3586 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3587 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3588 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3589 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3590 | // |
| 3591 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3592 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3593 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3594 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3595 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3596 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3597 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3598 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3599 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3600 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3601 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3602 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3606 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3607 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3608 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3609 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3610 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3611 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3612 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3613 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3614 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3615 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3616 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3617 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3618 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3619 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3620 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3621 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3622 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3623 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3624 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3625 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3626 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3627 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3628 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3629 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3630 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3631 | return 0; |
| 3632 | } |
| 3633 | |
| 3634 | |
| 3635 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3636 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3637 | uint8_t uDataType, |
| 3638 | uint8_t uNestingLevel, |
| 3639 | uint8_t uNextNest, |
| 3640 | int64_t nLabel, |
| 3641 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3642 | { |
| 3643 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3644 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3645 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3646 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 3647 | if(Item.uDataType != uDataType) return -1; |
| 3648 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3649 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 3650 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 3651 | return -1; |
| 3652 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3653 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3654 | if(Item.label.int64 != nLabel) return -1; |
| 3655 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3656 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3657 | } |
| 3658 | } |
| 3659 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3660 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3661 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3662 | if(pItem) { |
| 3663 | *pItem = Item; |
| 3664 | } |
| 3665 | return 0; |
| 3666 | } |
| 3667 | |
| 3668 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3669 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3670 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3671 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3672 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3673 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3674 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3675 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3676 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3677 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3678 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 3679 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 3680 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 3681 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 3682 | 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] | 3683 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3684 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 3685 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3686 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3687 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 3688 | 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] | 3689 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3690 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 3691 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3692 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3693 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3694 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3695 | return 0; |
| 3696 | } |
| 3697 | |
| 3698 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3699 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3700 | { |
| 3701 | -23: { |
| 3702 | -20: { |
| 3703 | -18: "Organization", |
| 3704 | -17: "SSG", |
| 3705 | -15: "Confusion", |
| 3706 | -16: "San Diego", |
| 3707 | -14: "US" |
| 3708 | }, |
| 3709 | -19: { |
| 3710 | -11: { |
| 3711 | -9: -7 |
| 3712 | }, |
| 3713 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 3714 | } |
| 3715 | }, |
| 3716 | -22: { |
| 3717 | -5: -3 |
| 3718 | } |
| 3719 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3720 | */ |
| 3721 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3722 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 3723 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3724 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3725 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3726 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3727 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3728 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 3729 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 3730 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 3731 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3732 | // Same map as above, but using indefinite lengths |
| 3733 | static const uint8_t spCSRInputIndefLen[] = { |
| 3734 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 3735 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3736 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3737 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3738 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3739 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3740 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 3741 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 3742 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 3743 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 3744 | |
| 3745 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3746 | int32_t NestedMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3747 | { |
| 3748 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3749 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3750 | QCBORDecode_Init(&DCtx, |
| 3751 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3752 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3753 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3754 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3755 | } |
| 3756 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3757 | |
| 3758 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3759 | int32_t StringDecoderModeFailTest() |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3760 | { |
| 3761 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3762 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3763 | QCBORDecode_Init(&DCtx, |
| 3764 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3765 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3766 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3767 | QCBORItem Item; |
| 3768 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3769 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3770 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3771 | return -1; |
| 3772 | } |
| 3773 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3774 | return -2; |
| 3775 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3776 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3777 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3778 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 3779 | return -3; |
| 3780 | } |
| 3781 | |
| 3782 | return 0; |
| 3783 | } |
| 3784 | |
| 3785 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3786 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3787 | int32_t NestedMapTestIndefLen() |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3788 | { |
| 3789 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3790 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3791 | QCBORDecode_Init(&DCtx, |
| 3792 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 3793 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3794 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3795 | return CheckCSRMaps(&DCtx); |
| 3796 | } |
| 3797 | |
| 3798 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3799 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3800 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 3801 | { |
| 3802 | UsefulOutBuf UOB; |
| 3803 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3804 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3805 | int i; |
| 3806 | for(i = 0; i < n; i++) { |
| 3807 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 3808 | } |
| 3809 | |
| 3810 | for(i = 0; i < n; i++) { |
| 3811 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3812 | } |
| 3813 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3814 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3815 | |
| 3816 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3817 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3818 | { |
| 3819 | QCBORDecodeContext DC; |
| 3820 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3821 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3822 | int j; |
| 3823 | for(j = 0; j < nNestLevel; j++) { |
| 3824 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3825 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3826 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 3827 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3828 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3829 | return -4; |
| 3830 | } else { |
| 3831 | return 0; // Decoding doesn't recover after an error |
| 3832 | } |
| 3833 | } else { |
| 3834 | // Should be no error |
| 3835 | if(nReturn) { |
| 3836 | return -9; // Should not have got an error |
| 3837 | } |
| 3838 | } |
| 3839 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3840 | return -7; |
| 3841 | } |
| 3842 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3843 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3844 | if(nReturn) { |
| 3845 | return -3; |
| 3846 | } |
| 3847 | return 0; |
| 3848 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3849 | |
| 3850 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3851 | int32_t IndefiniteLengthNestTest() |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3852 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3853 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3854 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3855 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3856 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3857 | int nReturn = parse_indeflen_nested(Nested, i); |
| 3858 | if(nReturn) { |
| 3859 | return nReturn; |
| 3860 | } |
| 3861 | } |
| 3862 | return 0; |
| 3863 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3864 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3865 | // [1, [2, 3]] |
| 3866 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 3867 | // No closing break |
| 3868 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 3869 | // Not enough closing breaks |
| 3870 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 3871 | // Too many closing breaks |
| 3872 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 3873 | // Unclosed indeflen inside def len |
| 3874 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 3875 | // confused tag |
| 3876 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3877 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3878 | int32_t IndefiniteLengthArrayMapTest() |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3879 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3880 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3881 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3882 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3883 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3884 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3885 | QCBORDecodeContext DC; |
| 3886 | QCBORItem Item; |
| 3887 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3888 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3889 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3890 | |
| 3891 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3892 | Item.uNestingLevel != 0 || |
| 3893 | Item.uNextNestLevel != 1) { |
| 3894 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3895 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3896 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3897 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3898 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 3899 | Item.uNestingLevel != 1 || |
| 3900 | Item.uNextNestLevel != 1) { |
| 3901 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3902 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3903 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3904 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3905 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3906 | Item.uNestingLevel != 1 || |
| 3907 | Item.uNextNestLevel != 2) { |
| 3908 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3909 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3910 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3911 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3912 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3913 | Item.uNestingLevel != 2 || |
| 3914 | Item.uNextNestLevel != 2) { |
| 3915 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3916 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3917 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3918 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3919 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3920 | Item.uNestingLevel != 2 || |
| 3921 | Item.uNextNestLevel != 0) { |
| 3922 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3923 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3924 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3925 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3926 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3927 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3928 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3929 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3930 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3931 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3932 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3933 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3934 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3935 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3936 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3937 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3938 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3939 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3940 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3941 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3942 | } |
| 3943 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3944 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3945 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3946 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3947 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3948 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3949 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3950 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3951 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3952 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3956 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3957 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3958 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3959 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3960 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3961 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3962 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3963 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3964 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3965 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3966 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3967 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3968 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3969 | |
| 3970 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3971 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3972 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3973 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3974 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3975 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3976 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3977 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3978 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3979 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3980 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3981 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3982 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3983 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3984 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3985 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3986 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3987 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3988 | return -140; |
| 3989 | } |
| 3990 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3991 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3992 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3993 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3994 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3995 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3996 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3997 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3998 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3999 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4000 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4001 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4002 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4003 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4004 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4005 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4006 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4007 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4008 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4009 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 4010 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4011 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4012 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4013 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4014 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4015 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4016 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4017 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 4018 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4019 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4020 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4021 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4022 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 4023 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 4024 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4025 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4026 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4027 | return 0; |
| 4028 | } |
| 4029 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4030 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4031 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 4032 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4033 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 4034 | 0x81, // Array of length one |
| 4035 | 0x7f, // text string marked with indefinite length |
| 4036 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4037 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4038 | 0xff // ending break |
| 4039 | }; |
| 4040 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4041 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4042 | 0x81, // Array of length one |
| 4043 | 0x7f, // text string marked with indefinite length |
| 4044 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4045 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 4046 | 0xff // ending break |
| 4047 | }; |
| 4048 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4049 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4050 | 0x81, // Array of length one |
| 4051 | 0x7f, // text string marked with indefinite length |
| 4052 | 0x01, 0x02, // Not a string |
| 4053 | 0xff // ending break |
| 4054 | }; |
| 4055 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4056 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4057 | 0x81, // Array of length one |
| 4058 | 0x7f, // text string marked with indefinite length |
| 4059 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4060 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4061 | // missing end of string |
| 4062 | }; |
| 4063 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4064 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4065 | 0xa1, // Array of length one |
| 4066 | 0x7f, // text string marked with indefinite length |
| 4067 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 4068 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4069 | 0xff, // ending break |
| 4070 | 0x01 // integer being labeled. |
| 4071 | }; |
| 4072 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4073 | /** |
| 4074 | Make an indefinite length string |
| 4075 | |
| 4076 | @param Storage Storage for string, must be 144 bytes in size |
| 4077 | @return The indefinite length string |
| 4078 | |
| 4079 | This makes an array with one indefinite length string that has 7 chunks |
| 4080 | from size of 1 byte up to 64 bytes. |
| 4081 | */ |
| 4082 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4083 | { |
| 4084 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4085 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4086 | UsefulOutBuf_Init(&UOB, Storage); |
| 4087 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 4088 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4089 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4090 | uint8_t uStringByte = 0; |
| 4091 | // Use of type int is intentional |
| 4092 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 4093 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4094 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4095 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 4096 | for(int j = 0; j < uChunkSize; j++) { |
| 4097 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 4098 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4102 | |
| 4103 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4104 | } |
| 4105 | |
| 4106 | static int CheckBigString(UsefulBufC BigString) |
| 4107 | { |
| 4108 | if(BigString.len != 255) { |
| 4109 | return 1; |
| 4110 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4111 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4112 | for(uint8_t i = 0; i < 255; i++){ |
| 4113 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 4114 | return 1; |
| 4115 | } |
| 4116 | } |
| 4117 | return 0; |
| 4118 | } |
| 4119 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4120 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4121 | int32_t IndefiniteLengthStringTest() |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4122 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4123 | QCBORDecodeContext DC; |
| 4124 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4125 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4126 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4127 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4128 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4129 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4130 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4131 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4132 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4133 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4134 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4135 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4136 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4137 | return -2; |
| 4138 | } |
| 4139 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 4140 | return -3; |
| 4141 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4142 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4143 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4144 | return -4; |
| 4145 | } |
| 4146 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 4147 | return -5; |
| 4148 | } |
| 4149 | if(QCBORDecode_Finish(&DC)) { |
| 4150 | return -6; |
| 4151 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4152 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4153 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4154 | QCBORDecode_Init(&DC, |
| 4155 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 4156 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4157 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4158 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4159 | return -7; |
| 4160 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4161 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4162 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4163 | return -8; |
| 4164 | } |
| 4165 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4166 | return -9; |
| 4167 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4168 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4169 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4170 | return -10; |
| 4171 | } |
| 4172 | |
| 4173 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4174 | QCBORDecode_Init(&DC, |
| 4175 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 4176 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4177 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4178 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4179 | return -11; |
| 4180 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4181 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4182 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4183 | return -12; |
| 4184 | } |
| 4185 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4186 | return -13; |
| 4187 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4188 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4189 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4190 | return -14; |
| 4191 | } |
| 4192 | |
| 4193 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4194 | QCBORDecode_Init(&DC, |
| 4195 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 4196 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4197 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4198 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4199 | return -15; |
| 4200 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4201 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4202 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4203 | return -16; |
| 4204 | } |
| 4205 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4206 | return -17; |
| 4207 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4208 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4209 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 4210 | return -18; |
| 4211 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4212 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4213 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4214 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4215 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4216 | QCBORDecode_GetNext(&DC, &Item); |
| 4217 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4218 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4219 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4220 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4221 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4222 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4223 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4224 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4225 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4226 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4227 | |
| 4228 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 4229 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4230 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4231 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4232 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4233 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4234 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4235 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4236 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4237 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 4238 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4239 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4240 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4241 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4242 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4243 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4244 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4245 | QCBORDecode_GetNext(&DC, &Item); |
| 4246 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4247 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4248 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4249 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4250 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4251 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4252 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4253 | // ---- big bstr ----- |
| 4254 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4255 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4256 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4257 | return -25; |
| 4258 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4259 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4260 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4261 | return -26; |
| 4262 | } |
| 4263 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4264 | return -26; |
| 4265 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4266 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4267 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4268 | return -27; |
| 4269 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4270 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4271 | return -28; |
| 4272 | } |
| 4273 | if(CheckBigString(Item.val.string)) { |
| 4274 | return -3; |
| 4275 | } |
| 4276 | if(QCBORDecode_Finish(&DC)) { |
| 4277 | return -29; |
| 4278 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4279 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4280 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4281 | 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] | 4282 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4283 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4284 | return -30; |
| 4285 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4286 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4287 | QCBORDecode_GetNext(&DC, &Item); |
| 4288 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4289 | return -31; |
| 4290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4291 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4292 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 4293 | return -32; |
| 4294 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4295 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4296 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4297 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 4298 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 4299 | return -33; |
| 4300 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4301 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4302 | if(QCBORDecode_Finish(&DC)) { |
| 4303 | return -34; |
| 4304 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4305 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4306 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4310 | int32_t AllocAllStringsTest() |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4311 | { |
| 4312 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4313 | QCBORError nCBORError; |
| 4314 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4315 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4316 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4317 | QCBORDecode_Init(&DC, |
| 4318 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4319 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4320 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4321 | 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] | 4322 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4323 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 4324 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4325 | return -1; |
| 4326 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4327 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4328 | if(CheckCSRMaps(&DC)) { |
| 4329 | return -2; |
| 4330 | } |
| 4331 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 4332 | // Next parse, save pointers to a few strings, destroy original and |
| 4333 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4334 | 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] | 4335 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4336 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4337 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 4338 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4339 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4340 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4341 | QCBORItem Item1, Item2, Item3, Item4; |
| 4342 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4343 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4344 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 4345 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4346 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4347 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4348 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4349 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4350 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4351 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4352 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4353 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4354 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4355 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 4356 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4357 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4358 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4359 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 4360 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4361 | Item1.uDataAlloc != 0 || |
| 4362 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4363 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4364 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4365 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4366 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4367 | |
| 4368 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4369 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4370 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4371 | Item2.uDataAlloc != 0 || |
| 4372 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4373 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4374 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4375 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4376 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4377 | Item3.uDataAlloc == 0 || |
| 4378 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4379 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4380 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4381 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4382 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4383 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4384 | Item4.uDataAlloc == 0 || |
| 4385 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4386 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4387 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4388 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4389 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4390 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4391 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4392 | QCBORDecode_Init(&DC, |
| 4393 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 4394 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4395 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 4396 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4397 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4398 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4399 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4400 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4401 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4402 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 4403 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 4404 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 4405 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 4406 | } |
| 4407 | } |
| 4408 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4409 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4410 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | return 0; |
| 4414 | } |
| 4415 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4416 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4417 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 4418 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4419 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4420 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4421 | QCBORDecodeContext DC; |
| 4422 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 4423 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4424 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4425 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4426 | // Then fish into the internals of the decode context |
| 4427 | // to get the allocator function so it can be called directly. |
| 4428 | // Also figure out how much pool is available for use |
| 4429 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4430 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4431 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 4432 | if(nError) { |
| 4433 | return -9; |
| 4434 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4435 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 4436 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4437 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4438 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4439 | // First test -- ask for one more byte than available and see failure |
| 4440 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4441 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 4442 | return -1; |
| 4443 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4444 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4445 | // Re do the set up for the next test that will do a successful alloc, |
| 4446 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4447 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4448 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4449 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4450 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4451 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4452 | // Allocate one byte less than available and see success |
| 4453 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4454 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4455 | return -2; |
| 4456 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4457 | // Ask for some more and see failure |
| 4458 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4459 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 4460 | return -3; |
| 4461 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4462 | // Free the first allocate, retry the second and see success |
| 4463 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 4464 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4465 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 4466 | return -4; |
| 4467 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4468 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4469 | // Re do set up for next test that involves a successful alloc, |
| 4470 | // and a successful realloc and a failed realloc |
| 4471 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4472 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4473 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4474 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4475 | // Allocate half the pool and see success |
| 4476 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4477 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4478 | return -5; |
| 4479 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4480 | // Reallocate to take up the whole pool and see success |
| 4481 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4482 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 4483 | return -6; |
| 4484 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4485 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4486 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 4487 | return -7; |
| 4488 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4489 | // Try to allocate more to be sure there is failure after a realloc |
| 4490 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 4491 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4492 | return -8; |
| 4493 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4494 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4495 | return 0; |
| 4496 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4497 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4498 | |
| 4499 | /* Just enough of an allocator to test configuration of one */ |
| 4500 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 4501 | { |
| 4502 | (void)pOldMem; // unused variable |
| 4503 | |
| 4504 | if(uNewSize) { |
| 4505 | // Assumes the context pointer is the buffer and |
| 4506 | // nothing too big will ever be asked for. |
| 4507 | // This is only good for this basic test! |
| 4508 | return (UsefulBuf) {pCtx, uNewSize}; |
| 4509 | } else { |
| 4510 | return NULLUsefulBuf; |
| 4511 | } |
| 4512 | } |
| 4513 | |
| 4514 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4515 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4516 | { |
| 4517 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4518 | // nothing can be done with it unless that is set up. |
| 4519 | QCBORDecodeContext DC; |
| 4520 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 4521 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 4522 | |
| 4523 | uint8_t pAllocatorBuffer[50]; |
| 4524 | |
| 4525 | // This is really just to test that this call works. |
| 4526 | // The full functionality of string allocators is tested |
| 4527 | // elsewhere with the MemPool internal allocator. |
| 4528 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 4529 | |
| 4530 | QCBORItem Item; |
| 4531 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 4532 | return -1; |
| 4533 | } |
| 4534 | |
| 4535 | if(Item.uDataAlloc == 0 || |
| 4536 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4537 | Item.val.string.ptr != pAllocatorBuffer) { |
| 4538 | return -2; |
| 4539 | } |
| 4540 | |
| 4541 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 4542 | return -3; |
| 4543 | } |
| 4544 | |
| 4545 | return 0; |
| 4546 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4547 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4548 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4549 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4550 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4551 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4552 | /* exponent, mantissa |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4553 | [ |
| 4554 | 4([-1, 3]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4555 | 4([-20, 4759477275222530853136]), |
| 4556 | 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4557 | 5([300, 100]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4558 | 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4559 | 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4560 | 5([ 9223372036854775806, -4759477275222530853137]) |
| 4561 | 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4562 | ] |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4563 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4564 | static const uint8_t spExpectedExponentsAndMantissas[] = { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4565 | 0x88, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4566 | 0xC4, 0x82, 0x20, |
| 4567 | 0x03, |
| 4568 | 0xC4, 0x82, 0x33, |
| 4569 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4570 | 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4571 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4572 | 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 4573 | 0x18, 0x64, |
| 4574 | 0xC5, 0x82, 0x33, |
| 4575 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4576 | 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4577 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4578 | 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4579 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4580 | 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4581 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE |
| 4582 | }; |
| 4583 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4584 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4585 | int32_t ExponentAndMantissaDecodeTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4586 | { |
| 4587 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4588 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4589 | QCBORItem item; |
| 4590 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 4591 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 4592 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 4593 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4594 | |
| 4595 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4596 | QCBORDecode_Init(&DC, |
| 4597 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4598 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4599 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4600 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4601 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4602 | return 1; |
| 4603 | } |
| 4604 | |
| 4605 | if(item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4606 | return 2; |
| 4607 | } |
| 4608 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4609 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4610 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4611 | return 3; |
| 4612 | } |
| 4613 | |
| 4614 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4615 | item.val.expAndMantissa.Mantissa.nInt != 3 || |
| 4616 | item.val.expAndMantissa.nExponent != -1) { |
| 4617 | return 4; |
| 4618 | } |
| 4619 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4620 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4621 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4622 | return 5; |
| 4623 | } |
| 4624 | |
| 4625 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4626 | item.val.expAndMantissa.nExponent != -20 || |
| 4627 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4628 | return 6; |
| 4629 | } |
| 4630 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4631 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4632 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4633 | return 7; |
| 4634 | } |
| 4635 | |
| 4636 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM || |
| 4637 | item.val.expAndMantissa.nExponent != 9223372036854775807 || |
| 4638 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4639 | return 8; |
| 4640 | } |
| 4641 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4642 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4643 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4644 | return 9; |
| 4645 | } |
| 4646 | |
| 4647 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4648 | item.val.expAndMantissa.Mantissa.nInt != 100 || |
| 4649 | item.val.expAndMantissa.nExponent != 300) { |
| 4650 | return 10; |
| 4651 | } |
| 4652 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4653 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4654 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4655 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4656 | return 11; |
| 4657 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4658 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM || |
| 4659 | item.val.expAndMantissa.nExponent != -20 || |
| 4660 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4661 | return 12; |
| 4662 | } |
| 4663 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4664 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4665 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4666 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4667 | return 13; |
| 4668 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4669 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4670 | item.val.expAndMantissa.nExponent != -9223372036854775807 || |
| 4671 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4672 | return 14; |
| 4673 | } |
| 4674 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4675 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4676 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4677 | if(uErr != QCBOR_SUCCESS) { |
| 4678 | return 15; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4679 | } |
| 4680 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4681 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4682 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4683 | return 16; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4686 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4687 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4688 | if(uErr != QCBOR_SUCCESS) { |
| 4689 | return 17; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4690 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4691 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4692 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4693 | item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4694 | return 18; |
| 4695 | } |
| 4696 | |
| 4697 | uErr = QCBORDecode_Finish(&DC); |
| 4698 | if(uErr != QCBOR_SUCCESS) { |
| 4699 | return 18; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | /* Now encode some stuff and then decode it */ |
| 4703 | uint8_t pBuf[40]; |
| 4704 | QCBOREncodeContext EC; |
| 4705 | UsefulBufC Encoded; |
| 4706 | |
| 4707 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 4708 | QCBOREncode_OpenArray(&EC); |
| 4709 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 4710 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 4711 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 4712 | QCBOREncode_CloseArray(&EC); |
| 4713 | QCBOREncode_Finish(&EC, &Encoded); |
| 4714 | |
| 4715 | |
| 4716 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4717 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4718 | if(uErr != QCBOR_SUCCESS) { |
| 4719 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4720 | } |
| 4721 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4722 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4723 | if(uErr != QCBOR_SUCCESS) { |
| 4724 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4728 | item.val.expAndMantissa.nExponent != 1000 || |
| 4729 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4730 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4731 | } |
| 4732 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4733 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4734 | if(uErr != QCBOR_SUCCESS) { |
| 4735 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4739 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 4740 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4741 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4742 | } |
| 4743 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4744 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4745 | if(uErr != QCBOR_SUCCESS) { |
| 4746 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4750 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 4751 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4752 | return 106; |
| 4753 | } |
| 4754 | |
| 4755 | |
| 4756 | int64_t nExp, nMant; |
| 4757 | UsefulBuf_MAKE_STACK_UB( MantBuf, 20); |
| 4758 | UsefulBufC Mant; |
| 4759 | bool bIsNeg; |
| 4760 | |
| 4761 | QCBORDecode_Init(&DC, |
| 4762 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4763 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4764 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4765 | |
| 4766 | // 4([-1, 3]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4767 | QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4768 | |
| 4769 | // 4([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4770 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, |
| 4771 | &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4772 | |
| 4773 | // 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4774 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4775 | MantBuf, &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4776 | |
| 4777 | // 5([300, 100]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4778 | QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4779 | |
| 4780 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4781 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4782 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4783 | |
| 4784 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4785 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4786 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4787 | |
| 4788 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4789 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4790 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4791 | |
| 4792 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4793 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4794 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4795 | |
| 4796 | QCBORDecode_ExitArray(&DC); |
| 4797 | |
| 4798 | uErr = QCBORDecode_Finish(&DC); |
| 4799 | if(uErr != QCBOR_SUCCESS) { |
| 4800 | return 200; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | return 0; |
| 4804 | } |
| 4805 | |
| 4806 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4807 | static const struct FailInput ExponentAndMantissaFailures[] = { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4808 | // Exponent > INT64_MAX |
| 4809 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4810 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4811 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4812 | // Mantissa > INT64_MAX |
| 4813 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4814 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 4815 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4816 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4817 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4818 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4819 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4820 | // bad content for big num |
| 4821 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 4822 | // bad content for big num |
| 4823 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 4824 | // Bad integer for exponent |
| 4825 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 4826 | // Bad integer for mantissa |
| 4827 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 4828 | // 3 items in array |
| 4829 | { {(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] | 4830 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4831 | // unterminated indefinite length array |
| 4832 | { {(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] | 4833 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4834 | // unterminated indefinite length array |
| 4835 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED}, |
| 4836 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4837 | // Empty array |
| 4838 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 4839 | // Second is not an integer |
| 4840 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4841 | // First is not an integer |
| 4842 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4843 | // Not an array |
| 4844 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 4845 | }; |
| 4846 | |
| 4847 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4848 | int32_t ExponentAndMantissaDecodeFailTests() |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4849 | { |
| 4850 | return ProcessFailures(ExponentAndMantissaFailures, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4851 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 4852 | struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4853 | } |
| 4854 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4855 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4856 | |
| 4857 | |
| 4858 | |
| 4859 | /* |
| 4860 | Some basic CBOR with map and array used in a lot of tests. |
| 4861 | The map labels are all strings |
| 4862 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 4863 | { |
| 4864 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4865 | "an array of two strings": [ |
| 4866 | "string1", "string2" |
| 4867 | ], |
| 4868 | "map in a map": { |
| 4869 | "bytes 1": h'78787878', |
| 4870 | "bytes 2": h'79797979', |
| 4871 | "another int": 98, |
| 4872 | "text 2": "lies, damn lies and statistics" |
| 4873 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4874 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4875 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4876 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4877 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4878 | { |
| 4879 | QCBORItem Item1, Item2, Item3; |
| 4880 | int64_t nDecodedInt1, nDecodedInt2; |
| 4881 | UsefulBufC B1, B2, S1, S2, S3; |
| 4882 | |
| 4883 | QCBORDecodeContext DCtx; |
| 4884 | QCBORError nCBORError; |
| 4885 | |
| 4886 | QCBORDecode_Init(&DCtx, input, 0); |
| 4887 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4888 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4889 | |
| 4890 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 4891 | |
| 4892 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 4893 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4894 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 4895 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 4896 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4897 | QCBORDecode_ExitMap(&DCtx); |
| 4898 | |
| 4899 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 4900 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 4901 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 4902 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4903 | return -400; |
| 4904 | } |
| 4905 | QCBORDecode_ExitArray(&DCtx); |
| 4906 | |
| 4907 | // Parse the same array again using GetText() instead of GetItem() |
| 4908 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4909 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 4910 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4911 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 4912 | return 5000; |
| 4913 | } |
| 4914 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 4915 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4916 | return 5001; |
| 4917 | } */ |
| 4918 | |
| 4919 | QCBORDecode_ExitArray(&DCtx); |
| 4920 | |
| 4921 | QCBORDecode_ExitMap(&DCtx); |
| 4922 | |
| 4923 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 4924 | |
| 4925 | if(nCBORError) { |
| 4926 | return (int32_t)nCBORError; |
| 4927 | } |
| 4928 | |
| 4929 | if(nDecodedInt1 != 42) { |
| 4930 | return 1001; |
| 4931 | } |
| 4932 | |
| 4933 | if(nDecodedInt2 != 98) { |
| 4934 | return 1002; |
| 4935 | } |
| 4936 | |
| 4937 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4938 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4939 | return 1003; |
| 4940 | } |
| 4941 | |
| 4942 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4943 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4944 | return 1004; |
| 4945 | } |
| 4946 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4947 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4948 | return 1005; |
| 4949 | } |
| 4950 | |
| 4951 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 4952 | return 1006; |
| 4953 | } |
| 4954 | |
| 4955 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 4956 | return 1007; |
| 4957 | } |
| 4958 | |
| 4959 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 4960 | return 1008; |
| 4961 | } |
| 4962 | |
| 4963 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 4964 | return 1009; |
| 4965 | } |
| 4966 | |
| 4967 | return 0; |
| 4968 | } |
| 4969 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4970 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4971 | { |
| 4972 | -75008: h'05083399', |
| 4973 | 88: [], |
| 4974 | 100100: { |
| 4975 | "sub1": { |
| 4976 | 10: [ |
| 4977 | 0 |
| 4978 | ], |
| 4979 | -75009: h'A46823990001', |
| 4980 | 100100: { |
| 4981 | "json": "{ \"ueid\", \"xyz\"}", |
| 4982 | "subsub": { |
| 4983 | 100002: h'141813191001' |
| 4984 | } |
| 4985 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4986 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4987 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4988 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4989 | */ |
| 4990 | |
| 4991 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4992 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 4993 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 4994 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 4995 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 4996 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 4997 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 4998 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 4999 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 5000 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 5001 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 5002 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 5003 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5004 | }; |
| 5005 | |
| 5006 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 5007 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 5008 | { |
| 5009 | UsefulBufC String; |
| 5010 | |
| 5011 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 5012 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 5013 | |
| 5014 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5015 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 5016 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5017 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 5018 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 5019 | if(QCBORDecode_GetError(pDCtx)) { |
| 5020 | return 4001; |
| 5021 | } |
| 5022 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 5023 | return 4002; |
| 5024 | } |
| 5025 | QCBORDecode_ExitMap(pDCtx); |
| 5026 | QCBORDecode_ExitMap(pDCtx); |
| 5027 | QCBORDecode_ExitMap(pDCtx); |
| 5028 | QCBORDecode_ExitMap(pDCtx); |
| 5029 | |
| 5030 | return 0; |
| 5031 | } |
| 5032 | |
| 5033 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 5034 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 5035 | { |
| 5036 | QCBORItem Item; |
| 5037 | QCBORError uErr; |
| 5038 | |
| 5039 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 5040 | for(int x = 0; x < 20; x++) { |
| 5041 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5042 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5043 | return 4100; |
| 5044 | |
| 5045 | } |
| 5046 | } |
| 5047 | QCBORDecode_ExitArray(pDCtx); |
| 5048 | if(QCBORDecode_GetError(pDCtx)) { |
| 5049 | return 4101; |
| 5050 | } |
| 5051 | |
| 5052 | return 0; |
| 5053 | } |
| 5054 | |
| 5055 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 5056 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 5057 | { |
| 5058 | QCBORError uErr; |
| 5059 | |
| 5060 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5061 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 5062 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5063 | int64_t nInt = 99; |
| 5064 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 5065 | if(nInt != 0) { |
| 5066 | return 4200; |
| 5067 | } |
| 5068 | for(int x = 0; x < 20; x++) { |
| 5069 | QCBORItem Item; |
| 5070 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5071 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5072 | return 4201; |
| 5073 | |
| 5074 | } |
| 5075 | } |
| 5076 | QCBORDecode_ExitArray(pDCtx); |
| 5077 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 5078 | return 4202; |
| 5079 | } |
| 5080 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5081 | UsefulBufC dD; |
| 5082 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5083 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5084 | return 4203; |
| 5085 | } |
| 5086 | for(int x = 0; x < 20; x++) { |
| 5087 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5088 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 5089 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5090 | return 4204; |
| 5091 | } |
| 5092 | } |
| 5093 | QCBORDecode_ExitArray(pDCtx); |
| 5094 | QCBORDecode_ExitMap(pDCtx); |
| 5095 | QCBORDecode_ExitMap(pDCtx); |
| 5096 | |
| 5097 | return 0; |
| 5098 | } |
| 5099 | |
| 5100 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 5101 | and arrays and such. */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 5102 | static int32_t DecodeNestedIterate(void) |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5103 | { |
| 5104 | QCBORDecodeContext DCtx; |
| 5105 | int32_t nReturn; |
| 5106 | QCBORError uErr; |
| 5107 | |
| 5108 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 5109 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5110 | |
| 5111 | for(int j = 0; j < 5; j++) { |
| 5112 | for(int i = 0; i < 20; i++) { |
| 5113 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 5114 | if(nReturn) { |
| 5115 | return nReturn; |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | for(int i = 0; i < 20; i++) { |
| 5120 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 5121 | if(nReturn ) { |
| 5122 | return nReturn; |
| 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | for(int i = 0; i < 20; i++) { |
| 5127 | nReturn = DecodeNestedGetZero(&DCtx); |
| 5128 | if(nReturn ) { |
| 5129 | return nReturn; |
| 5130 | } |
| 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | QCBORDecode_ExitMap(&DCtx); |
| 5135 | uErr = QCBORDecode_Finish(&DCtx); |
| 5136 | if(uErr) { |
| 5137 | return (int32_t)uErr + 4100; |
| 5138 | } |
| 5139 | |
| 5140 | return 0; |
| 5141 | } |
| 5142 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5143 | |
| 5144 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5145 | [ |
| 5146 | 23, |
| 5147 | 6000, |
| 5148 | h'67616C6163746963', |
| 5149 | h'686176656E20746F6B656E' |
| 5150 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5151 | */ |
| 5152 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5153 | 0x84, |
| 5154 | 0x17, |
| 5155 | 0x19, 0x17, 0x70, |
| 5156 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 5157 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5158 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5159 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5160 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 5161 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5162 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5163 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 5164 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5165 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5166 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5167 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5168 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5169 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5170 | /* |
| 5171 | { |
| 5172 | 0: [], |
| 5173 | 9: [ |
| 5174 | [], |
| 5175 | [] |
| 5176 | ], |
| 5177 | 8: { |
| 5178 | 1: [], |
| 5179 | 2: {}, |
| 5180 | 3: [] |
| 5181 | }, |
| 5182 | 4: {}, |
| 5183 | 5: [], |
| 5184 | 6: [ |
| 5185 | [], |
| 5186 | [] |
| 5187 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5188 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5189 | */ |
| 5190 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5191 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 5192 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 5193 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 5194 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5195 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5196 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5197 | |
| 5198 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5199 | /* |
| 5200 | Too many tags |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5201 | Duplicate label |
| 5202 | Integer overflow |
| 5203 | Date overflow |
| 5204 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5205 | { |
| 5206 | 1: 224(225(226(227(4(0))))), |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5207 | 3: -18446744073709551616, |
| 5208 | 4: 1(1.0e+300), |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 5209 | 5: 0, |
| 5210 | 8: 8 |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5211 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5212 | */ |
| 5213 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 5214 | 0xa6, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5215 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5216 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5217 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
| 5218 | 0x05, 0x00, |
| 5219 | 0x05, 0x00, |
| 5220 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5221 | }; |
| 5222 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5223 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5224 | static const uint8_t spUnRecoverableMapError1[] = { |
| 5225 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5226 | }; |
| 5227 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5228 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5229 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5230 | static const uint8_t spUnRecoverableMapError2[] = { |
| 5231 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5232 | }; |
| 5233 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5234 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5235 | static const uint8_t spUnRecoverableMapError3[] = { |
| 5236 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 5237 | }; |
| 5238 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5239 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5240 | static const uint8_t spUnRecoverableMapError4[] = { |
| 5241 | 0xbf, |
| 5242 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5243 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5244 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5245 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5246 | 0xff |
| 5247 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5248 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5249 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 5250 | const unsigned char not_well_formed_submod_section[] = { |
| 5251 | 0xa1, 0x14, 0x1f, |
| 5252 | }; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5253 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 5254 | |
| 5255 | /* Array of length 3, but only two items. */ |
| 5256 | const unsigned char spBadConsumeInput[] = { |
| 5257 | 0x83, 0x00, 0x00 |
| 5258 | }; |
| 5259 | |
| 5260 | /* Tag nesting too deep. */ |
| 5261 | const unsigned char spBadConsumeInput2[] = { |
| 5262 | 0x81, |
| 5263 | 0xD8, 0x37, |
| 5264 | 0xD8, 0x2C, |
| 5265 | 0xD8, 0x21, |
| 5266 | 0xD6, |
| 5267 | 0xCB, |
| 5268 | 00 |
| 5269 | }; |
| 5270 | |
| 5271 | const unsigned char spBadConsumeInput3[] = { |
| 5272 | 0x81, 0xc0, 0x81, 0x00 |
| 5273 | }; |
| 5274 | |
| 5275 | const unsigned char spBadConsumeInput4[] = { |
| 5276 | 0x81, 0x9f, 0x00, 0xff |
| 5277 | }; |
| 5278 | |
| 5279 | const unsigned char spBadConsumeInput5[] = { |
| 5280 | 0xa1, 0x80, 0x00 |
| 5281 | }; |
| 5282 | |
| 5283 | |
| 5284 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5285 | int32_t EnterMapTest() |
| 5286 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5287 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5288 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5289 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5290 | int32_t nReturn; |
| 5291 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5292 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5293 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5294 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5295 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5296 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5297 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5298 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5299 | QCBORDecode_ExitArray(&DCtx); |
| 5300 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5301 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 5302 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5303 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5304 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5305 | QCBORDecode_ExitArray(&DCtx); |
| 5306 | QCBORDecode_ExitArray(&DCtx); |
| 5307 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5308 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 5309 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5310 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5311 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5312 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5313 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5314 | QCBORDecode_ExitArray(&DCtx); |
| 5315 | QCBORDecode_ExitMap(&DCtx); |
| 5316 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5317 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5318 | QCBORDecode_ExitMap(&DCtx); |
| 5319 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5320 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5321 | QCBORDecode_ExitArray(&DCtx); |
| 5322 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5323 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 5324 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5325 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5326 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5327 | QCBORDecode_ExitArray(&DCtx); |
| 5328 | QCBORDecode_ExitArray(&DCtx); |
| 5329 | |
| 5330 | QCBORDecode_ExitMap(&DCtx); |
| 5331 | |
| 5332 | uErr = QCBORDecode_Finish(&DCtx); |
| 5333 | if(uErr != QCBOR_SUCCESS){ |
| 5334 | return 3011; |
| 5335 | } |
| 5336 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5337 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5338 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5339 | if(nReturn) { |
| 5340 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5341 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5342 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5343 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5344 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5345 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5346 | if(nReturn) { |
| 5347 | return nReturn; |
| 5348 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5349 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5350 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5351 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5352 | // These tests confirm the cursor is at the right place after entering |
| 5353 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5354 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5355 | |
| 5356 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5357 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5358 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5359 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5360 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 5361 | return 2001; |
| 5362 | } |
| 5363 | |
| 5364 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5365 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5366 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5367 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5368 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 5369 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5370 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5371 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 5372 | return 2051; |
| 5373 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5374 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5375 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 5376 | return 2002; |
| 5377 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5378 | QCBORDecode_ExitArray(&DCtx); |
| 5379 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 5380 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5381 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5382 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 5383 | return 2052; |
| 5384 | } |
| 5385 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5386 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5387 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5388 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5389 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5390 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5391 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5392 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5393 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5394 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 5395 | return 2003; |
| 5396 | } |
| 5397 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5398 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5399 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5400 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5401 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5402 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5403 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5404 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5405 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5406 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5407 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5408 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5409 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5410 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5411 | } |
| 5412 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5413 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5414 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 5415 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5416 | QCBORDecode_ExitArray(&DCtx); |
| 5417 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5418 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 5419 | return 2006; |
| 5420 | } |
| 5421 | QCBORDecode_ExitMap(&DCtx); |
| 5422 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5423 | return 2007; |
| 5424 | } |
| 5425 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5426 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5427 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5428 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5429 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5430 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5431 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5432 | return 2008; |
| 5433 | } |
| 5434 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5435 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5436 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5437 | return 2009; |
| 5438 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5439 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5440 | |
| 5441 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5442 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5443 | // This will fail because the map is empty. |
| 5444 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5445 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5446 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5447 | return 2010; |
| 5448 | } |
| 5449 | QCBORDecode_ExitMap(&DCtx); |
| 5450 | uErr = QCBORDecode_Finish(&DCtx); |
| 5451 | if(uErr != QCBOR_SUCCESS){ |
| 5452 | return 2011; |
| 5453 | } |
| 5454 | |
| 5455 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5456 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5457 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5458 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5459 | // This will fail because the map is empty. |
| 5460 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5461 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5462 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5463 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5464 | } |
| 5465 | QCBORDecode_ExitMap(&DCtx); |
| 5466 | uErr = QCBORDecode_Finish(&DCtx); |
| 5467 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5468 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5469 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5470 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5471 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5472 | |
| 5473 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5474 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5475 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5476 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5477 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5478 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5479 | QCBORDecode_ExitArray(&DCtx); |
| 5480 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 5481 | QCBORDecode_ExitArray(&DCtx); |
| 5482 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5483 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5484 | return 2014; |
| 5485 | } |
| 5486 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5487 | int64_t nInt; |
| 5488 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5489 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5490 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5491 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5492 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 5493 | return 2021; |
| 5494 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5495 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 5496 | return 2121; |
| 5497 | } |
| 5498 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 5499 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5500 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5501 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 5502 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5503 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 5504 | return 2023; |
| 5505 | } |
| 5506 | |
| 5507 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 5508 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5509 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5510 | return 2024; |
| 5511 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5512 | |
| 5513 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 5514 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5515 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 5516 | return 2025; |
| 5517 | } |
| 5518 | |
| 5519 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 5520 | |
| 5521 | QCBORDecode_ExitMap(&DCtx); |
| 5522 | uErr = QCBORDecode_Finish(&DCtx); |
| 5523 | if(uErr != QCBOR_SUCCESS) { |
| 5524 | return 2026; |
| 5525 | } |
| 5526 | |
| 5527 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5528 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5529 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5530 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5531 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 5532 | return 2030; |
| 5533 | } |
| 5534 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5535 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5536 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5537 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5538 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5539 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5540 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5541 | return 2031; |
| 5542 | } |
| 5543 | |
| 5544 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5545 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5546 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5547 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5548 | if(uErr != QCBOR_ERR_HIT_END) { |
| 5549 | return 2032; |
| 5550 | } |
| 5551 | |
| 5552 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5553 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5554 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5555 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5556 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 5557 | return 2033; |
| 5558 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5559 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5560 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 5561 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 5562 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5563 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 5564 | return 2401; |
| 5565 | } |
| 5566 | if(QCBORDecode_GetError(&DCtx)) { |
| 5567 | return 2402; |
| 5568 | } |
| 5569 | |
| 5570 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 5571 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5572 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5573 | Item1.val.uCount != 3 || |
| 5574 | Item1.uNextNestLevel != 1) { |
| 5575 | return 2403; |
| 5576 | } |
| 5577 | if(QCBORDecode_GetError(&DCtx)) { |
| 5578 | return 2404; |
| 5579 | } |
| 5580 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5581 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 5582 | Item1.uNextNestLevel != 1 || |
| 5583 | Item1.val.int64 != 42) { |
| 5584 | return 2405; |
| 5585 | } |
| 5586 | if(QCBORDecode_GetError(&DCtx)) { |
| 5587 | return 2406; |
| 5588 | } |
| 5589 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5590 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 5591 | Item1.uNestingLevel != 1 || |
| 5592 | Item1.uNextNestLevel != 1 || |
| 5593 | Item1.val.uCount != 2) { |
| 5594 | return 2407; |
| 5595 | } |
| 5596 | if(QCBORDecode_GetError(&DCtx)) { |
| 5597 | return 2408; |
| 5598 | } |
| 5599 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5600 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5601 | Item1.uNestingLevel != 1 || |
| 5602 | Item1.uNextNestLevel != 0 || |
| 5603 | Item1.val.uCount != 4) { |
| 5604 | return 2409; |
| 5605 | } |
| 5606 | if(QCBORDecode_GetError(&DCtx)) { |
| 5607 | return 2410; |
| 5608 | } |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 5609 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5610 | nReturn = DecodeNestedIterate(); |
| 5611 | |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 5612 | |
| 5613 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0); |
| 5614 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5615 | QCBORDecode_EnterMapFromMapN(&DCtx, 20); |
| 5616 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) { |
| 5617 | return 2500; |
| 5618 | } |
| 5619 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame^] | 5620 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0); |
| 5621 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5622 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5623 | return 2600; |
| 5624 | } |
| 5625 | |
| 5626 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0); |
| 5627 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5628 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 5629 | return 2700; |
| 5630 | } |
| 5631 | |
| 5632 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0); |
| 5633 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5634 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) { |
| 5635 | return 2800; |
| 5636 | } |
| 5637 | |
| 5638 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0); |
| 5639 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5640 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 5641 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 5642 | return 2900; |
| 5643 | } |
| 5644 | #else |
| 5645 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 5646 | return 2901; |
| 5647 | } |
| 5648 | #endif |
| 5649 | |
| 5650 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0); |
| 5651 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5652 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 5653 | return 3000; |
| 5654 | } |
| 5655 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5656 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5660 | struct NumberConversion { |
| 5661 | char *szDescription; |
| 5662 | UsefulBufC CBOR; |
| 5663 | int64_t nConvertedToInt64; |
| 5664 | QCBORError uErrorInt64; |
| 5665 | uint64_t uConvertToUInt64; |
| 5666 | QCBORError uErrorUint64; |
| 5667 | double dConvertToDouble; |
| 5668 | QCBORError uErrorDouble; |
| 5669 | }; |
| 5670 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5671 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
| 5672 | #define EXP_AND_MANTISSA_ERROR(x) x |
| 5673 | #else |
| 5674 | #define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE |
| 5675 | #endif |
| 5676 | |
| 5677 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5678 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5679 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5680 | "too large to fit into int64_t", |
| 5681 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 5682 | 0, |
| 5683 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5684 | 0, |
| 5685 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5686 | ((double)INT64_MIN) + 1 , |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5687 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5688 | }, |
| 5689 | { |
| 5690 | "largest negative int that fits in int64_t", |
| 5691 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 5692 | INT64_MIN, |
| 5693 | QCBOR_SUCCESS, |
| 5694 | 0, |
| 5695 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5696 | (double)INT64_MIN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5697 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5698 | }, |
| 5699 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5700 | "negative bignum -1", |
| 5701 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 5702 | -1, |
| 5703 | QCBOR_SUCCESS, |
| 5704 | 0, |
| 5705 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5706 | -1.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5707 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5708 | }, |
| 5709 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5710 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5711 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5712 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 5713 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5714 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5715 | 257000, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5716 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5717 | 257000.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5718 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5719 | }, |
| 5720 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5721 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5722 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5723 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5724 | -2064, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5725 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5726 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5727 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5728 | -2064.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5729 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5730 | }, |
| 5731 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5732 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5733 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5734 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 5735 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5736 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5737 | 2056, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5738 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5739 | 2056.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5740 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5741 | }, |
| 5742 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5743 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5744 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 5745 | 0, |
| 5746 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5747 | 0, |
| 5748 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5749 | -18446744073709551617.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5750 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5751 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5752 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5753 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5754 | "Positive bignum 0x01020304 indefinite length string", |
| 5755 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 5756 | 0x01020304, |
| 5757 | QCBOR_SUCCESS, |
| 5758 | 0x01020304, |
| 5759 | QCBOR_SUCCESS, |
| 5760 | 16909060.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5761 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5762 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5763 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5764 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5765 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5766 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5767 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 5768 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5769 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5770 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5771 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5772 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5773 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5774 | }, |
| 5775 | { |
| 5776 | "big float [9223372036854775806, 9223372036854775806]", |
| 5777 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 5778 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 5779 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5780 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5781 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5782 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5783 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5784 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5785 | }, |
| 5786 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5787 | "Big float 3 * 2^^2", |
| 5788 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 5789 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5790 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5791 | 12, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5792 | EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS), |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5793 | 12.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5794 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5795 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5796 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5797 | "Positive integer 18446744073709551615", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5798 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5799 | 0, |
| 5800 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5801 | 18446744073709551615ULL, |
| 5802 | QCBOR_SUCCESS, |
| 5803 | 18446744073709551615.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5804 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5805 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5806 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5807 | "Positive bignum 0xffff", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5808 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 5809 | 65536-1, |
| 5810 | QCBOR_SUCCESS, |
| 5811 | 0xffff, |
| 5812 | QCBOR_SUCCESS, |
| 5813 | 65535.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5814 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5815 | }, |
| 5816 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5817 | "Postive integer 0", |
| 5818 | {(uint8_t[]){0x0}, 1}, |
| 5819 | 0LL, |
| 5820 | QCBOR_SUCCESS, |
| 5821 | 0ULL, |
| 5822 | QCBOR_SUCCESS, |
| 5823 | 0.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5824 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5825 | }, |
| 5826 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5827 | "Negative integer -18446744073709551616", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5828 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 5829 | -9223372036854775807-1, // INT64_MIN |
| 5830 | QCBOR_SUCCESS, |
| 5831 | 0ULL, |
| 5832 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5833 | -9223372036854775808.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5834 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5835 | }, |
| 5836 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5837 | "Double Floating point value 100.3", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5838 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 5839 | 100L, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5840 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5841 | 100ULL, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5842 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5843 | 100.3, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5844 | FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5845 | }, |
| 5846 | { |
| 5847 | "Floating point value NaN 0xfa7fc00000", |
| 5848 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 5849 | 0, |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5850 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5851 | 0, |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5852 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5853 | NAN, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5854 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5855 | }, |
| 5856 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5857 | "half-precision Floating point value -4", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5858 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5859 | // Normal case with all enabled. |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5860 | -4, |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5861 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5862 | 0, |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5863 | FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5864 | -4.0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5865 | FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5866 | }, |
| 5867 | { |
| 5868 | "Decimal fraction 3/10", |
| 5869 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 5870 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5871 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5872 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5873 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5874 | 0.30000000000000004, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5875 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5876 | }, |
| 5877 | { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5878 | "+inifinity single precision", |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5879 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5880 | 0, |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5881 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION), |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5882 | 0, |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5883 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5884 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5885 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5886 | }, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5887 | |
| 5888 | { |
| 5889 | "extreme pos bignum", |
| 5890 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 5891 | // 50 rows of 8 is 400 digits. |
| 5892 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5893 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5894 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5895 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5896 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5897 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5898 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5899 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5900 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5901 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5902 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5903 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5904 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5905 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5906 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5907 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5908 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5909 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5910 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5911 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5912 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5913 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5914 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5915 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5916 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5917 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5918 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5919 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5920 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5921 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5922 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5923 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5924 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5925 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5926 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5927 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5928 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5929 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5930 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5931 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5932 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5933 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5934 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5935 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5936 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5937 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5938 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5939 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5940 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5941 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
| 5942 | 404}, |
| 5943 | 0, |
| 5944 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5945 | 0, |
| 5946 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5947 | INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5948 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS), |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5949 | }, |
| 5950 | |
| 5951 | { |
| 5952 | "extreme neg bignum", |
| 5953 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 5954 | // 50 rows of 8 is 400 digits. |
| 5955 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5956 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5957 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5958 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5959 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5960 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5961 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5962 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5963 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5964 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5965 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5966 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5967 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5968 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5969 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5970 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5971 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5972 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5973 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5974 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5975 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5976 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5977 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5978 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5979 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5980 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5981 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5982 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5983 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5984 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5985 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5986 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5987 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5988 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5989 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5990 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5991 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5992 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5993 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5994 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5995 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5996 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5997 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5998 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5999 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6000 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6001 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6002 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6003 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 6004 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
| 6005 | 404}, |
| 6006 | 0, |
| 6007 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6008 | 0, |
| 6009 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6010 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6011 | FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 6012 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6013 | |
| 6014 | { |
| 6015 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 6016 | {(uint8_t[]){ |
| 6017 | 0xC5, 0x82, |
| 6018 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6019 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6020 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6021 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6022 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6023 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6024 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6025 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6026 | }, |
| 6027 | |
| 6028 | { |
| 6029 | "bigfloat that evaluates to -INFINITY", |
| 6030 | {(uint8_t[]){ |
| 6031 | 0xC5, 0x82, |
| 6032 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6033 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6034 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6035 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6036 | 0, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6037 | EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION), |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6038 | -INFINITY, |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6039 | FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS)) |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6040 | }, |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6041 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6042 | |
| 6043 | |
| 6044 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6045 | |
| 6046 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 6047 | { |
| 6048 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 6049 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6050 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 6051 | return 1; |
| 6052 | } |
| 6053 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6054 | (void)Pool; |
| 6055 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6056 | return 0; |
| 6057 | } |
| 6058 | |
| 6059 | |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6060 | int32_t IntegerConvertTest() |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6061 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6062 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 6063 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6064 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6065 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 6066 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6067 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6068 | // Set up the decoding context including a memory pool so that |
| 6069 | // indefinite length items can be checked |
| 6070 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6071 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6072 | |
| 6073 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6074 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6075 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6076 | } |
| 6077 | |
| 6078 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6079 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6080 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6081 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6082 | } |
| 6083 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6084 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6085 | } |
| 6086 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6087 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6088 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6089 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6090 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6091 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6092 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6093 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6094 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6095 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6096 | } |
| 6097 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6098 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6099 | } |
| 6100 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6101 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6102 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6103 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6104 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6105 | |
| 6106 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6107 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6108 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6109 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6110 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6111 | } |
| 6112 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 6113 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6114 | // NaN's can't be compared for equality. A NaN is |
| 6115 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6116 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6117 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6118 | } |
| 6119 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6120 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6121 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6122 | } |
| 6123 | } |
| 6124 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6125 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6126 | } |
| 6127 | |
| 6128 | return 0; |
| 6129 | } |
| 6130 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6131 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6132 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6133 | int32_t CBORTestIssue134() |
| 6134 | { |
| 6135 | QCBORDecodeContext DCtx; |
| 6136 | QCBORItem Item; |
| 6137 | QCBORError uCBORError; |
| 6138 | const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6139 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6140 | QCBORDecode_Init(&DCtx, |
| 6141 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134), |
| 6142 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6143 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6144 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
| 6145 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 6146 | |
| 6147 | do { |
| 6148 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6149 | } while (QCBOR_SUCCESS == uCBORError); |
| 6150 | |
| 6151 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6152 | |
Laurence Lundblade | 11ea361 | 2022-07-01 13:26:23 -0700 | [diff] [blame] | 6153 | return (int32_t)uCBORError; |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 6154 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6155 | |
Laurence Lundblade | a875850 | 2022-05-15 17:57:46 -0700 | [diff] [blame] | 6156 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6157 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6158 | int32_t CBORSequenceDecodeTests(void) |
| 6159 | { |
| 6160 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6161 | QCBORItem Item; |
| 6162 | QCBORError uCBORError; |
| 6163 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6164 | |
| 6165 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6166 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6167 | // The input for the date test happens to be a sequence so it |
| 6168 | // is reused. It is a sequence because it doesn't start as |
| 6169 | // an array or map. |
| 6170 | QCBORDecode_Init(&DCtx, |
| 6171 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 6172 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6173 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6174 | // Get the first item |
| 6175 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6176 | if(uCBORError != QCBOR_SUCCESS) { |
| 6177 | return 1; |
| 6178 | } |
| 6179 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING) { |
| 6180 | return 2; |
| 6181 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6182 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6183 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6184 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
| 6185 | uConsumed != 12) { |
| 6186 | return 102; |
| 6187 | } |
| 6188 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6189 | // Get a second item |
| 6190 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6191 | if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) { |
| 6192 | return 66; |
| 6193 | } |
| 6194 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6195 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6196 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
| 6197 | uConsumed != 14) { |
| 6198 | return 102; |
| 6199 | } |
| 6200 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6201 | // Get a third item |
| 6202 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6203 | if(uCBORError != QCBOR_SUCCESS) { |
| 6204 | return 2; |
| 6205 | } |
| 6206 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 6207 | return 3; |
| 6208 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6209 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6210 | // A sequence can have stuff at the end that may |
| 6211 | // or may not be valid CBOR. The protocol decoder knows |
| 6212 | // when to stop by definition of the protocol, not |
| 6213 | // when the top-level map or array is ended. |
| 6214 | // Finish still has to be called to know that |
| 6215 | // maps and arrays (if there were any) were closed |
| 6216 | // off correctly. When called like this it |
| 6217 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 6218 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6219 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 6220 | return 4; |
| 6221 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6222 | |
| 6223 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6224 | // --- Test an empty input ---- |
| 6225 | uint8_t empty[1]; |
| 6226 | UsefulBufC Empty = {empty, 0}; |
| 6227 | QCBORDecode_Init(&DCtx, |
| 6228 | Empty, |
| 6229 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6230 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6231 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6232 | if(uCBORError != QCBOR_SUCCESS) { |
| 6233 | return 5; |
| 6234 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6235 | |
| 6236 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6237 | // --- Sequence with unclosed indefinite length array --- |
| 6238 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6239 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6240 | QCBORDecode_Init(&DCtx, |
| 6241 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 6242 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6243 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6244 | // Get the first item |
| 6245 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6246 | if(uCBORError != QCBOR_SUCCESS) { |
| 6247 | return 7; |
| 6248 | } |
| 6249 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6250 | return 8; |
| 6251 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6252 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6253 | // Get a second item |
| 6254 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6255 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6256 | if(uCBORError != QCBOR_SUCCESS) { |
| 6257 | return 9; |
| 6258 | } |
| 6259 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6260 | return 10; |
| 6261 | } |
| 6262 | |
| 6263 | // Try to finish before consuming all bytes to confirm |
| 6264 | // that the still-open error is returned. |
| 6265 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6266 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6267 | return 11; |
| 6268 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6269 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6270 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6271 | return 20; |
| 6272 | } |
| 6273 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6274 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6275 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6276 | // --- Sequence with a closed indefinite length array --- |
| 6277 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6278 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6279 | QCBORDecode_Init(&DCtx, |
| 6280 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 6281 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6282 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6283 | // Get the first item |
| 6284 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6285 | if(uCBORError != QCBOR_SUCCESS) { |
| 6286 | return 12; |
| 6287 | } |
| 6288 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6289 | return 13; |
| 6290 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6291 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6292 | // Get a second item |
| 6293 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6294 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6295 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6296 | if(uCBORError != QCBOR_SUCCESS) { |
| 6297 | return 14; |
| 6298 | } |
| 6299 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6300 | return 15; |
| 6301 | } |
| 6302 | |
| 6303 | // Try to finish before consuming all bytes to confirm |
| 6304 | // that the still-open error is returned. |
| 6305 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6306 | if(uCBORError != QCBOR_SUCCESS) { |
| 6307 | return 16; |
| 6308 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6309 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6310 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6311 | return 20; |
| 6312 | } |
| 6313 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6314 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6315 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6316 | return 0; |
| 6317 | } |
| 6318 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6319 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 6320 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6321 | int32_t IntToTests() |
| 6322 | { |
| 6323 | int nErrCode; |
| 6324 | int32_t n32; |
| 6325 | int16_t n16; |
| 6326 | int8_t n8; |
| 6327 | uint32_t u32; |
| 6328 | uint16_t u16; |
| 6329 | uint8_t u8; |
| 6330 | uint64_t u64; |
| 6331 | |
| 6332 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 6333 | if(nErrCode == -1 || n32 != 1) { |
| 6334 | return 1; |
| 6335 | } |
| 6336 | |
| 6337 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 6338 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 6339 | return 2; |
| 6340 | } |
| 6341 | |
| 6342 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 6343 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 6344 | return 3; |
| 6345 | } |
| 6346 | |
| 6347 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 6348 | if(nErrCode != -1) { |
| 6349 | return 4; |
| 6350 | } |
| 6351 | |
| 6352 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 6353 | if(nErrCode != -1) { |
| 6354 | return 5; |
| 6355 | } |
| 6356 | |
| 6357 | |
| 6358 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 6359 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 6360 | return 6; |
| 6361 | } |
| 6362 | |
| 6363 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 6364 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 6365 | return 7; |
| 6366 | } |
| 6367 | |
| 6368 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 6369 | if(nErrCode == -1 || n16 != 1) { |
| 6370 | return 8; |
| 6371 | } |
| 6372 | |
| 6373 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 6374 | if(nErrCode != -1) { |
| 6375 | return 9; |
| 6376 | } |
| 6377 | |
| 6378 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 6379 | if(nErrCode != -1) { |
| 6380 | return 10; |
| 6381 | } |
| 6382 | |
| 6383 | |
| 6384 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 6385 | if(nErrCode == -1 || n8 != 1) { |
| 6386 | return 11; |
| 6387 | } |
| 6388 | |
| 6389 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 6390 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 6391 | return 12; |
| 6392 | } |
| 6393 | |
| 6394 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 6395 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 6396 | return 13; |
| 6397 | } |
| 6398 | |
| 6399 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 6400 | if(nErrCode != -1) { |
| 6401 | return 14; |
| 6402 | } |
| 6403 | |
| 6404 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 6405 | if(nErrCode != -1) { |
| 6406 | return 15; |
| 6407 | } |
| 6408 | |
| 6409 | |
| 6410 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 6411 | if(nErrCode == -1 || u32 != 1) { |
| 6412 | return 16; |
| 6413 | } |
| 6414 | |
| 6415 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 6416 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 6417 | return 17; |
| 6418 | } |
| 6419 | |
| 6420 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 6421 | if(nErrCode == -1 || u32 != 0) { |
| 6422 | return 18; |
| 6423 | } |
| 6424 | |
| 6425 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 6426 | if(nErrCode != -1) { |
| 6427 | return 19; |
| 6428 | } |
| 6429 | |
| 6430 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 6431 | if(nErrCode != -1) { |
| 6432 | return 20; |
| 6433 | } |
| 6434 | |
| 6435 | |
| 6436 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 6437 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 6438 | return 21; |
| 6439 | } |
| 6440 | |
| 6441 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 6442 | if(nErrCode == -1 || u16 != 0) { |
| 6443 | return 22; |
| 6444 | } |
| 6445 | |
| 6446 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 6447 | if(nErrCode == -1 || u16 != 1) { |
| 6448 | return 23; |
| 6449 | } |
| 6450 | |
| 6451 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 6452 | if(nErrCode != -1) { |
| 6453 | return 24; |
| 6454 | } |
| 6455 | |
| 6456 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 6457 | if(nErrCode != -1) { |
| 6458 | return 25; |
| 6459 | } |
| 6460 | |
| 6461 | |
| 6462 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 6463 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 6464 | return 26; |
| 6465 | } |
| 6466 | |
| 6467 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 6468 | if(nErrCode == -1 || u8 != 0) { |
| 6469 | return 27; |
| 6470 | } |
| 6471 | |
| 6472 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 6473 | if(nErrCode == -1 || u8 != 1) { |
| 6474 | return 28; |
| 6475 | } |
| 6476 | |
| 6477 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 6478 | if(nErrCode != -1) { |
| 6479 | return 29; |
| 6480 | } |
| 6481 | |
| 6482 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 6483 | if(nErrCode != -1) { |
| 6484 | return 30; |
| 6485 | } |
| 6486 | |
| 6487 | |
| 6488 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 6489 | if(nErrCode == -1 || u64 != 1) { |
| 6490 | return 31; |
| 6491 | } |
| 6492 | |
| 6493 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 6494 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 6495 | return 32; |
| 6496 | } |
| 6497 | |
| 6498 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 6499 | if(nErrCode == -1 || u64 != 0) { |
| 6500 | return 33; |
| 6501 | } |
| 6502 | |
| 6503 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 6504 | if(nErrCode != -1) { |
| 6505 | return 34; |
| 6506 | } |
| 6507 | |
| 6508 | return 0; |
| 6509 | } |
| 6510 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6511 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6512 | |
| 6513 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6514 | /* |
| 6515 | A sequence with |
| 6516 | A wrapping bstr |
| 6517 | containing a map |
| 6518 | 1 |
| 6519 | 2 |
| 6520 | A wrapping bstr |
| 6521 | containing an array |
| 6522 | 3 |
| 6523 | wrapping bstr |
| 6524 | 4 |
| 6525 | 5 |
| 6526 | 6 |
| 6527 | array |
| 6528 | 7 |
| 6529 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6530 | */ |
| 6531 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6532 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6533 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6534 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6535 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6536 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6537 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6538 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6539 | |
| 6540 | QCBOREncode_BstrWrap(&EC); |
| 6541 | QCBOREncode_OpenMap(&EC); |
| 6542 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 6543 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 6544 | QCBOREncode_CloseMap(&EC); |
| 6545 | QCBOREncode_BstrWrap(&EC); |
| 6546 | QCBOREncode_OpenArray(&EC); |
| 6547 | QCBOREncode_AddInt64(&EC, 3); |
| 6548 | QCBOREncode_BstrWrap(&EC); |
| 6549 | QCBOREncode_AddInt64(&EC, 4); |
| 6550 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6551 | QCBOREncode_AddInt64(&EC, 5); |
| 6552 | QCBOREncode_CloseArray(&EC); |
| 6553 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6554 | QCBOREncode_AddInt64(&EC, 6); |
| 6555 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6556 | QCBOREncode_OpenArray(&EC); |
| 6557 | QCBOREncode_AddInt64(&EC, 7); |
| 6558 | QCBOREncode_AddInt64(&EC, 8); |
| 6559 | QCBOREncode_CloseArray(&EC); |
| 6560 | |
| 6561 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 6562 | if(uErr) { |
| 6563 | Encoded = NULLUsefulBufC; |
| 6564 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6565 | |
| 6566 | return Encoded; |
| 6567 | } |
| 6568 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6569 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6570 | static const uint8_t spBreakInByteString[] = { |
| 6571 | 0x41, 0xff |
| 6572 | }; |
| 6573 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6574 | |
| 6575 | int32_t EnterBstrTest() |
| 6576 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 6577 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6578 | |
| 6579 | QCBORDecodeContext DC; |
| 6580 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6581 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6582 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6583 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6584 | |
| 6585 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6586 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6587 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6588 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 6589 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6590 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6591 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6592 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6593 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6594 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6595 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6596 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6597 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6598 | QCBORDecode_ExitArray(&DC); |
| 6599 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6600 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6601 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6602 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6603 | QCBORDecode_GetInt64(&DC, &n7); |
| 6604 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6605 | QCBORDecode_ExitArray(&DC); |
| 6606 | |
| 6607 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6608 | if(uErr) { |
| 6609 | return (int32_t)uErr; |
| 6610 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6611 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6612 | |
| 6613 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 6614 | * Successful because no items are fetched from byte string. |
| 6615 | */ |
| 6616 | QCBORDecode_Init(&DC, |
| 6617 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 6618 | 0); |
| 6619 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6620 | uErr = QCBORDecode_GetError(&DC); |
| 6621 | if(uErr) { |
| 6622 | return 100 + (int32_t)uErr; |
| 6623 | } |
| 6624 | |
| 6625 | QCBORDecode_ExitBstrWrapped(&DC); |
| 6626 | uErr = QCBORDecode_GetError(&DC); |
| 6627 | if(uErr) { |
| 6628 | return 200 + (int32_t)uErr; |
| 6629 | } |
| 6630 | |
| 6631 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 6632 | * It fails because there should be no break. |
| 6633 | */ |
| 6634 | QCBORDecode_Init(&DC, |
| 6635 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 6636 | 0); |
| 6637 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6638 | QCBORItem Item; |
| 6639 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 6640 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 6641 | return 300 + (int32_t)uErr; |
| 6642 | } |
| 6643 | |
| 6644 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6645 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6646 | |
| 6647 | |
| 6648 | |
| 6649 | |
| 6650 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6651 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6652 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6653 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6654 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6655 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 6656 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 6657 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6658 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6659 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6660 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 6661 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 6662 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6663 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6664 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6665 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6666 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6667 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6668 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6669 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6670 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6671 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6672 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6673 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6674 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6675 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 6676 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6677 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6678 | 21, |
| 6679 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 6680 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 6681 | |
| 6682 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6683 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6684 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6685 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6686 | |
| 6687 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6688 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6689 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6690 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6691 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6692 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6693 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6694 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6695 | |
| 6696 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6697 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6698 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6699 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6700 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6701 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6702 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 6703 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6704 | |
| 6705 | 0x18, 0x33, |
| 6706 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 6707 | |
| 6708 | // MIME |
| 6709 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6710 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 6711 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 6712 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6713 | |
| 6714 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6715 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6716 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6717 | |
| 6718 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6719 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 6720 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 6721 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6722 | |
| 6723 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6724 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6725 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6726 | |
| 6727 | // UUID |
| 6728 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6729 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 6730 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6731 | |
| 6732 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6733 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 6734 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6735 | }; |
| 6736 | |
| 6737 | int32_t DecodeTaggedTypeTests() |
| 6738 | { |
| 6739 | QCBORDecodeContext DC; |
| 6740 | QCBORError uErr; |
| 6741 | |
| 6742 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 6743 | |
| 6744 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6745 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6746 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6747 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6748 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6749 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6750 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 6751 | return 1; |
| 6752 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6753 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6754 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6755 | return 2; |
| 6756 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6757 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6758 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6759 | return 3; |
| 6760 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6761 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6762 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6763 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6764 | return 4; |
| 6765 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6766 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6767 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6768 | return 5; |
| 6769 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6770 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6771 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6772 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6773 | bNeg != false) { |
| 6774 | return 10; |
| 6775 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6776 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6777 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6778 | bNeg != true) { |
| 6779 | return 11; |
| 6780 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6781 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6782 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6783 | return 12; |
| 6784 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6785 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6786 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6787 | return 13; |
| 6788 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6789 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6790 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6791 | return 14; |
| 6792 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6793 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6794 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6795 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6796 | return 20; |
| 6797 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6798 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6799 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6800 | return 21; |
| 6801 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6802 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6803 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6804 | return 22; |
| 6805 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6806 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6807 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6808 | return 23; |
| 6809 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6810 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6811 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6812 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6813 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6814 | return 30; |
| 6815 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6816 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6817 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6818 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6819 | return 31; |
| 6820 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6821 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6822 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6823 | return 32; |
| 6824 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6825 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6826 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6827 | return 33; |
| 6828 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6829 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6830 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6831 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6832 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6833 | return 40; |
| 6834 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6835 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6836 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6837 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6838 | return 41; |
| 6839 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6840 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6841 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6842 | return 42; |
| 6843 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6844 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6845 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6846 | return 43; |
| 6847 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6848 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6849 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6850 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6851 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6852 | return 50; |
| 6853 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6854 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6855 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6856 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6857 | return 51; |
| 6858 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6859 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6860 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6861 | return 52; |
| 6862 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6863 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6864 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6865 | return 53; |
| 6866 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6867 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6868 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6869 | // MIME |
| 6870 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6871 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6872 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6873 | bIsNot7Bit == true) { |
| 6874 | return 60; |
| 6875 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6876 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6877 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6878 | bIsNot7Bit == true) { |
| 6879 | return 61; |
| 6880 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6881 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6882 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6883 | bIsNot7Bit == false) { |
| 6884 | return 62; |
| 6885 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6886 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6887 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6888 | bIsNot7Bit == false) { |
| 6889 | return 63; |
| 6890 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6891 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6892 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6893 | return 64; |
| 6894 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6895 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6896 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6897 | return 65; |
| 6898 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6899 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6900 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6901 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6902 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6903 | return 70; |
| 6904 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6905 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 6906 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6907 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6908 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6909 | return 71; |
| 6910 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6911 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6912 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6913 | return 72; |
| 6914 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6915 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6916 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6917 | return 73; |
| 6918 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6919 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6920 | // Improvement: add some more error test cases |
| 6921 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6922 | QCBORDecode_ExitMap(&DC); |
| 6923 | |
| 6924 | uErr = QCBORDecode_Finish(&DC); |
| 6925 | if(uErr != QCBOR_SUCCESS) { |
| 6926 | return 100; |
| 6927 | } |
| 6928 | |
| 6929 | return 0; |
| 6930 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6931 | |
| 6932 | |
| 6933 | |
| 6934 | |
| 6935 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6936 | [ |
| 6937 | "aaaaaaaaaa", |
| 6938 | {} |
| 6939 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6940 | */ |
| 6941 | static const uint8_t spTooLarge1[] = { |
| 6942 | 0x9f, |
| 6943 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6944 | 0xa0, |
| 6945 | 0xff |
| 6946 | }; |
| 6947 | |
| 6948 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6949 | [ |
| 6950 | { |
| 6951 | 0: "aaaaaaaaaa" |
| 6952 | } |
| 6953 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6954 | */ |
| 6955 | static const uint8_t spTooLarge2[] = { |
| 6956 | 0x9f, |
| 6957 | 0xa1, |
| 6958 | 0x00, |
| 6959 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6960 | 0xff |
| 6961 | }; |
| 6962 | |
| 6963 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6964 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6965 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6966 | { |
| 6967 | 0: "aaaaaaaaaa" |
| 6968 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6969 | */ |
| 6970 | static const uint8_t spTooLarge3[] = { |
| 6971 | 0x4d, |
| 6972 | 0xa1, |
| 6973 | 0x00, |
| 6974 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6975 | }; |
| 6976 | |
| 6977 | int32_t TooLargeInputTest(void) |
| 6978 | { |
| 6979 | QCBORDecodeContext DC; |
| 6980 | QCBORError uErr; |
| 6981 | UsefulBufC String; |
| 6982 | |
| 6983 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 6984 | // to 10 There's not really any way to test this error |
| 6985 | // condition. The error condition is not complex, so setting |
| 6986 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 6987 | |
| 6988 | // The input CBOR is only too large because the |
| 6989 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 6990 | // |
| 6991 | // This test is disabled for the normal test runs because of the |
| 6992 | // special build requirement. |
| 6993 | |
| 6994 | |
| 6995 | // Tests the start of a map being too large |
| 6996 | 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] | 6997 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6998 | QCBORDecode_GetTextString(&DC, &String); |
| 6999 | uErr = QCBORDecode_GetError(&DC); |
| 7000 | if(uErr != QCBOR_SUCCESS) { |
| 7001 | return 1; |
| 7002 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7003 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7004 | uErr = QCBORDecode_GetError(&DC); |
| 7005 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7006 | return 2; |
| 7007 | } |
| 7008 | |
| 7009 | // Tests the end of a map being too large |
| 7010 | 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] | 7011 | QCBORDecode_EnterArray(&DC, NULL); |
| 7012 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 7013 | uErr = QCBORDecode_GetError(&DC); |
| 7014 | if(uErr != QCBOR_SUCCESS) { |
| 7015 | return 3; |
| 7016 | } |
| 7017 | QCBORDecode_ExitMap(&DC); |
| 7018 | uErr = QCBORDecode_GetError(&DC); |
| 7019 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7020 | return 4; |
| 7021 | } |
| 7022 | |
| 7023 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 7024 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 7025 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7026 | uErr = QCBORDecode_GetError(&DC); |
| 7027 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7028 | return 5; |
| 7029 | } |
| 7030 | |
| 7031 | return 0; |
| 7032 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7033 | |
| 7034 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7035 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7036 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7037 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7038 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7039 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 7040 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 7041 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 7042 | 0x03, |
| 7043 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 7044 | 0xc3, |
| 7045 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7046 | }; |
| 7047 | |
| 7048 | int32_t SpiffyIndefiniteLengthStringsTests() |
| 7049 | { |
| 7050 | QCBORDecodeContext DCtx; |
| 7051 | |
| 7052 | QCBORDecode_Init(&DCtx, |
| 7053 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 7054 | QCBOR_DECODE_MODE_NORMAL); |
| 7055 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7056 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7057 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 7058 | |
| 7059 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7060 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7061 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
| 7062 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7063 | return 1; |
| 7064 | } |
| 7065 | |
| 7066 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 7067 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 7068 | return 2; |
| 7069 | } |
| 7070 | |
| 7071 | uint64_t uInt; |
| 7072 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 7073 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7074 | return 3; |
| 7075 | } |
| 7076 | if(uInt != 3) { |
| 7077 | return 4; |
| 7078 | } |
| 7079 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7080 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7081 | double uDouble; |
| 7082 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 7083 | "label2", |
| 7084 | 0xff, |
| 7085 | &uDouble); |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7086 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7087 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7088 | return 5; |
| 7089 | } |
| 7090 | if(uDouble != -16777474) { |
| 7091 | return 6; |
| 7092 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7093 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7094 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 7095 | return 7; |
| 7096 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7097 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 7098 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7099 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7100 | |
| 7101 | QCBORDecode_ExitMap(&DCtx); |
| 7102 | |
| 7103 | if(QCBORDecode_Finish(&DCtx)) { |
| 7104 | return 99; |
| 7105 | } |
| 7106 | |
| 7107 | return 0; |
| 7108 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7109 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7110 | |
| 7111 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7112 | /* |
| 7113 | * An array of an integer and an array. The second array contains |
| 7114 | * a bstr-wrapped map. |
| 7115 | * |
| 7116 | * [7, [h'A36D6669... (see next lines) 73']] |
| 7117 | * |
| 7118 | * {"first integer": 42, |
| 7119 | * "an array of two strings": ["string1", "string2"], |
| 7120 | * "map in a map": |
| 7121 | * { "bytes 1": h'78787878', |
| 7122 | * "bytes 2": h'79797979', |
| 7123 | * "another int": 98, |
| 7124 | * "text 2": "lies, damn lies and statistics" |
| 7125 | * } |
| 7126 | * } |
| 7127 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7128 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7129 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 7130 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 7131 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7132 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7133 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7134 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7135 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7136 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7137 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7138 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7139 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7140 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7141 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7142 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7143 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7144 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7145 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7146 | 0x73 |
| 7147 | }; |
| 7148 | |
| 7149 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7150 | |
| 7151 | /* As above, but the arrays are indefinite length */ |
| 7152 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 7153 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 7154 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7155 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7156 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7157 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7158 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7159 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7160 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7161 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7162 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7163 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7164 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7165 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7166 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7167 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7168 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7169 | 0x73, |
| 7170 | 0xff, 0xff |
| 7171 | }; |
| 7172 | #endif |
| 7173 | |
| 7174 | |
| 7175 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 7176 | |
| 7177 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7178 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 7179 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7180 | |
| 7181 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7182 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 7183 | 0x81, |
| 7184 | 0xd8, 0x18, |
| 7185 | 0x5f, |
| 7186 | 0x41, 0x83, |
| 7187 | 0x41, 0x18, |
| 7188 | 0x43, 0x2A, 0x18, 0x2B, |
| 7189 | 0x42, 0x18, 0x2C, |
| 7190 | 0xff |
| 7191 | }; |
| 7192 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7193 | |
| 7194 | |
| 7195 | int32_t PeekAndRewindTest() |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7196 | { |
| 7197 | QCBORItem Item; |
| 7198 | QCBORError nCBORError; |
| 7199 | QCBORDecodeContext DCtx; |
| 7200 | |
| 7201 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7202 | |
| 7203 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7204 | return 100+(int32_t)nCBORError; |
| 7205 | } |
| 7206 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7207 | return 200; |
| 7208 | } |
| 7209 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 7210 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 7211 | if((nCBORError = QCBORDecode_GetError(&DCtx))) { |
| 7212 | return 150+(int32_t)nCBORError; |
| 7213 | } |
| 7214 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7215 | return 250; |
| 7216 | } |
| 7217 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7218 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7219 | return (int32_t)nCBORError; |
| 7220 | } |
| 7221 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7222 | return 300; |
| 7223 | } |
| 7224 | |
| 7225 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7226 | return 400 + (int32_t)nCBORError; |
| 7227 | } |
| 7228 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7229 | return 500; |
| 7230 | } |
| 7231 | |
| 7232 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7233 | return (int32_t)nCBORError; |
| 7234 | } |
| 7235 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7236 | return 600; |
| 7237 | } |
| 7238 | |
| 7239 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7240 | return 900 + (int32_t)nCBORError; |
| 7241 | } |
| 7242 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7243 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7244 | Item.val.int64 != 42 || |
| 7245 | Item.uDataAlloc || |
| 7246 | Item.uLabelAlloc || |
| 7247 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7248 | return 1000; |
| 7249 | } |
| 7250 | |
| 7251 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7252 | return 1100 + (int32_t)nCBORError; |
| 7253 | } |
| 7254 | |
| 7255 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7256 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7257 | Item.val.int64 != 42 || |
| 7258 | Item.uDataAlloc || |
| 7259 | Item.uLabelAlloc || |
| 7260 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7261 | return 1200; |
| 7262 | } |
| 7263 | |
| 7264 | |
| 7265 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7266 | return 1300 + (int32_t)nCBORError; |
| 7267 | } |
| 7268 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7269 | Item.uDataAlloc || |
| 7270 | Item.uLabelAlloc || |
| 7271 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7272 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7273 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7274 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7275 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7276 | |
| 7277 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7278 | return 1500 + (int32_t)nCBORError; |
| 7279 | } |
| 7280 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7281 | Item.uDataAlloc || |
| 7282 | Item.uLabelAlloc || |
| 7283 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7284 | return 1600; |
| 7285 | } |
| 7286 | |
| 7287 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7288 | return 1700 + (int32_t)nCBORError; |
| 7289 | } |
| 7290 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7291 | Item.uDataAlloc || |
| 7292 | Item.uLabelAlloc || |
| 7293 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7294 | return 1800; |
| 7295 | } |
| 7296 | |
| 7297 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7298 | return (int32_t)nCBORError; |
| 7299 | } |
| 7300 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7301 | Item.uDataAlloc || |
| 7302 | Item.uLabelAlloc || |
| 7303 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7304 | return 1900; |
| 7305 | } |
| 7306 | |
| 7307 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7308 | return (int32_t)nCBORError; |
| 7309 | } |
| 7310 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7311 | Item.uDataAlloc || |
| 7312 | Item.uLabelAlloc || |
| 7313 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7314 | return 2000; |
| 7315 | } |
| 7316 | |
| 7317 | |
| 7318 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7319 | return 2100 + (int32_t)nCBORError; |
| 7320 | } |
| 7321 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7322 | Item.uDataAlloc || |
| 7323 | Item.uLabelAlloc || |
| 7324 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 7325 | Item.uDataType != QCBOR_TYPE_MAP || |
| 7326 | Item.val.uCount != 4) { |
| 7327 | return 2100; |
| 7328 | } |
| 7329 | |
| 7330 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7331 | return 2200 + (int32_t)nCBORError; |
| 7332 | } |
| 7333 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7334 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 7335 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7336 | Item.uDataAlloc || |
| 7337 | Item.uLabelAlloc || |
| 7338 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 7339 | return 2300; |
| 7340 | } |
| 7341 | |
| 7342 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7343 | return 2400 + (int32_t)nCBORError; |
| 7344 | } |
| 7345 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7346 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7347 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7348 | Item.uDataAlloc || |
| 7349 | Item.uLabelAlloc || |
| 7350 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7351 | return 2500; |
| 7352 | } |
| 7353 | |
| 7354 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7355 | return 2600 + (int32_t)nCBORError; |
| 7356 | } |
| 7357 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7358 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7359 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7360 | Item.uDataAlloc || |
| 7361 | Item.uLabelAlloc || |
| 7362 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7363 | return 2700; |
| 7364 | } |
| 7365 | |
| 7366 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7367 | return 2800 + (int32_t)nCBORError; |
| 7368 | } |
| 7369 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7370 | Item.uDataAlloc || |
| 7371 | Item.uLabelAlloc || |
| 7372 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 7373 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7374 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7375 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7376 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7377 | |
| 7378 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7379 | return 3000 + (int32_t)nCBORError; |
| 7380 | } |
| 7381 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7382 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7383 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7384 | Item.uDataAlloc || |
| 7385 | Item.uLabelAlloc || |
| 7386 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7387 | return 3100; |
| 7388 | } |
| 7389 | |
| 7390 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7391 | return 3200 + (int32_t)nCBORError; |
| 7392 | } |
| 7393 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7394 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7395 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7396 | Item.uDataAlloc || |
| 7397 | Item.uLabelAlloc || |
| 7398 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7399 | return 3300; |
| 7400 | } |
| 7401 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 7402 | nCBORError = QCBORDecode_PeekNext(&DCtx, &Item); |
| 7403 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7404 | return 3300 + (int32_t)nCBORError; |
| 7405 | } |
| 7406 | |
| 7407 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 7408 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 7409 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7410 | return 3400 + (int32_t)nCBORError; |
| 7411 | } |
| 7412 | |
| 7413 | QCBORDecode_VPeekNext(&DCtx, &Item); |
| 7414 | nCBORError = QCBORDecode_GetError(&DCtx); |
| 7415 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7416 | return 3500 + (int32_t)nCBORError; |
| 7417 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7418 | |
| 7419 | |
| 7420 | // Rewind to top level after entering several maps |
| 7421 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7422 | |
| 7423 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7424 | return (int32_t)nCBORError; |
| 7425 | } |
| 7426 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 7427 | Item.val.uCount != 3) { |
| 7428 | return 400; |
| 7429 | } |
| 7430 | |
| 7431 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7432 | return 4000+(int32_t)nCBORError; |
| 7433 | } |
| 7434 | |
| 7435 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7436 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7437 | Item.val.int64 != 42 || |
| 7438 | Item.uDataAlloc || |
| 7439 | Item.uLabelAlloc || |
| 7440 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7441 | return 4100; |
| 7442 | } |
| 7443 | |
| 7444 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7445 | return 4100+(int32_t)nCBORError; |
| 7446 | } |
| 7447 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7448 | Item.uDataAlloc || |
| 7449 | Item.uLabelAlloc || |
| 7450 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7451 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7452 | Item.val.uCount != 2) { |
| 7453 | return 4200; |
| 7454 | } |
| 7455 | |
| 7456 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7457 | return 4200+(int32_t)nCBORError; |
| 7458 | } |
| 7459 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7460 | Item.uDataAlloc || |
| 7461 | Item.uLabelAlloc || |
| 7462 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7463 | return 4300; |
| 7464 | } |
| 7465 | |
| 7466 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7467 | return 4300+(int32_t)nCBORError; |
| 7468 | } |
| 7469 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7470 | Item.uDataAlloc || |
| 7471 | Item.uLabelAlloc || |
| 7472 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7473 | return 4400; |
| 7474 | } |
| 7475 | |
| 7476 | QCBORDecode_Rewind(&DCtx); |
| 7477 | |
| 7478 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7479 | return 4400+(int32_t)nCBORError; |
| 7480 | } |
| 7481 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 7482 | Item.val.uCount != 3) { |
| 7483 | return 4500; |
| 7484 | } |
| 7485 | |
| 7486 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7487 | return (int32_t)nCBORError; |
| 7488 | } |
| 7489 | |
| 7490 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7491 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7492 | Item.val.int64 != 42 || |
| 7493 | Item.uDataAlloc || |
| 7494 | Item.uLabelAlloc || |
| 7495 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7496 | return 4600; |
| 7497 | } |
| 7498 | |
| 7499 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7500 | return (int32_t)nCBORError; |
| 7501 | } |
| 7502 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7503 | Item.uDataAlloc || |
| 7504 | Item.uLabelAlloc || |
| 7505 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7506 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7507 | Item.val.uCount != 2) { |
| 7508 | return 4700; |
| 7509 | } |
| 7510 | |
| 7511 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7512 | return (int32_t)nCBORError; |
| 7513 | } |
| 7514 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7515 | Item.uDataAlloc || |
| 7516 | Item.uLabelAlloc || |
| 7517 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7518 | return 4800; |
| 7519 | } |
| 7520 | |
| 7521 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7522 | return 4900+(int32_t)nCBORError; |
| 7523 | } |
| 7524 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7525 | Item.uDataAlloc || |
| 7526 | Item.uLabelAlloc || |
| 7527 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7528 | return 5000; |
| 7529 | } |
| 7530 | |
| 7531 | |
| 7532 | // Rewind an entered map |
| 7533 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7534 | |
| 7535 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7536 | |
| 7537 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7538 | return 5100+(int32_t)nCBORError; |
| 7539 | } |
| 7540 | |
| 7541 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7542 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7543 | Item.val.int64 != 42 || |
| 7544 | Item.uDataAlloc || |
| 7545 | Item.uLabelAlloc || |
| 7546 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7547 | return 5200; |
| 7548 | } |
| 7549 | |
| 7550 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7551 | return 5200+(int32_t)nCBORError; |
| 7552 | } |
| 7553 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7554 | Item.uDataAlloc || |
| 7555 | Item.uLabelAlloc || |
| 7556 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7557 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7558 | Item.val.uCount != 2) { |
| 7559 | return -5300; |
| 7560 | } |
| 7561 | |
| 7562 | QCBORDecode_Rewind(&DCtx); |
| 7563 | |
| 7564 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7565 | return 5300+(int32_t)nCBORError; |
| 7566 | } |
| 7567 | |
| 7568 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7569 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7570 | Item.val.int64 != 42 || |
| 7571 | Item.uDataAlloc || |
| 7572 | Item.uLabelAlloc || |
| 7573 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7574 | return 5400; |
| 7575 | } |
| 7576 | |
| 7577 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7578 | return 5400+(int32_t)nCBORError; |
| 7579 | } |
| 7580 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7581 | Item.uDataAlloc || |
| 7582 | Item.uLabelAlloc || |
| 7583 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7584 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7585 | Item.val.uCount != 2) { |
| 7586 | return 5500; |
| 7587 | } |
| 7588 | |
| 7589 | |
| 7590 | // Rewind and entered array inside an entered map |
| 7591 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7592 | |
| 7593 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7594 | |
| 7595 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 7596 | |
| 7597 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7598 | return 5600+(int32_t)nCBORError; |
| 7599 | } |
| 7600 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7601 | Item.uDataAlloc || |
| 7602 | Item.uLabelAlloc || |
| 7603 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7604 | return 5700; |
| 7605 | } |
| 7606 | |
| 7607 | QCBORDecode_Rewind(&DCtx); |
| 7608 | |
| 7609 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7610 | return 5700+(int32_t)nCBORError; |
| 7611 | } |
| 7612 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7613 | Item.uDataAlloc || |
| 7614 | Item.uLabelAlloc || |
| 7615 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7616 | return 5800; |
| 7617 | } |
| 7618 | |
| 7619 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7620 | return (int32_t)nCBORError; |
| 7621 | } |
| 7622 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7623 | Item.uDataAlloc || |
| 7624 | Item.uLabelAlloc || |
| 7625 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7626 | return 5900; |
| 7627 | } |
| 7628 | |
| 7629 | QCBORDecode_Rewind(&DCtx); |
| 7630 | |
| 7631 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7632 | return 5900+(int32_t)nCBORError; |
| 7633 | } |
| 7634 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7635 | Item.uDataAlloc || |
| 7636 | Item.uLabelAlloc || |
| 7637 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7638 | return 6000; |
| 7639 | } |
| 7640 | |
| 7641 | |
| 7642 | // Rewind a byte string inside an array inside an array |
| 7643 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 7644 | |
| 7645 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7646 | |
| 7647 | uint64_t i; |
| 7648 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7649 | |
| 7650 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7651 | |
| 7652 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7653 | if(QCBORDecode_GetError(&DCtx)) { |
| 7654 | return 6100; |
| 7655 | } |
| 7656 | |
| 7657 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7658 | return (int32_t)nCBORError; |
| 7659 | } |
| 7660 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7661 | return 6200; |
| 7662 | } |
| 7663 | |
| 7664 | QCBORDecode_Rewind(&DCtx); |
| 7665 | |
| 7666 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7667 | return 6300+(int32_t)nCBORError; |
| 7668 | } |
| 7669 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7670 | return 6400; |
| 7671 | } |
| 7672 | |
| 7673 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7674 | // Rewind a byte string inside an indefinite-length array inside |
| 7675 | // indefinite-length array |
| 7676 | |
| 7677 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 7678 | |
| 7679 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7680 | |
| 7681 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7682 | |
| 7683 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7684 | |
| 7685 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7686 | if(QCBORDecode_GetError(&DCtx)) { |
| 7687 | return 6500; |
| 7688 | } |
| 7689 | |
| 7690 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7691 | return 6600+(int32_t)nCBORError; |
| 7692 | } |
| 7693 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7694 | return 6700; |
| 7695 | } |
| 7696 | |
| 7697 | QCBORDecode_Rewind(&DCtx); |
| 7698 | |
| 7699 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7700 | return 6800+(int32_t)nCBORError; |
| 7701 | } |
| 7702 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7703 | return 6900; |
| 7704 | } |
| 7705 | #endif |
| 7706 | |
| 7707 | // Rewind an empty map |
| 7708 | // [100, {}] |
| 7709 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 7710 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7711 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7712 | if(i != 100) { |
| 7713 | return 7010; |
| 7714 | } |
| 7715 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7716 | |
| 7717 | /* Do it 5 times to be sure multiple rewinds work */ |
| 7718 | for(int n = 0; n < 5; n++) { |
| 7719 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7720 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7721 | return 7000 + n; |
| 7722 | } |
| 7723 | QCBORDecode_Rewind(&DCtx); |
| 7724 | } |
| 7725 | QCBORDecode_ExitMap(&DCtx); |
| 7726 | QCBORDecode_Rewind(&DCtx); |
| 7727 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7728 | if(i != 100) { |
| 7729 | return 7010; |
| 7730 | } |
| 7731 | QCBORDecode_ExitArray(&DCtx); |
| 7732 | QCBORDecode_Rewind(&DCtx); |
| 7733 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7734 | i = 9; |
| 7735 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7736 | if(i != 100) { |
| 7737 | return 7020; |
| 7738 | } |
| 7739 | if(QCBORDecode_GetError(&DCtx)){ |
| 7740 | return 7030; |
| 7741 | } |
| 7742 | |
| 7743 | // Rewind an empty indefinite length map |
| 7744 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7745 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 7746 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7747 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7748 | if(i != 100) { |
| 7749 | return 7810; |
| 7750 | } |
| 7751 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7752 | |
| 7753 | /* Do it 5 times to be sure multiple rewinds work */ |
| 7754 | for(int n = 0; n < 5; n++) { |
| 7755 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7756 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7757 | return 7800 + n; |
| 7758 | } |
| 7759 | QCBORDecode_Rewind(&DCtx); |
| 7760 | } |
| 7761 | QCBORDecode_ExitMap(&DCtx); |
| 7762 | QCBORDecode_Rewind(&DCtx); |
| 7763 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7764 | if(i != 100) { |
| 7765 | return 7810; |
| 7766 | } |
| 7767 | QCBORDecode_ExitArray(&DCtx); |
| 7768 | QCBORDecode_Rewind(&DCtx); |
| 7769 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7770 | i = 9; |
| 7771 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7772 | if(i != 100) { |
| 7773 | return 7820; |
| 7774 | } |
| 7775 | if(QCBORDecode_GetError(&DCtx)){ |
| 7776 | return 7830; |
| 7777 | } |
| 7778 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7779 | |
| 7780 | // Rewind an indefnite length byte-string wrapped sequence |
| 7781 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7782 | QCBORDecode_Init(&DCtx, |
| 7783 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 7784 | 0); |
| 7785 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 7786 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 7787 | |
| 7788 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7789 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
| 7790 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 7791 | /* this is what happens when trying to enter |
| 7792 | indefinite-length byte string |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7793 | wrapped CBOR. Tolerate for now. Eventually it needs |
| 7794 | to be fixed so this works, but that is not simple. */ |
| 7795 | return 7300; |
| 7796 | } |
| 7797 | |
| 7798 | /* |
| 7799 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7800 | if(i != 42) { |
| 7801 | return 7110; |
| 7802 | } |
| 7803 | QCBORDecode_Rewind(&DCtx); |
| 7804 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7805 | if(i != 42) { |
| 7806 | return 7220; |
| 7807 | }*/ |
| 7808 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7809 | |
| 7810 | |
| 7811 | // Rewind an indefnite length byte-string wrapped sequence |
| 7812 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7813 | return 0; |
| 7814 | } |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 7815 | |
| 7816 | |
| 7817 | |
| 7818 | |
| 7819 | static const uint8_t spBooleansInMap[] = |
| 7820 | { |
| 7821 | 0xa1, 0x08, 0xf5 |
| 7822 | }; |
| 7823 | |
| 7824 | static const uint8_t spBooleansInMapWrongType[] = |
| 7825 | { |
| 7826 | 0xa1, 0x08, 0xf6 |
| 7827 | }; |
| 7828 | |
| 7829 | static const uint8_t spBooleansInMapNWF[] = |
| 7830 | { |
| 7831 | 0xa1, 0x08, 0x1a |
| 7832 | }; |
| 7833 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 7834 | static const uint8_t spNullInMap[] = |
| 7835 | { |
| 7836 | 0xa1, 0x08, 0xf6 |
| 7837 | }; |
| 7838 | |
| 7839 | static const uint8_t spUndefinedInMap[] = |
| 7840 | { |
| 7841 | 0xa1, 0x08, 0xf7 |
| 7842 | }; |
| 7843 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 7844 | |
| 7845 | int32_t BoolTest(void) |
| 7846 | { |
| 7847 | QCBORDecodeContext DCtx; |
| 7848 | bool b; |
| 7849 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 7850 | QCBORDecode_Init(&DCtx, |
| 7851 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 7852 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 7853 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7854 | QCBORDecode_GetBool(&DCtx, &b); |
| 7855 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 7856 | return 1; |
| 7857 | } |
| 7858 | |
| 7859 | QCBORDecode_GetBoolInMapN(&DCtx, 7, &b); |
| 7860 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 7861 | return 2; |
| 7862 | } |
| 7863 | |
| 7864 | QCBORDecode_GetBoolInMapN(&DCtx, 8, &b); |
| 7865 | if(QCBORDecode_GetAndResetError(&DCtx) || !b) { |
| 7866 | return 3; |
| 7867 | } |
| 7868 | |
| 7869 | |
| 7870 | QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b); |
| 7871 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) { |
| 7872 | return 4; |
| 7873 | } |
| 7874 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 7875 | QCBORDecode_Init(&DCtx, |
| 7876 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), |
| 7877 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 7878 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7879 | QCBORDecode_GetBool(&DCtx, &b); |
| 7880 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7881 | return 5; |
| 7882 | } |
| 7883 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 7884 | QCBORDecode_Init(&DCtx, |
| 7885 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 7886 | 0); |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 7887 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7888 | QCBORDecode_GetBool(&DCtx, &b); |
| 7889 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 7890 | return 6; |
| 7891 | } |
| 7892 | |
Laurence Lundblade | 8782dd3 | 2021-04-27 04:15:37 -0700 | [diff] [blame] | 7893 | |
| 7894 | QCBORDecode_Init(&DCtx, |
| 7895 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 7896 | 0); |
| 7897 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7898 | QCBORDecode_GetNull(&DCtx); |
| 7899 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7900 | return 7; |
| 7901 | } |
| 7902 | |
| 7903 | QCBORDecode_Init(&DCtx, |
| 7904 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 7905 | 0); |
| 7906 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7907 | QCBORDecode_GetNull(&DCtx); |
| 7908 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7909 | return 8; |
| 7910 | } |
| 7911 | |
| 7912 | QCBORDecode_Init(&DCtx, |
| 7913 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap), |
| 7914 | 0); |
| 7915 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7916 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 7917 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7918 | return 9; |
| 7919 | } |
| 7920 | |
| 7921 | QCBORDecode_Init(&DCtx, |
| 7922 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 7923 | 0); |
| 7924 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7925 | QCBORDecode_GetNullInMapN(&DCtx, 8); |
| 7926 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7927 | return 10; |
| 7928 | } |
| 7929 | |
| 7930 | QCBORDecode_Init(&DCtx, |
| 7931 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 7932 | 0); |
| 7933 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7934 | QCBORDecode_GetUndefined(&DCtx); |
| 7935 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 7936 | return 11; |
| 7937 | } |
| 7938 | |
| 7939 | QCBORDecode_Init(&DCtx, |
| 7940 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 7941 | 0); |
| 7942 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7943 | QCBORDecode_GetUndefined(&DCtx); |
| 7944 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7945 | return 12; |
| 7946 | } |
| 7947 | |
| 7948 | QCBORDecode_Init(&DCtx, |
| 7949 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 7950 | 0); |
| 7951 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7952 | QCBORDecode_GetUndefined(&DCtx); |
| 7953 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7954 | return 13; |
| 7955 | } |
| 7956 | |
| 7957 | QCBORDecode_Init(&DCtx, |
| 7958 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap), |
| 7959 | 0); |
| 7960 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7961 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 7962 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7963 | return 14; |
| 7964 | } |
| 7965 | |
| 7966 | QCBORDecode_Init(&DCtx, |
| 7967 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), |
| 7968 | 0); |
| 7969 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7970 | QCBORDecode_GetUndefinedInMapN(&DCtx, 8); |
| 7971 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 7972 | return 15; |
| 7973 | } |
| 7974 | |
| 7975 | QCBORDecode_Init(&DCtx, |
| 7976 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), |
| 7977 | 0); |
| 7978 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7979 | QCBORDecode_GetUndefined(&DCtx); |
| 7980 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) { |
| 7981 | return 15; |
| 7982 | } |
| 7983 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 7984 | return 0; |
| 7985 | } |