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