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 | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 3 | Copyright (c) 2018-2021, Laurence Lundblade. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 4 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following |
| 13 | disclaimer in the documentation and/or other materials provided |
| 14 | with the distribution. |
| 15 | * Neither the name of The Linux Foundation nor the names of its |
| 16 | contributors, nor the name "Laurence Lundblade" may be used to |
| 17 | endorse or promote products derived from this software without |
| 18 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 31 | =============================================================================*/ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 32 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 33 | #include "qcbor_decode_tests.h" |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 34 | #include "qcbor/qcbor_encode.h" |
| 35 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 36 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | d4728fd | 2018-12-17 15:15:56 -0800 | [diff] [blame] | 37 | #include <string.h> |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 38 | #include <math.h> // for fabs() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 39 | #include "not_well_formed_cbor.h" |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 40 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 41 | // Handy macro to compare a UsefulBuf to a C string |
| 42 | #define UsefulBufCompareToSZ(x, y) \ |
| 43 | UsefulBuf_Compare(x, UsefulBuf_FromSZ(y)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 44 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 45 | #ifdef PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 46 | #include <stdio.h> |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 47 | |
| 48 | static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 49 | { |
| 50 | if(szLabel) { |
| 51 | printf("%s ", szLabel); |
| 52 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 53 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 54 | size_t i; |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 55 | for(i = 0; i < Buf.len; i++) { |
| 56 | uint8_t Z = ((uint8_t *)Buf.ptr)[i]; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 57 | printf("%02x ", Z); |
| 58 | } |
| 59 | printf("\n"); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 60 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 61 | fflush(stdout); |
| 62 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 63 | #endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 64 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 65 | /* |
| 66 | [ |
| 67 | -9223372036854775808, |
| 68 | -4294967297, |
| 69 | -4294967296, |
| 70 | -4294967295, |
| 71 | -4294967294, |
| 72 | -2147483648, |
| 73 | -2147483647, |
| 74 | -65538, |
| 75 | -65537, |
| 76 | -65536, |
| 77 | -65535, |
| 78 | -65534, |
| 79 | -257, |
| 80 | -256, |
| 81 | -255, |
| 82 | -254, |
| 83 | -25, |
| 84 | -24, |
| 85 | -23, |
| 86 | -1, |
| 87 | 0, |
| 88 | 0, |
| 89 | 1, |
| 90 | 22, |
| 91 | 23, |
| 92 | 24, |
| 93 | 25, |
| 94 | 26, |
| 95 | 254, |
| 96 | 255, |
| 97 | 256, |
| 98 | 257, |
| 99 | 65534, |
| 100 | 65535, |
| 101 | 65536, |
| 102 | 65537, |
| 103 | 65538, |
| 104 | 2147483647, |
| 105 | 2147483647, |
| 106 | 2147483648, |
| 107 | 2147483649, |
| 108 | 4294967294, |
| 109 | 4294967295, |
| 110 | 4294967296, |
| 111 | 4294967297, |
| 112 | 9223372036854775807, |
| 113 | 18446744073709551615 |
| 114 | ] |
| 115 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 116 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 117 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 118 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 119 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 120 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 121 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 122 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 123 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 124 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 125 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 126 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 127 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 128 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 129 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 130 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 131 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 132 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 133 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 134 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 135 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 136 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 137 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 138 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 139 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 140 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 141 | 0xff, 0xff}; |
| 142 | |
| 143 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 144 | // return CBOR error or -1 if type of value doesn't match |
| 145 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 146 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 147 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 148 | QCBORItem Item; |
| 149 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 150 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 151 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 152 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 153 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 154 | return -1; |
| 155 | |
| 156 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 157 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 158 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 159 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 160 | return -1; |
| 161 | |
| 162 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 163 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 164 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 165 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 166 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 167 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 168 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 169 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 170 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 171 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 172 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 173 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 174 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 175 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 176 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 177 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 178 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 179 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 180 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 181 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 182 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 183 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 184 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 185 | |
| 186 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 187 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 188 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 189 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 190 | Item.val.int64 != -2147483648) |
| 191 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 192 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 193 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 194 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 195 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 196 | Item.val.int64 != -2147483647) |
| 197 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 198 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 199 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 200 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 202 | Item.val.int64 != -65538) |
| 203 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 204 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 205 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 206 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 207 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 208 | Item.val.int64 != -65537) |
| 209 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 210 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 211 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 212 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 213 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 214 | Item.val.int64 != -65536) |
| 215 | return -1; |
| 216 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 217 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 218 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 219 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 220 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 221 | Item.val.int64 != -65535) |
| 222 | return -1; |
| 223 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 224 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 225 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 226 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 227 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 228 | Item.val.int64 != -65534) |
| 229 | return -1; |
| 230 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 231 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 232 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 233 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 234 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 235 | Item.val.int64 != -257) |
| 236 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 237 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 238 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 239 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 240 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 241 | Item.val.int64 != -256) |
| 242 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 243 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 244 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 245 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 246 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 247 | Item.val.int64 != -255) |
| 248 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 249 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 250 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 251 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 252 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 253 | Item.val.int64 != -254) |
| 254 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 255 | |
| 256 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 257 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 258 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 259 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 260 | Item.val.int64 != -25) |
| 261 | return -1; |
| 262 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 263 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 264 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 265 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 266 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 267 | Item.val.int64 != -24) |
| 268 | return -1; |
| 269 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 270 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 271 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 272 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 273 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 274 | Item.val.int64 != -23) |
| 275 | return -1; |
| 276 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 277 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 278 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 279 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 280 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 281 | Item.val.int64 != -1) |
| 282 | return -1; |
| 283 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 284 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 285 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 286 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 287 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 288 | Item.val.int64 != 0) |
| 289 | return -1; |
| 290 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 291 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 292 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 293 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 294 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 295 | Item.val.int64 != 0) |
| 296 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 297 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 298 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 299 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 300 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 301 | Item.val.int64 != 1) |
| 302 | return -1; |
| 303 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 304 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 305 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 306 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 307 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 308 | Item.val.int64 != 22) |
| 309 | return -1; |
| 310 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 311 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 312 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 313 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 314 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 315 | Item.val.int64 != 23) |
| 316 | return -1; |
| 317 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 318 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 319 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 320 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 321 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 322 | Item.val.int64 != 24) |
| 323 | return -1; |
| 324 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 325 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 326 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 327 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 328 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 329 | Item.val.int64 != 25) |
| 330 | return -1; |
| 331 | |
| 332 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 333 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 334 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 335 | Item.val.int64 != 26) |
| 336 | return -1; |
| 337 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 338 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 339 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 340 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 341 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 342 | Item.val.int64 != 254) |
| 343 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 344 | |
| 345 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 346 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 347 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 348 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 349 | Item.val.int64 != 255) |
| 350 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 351 | |
| 352 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 353 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 354 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 355 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 356 | Item.val.int64 != 256) |
| 357 | return -1; |
| 358 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 359 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 360 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 361 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 362 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 363 | Item.val.int64 != 257) |
| 364 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 365 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 366 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 367 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 368 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 369 | Item.val.int64 != 65534) |
| 370 | return -1; |
| 371 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 372 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 373 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 374 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 375 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 376 | Item.val.int64 != 65535) |
| 377 | return -1; |
| 378 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 379 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 380 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 381 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 382 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 383 | Item.val.int64 != 65536) |
| 384 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 385 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 386 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 387 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 388 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 389 | Item.val.int64 != 65537) |
| 390 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 391 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 392 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 393 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 394 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 395 | Item.val.int64 != 65538) |
| 396 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 397 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 398 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 399 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 400 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 401 | Item.val.int64 != 2147483647) |
| 402 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 403 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 404 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 405 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 406 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 407 | Item.val.int64 != 2147483647) |
| 408 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 409 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 410 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 411 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 412 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 413 | Item.val.int64 != 2147483648) |
| 414 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 415 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 416 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 417 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 418 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 419 | Item.val.int64 != 2147483649) |
| 420 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 421 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 422 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 423 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 424 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 425 | Item.val.int64 != 4294967294) |
| 426 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 427 | |
| 428 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 429 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 430 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 431 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 432 | Item.val.int64 != 4294967295) |
| 433 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 434 | |
| 435 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 436 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 437 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 438 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 439 | Item.val.int64 != 4294967296) |
| 440 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 441 | |
| 442 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 443 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 444 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 445 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 446 | Item.val.int64 != 4294967297) |
| 447 | return -1; |
| 448 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 449 | |
| 450 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 451 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 452 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 453 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 454 | Item.val.int64 != 9223372036854775807LL) |
| 455 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 456 | |
| 457 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 458 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 459 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 460 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 461 | Item.val.uint64 != 18446744073709551615ULL) |
| 462 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 463 | |
| 464 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 465 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 466 | return -1; |
| 467 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 468 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 473 | /* One less than the smallest negative integer allowed in C. Decoding |
| 474 | this should fail. |
| 475 | -9223372036854775809 |
| 476 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 477 | static const uint8_t spTooSmallNegative[] = { |
| 478 | 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 479 | }; |
| 480 | |
| 481 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 482 | /* |
| 483 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 484 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 485 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 486 | int32_t IntegerValuesParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 487 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 488 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 489 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 490 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 491 | QCBORDecode_Init(&DCtx, |
| 492 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 493 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 494 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 495 | // The really big test of all successes |
| 496 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 497 | if(nReturn) { |
| 498 | return nReturn; |
| 499 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 500 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 501 | // The one large negative integer that can be parsed |
| 502 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 503 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative), |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 504 | QCBOR_DECODE_MODE_NORMAL); |
| 505 | |
| 506 | QCBORItem item; |
| 507 | if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) { |
| 508 | nReturn = -4000; |
| 509 | } |
| 510 | |
| 511 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
| 515 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 516 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 517 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 518 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 519 | 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] | 520 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 521 | */ |
| 522 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 523 | static uint8_t spSimpleArrayBuffer[50]; |
| 524 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 525 | 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] | 526 | { |
| 527 | QCBOREncodeContext ECtx; |
| 528 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 529 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 530 | *pEncoded = NULL; |
| 531 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 532 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 533 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 534 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 535 | // and last with the buffer to do the actual encoding |
| 536 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 537 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 538 | QCBOREncode_OpenArray(&ECtx); |
| 539 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 540 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 541 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 542 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 543 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 544 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 545 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 546 | goto Done; |
| 547 | |
| 548 | if(*pEncoded != NULL) { |
| 549 | nReturn = 0; |
| 550 | goto Done; |
| 551 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 552 | |
| 553 | // Use static buffer to avoid dependency on malloc() |
| 554 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 555 | goto Done; |
| 556 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 557 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 558 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 559 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 560 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 561 | Done: |
| 562 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 566 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 567 | Some basic CBOR with map and array used in a lot of tests. |
| 568 | The map labels are all strings |
| 569 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 570 | { |
| 571 | "first integer": 42, |
| 572 | "an array of two strings": [ |
| 573 | "string1", "string2" |
| 574 | ], |
| 575 | "map in a map": { |
| 576 | "bytes 1": h'78787878', |
| 577 | "bytes 2": h'79797979', |
| 578 | "another int": 98, |
| 579 | "text 2": "lies, damn lies and statistics" |
| 580 | } |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 581 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 582 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 583 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 584 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 585 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 586 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 587 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 588 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 589 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 590 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 591 | 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, |
| 592 | 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4, |
| 593 | 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 594 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, |
| 595 | 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, |
| 596 | 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68, |
| 597 | 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, |
| 598 | 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 599 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, |
| 600 | 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, |
| 601 | 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, |
| 602 | 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 603 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 604 | |
| 605 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 606 | // Same as above, but with indefinite lengths. |
| 607 | static const uint8_t pValidMapIndefEncoded[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 608 | 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, |
| 609 | 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, |
| 610 | 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, |
| 611 | 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, |
| 612 | 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 613 | 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 614 | 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, |
| 615 | 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20, |
| 616 | 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, |
| 617 | 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, |
| 618 | 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, |
| 619 | 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, |
| 620 | 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, |
| 621 | 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, |
| 622 | 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 623 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, |
| 624 | 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, |
| 625 | 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, |
| 626 | 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, |
| 627 | 0xff, 0xff}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 628 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 629 | |
| 630 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 631 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 632 | size_t nLen, |
| 633 | int64_t *pInt1, |
| 634 | int64_t *pInt2, |
| 635 | const uint8_t **pBuf3, |
| 636 | size_t *pBuf3Len, |
| 637 | const uint8_t **pBuf4, |
| 638 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 639 | { |
| 640 | QCBORDecodeContext DCtx; |
| 641 | QCBORItem Item; |
| 642 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 643 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 644 | QCBORDecode_Init(&DCtx, |
| 645 | (UsefulBufC){pEncoded, nLen}, |
| 646 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 647 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 648 | // Make sure the first thing is a map |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 649 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 650 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 651 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 652 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 653 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 654 | // First integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 655 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 656 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 657 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 658 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 659 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 660 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 661 | // Second integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 662 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 663 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 664 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 665 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 666 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 667 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 668 | // First string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 669 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 670 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 671 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 672 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 673 | *pBuf3 = Item.val.string.ptr; |
| 674 | *pBuf3Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 675 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 676 | // Second string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 677 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 678 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 679 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 680 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 681 | *pBuf4 = Item.val.string.ptr; |
| 682 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 683 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 684 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 685 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 686 | Done: |
| 687 | return(nReturn); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 692 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 693 | int32_t SimpleArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 694 | { |
| 695 | uint8_t *pEncoded; |
| 696 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 697 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 698 | int64_t i1=0, i2=0; |
| 699 | size_t i3=0, i4=0; |
| 700 | const uint8_t *s3= (uint8_t *)""; |
| 701 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 702 | |
| 703 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 704 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 705 | return(-1); |
| 706 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 707 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 708 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 709 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 710 | if(i1 != 23 || |
| 711 | i2 != 6000 || |
| 712 | i3 != 8 || |
| 713 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 714 | memcmp("galactic", s3, 8) !=0 || |
| 715 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 716 | return(-1); |
| 717 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 718 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 719 | return(0); |
| 720 | } |
| 721 | |
| 722 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 723 | /* |
| 724 | [ |
| 725 | 0, |
| 726 | [], |
| 727 | [ |
| 728 | [], |
| 729 | [ |
| 730 | 0 |
| 731 | ], |
| 732 | {}, |
| 733 | { |
| 734 | 1: {}, |
| 735 | 2: {}, |
| 736 | 3: [] |
| 737 | } |
| 738 | ] |
| 739 | ] |
| 740 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 741 | static uint8_t sEmpties[] = { |
| 742 | 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 743 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 744 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 745 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 746 | /* Same as above, but with indefinte lengths */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 747 | static const uint8_t sEmptiesIndef[] = { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 748 | 0x9F, |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 749 | 0x00, |
| 750 | 0x9F, |
| 751 | 0xFF, |
| 752 | 0x9F, |
| 753 | 0x9F, |
| 754 | 0xFF, |
| 755 | 0x9F, |
| 756 | 0x00, |
| 757 | 0xFF, |
| 758 | 0xBF, |
| 759 | 0xFF, |
| 760 | 0xBF, |
| 761 | 0x01, |
| 762 | 0xBF, |
| 763 | 0xFF, |
| 764 | 0x02, |
| 765 | 0xBF, |
| 766 | 0xFF, |
| 767 | 0x03, |
| 768 | 0x9F, |
| 769 | 0xFF, |
| 770 | 0xFF, |
| 771 | 0xFF, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 772 | 0xFF}; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 773 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 774 | |
| 775 | |
| 776 | static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts) |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 777 | { |
| 778 | QCBORDecodeContext DCtx; |
| 779 | QCBORItem Item; |
| 780 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 781 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 782 | input, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 783 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 784 | |
| 785 | // Array with 3 items |
| 786 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 787 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 788 | Item.uNestingLevel != 0 || |
| 789 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 790 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | // An integer 0 |
| 795 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 796 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 797 | Item.uNestingLevel != 1 || |
| 798 | Item.uNextNestLevel != 1 || |
| 799 | Item.val.uint64 != 0) { |
| 800 | return -2; |
| 801 | } |
| 802 | |
| 803 | // An empty array |
| 804 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 805 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 806 | Item.uNestingLevel != 1 || |
| 807 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 808 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 809 | return -3; |
| 810 | } |
| 811 | |
| 812 | // An array with 4 items |
| 813 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 814 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 815 | Item.uNestingLevel != 1 || |
| 816 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 817 | (bCheckCounts && Item.val.uCount != 4)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 818 | return -4; |
| 819 | } |
| 820 | |
| 821 | // An empty array |
| 822 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 823 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 824 | Item.uNestingLevel != 2 || |
| 825 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 826 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 827 | return -5; |
| 828 | } |
| 829 | |
| 830 | // An array with 1 item |
| 831 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 832 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 833 | Item.uNestingLevel != 2 || |
| 834 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 835 | (bCheckCounts && Item.val.uCount != 1)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 836 | return -6; |
| 837 | } |
| 838 | |
| 839 | // An integer 0 |
| 840 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 841 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 842 | Item.uNestingLevel != 3 || |
| 843 | Item.uNextNestLevel != 2 || |
| 844 | Item.val.uint64 != 0) { |
| 845 | return -7; |
| 846 | } |
| 847 | |
| 848 | // An empty map |
| 849 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 850 | Item.uDataType != QCBOR_TYPE_MAP || |
| 851 | Item.uNestingLevel != 2 || |
| 852 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 853 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 854 | return -8; |
| 855 | } |
| 856 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 857 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 858 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 859 | Item.uDataType != QCBOR_TYPE_MAP || |
| 860 | Item.uNestingLevel != 2 || |
| 861 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 862 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 863 | return -9; |
| 864 | } |
| 865 | |
| 866 | // An empty map |
| 867 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 868 | Item.uDataType != QCBOR_TYPE_MAP || |
| 869 | Item.uNestingLevel != 3 || |
| 870 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 871 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 872 | return -10; |
| 873 | } |
| 874 | |
| 875 | // An empty map |
| 876 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 877 | Item.uDataType != QCBOR_TYPE_MAP || |
| 878 | Item.uNestingLevel != 3 || |
| 879 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 880 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 881 | return -11; |
| 882 | } |
| 883 | |
| 884 | // An empty array |
| 885 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 886 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 887 | Item.uNestingLevel != 3 || |
| 888 | Item.uNextNestLevel != 0 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 889 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 890 | return -12; |
| 891 | } |
| 892 | |
| 893 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 894 | return -13; |
| 895 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | |
| 900 | int32_t EmptyMapsAndArraysTest() |
| 901 | { |
| 902 | int nResult; |
| 903 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 904 | true); |
| 905 | if(nResult) { |
| 906 | return nResult; |
| 907 | } |
| 908 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 909 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 910 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 911 | false); |
| 912 | |
| 913 | if(nResult) { |
| 914 | return nResult -100; |
| 915 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 916 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 921 | |
| 922 | static const uint8_t sEmptyMap[] = { |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 923 | 0xA1, //# map(1) |
| 924 | 0x02, //# unsigned(2) |
| 925 | 0xA0, //# map(0) |
| 926 | }; |
| 927 | |
| 928 | int32_t ParseEmptyMapInMapTest(void) |
| 929 | { |
| 930 | QCBORDecodeContext DCtx; |
| 931 | QCBORItem Item; |
| 932 | int nReturn = 0; |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 933 | QCBORError uErr; |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 934 | |
| 935 | QCBORDecode_Init(&DCtx, |
| 936 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap), |
| 937 | QCBOR_DECODE_MODE_NORMAL); |
| 938 | |
| 939 | /* now open the first Map */ |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 940 | uErr = QCBORDecode_GetNext(&DCtx, &Item); |
| 941 | if(uErr != QCBOR_SUCCESS || |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 942 | Item.uDataType != QCBOR_TYPE_MAP) { |
| 943 | nReturn = -3; |
| 944 | goto done; |
| 945 | } |
| 946 | |
| 947 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0) { |
| 948 | nReturn = -1; |
| 949 | goto done; |
| 950 | } |
| 951 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 952 | Item.uNestingLevel != 1 || |
| 953 | Item.label.int64 != 2) { |
| 954 | nReturn = -2; |
| 955 | goto done; |
| 956 | } |
| 957 | |
| 958 | done: |
| 959 | return(nReturn); |
| 960 | } |
| 961 | |
Laurence Lundblade | 4c532ca | 2021-02-18 21:31:49 -0700 | [diff] [blame] | 962 | |
Michael Richardson | 87de9af | 2021-02-18 23:13:31 -0500 | [diff] [blame] | 963 | /* [[[[[[[[[[]]]]]]]]]] */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 964 | static const uint8_t spDeepArrays[] = { |
| 965 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 966 | 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 967 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 968 | int32_t ParseDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 969 | { |
| 970 | QCBORDecodeContext DCtx; |
| 971 | int nReturn = 0; |
| 972 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 973 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 974 | QCBORDecode_Init(&DCtx, |
| 975 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 976 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 977 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 978 | for(i = 0; i < 10; i++) { |
| 979 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 980 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 981 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 982 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 983 | Item.uNestingLevel != i) { |
| 984 | nReturn = -1; |
| 985 | break; |
| 986 | } |
| 987 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 988 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 989 | return(nReturn); |
| 990 | } |
| 991 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 992 | /* Big enough to test nesting to the depth of 24 |
| 993 | [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]] |
| 994 | */ |
| 995 | static const uint8_t spTooDeepArrays[] = { |
| 996 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 997 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 998 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 999 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1000 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1001 | int32_t ParseTooDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1002 | { |
| 1003 | QCBORDecodeContext DCtx; |
| 1004 | int nReturn = 0; |
| 1005 | int i; |
| 1006 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1007 | |
| 1008 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1009 | QCBORDecode_Init(&DCtx, |
| 1010 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 1011 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1012 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 1013 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1014 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1015 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 1016 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1017 | Item.uNestingLevel != i) { |
| 1018 | nReturn = -1; |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1022 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1023 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1024 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1025 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1026 | return(nReturn); |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1032 | int32_t ShortBufferParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1033 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1034 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1035 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1036 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1037 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1038 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1039 | QCBORDecode_Init(&DCtx, |
| 1040 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 1041 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1042 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1043 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1044 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1045 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1046 | nResult = -1; |
| 1047 | goto Done; |
| 1048 | } |
| 1049 | } |
| 1050 | Done: |
| 1051 | return nResult; |
| 1052 | } |
| 1053 | |
| 1054 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1056 | int32_t ShortBufferParseTest2() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1057 | { |
| 1058 | uint8_t *pEncoded; |
| 1059 | int nReturn; |
| 1060 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1061 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1062 | int64_t i1, i2; |
| 1063 | size_t i3, i4; |
| 1064 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1065 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1066 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1067 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1068 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 1069 | return(-1); |
| 1070 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1071 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1072 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1073 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 1074 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1075 | if(nResult == 0) { |
| 1076 | nReturn = -1; |
| 1077 | } |
| 1078 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1079 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1080 | return(nReturn); |
| 1081 | } |
| 1082 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1083 | /* |
| 1084 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1085 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 1086 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1087 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1088 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1089 | { |
| 1090 | QCBORDecodeContext DCtx; |
| 1091 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1092 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1093 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1094 | QCBORDecode_Init(&DCtx, |
| 1095 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1096 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1097 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1098 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1099 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1100 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1101 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1102 | Item.val.uCount != 3) |
| 1103 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1104 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1105 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1106 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1107 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1108 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1109 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1110 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1111 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1112 | Item.uDataAlloc || |
| 1113 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1114 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1115 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1116 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1117 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1118 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1119 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1120 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1121 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1122 | Item.uDataAlloc || |
| 1123 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1124 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1125 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1126 | Item.val.uCount != 2) |
| 1127 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1128 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1129 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1130 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1131 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1132 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1133 | Item.uDataAlloc || |
| 1134 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1135 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1136 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1137 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1138 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1139 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1140 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1141 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1142 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1143 | Item.uDataAlloc || |
| 1144 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1145 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1146 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1147 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1148 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1149 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1150 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1151 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1152 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1153 | Item.uDataAlloc || |
| 1154 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1155 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1156 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1157 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1158 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1159 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1160 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1161 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1162 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1163 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1164 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1165 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1166 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1167 | Item.uDataAlloc || |
| 1168 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1169 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1170 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1171 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1172 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1173 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1174 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1175 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1176 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1177 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1178 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1179 | Item.uDataAlloc || |
| 1180 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1181 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1182 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1183 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1184 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1185 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1186 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1187 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1188 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1189 | Item.uDataAlloc || |
| 1190 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1191 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1192 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1193 | Item.val.int64 != 98) |
| 1194 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1195 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1196 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1197 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1198 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1199 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1200 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1201 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1202 | Item.uDataAlloc || |
| 1203 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1204 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1205 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1206 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1207 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1212 | /* |
| 1213 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1214 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1215 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1216 | int32_t ParseMapAsArrayTest() |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1217 | { |
| 1218 | QCBORDecodeContext DCtx; |
| 1219 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1220 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1221 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1222 | QCBORDecode_Init(&DCtx, |
| 1223 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1224 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1225 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1226 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1227 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1228 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1229 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1230 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1231 | return -1; |
| 1232 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1233 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1234 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1235 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1236 | } |
| 1237 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1238 | Item.uDataAlloc || |
| 1239 | Item.uLabelAlloc || |
| 1240 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1241 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1242 | return -2; |
| 1243 | } |
| 1244 | |
| 1245 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1246 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1247 | } |
| 1248 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1249 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1250 | Item.val.int64 != 42 || |
| 1251 | Item.uDataAlloc || |
| 1252 | Item.uLabelAlloc) { |
| 1253 | return -3; |
| 1254 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1255 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1256 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1257 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1258 | } |
| 1259 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1260 | Item.uDataAlloc || |
| 1261 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1262 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1263 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1264 | return -4; |
| 1265 | } |
| 1266 | |
| 1267 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1268 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1269 | } |
| 1270 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1271 | Item.uDataAlloc || |
| 1272 | Item.uLabelAlloc || |
| 1273 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1274 | Item.val.uCount != 2) { |
| 1275 | return -5; |
| 1276 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1277 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1278 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1279 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1280 | } |
| 1281 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1282 | Item.val.string.len != 7 || |
| 1283 | Item.uDataAlloc || |
| 1284 | Item.uLabelAlloc || |
| 1285 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1286 | return -6; |
| 1287 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1288 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1289 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1290 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1291 | } |
| 1292 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1293 | Item.uDataAlloc || |
| 1294 | Item.uLabelAlloc || |
| 1295 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1296 | return -7; |
| 1297 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1298 | |
| 1299 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1300 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1301 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1302 | } |
| 1303 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1304 | Item.uDataAlloc || |
| 1305 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1306 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1307 | return -8; |
| 1308 | } |
| 1309 | |
| 1310 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1311 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1312 | } |
| 1313 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1314 | Item.uDataAlloc || |
| 1315 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1316 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1317 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1318 | return -9; |
| 1319 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1320 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1321 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1322 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1323 | } |
| 1324 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1325 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1326 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1327 | Item.uDataAlloc || |
| 1328 | Item.uLabelAlloc) { |
| 1329 | return -10; |
| 1330 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1331 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1332 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1333 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1334 | } |
| 1335 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1336 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1337 | Item.uDataAlloc || |
| 1338 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1339 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1340 | return -11; |
| 1341 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1342 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1343 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1344 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1345 | } |
| 1346 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1347 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1348 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1349 | Item.uDataAlloc || |
| 1350 | Item.uLabelAlloc) { |
| 1351 | return -12; |
| 1352 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1353 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1354 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1355 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1356 | } |
| 1357 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1358 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1359 | Item.uDataAlloc || |
| 1360 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1361 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1362 | return -13; |
| 1363 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1364 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1365 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1366 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1367 | } |
| 1368 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1369 | Item.uDataAlloc || |
| 1370 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1371 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1372 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1373 | return -14; |
| 1374 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1375 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1376 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1377 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1378 | } |
| 1379 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1380 | Item.uDataAlloc || |
| 1381 | Item.uLabelAlloc || |
| 1382 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1383 | Item.val.int64 != 98) { |
| 1384 | return -15; |
| 1385 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1386 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1387 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1388 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1389 | } |
| 1390 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1391 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1392 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1393 | Item.uDataAlloc || |
| 1394 | Item.uLabelAlloc) { |
| 1395 | return -16; |
| 1396 | } |
| 1397 | |
| 1398 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1399 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1400 | } |
| 1401 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1402 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1403 | Item.uDataAlloc || |
| 1404 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1405 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1406 | return -17; |
| 1407 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1408 | |
| 1409 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1410 | /* |
| 1411 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1412 | map that when interpreted as an array will be too many. Test |
| 1413 | data just has the start of the map, not all the items in the map. |
| 1414 | */ |
| 1415 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1416 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1417 | QCBORDecode_Init(&DCtx, |
| 1418 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1419 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1420 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1421 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1422 | return -50; |
| 1423 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1424 | |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1425 | // TODO: test decoding of labels that are arrays or such |
| 1426 | // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY |
| 1427 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1428 | return 0; |
| 1429 | } |
| 1430 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1431 | |
| 1432 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1433 | Fully or partially decode pValidMapEncoded. When |
| 1434 | partially decoding check for the right error code. |
| 1435 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1436 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1437 | The partial decodes test error conditions of |
| 1438 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1439 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1440 | This could be combined with the above test |
| 1441 | and made prettier and maybe a little more |
| 1442 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1443 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1444 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1445 | { |
| 1446 | QCBORDecodeContext DCtx; |
| 1447 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1448 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1449 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1450 | QCBORDecode_Init(&DCtx, |
| 1451 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1452 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1453 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1454 | if(nLevel < 1) { |
| 1455 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1456 | return -1; |
| 1457 | } else { |
| 1458 | return 0; |
| 1459 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1460 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1461 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1462 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1463 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1464 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1465 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1466 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1467 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1468 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1469 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1470 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1471 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1472 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1473 | } else { |
| 1474 | return 0; |
| 1475 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1476 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1477 | |
| 1478 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1479 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1480 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1481 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1482 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1483 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1484 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1485 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1486 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1487 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1488 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1489 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1490 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1491 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1492 | } else { |
| 1493 | return 0; |
| 1494 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1495 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1496 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1497 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1498 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1499 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1500 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1501 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1502 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1503 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1504 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1505 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1506 | |
| 1507 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1508 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1509 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1510 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1511 | } else { |
| 1512 | return 0; |
| 1513 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1514 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1515 | |
| 1516 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1517 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1518 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1519 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1520 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1521 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1522 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1523 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1524 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1525 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1526 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1527 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1528 | } else { |
| 1529 | return 0; |
| 1530 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1531 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1532 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1533 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1534 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1535 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1536 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1537 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1538 | return -10; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1539 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1540 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1541 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1542 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1543 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1544 | } else { |
| 1545 | return 0; |
| 1546 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1547 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1548 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1549 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1550 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1551 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1552 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1553 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1554 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1555 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1556 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1557 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1558 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1559 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1560 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1561 | } else { |
| 1562 | return 0; |
| 1563 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1564 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1565 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1566 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1567 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1568 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1569 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1570 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1571 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1572 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1573 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1574 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1575 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1576 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1577 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1578 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1579 | } else { |
| 1580 | return 0; |
| 1581 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1582 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1583 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1584 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1585 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1586 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1587 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1588 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1589 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1590 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1591 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1592 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1593 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1594 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1595 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1596 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1597 | } else { |
| 1598 | return 0; |
| 1599 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1600 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1601 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1602 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1603 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1604 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1605 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1606 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1607 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1608 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1609 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1610 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1611 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1612 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1613 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1614 | } else { |
| 1615 | return 0; |
| 1616 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1617 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1618 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1619 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1620 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1621 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1622 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1623 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1624 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1625 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1626 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1627 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1628 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1629 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1630 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1631 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1632 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | |
| 1637 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1638 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1639 | int32_t ParseMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1640 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1641 | // Parse a moderatly complex map structure very thoroughly |
| 1642 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 1643 | if(nResult) { |
| 1644 | return nResult; |
| 1645 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1646 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1647 | // Again, but in strings-only mode. It should succeed since the input |
| 1648 | // map has only string labels. |
| 1649 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 1650 | if(nResult) { |
| 1651 | return nResult; |
| 1652 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1653 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1654 | // Again, but try to finish the decoding before the end of the |
| 1655 | // input at 10 different place and see that the right error code |
| 1656 | // is returned. |
| 1657 | for(int i = 0; i < 10; i++) { |
| 1658 | nResult = ExtraBytesTest(i); |
| 1659 | if(nResult) { |
| 1660 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1661 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1662 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1663 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1664 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1668 | /* The simple-values including some not well formed */ |
| 1669 | static const uint8_t spSimpleValues[] = { |
| 1670 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 1671 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 1672 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1673 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1674 | int32_t ParseSimpleTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1675 | { |
| 1676 | QCBORDecodeContext DCtx; |
| 1677 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1678 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1679 | |
| 1680 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1681 | QCBORDecode_Init(&DCtx, |
| 1682 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1683 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1684 | |
| 1685 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1686 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1687 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1688 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1689 | Item.val.uCount != 10) |
| 1690 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1691 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1692 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1693 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1694 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1695 | return -1; |
| 1696 | |
| 1697 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1698 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1699 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1700 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1701 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1702 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1703 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1704 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1705 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1706 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1707 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1708 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1709 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1710 | return -1; |
| 1711 | |
| 1712 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1713 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1714 | return -1; |
| 1715 | |
| 1716 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1717 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1718 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1719 | return -1; |
| 1720 | |
| 1721 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1722 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1723 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1724 | return -1; |
| 1725 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1726 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1727 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1728 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1729 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1730 | return -1; |
| 1731 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1732 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1733 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1734 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1735 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1736 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1737 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1738 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1739 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1740 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1741 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1742 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1743 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1744 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1745 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1746 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1750 | int32_t NotWellFormedTests() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1751 | { |
| 1752 | // Loop over all the not-well-formed instance of CBOR |
| 1753 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1754 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 1755 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1756 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 1757 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 1758 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 1759 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1760 | // Set up decoder context. String allocator needed for indefinite |
| 1761 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1762 | QCBORDecodeContext DCtx; |
| 1763 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1764 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1765 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1766 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1767 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1768 | |
| 1769 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1770 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1771 | do { |
| 1772 | QCBORItem Item; |
| 1773 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1774 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1775 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1776 | |
| 1777 | // Every test vector must fail with |
| 1778 | // a not-well-formed error. If not |
| 1779 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1780 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1781 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1782 | /* Return index of failure and QCBOR error in the result */ |
| 1783 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1790 | // 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] | 1791 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1792 | UsefulBufC Input; |
| 1793 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1794 | }; |
| 1795 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1796 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1797 | static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1798 | { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1799 | for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1800 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1801 | QCBORError uCBORError; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1802 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1803 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1804 | |
| 1805 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1806 | // Set up the decoding context including a memory pool so that |
| 1807 | // indefinite length items can be checked |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1808 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1809 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1810 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1811 | if(uCBORError) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1812 | return -9; |
| 1813 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1814 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1815 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 1816 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1817 | // Iterate until there is an error of some sort error |
| 1818 | QCBORItem Item; |
| 1819 | do { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1820 | // Set to something none-zero, something other than QCBOR_TYPE_NONE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1821 | memset(&Item, 0x33, sizeof(Item)); |
| 1822 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1823 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1824 | } while(uCBORError == QCBOR_SUCCESS); |
| 1825 | |
| 1826 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1827 | |
| 1828 | // Must get the expected error or the this test fails |
| 1829 | // The data and label type must also be QCBOR_TYPE_NONE |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1830 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1831 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1832 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1833 | // return index of CBOR + 100 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1834 | const size_t nIndex = (size_t)(pF - pFailInputs); |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1835 | return (int32_t)(nIndex * 100 + uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
| 1842 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1843 | static const struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1844 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1845 | // returned is also checked. |
| 1846 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1847 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1848 | // Indefinite length strings must be closed off |
| 1849 | // An indefinite length byte string not closed off |
| 1850 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1851 | // An indefinite length text string not closed off |
| 1852 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1853 | |
| 1854 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1855 | // All the chunks in an indefinite length string must be of the type of |
| 1856 | // indefinite length string |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1857 | // indefinite length byte string with text string chunk |
| 1858 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1859 | // indefinite length text string with a byte string chunk |
| 1860 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1861 | // indefinite length byte string with an positive integer chunk |
| 1862 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1863 | // indefinite length byte string with an negative integer chunk |
| 1864 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1865 | // indefinite length byte string with an array chunk |
| 1866 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1867 | // indefinite length byte string with an map chunk |
| 1868 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1869 | // indefinite length byte string with tagged integer chunk |
| 1870 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1871 | // indefinite length byte string with an simple type chunk |
| 1872 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1873 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1874 | // indefinite length text string with indefinite string inside |
| 1875 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1876 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1877 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1878 | |
| 1879 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1880 | // An indefinite length text string not closed off |
| 1881 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1882 | |
| 1883 | |
| 1884 | // All the chunks in an indefinite length string must be of the type of |
| 1885 | // indefinite length string |
| 1886 | // indefinite length byte string with text string chunk |
| 1887 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1888 | // indefinite length text string with a byte string chunk |
| 1889 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1890 | // indefinite length byte string with an positive integer chunk |
| 1891 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1892 | // indefinite length byte string with an negative integer chunk |
| 1893 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1894 | // indefinite length byte string with an array chunk |
| 1895 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1896 | // indefinite length byte string with an map chunk |
| 1897 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1898 | // indefinite length byte string with tagged integer chunk |
| 1899 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1900 | // indefinite length byte string with an simple type chunk |
| 1901 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1902 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1903 | // indefinite length text string with indefinite string inside |
| 1904 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1905 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1906 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1907 | |
| 1908 | // Definte length maps and arrays must be closed by having the right number of items |
| 1909 | // 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] | 1910 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1911 | // 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] | 1912 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1913 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1914 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1915 | // 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] | 1916 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1917 | // 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] | 1918 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1919 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1920 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1921 | // Indefinte length maps and arrays must be ended by a break |
| 1922 | // Indefinite length array with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1923 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1924 | // Indefinite length array with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1925 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1926 | // Indefinite length map with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1927 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1928 | // Indefinite length map with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1929 | { {(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] | 1930 | |
| 1931 | |
| 1932 | // 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] | 1933 | // Unclosed indefinite array containing a closed definite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1934 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1935 | // Definite length array containing an unclosed indefinite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1936 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1937 | // Unclosed indefinite map containing a closed definite length array |
| 1938 | { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
| 1939 | // Definite length map containing an unclosed indefinite length array |
| 1940 | { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1941 | // Deeply nested definite length arrays with deepest one unclosed |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1942 | { {(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] | 1943 | // Deeply nested indefinite length arrays with deepest one unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1944 | { {(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] | 1945 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1946 | { {(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] | 1947 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1948 | { {(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] | 1949 | // Unclosed indefinite length map in definite length maps |
| 1950 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8}, |
| 1951 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1952 | // Unclosed definite length map in indefinite length maps |
| 1953 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1954 | // Unclosed indefinite length array in definite length maps |
| 1955 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8}, |
| 1956 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1957 | // Unclosed definite length array in indefinite length maps |
| 1958 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1959 | // Unclosed indefinite length map in definite length arrays |
| 1960 | { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1961 | // Unclosed definite length map in indefinite length arrays |
| 1962 | { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1963 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1964 | |
| 1965 | // The "argument" for the data item is incomplete |
| 1966 | // Positive integer missing 1 byte argument |
| 1967 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1968 | // Positive integer missing 2 byte argument |
| 1969 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1970 | // Positive integer missing 4 byte argument |
| 1971 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1972 | // Positive integer missing 8 byte argument |
| 1973 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1974 | // Positive integer missing 1 byte of 2 byte argument |
| 1975 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1976 | // Positive integer missing 2 bytes of 4 byte argument |
| 1977 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1978 | // Positive integer missing 1 bytes of 7 byte argument |
| 1979 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1980 | // Negative integer missing 1 byte argument |
| 1981 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1982 | // Binary string missing 1 byte argument |
| 1983 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1984 | // Text string missing 1 byte argument |
| 1985 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 1986 | // Array missing 1 byte argument |
| 1987 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 1988 | // Map missing 1 byte argument |
| 1989 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 1990 | // Tag missing 1 byte argument |
| 1991 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 1992 | // Simple missing 1 byte argument |
| 1993 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1994 | // half-precision with 1 byte argument |
| 1995 | { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1996 | // single-precision with 2 byte argument |
| 1997 | { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1998 | // double-precision with 3 byte argument |
| 1999 | { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 2000 | |
| 2001 | |
| 2002 | // Tag with no content |
| 2003 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2004 | |
| 2005 | |
| 2006 | // Breaks must not occur in definite length arrays and maps |
| 2007 | // Array of length 1 with sole member replaced by a break |
| 2008 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2009 | // Array of length 2 with 2nd member replaced by a break |
| 2010 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2011 | // Map of length 1 with sole member label replaced by a break |
| 2012 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 2013 | // Map of length 1 with sole member label replaced by break |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2014 | // Alternate representation that some decoders handle differently |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2015 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2016 | // Array of length 1 with 2nd member value replaced by a break |
| 2017 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2018 | // Map of length 2 with 2nd member replaced by a break |
| 2019 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 2020 | |
| 2021 | |
| 2022 | // Breaks must not occur on their own out of an indefinite length data item |
| 2023 | // A bare break is not well formed |
| 2024 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 2025 | // A bare break after a zero length definite length array |
| 2026 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2027 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2028 | // A bare break after a zero length indefinite length map |
| 2029 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2030 | // A break inside a definite length array inside an indefenite length array |
| 2031 | { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2032 | // Complicated mixed nesting with break outside indefinite length array |
| 2033 | { {(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] | 2034 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2035 | |
| 2036 | |
| 2037 | // Forbidden two byte encodings of simple types |
| 2038 | // Must use 0xe0 instead |
| 2039 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2040 | // Should use 0xe1 instead |
| 2041 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2042 | // Should use 0xe2 instead |
| 2043 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2044 | // Should use 0xe3 instead |
| 2045 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2046 | // Should use 0xe4 instead |
| 2047 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2048 | // Should use 0xe5 instead |
| 2049 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2050 | // Should use 0xe6 instead |
| 2051 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2052 | // Should use 0xe7 instead |
| 2053 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2054 | // Should use 0xe8 instead |
| 2055 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2056 | // Should use 0xe9 instead |
| 2057 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2058 | // Should use 0xea instead |
| 2059 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2060 | // Should use 0xeb instead |
| 2061 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2062 | // Should use 0xec instead |
| 2063 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2064 | // Should use 0xed instead |
| 2065 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2066 | // Should use 0xee instead |
| 2067 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2068 | // Should use 0xef instead |
| 2069 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2070 | // Should use 0xf0 instead |
| 2071 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2072 | // Should use 0xf1 instead |
| 2073 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2074 | // Should use 0xf2 instead |
| 2075 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2076 | // Must use 0xf3 instead |
| 2077 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2078 | // Must use 0xf4 instead |
| 2079 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2080 | // Must use 0xf5 instead |
| 2081 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2082 | // Must use 0xf6 instead |
| 2083 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2084 | // Must use 0xf7 instead |
| 2085 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2086 | // Must use 0xf8 instead |
| 2087 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2088 | // Reserved |
| 2089 | { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2090 | |
| 2091 | // Integers with additional info indefinite length |
| 2092 | // Positive integer with additional info indefinite length |
| 2093 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2094 | // Negative integer with additional info indefinite length |
| 2095 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2096 | // CBOR tag with "argument" an indefinite length |
| 2097 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 2098 | // CBOR tag with "argument" an indefinite length alternate vector |
| 2099 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
| 2100 | |
| 2101 | |
| 2102 | // Missing bytes from a deterministic length string |
| 2103 | // A byte string is of length 1 without the 1 byte |
| 2104 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 2105 | // A text string is of length 1 without the 1 byte |
| 2106 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2107 | |
| 2108 | #if SIZE_MAX > 2147483647 |
Laurence Lundblade | 42272e4 | 2020-01-31 07:50:53 -0800 | [diff] [blame] | 2109 | // Byte string should have 2^32-15 bytes, but has one |
| 2110 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2111 | // Byte string should have 2^32-15 bytes, but has one |
| 2112 | { {(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] | 2113 | // Byte string should have 2^64 bytes, but has 3 |
| 2114 | { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2115 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2116 | // Text string should have 2^64 bytes, but has 3 |
| 2117 | { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2118 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2119 | #else |
| 2120 | // Byte string should have 2^32-15 bytes, but has one |
| 2121 | { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2122 | // Byte string should have 2^32-15 bytes, but has one |
| 2123 | { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2124 | // Byte string should have 2^16 bytes, but has 3 |
| 2125 | { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2126 | // Text string should have 2^64 bytes, but has 3 |
| 2127 | { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2128 | #endif |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2129 | |
| 2130 | // Use of unassigned additional information values |
| 2131 | // Major type positive integer with reserved value 28 |
| 2132 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2133 | // Major type positive integer with reserved value 29 |
| 2134 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2135 | // Major type positive integer with reserved value 30 |
| 2136 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2137 | // Major type negative integer with reserved value 28 |
| 2138 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2139 | // Major type negative integer with reserved value 29 |
| 2140 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2141 | // Major type negative integer with reserved value 30 |
| 2142 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2143 | // Major type byte string with reserved value 28 length |
| 2144 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2145 | // Major type byte string with reserved value 29 length |
| 2146 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2147 | // Major type byte string with reserved value 30 length |
| 2148 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2149 | // Major type text string with reserved value 28 length |
| 2150 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2151 | // Major type text string with reserved value 29 length |
| 2152 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2153 | // Major type text string with reserved value 30 length |
| 2154 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2155 | // Major type array with reserved value 28 length |
| 2156 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2157 | // Major type array with reserved value 29 length |
| 2158 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2159 | // Major type array with reserved value 30 length |
| 2160 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2161 | // Major type map with reserved value 28 length |
| 2162 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2163 | // Major type map with reserved value 29 length |
| 2164 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2165 | // Major type map with reserved value 30 length |
| 2166 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2167 | // Major type tag with reserved value 28 length |
| 2168 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2169 | // Major type tag with reserved value 29 length |
| 2170 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2171 | // Major type tag with reserved value 30 length |
| 2172 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2173 | // Major type simple with reserved value 28 length |
| 2174 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2175 | // Major type simple with reserved value 29 length |
| 2176 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2177 | // Major type simple with reserved value 30 length |
| 2178 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2179 | |
| 2180 | |
| 2181 | // Maps must have an even number of data items (key & value) |
| 2182 | // Map with 1 item when it should have 2 |
| 2183 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2184 | // Map with 3 item when it should have 4 |
| 2185 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2186 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2187 | // Map with 1 item when it should have 2 |
| 2188 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2189 | // Map with 3 item when it should have 4 |
| 2190 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2191 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2192 | |
| 2193 | |
| 2194 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2195 | // Text-based date, with an integer |
| 2196 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2197 | // Epoch date, with an byte string |
| 2198 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2199 | // tagged as both epoch and string dates |
| 2200 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2201 | // big num tagged an int, not a byte string |
| 2202 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2203 | }; |
| 2204 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2205 | int32_t DecodeFailureTests() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2206 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2207 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2208 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2209 | nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2210 | if(nResult) { |
| 2211 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2212 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2213 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2214 | // Corrupt the UsefulInputBuf and see that |
| 2215 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2216 | QCBORDecodeContext DCtx; |
| 2217 | QCBORItem Item; |
| 2218 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2219 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2220 | QCBORDecode_Init(&DCtx, |
| 2221 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2222 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2223 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2224 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2225 | return (int32_t)uQCBORError; |
| 2226 | } |
| 2227 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 2228 | // This wasn't supposed to happen |
| 2229 | return -1; |
| 2230 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2231 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2232 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2233 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2234 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2235 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2236 | // Did not get back the error expected |
| 2237 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2238 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2239 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2240 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2241 | /* |
| 2242 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 2243 | tests here can be performed to see that the max length |
| 2244 | error check works correctly. See DecodeBytes(). If the max |
| 2245 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2246 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2247 | This test will automatocally adapt the all CPU sizes |
| 2248 | through the use of SIZE_MAX. |
| 2249 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2250 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2251 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2252 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2253 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2254 | // This makes a CBOR head with a text string that is very long |
| 2255 | // but doesn't fill in the bytes of the text string as that is |
| 2256 | // not needed to test this part of QCBOR. |
| 2257 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 2258 | |
| 2259 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 2260 | |
| 2261 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2262 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2263 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2264 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2265 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2269 | /* Try all 256 values of the byte at nLen including recursing for |
| 2270 | each of the values to try values at nLen+1 ... up to nLenMax |
| 2271 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2272 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2273 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2274 | if(nLen >= nLenMax) { |
| 2275 | return; |
| 2276 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2277 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2278 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 2279 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2280 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2281 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2282 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2283 | // Get ready to parse |
| 2284 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2285 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2286 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2287 | // Parse by getting the next item until an error occurs |
| 2288 | // Just about every possible decoder error can occur here |
| 2289 | // The goal of this test is not to check for the correct |
| 2290 | // error since that is not really possible. It is to |
| 2291 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2292 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2293 | QCBORItem Item; |
| 2294 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2295 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2296 | break; |
| 2297 | } |
| 2298 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2299 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2300 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2305 | int32_t ComprehensiveInputTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2306 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2307 | // Size 2 tests 64K inputs and runs quickly |
| 2308 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2309 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2310 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2311 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2312 | return 0; |
| 2313 | } |
| 2314 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2315 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2316 | int32_t BigComprehensiveInputTest() |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2317 | { |
| 2318 | // size 3 tests 16 million inputs and runs OK |
| 2319 | // in seconds on fast machines. Size 4 takes |
| 2320 | // 10+ minutes and 5 half a day on fast |
| 2321 | // machines. This test is kept separate from |
| 2322 | // the others so as to no slow down the use |
| 2323 | // of them as a very frequent regression. |
| 2324 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2325 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2326 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2327 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2328 | return 0; |
| 2329 | } |
| 2330 | |
| 2331 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2332 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2333 | 0xc0, // tag for string date |
| 2334 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2335 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2336 | 0xc0, // tag for string date |
| 2337 | 0x00, // Wrong type for a string date |
| 2338 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2339 | 0xc1, // tag for epoch date |
| 2340 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2341 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2342 | 0xc1, |
| 2343 | 0x62, 'h', 'i', // wrong type tagged |
| 2344 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2345 | // CBOR_TAG_ENC_AS_B64 |
| 2346 | 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2347 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2348 | |
| 2349 | 0xc1, // tag for epoch date |
| 2350 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2351 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2352 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2353 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2354 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2355 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2356 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2357 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2358 | 0xc1, // tag for epoch date |
| 2359 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2360 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2361 | |
| 2362 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2363 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2364 | |
| 2365 | 0xc1, // tag for epoch date |
| 2366 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 2367 | |
| 2368 | 0xc1, |
| 2369 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 2370 | |
| 2371 | 0xc1, // tag for epoch date |
| 2372 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2373 | }; |
| 2374 | |
| 2375 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2376 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2377 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2378 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 2379 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2380 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2381 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2382 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2383 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2384 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2385 | return diff > 0.0000001; |
| 2386 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2387 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2388 | |
| 2389 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2390 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2391 | int32_t DateParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2392 | { |
| 2393 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2394 | QCBORItem Item; |
| 2395 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2396 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2397 | QCBORDecode_Init(&DCtx, |
| 2398 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2399 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2400 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2401 | // String date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2402 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2403 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2404 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2405 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2406 | UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2407 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2408 | } |
| 2409 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2410 | // Wrong type for a string date |
| 2411 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2412 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2413 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2417 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2418 | return -4; |
| 2419 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2420 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2421 | Item.val.epochDate.nSeconds != 1400000000 || |
| 2422 | Item.val.epochDate.fSecondsFraction != 0 ) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2423 | return -5; |
| 2424 | } |
| 2425 | |
| 2426 | // Wrong type for an epoch date |
| 2427 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 2428 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2429 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2430 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2431 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 2432 | // The date is decoded and the two tags are returned. This is to |
| 2433 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2434 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2435 | return -7; |
| 2436 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2437 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2438 | Item.val.epochDate.nSeconds != 1400000001 || |
| 2439 | Item.val.epochDate.fSecondsFraction != 0 || |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2440 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2441 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2442 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2443 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2444 | // Epoch date that is too large for our representation |
| 2445 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2446 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2447 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2448 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2449 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2450 | // Epoch date in float format with fractional seconds |
| 2451 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2452 | return -10; |
| 2453 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2454 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2455 | Item.val.epochDate.nSeconds != 1 || |
| 2456 | CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2457 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2458 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2459 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2460 | // Epoch date float that is too large for our representation |
| 2461 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2462 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2463 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2464 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2465 | // Epoch date double that is just slightly too large |
| 2466 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2467 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | // Largest double epoch date supported |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2471 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS || |
| 2472 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2473 | Item.val.epochDate.nSeconds != 9223372036854773760 || |
| 2474 | Item.val.epochDate.nSeconds == 0) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2475 | return -14; |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2476 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2477 | |
| 2478 | // Nan |
| 2479 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2480 | return -15; |
| 2481 | } |
| 2482 | |
| 2483 | // +Inifinity double-precision |
| 2484 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2485 | return -16; |
| 2486 | } |
| 2487 | |
| 2488 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2489 | // -Inifinity half-precision |
| 2490 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2491 | return -17; |
| 2492 | } |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2493 | #else |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2494 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) { |
| 2495 | return -18; |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2496 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2497 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2498 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2499 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2500 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2501 | return -19; |
| 2502 | } |
| 2503 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2504 | return -20; |
| 2505 | } |
| 2506 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2507 | return -21; |
| 2508 | } |
| 2509 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2510 | return -22; |
| 2511 | } |
| 2512 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2513 | return -23; |
| 2514 | } |
| 2515 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2516 | return -24; |
| 2517 | } |
| 2518 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2519 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2520 | return -25; |
| 2521 | } |
| 2522 | #else |
| 2523 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) { |
| 2524 | return -26; |
| 2525 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2526 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2527 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2528 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2529 | |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2533 | /* |
| 2534 | Test cases covered here. Some items cover more than one of these. |
| 2535 | positive integer (zero counts as a positive integer) |
| 2536 | negative integer |
| 2537 | half-precision float |
| 2538 | single-precision float |
| 2539 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2540 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2541 | float Overflow error |
| 2542 | Wrong type error for epoch |
| 2543 | Wrong type error for date string |
| 2544 | float disabled error |
| 2545 | half-precision disabled error |
| 2546 | -Infinity |
| 2547 | Slightly too large integer |
| 2548 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2549 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2550 | Get epoch by int |
| 2551 | Get string by int |
| 2552 | Get epoch by string |
| 2553 | Get string by string |
| 2554 | Fail to get epoch by wrong int label |
| 2555 | Fail to get string by wrong string label |
| 2556 | Fail to get epoch by string because it is invalid |
| 2557 | Fail to get epoch by int because it is invalid |
| 2558 | |
| 2559 | Untagged values |
| 2560 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2561 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2562 | 0x86, |
| 2563 | |
| 2564 | 0xc1, |
| 2565 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 2566 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2567 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2568 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 2569 | |
| 2570 | 0xc1, // tag for epoch date |
| 2571 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 2572 | |
| 2573 | 0xc1, // tag for epoch date |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2574 | 0x80, // Erroneous empty array as content for date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2575 | |
| 2576 | 0xc0, // tag for string date |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2577 | 0xa0, // Erroneous empty map as content for date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2578 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2579 | 0xa9, // Open a map for tests involving labels. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2580 | |
| 2581 | 0x00, |
| 2582 | 0xc0, // tag for string date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2583 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2584 | |
| 2585 | 0x01, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2586 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2587 | 0xc1, // tag for epoch date |
| 2588 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2589 | |
| 2590 | // Untagged integer 0 |
| 2591 | 0x08, |
| 2592 | 0x00, |
| 2593 | |
| 2594 | // Utagged date string with string label y |
| 2595 | 0x61, 0x79, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2596 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2597 | |
| 2598 | // Untagged -1000 with label z |
| 2599 | 0x61, 0x7a, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2600 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2601 | 0x39, 0x03, 0xe7, |
| 2602 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2603 | 0x07, |
| 2604 | 0xc1, // tag for epoch date |
| 2605 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2606 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2607 | 0x05, |
| 2608 | 0xc1, |
| 2609 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 2610 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2611 | // Untagged single-precision float with value 3.14 with string label x |
| 2612 | 0x61, 0x78, |
| 2613 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 2614 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2615 | // Untagged half-precision float with value -2 |
| 2616 | 0x09, |
| 2617 | 0xF9, 0xC0, 0x00, |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2618 | }; |
| 2619 | |
| 2620 | int32_t SpiffyDateDecodeTest() |
| 2621 | { |
| 2622 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2623 | QCBORError uError; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2624 | int64_t nEpochDate2, nEpochDate3, nEpochDate5, |
| 2625 | nEpochDate4, nEpochDate6, nEpochDateFail, |
| 2626 | nEpochDate1400000000; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2627 | UsefulBufC StringDate1, StringDate2; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2628 | uint64_t uTag1, uTag2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2629 | |
| 2630 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2631 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2632 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2633 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2634 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2635 | // Too-negative float, -9.2233720368547748E+18 |
| 2636 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2637 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2638 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2639 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 2640 | return 1111; |
| 2641 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2642 | #else |
| 2643 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2644 | return 1112; |
| 2645 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2646 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2647 | |
| 2648 | // Too-large integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2649 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2650 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2651 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2652 | return 1; |
| 2653 | } |
| 2654 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2655 | // Half-precision minus infinity |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2656 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2657 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2658 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2659 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2660 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW; |
| 2661 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2662 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED; |
| 2663 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2664 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 2665 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED; |
| 2666 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 2667 | if(uError != uExpectedforHalfMinusInfinity) { |
| 2668 | return 2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2669 | } |
| 2670 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2671 | // Bad content for epoch date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2672 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2673 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2674 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
| 2675 | return 3; |
| 2676 | } |
| 2677 | |
| 2678 | // Bad content for string date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2679 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2680 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2681 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
| 2682 | return 4; |
| 2683 | } |
| 2684 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2685 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2686 | |
| 2687 | // Get largest negative double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2688 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2689 | 5, |
| 2690 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 2691 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2692 | &nEpochDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2693 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2694 | if(nEpochDate2 != -9223372036854773760LL) { |
| 2695 | return 101; |
| 2696 | } |
| 2697 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2698 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2699 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2700 | return 102; |
| 2701 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2702 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2703 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2704 | // Get largest double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2705 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2706 | &nEpochDate2); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2707 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2708 | if(nEpochDate2 != 9223372036854773760ULL) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2709 | return 111; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2710 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2711 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2712 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2713 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2714 | return 112; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2715 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2716 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2717 | |
| 2718 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2719 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2720 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2721 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2722 | if(nEpochDate5 != 3) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2723 | return 103; |
| 2724 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2725 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2726 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2727 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2728 | return 104; |
| 2729 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2730 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2731 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2732 | // A half-precision date with value -2 FFF |
| 2733 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2734 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2735 | #if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT) |
| 2736 | if(nEpochDate4 != -2) { |
| 2737 | return 105; |
| 2738 | } |
| 2739 | #else |
| 2740 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2741 | if(uError == QCBOR_SUCCESS) { |
| 2742 | return 106; |
| 2743 | } |
| 2744 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2745 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2746 | |
| 2747 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2748 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 2749 | 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 107; |
| 2754 | } |
| 2755 | |
| 2756 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2757 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2758 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2759 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2760 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2761 | return 108; |
| 2762 | } |
| 2763 | |
| 2764 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2765 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 2766 | 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 109; |
| 2771 | } |
| 2772 | |
| 2773 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2774 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2775 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2776 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2777 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2778 | return 110; |
| 2779 | } |
| 2780 | |
| 2781 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2782 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2783 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2784 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2785 | 1, |
| 2786 | QCBOR_TAG_REQUIREMENT_TAG | |
| 2787 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2788 | &nEpochDate1400000000); |
| 2789 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2790 | // Tagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2791 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2792 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2793 | // Untagged integer 0 |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2794 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2795 | &nEpochDate3); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2796 | // Untagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2797 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2798 | &StringDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2799 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2800 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 2801 | "z", |
| 2802 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 2803 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2804 | &nEpochDate6); |
| 2805 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2806 | |
| 2807 | QCBORDecode_ExitMap(&DC); |
| 2808 | QCBORDecode_ExitArray(&DC); |
| 2809 | uError = QCBORDecode_Finish(&DC); |
| 2810 | if(uError) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2811 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2814 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2815 | return 200; |
| 2816 | } |
| 2817 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2818 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2819 | return 201; |
| 2820 | } |
| 2821 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2822 | if(nEpochDate3 != 0) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2823 | return 202; |
| 2824 | } |
| 2825 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2826 | if(nEpochDate6 != -1000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2827 | return 203; |
| 2828 | } |
| 2829 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2830 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2831 | return 204; |
| 2832 | } |
| 2833 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2834 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2835 | return 205; |
| 2836 | } |
| 2837 | |
| 2838 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 2839 | return 206; |
| 2840 | } |
| 2841 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2842 | return 0; |
| 2843 | } |
| 2844 | |
| 2845 | |
| 2846 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2847 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2848 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2849 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2850 | 0x81, // Array of one |
| 2851 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 2852 | 0x82, // Array of two that is the faction 1/3 |
| 2853 | 0x01, |
| 2854 | 0x03, |
| 2855 | |
| 2856 | /* |
| 2857 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 2858 | */ |
| 2859 | 0xd8, 0xe1, |
| 2860 | 0xd8, 0xe2, |
| 2861 | 0xd8, 0xe3, |
| 2862 | 0xd8, 0xe4, |
| 2863 | 0xd8, 0xe5, |
| 2864 | 0x80, |
| 2865 | |
| 2866 | /* tag 10489608748473423768( |
| 2867 | 2442302356( |
| 2868 | 21590( |
| 2869 | 240( |
| 2870 | [])))) |
| 2871 | */ |
| 2872 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2873 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 2874 | 0xd9, 0x54, 0x56, |
| 2875 | 0xd8, 0xf0, |
| 2876 | 0x80, |
| 2877 | |
| 2878 | /* tag 21590( |
| 2879 | 10489608748473423768( |
| 2880 | 2442302357( |
| 2881 | 65534( |
| 2882 | [])))) |
| 2883 | */ |
| 2884 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 2885 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2886 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 2887 | 0xd9, 0xff, 0xfe, |
| 2888 | 0x80, |
| 2889 | |
| 2890 | /* Make sure to blow past the limit of tags that must be mapped. |
| 2891 | works in conjuntion with entries above. |
| 2892 | 269488144(269488145(269488146(269488147([])))) |
| 2893 | */ |
| 2894 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 2895 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 2896 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 2897 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 2898 | 0x80, |
| 2899 | |
| 2900 | /* An invalid decimal fraction with an additional tag */ |
| 2901 | 0xd9, 0xff, 0xfa, |
| 2902 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 2903 | 0x00, // the integer 0; should be a byte string |
| 2904 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2905 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2906 | /* |
| 2907 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2908 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2909 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2910 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2911 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2912 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2913 | /* |
| 2914 | DB 9192939495969798 # tag(10489608748473423768) |
| 2915 | D8 88 # tag(136) |
| 2916 | C6 # tag(6) |
| 2917 | C7 # tag(7) |
| 2918 | 80 # array(0) |
| 2919 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2920 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2921 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2922 | |
| 2923 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2924 | 55799(55799(55799({ |
| 2925 | 6(7(-23)): 5859837686836516696(7({ |
| 2926 | 7(-20): 11({ |
| 2927 | 17(-18): 17(17(17("Organization"))), |
| 2928 | 9(-17): 773("SSG"), |
| 2929 | -15: 16(17(6(7("Confusion")))), |
| 2930 | 17(-16): 17("San Diego"), |
| 2931 | 17(-14): 17("US") |
| 2932 | }), |
| 2933 | 23(-19): 19({ |
| 2934 | -11: 9({ |
| 2935 | -9: -7 |
| 2936 | }), |
| 2937 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 2938 | }) |
| 2939 | })), |
| 2940 | 16(-22): 23({ |
| 2941 | 11(8(7(-5))): 8(-3) |
| 2942 | }) |
| 2943 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2944 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2945 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2946 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 2947 | 0xc6, 0xc7, 0x36, |
| 2948 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 2949 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 2950 | 0xcb, 0xa5, |
| 2951 | 0xd1, 0x31, |
| 2952 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 2953 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 2954 | 0xc9, 0x30, |
| 2955 | 0xd9, 0x03, 0x05, 0x63, |
| 2956 | 0x53, 0x53, 0x47, |
| 2957 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2958 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 2959 | 0x69, |
| 2960 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2961 | 0xd1, 0x2f, |
| 2962 | 0xd1, 0x69, |
| 2963 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 2964 | 0xd1, 0x2d, |
| 2965 | 0xd1, 0x62, |
| 2966 | 0x55, 0x53, |
| 2967 | 0xd7, 0x32, |
| 2968 | 0xd3, 0xa2, |
| 2969 | 0x2a, |
| 2970 | 0xc9, 0xa1, |
| 2971 | 0x28, |
| 2972 | 0x26, |
| 2973 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 2974 | 0xcc, 0x4a, |
| 2975 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 2976 | 0xd0, 0x35, |
| 2977 | 0xd7, 0xa1, |
| 2978 | 0xcb, 0xc8, 0xc7, 0x24, |
| 2979 | 0xc8, 0x22}; |
| 2980 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2981 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2982 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2983 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2984 | |
| 2985 | 0xc0, // tag for string date |
| 2986 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2987 | |
| 2988 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2989 | |
| 2990 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 2991 | |
| 2992 | 0xd8, 0x23, // tag for regex |
| 2993 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2994 | |
| 2995 | 0xc0, // tag for string date |
| 2996 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2997 | }; |
| 2998 | |
| 2999 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3000 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3001 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3002 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3003 | int32_t OptTagParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3004 | { |
| 3005 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3006 | QCBORItem Item; |
| 3007 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3008 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3009 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3010 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3011 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3012 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3013 | /* |
| 3014 | This test matches the magic number tag and the fraction tag |
| 3015 | 55799([...]) |
| 3016 | */ |
| 3017 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3018 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3019 | return -2; |
| 3020 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3021 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3022 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 3023 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3024 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3025 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3026 | /* |
| 3027 | 4([1,3]) |
| 3028 | */ |
| 3029 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3030 | #ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 3031 | if(uError != QCBOR_SUCCESS || |
| 3032 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3033 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 3034 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 3035 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3036 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3037 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3038 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 3039 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3040 | return -4; |
| 3041 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3042 | // consume the items in the array |
| 3043 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3044 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3045 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3046 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3047 | if(uError != QCBOR_SUCCESS || |
| 3048 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3049 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 3050 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3051 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3052 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3053 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 3054 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3055 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3056 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3057 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3058 | /* |
| 3059 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3060 | */ |
| 3061 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3062 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3063 | return -6; |
| 3064 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3065 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 3066 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 3067 | return -106; |
| 3068 | } |
| 3069 | |
| 3070 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3071 | /* tag 10489608748473423768( |
| 3072 | 2442302356( |
| 3073 | 21590( |
| 3074 | 240( |
| 3075 | [])))) |
| 3076 | */ |
| 3077 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3078 | if(uError != QCBOR_SUCCESS || |
| 3079 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3080 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 3081 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 3082 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 3083 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3084 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | /* tag 21590( |
| 3088 | 10489608748473423768( |
| 3089 | 2442302357( |
| 3090 | 21591( |
| 3091 | [])))) |
| 3092 | */ |
| 3093 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3094 | if(uError != QCBOR_SUCCESS || |
| 3095 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3096 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 3097 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 3098 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 3099 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 3100 | return -8; |
| 3101 | } |
| 3102 | |
| 3103 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3104 | works in conjuntion with entries above. |
| 3105 | 269488144(269488145(269488146(269488147([])))) |
| 3106 | */ |
| 3107 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3108 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3109 | return -9; |
| 3110 | } |
| 3111 | |
| 3112 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3113 | if(uError == QCBOR_SUCCESS) { |
| 3114 | return -10; |
| 3115 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3116 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3117 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3118 | // 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] | 3119 | // tage and then matches it. Caller-config lists are no longer |
| 3120 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3121 | QCBORDecode_Init(&DCtx, |
| 3122 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3123 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3124 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 3125 | const QCBORTagListIn TL = {2, puList}; |
| 3126 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3127 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3128 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3129 | return -8; |
| 3130 | } |
| 3131 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3132 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 3133 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 3134 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 3135 | Item.val.uCount != 0) { |
| 3136 | return -9; |
| 3137 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3138 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3139 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3140 | // 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] | 3141 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3142 | const uint64_t puLongList[17] = {1,2,1}; |
| 3143 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3144 | QCBORDecode_Init(&DCtx, |
| 3145 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3146 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3147 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 3148 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3149 | return -11; |
| 3150 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3151 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3152 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3153 | QCBORTagListOut Out = {0, 4, puTags}; |
| 3154 | |
| 3155 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3156 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3157 | QCBORDecode_Init(&DCtx, |
| 3158 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3159 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3160 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3161 | return -12; |
| 3162 | } |
| 3163 | if(puTags[0] != 0x9192939495969798 || |
| 3164 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3165 | puTags[2] != 0x06 || |
| 3166 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3167 | return -13; |
| 3168 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3169 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3170 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3171 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3172 | QCBORDecode_Init(&DCtx, |
| 3173 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3174 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3175 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 3176 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3177 | return -14; |
| 3178 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3179 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3180 | |
| 3181 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3182 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3183 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 3184 | // It is a bit of a messy test and maybe could be improved, but |
| 3185 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3186 | QCBORDecode_Init(&DCtx, |
| 3187 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3188 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3189 | int n = CheckCSRMaps(&DCtx); |
| 3190 | if(n) { |
| 3191 | return n-2000; |
| 3192 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3193 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3194 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3195 | QCBORDecode_Init(&DCtx, |
| 3196 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3197 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3198 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3199 | /* With the spiffy decode revision, this tag list is not used. |
| 3200 | It doesn't matter if a tag is in this list or not so some |
| 3201 | tests that couldn't process a tag because it isn't in this list |
| 3202 | now can process these unlisted tags. The tests have been |
| 3203 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3204 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 3205 | const QCBORTagListIn TagList = {3, puTagList}; |
| 3206 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3207 | |
| 3208 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3209 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3210 | return -100; |
| 3211 | } |
| 3212 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3213 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3214 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 3215 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 3216 | Item.val.uCount != 2 || |
| 3217 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 3218 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 3219 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 3220 | Out.uNumUsed != 3) { |
| 3221 | return -101; |
| 3222 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3223 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3224 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3225 | return -102; |
| 3226 | } |
| 3227 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3228 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3229 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3230 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3231 | Item.val.uCount != 2 || |
| 3232 | puTags[0] != 5859837686836516696 || |
| 3233 | puTags[1] != 7 || |
| 3234 | Out.uNumUsed != 2) { |
| 3235 | return -103; |
| 3236 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3237 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3238 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3239 | return -104; |
| 3240 | } |
| 3241 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3242 | Item.val.uCount != 5 || |
| 3243 | puTags[0] != 0x0b || |
| 3244 | Out.uNumUsed != 1) { |
| 3245 | return -105; |
| 3246 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3247 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3248 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3249 | return -106; |
| 3250 | } |
| 3251 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3252 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 3253 | Item.val.string.len != 12 || |
| 3254 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 3255 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 3256 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 3257 | Out.uNumUsed != 3) { |
| 3258 | return -105; |
| 3259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3260 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3261 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3262 | return -107; |
| 3263 | } |
| 3264 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3265 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 3266 | Item.val.string.len != 3 || |
| 3267 | puTags[0] != 773 || |
| 3268 | Out.uNumUsed != 1) { |
| 3269 | return -108; |
| 3270 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3271 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3272 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3273 | return -109; |
| 3274 | } |
| 3275 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3276 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3277 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3278 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3279 | puTags[3] != 7 || |
| 3280 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3281 | return -110; |
| 3282 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3283 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3284 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3285 | return -111; |
| 3286 | } |
| 3287 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3288 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3289 | Item.val.string.len != 9 || |
| 3290 | puTags[0] != 17 || |
| 3291 | Out.uNumUsed != 1) { |
| 3292 | return -112; |
| 3293 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3294 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3295 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3296 | return -111; |
| 3297 | } |
| 3298 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3299 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3300 | Item.val.string.len != 2 || |
| 3301 | puTags[0] != 17 || |
| 3302 | Out.uNumUsed != 1) { |
| 3303 | return -112; |
| 3304 | } |
| 3305 | |
| 3306 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3307 | return -113; |
| 3308 | } |
| 3309 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3310 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3311 | Item.val.uCount != 2 || |
| 3312 | puTags[0] != 19 || |
| 3313 | Out.uNumUsed != 1) { |
| 3314 | return -114; |
| 3315 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3316 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3317 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3318 | return -115; |
| 3319 | } |
| 3320 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3321 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3322 | Item.val.uCount != 1 || |
| 3323 | puTags[0] != 9 || |
| 3324 | Out.uNumUsed != 1) { |
| 3325 | return -116; |
| 3326 | } |
| 3327 | |
| 3328 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3329 | return -116; |
| 3330 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3331 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3332 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3333 | Out.uNumUsed != 0) { |
| 3334 | return -117; |
| 3335 | } |
| 3336 | |
| 3337 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3338 | return -118; |
| 3339 | } |
| 3340 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 3341 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3342 | puTags[0] != 12 || |
| 3343 | Out.uNumUsed != 1) { |
| 3344 | return -119; |
| 3345 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3346 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3347 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3348 | return -120; |
| 3349 | } |
| 3350 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3351 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 3352 | Item.val.uCount != 1 || |
| 3353 | puTags[0] != 0x17 || |
| 3354 | Out.uNumUsed != 1) { |
| 3355 | return -121; |
| 3356 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3357 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3358 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3359 | return -122; |
| 3360 | } |
| 3361 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3362 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3363 | Item.val.int64 != -3 || |
| 3364 | puTags[0] != 8 || |
| 3365 | Out.uNumUsed != 1) { |
| 3366 | return -123; |
| 3367 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3368 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3369 | if(QCBORDecode_Finish(&DCtx)) { |
| 3370 | return -124; |
| 3371 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3372 | |
| 3373 | UsefulBufC DateString; |
| 3374 | QCBORDecode_Init(&DCtx, |
| 3375 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3376 | QCBOR_DECODE_MODE_NORMAL); |
| 3377 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3378 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3379 | // tagged date string |
| 3380 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3381 | // untagged date string |
| 3382 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3383 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3384 | return 100; |
| 3385 | } |
| 3386 | // untagged byte string |
| 3387 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3388 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3389 | return 101; |
| 3390 | } |
| 3391 | // tagged regex |
| 3392 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3393 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3394 | return 102; |
| 3395 | } |
| 3396 | // tagged date string with a byte string |
| 3397 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3398 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3399 | return 103; |
| 3400 | } |
| 3401 | QCBORDecode_ExitArray(&DCtx); |
| 3402 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3403 | return 104; |
| 3404 | } |
| 3405 | |
| 3406 | |
| 3407 | QCBORDecode_Init(&DCtx, |
| 3408 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3409 | QCBOR_DECODE_MODE_NORMAL); |
| 3410 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3411 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3412 | // tagged date string |
| 3413 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3414 | // untagged date string |
| 3415 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3416 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3417 | return 200; |
| 3418 | } |
| 3419 | // untagged byte string |
| 3420 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3421 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3422 | return 201; |
| 3423 | } |
| 3424 | // tagged regex |
| 3425 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3426 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3427 | return 202; |
| 3428 | } |
| 3429 | // tagged date string with a byte string |
| 3430 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3431 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3432 | return 203; |
| 3433 | } |
| 3434 | QCBORDecode_ExitArray(&DCtx); |
| 3435 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3436 | return 204; |
| 3437 | } |
| 3438 | |
| 3439 | QCBORDecode_Init(&DCtx, |
| 3440 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3441 | QCBOR_DECODE_MODE_NORMAL); |
| 3442 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3443 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3444 | // tagged date string |
| 3445 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3446 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3447 | return 300; |
| 3448 | } |
| 3449 | // untagged date string |
| 3450 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3451 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3452 | return 301; |
| 3453 | } |
| 3454 | // untagged byte string |
| 3455 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3456 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3457 | return 302; |
| 3458 | } |
| 3459 | // tagged regex |
| 3460 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3461 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3462 | return 303; |
| 3463 | } |
| 3464 | // tagged date string with a byte string |
| 3465 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3466 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3467 | return 304; |
| 3468 | } |
| 3469 | QCBORDecode_ExitArray(&DCtx); |
| 3470 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3471 | return 305; |
| 3472 | } |
| 3473 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3474 | return 0; |
| 3475 | } |
| 3476 | |
| 3477 | |
| 3478 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3479 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3480 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3481 | 0x83, |
| 3482 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3483 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3484 | 0xA4, |
| 3485 | 0x63, 0x42, 0x4E, 0x2B, |
| 3486 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3487 | 0x18, 0x40, |
| 3488 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3489 | 0x63, 0x42, 0x4E, 0x2D, |
| 3490 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3491 | 0x38, 0x3F, |
| 3492 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 3493 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3494 | /* The expected big num */ |
| 3495 | static const uint8_t spBigNum[] = { |
| 3496 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3497 | 0x00}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3498 | |
| 3499 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3500 | int32_t BignumParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3501 | { |
| 3502 | QCBORDecodeContext DCtx; |
| 3503 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3504 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3505 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3506 | QCBORDecode_Init(&DCtx, |
| 3507 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 3508 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3509 | |
| 3510 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3511 | // |
| 3512 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 3513 | return -1; |
| 3514 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3515 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3516 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3517 | |
| 3518 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3519 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3520 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3521 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3522 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3523 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | // |
| 3527 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3528 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3529 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3530 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3531 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3532 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3533 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3534 | // |
| 3535 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3536 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3537 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3538 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3539 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3540 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3541 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3542 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3543 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3544 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3545 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3546 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3550 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3551 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3552 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3553 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3554 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3555 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3556 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3557 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3558 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3559 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3560 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3561 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3562 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3563 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3564 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3565 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3566 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3567 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3568 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3569 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3570 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3571 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3572 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3573 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3574 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | |
| 3579 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3580 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3581 | uint8_t uDataType, |
| 3582 | uint8_t uNestingLevel, |
| 3583 | uint8_t uNextNest, |
| 3584 | int64_t nLabel, |
| 3585 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3586 | { |
| 3587 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3588 | QCBORError nCBORError; |
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 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 3591 | if(Item.uDataType != uDataType) return -1; |
| 3592 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3593 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 3594 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 3595 | return -1; |
| 3596 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3597 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3598 | if(Item.label.int64 != nLabel) return -1; |
| 3599 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3600 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3604 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3605 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3606 | if(pItem) { |
| 3607 | *pItem = Item; |
| 3608 | } |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
| 3612 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3613 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3614 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3615 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3616 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3617 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3618 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3619 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3620 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3621 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3622 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 3623 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 3624 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 3625 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 3626 | 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] | 3627 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3628 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 3629 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3630 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3631 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 3632 | 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] | 3633 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3634 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 3635 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3636 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3637 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3638 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3639 | return 0; |
| 3640 | } |
| 3641 | |
| 3642 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3643 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3644 | { |
| 3645 | -23: { |
| 3646 | -20: { |
| 3647 | -18: "Organization", |
| 3648 | -17: "SSG", |
| 3649 | -15: "Confusion", |
| 3650 | -16: "San Diego", |
| 3651 | -14: "US" |
| 3652 | }, |
| 3653 | -19: { |
| 3654 | -11: { |
| 3655 | -9: -7 |
| 3656 | }, |
| 3657 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 3658 | } |
| 3659 | }, |
| 3660 | -22: { |
| 3661 | -5: -3 |
| 3662 | } |
| 3663 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3664 | */ |
| 3665 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3666 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 3667 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3668 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3669 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3670 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3671 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3672 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 3673 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 3674 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 3675 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3676 | // Same map as above, but using indefinite lengths |
| 3677 | static const uint8_t spCSRInputIndefLen[] = { |
| 3678 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 3679 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3680 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3681 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3682 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3683 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3684 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 3685 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 3686 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 3687 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 3688 | |
| 3689 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3690 | int32_t NestedMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3691 | { |
| 3692 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3693 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3694 | QCBORDecode_Init(&DCtx, |
| 3695 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3696 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3697 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3698 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3699 | } |
| 3700 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3701 | |
| 3702 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3703 | int32_t StringDecoderModeFailTest() |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3704 | { |
| 3705 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3706 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3707 | QCBORDecode_Init(&DCtx, |
| 3708 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3709 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3710 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3711 | QCBORItem Item; |
| 3712 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3713 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3714 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3715 | return -1; |
| 3716 | } |
| 3717 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3718 | return -2; |
| 3719 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3720 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3721 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3722 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 3723 | return -3; |
| 3724 | } |
| 3725 | |
| 3726 | return 0; |
| 3727 | } |
| 3728 | |
| 3729 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3730 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3731 | int32_t NestedMapTestIndefLen() |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3732 | { |
| 3733 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3734 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3735 | QCBORDecode_Init(&DCtx, |
| 3736 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 3737 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3738 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3739 | return CheckCSRMaps(&DCtx); |
| 3740 | } |
| 3741 | |
| 3742 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3743 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3744 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 3745 | { |
| 3746 | UsefulOutBuf UOB; |
| 3747 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3748 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3749 | int i; |
| 3750 | for(i = 0; i < n; i++) { |
| 3751 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 3752 | } |
| 3753 | |
| 3754 | for(i = 0; i < n; i++) { |
| 3755 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3756 | } |
| 3757 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3758 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3759 | |
| 3760 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3761 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3762 | { |
| 3763 | QCBORDecodeContext DC; |
| 3764 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3765 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3766 | int j; |
| 3767 | for(j = 0; j < nNestLevel; j++) { |
| 3768 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3769 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3770 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 3771 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3772 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3773 | return -4; |
| 3774 | } else { |
| 3775 | return 0; // Decoding doesn't recover after an error |
| 3776 | } |
| 3777 | } else { |
| 3778 | // Should be no error |
| 3779 | if(nReturn) { |
| 3780 | return -9; // Should not have got an error |
| 3781 | } |
| 3782 | } |
| 3783 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3784 | return -7; |
| 3785 | } |
| 3786 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3787 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3788 | if(nReturn) { |
| 3789 | return -3; |
| 3790 | } |
| 3791 | return 0; |
| 3792 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3793 | |
| 3794 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3795 | int32_t IndefiniteLengthNestTest() |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3796 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3797 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3798 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3799 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3800 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3801 | int nReturn = parse_indeflen_nested(Nested, i); |
| 3802 | if(nReturn) { |
| 3803 | return nReturn; |
| 3804 | } |
| 3805 | } |
| 3806 | return 0; |
| 3807 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3808 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3809 | // [1, [2, 3]] |
| 3810 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 3811 | // No closing break |
| 3812 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 3813 | // Not enough closing breaks |
| 3814 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 3815 | // Too many closing breaks |
| 3816 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 3817 | // Unclosed indeflen inside def len |
| 3818 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 3819 | // confused tag |
| 3820 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3821 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3822 | int32_t IndefiniteLengthArrayMapTest() |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3823 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3824 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3825 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3826 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3827 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3828 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3829 | QCBORDecodeContext DC; |
| 3830 | QCBORItem Item; |
| 3831 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3832 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3833 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3834 | |
| 3835 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3836 | Item.uNestingLevel != 0 || |
| 3837 | Item.uNextNestLevel != 1) { |
| 3838 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3839 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3840 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3841 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3842 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 3843 | Item.uNestingLevel != 1 || |
| 3844 | Item.uNextNestLevel != 1) { |
| 3845 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3846 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3847 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3848 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3849 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3850 | Item.uNestingLevel != 1 || |
| 3851 | Item.uNextNestLevel != 2) { |
| 3852 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3853 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3854 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3855 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3856 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3857 | Item.uNestingLevel != 2 || |
| 3858 | Item.uNextNestLevel != 2) { |
| 3859 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3860 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3861 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3862 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3863 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3864 | Item.uNestingLevel != 2 || |
| 3865 | Item.uNextNestLevel != 0) { |
| 3866 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3867 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3868 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3869 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3870 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3871 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3872 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3873 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3874 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3875 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3876 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3877 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3878 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3879 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3880 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3881 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3882 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3883 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3884 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3885 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3886 | } |
| 3887 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3888 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3889 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3890 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3891 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3892 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3893 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3894 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3895 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3896 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3900 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3901 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3902 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3903 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3904 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3905 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3906 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3907 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3908 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3909 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3910 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3911 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3912 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3913 | |
| 3914 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3915 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3916 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3917 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3918 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3919 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3920 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3921 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3922 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3923 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3924 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3925 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3926 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3927 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3928 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3929 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3930 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3931 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3932 | return -140; |
| 3933 | } |
| 3934 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3935 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3936 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3937 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
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 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3940 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3941 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3942 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3943 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3944 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3945 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3946 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3947 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3948 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3949 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3950 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3951 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3952 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3953 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3954 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3955 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3956 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3957 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3958 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3959 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3960 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3961 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3962 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3963 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3964 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3965 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3966 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3967 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3968 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3969 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3970 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3971 | return 0; |
| 3972 | } |
| 3973 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3974 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 3975 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 3976 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3977 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3978 | 0x81, // Array of length one |
| 3979 | 0x7f, // text string marked with indefinite length |
| 3980 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3981 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3982 | 0xff // ending break |
| 3983 | }; |
| 3984 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3985 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3986 | 0x81, // Array of length one |
| 3987 | 0x7f, // text string marked with indefinite length |
| 3988 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3989 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 3990 | 0xff // ending break |
| 3991 | }; |
| 3992 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3993 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3994 | 0x81, // Array of length one |
| 3995 | 0x7f, // text string marked with indefinite length |
| 3996 | 0x01, 0x02, // Not a string |
| 3997 | 0xff // ending break |
| 3998 | }; |
| 3999 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4000 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4001 | 0x81, // Array of length one |
| 4002 | 0x7f, // text string marked with indefinite length |
| 4003 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 4004 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4005 | // missing end of string |
| 4006 | }; |
| 4007 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4008 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4009 | 0xa1, // Array of length one |
| 4010 | 0x7f, // text string marked with indefinite length |
| 4011 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 4012 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 4013 | 0xff, // ending break |
| 4014 | 0x01 // integer being labeled. |
| 4015 | }; |
| 4016 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4017 | /** |
| 4018 | Make an indefinite length string |
| 4019 | |
| 4020 | @param Storage Storage for string, must be 144 bytes in size |
| 4021 | @return The indefinite length string |
| 4022 | |
| 4023 | This makes an array with one indefinite length string that has 7 chunks |
| 4024 | from size of 1 byte up to 64 bytes. |
| 4025 | */ |
| 4026 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4027 | { |
| 4028 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4029 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4030 | UsefulOutBuf_Init(&UOB, Storage); |
| 4031 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 4032 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4033 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4034 | uint8_t uStringByte = 0; |
| 4035 | // Use of type int is intentional |
| 4036 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 4037 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4038 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4039 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 4040 | for(int j = 0; j < uChunkSize; j++) { |
| 4041 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 4042 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4046 | |
| 4047 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4048 | } |
| 4049 | |
| 4050 | static int CheckBigString(UsefulBufC BigString) |
| 4051 | { |
| 4052 | if(BigString.len != 255) { |
| 4053 | return 1; |
| 4054 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4055 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4056 | for(uint8_t i = 0; i < 255; i++){ |
| 4057 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 4058 | return 1; |
| 4059 | } |
| 4060 | } |
| 4061 | return 0; |
| 4062 | } |
| 4063 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4064 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4065 | int32_t IndefiniteLengthStringTest() |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4066 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4067 | QCBORDecodeContext DC; |
| 4068 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4069 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4070 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4071 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4072 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4073 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4074 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4075 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4076 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4077 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4078 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4079 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4080 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4081 | return -2; |
| 4082 | } |
| 4083 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 4084 | return -3; |
| 4085 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4086 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4087 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4088 | return -4; |
| 4089 | } |
| 4090 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 4091 | return -5; |
| 4092 | } |
| 4093 | if(QCBORDecode_Finish(&DC)) { |
| 4094 | return -6; |
| 4095 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4096 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4097 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4098 | QCBORDecode_Init(&DC, |
| 4099 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 4100 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4101 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4102 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4103 | return -7; |
| 4104 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4105 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4106 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4107 | return -8; |
| 4108 | } |
| 4109 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4110 | return -9; |
| 4111 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4112 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4113 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4114 | return -10; |
| 4115 | } |
| 4116 | |
| 4117 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4118 | QCBORDecode_Init(&DC, |
| 4119 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 4120 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4121 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4122 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4123 | return -11; |
| 4124 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4125 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4126 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4127 | return -12; |
| 4128 | } |
| 4129 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4130 | return -13; |
| 4131 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4132 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4133 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4134 | return -14; |
| 4135 | } |
| 4136 | |
| 4137 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4138 | QCBORDecode_Init(&DC, |
| 4139 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 4140 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4141 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4142 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4143 | return -15; |
| 4144 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4145 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4146 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4147 | return -16; |
| 4148 | } |
| 4149 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4150 | return -17; |
| 4151 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4152 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4153 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 4154 | return -18; |
| 4155 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4156 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4157 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4158 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4159 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4160 | QCBORDecode_GetNext(&DC, &Item); |
| 4161 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4162 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4163 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4164 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4165 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4166 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4167 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4168 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4169 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4170 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4171 | |
| 4172 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 4173 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4174 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4175 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4176 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4177 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4178 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4179 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4180 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4181 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 4182 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4183 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4184 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4185 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4186 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4187 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4188 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4189 | QCBORDecode_GetNext(&DC, &Item); |
| 4190 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4191 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4192 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4193 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4194 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4195 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4196 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4197 | // ---- big bstr ----- |
| 4198 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4199 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4200 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4201 | return -25; |
| 4202 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4203 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4204 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4205 | return -26; |
| 4206 | } |
| 4207 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4208 | return -26; |
| 4209 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4210 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4211 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4212 | return -27; |
| 4213 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4214 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4215 | return -28; |
| 4216 | } |
| 4217 | if(CheckBigString(Item.val.string)) { |
| 4218 | return -3; |
| 4219 | } |
| 4220 | if(QCBORDecode_Finish(&DC)) { |
| 4221 | return -29; |
| 4222 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4223 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4224 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4225 | 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] | 4226 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4227 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4228 | return -30; |
| 4229 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4230 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4231 | QCBORDecode_GetNext(&DC, &Item); |
| 4232 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4233 | return -31; |
| 4234 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4235 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4236 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 4237 | return -32; |
| 4238 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4239 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4240 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4241 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 4242 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 4243 | return -33; |
| 4244 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4245 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4246 | if(QCBORDecode_Finish(&DC)) { |
| 4247 | return -34; |
| 4248 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4249 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4250 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4254 | int32_t AllocAllStringsTest() |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4255 | { |
| 4256 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4257 | QCBORError nCBORError; |
| 4258 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4259 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4260 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4261 | QCBORDecode_Init(&DC, |
| 4262 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4263 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4264 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4265 | 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] | 4266 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4267 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 4268 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4269 | return -1; |
| 4270 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4271 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4272 | if(CheckCSRMaps(&DC)) { |
| 4273 | return -2; |
| 4274 | } |
| 4275 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 4276 | // Next parse, save pointers to a few strings, destroy original and |
| 4277 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4278 | 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] | 4279 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4280 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4281 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 4282 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4283 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4284 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4285 | QCBORItem Item1, Item2, Item3, Item4; |
| 4286 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4287 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4288 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 4289 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4290 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4291 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4292 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4293 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4294 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4295 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4296 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4297 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4298 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4299 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 4300 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4301 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4302 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4303 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 4304 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4305 | Item1.uDataAlloc != 0 || |
| 4306 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4307 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4308 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4309 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4310 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4311 | |
| 4312 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4313 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4314 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4315 | Item2.uDataAlloc != 0 || |
| 4316 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4317 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4318 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4319 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4320 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4321 | Item3.uDataAlloc == 0 || |
| 4322 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4323 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4324 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4325 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4326 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4327 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4328 | Item4.uDataAlloc == 0 || |
| 4329 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4330 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4331 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4332 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4333 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4334 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4335 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4336 | QCBORDecode_Init(&DC, |
| 4337 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 4338 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4339 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 4340 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4341 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4342 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4343 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4344 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4345 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4346 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 4347 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 4348 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 4349 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 4350 | } |
| 4351 | } |
| 4352 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4353 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4354 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | return 0; |
| 4358 | } |
| 4359 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4360 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4361 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 4362 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4363 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4364 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4365 | QCBORDecodeContext DC; |
| 4366 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 4367 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4368 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4369 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4370 | // Then fish into the internals of the decode context |
| 4371 | // to get the allocator function so it can be called directly. |
| 4372 | // Also figure out how much pool is available for use |
| 4373 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4374 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4375 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 4376 | if(nError) { |
| 4377 | return -9; |
| 4378 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4379 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 4380 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4381 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4382 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4383 | // First test -- ask for one more byte than available and see failure |
| 4384 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4385 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 4386 | return -1; |
| 4387 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4388 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4389 | // Re do the set up for the next test that will do a successful alloc, |
| 4390 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4391 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4392 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4393 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4394 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4395 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4396 | // Allocate one byte less than available and see success |
| 4397 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4398 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4399 | return -2; |
| 4400 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4401 | // Ask for some more and see failure |
| 4402 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4403 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 4404 | return -3; |
| 4405 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4406 | // Free the first allocate, retry the second and see success |
| 4407 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 4408 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4409 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 4410 | return -4; |
| 4411 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4412 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4413 | // Re do set up for next test that involves a successful alloc, |
| 4414 | // and a successful realloc and a failed realloc |
| 4415 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4416 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4417 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4418 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4419 | // Allocate half the pool and see success |
| 4420 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4421 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4422 | return -5; |
| 4423 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4424 | // Reallocate to take up the whole pool and see success |
| 4425 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4426 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 4427 | return -6; |
| 4428 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4429 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4430 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 4431 | return -7; |
| 4432 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4433 | // Try to allocate more to be sure there is failure after a realloc |
| 4434 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 4435 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4436 | return -8; |
| 4437 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4438 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4439 | return 0; |
| 4440 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4441 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4442 | |
| 4443 | /* Just enough of an allocator to test configuration of one */ |
| 4444 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 4445 | { |
| 4446 | (void)pOldMem; // unused variable |
| 4447 | |
| 4448 | if(uNewSize) { |
| 4449 | // Assumes the context pointer is the buffer and |
| 4450 | // nothing too big will ever be asked for. |
| 4451 | // This is only good for this basic test! |
| 4452 | return (UsefulBuf) {pCtx, uNewSize}; |
| 4453 | } else { |
| 4454 | return NULLUsefulBuf; |
| 4455 | } |
| 4456 | } |
| 4457 | |
| 4458 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4459 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4460 | { |
| 4461 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4462 | // nothing can be done with it unless that is set up. |
| 4463 | QCBORDecodeContext DC; |
| 4464 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 4465 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 4466 | |
| 4467 | uint8_t pAllocatorBuffer[50]; |
| 4468 | |
| 4469 | // This is really just to test that this call works. |
| 4470 | // The full functionality of string allocators is tested |
| 4471 | // elsewhere with the MemPool internal allocator. |
| 4472 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 4473 | |
| 4474 | QCBORItem Item; |
| 4475 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 4476 | return -1; |
| 4477 | } |
| 4478 | |
| 4479 | if(Item.uDataAlloc == 0 || |
| 4480 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4481 | Item.val.string.ptr != pAllocatorBuffer) { |
| 4482 | return -2; |
| 4483 | } |
| 4484 | |
| 4485 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 4486 | return -3; |
| 4487 | } |
| 4488 | |
| 4489 | return 0; |
| 4490 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4491 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4492 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4493 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4494 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4495 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4496 | /* exponent, mantissa |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4497 | [ |
| 4498 | 4([-1, 3]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4499 | 4([-20, 4759477275222530853136]), |
| 4500 | 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4501 | 5([300, 100]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4502 | 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4503 | 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4504 | 5([ 9223372036854775806, -4759477275222530853137]) |
| 4505 | 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4506 | ] |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4507 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4508 | static const uint8_t spExpectedExponentsAndMantissas[] = { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4509 | 0x88, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4510 | 0xC4, 0x82, 0x20, |
| 4511 | 0x03, |
| 4512 | 0xC4, 0x82, 0x33, |
| 4513 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4514 | 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4515 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4516 | 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 4517 | 0x18, 0x64, |
| 4518 | 0xC5, 0x82, 0x33, |
| 4519 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4520 | 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4521 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4522 | 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4523 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4524 | 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4525 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE |
| 4526 | }; |
| 4527 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4528 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4529 | int32_t ExponentAndMantissaDecodeTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4530 | { |
| 4531 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4532 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4533 | QCBORItem item; |
| 4534 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 4535 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 4536 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 4537 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4538 | |
| 4539 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4540 | QCBORDecode_Init(&DC, |
| 4541 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4542 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4543 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4544 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4545 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4546 | return 1; |
| 4547 | } |
| 4548 | |
| 4549 | if(item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4550 | return 2; |
| 4551 | } |
| 4552 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4553 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4554 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4555 | return 3; |
| 4556 | } |
| 4557 | |
| 4558 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4559 | item.val.expAndMantissa.Mantissa.nInt != 3 || |
| 4560 | item.val.expAndMantissa.nExponent != -1) { |
| 4561 | return 4; |
| 4562 | } |
| 4563 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4564 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4565 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4566 | return 5; |
| 4567 | } |
| 4568 | |
| 4569 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4570 | item.val.expAndMantissa.nExponent != -20 || |
| 4571 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4572 | return 6; |
| 4573 | } |
| 4574 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4575 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4576 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4577 | return 7; |
| 4578 | } |
| 4579 | |
| 4580 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM || |
| 4581 | item.val.expAndMantissa.nExponent != 9223372036854775807 || |
| 4582 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4583 | return 8; |
| 4584 | } |
| 4585 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4586 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4587 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4588 | return 9; |
| 4589 | } |
| 4590 | |
| 4591 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4592 | item.val.expAndMantissa.Mantissa.nInt != 100 || |
| 4593 | item.val.expAndMantissa.nExponent != 300) { |
| 4594 | return 10; |
| 4595 | } |
| 4596 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4597 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4598 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4599 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4600 | return 11; |
| 4601 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4602 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM || |
| 4603 | item.val.expAndMantissa.nExponent != -20 || |
| 4604 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4605 | return 12; |
| 4606 | } |
| 4607 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4608 | // 5([-9223372036854775807, -4759477275222530853137]) |
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 13; |
| 4612 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4613 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4614 | item.val.expAndMantissa.nExponent != -9223372036854775807 || |
| 4615 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4616 | return 14; |
| 4617 | } |
| 4618 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4619 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4620 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4621 | if(uErr != QCBOR_SUCCESS) { |
| 4622 | return 15; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4623 | } |
| 4624 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4625 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4626 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4627 | return 16; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4628 | } |
| 4629 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4630 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4631 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4632 | if(uErr != QCBOR_SUCCESS) { |
| 4633 | return 17; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4634 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4635 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4636 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4637 | item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4638 | return 18; |
| 4639 | } |
| 4640 | |
| 4641 | uErr = QCBORDecode_Finish(&DC); |
| 4642 | if(uErr != QCBOR_SUCCESS) { |
| 4643 | return 18; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | /* Now encode some stuff and then decode it */ |
| 4647 | uint8_t pBuf[40]; |
| 4648 | QCBOREncodeContext EC; |
| 4649 | UsefulBufC Encoded; |
| 4650 | |
| 4651 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 4652 | QCBOREncode_OpenArray(&EC); |
| 4653 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 4654 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 4655 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 4656 | QCBOREncode_CloseArray(&EC); |
| 4657 | QCBOREncode_Finish(&EC, &Encoded); |
| 4658 | |
| 4659 | |
| 4660 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4661 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4662 | if(uErr != QCBOR_SUCCESS) { |
| 4663 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4664 | } |
| 4665 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4666 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4667 | if(uErr != QCBOR_SUCCESS) { |
| 4668 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4672 | item.val.expAndMantissa.nExponent != 1000 || |
| 4673 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4674 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4675 | } |
| 4676 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4677 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4678 | if(uErr != QCBOR_SUCCESS) { |
| 4679 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4683 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 4684 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4685 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4686 | } |
| 4687 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4688 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4689 | if(uErr != QCBOR_SUCCESS) { |
| 4690 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4694 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 4695 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4696 | return 106; |
| 4697 | } |
| 4698 | |
| 4699 | |
| 4700 | int64_t nExp, nMant; |
| 4701 | UsefulBuf_MAKE_STACK_UB( MantBuf, 20); |
| 4702 | UsefulBufC Mant; |
| 4703 | bool bIsNeg; |
| 4704 | |
| 4705 | QCBORDecode_Init(&DC, |
| 4706 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4707 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4708 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4709 | |
| 4710 | // 4([-1, 3]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4711 | QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4712 | |
| 4713 | // 4([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4714 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, |
| 4715 | &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4716 | |
| 4717 | // 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4718 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4719 | MantBuf, &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4720 | |
| 4721 | // 5([300, 100]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4722 | QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4723 | |
| 4724 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4725 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4726 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4727 | |
| 4728 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4729 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4730 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4731 | |
| 4732 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4733 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4734 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4735 | |
| 4736 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4737 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4738 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4739 | |
| 4740 | QCBORDecode_ExitArray(&DC); |
| 4741 | |
| 4742 | uErr = QCBORDecode_Finish(&DC); |
| 4743 | if(uErr != QCBOR_SUCCESS) { |
| 4744 | return 200; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | return 0; |
| 4748 | } |
| 4749 | |
| 4750 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4751 | static const struct FailInput ExponentAndMantissaFailures[] = { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4752 | // Exponent > INT64_MAX |
| 4753 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4754 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4755 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4756 | // Mantissa > INT64_MAX |
| 4757 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4758 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 4759 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4760 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4761 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4762 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4763 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4764 | // bad content for big num |
| 4765 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 4766 | // bad content for big num |
| 4767 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 4768 | // Bad integer for exponent |
| 4769 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 4770 | // Bad integer for mantissa |
| 4771 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 4772 | // 3 items in array |
| 4773 | { {(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] | 4774 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4775 | // unterminated indefinite length array |
| 4776 | { {(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] | 4777 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4778 | // unterminated indefinite length array |
| 4779 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED}, |
| 4780 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4781 | // Empty array |
| 4782 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 4783 | // Second is not an integer |
| 4784 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4785 | // First is not an integer |
| 4786 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4787 | // Not an array |
| 4788 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 4789 | }; |
| 4790 | |
| 4791 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4792 | int32_t ExponentAndMantissaDecodeFailTests() |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4793 | { |
| 4794 | return ProcessFailures(ExponentAndMantissaFailures, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4795 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 4796 | struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4800 | |
| 4801 | |
| 4802 | |
| 4803 | /* |
| 4804 | Some basic CBOR with map and array used in a lot of tests. |
| 4805 | The map labels are all strings |
| 4806 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 4807 | { |
| 4808 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4809 | "an array of two strings": [ |
| 4810 | "string1", "string2" |
| 4811 | ], |
| 4812 | "map in a map": { |
| 4813 | "bytes 1": h'78787878', |
| 4814 | "bytes 2": h'79797979', |
| 4815 | "another int": 98, |
| 4816 | "text 2": "lies, damn lies and statistics" |
| 4817 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4818 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4819 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4820 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4821 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4822 | { |
| 4823 | QCBORItem Item1, Item2, Item3; |
| 4824 | int64_t nDecodedInt1, nDecodedInt2; |
| 4825 | UsefulBufC B1, B2, S1, S2, S3; |
| 4826 | |
| 4827 | QCBORDecodeContext DCtx; |
| 4828 | QCBORError nCBORError; |
| 4829 | |
| 4830 | QCBORDecode_Init(&DCtx, input, 0); |
| 4831 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4832 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4833 | |
| 4834 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 4835 | |
| 4836 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 4837 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4838 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 4839 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 4840 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4841 | QCBORDecode_ExitMap(&DCtx); |
| 4842 | |
| 4843 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 4844 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 4845 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 4846 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4847 | return -400; |
| 4848 | } |
| 4849 | QCBORDecode_ExitArray(&DCtx); |
| 4850 | |
| 4851 | // Parse the same array again using GetText() instead of GetItem() |
| 4852 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4853 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 4854 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4855 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 4856 | return 5000; |
| 4857 | } |
| 4858 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 4859 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4860 | return 5001; |
| 4861 | } */ |
| 4862 | |
| 4863 | QCBORDecode_ExitArray(&DCtx); |
| 4864 | |
| 4865 | QCBORDecode_ExitMap(&DCtx); |
| 4866 | |
| 4867 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 4868 | |
| 4869 | if(nCBORError) { |
| 4870 | return (int32_t)nCBORError; |
| 4871 | } |
| 4872 | |
| 4873 | if(nDecodedInt1 != 42) { |
| 4874 | return 1001; |
| 4875 | } |
| 4876 | |
| 4877 | if(nDecodedInt2 != 98) { |
| 4878 | return 1002; |
| 4879 | } |
| 4880 | |
| 4881 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4882 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4883 | return 1003; |
| 4884 | } |
| 4885 | |
| 4886 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4887 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4888 | return 1004; |
| 4889 | } |
| 4890 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4891 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4892 | return 1005; |
| 4893 | } |
| 4894 | |
| 4895 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 4896 | return 1006; |
| 4897 | } |
| 4898 | |
| 4899 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 4900 | return 1007; |
| 4901 | } |
| 4902 | |
| 4903 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 4904 | return 1008; |
| 4905 | } |
| 4906 | |
| 4907 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 4908 | return 1009; |
| 4909 | } |
| 4910 | |
| 4911 | return 0; |
| 4912 | } |
| 4913 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4914 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4915 | { |
| 4916 | -75008: h'05083399', |
| 4917 | 88: [], |
| 4918 | 100100: { |
| 4919 | "sub1": { |
| 4920 | 10: [ |
| 4921 | 0 |
| 4922 | ], |
| 4923 | -75009: h'A46823990001', |
| 4924 | 100100: { |
| 4925 | "json": "{ \"ueid\", \"xyz\"}", |
| 4926 | "subsub": { |
| 4927 | 100002: h'141813191001' |
| 4928 | } |
| 4929 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4930 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4931 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4932 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4933 | */ |
| 4934 | |
| 4935 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4936 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 4937 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 4938 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 4939 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 4940 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 4941 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 4942 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 4943 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 4944 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 4945 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 4946 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 4947 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4948 | }; |
| 4949 | |
| 4950 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 4951 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 4952 | { |
| 4953 | UsefulBufC String; |
| 4954 | |
| 4955 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 4956 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 4957 | |
| 4958 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4959 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 4960 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4961 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 4962 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 4963 | if(QCBORDecode_GetError(pDCtx)) { |
| 4964 | return 4001; |
| 4965 | } |
| 4966 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 4967 | return 4002; |
| 4968 | } |
| 4969 | QCBORDecode_ExitMap(pDCtx); |
| 4970 | QCBORDecode_ExitMap(pDCtx); |
| 4971 | QCBORDecode_ExitMap(pDCtx); |
| 4972 | QCBORDecode_ExitMap(pDCtx); |
| 4973 | |
| 4974 | return 0; |
| 4975 | } |
| 4976 | |
| 4977 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 4978 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 4979 | { |
| 4980 | QCBORItem Item; |
| 4981 | QCBORError uErr; |
| 4982 | |
| 4983 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 4984 | for(int x = 0; x < 20; x++) { |
| 4985 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 4986 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4987 | return 4100; |
| 4988 | |
| 4989 | } |
| 4990 | } |
| 4991 | QCBORDecode_ExitArray(pDCtx); |
| 4992 | if(QCBORDecode_GetError(pDCtx)) { |
| 4993 | return 4101; |
| 4994 | } |
| 4995 | |
| 4996 | return 0; |
| 4997 | } |
| 4998 | |
| 4999 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 5000 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 5001 | { |
| 5002 | QCBORError uErr; |
| 5003 | |
| 5004 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 5005 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 5006 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5007 | int64_t nInt = 99; |
| 5008 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 5009 | if(nInt != 0) { |
| 5010 | return 4200; |
| 5011 | } |
| 5012 | for(int x = 0; x < 20; x++) { |
| 5013 | QCBORItem Item; |
| 5014 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 5015 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5016 | return 4201; |
| 5017 | |
| 5018 | } |
| 5019 | } |
| 5020 | QCBORDecode_ExitArray(pDCtx); |
| 5021 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 5022 | return 4202; |
| 5023 | } |
| 5024 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 5025 | UsefulBufC dD; |
| 5026 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5027 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5028 | return 4203; |
| 5029 | } |
| 5030 | for(int x = 0; x < 20; x++) { |
| 5031 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 5032 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 5033 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5034 | return 4204; |
| 5035 | } |
| 5036 | } |
| 5037 | QCBORDecode_ExitArray(pDCtx); |
| 5038 | QCBORDecode_ExitMap(pDCtx); |
| 5039 | QCBORDecode_ExitMap(pDCtx); |
| 5040 | |
| 5041 | return 0; |
| 5042 | } |
| 5043 | |
| 5044 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 5045 | and arrays and such. */ |
| 5046 | static int32_t DecodeNestedIterate() |
| 5047 | { |
| 5048 | QCBORDecodeContext DCtx; |
| 5049 | int32_t nReturn; |
| 5050 | QCBORError uErr; |
| 5051 | |
| 5052 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 5053 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5054 | |
| 5055 | for(int j = 0; j < 5; j++) { |
| 5056 | for(int i = 0; i < 20; i++) { |
| 5057 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 5058 | if(nReturn) { |
| 5059 | return nReturn; |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | for(int i = 0; i < 20; i++) { |
| 5064 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 5065 | if(nReturn ) { |
| 5066 | return nReturn; |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | for(int i = 0; i < 20; i++) { |
| 5071 | nReturn = DecodeNestedGetZero(&DCtx); |
| 5072 | if(nReturn ) { |
| 5073 | return nReturn; |
| 5074 | } |
| 5075 | } |
| 5076 | } |
| 5077 | |
| 5078 | QCBORDecode_ExitMap(&DCtx); |
| 5079 | uErr = QCBORDecode_Finish(&DCtx); |
| 5080 | if(uErr) { |
| 5081 | return (int32_t)uErr + 4100; |
| 5082 | } |
| 5083 | |
| 5084 | return 0; |
| 5085 | } |
| 5086 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5087 | |
| 5088 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5089 | [ |
| 5090 | 23, |
| 5091 | 6000, |
| 5092 | h'67616C6163746963', |
| 5093 | h'686176656E20746F6B656E' |
| 5094 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5095 | */ |
| 5096 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5097 | 0x84, |
| 5098 | 0x17, |
| 5099 | 0x19, 0x17, 0x70, |
| 5100 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 5101 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5102 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5103 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5104 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 5105 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5106 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5107 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 5108 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5109 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5110 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5111 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5112 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5113 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5114 | /* |
| 5115 | { |
| 5116 | 0: [], |
| 5117 | 9: [ |
| 5118 | [], |
| 5119 | [] |
| 5120 | ], |
| 5121 | 8: { |
| 5122 | 1: [], |
| 5123 | 2: {}, |
| 5124 | 3: [] |
| 5125 | }, |
| 5126 | 4: {}, |
| 5127 | 5: [], |
| 5128 | 6: [ |
| 5129 | [], |
| 5130 | [] |
| 5131 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5132 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5133 | */ |
| 5134 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5135 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 5136 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 5137 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 5138 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5139 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5140 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5141 | |
| 5142 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5143 | /* |
| 5144 | Too many tags |
| 5145 | Invalid tag content |
| 5146 | Duplicate label |
| 5147 | Integer overflow |
| 5148 | Date overflow |
| 5149 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5150 | { |
| 5151 | 1: 224(225(226(227(4(0))))), |
| 5152 | 2: 1(h''), |
| 5153 | 3: -18446744073709551616, |
| 5154 | 4: 1(1.0e+300), |
| 5155 | 5: 0, 8: 8 |
| 5156 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5157 | */ |
| 5158 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5159 | 0xa7, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5160 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 5161 | 0x02, 0xc1, 0x40, |
| 5162 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5163 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
| 5164 | 0x05, 0x00, |
| 5165 | 0x05, 0x00, |
| 5166 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5167 | }; |
| 5168 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5169 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5170 | static const uint8_t spUnRecoverableMapError1[] = { |
| 5171 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5172 | }; |
| 5173 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5174 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5175 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5176 | static const uint8_t spUnRecoverableMapError2[] = { |
| 5177 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5178 | }; |
| 5179 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5180 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5181 | static const uint8_t spUnRecoverableMapError3[] = { |
| 5182 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 5183 | }; |
| 5184 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5185 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5186 | static const uint8_t spUnRecoverableMapError4[] = { |
| 5187 | 0xbf, |
| 5188 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5189 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5190 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5191 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5192 | 0xff |
| 5193 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5194 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5195 | |
| 5196 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5197 | int32_t EnterMapTest() |
| 5198 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5199 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5200 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5201 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5202 | int32_t nReturn; |
| 5203 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5204 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5205 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5206 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5207 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5208 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5209 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5210 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5211 | QCBORDecode_ExitArray(&DCtx); |
| 5212 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5213 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 5214 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5215 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5216 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5217 | QCBORDecode_ExitArray(&DCtx); |
| 5218 | QCBORDecode_ExitArray(&DCtx); |
| 5219 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5220 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 5221 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5222 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5223 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5224 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5225 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5226 | QCBORDecode_ExitArray(&DCtx); |
| 5227 | QCBORDecode_ExitMap(&DCtx); |
| 5228 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5229 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5230 | QCBORDecode_ExitMap(&DCtx); |
| 5231 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5232 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5233 | QCBORDecode_ExitArray(&DCtx); |
| 5234 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5235 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 5236 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5237 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5238 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5239 | QCBORDecode_ExitArray(&DCtx); |
| 5240 | QCBORDecode_ExitArray(&DCtx); |
| 5241 | |
| 5242 | QCBORDecode_ExitMap(&DCtx); |
| 5243 | |
| 5244 | uErr = QCBORDecode_Finish(&DCtx); |
| 5245 | if(uErr != QCBOR_SUCCESS){ |
| 5246 | return 3011; |
| 5247 | } |
| 5248 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5249 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5250 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5251 | if(nReturn) { |
| 5252 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5253 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5254 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5255 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5256 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5257 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5258 | if(nReturn) { |
| 5259 | return nReturn; |
| 5260 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5261 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5262 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5263 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5264 | // These tests confirm the cursor is at the right place after entering |
| 5265 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5266 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5267 | |
| 5268 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5269 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5270 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5271 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5272 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 5273 | return 2001; |
| 5274 | } |
| 5275 | |
| 5276 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5277 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5278 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5279 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5280 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 5281 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5282 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5283 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 5284 | return 2051; |
| 5285 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5286 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5287 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 5288 | return 2002; |
| 5289 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5290 | QCBORDecode_ExitArray(&DCtx); |
| 5291 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 5292 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5293 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5294 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 5295 | return 2052; |
| 5296 | } |
| 5297 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5298 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5299 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5300 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5301 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5302 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5303 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5304 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5305 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5306 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 5307 | return 2003; |
| 5308 | } |
| 5309 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5310 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5311 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5312 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5313 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5314 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5315 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5316 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5317 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5318 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5319 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5320 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5321 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5322 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5323 | } |
| 5324 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5325 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5326 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 5327 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5328 | QCBORDecode_ExitArray(&DCtx); |
| 5329 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5330 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 5331 | return 2006; |
| 5332 | } |
| 5333 | QCBORDecode_ExitMap(&DCtx); |
| 5334 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5335 | return 2007; |
| 5336 | } |
| 5337 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5338 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5339 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5340 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5341 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5342 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5343 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5344 | return 2008; |
| 5345 | } |
| 5346 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5347 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5348 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5349 | return 2009; |
| 5350 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5351 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5352 | |
| 5353 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5354 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5355 | // This will fail because the map is empty. |
| 5356 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5357 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5358 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5359 | return 2010; |
| 5360 | } |
| 5361 | QCBORDecode_ExitMap(&DCtx); |
| 5362 | uErr = QCBORDecode_Finish(&DCtx); |
| 5363 | if(uErr != QCBOR_SUCCESS){ |
| 5364 | return 2011; |
| 5365 | } |
| 5366 | |
| 5367 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5368 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5369 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5370 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5371 | // This will fail because the map is empty. |
| 5372 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5373 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5374 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5375 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5376 | } |
| 5377 | QCBORDecode_ExitMap(&DCtx); |
| 5378 | uErr = QCBORDecode_Finish(&DCtx); |
| 5379 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5380 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5381 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5382 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5383 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5384 | |
| 5385 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5386 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5387 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5388 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5389 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5390 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5391 | QCBORDecode_ExitArray(&DCtx); |
| 5392 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 5393 | QCBORDecode_ExitArray(&DCtx); |
| 5394 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5395 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5396 | return 2014; |
| 5397 | } |
| 5398 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5399 | int64_t nInt; |
| 5400 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5401 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5402 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5403 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5404 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 5405 | return 2021; |
| 5406 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5407 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 5408 | return 2121; |
| 5409 | } |
| 5410 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 5411 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5412 | |
| 5413 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 5414 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5415 | if(uErr != QCBOR_ERR_BAD_OPT_TAG) { |
| 5416 | return 2022; |
| 5417 | } |
| 5418 | |
| 5419 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 5420 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5421 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 5422 | return 2023; |
| 5423 | } |
| 5424 | |
| 5425 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 5426 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5427 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5428 | if(uErr != QCBOR_ERR_DATE_OVERFLOW) { |
| 5429 | return 2024; |
| 5430 | } |
| 5431 | #else |
| 5432 | if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 5433 | return 2027; |
| 5434 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5435 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5436 | |
| 5437 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 5438 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5439 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 5440 | return 2025; |
| 5441 | } |
| 5442 | |
| 5443 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 5444 | |
| 5445 | QCBORDecode_ExitMap(&DCtx); |
| 5446 | uErr = QCBORDecode_Finish(&DCtx); |
| 5447 | if(uErr != QCBOR_SUCCESS) { |
| 5448 | return 2026; |
| 5449 | } |
| 5450 | |
| 5451 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5452 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5453 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5454 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5455 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 5456 | return 2030; |
| 5457 | } |
| 5458 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5459 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5460 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5461 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5462 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5463 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5464 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5465 | return 2031; |
| 5466 | } |
| 5467 | |
| 5468 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5469 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5470 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5471 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5472 | if(uErr != QCBOR_ERR_HIT_END) { |
| 5473 | return 2032; |
| 5474 | } |
| 5475 | |
| 5476 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5477 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5478 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5479 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5480 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 5481 | return 2033; |
| 5482 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5483 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5484 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 5485 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 5486 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5487 | if(Item1.uDataType != QCBOR_TYPE_MAP) { |
| 5488 | return 2401; |
| 5489 | } |
| 5490 | if(QCBORDecode_GetError(&DCtx)) { |
| 5491 | return 2402; |
| 5492 | } |
| 5493 | |
| 5494 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 5495 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5496 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5497 | Item1.val.uCount != 3 || |
| 5498 | Item1.uNextNestLevel != 1) { |
| 5499 | return 2403; |
| 5500 | } |
| 5501 | if(QCBORDecode_GetError(&DCtx)) { |
| 5502 | return 2404; |
| 5503 | } |
| 5504 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5505 | if(Item1.uDataType != QCBOR_TYPE_INT64 || |
| 5506 | Item1.uNextNestLevel != 1 || |
| 5507 | Item1.val.int64 != 42) { |
| 5508 | return 2405; |
| 5509 | } |
| 5510 | if(QCBORDecode_GetError(&DCtx)) { |
| 5511 | return 2406; |
| 5512 | } |
| 5513 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5514 | if(Item1.uDataType != QCBOR_TYPE_ARRAY || |
| 5515 | Item1.uNestingLevel != 1 || |
| 5516 | Item1.uNextNestLevel != 1 || |
| 5517 | Item1.val.uCount != 2) { |
| 5518 | return 2407; |
| 5519 | } |
| 5520 | if(QCBORDecode_GetError(&DCtx)) { |
| 5521 | return 2408; |
| 5522 | } |
| 5523 | QCBORDecode_VGetNextConsume(&DCtx, &Item1); |
| 5524 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 5525 | Item1.uNestingLevel != 1 || |
| 5526 | Item1.uNextNestLevel != 0 || |
| 5527 | Item1.val.uCount != 4) { |
| 5528 | return 2409; |
| 5529 | } |
| 5530 | if(QCBORDecode_GetError(&DCtx)) { |
| 5531 | return 2410; |
| 5532 | } |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 5533 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5534 | nReturn = DecodeNestedIterate(); |
| 5535 | |
| 5536 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5540 | struct NumberConversion { |
| 5541 | char *szDescription; |
| 5542 | UsefulBufC CBOR; |
| 5543 | int64_t nConvertedToInt64; |
| 5544 | QCBORError uErrorInt64; |
| 5545 | uint64_t uConvertToUInt64; |
| 5546 | QCBORError uErrorUint64; |
| 5547 | double dConvertToDouble; |
| 5548 | QCBORError uErrorDouble; |
| 5549 | }; |
| 5550 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5551 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5552 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5553 | "too large to fit into int64_t", |
| 5554 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 5555 | 0, |
| 5556 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5557 | 0, |
| 5558 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5559 | ((double)INT64_MIN) + 1 , |
| 5560 | QCBOR_SUCCESS |
| 5561 | }, |
| 5562 | { |
| 5563 | "largest negative int that fits in int64_t", |
| 5564 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 5565 | INT64_MIN, |
| 5566 | QCBOR_SUCCESS, |
| 5567 | 0, |
| 5568 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5569 | (double)INT64_MIN, |
| 5570 | QCBOR_SUCCESS |
| 5571 | }, |
| 5572 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5573 | "negative bignum -1", |
| 5574 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 5575 | -1, |
| 5576 | QCBOR_SUCCESS, |
| 5577 | 0, |
| 5578 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5579 | -1.0, |
| 5580 | QCBOR_SUCCESS |
| 5581 | }, |
| 5582 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5583 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5584 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5585 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5586 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5587 | 257000, |
| 5588 | QCBOR_SUCCESS, |
| 5589 | 257000, |
| 5590 | QCBOR_SUCCESS, |
| 5591 | 257000.0, |
| 5592 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5593 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5594 | 0, |
| 5595 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5596 | 0, |
| 5597 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5598 | 0.0, |
| 5599 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5600 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5601 | }, |
| 5602 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5603 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5604 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5605 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5606 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5607 | -2064, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5608 | QCBOR_SUCCESS, |
| 5609 | 0, |
| 5610 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5611 | -2064.0, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5612 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5613 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5614 | 0, |
| 5615 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5616 | 0, |
| 5617 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5618 | 0.0, |
| 5619 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5620 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5621 | }, |
| 5622 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5623 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5624 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5625 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5626 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5627 | 2056, |
| 5628 | QCBOR_SUCCESS, |
| 5629 | 2056, |
| 5630 | QCBOR_SUCCESS, |
| 5631 | 2056.0, |
| 5632 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5633 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5634 | 0, |
| 5635 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5636 | 0, |
| 5637 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5638 | 0.0, |
| 5639 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5640 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5641 | }, |
| 5642 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5643 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5644 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 5645 | 0, |
| 5646 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5647 | 0, |
| 5648 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5649 | -18446744073709551617.0, |
| 5650 | QCBOR_SUCCESS |
| 5651 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5652 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5653 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5654 | "Positive bignum 0x01020304 indefinite length string", |
| 5655 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 5656 | 0x01020304, |
| 5657 | QCBOR_SUCCESS, |
| 5658 | 0x01020304, |
| 5659 | QCBOR_SUCCESS, |
| 5660 | 16909060.0, |
| 5661 | QCBOR_SUCCESS |
| 5662 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5663 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5664 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5665 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5666 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5667 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5668 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5669 | 0, |
| 5670 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5671 | 0, |
| 5672 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5673 | -INFINITY, |
| 5674 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5675 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5676 | 0, |
| 5677 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5678 | 0, |
| 5679 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5680 | 0.0, |
| 5681 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5682 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5683 | }, |
| 5684 | { |
| 5685 | "big float [9223372036854775806, 9223372036854775806]", |
| 5686 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 5687 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5688 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5689 | 0, |
| 5690 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5691 | 0, |
| 5692 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5693 | INFINITY, |
| 5694 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5695 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5696 | 0, |
| 5697 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5698 | 0, |
| 5699 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5700 | 0.0, |
| 5701 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5702 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5703 | }, |
| 5704 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5705 | "Big float 3 * 2^^2", |
| 5706 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5707 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5708 | 12, |
| 5709 | QCBOR_SUCCESS, |
| 5710 | 12, |
| 5711 | QCBOR_SUCCESS, |
| 5712 | 12.0, |
| 5713 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5714 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5715 | 0, |
| 5716 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5717 | 0, |
| 5718 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5719 | 0.0, |
| 5720 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5721 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5722 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5723 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5724 | "Positive integer 18446744073709551615", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5725 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5726 | 0, |
| 5727 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5728 | 18446744073709551615ULL, |
| 5729 | QCBOR_SUCCESS, |
| 5730 | 18446744073709551615.0, |
| 5731 | QCBOR_SUCCESS |
| 5732 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5733 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5734 | "Positive bignum 0xffff", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5735 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 5736 | 65536-1, |
| 5737 | QCBOR_SUCCESS, |
| 5738 | 0xffff, |
| 5739 | QCBOR_SUCCESS, |
| 5740 | 65535.0, |
| 5741 | QCBOR_SUCCESS |
| 5742 | }, |
| 5743 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5744 | "Postive integer 0", |
| 5745 | {(uint8_t[]){0x0}, 1}, |
| 5746 | 0LL, |
| 5747 | QCBOR_SUCCESS, |
| 5748 | 0ULL, |
| 5749 | QCBOR_SUCCESS, |
| 5750 | 0.0, |
| 5751 | QCBOR_SUCCESS |
| 5752 | }, |
| 5753 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5754 | "Negative integer -18446744073709551616", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5755 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 5756 | -9223372036854775807-1, // INT64_MIN |
| 5757 | QCBOR_SUCCESS, |
| 5758 | 0ULL, |
| 5759 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5760 | -9223372036854775808.0, |
| 5761 | QCBOR_SUCCESS |
| 5762 | }, |
| 5763 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5764 | "Double Floating point value 100.3", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5765 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5766 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5767 | 100L, |
| 5768 | QCBOR_SUCCESS, |
| 5769 | 100ULL, |
| 5770 | QCBOR_SUCCESS, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5771 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5772 | 0, |
| 5773 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5774 | 0, |
| 5775 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5776 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5777 | 100.3, |
| 5778 | QCBOR_SUCCESS |
| 5779 | }, |
| 5780 | { |
| 5781 | "Floating point value NaN 0xfa7fc00000", |
| 5782 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5783 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5784 | 0, |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5785 | QCBOR_ERR_FLOAT_EXCEPTION, |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5786 | 0, |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5787 | QCBOR_ERR_FLOAT_EXCEPTION, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5788 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5789 | 0, |
| 5790 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5791 | 0, |
| 5792 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5793 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5794 | NAN, |
| 5795 | QCBOR_SUCCESS |
| 5796 | }, |
| 5797 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5798 | "half-precision Floating point value -4", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5799 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5800 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 5801 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5802 | // Normal case with all enabled. |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5803 | -4, |
| 5804 | QCBOR_SUCCESS, |
| 5805 | 0, |
| 5806 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5807 | -4.0, |
| 5808 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5809 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5810 | // Float HW disabled |
| 5811 | -4, |
| 5812 | QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer |
| 5813 | 0, |
| 5814 | QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer |
| 5815 | -4.0, |
| 5816 | QCBOR_SUCCESS // Uses ieee754.h to conver, not HW |
| 5817 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5818 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5819 | // Half-precision disabled |
| 5820 | -4, |
| 5821 | QCBOR_ERR_HALF_PRECISION_DISABLED, |
| 5822 | 0, |
| 5823 | QCBOR_ERR_HALF_PRECISION_DISABLED, |
| 5824 | -4.0, |
| 5825 | QCBOR_ERR_HALF_PRECISION_DISABLED |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5826 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5827 | }, |
| 5828 | { |
| 5829 | "Decimal fraction 3/10", |
| 5830 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5831 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5832 | 0, |
| 5833 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5834 | 0, |
| 5835 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5836 | 0.30000000000000004, |
| 5837 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5838 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5839 | 0, |
| 5840 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5841 | 0, |
| 5842 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5843 | 0.0, |
| 5844 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5845 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5846 | }, |
| 5847 | { |
| 5848 | "+inifinity", |
| 5849 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 5850 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5851 | 0, |
| 5852 | QCBOR_ERR_FLOAT_EXCEPTION, |
| 5853 | 0, |
| 5854 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5855 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5856 | 0, |
| 5857 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5858 | 0, |
| 5859 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5860 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5861 | INFINITY, |
| 5862 | QCBOR_SUCCESS |
| 5863 | }, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5864 | |
| 5865 | { |
| 5866 | "extreme pos bignum", |
| 5867 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 5868 | // 50 rows of 8 is 400 digits. |
| 5869 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5870 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5871 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5872 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5873 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5874 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5875 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5876 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5877 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5878 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5879 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5880 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5881 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5882 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5883 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5884 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5885 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5886 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5887 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5888 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5889 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5890 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5891 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 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}, |
| 5919 | 404}, |
| 5920 | 0, |
| 5921 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5922 | 0, |
| 5923 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5924 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5925 | INFINITY, |
| 5926 | QCBOR_SUCCESS |
| 5927 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5928 | 0, |
| 5929 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5930 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5931 | }, |
| 5932 | |
| 5933 | { |
| 5934 | "extreme neg bignum", |
| 5935 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 5936 | // 50 rows of 8 is 400 digits. |
| 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, 0xf0, |
| 5942 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5943 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5944 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5945 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5946 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5947 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5948 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5949 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5950 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5951 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5952 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5953 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5954 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 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}, |
| 5987 | 404}, |
| 5988 | 0, |
| 5989 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5990 | 0, |
| 5991 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5992 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5993 | -INFINITY, |
| 5994 | QCBOR_SUCCESS |
| 5995 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5996 | 0, |
| 5997 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5998 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5999 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6000 | |
| 6001 | { |
| 6002 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 6003 | {(uint8_t[]){ |
| 6004 | 0xC5, 0x82, |
| 6005 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 6006 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 6007 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 6008 | 0, |
| 6009 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6010 | 0, |
| 6011 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6012 | 0, |
| 6013 | QCBOR_SUCCESS |
| 6014 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 6015 | 0, |
| 6016 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 6017 | 0, |
| 6018 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 6019 | 0.0, |
| 6020 | QCBOR_ERR_UNEXPECTED_TYPE |
| 6021 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 6022 | }, |
| 6023 | |
| 6024 | { |
| 6025 | "bigfloat that evaluates to -INFINITY", |
| 6026 | {(uint8_t[]){ |
| 6027 | 0xC5, 0x82, |
| 6028 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 6029 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
| 6030 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 6031 | 0, |
| 6032 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 6033 | 0, |
| 6034 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 6035 | -INFINITY, |
| 6036 | QCBOR_SUCCESS |
| 6037 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 6038 | 0, |
| 6039 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 6040 | 0, |
| 6041 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 6042 | 0.0, |
| 6043 | QCBOR_ERR_UNEXPECTED_TYPE |
| 6044 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
| 6045 | }, |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6046 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6047 | |
| 6048 | |
| 6049 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6050 | |
| 6051 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 6052 | { |
| 6053 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 6054 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6055 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 6056 | return 1; |
| 6057 | } |
| 6058 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6059 | (void)Pool; |
| 6060 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6061 | return 0; |
| 6062 | } |
| 6063 | |
| 6064 | |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 6065 | int32_t IntegerConvertTest() |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6066 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6067 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 6068 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6069 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6070 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 6071 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6072 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6073 | // Set up the decoding context including a memory pool so that |
| 6074 | // indefinite length items can be checked |
| 6075 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6076 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6077 | |
| 6078 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6079 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6080 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6081 | } |
| 6082 | |
| 6083 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6084 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6085 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6086 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6087 | } |
| 6088 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6089 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6090 | } |
| 6091 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6092 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6093 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6094 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6095 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6096 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6097 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6098 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6099 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6100 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6101 | } |
| 6102 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6103 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6104 | } |
| 6105 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6106 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6107 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6108 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6109 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6110 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6111 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6112 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6113 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6114 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6115 | } |
| 6116 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 6117 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6118 | // NaN's can't be compared for equality. A NaN is |
| 6119 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6120 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6121 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6122 | } |
| 6123 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6124 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6125 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6126 | } |
| 6127 | } |
| 6128 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6129 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | return 0; |
| 6133 | } |
| 6134 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6135 | |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6136 | |
| 6137 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6138 | int32_t CBORSequenceDecodeTests(void) |
| 6139 | { |
| 6140 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6141 | QCBORItem Item; |
| 6142 | QCBORError uCBORError; |
| 6143 | size_t uConsumed; |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6144 | |
| 6145 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6146 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6147 | // The input for the date test happens to be a sequence so it |
| 6148 | // is reused. It is a sequence because it doesn't start as |
| 6149 | // an array or map. |
| 6150 | QCBORDecode_Init(&DCtx, |
| 6151 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 6152 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6153 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6154 | // Get the first item |
| 6155 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6156 | if(uCBORError != QCBOR_SUCCESS) { |
| 6157 | return 1; |
| 6158 | } |
| 6159 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING) { |
| 6160 | return 2; |
| 6161 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6162 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6163 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6164 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
| 6165 | uConsumed != 12) { |
| 6166 | return 102; |
| 6167 | } |
| 6168 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6169 | // Get a second item |
| 6170 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6171 | if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) { |
| 6172 | return 66; |
| 6173 | } |
| 6174 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 6175 | uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed); |
| 6176 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES || |
| 6177 | uConsumed != 14) { |
| 6178 | return 102; |
| 6179 | } |
| 6180 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6181 | // Get a third item |
| 6182 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6183 | if(uCBORError != QCBOR_SUCCESS) { |
| 6184 | return 2; |
| 6185 | } |
| 6186 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 6187 | return 3; |
| 6188 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6189 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6190 | // A sequence can have stuff at the end that may |
| 6191 | // or may not be valid CBOR. The protocol decoder knows |
| 6192 | // when to stop by definition of the protocol, not |
| 6193 | // when the top-level map or array is ended. |
| 6194 | // Finish still has to be called to know that |
| 6195 | // maps and arrays (if there were any) were closed |
| 6196 | // off correctly. When called like this it |
| 6197 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 6198 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6199 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 6200 | return 4; |
| 6201 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6202 | |
| 6203 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6204 | // --- Test an empty input ---- |
| 6205 | uint8_t empty[1]; |
| 6206 | UsefulBufC Empty = {empty, 0}; |
| 6207 | QCBORDecode_Init(&DCtx, |
| 6208 | Empty, |
| 6209 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6210 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6211 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6212 | if(uCBORError != QCBOR_SUCCESS) { |
| 6213 | return 5; |
| 6214 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6215 | |
| 6216 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6217 | // --- Sequence with unclosed indefinite length array --- |
| 6218 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6219 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6220 | QCBORDecode_Init(&DCtx, |
| 6221 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 6222 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6223 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6224 | // Get the first item |
| 6225 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6226 | if(uCBORError != QCBOR_SUCCESS) { |
| 6227 | return 7; |
| 6228 | } |
| 6229 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6230 | return 8; |
| 6231 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6232 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6233 | // Get a second item |
| 6234 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6235 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6236 | if(uCBORError != QCBOR_SUCCESS) { |
| 6237 | return 9; |
| 6238 | } |
| 6239 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6240 | return 10; |
| 6241 | } |
| 6242 | |
| 6243 | // Try to finish before consuming all bytes to confirm |
| 6244 | // that the still-open error is returned. |
| 6245 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6246 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6247 | return 11; |
| 6248 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6249 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6250 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6251 | return 20; |
| 6252 | } |
| 6253 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6254 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6255 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6256 | // --- Sequence with a closed indefinite length array --- |
| 6257 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6258 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6259 | QCBORDecode_Init(&DCtx, |
| 6260 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 6261 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6262 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6263 | // Get the first item |
| 6264 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6265 | if(uCBORError != QCBOR_SUCCESS) { |
| 6266 | return 12; |
| 6267 | } |
| 6268 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6269 | return 13; |
| 6270 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6271 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6272 | // Get a second item |
| 6273 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6274 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6275 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6276 | if(uCBORError != QCBOR_SUCCESS) { |
| 6277 | return 14; |
| 6278 | } |
| 6279 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6280 | return 15; |
| 6281 | } |
| 6282 | |
| 6283 | // Try to finish before consuming all bytes to confirm |
| 6284 | // that the still-open error is returned. |
| 6285 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6286 | if(uCBORError != QCBOR_SUCCESS) { |
| 6287 | return 16; |
| 6288 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6289 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6290 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6291 | return 20; |
| 6292 | } |
| 6293 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6294 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6295 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6296 | return 0; |
| 6297 | } |
| 6298 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6299 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 6300 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6301 | int32_t IntToTests() |
| 6302 | { |
| 6303 | int nErrCode; |
| 6304 | int32_t n32; |
| 6305 | int16_t n16; |
| 6306 | int8_t n8; |
| 6307 | uint32_t u32; |
| 6308 | uint16_t u16; |
| 6309 | uint8_t u8; |
| 6310 | uint64_t u64; |
| 6311 | |
| 6312 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 6313 | if(nErrCode == -1 || n32 != 1) { |
| 6314 | return 1; |
| 6315 | } |
| 6316 | |
| 6317 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 6318 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 6319 | return 2; |
| 6320 | } |
| 6321 | |
| 6322 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 6323 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 6324 | return 3; |
| 6325 | } |
| 6326 | |
| 6327 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 6328 | if(nErrCode != -1) { |
| 6329 | return 4; |
| 6330 | } |
| 6331 | |
| 6332 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 6333 | if(nErrCode != -1) { |
| 6334 | return 5; |
| 6335 | } |
| 6336 | |
| 6337 | |
| 6338 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 6339 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 6340 | return 6; |
| 6341 | } |
| 6342 | |
| 6343 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 6344 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 6345 | return 7; |
| 6346 | } |
| 6347 | |
| 6348 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 6349 | if(nErrCode == -1 || n16 != 1) { |
| 6350 | return 8; |
| 6351 | } |
| 6352 | |
| 6353 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 6354 | if(nErrCode != -1) { |
| 6355 | return 9; |
| 6356 | } |
| 6357 | |
| 6358 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 6359 | if(nErrCode != -1) { |
| 6360 | return 10; |
| 6361 | } |
| 6362 | |
| 6363 | |
| 6364 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 6365 | if(nErrCode == -1 || n8 != 1) { |
| 6366 | return 11; |
| 6367 | } |
| 6368 | |
| 6369 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 6370 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 6371 | return 12; |
| 6372 | } |
| 6373 | |
| 6374 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 6375 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 6376 | return 13; |
| 6377 | } |
| 6378 | |
| 6379 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 6380 | if(nErrCode != -1) { |
| 6381 | return 14; |
| 6382 | } |
| 6383 | |
| 6384 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 6385 | if(nErrCode != -1) { |
| 6386 | return 15; |
| 6387 | } |
| 6388 | |
| 6389 | |
| 6390 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 6391 | if(nErrCode == -1 || u32 != 1) { |
| 6392 | return 16; |
| 6393 | } |
| 6394 | |
| 6395 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 6396 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 6397 | return 17; |
| 6398 | } |
| 6399 | |
| 6400 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 6401 | if(nErrCode == -1 || u32 != 0) { |
| 6402 | return 18; |
| 6403 | } |
| 6404 | |
| 6405 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 6406 | if(nErrCode != -1) { |
| 6407 | return 19; |
| 6408 | } |
| 6409 | |
| 6410 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 6411 | if(nErrCode != -1) { |
| 6412 | return 20; |
| 6413 | } |
| 6414 | |
| 6415 | |
| 6416 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 6417 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 6418 | return 21; |
| 6419 | } |
| 6420 | |
| 6421 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 6422 | if(nErrCode == -1 || u16 != 0) { |
| 6423 | return 22; |
| 6424 | } |
| 6425 | |
| 6426 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 6427 | if(nErrCode == -1 || u16 != 1) { |
| 6428 | return 23; |
| 6429 | } |
| 6430 | |
| 6431 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 6432 | if(nErrCode != -1) { |
| 6433 | return 24; |
| 6434 | } |
| 6435 | |
| 6436 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 6437 | if(nErrCode != -1) { |
| 6438 | return 25; |
| 6439 | } |
| 6440 | |
| 6441 | |
| 6442 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 6443 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 6444 | return 26; |
| 6445 | } |
| 6446 | |
| 6447 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 6448 | if(nErrCode == -1 || u8 != 0) { |
| 6449 | return 27; |
| 6450 | } |
| 6451 | |
| 6452 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 6453 | if(nErrCode == -1 || u8 != 1) { |
| 6454 | return 28; |
| 6455 | } |
| 6456 | |
| 6457 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 6458 | if(nErrCode != -1) { |
| 6459 | return 29; |
| 6460 | } |
| 6461 | |
| 6462 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 6463 | if(nErrCode != -1) { |
| 6464 | return 30; |
| 6465 | } |
| 6466 | |
| 6467 | |
| 6468 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 6469 | if(nErrCode == -1 || u64 != 1) { |
| 6470 | return 31; |
| 6471 | } |
| 6472 | |
| 6473 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 6474 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 6475 | return 32; |
| 6476 | } |
| 6477 | |
| 6478 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 6479 | if(nErrCode == -1 || u64 != 0) { |
| 6480 | return 33; |
| 6481 | } |
| 6482 | |
| 6483 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 6484 | if(nErrCode != -1) { |
| 6485 | return 34; |
| 6486 | } |
| 6487 | |
| 6488 | return 0; |
| 6489 | } |
| 6490 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6491 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6492 | |
| 6493 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6494 | /* |
| 6495 | A sequence with |
| 6496 | A wrapping bstr |
| 6497 | containing a map |
| 6498 | 1 |
| 6499 | 2 |
| 6500 | A wrapping bstr |
| 6501 | containing an array |
| 6502 | 3 |
| 6503 | wrapping bstr |
| 6504 | 4 |
| 6505 | 5 |
| 6506 | 6 |
| 6507 | array |
| 6508 | 7 |
| 6509 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6510 | */ |
| 6511 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6512 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6513 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6514 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6515 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6516 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6517 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6518 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6519 | |
| 6520 | QCBOREncode_BstrWrap(&EC); |
| 6521 | QCBOREncode_OpenMap(&EC); |
| 6522 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 6523 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 6524 | QCBOREncode_CloseMap(&EC); |
| 6525 | QCBOREncode_BstrWrap(&EC); |
| 6526 | QCBOREncode_OpenArray(&EC); |
| 6527 | QCBOREncode_AddInt64(&EC, 3); |
| 6528 | QCBOREncode_BstrWrap(&EC); |
| 6529 | QCBOREncode_AddInt64(&EC, 4); |
| 6530 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6531 | QCBOREncode_AddInt64(&EC, 5); |
| 6532 | QCBOREncode_CloseArray(&EC); |
| 6533 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6534 | QCBOREncode_AddInt64(&EC, 6); |
| 6535 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6536 | QCBOREncode_OpenArray(&EC); |
| 6537 | QCBOREncode_AddInt64(&EC, 7); |
| 6538 | QCBOREncode_AddInt64(&EC, 8); |
| 6539 | QCBOREncode_CloseArray(&EC); |
| 6540 | |
| 6541 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 6542 | if(uErr) { |
| 6543 | Encoded = NULLUsefulBufC; |
| 6544 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6545 | |
| 6546 | return Encoded; |
| 6547 | } |
| 6548 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6549 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6550 | static const uint8_t spBreakInByteString[] = { |
| 6551 | 0x41, 0xff |
| 6552 | }; |
| 6553 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6554 | |
| 6555 | int32_t EnterBstrTest() |
| 6556 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 6557 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6558 | |
| 6559 | QCBORDecodeContext DC; |
| 6560 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6561 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6562 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6563 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6564 | |
| 6565 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6566 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6567 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6568 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 6569 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6570 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6571 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6572 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6573 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6574 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6575 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6576 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6577 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6578 | QCBORDecode_ExitArray(&DC); |
| 6579 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6580 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6581 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6582 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6583 | QCBORDecode_GetInt64(&DC, &n7); |
| 6584 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6585 | QCBORDecode_ExitArray(&DC); |
| 6586 | |
| 6587 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6588 | if(uErr) { |
| 6589 | return (int32_t)uErr; |
| 6590 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6591 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6592 | |
| 6593 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 6594 | * Successful because no items are fetched from byte string. |
| 6595 | */ |
| 6596 | QCBORDecode_Init(&DC, |
| 6597 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 6598 | 0); |
| 6599 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6600 | uErr = QCBORDecode_GetError(&DC); |
| 6601 | if(uErr) { |
| 6602 | return 100 + (int32_t)uErr; |
| 6603 | } |
| 6604 | |
| 6605 | QCBORDecode_ExitBstrWrapped(&DC); |
| 6606 | uErr = QCBORDecode_GetError(&DC); |
| 6607 | if(uErr) { |
| 6608 | return 200 + (int32_t)uErr; |
| 6609 | } |
| 6610 | |
| 6611 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 6612 | * It fails because there should be no break. |
| 6613 | */ |
| 6614 | QCBORDecode_Init(&DC, |
| 6615 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 6616 | 0); |
| 6617 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6618 | QCBORItem Item; |
| 6619 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 6620 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 6621 | return 300 + (int32_t)uErr; |
| 6622 | } |
| 6623 | |
| 6624 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6625 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6626 | |
| 6627 | |
| 6628 | |
| 6629 | |
| 6630 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6631 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6632 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6633 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6634 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6635 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 6636 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 6637 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6638 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6639 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6640 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 6641 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 6642 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6643 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6644 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6645 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6646 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6647 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6648 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6649 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6650 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6651 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6652 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6653 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6654 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6655 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 6656 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6657 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6658 | 21, |
| 6659 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 6660 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 6661 | |
| 6662 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6663 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6664 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6665 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6666 | |
| 6667 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6668 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6669 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6670 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6671 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6672 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6673 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6674 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6675 | |
| 6676 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6677 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6678 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6679 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6680 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6681 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6682 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 6683 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6684 | |
| 6685 | 0x18, 0x33, |
| 6686 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 6687 | |
| 6688 | // MIME |
| 6689 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6690 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 6691 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 6692 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6693 | |
| 6694 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6695 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6696 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6697 | |
| 6698 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6699 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 6700 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 6701 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6702 | |
| 6703 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6704 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6705 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6706 | |
| 6707 | // UUID |
| 6708 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6709 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 6710 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6711 | |
| 6712 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6713 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 6714 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6715 | }; |
| 6716 | |
| 6717 | int32_t DecodeTaggedTypeTests() |
| 6718 | { |
| 6719 | QCBORDecodeContext DC; |
| 6720 | QCBORError uErr; |
| 6721 | |
| 6722 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 6723 | |
| 6724 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6725 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6726 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6727 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6728 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6729 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6730 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 6731 | return 1; |
| 6732 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6733 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6734 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6735 | return 2; |
| 6736 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6737 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6738 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6739 | return 3; |
| 6740 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6741 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6742 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6743 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6744 | return 4; |
| 6745 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6746 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6747 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6748 | return 5; |
| 6749 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6750 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6751 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6752 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6753 | bNeg != false) { |
| 6754 | return 10; |
| 6755 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6756 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6757 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6758 | bNeg != true) { |
| 6759 | return 11; |
| 6760 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6761 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6762 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6763 | return 12; |
| 6764 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6765 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6766 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6767 | return 13; |
| 6768 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6769 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6770 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6771 | return 14; |
| 6772 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6773 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6774 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6775 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6776 | return 20; |
| 6777 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6778 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6779 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6780 | return 21; |
| 6781 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6782 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6783 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6784 | return 22; |
| 6785 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6786 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6787 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6788 | return 23; |
| 6789 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6790 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6791 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6792 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6793 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6794 | return 30; |
| 6795 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6796 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6797 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6798 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6799 | return 31; |
| 6800 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6801 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6802 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6803 | return 32; |
| 6804 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6805 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6806 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6807 | return 33; |
| 6808 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6809 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6810 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6811 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6812 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6813 | return 40; |
| 6814 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6815 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6816 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6817 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6818 | return 41; |
| 6819 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6820 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6821 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6822 | return 42; |
| 6823 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6824 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6825 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6826 | return 43; |
| 6827 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6828 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6829 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6830 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6831 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6832 | return 50; |
| 6833 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6834 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6835 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6836 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6837 | return 51; |
| 6838 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6839 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6840 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6841 | return 52; |
| 6842 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6843 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6844 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6845 | return 53; |
| 6846 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6847 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6848 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6849 | // MIME |
| 6850 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6851 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6852 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6853 | bIsNot7Bit == true) { |
| 6854 | return 60; |
| 6855 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6856 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6857 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6858 | bIsNot7Bit == true) { |
| 6859 | return 61; |
| 6860 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6861 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6862 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6863 | bIsNot7Bit == false) { |
| 6864 | return 62; |
| 6865 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6866 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6867 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6868 | bIsNot7Bit == false) { |
| 6869 | return 63; |
| 6870 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6871 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6872 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6873 | return 64; |
| 6874 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6875 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6876 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6877 | return 65; |
| 6878 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6879 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6880 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6881 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6882 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6883 | return 70; |
| 6884 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 6885 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 6886 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6887 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6888 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6889 | return 71; |
| 6890 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6891 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
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 72; |
| 6894 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6895 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
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 73; |
| 6898 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6899 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6900 | // Improvement: add some more error test cases |
| 6901 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6902 | QCBORDecode_ExitMap(&DC); |
| 6903 | |
| 6904 | uErr = QCBORDecode_Finish(&DC); |
| 6905 | if(uErr != QCBOR_SUCCESS) { |
| 6906 | return 100; |
| 6907 | } |
| 6908 | |
| 6909 | return 0; |
| 6910 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6911 | |
| 6912 | |
| 6913 | |
| 6914 | |
| 6915 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6916 | [ |
| 6917 | "aaaaaaaaaa", |
| 6918 | {} |
| 6919 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6920 | */ |
| 6921 | static const uint8_t spTooLarge1[] = { |
| 6922 | 0x9f, |
| 6923 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6924 | 0xa0, |
| 6925 | 0xff |
| 6926 | }; |
| 6927 | |
| 6928 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6929 | [ |
| 6930 | { |
| 6931 | 0: "aaaaaaaaaa" |
| 6932 | } |
| 6933 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6934 | */ |
| 6935 | static const uint8_t spTooLarge2[] = { |
| 6936 | 0x9f, |
| 6937 | 0xa1, |
| 6938 | 0x00, |
| 6939 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6940 | 0xff |
| 6941 | }; |
| 6942 | |
| 6943 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6944 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6945 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6946 | { |
| 6947 | 0: "aaaaaaaaaa" |
| 6948 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6949 | */ |
| 6950 | static const uint8_t spTooLarge3[] = { |
| 6951 | 0x4d, |
| 6952 | 0xa1, |
| 6953 | 0x00, |
| 6954 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6955 | }; |
| 6956 | |
| 6957 | int32_t TooLargeInputTest(void) |
| 6958 | { |
| 6959 | QCBORDecodeContext DC; |
| 6960 | QCBORError uErr; |
| 6961 | UsefulBufC String; |
| 6962 | |
| 6963 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 6964 | // to 10 There's not really any way to test this error |
| 6965 | // condition. The error condition is not complex, so setting |
| 6966 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 6967 | |
| 6968 | // The input CBOR is only too large because the |
| 6969 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 6970 | // |
| 6971 | // This test is disabled for the normal test runs because of the |
| 6972 | // special build requirement. |
| 6973 | |
| 6974 | |
| 6975 | // Tests the start of a map being too large |
| 6976 | 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] | 6977 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6978 | QCBORDecode_GetTextString(&DC, &String); |
| 6979 | uErr = QCBORDecode_GetError(&DC); |
| 6980 | if(uErr != QCBOR_SUCCESS) { |
| 6981 | return 1; |
| 6982 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6983 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6984 | uErr = QCBORDecode_GetError(&DC); |
| 6985 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 6986 | return 2; |
| 6987 | } |
| 6988 | |
| 6989 | // Tests the end of a map being too large |
| 6990 | 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] | 6991 | QCBORDecode_EnterArray(&DC, NULL); |
| 6992 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6993 | uErr = QCBORDecode_GetError(&DC); |
| 6994 | if(uErr != QCBOR_SUCCESS) { |
| 6995 | return 3; |
| 6996 | } |
| 6997 | QCBORDecode_ExitMap(&DC); |
| 6998 | uErr = QCBORDecode_GetError(&DC); |
| 6999 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7000 | return 4; |
| 7001 | } |
| 7002 | |
| 7003 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 7004 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 7005 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7006 | uErr = QCBORDecode_GetError(&DC); |
| 7007 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 7008 | return 5; |
| 7009 | } |
| 7010 | |
| 7011 | return 0; |
| 7012 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7013 | |
| 7014 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7015 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7016 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7017 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7018 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7019 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 7020 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 7021 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 7022 | 0x03, |
| 7023 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 7024 | 0xc3, |
| 7025 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7026 | }; |
| 7027 | |
| 7028 | int32_t SpiffyIndefiniteLengthStringsTests() |
| 7029 | { |
| 7030 | QCBORDecodeContext DCtx; |
| 7031 | |
| 7032 | QCBORDecode_Init(&DCtx, |
| 7033 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 7034 | QCBOR_DECODE_MODE_NORMAL); |
| 7035 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 7036 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7037 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 7038 | |
| 7039 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 7040 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7041 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
| 7042 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7043 | return 1; |
| 7044 | } |
| 7045 | |
| 7046 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 7047 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 7048 | return 2; |
| 7049 | } |
| 7050 | |
| 7051 | uint64_t uInt; |
| 7052 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 7053 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7054 | return 3; |
| 7055 | } |
| 7056 | if(uInt != 3) { |
| 7057 | return 4; |
| 7058 | } |
| 7059 | |
| 7060 | double uDouble; |
| 7061 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 7062 | "label2", |
| 7063 | 0xff, |
| 7064 | &uDouble); |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7065 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7066 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 7067 | return 5; |
| 7068 | } |
| 7069 | if(uDouble != -16777474) { |
| 7070 | return 6; |
| 7071 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7072 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7073 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 7074 | return 7; |
| 7075 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 7076 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 7077 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 7078 | |
| 7079 | QCBORDecode_ExitMap(&DCtx); |
| 7080 | |
| 7081 | if(QCBORDecode_Finish(&DCtx)) { |
| 7082 | return 99; |
| 7083 | } |
| 7084 | |
| 7085 | return 0; |
| 7086 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 7087 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7088 | |
| 7089 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7090 | /* |
| 7091 | * An array of an integer and an array. The second array contains |
| 7092 | * a bstr-wrapped map. |
| 7093 | * |
| 7094 | * [7, [h'A36D6669... (see next lines) 73']] |
| 7095 | * |
| 7096 | * {"first integer": 42, |
| 7097 | * "an array of two strings": ["string1", "string2"], |
| 7098 | * "map in a map": |
| 7099 | * { "bytes 1": h'78787878', |
| 7100 | * "bytes 2": h'79797979', |
| 7101 | * "another int": 98, |
| 7102 | * "text 2": "lies, damn lies and statistics" |
| 7103 | * } |
| 7104 | * } |
| 7105 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7106 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7107 | static const uint8_t pValidWrappedMapEncoded[] = { |
| 7108 | 0x82, 0x07, 0x81, 0x58, 0x97, |
| 7109 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7110 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7111 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7112 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7113 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7114 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7115 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7116 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7117 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7118 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7119 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7120 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7121 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7122 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7123 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7124 | 0x73 |
| 7125 | }; |
| 7126 | |
| 7127 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7128 | |
| 7129 | /* As above, but the arrays are indefinite length */ |
| 7130 | static const uint8_t pValidIndefWrappedMapEncoded[] = { |
| 7131 | 0x9f, 0x07, 0x9f, 0x58, 0x97, |
| 7132 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 7133 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 7134 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 7135 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 7136 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 7137 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 7138 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 7139 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 7140 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 7141 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 7142 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 7143 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 7144 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 7145 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 7146 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 7147 | 0x73, |
| 7148 | 0xff, 0xff |
| 7149 | }; |
| 7150 | #endif |
| 7151 | |
| 7152 | |
| 7153 | static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0}; |
| 7154 | |
| 7155 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7156 | static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff}; |
| 7157 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7158 | |
| 7159 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7160 | static const uint8_t pWrappedByIndefiniteLength[] = { |
| 7161 | 0x81, |
| 7162 | 0xd8, 0x18, |
| 7163 | 0x5f, |
| 7164 | 0x41, 0x83, |
| 7165 | 0x41, 0x18, |
| 7166 | 0x43, 0x2A, 0x18, 0x2B, |
| 7167 | 0x42, 0x18, 0x2C, |
| 7168 | 0xff |
| 7169 | }; |
| 7170 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7171 | |
| 7172 | |
| 7173 | int32_t PeekAndRewindTest() |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7174 | { |
| 7175 | QCBORItem Item; |
| 7176 | QCBORError nCBORError; |
| 7177 | QCBORDecodeContext DCtx; |
| 7178 | |
| 7179 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7180 | |
| 7181 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7182 | return 100+(int32_t)nCBORError; |
| 7183 | } |
| 7184 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7185 | return 200; |
| 7186 | } |
| 7187 | |
| 7188 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7189 | return (int32_t)nCBORError; |
| 7190 | } |
| 7191 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7192 | return 300; |
| 7193 | } |
| 7194 | |
| 7195 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7196 | return 400 + (int32_t)nCBORError; |
| 7197 | } |
| 7198 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7199 | return 500; |
| 7200 | } |
| 7201 | |
| 7202 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7203 | return (int32_t)nCBORError; |
| 7204 | } |
| 7205 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7206 | return 600; |
| 7207 | } |
| 7208 | |
| 7209 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7210 | return 900 + (int32_t)nCBORError; |
| 7211 | } |
| 7212 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7213 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7214 | Item.val.int64 != 42 || |
| 7215 | Item.uDataAlloc || |
| 7216 | Item.uLabelAlloc || |
| 7217 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7218 | return 1000; |
| 7219 | } |
| 7220 | |
| 7221 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7222 | return 1100 + (int32_t)nCBORError; |
| 7223 | } |
| 7224 | |
| 7225 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7226 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7227 | Item.val.int64 != 42 || |
| 7228 | Item.uDataAlloc || |
| 7229 | Item.uLabelAlloc || |
| 7230 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7231 | return 1200; |
| 7232 | } |
| 7233 | |
| 7234 | |
| 7235 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7236 | return 1300 + (int32_t)nCBORError; |
| 7237 | } |
| 7238 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7239 | Item.uDataAlloc || |
| 7240 | Item.uLabelAlloc || |
| 7241 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7242 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7243 | Item.val.uCount != 2) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7244 | return 1400; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7245 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7246 | |
| 7247 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7248 | return 1500 + (int32_t)nCBORError; |
| 7249 | } |
| 7250 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7251 | Item.uDataAlloc || |
| 7252 | Item.uLabelAlloc || |
| 7253 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7254 | return 1600; |
| 7255 | } |
| 7256 | |
| 7257 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7258 | return 1700 + (int32_t)nCBORError; |
| 7259 | } |
| 7260 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7261 | Item.uDataAlloc || |
| 7262 | Item.uLabelAlloc || |
| 7263 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7264 | return 1800; |
| 7265 | } |
| 7266 | |
| 7267 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7268 | return (int32_t)nCBORError; |
| 7269 | } |
| 7270 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7271 | Item.uDataAlloc || |
| 7272 | Item.uLabelAlloc || |
| 7273 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7274 | return 1900; |
| 7275 | } |
| 7276 | |
| 7277 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7278 | return (int32_t)nCBORError; |
| 7279 | } |
| 7280 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7281 | Item.uDataAlloc || |
| 7282 | Item.uLabelAlloc || |
| 7283 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7284 | return 2000; |
| 7285 | } |
| 7286 | |
| 7287 | |
| 7288 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7289 | return 2100 + (int32_t)nCBORError; |
| 7290 | } |
| 7291 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7292 | Item.uDataAlloc || |
| 7293 | Item.uLabelAlloc || |
| 7294 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 7295 | Item.uDataType != QCBOR_TYPE_MAP || |
| 7296 | Item.val.uCount != 4) { |
| 7297 | return 2100; |
| 7298 | } |
| 7299 | |
| 7300 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7301 | return 2200 + (int32_t)nCBORError; |
| 7302 | } |
| 7303 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7304 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 7305 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7306 | Item.uDataAlloc || |
| 7307 | Item.uLabelAlloc || |
| 7308 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 7309 | return 2300; |
| 7310 | } |
| 7311 | |
| 7312 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7313 | return 2400 + (int32_t)nCBORError; |
| 7314 | } |
| 7315 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7316 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7317 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7318 | Item.uDataAlloc || |
| 7319 | Item.uLabelAlloc || |
| 7320 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7321 | return 2500; |
| 7322 | } |
| 7323 | |
| 7324 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7325 | return 2600 + (int32_t)nCBORError; |
| 7326 | } |
| 7327 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7328 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7329 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7330 | Item.uDataAlloc || |
| 7331 | Item.uLabelAlloc || |
| 7332 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7333 | return 2700; |
| 7334 | } |
| 7335 | |
| 7336 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7337 | return 2800 + (int32_t)nCBORError; |
| 7338 | } |
| 7339 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7340 | Item.uDataAlloc || |
| 7341 | Item.uLabelAlloc || |
| 7342 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 7343 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7344 | Item.val.int64 != 98) { |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7345 | return 2900; |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7346 | } |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7347 | |
| 7348 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7349 | return 3000 + (int32_t)nCBORError; |
| 7350 | } |
| 7351 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7352 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7353 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7354 | Item.uDataAlloc || |
| 7355 | Item.uLabelAlloc || |
| 7356 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7357 | return 3100; |
| 7358 | } |
| 7359 | |
| 7360 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7361 | return 3200 + (int32_t)nCBORError; |
| 7362 | } |
| 7363 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7364 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7365 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7366 | Item.uDataAlloc || |
| 7367 | Item.uLabelAlloc || |
| 7368 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7369 | return 3300; |
| 7370 | } |
| 7371 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7372 | |
| 7373 | |
| 7374 | // Rewind to top level after entering several maps |
| 7375 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7376 | |
| 7377 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7378 | return (int32_t)nCBORError; |
| 7379 | } |
| 7380 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 7381 | Item.val.uCount != 3) { |
| 7382 | return 400; |
| 7383 | } |
| 7384 | |
| 7385 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7386 | return 4000+(int32_t)nCBORError; |
| 7387 | } |
| 7388 | |
| 7389 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7390 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7391 | Item.val.int64 != 42 || |
| 7392 | Item.uDataAlloc || |
| 7393 | Item.uLabelAlloc || |
| 7394 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7395 | return 4100; |
| 7396 | } |
| 7397 | |
| 7398 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7399 | return 4100+(int32_t)nCBORError; |
| 7400 | } |
| 7401 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7402 | Item.uDataAlloc || |
| 7403 | Item.uLabelAlloc || |
| 7404 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7405 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7406 | Item.val.uCount != 2) { |
| 7407 | return 4200; |
| 7408 | } |
| 7409 | |
| 7410 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7411 | return 4200+(int32_t)nCBORError; |
| 7412 | } |
| 7413 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7414 | Item.uDataAlloc || |
| 7415 | Item.uLabelAlloc || |
| 7416 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7417 | return 4300; |
| 7418 | } |
| 7419 | |
| 7420 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7421 | return 4300+(int32_t)nCBORError; |
| 7422 | } |
| 7423 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7424 | Item.uDataAlloc || |
| 7425 | Item.uLabelAlloc || |
| 7426 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7427 | return 4400; |
| 7428 | } |
| 7429 | |
| 7430 | QCBORDecode_Rewind(&DCtx); |
| 7431 | |
| 7432 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7433 | return 4400+(int32_t)nCBORError; |
| 7434 | } |
| 7435 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 7436 | Item.val.uCount != 3) { |
| 7437 | return 4500; |
| 7438 | } |
| 7439 | |
| 7440 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7441 | return (int32_t)nCBORError; |
| 7442 | } |
| 7443 | |
| 7444 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7445 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7446 | Item.val.int64 != 42 || |
| 7447 | Item.uDataAlloc || |
| 7448 | Item.uLabelAlloc || |
| 7449 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7450 | return 4600; |
| 7451 | } |
| 7452 | |
| 7453 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7454 | return (int32_t)nCBORError; |
| 7455 | } |
| 7456 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7457 | Item.uDataAlloc || |
| 7458 | Item.uLabelAlloc || |
| 7459 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7460 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7461 | Item.val.uCount != 2) { |
| 7462 | return 4700; |
| 7463 | } |
| 7464 | |
| 7465 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7466 | return (int32_t)nCBORError; |
| 7467 | } |
| 7468 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7469 | Item.uDataAlloc || |
| 7470 | Item.uLabelAlloc || |
| 7471 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7472 | return 4800; |
| 7473 | } |
| 7474 | |
| 7475 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7476 | return 4900+(int32_t)nCBORError; |
| 7477 | } |
| 7478 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7479 | Item.uDataAlloc || |
| 7480 | Item.uLabelAlloc || |
| 7481 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7482 | return 5000; |
| 7483 | } |
| 7484 | |
| 7485 | |
| 7486 | // Rewind an entered map |
| 7487 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7488 | |
| 7489 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7490 | |
| 7491 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7492 | return 5100+(int32_t)nCBORError; |
| 7493 | } |
| 7494 | |
| 7495 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7496 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7497 | Item.val.int64 != 42 || |
| 7498 | Item.uDataAlloc || |
| 7499 | Item.uLabelAlloc || |
| 7500 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7501 | return 5200; |
| 7502 | } |
| 7503 | |
| 7504 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7505 | return 5200+(int32_t)nCBORError; |
| 7506 | } |
| 7507 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7508 | Item.uDataAlloc || |
| 7509 | Item.uLabelAlloc || |
| 7510 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7511 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7512 | Item.val.uCount != 2) { |
| 7513 | return -5300; |
| 7514 | } |
| 7515 | |
| 7516 | QCBORDecode_Rewind(&DCtx); |
| 7517 | |
| 7518 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7519 | return 5300+(int32_t)nCBORError; |
| 7520 | } |
| 7521 | |
| 7522 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7523 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7524 | Item.val.int64 != 42 || |
| 7525 | Item.uDataAlloc || |
| 7526 | Item.uLabelAlloc || |
| 7527 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7528 | return 5400; |
| 7529 | } |
| 7530 | |
| 7531 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7532 | return 5400+(int32_t)nCBORError; |
| 7533 | } |
| 7534 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7535 | Item.uDataAlloc || |
| 7536 | Item.uLabelAlloc || |
| 7537 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7538 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7539 | Item.val.uCount != 2) { |
| 7540 | return 5500; |
| 7541 | } |
| 7542 | |
| 7543 | |
| 7544 | // Rewind and entered array inside an entered map |
| 7545 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 7546 | |
| 7547 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7548 | |
| 7549 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 7550 | |
| 7551 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7552 | return 5600+(int32_t)nCBORError; |
| 7553 | } |
| 7554 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7555 | Item.uDataAlloc || |
| 7556 | Item.uLabelAlloc || |
| 7557 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7558 | return 5700; |
| 7559 | } |
| 7560 | |
| 7561 | QCBORDecode_Rewind(&DCtx); |
| 7562 | |
| 7563 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7564 | return 5700+(int32_t)nCBORError; |
| 7565 | } |
| 7566 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7567 | Item.uDataAlloc || |
| 7568 | Item.uLabelAlloc || |
| 7569 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7570 | return 5800; |
| 7571 | } |
| 7572 | |
| 7573 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7574 | return (int32_t)nCBORError; |
| 7575 | } |
| 7576 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7577 | Item.uDataAlloc || |
| 7578 | Item.uLabelAlloc || |
| 7579 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7580 | return 5900; |
| 7581 | } |
| 7582 | |
| 7583 | QCBORDecode_Rewind(&DCtx); |
| 7584 | |
| 7585 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7586 | return 5900+(int32_t)nCBORError; |
| 7587 | } |
| 7588 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7589 | Item.uDataAlloc || |
| 7590 | Item.uLabelAlloc || |
| 7591 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7592 | return 6000; |
| 7593 | } |
| 7594 | |
| 7595 | |
| 7596 | // Rewind a byte string inside an array inside an array |
| 7597 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0); |
| 7598 | |
| 7599 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7600 | |
| 7601 | uint64_t i; |
| 7602 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7603 | |
| 7604 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7605 | |
| 7606 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7607 | if(QCBORDecode_GetError(&DCtx)) { |
| 7608 | return 6100; |
| 7609 | } |
| 7610 | |
| 7611 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7612 | return (int32_t)nCBORError; |
| 7613 | } |
| 7614 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7615 | return 6200; |
| 7616 | } |
| 7617 | |
| 7618 | QCBORDecode_Rewind(&DCtx); |
| 7619 | |
| 7620 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7621 | return 6300+(int32_t)nCBORError; |
| 7622 | } |
| 7623 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7624 | return 6400; |
| 7625 | } |
| 7626 | |
| 7627 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7628 | // Rewind a byte string inside an indefinite-length array inside |
| 7629 | // indefinite-length array |
| 7630 | |
| 7631 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0); |
| 7632 | |
| 7633 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7634 | |
| 7635 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7636 | |
| 7637 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7638 | |
| 7639 | QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 7640 | if(QCBORDecode_GetError(&DCtx)) { |
| 7641 | return 6500; |
| 7642 | } |
| 7643 | |
| 7644 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7645 | return 6600+(int32_t)nCBORError; |
| 7646 | } |
| 7647 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7648 | return 6700; |
| 7649 | } |
| 7650 | |
| 7651 | QCBORDecode_Rewind(&DCtx); |
| 7652 | |
| 7653 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7654 | return 6800+(int32_t)nCBORError; |
| 7655 | } |
| 7656 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7657 | return 6900; |
| 7658 | } |
| 7659 | #endif |
| 7660 | |
| 7661 | // Rewind an empty map |
| 7662 | // [100, {}] |
| 7663 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0); |
| 7664 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7665 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7666 | if(i != 100) { |
| 7667 | return 7010; |
| 7668 | } |
| 7669 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7670 | |
| 7671 | /* Do it 5 times to be sure multiple rewinds work */ |
| 7672 | for(int n = 0; n < 5; n++) { |
| 7673 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7674 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7675 | return 7000 + n; |
| 7676 | } |
| 7677 | QCBORDecode_Rewind(&DCtx); |
| 7678 | } |
| 7679 | QCBORDecode_ExitMap(&DCtx); |
| 7680 | QCBORDecode_Rewind(&DCtx); |
| 7681 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7682 | if(i != 100) { |
| 7683 | return 7010; |
| 7684 | } |
| 7685 | QCBORDecode_ExitArray(&DCtx); |
| 7686 | QCBORDecode_Rewind(&DCtx); |
| 7687 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7688 | i = 9; |
| 7689 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7690 | if(i != 100) { |
| 7691 | return 7020; |
| 7692 | } |
| 7693 | if(QCBORDecode_GetError(&DCtx)){ |
| 7694 | return 7030; |
| 7695 | } |
| 7696 | |
| 7697 | // Rewind an empty indefinite length map |
| 7698 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 7699 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0); |
| 7700 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7701 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7702 | if(i != 100) { |
| 7703 | return 7810; |
| 7704 | } |
| 7705 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 7706 | |
| 7707 | /* Do it 5 times to be sure multiple rewinds work */ |
| 7708 | for(int n = 0; n < 5; n++) { |
| 7709 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 7710 | if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 7711 | return 7800 + n; |
| 7712 | } |
| 7713 | QCBORDecode_Rewind(&DCtx); |
| 7714 | } |
| 7715 | QCBORDecode_ExitMap(&DCtx); |
| 7716 | QCBORDecode_Rewind(&DCtx); |
| 7717 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7718 | if(i != 100) { |
| 7719 | return 7810; |
| 7720 | } |
| 7721 | QCBORDecode_ExitArray(&DCtx); |
| 7722 | QCBORDecode_Rewind(&DCtx); |
| 7723 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7724 | i = 9; |
| 7725 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7726 | if(i != 100) { |
| 7727 | return 7820; |
| 7728 | } |
| 7729 | if(QCBORDecode_GetError(&DCtx)){ |
| 7730 | return 7830; |
| 7731 | } |
| 7732 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 7733 | |
| 7734 | // Rewind an indefnite length byte-string wrapped sequence |
| 7735 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 7736 | QCBORDecode_Init(&DCtx, |
| 7737 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength), |
| 7738 | 0); |
| 7739 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 7740 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 7741 | |
| 7742 | QCBORDecode_EnterArray(&DCtx, NULL); |
| 7743 | QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL); |
| 7744 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 7745 | /* this is what happens when trying to enter |
| 7746 | indefinite-length byte string |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 7747 | wrapped CBOR. Tolerate for now. Eventually it needs |
| 7748 | to be fixed so this works, but that is not simple. */ |
| 7749 | return 7300; |
| 7750 | } |
| 7751 | |
| 7752 | /* |
| 7753 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7754 | if(i != 42) { |
| 7755 | return 7110; |
| 7756 | } |
| 7757 | QCBORDecode_Rewind(&DCtx); |
| 7758 | QCBORDecode_GetUInt64(&DCtx, &i); |
| 7759 | if(i != 42) { |
| 7760 | return 7220; |
| 7761 | }*/ |
| 7762 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 7763 | |
| 7764 | |
| 7765 | // Rewind an indefnite length byte-string wrapped sequence |
| 7766 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 7767 | return 0; |
| 7768 | } |