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