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