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 | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2020, 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 | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 921 | /* [[[[[[[[[[]]]]]]]]]] */ |
| 922 | static const uint8_t spDeepArrays[] = { |
| 923 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 924 | 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 925 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 926 | int32_t ParseDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 927 | { |
| 928 | QCBORDecodeContext DCtx; |
| 929 | int nReturn = 0; |
| 930 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 931 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 932 | QCBORDecode_Init(&DCtx, |
| 933 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 934 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 935 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 936 | for(i = 0; i < 10; i++) { |
| 937 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 938 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 939 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 940 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 941 | Item.uNestingLevel != i) { |
| 942 | nReturn = -1; |
| 943 | break; |
| 944 | } |
| 945 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 946 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 947 | return(nReturn); |
| 948 | } |
| 949 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 950 | /* Big enough to test nesting to the depth of 24 |
| 951 | [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]] |
| 952 | */ |
| 953 | static const uint8_t spTooDeepArrays[] = { |
| 954 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 955 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 956 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 957 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 958 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 959 | int32_t ParseTooDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 960 | { |
| 961 | QCBORDecodeContext DCtx; |
| 962 | int nReturn = 0; |
| 963 | int i; |
| 964 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 965 | |
| 966 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 967 | QCBORDecode_Init(&DCtx, |
| 968 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 969 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 970 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 971 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 972 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 973 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 974 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 975 | Item.uNestingLevel != i) { |
| 976 | nReturn = -1; |
| 977 | break; |
| 978 | } |
| 979 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 980 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 981 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 982 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 983 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 984 | return(nReturn); |
| 985 | } |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 990 | int32_t ShortBufferParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 991 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 992 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 993 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 994 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 995 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 996 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 997 | QCBORDecode_Init(&DCtx, |
| 998 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 999 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1000 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1001 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1002 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1003 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1004 | nResult = -1; |
| 1005 | goto Done; |
| 1006 | } |
| 1007 | } |
| 1008 | Done: |
| 1009 | return nResult; |
| 1010 | } |
| 1011 | |
| 1012 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1013 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1014 | int32_t ShortBufferParseTest2() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1015 | { |
| 1016 | uint8_t *pEncoded; |
| 1017 | int nReturn; |
| 1018 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1019 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1020 | int64_t i1, i2; |
| 1021 | size_t i3, i4; |
| 1022 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1023 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1024 | nReturn = 0; |
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 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 1027 | return(-1); |
| 1028 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1029 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1030 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1031 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 1032 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1033 | if(nResult == 0) { |
| 1034 | nReturn = -1; |
| 1035 | } |
| 1036 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1037 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1038 | return(nReturn); |
| 1039 | } |
| 1040 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1041 | /* |
| 1042 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1043 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 1044 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1045 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1046 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1047 | { |
| 1048 | QCBORDecodeContext DCtx; |
| 1049 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1050 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1051 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1052 | QCBORDecode_Init(&DCtx, |
| 1053 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1054 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1056 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1057 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1058 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1059 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1060 | Item.val.uCount != 3) |
| 1061 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1062 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1063 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1064 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1065 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1066 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1067 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1068 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1069 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1070 | Item.uDataAlloc || |
| 1071 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1072 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1073 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1074 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1075 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1076 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1077 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1078 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1079 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1080 | Item.uDataAlloc || |
| 1081 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1082 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1083 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1084 | Item.val.uCount != 2) |
| 1085 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1086 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1087 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1088 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1089 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1090 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1091 | Item.uDataAlloc || |
| 1092 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1093 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1094 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1095 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1096 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1097 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1098 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1099 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1100 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1101 | Item.uDataAlloc || |
| 1102 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1103 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1104 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1105 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1106 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1107 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1108 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1109 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1110 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1111 | Item.uDataAlloc || |
| 1112 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1113 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1114 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1115 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1116 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1117 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1118 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1119 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1120 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1121 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1122 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1123 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1124 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1125 | Item.uDataAlloc || |
| 1126 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1127 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1128 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1129 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1130 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1131 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1132 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1133 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1134 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1135 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1136 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1137 | Item.uDataAlloc || |
| 1138 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1139 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1140 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1141 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1142 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1143 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1144 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1145 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1146 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1147 | Item.uDataAlloc || |
| 1148 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1149 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1150 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1151 | Item.val.int64 != 98) |
| 1152 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1153 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1154 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1155 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1156 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1157 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1158 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1159 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1160 | Item.uDataAlloc || |
| 1161 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1162 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1163 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1164 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1165 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1170 | /* |
| 1171 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1172 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1173 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1174 | int32_t ParseMapAsArrayTest() |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1175 | { |
| 1176 | QCBORDecodeContext DCtx; |
| 1177 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1178 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1179 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1180 | QCBORDecode_Init(&DCtx, |
| 1181 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1182 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1183 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1184 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1185 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1186 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1187 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1188 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1189 | return -1; |
| 1190 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1191 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1192 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1193 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1194 | } |
| 1195 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1196 | Item.uDataAlloc || |
| 1197 | Item.uLabelAlloc || |
| 1198 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1199 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1200 | return -2; |
| 1201 | } |
| 1202 | |
| 1203 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1204 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1205 | } |
| 1206 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1207 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1208 | Item.val.int64 != 42 || |
| 1209 | Item.uDataAlloc || |
| 1210 | Item.uLabelAlloc) { |
| 1211 | return -3; |
| 1212 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1213 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1214 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1215 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1216 | } |
| 1217 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1218 | Item.uDataAlloc || |
| 1219 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1220 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1221 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1222 | return -4; |
| 1223 | } |
| 1224 | |
| 1225 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1226 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1227 | } |
| 1228 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1229 | Item.uDataAlloc || |
| 1230 | Item.uLabelAlloc || |
| 1231 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1232 | Item.val.uCount != 2) { |
| 1233 | return -5; |
| 1234 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1235 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1236 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1237 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1238 | } |
| 1239 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1240 | Item.val.string.len != 7 || |
| 1241 | Item.uDataAlloc || |
| 1242 | Item.uLabelAlloc || |
| 1243 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1244 | return -6; |
| 1245 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1246 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1247 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1248 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1249 | } |
| 1250 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1251 | Item.uDataAlloc || |
| 1252 | Item.uLabelAlloc || |
| 1253 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1254 | return -7; |
| 1255 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1256 | |
| 1257 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1258 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1259 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1260 | } |
| 1261 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1262 | Item.uDataAlloc || |
| 1263 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1264 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1265 | return -8; |
| 1266 | } |
| 1267 | |
| 1268 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1269 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1270 | } |
| 1271 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1272 | Item.uDataAlloc || |
| 1273 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1274 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1275 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1276 | return -9; |
| 1277 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1278 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1279 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1280 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1281 | } |
| 1282 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1283 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1284 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1285 | Item.uDataAlloc || |
| 1286 | Item.uLabelAlloc) { |
| 1287 | return -10; |
| 1288 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1289 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1290 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1291 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1292 | } |
| 1293 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1294 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1295 | Item.uDataAlloc || |
| 1296 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1297 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1298 | return -11; |
| 1299 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1300 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1301 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1302 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1303 | } |
| 1304 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1305 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1306 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1307 | Item.uDataAlloc || |
| 1308 | Item.uLabelAlloc) { |
| 1309 | return -12; |
| 1310 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1311 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1312 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1313 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1314 | } |
| 1315 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1316 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1317 | Item.uDataAlloc || |
| 1318 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1319 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1320 | return -13; |
| 1321 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1322 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1323 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1324 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1325 | } |
| 1326 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1327 | Item.uDataAlloc || |
| 1328 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1329 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1330 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1331 | return -14; |
| 1332 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1333 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1334 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1335 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1336 | } |
| 1337 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1338 | Item.uDataAlloc || |
| 1339 | Item.uLabelAlloc || |
| 1340 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1341 | Item.val.int64 != 98) { |
| 1342 | return -15; |
| 1343 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1344 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1345 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1346 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1347 | } |
| 1348 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1349 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1350 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1351 | Item.uDataAlloc || |
| 1352 | Item.uLabelAlloc) { |
| 1353 | return -16; |
| 1354 | } |
| 1355 | |
| 1356 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1357 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1358 | } |
| 1359 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1360 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1361 | Item.uDataAlloc || |
| 1362 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1363 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1364 | return -17; |
| 1365 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1366 | |
| 1367 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1368 | /* |
| 1369 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1370 | map that when interpreted as an array will be too many. Test |
| 1371 | data just has the start of the map, not all the items in the map. |
| 1372 | */ |
| 1373 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1374 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1375 | QCBORDecode_Init(&DCtx, |
| 1376 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1377 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1378 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1379 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1380 | return -50; |
| 1381 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1382 | |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1383 | // TODO: test decoding of labels that are arrays or such |
| 1384 | // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY |
| 1385 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1389 | |
| 1390 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1391 | Fully or partially decode pValidMapEncoded. When |
| 1392 | partially decoding check for the right error code. |
| 1393 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1394 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1395 | The partial decodes test error conditions of |
| 1396 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1397 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1398 | This could be combined with the above test |
| 1399 | and made prettier and maybe a little more |
| 1400 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1401 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1402 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1403 | { |
| 1404 | QCBORDecodeContext DCtx; |
| 1405 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1406 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1407 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1408 | QCBORDecode_Init(&DCtx, |
| 1409 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1410 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1411 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1412 | if(nLevel < 1) { |
| 1413 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1414 | return -1; |
| 1415 | } else { |
| 1416 | return 0; |
| 1417 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1418 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1419 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1420 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1421 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1422 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1423 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1424 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1425 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1426 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1427 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1428 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1429 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1430 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1431 | } else { |
| 1432 | return 0; |
| 1433 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1434 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1435 | |
| 1436 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1437 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1438 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1439 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1440 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1441 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1442 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1443 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1444 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1445 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1446 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1447 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1448 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1449 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1450 | } else { |
| 1451 | return 0; |
| 1452 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1453 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1454 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1455 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1456 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1457 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1458 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1459 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1460 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1461 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1462 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1463 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1464 | |
| 1465 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1466 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1467 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1468 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1469 | } else { |
| 1470 | return 0; |
| 1471 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1472 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1473 | |
| 1474 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1475 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1476 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1477 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1478 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1479 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1480 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1481 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1482 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1483 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1484 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1485 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1486 | } else { |
| 1487 | return 0; |
| 1488 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1489 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1490 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1491 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1492 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1493 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1494 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1495 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1496 | return -10; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1497 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1498 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1499 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1500 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1501 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1502 | } else { |
| 1503 | return 0; |
| 1504 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1505 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1506 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1507 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1508 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1509 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1510 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1511 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1512 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1513 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1514 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1515 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1516 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1517 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1518 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1519 | } else { |
| 1520 | return 0; |
| 1521 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1522 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1523 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1524 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1525 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1526 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1527 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1528 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1529 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1530 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1531 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1532 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1533 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1534 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1535 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1536 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1537 | } else { |
| 1538 | return 0; |
| 1539 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1540 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1541 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1542 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1543 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1544 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1545 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1546 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1547 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1548 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1549 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1550 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1551 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1552 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1553 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1554 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1555 | } else { |
| 1556 | return 0; |
| 1557 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1558 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1559 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1560 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1561 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1562 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1563 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1564 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1565 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1566 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1567 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1568 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1569 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1570 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1571 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1572 | } else { |
| 1573 | return 0; |
| 1574 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1575 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1576 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1577 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1578 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1579 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1580 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1581 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1582 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1583 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1584 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1585 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1586 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1587 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1588 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1589 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1590 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1596 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1597 | int32_t ParseMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1598 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1599 | // Parse a moderatly complex map structure very thoroughly |
| 1600 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 1601 | if(nResult) { |
| 1602 | return nResult; |
| 1603 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1604 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1605 | // Again, but in strings-only mode. It should succeed since the input |
| 1606 | // map has only string labels. |
| 1607 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 1608 | if(nResult) { |
| 1609 | return nResult; |
| 1610 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1611 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1612 | // Again, but try to finish the decoding before the end of the |
| 1613 | // input at 10 different place and see that the right error code |
| 1614 | // is returned. |
| 1615 | for(int i = 0; i < 10; i++) { |
| 1616 | nResult = ExtraBytesTest(i); |
| 1617 | if(nResult) { |
| 1618 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1619 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1620 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1621 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1622 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1626 | /* The simple-values including some not well formed */ |
| 1627 | static const uint8_t spSimpleValues[] = { |
| 1628 | 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, |
| 1629 | 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, |
| 1630 | 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1631 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1632 | int32_t ParseSimpleTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1633 | { |
| 1634 | QCBORDecodeContext DCtx; |
| 1635 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1636 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1637 | |
| 1638 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1639 | QCBORDecode_Init(&DCtx, |
| 1640 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1641 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1642 | |
| 1643 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1644 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1645 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1646 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1647 | Item.val.uCount != 10) |
| 1648 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1649 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1650 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1651 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1652 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1653 | return -1; |
| 1654 | |
| 1655 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1656 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1657 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1658 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1659 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1660 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1661 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1662 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1663 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1664 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1665 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1666 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1667 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1668 | return -1; |
| 1669 | |
| 1670 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1671 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1672 | return -1; |
| 1673 | |
| 1674 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1675 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1676 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1677 | return -1; |
| 1678 | |
| 1679 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1680 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1681 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1682 | return -1; |
| 1683 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1684 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1685 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1686 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1687 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1688 | return -1; |
| 1689 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1690 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1691 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1692 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1693 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1694 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1695 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1696 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1697 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1698 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1699 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1700 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1701 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1702 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1703 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1704 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1708 | int32_t NotWellFormedTests() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1709 | { |
| 1710 | // Loop over all the not-well-formed instance of CBOR |
| 1711 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1712 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 1713 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1714 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 1715 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 1716 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 1717 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1718 | // Set up decoder context. String allocator needed for indefinite |
| 1719 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1720 | QCBORDecodeContext DCtx; |
| 1721 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1722 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1723 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1724 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1725 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1726 | |
| 1727 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1728 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1729 | do { |
| 1730 | QCBORItem Item; |
| 1731 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1732 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1733 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1734 | |
| 1735 | // Every test vector must fail with |
| 1736 | // a not-well-formed error. If not |
| 1737 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1738 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1739 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1740 | /* Return index of failure and QCBOR error in the result */ |
| 1741 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1748 | // 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] | 1749 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1750 | UsefulBufC Input; |
| 1751 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1752 | }; |
| 1753 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1754 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1755 | static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1756 | { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1757 | for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1758 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1759 | QCBORError uCBORError; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1760 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1761 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1762 | |
| 1763 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1764 | // Set up the decoding context including a memory pool so that |
| 1765 | // indefinite length items can be checked |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1766 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1767 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1768 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1769 | if(uCBORError) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1770 | return -9; |
| 1771 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1772 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1773 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1774 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1775 | // Iterate until there is an error of some sort error |
| 1776 | QCBORItem Item; |
| 1777 | do { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1778 | // Set to something none-zero, something other than QCBOR_TYPE_NONE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1779 | memset(&Item, 0x33, sizeof(Item)); |
| 1780 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1781 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1782 | } while(uCBORError == QCBOR_SUCCESS); |
| 1783 | |
| 1784 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1785 | |
| 1786 | // Must get the expected error or the this test fails |
| 1787 | // The data and label type must also be QCBOR_TYPE_NONE |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1788 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1789 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1790 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1791 | // return index of CBOR + 100 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1792 | const size_t nIndex = (size_t)(pF - pFailInputs); |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1793 | return (int32_t)(nIndex * 100 + uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 1801 | static const struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1802 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1803 | // returned is also checked. |
| 1804 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1805 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1806 | // Indefinite length strings must be closed off |
| 1807 | // An indefinite length byte string not closed off |
| 1808 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1809 | // An indefinite length text string not closed off |
| 1810 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1811 | |
| 1812 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1813 | // All the chunks in an indefinite length string must be of the type of |
| 1814 | // indefinite length string |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1815 | // indefinite length byte string with text string chunk |
| 1816 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1817 | // indefinite length text string with a byte string chunk |
| 1818 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1819 | // indefinite length byte string with an positive integer chunk |
| 1820 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1821 | // indefinite length byte string with an negative integer chunk |
| 1822 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1823 | // indefinite length byte string with an array chunk |
| 1824 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1825 | // indefinite length byte string with an map chunk |
| 1826 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1827 | // indefinite length byte string with tagged integer chunk |
| 1828 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1829 | // indefinite length byte string with an simple type chunk |
| 1830 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1831 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1832 | // indefinite length text string with indefinite string inside |
| 1833 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1834 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1835 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1836 | |
| 1837 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1838 | // An indefinite length text string not closed off |
| 1839 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1840 | |
| 1841 | |
| 1842 | // All the chunks in an indefinite length string must be of the type of |
| 1843 | // indefinite length string |
| 1844 | // indefinite length byte string with text string chunk |
| 1845 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1846 | // indefinite length text string with a byte string chunk |
| 1847 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1848 | // indefinite length byte string with an positive integer chunk |
| 1849 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1850 | // indefinite length byte string with an negative integer chunk |
| 1851 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1852 | // indefinite length byte string with an array chunk |
| 1853 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1854 | // indefinite length byte string with an map chunk |
| 1855 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1856 | // indefinite length byte string with tagged integer chunk |
| 1857 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1858 | // indefinite length byte string with an simple type chunk |
| 1859 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1860 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1861 | // indefinite length text string with indefinite string inside |
| 1862 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1863 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1864 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1865 | |
| 1866 | // Definte length maps and arrays must be closed by having the right number of items |
| 1867 | // 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] | 1868 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1869 | // 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] | 1870 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1871 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1872 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1873 | // 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] | 1874 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1875 | // 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] | 1876 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1877 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1878 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1879 | // Indefinte length maps and arrays must be ended by a break |
| 1880 | // Indefinite length array with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1881 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1882 | // Indefinite length array with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1883 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1884 | // Indefinite length map with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1885 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1886 | // Indefinite length map with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1887 | { {(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] | 1888 | |
| 1889 | |
| 1890 | // 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] | 1891 | // Unclosed indefinite array containing a closed definite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1892 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1893 | // Definite length array containing an unclosed indefinite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1894 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1895 | // Unclosed indefinite map containing a closed definite length array |
| 1896 | { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
| 1897 | // Definite length map containing an unclosed indefinite length array |
| 1898 | { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1899 | // Deeply nested definite length arrays with deepest one unclosed |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1900 | { {(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] | 1901 | // Deeply nested indefinite length arrays with deepest one unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1902 | { {(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] | 1903 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1904 | { {(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] | 1905 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1906 | { {(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] | 1907 | // Unclosed indefinite length map in definite length maps |
| 1908 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8}, |
| 1909 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1910 | // Unclosed definite length map in indefinite length maps |
| 1911 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1912 | // Unclosed indefinite length array in definite length maps |
| 1913 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8}, |
| 1914 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1915 | // Unclosed definite length array in indefinite length maps |
| 1916 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1917 | // Unclosed indefinite length map in definite length arrays |
| 1918 | { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1919 | // Unclosed definite length map in indefinite length arrays |
| 1920 | { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1921 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1922 | |
| 1923 | // The "argument" for the data item is incomplete |
| 1924 | // Positive integer missing 1 byte argument |
| 1925 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1926 | // Positive integer missing 2 byte argument |
| 1927 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1928 | // Positive integer missing 4 byte argument |
| 1929 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1930 | // Positive integer missing 8 byte argument |
| 1931 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1932 | // Positive integer missing 1 byte of 2 byte argument |
| 1933 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1934 | // Positive integer missing 2 bytes of 4 byte argument |
| 1935 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1936 | // Positive integer missing 1 bytes of 7 byte argument |
| 1937 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1938 | // Negative integer missing 1 byte argument |
| 1939 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1940 | // Binary string missing 1 byte argument |
| 1941 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1942 | // Text string missing 1 byte argument |
| 1943 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 1944 | // Array missing 1 byte argument |
| 1945 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 1946 | // Map missing 1 byte argument |
| 1947 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 1948 | // Tag missing 1 byte argument |
| 1949 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 1950 | // Simple missing 1 byte argument |
| 1951 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1952 | // half-precision with 1 byte argument |
| 1953 | { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1954 | // single-precision with 2 byte argument |
| 1955 | { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1956 | // double-precision with 3 byte argument |
| 1957 | { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1958 | |
| 1959 | |
| 1960 | // Tag with no content |
| 1961 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1962 | |
| 1963 | |
| 1964 | // Breaks must not occur in definite length arrays and maps |
| 1965 | // Array of length 1 with sole member replaced by a break |
| 1966 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1967 | // Array of length 2 with 2nd member replaced by a break |
| 1968 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1969 | // Map of length 1 with sole member label replaced by a break |
| 1970 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1971 | // Map of length 1 with sole member label replaced by break |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1972 | // Alternate representation that some decoders handle differently |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1973 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1974 | // Array of length 1 with 2nd member value replaced by a break |
| 1975 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1976 | // Map of length 2 with 2nd member replaced by a break |
| 1977 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 1978 | |
| 1979 | |
| 1980 | // Breaks must not occur on their own out of an indefinite length data item |
| 1981 | // A bare break is not well formed |
| 1982 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 1983 | // A bare break after a zero length definite length array |
| 1984 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1985 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1986 | // A bare break after a zero length indefinite length map |
| 1987 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1988 | // A break inside a definite length array inside an indefenite length array |
| 1989 | { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1990 | // Complicated mixed nesting with break outside indefinite length array |
| 1991 | { {(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] | 1992 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1993 | |
| 1994 | |
| 1995 | // Forbidden two byte encodings of simple types |
| 1996 | // Must use 0xe0 instead |
| 1997 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1998 | // Should use 0xe1 instead |
| 1999 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2000 | // Should use 0xe2 instead |
| 2001 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2002 | // Should use 0xe3 instead |
| 2003 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2004 | // Should use 0xe4 instead |
| 2005 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2006 | // Should use 0xe5 instead |
| 2007 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2008 | // Should use 0xe6 instead |
| 2009 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2010 | // Should use 0xe7 instead |
| 2011 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2012 | // Should use 0xe8 instead |
| 2013 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2014 | // Should use 0xe9 instead |
| 2015 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2016 | // Should use 0xea instead |
| 2017 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2018 | // Should use 0xeb instead |
| 2019 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2020 | // Should use 0xec instead |
| 2021 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2022 | // Should use 0xed instead |
| 2023 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2024 | // Should use 0xee instead |
| 2025 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2026 | // Should use 0xef instead |
| 2027 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2028 | // Should use 0xf0 instead |
| 2029 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2030 | // Should use 0xf1 instead |
| 2031 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2032 | // Should use 0xf2 instead |
| 2033 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2034 | // Must use 0xf3 instead |
| 2035 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2036 | // Must use 0xf4 instead |
| 2037 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2038 | // Must use 0xf5 instead |
| 2039 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2040 | // Must use 0xf6 instead |
| 2041 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2042 | // Must use 0xf7 instead |
| 2043 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 2044 | // Must use 0xf8 instead |
| 2045 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 2046 | // Reserved |
| 2047 | { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2048 | |
| 2049 | // Integers with additional info indefinite length |
| 2050 | // Positive integer with additional info indefinite length |
| 2051 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2052 | // Negative integer with additional info indefinite length |
| 2053 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
| 2054 | // CBOR tag with "argument" an indefinite length |
| 2055 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 2056 | // CBOR tag with "argument" an indefinite length alternate vector |
| 2057 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
| 2058 | |
| 2059 | |
| 2060 | // Missing bytes from a deterministic length string |
| 2061 | // A byte string is of length 1 without the 1 byte |
| 2062 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 2063 | // A text string is of length 1 without the 1 byte |
| 2064 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2065 | |
| 2066 | #if SIZE_MAX > 2147483647 |
Laurence Lundblade | 42272e4 | 2020-01-31 07:50:53 -0800 | [diff] [blame] | 2067 | // Byte string should have 2^32-15 bytes, but has one |
| 2068 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2069 | // Byte string should have 2^32-15 bytes, but has one |
| 2070 | { {(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] | 2071 | // Byte string should have 2^64 bytes, but has 3 |
| 2072 | { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2073 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2074 | // Text string should have 2^64 bytes, but has 3 |
| 2075 | { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2076 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 2077 | #else |
| 2078 | // Byte string should have 2^32-15 bytes, but has one |
| 2079 | { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2080 | // Byte string should have 2^32-15 bytes, but has one |
| 2081 | { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2082 | // Byte string should have 2^16 bytes, but has 3 |
| 2083 | { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2084 | // Text string should have 2^64 bytes, but has 3 |
| 2085 | { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2086 | #endif |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2087 | |
| 2088 | // Use of unassigned additional information values |
| 2089 | // Major type positive integer with reserved value 28 |
| 2090 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2091 | // Major type positive integer with reserved value 29 |
| 2092 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2093 | // Major type positive integer with reserved value 30 |
| 2094 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2095 | // Major type negative integer with reserved value 28 |
| 2096 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2097 | // Major type negative integer with reserved value 29 |
| 2098 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2099 | // Major type negative integer with reserved value 30 |
| 2100 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2101 | // Major type byte string with reserved value 28 length |
| 2102 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2103 | // Major type byte string with reserved value 29 length |
| 2104 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2105 | // Major type byte string with reserved value 30 length |
| 2106 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2107 | // Major type text string with reserved value 28 length |
| 2108 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2109 | // Major type text string with reserved value 29 length |
| 2110 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2111 | // Major type text string with reserved value 30 length |
| 2112 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2113 | // Major type array with reserved value 28 length |
| 2114 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2115 | // Major type array with reserved value 29 length |
| 2116 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2117 | // Major type array with reserved value 30 length |
| 2118 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2119 | // Major type map with reserved value 28 length |
| 2120 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2121 | // Major type map with reserved value 29 length |
| 2122 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2123 | // Major type map with reserved value 30 length |
| 2124 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2125 | // Major type tag with reserved value 28 length |
| 2126 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2127 | // Major type tag with reserved value 29 length |
| 2128 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2129 | // Major type tag with reserved value 30 length |
| 2130 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2131 | // Major type simple with reserved value 28 length |
| 2132 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2133 | // Major type simple with reserved value 29 length |
| 2134 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2135 | // Major type simple with reserved value 30 length |
| 2136 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2137 | |
| 2138 | |
| 2139 | // Maps must have an even number of data items (key & value) |
| 2140 | // Map with 1 item when it should have 2 |
| 2141 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2142 | // Map with 3 item when it should have 4 |
| 2143 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2144 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2145 | // Map with 1 item when it should have 2 |
| 2146 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2147 | // Map with 3 item when it should have 4 |
| 2148 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2149 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2150 | |
| 2151 | |
| 2152 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2153 | // Text-based date, with an integer |
| 2154 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2155 | // Epoch date, with an byte string |
| 2156 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2157 | // tagged as both epoch and string dates |
| 2158 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2159 | // big num tagged an int, not a byte string |
| 2160 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2161 | }; |
| 2162 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2163 | int32_t DecodeFailureTests() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2164 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2165 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2166 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2167 | nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2168 | if(nResult) { |
| 2169 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2170 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2171 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2172 | // Corrupt the UsefulInputBuf and see that |
| 2173 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2174 | QCBORDecodeContext DCtx; |
| 2175 | QCBORItem Item; |
| 2176 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2177 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2178 | QCBORDecode_Init(&DCtx, |
| 2179 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2180 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2181 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2182 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2183 | return (int32_t)uQCBORError; |
| 2184 | } |
| 2185 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 2186 | // This wasn't supposed to happen |
| 2187 | return -1; |
| 2188 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2189 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2190 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2191 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2192 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2193 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2194 | // Did not get back the error expected |
| 2195 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2196 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2197 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2198 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2199 | /* |
| 2200 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 2201 | tests here can be performed to see that the max length |
| 2202 | error check works correctly. See DecodeBytes(). If the max |
| 2203 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2204 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2205 | This test will automatocally adapt the all CPU sizes |
| 2206 | through the use of SIZE_MAX. |
| 2207 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2208 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2209 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2210 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2211 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2212 | // This makes a CBOR head with a text string that is very long |
| 2213 | // but doesn't fill in the bytes of the text string as that is |
| 2214 | // not needed to test this part of QCBOR. |
| 2215 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 2216 | |
| 2217 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 2218 | |
| 2219 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2220 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2221 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2222 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2223 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2227 | /* Try all 256 values of the byte at nLen including recursing for |
| 2228 | each of the values to try values at nLen+1 ... up to nLenMax |
| 2229 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2230 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2231 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2232 | if(nLen >= nLenMax) { |
| 2233 | return; |
| 2234 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2235 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2236 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 2237 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2238 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2239 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2240 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2241 | // Get ready to parse |
| 2242 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2243 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2244 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2245 | // Parse by getting the next item until an error occurs |
| 2246 | // Just about every possible decoder error can occur here |
| 2247 | // The goal of this test is not to check for the correct |
| 2248 | // error since that is not really possible. It is to |
| 2249 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2250 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2251 | QCBORItem Item; |
| 2252 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2253 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2254 | break; |
| 2255 | } |
| 2256 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2257 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2258 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2263 | int32_t ComprehensiveInputTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2264 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2265 | // Size 2 tests 64K inputs and runs quickly |
| 2266 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2267 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2268 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2269 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2270 | return 0; |
| 2271 | } |
| 2272 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2273 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2274 | int32_t BigComprehensiveInputTest() |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2275 | { |
| 2276 | // size 3 tests 16 million inputs and runs OK |
| 2277 | // in seconds on fast machines. Size 4 takes |
| 2278 | // 10+ minutes and 5 half a day on fast |
| 2279 | // machines. This test is kept separate from |
| 2280 | // the others so as to no slow down the use |
| 2281 | // of them as a very frequent regression. |
| 2282 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2283 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2284 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2285 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2286 | return 0; |
| 2287 | } |
| 2288 | |
| 2289 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2290 | static const uint8_t spDateTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2291 | 0xc0, // tag for string date |
| 2292 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2293 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2294 | 0xc0, // tag for string date |
| 2295 | 0x00, // Wrong type for a string date |
| 2296 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2297 | 0xc1, // tag for epoch date |
| 2298 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2299 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2300 | 0xc1, |
| 2301 | 0x62, 'h', 'i', // wrong type tagged |
| 2302 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2303 | // CBOR_TAG_ENC_AS_B64 |
| 2304 | 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2305 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2306 | |
| 2307 | 0xc1, // tag for epoch date |
| 2308 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2309 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2310 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2311 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2312 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2313 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2314 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2315 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2316 | 0xc1, // tag for epoch date |
| 2317 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2318 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2319 | |
| 2320 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2321 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2322 | |
| 2323 | 0xc1, // tag for epoch date |
| 2324 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 2325 | |
| 2326 | 0xc1, |
| 2327 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 2328 | |
| 2329 | 0xc1, // tag for epoch date |
| 2330 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2331 | }; |
| 2332 | |
| 2333 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2334 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2335 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2336 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 2337 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2338 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2339 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2340 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2341 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2342 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2343 | return diff > 0.0000001; |
| 2344 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2345 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2346 | |
| 2347 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2348 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2349 | int32_t DateParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2350 | { |
| 2351 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2352 | QCBORItem Item; |
| 2353 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2354 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2355 | QCBORDecode_Init(&DCtx, |
| 2356 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2357 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2358 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2359 | // String date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2360 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2361 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2362 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2363 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2364 | UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2365 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2366 | } |
| 2367 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2368 | // Wrong type for a string date |
| 2369 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2370 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2371 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2375 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2376 | return -4; |
| 2377 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2378 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2379 | Item.val.epochDate.nSeconds != 1400000000 || |
| 2380 | Item.val.epochDate.fSecondsFraction != 0 ) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2381 | return -5; |
| 2382 | } |
| 2383 | |
| 2384 | // Wrong type for an epoch date |
| 2385 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 2386 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2387 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2388 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2389 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 2390 | // The date is decoded and the two tags are returned. This is to |
| 2391 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2392 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2393 | return -7; |
| 2394 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2395 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2396 | Item.val.epochDate.nSeconds != 1400000001 || |
| 2397 | Item.val.epochDate.fSecondsFraction != 0 || |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2398 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2399 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2400 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2401 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2402 | // Epoch date that is too large for our representation |
| 2403 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2404 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2405 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2406 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2407 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2408 | // Epoch date in float format with fractional seconds |
| 2409 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2410 | return -10; |
| 2411 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2412 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2413 | Item.val.epochDate.nSeconds != 1 || |
| 2414 | CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2415 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2416 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2417 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2418 | // Epoch date float that is too large for our representation |
| 2419 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2420 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2421 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2422 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2423 | // Epoch date double that is just slightly too large |
| 2424 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2425 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | // Largest double epoch date supported |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2429 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS || |
| 2430 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2431 | Item.val.epochDate.nSeconds != 9223372036854773760 || |
| 2432 | Item.val.epochDate.nSeconds == 0) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2433 | return -14; |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2434 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2435 | |
| 2436 | // Nan |
| 2437 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2438 | return -15; |
| 2439 | } |
| 2440 | |
| 2441 | // +Inifinity double-precision |
| 2442 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2443 | return -16; |
| 2444 | } |
| 2445 | |
| 2446 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2447 | // -Inifinity half-precision |
| 2448 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2449 | return -17; |
| 2450 | } |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2451 | #else |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2452 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) { |
| 2453 | return -18; |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2454 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2455 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2456 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2457 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2458 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2459 | return -19; |
| 2460 | } |
| 2461 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2462 | return -20; |
| 2463 | } |
| 2464 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2465 | return -21; |
| 2466 | } |
| 2467 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2468 | return -22; |
| 2469 | } |
| 2470 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2471 | return -23; |
| 2472 | } |
| 2473 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2474 | return -24; |
| 2475 | } |
| 2476 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2477 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2478 | return -25; |
| 2479 | } |
| 2480 | #else |
| 2481 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) { |
| 2482 | return -26; |
| 2483 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2484 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2485 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2486 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2487 | |
| 2488 | return 0; |
| 2489 | } |
| 2490 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2491 | /* |
| 2492 | Test cases covered here. Some items cover more than one of these. |
| 2493 | positive integer (zero counts as a positive integer) |
| 2494 | negative integer |
| 2495 | half-precision float |
| 2496 | single-precision float |
| 2497 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2498 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2499 | float Overflow error |
| 2500 | Wrong type error for epoch |
| 2501 | Wrong type error for date string |
| 2502 | float disabled error |
| 2503 | half-precision disabled error |
| 2504 | -Infinity |
| 2505 | Slightly too large integer |
| 2506 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2507 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2508 | Get epoch by int |
| 2509 | Get string by int |
| 2510 | Get epoch by string |
| 2511 | Get string by string |
| 2512 | Fail to get epoch by wrong int label |
| 2513 | Fail to get string by wrong string label |
| 2514 | Fail to get epoch by string because it is invalid |
| 2515 | Fail to get epoch by int because it is invalid |
| 2516 | |
| 2517 | Untagged values |
| 2518 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2519 | static const uint8_t spSpiffyDateTestInput[] = { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2520 | 0x86, |
| 2521 | |
| 2522 | 0xc1, |
| 2523 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 2524 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2525 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2526 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 2527 | |
| 2528 | 0xc1, // tag for epoch date |
| 2529 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 2530 | |
| 2531 | 0xc1, // tag for epoch date |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2532 | 0x80, // Erroneous empty array as content for date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2533 | |
| 2534 | 0xc0, // tag for string date |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2535 | 0xa0, // Erroneous empty map as content for date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2536 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2537 | 0xa9, // Open a map for tests involving labels. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2538 | |
| 2539 | 0x00, |
| 2540 | 0xc0, // tag for string date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2541 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2542 | |
| 2543 | 0x01, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2544 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2545 | 0xc1, // tag for epoch date |
| 2546 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2547 | |
| 2548 | // Untagged integer 0 |
| 2549 | 0x08, |
| 2550 | 0x00, |
| 2551 | |
| 2552 | // Utagged date string with string label y |
| 2553 | 0x61, 0x79, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2554 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2555 | |
| 2556 | // Untagged -1000 with label z |
| 2557 | 0x61, 0x7a, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2558 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2559 | 0x39, 0x03, 0xe7, |
| 2560 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2561 | 0x07, |
| 2562 | 0xc1, // tag for epoch date |
| 2563 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2564 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2565 | 0x05, |
| 2566 | 0xc1, |
| 2567 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 2568 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2569 | // Untagged single-precision float with value 3.14 with string label x |
| 2570 | 0x61, 0x78, |
| 2571 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 2572 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2573 | // Untagged half-precision float with value -2 |
| 2574 | 0x09, |
| 2575 | 0xF9, 0xC0, 0x00, |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2576 | }; |
| 2577 | |
| 2578 | int32_t SpiffyDateDecodeTest() |
| 2579 | { |
| 2580 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2581 | QCBORError uError; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2582 | int64_t nEpochDate2, nEpochDate3, nEpochDate5, |
| 2583 | nEpochDate4, nEpochDate6, nEpochDateFail, |
| 2584 | nEpochDate1400000000; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2585 | UsefulBufC StringDate1, StringDate2; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2586 | uint64_t uTag1, uTag2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2587 | |
| 2588 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2589 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2590 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2591 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2592 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2593 | // Too-negative float, -9.2233720368547748E+18 |
| 2594 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2595 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2596 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2597 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 2598 | return 1111; |
| 2599 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2600 | #else |
| 2601 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2602 | return 1112; |
| 2603 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2604 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2605 | |
| 2606 | // Too-large integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2607 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2608 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2609 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2610 | return 1; |
| 2611 | } |
| 2612 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2613 | // Half-precision minus infinity |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2614 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2615 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2616 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2617 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2618 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW; |
| 2619 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2620 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED; |
| 2621 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2622 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 2623 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED; |
| 2624 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 2625 | if(uError != uExpectedforHalfMinusInfinity) { |
| 2626 | return 2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2629 | // Bad content for epoch date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2630 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2631 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2632 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
| 2633 | return 3; |
| 2634 | } |
| 2635 | |
| 2636 | // Bad content for string date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2637 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2638 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2639 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
| 2640 | return 4; |
| 2641 | } |
| 2642 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2643 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2644 | |
| 2645 | // Get largest negative double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2646 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2647 | 5, |
| 2648 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 2649 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2650 | &nEpochDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2651 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2652 | if(nEpochDate2 != -9223372036854773760LL) { |
| 2653 | return 101; |
| 2654 | } |
| 2655 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2656 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2657 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2658 | return 102; |
| 2659 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2660 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2661 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2662 | // Get largest double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2663 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2664 | &nEpochDate2); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2665 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2666 | if(nEpochDate2 != 9223372036854773760ULL) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2667 | return 111; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2668 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2669 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2670 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2671 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2672 | return 112; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2673 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2674 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2675 | |
| 2676 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2677 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2678 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2679 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2680 | if(nEpochDate5 != 3) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2681 | return 103; |
| 2682 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2683 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2684 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2685 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2686 | return 104; |
| 2687 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2688 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2689 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2690 | // A half-precision date with value -2 FFF |
| 2691 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2692 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2693 | #if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT) |
| 2694 | if(nEpochDate4 != -2) { |
| 2695 | return 105; |
| 2696 | } |
| 2697 | #else |
| 2698 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2699 | if(uError == QCBOR_SUCCESS) { |
| 2700 | return 106; |
| 2701 | } |
| 2702 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2703 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2704 | |
| 2705 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2706 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 2707 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2708 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2709 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2710 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2711 | return 107; |
| 2712 | } |
| 2713 | |
| 2714 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2715 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2716 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2717 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2718 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2719 | return 108; |
| 2720 | } |
| 2721 | |
| 2722 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2723 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 2724 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2725 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2726 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2727 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2728 | return 109; |
| 2729 | } |
| 2730 | |
| 2731 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2732 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2733 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2734 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2735 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2736 | return 110; |
| 2737 | } |
| 2738 | |
| 2739 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2740 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2741 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2742 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2743 | 1, |
| 2744 | QCBOR_TAG_REQUIREMENT_TAG | |
| 2745 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2746 | &nEpochDate1400000000); |
| 2747 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2748 | // Tagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2749 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2750 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2751 | // Untagged integer 0 |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2752 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2753 | &nEpochDate3); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2754 | // Untagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2755 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2756 | &StringDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2757 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2758 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 2759 | "z", |
| 2760 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 2761 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2762 | &nEpochDate6); |
| 2763 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2764 | |
| 2765 | QCBORDecode_ExitMap(&DC); |
| 2766 | QCBORDecode_ExitArray(&DC); |
| 2767 | uError = QCBORDecode_Finish(&DC); |
| 2768 | if(uError) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2769 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2772 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2773 | return 200; |
| 2774 | } |
| 2775 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2776 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2777 | return 201; |
| 2778 | } |
| 2779 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2780 | if(nEpochDate3 != 0) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2781 | return 202; |
| 2782 | } |
| 2783 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2784 | if(nEpochDate6 != -1000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2785 | return 203; |
| 2786 | } |
| 2787 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2788 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2789 | return 204; |
| 2790 | } |
| 2791 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2792 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2793 | return 205; |
| 2794 | } |
| 2795 | |
| 2796 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 2797 | return 206; |
| 2798 | } |
| 2799 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2800 | return 0; |
| 2801 | } |
| 2802 | |
| 2803 | |
| 2804 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2805 | // Input for one of the tagging tests |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2806 | static const uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2807 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2808 | 0x81, // Array of one |
| 2809 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 2810 | 0x82, // Array of two that is the faction 1/3 |
| 2811 | 0x01, |
| 2812 | 0x03, |
| 2813 | |
| 2814 | /* |
| 2815 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 2816 | */ |
| 2817 | 0xd8, 0xe1, |
| 2818 | 0xd8, 0xe2, |
| 2819 | 0xd8, 0xe3, |
| 2820 | 0xd8, 0xe4, |
| 2821 | 0xd8, 0xe5, |
| 2822 | 0x80, |
| 2823 | |
| 2824 | /* tag 10489608748473423768( |
| 2825 | 2442302356( |
| 2826 | 21590( |
| 2827 | 240( |
| 2828 | [])))) |
| 2829 | */ |
| 2830 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2831 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 2832 | 0xd9, 0x54, 0x56, |
| 2833 | 0xd8, 0xf0, |
| 2834 | 0x80, |
| 2835 | |
| 2836 | /* tag 21590( |
| 2837 | 10489608748473423768( |
| 2838 | 2442302357( |
| 2839 | 65534( |
| 2840 | [])))) |
| 2841 | */ |
| 2842 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 2843 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2844 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 2845 | 0xd9, 0xff, 0xfe, |
| 2846 | 0x80, |
| 2847 | |
| 2848 | /* Make sure to blow past the limit of tags that must be mapped. |
| 2849 | works in conjuntion with entries above. |
| 2850 | 269488144(269488145(269488146(269488147([])))) |
| 2851 | */ |
| 2852 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 2853 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 2854 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 2855 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 2856 | 0x80, |
| 2857 | |
| 2858 | /* An invalid decimal fraction with an additional tag */ |
| 2859 | 0xd9, 0xff, 0xfa, |
| 2860 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 2861 | 0x00, // the integer 0; should be a byte string |
| 2862 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2863 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2864 | /* |
| 2865 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2866 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2867 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2868 | static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2869 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2870 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2871 | /* |
| 2872 | DB 9192939495969798 # tag(10489608748473423768) |
| 2873 | D8 88 # tag(136) |
| 2874 | C6 # tag(6) |
| 2875 | C7 # tag(7) |
| 2876 | 80 # array(0) |
| 2877 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2878 | static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2879 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2880 | |
| 2881 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2882 | 55799(55799(55799({ |
| 2883 | 6(7(-23)): 5859837686836516696(7({ |
| 2884 | 7(-20): 11({ |
| 2885 | 17(-18): 17(17(17("Organization"))), |
| 2886 | 9(-17): 773("SSG"), |
| 2887 | -15: 16(17(6(7("Confusion")))), |
| 2888 | 17(-16): 17("San Diego"), |
| 2889 | 17(-14): 17("US") |
| 2890 | }), |
| 2891 | 23(-19): 19({ |
| 2892 | -11: 9({ |
| 2893 | -9: -7 |
| 2894 | }), |
| 2895 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 2896 | }) |
| 2897 | })), |
| 2898 | 16(-22): 23({ |
| 2899 | 11(8(7(-5))): 8(-3) |
| 2900 | }) |
| 2901 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2902 | */ |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2903 | static const uint8_t spCSRWithTags[] = { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2904 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 2905 | 0xc6, 0xc7, 0x36, |
| 2906 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 2907 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 2908 | 0xcb, 0xa5, |
| 2909 | 0xd1, 0x31, |
| 2910 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 2911 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 2912 | 0xc9, 0x30, |
| 2913 | 0xd9, 0x03, 0x05, 0x63, |
| 2914 | 0x53, 0x53, 0x47, |
| 2915 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2916 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 2917 | 0x69, |
| 2918 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2919 | 0xd1, 0x2f, |
| 2920 | 0xd1, 0x69, |
| 2921 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 2922 | 0xd1, 0x2d, |
| 2923 | 0xd1, 0x62, |
| 2924 | 0x55, 0x53, |
| 2925 | 0xd7, 0x32, |
| 2926 | 0xd3, 0xa2, |
| 2927 | 0x2a, |
| 2928 | 0xc9, 0xa1, |
| 2929 | 0x28, |
| 2930 | 0x26, |
| 2931 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 2932 | 0xcc, 0x4a, |
| 2933 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 2934 | 0xd0, 0x35, |
| 2935 | 0xd7, 0xa1, |
| 2936 | 0xcb, 0xc8, 0xc7, 0x24, |
| 2937 | 0xc8, 0x22}; |
| 2938 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2939 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 2940 | static const uint8_t spSpiffyTagInput[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2941 | 0x85, // Open array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2942 | |
| 2943 | 0xc0, // tag for string date |
| 2944 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2945 | |
| 2946 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2947 | |
| 2948 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 2949 | |
| 2950 | 0xd8, 0x23, // tag for regex |
| 2951 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2952 | |
| 2953 | 0xc0, // tag for string date |
| 2954 | 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] | 2955 | }; |
| 2956 | |
| 2957 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2958 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2959 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2960 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2961 | int32_t OptTagParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2962 | { |
| 2963 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2964 | QCBORItem Item; |
| 2965 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2966 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2967 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2968 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2969 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2970 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2971 | /* |
| 2972 | This test matches the magic number tag and the fraction tag |
| 2973 | 55799([...]) |
| 2974 | */ |
| 2975 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2976 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2977 | return -2; |
| 2978 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2979 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2980 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 2981 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2982 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2983 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2984 | /* |
| 2985 | 4([1,3]) |
| 2986 | */ |
| 2987 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2988 | #ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 2989 | if(uError != QCBOR_SUCCESS || |
| 2990 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2991 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 2992 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 2993 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 2994 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 2995 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 2996 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 2997 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2998 | return -4; |
| 2999 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3000 | // consume the items in the array |
| 3001 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3002 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3003 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3004 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3005 | if(uError != QCBOR_SUCCESS || |
| 3006 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3007 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 3008 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 3009 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 3010 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 3011 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 3012 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3013 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 3014 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3015 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3016 | /* |
| 3017 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 3018 | */ |
| 3019 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3020 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3021 | return -6; |
| 3022 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3023 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 3024 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 3025 | return -106; |
| 3026 | } |
| 3027 | |
| 3028 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3029 | /* tag 10489608748473423768( |
| 3030 | 2442302356( |
| 3031 | 21590( |
| 3032 | 240( |
| 3033 | [])))) |
| 3034 | */ |
| 3035 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3036 | if(uError != QCBOR_SUCCESS || |
| 3037 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3038 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 3039 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 3040 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 3041 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3042 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /* tag 21590( |
| 3046 | 10489608748473423768( |
| 3047 | 2442302357( |
| 3048 | 21591( |
| 3049 | [])))) |
| 3050 | */ |
| 3051 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3052 | if(uError != QCBOR_SUCCESS || |
| 3053 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3054 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 3055 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 3056 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 3057 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 3058 | return -8; |
| 3059 | } |
| 3060 | |
| 3061 | /* Make sure to blow past the limit of tags that must be mapped. |
| 3062 | works in conjuntion with entries above. |
| 3063 | 269488144(269488145(269488146(269488147([])))) |
| 3064 | */ |
| 3065 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3066 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3067 | return -9; |
| 3068 | } |
| 3069 | |
| 3070 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3071 | if(uError == QCBOR_SUCCESS) { |
| 3072 | return -10; |
| 3073 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3074 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3075 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3076 | // 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] | 3077 | // tage and then matches it. Caller-config lists are no longer |
| 3078 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3079 | QCBORDecode_Init(&DCtx, |
| 3080 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3081 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3082 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 3083 | const QCBORTagListIn TL = {2, puList}; |
| 3084 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3085 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3086 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3087 | return -8; |
| 3088 | } |
| 3089 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3090 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 3091 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 3092 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 3093 | Item.val.uCount != 0) { |
| 3094 | return -9; |
| 3095 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3096 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3097 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3098 | // 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] | 3099 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3100 | const uint64_t puLongList[17] = {1,2,1}; |
| 3101 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3102 | QCBORDecode_Init(&DCtx, |
| 3103 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3104 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3105 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 3106 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3107 | return -11; |
| 3108 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3109 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3110 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3111 | QCBORTagListOut Out = {0, 4, puTags}; |
| 3112 | |
| 3113 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3114 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3115 | QCBORDecode_Init(&DCtx, |
| 3116 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3117 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3118 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3119 | return -12; |
| 3120 | } |
| 3121 | if(puTags[0] != 0x9192939495969798 || |
| 3122 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3123 | puTags[2] != 0x06 || |
| 3124 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3125 | return -13; |
| 3126 | } |
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 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3129 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3130 | QCBORDecode_Init(&DCtx, |
| 3131 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3132 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3133 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 3134 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3135 | return -14; |
| 3136 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3137 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3138 | |
| 3139 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3140 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3141 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 3142 | // It is a bit of a messy test and maybe could be improved, but |
| 3143 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3144 | QCBORDecode_Init(&DCtx, |
| 3145 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3146 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3147 | int n = CheckCSRMaps(&DCtx); |
| 3148 | if(n) { |
| 3149 | return n-2000; |
| 3150 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3151 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3152 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3153 | QCBORDecode_Init(&DCtx, |
| 3154 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3155 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3156 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3157 | /* With the spiffy decode revision, this tag list is not used. |
| 3158 | It doesn't matter if a tag is in this list or not so some |
| 3159 | tests that couldn't process a tag because it isn't in this list |
| 3160 | now can process these unlisted tags. The tests have been |
| 3161 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3162 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 3163 | const QCBORTagListIn TagList = {3, puTagList}; |
| 3164 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3165 | |
| 3166 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3167 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3168 | return -100; |
| 3169 | } |
| 3170 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3171 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3172 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 3173 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 3174 | Item.val.uCount != 2 || |
| 3175 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 3176 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 3177 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 3178 | Out.uNumUsed != 3) { |
| 3179 | return -101; |
| 3180 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3181 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3182 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3183 | return -102; |
| 3184 | } |
| 3185 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3186 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3187 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3188 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3189 | Item.val.uCount != 2 || |
| 3190 | puTags[0] != 5859837686836516696 || |
| 3191 | puTags[1] != 7 || |
| 3192 | Out.uNumUsed != 2) { |
| 3193 | return -103; |
| 3194 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3195 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3196 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3197 | return -104; |
| 3198 | } |
| 3199 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3200 | Item.val.uCount != 5 || |
| 3201 | puTags[0] != 0x0b || |
| 3202 | Out.uNumUsed != 1) { |
| 3203 | return -105; |
| 3204 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3205 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3206 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3207 | return -106; |
| 3208 | } |
| 3209 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3210 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 3211 | Item.val.string.len != 12 || |
| 3212 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 3213 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 3214 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 3215 | Out.uNumUsed != 3) { |
| 3216 | return -105; |
| 3217 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3218 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3219 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3220 | return -107; |
| 3221 | } |
| 3222 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3223 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 3224 | Item.val.string.len != 3 || |
| 3225 | puTags[0] != 773 || |
| 3226 | Out.uNumUsed != 1) { |
| 3227 | return -108; |
| 3228 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3229 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3230 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3231 | return -109; |
| 3232 | } |
| 3233 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3234 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3235 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3236 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3237 | puTags[3] != 7 || |
| 3238 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3239 | return -110; |
| 3240 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3241 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3242 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3243 | return -111; |
| 3244 | } |
| 3245 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3246 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3247 | Item.val.string.len != 9 || |
| 3248 | puTags[0] != 17 || |
| 3249 | Out.uNumUsed != 1) { |
| 3250 | return -112; |
| 3251 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3252 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3253 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3254 | return -111; |
| 3255 | } |
| 3256 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3257 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3258 | Item.val.string.len != 2 || |
| 3259 | puTags[0] != 17 || |
| 3260 | Out.uNumUsed != 1) { |
| 3261 | return -112; |
| 3262 | } |
| 3263 | |
| 3264 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3265 | return -113; |
| 3266 | } |
| 3267 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3268 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3269 | Item.val.uCount != 2 || |
| 3270 | puTags[0] != 19 || |
| 3271 | Out.uNumUsed != 1) { |
| 3272 | return -114; |
| 3273 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3274 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3275 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3276 | return -115; |
| 3277 | } |
| 3278 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3279 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3280 | Item.val.uCount != 1 || |
| 3281 | puTags[0] != 9 || |
| 3282 | Out.uNumUsed != 1) { |
| 3283 | return -116; |
| 3284 | } |
| 3285 | |
| 3286 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3287 | return -116; |
| 3288 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3289 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3290 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3291 | Out.uNumUsed != 0) { |
| 3292 | return -117; |
| 3293 | } |
| 3294 | |
| 3295 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3296 | return -118; |
| 3297 | } |
| 3298 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 3299 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3300 | puTags[0] != 12 || |
| 3301 | Out.uNumUsed != 1) { |
| 3302 | return -119; |
| 3303 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3304 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3305 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3306 | return -120; |
| 3307 | } |
| 3308 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3309 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 3310 | Item.val.uCount != 1 || |
| 3311 | puTags[0] != 0x17 || |
| 3312 | Out.uNumUsed != 1) { |
| 3313 | return -121; |
| 3314 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3315 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3316 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3317 | return -122; |
| 3318 | } |
| 3319 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3320 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3321 | Item.val.int64 != -3 || |
| 3322 | puTags[0] != 8 || |
| 3323 | Out.uNumUsed != 1) { |
| 3324 | return -123; |
| 3325 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3326 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3327 | if(QCBORDecode_Finish(&DCtx)) { |
| 3328 | return -124; |
| 3329 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3330 | |
| 3331 | UsefulBufC DateString; |
| 3332 | QCBORDecode_Init(&DCtx, |
| 3333 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3334 | QCBOR_DECODE_MODE_NORMAL); |
| 3335 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3336 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3337 | // tagged date string |
| 3338 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3339 | // untagged date string |
| 3340 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3341 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3342 | return 100; |
| 3343 | } |
| 3344 | // untagged byte string |
| 3345 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3346 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3347 | return 101; |
| 3348 | } |
| 3349 | // tagged regex |
| 3350 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3351 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3352 | return 102; |
| 3353 | } |
| 3354 | // tagged date string with a byte string |
| 3355 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3356 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3357 | return 103; |
| 3358 | } |
| 3359 | QCBORDecode_ExitArray(&DCtx); |
| 3360 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3361 | return 104; |
| 3362 | } |
| 3363 | |
| 3364 | |
| 3365 | QCBORDecode_Init(&DCtx, |
| 3366 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3367 | QCBOR_DECODE_MODE_NORMAL); |
| 3368 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3369 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3370 | // tagged date string |
| 3371 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3372 | // untagged date string |
| 3373 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3374 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3375 | return 200; |
| 3376 | } |
| 3377 | // untagged byte string |
| 3378 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3379 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3380 | return 201; |
| 3381 | } |
| 3382 | // tagged regex |
| 3383 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3384 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3385 | return 202; |
| 3386 | } |
| 3387 | // tagged date string with a byte string |
| 3388 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3389 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3390 | return 203; |
| 3391 | } |
| 3392 | QCBORDecode_ExitArray(&DCtx); |
| 3393 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3394 | return 204; |
| 3395 | } |
| 3396 | |
| 3397 | QCBORDecode_Init(&DCtx, |
| 3398 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3399 | QCBOR_DECODE_MODE_NORMAL); |
| 3400 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3401 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3402 | // tagged date string |
| 3403 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3404 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3405 | return 300; |
| 3406 | } |
| 3407 | // untagged date string |
| 3408 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3409 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3410 | return 301; |
| 3411 | } |
| 3412 | // untagged byte string |
| 3413 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3414 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3415 | return 302; |
| 3416 | } |
| 3417 | // tagged regex |
| 3418 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3419 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3420 | return 303; |
| 3421 | } |
| 3422 | // tagged date string with a byte string |
| 3423 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3424 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3425 | return 304; |
| 3426 | } |
| 3427 | QCBORDecode_ExitArray(&DCtx); |
| 3428 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3429 | return 305; |
| 3430 | } |
| 3431 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | |
| 3436 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3437 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3438 | static const uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3439 | 0x83, |
| 3440 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3441 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3442 | 0xA4, |
| 3443 | 0x63, 0x42, 0x4E, 0x2B, |
| 3444 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3445 | 0x18, 0x40, |
| 3446 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3447 | 0x63, 0x42, 0x4E, 0x2D, |
| 3448 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3449 | 0x38, 0x3F, |
| 3450 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 3451 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3452 | /* The expected big num */ |
| 3453 | static const uint8_t spBigNum[] = { |
| 3454 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3455 | 0x00}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3456 | |
| 3457 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3458 | int32_t BignumParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3459 | { |
| 3460 | QCBORDecodeContext DCtx; |
| 3461 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3462 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3463 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3464 | QCBORDecode_Init(&DCtx, |
| 3465 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 3466 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3467 | |
| 3468 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3469 | // |
| 3470 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 3471 | return -1; |
| 3472 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3473 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3474 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3475 | |
| 3476 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3477 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3478 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3479 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3480 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3481 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | // |
| 3485 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3486 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3487 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3488 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3489 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3490 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3491 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3492 | // |
| 3493 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3494 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3495 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3496 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3497 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3498 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3499 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3500 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3501 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3502 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3503 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3504 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3508 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3509 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3510 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3511 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3512 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3513 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3514 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3515 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3516 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3517 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3518 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3519 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3520 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3521 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3522 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3523 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3524 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3525 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3526 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3527 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3528 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3529 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3530 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3531 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3532 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3533 | return 0; |
| 3534 | } |
| 3535 | |
| 3536 | |
| 3537 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3538 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3539 | uint8_t uDataType, |
| 3540 | uint8_t uNestingLevel, |
| 3541 | uint8_t uNextNest, |
| 3542 | int64_t nLabel, |
| 3543 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3544 | { |
| 3545 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3546 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3547 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3548 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 3549 | if(Item.uDataType != uDataType) return -1; |
| 3550 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3551 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 3552 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 3553 | return -1; |
| 3554 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3555 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3556 | if(Item.label.int64 != nLabel) return -1; |
| 3557 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3558 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3559 | } |
| 3560 | } |
| 3561 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3562 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3563 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3564 | if(pItem) { |
| 3565 | *pItem = Item; |
| 3566 | } |
| 3567 | return 0; |
| 3568 | } |
| 3569 | |
| 3570 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3571 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3572 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3573 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3574 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3575 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3576 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3577 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3578 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3579 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3580 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 3581 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 3582 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 3583 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 3584 | 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] | 3585 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3586 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 3587 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3588 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3589 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 3590 | 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] | 3591 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3592 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 3593 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3594 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3595 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3596 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3597 | return 0; |
| 3598 | } |
| 3599 | |
| 3600 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3601 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3602 | { |
| 3603 | -23: { |
| 3604 | -20: { |
| 3605 | -18: "Organization", |
| 3606 | -17: "SSG", |
| 3607 | -15: "Confusion", |
| 3608 | -16: "San Diego", |
| 3609 | -14: "US" |
| 3610 | }, |
| 3611 | -19: { |
| 3612 | -11: { |
| 3613 | -9: -7 |
| 3614 | }, |
| 3615 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 3616 | } |
| 3617 | }, |
| 3618 | -22: { |
| 3619 | -5: -3 |
| 3620 | } |
| 3621 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3622 | */ |
| 3623 | static const uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3624 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 3625 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3626 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3627 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3628 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3629 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3630 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 3631 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 3632 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 3633 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 3634 | // Same map as above, but using indefinite lengths |
| 3635 | static const uint8_t spCSRInputIndefLen[] = { |
| 3636 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 3637 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3638 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3639 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3640 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3641 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3642 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 3643 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 3644 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 3645 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
| 3646 | |
| 3647 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3648 | int32_t NestedMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3649 | { |
| 3650 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3651 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3652 | QCBORDecode_Init(&DCtx, |
| 3653 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3654 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3655 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3656 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3657 | } |
| 3658 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3659 | |
| 3660 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3661 | int32_t StringDecoderModeFailTest() |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3662 | { |
| 3663 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3664 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3665 | QCBORDecode_Init(&DCtx, |
| 3666 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3667 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3668 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3669 | QCBORItem Item; |
| 3670 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3671 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3672 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3673 | return -1; |
| 3674 | } |
| 3675 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3676 | return -2; |
| 3677 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3678 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3679 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3680 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 3681 | return -3; |
| 3682 | } |
| 3683 | |
| 3684 | return 0; |
| 3685 | } |
| 3686 | |
| 3687 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3688 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3689 | int32_t NestedMapTestIndefLen() |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3690 | { |
| 3691 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3692 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3693 | QCBORDecode_Init(&DCtx, |
| 3694 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 3695 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3696 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3697 | return CheckCSRMaps(&DCtx); |
| 3698 | } |
| 3699 | |
| 3700 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3701 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3702 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 3703 | { |
| 3704 | UsefulOutBuf UOB; |
| 3705 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3706 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3707 | int i; |
| 3708 | for(i = 0; i < n; i++) { |
| 3709 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 3710 | } |
| 3711 | |
| 3712 | for(i = 0; i < n; i++) { |
| 3713 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3714 | } |
| 3715 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3716 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3717 | |
| 3718 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3719 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3720 | { |
| 3721 | QCBORDecodeContext DC; |
| 3722 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3723 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3724 | int j; |
| 3725 | for(j = 0; j < nNestLevel; j++) { |
| 3726 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3727 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3728 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 3729 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3730 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3731 | return -4; |
| 3732 | } else { |
| 3733 | return 0; // Decoding doesn't recover after an error |
| 3734 | } |
| 3735 | } else { |
| 3736 | // Should be no error |
| 3737 | if(nReturn) { |
| 3738 | return -9; // Should not have got an error |
| 3739 | } |
| 3740 | } |
| 3741 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3742 | return -7; |
| 3743 | } |
| 3744 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3745 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3746 | if(nReturn) { |
| 3747 | return -3; |
| 3748 | } |
| 3749 | return 0; |
| 3750 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3751 | |
| 3752 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3753 | int32_t IndefiniteLengthNestTest() |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3754 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3755 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3756 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3757 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3758 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3759 | int nReturn = parse_indeflen_nested(Nested, i); |
| 3760 | if(nReturn) { |
| 3761 | return nReturn; |
| 3762 | } |
| 3763 | } |
| 3764 | return 0; |
| 3765 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3766 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3767 | // [1, [2, 3]] |
| 3768 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 3769 | // No closing break |
| 3770 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 3771 | // Not enough closing breaks |
| 3772 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 3773 | // Too many closing breaks |
| 3774 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 3775 | // Unclosed indeflen inside def len |
| 3776 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 3777 | // confused tag |
| 3778 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3779 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3780 | int32_t IndefiniteLengthArrayMapTest() |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3781 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3782 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3783 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3784 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3785 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3786 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3787 | QCBORDecodeContext DC; |
| 3788 | QCBORItem Item; |
| 3789 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3790 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3791 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3792 | |
| 3793 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3794 | Item.uNestingLevel != 0 || |
| 3795 | Item.uNextNestLevel != 1) { |
| 3796 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3797 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3798 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3799 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3800 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 3801 | Item.uNestingLevel != 1 || |
| 3802 | Item.uNextNestLevel != 1) { |
| 3803 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3804 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3805 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3806 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3807 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3808 | Item.uNestingLevel != 1 || |
| 3809 | Item.uNextNestLevel != 2) { |
| 3810 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3811 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3812 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3813 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3814 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3815 | Item.uNestingLevel != 2 || |
| 3816 | Item.uNextNestLevel != 2) { |
| 3817 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3818 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3819 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3820 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3821 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3822 | Item.uNestingLevel != 2 || |
| 3823 | Item.uNextNestLevel != 0) { |
| 3824 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3825 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3826 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3827 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3828 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3829 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3830 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3831 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3832 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3833 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3834 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3835 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3836 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3837 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3838 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3839 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3840 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3841 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3842 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3843 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3844 | } |
| 3845 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3846 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3847 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3848 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3849 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3850 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3851 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3852 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3853 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3854 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3858 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3859 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3860 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3861 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3862 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3863 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3864 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3865 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3866 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3867 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3868 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3869 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3870 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3871 | |
| 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(spIndefiniteArrayBad3); |
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 -13; |
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 | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3883 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3884 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3885 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3886 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3887 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3888 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3889 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3890 | return -140; |
| 3891 | } |
| 3892 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3893 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3894 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3895 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3896 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3897 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3898 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 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 -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3902 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3903 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3904 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3905 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3906 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3907 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3908 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +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 -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3912 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3913 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3914 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3915 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3916 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3917 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3918 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3919 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3920 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3921 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3922 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3923 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3924 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3925 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3926 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3927 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3928 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3929 | return 0; |
| 3930 | } |
| 3931 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3932 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 3933 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 3934 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3935 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3936 | 0x81, // Array of length one |
| 3937 | 0x7f, // text string marked with indefinite length |
| 3938 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3939 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3940 | 0xff // ending break |
| 3941 | }; |
| 3942 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3943 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3944 | 0x81, // Array of length one |
| 3945 | 0x7f, // text string marked with indefinite length |
| 3946 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3947 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 3948 | 0xff // ending break |
| 3949 | }; |
| 3950 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3951 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3952 | 0x81, // Array of length one |
| 3953 | 0x7f, // text string marked with indefinite length |
| 3954 | 0x01, 0x02, // Not a string |
| 3955 | 0xff // ending break |
| 3956 | }; |
| 3957 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3958 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3959 | 0x81, // Array of length one |
| 3960 | 0x7f, // text string marked with indefinite length |
| 3961 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3962 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3963 | // missing end of string |
| 3964 | }; |
| 3965 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3966 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3967 | 0xa1, // Array of length one |
| 3968 | 0x7f, // text string marked with indefinite length |
| 3969 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 3970 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3971 | 0xff, // ending break |
| 3972 | 0x01 // integer being labeled. |
| 3973 | }; |
| 3974 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3975 | /** |
| 3976 | Make an indefinite length string |
| 3977 | |
| 3978 | @param Storage Storage for string, must be 144 bytes in size |
| 3979 | @return The indefinite length string |
| 3980 | |
| 3981 | This makes an array with one indefinite length string that has 7 chunks |
| 3982 | from size of 1 byte up to 64 bytes. |
| 3983 | */ |
| 3984 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3985 | { |
| 3986 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3987 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3988 | UsefulOutBuf_Init(&UOB, Storage); |
| 3989 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 3990 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3991 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3992 | uint8_t uStringByte = 0; |
| 3993 | // Use of type int is intentional |
| 3994 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 3995 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3996 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3997 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 3998 | for(int j = 0; j < uChunkSize; j++) { |
| 3999 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 4000 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 4004 | |
| 4005 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4006 | } |
| 4007 | |
| 4008 | static int CheckBigString(UsefulBufC BigString) |
| 4009 | { |
| 4010 | if(BigString.len != 255) { |
| 4011 | return 1; |
| 4012 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4013 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4014 | for(uint8_t i = 0; i < 255; i++){ |
| 4015 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 4016 | return 1; |
| 4017 | } |
| 4018 | } |
| 4019 | return 0; |
| 4020 | } |
| 4021 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4022 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4023 | int32_t IndefiniteLengthStringTest() |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4024 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4025 | QCBORDecodeContext DC; |
| 4026 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4027 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4028 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
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 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4031 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4032 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4033 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4034 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4035 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4036 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4037 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4038 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4039 | return -2; |
| 4040 | } |
| 4041 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 4042 | return -3; |
| 4043 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4044 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4045 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4046 | return -4; |
| 4047 | } |
| 4048 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 4049 | return -5; |
| 4050 | } |
| 4051 | if(QCBORDecode_Finish(&DC)) { |
| 4052 | return -6; |
| 4053 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4054 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4055 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4056 | QCBORDecode_Init(&DC, |
| 4057 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 4058 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4059 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4060 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4061 | return -7; |
| 4062 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4063 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4064 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4065 | return -8; |
| 4066 | } |
| 4067 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4068 | return -9; |
| 4069 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4070 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4071 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4072 | return -10; |
| 4073 | } |
| 4074 | |
| 4075 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4076 | QCBORDecode_Init(&DC, |
| 4077 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 4078 | QCBOR_DECODE_MODE_NORMAL); |
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_SetMemPool(&DC, MemPool, false)) { |
| 4081 | return -11; |
| 4082 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4083 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4084 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4085 | return -12; |
| 4086 | } |
| 4087 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4088 | return -13; |
| 4089 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4090 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4091 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4092 | return -14; |
| 4093 | } |
| 4094 | |
| 4095 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4096 | QCBORDecode_Init(&DC, |
| 4097 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 4098 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4099 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4100 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4101 | return -15; |
| 4102 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4103 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4104 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4105 | return -16; |
| 4106 | } |
| 4107 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4108 | return -17; |
| 4109 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4110 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4111 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 4112 | return -18; |
| 4113 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4114 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4115 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4116 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4117 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4118 | QCBORDecode_GetNext(&DC, &Item); |
| 4119 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4120 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4121 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4122 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4123 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4124 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4125 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4126 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4127 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4128 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4129 | |
| 4130 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 4131 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4132 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4133 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4134 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4135 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4136 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4137 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4138 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4139 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 4140 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
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 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4143 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4144 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4145 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4146 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4147 | QCBORDecode_GetNext(&DC, &Item); |
| 4148 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4149 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4150 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4151 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4152 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4153 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4154 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4155 | // ---- big bstr ----- |
| 4156 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4157 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4158 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4159 | return -25; |
| 4160 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4161 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4162 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4163 | return -26; |
| 4164 | } |
| 4165 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4166 | return -26; |
| 4167 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4168 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4169 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4170 | return -27; |
| 4171 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4172 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4173 | return -28; |
| 4174 | } |
| 4175 | if(CheckBigString(Item.val.string)) { |
| 4176 | return -3; |
| 4177 | } |
| 4178 | if(QCBORDecode_Finish(&DC)) { |
| 4179 | return -29; |
| 4180 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4181 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4182 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4183 | 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] | 4184 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4185 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4186 | return -30; |
| 4187 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4188 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4189 | QCBORDecode_GetNext(&DC, &Item); |
| 4190 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4191 | return -31; |
| 4192 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4193 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4194 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 4195 | return -32; |
| 4196 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4197 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4198 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4199 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 4200 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 4201 | return -33; |
| 4202 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4203 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4204 | if(QCBORDecode_Finish(&DC)) { |
| 4205 | return -34; |
| 4206 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4207 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4208 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4212 | int32_t AllocAllStringsTest() |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4213 | { |
| 4214 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4215 | QCBORError nCBORError; |
| 4216 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4217 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4218 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4219 | QCBORDecode_Init(&DC, |
| 4220 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4221 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4222 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4223 | 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] | 4224 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4225 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 4226 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4227 | return -1; |
| 4228 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4229 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4230 | if(CheckCSRMaps(&DC)) { |
| 4231 | return -2; |
| 4232 | } |
| 4233 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 4234 | // Next parse, save pointers to a few strings, destroy original and |
| 4235 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4236 | 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] | 4237 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4238 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4239 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 4240 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4241 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4242 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4243 | QCBORItem Item1, Item2, Item3, Item4; |
| 4244 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4245 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4246 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 4247 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4248 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4249 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4250 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4251 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4252 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4253 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4254 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4255 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4256 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4257 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 4258 | UsefulBuf_Set(CopyOfStorage, '_'); |
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 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4261 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 4262 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4263 | Item1.uDataAlloc != 0 || |
| 4264 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4265 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4266 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4267 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4268 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4269 | |
| 4270 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4271 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4272 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4273 | Item2.uDataAlloc != 0 || |
| 4274 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4275 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4276 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4277 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4278 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4279 | Item3.uDataAlloc == 0 || |
| 4280 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4281 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4282 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4283 | } |
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 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4286 | Item4.uDataAlloc == 0 || |
| 4287 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4288 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4289 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4291 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4292 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4293 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4294 | QCBORDecode_Init(&DC, |
| 4295 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 4296 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4297 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 4298 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4299 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4300 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4301 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4302 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4303 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4304 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 4305 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 4306 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 4307 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 4308 | } |
| 4309 | } |
| 4310 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4311 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4312 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4313 | } |
| 4314 | |
| 4315 | return 0; |
| 4316 | } |
| 4317 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4318 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4319 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 4320 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4321 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4322 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4323 | QCBORDecodeContext DC; |
| 4324 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 4325 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4326 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4327 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4328 | // Then fish into the internals of the decode context |
| 4329 | // to get the allocator function so it can be called directly. |
| 4330 | // Also figure out how much pool is available for use |
| 4331 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4332 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4333 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 4334 | if(nError) { |
| 4335 | return -9; |
| 4336 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4337 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 4338 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4339 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4340 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4341 | // First test -- ask for one more byte than available and see failure |
| 4342 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4343 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 4344 | return -1; |
| 4345 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4346 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4347 | // Re do the set up for the next test that will do a successful alloc, |
| 4348 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4349 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4350 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4351 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4352 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4353 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4354 | // Allocate one byte less than available and see success |
| 4355 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4356 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4357 | return -2; |
| 4358 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4359 | // Ask for some more and see failure |
| 4360 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4361 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 4362 | return -3; |
| 4363 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4364 | // Free the first allocate, retry the second and see success |
| 4365 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 4366 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4367 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 4368 | return -4; |
| 4369 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4370 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4371 | // Re do set up for next test that involves a successful alloc, |
| 4372 | // and a successful realloc and a failed realloc |
| 4373 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4374 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4375 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4376 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4377 | // Allocate half the pool and see success |
| 4378 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4379 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4380 | return -5; |
| 4381 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4382 | // Reallocate to take up the whole pool and see success |
| 4383 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4384 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 4385 | return -6; |
| 4386 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4387 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4388 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 4389 | return -7; |
| 4390 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4391 | // Try to allocate more to be sure there is failure after a realloc |
| 4392 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 4393 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4394 | return -8; |
| 4395 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4396 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4397 | return 0; |
| 4398 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4399 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4400 | |
| 4401 | /* Just enough of an allocator to test configuration of one */ |
| 4402 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 4403 | { |
| 4404 | (void)pOldMem; // unused variable |
| 4405 | |
| 4406 | if(uNewSize) { |
| 4407 | // Assumes the context pointer is the buffer and |
| 4408 | // nothing too big will ever be asked for. |
| 4409 | // This is only good for this basic test! |
| 4410 | return (UsefulBuf) {pCtx, uNewSize}; |
| 4411 | } else { |
| 4412 | return NULLUsefulBuf; |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4417 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4418 | { |
| 4419 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4420 | // nothing can be done with it unless that is set up. |
| 4421 | QCBORDecodeContext DC; |
| 4422 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 4423 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 4424 | |
| 4425 | uint8_t pAllocatorBuffer[50]; |
| 4426 | |
| 4427 | // This is really just to test that this call works. |
| 4428 | // The full functionality of string allocators is tested |
| 4429 | // elsewhere with the MemPool internal allocator. |
| 4430 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 4431 | |
| 4432 | QCBORItem Item; |
| 4433 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 4434 | return -1; |
| 4435 | } |
| 4436 | |
| 4437 | if(Item.uDataAlloc == 0 || |
| 4438 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4439 | Item.val.string.ptr != pAllocatorBuffer) { |
| 4440 | return -2; |
| 4441 | } |
| 4442 | |
| 4443 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 4444 | return -3; |
| 4445 | } |
| 4446 | |
| 4447 | return 0; |
| 4448 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4449 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4450 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4451 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4452 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4453 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4454 | /* exponent, mantissa |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4455 | [ |
| 4456 | 4([-1, 3]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4457 | 4([-20, 4759477275222530853136]), |
| 4458 | 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4459 | 5([300, 100]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4460 | 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4461 | 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4462 | 5([ 9223372036854775806, -4759477275222530853137]) |
| 4463 | 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4464 | ] |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4465 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4466 | static const uint8_t spExpectedExponentsAndMantissas[] = { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4467 | 0x88, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4468 | 0xC4, 0x82, 0x20, |
| 4469 | 0x03, |
| 4470 | 0xC4, 0x82, 0x33, |
| 4471 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4472 | 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4473 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4474 | 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 4475 | 0x18, 0x64, |
| 4476 | 0xC5, 0x82, 0x33, |
| 4477 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4478 | 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4479 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4480 | 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4481 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4482 | 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4483 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE |
| 4484 | }; |
| 4485 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4486 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4487 | int32_t ExponentAndMantissaDecodeTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4488 | { |
| 4489 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4490 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4491 | QCBORItem item; |
| 4492 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 4493 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 4494 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 4495 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4496 | |
| 4497 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4498 | QCBORDecode_Init(&DC, |
| 4499 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4500 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4501 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4502 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4503 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4504 | return 1; |
| 4505 | } |
| 4506 | |
| 4507 | if(item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4508 | return 2; |
| 4509 | } |
| 4510 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4511 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4512 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4513 | return 3; |
| 4514 | } |
| 4515 | |
| 4516 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4517 | item.val.expAndMantissa.Mantissa.nInt != 3 || |
| 4518 | item.val.expAndMantissa.nExponent != -1) { |
| 4519 | return 4; |
| 4520 | } |
| 4521 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4522 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4523 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4524 | return 5; |
| 4525 | } |
| 4526 | |
| 4527 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4528 | item.val.expAndMantissa.nExponent != -20 || |
| 4529 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4530 | return 6; |
| 4531 | } |
| 4532 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4533 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4534 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4535 | return 7; |
| 4536 | } |
| 4537 | |
| 4538 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM || |
| 4539 | item.val.expAndMantissa.nExponent != 9223372036854775807 || |
| 4540 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4541 | return 8; |
| 4542 | } |
| 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 9; |
| 4547 | } |
| 4548 | |
| 4549 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4550 | item.val.expAndMantissa.Mantissa.nInt != 100 || |
| 4551 | item.val.expAndMantissa.nExponent != 300) { |
| 4552 | return 10; |
| 4553 | } |
| 4554 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4555 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4556 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4557 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4558 | return 11; |
| 4559 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4560 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM || |
| 4561 | item.val.expAndMantissa.nExponent != -20 || |
| 4562 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4563 | return 12; |
| 4564 | } |
| 4565 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4566 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4567 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4568 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4569 | return 13; |
| 4570 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4571 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4572 | item.val.expAndMantissa.nExponent != -9223372036854775807 || |
| 4573 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4574 | return 14; |
| 4575 | } |
| 4576 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4577 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4578 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4579 | if(uErr != QCBOR_SUCCESS) { |
| 4580 | return 15; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4581 | } |
| 4582 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4583 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4584 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4585 | return 16; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4586 | } |
| 4587 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4588 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4589 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4590 | if(uErr != QCBOR_SUCCESS) { |
| 4591 | return 17; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4592 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4593 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4594 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4595 | item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4596 | return 18; |
| 4597 | } |
| 4598 | |
| 4599 | uErr = QCBORDecode_Finish(&DC); |
| 4600 | if(uErr != QCBOR_SUCCESS) { |
| 4601 | return 18; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | /* Now encode some stuff and then decode it */ |
| 4605 | uint8_t pBuf[40]; |
| 4606 | QCBOREncodeContext EC; |
| 4607 | UsefulBufC Encoded; |
| 4608 | |
| 4609 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 4610 | QCBOREncode_OpenArray(&EC); |
| 4611 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 4612 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 4613 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 4614 | QCBOREncode_CloseArray(&EC); |
| 4615 | QCBOREncode_Finish(&EC, &Encoded); |
| 4616 | |
| 4617 | |
| 4618 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4619 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4620 | if(uErr != QCBOR_SUCCESS) { |
| 4621 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4622 | } |
| 4623 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4624 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4625 | if(uErr != QCBOR_SUCCESS) { |
| 4626 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4630 | item.val.expAndMantissa.nExponent != 1000 || |
| 4631 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4632 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4633 | } |
| 4634 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4635 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4636 | if(uErr != QCBOR_SUCCESS) { |
| 4637 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4641 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 4642 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4643 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4644 | } |
| 4645 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4646 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4647 | if(uErr != QCBOR_SUCCESS) { |
| 4648 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4652 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 4653 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4654 | return 106; |
| 4655 | } |
| 4656 | |
| 4657 | |
| 4658 | int64_t nExp, nMant; |
| 4659 | UsefulBuf_MAKE_STACK_UB( MantBuf, 20); |
| 4660 | UsefulBufC Mant; |
| 4661 | bool bIsNeg; |
| 4662 | |
| 4663 | QCBORDecode_Init(&DC, |
| 4664 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4665 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4666 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4667 | |
| 4668 | // 4([-1, 3]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4669 | QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4670 | |
| 4671 | // 4([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4672 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, |
| 4673 | &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4674 | |
| 4675 | // 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4676 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4677 | MantBuf, &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4678 | |
| 4679 | // 5([300, 100]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4680 | QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4681 | |
| 4682 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4683 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4684 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4685 | |
| 4686 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4687 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4688 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4689 | |
| 4690 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4691 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4692 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4693 | |
| 4694 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4695 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4696 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4697 | |
| 4698 | QCBORDecode_ExitArray(&DC); |
| 4699 | |
| 4700 | uErr = QCBORDecode_Finish(&DC); |
| 4701 | if(uErr != QCBOR_SUCCESS) { |
| 4702 | return 200; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4709 | static const struct FailInput ExponentAndMantissaFailures[] = { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4710 | // Exponent > INT64_MAX |
| 4711 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4712 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4713 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4714 | // Mantissa > INT64_MAX |
| 4715 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4716 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 4717 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4718 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4719 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4720 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4721 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4722 | // bad content for big num |
| 4723 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 4724 | // bad content for big num |
| 4725 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 4726 | // Bad integer for exponent |
| 4727 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 4728 | // Bad integer for mantissa |
| 4729 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 4730 | // 3 items in array |
| 4731 | { {(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] | 4732 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4733 | // unterminated indefinite length array |
| 4734 | { {(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] | 4735 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4736 | // unterminated indefinite length array |
| 4737 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED}, |
| 4738 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4739 | // Empty array |
| 4740 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 4741 | // Second is not an integer |
| 4742 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4743 | // First is not an integer |
| 4744 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4745 | // Not an array |
| 4746 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 4747 | }; |
| 4748 | |
| 4749 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4750 | int32_t ExponentAndMantissaDecodeFailTests() |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4751 | { |
| 4752 | return ProcessFailures(ExponentAndMantissaFailures, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4753 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 4754 | struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4758 | |
| 4759 | |
| 4760 | |
| 4761 | /* |
| 4762 | Some basic CBOR with map and array used in a lot of tests. |
| 4763 | The map labels are all strings |
| 4764 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 4765 | { |
| 4766 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4767 | "an array of two strings": [ |
| 4768 | "string1", "string2" |
| 4769 | ], |
| 4770 | "map in a map": { |
| 4771 | "bytes 1": h'78787878', |
| 4772 | "bytes 2": h'79797979', |
| 4773 | "another int": 98, |
| 4774 | "text 2": "lies, damn lies and statistics" |
| 4775 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4776 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4777 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4778 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4779 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4780 | { |
| 4781 | QCBORItem Item1, Item2, Item3; |
| 4782 | int64_t nDecodedInt1, nDecodedInt2; |
| 4783 | UsefulBufC B1, B2, S1, S2, S3; |
| 4784 | |
| 4785 | QCBORDecodeContext DCtx; |
| 4786 | QCBORError nCBORError; |
| 4787 | |
| 4788 | QCBORDecode_Init(&DCtx, input, 0); |
| 4789 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4790 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4791 | |
| 4792 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 4793 | |
| 4794 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 4795 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4796 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 4797 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 4798 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4799 | QCBORDecode_ExitMap(&DCtx); |
| 4800 | |
| 4801 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 4802 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 4803 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 4804 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4805 | return -400; |
| 4806 | } |
| 4807 | QCBORDecode_ExitArray(&DCtx); |
| 4808 | |
| 4809 | // Parse the same array again using GetText() instead of GetItem() |
| 4810 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4811 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 4812 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4813 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 4814 | return 5000; |
| 4815 | } |
| 4816 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 4817 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4818 | return 5001; |
| 4819 | } */ |
| 4820 | |
| 4821 | QCBORDecode_ExitArray(&DCtx); |
| 4822 | |
| 4823 | QCBORDecode_ExitMap(&DCtx); |
| 4824 | |
| 4825 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 4826 | |
| 4827 | if(nCBORError) { |
| 4828 | return (int32_t)nCBORError; |
| 4829 | } |
| 4830 | |
| 4831 | if(nDecodedInt1 != 42) { |
| 4832 | return 1001; |
| 4833 | } |
| 4834 | |
| 4835 | if(nDecodedInt2 != 98) { |
| 4836 | return 1002; |
| 4837 | } |
| 4838 | |
| 4839 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4840 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4841 | return 1003; |
| 4842 | } |
| 4843 | |
| 4844 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4845 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4846 | return 1004; |
| 4847 | } |
| 4848 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4849 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4850 | return 1005; |
| 4851 | } |
| 4852 | |
| 4853 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 4854 | return 1006; |
| 4855 | } |
| 4856 | |
| 4857 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 4858 | return 1007; |
| 4859 | } |
| 4860 | |
| 4861 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 4862 | return 1008; |
| 4863 | } |
| 4864 | |
| 4865 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 4866 | return 1009; |
| 4867 | } |
| 4868 | |
| 4869 | return 0; |
| 4870 | } |
| 4871 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4872 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4873 | { |
| 4874 | -75008: h'05083399', |
| 4875 | 88: [], |
| 4876 | 100100: { |
| 4877 | "sub1": { |
| 4878 | 10: [ |
| 4879 | 0 |
| 4880 | ], |
| 4881 | -75009: h'A46823990001', |
| 4882 | 100100: { |
| 4883 | "json": "{ \"ueid\", \"xyz\"}", |
| 4884 | "subsub": { |
| 4885 | 100002: h'141813191001' |
| 4886 | } |
| 4887 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4888 | } |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4889 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4890 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4891 | */ |
| 4892 | |
| 4893 | static const uint8_t spNestedCBOR[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 4894 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, |
| 4895 | 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 4896 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, |
| 4897 | 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 4898 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, |
| 4899 | 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 4900 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, |
| 4901 | 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 4902 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, |
| 4903 | 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 4904 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, |
| 4905 | 0x01 |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4906 | }; |
| 4907 | |
| 4908 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 4909 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 4910 | { |
| 4911 | UsefulBufC String; |
| 4912 | |
| 4913 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 4914 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 4915 | |
| 4916 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4917 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 4918 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4919 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 4920 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 4921 | if(QCBORDecode_GetError(pDCtx)) { |
| 4922 | return 4001; |
| 4923 | } |
| 4924 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 4925 | return 4002; |
| 4926 | } |
| 4927 | QCBORDecode_ExitMap(pDCtx); |
| 4928 | QCBORDecode_ExitMap(pDCtx); |
| 4929 | QCBORDecode_ExitMap(pDCtx); |
| 4930 | QCBORDecode_ExitMap(pDCtx); |
| 4931 | |
| 4932 | return 0; |
| 4933 | } |
| 4934 | |
| 4935 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 4936 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 4937 | { |
| 4938 | QCBORItem Item; |
| 4939 | QCBORError uErr; |
| 4940 | |
| 4941 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 4942 | for(int x = 0; x < 20; x++) { |
| 4943 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 4944 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4945 | return 4100; |
| 4946 | |
| 4947 | } |
| 4948 | } |
| 4949 | QCBORDecode_ExitArray(pDCtx); |
| 4950 | if(QCBORDecode_GetError(pDCtx)) { |
| 4951 | return 4101; |
| 4952 | } |
| 4953 | |
| 4954 | return 0; |
| 4955 | } |
| 4956 | |
| 4957 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 4958 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 4959 | { |
| 4960 | QCBORError uErr; |
| 4961 | |
| 4962 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4963 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 4964 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 4965 | int64_t nInt = 99; |
| 4966 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 4967 | if(nInt != 0) { |
| 4968 | return 4200; |
| 4969 | } |
| 4970 | for(int x = 0; x < 20; x++) { |
| 4971 | QCBORItem Item; |
| 4972 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 4973 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4974 | return 4201; |
| 4975 | |
| 4976 | } |
| 4977 | } |
| 4978 | QCBORDecode_ExitArray(pDCtx); |
| 4979 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 4980 | return 4202; |
| 4981 | } |
| 4982 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 4983 | UsefulBufC dD; |
| 4984 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 4985 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4986 | return 4203; |
| 4987 | } |
| 4988 | for(int x = 0; x < 20; x++) { |
| 4989 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 4990 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 4991 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4992 | return 4204; |
| 4993 | } |
| 4994 | } |
| 4995 | QCBORDecode_ExitArray(pDCtx); |
| 4996 | QCBORDecode_ExitMap(pDCtx); |
| 4997 | QCBORDecode_ExitMap(pDCtx); |
| 4998 | |
| 4999 | return 0; |
| 5000 | } |
| 5001 | |
| 5002 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 5003 | and arrays and such. */ |
| 5004 | static int32_t DecodeNestedIterate() |
| 5005 | { |
| 5006 | QCBORDecodeContext DCtx; |
| 5007 | int32_t nReturn; |
| 5008 | QCBORError uErr; |
| 5009 | |
| 5010 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 5011 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 5012 | |
| 5013 | for(int j = 0; j < 5; j++) { |
| 5014 | for(int i = 0; i < 20; i++) { |
| 5015 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 5016 | if(nReturn) { |
| 5017 | return nReturn; |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | for(int i = 0; i < 20; i++) { |
| 5022 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 5023 | if(nReturn ) { |
| 5024 | return nReturn; |
| 5025 | } |
| 5026 | } |
| 5027 | |
| 5028 | for(int i = 0; i < 20; i++) { |
| 5029 | nReturn = DecodeNestedGetZero(&DCtx); |
| 5030 | if(nReturn ) { |
| 5031 | return nReturn; |
| 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | QCBORDecode_ExitMap(&DCtx); |
| 5037 | uErr = QCBORDecode_Finish(&DCtx); |
| 5038 | if(uErr) { |
| 5039 | return (int32_t)uErr + 4100; |
| 5040 | } |
| 5041 | |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5045 | |
| 5046 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5047 | [ |
| 5048 | 23, |
| 5049 | 6000, |
| 5050 | h'67616C6163746963', |
| 5051 | h'686176656E20746F6B656E' |
| 5052 | ] |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5053 | */ |
| 5054 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5055 | 0x84, |
| 5056 | 0x17, |
| 5057 | 0x19, 0x17, 0x70, |
| 5058 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 5059 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5060 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5061 | /* [h'', {}, [], 0] */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5062 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 5063 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5064 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5065 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 5066 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5067 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5068 | /* {} */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5069 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5070 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5071 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5072 | /* |
| 5073 | { |
| 5074 | 0: [], |
| 5075 | 9: [ |
| 5076 | [], |
| 5077 | [] |
| 5078 | ], |
| 5079 | 8: { |
| 5080 | 1: [], |
| 5081 | 2: {}, |
| 5082 | 3: [] |
| 5083 | }, |
| 5084 | 4: {}, |
| 5085 | 5: [], |
| 5086 | 6: [ |
| 5087 | [], |
| 5088 | [] |
| 5089 | ] |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5090 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5091 | */ |
| 5092 | static const uint8_t spMapOfEmpty[] = { |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5093 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, |
| 5094 | 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, |
| 5095 | 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f, |
| 5096 | 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5097 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5098 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5099 | |
| 5100 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5101 | /* |
| 5102 | Too many tags |
| 5103 | Invalid tag content |
| 5104 | Duplicate label |
| 5105 | Integer overflow |
| 5106 | Date overflow |
| 5107 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5108 | { |
| 5109 | 1: 224(225(226(227(4(0))))), |
| 5110 | 2: 1(h''), |
| 5111 | 3: -18446744073709551616, |
| 5112 | 4: 1(1.0e+300), |
| 5113 | 5: 0, 8: 8 |
| 5114 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5115 | */ |
| 5116 | static const uint8_t spRecoverableMapErrors[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5117 | 0xa7, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5118 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 5119 | 0x02, 0xc1, 0x40, |
| 5120 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5121 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
| 5122 | 0x05, 0x00, |
| 5123 | 0x05, 0x00, |
| 5124 | 0x08, 0x08, |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5125 | }; |
| 5126 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5127 | /* Bad break */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5128 | static const uint8_t spUnRecoverableMapError1[] = { |
| 5129 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5130 | }; |
| 5131 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5132 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5133 | /* No more items */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5134 | static const uint8_t spUnRecoverableMapError2[] = { |
| 5135 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5136 | }; |
| 5137 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5138 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5139 | static const uint8_t spUnRecoverableMapError3[] = { |
| 5140 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 5141 | }; |
| 5142 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 5143 | /* Hit end because string is too long */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5144 | static const uint8_t spUnRecoverableMapError4[] = { |
| 5145 | 0xbf, |
| 5146 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5147 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5148 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5149 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5150 | 0xff |
| 5151 | }; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5152 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5153 | |
| 5154 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5155 | int32_t EnterMapTest() |
| 5156 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5157 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5158 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5159 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5160 | int32_t nReturn; |
| 5161 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5162 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5163 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5164 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5165 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5166 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5167 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5168 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5169 | QCBORDecode_ExitArray(&DCtx); |
| 5170 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5171 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 5172 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5173 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5174 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5175 | QCBORDecode_ExitArray(&DCtx); |
| 5176 | QCBORDecode_ExitArray(&DCtx); |
| 5177 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5178 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 5179 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5180 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5181 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5182 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5183 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5184 | QCBORDecode_ExitArray(&DCtx); |
| 5185 | QCBORDecode_ExitMap(&DCtx); |
| 5186 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5187 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5188 | QCBORDecode_ExitMap(&DCtx); |
| 5189 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5190 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5191 | QCBORDecode_ExitArray(&DCtx); |
| 5192 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5193 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 5194 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5195 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5196 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5197 | QCBORDecode_ExitArray(&DCtx); |
| 5198 | QCBORDecode_ExitArray(&DCtx); |
| 5199 | |
| 5200 | QCBORDecode_ExitMap(&DCtx); |
| 5201 | |
| 5202 | uErr = QCBORDecode_Finish(&DCtx); |
| 5203 | if(uErr != QCBOR_SUCCESS){ |
| 5204 | return 3011; |
| 5205 | } |
| 5206 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5207 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5208 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5209 | if(nReturn) { |
| 5210 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5211 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5212 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5213 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5214 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5215 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5216 | if(nReturn) { |
| 5217 | return nReturn; |
| 5218 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5219 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5220 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5221 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5222 | // These tests confirm the cursor is at the right place after entering |
| 5223 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5224 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5225 | |
| 5226 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5227 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5228 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5229 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5230 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 5231 | return 2001; |
| 5232 | } |
| 5233 | |
| 5234 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5235 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5236 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5237 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5238 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 5239 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5240 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5241 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 5242 | return 2051; |
| 5243 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5244 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5245 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 5246 | return 2002; |
| 5247 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5248 | QCBORDecode_ExitArray(&DCtx); |
| 5249 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 5250 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5251 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5252 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 5253 | return 2052; |
| 5254 | } |
| 5255 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5256 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5257 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5258 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5259 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5260 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5261 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5262 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5263 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5264 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 5265 | return 2003; |
| 5266 | } |
| 5267 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5268 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5269 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5270 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5271 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5272 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5273 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5274 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5275 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5276 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5277 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5278 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5279 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5280 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5281 | } |
| 5282 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5283 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5284 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 5285 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5286 | QCBORDecode_ExitArray(&DCtx); |
| 5287 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5288 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 5289 | return 2006; |
| 5290 | } |
| 5291 | QCBORDecode_ExitMap(&DCtx); |
| 5292 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5293 | return 2007; |
| 5294 | } |
| 5295 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5296 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5297 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5298 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5299 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5300 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5301 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5302 | return 2008; |
| 5303 | } |
| 5304 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5305 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5306 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5307 | return 2009; |
| 5308 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5309 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5310 | |
| 5311 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5312 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5313 | // This will fail because the map is empty. |
| 5314 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5315 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5316 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5317 | return 2010; |
| 5318 | } |
| 5319 | QCBORDecode_ExitMap(&DCtx); |
| 5320 | uErr = QCBORDecode_Finish(&DCtx); |
| 5321 | if(uErr != QCBOR_SUCCESS){ |
| 5322 | return 2011; |
| 5323 | } |
| 5324 | |
| 5325 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5326 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5327 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5328 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5329 | // This will fail because the map is empty. |
| 5330 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5331 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5332 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5333 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5334 | } |
| 5335 | QCBORDecode_ExitMap(&DCtx); |
| 5336 | uErr = QCBORDecode_Finish(&DCtx); |
| 5337 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5338 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5339 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5340 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5341 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5342 | |
| 5343 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5344 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5345 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5346 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5347 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5348 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5349 | QCBORDecode_ExitArray(&DCtx); |
| 5350 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 5351 | QCBORDecode_ExitArray(&DCtx); |
| 5352 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5353 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5354 | return 2014; |
| 5355 | } |
| 5356 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5357 | int64_t nInt; |
| 5358 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5359 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5360 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5361 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5362 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 5363 | return 2021; |
| 5364 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5365 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 5366 | return 2121; |
| 5367 | } |
| 5368 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 5369 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5370 | |
| 5371 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 5372 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5373 | if(uErr != QCBOR_ERR_BAD_OPT_TAG) { |
| 5374 | return 2022; |
| 5375 | } |
| 5376 | |
| 5377 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 5378 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5379 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 5380 | return 2023; |
| 5381 | } |
| 5382 | |
| 5383 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 5384 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5385 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5386 | if(uErr != QCBOR_ERR_DATE_OVERFLOW) { |
| 5387 | return 2024; |
| 5388 | } |
| 5389 | #else |
| 5390 | if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 5391 | return 2027; |
| 5392 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5393 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5394 | |
| 5395 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 5396 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5397 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 5398 | return 2025; |
| 5399 | } |
| 5400 | |
| 5401 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 5402 | |
| 5403 | QCBORDecode_ExitMap(&DCtx); |
| 5404 | uErr = QCBORDecode_Finish(&DCtx); |
| 5405 | if(uErr != QCBOR_SUCCESS) { |
| 5406 | return 2026; |
| 5407 | } |
| 5408 | |
| 5409 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5410 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5411 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5412 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5413 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 5414 | return 2030; |
| 5415 | } |
| 5416 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5417 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5418 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5419 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5420 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5421 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5422 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5423 | return 2031; |
| 5424 | } |
| 5425 | |
| 5426 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5427 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5428 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5429 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5430 | if(uErr != QCBOR_ERR_HIT_END) { |
| 5431 | return 2032; |
| 5432 | } |
| 5433 | |
| 5434 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5435 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5436 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5437 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5438 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 5439 | return 2033; |
| 5440 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5441 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 5442 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 5443 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5444 | nReturn = DecodeNestedIterate(); |
| 5445 | |
| 5446 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5450 | struct NumberConversion { |
| 5451 | char *szDescription; |
| 5452 | UsefulBufC CBOR; |
| 5453 | int64_t nConvertedToInt64; |
| 5454 | QCBORError uErrorInt64; |
| 5455 | uint64_t uConvertToUInt64; |
| 5456 | QCBORError uErrorUint64; |
| 5457 | double dConvertToDouble; |
| 5458 | QCBORError uErrorDouble; |
| 5459 | }; |
| 5460 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5461 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5462 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5463 | "too large to fit into int64_t", |
| 5464 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 5465 | 0, |
| 5466 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5467 | 0, |
| 5468 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5469 | ((double)INT64_MIN) + 1 , |
| 5470 | QCBOR_SUCCESS |
| 5471 | }, |
| 5472 | { |
| 5473 | "largest negative int that fits in int64_t", |
| 5474 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 5475 | INT64_MIN, |
| 5476 | QCBOR_SUCCESS, |
| 5477 | 0, |
| 5478 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5479 | (double)INT64_MIN, |
| 5480 | QCBOR_SUCCESS |
| 5481 | }, |
| 5482 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5483 | "negative bignum -1", |
| 5484 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 5485 | -1, |
| 5486 | QCBOR_SUCCESS, |
| 5487 | 0, |
| 5488 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5489 | -1.0, |
| 5490 | QCBOR_SUCCESS |
| 5491 | }, |
| 5492 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5493 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5494 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5495 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5496 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5497 | 257000, |
| 5498 | QCBOR_SUCCESS, |
| 5499 | 257000, |
| 5500 | QCBOR_SUCCESS, |
| 5501 | 257000.0, |
| 5502 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5503 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5504 | 0, |
| 5505 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5506 | 0, |
| 5507 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5508 | 0.0, |
| 5509 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5510 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5511 | }, |
| 5512 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5513 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5514 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5515 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5516 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5517 | -2064, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5518 | QCBOR_SUCCESS, |
| 5519 | 0, |
| 5520 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5521 | -2064.0, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5522 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5523 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5524 | 0, |
| 5525 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5526 | 0, |
| 5527 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5528 | 0.0, |
| 5529 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5530 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5531 | }, |
| 5532 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5533 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5534 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5535 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5536 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5537 | 2056, |
| 5538 | QCBOR_SUCCESS, |
| 5539 | 2056, |
| 5540 | QCBOR_SUCCESS, |
| 5541 | 2056.0, |
| 5542 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5543 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5544 | 0, |
| 5545 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5546 | 0, |
| 5547 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5548 | 0.0, |
| 5549 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5550 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5551 | }, |
| 5552 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5553 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5554 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 5555 | 0, |
| 5556 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5557 | 0, |
| 5558 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5559 | -18446744073709551617.0, |
| 5560 | QCBOR_SUCCESS |
| 5561 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5562 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5563 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5564 | "Positive bignum 0x01020304 indefinite length string", |
| 5565 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 5566 | 0x01020304, |
| 5567 | QCBOR_SUCCESS, |
| 5568 | 0x01020304, |
| 5569 | QCBOR_SUCCESS, |
| 5570 | 16909060.0, |
| 5571 | QCBOR_SUCCESS |
| 5572 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5573 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5574 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5575 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5576 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5577 | 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] | 5578 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5579 | 0, |
| 5580 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5581 | 0, |
| 5582 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5583 | -INFINITY, |
| 5584 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5585 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5586 | 0, |
| 5587 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5588 | 0, |
| 5589 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5590 | 0.0, |
| 5591 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5592 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5593 | }, |
| 5594 | { |
| 5595 | "big float [9223372036854775806, 9223372036854775806]", |
| 5596 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 5597 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5598 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5599 | 0, |
| 5600 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5601 | 0, |
| 5602 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5603 | INFINITY, |
| 5604 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5605 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5606 | 0, |
| 5607 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5608 | 0, |
| 5609 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5610 | 0.0, |
| 5611 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5612 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5613 | }, |
| 5614 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5615 | "Big float 3 * 2^^2", |
| 5616 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5617 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5618 | 12, |
| 5619 | QCBOR_SUCCESS, |
| 5620 | 12, |
| 5621 | QCBOR_SUCCESS, |
| 5622 | 12.0, |
| 5623 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5624 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5625 | 0, |
| 5626 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5627 | 0, |
| 5628 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5629 | 0.0, |
| 5630 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5631 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5632 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5633 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5634 | "Positive integer 18446744073709551615", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5635 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5636 | 0, |
| 5637 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5638 | 18446744073709551615ULL, |
| 5639 | QCBOR_SUCCESS, |
| 5640 | 18446744073709551615.0, |
| 5641 | QCBOR_SUCCESS |
| 5642 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5643 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5644 | "Positive bignum 0xffff", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5645 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 5646 | 65536-1, |
| 5647 | QCBOR_SUCCESS, |
| 5648 | 0xffff, |
| 5649 | QCBOR_SUCCESS, |
| 5650 | 65535.0, |
| 5651 | QCBOR_SUCCESS |
| 5652 | }, |
| 5653 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5654 | "Postive integer 0", |
| 5655 | {(uint8_t[]){0x0}, 1}, |
| 5656 | 0LL, |
| 5657 | QCBOR_SUCCESS, |
| 5658 | 0ULL, |
| 5659 | QCBOR_SUCCESS, |
| 5660 | 0.0, |
| 5661 | QCBOR_SUCCESS |
| 5662 | }, |
| 5663 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5664 | "Negative integer -18446744073709551616", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5665 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 5666 | -9223372036854775807-1, // INT64_MIN |
| 5667 | QCBOR_SUCCESS, |
| 5668 | 0ULL, |
| 5669 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5670 | -9223372036854775808.0, |
| 5671 | QCBOR_SUCCESS |
| 5672 | }, |
| 5673 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5674 | "Double Floating point value 100.3", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5675 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5676 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5677 | 100L, |
| 5678 | QCBOR_SUCCESS, |
| 5679 | 100ULL, |
| 5680 | QCBOR_SUCCESS, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5681 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5682 | 0, |
| 5683 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5684 | 0, |
| 5685 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5686 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5687 | 100.3, |
| 5688 | QCBOR_SUCCESS |
| 5689 | }, |
| 5690 | { |
| 5691 | "Floating point value NaN 0xfa7fc00000", |
| 5692 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5693 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5694 | 0, |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5695 | QCBOR_ERR_FLOAT_EXCEPTION, |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5696 | 0, |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5697 | QCBOR_ERR_FLOAT_EXCEPTION, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5698 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5699 | 0, |
| 5700 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5701 | 0, |
| 5702 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5703 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5704 | NAN, |
| 5705 | QCBOR_SUCCESS |
| 5706 | }, |
| 5707 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5708 | "half-precision Floating point value -4", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5709 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5710 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 5711 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5712 | // Normal case with all enabled. |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5713 | -4, |
| 5714 | QCBOR_SUCCESS, |
| 5715 | 0, |
| 5716 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5717 | -4.0, |
| 5718 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5719 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5720 | // Float HW disabled |
| 5721 | -4, |
| 5722 | QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer |
| 5723 | 0, |
| 5724 | QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer |
| 5725 | -4.0, |
| 5726 | QCBOR_SUCCESS // Uses ieee754.h to conver, not HW |
| 5727 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5728 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5729 | // Half-precision disabled |
| 5730 | -4, |
| 5731 | QCBOR_ERR_HALF_PRECISION_DISABLED, |
| 5732 | 0, |
| 5733 | QCBOR_ERR_HALF_PRECISION_DISABLED, |
| 5734 | -4.0, |
| 5735 | QCBOR_ERR_HALF_PRECISION_DISABLED |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5736 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5737 | }, |
| 5738 | { |
| 5739 | "Decimal fraction 3/10", |
| 5740 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5741 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5742 | 0, |
| 5743 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5744 | 0, |
| 5745 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5746 | 0.30000000000000004, |
| 5747 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5748 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5749 | 0, |
| 5750 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5751 | 0, |
| 5752 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5753 | 0.0, |
| 5754 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5755 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5756 | }, |
| 5757 | { |
| 5758 | "+inifinity", |
| 5759 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 5760 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5761 | 0, |
| 5762 | QCBOR_ERR_FLOAT_EXCEPTION, |
| 5763 | 0, |
| 5764 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5765 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5766 | 0, |
| 5767 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5768 | 0, |
| 5769 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5770 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5771 | INFINITY, |
| 5772 | QCBOR_SUCCESS |
| 5773 | }, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5774 | |
| 5775 | { |
| 5776 | "extreme pos bignum", |
| 5777 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 5778 | // 50 rows of 8 is 400 digits. |
| 5779 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5780 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5781 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5782 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5783 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5784 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5785 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5786 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5787 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5788 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5789 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5790 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5791 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5792 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5793 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5794 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5795 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5796 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5797 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5798 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5799 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5800 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5801 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5802 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5803 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5804 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5805 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5806 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5807 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5808 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5809 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5810 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5811 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5812 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5813 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5814 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5815 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5816 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5817 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5818 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5819 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5820 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5821 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5822 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5823 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5824 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5825 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5826 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5827 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5828 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
| 5829 | 404}, |
| 5830 | 0, |
| 5831 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5832 | 0, |
| 5833 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5834 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5835 | INFINITY, |
| 5836 | QCBOR_SUCCESS |
| 5837 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5838 | 0, |
| 5839 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5840 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5841 | }, |
| 5842 | |
| 5843 | { |
| 5844 | "extreme neg bignum", |
| 5845 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 5846 | // 50 rows of 8 is 400 digits. |
| 5847 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5848 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5849 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5850 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5851 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5852 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5853 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5854 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5855 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5856 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5857 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5858 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5859 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5860 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5861 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5862 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5863 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5864 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5865 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5866 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5867 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5868 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 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}, |
| 5897 | 404}, |
| 5898 | 0, |
| 5899 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5900 | 0, |
| 5901 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5902 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5903 | -INFINITY, |
| 5904 | QCBOR_SUCCESS |
| 5905 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5906 | 0, |
| 5907 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5908 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5909 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5910 | |
| 5911 | { |
| 5912 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 5913 | {(uint8_t[]){ |
| 5914 | 0xC5, 0x82, |
| 5915 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 5916 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 5917 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 5918 | 0, |
| 5919 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5920 | 0, |
| 5921 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5922 | 0, |
| 5923 | QCBOR_SUCCESS |
| 5924 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5925 | 0, |
| 5926 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5927 | 0, |
| 5928 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5929 | 0.0, |
| 5930 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5931 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5932 | }, |
| 5933 | |
| 5934 | { |
| 5935 | "bigfloat that evaluates to -INFINITY", |
| 5936 | {(uint8_t[]){ |
| 5937 | 0xC5, 0x82, |
| 5938 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5939 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
| 5940 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 5941 | 0, |
| 5942 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5943 | 0, |
| 5944 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5945 | -INFINITY, |
| 5946 | QCBOR_SUCCESS |
| 5947 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5948 | 0, |
| 5949 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5950 | 0, |
| 5951 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5952 | 0.0, |
| 5953 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5954 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
| 5955 | }, |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5956 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5957 | |
| 5958 | |
| 5959 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5960 | |
| 5961 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 5962 | { |
| 5963 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 5964 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 5965 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 5966 | return 1; |
| 5967 | } |
| 5968 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5969 | (void)Pool; |
| 5970 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5971 | return 0; |
| 5972 | } |
| 5973 | |
| 5974 | |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5975 | int32_t IntegerConvertTest() |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5976 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5977 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 5978 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5979 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5980 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 5981 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5982 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5983 | // Set up the decoding context including a memory pool so that |
| 5984 | // indefinite length items can be checked |
| 5985 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5986 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5987 | |
| 5988 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5989 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 5990 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5994 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5995 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5996 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5997 | } |
| 5998 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5999 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6000 | } |
| 6001 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6002 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6003 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6004 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6005 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6006 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6007 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6008 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6009 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6010 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6011 | } |
| 6012 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6013 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6014 | } |
| 6015 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6016 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6017 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 6018 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6019 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6020 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6021 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6022 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6023 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6024 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6025 | } |
| 6026 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 6027 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 6028 | // NaN's can't be compared for equality. A NaN is |
| 6029 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6030 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6031 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6032 | } |
| 6033 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6034 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6035 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6036 | } |
| 6037 | } |
| 6038 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6039 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | return 0; |
| 6043 | } |
| 6044 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6045 | |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 6046 | |
| 6047 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6048 | int32_t CBORSequenceDecodeTests(void) |
| 6049 | { |
| 6050 | QCBORDecodeContext DCtx; |
| 6051 | QCBORItem Item; |
| 6052 | QCBORError uCBORError; |
| 6053 | |
| 6054 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6055 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6056 | // The input for the date test happens to be a sequence so it |
| 6057 | // is reused. It is a sequence because it doesn't start as |
| 6058 | // an array or map. |
| 6059 | QCBORDecode_Init(&DCtx, |
| 6060 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 6061 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6062 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6063 | // Get the first item |
| 6064 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6065 | if(uCBORError != QCBOR_SUCCESS) { |
| 6066 | return 1; |
| 6067 | } |
| 6068 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING) { |
| 6069 | return 2; |
| 6070 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6071 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6072 | // Get a second item |
| 6073 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 6074 | if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) { |
| 6075 | return 66; |
| 6076 | } |
| 6077 | |
| 6078 | // Get a third item |
| 6079 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6080 | if(uCBORError != QCBOR_SUCCESS) { |
| 6081 | return 2; |
| 6082 | } |
| 6083 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 6084 | return 3; |
| 6085 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6086 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6087 | // A sequence can have stuff at the end that may |
| 6088 | // or may not be valid CBOR. The protocol decoder knows |
| 6089 | // when to stop by definition of the protocol, not |
| 6090 | // when the top-level map or array is ended. |
| 6091 | // Finish still has to be called to know that |
| 6092 | // maps and arrays (if there were any) were closed |
| 6093 | // off correctly. When called like this it |
| 6094 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 6095 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6096 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 6097 | return 4; |
| 6098 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6099 | |
| 6100 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6101 | // --- Test an empty input ---- |
| 6102 | uint8_t empty[1]; |
| 6103 | UsefulBufC Empty = {empty, 0}; |
| 6104 | QCBORDecode_Init(&DCtx, |
| 6105 | Empty, |
| 6106 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6107 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6108 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6109 | if(uCBORError != QCBOR_SUCCESS) { |
| 6110 | return 5; |
| 6111 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6112 | |
| 6113 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6114 | // --- Sequence with unclosed indefinite length array --- |
| 6115 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6116 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6117 | QCBORDecode_Init(&DCtx, |
| 6118 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 6119 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6120 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6121 | // Get the first item |
| 6122 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6123 | if(uCBORError != QCBOR_SUCCESS) { |
| 6124 | return 7; |
| 6125 | } |
| 6126 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6127 | return 8; |
| 6128 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6129 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6130 | // Get a second item |
| 6131 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6132 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6133 | if(uCBORError != QCBOR_SUCCESS) { |
| 6134 | return 9; |
| 6135 | } |
| 6136 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6137 | return 10; |
| 6138 | } |
| 6139 | |
| 6140 | // Try to finish before consuming all bytes to confirm |
| 6141 | // that the still-open error is returned. |
| 6142 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6143 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6144 | return 11; |
| 6145 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6146 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6147 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6148 | return 20; |
| 6149 | } |
| 6150 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6151 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6152 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6153 | // --- Sequence with a closed indefinite length array --- |
| 6154 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6155 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6156 | QCBORDecode_Init(&DCtx, |
| 6157 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 6158 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6159 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6160 | // Get the first item |
| 6161 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6162 | if(uCBORError != QCBOR_SUCCESS) { |
| 6163 | return 12; |
| 6164 | } |
| 6165 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6166 | return 13; |
| 6167 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 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 | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6171 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 6172 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6173 | if(uCBORError != QCBOR_SUCCESS) { |
| 6174 | return 14; |
| 6175 | } |
| 6176 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6177 | return 15; |
| 6178 | } |
| 6179 | |
| 6180 | // Try to finish before consuming all bytes to confirm |
| 6181 | // that the still-open error is returned. |
| 6182 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6183 | if(uCBORError != QCBOR_SUCCESS) { |
| 6184 | return 16; |
| 6185 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6186 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 6187 | if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) { |
| 6188 | return 20; |
| 6189 | } |
| 6190 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6191 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6192 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6193 | return 0; |
| 6194 | } |
| 6195 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6196 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 6197 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6198 | int32_t IntToTests() |
| 6199 | { |
| 6200 | int nErrCode; |
| 6201 | int32_t n32; |
| 6202 | int16_t n16; |
| 6203 | int8_t n8; |
| 6204 | uint32_t u32; |
| 6205 | uint16_t u16; |
| 6206 | uint8_t u8; |
| 6207 | uint64_t u64; |
| 6208 | |
| 6209 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 6210 | if(nErrCode == -1 || n32 != 1) { |
| 6211 | return 1; |
| 6212 | } |
| 6213 | |
| 6214 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 6215 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 6216 | return 2; |
| 6217 | } |
| 6218 | |
| 6219 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 6220 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 6221 | return 3; |
| 6222 | } |
| 6223 | |
| 6224 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 6225 | if(nErrCode != -1) { |
| 6226 | return 4; |
| 6227 | } |
| 6228 | |
| 6229 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 6230 | if(nErrCode != -1) { |
| 6231 | return 5; |
| 6232 | } |
| 6233 | |
| 6234 | |
| 6235 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 6236 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 6237 | return 6; |
| 6238 | } |
| 6239 | |
| 6240 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 6241 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 6242 | return 7; |
| 6243 | } |
| 6244 | |
| 6245 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 6246 | if(nErrCode == -1 || n16 != 1) { |
| 6247 | return 8; |
| 6248 | } |
| 6249 | |
| 6250 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 6251 | if(nErrCode != -1) { |
| 6252 | return 9; |
| 6253 | } |
| 6254 | |
| 6255 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 6256 | if(nErrCode != -1) { |
| 6257 | return 10; |
| 6258 | } |
| 6259 | |
| 6260 | |
| 6261 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 6262 | if(nErrCode == -1 || n8 != 1) { |
| 6263 | return 11; |
| 6264 | } |
| 6265 | |
| 6266 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 6267 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 6268 | return 12; |
| 6269 | } |
| 6270 | |
| 6271 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 6272 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 6273 | return 13; |
| 6274 | } |
| 6275 | |
| 6276 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 6277 | if(nErrCode != -1) { |
| 6278 | return 14; |
| 6279 | } |
| 6280 | |
| 6281 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 6282 | if(nErrCode != -1) { |
| 6283 | return 15; |
| 6284 | } |
| 6285 | |
| 6286 | |
| 6287 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 6288 | if(nErrCode == -1 || u32 != 1) { |
| 6289 | return 16; |
| 6290 | } |
| 6291 | |
| 6292 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 6293 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 6294 | return 17; |
| 6295 | } |
| 6296 | |
| 6297 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 6298 | if(nErrCode == -1 || u32 != 0) { |
| 6299 | return 18; |
| 6300 | } |
| 6301 | |
| 6302 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 6303 | if(nErrCode != -1) { |
| 6304 | return 19; |
| 6305 | } |
| 6306 | |
| 6307 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 6308 | if(nErrCode != -1) { |
| 6309 | return 20; |
| 6310 | } |
| 6311 | |
| 6312 | |
| 6313 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 6314 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 6315 | return 21; |
| 6316 | } |
| 6317 | |
| 6318 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 6319 | if(nErrCode == -1 || u16 != 0) { |
| 6320 | return 22; |
| 6321 | } |
| 6322 | |
| 6323 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 6324 | if(nErrCode == -1 || u16 != 1) { |
| 6325 | return 23; |
| 6326 | } |
| 6327 | |
| 6328 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 6329 | if(nErrCode != -1) { |
| 6330 | return 24; |
| 6331 | } |
| 6332 | |
| 6333 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 6334 | if(nErrCode != -1) { |
| 6335 | return 25; |
| 6336 | } |
| 6337 | |
| 6338 | |
| 6339 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 6340 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 6341 | return 26; |
| 6342 | } |
| 6343 | |
| 6344 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 6345 | if(nErrCode == -1 || u8 != 0) { |
| 6346 | return 27; |
| 6347 | } |
| 6348 | |
| 6349 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 6350 | if(nErrCode == -1 || u8 != 1) { |
| 6351 | return 28; |
| 6352 | } |
| 6353 | |
| 6354 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 6355 | if(nErrCode != -1) { |
| 6356 | return 29; |
| 6357 | } |
| 6358 | |
| 6359 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 6360 | if(nErrCode != -1) { |
| 6361 | return 30; |
| 6362 | } |
| 6363 | |
| 6364 | |
| 6365 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 6366 | if(nErrCode == -1 || u64 != 1) { |
| 6367 | return 31; |
| 6368 | } |
| 6369 | |
| 6370 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 6371 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 6372 | return 32; |
| 6373 | } |
| 6374 | |
| 6375 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 6376 | if(nErrCode == -1 || u64 != 0) { |
| 6377 | return 33; |
| 6378 | } |
| 6379 | |
| 6380 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 6381 | if(nErrCode != -1) { |
| 6382 | return 34; |
| 6383 | } |
| 6384 | |
| 6385 | return 0; |
| 6386 | } |
| 6387 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6388 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6389 | |
| 6390 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6391 | /* |
| 6392 | A sequence with |
| 6393 | A wrapping bstr |
| 6394 | containing a map |
| 6395 | 1 |
| 6396 | 2 |
| 6397 | A wrapping bstr |
| 6398 | containing an array |
| 6399 | 3 |
| 6400 | wrapping bstr |
| 6401 | 4 |
| 6402 | 5 |
| 6403 | 6 |
| 6404 | array |
| 6405 | 7 |
| 6406 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6407 | */ |
| 6408 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6409 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6410 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6411 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6412 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6413 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6414 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6415 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6416 | |
| 6417 | QCBOREncode_BstrWrap(&EC); |
| 6418 | QCBOREncode_OpenMap(&EC); |
| 6419 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 6420 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 6421 | QCBOREncode_CloseMap(&EC); |
| 6422 | QCBOREncode_BstrWrap(&EC); |
| 6423 | QCBOREncode_OpenArray(&EC); |
| 6424 | QCBOREncode_AddInt64(&EC, 3); |
| 6425 | QCBOREncode_BstrWrap(&EC); |
| 6426 | QCBOREncode_AddInt64(&EC, 4); |
| 6427 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6428 | QCBOREncode_AddInt64(&EC, 5); |
| 6429 | QCBOREncode_CloseArray(&EC); |
| 6430 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6431 | QCBOREncode_AddInt64(&EC, 6); |
| 6432 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6433 | QCBOREncode_OpenArray(&EC); |
| 6434 | QCBOREncode_AddInt64(&EC, 7); |
| 6435 | QCBOREncode_AddInt64(&EC, 8); |
| 6436 | QCBOREncode_CloseArray(&EC); |
| 6437 | |
| 6438 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 6439 | if(uErr) { |
| 6440 | Encoded = NULLUsefulBufC; |
| 6441 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6442 | |
| 6443 | return Encoded; |
| 6444 | } |
| 6445 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6446 | /* h'FF' */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6447 | static const uint8_t spBreakInByteString[] = { |
| 6448 | 0x41, 0xff |
| 6449 | }; |
| 6450 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6451 | |
| 6452 | int32_t EnterBstrTest() |
| 6453 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 6454 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6455 | |
| 6456 | QCBORDecodeContext DC; |
| 6457 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6458 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6459 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6460 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6461 | |
| 6462 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6463 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6464 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6465 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 6466 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6467 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6468 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6469 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6470 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6471 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6472 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6473 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6474 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6475 | QCBORDecode_ExitArray(&DC); |
| 6476 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6477 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6478 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6479 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6480 | QCBORDecode_GetInt64(&DC, &n7); |
| 6481 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6482 | QCBORDecode_ExitArray(&DC); |
| 6483 | |
| 6484 | QCBORError uErr = QCBORDecode_Finish(&DC); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6485 | if(uErr) { |
| 6486 | return (int32_t)uErr; |
| 6487 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6488 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6489 | |
| 6490 | /* Enter and exit byte string wrapped CBOR that is bad. It has just a break. |
| 6491 | * Successful because no items are fetched from byte string. |
| 6492 | */ |
| 6493 | QCBORDecode_Init(&DC, |
| 6494 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 6495 | 0); |
| 6496 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6497 | uErr = QCBORDecode_GetError(&DC); |
| 6498 | if(uErr) { |
| 6499 | return 100 + (int32_t)uErr; |
| 6500 | } |
| 6501 | |
| 6502 | QCBORDecode_ExitBstrWrapped(&DC); |
| 6503 | uErr = QCBORDecode_GetError(&DC); |
| 6504 | if(uErr) { |
| 6505 | return 200 + (int32_t)uErr; |
| 6506 | } |
| 6507 | |
| 6508 | /* Try to get item that is a break out of a byte string wrapped CBOR. |
| 6509 | * It fails because there should be no break. |
| 6510 | */ |
| 6511 | QCBORDecode_Init(&DC, |
| 6512 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString), |
| 6513 | 0); |
| 6514 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6515 | QCBORItem Item; |
| 6516 | uErr = QCBORDecode_GetNext(&DC, &Item); |
| 6517 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 6518 | return 300 + (int32_t)uErr; |
| 6519 | } |
| 6520 | |
| 6521 | return 0; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6522 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6523 | |
| 6524 | |
| 6525 | |
| 6526 | |
| 6527 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6528 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6529 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6530 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6531 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6532 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 6533 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 6534 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6535 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6536 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6537 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 6538 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 6539 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6540 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6541 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6542 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6543 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6544 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6545 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6546 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6547 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6548 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6549 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6550 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6551 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6552 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 6553 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6554 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6555 | 21, |
| 6556 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 6557 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 6558 | |
| 6559 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6560 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6561 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6562 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6563 | |
| 6564 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6565 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6566 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6567 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6568 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6569 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6570 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6571 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6572 | |
| 6573 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6574 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6575 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6576 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6577 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6578 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6579 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 6580 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6581 | |
| 6582 | 0x18, 0x33, |
| 6583 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 6584 | |
| 6585 | // MIME |
| 6586 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6587 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 6588 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 6589 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6590 | |
| 6591 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6592 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6593 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6594 | |
| 6595 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6596 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 6597 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 6598 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6599 | |
| 6600 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6601 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6602 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6603 | |
| 6604 | // UUID |
| 6605 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6606 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 6607 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6608 | |
| 6609 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6610 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 6611 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6612 | }; |
| 6613 | |
| 6614 | int32_t DecodeTaggedTypeTests() |
| 6615 | { |
| 6616 | QCBORDecodeContext DC; |
| 6617 | QCBORError uErr; |
| 6618 | |
| 6619 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 6620 | |
| 6621 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6622 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6623 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6624 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6625 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6626 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6627 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 6628 | return 1; |
| 6629 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6630 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6631 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6632 | return 2; |
| 6633 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6634 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6635 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6636 | return 3; |
| 6637 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6638 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6639 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6640 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6641 | return 4; |
| 6642 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6643 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6644 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6645 | return 5; |
| 6646 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6647 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6648 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6649 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6650 | bNeg != false) { |
| 6651 | return 10; |
| 6652 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6653 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6654 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6655 | bNeg != true) { |
| 6656 | return 11; |
| 6657 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6658 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6659 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6660 | return 12; |
| 6661 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6662 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6663 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6664 | return 13; |
| 6665 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6666 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6667 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6668 | return 14; |
| 6669 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6670 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6671 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6672 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6673 | return 20; |
| 6674 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6675 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6676 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6677 | return 21; |
| 6678 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6679 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6680 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6681 | return 22; |
| 6682 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6683 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6684 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6685 | return 23; |
| 6686 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6687 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6688 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6689 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6690 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6691 | return 30; |
| 6692 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6693 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6694 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6695 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6696 | return 31; |
| 6697 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6698 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6699 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6700 | return 32; |
| 6701 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6702 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6703 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6704 | return 33; |
| 6705 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6706 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6707 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6708 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6709 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6710 | return 40; |
| 6711 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6712 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6713 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6714 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6715 | return 41; |
| 6716 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6717 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6718 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6719 | return 42; |
| 6720 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6721 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6722 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6723 | return 43; |
| 6724 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6725 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6726 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6727 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6728 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6729 | return 50; |
| 6730 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6731 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6732 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6733 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6734 | return 51; |
| 6735 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6736 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6737 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6738 | return 52; |
| 6739 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6740 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6741 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6742 | return 53; |
| 6743 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6744 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6745 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6746 | // MIME |
| 6747 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6748 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6749 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6750 | bIsNot7Bit == true) { |
| 6751 | return 60; |
| 6752 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6753 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6754 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6755 | bIsNot7Bit == true) { |
| 6756 | return 61; |
| 6757 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6758 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6759 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6760 | bIsNot7Bit == false) { |
| 6761 | return 62; |
| 6762 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6763 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6764 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6765 | bIsNot7Bit == false) { |
| 6766 | return 63; |
| 6767 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6768 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6769 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6770 | return 64; |
| 6771 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6772 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6773 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6774 | return 65; |
| 6775 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6776 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6777 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6778 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6779 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6780 | return 70; |
| 6781 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame^] | 6782 | #endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */ |
| 6783 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6784 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6785 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6786 | return 71; |
| 6787 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6788 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6789 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6790 | return 72; |
| 6791 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6792 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6793 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6794 | return 73; |
| 6795 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6796 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6797 | // Improvement: add some more error test cases |
| 6798 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6799 | QCBORDecode_ExitMap(&DC); |
| 6800 | |
| 6801 | uErr = QCBORDecode_Finish(&DC); |
| 6802 | if(uErr != QCBOR_SUCCESS) { |
| 6803 | return 100; |
| 6804 | } |
| 6805 | |
| 6806 | return 0; |
| 6807 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6808 | |
| 6809 | |
| 6810 | |
| 6811 | |
| 6812 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6813 | [ |
| 6814 | "aaaaaaaaaa", |
| 6815 | {} |
| 6816 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6817 | */ |
| 6818 | static const uint8_t spTooLarge1[] = { |
| 6819 | 0x9f, |
| 6820 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6821 | 0xa0, |
| 6822 | 0xff |
| 6823 | }; |
| 6824 | |
| 6825 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6826 | [ |
| 6827 | { |
| 6828 | 0: "aaaaaaaaaa" |
| 6829 | } |
| 6830 | ] |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6831 | */ |
| 6832 | static const uint8_t spTooLarge2[] = { |
| 6833 | 0x9f, |
| 6834 | 0xa1, |
| 6835 | 0x00, |
| 6836 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6837 | 0xff |
| 6838 | }; |
| 6839 | |
| 6840 | /* |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6841 | h'A1006A61616161616161616161' |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6842 | |
Laurence Lundblade | cc7da41 | 2020-12-27 00:09:07 -0800 | [diff] [blame] | 6843 | { |
| 6844 | 0: "aaaaaaaaaa" |
| 6845 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6846 | */ |
| 6847 | static const uint8_t spTooLarge3[] = { |
| 6848 | 0x4d, |
| 6849 | 0xa1, |
| 6850 | 0x00, |
| 6851 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6852 | }; |
| 6853 | |
| 6854 | int32_t TooLargeInputTest(void) |
| 6855 | { |
| 6856 | QCBORDecodeContext DC; |
| 6857 | QCBORError uErr; |
| 6858 | UsefulBufC String; |
| 6859 | |
| 6860 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 6861 | // to 10 There's not really any way to test this error |
| 6862 | // condition. The error condition is not complex, so setting |
| 6863 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 6864 | |
| 6865 | // The input CBOR is only too large because the |
| 6866 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 6867 | // |
| 6868 | // This test is disabled for the normal test runs because of the |
| 6869 | // special build requirement. |
| 6870 | |
| 6871 | |
| 6872 | // Tests the start of a map being too large |
| 6873 | 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] | 6874 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6875 | QCBORDecode_GetTextString(&DC, &String); |
| 6876 | uErr = QCBORDecode_GetError(&DC); |
| 6877 | if(uErr != QCBOR_SUCCESS) { |
| 6878 | return 1; |
| 6879 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6880 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6881 | uErr = QCBORDecode_GetError(&DC); |
| 6882 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 6883 | return 2; |
| 6884 | } |
| 6885 | |
| 6886 | // Tests the end of a map being too large |
| 6887 | 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] | 6888 | QCBORDecode_EnterArray(&DC, NULL); |
| 6889 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6890 | uErr = QCBORDecode_GetError(&DC); |
| 6891 | if(uErr != QCBOR_SUCCESS) { |
| 6892 | return 3; |
| 6893 | } |
| 6894 | QCBORDecode_ExitMap(&DC); |
| 6895 | uErr = QCBORDecode_GetError(&DC); |
| 6896 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 6897 | return 4; |
| 6898 | } |
| 6899 | |
| 6900 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 6901 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 6902 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6903 | uErr = QCBORDecode_GetError(&DC); |
| 6904 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 6905 | return 5; |
| 6906 | } |
| 6907 | |
| 6908 | return 0; |
| 6909 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6910 | |
| 6911 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6912 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6913 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6914 | static const uint8_t spMapWithIndefLenStrings[] = { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6915 | 0xa3, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6916 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 6917 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 6918 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 6919 | 0x03, |
| 6920 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 6921 | 0xc3, |
| 6922 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6923 | }; |
| 6924 | |
| 6925 | int32_t SpiffyIndefiniteLengthStringsTests() |
| 6926 | { |
| 6927 | QCBORDecodeContext DCtx; |
| 6928 | |
| 6929 | QCBORDecode_Init(&DCtx, |
| 6930 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 6931 | QCBOR_DECODE_MODE_NORMAL); |
| 6932 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 6933 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6934 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 6935 | |
| 6936 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6937 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6938 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
| 6939 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 6940 | return 1; |
| 6941 | } |
| 6942 | |
| 6943 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 6944 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 6945 | return 2; |
| 6946 | } |
| 6947 | |
| 6948 | uint64_t uInt; |
| 6949 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 6950 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 6951 | return 3; |
| 6952 | } |
| 6953 | if(uInt != 3) { |
| 6954 | return 4; |
| 6955 | } |
| 6956 | |
| 6957 | double uDouble; |
| 6958 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 6959 | "label2", |
| 6960 | 0xff, |
| 6961 | &uDouble); |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 6962 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6963 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 6964 | return 5; |
| 6965 | } |
| 6966 | if(uDouble != -16777474) { |
| 6967 | return 6; |
| 6968 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6969 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 6970 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 6971 | return 7; |
| 6972 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6973 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 6974 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6975 | |
| 6976 | QCBORDecode_ExitMap(&DCtx); |
| 6977 | |
| 6978 | if(QCBORDecode_Finish(&DCtx)) { |
| 6979 | return 99; |
| 6980 | } |
| 6981 | |
| 6982 | return 0; |
| 6983 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6984 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 6985 | |
| 6986 | |
| 6987 | |
| 6988 | int32_t PeekTest() |
| 6989 | { |
| 6990 | QCBORItem Item; |
| 6991 | QCBORError nCBORError; |
| 6992 | QCBORDecodeContext DCtx; |
| 6993 | |
| 6994 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6995 | |
| 6996 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6997 | return 100+(int32_t)nCBORError; |
| 6998 | } |
| 6999 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7000 | return 200; |
| 7001 | } |
| 7002 | |
| 7003 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7004 | return (int32_t)nCBORError; |
| 7005 | } |
| 7006 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7007 | return 300; |
| 7008 | } |
| 7009 | |
| 7010 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7011 | return 400 + (int32_t)nCBORError; |
| 7012 | } |
| 7013 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7014 | return 500; |
| 7015 | } |
| 7016 | |
| 7017 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7018 | return (int32_t)nCBORError; |
| 7019 | } |
| 7020 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 7021 | return 600; |
| 7022 | } |
| 7023 | |
| 7024 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7025 | return 900 + (int32_t)nCBORError; |
| 7026 | } |
| 7027 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7028 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7029 | Item.val.int64 != 42 || |
| 7030 | Item.uDataAlloc || |
| 7031 | Item.uLabelAlloc || |
| 7032 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7033 | return 1000; |
| 7034 | } |
| 7035 | |
| 7036 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7037 | return 1100 + (int32_t)nCBORError; |
| 7038 | } |
| 7039 | |
| 7040 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7041 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7042 | Item.val.int64 != 42 || |
| 7043 | Item.uDataAlloc || |
| 7044 | Item.uLabelAlloc || |
| 7045 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 7046 | return 1200; |
| 7047 | } |
| 7048 | |
| 7049 | |
| 7050 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7051 | return 1300 + (int32_t)nCBORError; |
| 7052 | } |
| 7053 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7054 | Item.uDataAlloc || |
| 7055 | Item.uLabelAlloc || |
| 7056 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 7057 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 7058 | Item.val.uCount != 2) |
| 7059 | return 1400; |
| 7060 | |
| 7061 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7062 | return 1500 + (int32_t)nCBORError; |
| 7063 | } |
| 7064 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7065 | Item.uDataAlloc || |
| 7066 | Item.uLabelAlloc || |
| 7067 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 7068 | return 1600; |
| 7069 | } |
| 7070 | |
| 7071 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7072 | return 1700 + (int32_t)nCBORError; |
| 7073 | } |
| 7074 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7075 | Item.uDataAlloc || |
| 7076 | Item.uLabelAlloc || |
| 7077 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7078 | return 1800; |
| 7079 | } |
| 7080 | |
| 7081 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7082 | return (int32_t)nCBORError; |
| 7083 | } |
| 7084 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7085 | Item.uDataAlloc || |
| 7086 | Item.uLabelAlloc || |
| 7087 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7088 | return 1900; |
| 7089 | } |
| 7090 | |
| 7091 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7092 | return (int32_t)nCBORError; |
| 7093 | } |
| 7094 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7095 | Item.uDataAlloc || |
| 7096 | Item.uLabelAlloc || |
| 7097 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 7098 | return 2000; |
| 7099 | } |
| 7100 | |
| 7101 | |
| 7102 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7103 | return 2100 + (int32_t)nCBORError; |
| 7104 | } |
| 7105 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7106 | Item.uDataAlloc || |
| 7107 | Item.uLabelAlloc || |
| 7108 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 7109 | Item.uDataType != QCBOR_TYPE_MAP || |
| 7110 | Item.val.uCount != 4) { |
| 7111 | return 2100; |
| 7112 | } |
| 7113 | |
| 7114 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7115 | return 2200 + (int32_t)nCBORError; |
| 7116 | } |
| 7117 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7118 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 7119 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7120 | Item.uDataAlloc || |
| 7121 | Item.uLabelAlloc || |
| 7122 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 7123 | return 2300; |
| 7124 | } |
| 7125 | |
| 7126 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7127 | return 2400 + (int32_t)nCBORError; |
| 7128 | } |
| 7129 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7130 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7131 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7132 | Item.uDataAlloc || |
| 7133 | Item.uLabelAlloc || |
| 7134 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7135 | return 2500; |
| 7136 | } |
| 7137 | |
| 7138 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7139 | return 2600 + (int32_t)nCBORError; |
| 7140 | } |
| 7141 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7142 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 7143 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 7144 | Item.uDataAlloc || |
| 7145 | Item.uLabelAlloc || |
| 7146 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 7147 | return 2700; |
| 7148 | } |
| 7149 | |
| 7150 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7151 | return 2800 + (int32_t)nCBORError; |
| 7152 | } |
| 7153 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7154 | Item.uDataAlloc || |
| 7155 | Item.uLabelAlloc || |
| 7156 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 7157 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 7158 | Item.val.int64 != 98) |
| 7159 | return 2900; |
| 7160 | |
| 7161 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 7162 | return 3000 + (int32_t)nCBORError; |
| 7163 | } |
| 7164 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7165 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7166 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7167 | Item.uDataAlloc || |
| 7168 | Item.uLabelAlloc || |
| 7169 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7170 | return 3100; |
| 7171 | } |
| 7172 | |
| 7173 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7174 | return 3200 + (int32_t)nCBORError; |
| 7175 | } |
| 7176 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7177 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7178 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7179 | Item.uDataAlloc || |
| 7180 | Item.uLabelAlloc || |
| 7181 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7182 | return 3300; |
| 7183 | } |
| 7184 | |
| 7185 | return 0; |
| 7186 | } |