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 | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 34 | #include "qcbor.h" |
Laurence Lundblade | d4728fd | 2018-12-17 15:15:56 -0800 | [diff] [blame] | 35 | #include <string.h> |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 36 | #include <math.h> // for fabs() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 37 | #include "not_well_formed_cbor.h" |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 38 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 39 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 40 | #ifdef PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 41 | #include <stdio.h> |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 42 | |
| 43 | static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 44 | { |
| 45 | if(szLabel) { |
| 46 | printf("%s ", szLabel); |
| 47 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 48 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 49 | size_t i; |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 50 | for(i = 0; i < Buf.len; i++) { |
| 51 | uint8_t Z = ((uint8_t *)Buf.ptr)[i]; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 52 | printf("%02x ", Z); |
| 53 | } |
| 54 | printf("\n"); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 55 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 56 | fflush(stdout); |
| 57 | } |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 58 | #endif |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 59 | |
| 60 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 61 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 62 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 63 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 64 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 65 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 66 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 67 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 68 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 69 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 70 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 71 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 72 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 73 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 74 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 75 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 76 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 77 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 78 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 79 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 80 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 81 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 82 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 83 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 84 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 85 | 0xff, 0xff}; |
| 86 | |
| 87 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 88 | // return CBOR error or -1 if type of value doesn't match |
| 89 | |
| 90 | static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
| 91 | { |
| 92 | QCBORItem Item; |
| 93 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 94 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 95 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 96 | return nCBORError; |
| 97 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 98 | return -1; |
| 99 | |
| 100 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 101 | return nCBORError; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 102 | if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix for 32-bit machines |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 103 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 104 | return -1; |
| 105 | |
| 106 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 107 | return nCBORError; |
| 108 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 109 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 110 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 111 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 112 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 113 | return nCBORError; |
| 114 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 115 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 116 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 117 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 118 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 119 | return nCBORError; |
| 120 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 121 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 122 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 123 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 124 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 125 | return nCBORError; |
| 126 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 127 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 128 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 129 | |
| 130 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 131 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 132 | return nCBORError; |
| 133 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 134 | Item.val.int64 != -2147483648) |
| 135 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 136 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 137 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 138 | return nCBORError; |
| 139 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 140 | Item.val.int64 != -2147483647) |
| 141 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 142 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 143 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 144 | return nCBORError; |
| 145 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 146 | Item.val.int64 != -65538) |
| 147 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 148 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 149 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 150 | return nCBORError; |
| 151 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 152 | Item.val.int64 != -65537) |
| 153 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 154 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 155 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 156 | return nCBORError; |
| 157 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 158 | Item.val.int64 != -65536) |
| 159 | return -1; |
| 160 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 161 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 162 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 163 | return nCBORError; |
| 164 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 165 | Item.val.int64 != -65535) |
| 166 | return -1; |
| 167 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 168 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 169 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 170 | return nCBORError; |
| 171 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 172 | Item.val.int64 != -65534) |
| 173 | return -1; |
| 174 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 175 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 176 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 177 | return nCBORError; |
| 178 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 179 | Item.val.int64 != -257) |
| 180 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 181 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 182 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 183 | return nCBORError; |
| 184 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 185 | Item.val.int64 != -256) |
| 186 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 187 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 188 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 189 | return nCBORError; |
| 190 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 191 | Item.val.int64 != -255) |
| 192 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 193 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 194 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 195 | return nCBORError; |
| 196 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 197 | Item.val.int64 != -254) |
| 198 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 199 | |
| 200 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 202 | return nCBORError; |
| 203 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 204 | Item.val.int64 != -25) |
| 205 | return -1; |
| 206 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 207 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 208 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 209 | return nCBORError; |
| 210 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 211 | Item.val.int64 != -24) |
| 212 | return -1; |
| 213 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 214 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 215 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 216 | return nCBORError; |
| 217 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 218 | Item.val.int64 != -23) |
| 219 | return -1; |
| 220 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 221 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 222 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 223 | return nCBORError; |
| 224 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 225 | Item.val.int64 != -1) |
| 226 | return -1; |
| 227 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 228 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 229 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 230 | return nCBORError; |
| 231 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 232 | Item.val.int64 != 0) |
| 233 | return -1; |
| 234 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 235 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 236 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 237 | return nCBORError; |
| 238 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 239 | Item.val.int64 != 0) |
| 240 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 241 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 242 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 243 | return nCBORError; |
| 244 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 245 | Item.val.int64 != 1) |
| 246 | return -1; |
| 247 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 248 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 249 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 250 | return nCBORError; |
| 251 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 252 | Item.val.int64 != 22) |
| 253 | return -1; |
| 254 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 255 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 256 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 257 | return nCBORError; |
| 258 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 259 | Item.val.int64 != 23) |
| 260 | return -1; |
| 261 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 262 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 263 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 264 | return nCBORError; |
| 265 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 266 | Item.val.int64 != 24) |
| 267 | return -1; |
| 268 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 269 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 270 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 271 | return nCBORError; |
| 272 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 273 | Item.val.int64 != 25) |
| 274 | return -1; |
| 275 | |
| 276 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 277 | return nCBORError; |
| 278 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 279 | Item.val.int64 != 26) |
| 280 | return -1; |
| 281 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 282 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 283 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 284 | return nCBORError; |
| 285 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 286 | Item.val.int64 != 254) |
| 287 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 288 | |
| 289 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 290 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 291 | return nCBORError; |
| 292 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 293 | Item.val.int64 != 255) |
| 294 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 295 | |
| 296 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 297 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 298 | return nCBORError; |
| 299 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 300 | Item.val.int64 != 256) |
| 301 | return -1; |
| 302 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 303 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 304 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 305 | return nCBORError; |
| 306 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 307 | Item.val.int64 != 257) |
| 308 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 309 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 310 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 311 | return nCBORError; |
| 312 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 313 | Item.val.int64 != 65534) |
| 314 | return -1; |
| 315 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 316 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 317 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 318 | return nCBORError; |
| 319 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 320 | Item.val.int64 != 65535) |
| 321 | return -1; |
| 322 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 323 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 324 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 325 | return nCBORError; |
| 326 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 327 | Item.val.int64 != 65536) |
| 328 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 329 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 330 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 331 | return nCBORError; |
| 332 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 333 | Item.val.int64 != 65537) |
| 334 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 335 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 336 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 337 | return nCBORError; |
| 338 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 339 | Item.val.int64 != 65538) |
| 340 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 341 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 342 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 343 | return nCBORError; |
| 344 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 345 | Item.val.int64 != 2147483647) |
| 346 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 347 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 348 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 349 | return nCBORError; |
| 350 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 351 | Item.val.int64 != 2147483647) |
| 352 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 353 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 354 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 355 | return nCBORError; |
| 356 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 357 | Item.val.int64 != 2147483648) |
| 358 | return -1; |
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))) |
| 361 | return nCBORError; |
| 362 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 363 | Item.val.int64 != 2147483649) |
| 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))) |
| 367 | return nCBORError; |
| 368 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 369 | Item.val.int64 != 4294967294) |
| 370 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 371 | |
| 372 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 373 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 374 | return nCBORError; |
| 375 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 376 | Item.val.int64 != 4294967295) |
| 377 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 378 | |
| 379 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 380 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 381 | return nCBORError; |
| 382 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 383 | Item.val.int64 != 4294967296) |
| 384 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 385 | |
| 386 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 387 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 388 | return nCBORError; |
| 389 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 390 | Item.val.int64 != 4294967297) |
| 391 | return -1; |
| 392 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 393 | |
| 394 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 395 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 396 | return nCBORError; |
| 397 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 398 | Item.val.int64 != 9223372036854775807LL) |
| 399 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 400 | |
| 401 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 402 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 403 | return nCBORError; |
| 404 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 405 | Item.val.uint64 != 18446744073709551615ULL) |
| 406 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 407 | |
| 408 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 409 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 410 | return -1; |
| 411 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 412 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 417 | // The largest negative int possible in CBOR. |
| 418 | // Not possible in C. |
| 419 | static const uint8_t spTooBigNegative[] = { |
| 420 | 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 421 | }; |
| 422 | |
| 423 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 424 | /* |
| 425 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 426 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 427 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 428 | int IntegerValuesParseTest() |
| 429 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 430 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 431 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 432 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 433 | QCBORDecode_Init(&DCtx, |
| 434 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 435 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 436 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 437 | // The really big test of all successes |
| 438 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 439 | if(nReturn) { |
| 440 | return nReturn; |
| 441 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 442 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 443 | // The one large negative integer that can be parsed |
| 444 | QCBORDecode_Init(&DCtx, |
| 445 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooBigNegative), |
| 446 | QCBOR_DECODE_MODE_NORMAL); |
| 447 | |
| 448 | QCBORItem item; |
| 449 | if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) { |
| 450 | nReturn = -4000; |
| 451 | } |
| 452 | |
| 453 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 458 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 459 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 460 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 461 | 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] | 462 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 463 | */ |
| 464 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 465 | static uint8_t spSimpleArrayBuffer[50]; |
| 466 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 467 | static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen) |
| 468 | { |
| 469 | QCBOREncodeContext ECtx; |
| 470 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 471 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 472 | *pEncoded = NULL; |
| 473 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 474 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 475 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 476 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 477 | // and last with the buffer to do the actual encoding |
| 478 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 479 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 480 | QCBOREncode_OpenArray(&ECtx); |
| 481 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 482 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 483 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 484 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 485 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 486 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 487 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 488 | goto Done; |
| 489 | |
| 490 | if(*pEncoded != NULL) { |
| 491 | nReturn = 0; |
| 492 | goto Done; |
| 493 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 494 | |
| 495 | // Use static buffer to avoid dependency on malloc() |
| 496 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 497 | goto Done; |
| 498 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 499 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 500 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 501 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 502 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 503 | Done: |
| 504 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 508 | /* |
| 509 | {"first integer": 42, |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 510 | "an array of two strings": [ |
| 511 | "string1", "string2" |
| 512 | ], |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 513 | "map in a map": { |
| 514 | "bytes 1": h'78787878', |
| 515 | "bytes 2": h'79797979', |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 516 | "another int": 98, |
| 517 | "text 2": "lies, damn lies and statistics" |
| 518 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 519 | } |
| 520 | */ |
| 521 | |
| 522 | static uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 523 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 524 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 525 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 526 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 527 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 528 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 529 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 530 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 531 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 532 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 533 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 534 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 535 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 536 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 537 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 538 | 0x73 } ; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 539 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 540 | static int ParseOrderedArray(const uint8_t *pEncoded, |
| 541 | size_t nLen, |
| 542 | int64_t *pInt1, |
| 543 | int64_t *pInt2, |
| 544 | const uint8_t **pBuf3, |
| 545 | size_t *pBuf3Len, |
| 546 | const uint8_t **pBuf4, |
| 547 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 548 | { |
| 549 | QCBORDecodeContext DCtx; |
| 550 | QCBORItem Item; |
| 551 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 552 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 553 | QCBORDecode_Init(&DCtx, |
| 554 | (UsefulBufC){pEncoded, nLen}, |
| 555 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 556 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 557 | // Make sure the first thing is a map |
| 558 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY) |
| 559 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 560 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 561 | // First integer |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 562 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 563 | goto Done; |
| 564 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 565 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 566 | // Second integer |
| 567 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64) |
| 568 | goto Done; |
| 569 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 570 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 571 | // First string |
| 572 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING) |
| 573 | goto Done; |
| 574 | *pBuf3 = Item.val.string.ptr; |
| 575 | *pBuf3Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 576 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 577 | // Second string |
| 578 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING) |
| 579 | goto Done; |
| 580 | *pBuf4 = Item.val.string.ptr; |
| 581 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 582 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 583 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 584 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 585 | Done: |
| 586 | return(nReturn); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 591 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 592 | int SimpleArrayTest() |
| 593 | { |
| 594 | uint8_t *pEncoded; |
| 595 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 596 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 597 | int64_t i1=0, i2=0; |
| 598 | size_t i3=0, i4=0; |
| 599 | const uint8_t *s3= (uint8_t *)""; |
| 600 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 601 | |
| 602 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 603 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 604 | return(-1); |
| 605 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 606 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 607 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 608 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 609 | if(i1 != 23 || |
| 610 | i2 != 6000 || |
| 611 | i3 != 8 || |
| 612 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 613 | memcmp("galactic", s3, 8) !=0 || |
| 614 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 615 | return(-1); |
| 616 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 617 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 618 | return(0); |
| 619 | } |
| 620 | |
| 621 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 622 | /* |
| 623 | [ |
| 624 | 0, |
| 625 | [], |
| 626 | [ |
| 627 | [], |
| 628 | [ |
| 629 | 0 |
| 630 | ], |
| 631 | {}, |
| 632 | { |
| 633 | 1: {}, |
| 634 | 2: {}, |
| 635 | 3: [] |
| 636 | } |
| 637 | ] |
| 638 | ] |
| 639 | */ |
| 640 | static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 641 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
| 642 | |
| 643 | int EmptyMapsAndArraysTest() |
| 644 | { |
| 645 | QCBORDecodeContext DCtx; |
| 646 | QCBORItem Item; |
| 647 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 648 | QCBORDecode_Init(&DCtx, |
| 649 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 650 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 651 | |
| 652 | // Array with 3 items |
| 653 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 654 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 655 | Item.uNestingLevel != 0 || |
| 656 | Item.uNextNestLevel != 1 || |
| 657 | Item.val.uCount != 3) { |
| 658 | return -1; |
| 659 | } |
| 660 | |
| 661 | // An integer 0 |
| 662 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 663 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 664 | Item.uNestingLevel != 1 || |
| 665 | Item.uNextNestLevel != 1 || |
| 666 | Item.val.uint64 != 0) { |
| 667 | return -2; |
| 668 | } |
| 669 | |
| 670 | // An empty array |
| 671 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 672 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 673 | Item.uNestingLevel != 1 || |
| 674 | Item.uNextNestLevel != 1 || |
| 675 | Item.val.uCount != 0) { |
| 676 | return -3; |
| 677 | } |
| 678 | |
| 679 | // An array with 4 items |
| 680 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 681 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 682 | Item.uNestingLevel != 1 || |
| 683 | Item.uNextNestLevel != 2 || |
| 684 | Item.val.uCount != 4) { |
| 685 | return -4; |
| 686 | } |
| 687 | |
| 688 | // An empty array |
| 689 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 690 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 691 | Item.uNestingLevel != 2 || |
| 692 | Item.uNextNestLevel != 2 || |
| 693 | Item.val.uCount != 0) { |
| 694 | return -5; |
| 695 | } |
| 696 | |
| 697 | // An array with 1 item |
| 698 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 699 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 700 | Item.uNestingLevel != 2 || |
| 701 | Item.uNextNestLevel != 3 || |
| 702 | Item.val.uCount != 1) { |
| 703 | return -6; |
| 704 | } |
| 705 | |
| 706 | // An integer 0 |
| 707 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 708 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 709 | Item.uNestingLevel != 3 || |
| 710 | Item.uNextNestLevel != 2 || |
| 711 | Item.val.uint64 != 0) { |
| 712 | return -7; |
| 713 | } |
| 714 | |
| 715 | // An empty map |
| 716 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 717 | Item.uDataType != QCBOR_TYPE_MAP || |
| 718 | Item.uNestingLevel != 2 || |
| 719 | Item.uNextNestLevel != 2 || |
| 720 | Item.val.uCount != 0) { |
| 721 | return -8; |
| 722 | } |
| 723 | |
| 724 | // An map with 3 items |
| 725 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 726 | Item.uDataType != QCBOR_TYPE_MAP || |
| 727 | Item.uNestingLevel != 2 || |
| 728 | Item.uNextNestLevel != 3 || |
| 729 | Item.val.uCount != 3) { |
| 730 | return -9; |
| 731 | } |
| 732 | |
| 733 | // An empty map |
| 734 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 735 | Item.uDataType != QCBOR_TYPE_MAP || |
| 736 | Item.uNestingLevel != 3 || |
| 737 | Item.uNextNestLevel != 3 || |
| 738 | Item.val.uCount != 0) { |
| 739 | return -10; |
| 740 | } |
| 741 | |
| 742 | // An empty map |
| 743 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 744 | Item.uDataType != QCBOR_TYPE_MAP || |
| 745 | Item.uNestingLevel != 3 || |
| 746 | Item.uNextNestLevel != 3 || |
| 747 | Item.val.uCount != 0) { |
| 748 | return -11; |
| 749 | } |
| 750 | |
| 751 | // An empty array |
| 752 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 753 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 754 | Item.uNestingLevel != 3 || |
| 755 | Item.uNextNestLevel != 0 || |
| 756 | Item.val.uCount != 0) { |
| 757 | return -12; |
| 758 | } |
| 759 | |
| 760 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 761 | return -13; |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 767 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 768 | static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 769 | 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 770 | |
| 771 | int ParseDeepArrayTest() |
| 772 | { |
| 773 | QCBORDecodeContext DCtx; |
| 774 | int nReturn = 0; |
| 775 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 776 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 777 | QCBORDecode_Init(&DCtx, |
| 778 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 779 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 780 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 781 | for(i = 0; i < 10; i++) { |
| 782 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 783 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 784 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 785 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 786 | Item.uNestingLevel != i) { |
| 787 | nReturn = -1; |
| 788 | break; |
| 789 | } |
| 790 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 791 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 792 | return(nReturn); |
| 793 | } |
| 794 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 795 | // Big enough to test nesting to the depth of 24 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 796 | static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 797 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 798 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 799 | 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 800 | |
| 801 | int ParseTooDeepArrayTest() |
| 802 | { |
| 803 | QCBORDecodeContext DCtx; |
| 804 | int nReturn = 0; |
| 805 | int i; |
| 806 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 807 | |
| 808 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 809 | QCBORDecode_Init(&DCtx, |
| 810 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 811 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 812 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 813 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 814 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 815 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 816 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 817 | Item.uNestingLevel != i) { |
| 818 | nReturn = -1; |
| 819 | break; |
| 820 | } |
| 821 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 822 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 823 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) |
| 824 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 825 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 826 | return(nReturn); |
| 827 | } |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 832 | int ShortBufferParseTest() |
| 833 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 834 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 835 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 836 | for(int nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
| 837 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 838 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 839 | QCBORDecode_Init(&DCtx, |
| 840 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 841 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 842 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 843 | const QCBORError nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 844 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 845 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 846 | nResult = -1; |
| 847 | goto Done; |
| 848 | } |
| 849 | } |
| 850 | Done: |
| 851 | return nResult; |
| 852 | } |
| 853 | |
| 854 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 855 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 856 | int ShortBufferParseTest2() |
| 857 | { |
| 858 | uint8_t *pEncoded; |
| 859 | int nReturn; |
| 860 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 861 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 862 | int64_t i1, i2; |
| 863 | size_t i3, i4; |
| 864 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 865 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 866 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 867 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 868 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 869 | return(-1); |
| 870 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 871 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 872 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
| 873 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
| 874 | if(nResult == 0) { |
| 875 | nReturn = -1; |
| 876 | } |
| 877 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 878 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 879 | return(nReturn); |
| 880 | } |
| 881 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 882 | /* |
| 883 | Decode and thoroughly check a moderately complex |
| 884 | set of maps |
| 885 | */ |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 886 | static int ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 887 | { |
| 888 | QCBORDecodeContext DCtx; |
| 889 | QCBORItem Item; |
| 890 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 891 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 892 | QCBORDecode_Init(&DCtx, |
| 893 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 894 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 895 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 896 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 897 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 898 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 899 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 900 | Item.val.uCount != 3) |
| 901 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 902 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 903 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 904 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 905 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 906 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 907 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 908 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 909 | Item.uDataAlloc || |
| 910 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 911 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 912 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 913 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 914 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 915 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 916 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 917 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 918 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 919 | Item.uDataAlloc || |
| 920 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 921 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 922 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 923 | Item.val.uCount != 2) |
| 924 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 925 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 926 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 927 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 928 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 929 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 930 | Item.uDataAlloc || |
| 931 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 932 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 933 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 934 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 935 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 936 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 937 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 938 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 939 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 940 | Item.uDataAlloc || |
| 941 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 942 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 943 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 944 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 945 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 946 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 947 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 948 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 949 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 950 | Item.uDataAlloc || |
| 951 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 952 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 953 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 954 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 955 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 956 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 957 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 958 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 959 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 960 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 961 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 962 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 963 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 964 | Item.uDataAlloc || |
| 965 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 966 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 967 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 968 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 969 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 970 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 971 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 972 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 973 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 974 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 975 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 976 | Item.uDataAlloc || |
| 977 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 978 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 979 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 980 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 981 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 982 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 983 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 984 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 985 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 986 | Item.uDataAlloc || |
| 987 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 988 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 989 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 990 | Item.val.int64 != 98) |
| 991 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 992 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 993 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 994 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 995 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 996 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 997 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 998 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 999 | Item.uDataAlloc || |
| 1000 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1001 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1002 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1003 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1004 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1009 | /* |
| 1010 | Decode and thoroughly check a moderately complex |
| 1011 | set of maps |
| 1012 | */ |
| 1013 | int ParseMapAsArrayTest() |
| 1014 | { |
| 1015 | QCBORDecodeContext DCtx; |
| 1016 | QCBORItem Item; |
| 1017 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1018 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1019 | QCBORDecode_Init(&DCtx, |
| 1020 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1021 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1022 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1023 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1024 | return nCBORError; |
| 1025 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1026 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1027 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1028 | return -1; |
| 1029 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1030 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1031 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1032 | return nCBORError; |
| 1033 | } |
| 1034 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1035 | Item.uDataAlloc || |
| 1036 | Item.uLabelAlloc || |
| 1037 | Item.uLabelType != QCBOR_TYPE_NONE || |
| 1038 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) { |
| 1039 | return -2; |
| 1040 | } |
| 1041 | |
| 1042 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1043 | return nCBORError; |
| 1044 | } |
| 1045 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1046 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1047 | Item.val.int64 != 42 || |
| 1048 | Item.uDataAlloc || |
| 1049 | Item.uLabelAlloc) { |
| 1050 | return -3; |
| 1051 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1052 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1053 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1054 | return nCBORError; |
| 1055 | } |
| 1056 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1057 | Item.uDataAlloc || |
| 1058 | Item.uLabelAlloc || |
| 1059 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) || |
| 1060 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1061 | return -4; |
| 1062 | } |
| 1063 | |
| 1064 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1065 | return nCBORError; |
| 1066 | } |
| 1067 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1068 | Item.uDataAlloc || |
| 1069 | Item.uLabelAlloc || |
| 1070 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1071 | Item.val.uCount != 2) { |
| 1072 | return -5; |
| 1073 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1074 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1075 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1076 | return nCBORError; |
| 1077 | } |
| 1078 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1079 | Item.val.string.len != 7 || |
| 1080 | Item.uDataAlloc || |
| 1081 | Item.uLabelAlloc || |
| 1082 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1083 | return -6; |
| 1084 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1085 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1086 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1087 | return nCBORError; |
| 1088 | } |
| 1089 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1090 | Item.uDataAlloc || |
| 1091 | Item.uLabelAlloc || |
| 1092 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1093 | return -7; |
| 1094 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1095 | |
| 1096 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1097 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1098 | return nCBORError; |
| 1099 | } |
| 1100 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1101 | Item.uDataAlloc || |
| 1102 | Item.uLabelAlloc || |
| 1103 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) { |
| 1104 | return -8; |
| 1105 | } |
| 1106 | |
| 1107 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1108 | return nCBORError; |
| 1109 | } |
| 1110 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1111 | Item.uDataAlloc || |
| 1112 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1113 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1114 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1115 | return -9; |
| 1116 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1117 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1118 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1119 | return nCBORError; |
| 1120 | } |
| 1121 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1122 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 1123 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1124 | Item.uDataAlloc || |
| 1125 | Item.uLabelAlloc) { |
| 1126 | return -10; |
| 1127 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1128 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1129 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1130 | return nCBORError; |
| 1131 | } |
| 1132 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1133 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1134 | Item.uDataAlloc || |
| 1135 | Item.uLabelAlloc || |
| 1136 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
| 1137 | return -11; |
| 1138 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1139 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1140 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1141 | return nCBORError; |
| 1142 | } |
| 1143 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1144 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) || |
| 1145 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1146 | Item.uDataAlloc || |
| 1147 | Item.uLabelAlloc) { |
| 1148 | return -12; |
| 1149 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1150 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1151 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1152 | return nCBORError; |
| 1153 | } |
| 1154 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1155 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1156 | Item.uDataAlloc || |
| 1157 | Item.uLabelAlloc || |
| 1158 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
| 1159 | return -13; |
| 1160 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1161 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1162 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1163 | return nCBORError; |
| 1164 | } |
| 1165 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1166 | Item.uDataAlloc || |
| 1167 | Item.uLabelAlloc || |
| 1168 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) || |
| 1169 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1170 | return -14; |
| 1171 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1172 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1173 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1174 | return nCBORError; |
| 1175 | } |
| 1176 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1177 | Item.uDataAlloc || |
| 1178 | Item.uLabelAlloc || |
| 1179 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1180 | Item.val.int64 != 98) { |
| 1181 | return -15; |
| 1182 | } |
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))) { |
| 1185 | return nCBORError; |
| 1186 | } |
| 1187 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1188 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))|| |
| 1189 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1190 | Item.uDataAlloc || |
| 1191 | Item.uLabelAlloc) { |
| 1192 | return -16; |
| 1193 | } |
| 1194 | |
| 1195 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1196 | return nCBORError; |
| 1197 | } |
| 1198 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1199 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1200 | Item.uDataAlloc || |
| 1201 | Item.uLabelAlloc || |
| 1202 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
| 1203 | return -17; |
| 1204 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1205 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1206 | return 0; |
| 1207 | } |
| 1208 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1209 | |
| 1210 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1211 | Fully or partially decode pValidMapEncoded. When |
| 1212 | partially decoding check for the right error code. |
| 1213 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1214 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1215 | The partial decodes test error conditions of |
| 1216 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1217 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1218 | This could be combined with the above test |
| 1219 | and made prettier and maybe a little more |
| 1220 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1221 | */ |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1222 | static int ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1223 | { |
| 1224 | QCBORDecodeContext DCtx; |
| 1225 | QCBORItem Item; |
| 1226 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1227 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1228 | QCBORDecode_Init(&DCtx, |
| 1229 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1230 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1231 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1232 | if(nLevel < 1) { |
| 1233 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1234 | return -1; |
| 1235 | } else { |
| 1236 | return 0; |
| 1237 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1238 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1239 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1240 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1241 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1242 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1243 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1244 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1245 | Item.val.uCount != 3) |
| 1246 | return -1; |
| 1247 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1248 | if(nLevel < 2) { |
| 1249 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1250 | return -1; |
| 1251 | } else { |
| 1252 | return 0; |
| 1253 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1254 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1255 | |
| 1256 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1257 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1258 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1259 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1260 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1261 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1262 | Item.val.uCount != 42 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1263 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1264 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1265 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1266 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1267 | if(nLevel < 3) { |
| 1268 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1269 | return -1; |
| 1270 | } else { |
| 1271 | return 0; |
| 1272 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1273 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1274 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1275 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1276 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1277 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1278 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1279 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1280 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1281 | Item.val.uCount != 2) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1282 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1283 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1284 | |
| 1285 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1286 | if(nLevel < 4) { |
| 1287 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1288 | return -1; |
| 1289 | } else { |
| 1290 | return 0; |
| 1291 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1292 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1293 | |
| 1294 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1295 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1296 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1297 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1298 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1299 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1300 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1301 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1302 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1303 | if(nLevel < 5) { |
| 1304 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1305 | return -1; |
| 1306 | } else { |
| 1307 | return 0; |
| 1308 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1309 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1310 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1311 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1312 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1313 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1314 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1315 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1316 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1317 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1318 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1319 | if(nLevel < 6) { |
| 1320 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1321 | return -1; |
| 1322 | } else { |
| 1323 | return 0; |
| 1324 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1325 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1326 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1327 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1328 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1329 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1330 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1331 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1332 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1333 | Item.val.uCount != 4) |
| 1334 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1335 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1336 | if(nLevel < 7) { |
| 1337 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1338 | return -1; |
| 1339 | } else { |
| 1340 | return 0; |
| 1341 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1342 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1343 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1344 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1345 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1346 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1347 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1348 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1349 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1350 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1351 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1352 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1353 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1354 | if(nLevel < 8) { |
| 1355 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1356 | return -1; |
| 1357 | } else { |
| 1358 | return 0; |
| 1359 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1360 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1361 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1362 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1363 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1364 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1365 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1366 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1367 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1368 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1369 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1370 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1371 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1372 | if(nLevel < 9) { |
| 1373 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1374 | return -1; |
| 1375 | } else { |
| 1376 | return 0; |
| 1377 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1378 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1379 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1380 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1381 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1382 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1383 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1384 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1385 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1386 | Item.val.int64 != 98) |
| 1387 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1388 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1389 | if(nLevel < 10) { |
| 1390 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1391 | return -1; |
| 1392 | } else { |
| 1393 | return 0; |
| 1394 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1395 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1396 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1397 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1398 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1399 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1400 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1401 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1402 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1403 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1404 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1405 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1406 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1407 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1408 | return -1; |
| 1409 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1410 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1411 | return 0; |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | int ParseMapTest() |
| 1418 | { |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1419 | // Parse a moderatly complex map structure very thoroughl |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1420 | int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 1421 | |
| 1422 | n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 1423 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1424 | if(!n) { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1425 | for(int i = 0; i < 10; i++) { |
| 1426 | n = ExtraBytesTest(i); |
| 1427 | if(n) { |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1431 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1432 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1433 | return(n); |
| 1434 | } |
| 1435 | |
| 1436 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1437 | static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, |
| 1438 | 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, |
| 1439 | 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1440 | |
| 1441 | int ParseSimpleTest() |
| 1442 | { |
| 1443 | QCBORDecodeContext DCtx; |
| 1444 | QCBORItem Item; |
| 1445 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1446 | |
| 1447 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1448 | QCBORDecode_Init(&DCtx, |
| 1449 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1450 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1451 | |
| 1452 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1453 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1454 | return nCBORError; |
| 1455 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1456 | Item.val.uCount != 10) |
| 1457 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1458 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1459 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1460 | return nCBORError; |
| 1461 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1462 | return -1; |
| 1463 | |
| 1464 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1465 | return nCBORError; |
| 1466 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1467 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1468 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1469 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1470 | return nCBORError; |
| 1471 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1472 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1473 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1474 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1475 | return nCBORError; |
| 1476 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1477 | return -1; |
| 1478 | |
| 1479 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1480 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1481 | return -1; |
| 1482 | |
| 1483 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1484 | return nCBORError; |
| 1485 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1486 | return -1; |
| 1487 | |
| 1488 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1489 | return nCBORError; |
| 1490 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1491 | return -1; |
| 1492 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1493 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1494 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1495 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1496 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1497 | return -1; |
| 1498 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1499 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1500 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1501 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1502 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1503 | return nCBORError; |
| 1504 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1505 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1506 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1507 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1508 | return nCBORError; |
| 1509 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1510 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1511 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1512 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1513 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1517 | static int IsNotWellFormedError(QCBORError nErr) |
| 1518 | { |
| 1519 | switch(nErr){ |
| 1520 | case QCBOR_ERR_INDEFINITE_STRING_CHUNK: |
| 1521 | case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN: |
| 1522 | case QCBOR_ERR_UNSUPPORTED: |
| 1523 | case QCBOR_ERR_HIT_END: |
| 1524 | case QCBOR_ERR_BAD_TYPE_7: |
| 1525 | case QCBOR_ERR_BAD_BREAK: |
| 1526 | case QCBOR_ERR_EXTRA_BYTES: |
| 1527 | case QCBOR_ERR_BAD_INT: |
| 1528 | return 1; |
| 1529 | default: |
| 1530 | return 0; |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | int NotWellFormedTests() |
| 1536 | { |
| 1537 | // Loop over all the not-well-formed instance of CBOR |
| 1538 | // that are test vectors in not_well_formed_cbor.h |
| 1539 | const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes); |
| 1540 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 1541 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 1542 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 1543 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1544 | // Set up decoder context. String allocator needed for indefinite |
| 1545 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1546 | QCBORDecodeContext DCtx; |
| 1547 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
| 1548 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1549 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1550 | |
| 1551 | // Loop getting items until no more to get |
| 1552 | QCBORError nCBORError; |
| 1553 | do { |
| 1554 | QCBORItem Item; |
| 1555 | |
| 1556 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1557 | } while(nCBORError == QCBOR_SUCCESS); |
| 1558 | |
| 1559 | // Every test vector must fail with |
| 1560 | // a not-well-formed error. If not |
| 1561 | // this test fails. |
| 1562 | if(!IsNotWellFormedError(nCBORError)) { |
| 1563 | // Return index of failure in the error code |
| 1564 | return 2000 + nIterate; |
| 1565 | } |
| 1566 | } |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1571 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1572 | UsefulBufC Input; |
| 1573 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1574 | }; |
| 1575 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1576 | |
| 1577 | static int ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails) |
| 1578 | { |
| 1579 | for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
| 1580 | // Set up the decoding context including a memory pool so that |
| 1581 | // indefinite length items can be checked |
| 1582 | QCBORDecodeContext DCtx; |
| 1583 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
| 1584 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1585 | QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1586 | if(nCBORError) { |
| 1587 | return -9; |
| 1588 | } |
| 1589 | |
| 1590 | // Iterate until there is an error of some sort error |
| 1591 | QCBORItem Item; |
| 1592 | do { |
| 1593 | // Set to something none-zero other than QCBOR_TYPE_NONE |
| 1594 | memset(&Item, 0x33, sizeof(Item)); |
| 1595 | |
| 1596 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1597 | } while(nCBORError == QCBOR_SUCCESS); |
| 1598 | |
| 1599 | // Must get the expected error or the this test fails |
| 1600 | // The data and label type must also be QCBOR_TYPE_NONE |
| 1601 | if(nCBORError != pF->nError || |
| 1602 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1603 | Item.uLabelType != QCBOR_TYPE_NONE) { |
| 1604 | // return index of CBOR + 1000 |
| 1605 | return (int)(pF - pFailInputs) * 100 + nCBORError; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1613 | struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1614 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1615 | // returned is also checked. |
| 1616 | |
| 1617 | // Indefinite length strings must be closed off |
| 1618 | // An indefinite length byte string not closed off |
| 1619 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1620 | // An indefinite length text string not closed off |
| 1621 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1622 | |
| 1623 | |
| 1624 | // All the chunks in an indefinite length string must be of the type of indefinite length string |
| 1625 | // indefinite length byte string with text string chunk |
| 1626 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1627 | // indefinite length text string with a byte string chunk |
| 1628 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1629 | // indefinite length byte string with an positive integer chunk |
| 1630 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1631 | // indefinite length byte string with an negative integer chunk |
| 1632 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1633 | // indefinite length byte string with an array chunk |
| 1634 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1635 | // indefinite length byte string with an map chunk |
| 1636 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1637 | // indefinite length byte string with tagged integer chunk |
| 1638 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1639 | // indefinite length byte string with an simple type chunk |
| 1640 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1641 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1642 | // indefinite length text string with indefinite string inside |
| 1643 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1644 | |
| 1645 | |
| 1646 | // Definte length maps and arrays must be closed by having the right number of items |
| 1647 | // A definte length array that is supposed to have 1 item, but has none |
| 1648 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END }, |
| 1649 | // A definte length array that is supposed to have 2 items, but has only 1 |
| 1650 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1651 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1652 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1653 | // A definte length map that is supposed to have 1 item, but has none |
| 1654 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END }, |
| 1655 | // A definte length map that is supposed to have s item, but has only 1 |
| 1656 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1657 | |
| 1658 | |
| 1659 | // Indefinte length maps and arrays must be ended by a break |
| 1660 | // Indefinite length array with zero items and no break |
| 1661 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END }, |
| 1662 | // Indefinite length array with two items and no break |
| 1663 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1664 | // Indefinite length map with zero items and no break |
| 1665 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END }, |
| 1666 | // Indefinite length map with two items and no break |
| 1667 | { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END }, |
| 1668 | |
| 1669 | |
| 1670 | // Nested maps and arrays must be closed off (some extra nested test vectors) |
| 1671 | // Unclosed indefinite array containing a close definite array |
| 1672 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1673 | // Definite length array containing an unclosed indefinite array |
| 1674 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END }, |
| 1675 | // Deeply nested definite length arrays with deepest one unclosed |
| 1676 | { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END }, |
| 1677 | // Deeply nested indefinite length arrays with deepest one unclosed |
| 1678 | { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END }, |
| 1679 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1680 | // TODO: think through this one |
| 1681 | { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1682 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1683 | // TODO: think through this one |
| 1684 | { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1685 | |
| 1686 | |
| 1687 | // The "argument" for the data item is incomplete |
| 1688 | // Positive integer missing 1 byte argument |
| 1689 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1690 | // Positive integer missing 2 byte argument |
| 1691 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1692 | // Positive integer missing 4 byte argument |
| 1693 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1694 | // Positive integer missing 8 byte argument |
| 1695 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1696 | // Positive integer missing 1 byte of 2 byte argument |
| 1697 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1698 | // Positive integer missing 2 bytes of 4 byte argument |
| 1699 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1700 | // Positive integer missing 1 bytes of 7 byte argument |
| 1701 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1702 | // Negative integer missing 1 byte argument |
| 1703 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1704 | // Binary string missing 1 byte argument |
| 1705 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1706 | // Text string missing 1 byte argument |
| 1707 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 1708 | // Array missing 1 byte argument |
| 1709 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 1710 | // Map missing 1 byte argument |
| 1711 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 1712 | // Tag missing 1 byte argument |
| 1713 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 1714 | // Simple missing 1 byte argument |
| 1715 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
| 1716 | |
| 1717 | |
| 1718 | // Breaks must not occur in definite length arrays and maps |
| 1719 | // Array of length 1 with sole member replaced by a break |
| 1720 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1721 | // Array of length 2 with 2nd member replaced by a break |
| 1722 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1723 | // Map of length 1 with sole member label replaced by a break |
| 1724 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1725 | // Map of length 1 with sole member label replaced by break |
| 1726 | // Alternate representation that some decoders handle difference |
| 1727 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1728 | // Array of length 1 with 2nd member value replaced by a break |
| 1729 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1730 | // Map of length 2 with 2nd member replaced by a break |
| 1731 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 1732 | |
| 1733 | |
| 1734 | // Breaks must not occur on their own out of an indefinite length data item |
| 1735 | // A bare break is not well formed |
| 1736 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 1737 | // A bare break after a zero length definite length array |
| 1738 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1739 | // A bare break after a zero length indefinite length map |
| 1740 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1741 | |
| 1742 | |
| 1743 | // Forbidden two byte encodings of simple types |
| 1744 | // Must use 0xe0 instead |
| 1745 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1746 | // Should use 0xe1 instead |
| 1747 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1748 | // Should use 0xe2 instead |
| 1749 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1750 | // Should use 0xe3 instead |
| 1751 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1752 | // Should use 0xe4 instead |
| 1753 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1754 | // Should use 0xe5 instead |
| 1755 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1756 | // Should use 0xe6 instead |
| 1757 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1758 | // Should use 0xe7 instead |
| 1759 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1760 | // Should use 0xe8 instead |
| 1761 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1762 | // Should use 0xe9 instead |
| 1763 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1764 | // Should use 0xea instead |
| 1765 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1766 | // Should use 0xeb instead |
| 1767 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1768 | // Should use 0xec instead |
| 1769 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1770 | // Should use 0xed instead |
| 1771 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1772 | // Should use 0xee instead |
| 1773 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1774 | // Should use 0xef instead |
| 1775 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1776 | // Should use 0xf0 instead |
| 1777 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1778 | // Should use 0xf1 instead |
| 1779 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1780 | // Should use 0xf2 instead |
| 1781 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1782 | // Must use 0xf3 instead |
| 1783 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1784 | // Must use 0xf4 instead |
| 1785 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1786 | // Must use 0xf5 instead |
| 1787 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1788 | // Must use 0xf6 instead |
| 1789 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1790 | // Must use 0xf7 instead |
| 1791 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1792 | // Must use 0xf8 instead |
| 1793 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1794 | |
| 1795 | |
| 1796 | // Integers with additional info indefinite length |
| 1797 | // Positive integer with additional info indefinite length |
| 1798 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 1799 | // Negative integer with additional info indefinite length |
| 1800 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
| 1801 | // CBOR tag with "argument" an indefinite length |
| 1802 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 1803 | // CBOR tag with "argument" an indefinite length alternate vector |
| 1804 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
| 1805 | |
| 1806 | |
| 1807 | // Missing bytes from a deterministic length string |
| 1808 | // A byte string is of length 1 without the 1 byte |
| 1809 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 1810 | // A text string is of length 1 without the 1 byte |
| 1811 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
| 1812 | // Byte string should have 2^32-1 bytes, but has one |
| 1813 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 1814 | // Byte string should have 2^32-1 bytes, but has one |
| 1815 | { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 1816 | |
| 1817 | |
| 1818 | // Use of unassigned additional information values |
| 1819 | // Major type positive integer with reserved value 28 |
| 1820 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1821 | // Major type positive integer with reserved value 29 |
| 1822 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1823 | // Major type positive integer with reserved value 30 |
| 1824 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1825 | // Major type negative integer with reserved value 28 |
| 1826 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1827 | // Major type negative integer with reserved value 29 |
| 1828 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1829 | // Major type negative integer with reserved value 30 |
| 1830 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1831 | // Major type byte string with reserved value 28 length |
| 1832 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1833 | // Major type byte string with reserved value 29 length |
| 1834 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1835 | // Major type byte string with reserved value 30 length |
| 1836 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1837 | // Major type text string with reserved value 28 length |
| 1838 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1839 | // Major type text string with reserved value 29 length |
| 1840 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1841 | // Major type text string with reserved value 30 length |
| 1842 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1843 | // Major type array with reserved value 28 length |
| 1844 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1845 | // Major type array with reserved value 29 length |
| 1846 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1847 | // Major type array with reserved value 30 length |
| 1848 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1849 | // Major type map with reserved value 28 length |
| 1850 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1851 | // Major type map with reserved value 29 length |
| 1852 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1853 | // Major type map with reserved value 30 length |
| 1854 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1855 | // Major type tag with reserved value 28 length |
| 1856 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1857 | // Major type tag with reserved value 29 length |
| 1858 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1859 | // Major type tag with reserved value 30 length |
| 1860 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1861 | // Major type simple with reserved value 28 length |
| 1862 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1863 | // Major type simple with reserved value 29 length |
| 1864 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1865 | // Major type simple with reserved value 30 length |
| 1866 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1867 | |
| 1868 | |
| 1869 | // Maps must have an even number of data items (key & value) |
| 1870 | // Map with 1 item when it should have 2 |
| 1871 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1872 | // Map with 3 item when it should have 4 |
| 1873 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1874 | // Map with 1 item when it should have 2 |
| 1875 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1876 | // Map with 3 item when it should have 4 |
| 1877 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
| 1878 | |
| 1879 | |
| 1880 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1881 | // Text-based date, with an integer |
| 1882 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 1883 | // Epoch date, with an byte string |
| 1884 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 1885 | // tagged as both epoch and string dates |
| 1886 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 1887 | // big num tagged an int, not a byte string |
| 1888 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1889 | }; |
| 1890 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1891 | int DecodeFailureTests() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1892 | { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1893 | int nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1894 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1895 | nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput)); |
| 1896 | if(nResult) { |
| 1897 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1898 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1899 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1900 | // Corrupt the UsefulInputBuf and see that |
| 1901 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1902 | { |
| 1903 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1904 | QCBORItem Item; |
| 1905 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1906 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1907 | QCBORDecode_Init(&DCtx, |
| 1908 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1909 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1910 | |
| 1911 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1912 | return nCBORError; |
| 1913 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1914 | Item.val.uCount != 10) { |
| 1915 | // This wasn't supposed to happen |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1916 | return -1; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1917 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1918 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1919 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1920 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1921 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1922 | if(nCBORError != QCBOR_ERR_HIT_END) { |
| 1923 | // Did not get back the error expected |
| 1924 | return -2; |
| 1925 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1926 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1927 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1928 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1932 | /* Try all 256 values of the byte at nLen including recursing for |
| 1933 | each of the values to try values at nLen+1 ... up to nLenMax |
| 1934 | */ |
| 1935 | static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1936 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1937 | if(nLen >= nLenMax) { |
| 1938 | return; |
| 1939 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 1940 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1941 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 1942 | // Set up the input |
| 1943 | pBuf[nLen] = inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 1944 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 1945 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1946 | // Get ready to parse |
| 1947 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1948 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1949 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1950 | // Parse by getting the next item until an error occurs |
| 1951 | // Just about every possible decoder error can occur here |
| 1952 | // The goal of this test is not to check for the correct |
| 1953 | // error since that is not really possible. It is to |
| 1954 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1955 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1956 | QCBORItem Item; |
| 1957 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1958 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1959 | break; |
| 1960 | } |
| 1961 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1962 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1963 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | |
| 1968 | /* |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1969 | Public function for initialization. See header qcbor.h |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1970 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1971 | int ComprehensiveInputTest() |
| 1972 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1973 | // Size 2 tests 64K inputs and runs quickly |
| 1974 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1975 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1976 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1977 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1978 | return 0; |
| 1979 | } |
| 1980 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1981 | |
| 1982 | /* |
| 1983 | Public function for initialization. See header qcbor.h |
| 1984 | */ |
| 1985 | int BigComprehensiveInputTest() |
| 1986 | { |
| 1987 | // size 3 tests 16 million inputs and runs OK |
| 1988 | // in seconds on fast machines. Size 4 takes |
| 1989 | // 10+ minutes and 5 half a day on fast |
| 1990 | // machines. This test is kept separate from |
| 1991 | // the others so as to no slow down the use |
| 1992 | // of them as a very frequent regression. |
| 1993 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 1994 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1995 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 1996 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1997 | return 0; |
| 1998 | } |
| 1999 | |
| 2000 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2001 | static uint8_t spDateTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2002 | 0xc0, // tag for string date |
| 2003 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2004 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2005 | 0xc1, // tag for epoch date |
| 2006 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2007 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2008 | // CBOR_TAG_B64 |
| 2009 | 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2010 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2011 | |
| 2012 | 0xc1, // tag for epoch date |
| 2013 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2014 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2015 | 0xc1, // tag for epoch date |
| 2016 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2017 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2018 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2019 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2020 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2021 | 0xc1, // tag for epoch date |
| 2022 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2023 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2024 | |
| 2025 | 0xc1, // tag for epoch date |
| 2026 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2027 | }; |
| 2028 | |
| 2029 | |
| 2030 | // have to check float expected only to within an epsilon |
| 2031 | int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2032 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2033 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2034 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2035 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2036 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2037 | return diff > 0.0000001; |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | int DateParseTest() |
| 2042 | { |
| 2043 | QCBORDecodeContext DCtx; |
| 2044 | QCBORItem Item; |
| 2045 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2046 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2047 | QCBORDecode_Init(&DCtx, |
| 2048 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2049 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2050 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2051 | const uint64_t uTags[] = {15}; |
| 2052 | QCBORTagListIn TagList = {1, uTags}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2053 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2054 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2055 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2056 | // String date |
| 2057 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2058 | return -1; |
| 2059 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 2060 | UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2061 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | // Epoch date |
| 2065 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2066 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2067 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2068 | Item.val.epochDate.nSeconds != 1400000000 || |
| 2069 | Item.val.epochDate.fSecondsFraction != 0 ) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2070 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2071 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2072 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2073 | // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything |
| 2074 | // but want to be sure extra tag doesn't cause a problem |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2075 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2076 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2077 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2078 | Item.val.epochDate.nSeconds != 1400000001 || |
| 2079 | Item.val.epochDate.fSecondsFraction != 0 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2080 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2081 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2082 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2083 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2084 | // Epoch date that is too large for our representation |
| 2085 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2086 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2087 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2088 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2089 | // Epoch date in float format with fractional seconds |
| 2090 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2091 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2092 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2093 | Item.val.epochDate.nSeconds != 1 || |
| 2094 | CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2095 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2096 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2097 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2098 | // Epoch date float that is too large for our representation |
| 2099 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2100 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2101 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2102 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2103 | // Epoch date double that is just slightly too large |
| 2104 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2105 | return -11; |
| 2106 | } |
| 2107 | |
| 2108 | // Largest double epoch date supported |
| 2109 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS || |
| 2110 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2111 | Item.val.epochDate.nSeconds != 9223372036854773760 || |
| 2112 | Item.val.epochDate.nSeconds == 0) { |
| 2113 | return -12; |
| 2114 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2115 | // TODO: could use a few more tests with float, double, and half precsion |
| 2116 | // and negative (but coverage is still pretty good) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2117 | |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2121 | // Really simple basic input for tagging test |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2122 | static uint8_t spOptTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2123 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2124 | 0x81, // Array of one |
| 2125 | 0xd8, 0x04, // non-preferred serialization of tag 4 |
| 2126 | 0x82, 0x01, 0x03}; // fraction 1/3 |
| 2127 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2128 | /* |
| 2129 | DB 9192939495969798 # tag(10489608748473423768) |
| 2130 | 80 # array(0) |
| 2131 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2132 | static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
| 2133 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2134 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2135 | /* |
| 2136 | DB 9192939495969798 # tag(10489608748473423768) |
| 2137 | D8 88 # tag(136) |
| 2138 | C6 # tag(6) |
| 2139 | C7 # tag(7) |
| 2140 | 80 # array(0) |
| 2141 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2142 | static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
| 2143 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2144 | |
| 2145 | /* |
| 2146 | The cbor.me parse of this. |
| 2147 | 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2148 | 9(-17): 773("SSG"), -15: 16(17(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"), |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2149 | 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}), |
| 2150 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})), |
| 2151 | 16(-22): 23({11(8(7(-5))): 8(-3)})}))) |
| 2152 | */ |
| 2153 | static uint8_t spCSRWithTags[] = { |
| 2154 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 2155 | 0xc6, 0xc7, 0x36, |
| 2156 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 2157 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 2158 | 0xcb, 0xa5, |
| 2159 | 0xd1, 0x31, |
| 2160 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 2161 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 2162 | 0xc9, 0x30, |
| 2163 | 0xd9, 0x03, 0x05, 0x63, |
| 2164 | 0x53, 0x53, 0x47, |
| 2165 | 0x2e, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2166 | 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2167 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
| 2168 | 0xd1, 0x2f, |
| 2169 | 0xd1, 0x69, |
| 2170 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 2171 | 0xd1, 0x2d, |
| 2172 | 0xd1, 0x62, |
| 2173 | 0x55, 0x53, |
| 2174 | 0xd7, 0x32, |
| 2175 | 0xd3, 0xa2, |
| 2176 | 0x2a, |
| 2177 | 0xc9, 0xa1, |
| 2178 | 0x28, |
| 2179 | 0x26, |
| 2180 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 2181 | 0xcc, 0x4a, |
| 2182 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 2183 | 0xd0, 0x35, |
| 2184 | 0xd7, 0xa1, |
| 2185 | 0xcb, 0xc8, 0xc7, 0x24, |
| 2186 | 0xc8, 0x22}; |
| 2187 | |
| 2188 | static int CheckCSRMaps(QCBORDecodeContext *pDC); |
| 2189 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2190 | |
| 2191 | int OptTagParseTest() |
| 2192 | { |
| 2193 | QCBORDecodeContext DCtx; |
| 2194 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2195 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2196 | QCBORDecode_Init(&DCtx, |
| 2197 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), |
| 2198 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2199 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2200 | //------------------------- |
| 2201 | // This text matches the magic number tag and the fraction tag |
| 2202 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2203 | return -2; |
| 2204 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2205 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2206 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 2207 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2208 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2209 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2210 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2211 | return -4; |
| 2212 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2213 | |
| 2214 | #ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2215 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2216 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2217 | Item.val.uCount != 2) { |
| 2218 | return -5; |
| 2219 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2220 | #else |
| 2221 | if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) { |
| 2222 | return -6; |
| 2223 | } |
| 2224 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2225 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2226 | // -------------------------------- |
| 2227 | // This test decodes the very large tag, but it is not in |
| 2228 | // any list so it is ignored. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2229 | QCBORDecode_Init(&DCtx, |
| 2230 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 2231 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2232 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2233 | return -6; |
| 2234 | } |
| 2235 | if(Item.uTagBits) { |
| 2236 | return -7; |
| 2237 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2238 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2239 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2240 | // This test sets up a caller-config list that includes the very large |
| 2241 | // tage and then matches it. |
| 2242 | QCBORDecode_Init(&DCtx, |
| 2243 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 2244 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2245 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 2246 | const QCBORTagListIn TL = {2, puList}; |
| 2247 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2248 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2249 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2250 | return -8; |
| 2251 | } |
| 2252 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2253 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 2254 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 2255 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 2256 | Item.val.uCount != 0) { |
| 2257 | return -9; |
| 2258 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2259 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2260 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2261 | // Sets up a caller-configured list and look up something not in it |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2262 | const uint64_t puLongList[17] = {1,2,1}; |
| 2263 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2264 | QCBORDecode_Init(&DCtx, |
| 2265 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 2266 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2267 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 2268 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2269 | return -11; |
| 2270 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2271 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2272 | // ----------------------- |
| 2273 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2274 | QCBORDecode_Init(&DCtx, |
| 2275 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 2276 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2277 | uint64_t puTags[16]; |
| 2278 | QCBORTagListOut Out = {0, 4, puTags}; |
| 2279 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2280 | return -12; |
| 2281 | } |
| 2282 | if(puTags[0] != 0x9192939495969798 || |
| 2283 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2284 | puTags[2] != 0x06 || |
| 2285 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2286 | return -13; |
| 2287 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2288 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2289 | // ---------------------- |
| 2290 | // This text if too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2291 | QCBORDecode_Init(&DCtx, |
| 2292 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 2293 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2294 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 2295 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 2296 | return -14; |
| 2297 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2298 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2299 | // --------------- |
| 2300 | // Parse a version of the "CSR" that has had a ton of tags randomly inserted |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2301 | QCBORDecode_Init(&DCtx, |
| 2302 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 2303 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2304 | int n = CheckCSRMaps(&DCtx); |
| 2305 | if(n) { |
| 2306 | return n-2000; |
| 2307 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2308 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2309 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2310 | QCBORDecode_Init(&DCtx, |
| 2311 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 2312 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2313 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2314 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 2315 | const QCBORTagListIn TagList = {3, puTagList}; |
| 2316 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2317 | |
| 2318 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2319 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2320 | return -100; |
| 2321 | } |
| 2322 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2323 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 2324 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 2325 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 2326 | Item.val.uCount != 2 || |
| 2327 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 2328 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 2329 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 2330 | Out.uNumUsed != 3) { |
| 2331 | return -101; |
| 2332 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2333 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2334 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2335 | return -102; |
| 2336 | } |
| 2337 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2338 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 2339 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
| 2340 | QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized |
| 2341 | Item.val.uCount != 2 || |
| 2342 | puTags[0] != 5859837686836516696 || |
| 2343 | puTags[1] != 7 || |
| 2344 | Out.uNumUsed != 2) { |
| 2345 | return -103; |
| 2346 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2347 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2348 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2349 | return -104; |
| 2350 | } |
| 2351 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2352 | Item.uTagBits || |
| 2353 | Item.val.uCount != 5 || |
| 2354 | puTags[0] != 0x0b || |
| 2355 | Out.uNumUsed != 1) { |
| 2356 | return -105; |
| 2357 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2358 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2359 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2360 | return -106; |
| 2361 | } |
| 2362 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2363 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 2364 | Item.val.string.len != 12 || |
| 2365 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 2366 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 2367 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 2368 | Out.uNumUsed != 3) { |
| 2369 | return -105; |
| 2370 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2371 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2372 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2373 | return -107; |
| 2374 | } |
| 2375 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2376 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 2377 | Item.val.string.len != 3 || |
| 2378 | puTags[0] != 773 || |
| 2379 | Out.uNumUsed != 1) { |
| 2380 | return -108; |
| 2381 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2382 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2383 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2384 | return -109; |
| 2385 | } |
| 2386 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2387 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2388 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2389 | puTags[0] != 16 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2390 | puTags[11] != 0x0f || |
| 2391 | Out.uNumUsed != 12) { |
| 2392 | return -110; |
| 2393 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2394 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2395 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2396 | return -111; |
| 2397 | } |
| 2398 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2399 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 2400 | Item.val.string.len != 9 || |
| 2401 | puTags[0] != 17 || |
| 2402 | Out.uNumUsed != 1) { |
| 2403 | return -112; |
| 2404 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2405 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2406 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2407 | return -111; |
| 2408 | } |
| 2409 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2410 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 2411 | Item.val.string.len != 2 || |
| 2412 | puTags[0] != 17 || |
| 2413 | Out.uNumUsed != 1) { |
| 2414 | return -112; |
| 2415 | } |
| 2416 | |
| 2417 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2418 | return -113; |
| 2419 | } |
| 2420 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2421 | QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
| 2422 | Item.val.uCount != 2 || |
| 2423 | puTags[0] != 19 || |
| 2424 | Out.uNumUsed != 1) { |
| 2425 | return -114; |
| 2426 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2427 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2428 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2429 | return -115; |
| 2430 | } |
| 2431 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2432 | QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
| 2433 | Item.uTagBits || |
| 2434 | Item.val.uCount != 1 || |
| 2435 | puTags[0] != 9 || |
| 2436 | Out.uNumUsed != 1) { |
| 2437 | return -116; |
| 2438 | } |
| 2439 | |
| 2440 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2441 | return -116; |
| 2442 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2443 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2444 | Item.val.int64 != -7 || |
| 2445 | Item.uTagBits || |
| 2446 | Out.uNumUsed != 0) { |
| 2447 | return -117; |
| 2448 | } |
| 2449 | |
| 2450 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2451 | return -118; |
| 2452 | } |
| 2453 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 2454 | Item.val.string.len != 10 || |
| 2455 | Item.uTagBits || |
| 2456 | puTags[0] != 12 || |
| 2457 | Out.uNumUsed != 1) { |
| 2458 | return -119; |
| 2459 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2460 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2461 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2462 | return -120; |
| 2463 | } |
| 2464 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2465 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 2466 | Item.val.uCount != 1 || |
| 2467 | puTags[0] != 0x17 || |
| 2468 | Out.uNumUsed != 1) { |
| 2469 | return -121; |
| 2470 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2471 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2472 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2473 | return -122; |
| 2474 | } |
| 2475 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 2476 | QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
| 2477 | Item.val.int64 != -3 || |
| 2478 | puTags[0] != 8 || |
| 2479 | Out.uNumUsed != 1) { |
| 2480 | return -123; |
| 2481 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2482 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2483 | if(QCBORDecode_Finish(&DCtx)) { |
| 2484 | return -124; |
| 2485 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2486 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | |
| 2491 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2492 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2493 | static uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2494 | 0x83, |
| 2495 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2496 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2497 | 0xA4, |
| 2498 | 0x63, 0x42, 0x4E, 0x2B, |
| 2499 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2500 | 0x18, 0x40, |
| 2501 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2502 | 0x63, 0x42, 0x4E, 0x2D, |
| 2503 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2504 | 0x38, 0x3F, |
| 2505 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 2506 | |
| 2507 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2508 | static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2509 | |
| 2510 | |
| 2511 | int BignumParseTest() |
| 2512 | { |
| 2513 | QCBORDecodeContext DCtx; |
| 2514 | QCBORItem Item; |
| 2515 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2516 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2517 | QCBORDecode_Init(&DCtx, |
| 2518 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 2519 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2520 | |
| 2521 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2522 | // |
| 2523 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2524 | return -1; |
| 2525 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2526 | return -1; |
| 2527 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2528 | |
| 2529 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2530 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2531 | return -1; |
| 2532 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2533 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2534 | return -1; |
| 2535 | } |
| 2536 | |
| 2537 | // |
| 2538 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2539 | return -1; |
| 2540 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2541 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2542 | return -1; |
| 2543 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2544 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2545 | // |
| 2546 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2547 | return -1; |
| 2548 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 2549 | return -1; |
| 2550 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2551 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2552 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2553 | return -1; |
| 2554 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 2555 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2556 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2557 | return -1; |
| 2558 | } |
| 2559 | |
| 2560 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2561 | return -1; |
| 2562 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 2563 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 2564 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2565 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2566 | return -1; |
| 2567 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2568 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2569 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2570 | return -1; |
| 2571 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 2572 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2573 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2574 | return -1; |
| 2575 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2576 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2577 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2578 | return -1; |
| 2579 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 2580 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 2581 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2582 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2583 | return -1; |
| 2584 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2585 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2586 | return 0; |
| 2587 | } |
| 2588 | |
| 2589 | |
| 2590 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2591 | static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
| 2592 | uint8_t uDataType, |
| 2593 | uint8_t uNestingLevel, |
| 2594 | uint8_t uNextNest, |
| 2595 | int64_t nLabel, |
| 2596 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2597 | { |
| 2598 | QCBORItem Item; |
| 2599 | int nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2600 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2601 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 2602 | if(Item.uDataType != uDataType) return -1; |
| 2603 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2604 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 2605 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 2606 | return -1; |
| 2607 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2608 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 2609 | if(Item.label.int64 != nLabel) return -1; |
| 2610 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2611 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2612 | } |
| 2613 | } |
| 2614 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2615 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2616 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2617 | if(pItem) { |
| 2618 | *pItem = Item; |
| 2619 | } |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
| 2623 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2624 | // Same code checks definite and indefinite length versions of the map |
| 2625 | static int CheckCSRMaps(QCBORDecodeContext *pDC) |
| 2626 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2627 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2628 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2629 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2630 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2631 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2632 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2633 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1; |
| 2634 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1; |
| 2635 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1; |
| 2636 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1; |
| 2637 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2638 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2639 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1; |
| 2640 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2641 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2642 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1; |
| 2643 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2644 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2645 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1; |
| 2646 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2647 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2648 | if(QCBORDecode_Finish(pDC)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2649 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2650 | return 0; |
| 2651 | } |
| 2652 | |
| 2653 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2654 | /* |
| 2655 | // cbor.me decoded output |
| 2656 | { |
| 2657 | -23: { |
| 2658 | -20: { |
| 2659 | -18: "Organization", |
| 2660 | -17: "SSG", |
| 2661 | -15: "Confusion", |
| 2662 | -16: "San Diego", |
| 2663 | -14: "US" |
| 2664 | }, |
| 2665 | -19: { |
| 2666 | -11: { |
| 2667 | -9: -7 |
| 2668 | }, |
| 2669 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 2670 | } |
| 2671 | }, |
| 2672 | -22: { |
| 2673 | -5: -3 |
| 2674 | } |
| 2675 | } |
| 2676 | */ |
| 2677 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2678 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2679 | static uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2680 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 2681 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 2682 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 2683 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 2684 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 2685 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 2686 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 2687 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 2688 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 2689 | |
| 2690 | int NestedMapTest() |
| 2691 | { |
| 2692 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2693 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2694 | QCBORDecode_Init(&DCtx, |
| 2695 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 2696 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2697 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2698 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2699 | } |
| 2700 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2701 | |
| 2702 | |
| 2703 | int StringDecoderModeFailTest() |
| 2704 | { |
| 2705 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2706 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2707 | QCBORDecode_Init(&DCtx, |
| 2708 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 2709 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2710 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2711 | QCBORItem Item; |
| 2712 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2713 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2714 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2715 | return -1; |
| 2716 | } |
| 2717 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 2718 | return -2; |
| 2719 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2720 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2721 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2722 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 2723 | return -3; |
| 2724 | } |
| 2725 | |
| 2726 | return 0; |
| 2727 | } |
| 2728 | |
| 2729 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2730 | // Same map as above, but using indefinite lengths |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2731 | static uint8_t spCSRInputIndefLen[] = { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2732 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 2733 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 2734 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 2735 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 2736 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 2737 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2738 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 2739 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 2740 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 2741 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2742 | |
| 2743 | int NestedMapTestIndefLen() |
| 2744 | { |
| 2745 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2746 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2747 | QCBORDecode_Init(&DCtx, |
| 2748 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 2749 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2750 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2751 | return CheckCSRMaps(&DCtx); |
| 2752 | } |
| 2753 | |
| 2754 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2755 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2756 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 2757 | { |
| 2758 | UsefulOutBuf UOB; |
| 2759 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2760 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2761 | int i; |
| 2762 | for(i = 0; i < n; i++) { |
| 2763 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 2764 | } |
| 2765 | |
| 2766 | for(i = 0; i < n; i++) { |
| 2767 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 2768 | } |
| 2769 | return UsefulOutBuf_OutUBuf(&UOB); |
| 2770 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2771 | |
| 2772 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2773 | static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
| 2774 | { |
| 2775 | QCBORDecodeContext DC; |
| 2776 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2777 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2778 | int j; |
| 2779 | for(j = 0; j < nNestLevel; j++) { |
| 2780 | QCBORItem Item; |
| 2781 | int nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 2782 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 2783 | // Should be in error |
| 2784 | if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
| 2785 | return -4; |
| 2786 | } else { |
| 2787 | return 0; // Decoding doesn't recover after an error |
| 2788 | } |
| 2789 | } else { |
| 2790 | // Should be no error |
| 2791 | if(nReturn) { |
| 2792 | return -9; // Should not have got an error |
| 2793 | } |
| 2794 | } |
| 2795 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2796 | return -7; |
| 2797 | } |
| 2798 | } |
| 2799 | int nReturn = QCBORDecode_Finish(&DC); |
| 2800 | if(nReturn) { |
| 2801 | return -3; |
| 2802 | } |
| 2803 | return 0; |
| 2804 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2805 | |
| 2806 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2807 | int IndefiniteLengthNestTest() |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2808 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2809 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2810 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2811 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2812 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2813 | int nReturn = parse_indeflen_nested(Nested, i); |
| 2814 | if(nReturn) { |
| 2815 | return nReturn; |
| 2816 | } |
| 2817 | } |
| 2818 | return 0; |
| 2819 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2820 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2821 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2822 | // [1, [2, 3]] |
| 2823 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 2824 | // No closing break |
| 2825 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 2826 | // Not enough closing breaks |
| 2827 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 2828 | // Too many closing breaks |
| 2829 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 2830 | // Unclosed indeflen inside def len |
| 2831 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 2832 | // confused tag |
| 2833 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2834 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2835 | int IndefiniteLengthArrayMapTest() |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2836 | { |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2837 | int nResult; |
| 2838 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2839 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2840 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2841 | // Decode it and see if it is OK |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2842 | UsefulBuf_MAKE_STACK_UB(MemPool, 150); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2843 | QCBORDecodeContext DC; |
| 2844 | QCBORItem Item; |
| 2845 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2846 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2847 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2848 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2849 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2850 | |
| 2851 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2852 | Item.uNestingLevel != 0 || |
| 2853 | Item.uNextNestLevel != 1) { |
| 2854 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2855 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2856 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2857 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2858 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 2859 | Item.uNestingLevel != 1 || |
| 2860 | Item.uNextNestLevel != 1) { |
| 2861 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2862 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2863 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2864 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2865 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2866 | Item.uNestingLevel != 1 || |
| 2867 | Item.uNextNestLevel != 2) { |
| 2868 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2869 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2870 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2871 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 2872 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2873 | Item.uNestingLevel != 2 || |
| 2874 | Item.uNextNestLevel != 2) { |
| 2875 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2876 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2877 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2878 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 2879 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2880 | Item.uNestingLevel != 2 || |
| 2881 | Item.uNextNestLevel != 0) { |
| 2882 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2883 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2884 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2885 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2886 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2887 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2888 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2889 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2890 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2891 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2892 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2893 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2894 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2895 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2896 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2897 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2898 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2899 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2900 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2901 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2902 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2903 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2904 | } |
| 2905 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2906 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2907 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2908 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2909 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2910 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2911 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2912 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2913 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2914 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2915 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2916 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2920 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2921 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2922 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2923 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2924 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2925 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2926 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2927 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2928 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2929 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2930 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2931 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2932 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2933 | |
| 2934 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2935 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2936 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2937 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2938 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2939 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2940 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2941 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2942 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2943 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2944 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2945 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2946 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2947 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2948 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 2949 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2950 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2951 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2952 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2953 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2954 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2955 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2956 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2957 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2958 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2959 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2960 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2961 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2962 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2963 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2964 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2965 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2966 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2967 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2968 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2969 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2970 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2971 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2972 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2973 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2974 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2975 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2976 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2977 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2978 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2979 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2980 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2981 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2982 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2983 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2984 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2985 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2986 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2987 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2988 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 2989 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2990 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2991 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2992 | return 0; |
| 2993 | } |
| 2994 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2995 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2996 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2997 | 0x81, // Array of length one |
| 2998 | 0x7f, // text string marked with indefinite length |
| 2999 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3000 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3001 | 0xff // ending break |
| 3002 | }; |
| 3003 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3004 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3005 | 0x81, // Array of length one |
| 3006 | 0x7f, // text string marked with indefinite length |
| 3007 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3008 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 3009 | 0xff // ending break |
| 3010 | }; |
| 3011 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3012 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3013 | 0x81, // Array of length one |
| 3014 | 0x7f, // text string marked with indefinite length |
| 3015 | 0x01, 0x02, // Not a string |
| 3016 | 0xff // ending break |
| 3017 | }; |
| 3018 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3019 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3020 | 0x81, // Array of length one |
| 3021 | 0x7f, // text string marked with indefinite length |
| 3022 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3023 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3024 | // missing end of string |
| 3025 | }; |
| 3026 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3027 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3028 | 0xa1, // Array of length one |
| 3029 | 0x7f, // text string marked with indefinite length |
| 3030 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 3031 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3032 | 0xff, // ending break |
| 3033 | 0x01 // integer being labeled. |
| 3034 | }; |
| 3035 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3036 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3037 | { |
| 3038 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3039 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3040 | UsefulOutBuf_Init(&UOB, Storage); |
| 3041 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 3042 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3043 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3044 | int i = 0; |
| 3045 | for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) { |
| 3046 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
| 3047 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize); |
| 3048 | for(int j = 0; j < nChunkSize; j++ ) { |
| 3049 | UsefulOutBuf_AppendByte(&UOB, i); |
| 3050 | i++; |
| 3051 | } |
| 3052 | } |
| 3053 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3054 | |
| 3055 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3056 | } |
| 3057 | |
| 3058 | static int CheckBigString(UsefulBufC BigString) |
| 3059 | { |
| 3060 | if(BigString.len != 255) { |
| 3061 | return 1; |
| 3062 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3063 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3064 | for(uint8_t i = 0; i < 255; i++){ |
| 3065 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 3066 | return 1; |
| 3067 | } |
| 3068 | } |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3072 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3073 | int IndefiniteLengthStringTest() |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3074 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3075 | QCBORDecodeContext DC; |
| 3076 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3077 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3078 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3079 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3080 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3081 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3082 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3083 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3084 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3085 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3086 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3087 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3088 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3089 | return -2; |
| 3090 | } |
| 3091 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 3092 | return -3; |
| 3093 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3094 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3095 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3096 | return -4; |
| 3097 | } |
| 3098 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 3099 | return -5; |
| 3100 | } |
| 3101 | if(QCBORDecode_Finish(&DC)) { |
| 3102 | return -6; |
| 3103 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3104 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3105 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3106 | QCBORDecode_Init(&DC, |
| 3107 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 3108 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3109 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3110 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3111 | return -7; |
| 3112 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3113 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3114 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3115 | return -8; |
| 3116 | } |
| 3117 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3118 | return -9; |
| 3119 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3120 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3121 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3122 | return -10; |
| 3123 | } |
| 3124 | |
| 3125 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3126 | QCBORDecode_Init(&DC, |
| 3127 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 3128 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3129 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3130 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3131 | return -11; |
| 3132 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3133 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3134 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3135 | return -12; |
| 3136 | } |
| 3137 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3138 | return -13; |
| 3139 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3140 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3141 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3142 | return -14; |
| 3143 | } |
| 3144 | |
| 3145 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3146 | QCBORDecode_Init(&DC, |
| 3147 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 3148 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3149 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3150 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3151 | return -15; |
| 3152 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3153 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3154 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3155 | return -16; |
| 3156 | } |
| 3157 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3158 | return -17; |
| 3159 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3160 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3161 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 3162 | return -18; |
| 3163 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3164 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3165 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3166 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3167 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3168 | QCBORDecode_GetNext(&DC, &Item); |
| 3169 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3170 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3171 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3172 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3173 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3174 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3175 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3176 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3177 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3178 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3179 | |
| 3180 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 3181 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3182 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3183 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3184 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3185 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3186 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3187 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3188 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3189 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 3190 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3191 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3192 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3193 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3194 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3195 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3196 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3197 | QCBORDecode_GetNext(&DC, &Item); |
| 3198 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3199 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3200 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3201 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3202 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3203 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3204 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3205 | // ---- big bstr ----- |
| 3206 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3207 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3208 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3209 | return -25; |
| 3210 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3211 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3212 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3213 | return -26; |
| 3214 | } |
| 3215 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3216 | return -26; |
| 3217 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3218 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3219 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3220 | return -27; |
| 3221 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3222 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3223 | return -28; |
| 3224 | } |
| 3225 | if(CheckBigString(Item.val.string)) { |
| 3226 | return -3; |
| 3227 | } |
| 3228 | if(QCBORDecode_Finish(&DC)) { |
| 3229 | return -29; |
| 3230 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3231 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3232 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3233 | 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] | 3234 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3235 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3236 | return -30; |
| 3237 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3238 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3239 | QCBORDecode_GetNext(&DC, &Item); |
| 3240 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3241 | return -31; |
| 3242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3243 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3244 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 3245 | return -32; |
| 3246 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3247 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 3248 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3249 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 3250 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 3251 | return -33; |
| 3252 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3253 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3254 | if(QCBORDecode_Finish(&DC)) { |
| 3255 | return -34; |
| 3256 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3257 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3258 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3262 | int AllocAllStringsTest() |
| 3263 | { |
| 3264 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3265 | QCBORError nCBORError; |
| 3266 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3267 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3268 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3269 | QCBORDecode_Init(&DC, |
| 3270 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3271 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3272 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3273 | 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] | 3274 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3275 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 3276 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3277 | return -1; |
| 3278 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3279 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3280 | if(CheckCSRMaps(&DC)) { |
| 3281 | return -2; |
| 3282 | } |
| 3283 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3284 | // Next parse, save pointers to a few strings, destroy original and see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3285 | 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] | 3286 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3287 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3288 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3289 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3290 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3291 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3292 | QCBORItem Item1, Item2, Item3, Item4; |
| 3293 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
| 3294 | return nCBORError; |
| 3295 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 3296 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3297 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3298 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
| 3299 | return nCBORError; |
| 3300 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
| 3301 | return nCBORError; |
| 3302 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
| 3303 | return nCBORError; |
| 3304 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
| 3305 | return nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3306 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 3307 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3308 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3309 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3310 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 3311 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3312 | Item1.uDataAlloc != 0 || |
| 3313 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3314 | UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3315 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3316 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3317 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3318 | |
| 3319 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3320 | UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3321 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3322 | Item2.uDataAlloc != 0 || |
| 3323 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3324 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3325 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3326 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3327 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3328 | Item3.uDataAlloc == 0 || |
| 3329 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3330 | UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3331 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3332 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3333 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3334 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3335 | Item4.uDataAlloc == 0 || |
| 3336 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3337 | UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3338 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3339 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3340 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3341 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3342 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3343 | QCBORDecode_Init(&DC, |
| 3344 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 3345 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3346 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 3347 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3348 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3349 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3350 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3351 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3352 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3353 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 3354 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 3355 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 3356 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 3357 | } |
| 3358 | } |
| 3359 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3360 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3361 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3367 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3368 | |
| 3369 | int MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 3370 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3371 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 3372 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3373 | QCBORDecodeContext DC; |
| 3374 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 3375 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3376 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3377 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3378 | // Then fish into the internals of the decode context |
| 3379 | // to get the allocator function so it can be called directly. |
| 3380 | // Also figure out how much pool is available for use |
| 3381 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3382 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3383 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 3384 | if(nError) { |
| 3385 | return -9; |
| 3386 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3387 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 3388 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 3389 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3390 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3391 | // First test -- ask for one more byte than available and see failure |
| 3392 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3393 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 3394 | return -1; |
| 3395 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3396 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3397 | // Re do the set up for the next test that will do a successful alloc, |
| 3398 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3399 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3400 | pAlloc = DC.StringAllocator.pfAllocator; |
| 3401 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 3402 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3403 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3404 | // Allocate one byte less than available and see success |
| 3405 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3406 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 3407 | return -2; |
| 3408 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3409 | // Ask for some more and see failure |
| 3410 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3411 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 3412 | return -3; |
| 3413 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3414 | // Free the first allocate, retry the second and see success |
| 3415 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 3416 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3417 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 3418 | return -4; |
| 3419 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3420 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3421 | // Re do set up for next test that involves a successful alloc, |
| 3422 | // and a successful realloc and a failed realloc |
| 3423 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3424 | pAlloc = DC.StringAllocator.pfAllocator; |
| 3425 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3426 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3427 | // Allocate half the pool and see success |
| 3428 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3429 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 3430 | return -5; |
| 3431 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3432 | // Reallocate to take up the whole pool and see success |
| 3433 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3434 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 3435 | return -6; |
| 3436 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3437 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3438 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 3439 | return -7; |
| 3440 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3441 | // Try to allocate more to be sure there is failure after a realloc |
| 3442 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 3443 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3444 | return -8; |
| 3445 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3446 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3447 | return 0; |
| 3448 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3449 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3450 | |
| 3451 | /* Just enough of an allocator to test configuration of one */ |
| 3452 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 3453 | { |
| 3454 | (void)pOldMem; // unused variable |
| 3455 | |
| 3456 | if(uNewSize) { |
| 3457 | // Assumes the context pointer is the buffer and |
| 3458 | // nothing too big will ever be asked for. |
| 3459 | // This is only good for this basic test! |
| 3460 | return (UsefulBuf) {pCtx, uNewSize}; |
| 3461 | } else { |
| 3462 | return NULLUsefulBuf; |
| 3463 | } |
| 3464 | } |
| 3465 | |
| 3466 | |
| 3467 | int SetUpAllocatorTest(void) |
| 3468 | { |
| 3469 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 3470 | // nothing can be done with it unless that is set up. |
| 3471 | QCBORDecodeContext DC; |
| 3472 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 3473 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 3474 | |
| 3475 | uint8_t pAllocatorBuffer[50]; |
| 3476 | |
| 3477 | // This is really just to test that this call works. |
| 3478 | // The full functionality of string allocators is tested |
| 3479 | // elsewhere with the MemPool internal allocator. |
| 3480 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 3481 | |
| 3482 | QCBORItem Item; |
| 3483 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 3484 | return -1; |
| 3485 | } |
| 3486 | |
| 3487 | if(Item.uDataAlloc == 0 || |
| 3488 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3489 | Item.val.string.ptr != pAllocatorBuffer) { |
| 3490 | return -2; |
| 3491 | } |
| 3492 | |
| 3493 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 3494 | return -3; |
| 3495 | } |
| 3496 | |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3500 | |
| 3501 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 3502 | /* |
| 3503 | [ |
| 3504 | 4([-1, 3]), |
| 3505 | 4([-20, 4759477275222530853136]), |
| 3506 | 4([9223372036854775807, -4759477275222530853137]), |
| 3507 | 5([300, 100]), |
| 3508 | 5([-20, 4759477275222530853136]), |
| 3509 | 5([-9223372036854775807, -4759477275222530853137]) |
| 3510 | 5([9223372036854775806, -4759477275222530853137]) |
| 3511 | 5([9223372036854775806, 9223372036854775806])] |
| 3512 | ] |
| 3513 | */ |
| 3514 | |
| 3515 | static const uint8_t spExpectedExponentsAndMantissas[] = { |
| 3516 | 0x87, |
| 3517 | 0xC4, 0x82, 0x20, |
| 3518 | 0x03, |
| 3519 | 0xC4, 0x82, 0x33, |
| 3520 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 3521 | 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3522 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 3523 | 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 3524 | 0x18, 0x64, |
| 3525 | 0xC5, 0x82, 0x33, |
| 3526 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 3527 | 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 3528 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 3529 | 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 3530 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE |
| 3531 | }; |
| 3532 | |
| 3533 | int ExponentAndMantissaDecodeTests(void) |
| 3534 | { |
| 3535 | QCBORDecodeContext DC; |
| 3536 | QCBORError nCBORError; |
| 3537 | QCBORItem item; |
| 3538 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 3539 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 3540 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 3541 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3542 | |
| 3543 | |
| 3544 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL); |
| 3545 | |
| 3546 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3547 | if(nCBORError != QCBOR_SUCCESS) { |
| 3548 | return 1; |
| 3549 | } |
| 3550 | |
| 3551 | if(item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3552 | return 2; |
| 3553 | } |
| 3554 | |
| 3555 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3556 | if(nCBORError != QCBOR_SUCCESS) { |
| 3557 | return 3; |
| 3558 | } |
| 3559 | |
| 3560 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3561 | item.val.expAndMantissa.Mantissa.nInt != 3 || |
| 3562 | item.val.expAndMantissa.nExponent != -1) { |
| 3563 | return 4; |
| 3564 | } |
| 3565 | |
| 3566 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3567 | if(nCBORError != QCBOR_SUCCESS) { |
| 3568 | return 5; |
| 3569 | } |
| 3570 | |
| 3571 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 3572 | item.val.expAndMantissa.nExponent != -20 || |
| 3573 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3574 | return 6; |
| 3575 | } |
| 3576 | |
| 3577 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3578 | if(nCBORError != QCBOR_SUCCESS) { |
| 3579 | return 7; |
| 3580 | } |
| 3581 | |
| 3582 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM || |
| 3583 | item.val.expAndMantissa.nExponent != 9223372036854775807 || |
| 3584 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3585 | return 8; |
| 3586 | } |
| 3587 | |
| 3588 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3589 | if(nCBORError != QCBOR_SUCCESS) { |
| 3590 | return 9; |
| 3591 | } |
| 3592 | |
| 3593 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 3594 | item.val.expAndMantissa.Mantissa.nInt != 100 || |
| 3595 | item.val.expAndMantissa.nExponent != 300) { |
| 3596 | return 10; |
| 3597 | } |
| 3598 | |
| 3599 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3600 | if(nCBORError != QCBOR_SUCCESS) { |
| 3601 | return 11; |
| 3602 | } |
| 3603 | |
| 3604 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM || |
| 3605 | item.val.expAndMantissa.nExponent != -20 || |
| 3606 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3607 | return 12; |
| 3608 | } |
| 3609 | |
| 3610 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3611 | if(nCBORError != QCBOR_SUCCESS) { |
| 3612 | return 13; |
| 3613 | } |
| 3614 | |
| 3615 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 3616 | item.val.expAndMantissa.nExponent != -9223372036854775807 || |
| 3617 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3618 | return 14; |
| 3619 | } |
| 3620 | |
| 3621 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3622 | if(nCBORError != QCBOR_SUCCESS) { |
| 3623 | return 15; |
| 3624 | } |
| 3625 | |
| 3626 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 3627 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 3628 | item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) { |
| 3629 | return 16; |
| 3630 | } |
| 3631 | |
| 3632 | /* Now encode some stuff and then decode it */ |
| 3633 | uint8_t pBuf[40]; |
| 3634 | QCBOREncodeContext EC; |
| 3635 | UsefulBufC Encoded; |
| 3636 | |
| 3637 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 3638 | QCBOREncode_OpenArray(&EC); |
| 3639 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 3640 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 3641 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 3642 | QCBOREncode_CloseArray(&EC); |
| 3643 | QCBOREncode_Finish(&EC, &Encoded); |
| 3644 | |
| 3645 | |
| 3646 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
| 3647 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3648 | if(nCBORError != QCBOR_SUCCESS) { |
| 3649 | return 13; |
| 3650 | } |
| 3651 | |
| 3652 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3653 | if(nCBORError != QCBOR_SUCCESS) { |
| 3654 | return 13; |
| 3655 | } |
| 3656 | |
| 3657 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3658 | item.val.expAndMantissa.nExponent != 1000 || |
| 3659 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
| 3660 | return 15; |
| 3661 | } |
| 3662 | |
| 3663 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3664 | if(nCBORError != QCBOR_SUCCESS) { |
| 3665 | return 13; |
| 3666 | } |
| 3667 | |
| 3668 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 3669 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 3670 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
| 3671 | return 15; |
| 3672 | } |
| 3673 | |
| 3674 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3675 | if(nCBORError != QCBOR_SUCCESS) { |
| 3676 | return 13; |
| 3677 | } |
| 3678 | |
| 3679 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 3680 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 3681 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3682 | return 12; |
| 3683 | } |
| 3684 | |
| 3685 | return 0; |
| 3686 | } |
| 3687 | |
| 3688 | |
| 3689 | static struct FailInput ExponentAndMantissaFailures[] = { |
| 3690 | // Exponent > INT64_MAX |
| 3691 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3692 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3693 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3694 | // Mantissa > INT64_MAX |
| 3695 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3696 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 3697 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3698 | // End of input |
| 3699 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END}, |
| 3700 | // End of input |
| 3701 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END}, |
| 3702 | // bad content for big num |
| 3703 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 3704 | // bad content for big num |
| 3705 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 3706 | // Bad integer for exponent |
| 3707 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 3708 | // Bad integer for mantissa |
| 3709 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 3710 | // 3 items in array |
| 3711 | { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3712 | // unterminated indefinite length array |
| 3713 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3714 | // Empty array |
| 3715 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 3716 | // Second is not an integer |
| 3717 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3718 | // First is not an integer |
| 3719 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3720 | // Not an array |
| 3721 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 3722 | }; |
| 3723 | |
| 3724 | |
| 3725 | int ExponentAndMantissaDecodeFailTests() |
| 3726 | { |
| 3727 | return ProcessFailures(ExponentAndMantissaFailures, |
| 3728 | sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput)); |
| 3729 | } |
| 3730 | |
| 3731 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |