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 | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2020, 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 | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 31 | =============================================================================*/ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 32 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 33 | #include "qcbor_decode_tests.h" |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 34 | #include "qcbor/qcbor_encode.h" |
| 35 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | d4728fd | 2018-12-17 15:15:56 -0800 | [diff] [blame] | 36 | #include <string.h> |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 37 | #include <math.h> // for fabs() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 38 | #include "not_well_formed_cbor.h" |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 39 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 40 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 41 | #ifdef PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 42 | #include <stdio.h> |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 43 | |
| 44 | static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 45 | { |
| 46 | if(szLabel) { |
| 47 | printf("%s ", szLabel); |
| 48 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 49 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 50 | size_t i; |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 51 | for(i = 0; i < Buf.len; i++) { |
| 52 | uint8_t Z = ((uint8_t *)Buf.ptr)[i]; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 53 | printf("%02x ", Z); |
| 54 | } |
| 55 | printf("\n"); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 56 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 57 | fflush(stdout); |
| 58 | } |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 59 | #endif |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 60 | |
| 61 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 62 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 63 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 64 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 65 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 66 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 67 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 68 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 69 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 70 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 71 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 72 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 73 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 74 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 75 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 76 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 77 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 78 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 79 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 80 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 81 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 82 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 83 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 84 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 85 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 86 | 0xff, 0xff}; |
| 87 | |
| 88 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 89 | // return CBOR error or -1 if type of value doesn't match |
| 90 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 91 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 92 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 93 | QCBORItem Item; |
| 94 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 95 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 96 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 97 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 98 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 99 | return -1; |
| 100 | |
| 101 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 102 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 103 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 104 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 105 | return -1; |
| 106 | |
| 107 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 108 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 109 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 110 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 111 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 112 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 113 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 114 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 115 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 116 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 117 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 118 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 119 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 120 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 121 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 122 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 123 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 124 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 125 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 126 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 127 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 128 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 129 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 130 | |
| 131 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 132 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 133 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 134 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 135 | Item.val.int64 != -2147483648) |
| 136 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 137 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 138 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 139 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 140 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 141 | Item.val.int64 != -2147483647) |
| 142 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 143 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 144 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 145 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 146 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 147 | Item.val.int64 != -65538) |
| 148 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 149 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 150 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 151 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 152 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 153 | Item.val.int64 != -65537) |
| 154 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 155 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 156 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 157 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 158 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 159 | Item.val.int64 != -65536) |
| 160 | return -1; |
| 161 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 162 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 163 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 164 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 165 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 166 | Item.val.int64 != -65535) |
| 167 | return -1; |
| 168 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 169 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 170 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 171 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 172 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 173 | Item.val.int64 != -65534) |
| 174 | return -1; |
| 175 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 176 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 177 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 178 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 179 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 180 | Item.val.int64 != -257) |
| 181 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 182 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 183 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 184 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 185 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 186 | Item.val.int64 != -256) |
| 187 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 188 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 189 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 190 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 191 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 192 | Item.val.int64 != -255) |
| 193 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 194 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 195 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 196 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 197 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 198 | Item.val.int64 != -254) |
| 199 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 200 | |
| 201 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 202 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 203 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 204 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 205 | Item.val.int64 != -25) |
| 206 | return -1; |
| 207 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 208 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 209 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 210 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 211 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 212 | Item.val.int64 != -24) |
| 213 | return -1; |
| 214 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 215 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 216 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 217 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 218 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 219 | Item.val.int64 != -23) |
| 220 | return -1; |
| 221 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 222 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 223 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 224 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 225 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 226 | Item.val.int64 != -1) |
| 227 | return -1; |
| 228 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 229 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 230 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 231 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 232 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 233 | Item.val.int64 != 0) |
| 234 | return -1; |
| 235 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 236 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 237 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 238 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 239 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 240 | Item.val.int64 != 0) |
| 241 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 242 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 243 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 244 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 245 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 246 | Item.val.int64 != 1) |
| 247 | return -1; |
| 248 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 249 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 250 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 251 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 252 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 253 | Item.val.int64 != 22) |
| 254 | return -1; |
| 255 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 256 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 257 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 258 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 259 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 260 | Item.val.int64 != 23) |
| 261 | return -1; |
| 262 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 263 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 264 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 265 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 266 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 267 | Item.val.int64 != 24) |
| 268 | return -1; |
| 269 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 270 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 271 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 272 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 273 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 274 | Item.val.int64 != 25) |
| 275 | return -1; |
| 276 | |
| 277 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 278 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 279 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 280 | Item.val.int64 != 26) |
| 281 | return -1; |
| 282 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 283 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 284 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 285 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 286 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 287 | Item.val.int64 != 254) |
| 288 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 289 | |
| 290 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 291 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 292 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 293 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 294 | Item.val.int64 != 255) |
| 295 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 296 | |
| 297 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 298 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 299 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 300 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 301 | Item.val.int64 != 256) |
| 302 | return -1; |
| 303 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 304 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 305 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 306 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 307 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 308 | Item.val.int64 != 257) |
| 309 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 310 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 311 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 312 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 313 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 314 | Item.val.int64 != 65534) |
| 315 | return -1; |
| 316 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 317 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 318 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 319 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 320 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 321 | Item.val.int64 != 65535) |
| 322 | return -1; |
| 323 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 324 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 325 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 326 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 327 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 328 | Item.val.int64 != 65536) |
| 329 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 330 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 331 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 332 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 333 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 334 | Item.val.int64 != 65537) |
| 335 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 336 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 337 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 338 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 339 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 340 | Item.val.int64 != 65538) |
| 341 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 342 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 343 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 344 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 345 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 346 | Item.val.int64 != 2147483647) |
| 347 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 348 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 349 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 350 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 351 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 352 | Item.val.int64 != 2147483647) |
| 353 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 354 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 355 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 356 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 357 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 358 | Item.val.int64 != 2147483648) |
| 359 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 360 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 361 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 362 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 363 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 364 | Item.val.int64 != 2147483649) |
| 365 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 366 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 367 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 368 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 369 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 370 | Item.val.int64 != 4294967294) |
| 371 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 372 | |
| 373 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 374 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
| 375 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 376 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 377 | Item.val.int64 != 4294967295) |
| 378 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 379 | |
| 380 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 381 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 382 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 383 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 384 | Item.val.int64 != 4294967296) |
| 385 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 386 | |
| 387 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 388 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 389 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 390 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 391 | Item.val.int64 != 4294967297) |
| 392 | return -1; |
| 393 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 394 | |
| 395 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 396 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 397 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 398 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 399 | Item.val.int64 != 9223372036854775807LL) |
| 400 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 401 | |
| 402 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 403 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 404 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 405 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 406 | Item.val.uint64 != 18446744073709551615ULL) |
| 407 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 408 | |
| 409 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 410 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 411 | return -1; |
| 412 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 413 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 418 | // One less than the smallest negative integer allowed in C. Decoding |
| 419 | // this should fail. |
| 420 | static const uint8_t spTooSmallNegative[] = { |
| 421 | 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 425 | /* |
| 426 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 427 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 428 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 429 | int32_t IntegerValuesParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 430 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 431 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 432 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 433 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 434 | QCBORDecode_Init(&DCtx, |
| 435 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 436 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 437 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 438 | // The really big test of all successes |
| 439 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 440 | if(nReturn) { |
| 441 | return nReturn; |
| 442 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 443 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 444 | // The one large negative integer that can be parsed |
| 445 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 446 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative), |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 447 | QCBOR_DECODE_MODE_NORMAL); |
| 448 | |
| 449 | QCBORItem item; |
| 450 | if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) { |
| 451 | nReturn = -4000; |
| 452 | } |
| 453 | |
| 454 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
| 458 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 459 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 460 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 461 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 462 | 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] | 463 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 464 | */ |
| 465 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 466 | static uint8_t spSimpleArrayBuffer[50]; |
| 467 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 468 | static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 469 | { |
| 470 | QCBOREncodeContext ECtx; |
| 471 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 472 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 473 | *pEncoded = NULL; |
| 474 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 475 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 476 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 477 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 478 | // and last with the buffer to do the actual encoding |
| 479 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 480 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 481 | QCBOREncode_OpenArray(&ECtx); |
| 482 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 483 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 484 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 485 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 486 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 487 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 488 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 489 | goto Done; |
| 490 | |
| 491 | if(*pEncoded != NULL) { |
| 492 | nReturn = 0; |
| 493 | goto Done; |
| 494 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 495 | |
| 496 | // Use static buffer to avoid dependency on malloc() |
| 497 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 498 | goto Done; |
| 499 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 500 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 501 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 502 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 503 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 504 | Done: |
| 505 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 509 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 510 | Some basic CBOR with map and array used in a lot of tests. |
| 511 | The map labels are all strings |
| 512 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 513 | {"first integer": 42, |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 514 | "an array of two strings": [ |
| 515 | "string1", "string2" |
| 516 | ], |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 517 | "map in a map": { |
| 518 | "bytes 1": h'78787878', |
| 519 | "bytes 2": h'79797979', |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 520 | "another int": 98, |
| 521 | "text 2": "lies, damn lies and statistics" |
| 522 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 523 | } |
| 524 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 525 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 526 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 527 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 528 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 529 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 530 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 531 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 532 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 533 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 534 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 535 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 536 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 537 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 538 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 539 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 540 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 541 | 0x73 } ; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 542 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 543 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 544 | size_t nLen, |
| 545 | int64_t *pInt1, |
| 546 | int64_t *pInt2, |
| 547 | const uint8_t **pBuf3, |
| 548 | size_t *pBuf3Len, |
| 549 | const uint8_t **pBuf4, |
| 550 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 551 | { |
| 552 | QCBORDecodeContext DCtx; |
| 553 | QCBORItem Item; |
| 554 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 555 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 556 | QCBORDecode_Init(&DCtx, |
| 557 | (UsefulBufC){pEncoded, nLen}, |
| 558 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 559 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 560 | // Make sure the first thing is a map |
| 561 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY) |
| 562 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 563 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 564 | // First integer |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 565 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 566 | goto Done; |
| 567 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 568 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 569 | // Second integer |
| 570 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64) |
| 571 | goto Done; |
| 572 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 573 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 574 | // First string |
| 575 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING) |
| 576 | goto Done; |
| 577 | *pBuf3 = Item.val.string.ptr; |
| 578 | *pBuf3Len = Item.val.string.len; |
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 | // Second string |
| 581 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING) |
| 582 | goto Done; |
| 583 | *pBuf4 = Item.val.string.ptr; |
| 584 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 585 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 586 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 587 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 588 | Done: |
| 589 | return(nReturn); |
| 590 | } |
| 591 | |
| 592 | |
| 593 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 594 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 595 | int32_t SimpleArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 596 | { |
| 597 | uint8_t *pEncoded; |
| 598 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 599 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 600 | int64_t i1=0, i2=0; |
| 601 | size_t i3=0, i4=0; |
| 602 | const uint8_t *s3= (uint8_t *)""; |
| 603 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 604 | |
| 605 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 606 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 607 | return(-1); |
| 608 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 609 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 610 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 611 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 612 | if(i1 != 23 || |
| 613 | i2 != 6000 || |
| 614 | i3 != 8 || |
| 615 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 616 | memcmp("galactic", s3, 8) !=0 || |
| 617 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 618 | return(-1); |
| 619 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 620 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 621 | return(0); |
| 622 | } |
| 623 | |
| 624 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 625 | /* |
| 626 | [ |
| 627 | 0, |
| 628 | [], |
| 629 | [ |
| 630 | [], |
| 631 | [ |
| 632 | 0 |
| 633 | ], |
| 634 | {}, |
| 635 | { |
| 636 | 1: {}, |
| 637 | 2: {}, |
| 638 | 3: [] |
| 639 | } |
| 640 | ] |
| 641 | ] |
| 642 | */ |
| 643 | static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 644 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
| 645 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 646 | int32_t EmptyMapsAndArraysTest() |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 647 | { |
| 648 | QCBORDecodeContext DCtx; |
| 649 | QCBORItem Item; |
| 650 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 651 | QCBORDecode_Init(&DCtx, |
| 652 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 653 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 654 | |
| 655 | // Array with 3 items |
| 656 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 657 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 658 | Item.uNestingLevel != 0 || |
| 659 | Item.uNextNestLevel != 1 || |
| 660 | Item.val.uCount != 3) { |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | // An integer 0 |
| 665 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 666 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 667 | Item.uNestingLevel != 1 || |
| 668 | Item.uNextNestLevel != 1 || |
| 669 | Item.val.uint64 != 0) { |
| 670 | return -2; |
| 671 | } |
| 672 | |
| 673 | // An empty array |
| 674 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 675 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 676 | Item.uNestingLevel != 1 || |
| 677 | Item.uNextNestLevel != 1 || |
| 678 | Item.val.uCount != 0) { |
| 679 | return -3; |
| 680 | } |
| 681 | |
| 682 | // An array with 4 items |
| 683 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 684 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 685 | Item.uNestingLevel != 1 || |
| 686 | Item.uNextNestLevel != 2 || |
| 687 | Item.val.uCount != 4) { |
| 688 | return -4; |
| 689 | } |
| 690 | |
| 691 | // An empty array |
| 692 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 693 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 694 | Item.uNestingLevel != 2 || |
| 695 | Item.uNextNestLevel != 2 || |
| 696 | Item.val.uCount != 0) { |
| 697 | return -5; |
| 698 | } |
| 699 | |
| 700 | // An array with 1 item |
| 701 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 702 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 703 | Item.uNestingLevel != 2 || |
| 704 | Item.uNextNestLevel != 3 || |
| 705 | Item.val.uCount != 1) { |
| 706 | return -6; |
| 707 | } |
| 708 | |
| 709 | // An integer 0 |
| 710 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 711 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 712 | Item.uNestingLevel != 3 || |
| 713 | Item.uNextNestLevel != 2 || |
| 714 | Item.val.uint64 != 0) { |
| 715 | return -7; |
| 716 | } |
| 717 | |
| 718 | // An empty map |
| 719 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 720 | Item.uDataType != QCBOR_TYPE_MAP || |
| 721 | Item.uNestingLevel != 2 || |
| 722 | Item.uNextNestLevel != 2 || |
| 723 | Item.val.uCount != 0) { |
| 724 | return -8; |
| 725 | } |
| 726 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 727 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 728 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 729 | Item.uDataType != QCBOR_TYPE_MAP || |
| 730 | Item.uNestingLevel != 2 || |
| 731 | Item.uNextNestLevel != 3 || |
| 732 | Item.val.uCount != 3) { |
| 733 | return -9; |
| 734 | } |
| 735 | |
| 736 | // An empty map |
| 737 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 738 | Item.uDataType != QCBOR_TYPE_MAP || |
| 739 | Item.uNestingLevel != 3 || |
| 740 | Item.uNextNestLevel != 3 || |
| 741 | Item.val.uCount != 0) { |
| 742 | return -10; |
| 743 | } |
| 744 | |
| 745 | // An empty map |
| 746 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 747 | Item.uDataType != QCBOR_TYPE_MAP || |
| 748 | Item.uNestingLevel != 3 || |
| 749 | Item.uNextNestLevel != 3 || |
| 750 | Item.val.uCount != 0) { |
| 751 | return -11; |
| 752 | } |
| 753 | |
| 754 | // An empty array |
| 755 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 756 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 757 | Item.uNestingLevel != 3 || |
| 758 | Item.uNextNestLevel != 0 || |
| 759 | Item.val.uCount != 0) { |
| 760 | return -12; |
| 761 | } |
| 762 | |
| 763 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 764 | return -13; |
| 765 | } |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 770 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 771 | static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 772 | 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 773 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 774 | int32_t ParseDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 775 | { |
| 776 | QCBORDecodeContext DCtx; |
| 777 | int nReturn = 0; |
| 778 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 779 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 780 | QCBORDecode_Init(&DCtx, |
| 781 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 782 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 783 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 784 | for(i = 0; i < 10; i++) { |
| 785 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 786 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 787 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 788 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 789 | Item.uNestingLevel != i) { |
| 790 | nReturn = -1; |
| 791 | break; |
| 792 | } |
| 793 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 794 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 795 | return(nReturn); |
| 796 | } |
| 797 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 798 | // Big enough to test nesting to the depth of 24 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 799 | static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 800 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 801 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 802 | 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 803 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 804 | int32_t ParseTooDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 805 | { |
| 806 | QCBORDecodeContext DCtx; |
| 807 | int nReturn = 0; |
| 808 | int i; |
| 809 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 810 | |
| 811 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 812 | QCBORDecode_Init(&DCtx, |
| 813 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 814 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 815 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 816 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 817 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 818 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 819 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 820 | Item.uNestingLevel != i) { |
| 821 | nReturn = -1; |
| 822 | break; |
| 823 | } |
| 824 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 825 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 826 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) |
| 827 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 828 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 829 | return(nReturn); |
| 830 | } |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 835 | int32_t ShortBufferParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 836 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 837 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 838 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 839 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 840 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 841 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 842 | QCBORDecode_Init(&DCtx, |
| 843 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 844 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 845 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 846 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 847 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 848 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 849 | nResult = -1; |
| 850 | goto Done; |
| 851 | } |
| 852 | } |
| 853 | Done: |
| 854 | return nResult; |
| 855 | } |
| 856 | |
| 857 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 858 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 859 | int32_t ShortBufferParseTest2() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 860 | { |
| 861 | uint8_t *pEncoded; |
| 862 | int nReturn; |
| 863 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 864 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 865 | int64_t i1, i2; |
| 866 | size_t i3, i4; |
| 867 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 868 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 869 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 870 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 871 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 872 | return(-1); |
| 873 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 874 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 875 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
| 876 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
| 877 | if(nResult == 0) { |
| 878 | nReturn = -1; |
| 879 | } |
| 880 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 881 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 882 | return(nReturn); |
| 883 | } |
| 884 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 885 | /* |
| 886 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 887 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 888 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 889 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 890 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 891 | { |
| 892 | QCBORDecodeContext DCtx; |
| 893 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 894 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 895 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 896 | QCBORDecode_Init(&DCtx, |
| 897 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 898 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 899 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 900 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 901 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 902 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 903 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 904 | Item.val.uCount != 3) |
| 905 | return -1; |
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))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 908 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 909 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 910 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 911 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 912 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 913 | Item.uDataAlloc || |
| 914 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 915 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 916 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 917 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 918 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 919 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 920 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 921 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 922 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 923 | Item.uDataAlloc || |
| 924 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 925 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 926 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 927 | Item.val.uCount != 2) |
| 928 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 929 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 930 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 931 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 932 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 933 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 934 | Item.uDataAlloc || |
| 935 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 936 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 937 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 938 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 939 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 940 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 941 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 942 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 943 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 944 | Item.uDataAlloc || |
| 945 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 946 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 947 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 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))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 951 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 952 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 953 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 954 | Item.uDataAlloc || |
| 955 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 956 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 957 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 958 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 959 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 960 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 961 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 962 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 963 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 964 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 965 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 966 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 967 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 968 | Item.uDataAlloc || |
| 969 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 970 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 971 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 972 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 973 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 974 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 975 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 976 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 977 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 978 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 979 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 980 | Item.uDataAlloc || |
| 981 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 982 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 983 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 984 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 985 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 986 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 987 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 988 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 989 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 990 | Item.uDataAlloc || |
| 991 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 992 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 993 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 994 | Item.val.int64 != 98) |
| 995 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 996 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 997 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 998 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 999 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1000 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1001 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1002 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1003 | Item.uDataAlloc || |
| 1004 | Item.uLabelAlloc || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1005 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1006 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1007 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1008 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1013 | /* |
| 1014 | Decode and thoroughly check a moderately complex |
| 1015 | set of maps |
| 1016 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1017 | int32_t ParseMapAsArrayTest() |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1018 | { |
| 1019 | QCBORDecodeContext DCtx; |
| 1020 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1021 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1022 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1023 | QCBORDecode_Init(&DCtx, |
| 1024 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1025 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
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 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1028 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1029 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1030 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1031 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1032 | return -1; |
| 1033 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1034 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1035 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1036 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1037 | } |
| 1038 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1039 | Item.uDataAlloc || |
| 1040 | Item.uLabelAlloc || |
| 1041 | Item.uLabelType != QCBOR_TYPE_NONE || |
| 1042 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) { |
| 1043 | return -2; |
| 1044 | } |
| 1045 | |
| 1046 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1047 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1048 | } |
| 1049 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1050 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1051 | Item.val.int64 != 42 || |
| 1052 | Item.uDataAlloc || |
| 1053 | Item.uLabelAlloc) { |
| 1054 | return -3; |
| 1055 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1056 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1057 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1058 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1059 | } |
| 1060 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1061 | Item.uDataAlloc || |
| 1062 | Item.uLabelAlloc || |
| 1063 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) || |
| 1064 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1065 | return -4; |
| 1066 | } |
| 1067 | |
| 1068 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1069 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1070 | } |
| 1071 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1072 | Item.uDataAlloc || |
| 1073 | Item.uLabelAlloc || |
| 1074 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1075 | Item.val.uCount != 2) { |
| 1076 | return -5; |
| 1077 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1078 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1079 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1080 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1081 | } |
| 1082 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1083 | Item.val.string.len != 7 || |
| 1084 | Item.uDataAlloc || |
| 1085 | Item.uLabelAlloc || |
| 1086 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1087 | return -6; |
| 1088 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1089 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1090 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1091 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1092 | } |
| 1093 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1094 | Item.uDataAlloc || |
| 1095 | Item.uLabelAlloc || |
| 1096 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1097 | return -7; |
| 1098 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1099 | |
| 1100 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1101 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1102 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1103 | } |
| 1104 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1105 | Item.uDataAlloc || |
| 1106 | Item.uLabelAlloc || |
| 1107 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) { |
| 1108 | return -8; |
| 1109 | } |
| 1110 | |
| 1111 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1112 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1113 | } |
| 1114 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1115 | Item.uDataAlloc || |
| 1116 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1117 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1118 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1119 | return -9; |
| 1120 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1121 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1122 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1123 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1124 | } |
| 1125 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1126 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 1127 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1128 | Item.uDataAlloc || |
| 1129 | Item.uLabelAlloc) { |
| 1130 | return -10; |
| 1131 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1132 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1133 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1134 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1135 | } |
| 1136 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1137 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1138 | Item.uDataAlloc || |
| 1139 | Item.uLabelAlloc || |
| 1140 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
| 1141 | return -11; |
| 1142 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1143 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1144 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1145 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1146 | } |
| 1147 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1148 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) || |
| 1149 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1150 | Item.uDataAlloc || |
| 1151 | Item.uLabelAlloc) { |
| 1152 | return -12; |
| 1153 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1154 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1155 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1156 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1157 | } |
| 1158 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1159 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1160 | Item.uDataAlloc || |
| 1161 | Item.uLabelAlloc || |
| 1162 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
| 1163 | return -13; |
| 1164 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1165 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1166 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1167 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1168 | } |
| 1169 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1170 | Item.uDataAlloc || |
| 1171 | Item.uLabelAlloc || |
| 1172 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) || |
| 1173 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1174 | return -14; |
| 1175 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1176 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1177 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1178 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1179 | } |
| 1180 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1181 | Item.uDataAlloc || |
| 1182 | Item.uLabelAlloc || |
| 1183 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1184 | Item.val.int64 != 98) { |
| 1185 | return -15; |
| 1186 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1187 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1188 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1189 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1190 | } |
| 1191 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1192 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))|| |
| 1193 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1194 | Item.uDataAlloc || |
| 1195 | Item.uLabelAlloc) { |
| 1196 | return -16; |
| 1197 | } |
| 1198 | |
| 1199 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1200 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1201 | } |
| 1202 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1203 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1204 | Item.uDataAlloc || |
| 1205 | Item.uLabelAlloc || |
| 1206 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
| 1207 | return -17; |
| 1208 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1209 | |
| 1210 | |
| 1211 | /* |
| 1212 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1213 | map that when interpreted as an array will be too many. Test |
| 1214 | data just has the start of the map, not all the items in the map. |
| 1215 | */ |
| 1216 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
| 1217 | |
| 1218 | QCBORDecode_Init(&DCtx, |
| 1219 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1220 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
| 1221 | |
| 1222 | if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
| 1223 | return -50; |
| 1224 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1225 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1226 | return 0; |
| 1227 | } |
| 1228 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1229 | |
| 1230 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1231 | Fully or partially decode pValidMapEncoded. When |
| 1232 | partially decoding check for the right error code. |
| 1233 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1234 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1235 | The partial decodes test error conditions of |
| 1236 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1237 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1238 | This could be combined with the above test |
| 1239 | and made prettier and maybe a little more |
| 1240 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1241 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1242 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1243 | { |
| 1244 | QCBORDecodeContext DCtx; |
| 1245 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1246 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1247 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1248 | QCBORDecode_Init(&DCtx, |
| 1249 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1250 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1251 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1252 | if(nLevel < 1) { |
| 1253 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1254 | return -1; |
| 1255 | } else { |
| 1256 | return 0; |
| 1257 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1258 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1259 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1260 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1261 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1262 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1263 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1264 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1265 | Item.val.uCount != 3) |
| 1266 | return -1; |
| 1267 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1268 | if(nLevel < 2) { |
| 1269 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1270 | return -1; |
| 1271 | } else { |
| 1272 | return 0; |
| 1273 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1274 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1275 | |
| 1276 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1277 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1278 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1279 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1280 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1281 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1282 | Item.val.uCount != 42 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1283 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1284 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1285 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1286 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1287 | if(nLevel < 3) { |
| 1288 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1289 | return -1; |
| 1290 | } else { |
| 1291 | return 0; |
| 1292 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1293 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1294 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1295 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1296 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1297 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1298 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1299 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1300 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1301 | Item.val.uCount != 2) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1302 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1303 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1304 | |
| 1305 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1306 | if(nLevel < 4) { |
| 1307 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1308 | return -1; |
| 1309 | } else { |
| 1310 | return 0; |
| 1311 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1312 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1313 | |
| 1314 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1315 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1316 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1317 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1318 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1319 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1320 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1321 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1322 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1323 | if(nLevel < 5) { |
| 1324 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1325 | return -1; |
| 1326 | } else { |
| 1327 | return 0; |
| 1328 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1329 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1330 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1331 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1332 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1333 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1334 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1335 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1336 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1337 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1338 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1339 | if(nLevel < 6) { |
| 1340 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1341 | return -1; |
| 1342 | } else { |
| 1343 | return 0; |
| 1344 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1345 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1346 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1347 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1348 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1349 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1350 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1351 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1352 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1353 | Item.val.uCount != 4) |
| 1354 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1355 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1356 | if(nLevel < 7) { |
| 1357 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1358 | return -1; |
| 1359 | } else { |
| 1360 | return 0; |
| 1361 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1362 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1363 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1364 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1365 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1366 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1367 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1368 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1369 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1370 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1371 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1372 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1373 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1374 | if(nLevel < 8) { |
| 1375 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1376 | return -1; |
| 1377 | } else { |
| 1378 | return 0; |
| 1379 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1380 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1381 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1382 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1383 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1384 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1385 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1386 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1387 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1388 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1389 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1390 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1391 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1392 | if(nLevel < 9) { |
| 1393 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1394 | return -1; |
| 1395 | } else { |
| 1396 | return 0; |
| 1397 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1398 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1399 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1400 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1401 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1402 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1403 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1404 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1405 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1406 | Item.val.int64 != 98) |
| 1407 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1408 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1409 | if(nLevel < 10) { |
| 1410 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 1411 | return -1; |
| 1412 | } else { |
| 1413 | return 0; |
| 1414 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1415 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1416 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1417 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1418 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1419 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1420 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1421 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1422 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1423 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1424 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1425 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1426 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1427 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1428 | return -1; |
| 1429 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1430 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1436 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1437 | int32_t ParseMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1438 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1439 | // Parse a moderatly complex map structure very thoroughly |
| 1440 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 1441 | if(nResult) { |
| 1442 | return nResult; |
| 1443 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1444 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1445 | // Again, but in strings-only mode. It should succeed since the input |
| 1446 | // map has only string labels. |
| 1447 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 1448 | if(nResult) { |
| 1449 | return nResult; |
| 1450 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1451 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1452 | // Again, but try to finish the decoding before the end of the |
| 1453 | // input at 10 different place and see that the right error code |
| 1454 | // is returned. |
| 1455 | for(int i = 0; i < 10; i++) { |
| 1456 | nResult = ExtraBytesTest(i); |
| 1457 | if(nResult) { |
| 1458 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1459 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1460 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1461 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1462 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1466 | static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, |
| 1467 | 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, |
| 1468 | 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1469 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1470 | int32_t ParseSimpleTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1471 | { |
| 1472 | QCBORDecodeContext DCtx; |
| 1473 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1474 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1475 | |
| 1476 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1477 | QCBORDecode_Init(&DCtx, |
| 1478 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1479 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1480 | |
| 1481 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1482 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1483 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1484 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1485 | Item.val.uCount != 10) |
| 1486 | return -1; |
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 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1489 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1490 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1491 | return -1; |
| 1492 | |
| 1493 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1494 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1495 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1496 | return -1; |
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 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1499 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1500 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1501 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1502 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1503 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1504 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1505 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1506 | return -1; |
| 1507 | |
| 1508 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1509 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1510 | return -1; |
| 1511 | |
| 1512 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1513 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1514 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1515 | return -1; |
| 1516 | |
| 1517 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1518 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1519 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1520 | return -1; |
| 1521 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1522 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1523 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1524 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1525 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1526 | return -1; |
| 1527 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1528 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1529 | return -1; |
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 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1532 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1533 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1534 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1535 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1536 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1537 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1538 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1539 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1540 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1541 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1542 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1546 | static int IsNotWellFormedError(QCBORError nErr) |
| 1547 | { |
| 1548 | switch(nErr){ |
| 1549 | case QCBOR_ERR_INDEFINITE_STRING_CHUNK: |
| 1550 | case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN: |
| 1551 | case QCBOR_ERR_UNSUPPORTED: |
| 1552 | case QCBOR_ERR_HIT_END: |
| 1553 | case QCBOR_ERR_BAD_TYPE_7: |
| 1554 | case QCBOR_ERR_BAD_BREAK: |
| 1555 | case QCBOR_ERR_EXTRA_BYTES: |
| 1556 | case QCBOR_ERR_BAD_INT: |
| 1557 | return 1; |
| 1558 | default: |
| 1559 | return 0; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1564 | int32_t NotWellFormedTests() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1565 | { |
| 1566 | // Loop over all the not-well-formed instance of CBOR |
| 1567 | // that are test vectors in not_well_formed_cbor.h |
| 1568 | const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes); |
| 1569 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 1570 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 1571 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 1572 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1573 | // Set up decoder context. String allocator needed for indefinite |
| 1574 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1575 | QCBORDecodeContext DCtx; |
| 1576 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
| 1577 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1578 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1579 | |
| 1580 | // Loop getting items until no more to get |
| 1581 | QCBORError nCBORError; |
| 1582 | do { |
| 1583 | QCBORItem Item; |
| 1584 | |
| 1585 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1586 | } while(nCBORError == QCBOR_SUCCESS); |
| 1587 | |
| 1588 | // Every test vector must fail with |
| 1589 | // a not-well-formed error. If not |
| 1590 | // this test fails. |
| 1591 | if(!IsNotWellFormedError(nCBORError)) { |
| 1592 | // Return index of failure in the error code |
| 1593 | return 2000 + nIterate; |
| 1594 | } |
| 1595 | } |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
| 1599 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1600 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1601 | UsefulBufC Input; |
| 1602 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1603 | }; |
| 1604 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1605 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1606 | static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1607 | { |
| 1608 | for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
| 1609 | // Set up the decoding context including a memory pool so that |
| 1610 | // indefinite length items can be checked |
| 1611 | QCBORDecodeContext DCtx; |
| 1612 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
| 1613 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1614 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1615 | QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1616 | if(nCBORError) { |
| 1617 | return -9; |
| 1618 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1619 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1620 | // Iterate until there is an error of some sort error |
| 1621 | QCBORItem Item; |
| 1622 | do { |
| 1623 | // Set to something none-zero other than QCBOR_TYPE_NONE |
| 1624 | memset(&Item, 0x33, sizeof(Item)); |
| 1625 | |
| 1626 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1627 | } while(nCBORError == QCBOR_SUCCESS); |
| 1628 | |
| 1629 | // Must get the expected error or the this test fails |
| 1630 | // The data and label type must also be QCBOR_TYPE_NONE |
| 1631 | if(nCBORError != pF->nError || |
| 1632 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1633 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1634 | // return index of CBOR + 100 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1635 | const size_t nIndex = (size_t)(pF - pFailInputs); |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1636 | return (int32_t)(nIndex * 100 + nCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
| 1643 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1644 | struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1645 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1646 | // returned is also checked. |
| 1647 | |
| 1648 | // Indefinite length strings must be closed off |
| 1649 | // An indefinite length byte string not closed off |
| 1650 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1651 | // An indefinite length text string not closed off |
| 1652 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1653 | |
| 1654 | |
| 1655 | // All the chunks in an indefinite length string must be of the type of indefinite length string |
| 1656 | // indefinite length byte string with text string chunk |
| 1657 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1658 | // indefinite length text string with a byte string chunk |
| 1659 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1660 | // indefinite length byte string with an positive integer chunk |
| 1661 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1662 | // indefinite length byte string with an negative integer chunk |
| 1663 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1664 | // indefinite length byte string with an array chunk |
| 1665 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1666 | // indefinite length byte string with an map chunk |
| 1667 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1668 | // indefinite length byte string with tagged integer chunk |
| 1669 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1670 | // indefinite length byte string with an simple type chunk |
| 1671 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1672 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1673 | // indefinite length text string with indefinite string inside |
| 1674 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1675 | |
| 1676 | |
| 1677 | // Definte length maps and arrays must be closed by having the right number of items |
| 1678 | // A definte length array that is supposed to have 1 item, but has none |
| 1679 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END }, |
| 1680 | // A definte length array that is supposed to have 2 items, but has only 1 |
| 1681 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1682 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1683 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1684 | // A definte length map that is supposed to have 1 item, but has none |
| 1685 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END }, |
| 1686 | // A definte length map that is supposed to have s item, but has only 1 |
| 1687 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1688 | |
| 1689 | |
| 1690 | // Indefinte length maps and arrays must be ended by a break |
| 1691 | // Indefinite length array with zero items and no break |
| 1692 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END }, |
| 1693 | // Indefinite length array with two items and no break |
| 1694 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1695 | // Indefinite length map with zero items and no break |
| 1696 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END }, |
| 1697 | // Indefinite length map with two items and no break |
| 1698 | { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END }, |
| 1699 | |
| 1700 | |
| 1701 | // Nested maps and arrays must be closed off (some extra nested test vectors) |
| 1702 | // Unclosed indefinite array containing a close definite array |
| 1703 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1704 | // Definite length array containing an unclosed indefinite array |
| 1705 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END }, |
| 1706 | // Deeply nested definite length arrays with deepest one unclosed |
| 1707 | { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END }, |
| 1708 | // Deeply nested indefinite length arrays with deepest one unclosed |
| 1709 | { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END }, |
| 1710 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1711 | { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1712 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1713 | { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1714 | // TODO: a few more definite indefinite length combos and check with CBORbis. |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1715 | |
| 1716 | |
| 1717 | // The "argument" for the data item is incomplete |
| 1718 | // Positive integer missing 1 byte argument |
| 1719 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1720 | // Positive integer missing 2 byte argument |
| 1721 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1722 | // Positive integer missing 4 byte argument |
| 1723 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1724 | // Positive integer missing 8 byte argument |
| 1725 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1726 | // Positive integer missing 1 byte of 2 byte argument |
| 1727 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1728 | // Positive integer missing 2 bytes of 4 byte argument |
| 1729 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1730 | // Positive integer missing 1 bytes of 7 byte argument |
| 1731 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1732 | // Negative integer missing 1 byte argument |
| 1733 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1734 | // Binary string missing 1 byte argument |
| 1735 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1736 | // Text string missing 1 byte argument |
| 1737 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 1738 | // Array missing 1 byte argument |
| 1739 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 1740 | // Map missing 1 byte argument |
| 1741 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 1742 | // Tag missing 1 byte argument |
| 1743 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 1744 | // Simple missing 1 byte argument |
| 1745 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
| 1746 | |
| 1747 | |
| 1748 | // Breaks must not occur in definite length arrays and maps |
| 1749 | // Array of length 1 with sole member replaced by a break |
| 1750 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1751 | // Array of length 2 with 2nd member replaced by a break |
| 1752 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1753 | // Map of length 1 with sole member label replaced by a break |
| 1754 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1755 | // Map of length 1 with sole member label replaced by break |
| 1756 | // Alternate representation that some decoders handle difference |
| 1757 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1758 | // Array of length 1 with 2nd member value replaced by a break |
| 1759 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1760 | // Map of length 2 with 2nd member replaced by a break |
| 1761 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 1762 | |
| 1763 | |
| 1764 | // Breaks must not occur on their own out of an indefinite length data item |
| 1765 | // A bare break is not well formed |
| 1766 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 1767 | // A bare break after a zero length definite length array |
| 1768 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1769 | // A bare break after a zero length indefinite length map |
| 1770 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1771 | |
| 1772 | |
| 1773 | // Forbidden two byte encodings of simple types |
| 1774 | // Must use 0xe0 instead |
| 1775 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1776 | // Should use 0xe1 instead |
| 1777 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1778 | // Should use 0xe2 instead |
| 1779 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1780 | // Should use 0xe3 instead |
| 1781 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1782 | // Should use 0xe4 instead |
| 1783 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1784 | // Should use 0xe5 instead |
| 1785 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1786 | // Should use 0xe6 instead |
| 1787 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1788 | // Should use 0xe7 instead |
| 1789 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1790 | // Should use 0xe8 instead |
| 1791 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1792 | // Should use 0xe9 instead |
| 1793 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1794 | // Should use 0xea instead |
| 1795 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1796 | // Should use 0xeb instead |
| 1797 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1798 | // Should use 0xec instead |
| 1799 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1800 | // Should use 0xed instead |
| 1801 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1802 | // Should use 0xee instead |
| 1803 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1804 | // Should use 0xef instead |
| 1805 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1806 | // Should use 0xf0 instead |
| 1807 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1808 | // Should use 0xf1 instead |
| 1809 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1810 | // Should use 0xf2 instead |
| 1811 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1812 | // Must use 0xf3 instead |
| 1813 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1814 | // Must use 0xf4 instead |
| 1815 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1816 | // Must use 0xf5 instead |
| 1817 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1818 | // Must use 0xf6 instead |
| 1819 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1820 | // Must use 0xf7 instead |
| 1821 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1822 | // Must use 0xf8 instead |
| 1823 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1824 | |
| 1825 | |
| 1826 | // Integers with additional info indefinite length |
| 1827 | // Positive integer with additional info indefinite length |
| 1828 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 1829 | // Negative integer with additional info indefinite length |
| 1830 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
| 1831 | // CBOR tag with "argument" an indefinite length |
| 1832 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 1833 | // CBOR tag with "argument" an indefinite length alternate vector |
| 1834 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
| 1835 | |
| 1836 | |
| 1837 | // Missing bytes from a deterministic length string |
| 1838 | // A byte string is of length 1 without the 1 byte |
| 1839 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 1840 | // A text string is of length 1 without the 1 byte |
| 1841 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 42272e4 | 2020-01-31 07:50:53 -0800 | [diff] [blame] | 1842 | // Byte string should have 2^32-15 bytes, but has one |
| 1843 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 1844 | // Byte string should have 2^32-15 bytes, but has one |
| 1845 | { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1846 | |
| 1847 | |
| 1848 | // Use of unassigned additional information values |
| 1849 | // Major type positive integer with reserved value 28 |
| 1850 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1851 | // Major type positive integer with reserved value 29 |
| 1852 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1853 | // Major type positive integer with reserved value 30 |
| 1854 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1855 | // Major type negative integer with reserved value 28 |
| 1856 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1857 | // Major type negative integer with reserved value 29 |
| 1858 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1859 | // Major type negative integer with reserved value 30 |
| 1860 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1861 | // Major type byte string with reserved value 28 length |
| 1862 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1863 | // Major type byte string with reserved value 29 length |
| 1864 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1865 | // Major type byte string with reserved value 30 length |
| 1866 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1867 | // Major type text string with reserved value 28 length |
| 1868 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1869 | // Major type text string with reserved value 29 length |
| 1870 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1871 | // Major type text string with reserved value 30 length |
| 1872 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1873 | // Major type array with reserved value 28 length |
| 1874 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1875 | // Major type array with reserved value 29 length |
| 1876 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1877 | // Major type array with reserved value 30 length |
| 1878 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1879 | // Major type map with reserved value 28 length |
| 1880 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1881 | // Major type map with reserved value 29 length |
| 1882 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1883 | // Major type map with reserved value 30 length |
| 1884 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1885 | // Major type tag with reserved value 28 length |
| 1886 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1887 | // Major type tag with reserved value 29 length |
| 1888 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1889 | // Major type tag with reserved value 30 length |
| 1890 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1891 | // Major type simple with reserved value 28 length |
| 1892 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1893 | // Major type simple with reserved value 29 length |
| 1894 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1895 | // Major type simple with reserved value 30 length |
| 1896 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 1897 | |
| 1898 | |
| 1899 | // Maps must have an even number of data items (key & value) |
| 1900 | // Map with 1 item when it should have 2 |
| 1901 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1902 | // Map with 3 item when it should have 4 |
| 1903 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1904 | // Map with 1 item when it should have 2 |
| 1905 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1906 | // Map with 3 item when it should have 4 |
| 1907 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
| 1908 | |
| 1909 | |
| 1910 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1911 | // Text-based date, with an integer |
| 1912 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 1913 | // Epoch date, with an byte string |
| 1914 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 1915 | // tagged as both epoch and string dates |
| 1916 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 1917 | // big num tagged an int, not a byte string |
| 1918 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1919 | }; |
| 1920 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1921 | int32_t DecodeFailureTests() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1922 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1923 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1924 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1925 | nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput)); |
| 1926 | if(nResult) { |
| 1927 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1928 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1929 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1930 | // Corrupt the UsefulInputBuf and see that |
| 1931 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1932 | { |
| 1933 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1934 | QCBORItem Item; |
| 1935 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1936 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1937 | QCBORDecode_Init(&DCtx, |
| 1938 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1939 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1940 | |
| 1941 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1942 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1943 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1944 | Item.val.uCount != 10) { |
| 1945 | // This wasn't supposed to happen |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1946 | return -1; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1947 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1948 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1949 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1950 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1951 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1952 | if(nCBORError != QCBOR_ERR_HIT_END) { |
| 1953 | // Did not get back the error expected |
| 1954 | return -2; |
| 1955 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1956 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1957 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1958 | /* |
| 1959 | This test is disabled until QCBOREncode_EncodeHead() is brought in so |
| 1960 | the size encoded can be tied to SIZE_MAX and work for all size CPUs. |
| 1961 | |
| 1962 | This relies on the largest string allowed being SIZE_MAX -4 rather than |
| 1963 | SIZE_MAX. That way the test can be performed. |
| 1964 | { |
| 1965 | QCBORDecodeContext DCtx; |
| 1966 | QCBORItem Item; |
| 1967 | |
| 1968 | static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff, |
| 1969 | 0xff, 0xff, 0xff, 0xff}; |
| 1970 | |
| 1971 | QCBORDecode_Init(&DCtx, |
| 1972 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo), |
| 1973 | QCBOR_DECODE_MODE_NORMAL); |
| 1974 | |
| 1975 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 1976 | return -4; |
| 1977 | } |
| 1978 | } |
| 1979 | */ |
| 1980 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1981 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1985 | /* Try all 256 values of the byte at nLen including recursing for |
| 1986 | each of the values to try values at nLen+1 ... up to nLenMax |
| 1987 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1988 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1989 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1990 | if(nLen >= nLenMax) { |
| 1991 | return; |
| 1992 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 1993 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1994 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 1995 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1996 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 1997 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 1998 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 1999 | // Get ready to parse |
| 2000 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2001 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2002 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2003 | // Parse by getting the next item until an error occurs |
| 2004 | // Just about every possible decoder error can occur here |
| 2005 | // The goal of this test is not to check for the correct |
| 2006 | // error since that is not really possible. It is to |
| 2007 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2008 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2009 | QCBORItem Item; |
| 2010 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2011 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2012 | break; |
| 2013 | } |
| 2014 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2015 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2016 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2021 | int32_t ComprehensiveInputTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2022 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2023 | // Size 2 tests 64K inputs and runs quickly |
| 2024 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2025 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2026 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2027 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2028 | return 0; |
| 2029 | } |
| 2030 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2031 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2032 | int32_t BigComprehensiveInputTest() |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2033 | { |
| 2034 | // size 3 tests 16 million inputs and runs OK |
| 2035 | // in seconds on fast machines. Size 4 takes |
| 2036 | // 10+ minutes and 5 half a day on fast |
| 2037 | // machines. This test is kept separate from |
| 2038 | // the others so as to no slow down the use |
| 2039 | // of them as a very frequent regression. |
| 2040 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2041 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2042 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2043 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2044 | return 0; |
| 2045 | } |
| 2046 | |
| 2047 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2048 | static uint8_t spDateTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2049 | 0xc0, // tag for string date |
| 2050 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2051 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2052 | 0xc1, // tag for epoch date |
| 2053 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2054 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2055 | // CBOR_TAG_B64 |
| 2056 | 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] | 2057 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2058 | |
| 2059 | 0xc1, // tag for epoch date |
| 2060 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2061 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2062 | 0xc1, // tag for epoch date |
| 2063 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2064 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2065 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2066 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2067 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2068 | 0xc1, // tag for epoch date |
| 2069 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2070 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2071 | |
| 2072 | 0xc1, // tag for epoch date |
| 2073 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2074 | }; |
| 2075 | |
| 2076 | |
| 2077 | // have to check float expected only to within an epsilon |
| 2078 | int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2079 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2080 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2081 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2082 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2083 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2084 | return diff > 0.0000001; |
| 2085 | } |
| 2086 | |
| 2087 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2088 | int32_t DateParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2089 | { |
| 2090 | QCBORDecodeContext DCtx; |
| 2091 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2092 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2093 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2094 | QCBORDecode_Init(&DCtx, |
| 2095 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2096 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2097 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2098 | const uint64_t uTags[] = {15}; |
| 2099 | QCBORTagListIn TagList = {1, uTags}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2100 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2101 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2102 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2103 | // String date |
| 2104 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2105 | return -1; |
| 2106 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 2107 | UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2108 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | // Epoch date |
| 2112 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2113 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2114 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2115 | Item.val.epochDate.nSeconds != 1400000000 || |
| 2116 | Item.val.epochDate.fSecondsFraction != 0 ) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2117 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2118 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2119 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2120 | // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything |
| 2121 | // but want to be sure extra tag doesn't cause a problem |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2122 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2123 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2124 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2125 | Item.val.epochDate.nSeconds != 1400000001 || |
| 2126 | Item.val.epochDate.fSecondsFraction != 0 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2127 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2128 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2129 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2130 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2131 | // Epoch date that is too large for our representation |
| 2132 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2133 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2134 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2135 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2136 | // Epoch date in float format with fractional seconds |
| 2137 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2138 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2139 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2140 | Item.val.epochDate.nSeconds != 1 || |
| 2141 | CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2142 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2143 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2144 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2145 | // Epoch date float that is too large for our representation |
| 2146 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2147 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2148 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2149 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2150 | // Epoch date double that is just slightly too large |
| 2151 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2152 | return -11; |
| 2153 | } |
| 2154 | |
| 2155 | // Largest double epoch date supported |
| 2156 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS || |
| 2157 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2158 | Item.val.epochDate.nSeconds != 9223372036854773760 || |
| 2159 | Item.val.epochDate.nSeconds == 0) { |
| 2160 | return -12; |
| 2161 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2162 | // TODO: could use a few more tests with float, double, and half precsion |
| 2163 | // and negative (but coverage is still pretty good) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2164 | |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2168 | // Really simple basic input for tagging test |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2169 | static uint8_t spOptTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2170 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2171 | 0x81, // Array of one |
| 2172 | 0xd8, 0x04, // non-preferred serialization of tag 4 |
| 2173 | 0x82, 0x01, 0x03}; // fraction 1/3 |
| 2174 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2175 | /* |
| 2176 | DB 9192939495969798 # tag(10489608748473423768) |
| 2177 | 80 # array(0) |
| 2178 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2179 | static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
| 2180 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2181 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2182 | /* |
| 2183 | DB 9192939495969798 # tag(10489608748473423768) |
| 2184 | D8 88 # tag(136) |
| 2185 | C6 # tag(6) |
| 2186 | C7 # tag(7) |
| 2187 | 80 # array(0) |
| 2188 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2189 | static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
| 2190 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2191 | |
| 2192 | /* |
| 2193 | The cbor.me parse of this. |
| 2194 | 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2195 | 9(-17): 773("SSG"), -15: 16(17(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"), |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2196 | 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}), |
| 2197 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})), |
| 2198 | 16(-22): 23({11(8(7(-5))): 8(-3)})}))) |
| 2199 | */ |
| 2200 | static uint8_t spCSRWithTags[] = { |
| 2201 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 2202 | 0xc6, 0xc7, 0x36, |
| 2203 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 2204 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 2205 | 0xcb, 0xa5, |
| 2206 | 0xd1, 0x31, |
| 2207 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 2208 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 2209 | 0xc9, 0x30, |
| 2210 | 0xd9, 0x03, 0x05, 0x63, |
| 2211 | 0x53, 0x53, 0x47, |
| 2212 | 0x2e, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2213 | 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2214 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
| 2215 | 0xd1, 0x2f, |
| 2216 | 0xd1, 0x69, |
| 2217 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 2218 | 0xd1, 0x2d, |
| 2219 | 0xd1, 0x62, |
| 2220 | 0x55, 0x53, |
| 2221 | 0xd7, 0x32, |
| 2222 | 0xd3, 0xa2, |
| 2223 | 0x2a, |
| 2224 | 0xc9, 0xa1, |
| 2225 | 0x28, |
| 2226 | 0x26, |
| 2227 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 2228 | 0xcc, 0x4a, |
| 2229 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 2230 | 0xd0, 0x35, |
| 2231 | 0xd7, 0xa1, |
| 2232 | 0xcb, 0xc8, 0xc7, 0x24, |
| 2233 | 0xc8, 0x22}; |
| 2234 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2235 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2236 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2237 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2238 | int32_t OptTagParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2239 | { |
| 2240 | QCBORDecodeContext DCtx; |
| 2241 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2242 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2243 | QCBORDecode_Init(&DCtx, |
| 2244 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), |
| 2245 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2246 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2247 | //------------------------- |
| 2248 | // This text matches the magic number tag and the fraction tag |
| 2249 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2250 | return -2; |
| 2251 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2252 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2253 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 2254 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2255 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2256 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2257 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2258 | return -4; |
| 2259 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2260 | |
| 2261 | #ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2262 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2263 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2264 | Item.val.uCount != 2) { |
| 2265 | return -5; |
| 2266 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2267 | #else |
| 2268 | if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) { |
| 2269 | return -6; |
| 2270 | } |
| 2271 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2272 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2273 | // -------------------------------- |
| 2274 | // This test decodes the very large tag, but it is not in |
| 2275 | // any list so it is ignored. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2276 | QCBORDecode_Init(&DCtx, |
| 2277 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 2278 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2279 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2280 | return -6; |
| 2281 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2282 | /* |
| 2283 | if(Item.uTagBits) { // TODO: make sure it is OK to remove this |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2284 | return -7; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2285 | }*/ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2286 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2287 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2288 | // This test sets up a caller-config list that includes the very large |
| 2289 | // tage and then matches it. |
| 2290 | QCBORDecode_Init(&DCtx, |
| 2291 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 2292 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2293 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 2294 | const QCBORTagListIn TL = {2, puList}; |
| 2295 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2296 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2297 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2298 | return -8; |
| 2299 | } |
| 2300 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2301 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 2302 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 2303 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 2304 | Item.val.uCount != 0) { |
| 2305 | return -9; |
| 2306 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2307 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2308 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2309 | // Sets up a caller-configured list and look up something not in it |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2310 | const uint64_t puLongList[17] = {1,2,1}; |
| 2311 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2312 | QCBORDecode_Init(&DCtx, |
| 2313 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 2314 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2315 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 2316 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2317 | return -11; |
| 2318 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2319 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2320 | // ----------------------- |
| 2321 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2322 | QCBORDecode_Init(&DCtx, |
| 2323 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 2324 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2325 | uint64_t puTags[16]; |
| 2326 | QCBORTagListOut Out = {0, 4, puTags}; |
| 2327 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2328 | return -12; |
| 2329 | } |
| 2330 | if(puTags[0] != 0x9192939495969798 || |
| 2331 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2332 | puTags[2] != 0x06 || |
| 2333 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2334 | return -13; |
| 2335 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2336 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2337 | // ---------------------- |
| 2338 | // This text if too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2339 | QCBORDecode_Init(&DCtx, |
| 2340 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 2341 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2342 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 2343 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 2344 | return -14; |
| 2345 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2346 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2347 | #if 0 |
| 2348 | // TODO: this test needs to be re evaluated |
| 2349 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2350 | // --------------- |
| 2351 | // Parse a version of the "CSR" that has had a ton of tags randomly inserted |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2352 | QCBORDecode_Init(&DCtx, |
| 2353 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 2354 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2355 | int n = CheckCSRMaps(&DCtx); |
| 2356 | if(n) { |
| 2357 | return n-2000; |
| 2358 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2359 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2360 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2361 | QCBORDecode_Init(&DCtx, |
| 2362 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 2363 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2364 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2365 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 2366 | const QCBORTagListIn TagList = {3, puTagList}; |
| 2367 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2368 | |
| 2369 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2370 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2371 | return -100; |
| 2372 | } |
| 2373 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2374 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 2375 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 2376 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 2377 | Item.val.uCount != 2 || |
| 2378 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 2379 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 2380 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 2381 | Out.uNumUsed != 3) { |
| 2382 | return -101; |
| 2383 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2384 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2385 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2386 | return -102; |
| 2387 | } |
| 2388 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2389 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 2390 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
| 2391 | QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized |
| 2392 | Item.val.uCount != 2 || |
| 2393 | puTags[0] != 5859837686836516696 || |
| 2394 | puTags[1] != 7 || |
| 2395 | Out.uNumUsed != 2) { |
| 2396 | return -103; |
| 2397 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2398 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2399 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2400 | return -104; |
| 2401 | } |
| 2402 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2403 | Item.uTagBits || |
| 2404 | Item.val.uCount != 5 || |
| 2405 | puTags[0] != 0x0b || |
| 2406 | Out.uNumUsed != 1) { |
| 2407 | return -105; |
| 2408 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2409 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2410 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2411 | return -106; |
| 2412 | } |
| 2413 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2414 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 2415 | Item.val.string.len != 12 || |
| 2416 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 2417 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 2418 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 2419 | Out.uNumUsed != 3) { |
| 2420 | return -105; |
| 2421 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2422 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2423 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2424 | return -107; |
| 2425 | } |
| 2426 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2427 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 2428 | Item.val.string.len != 3 || |
| 2429 | puTags[0] != 773 || |
| 2430 | Out.uNumUsed != 1) { |
| 2431 | return -108; |
| 2432 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2433 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2434 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2435 | return -109; |
| 2436 | } |
| 2437 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2438 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2439 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2440 | puTags[0] != 16 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2441 | puTags[11] != 0x0f || |
| 2442 | Out.uNumUsed != 12) { |
| 2443 | return -110; |
| 2444 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2445 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2446 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2447 | return -111; |
| 2448 | } |
| 2449 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2450 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 2451 | Item.val.string.len != 9 || |
| 2452 | puTags[0] != 17 || |
| 2453 | Out.uNumUsed != 1) { |
| 2454 | return -112; |
| 2455 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2456 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2457 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2458 | return -111; |
| 2459 | } |
| 2460 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 2461 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 2462 | Item.val.string.len != 2 || |
| 2463 | puTags[0] != 17 || |
| 2464 | Out.uNumUsed != 1) { |
| 2465 | return -112; |
| 2466 | } |
| 2467 | |
| 2468 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2469 | return -113; |
| 2470 | } |
| 2471 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2472 | QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
| 2473 | Item.val.uCount != 2 || |
| 2474 | puTags[0] != 19 || |
| 2475 | Out.uNumUsed != 1) { |
| 2476 | return -114; |
| 2477 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2478 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2479 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2480 | return -115; |
| 2481 | } |
| 2482 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2483 | QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
| 2484 | Item.uTagBits || |
| 2485 | Item.val.uCount != 1 || |
| 2486 | puTags[0] != 9 || |
| 2487 | Out.uNumUsed != 1) { |
| 2488 | return -116; |
| 2489 | } |
| 2490 | |
| 2491 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2492 | return -116; |
| 2493 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2494 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2495 | Item.val.int64 != -7 || |
| 2496 | Item.uTagBits || |
| 2497 | Out.uNumUsed != 0) { |
| 2498 | return -117; |
| 2499 | } |
| 2500 | |
| 2501 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2502 | return -118; |
| 2503 | } |
| 2504 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 2505 | Item.val.string.len != 10 || |
| 2506 | Item.uTagBits || |
| 2507 | puTags[0] != 12 || |
| 2508 | Out.uNumUsed != 1) { |
| 2509 | return -119; |
| 2510 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2511 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2512 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2513 | return -120; |
| 2514 | } |
| 2515 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 2516 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 2517 | Item.val.uCount != 1 || |
| 2518 | puTags[0] != 0x17 || |
| 2519 | Out.uNumUsed != 1) { |
| 2520 | return -121; |
| 2521 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2522 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2523 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 2524 | return -122; |
| 2525 | } |
| 2526 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 2527 | QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
| 2528 | Item.val.int64 != -3 || |
| 2529 | puTags[0] != 8 || |
| 2530 | Out.uNumUsed != 1) { |
| 2531 | return -123; |
| 2532 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2533 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2534 | if(QCBORDecode_Finish(&DCtx)) { |
| 2535 | return -124; |
| 2536 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2537 | #else |
| 2538 | (void)spCSRWithTags; |
| 2539 | #endif |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2540 | return 0; |
| 2541 | } |
| 2542 | |
| 2543 | |
| 2544 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2545 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2546 | static uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2547 | 0x83, |
| 2548 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2549 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2550 | 0xA4, |
| 2551 | 0x63, 0x42, 0x4E, 0x2B, |
| 2552 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2553 | 0x18, 0x40, |
| 2554 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2555 | 0x63, 0x42, 0x4E, 0x2D, |
| 2556 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2557 | 0x38, 0x3F, |
| 2558 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 2559 | |
| 2560 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2561 | 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] | 2562 | |
| 2563 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2564 | int32_t BignumParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2565 | { |
| 2566 | QCBORDecodeContext DCtx; |
| 2567 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2568 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2569 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2570 | QCBORDecode_Init(&DCtx, |
| 2571 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 2572 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2573 | |
| 2574 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2575 | // |
| 2576 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 2577 | return -1; |
| 2578 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2579 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2580 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2581 | |
| 2582 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2583 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2584 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2585 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2586 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2587 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | // |
| 2591 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2592 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2593 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2594 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2595 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2596 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2597 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2598 | // |
| 2599 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2600 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2601 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2602 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2603 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2604 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2605 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2606 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2607 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 2608 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2609 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2610 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2614 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2615 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 2616 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 2617 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2618 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2619 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2620 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2621 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2622 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2623 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2624 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 2625 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2626 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2627 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2628 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2629 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2630 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2631 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2632 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 2633 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 2634 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2635 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2636 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2637 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2638 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2639 | return 0; |
| 2640 | } |
| 2641 | |
| 2642 | |
| 2643 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2644 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2645 | uint8_t uDataType, |
| 2646 | uint8_t uNestingLevel, |
| 2647 | uint8_t uNextNest, |
| 2648 | int64_t nLabel, |
| 2649 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2650 | { |
| 2651 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2652 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2653 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2654 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 2655 | if(Item.uDataType != uDataType) return -1; |
| 2656 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2657 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 2658 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 2659 | return -1; |
| 2660 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2661 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 2662 | if(Item.label.int64 != nLabel) return -1; |
| 2663 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2664 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2668 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2669 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2670 | if(pItem) { |
| 2671 | *pItem = Item; |
| 2672 | } |
| 2673 | return 0; |
| 2674 | } |
| 2675 | |
| 2676 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2677 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2678 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2679 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2680 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2681 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2682 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2683 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2684 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2685 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2686 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1; |
| 2687 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1; |
| 2688 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1; |
| 2689 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1; |
| 2690 | 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] | 2691 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2692 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1; |
| 2693 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2694 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2695 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1; |
| 2696 | 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] | 2697 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 2698 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1; |
| 2699 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2700 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2701 | if(QCBORDecode_Finish(pDC)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2702 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2703 | return 0; |
| 2704 | } |
| 2705 | |
| 2706 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2707 | /* |
| 2708 | // cbor.me decoded output |
| 2709 | { |
| 2710 | -23: { |
| 2711 | -20: { |
| 2712 | -18: "Organization", |
| 2713 | -17: "SSG", |
| 2714 | -15: "Confusion", |
| 2715 | -16: "San Diego", |
| 2716 | -14: "US" |
| 2717 | }, |
| 2718 | -19: { |
| 2719 | -11: { |
| 2720 | -9: -7 |
| 2721 | }, |
| 2722 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 2723 | } |
| 2724 | }, |
| 2725 | -22: { |
| 2726 | -5: -3 |
| 2727 | } |
| 2728 | } |
| 2729 | */ |
| 2730 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2731 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2732 | static uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2733 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 2734 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 2735 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 2736 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 2737 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 2738 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 2739 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 2740 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 2741 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 2742 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2743 | int32_t NestedMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2744 | { |
| 2745 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2746 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2747 | QCBORDecode_Init(&DCtx, |
| 2748 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 2749 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2750 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2751 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2752 | } |
| 2753 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2754 | |
| 2755 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2756 | int32_t StringDecoderModeFailTest() |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2757 | { |
| 2758 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2759 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2760 | QCBORDecode_Init(&DCtx, |
| 2761 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 2762 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2763 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2764 | QCBORItem Item; |
| 2765 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2766 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2767 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2768 | return -1; |
| 2769 | } |
| 2770 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 2771 | return -2; |
| 2772 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2773 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 2774 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2775 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 2776 | return -3; |
| 2777 | } |
| 2778 | |
| 2779 | return 0; |
| 2780 | } |
| 2781 | |
| 2782 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2783 | // Same map as above, but using indefinite lengths |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2784 | static uint8_t spCSRInputIndefLen[] = { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2785 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 2786 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 2787 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 2788 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 2789 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 2790 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2791 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 2792 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 2793 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 2794 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2795 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2796 | int32_t NestedMapTestIndefLen() |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2797 | { |
| 2798 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2799 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2800 | QCBORDecode_Init(&DCtx, |
| 2801 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 2802 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2803 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2804 | return CheckCSRMaps(&DCtx); |
| 2805 | } |
| 2806 | |
| 2807 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2808 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2809 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 2810 | { |
| 2811 | UsefulOutBuf UOB; |
| 2812 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2813 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2814 | int i; |
| 2815 | for(i = 0; i < n; i++) { |
| 2816 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 2817 | } |
| 2818 | |
| 2819 | for(i = 0; i < n; i++) { |
| 2820 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 2821 | } |
| 2822 | return UsefulOutBuf_OutUBuf(&UOB); |
| 2823 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2824 | |
| 2825 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2826 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2827 | { |
| 2828 | QCBORDecodeContext DC; |
| 2829 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2830 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2831 | int j; |
| 2832 | for(j = 0; j < nNestLevel; j++) { |
| 2833 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2834 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2835 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 2836 | // Should be in error |
| 2837 | if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
| 2838 | return -4; |
| 2839 | } else { |
| 2840 | return 0; // Decoding doesn't recover after an error |
| 2841 | } |
| 2842 | } else { |
| 2843 | // Should be no error |
| 2844 | if(nReturn) { |
| 2845 | return -9; // Should not have got an error |
| 2846 | } |
| 2847 | } |
| 2848 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2849 | return -7; |
| 2850 | } |
| 2851 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2852 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2853 | if(nReturn) { |
| 2854 | return -3; |
| 2855 | } |
| 2856 | return 0; |
| 2857 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2858 | |
| 2859 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2860 | // TODO: investigate why this doesn't fail in master and does here. It seems like a broken test. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2861 | int32_t IndefiniteLengthNestTest() |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2862 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2863 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2864 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2865 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2866 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 2867 | int nReturn = parse_indeflen_nested(Nested, i); |
| 2868 | if(nReturn) { |
| 2869 | return nReturn; |
| 2870 | } |
| 2871 | } |
| 2872 | return 0; |
| 2873 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2874 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2875 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2876 | // [1, [2, 3]] |
| 2877 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 2878 | // No closing break |
| 2879 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 2880 | // Not enough closing breaks |
| 2881 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 2882 | // Too many closing breaks |
| 2883 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 2884 | // Unclosed indeflen inside def len |
| 2885 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 2886 | // confused tag |
| 2887 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2888 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2889 | int32_t IndefiniteLengthArrayMapTest() |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2890 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 2891 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2892 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2893 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2894 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2895 | // Decode it and see if it is OK |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2896 | UsefulBuf_MAKE_STACK_UB(MemPool, 150); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2897 | QCBORDecodeContext DC; |
| 2898 | QCBORItem Item; |
| 2899 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2900 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2901 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2902 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2903 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2904 | |
| 2905 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2906 | Item.uNestingLevel != 0 || |
| 2907 | Item.uNextNestLevel != 1) { |
| 2908 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2909 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2910 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2911 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2912 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 2913 | Item.uNestingLevel != 1 || |
| 2914 | Item.uNextNestLevel != 1) { |
| 2915 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2916 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2917 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2918 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2919 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2920 | Item.uNestingLevel != 1 || |
| 2921 | Item.uNextNestLevel != 2) { |
| 2922 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2923 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2924 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2925 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 2926 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2927 | Item.uNestingLevel != 2 || |
| 2928 | Item.uNextNestLevel != 2) { |
| 2929 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2930 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2931 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2932 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 2933 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2934 | Item.uNestingLevel != 2 || |
| 2935 | Item.uNextNestLevel != 0) { |
| 2936 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2937 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2938 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2939 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2940 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 2941 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2942 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2943 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2944 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2945 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2946 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2947 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2948 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2949 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2950 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2951 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2952 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2953 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2954 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 2955 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2956 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2957 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2958 | } |
| 2959 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2960 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2961 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2962 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2963 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2964 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2965 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2966 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2967 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2968 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2969 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2970 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2974 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2975 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2976 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2977 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2978 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2979 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2980 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2981 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2982 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2983 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2984 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2985 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2986 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2987 | |
| 2988 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2989 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2990 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2991 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2992 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2993 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2994 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2995 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2996 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 2997 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2998 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 2999 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3000 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3001 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3002 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3003 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3004 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3005 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3006 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3007 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3008 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3009 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3010 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3011 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3012 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3013 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3014 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3015 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3016 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3017 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3018 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3019 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3020 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3021 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3022 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3023 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3024 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3025 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 3026 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3027 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3028 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3029 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3030 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3031 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3032 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3033 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3034 | QCBORDecode_SetMemPool(&DC, MemPool, false); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3035 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3036 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3037 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3038 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3039 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3040 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3041 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3042 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3043 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3044 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3045 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3046 | return 0; |
| 3047 | } |
| 3048 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3049 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3050 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3051 | 0x81, // Array of length one |
| 3052 | 0x7f, // text string marked with indefinite length |
| 3053 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3054 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3055 | 0xff // ending break |
| 3056 | }; |
| 3057 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3058 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3059 | 0x81, // Array of length one |
| 3060 | 0x7f, // text string marked with indefinite length |
| 3061 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3062 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 3063 | 0xff // ending break |
| 3064 | }; |
| 3065 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3066 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3067 | 0x81, // Array of length one |
| 3068 | 0x7f, // text string marked with indefinite length |
| 3069 | 0x01, 0x02, // Not a string |
| 3070 | 0xff // ending break |
| 3071 | }; |
| 3072 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3073 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3074 | 0x81, // Array of length one |
| 3075 | 0x7f, // text string marked with indefinite length |
| 3076 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3077 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3078 | // missing end of string |
| 3079 | }; |
| 3080 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3081 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3082 | 0xa1, // Array of length one |
| 3083 | 0x7f, // text string marked with indefinite length |
| 3084 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 3085 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3086 | 0xff, // ending break |
| 3087 | 0x01 // integer being labeled. |
| 3088 | }; |
| 3089 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3090 | /** |
| 3091 | Make an indefinite length string |
| 3092 | |
| 3093 | @param Storage Storage for string, must be 144 bytes in size |
| 3094 | @return The indefinite length string |
| 3095 | |
| 3096 | This makes an array with one indefinite length string that has 7 chunks |
| 3097 | from size of 1 byte up to 64 bytes. |
| 3098 | */ |
| 3099 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3100 | { |
| 3101 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3102 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3103 | UsefulOutBuf_Init(&UOB, Storage); |
| 3104 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 3105 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3106 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3107 | uint8_t uStringByte = 0; |
| 3108 | // Use of type int is intentional |
| 3109 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 3110 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3111 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3112 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 3113 | for(int j = 0; j < uChunkSize; j++) { |
| 3114 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 3115 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3116 | } |
| 3117 | } |
| 3118 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3119 | |
| 3120 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3121 | } |
| 3122 | |
| 3123 | static int CheckBigString(UsefulBufC BigString) |
| 3124 | { |
| 3125 | if(BigString.len != 255) { |
| 3126 | return 1; |
| 3127 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3128 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3129 | for(uint8_t i = 0; i < 255; i++){ |
| 3130 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 3131 | return 1; |
| 3132 | } |
| 3133 | } |
| 3134 | return 0; |
| 3135 | } |
| 3136 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3137 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3138 | int32_t IndefiniteLengthStringTest() |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3139 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3140 | QCBORDecodeContext DC; |
| 3141 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3142 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3143 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3144 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3145 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3146 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3147 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3148 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3149 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3150 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3151 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3152 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3153 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3154 | return -2; |
| 3155 | } |
| 3156 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 3157 | return -3; |
| 3158 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3159 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3160 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3161 | return -4; |
| 3162 | } |
| 3163 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 3164 | return -5; |
| 3165 | } |
| 3166 | if(QCBORDecode_Finish(&DC)) { |
| 3167 | return -6; |
| 3168 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3169 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3170 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3171 | QCBORDecode_Init(&DC, |
| 3172 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 3173 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3174 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3175 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3176 | return -7; |
| 3177 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3178 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3179 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3180 | return -8; |
| 3181 | } |
| 3182 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3183 | return -9; |
| 3184 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3185 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3186 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3187 | return -10; |
| 3188 | } |
| 3189 | |
| 3190 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3191 | QCBORDecode_Init(&DC, |
| 3192 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 3193 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3194 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3195 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3196 | return -11; |
| 3197 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3198 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3199 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3200 | return -12; |
| 3201 | } |
| 3202 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3203 | return -13; |
| 3204 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3205 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3206 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3207 | return -14; |
| 3208 | } |
| 3209 | |
| 3210 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3211 | QCBORDecode_Init(&DC, |
| 3212 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 3213 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3214 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3215 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3216 | return -15; |
| 3217 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3218 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3219 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3220 | return -16; |
| 3221 | } |
| 3222 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3223 | return -17; |
| 3224 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3225 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3226 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 3227 | return -18; |
| 3228 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3229 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3230 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3231 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3232 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3233 | QCBORDecode_GetNext(&DC, &Item); |
| 3234 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3235 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3236 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3237 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3238 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3239 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3240 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3241 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3242 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3243 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3244 | |
| 3245 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 3246 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3247 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3248 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3249 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3250 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3251 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3252 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3253 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3254 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 3255 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3256 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3257 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3258 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3259 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3260 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3261 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3262 | QCBORDecode_GetNext(&DC, &Item); |
| 3263 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3264 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3265 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3266 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3267 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3268 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3269 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3270 | // ---- big bstr ----- |
| 3271 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3272 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3273 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3274 | return -25; |
| 3275 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3276 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3277 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3278 | return -26; |
| 3279 | } |
| 3280 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3281 | return -26; |
| 3282 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3283 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3284 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3285 | return -27; |
| 3286 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3287 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3288 | return -28; |
| 3289 | } |
| 3290 | if(CheckBigString(Item.val.string)) { |
| 3291 | return -3; |
| 3292 | } |
| 3293 | if(QCBORDecode_Finish(&DC)) { |
| 3294 | return -29; |
| 3295 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3296 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3297 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3298 | 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] | 3299 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3300 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3301 | return -30; |
| 3302 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3303 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3304 | QCBORDecode_GetNext(&DC, &Item); |
| 3305 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3306 | return -31; |
| 3307 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3308 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3309 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 3310 | return -32; |
| 3311 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3312 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 3313 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3314 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 3315 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 3316 | return -33; |
| 3317 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3318 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3319 | if(QCBORDecode_Finish(&DC)) { |
| 3320 | return -34; |
| 3321 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3322 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3323 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3327 | int32_t AllocAllStringsTest() |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3328 | { |
| 3329 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3330 | QCBORError nCBORError; |
| 3331 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3332 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3333 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3334 | QCBORDecode_Init(&DC, |
| 3335 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3336 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3337 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3338 | 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] | 3339 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3340 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 3341 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3342 | return -1; |
| 3343 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3344 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3345 | if(CheckCSRMaps(&DC)) { |
| 3346 | return -2; |
| 3347 | } |
| 3348 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3349 | // 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] | 3350 | 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] | 3351 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3352 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3353 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 3354 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3355 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3356 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3357 | QCBORItem Item1, Item2, Item3, Item4; |
| 3358 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3359 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3360 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 3361 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3362 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3363 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3364 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3365 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3366 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3367 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3368 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3369 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3370 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3371 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 3372 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3373 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3374 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3375 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 3376 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3377 | Item1.uDataAlloc != 0 || |
| 3378 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3379 | UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3380 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3381 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3382 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3383 | |
| 3384 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3385 | UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3386 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3387 | Item2.uDataAlloc != 0 || |
| 3388 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3389 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3390 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3391 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3392 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3393 | Item3.uDataAlloc == 0 || |
| 3394 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3395 | UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3396 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3397 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3398 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3399 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3400 | Item4.uDataAlloc == 0 || |
| 3401 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3402 | UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3403 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3404 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3405 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3406 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3407 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3408 | QCBORDecode_Init(&DC, |
| 3409 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 3410 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3411 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 3412 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3413 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3414 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3415 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3416 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 3417 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3418 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 3419 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 3420 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 3421 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 3422 | } |
| 3423 | } |
| 3424 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3425 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3426 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | return 0; |
| 3430 | } |
| 3431 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3432 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3433 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3434 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 3435 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3436 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 3437 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3438 | QCBORDecodeContext DC; |
| 3439 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 3440 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3441 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3442 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3443 | // Then fish into the internals of the decode context |
| 3444 | // to get the allocator function so it can be called directly. |
| 3445 | // Also figure out how much pool is available for use |
| 3446 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3447 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3448 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 3449 | if(nError) { |
| 3450 | return -9; |
| 3451 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3452 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 3453 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 3454 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3455 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3456 | // First test -- ask for one more byte than available and see failure |
| 3457 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3458 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 3459 | return -1; |
| 3460 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3461 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3462 | // Re do the set up for the next test that will do a successful alloc, |
| 3463 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3464 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3465 | pAlloc = DC.StringAllocator.pfAllocator; |
| 3466 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 3467 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3468 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3469 | // Allocate one byte less than available and see success |
| 3470 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3471 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 3472 | return -2; |
| 3473 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3474 | // Ask for some more and see failure |
| 3475 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3476 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 3477 | return -3; |
| 3478 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3479 | // Free the first allocate, retry the second and see success |
| 3480 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 3481 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3482 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 3483 | return -4; |
| 3484 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3485 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3486 | // Re do set up for next test that involves a successful alloc, |
| 3487 | // and a successful realloc and a failed realloc |
| 3488 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3489 | pAlloc = DC.StringAllocator.pfAllocator; |
| 3490 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3491 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3492 | // Allocate half the pool and see success |
| 3493 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3494 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 3495 | return -5; |
| 3496 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3497 | // Reallocate to take up the whole pool and see success |
| 3498 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3499 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 3500 | return -6; |
| 3501 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3502 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3503 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 3504 | return -7; |
| 3505 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3506 | // Try to allocate more to be sure there is failure after a realloc |
| 3507 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 3508 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3509 | return -8; |
| 3510 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3511 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3512 | return 0; |
| 3513 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3514 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3515 | |
| 3516 | /* Just enough of an allocator to test configuration of one */ |
| 3517 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 3518 | { |
| 3519 | (void)pOldMem; // unused variable |
| 3520 | |
| 3521 | if(uNewSize) { |
| 3522 | // Assumes the context pointer is the buffer and |
| 3523 | // nothing too big will ever be asked for. |
| 3524 | // This is only good for this basic test! |
| 3525 | return (UsefulBuf) {pCtx, uNewSize}; |
| 3526 | } else { |
| 3527 | return NULLUsefulBuf; |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3532 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3533 | { |
| 3534 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 3535 | // nothing can be done with it unless that is set up. |
| 3536 | QCBORDecodeContext DC; |
| 3537 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 3538 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 3539 | |
| 3540 | uint8_t pAllocatorBuffer[50]; |
| 3541 | |
| 3542 | // This is really just to test that this call works. |
| 3543 | // The full functionality of string allocators is tested |
| 3544 | // elsewhere with the MemPool internal allocator. |
| 3545 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 3546 | |
| 3547 | QCBORItem Item; |
| 3548 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 3549 | return -1; |
| 3550 | } |
| 3551 | |
| 3552 | if(Item.uDataAlloc == 0 || |
| 3553 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3554 | Item.val.string.ptr != pAllocatorBuffer) { |
| 3555 | return -2; |
| 3556 | } |
| 3557 | |
| 3558 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 3559 | return -3; |
| 3560 | } |
| 3561 | |
| 3562 | return 0; |
| 3563 | } |
| 3564 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3565 | |
| 3566 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3567 | /* exponent, mantissa |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3568 | [ |
| 3569 | 4([-1, 3]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3570 | 4([-20, 4759477275222530853136]), |
| 3571 | 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3572 | 5([300, 100]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3573 | 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3574 | 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3575 | 5([ 9223372036854775806, -4759477275222530853137]) |
| 3576 | 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3577 | ] |
| 3578 | */ |
| 3579 | |
| 3580 | static const uint8_t spExpectedExponentsAndMantissas[] = { |
| 3581 | 0x87, |
| 3582 | 0xC4, 0x82, 0x20, |
| 3583 | 0x03, |
| 3584 | 0xC4, 0x82, 0x33, |
| 3585 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 3586 | 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3587 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 3588 | 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 3589 | 0x18, 0x64, |
| 3590 | 0xC5, 0x82, 0x33, |
| 3591 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 3592 | 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 3593 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3594 | 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 3595 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3596 | 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 3597 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE |
| 3598 | }; |
| 3599 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3600 | int32_t ExponentAndMantissaDecodeTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3601 | { |
| 3602 | QCBORDecodeContext DC; |
| 3603 | QCBORError nCBORError; |
| 3604 | QCBORItem item; |
| 3605 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 3606 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 3607 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 3608 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3609 | |
| 3610 | |
| 3611 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL); |
| 3612 | |
| 3613 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3614 | if(nCBORError != QCBOR_SUCCESS) { |
| 3615 | return 1; |
| 3616 | } |
| 3617 | |
| 3618 | if(item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3619 | return 2; |
| 3620 | } |
| 3621 | |
| 3622 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3623 | if(nCBORError != QCBOR_SUCCESS) { |
| 3624 | return 3; |
| 3625 | } |
| 3626 | |
| 3627 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3628 | item.val.expAndMantissa.Mantissa.nInt != 3 || |
| 3629 | item.val.expAndMantissa.nExponent != -1) { |
| 3630 | return 4; |
| 3631 | } |
| 3632 | |
| 3633 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3634 | if(nCBORError != QCBOR_SUCCESS) { |
| 3635 | return 5; |
| 3636 | } |
| 3637 | |
| 3638 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 3639 | item.val.expAndMantissa.nExponent != -20 || |
| 3640 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3641 | return 6; |
| 3642 | } |
| 3643 | |
| 3644 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3645 | if(nCBORError != QCBOR_SUCCESS) { |
| 3646 | return 7; |
| 3647 | } |
| 3648 | |
| 3649 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM || |
| 3650 | item.val.expAndMantissa.nExponent != 9223372036854775807 || |
| 3651 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3652 | return 8; |
| 3653 | } |
| 3654 | |
| 3655 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3656 | if(nCBORError != QCBOR_SUCCESS) { |
| 3657 | return 9; |
| 3658 | } |
| 3659 | |
| 3660 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 3661 | item.val.expAndMantissa.Mantissa.nInt != 100 || |
| 3662 | item.val.expAndMantissa.nExponent != 300) { |
| 3663 | return 10; |
| 3664 | } |
| 3665 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3666 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3667 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3668 | if(nCBORError != QCBOR_SUCCESS) { |
| 3669 | return 11; |
| 3670 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3671 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM || |
| 3672 | item.val.expAndMantissa.nExponent != -20 || |
| 3673 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3674 | return 12; |
| 3675 | } |
| 3676 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3677 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3678 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3679 | if(nCBORError != QCBOR_SUCCESS) { |
| 3680 | return 13; |
| 3681 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3682 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 3683 | item.val.expAndMantissa.nExponent != -9223372036854775807 || |
| 3684 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3685 | return 14; |
| 3686 | } |
| 3687 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 3688 | // 5([ 9223372036854775806, -4759477275222530853137]) |
| 3689 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3690 | if(nCBORError != QCBOR_SUCCESS) { |
| 3691 | return 13; |
| 3692 | } |
| 3693 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 3694 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 3695 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3696 | return 14; |
| 3697 | } |
| 3698 | |
| 3699 | |
| 3700 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3701 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3702 | if(nCBORError != QCBOR_SUCCESS) { |
| 3703 | return 15; |
| 3704 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3705 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 3706 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 3707 | item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) { |
| 3708 | return 16; |
| 3709 | } |
| 3710 | |
| 3711 | /* Now encode some stuff and then decode it */ |
| 3712 | uint8_t pBuf[40]; |
| 3713 | QCBOREncodeContext EC; |
| 3714 | UsefulBufC Encoded; |
| 3715 | |
| 3716 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 3717 | QCBOREncode_OpenArray(&EC); |
| 3718 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 3719 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 3720 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 3721 | QCBOREncode_CloseArray(&EC); |
| 3722 | QCBOREncode_Finish(&EC, &Encoded); |
| 3723 | |
| 3724 | |
| 3725 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
| 3726 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3727 | if(nCBORError != QCBOR_SUCCESS) { |
| 3728 | return 13; |
| 3729 | } |
| 3730 | |
| 3731 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3732 | if(nCBORError != QCBOR_SUCCESS) { |
| 3733 | return 13; |
| 3734 | } |
| 3735 | |
| 3736 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 3737 | item.val.expAndMantissa.nExponent != 1000 || |
| 3738 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
| 3739 | return 15; |
| 3740 | } |
| 3741 | |
| 3742 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3743 | if(nCBORError != QCBOR_SUCCESS) { |
| 3744 | return 13; |
| 3745 | } |
| 3746 | |
| 3747 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 3748 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 3749 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
| 3750 | return 15; |
| 3751 | } |
| 3752 | |
| 3753 | nCBORError = QCBORDecode_GetNext(&DC, &item); |
| 3754 | if(nCBORError != QCBOR_SUCCESS) { |
| 3755 | return 13; |
| 3756 | } |
| 3757 | |
| 3758 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 3759 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 3760 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 3761 | return 12; |
| 3762 | } |
| 3763 | |
| 3764 | return 0; |
| 3765 | } |
| 3766 | |
| 3767 | |
| 3768 | static struct FailInput ExponentAndMantissaFailures[] = { |
| 3769 | // Exponent > INT64_MAX |
| 3770 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3771 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3772 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3773 | // Mantissa > INT64_MAX |
| 3774 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 3775 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 3776 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3777 | // End of input |
| 3778 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END}, |
| 3779 | // End of input |
| 3780 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END}, |
| 3781 | // bad content for big num |
| 3782 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 3783 | // bad content for big num |
| 3784 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 3785 | // Bad integer for exponent |
| 3786 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 3787 | // Bad integer for mantissa |
| 3788 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 3789 | // 3 items in array |
| 3790 | { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3791 | // unterminated indefinite length array |
| 3792 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3793 | // Empty array |
| 3794 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 3795 | // Second is not an integer |
| 3796 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3797 | // First is not an integer |
| 3798 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 3799 | // Not an array |
| 3800 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 3801 | }; |
| 3802 | |
| 3803 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3804 | int32_t ExponentAndMantissaDecodeFailTests() |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3805 | { |
| 3806 | return ProcessFailures(ExponentAndMantissaFailures, |
| 3807 | sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput)); |
| 3808 | } |
| 3809 | |
| 3810 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3811 | |
| 3812 | |
| 3813 | |
| 3814 | /* |
| 3815 | Some basic CBOR with map and array used in a lot of tests. |
| 3816 | The map labels are all strings |
| 3817 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3818 | { |
| 3819 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3820 | "an array of two strings": [ |
| 3821 | "string1", "string2" |
| 3822 | ], |
| 3823 | "map in a map": { |
| 3824 | "bytes 1": h'78787878', |
| 3825 | "bytes 2": h'79797979', |
| 3826 | "another int": 98, |
| 3827 | "text 2": "lies, damn lies and statistics" |
| 3828 | } |
| 3829 | } |
| 3830 | */ |
| 3831 | |
Laurence Lundblade | eb0b53d | 2020-04-14 18:34:25 -0700 | [diff] [blame] | 3832 | #include "qcbor/qcbor_decode_map.h" |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3833 | #include <stdio.h> |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3834 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3835 | static char strbuf[10]; |
| 3836 | const char *PrintType(uint8_t type) { |
| 3837 | switch(type) { |
| 3838 | case QCBOR_TYPE_INT64: return "INT64"; |
| 3839 | case QCBOR_TYPE_UINT64: return "UINT64"; |
| 3840 | case QCBOR_TYPE_ARRAY: return "ARRAY"; |
| 3841 | case QCBOR_TYPE_MAP: return "MAP"; |
| 3842 | case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING"; |
| 3843 | case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING"; |
| 3844 | default: |
| 3845 | sprintf(strbuf, "%d", type); |
| 3846 | return strbuf; |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | |
| 3851 | void PrintItem(QCBORItem Item) |
| 3852 | { |
| 3853 | printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":""); |
| 3854 | if(Item.uLabelType) { |
| 3855 | printf("Label: %s ", PrintType(Item.uLabelType)); |
| 3856 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3857 | printf("%lld\n", Item.label.int64); |
| 3858 | } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
| 3859 | printf("\"%4.4s\"\n", Item.label.string.ptr); |
| 3860 | } |
| 3861 | } |
| 3862 | } |
| 3863 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3864 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3865 | int32_t EnterMapTest() |
| 3866 | { |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3867 | QCBORItem Item1, Item2, Item3; |
| 3868 | int64_t nDecodedInt1, nDecodedInt2; |
| 3869 | UsefulBufC B1, B2, S1, S2, S3; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3870 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3871 | QCBORDecodeContext DCtx; |
| 3872 | QCBORError nCBORError; |
| 3873 | |
| 3874 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3875 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3876 | QCBORDecode_EnterMap(&DCtx); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3877 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3878 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3879 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3880 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3881 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 3882 | QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1); |
| 3883 | QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2); |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3884 | QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1); |
| 3885 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3886 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3887 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 3888 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3889 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 3890 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 3891 | return -400; |
| 3892 | } |
| 3893 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3894 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3895 | // Parse the same array again using GetText() instead of GetItem() |
| 3896 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 3897 | QCBORDecode_GetText(&DCtx, &S2); |
| 3898 | QCBORDecode_GetText(&DCtx, &S3); |
| 3899 | // TODO, check for end of array? |
| 3900 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3901 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3902 | QCBORDecode_ExitMap(&DCtx); |
| 3903 | |
| 3904 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 3905 | |
| 3906 | if(nCBORError) { |
| 3907 | return (int32_t)nCBORError; |
| 3908 | } |
| 3909 | |
| 3910 | if(nDecodedInt1 != 42) { |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3911 | return 1001; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3912 | } |
| 3913 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3914 | if(nDecodedInt2 != 98) { |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3915 | return 1002; |
| 3916 | } |
| 3917 | |
| 3918 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3919 | UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){ |
| 3920 | return 1003; |
| 3921 | } |
| 3922 | |
| 3923 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3924 | UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){ |
| 3925 | return 1004; |
| 3926 | } |
| 3927 | |
| 3928 | if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){ |
| 3929 | return 1005; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3930 | } |
| 3931 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 3932 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 3933 | return 1006; |
| 3934 | } |
| 3935 | |
| 3936 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 3937 | return 1007; |
| 3938 | } |
| 3939 | |
| 3940 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 3941 | return 1008; |
| 3942 | } |
| 3943 | |
| 3944 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 3945 | return 1009; |
| 3946 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 3947 | |
| 3948 | // These tests confirm the cursor is at the right place after entering a map or array |
| 3949 | |
| 3950 | // Confirm cursor is at right place |
| 3951 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 3952 | QCBORDecode_EnterMap(&DCtx); |
| 3953 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3954 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 3955 | return 2001; |
| 3956 | } |
| 3957 | |
| 3958 | |
| 3959 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 3960 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3961 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3962 | QCBORDecode_EnterArray(&DCtx); |
| 3963 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3964 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 3965 | return 2002; |
| 3966 | } |
| 3967 | |
| 3968 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 3969 | QCBORDecode_EnterMap(&DCtx); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 3970 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3971 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3972 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 3973 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 3974 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3975 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3976 | return 2003; |
| 3977 | } |
| 3978 | |
| 3979 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 3980 | QCBORDecode_EnterMap(&DCtx); |
| 3981 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3982 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3983 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3984 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3985 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3986 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3987 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3988 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 3989 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 3990 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 3991 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3995 | return 0; |
| 3996 | } |
| 3997 | |
| 3998 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 3999 | struct NumberConversion { |
| 4000 | char *szDescription; |
| 4001 | UsefulBufC CBOR; |
| 4002 | int64_t nConvertedToInt64; |
| 4003 | QCBORError uErrorInt64; |
| 4004 | uint64_t uConvertToUInt64; |
| 4005 | QCBORError uErrorUint64; |
| 4006 | double dConvertToDouble; |
| 4007 | QCBORError uErrorDouble; |
| 4008 | }; |
| 4009 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4010 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4011 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4012 | "negative bignum -1", |
| 4013 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 4014 | -1, |
| 4015 | QCBOR_SUCCESS, |
| 4016 | 0, |
| 4017 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 4018 | -1.0, |
| 4019 | QCBOR_SUCCESS |
| 4020 | }, |
| 4021 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4022 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4023 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 4024 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 4025 | 257000, |
| 4026 | QCBOR_SUCCESS, |
| 4027 | 257000, |
| 4028 | QCBOR_SUCCESS, |
| 4029 | 257000.0, |
| 4030 | QCBOR_SUCCESS |
| 4031 | }, |
| 4032 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4033 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4034 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 4035 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4036 | -2064, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4037 | QCBOR_SUCCESS, |
| 4038 | 0, |
| 4039 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4040 | -2064.0, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4041 | QCBOR_SUCCESS |
| 4042 | }, |
| 4043 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4044 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4045 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 4046 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
| 4047 | 2056, |
| 4048 | QCBOR_SUCCESS, |
| 4049 | 2056, |
| 4050 | QCBOR_SUCCESS, |
| 4051 | 2056.0, |
| 4052 | QCBOR_SUCCESS |
| 4053 | }, |
| 4054 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4055 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4056 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 4057 | 0, |
| 4058 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4059 | 0, |
| 4060 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 4061 | -18446744073709551617.0, |
| 4062 | QCBOR_SUCCESS |
| 4063 | }, |
| 4064 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4065 | "Positive bignum 0x01020304 indefinite length string", |
| 4066 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 4067 | 0x01020304, |
| 4068 | QCBOR_SUCCESS, |
| 4069 | 0x01020304, |
| 4070 | QCBOR_SUCCESS, |
| 4071 | 16909060.0, |
| 4072 | QCBOR_SUCCESS |
| 4073 | }, |
| 4074 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4075 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4076 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4077 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
| 4078 | 0, |
| 4079 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4080 | 0, |
| 4081 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 4082 | -INFINITY, |
| 4083 | QCBOR_SUCCESS |
| 4084 | }, |
| 4085 | { |
| 4086 | "big float [9223372036854775806, 9223372036854775806]", |
| 4087 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4088 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 4089 | 0, |
| 4090 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4091 | 0, |
| 4092 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4093 | INFINITY, |
| 4094 | QCBOR_SUCCESS |
| 4095 | }, |
| 4096 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4097 | "Big float 3 * 2^^2", |
| 4098 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
| 4099 | 12, |
| 4100 | QCBOR_SUCCESS, |
| 4101 | 12, |
| 4102 | QCBOR_SUCCESS, |
| 4103 | 12.0, |
| 4104 | QCBOR_SUCCESS |
| 4105 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4106 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4107 | "Positive integer 18446744073709551615", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4108 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 4109 | 0, |
| 4110 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4111 | 18446744073709551615ULL, |
| 4112 | QCBOR_SUCCESS, |
| 4113 | 18446744073709551615.0, |
| 4114 | QCBOR_SUCCESS |
| 4115 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4116 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4117 | "Positive bignum 0xffff", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4118 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 4119 | 65536-1, |
| 4120 | QCBOR_SUCCESS, |
| 4121 | 0xffff, |
| 4122 | QCBOR_SUCCESS, |
| 4123 | 65535.0, |
| 4124 | QCBOR_SUCCESS |
| 4125 | }, |
| 4126 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4127 | "Postive integer 0", |
| 4128 | {(uint8_t[]){0x0}, 1}, |
| 4129 | 0LL, |
| 4130 | QCBOR_SUCCESS, |
| 4131 | 0ULL, |
| 4132 | QCBOR_SUCCESS, |
| 4133 | 0.0, |
| 4134 | QCBOR_SUCCESS |
| 4135 | }, |
| 4136 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4137 | "Negative integer -18446744073709551616", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4138 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 4139 | -9223372036854775807-1, // INT64_MIN |
| 4140 | QCBOR_SUCCESS, |
| 4141 | 0ULL, |
| 4142 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 4143 | -9223372036854775808.0, |
| 4144 | QCBOR_SUCCESS |
| 4145 | }, |
| 4146 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4147 | "Double Floating point value 100.3", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4148 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
| 4149 | 100L, |
| 4150 | QCBOR_SUCCESS, |
| 4151 | 100ULL, |
| 4152 | QCBOR_SUCCESS, |
| 4153 | 100.3, |
| 4154 | QCBOR_SUCCESS |
| 4155 | }, |
| 4156 | { |
| 4157 | "Floating point value NaN 0xfa7fc00000", |
| 4158 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
| 4159 | 0, |
| 4160 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4161 | 0, |
| 4162 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4163 | NAN, |
| 4164 | QCBOR_SUCCESS |
| 4165 | }, |
| 4166 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4167 | "half-precision Floating point value -4", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4168 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
| 4169 | -4, |
| 4170 | QCBOR_SUCCESS, |
| 4171 | 0, |
| 4172 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 4173 | -4.0, |
| 4174 | QCBOR_SUCCESS |
| 4175 | }, |
| 4176 | { |
| 4177 | "Decimal fraction 3/10", |
| 4178 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
| 4179 | 0, |
| 4180 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4181 | 0, |
| 4182 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 4183 | 0.30000000000000004, |
| 4184 | QCBOR_SUCCESS |
| 4185 | } |
| 4186 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4187 | |
| 4188 | |
| 4189 | |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4190 | int32_t IntegerConvertTest() |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4191 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4192 | const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4193 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4194 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 4195 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4196 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4197 | // Set up the decoding context including a memory pool so that |
| 4198 | // indefinite length items can be checked |
| 4199 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4200 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4201 | |
| 4202 | /* ----- test conversion to int64_t ------ */ |
| 4203 | QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4204 | QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 4205 | if(nCBORError) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4206 | return (int32_t)(1000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4210 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4211 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4212 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4213 | } |
| 4214 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4215 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4218 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4219 | QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 4220 | nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 4221 | if(nCBORError) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4222 | return (int32_t)(1000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4223 | } |
| 4224 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4225 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4226 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4227 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4228 | } |
| 4229 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4230 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4231 | } |
| 4232 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4233 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4234 | QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 4235 | nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 4236 | if(nCBORError) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4237 | return (int32_t)(1000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4238 | } |
| 4239 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4240 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4241 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4242 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4243 | } |
| 4244 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 4245 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4246 | // NaN's can't be compared for equality. A NaN is |
| 4247 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4248 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4249 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4250 | } |
| 4251 | } else { |
| 4252 | // TODO: this comparison may need a margin of error |
| 4253 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4254 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | } |
| 4259 | |
| 4260 | return 0; |
| 4261 | } |
| 4262 | |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4263 | int32_t IntegerConvertTestOld() |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4264 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4265 | QCBORDecodeContext DCtx; |
| 4266 | QCBORError nCBORError; |
| 4267 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4268 | /* exponent, mantissa |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4269 | [ |
| 4270 | 4([-1, 3]), |
| 4271 | 4([-20, 4759477275222530853136]), |
| 4272 | 4([9223372036854775807, -4759477275222530853137]), |
| 4273 | 5([300, 100]), |
| 4274 | 5([-20, 4759477275222530853136]), |
| 4275 | 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4276 | 5([ 9223372036854775806, -4759477275222530853137]) |
| 4277 | 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4278 | ] |
| 4279 | */ |
| 4280 | |
| 4281 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0); |
| 4282 | |
| 4283 | QCBORItem Item; |
| 4284 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4285 | if(nCBORError) { |
| 4286 | return -1; |
| 4287 | } |
| 4288 | |
| 4289 | int64_t integer; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4290 | // 4([-1, 3]), |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4291 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4292 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4293 | return -2; |
| 4294 | } |
| 4295 | DCtx.uLastError = 0; // TODO: a method for this |
| 4296 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4297 | // 4([-20, 4759477275222530853136]), |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4298 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4299 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4300 | return -2; |
| 4301 | } |
| 4302 | DCtx.uLastError = 0; // TODO: a method for this |
| 4303 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4304 | // 4([9223372036854775807, -4759477275222530853137]), |
| 4305 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4306 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4307 | return -2; |
| 4308 | } |
| 4309 | DCtx.uLastError = 0; // TODO: a method for this |
| 4310 | |
| 4311 | // 5([300, 100]), |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4312 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4313 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4314 | return -2; |
| 4315 | } |
| 4316 | DCtx.uLastError = 0; // TODO: a method for this |
| 4317 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4318 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4319 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4320 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4321 | return -2; |
| 4322 | } |
| 4323 | DCtx.uLastError = 0; // TODO: a method for this |
| 4324 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4325 | // 5([-9223372036854775807, -4759477275222530853137]) |
| 4326 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4327 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4328 | return -2; |
| 4329 | } |
| 4330 | DCtx.uLastError = 0; // TODO: a method for this |
| 4331 | |
| 4332 | // 5([ 9223372036854775806, -4759477275222530853137]) |
| 4333 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4334 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4335 | return -2; |
| 4336 | } |
| 4337 | DCtx.uLastError = 0; // TODO: a method for this |
| 4338 | |
| 4339 | // 5([ 9223372036854775806, 9223372036854775806])] |
| 4340 | QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4341 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4342 | return -2; |
| 4343 | } |
| 4344 | DCtx.uLastError = 0; // TODO: a method for this |
| 4345 | |
| 4346 | |
| 4347 | |
| 4348 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0); |
| 4349 | |
| 4350 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4351 | if(nCBORError) { |
| 4352 | return -1; |
| 4353 | } |
| 4354 | |
| 4355 | uint64_t uinteger; |
| 4356 | // 4([-1, 3]), |
| 4357 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4358 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4359 | return -2; |
| 4360 | } |
| 4361 | DCtx.uLastError = 0; // TODO: a method for this |
| 4362 | |
| 4363 | // 4([-20, 4759477275222530853136]), |
| 4364 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4365 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4366 | return -2; |
| 4367 | } |
| 4368 | DCtx.uLastError = 0; // TODO: a method for this |
| 4369 | |
| 4370 | // 4([9223372036854775807, -4759477275222530853137]), |
| 4371 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4372 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4373 | return -2; |
| 4374 | } |
| 4375 | DCtx.uLastError = 0; // TODO: a method for this |
| 4376 | |
| 4377 | // 5([300, 100]), |
| 4378 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4379 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4380 | return -2; |
| 4381 | } |
| 4382 | DCtx.uLastError = 0; // TODO: a method for this |
| 4383 | |
| 4384 | // 5([-20, 4759477275222530853136]), |
| 4385 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4386 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4387 | return -2; |
| 4388 | } |
| 4389 | DCtx.uLastError = 0; // TODO: a method for this |
| 4390 | |
| 4391 | // 5([-9223372036854775807, -4759477275222530853137]) |
| 4392 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4393 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4394 | return -2; |
| 4395 | } |
| 4396 | DCtx.uLastError = 0; // TODO: a method for this |
| 4397 | |
| 4398 | // 5([ 9223372036854775806, -4759477275222530853137]) |
| 4399 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4400 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4401 | return -2; |
| 4402 | } |
| 4403 | DCtx.uLastError = 0; // TODO: a method for this |
| 4404 | |
| 4405 | // 5([ 9223372036854775806, 9223372036854775806])] |
| 4406 | QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4407 | if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) { |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4408 | return -2; |
| 4409 | } |
| 4410 | DCtx.uLastError = 0; // TODO: a method for this |
| 4411 | |
| 4412 | |
| 4413 | |
| 4414 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0); |
| 4415 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4416 | if(nCBORError) { |
| 4417 | return -1; |
| 4418 | } |
| 4419 | |
| 4420 | double dResult; |
| 4421 | // 4([-1, 3]), |
| 4422 | QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4423 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS && |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4424 | dResult != 0.3) { |
| 4425 | return -2; |
| 4426 | } |
| 4427 | |
| 4428 | // 4([-20, 4759477275222530853136]), |
| 4429 | QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4430 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS && |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4431 | dResult != 47.408855671161923) { |
| 4432 | return -2; |
| 4433 | } |
| 4434 | |
| 4435 | // 4([9223372036854775807, -4759477275222530853137]), |
| 4436 | QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4437 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS && |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4438 | dResult != -INFINITY) { |
| 4439 | return -2; |
| 4440 | } |
| 4441 | |
| 4442 | // 5([300, 100]), |
| 4443 | QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4444 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS && |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4445 | dResult != -INFINITY) { |
| 4446 | return -2; |
| 4447 | } |
| 4448 | |
| 4449 | // 5([-20, 4759477275222530853136]), |
| 4450 | QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4451 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS && |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4452 | dResult != 4521260802379792.0) { |
| 4453 | return -2; |
| 4454 | } |
| 4455 | |
| 4456 | // 5([-9223372036854775807, -4759477275222530853137]) |
| 4457 | QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4458 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS && |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4459 | dResult != -0.0) { |
| 4460 | return -2; |
| 4461 | } |
| 4462 | |
| 4463 | // 5([9223372036854775806, 9223372036854775806])] |
| 4464 | QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4465 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS && |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4466 | dResult != INFINITY) { |
| 4467 | return -2; |
| 4468 | } |
| 4469 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4470 | return 0; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4471 | } |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 4472 | |
| 4473 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 4474 | int32_t CBORSequenceDecodeTests(void) |
| 4475 | { |
| 4476 | QCBORDecodeContext DCtx; |
| 4477 | QCBORItem Item; |
| 4478 | QCBORError uCBORError; |
| 4479 | |
| 4480 | // --- Test a sequence with extra bytes --- |
| 4481 | |
| 4482 | // The input for the date test happens to be a sequence so it |
| 4483 | // is reused. It is a sequence because it doesn't start as |
| 4484 | // an array or map. |
| 4485 | QCBORDecode_Init(&DCtx, |
| 4486 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 4487 | QCBOR_DECODE_MODE_NORMAL); |
| 4488 | |
| 4489 | // Get the first item |
| 4490 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4491 | if(uCBORError != QCBOR_SUCCESS) { |
| 4492 | return 1; |
| 4493 | } |
| 4494 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING) { |
| 4495 | return 2; |
| 4496 | } |
| 4497 | |
| 4498 | // Get a second item |
| 4499 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4500 | if(uCBORError != QCBOR_SUCCESS) { |
| 4501 | return 2; |
| 4502 | } |
| 4503 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 4504 | return 3; |
| 4505 | } |
| 4506 | |
| 4507 | // A sequence can have stuff at the end that may |
| 4508 | // or may not be valid CBOR. The protocol decoder knows |
| 4509 | // when to stop by definition of the protocol, not |
| 4510 | // when the top-level map or array is ended. |
| 4511 | // Finish still has to be called to know that |
| 4512 | // maps and arrays (if there were any) were closed |
| 4513 | // off correctly. When called like this it |
| 4514 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 4515 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 4516 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 4517 | return 4; |
| 4518 | } |
| 4519 | |
| 4520 | |
| 4521 | // --- Test an empty input ---- |
| 4522 | uint8_t empty[1]; |
| 4523 | UsefulBufC Empty = {empty, 0}; |
| 4524 | QCBORDecode_Init(&DCtx, |
| 4525 | Empty, |
| 4526 | QCBOR_DECODE_MODE_NORMAL); |
| 4527 | |
| 4528 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 4529 | if(uCBORError != QCBOR_SUCCESS) { |
| 4530 | return 5; |
| 4531 | } |
| 4532 | |
| 4533 | |
| 4534 | // --- Sequence with unclosed indefinite length array --- |
| 4535 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
| 4536 | |
| 4537 | QCBORDecode_Init(&DCtx, |
| 4538 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 4539 | QCBOR_DECODE_MODE_NORMAL); |
| 4540 | |
| 4541 | // Get the first item |
| 4542 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4543 | if(uCBORError != QCBOR_SUCCESS) { |
| 4544 | return 7; |
| 4545 | } |
| 4546 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 4547 | return 8; |
| 4548 | } |
| 4549 | |
| 4550 | // Get a second item |
| 4551 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4552 | if(uCBORError != QCBOR_SUCCESS) { |
| 4553 | return 9; |
| 4554 | } |
| 4555 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4556 | return 10; |
| 4557 | } |
| 4558 | |
| 4559 | // Try to finish before consuming all bytes to confirm |
| 4560 | // that the still-open error is returned. |
| 4561 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 4562 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 4563 | return 11; |
| 4564 | } |
| 4565 | |
| 4566 | |
| 4567 | // --- Sequence with a closed indefinite length array --- |
| 4568 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
| 4569 | |
| 4570 | QCBORDecode_Init(&DCtx, |
| 4571 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 4572 | QCBOR_DECODE_MODE_NORMAL); |
| 4573 | |
| 4574 | // Get the first item |
| 4575 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4576 | if(uCBORError != QCBOR_SUCCESS) { |
| 4577 | return 12; |
| 4578 | } |
| 4579 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 4580 | return 13; |
| 4581 | } |
| 4582 | |
| 4583 | // Get a second item |
| 4584 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 4585 | if(uCBORError != QCBOR_SUCCESS) { |
| 4586 | return 14; |
| 4587 | } |
| 4588 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4589 | return 15; |
| 4590 | } |
| 4591 | |
| 4592 | // Try to finish before consuming all bytes to confirm |
| 4593 | // that the still-open error is returned. |
| 4594 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 4595 | if(uCBORError != QCBOR_SUCCESS) { |
| 4596 | return 16; |
| 4597 | } |
| 4598 | |
| 4599 | |
| 4600 | return 0; |
| 4601 | } |
| 4602 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 4603 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame^] | 4604 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 4605 | int32_t IntToTests() |
| 4606 | { |
| 4607 | int nErrCode; |
| 4608 | int32_t n32; |
| 4609 | int16_t n16; |
| 4610 | int8_t n8; |
| 4611 | uint32_t u32; |
| 4612 | uint16_t u16; |
| 4613 | uint8_t u8; |
| 4614 | uint64_t u64; |
| 4615 | |
| 4616 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 4617 | if(nErrCode == -1 || n32 != 1) { |
| 4618 | return 1; |
| 4619 | } |
| 4620 | |
| 4621 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 4622 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 4623 | return 2; |
| 4624 | } |
| 4625 | |
| 4626 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 4627 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 4628 | return 3; |
| 4629 | } |
| 4630 | |
| 4631 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 4632 | if(nErrCode != -1) { |
| 4633 | return 4; |
| 4634 | } |
| 4635 | |
| 4636 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 4637 | if(nErrCode != -1) { |
| 4638 | return 5; |
| 4639 | } |
| 4640 | |
| 4641 | |
| 4642 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 4643 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 4644 | return 6; |
| 4645 | } |
| 4646 | |
| 4647 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 4648 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 4649 | return 7; |
| 4650 | } |
| 4651 | |
| 4652 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 4653 | if(nErrCode == -1 || n16 != 1) { |
| 4654 | return 8; |
| 4655 | } |
| 4656 | |
| 4657 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 4658 | if(nErrCode != -1) { |
| 4659 | return 9; |
| 4660 | } |
| 4661 | |
| 4662 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 4663 | if(nErrCode != -1) { |
| 4664 | return 10; |
| 4665 | } |
| 4666 | |
| 4667 | |
| 4668 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 4669 | if(nErrCode == -1 || n8 != 1) { |
| 4670 | return 11; |
| 4671 | } |
| 4672 | |
| 4673 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 4674 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 4675 | return 12; |
| 4676 | } |
| 4677 | |
| 4678 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 4679 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 4680 | return 13; |
| 4681 | } |
| 4682 | |
| 4683 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 4684 | if(nErrCode != -1) { |
| 4685 | return 14; |
| 4686 | } |
| 4687 | |
| 4688 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 4689 | if(nErrCode != -1) { |
| 4690 | return 15; |
| 4691 | } |
| 4692 | |
| 4693 | |
| 4694 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 4695 | if(nErrCode == -1 || u32 != 1) { |
| 4696 | return 16; |
| 4697 | } |
| 4698 | |
| 4699 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 4700 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 4701 | return 17; |
| 4702 | } |
| 4703 | |
| 4704 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 4705 | if(nErrCode == -1 || u32 != 0) { |
| 4706 | return 18; |
| 4707 | } |
| 4708 | |
| 4709 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 4710 | if(nErrCode != -1) { |
| 4711 | return 19; |
| 4712 | } |
| 4713 | |
| 4714 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 4715 | if(nErrCode != -1) { |
| 4716 | return 20; |
| 4717 | } |
| 4718 | |
| 4719 | |
| 4720 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 4721 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 4722 | return 21; |
| 4723 | } |
| 4724 | |
| 4725 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 4726 | if(nErrCode == -1 || u16 != 0) { |
| 4727 | return 22; |
| 4728 | } |
| 4729 | |
| 4730 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 4731 | if(nErrCode == -1 || u16 != 1) { |
| 4732 | return 23; |
| 4733 | } |
| 4734 | |
| 4735 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 4736 | if(nErrCode != -1) { |
| 4737 | return 24; |
| 4738 | } |
| 4739 | |
| 4740 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 4741 | if(nErrCode != -1) { |
| 4742 | return 25; |
| 4743 | } |
| 4744 | |
| 4745 | |
| 4746 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 4747 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 4748 | return 26; |
| 4749 | } |
| 4750 | |
| 4751 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 4752 | if(nErrCode == -1 || u8 != 0) { |
| 4753 | return 27; |
| 4754 | } |
| 4755 | |
| 4756 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 4757 | if(nErrCode == -1 || u8 != 1) { |
| 4758 | return 28; |
| 4759 | } |
| 4760 | |
| 4761 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 4762 | if(nErrCode != -1) { |
| 4763 | return 29; |
| 4764 | } |
| 4765 | |
| 4766 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 4767 | if(nErrCode != -1) { |
| 4768 | return 30; |
| 4769 | } |
| 4770 | |
| 4771 | |
| 4772 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 4773 | if(nErrCode == -1 || u64 != 1) { |
| 4774 | return 31; |
| 4775 | } |
| 4776 | |
| 4777 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 4778 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 4779 | return 32; |
| 4780 | } |
| 4781 | |
| 4782 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 4783 | if(nErrCode == -1 || u64 != 0) { |
| 4784 | return 33; |
| 4785 | } |
| 4786 | |
| 4787 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 4788 | if(nErrCode != -1) { |
| 4789 | return 34; |
| 4790 | } |
| 4791 | |
| 4792 | return 0; |
| 4793 | } |
| 4794 | |