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. |
| 3 | Copyright (c) 2018, Laurence Lundblade. |
| 4 | All rights reserved. |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +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 | 9e3651c | 2018-10-10 11:49:55 +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 | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 31 | ==============================================================================*/ |
| 32 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 33 | #include "qcbor.h" |
| 34 | #include "qcbor_decode_tests.h" |
| 35 | #include <stdio.h> |
| 36 | #include <strings.h> |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 37 | #include <math.h> // for fabs() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | |
| 40 | |
| 41 | // TODO: test other than the normal decoder mode |
| 42 | |
| 43 | static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen) |
| 44 | { |
| 45 | if(szLabel) { |
| 46 | printf("%s ", szLabel); |
| 47 | } |
| 48 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 49 | size_t i; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 50 | for(i = 0; i < nLen; i++) { |
| 51 | uint8_t Z = pEncoded[i]; |
| 52 | printf("%02x ", Z); |
| 53 | } |
| 54 | printf("\n"); |
| 55 | |
| 56 | fflush(stdout); |
| 57 | } |
| 58 | |
| 59 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 60 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 61 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 62 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 63 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 64 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 65 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 66 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 67 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 68 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 69 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 70 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 71 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 72 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 73 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 74 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 75 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 76 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 77 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 78 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 79 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 80 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 81 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 82 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 83 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 84 | 0xff, 0xff}; |
| 85 | |
| 86 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 87 | // return CBOR error or -1 if type of value doesn't match |
| 88 | |
| 89 | static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
| 90 | { |
| 91 | QCBORItem Item; |
| 92 | int nCBORError; |
| 93 | |
| 94 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 95 | return nCBORError; |
| 96 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 97 | return -1; |
| 98 | |
| 99 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 100 | return nCBORError; |
| 101 | if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 102 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 103 | return -1; |
| 104 | |
| 105 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 106 | return nCBORError; |
| 107 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 108 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 109 | return -1; |
| 110 | |
| 111 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 112 | return nCBORError; |
| 113 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 114 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 115 | return -1; |
| 116 | |
| 117 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 118 | return nCBORError; |
| 119 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 120 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 121 | return -1; |
| 122 | |
| 123 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 124 | return nCBORError; |
| 125 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 126 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 127 | return -1; |
| 128 | |
| 129 | |
| 130 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 131 | return nCBORError; |
| 132 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 133 | Item.val.int64 != -2147483648) |
| 134 | return -1; |
| 135 | |
| 136 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 137 | return nCBORError; |
| 138 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 139 | Item.val.int64 != -2147483647) |
| 140 | return -1; |
| 141 | |
| 142 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 143 | return nCBORError; |
| 144 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 145 | Item.val.int64 != -65538) |
| 146 | return -1; |
| 147 | |
| 148 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 149 | return nCBORError; |
| 150 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 151 | Item.val.int64 != -65537) |
| 152 | return -1; |
| 153 | |
| 154 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 155 | return nCBORError; |
| 156 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 157 | Item.val.int64 != -65536) |
| 158 | return -1; |
| 159 | |
| 160 | |
| 161 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 162 | return nCBORError; |
| 163 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 164 | Item.val.int64 != -65535) |
| 165 | return -1; |
| 166 | |
| 167 | |
| 168 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 169 | return nCBORError; |
| 170 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 171 | Item.val.int64 != -65534) |
| 172 | return -1; |
| 173 | |
| 174 | |
| 175 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 176 | return nCBORError; |
| 177 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 178 | Item.val.int64 != -257) |
| 179 | return -1; |
| 180 | |
| 181 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 182 | return nCBORError; |
| 183 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 184 | Item.val.int64 != -256) |
| 185 | return -1; |
| 186 | |
| 187 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 188 | return nCBORError; |
| 189 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 190 | Item.val.int64 != -255) |
| 191 | return -1; |
| 192 | |
| 193 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 194 | return nCBORError; |
| 195 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 196 | Item.val.int64 != -254) |
| 197 | return -1; |
| 198 | |
| 199 | |
| 200 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 201 | return nCBORError; |
| 202 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 203 | Item.val.int64 != -25) |
| 204 | return -1; |
| 205 | |
| 206 | |
| 207 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 208 | return nCBORError; |
| 209 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 210 | Item.val.int64 != -24) |
| 211 | return -1; |
| 212 | |
| 213 | |
| 214 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 215 | return nCBORError; |
| 216 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 217 | Item.val.int64 != -23) |
| 218 | return -1; |
| 219 | |
| 220 | |
| 221 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 222 | return nCBORError; |
| 223 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 224 | Item.val.int64 != -1) |
| 225 | return -1; |
| 226 | |
| 227 | |
| 228 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 229 | return nCBORError; |
| 230 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 231 | Item.val.int64 != 0) |
| 232 | return -1; |
| 233 | |
| 234 | |
| 235 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 236 | return nCBORError; |
| 237 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 238 | Item.val.int64 != 0) |
| 239 | return -1; |
| 240 | |
| 241 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 242 | return nCBORError; |
| 243 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 244 | Item.val.int64 != 1) |
| 245 | return -1; |
| 246 | |
| 247 | |
| 248 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 249 | return nCBORError; |
| 250 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 251 | Item.val.int64 != 22) |
| 252 | return -1; |
| 253 | |
| 254 | |
| 255 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 256 | return nCBORError; |
| 257 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 258 | Item.val.int64 != 23) |
| 259 | return -1; |
| 260 | |
| 261 | |
| 262 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 263 | return nCBORError; |
| 264 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 265 | Item.val.int64 != 24) |
| 266 | return -1; |
| 267 | |
| 268 | |
| 269 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 270 | return nCBORError; |
| 271 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 272 | Item.val.int64 != 25) |
| 273 | return -1; |
| 274 | |
| 275 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 276 | return nCBORError; |
| 277 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 278 | Item.val.int64 != 26) |
| 279 | return -1; |
| 280 | |
| 281 | |
| 282 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 283 | return nCBORError; |
| 284 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 285 | Item.val.int64 != 254) |
| 286 | return -1; |
| 287 | |
| 288 | |
| 289 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 290 | return nCBORError; |
| 291 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 292 | Item.val.int64 != 255) |
| 293 | return -1; |
| 294 | |
| 295 | |
| 296 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 297 | return nCBORError; |
| 298 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 299 | Item.val.int64 != 256) |
| 300 | return -1; |
| 301 | |
| 302 | |
| 303 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 304 | return nCBORError; |
| 305 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 306 | Item.val.int64 != 257) |
| 307 | return -1; |
| 308 | |
| 309 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 310 | return nCBORError; |
| 311 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 312 | Item.val.int64 != 65534) |
| 313 | return -1; |
| 314 | |
| 315 | |
| 316 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 317 | return nCBORError; |
| 318 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 319 | Item.val.int64 != 65535) |
| 320 | return -1; |
| 321 | |
| 322 | |
| 323 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 324 | return nCBORError; |
| 325 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 326 | Item.val.int64 != 65536) |
| 327 | return -1; |
| 328 | |
| 329 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 330 | return nCBORError; |
| 331 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 332 | Item.val.int64 != 65537) |
| 333 | return -1; |
| 334 | |
| 335 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 336 | return nCBORError; |
| 337 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 338 | Item.val.int64 != 65538) |
| 339 | return -1; |
| 340 | |
| 341 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 342 | return nCBORError; |
| 343 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 344 | Item.val.int64 != 2147483647) |
| 345 | return -1; |
| 346 | |
| 347 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 348 | return nCBORError; |
| 349 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 350 | Item.val.int64 != 2147483647) |
| 351 | return -1; |
| 352 | |
| 353 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 354 | return nCBORError; |
| 355 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 356 | Item.val.int64 != 2147483648) |
| 357 | return -1; |
| 358 | |
| 359 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 360 | return nCBORError; |
| 361 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 362 | Item.val.int64 != 2147483649) |
| 363 | return -1; |
| 364 | |
| 365 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 366 | return nCBORError; |
| 367 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 368 | Item.val.int64 != 4294967294) |
| 369 | return -1; |
| 370 | |
| 371 | |
| 372 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 373 | return nCBORError; |
| 374 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 375 | Item.val.int64 != 4294967295) |
| 376 | return -1; |
| 377 | |
| 378 | |
| 379 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 380 | return nCBORError; |
| 381 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 382 | Item.val.int64 != 4294967296) |
| 383 | return -1; |
| 384 | |
| 385 | |
| 386 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 387 | return nCBORError; |
| 388 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 389 | Item.val.int64 != 4294967297) |
| 390 | return -1; |
| 391 | |
| 392 | |
| 393 | |
| 394 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 395 | return nCBORError; |
| 396 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 397 | Item.val.int64 != 9223372036854775807LL) |
| 398 | return -1; |
| 399 | |
| 400 | |
| 401 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 402 | return nCBORError; |
| 403 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 404 | Item.val.uint64 != 18446744073709551615ULL) |
| 405 | return -1; |
| 406 | |
| 407 | |
| 408 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /* |
| 417 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 418 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 419 | */ |
| 420 | |
| 421 | int IntegerValuesParseTest() |
| 422 | { |
| 423 | int n; |
| 424 | QCBORDecodeContext DCtx; |
| 425 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 426 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 427 | |
| 428 | n = IntegerValuesParseTestInternal(&DCtx); |
| 429 | |
| 430 | return(n); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* |
| 435 | Creates a simple CBOR array and returns it in *pEncoded. The array is malloced |
| 436 | and needs to be freed. This is used by several tests. |
| 437 | |
| 438 | Two of the inputs can be set. Two other items in the array are fixed. |
| 439 | |
| 440 | */ |
| 441 | |
| 442 | static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen) |
| 443 | { |
| 444 | QCBOREncodeContext ECtx; |
| 445 | int nReturn = -1; |
| 446 | |
| 447 | *pEncoded = NULL; |
| 448 | *pEncodedLen = INT32_MAX; |
| 449 | |
| 450 | // loop runs CBOR encoding twice. First with no buffer to |
| 451 | // calucate the length so buffer can be allocated correctly, |
| 452 | // and last with the buffer to do the actual encoding |
| 453 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 454 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 455 | QCBOREncode_OpenArray(&ECtx); |
| 456 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 457 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 458 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 459 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 460 | QCBOREncode_CloseArray(&ECtx); |
| 461 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 462 | UsefulBufC Encoded; |
| 463 | if(QCBOREncode_Finish(&ECtx, &Encoded)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 464 | goto Done; |
| 465 | |
| 466 | if(*pEncoded != NULL) { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 467 | *pEncodedLen = Encoded.len; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 468 | nReturn = 0; |
| 469 | goto Done; |
| 470 | } |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 471 | *pEncoded = malloc(Encoded.len); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 472 | if(*pEncoded == NULL) { |
| 473 | nReturn = -1; |
| 474 | goto Done; |
| 475 | } |
| 476 | |
| 477 | } while(1); |
| 478 | Done: |
| 479 | return (nReturn); |
| 480 | |
| 481 | } |
| 482 | |
| 483 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 484 | /* |
| 485 | {"first integer": 42, |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 486 | "an array of two strings": [ |
| 487 | "string1", "string2" |
| 488 | ], |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 489 | "map in a map": { |
| 490 | "bytes 1": h'78787878', |
| 491 | "bytes 2": h'79797979', |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 492 | "another int": 98, |
| 493 | "text 2": "lies, damn lies and statistics" |
| 494 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 495 | } |
| 496 | */ |
| 497 | |
| 498 | static uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 499 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 500 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 501 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 502 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 503 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 504 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 505 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 506 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 507 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 508 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 509 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 510 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 511 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 512 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 513 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 514 | 0x73 } ; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 515 | |
| 516 | static int ParseOrderedArray(const uint8_t *pEncoded, size_t nLen, int64_t *pInt1, int64_t *pInt2, const uint8_t **pBuf3, size_t *pBuf3Len, const uint8_t **pBuf4, size_t *pBuf4Len) |
| 517 | { |
| 518 | QCBORDecodeContext DCtx; |
| 519 | QCBORItem Item; |
| 520 | int nReturn = -1; // assume error until success |
| 521 | |
| 522 | QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL); |
| 523 | |
| 524 | // Make sure the first thing is a map |
| 525 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY) |
| 526 | goto Done; |
| 527 | |
| 528 | // First integer |
| 529 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 | Item.uDataType != QCBOR_TYPE_INT64) |
| 530 | goto Done; |
| 531 | *pInt1 = Item.val.int64; |
| 532 | |
| 533 | // Second integer |
| 534 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64) |
| 535 | goto Done; |
| 536 | *pInt2 = Item.val.int64; |
| 537 | |
| 538 | // First string |
| 539 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING) |
| 540 | goto Done; |
| 541 | *pBuf3 = Item.val.string.ptr; |
| 542 | *pBuf3Len = Item.val.string.len; |
| 543 | |
| 544 | // Second string |
| 545 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING) |
| 546 | goto Done; |
| 547 | *pBuf4 = Item.val.string.ptr; |
| 548 | *pBuf4Len = Item.val.string.len; |
| 549 | |
| 550 | nReturn = 0; |
| 551 | |
| 552 | Done: |
| 553 | return(nReturn); |
| 554 | } |
| 555 | |
| 556 | |
| 557 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 558 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 559 | int SimpleArrayTest() |
| 560 | { |
| 561 | uint8_t *pEncoded; |
| 562 | size_t nEncodedLen; |
| 563 | |
| 564 | int64_t i1, i2; |
| 565 | size_t i3, i4; |
| 566 | const uint8_t *s3, *s4; |
| 567 | |
| 568 | |
| 569 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 570 | return(-1); |
| 571 | } |
| 572 | |
| 573 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
| 574 | |
| 575 | if(i1 != 23 || |
| 576 | i2 != 6000 || |
| 577 | i3 != 8 || |
| 578 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 579 | memcmp("galactic", s3, 8) !=0 || |
| 580 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 581 | printf("SimpleArraryTest Failed\n"); |
| 582 | return(-1); |
| 583 | } |
| 584 | |
| 585 | return(0); |
| 586 | } |
| 587 | |
| 588 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 589 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 590 | static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 591 | |
| 592 | int ParseDeepArrayTest() |
| 593 | { |
| 594 | QCBORDecodeContext DCtx; |
| 595 | int nReturn = 0; |
| 596 | int i; |
| 597 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 598 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 599 | |
| 600 | for(i = 0; i < 10; i++) { |
| 601 | QCBORItem Item; |
| 602 | |
| 603 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 604 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 605 | Item.uNestingLevel != i) { |
| 606 | nReturn = -1; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return(nReturn); |
| 612 | } |
| 613 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 614 | // Big enough to test nesting to the depth of 24 |
| 615 | static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 616 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 617 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 618 | 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 619 | |
| 620 | int ParseTooDeepArrayTest() |
| 621 | { |
| 622 | QCBORDecodeContext DCtx; |
| 623 | int nReturn = 0; |
| 624 | int i; |
| 625 | QCBORItem Item; |
| 626 | |
| 627 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 628 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 629 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 630 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 631 | |
| 632 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 633 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 634 | Item.uNestingLevel != i) { |
| 635 | nReturn = -1; |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) |
| 641 | nReturn = -1; |
| 642 | |
| 643 | return(nReturn); |
| 644 | } |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 649 | int ShortBufferParseTest() |
| 650 | { |
| 651 | int nResult = 0; |
| 652 | QCBORDecodeContext DCtx; |
| 653 | int num; |
| 654 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 655 | for(num = sizeof(spExpectedEncodedInts)-1; num; num--) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 656 | int n; |
| 657 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 658 | QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 659 | |
| 660 | n = IntegerValuesParseTestInternal(&DCtx); |
| 661 | |
| 662 | //printf("Len %d, result: %d\n", num, n); |
| 663 | |
| 664 | if(n != QCBOR_ERR_HIT_END) { |
| 665 | nResult = -1; |
| 666 | goto Done; |
| 667 | } |
| 668 | } |
| 669 | Done: |
| 670 | return nResult; |
| 671 | } |
| 672 | |
| 673 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 674 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 675 | int ShortBufferParseTest2() |
| 676 | { |
| 677 | uint8_t *pEncoded; |
| 678 | int nReturn; |
| 679 | size_t nEncodedLen; |
| 680 | |
| 681 | int64_t i1, i2; |
| 682 | size_t i3, i4; |
| 683 | const uint8_t *s3, *s4; |
| 684 | |
| 685 | nReturn = 0; |
| 686 | |
| 687 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 688 | return(-1); |
| 689 | } |
| 690 | |
| 691 | //printencoded(pEncoded, nEncodedLen); |
| 692 | |
| 693 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
| 694 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
| 695 | if(nResult == 0) { |
| 696 | nReturn = -1; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return(nReturn); |
| 701 | } |
| 702 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 703 | /* |
| 704 | Decode and thoroughly check a moderately complex |
| 705 | set of maps |
| 706 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 707 | static int ParseMapTest1() |
| 708 | { |
| 709 | QCBORDecodeContext DCtx; |
| 710 | QCBORItem Item; |
| 711 | int nCBORError; |
| 712 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 713 | QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL); |
| 714 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 715 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 716 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 717 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 718 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 719 | Item.val.uCount != 3) |
| 720 | return -1; |
| 721 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 722 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 723 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 724 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 725 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 726 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 727 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 728 | Item.uDataAlloc || |
| 729 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 730 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 731 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 732 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 733 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 734 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 735 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 736 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 737 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 738 | Item.uDataAlloc || |
| 739 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 740 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 741 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 742 | Item.val.uCount != 2) |
| 743 | return -1; |
| 744 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 745 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 746 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 747 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 748 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 749 | Item.uDataAlloc || |
| 750 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 751 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 752 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 753 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 754 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 755 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 756 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 757 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 758 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 759 | Item.uDataAlloc || |
| 760 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 761 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 762 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 763 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 764 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 765 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 766 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 767 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 768 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 769 | Item.uDataAlloc || |
| 770 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 771 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 772 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 773 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 774 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 775 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 776 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 777 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 778 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 779 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 780 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 781 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 782 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 783 | Item.uDataAlloc || |
| 784 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 785 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 786 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 787 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 788 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 789 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 790 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 791 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 792 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 793 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 794 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 795 | Item.uDataAlloc || |
| 796 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 797 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 798 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 799 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 800 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 801 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 802 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 803 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 804 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 805 | Item.uDataAlloc || |
| 806 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 807 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 808 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 809 | Item.val.int64 != 98) |
| 810 | return -1; |
| 811 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 812 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 813 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 814 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 815 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 816 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 817 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 818 | Item.uDataAlloc || |
| 819 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 820 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 821 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 822 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 828 | /* |
| 829 | Decode and thoroughly check a moderately complex |
| 830 | set of maps |
| 831 | */ |
| 832 | int ParseMapAsArrayTest() |
| 833 | { |
| 834 | QCBORDecodeContext DCtx; |
| 835 | QCBORItem Item; |
| 836 | int nCBORError; |
| 837 | |
| 838 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 839 | |
| 840 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 841 | return nCBORError; |
| 842 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame^] | 843 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 844 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 849 | return nCBORError; |
| 850 | } |
| 851 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 852 | Item.uDataAlloc || |
| 853 | Item.uLabelAlloc || |
| 854 | Item.uLabelType != QCBOR_TYPE_NONE || |
| 855 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) { |
| 856 | return -2; |
| 857 | } |
| 858 | |
| 859 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 860 | return nCBORError; |
| 861 | } |
| 862 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 863 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 864 | Item.val.int64 != 42 || |
| 865 | Item.uDataAlloc || |
| 866 | Item.uLabelAlloc) { |
| 867 | return -3; |
| 868 | } |
| 869 | |
| 870 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 871 | return nCBORError; |
| 872 | } |
| 873 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 874 | Item.uDataAlloc || |
| 875 | Item.uLabelAlloc || |
| 876 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) || |
| 877 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 878 | return -4; |
| 879 | } |
| 880 | |
| 881 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 882 | return nCBORError; |
| 883 | } |
| 884 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 885 | Item.uDataAlloc || |
| 886 | Item.uLabelAlloc || |
| 887 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 888 | Item.val.uCount != 2) { |
| 889 | return -5; |
| 890 | } |
| 891 | |
| 892 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 893 | return nCBORError; |
| 894 | } |
| 895 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 896 | Item.val.string.len != 7 || |
| 897 | Item.uDataAlloc || |
| 898 | Item.uLabelAlloc || |
| 899 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 900 | return -6; |
| 901 | } |
| 902 | |
| 903 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 904 | return nCBORError; |
| 905 | } |
| 906 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 907 | Item.uDataAlloc || |
| 908 | Item.uLabelAlloc || |
| 909 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 910 | return -7; |
| 911 | } |
| 912 | |
| 913 | |
| 914 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 915 | return nCBORError; |
| 916 | } |
| 917 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 918 | Item.uDataAlloc || |
| 919 | Item.uLabelAlloc || |
| 920 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) { |
| 921 | return -8; |
| 922 | } |
| 923 | |
| 924 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 925 | return nCBORError; |
| 926 | } |
| 927 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 928 | Item.uDataAlloc || |
| 929 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame^] | 930 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 931 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 932 | return -9; |
| 933 | } |
| 934 | |
| 935 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 936 | return nCBORError; |
| 937 | } |
| 938 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 939 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 940 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 941 | Item.uDataAlloc || |
| 942 | Item.uLabelAlloc) { |
| 943 | return -10; |
| 944 | } |
| 945 | |
| 946 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 947 | return nCBORError; |
| 948 | } |
| 949 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 950 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 951 | Item.uDataAlloc || |
| 952 | Item.uLabelAlloc || |
| 953 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
| 954 | return -11; |
| 955 | } |
| 956 | |
| 957 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 958 | return nCBORError; |
| 959 | } |
| 960 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 961 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) || |
| 962 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 963 | Item.uDataAlloc || |
| 964 | Item.uLabelAlloc) { |
| 965 | return -12; |
| 966 | } |
| 967 | |
| 968 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 969 | return nCBORError; |
| 970 | } |
| 971 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 972 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 973 | Item.uDataAlloc || |
| 974 | Item.uLabelAlloc || |
| 975 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
| 976 | return -13; |
| 977 | } |
| 978 | |
| 979 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 980 | return nCBORError; |
| 981 | } |
| 982 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 983 | Item.uDataAlloc || |
| 984 | Item.uLabelAlloc || |
| 985 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) || |
| 986 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 987 | return -14; |
| 988 | } |
| 989 | |
| 990 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 991 | return nCBORError; |
| 992 | } |
| 993 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 994 | Item.uDataAlloc || |
| 995 | Item.uLabelAlloc || |
| 996 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 997 | Item.val.int64 != 98) { |
| 998 | return -15; |
| 999 | } |
| 1000 | |
| 1001 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1002 | return nCBORError; |
| 1003 | } |
| 1004 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1005 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))|| |
| 1006 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1007 | Item.uDataAlloc || |
| 1008 | Item.uLabelAlloc) { |
| 1009 | return -16; |
| 1010 | } |
| 1011 | |
| 1012 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1013 | return nCBORError; |
| 1014 | } |
| 1015 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1016 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1017 | Item.uDataAlloc || |
| 1018 | Item.uLabelAlloc || |
| 1019 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
| 1020 | return -17; |
| 1021 | } |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1026 | |
| 1027 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1028 | Fully or partially decode pValidMapEncoded. When |
| 1029 | partially decoding check for the right error code. |
| 1030 | How much partial decoding depends on nLevel. |
| 1031 | |
| 1032 | The partial decodes test error conditions of |
| 1033 | incomplete encoded input. |
| 1034 | |
| 1035 | This could be combined with the above test |
| 1036 | and made prettier and maybe a little more |
| 1037 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1038 | */ |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1039 | static int ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1040 | { |
| 1041 | QCBORDecodeContext DCtx; |
| 1042 | QCBORItem Item; |
| 1043 | int nCBORError; |
| 1044 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1045 | QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL); |
| 1046 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1047 | if(nLevel < 1) { |
| 1048 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1049 | return -1; |
| 1050 | } else { |
| 1051 | return 0; |
| 1052 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1053 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1054 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1056 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1057 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1058 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1059 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1060 | Item.val.uCount != 3) |
| 1061 | return -1; |
| 1062 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1063 | if(nLevel < 2) { |
| 1064 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1065 | return -1; |
| 1066 | } else { |
| 1067 | return 0; |
| 1068 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1071 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1072 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1073 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1074 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1075 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1076 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1077 | Item.val.uCount != 42 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1078 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1079 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1080 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1081 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1082 | if(nLevel < 3) { |
| 1083 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1084 | return -1; |
| 1085 | } else { |
| 1086 | return 0; |
| 1087 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1090 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1091 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1092 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1093 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1094 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1095 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1096 | Item.val.uCount != 2) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1097 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1098 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1099 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1100 | |
| 1101 | if(nLevel < 4) { |
| 1102 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1103 | return -1; |
| 1104 | } else { |
| 1105 | return 0; |
| 1106 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1110 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1111 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1112 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1113 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1114 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1115 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1116 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1117 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1118 | if(nLevel < 5) { |
| 1119 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1120 | return -1; |
| 1121 | } else { |
| 1122 | return 0; |
| 1123 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1126 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1127 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1128 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1129 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1130 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1131 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1132 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1133 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1134 | if(nLevel < 6) { |
| 1135 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1136 | return -1; |
| 1137 | } else { |
| 1138 | return 0; |
| 1139 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1142 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1143 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1144 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1145 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1146 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1147 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1148 | Item.val.uCount != 4) |
| 1149 | return -1; |
| 1150 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1151 | if(nLevel < 7) { |
| 1152 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1153 | return -1; |
| 1154 | } else { |
| 1155 | return 0; |
| 1156 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1159 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1160 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1161 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1162 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1163 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1164 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1165 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1166 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1167 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1168 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1169 | if(nLevel < 8) { |
| 1170 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1171 | return -1; |
| 1172 | } else { |
| 1173 | return 0; |
| 1174 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1177 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1178 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1179 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1180 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1181 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1182 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1183 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1184 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1185 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1186 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1187 | if(nLevel < 9) { |
| 1188 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1189 | return -1; |
| 1190 | } else { |
| 1191 | return 0; |
| 1192 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1195 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1196 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1197 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1198 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1199 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1200 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1201 | Item.val.int64 != 98) |
| 1202 | return -1; |
| 1203 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1204 | if(nLevel < 10) { |
| 1205 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1206 | return -1; |
| 1207 | } else { |
| 1208 | return 0; |
| 1209 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1212 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1213 | return nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1214 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1215 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1216 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1217 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1218 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1219 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1220 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1221 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1222 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1223 | return -1; |
| 1224 | } |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | int ParseMapTest() |
| 1233 | { |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1234 | // Parse a moderatly complex map structure very thoroughl |
| 1235 | int n = ParseMapTest1(); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1236 | |
| 1237 | if(!n) { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1238 | for(int i = 0; i < 10; i++) { |
| 1239 | n = ExtraBytesTest(i); |
| 1240 | if(n) { |
| 1241 | break; |
| 1242 | } |
| 1243 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | return(n); |
| 1247 | } |
| 1248 | |
| 1249 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1250 | static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1251 | |
| 1252 | int ParseSimpleTest() |
| 1253 | { |
| 1254 | QCBORDecodeContext DCtx; |
| 1255 | QCBORItem Item; |
| 1256 | int nCBORError; |
| 1257 | |
| 1258 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1259 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1260 | |
| 1261 | |
| 1262 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1263 | return nCBORError; |
| 1264 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1265 | Item.val.uCount != 10) |
| 1266 | return -1; |
| 1267 | |
| 1268 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1269 | return nCBORError; |
| 1270 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1271 | return -1; |
| 1272 | |
| 1273 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1274 | return nCBORError; |
| 1275 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1276 | return -1; |
| 1277 | |
| 1278 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1279 | return nCBORError; |
| 1280 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1281 | return -1; |
| 1282 | |
| 1283 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1284 | return nCBORError; |
| 1285 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1286 | return -1; |
| 1287 | |
| 1288 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1289 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1290 | return -1; |
| 1291 | |
| 1292 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1293 | return nCBORError; |
| 1294 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1295 | return -1; |
| 1296 | |
| 1297 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1298 | return nCBORError; |
| 1299 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1300 | return -1; |
| 1301 | |
| 1302 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR) |
| 1303 | return -1; |
| 1304 | |
| 1305 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR) |
| 1306 | return -1; |
| 1307 | |
| 1308 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR) |
| 1309 | return -1; |
| 1310 | |
| 1311 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1312 | return nCBORError; |
| 1313 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1314 | return -1; |
| 1315 | |
| 1316 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1317 | return nCBORError; |
| 1318 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1319 | return -1; |
| 1320 | |
| 1321 | return 0; |
| 1322 | |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | struct FailInput { |
| 1327 | UsefulBufC Input; |
| 1328 | int nError; |
| 1329 | }; |
| 1330 | |
| 1331 | |
| 1332 | struct FailInput Failures[] = { |
| 1333 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte |
| 1334 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28 |
| 1335 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29 |
| 1336 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30 |
| 1337 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer |
| 1338 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte |
| 1339 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte |
| 1340 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte |
| 1341 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer |
| 1342 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string |
| 1343 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28 |
| 1344 | { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string |
| 1345 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string |
| 1346 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28 |
| 1347 | { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string |
| 1348 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break |
| 1349 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type |
| 1350 | { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type |
| 1351 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer |
| 1352 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string |
| 1353 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates |
| 1354 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string |
| 1355 | |
| 1356 | }; |
| 1357 | |
| 1358 | |
| 1359 | void Dump(UsefulBufC Input, int x) |
| 1360 | { |
| 1361 | char label[10]; |
| 1362 | |
| 1363 | sprintf(label, "%d", x); |
| 1364 | |
| 1365 | printencoded(label, Input.ptr, Input.len); |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | int FailureTests() |
| 1370 | { |
| 1371 | int nResult = 0; |
| 1372 | |
| 1373 | struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput); |
| 1374 | |
| 1375 | for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) { |
| 1376 | QCBORDecodeContext DCtx; |
| 1377 | QCBORItem Item; |
| 1378 | int nCBORError; |
| 1379 | |
| 1380 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
| 1381 | |
| 1382 | while(1) { |
| 1383 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1384 | if(QCBOR_ERR_HIT_END == nCBORError) { |
| 1385 | break; |
| 1386 | } |
| 1387 | if(nCBORError != pF->nError) { |
| 1388 | nResult = 1; |
| 1389 | // Dump(pF->Input, nCBORError); |
| 1390 | break; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | { |
| 1396 | QCBORDecodeContext DCtx; |
| 1397 | QCBORItem Item; |
| 1398 | int nCBORError; |
| 1399 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1400 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1401 | |
| 1402 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1403 | return nCBORError; |
| 1404 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1405 | Item.val.uCount != 10) |
| 1406 | return -1; |
| 1407 | |
| 1408 | DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf |
| 1409 | |
| 1410 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1411 | if(nCBORError != QCBOR_ERR_HIT_END) |
| 1412 | return -1; |
| 1413 | } |
| 1414 | |
| 1415 | |
| 1416 | return nResult; |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | static void Recurser(uint8_t *pBuf, int nLen, int nLenMax) |
| 1423 | { |
| 1424 | |
| 1425 | if(nLen >= nLenMax) { |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | //printf("__%d__%d__\n", nLen, nLenMax); |
| 1430 | |
| 1431 | for(int i = 0; i < 256; i++) { |
| 1432 | pBuf[nLen] = i; |
| 1433 | |
| 1434 | QCBORDecodeContext DCtx; |
| 1435 | QCBORItem Item; |
| 1436 | int nCBORError; |
| 1437 | |
| 1438 | UsefulBufC Input = {pBuf, nLen+1}; |
| 1439 | |
| 1440 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
| 1441 | |
| 1442 | while(1) { |
| 1443 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1444 | if(QCBOR_ERR_HIT_END == nCBORError) { |
| 1445 | break; |
| 1446 | } |
| 1447 | if(nCBORError != QCBOR_SUCCESS) { |
| 1448 | if(nCBORError != QCBOR_ERR_UNSUPPORTED && nCBORError != QCBOR_ERR_HIT_END && nCBORError != QCBOR_ERR_INVALID_CBOR) { |
| 1449 | //Dump(Input, nCBORError); |
| 1450 | } |
| 1451 | break; |
| 1452 | } |
| 1453 | } |
| 1454 | //Dump(Input, -1); |
| 1455 | |
| 1456 | |
| 1457 | Recurser(pBuf, nLen+1, nLenMax); |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | /* |
| 1463 | Runs all possible input strings of a given length. This is set to 3 to make the test |
| 1464 | run in reasonable time. |
| 1465 | Main point of this test is to not crash. |
| 1466 | */ |
| 1467 | |
| 1468 | int ComprehensiveInputTest() |
| 1469 | { |
| 1470 | uint8_t pBuf[3]; // 3 keeps it running in reasonable time. 4 takes tens of minutes. |
| 1471 | |
| 1472 | Recurser(pBuf, 0, sizeof(pBuf)); |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1477 | static uint8_t spDateTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1478 | 0xc0, // tag for string date |
| 1479 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 1480 | |
| 1481 | 0xc1, // tag for epoch date |
| 1482 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 1483 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1484 | // CBOR_TAG_B64 |
| 1485 | 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] | 1486 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 1487 | |
| 1488 | 0xc1, // tag for epoch date |
| 1489 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
| 1490 | |
| 1491 | 0xc1, // tag for epoch date |
| 1492 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1 |
| 1493 | |
| 1494 | 0xc1, // tag for epoch date |
| 1495 | 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 1496 | |
| 1497 | }; |
| 1498 | |
| 1499 | |
| 1500 | // have to check float expected only to within an epsilon |
| 1501 | int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
| 1502 | |
| 1503 | double diff = val - expected; |
| 1504 | |
| 1505 | diff = fabs(diff); |
| 1506 | |
| 1507 | return diff > 0.0000001; |
| 1508 | } |
| 1509 | |
| 1510 | |
| 1511 | int DateParseTest() |
| 1512 | { |
| 1513 | QCBORDecodeContext DCtx; |
| 1514 | QCBORItem Item; |
| 1515 | int nCBORError; |
| 1516 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1517 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1518 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1519 | const uint64_t uTags[] = {15}; |
| 1520 | QCBORTagListIn TagList = {1, uTags}; |
| 1521 | |
| 1522 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
| 1523 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1524 | // String date |
| 1525 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1526 | return -1; |
| 1527 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1528 | UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1529 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | // Epoch date |
| 1533 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1534 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1535 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 1536 | Item.val.epochDate.nSeconds != 1400000000 || |
| 1537 | Item.val.epochDate.fSecondsFraction != 0 ) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1538 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1539 | } |
| 1540 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1541 | // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything |
| 1542 | // but want to be sure extra tag doesn't cause a problem |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1543 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1544 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1545 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 1546 | Item.val.epochDate.nSeconds != 1400000001 || |
| 1547 | Item.val.epochDate.fSecondsFraction != 0 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1548 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1549 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | // Epoch date that is too large for our representation |
| 1553 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1554 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | // Epoch date in float format with fractional seconds |
| 1558 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1559 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1560 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 1561 | Item.val.epochDate.nSeconds != 1 || |
| 1562 | CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1563 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | // Epoch date float that is too large for our representation |
| 1567 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1568 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good) |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1576 | // Really simple basic input for tagging test |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1577 | static uint8_t spOptTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1578 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1579 | 0x81, // Array of one |
| 1580 | 0xd8, 0x04, // non-preferred serialization of tag 4 |
| 1581 | 0x82, 0x01, 0x03}; // fraction 1/3 |
| 1582 | |
| 1583 | static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80}; |
| 1584 | |
| 1585 | // 0x9192939495969798, 0x88, 0x01, 0x04 |
| 1586 | static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80}; |
| 1587 | |
| 1588 | /* |
| 1589 | The cbor.me parse of this. |
| 1590 | 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))), |
| 1591 | 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"), |
| 1592 | 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}), |
| 1593 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})), |
| 1594 | 16(-22): 23({11(8(7(-5))): 8(-3)})}))) |
| 1595 | */ |
| 1596 | static uint8_t spCSRWithTags[] = { |
| 1597 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 1598 | 0xc6, 0xc7, 0x36, |
| 1599 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 1600 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 1601 | 0xcb, 0xa5, |
| 1602 | 0xd1, 0x31, |
| 1603 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 1604 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 1605 | 0xc9, 0x30, |
| 1606 | 0xd9, 0x03, 0x05, 0x63, |
| 1607 | 0x53, 0x53, 0x47, |
| 1608 | 0x2e, |
| 1609 | 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69, |
| 1610 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
| 1611 | 0xd1, 0x2f, |
| 1612 | 0xd1, 0x69, |
| 1613 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 1614 | 0xd1, 0x2d, |
| 1615 | 0xd1, 0x62, |
| 1616 | 0x55, 0x53, |
| 1617 | 0xd7, 0x32, |
| 1618 | 0xd3, 0xa2, |
| 1619 | 0x2a, |
| 1620 | 0xc9, 0xa1, |
| 1621 | 0x28, |
| 1622 | 0x26, |
| 1623 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 1624 | 0xcc, 0x4a, |
| 1625 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 1626 | 0xd0, 0x35, |
| 1627 | 0xd7, 0xa1, |
| 1628 | 0xcb, 0xc8, 0xc7, 0x24, |
| 1629 | 0xc8, 0x22}; |
| 1630 | |
| 1631 | static int CheckCSRMaps(QCBORDecodeContext *pDC); |
| 1632 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1633 | |
| 1634 | int OptTagParseTest() |
| 1635 | { |
| 1636 | QCBORDecodeContext DCtx; |
| 1637 | QCBORItem Item; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1638 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1639 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1640 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1641 | //------------------------- |
| 1642 | // This text matches the magic number tag and the fraction tag |
| 1643 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 1644 | return -2; |
| 1645 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1646 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1647 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 1648 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1649 | } |
| 1650 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1651 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 1652 | return -4; |
| 1653 | } |
| 1654 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1655 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) || |
| 1656 | Item.val.uCount != 2) { |
| 1657 | return -5; |
| 1658 | } |
| 1659 | |
| 1660 | // -------------------------------- |
| 1661 | // This test decodes the very large tag, but it is not in |
| 1662 | // any list so it is ignored. |
| 1663 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL); |
| 1664 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 1665 | return -6; |
| 1666 | } |
| 1667 | if(Item.uTagBits) { |
| 1668 | return -7; |
| 1669 | } |
| 1670 | |
| 1671 | // ---------------------------------- |
| 1672 | // This test sets up a caller-config list that includes the very large tage and then matches it. |
| 1673 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL); |
| 1674 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 1675 | const QCBORTagListIn TL = {2, puList}; |
| 1676 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
| 1677 | |
| 1678 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 1679 | return -8; |
| 1680 | } |
| 1681 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1682 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 1683 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 1684 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 1685 | Item.val.uCount != 0) { |
| 1686 | return -9; |
| 1687 | } |
| 1688 | |
| 1689 | //------------------------ |
| 1690 | // This test sets up a caller-configured list, and looks up something not in it |
| 1691 | const uint64_t puLongList[17] = {1,2,1}; |
| 1692 | const QCBORTagListIn TLLong = {17, puLongList}; |
| 1693 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL); |
| 1694 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 1695 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 1696 | return -11; |
| 1697 | } |
| 1698 | |
| 1699 | // ----------------------- |
| 1700 | // This tests retrievel of the full tag list |
| 1701 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL); |
| 1702 | uint64_t puTags[16]; |
| 1703 | QCBORTagListOut Out = {0, 4, puTags}; |
| 1704 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1705 | return -12; |
| 1706 | } |
| 1707 | if(puTags[0] != 0x9192939495969798 || |
| 1708 | puTags[1] != 0x88 || |
| 1709 | puTags[2] != 0x05 || |
| 1710 | puTags[3] != 0x04) { |
| 1711 | return -13; |
| 1712 | } |
| 1713 | |
| 1714 | // ---------------------- |
| 1715 | // This text if too small of an out list |
| 1716 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL); |
| 1717 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 1718 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 1719 | return -14; |
| 1720 | } |
| 1721 | |
| 1722 | // --------------- |
| 1723 | // Parse a version of the "CSR" that has had a ton of tags randomly inserted |
| 1724 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL); |
| 1725 | int n = CheckCSRMaps(&DCtx); |
| 1726 | if(n) { |
| 1727 | return n-2000; |
| 1728 | } |
| 1729 | |
| 1730 | Out = (QCBORTagListOut){0,16, puTags}; |
| 1731 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL); |
| 1732 | |
| 1733 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 1734 | const QCBORTagListIn TagList = {3, puTagList}; |
| 1735 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
| 1736 | |
| 1737 | |
| 1738 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1739 | return -100; |
| 1740 | } |
| 1741 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1742 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 1743 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 1744 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 1745 | Item.val.uCount != 2 || |
| 1746 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 1747 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 1748 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 1749 | Out.uNumUsed != 3) { |
| 1750 | return -101; |
| 1751 | } |
| 1752 | |
| 1753 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1754 | return -102; |
| 1755 | } |
| 1756 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1757 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 1758 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
| 1759 | QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized |
| 1760 | Item.val.uCount != 2 || |
| 1761 | puTags[0] != 5859837686836516696 || |
| 1762 | puTags[1] != 7 || |
| 1763 | Out.uNumUsed != 2) { |
| 1764 | return -103; |
| 1765 | } |
| 1766 | |
| 1767 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1768 | return -104; |
| 1769 | } |
| 1770 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1771 | Item.uTagBits || |
| 1772 | Item.val.uCount != 5 || |
| 1773 | puTags[0] != 0x0b || |
| 1774 | Out.uNumUsed != 1) { |
| 1775 | return -105; |
| 1776 | } |
| 1777 | |
| 1778 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1779 | return -106; |
| 1780 | } |
| 1781 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1782 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 1783 | Item.val.string.len != 12 || |
| 1784 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 1785 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 1786 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 1787 | Out.uNumUsed != 3) { |
| 1788 | return -105; |
| 1789 | } |
| 1790 | |
| 1791 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1792 | return -107; |
| 1793 | } |
| 1794 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1795 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 1796 | Item.val.string.len != 3 || |
| 1797 | puTags[0] != 773 || |
| 1798 | Out.uNumUsed != 1) { |
| 1799 | return -108; |
| 1800 | } |
| 1801 | |
| 1802 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1803 | return -109; |
| 1804 | } |
| 1805 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1806 | !QCBORDecode_IsTagged(&DCtx, &Item, 4) || |
| 1807 | Item.val.string.len != 9 || |
| 1808 | puTags[0] != 4 || |
| 1809 | puTags[11] != 0x0f || |
| 1810 | Out.uNumUsed != 12) { |
| 1811 | return -110; |
| 1812 | } |
| 1813 | |
| 1814 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1815 | return -111; |
| 1816 | } |
| 1817 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1818 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 1819 | Item.val.string.len != 9 || |
| 1820 | puTags[0] != 17 || |
| 1821 | Out.uNumUsed != 1) { |
| 1822 | return -112; |
| 1823 | } |
| 1824 | |
| 1825 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1826 | return -111; |
| 1827 | } |
| 1828 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1829 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 1830 | Item.val.string.len != 2 || |
| 1831 | puTags[0] != 17 || |
| 1832 | Out.uNumUsed != 1) { |
| 1833 | return -112; |
| 1834 | } |
| 1835 | |
| 1836 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1837 | return -113; |
| 1838 | } |
| 1839 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1840 | QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
| 1841 | Item.val.uCount != 2 || |
| 1842 | puTags[0] != 19 || |
| 1843 | Out.uNumUsed != 1) { |
| 1844 | return -114; |
| 1845 | } |
| 1846 | |
| 1847 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1848 | return -115; |
| 1849 | } |
| 1850 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1851 | QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
| 1852 | Item.uTagBits || |
| 1853 | Item.val.uCount != 1 || |
| 1854 | puTags[0] != 9 || |
| 1855 | Out.uNumUsed != 1) { |
| 1856 | return -116; |
| 1857 | } |
| 1858 | |
| 1859 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1860 | return -116; |
| 1861 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1862 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1863 | Item.val.int64 != -7 || |
| 1864 | Item.uTagBits || |
| 1865 | Out.uNumUsed != 0) { |
| 1866 | return -117; |
| 1867 | } |
| 1868 | |
| 1869 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1870 | return -118; |
| 1871 | } |
| 1872 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1873 | Item.val.string.len != 10 || |
| 1874 | Item.uTagBits || |
| 1875 | puTags[0] != 12 || |
| 1876 | Out.uNumUsed != 1) { |
| 1877 | return -119; |
| 1878 | } |
| 1879 | |
| 1880 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1881 | return -120; |
| 1882 | } |
| 1883 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1884 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 1885 | Item.val.uCount != 1 || |
| 1886 | puTags[0] != 0x17 || |
| 1887 | Out.uNumUsed != 1) { |
| 1888 | return -121; |
| 1889 | } |
| 1890 | |
| 1891 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 1892 | return -122; |
| 1893 | } |
| 1894 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 1895 | QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
| 1896 | Item.val.int64 != -3 || |
| 1897 | puTags[0] != 8 || |
| 1898 | Out.uNumUsed != 1) { |
| 1899 | return -123; |
| 1900 | } |
| 1901 | |
| 1902 | if(QCBORDecode_Finish(&DCtx)) { |
| 1903 | return -124; |
| 1904 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1905 | |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | |
| 1910 | |
| 1911 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1912 | static uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1913 | 0x83, |
| 1914 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1915 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1916 | 0xA4, |
| 1917 | 0x63, 0x42, 0x4E, 0x2B, |
| 1918 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1919 | 0x18, 0x40, |
| 1920 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1921 | 0x63, 0x42, 0x4E, 0x2D, |
| 1922 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1923 | 0x38, 0x3F, |
| 1924 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 1925 | |
| 1926 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1927 | 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] | 1928 | |
| 1929 | |
| 1930 | int BignumParseTest() |
| 1931 | { |
| 1932 | QCBORDecodeContext DCtx; |
| 1933 | QCBORItem Item; |
| 1934 | int nCBORError; |
| 1935 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1936 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1937 | |
| 1938 | |
| 1939 | // |
| 1940 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1941 | return -1; |
| 1942 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 1943 | return -1; |
| 1944 | } |
| 1945 | |
| 1946 | // |
| 1947 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1948 | return -1; |
| 1949 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1950 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1951 | return -1; |
| 1952 | } |
| 1953 | |
| 1954 | // |
| 1955 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1956 | return -1; |
| 1957 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1958 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1959 | return -1; |
| 1960 | } |
| 1961 | |
| 1962 | // |
| 1963 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1964 | return -1; |
| 1965 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 1966 | return -1; |
| 1967 | } |
| 1968 | |
| 1969 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1970 | return -1; |
| 1971 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 1972 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1973 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1974 | return -1; |
| 1975 | } |
| 1976 | |
| 1977 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1978 | return -1; |
| 1979 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 1980 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 1981 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1982 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1983 | return -1; |
| 1984 | } |
| 1985 | |
| 1986 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1987 | return -1; |
| 1988 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 1989 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1990 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1991 | return -1; |
| 1992 | } |
| 1993 | |
| 1994 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 1995 | return -1; |
| 1996 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 1997 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 1998 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 1999 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2000 | return -1; |
| 2001 | } |
| 2002 | |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2008 | static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, uint8_t uDataType, uint8_t uNestingLevel, uint8_t uNextNest, int64_t nLabel, QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2009 | { |
| 2010 | QCBORItem Item; |
| 2011 | int nCBORError; |
| 2012 | |
| 2013 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 2014 | if(Item.uDataType != uDataType) return -1; |
| 2015 | if(uNestingLevel > 0) { |
| 2016 | if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1; |
| 2017 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 2018 | if(Item.label.int64 != nLabel) return -1; |
| 2019 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2020 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2024 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2025 | |
| 2026 | if(pItem) { |
| 2027 | *pItem = Item; |
| 2028 | } |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2033 | // Same code checks definite and indefinite length versions of the map |
| 2034 | static int CheckCSRMaps(QCBORDecodeContext *pDC) |
| 2035 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2036 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2037 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2038 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2039 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2040 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2041 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2042 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1; |
| 2043 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1; |
| 2044 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1; |
| 2045 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1; |
| 2046 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2047 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2048 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1; |
| 2049 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2050 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2051 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1; |
| 2052 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2053 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2054 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1; |
| 2055 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2056 | |
| 2057 | if(QCBORDecode_Finish(pDC)) return -2; |
| 2058 | |
| 2059 | return 0; |
| 2060 | } |
| 2061 | |
| 2062 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2063 | /* |
| 2064 | // cbor.me decoded output |
| 2065 | { |
| 2066 | -23: { |
| 2067 | -20: { |
| 2068 | -18: "Organization", |
| 2069 | -17: "SSG", |
| 2070 | -15: "Confusion", |
| 2071 | -16: "San Diego", |
| 2072 | -14: "US" |
| 2073 | }, |
| 2074 | -19: { |
| 2075 | -11: { |
| 2076 | -9: -7 |
| 2077 | }, |
| 2078 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 2079 | } |
| 2080 | }, |
| 2081 | -22: { |
| 2082 | -5: -3 |
| 2083 | } |
| 2084 | } |
| 2085 | */ |
| 2086 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2087 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2088 | static uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2089 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 2090 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 2091 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 2092 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 2093 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 2094 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 2095 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 2096 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 2097 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 2098 | |
| 2099 | int NestedMapTest() |
| 2100 | { |
| 2101 | QCBORDecodeContext DCtx; |
| 2102 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2103 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2104 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2105 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2106 | } |
| 2107 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2108 | // Same map as above, but using indefinite lengths |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2109 | static uint8_t spCSRInputIndefLen[] = { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2110 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 2111 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 2112 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 2113 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 2114 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 2115 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2116 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 2117 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 2118 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 2119 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2120 | |
| 2121 | int NestedMapTestIndefLen() |
| 2122 | { |
| 2123 | QCBORDecodeContext DCtx; |
| 2124 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2125 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2126 | |
| 2127 | return CheckCSRMaps(&DCtx); |
| 2128 | } |
| 2129 | |
| 2130 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2131 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2132 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 2133 | { |
| 2134 | UsefulOutBuf UOB; |
| 2135 | UsefulOutBuf_Init(&UOB, Storage); |
| 2136 | |
| 2137 | int i; |
| 2138 | for(i = 0; i < n; i++) { |
| 2139 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 2140 | } |
| 2141 | |
| 2142 | for(i = 0; i < n; i++) { |
| 2143 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 2144 | } |
| 2145 | return UsefulOutBuf_OutUBuf(&UOB); |
| 2146 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2147 | |
| 2148 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2149 | static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
| 2150 | { |
| 2151 | QCBORDecodeContext DC; |
| 2152 | QCBORDecode_Init(&DC, Nested, 0); |
| 2153 | |
| 2154 | int j; |
| 2155 | for(j = 0; j < nNestLevel; j++) { |
| 2156 | QCBORItem Item; |
| 2157 | int nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 2158 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 2159 | // Should be in error |
| 2160 | if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
| 2161 | return -4; |
| 2162 | } else { |
| 2163 | return 0; // Decoding doesn't recover after an error |
| 2164 | } |
| 2165 | } else { |
| 2166 | // Should be no error |
| 2167 | if(nReturn) { |
| 2168 | return -9; // Should not have got an error |
| 2169 | } |
| 2170 | } |
| 2171 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2172 | return -7; |
| 2173 | } |
| 2174 | } |
| 2175 | int nReturn = QCBORDecode_Finish(&DC); |
| 2176 | if(nReturn) { |
| 2177 | return -3; |
| 2178 | } |
| 2179 | return 0; |
| 2180 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2181 | |
| 2182 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2183 | int IndefiniteLengthNestTest() |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2184 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2185 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2186 | int i; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2187 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2188 | UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
| 2189 | int nReturn = parse_indeflen_nested(Nested, i); |
| 2190 | if(nReturn) { |
| 2191 | return nReturn; |
| 2192 | } |
| 2193 | } |
| 2194 | return 0; |
| 2195 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2196 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2197 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2198 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2199 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]] |
| 2200 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break |
| 2201 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks |
| 2202 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks |
| 2203 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len |
| 2204 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2205 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2206 | int IndefiniteLengthArrayMapTest() |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2207 | { |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2208 | int nResult; |
| 2209 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2210 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2211 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2212 | // Decode it and see if it is OK |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2213 | UsefulBuf_MAKE_STACK_UB(MemPool, 150); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2214 | QCBORDecodeContext DC; |
| 2215 | QCBORItem Item; |
| 2216 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2217 | |
| 2218 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2219 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2220 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2221 | |
| 2222 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2223 | Item.uNestingLevel != 0 || |
| 2224 | Item.uNextNestLevel != 1) { |
| 2225 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2229 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 2230 | Item.uNestingLevel != 1 || |
| 2231 | Item.uNextNestLevel != 1) { |
| 2232 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2236 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2237 | Item.uNestingLevel != 1 || |
| 2238 | Item.uNextNestLevel != 2) { |
| 2239 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2243 | if(Item.uDataType != QCBOR_TYPE_INT64 | |
| 2244 | Item.uNestingLevel != 2 || |
| 2245 | Item.uNextNestLevel != 2) { |
| 2246 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2250 | if(Item.uDataType != QCBOR_TYPE_INT64 | |
| 2251 | Item.uNestingLevel != 2 || |
| 2252 | Item.uNextNestLevel != 0) { |
| 2253 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2257 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2258 | } |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2259 | |
| 2260 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2261 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2262 | |
| 2263 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2264 | |
| 2265 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
| 2266 | |
| 2267 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2268 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2269 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2270 | } |
| 2271 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2272 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2273 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2274 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2279 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2280 | |
| 2281 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2282 | |
| 2283 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
| 2284 | |
| 2285 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2286 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2287 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2291 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2292 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2296 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2297 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2301 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2302 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | |
| 2306 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2307 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2308 | |
| 2309 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2310 | |
| 2311 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
| 2312 | |
| 2313 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2314 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2315 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2319 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 2320 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2321 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2322 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2323 | |
| 2324 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2325 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2326 | |
| 2327 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2328 | |
| 2329 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
| 2330 | |
| 2331 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2332 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2333 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2334 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2335 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2336 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2337 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2338 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2342 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2343 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2344 | } |
| 2345 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2346 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2347 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2348 | |
| 2349 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2350 | |
| 2351 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
| 2352 | |
| 2353 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2354 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2355 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2359 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 2360 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2361 | } |
| 2362 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2363 | return 0; |
| 2364 | } |
| 2365 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2366 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2367 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2368 | 0x81, // Array of length one |
| 2369 | 0x7f, // text string marked with indefinite length |
| 2370 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 2371 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 2372 | 0xff // ending break |
| 2373 | }; |
| 2374 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2375 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2376 | 0x81, // Array of length one |
| 2377 | 0x7f, // text string marked with indefinite length |
| 2378 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 2379 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 2380 | 0xff // ending break |
| 2381 | }; |
| 2382 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2383 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2384 | 0x81, // Array of length one |
| 2385 | 0x7f, // text string marked with indefinite length |
| 2386 | 0x01, 0x02, // Not a string |
| 2387 | 0xff // ending break |
| 2388 | }; |
| 2389 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2390 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2391 | 0x81, // Array of length one |
| 2392 | 0x7f, // text string marked with indefinite length |
| 2393 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 2394 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 2395 | // missing end of string |
| 2396 | }; |
| 2397 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2398 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2399 | 0xa1, // Array of length one |
| 2400 | 0x7f, // text string marked with indefinite length |
| 2401 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 2402 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 2403 | 0xff, // ending break |
| 2404 | 0x01 // integer being labeled. |
| 2405 | }; |
| 2406 | |
| 2407 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
| 2408 | { |
| 2409 | UsefulOutBuf UOB; |
| 2410 | |
| 2411 | UsefulOutBuf_Init(&UOB, Storage); |
| 2412 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 2413 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
| 2414 | |
| 2415 | int i = 0; |
| 2416 | for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) { |
| 2417 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
| 2418 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize); |
| 2419 | for(int j = 0; j < nChunkSize; j++ ) { |
| 2420 | UsefulOutBuf_AppendByte(&UOB, i); |
| 2421 | i++; |
| 2422 | } |
| 2423 | } |
| 2424 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 2425 | |
| 2426 | return UsefulOutBuf_OutUBuf(&UOB); |
| 2427 | } |
| 2428 | |
| 2429 | static int CheckBigString(UsefulBufC BigString) |
| 2430 | { |
| 2431 | if(BigString.len != 255) { |
| 2432 | return 1; |
| 2433 | } |
| 2434 | |
| 2435 | for(uint8_t i = 0; i < 255; i++){ |
| 2436 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 2437 | return 1; |
| 2438 | } |
| 2439 | } |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 2443 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2444 | int IndefiniteLengthStringTest() |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2445 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2446 | QCBORDecodeContext DC; |
| 2447 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 2448 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2449 | UsefulBuf_MAKE_STACK_UB(MemPool, 320); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2450 | |
| 2451 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2452 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2453 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2454 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 2455 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2456 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2457 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2458 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2459 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 2460 | return -2; |
| 2461 | } |
| 2462 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 2463 | return -3; |
| 2464 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2465 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2466 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 2467 | return -4; |
| 2468 | } |
| 2469 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 2470 | return -5; |
| 2471 | } |
| 2472 | if(QCBORDecode_Finish(&DC)) { |
| 2473 | return -6; |
| 2474 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2475 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2476 | // ----- types mismatch --- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2477 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2478 | |
| 2479 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 2480 | return -7; |
| 2481 | } |
| 2482 | |
| 2483 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 2484 | return -8; |
| 2485 | } |
| 2486 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2487 | return -9; |
| 2488 | } |
| 2489 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2490 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2491 | return -10; |
| 2492 | } |
| 2493 | |
| 2494 | // ----- not a string --- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2495 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2496 | |
| 2497 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 2498 | return -11; |
| 2499 | } |
| 2500 | |
| 2501 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 2502 | return -12; |
| 2503 | } |
| 2504 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2505 | return -13; |
| 2506 | } |
| 2507 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2508 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2509 | return -14; |
| 2510 | } |
| 2511 | |
| 2512 | // ----- no end ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2513 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2514 | |
| 2515 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 2516 | return -15; |
| 2517 | } |
| 2518 | |
| 2519 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 2520 | return -16; |
| 2521 | } |
| 2522 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2523 | return -17; |
| 2524 | } |
| 2525 | |
| 2526 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 2527 | return -18; |
| 2528 | } |
| 2529 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2530 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2531 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2532 | |
| 2533 | QCBORDecode_GetNext(&DC, &Item); |
| 2534 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2535 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2539 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2543 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2544 | |
| 2545 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 2546 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2547 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2548 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2549 | |
| 2550 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2551 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2552 | UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2553 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2554 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2555 | |
| 2556 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2557 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2558 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | QCBORDecode_GetNext(&DC, &Item); |
| 2562 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2563 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2564 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2565 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2566 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 2567 | } |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2568 | |
| 2569 | // ---- big bstr ----- |
| 2570 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
| 2571 | |
| 2572 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 2573 | return -25; |
| 2574 | } |
| 2575 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 2576 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 2577 | return -26; |
| 2578 | } |
| 2579 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2580 | return -26; |
| 2581 | } |
| 2582 | |
| 2583 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 2584 | return -27; |
| 2585 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 2586 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2587 | return -28; |
| 2588 | } |
| 2589 | if(CheckBigString(Item.val.string)) { |
| 2590 | return -3; |
| 2591 | } |
| 2592 | if(QCBORDecode_Finish(&DC)) { |
| 2593 | return -29; |
| 2594 | } |
| 2595 | |
| 2596 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2597 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 2598 | |
| 2599 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 2600 | return -30; |
| 2601 | } |
| 2602 | |
| 2603 | QCBORDecode_GetNext(&DC, &Item); |
| 2604 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 2605 | return -31; |
| 2606 | } |
| 2607 | |
| 2608 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 2609 | return -32; |
| 2610 | } |
| 2611 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 || |
| 2612 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 2613 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 2614 | return -33; |
| 2615 | } |
| 2616 | |
| 2617 | if(QCBORDecode_Finish(&DC)) { |
| 2618 | return -34; |
| 2619 | } |
| 2620 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2621 | return 0; |
| 2622 | } |
| 2623 | |
| 2624 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2625 | int AllocAllStringsTest() |
| 2626 | { |
| 2627 | QCBORDecodeContext DC; |
| 2628 | |
| 2629 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2630 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2631 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2632 | UsefulBuf_MAKE_STACK_UB(Pool, 300); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2633 | |
| 2634 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 2635 | |
| 2636 | if(CheckCSRMaps(&DC)) { |
| 2637 | return -1; |
| 2638 | } |
| 2639 | |
| 2640 | // Next parse, save pointers to a few strings, destroy original and see all is OK. |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2641 | UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160); |
| 2642 | UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2643 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2644 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
| 2645 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 2646 | |
| 2647 | int nCBORError; |
| 2648 | QCBORItem Item1, Item2, Item3, Item4; |
| 2649 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
| 2650 | return nCBORError; |
| 2651 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 2652 | Item1.val.uCount != 3) |
| 2653 | return -1; |
| 2654 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
| 2655 | return nCBORError; |
| 2656 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
| 2657 | return nCBORError; |
| 2658 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
| 2659 | return nCBORError; |
| 2660 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
| 2661 | return nCBORError; |
| 2662 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 2663 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2664 | |
| 2665 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2666 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 2667 | Item1.val.int64 != 42 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2668 | UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2669 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2670 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2671 | |
| 2672 | |
| 2673 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2674 | UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2675 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
| 2676 | Item2.val.uCount != 2) |
| 2677 | return -1; |
| 2678 | |
| 2679 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2680 | UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2681 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2682 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2683 | |
| 2684 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2685 | UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2686 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2687 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2688 | |
| 2689 | // Next parse with a pool that is too small |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2690 | UsefulBuf_MAKE_STACK_UB(SmallPool, 80); |
| 2691 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2692 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 2693 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
| 2694 | return nCBORError; |
| 2695 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2696 | Item1.val.uCount != 3) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2697 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2698 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2699 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 2700 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 2701 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 2702 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 2703 | } |
| 2704 | } |
| 2705 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2706 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2707 | return -5; |
| 2708 | } |
| 2709 | |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2713 | // Cheating declaration to get to the special test hook |
| 2714 | size_t MemPoolTestHook_GetPoolSize(void *ctx); |
| 2715 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2716 | |
| 2717 | int MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 2718 | { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2719 | // Set up the decoder with a tiny bit of CBOR to parse |
| 2720 | QCBORDecodeContext DC; |
| 2721 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 2722 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2723 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2724 | // Set up an memory pool of 100 bytes |
| 2725 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 2726 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 2727 | |
| 2728 | // Cheat a little to get to the string allocator object |
| 2729 | // so we can call it directly to test it |
| 2730 | QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator; |
| 2731 | // Cheat some more to know exactly the |
| 2732 | size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc); |
| 2733 | |
| 2734 | // First test -- ask for too much in one go |
| 2735 | UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1); |
| 2736 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 2737 | return -1; |
| 2738 | } |
| 2739 | |
| 2740 | |
| 2741 | // Re do the set up for the next test that will do a successful alloc, |
| 2742 | // a fail, a free and then success |
| 2743 | // This test should work on 32 and 64-bit machines if the compiler |
| 2744 | // does the expected thing with pointer sizes for the internal |
| 2745 | // MemPool implementation leaving 44 or 72 bytes of pool memory. |
| 2746 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 2747 | |
| 2748 | // Cheat a little to get to the string allocator object |
| 2749 | // so we can call it directly to test it |
| 2750 | pAlloc = (QCBORStringAllocator *)DC.pStringAllocator; |
| 2751 | // Cheat some more to know exactly the |
| 2752 | uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc); |
| 2753 | |
| 2754 | Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1); |
| 2755 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 2756 | return -2; |
| 2757 | } |
| 2758 | UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2); |
| 2759 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 2760 | return -3; |
| 2761 | } |
| 2762 | (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr); |
| 2763 | Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2); |
| 2764 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 2765 | return -4; |
| 2766 | } |
| 2767 | |
| 2768 | |
| 2769 | // Re do set up for next test that involves a successful alloc, |
| 2770 | // and a successful realloc and a failed realloc |
| 2771 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 2772 | |
| 2773 | // Cheat a little to get to the string allocator object |
| 2774 | // so we can call it directly to test it |
| 2775 | pAlloc = (QCBORStringAllocator *)DC.pStringAllocator; |
| 2776 | Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2); |
| 2777 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 2778 | return -5; |
| 2779 | } |
| 2780 | Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool); |
| 2781 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 2782 | return -6; |
| 2783 | } |
| 2784 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 2785 | return -7; |
| 2786 | } |
| 2787 | UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1); |
| 2788 | if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail |
| 2789 | return -8; |
| 2790 | } |
| 2791 | |
| 2792 | return 0; |
| 2793 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2794 | |