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