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