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 | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 36 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | d4728fd | 2018-12-17 15:15:56 -0800 | [diff] [blame] | 37 | #include <string.h> |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 38 | #include <math.h> // for fabs() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 39 | #include "not_well_formed_cbor.h" |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 40 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 41 | // Handy macro to compare a UsefulBuf to a C string |
| 42 | #define UsefulBufCompareToSZ(x, y) \ |
| 43 | UsefulBuf_Compare(x, UsefulBuf_FromSZ(y)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 44 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 45 | #ifdef PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 46 | #include <stdio.h> |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 47 | |
| 48 | static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 49 | { |
| 50 | if(szLabel) { |
| 51 | printf("%s ", szLabel); |
| 52 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 53 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 54 | size_t i; |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 55 | for(i = 0; i < Buf.len; i++) { |
| 56 | uint8_t Z = ((uint8_t *)Buf.ptr)[i]; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 57 | printf("%02x ", Z); |
| 58 | } |
| 59 | printf("\n"); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 60 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 61 | fflush(stdout); |
| 62 | } |
Laurence Lundblade | 20db9c9 | 2018-12-17 11:40:37 -0800 | [diff] [blame] | 63 | #endif |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 64 | |
| 65 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 66 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 67 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 68 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 69 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 70 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 71 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 72 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 73 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 74 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 75 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 76 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 77 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 78 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 79 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 80 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 81 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 82 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 83 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 84 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 85 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 86 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 87 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 88 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 89 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 90 | 0xff, 0xff}; |
| 91 | |
| 92 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 93 | // return CBOR error or -1 if type of value doesn't match |
| 94 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 95 | static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 96 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 97 | QCBORItem Item; |
| 98 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 99 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 100 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 101 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 102 | if(Item.uDataType != QCBOR_TYPE_ARRAY) |
| 103 | return -1; |
| 104 | |
| 105 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 106 | return (int32_t)nCBORError; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 107 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 108 | Item.val.int64 != -9223372036854775807LL - 1) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 109 | return -1; |
| 110 | |
| 111 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 112 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 113 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 114 | Item.val.int64 != -4294967297) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 115 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 116 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 117 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 118 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 119 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 120 | Item.val.int64 != -4294967296) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 121 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 122 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 123 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 124 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 125 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 126 | Item.val.int64 != -4294967295) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 127 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 128 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 129 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 130 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 131 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 132 | Item.val.int64 != -4294967294) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 133 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 134 | |
| 135 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 136 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 137 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 138 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 139 | Item.val.int64 != -2147483648) |
| 140 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 141 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 142 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 143 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 144 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 145 | Item.val.int64 != -2147483647) |
| 146 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 147 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 148 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 149 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 150 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 151 | Item.val.int64 != -65538) |
| 152 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 153 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 154 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 155 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 156 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 157 | Item.val.int64 != -65537) |
| 158 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 159 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 160 | if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 161 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 162 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 163 | Item.val.int64 != -65536) |
| 164 | return -1; |
| 165 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 166 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 167 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 168 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 169 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 170 | Item.val.int64 != -65535) |
| 171 | return -1; |
| 172 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 173 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 174 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 175 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 176 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 177 | Item.val.int64 != -65534) |
| 178 | return -1; |
| 179 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 180 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 181 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 182 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 183 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 184 | Item.val.int64 != -257) |
| 185 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 186 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 187 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 188 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 189 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 190 | Item.val.int64 != -256) |
| 191 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 192 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 193 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 194 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 195 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 196 | Item.val.int64 != -255) |
| 197 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 198 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 199 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 200 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 202 | Item.val.int64 != -254) |
| 203 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 204 | |
| 205 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 206 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 207 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 208 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 209 | Item.val.int64 != -25) |
| 210 | return -1; |
| 211 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 212 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 213 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 214 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 215 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 216 | Item.val.int64 != -24) |
| 217 | return -1; |
| 218 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 219 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 220 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 221 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 222 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 223 | Item.val.int64 != -23) |
| 224 | return -1; |
| 225 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 226 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 227 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 228 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 229 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 230 | Item.val.int64 != -1) |
| 231 | return -1; |
| 232 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 233 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 234 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 235 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 236 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 237 | Item.val.int64 != 0) |
| 238 | return -1; |
| 239 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 240 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 241 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 242 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 243 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 244 | Item.val.int64 != 0) |
| 245 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 246 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 247 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 248 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 249 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 250 | Item.val.int64 != 1) |
| 251 | return -1; |
| 252 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 253 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 254 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 255 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 256 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 257 | Item.val.int64 != 22) |
| 258 | return -1; |
| 259 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 260 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 261 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 262 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 263 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 264 | Item.val.int64 != 23) |
| 265 | return -1; |
| 266 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 267 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 268 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 269 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 270 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 271 | Item.val.int64 != 24) |
| 272 | return -1; |
| 273 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 274 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 275 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 276 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 277 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 278 | Item.val.int64 != 25) |
| 279 | return -1; |
| 280 | |
| 281 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 282 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 283 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 284 | Item.val.int64 != 26) |
| 285 | return -1; |
| 286 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 287 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 288 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 289 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 290 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 291 | Item.val.int64 != 254) |
| 292 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 293 | |
| 294 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 295 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 296 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 297 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 298 | Item.val.int64 != 255) |
| 299 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 300 | |
| 301 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 302 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 303 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 304 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 305 | Item.val.int64 != 256) |
| 306 | return -1; |
| 307 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 308 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 309 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 310 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 311 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 312 | Item.val.int64 != 257) |
| 313 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 314 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 315 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 316 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 317 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 318 | Item.val.int64 != 65534) |
| 319 | return -1; |
| 320 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 321 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 322 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 323 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 324 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 325 | Item.val.int64 != 65535) |
| 326 | return -1; |
| 327 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 328 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 329 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 330 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 331 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 332 | Item.val.int64 != 65536) |
| 333 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 334 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 335 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 336 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 337 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 338 | Item.val.int64 != 65537) |
| 339 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 340 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 341 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 342 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 343 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 344 | Item.val.int64 != 65538) |
| 345 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 346 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 347 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 348 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 349 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 350 | Item.val.int64 != 2147483647) |
| 351 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 352 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 353 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 354 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 355 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 356 | Item.val.int64 != 2147483647) |
| 357 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 358 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 359 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 360 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 361 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 362 | Item.val.int64 != 2147483648) |
| 363 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 364 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 365 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 366 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 367 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 368 | Item.val.int64 != 2147483649) |
| 369 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 370 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 371 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 372 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 373 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 374 | Item.val.int64 != 4294967294) |
| 375 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 376 | |
| 377 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 378 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 379 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 380 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 381 | Item.val.int64 != 4294967295) |
| 382 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 383 | |
| 384 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 385 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 386 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 387 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 388 | Item.val.int64 != 4294967296) |
| 389 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 390 | |
| 391 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 392 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 393 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 394 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 395 | Item.val.int64 != 4294967297) |
| 396 | return -1; |
| 397 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 398 | |
| 399 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 400 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 401 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 402 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 403 | Item.val.int64 != 9223372036854775807LL) |
| 404 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 405 | |
| 406 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 407 | if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 408 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 409 | if(Item.uDataType != QCBOR_TYPE_UINT64 || |
| 410 | Item.val.uint64 != 18446744073709551615ULL) |
| 411 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 412 | |
| 413 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 414 | if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) { |
| 415 | return -1; |
| 416 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 417 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 422 | // One less than the smallest negative integer allowed in C. Decoding |
| 423 | // this should fail. |
| 424 | static const uint8_t spTooSmallNegative[] = { |
| 425 | 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 429 | /* |
| 430 | Tests the decoding of lots of different integers sizes |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 431 | and values. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 432 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 433 | int32_t IntegerValuesParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 434 | { |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 435 | int nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 436 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 437 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 438 | QCBORDecode_Init(&DCtx, |
| 439 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), |
| 440 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 441 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 442 | // The really big test of all successes |
| 443 | nReturn = IntegerValuesParseTestInternal(&DCtx); |
| 444 | if(nReturn) { |
| 445 | return nReturn; |
| 446 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 447 | |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 448 | // The one large negative integer that can be parsed |
| 449 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 450 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative), |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 451 | QCBOR_DECODE_MODE_NORMAL); |
| 452 | |
| 453 | QCBORItem item; |
| 454 | if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) { |
| 455 | nReturn = -4000; |
| 456 | } |
| 457 | |
| 458 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
| 462 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 463 | Creates a simple CBOR array and returns it in *pEncoded. The array is |
| 464 | malloced and needs to be freed. This is used by several tests. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 465 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 466 | Two of the inputs can be set. Two other items in the array are fixed. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 467 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 468 | */ |
| 469 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 470 | static uint8_t spSimpleArrayBuffer[50]; |
| 471 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 472 | 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] | 473 | { |
| 474 | QCBOREncodeContext ECtx; |
| 475 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 476 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 477 | *pEncoded = NULL; |
| 478 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 479 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 480 | // loop runs CBOR encoding twice. First with no buffer to |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 481 | // calculate the length so buffer can be allocated correctly, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 482 | // and last with the buffer to do the actual encoding |
| 483 | do { |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 484 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 485 | QCBOREncode_OpenArray(&ECtx); |
| 486 | QCBOREncode_AddInt64(&ECtx, nInt1); |
| 487 | QCBOREncode_AddInt64(&ECtx, nInt2); |
| 488 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8})); |
| 489 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11})); |
| 490 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 491 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 492 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 493 | goto Done; |
| 494 | |
| 495 | if(*pEncoded != NULL) { |
| 496 | nReturn = 0; |
| 497 | goto Done; |
| 498 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 499 | |
| 500 | // Use static buffer to avoid dependency on malloc() |
| 501 | if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 502 | goto Done; |
| 503 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 504 | *pEncoded = spSimpleArrayBuffer; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 505 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 506 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 507 | |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 508 | Done: |
| 509 | return nReturn; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 513 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 514 | Some basic CBOR with map and array used in a lot of tests. |
| 515 | The map labels are all strings |
| 516 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 517 | {"first integer": 42, |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 518 | "an array of two strings": [ |
| 519 | "string1", "string2" |
| 520 | ], |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 521 | "map in a map": { |
| 522 | "bytes 1": h'78787878', |
| 523 | "bytes 2": h'79797979', |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 524 | "another int": 98, |
| 525 | "text 2": "lies, damn lies and statistics" |
| 526 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 527 | } |
| 528 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 529 | static const uint8_t pValidMapEncoded[] = { |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 530 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 531 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 532 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 533 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 534 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 535 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 536 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 537 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 538 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 539 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 540 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 541 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 542 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 543 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 544 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 545 | 0x73 }; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 546 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 547 | // Same as above, but with indefinite lengths. |
| 548 | static const uint8_t pValidMapIndefEncoded[] = { |
| 549 | 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 550 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 551 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 552 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 553 | 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 554 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d, |
| 555 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 556 | 0x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 557 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 558 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 559 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 560 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 561 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 562 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 563 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 564 | 0x73, 0xff, 0xff}; |
| 565 | |
| 566 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 567 | static int32_t ParseOrderedArray(const uint8_t *pEncoded, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 568 | size_t nLen, |
| 569 | int64_t *pInt1, |
| 570 | int64_t *pInt2, |
| 571 | const uint8_t **pBuf3, |
| 572 | size_t *pBuf3Len, |
| 573 | const uint8_t **pBuf4, |
| 574 | size_t *pBuf4Len) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 575 | { |
| 576 | QCBORDecodeContext DCtx; |
| 577 | QCBORItem Item; |
| 578 | int nReturn = -1; // assume error until success |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 579 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 580 | QCBORDecode_Init(&DCtx, |
| 581 | (UsefulBufC){pEncoded, nLen}, |
| 582 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 583 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 584 | // Make sure the first thing is a map |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 585 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 586 | Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 587 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 588 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 589 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 590 | // First integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 591 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 592 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 593 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 594 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 595 | *pInt1 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 596 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 597 | // Second integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 598 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 599 | Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 600 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 601 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 602 | *pInt2 = Item.val.int64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 603 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 604 | // First string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 605 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 606 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 607 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 608 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 609 | *pBuf3 = Item.val.string.ptr; |
| 610 | *pBuf3Len = Item.val.string.len; |
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 | // Second string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 613 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 614 | Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 615 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 616 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 617 | *pBuf4 = Item.val.string.ptr; |
| 618 | *pBuf4Len = Item.val.string.len; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 619 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 620 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 621 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 622 | Done: |
| 623 | return(nReturn); |
| 624 | } |
| 625 | |
| 626 | |
| 627 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 628 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 629 | int32_t SimpleArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 630 | { |
| 631 | uint8_t *pEncoded; |
| 632 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 633 | |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 634 | int64_t i1=0, i2=0; |
| 635 | size_t i3=0, i4=0; |
| 636 | const uint8_t *s3= (uint8_t *)""; |
| 637 | const uint8_t *s4= (uint8_t *)""; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 638 | |
| 639 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 640 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 641 | return(-1); |
| 642 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 643 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 644 | ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 645 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 646 | if(i1 != 23 || |
| 647 | i2 != 6000 || |
| 648 | i3 != 8 || |
| 649 | i4 != 11 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 650 | memcmp("galactic", s3, 8) !=0 || |
| 651 | memcmp("haven token", s4, 11) !=0) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 652 | return(-1); |
| 653 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 654 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 655 | return(0); |
| 656 | } |
| 657 | |
| 658 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 659 | /* |
| 660 | [ |
| 661 | 0, |
| 662 | [], |
| 663 | [ |
| 664 | [], |
| 665 | [ |
| 666 | 0 |
| 667 | ], |
| 668 | {}, |
| 669 | { |
| 670 | 1: {}, |
| 671 | 2: {}, |
| 672 | 3: [] |
| 673 | } |
| 674 | ] |
| 675 | ] |
| 676 | */ |
| 677 | static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0, |
| 678 | 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80}; |
| 679 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 680 | /* Same as above, but with indefinte lengths */ |
| 681 | static uint8_t sEmptiesIndef[] = { |
| 682 | 0x9F, |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 683 | 0x00, |
| 684 | 0x9F, |
| 685 | 0xFF, |
| 686 | 0x9F, |
| 687 | 0x9F, |
| 688 | 0xFF, |
| 689 | 0x9F, |
| 690 | 0x00, |
| 691 | 0xFF, |
| 692 | 0xBF, |
| 693 | 0xFF, |
| 694 | 0xBF, |
| 695 | 0x01, |
| 696 | 0xBF, |
| 697 | 0xFF, |
| 698 | 0x02, |
| 699 | 0xBF, |
| 700 | 0xFF, |
| 701 | 0x03, |
| 702 | 0x9F, |
| 703 | 0xFF, |
| 704 | 0xFF, |
| 705 | 0xFF, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 706 | 0xFF}; |
| 707 | |
| 708 | |
| 709 | |
| 710 | static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts) |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 711 | { |
| 712 | QCBORDecodeContext DCtx; |
| 713 | QCBORItem Item; |
| 714 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 715 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 716 | input, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 717 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 718 | |
| 719 | // Array with 3 items |
| 720 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 721 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 722 | Item.uNestingLevel != 0 || |
| 723 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 724 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | // An integer 0 |
| 729 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 730 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 731 | Item.uNestingLevel != 1 || |
| 732 | Item.uNextNestLevel != 1 || |
| 733 | Item.val.uint64 != 0) { |
| 734 | return -2; |
| 735 | } |
| 736 | |
| 737 | // An empty array |
| 738 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 739 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 740 | Item.uNestingLevel != 1 || |
| 741 | Item.uNextNestLevel != 1 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 742 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 743 | return -3; |
| 744 | } |
| 745 | |
| 746 | // An array with 4 items |
| 747 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 748 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 749 | Item.uNestingLevel != 1 || |
| 750 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 751 | (bCheckCounts && Item.val.uCount != 4)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 752 | return -4; |
| 753 | } |
| 754 | |
| 755 | // An empty array |
| 756 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 757 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 758 | Item.uNestingLevel != 2 || |
| 759 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 760 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 761 | return -5; |
| 762 | } |
| 763 | |
| 764 | // An array with 1 item |
| 765 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 766 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 767 | Item.uNestingLevel != 2 || |
| 768 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 769 | (bCheckCounts && Item.val.uCount != 1)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 770 | return -6; |
| 771 | } |
| 772 | |
| 773 | // An integer 0 |
| 774 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 775 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 776 | Item.uNestingLevel != 3 || |
| 777 | Item.uNextNestLevel != 2 || |
| 778 | Item.val.uint64 != 0) { |
| 779 | return -7; |
| 780 | } |
| 781 | |
| 782 | // An empty map |
| 783 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 784 | Item.uDataType != QCBOR_TYPE_MAP || |
| 785 | Item.uNestingLevel != 2 || |
| 786 | Item.uNextNestLevel != 2 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 787 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 788 | return -8; |
| 789 | } |
| 790 | |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 791 | // A map with 3 items |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 792 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 793 | Item.uDataType != QCBOR_TYPE_MAP || |
| 794 | Item.uNestingLevel != 2 || |
| 795 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 796 | (bCheckCounts && Item.val.uCount != 3)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 797 | return -9; |
| 798 | } |
| 799 | |
| 800 | // An empty map |
| 801 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 802 | Item.uDataType != QCBOR_TYPE_MAP || |
| 803 | Item.uNestingLevel != 3 || |
| 804 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 805 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 806 | return -10; |
| 807 | } |
| 808 | |
| 809 | // An empty map |
| 810 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 811 | Item.uDataType != QCBOR_TYPE_MAP || |
| 812 | Item.uNestingLevel != 3 || |
| 813 | Item.uNextNestLevel != 3 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 814 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 815 | return -11; |
| 816 | } |
| 817 | |
| 818 | // An empty array |
| 819 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 820 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 821 | Item.uNestingLevel != 3 || |
| 822 | Item.uNextNestLevel != 0 || |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 823 | (bCheckCounts && Item.val.uCount != 0)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 824 | return -12; |
| 825 | } |
| 826 | |
| 827 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 828 | return -13; |
| 829 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | int32_t EmptyMapsAndArraysTest() |
| 835 | { |
| 836 | int nResult; |
| 837 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), |
| 838 | true); |
| 839 | if(nResult) { |
| 840 | return nResult; |
| 841 | } |
| 842 | |
| 843 | nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef), |
| 844 | false); |
| 845 | |
| 846 | if(nResult) { |
| 847 | return nResult -100; |
| 848 | } |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 853 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 854 | static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 855 | 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 856 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 857 | int32_t ParseDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 858 | { |
| 859 | QCBORDecodeContext DCtx; |
| 860 | int nReturn = 0; |
| 861 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 862 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 863 | QCBORDecode_Init(&DCtx, |
| 864 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), |
| 865 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 866 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 867 | for(i = 0; i < 10; i++) { |
| 868 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 869 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 870 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 871 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 872 | Item.uNestingLevel != i) { |
| 873 | nReturn = -1; |
| 874 | break; |
| 875 | } |
| 876 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 877 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 878 | return(nReturn); |
| 879 | } |
| 880 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 881 | // Big enough to test nesting to the depth of 24 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 882 | static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 883 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 884 | 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, |
| 885 | 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 886 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 887 | int32_t ParseTooDeepArrayTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 888 | { |
| 889 | QCBORDecodeContext DCtx; |
| 890 | int nReturn = 0; |
| 891 | int i; |
| 892 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 893 | |
| 894 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 895 | QCBORDecode_Init(&DCtx, |
| 896 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), |
| 897 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 898 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 899 | for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 900 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 901 | if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || |
| 902 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 903 | Item.uNestingLevel != i) { |
| 904 | nReturn = -1; |
| 905 | break; |
| 906 | } |
| 907 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 908 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 909 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 910 | nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 911 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 912 | return(nReturn); |
| 913 | } |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 918 | int32_t ShortBufferParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 919 | { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 920 | int nResult = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 921 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 922 | for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 923 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 924 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 925 | QCBORDecode_Init(&DCtx, |
| 926 | (UsefulBufC){spExpectedEncodedInts, nNum}, |
| 927 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 928 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 929 | const int nErr = IntegerValuesParseTestInternal(&DCtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 930 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 931 | if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 932 | nResult = -1; |
| 933 | goto Done; |
| 934 | } |
| 935 | } |
| 936 | Done: |
| 937 | return nResult; |
| 938 | } |
| 939 | |
| 940 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 941 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 942 | int32_t ShortBufferParseTest2() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 943 | { |
| 944 | uint8_t *pEncoded; |
| 945 | int nReturn; |
| 946 | size_t nEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 947 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 948 | int64_t i1, i2; |
| 949 | size_t i3, i4; |
| 950 | const uint8_t *s3, *s4; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 951 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 952 | nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 953 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 954 | if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) { |
| 955 | return(-1); |
| 956 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 957 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 958 | for(nEncodedLen--; nEncodedLen; nEncodedLen--) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 959 | int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, |
| 960 | &i2, &s3, &i3, &s4, &i4); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 961 | if(nResult == 0) { |
| 962 | nReturn = -1; |
| 963 | } |
| 964 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 965 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 966 | return(nReturn); |
| 967 | } |
| 968 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 969 | /* |
| 970 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 971 | set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in |
| 972 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY. |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 973 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 974 | static int32_t ParseMapTest1(QCBORDecodeMode nMode) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 975 | { |
| 976 | QCBORDecodeContext DCtx; |
| 977 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 978 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 979 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 980 | QCBORDecode_Init(&DCtx, |
| 981 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 982 | nMode); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 983 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 984 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 985 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 986 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 987 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 988 | Item.val.uCount != 3) |
| 989 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 990 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 991 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 992 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 993 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 994 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 995 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 996 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 997 | Item.val.int64 != 42 || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 998 | Item.uDataAlloc || |
| 999 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1000 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1001 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1002 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1003 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1004 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1005 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1006 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1007 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1008 | Item.uDataAlloc || |
| 1009 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1010 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1011 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1012 | Item.val.uCount != 2) |
| 1013 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1014 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1015 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1016 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1017 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1018 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1019 | Item.uDataAlloc || |
| 1020 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1021 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1022 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1023 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1024 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1025 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1026 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1027 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1028 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1029 | Item.uDataAlloc || |
| 1030 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1031 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1032 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 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 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1038 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1039 | Item.uDataAlloc || |
| 1040 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1041 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1042 | Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1043 | Item.val.uCount != 4) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1044 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1045 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1046 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1047 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1048 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1049 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1050 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1051 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1052 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1053 | Item.uDataAlloc || |
| 1054 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1055 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1056 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1057 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1058 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1059 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1060 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1061 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1062 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1063 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1064 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1065 | Item.uDataAlloc || |
| 1066 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1067 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1068 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1069 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1070 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1071 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1072 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1073 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1074 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1075 | Item.uDataAlloc || |
| 1076 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1077 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1078 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1079 | Item.val.int64 != 98) |
| 1080 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1081 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1082 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1083 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1084 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1085 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1086 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1087 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1088 | Item.uDataAlloc || |
| 1089 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1090 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1091 | return -1; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1092 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1093 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1098 | /* |
| 1099 | Decode and thoroughly check a moderately complex |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1100 | set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1101 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1102 | int32_t ParseMapAsArrayTest() |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1103 | { |
| 1104 | QCBORDecodeContext DCtx; |
| 1105 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1106 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1107 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1108 | QCBORDecode_Init(&DCtx, |
| 1109 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 1110 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1111 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1112 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1113 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1114 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1115 | if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1116 | Item.val.uCount != 6) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1117 | return -1; |
| 1118 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1119 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1120 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1121 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1122 | } |
| 1123 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1124 | Item.uDataAlloc || |
| 1125 | Item.uLabelAlloc || |
| 1126 | Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1127 | UsefulBufCompareToSZ(Item.val.string, "first integer")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1128 | return -2; |
| 1129 | } |
| 1130 | |
| 1131 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1132 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1133 | } |
| 1134 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1135 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1136 | Item.val.int64 != 42 || |
| 1137 | Item.uDataAlloc || |
| 1138 | Item.uLabelAlloc) { |
| 1139 | return -3; |
| 1140 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1141 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1142 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1143 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1144 | } |
| 1145 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1146 | Item.uDataAlloc || |
| 1147 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1148 | UsefulBufCompareToSZ(Item.val.string, "an array of two strings") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1149 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1150 | return -4; |
| 1151 | } |
| 1152 | |
| 1153 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1154 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1155 | } |
| 1156 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1157 | Item.uDataAlloc || |
| 1158 | Item.uLabelAlloc || |
| 1159 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1160 | Item.val.uCount != 2) { |
| 1161 | return -5; |
| 1162 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1163 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1164 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1165 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1166 | } |
| 1167 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1168 | Item.val.string.len != 7 || |
| 1169 | Item.uDataAlloc || |
| 1170 | Item.uLabelAlloc || |
| 1171 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) { |
| 1172 | return -6; |
| 1173 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1174 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1175 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1176 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1177 | } |
| 1178 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1179 | Item.uDataAlloc || |
| 1180 | Item.uLabelAlloc || |
| 1181 | UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) { |
| 1182 | return -7; |
| 1183 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1184 | |
| 1185 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1186 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1187 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1188 | } |
| 1189 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1190 | Item.uDataAlloc || |
| 1191 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1192 | UsefulBufCompareToSZ(Item.val.string, "map in a map")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1193 | return -8; |
| 1194 | } |
| 1195 | |
| 1196 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1197 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1198 | } |
| 1199 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1200 | Item.uDataAlloc || |
| 1201 | Item.uLabelAlloc || |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1202 | Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY || |
| 1203 | Item.val.uCount != 8) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1204 | return -9; |
| 1205 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1206 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1207 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1208 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1209 | } |
| 1210 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1211 | UsefulBufCompareToSZ(Item.val.string, "bytes 1") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1212 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1213 | Item.uDataAlloc || |
| 1214 | Item.uLabelAlloc) { |
| 1215 | return -10; |
| 1216 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1217 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1218 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1219 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1220 | } |
| 1221 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1222 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1223 | Item.uDataAlloc || |
| 1224 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1225 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1226 | return -11; |
| 1227 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1228 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1229 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1230 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1231 | } |
| 1232 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1233 | UsefulBufCompareToSZ(Item.val.string, "bytes 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1234 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1235 | Item.uDataAlloc || |
| 1236 | Item.uLabelAlloc) { |
| 1237 | return -12; |
| 1238 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1239 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1240 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1241 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1242 | } |
| 1243 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1244 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 1245 | Item.uDataAlloc || |
| 1246 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1247 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1248 | return -13; |
| 1249 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1250 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1251 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1252 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1253 | } |
| 1254 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1255 | Item.uDataAlloc || |
| 1256 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1257 | UsefulBufCompareToSZ(Item.val.string, "another int") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1258 | Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1259 | return -14; |
| 1260 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1261 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1262 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1263 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1264 | } |
| 1265 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1266 | Item.uDataAlloc || |
| 1267 | Item.uLabelAlloc || |
| 1268 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1269 | Item.val.int64 != 98) { |
| 1270 | return -15; |
| 1271 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1272 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1273 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1274 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1275 | } |
| 1276 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1277 | UsefulBufCompareToSZ(Item.val.string, "text 2") || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1278 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1279 | Item.uDataAlloc || |
| 1280 | Item.uLabelAlloc) { |
| 1281 | return -16; |
| 1282 | } |
| 1283 | |
| 1284 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1285 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1286 | } |
| 1287 | if(Item.uLabelType != QCBOR_TYPE_NONE || |
| 1288 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 1289 | Item.uDataAlloc || |
| 1290 | Item.uLabelAlloc || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1291 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1292 | return -17; |
| 1293 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1294 | |
| 1295 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1296 | /* |
| 1297 | Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a |
| 1298 | map that when interpreted as an array will be too many. Test |
| 1299 | data just has the start of the map, not all the items in the map. |
| 1300 | */ |
| 1301 | static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1302 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1303 | QCBORDecode_Init(&DCtx, |
| 1304 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap), |
| 1305 | QCBOR_DECODE_MODE_MAP_AS_ARRAY); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1306 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1307 | if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1308 | return -50; |
| 1309 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1310 | |
Laurence Lundblade | d4cc103 | 2020-10-12 04:19:47 -0700 | [diff] [blame] | 1311 | // TODO: test decoding of labels that are arrays or such |
| 1312 | // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY |
| 1313 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1317 | |
| 1318 | /* |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1319 | Fully or partially decode pValidMapEncoded. When |
| 1320 | partially decoding check for the right error code. |
| 1321 | How much partial decoding depends on nLevel. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1322 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1323 | The partial decodes test error conditions of |
| 1324 | incomplete encoded input. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1325 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1326 | This could be combined with the above test |
| 1327 | and made prettier and maybe a little more |
| 1328 | thorough. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1329 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1330 | static int32_t ExtraBytesTest(int nLevel) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1331 | { |
| 1332 | QCBORDecodeContext DCtx; |
| 1333 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1334 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1335 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1336 | QCBORDecode_Init(&DCtx, |
| 1337 | (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, |
| 1338 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1339 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1340 | if(nLevel < 1) { |
| 1341 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) { |
| 1342 | return -1; |
| 1343 | } else { |
| 1344 | return 0; |
| 1345 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1346 | } |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1347 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1348 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1349 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1350 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1351 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1352 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 1353 | Item.val.uCount != 3) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1354 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1355 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1356 | if(nLevel < 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1357 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1358 | return -3; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 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 | |
| 1364 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1365 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1366 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1367 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1368 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1369 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1370 | Item.val.uCount != 42 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1371 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1372 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1373 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1374 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1375 | if(nLevel < 3) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1376 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1377 | return -5; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1378 | } else { |
| 1379 | return 0; |
| 1380 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1381 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1382 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1383 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1384 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1385 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1386 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1387 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1388 | Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1389 | Item.val.uCount != 2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1390 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1391 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1392 | |
| 1393 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1394 | if(nLevel < 4) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1395 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1396 | return -7; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1397 | } else { |
| 1398 | return 0; |
| 1399 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1400 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1401 | |
| 1402 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1403 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1404 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1405 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1406 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1407 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1408 | return -8; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1409 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1410 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1411 | if(nLevel < 5) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1412 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1413 | return -9; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1414 | } else { |
| 1415 | return 0; |
| 1416 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1417 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1418 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1419 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1420 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1421 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1422 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1423 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1424 | return -10; |
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 | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1427 | if(nLevel < 6) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1428 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1429 | return -11; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1430 | } else { |
| 1431 | return 0; |
| 1432 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1433 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1434 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1435 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1436 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1437 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1438 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1439 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1440 | Item.uDataType != QCBOR_TYPE_MAP || |
| 1441 | Item.val.uCount != 4) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1442 | return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1443 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1444 | if(nLevel < 7) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1445 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1446 | return -13; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1447 | } else { |
| 1448 | return 0; |
| 1449 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1450 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1451 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1452 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1453 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1454 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1455 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1456 | UsefulBufCompareToSZ(Item.label.string, "bytes 1") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1457 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1458 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1459 | return -14; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1460 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1461 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1462 | if(nLevel < 8) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1463 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1464 | return -15; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1465 | } else { |
| 1466 | return 0; |
| 1467 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1468 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1469 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1470 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1471 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1472 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1473 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1474 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1475 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1476 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1477 | return -16; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1478 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1479 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1480 | if(nLevel < 9) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1481 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1482 | return -17; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1483 | } else { |
| 1484 | return 0; |
| 1485 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1486 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1487 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [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 | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1490 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1491 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1492 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1493 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 1494 | Item.val.int64 != 98) |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1495 | return -18; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1496 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1497 | if(nLevel < 10) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1498 | if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
| 1499 | return -19; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1500 | } else { |
| 1501 | return 0; |
| 1502 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1503 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1504 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1505 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1506 | return (int32_t)nCBORError; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1507 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1508 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1509 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1510 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1511 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1512 | return -20; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1513 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1514 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 1515 | if(QCBORDecode_Finish(&DCtx)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1516 | return -21; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1517 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1518 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | |
| 1522 | |
| 1523 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1524 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1525 | int32_t ParseMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1526 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1527 | // Parse a moderatly complex map structure very thoroughly |
| 1528 | int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL); |
| 1529 | if(nResult) { |
| 1530 | return nResult; |
| 1531 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1532 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1533 | // Again, but in strings-only mode. It should succeed since the input |
| 1534 | // map has only string labels. |
| 1535 | nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
| 1536 | if(nResult) { |
| 1537 | return nResult; |
| 1538 | } |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 1539 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1540 | // Again, but try to finish the decoding before the end of the |
| 1541 | // input at 10 different place and see that the right error code |
| 1542 | // is returned. |
| 1543 | for(int i = 0; i < 10; i++) { |
| 1544 | nResult = ExtraBytesTest(i); |
| 1545 | if(nResult) { |
| 1546 | break; |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1547 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1548 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1549 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1550 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1554 | static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, |
| 1555 | 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, |
| 1556 | 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1557 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1558 | int32_t ParseSimpleTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1559 | { |
| 1560 | QCBORDecodeContext DCtx; |
| 1561 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1562 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1563 | |
| 1564 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1565 | QCBORDecode_Init(&DCtx, |
| 1566 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 1567 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1568 | |
| 1569 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1570 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1571 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1572 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 1573 | Item.val.uCount != 10) |
| 1574 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1575 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1576 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1577 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1578 | if(Item.uDataType != QCBOR_TYPE_FALSE) |
| 1579 | return -1; |
| 1580 | |
| 1581 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1582 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1583 | if(Item.uDataType != QCBOR_TYPE_TRUE) |
| 1584 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1585 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1586 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1587 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1588 | if(Item.uDataType != QCBOR_TYPE_NULL) |
| 1589 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1590 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1591 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1592 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1593 | if(Item.uDataType != QCBOR_TYPE_UNDEF) |
| 1594 | return -1; |
| 1595 | |
| 1596 | // A break |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1597 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1598 | return -1; |
| 1599 | |
| 1600 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1601 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1602 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0) |
| 1603 | return -1; |
| 1604 | |
| 1605 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1606 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1607 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19) |
| 1608 | return -1; |
| 1609 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1610 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1611 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1612 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1613 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1614 | return -1; |
| 1615 | |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 1616 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1617 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1618 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1619 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1620 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1621 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32) |
| 1622 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1623 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1624 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1625 | return (int32_t)nCBORError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1626 | if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255) |
| 1627 | return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1628 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1629 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1630 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1634 | int32_t NotWellFormedTests() |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1635 | { |
| 1636 | // Loop over all the not-well-formed instance of CBOR |
| 1637 | // that are test vectors in not_well_formed_cbor.h |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1638 | const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR, |
| 1639 | struct someBinaryBytes); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1640 | for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) { |
| 1641 | const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate]; |
| 1642 | const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n}; |
| 1643 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1644 | // Set up decoder context. String allocator needed for indefinite |
| 1645 | // string test cases |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1646 | QCBORDecodeContext DCtx; |
| 1647 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1648 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1649 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
| 1650 | QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1651 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1652 | |
| 1653 | // Loop getting items until no more to get |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1654 | QCBORError uCBORError; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1655 | do { |
| 1656 | QCBORItem Item; |
| 1657 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1658 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1659 | } while(uCBORError == QCBOR_SUCCESS); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1660 | |
| 1661 | // Every test vector must fail with |
| 1662 | // a not-well-formed error. If not |
| 1663 | // this test fails. |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1664 | if(!QCBORDecode_IsNotWellFormedError(uCBORError) && |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 1665 | uCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1666 | /* Return index of failure and QCBOR error in the result */ |
| 1667 | return (int32_t)(nIterate * 100 + uCBORError); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1674 | // TODO: add a test index and report it so it is eaier to figure out which test failed. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1675 | struct FailInput { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1676 | UsefulBufC Input; |
| 1677 | QCBORError nError; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1678 | }; |
| 1679 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1680 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1681 | static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1682 | { |
| 1683 | for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1684 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1685 | QCBORError uCBORError; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1686 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1687 | QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1688 | |
| 1689 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1690 | // Set up the decoding context including a memory pool so that |
| 1691 | // indefinite length items can be checked |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1692 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1693 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1694 | uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0); |
| 1695 | if(uCBORError) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1696 | return -9; |
| 1697 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1698 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1699 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1700 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1701 | // Iterate until there is an error of some sort error |
| 1702 | QCBORItem Item; |
| 1703 | do { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1704 | // Set to something none-zero, something other than QCBOR_TYPE_NONE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1705 | memset(&Item, 0x33, sizeof(Item)); |
| 1706 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1707 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 1708 | } while(uCBORError == QCBOR_SUCCESS); |
| 1709 | |
| 1710 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1711 | |
| 1712 | // Must get the expected error or the this test fails |
| 1713 | // The data and label type must also be QCBOR_TYPE_NONE |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1714 | if(uCBORError != pF->nError || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1715 | Item.uDataType != QCBOR_TYPE_NONE || |
| 1716 | Item.uLabelType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1717 | // return index of CBOR + 100 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1718 | const size_t nIndex = (size_t)(pF - pFailInputs); |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1719 | return (int32_t)(nIndex * 100 + uCBORError); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | return 0; |
| 1724 | } |
| 1725 | |
| 1726 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1727 | struct FailInput Failures[] = { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1728 | // Most of this is copied from not_well_formed.h. Here the error code |
| 1729 | // returned is also checked. |
| 1730 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1731 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1732 | // Indefinite length strings must be closed off |
| 1733 | // An indefinite length byte string not closed off |
| 1734 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1735 | // An indefinite length text string not closed off |
| 1736 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1737 | |
| 1738 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1739 | // All the chunks in an indefinite length string must be of the type of |
| 1740 | // indefinite length string |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1741 | // indefinite length byte string with text string chunk |
| 1742 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1743 | // indefinite length text string with a byte string chunk |
| 1744 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1745 | // indefinite length byte string with an positive integer chunk |
| 1746 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1747 | // indefinite length byte string with an negative integer chunk |
| 1748 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1749 | // indefinite length byte string with an array chunk |
| 1750 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1751 | // indefinite length byte string with an map chunk |
| 1752 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1753 | // indefinite length byte string with tagged integer chunk |
| 1754 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1755 | // indefinite length byte string with an simple type chunk |
| 1756 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK }, |
| 1757 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1758 | // indefinite length text string with indefinite string inside |
| 1759 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK}, |
| 1760 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1761 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1762 | |
| 1763 | { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1764 | // An indefinite length text string not closed off |
| 1765 | { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1766 | |
| 1767 | |
| 1768 | // All the chunks in an indefinite length string must be of the type of |
| 1769 | // indefinite length string |
| 1770 | // indefinite length byte string with text string chunk |
| 1771 | { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1772 | // indefinite length text string with a byte string chunk |
| 1773 | { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1774 | // indefinite length byte string with an positive integer chunk |
| 1775 | { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1776 | // indefinite length byte string with an negative integer chunk |
| 1777 | { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1778 | // indefinite length byte string with an array chunk |
| 1779 | { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1780 | // indefinite length byte string with an map chunk |
| 1781 | { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1782 | // indefinite length byte string with tagged integer chunk |
| 1783 | { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1784 | // indefinite length byte string with an simple type chunk |
| 1785 | { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED }, |
| 1786 | { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1787 | // indefinite length text string with indefinite string inside |
| 1788 | { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED}, |
| 1789 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1790 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1791 | |
| 1792 | // Definte length maps and arrays must be closed by having the right number of items |
| 1793 | // A definte length array that is supposed to have 1 item, but has none |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1794 | { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1795 | // A definte length array that is supposed to have 2 items, but has only 1 |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1796 | { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1797 | // A definte length array that is supposed to have 511 items, but has only 1 |
| 1798 | { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1799 | // A definte length map that is supposed to have 1 item, but has none |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1800 | { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1801 | // A definte length map that is supposed to have s item, but has only 1 |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1802 | { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1803 | |
| 1804 | |
| 1805 | // Indefinte length maps and arrays must be ended by a break |
| 1806 | // Indefinite length array with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1807 | { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1808 | // Indefinite length array with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1809 | { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1810 | // Indefinite length map with zero items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1811 | { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1812 | // Indefinite length map with two items and no break |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1813 | { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1814 | |
| 1815 | |
| 1816 | // Nested maps and arrays must be closed off (some extra nested test vectors) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1817 | // Unclosed indefinite array containing a closed definite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1818 | { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1819 | // Definite length array containing an unclosed indefinite length array |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1820 | { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1821 | // Unclosed indefinite map containing a closed definite length array |
| 1822 | { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS }, |
| 1823 | // Definite length map containing an unclosed indefinite length array |
| 1824 | { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1825 | // Deeply nested definite length arrays with deepest one unclosed |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1826 | { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1827 | // Deeply nested indefinite length arrays with deepest one unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1828 | { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1829 | // Mixed nesting with indefinite unclosed |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1830 | { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1831 | // Mixed nesting with definite unclosed |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1832 | { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1833 | // Unclosed indefinite length map in definite length maps |
| 1834 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8}, |
| 1835 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1836 | // Unclosed definite length map in indefinite length maps |
| 1837 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1838 | // Unclosed indefinite length array in definite length maps |
| 1839 | { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8}, |
| 1840 | QCBOR_ERR_NO_MORE_ITEMS}, |
| 1841 | // Unclosed definite length array in indefinite length maps |
| 1842 | { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1843 | // Unclosed indefinite length map in definite length arrays |
| 1844 | { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 1845 | // Unclosed definite length map in indefinite length arrays |
| 1846 | { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1847 | |
| 1848 | |
| 1849 | // The "argument" for the data item is incomplete |
| 1850 | // Positive integer missing 1 byte argument |
| 1851 | { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, |
| 1852 | // Positive integer missing 2 byte argument |
| 1853 | { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END }, |
| 1854 | // Positive integer missing 4 byte argument |
| 1855 | { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END }, |
| 1856 | // Positive integer missing 8 byte argument |
| 1857 | { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END }, |
| 1858 | // Positive integer missing 1 byte of 2 byte argument |
| 1859 | { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END }, |
| 1860 | // Positive integer missing 2 bytes of 4 byte argument |
| 1861 | { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END }, |
| 1862 | // Positive integer missing 1 bytes of 7 byte argument |
| 1863 | { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END }, |
| 1864 | // Negative integer missing 1 byte argument |
| 1865 | { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END }, |
| 1866 | // Binary string missing 1 byte argument |
| 1867 | { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END }, |
| 1868 | // Text string missing 1 byte argument |
| 1869 | { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END }, |
| 1870 | // Array missing 1 byte argument |
| 1871 | { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END }, |
| 1872 | // Map missing 1 byte argument |
| 1873 | { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END }, |
| 1874 | // Tag missing 1 byte argument |
| 1875 | { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END }, |
| 1876 | // Simple missing 1 byte argument |
| 1877 | { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1878 | // half-precision with 1 byte argument |
| 1879 | { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 1880 | // single-precision with 2 byte argument |
| 1881 | { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END }, |
| 1882 | // double-precision with 3 byte argument |
| 1883 | { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END }, |
| 1884 | |
| 1885 | |
| 1886 | // Tag with no content |
| 1887 | { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1888 | |
| 1889 | |
| 1890 | // Breaks must not occur in definite length arrays and maps |
| 1891 | // Array of length 1 with sole member replaced by a break |
| 1892 | { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1893 | // Array of length 2 with 2nd member replaced by a break |
| 1894 | { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1895 | // Map of length 1 with sole member label replaced by a break |
| 1896 | { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1897 | // Map of length 1 with sole member label replaced by break |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1898 | // Alternate representation that some decoders handle differently |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1899 | { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1900 | // Array of length 1 with 2nd member value replaced by a break |
| 1901 | { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1902 | // Map of length 2 with 2nd member replaced by a break |
| 1903 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK }, |
| 1904 | |
| 1905 | |
| 1906 | // Breaks must not occur on their own out of an indefinite length data item |
| 1907 | // A bare break is not well formed |
| 1908 | { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK }, |
| 1909 | // A bare break after a zero length definite length array |
| 1910 | { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK }, |
| 1911 | // A bare break after a zero length indefinite length map |
| 1912 | { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1913 | // A break inside a definite length array inside an indefenite length array |
| 1914 | { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 1915 | // Complicated mixed nesting with break outside indefinite length array |
| 1916 | { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1917 | |
| 1918 | |
| 1919 | // Forbidden two byte encodings of simple types |
| 1920 | // Must use 0xe0 instead |
| 1921 | { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1922 | // Should use 0xe1 instead |
| 1923 | { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1924 | // Should use 0xe2 instead |
| 1925 | { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1926 | // Should use 0xe3 instead |
| 1927 | { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1928 | // Should use 0xe4 instead |
| 1929 | { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1930 | // Should use 0xe5 instead |
| 1931 | { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1932 | // Should use 0xe6 instead |
| 1933 | { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1934 | // Should use 0xe7 instead |
| 1935 | { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1936 | // Should use 0xe8 instead |
| 1937 | { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1938 | // Should use 0xe9 instead |
| 1939 | { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1940 | // Should use 0xea instead |
| 1941 | { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1942 | // Should use 0xeb instead |
| 1943 | { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1944 | // Should use 0xec instead |
| 1945 | { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1946 | // Should use 0xed instead |
| 1947 | { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1948 | // Should use 0xee instead |
| 1949 | { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1950 | // Should use 0xef instead |
| 1951 | { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1952 | // Should use 0xf0 instead |
| 1953 | { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1954 | // Should use 0xf1 instead |
| 1955 | { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1956 | // Should use 0xf2 instead |
| 1957 | { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1958 | // Must use 0xf3 instead |
| 1959 | { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1960 | // Must use 0xf4 instead |
| 1961 | { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1962 | // Must use 0xf5 instead |
| 1963 | { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1964 | // Must use 0xf6 instead |
| 1965 | { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1966 | // Must use 0xf7 instead |
| 1967 | { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
| 1968 | // Must use 0xf8 instead |
| 1969 | { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1970 | // Reserved |
| 1971 | { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1972 | |
| 1973 | // Integers with additional info indefinite length |
| 1974 | // Positive integer with additional info indefinite length |
| 1975 | { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, |
| 1976 | // Negative integer with additional info indefinite length |
| 1977 | { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, |
| 1978 | // CBOR tag with "argument" an indefinite length |
| 1979 | { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT }, |
| 1980 | // CBOR tag with "argument" an indefinite length alternate vector |
| 1981 | { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT }, |
| 1982 | |
| 1983 | |
| 1984 | // Missing bytes from a deterministic length string |
| 1985 | // A byte string is of length 1 without the 1 byte |
| 1986 | { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, |
| 1987 | // A text string is of length 1 without the 1 byte |
| 1988 | { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 1989 | |
| 1990 | #if SIZE_MAX > 2147483647 |
Laurence Lundblade | 42272e4 | 2020-01-31 07:50:53 -0800 | [diff] [blame] | 1991 | // Byte string should have 2^32-15 bytes, but has one |
| 1992 | { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 1993 | // Byte string should have 2^32-15 bytes, but has one |
| 1994 | { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 1995 | // Byte string should have 2^64 bytes, but has 3 |
| 1996 | { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 1997 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 1998 | // Text string should have 2^64 bytes, but has 3 |
| 1999 | { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 2000 | 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame^] | 2001 | #else |
| 2002 | // Byte string should have 2^32-15 bytes, but has one |
| 2003 | { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2004 | // Byte string should have 2^32-15 bytes, but has one |
| 2005 | { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END }, |
| 2006 | // Byte string should have 2^16 bytes, but has 3 |
| 2007 | { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2008 | // Text string should have 2^64 bytes, but has 3 |
| 2009 | { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END }, |
| 2010 | #endif |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2011 | |
| 2012 | // Use of unassigned additional information values |
| 2013 | // Major type positive integer with reserved value 28 |
| 2014 | { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2015 | // Major type positive integer with reserved value 29 |
| 2016 | { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2017 | // Major type positive integer with reserved value 30 |
| 2018 | { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2019 | // Major type negative integer with reserved value 28 |
| 2020 | { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2021 | // Major type negative integer with reserved value 29 |
| 2022 | { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2023 | // Major type negative integer with reserved value 30 |
| 2024 | { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2025 | // Major type byte string with reserved value 28 length |
| 2026 | { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2027 | // Major type byte string with reserved value 29 length |
| 2028 | { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2029 | // Major type byte string with reserved value 30 length |
| 2030 | { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2031 | // Major type text string with reserved value 28 length |
| 2032 | { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2033 | // Major type text string with reserved value 29 length |
| 2034 | { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2035 | // Major type text string with reserved value 30 length |
| 2036 | { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2037 | // Major type array with reserved value 28 length |
| 2038 | { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2039 | // Major type array with reserved value 29 length |
| 2040 | { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2041 | // Major type array with reserved value 30 length |
| 2042 | { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2043 | // Major type map with reserved value 28 length |
| 2044 | { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2045 | // Major type map with reserved value 29 length |
| 2046 | { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2047 | // Major type map with reserved value 30 length |
| 2048 | { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2049 | // Major type tag with reserved value 28 length |
| 2050 | { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2051 | // Major type tag with reserved value 29 length |
| 2052 | { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2053 | // Major type tag with reserved value 30 length |
| 2054 | { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2055 | // Major type simple with reserved value 28 length |
| 2056 | { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2057 | // Major type simple with reserved value 29 length |
| 2058 | { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2059 | // Major type simple with reserved value 30 length |
| 2060 | { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED }, |
| 2061 | |
| 2062 | |
| 2063 | // Maps must have an even number of data items (key & value) |
| 2064 | // Map with 1 item when it should have 2 |
| 2065 | { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2066 | // Map with 3 item when it should have 4 |
| 2067 | { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END }, |
| 2068 | // Map with 1 item when it should have 2 |
| 2069 | { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK }, |
| 2070 | // Map with 3 item when it should have 4 |
| 2071 | { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK }, |
| 2072 | |
| 2073 | |
| 2074 | // In addition to not-well-formed, some invalid CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2075 | // Text-based date, with an integer |
| 2076 | { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2077 | // Epoch date, with an byte string |
| 2078 | { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2079 | // tagged as both epoch and string dates |
| 2080 | { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, |
| 2081 | // big num tagged an int, not a byte string |
| 2082 | { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2083 | }; |
| 2084 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2085 | int32_t DecodeFailureTests() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2086 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2087 | int32_t nResult; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2088 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2089 | nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2090 | if(nResult) { |
| 2091 | return nResult; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2092 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2093 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2094 | // Corrupt the UsefulInputBuf and see that |
| 2095 | // it reflected correctly for CBOR decoding |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2096 | QCBORDecodeContext DCtx; |
| 2097 | QCBORItem Item; |
| 2098 | QCBORError uQCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2099 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2100 | QCBORDecode_Init(&DCtx, |
| 2101 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), |
| 2102 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2103 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2104 | if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2105 | return (int32_t)uQCBORError; |
| 2106 | } |
| 2107 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) { |
| 2108 | // This wasn't supposed to happen |
| 2109 | return -1; |
| 2110 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2111 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2112 | DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2113 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2114 | uQCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2115 | if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) { |
| 2116 | // Did not get back the error expected |
| 2117 | return -2; |
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 | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2120 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2121 | /* |
| 2122 | The max size of a string for QCBOR is SIZE_MAX - 4 so this |
| 2123 | tests here can be performed to see that the max length |
| 2124 | error check works correctly. See DecodeBytes(). If the max |
| 2125 | size was SIZE_MAX, it wouldn't be possible to test this. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2126 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2127 | This test will automatocally adapt the all CPU sizes |
| 2128 | through the use of SIZE_MAX. |
| 2129 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2130 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2131 | UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2132 | UsefulBufC EncodedHead; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2133 | |
Laurence Lundblade | 98427e9 | 2020-09-28 21:33:23 -0700 | [diff] [blame] | 2134 | // This makes a CBOR head with a text string that is very long |
| 2135 | // but doesn't fill in the bytes of the text string as that is |
| 2136 | // not needed to test this part of QCBOR. |
| 2137 | EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX); |
| 2138 | |
| 2139 | QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL); |
| 2140 | |
| 2141 | if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) { |
| 2142 | return -4; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2143 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2144 | |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 2145 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2149 | /* Try all 256 values of the byte at nLen including recursing for |
| 2150 | each of the values to try values at nLen+1 ... up to nLenMax |
| 2151 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2152 | static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2153 | { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2154 | if(nLen >= nLenMax) { |
| 2155 | return; |
| 2156 | } |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2157 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2158 | for(int inputByte = 0; inputByte < 256; inputByte++) { |
| 2159 | // Set up the input |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2160 | pBuf[nLen] = (uint8_t)inputByte; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 2161 | const UsefulBufC Input = {pBuf, nLen+1}; |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2162 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2163 | // Get ready to parse |
| 2164 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2165 | QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2166 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2167 | // Parse by getting the next item until an error occurs |
| 2168 | // Just about every possible decoder error can occur here |
| 2169 | // The goal of this test is not to check for the correct |
| 2170 | // error since that is not really possible. It is to |
| 2171 | // see that there is no crash on hostile input. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2172 | while(1) { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2173 | QCBORItem Item; |
| 2174 | QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2175 | if(nCBORError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2176 | break; |
| 2177 | } |
| 2178 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2179 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2180 | ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2185 | int32_t ComprehensiveInputTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2186 | { |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2187 | // Size 2 tests 64K inputs and runs quickly |
| 2188 | uint8_t pBuf[2]; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2189 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2190 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2191 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2192 | return 0; |
| 2193 | } |
| 2194 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2195 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2196 | int32_t BigComprehensiveInputTest() |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2197 | { |
| 2198 | // size 3 tests 16 million inputs and runs OK |
| 2199 | // in seconds on fast machines. Size 4 takes |
| 2200 | // 10+ minutes and 5 half a day on fast |
| 2201 | // machines. This test is kept separate from |
| 2202 | // the others so as to no slow down the use |
| 2203 | // of them as a very frequent regression. |
| 2204 | uint8_t pBuf[3]; // |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2205 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2206 | ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf)); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 2207 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 2208 | return 0; |
| 2209 | } |
| 2210 | |
| 2211 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 2212 | static uint8_t spDateTestInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2213 | 0xc0, // tag for string date |
| 2214 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2215 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2216 | 0xc0, // tag for string date |
| 2217 | 0x00, // Wrong type for a string date |
| 2218 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2219 | 0xc1, // tag for epoch date |
| 2220 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2221 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2222 | 0xc1, |
| 2223 | 0x62, 'h', 'i', // wrong type tagged |
| 2224 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2225 | // CBOR_TAG_ENC_AS_B64 |
| 2226 | 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2227 | 0x1a, 0x53, 0x72, 0x4E, 0x01, |
| 2228 | |
| 2229 | 0xc1, // tag for epoch date |
| 2230 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2231 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2232 | 0xc1, // tag for epoch date |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2233 | 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2234 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2235 | 0xc1, // tag for epoch date |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2236 | 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2237 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2238 | 0xc1, // tag for epoch date |
| 2239 | 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large |
| 2240 | //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large |
| 2241 | |
| 2242 | 0xc1, // tag for epoch date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2243 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2244 | |
| 2245 | 0xc1, // tag for epoch date |
| 2246 | 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN |
| 2247 | |
| 2248 | 0xc1, |
| 2249 | 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity |
| 2250 | |
| 2251 | 0xc1, // tag for epoch date |
| 2252 | 0xf9, 0xfc, 0x00, // -Infinity |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2253 | }; |
| 2254 | |
| 2255 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2256 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2257 | // have to check float expected only to within an epsilon |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2258 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 2259 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2260 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2261 | double diff = val - expected; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2262 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2263 | diff = fabs(diff); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2264 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2265 | return diff > 0.0000001; |
| 2266 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 2267 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2268 | |
| 2269 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2270 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2271 | int32_t DateParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2272 | { |
| 2273 | QCBORDecodeContext DCtx; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2274 | QCBORItem Item; |
| 2275 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2276 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2277 | QCBORDecode_Init(&DCtx, |
| 2278 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 2279 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2280 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2281 | // String date |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2282 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2283 | return -1; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2284 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2285 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2286 | UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){ |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2287 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2288 | } |
| 2289 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2290 | // Wrong type for a string date |
| 2291 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2292 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 2293 | return -3; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2297 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2298 | return -4; |
| 2299 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2300 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2301 | Item.val.epochDate.nSeconds != 1400000000 || |
| 2302 | Item.val.epochDate.fSecondsFraction != 0 ) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2303 | return -5; |
| 2304 | } |
| 2305 | |
| 2306 | // Wrong type for an epoch date |
| 2307 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) { |
| 2308 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2309 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2310 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2311 | // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag. |
| 2312 | // The date is decoded and the two tags are returned. This is to |
| 2313 | // make sure the wrapping of epoch date in another tag works OK. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2314 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2315 | return -7; |
| 2316 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2317 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2318 | Item.val.epochDate.nSeconds != 1400000001 || |
| 2319 | Item.val.epochDate.fSecondsFraction != 0 || |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2320 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2321 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2322 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2323 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2324 | // Epoch date that is too large for our representation |
| 2325 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2326 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2327 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2328 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2329 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2330 | // Epoch date in float format with fractional seconds |
| 2331 | if((uError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 2332 | return -10; |
| 2333 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2334 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2335 | Item.val.epochDate.nSeconds != 1 || |
| 2336 | CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2337 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2338 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2339 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2340 | // Epoch date float that is too large for our representation |
| 2341 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2342 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2343 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2344 | |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2345 | // Epoch date double that is just slightly too large |
| 2346 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2347 | return -13; |
Laurence Lundblade | a1ad878 | 2019-11-08 00:12:11 -0800 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | // Largest double epoch date supported |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2351 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS || |
| 2352 | Item.uDataType != QCBOR_TYPE_DATE_EPOCH || |
| 2353 | Item.val.epochDate.nSeconds != 9223372036854773760 || |
| 2354 | Item.val.epochDate.nSeconds == 0) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2355 | return -14; |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2356 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2357 | |
| 2358 | // Nan |
| 2359 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2360 | return -15; |
| 2361 | } |
| 2362 | |
| 2363 | // +Inifinity double-precision |
| 2364 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2365 | return -16; |
| 2366 | } |
| 2367 | |
| 2368 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2369 | // -Inifinity half-precision |
| 2370 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) { |
| 2371 | return -17; |
| 2372 | } |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2373 | #else |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2374 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) { |
| 2375 | return -18; |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2376 | } |
| 2377 | #endif |
| 2378 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2379 | #else |
| 2380 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2381 | return -19; |
| 2382 | } |
| 2383 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2384 | return -20; |
| 2385 | } |
| 2386 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2387 | return -21; |
| 2388 | } |
| 2389 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2390 | return -22; |
| 2391 | } |
| 2392 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2393 | return -23; |
| 2394 | } |
| 2395 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2396 | return -24; |
| 2397 | } |
| 2398 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2399 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2400 | return -25; |
| 2401 | } |
| 2402 | #else |
| 2403 | if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) { |
| 2404 | return -26; |
| 2405 | } |
| 2406 | #endif |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2407 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2408 | #endif |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2409 | |
| 2410 | return 0; |
| 2411 | } |
| 2412 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2413 | /* |
| 2414 | Test cases covered here. Some items cover more than one of these. |
| 2415 | positive integer (zero counts as a positive integer) |
| 2416 | negative integer |
| 2417 | half-precision float |
| 2418 | single-precision float |
| 2419 | double-precision float |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2420 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2421 | float Overflow error |
| 2422 | Wrong type error for epoch |
| 2423 | Wrong type error for date string |
| 2424 | float disabled error |
| 2425 | half-precision disabled error |
| 2426 | -Infinity |
| 2427 | Slightly too large integer |
| 2428 | Slightly too far from zero |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2429 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2430 | Get epoch by int |
| 2431 | Get string by int |
| 2432 | Get epoch by string |
| 2433 | Get string by string |
| 2434 | Fail to get epoch by wrong int label |
| 2435 | Fail to get string by wrong string label |
| 2436 | Fail to get epoch by string because it is invalid |
| 2437 | Fail to get epoch by int because it is invalid |
| 2438 | |
| 2439 | Untagged values |
| 2440 | */ |
| 2441 | static uint8_t spSpiffyDateTestInput[] = { |
| 2442 | 0x86, |
| 2443 | |
| 2444 | 0xc1, |
| 2445 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative |
| 2446 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2447 | 0xc1, // tag for epoch date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2448 | 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer |
| 2449 | |
| 2450 | 0xc1, // tag for epoch date |
| 2451 | 0xf9, 0xfc, 0x00, // Half-precision -Infinity |
| 2452 | |
| 2453 | 0xc1, // tag for epoch date |
| 2454 | 0x9f, 0xff, // Erroneous empty array as content for date |
| 2455 | |
| 2456 | 0xc0, // tag for string date |
| 2457 | 0xbf, 0xff, // Erroneous empty map as content for date |
| 2458 | |
| 2459 | 0xbf, // Open a map for tests involving labels. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2460 | |
| 2461 | 0x00, |
| 2462 | 0xc0, // tag for string date |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2463 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2464 | |
| 2465 | 0x01, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2466 | 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2467 | 0xc1, // tag for epoch date |
| 2468 | 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
| 2469 | |
| 2470 | // Untagged integer 0 |
| 2471 | 0x08, |
| 2472 | 0x00, |
| 2473 | |
| 2474 | // Utagged date string with string label y |
| 2475 | 0x61, 0x79, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2476 | 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2477 | |
| 2478 | // Untagged -1000 with label z |
| 2479 | 0x61, 0x7a, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2480 | 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2481 | 0x39, 0x03, 0xe7, |
| 2482 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2483 | 0x07, |
| 2484 | 0xc1, // tag for epoch date |
| 2485 | 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported |
| 2486 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2487 | 0x05, |
| 2488 | 0xc1, |
| 2489 | 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative |
| 2490 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2491 | // Untagged single-precision float with value 3.14 with string label x |
| 2492 | 0x61, 0x78, |
| 2493 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 2494 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2495 | // Untagged half-precision float with value -2 |
| 2496 | 0x09, |
| 2497 | 0xF9, 0xC0, 0x00, |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2498 | |
| 2499 | 0xff, |
| 2500 | }; |
| 2501 | |
| 2502 | int32_t SpiffyDateDecodeTest() |
| 2503 | { |
| 2504 | QCBORDecodeContext DC; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2505 | QCBORError uError; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2506 | int64_t nEpochDate2, nEpochDate3, nEpochDate5, |
| 2507 | nEpochDate4, nEpochDate6, nEpochDateFail, |
| 2508 | nEpochDate1400000000; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2509 | UsefulBufC StringDate1, StringDate2; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2510 | uint64_t uTag1, uTag2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2511 | |
| 2512 | QCBORDecode_Init(&DC, |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2513 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput), |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2514 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2515 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2516 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2517 | // Too-negative float, -9.2233720368547748E+18 |
| 2518 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2519 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2520 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2521 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
| 2522 | return 1111; |
| 2523 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2524 | #else |
| 2525 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2526 | return 1112; |
| 2527 | } |
| 2528 | #endif |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2529 | |
| 2530 | // Too-large integer |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2531 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2532 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2533 | if(uError != QCBOR_ERR_DATE_OVERFLOW) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2534 | return 1; |
| 2535 | } |
| 2536 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2537 | // Half-precision minus infinity |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2538 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2539 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2540 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 2541 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2542 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW; |
| 2543 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2544 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED; |
| 2545 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2546 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 2547 | const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED; |
| 2548 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 2549 | if(uError != uExpectedforHalfMinusInfinity) { |
| 2550 | return 2; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2553 | // Bad content for epoch date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2554 | QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2555 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2556 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
| 2557 | return 3; |
| 2558 | } |
| 2559 | |
| 2560 | // Bad content for string date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2561 | QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2562 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2563 | if(uError != QCBOR_ERR_BAD_OPT_TAG) { |
| 2564 | return 4; |
| 2565 | } |
| 2566 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2567 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2568 | |
| 2569 | // Get largest negative double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2570 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2571 | 5, |
| 2572 | QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG | |
| 2573 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2574 | &nEpochDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2575 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2576 | if(nEpochDate2 != -9223372036854773760LL) { |
| 2577 | return 101; |
| 2578 | } |
| 2579 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2580 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2581 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2582 | return 102; |
| 2583 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2584 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2585 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2586 | // Get largest double precision epoch date allowed |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2587 | QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2588 | &nEpochDate2); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2589 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2590 | if(nEpochDate2 != 9223372036854773760ULL) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2591 | return 111; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2592 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2593 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2594 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2595 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2596 | return 112; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2597 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2598 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2599 | |
| 2600 | // A single-precision date |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2601 | QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2602 | &nEpochDate5); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2603 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 2604 | if(nEpochDate5 != 3) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2605 | return 103; |
| 2606 | } |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2607 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2608 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2609 | if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 2610 | return 104; |
| 2611 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2612 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 2613 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2614 | // A half-precision date with value -2 FFF |
| 2615 | QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2616 | &nEpochDate4); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2617 | #if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT) |
| 2618 | if(nEpochDate4 != -2) { |
| 2619 | return 105; |
| 2620 | } |
| 2621 | #else |
| 2622 | uError = QCBORDecode_GetAndResetError(&DC); |
| 2623 | if(uError == QCBOR_SUCCESS) { |
| 2624 | return 106; |
| 2625 | } |
| 2626 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2627 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2628 | |
| 2629 | // Fail to get an epoch date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2630 | QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", |
| 2631 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2632 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2633 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2634 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2635 | return 107; |
| 2636 | } |
| 2637 | |
| 2638 | // Fail to get an epoch date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2639 | QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2640 | &nEpochDate6); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2641 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2642 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2643 | return 108; |
| 2644 | } |
| 2645 | |
| 2646 | // Fail to get a string date by string label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2647 | QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", |
| 2648 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2649 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2650 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2651 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2652 | return 109; |
| 2653 | } |
| 2654 | |
| 2655 | // Fail to get a string date by integer label |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2656 | QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2657 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2658 | uError = QCBORDecode_GetAndResetError(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2659 | if(uError != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2660 | return 110; |
| 2661 | } |
| 2662 | |
| 2663 | // The rest of these succeed even if float features are disabled |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2664 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2665 | // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2666 | QCBORDecode_GetEpochDateInMapN(&DC, |
| 2667 | 1, |
| 2668 | QCBOR_TAG_REQUIREMENT_TAG | |
| 2669 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2670 | &nEpochDate1400000000); |
| 2671 | uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2672 | // Tagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2673 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 2674 | &StringDate1); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2675 | // Untagged integer 0 |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2676 | QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2677 | &nEpochDate3); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2678 | // Untagged date string |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2679 | QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, |
| 2680 | &StringDate2); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2681 | // Untagged -1000 with label z |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2682 | QCBORDecode_GetEpochDateInMapSZ(&DC, |
| 2683 | "z", |
| 2684 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG | |
| 2685 | QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS, |
| 2686 | &nEpochDate6); |
| 2687 | uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2688 | |
| 2689 | QCBORDecode_ExitMap(&DC); |
| 2690 | QCBORDecode_ExitArray(&DC); |
| 2691 | uError = QCBORDecode_Finish(&DC); |
| 2692 | if(uError) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2693 | return 1000 + (int32_t)uError; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2696 | if(nEpochDate1400000000 != 1400000000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2697 | return 200; |
| 2698 | } |
| 2699 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2700 | if(uTag1 != 0x03030303) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2701 | return 201; |
| 2702 | } |
| 2703 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2704 | if(nEpochDate3 != 0) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2705 | return 202; |
| 2706 | } |
| 2707 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2708 | if(nEpochDate6 != -1000) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2709 | return 203; |
| 2710 | } |
| 2711 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2712 | if(uTag2 != 0x01010101) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2713 | return 204; |
| 2714 | } |
| 2715 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2716 | if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) { |
| 2717 | return 205; |
| 2718 | } |
| 2719 | |
| 2720 | if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) { |
| 2721 | return 206; |
| 2722 | } |
| 2723 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2724 | return 0; |
| 2725 | } |
| 2726 | |
| 2727 | |
| 2728 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2729 | // Input for one of the tagging tests |
| 2730 | static uint8_t spTagInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2731 | 0xd9, 0xd9, 0xf7, // CBOR magic number |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2732 | 0x81, // Array of one |
| 2733 | 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction |
| 2734 | 0x82, // Array of two that is the faction 1/3 |
| 2735 | 0x01, |
| 2736 | 0x03, |
| 2737 | |
| 2738 | /* |
| 2739 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 2740 | */ |
| 2741 | 0xd8, 0xe1, |
| 2742 | 0xd8, 0xe2, |
| 2743 | 0xd8, 0xe3, |
| 2744 | 0xd8, 0xe4, |
| 2745 | 0xd8, 0xe5, |
| 2746 | 0x80, |
| 2747 | |
| 2748 | /* tag 10489608748473423768( |
| 2749 | 2442302356( |
| 2750 | 21590( |
| 2751 | 240( |
| 2752 | [])))) |
| 2753 | */ |
| 2754 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2755 | 0xda, 0x91, 0x92, 0x93, 0x94, |
| 2756 | 0xd9, 0x54, 0x56, |
| 2757 | 0xd8, 0xf0, |
| 2758 | 0x80, |
| 2759 | |
| 2760 | /* tag 21590( |
| 2761 | 10489608748473423768( |
| 2762 | 2442302357( |
| 2763 | 65534( |
| 2764 | [])))) |
| 2765 | */ |
| 2766 | 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56, |
| 2767 | 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 2768 | 0xda, 0x91, 0x92, 0x93, 0x95, |
| 2769 | 0xd9, 0xff, 0xfe, |
| 2770 | 0x80, |
| 2771 | |
| 2772 | /* Make sure to blow past the limit of tags that must be mapped. |
| 2773 | works in conjuntion with entries above. |
| 2774 | 269488144(269488145(269488146(269488147([])))) |
| 2775 | */ |
| 2776 | 0xda, 0x10, 0x10, 0x10, 0x10, |
| 2777 | 0xda, 0x10, 0x10, 0x10, 0x11, |
| 2778 | 0xda, 0x10, 0x10, 0x10, 0x12, |
| 2779 | 0xda, 0x10, 0x10, 0x10, 0x13, |
| 2780 | 0x80, |
| 2781 | |
| 2782 | /* An invalid decimal fraction with an additional tag */ |
| 2783 | 0xd9, 0xff, 0xfa, |
| 2784 | 0xd8, 0x02, // non-preferred serialization of tag 2, a big num |
| 2785 | 0x00, // the integer 0; should be a byte string |
| 2786 | }; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2787 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2788 | /* |
| 2789 | DB 9192939495969798 # tag(10489608748473423768) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2790 | 80 # array(0) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2791 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2792 | static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, |
| 2793 | 0x96, 0x97, 0x98, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2794 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2795 | /* |
| 2796 | DB 9192939495969798 # tag(10489608748473423768) |
| 2797 | D8 88 # tag(136) |
| 2798 | C6 # tag(6) |
| 2799 | C7 # tag(7) |
| 2800 | 80 # array(0) |
| 2801 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2802 | static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
| 2803 | 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2804 | |
| 2805 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2806 | 55799(55799(55799({ |
| 2807 | 6(7(-23)): 5859837686836516696(7({ |
| 2808 | 7(-20): 11({ |
| 2809 | 17(-18): 17(17(17("Organization"))), |
| 2810 | 9(-17): 773("SSG"), |
| 2811 | -15: 16(17(6(7("Confusion")))), |
| 2812 | 17(-16): 17("San Diego"), |
| 2813 | 17(-14): 17("US") |
| 2814 | }), |
| 2815 | 23(-19): 19({ |
| 2816 | -11: 9({ |
| 2817 | -9: -7 |
| 2818 | }), |
| 2819 | 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A') |
| 2820 | }) |
| 2821 | })), |
| 2822 | 16(-22): 23({ |
| 2823 | 11(8(7(-5))): 8(-3) |
| 2824 | }) |
| 2825 | }))) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2826 | */ |
| 2827 | static uint8_t spCSRWithTags[] = { |
| 2828 | 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2, |
| 2829 | 0xc6, 0xc7, 0x36, |
| 2830 | 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2, |
| 2831 | 0xda, 0x00, 0x00, 0x00, 0x07, 0x33, |
| 2832 | 0xcb, 0xa5, |
| 2833 | 0xd1, 0x31, |
| 2834 | 0xd1, 0xd1, 0xd1, 0x6c, |
| 2835 | 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, |
| 2836 | 0xc9, 0x30, |
| 2837 | 0xd9, 0x03, 0x05, 0x63, |
| 2838 | 0x53, 0x53, 0x47, |
| 2839 | 0x2e, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2840 | 0xd0, 0xd1, 0xc6, 0xc7, |
| 2841 | 0x69, |
| 2842 | 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e, |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2843 | 0xd1, 0x2f, |
| 2844 | 0xd1, 0x69, |
| 2845 | 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, |
| 2846 | 0xd1, 0x2d, |
| 2847 | 0xd1, 0x62, |
| 2848 | 0x55, 0x53, |
| 2849 | 0xd7, 0x32, |
| 2850 | 0xd3, 0xa2, |
| 2851 | 0x2a, |
| 2852 | 0xc9, 0xa1, |
| 2853 | 0x28, |
| 2854 | 0x26, |
| 2855 | 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29, |
| 2856 | 0xcc, 0x4a, |
| 2857 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a, |
| 2858 | 0xd0, 0x35, |
| 2859 | 0xd7, 0xa1, |
| 2860 | 0xcb, 0xc8, 0xc7, 0x24, |
| 2861 | 0xc8, 0x22}; |
| 2862 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2863 | |
| 2864 | static uint8_t spSpiffyTagInput[] = { |
| 2865 | 0x9f, // Open indefinite array |
| 2866 | |
| 2867 | 0xc0, // tag for string date |
| 2868 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2869 | |
| 2870 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2871 | |
| 2872 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 2873 | |
| 2874 | 0xd8, 0x23, // tag for regex |
| 2875 | 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string |
| 2876 | |
| 2877 | 0xc0, // tag for string date |
| 2878 | 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string |
| 2879 | |
| 2880 | 0xff |
| 2881 | }; |
| 2882 | |
| 2883 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2884 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2885 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2886 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 2887 | int32_t OptTagParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2888 | { |
| 2889 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2890 | QCBORItem Item; |
| 2891 | QCBORError uError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2892 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2893 | QCBORDecode_Init(&DCtx, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2894 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput), |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2895 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2896 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2897 | /* |
| 2898 | This test matches the magic number tag and the fraction tag |
| 2899 | 55799([...]) |
| 2900 | */ |
| 2901 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2902 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2903 | return -2; |
| 2904 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2905 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2906 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) { |
| 2907 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 2908 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2909 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2910 | /* |
| 2911 | 4([1,3]) |
| 2912 | */ |
| 2913 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2914 | #ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 2915 | if(uError != QCBOR_SUCCESS || |
| 2916 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2917 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) || |
| 2918 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION || |
| 2919 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 2920 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 2921 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 2922 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 || |
| 2923 | Item.val.uCount != 2) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2924 | return -4; |
| 2925 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2926 | // consume the items in the array |
| 2927 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2928 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2929 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2930 | #else |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2931 | if(uError != QCBOR_SUCCESS || |
| 2932 | Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 2933 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 || |
| 2934 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 || |
| 2935 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 || |
| 2936 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 || |
| 2937 | QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) { |
| 2938 | return -5; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2939 | } |
| 2940 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2941 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2942 | /* |
| 2943 | More than 4 tags on an item 225(226(227(228(229([]))))) |
| 2944 | */ |
| 2945 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2946 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2947 | return -6; |
| 2948 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2949 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2950 | if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) { |
| 2951 | return -106; |
| 2952 | } |
| 2953 | |
| 2954 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2955 | /* tag 10489608748473423768( |
| 2956 | 2442302356( |
| 2957 | 21590( |
| 2958 | 240( |
| 2959 | [])))) |
| 2960 | */ |
| 2961 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2962 | if(uError != QCBOR_SUCCESS || |
| 2963 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2964 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL || |
| 2965 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL || |
| 2966 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL || |
| 2967 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2968 | return -7; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | /* tag 21590( |
| 2972 | 10489608748473423768( |
| 2973 | 2442302357( |
| 2974 | 21591( |
| 2975 | [])))) |
| 2976 | */ |
| 2977 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2978 | if(uError != QCBOR_SUCCESS || |
| 2979 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 2980 | QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL || |
| 2981 | QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL || |
| 2982 | QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL || |
| 2983 | QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) { |
| 2984 | return -8; |
| 2985 | } |
| 2986 | |
| 2987 | /* Make sure to blow past the limit of tags that must be mapped. |
| 2988 | works in conjuntion with entries above. |
| 2989 | 269488144(269488145(269488146(269488147([])))) |
| 2990 | */ |
| 2991 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2992 | if(uError != QCBOR_ERR_TOO_MANY_TAGS) { |
| 2993 | return -9; |
| 2994 | } |
| 2995 | |
| 2996 | uError = QCBORDecode_GetNext(&DCtx, &Item); |
| 2997 | if(uError == QCBOR_SUCCESS) { |
| 2998 | return -10; |
| 2999 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3000 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3001 | // ---------------------------------- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3002 | // This test sets up a caller-config list that includes the very large |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3003 | // tage and then matches it. Caller-config lists are no longer |
| 3004 | // used or needed. This tests backwards compatibility with them. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3005 | QCBORDecode_Init(&DCtx, |
| 3006 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3007 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3008 | const uint64_t puList[] = {0x9192939495969798, 257}; |
| 3009 | const QCBORTagListIn TL = {2, puList}; |
| 3010 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3011 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3012 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3013 | return -8; |
| 3014 | } |
| 3015 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3016 | !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) || |
| 3017 | QCBORDecode_IsTagged(&DCtx, &Item, 257) || |
| 3018 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) || |
| 3019 | Item.val.uCount != 0) { |
| 3020 | return -9; |
| 3021 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3022 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3023 | //------------------------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3024 | // Sets up a caller-configured list and look up something not in it |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3025 | // Another backwards compatibility test. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3026 | const uint64_t puLongList[17] = {1,2,1}; |
| 3027 | const QCBORTagListIn TLLong = {17, puLongList}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3028 | QCBORDecode_Init(&DCtx, |
| 3029 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), |
| 3030 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3031 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong); |
| 3032 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3033 | return -11; |
| 3034 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3035 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3036 | uint64_t puTags[4]; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3037 | QCBORTagListOut Out = {0, 4, puTags}; |
| 3038 | |
| 3039 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3040 | // This tests retrievel of the full tag list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3041 | QCBORDecode_Init(&DCtx, |
| 3042 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3043 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3044 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3045 | return -12; |
| 3046 | } |
| 3047 | if(puTags[0] != 0x9192939495969798 || |
| 3048 | puTags[1] != 0x88 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3049 | puTags[2] != 0x06 || |
| 3050 | puTags[3] != 0x07) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3051 | return -13; |
| 3052 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3053 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3054 | // ---------------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3055 | // This tests too small of an out list |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3056 | QCBORDecode_Init(&DCtx, |
| 3057 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), |
| 3058 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3059 | QCBORTagListOut OutSmall = {0, 3, puTags}; |
| 3060 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) { |
| 3061 | return -14; |
| 3062 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3063 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3064 | |
| 3065 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3066 | // --------------- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3067 | // Decode a version of the "CSR" that has had a ton of tags randomly inserted |
| 3068 | // It is a bit of a messy test and maybe could be improved, but |
| 3069 | // it is retained as a backwards compatibility check. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3070 | QCBORDecode_Init(&DCtx, |
| 3071 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3072 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3073 | int n = CheckCSRMaps(&DCtx); |
| 3074 | if(n) { |
| 3075 | return n-2000; |
| 3076 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3077 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3078 | Out = (QCBORTagListOut){0, 16, puTags}; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3079 | QCBORDecode_Init(&DCtx, |
| 3080 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), |
| 3081 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3082 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3083 | /* With the spiffy decode revision, this tag list is not used. |
| 3084 | It doesn't matter if a tag is in this list or not so some |
| 3085 | tests that couldn't process a tag because it isn't in this list |
| 3086 | now can process these unlisted tags. The tests have been |
| 3087 | adjusted for this. */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3088 | const uint64_t puTagList[] = {773, 1, 90599561}; |
| 3089 | const QCBORTagListIn TagList = {3, puTagList}; |
| 3090 | QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3091 | |
| 3092 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3093 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3094 | return -100; |
| 3095 | } |
| 3096 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3097 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3098 | QCBORDecode_IsTagged(&DCtx, &Item, 90599561) || |
| 3099 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) || |
| 3100 | Item.val.uCount != 2 || |
| 3101 | puTags[0] != CBOR_TAG_CBOR_MAGIC || |
| 3102 | puTags[1] != CBOR_TAG_CBOR_MAGIC || |
| 3103 | puTags[2] != CBOR_TAG_CBOR_MAGIC || |
| 3104 | Out.uNumUsed != 3) { |
| 3105 | return -101; |
| 3106 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3107 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3108 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3109 | return -102; |
| 3110 | } |
| 3111 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3112 | QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) || |
| 3113 | QCBORDecode_IsTagged(&DCtx, &Item, 6) || |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3114 | !QCBORDecode_IsTagged(&DCtx, &Item, 7) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3115 | Item.val.uCount != 2 || |
| 3116 | puTags[0] != 5859837686836516696 || |
| 3117 | puTags[1] != 7 || |
| 3118 | Out.uNumUsed != 2) { |
| 3119 | return -103; |
| 3120 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3121 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3122 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3123 | return -104; |
| 3124 | } |
| 3125 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3126 | Item.val.uCount != 5 || |
| 3127 | puTags[0] != 0x0b || |
| 3128 | Out.uNumUsed != 1) { |
| 3129 | return -105; |
| 3130 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3131 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3132 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3133 | return -106; |
| 3134 | } |
| 3135 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3136 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) || |
| 3137 | Item.val.string.len != 12 || |
| 3138 | puTags[0] != CBOR_TAG_COSE_MAC0 || |
| 3139 | puTags[1] != CBOR_TAG_COSE_MAC0 || |
| 3140 | puTags[2] != CBOR_TAG_COSE_MAC0 || |
| 3141 | Out.uNumUsed != 3) { |
| 3142 | return -105; |
| 3143 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3144 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3145 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3146 | return -107; |
| 3147 | } |
| 3148 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3149 | !QCBORDecode_IsTagged(&DCtx, &Item, 773) || |
| 3150 | Item.val.string.len != 3 || |
| 3151 | puTags[0] != 773 || |
| 3152 | Out.uNumUsed != 1) { |
| 3153 | return -108; |
| 3154 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3155 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3156 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3157 | return -109; |
| 3158 | } |
| 3159 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3160 | !QCBORDecode_IsTagged(&DCtx, &Item, 16) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3161 | Item.val.string.len != 9 || |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 3162 | puTags[0] != 16 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3163 | puTags[3] != 7 || |
| 3164 | Out.uNumUsed != 4) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3165 | return -110; |
| 3166 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3167 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3168 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3169 | return -111; |
| 3170 | } |
| 3171 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3172 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3173 | Item.val.string.len != 9 || |
| 3174 | puTags[0] != 17 || |
| 3175 | Out.uNumUsed != 1) { |
| 3176 | return -112; |
| 3177 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3178 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3179 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3180 | return -111; |
| 3181 | } |
| 3182 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 3183 | !QCBORDecode_IsTagged(&DCtx, &Item, 17) || |
| 3184 | Item.val.string.len != 2 || |
| 3185 | puTags[0] != 17 || |
| 3186 | Out.uNumUsed != 1) { |
| 3187 | return -112; |
| 3188 | } |
| 3189 | |
| 3190 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3191 | return -113; |
| 3192 | } |
| 3193 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3194 | !QCBORDecode_IsTagged(&DCtx, &Item, 19) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3195 | Item.val.uCount != 2 || |
| 3196 | puTags[0] != 19 || |
| 3197 | Out.uNumUsed != 1) { |
| 3198 | return -114; |
| 3199 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3200 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3201 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3202 | return -115; |
| 3203 | } |
| 3204 | if(Item.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3205 | !QCBORDecode_IsTagged(&DCtx, &Item, 9) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3206 | Item.val.uCount != 1 || |
| 3207 | puTags[0] != 9 || |
| 3208 | Out.uNumUsed != 1) { |
| 3209 | return -116; |
| 3210 | } |
| 3211 | |
| 3212 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3213 | return -116; |
| 3214 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3215 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3216 | Item.val.int64 != -7 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3217 | Out.uNumUsed != 0) { |
| 3218 | return -117; |
| 3219 | } |
| 3220 | |
| 3221 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3222 | return -118; |
| 3223 | } |
| 3224 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 3225 | Item.val.string.len != 10 || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3226 | puTags[0] != 12 || |
| 3227 | Out.uNumUsed != 1) { |
| 3228 | return -119; |
| 3229 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3230 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3231 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3232 | return -120; |
| 3233 | } |
| 3234 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 3235 | !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) || |
| 3236 | Item.val.uCount != 1 || |
| 3237 | puTags[0] != 0x17 || |
| 3238 | Out.uNumUsed != 1) { |
| 3239 | return -121; |
| 3240 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3241 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3242 | if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) { |
| 3243 | return -122; |
| 3244 | } |
| 3245 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3246 | !QCBORDecode_IsTagged(&DCtx, &Item, 8) || |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3247 | Item.val.int64 != -3 || |
| 3248 | puTags[0] != 8 || |
| 3249 | Out.uNumUsed != 1) { |
| 3250 | return -123; |
| 3251 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3252 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 3253 | if(QCBORDecode_Finish(&DCtx)) { |
| 3254 | return -124; |
| 3255 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3256 | |
| 3257 | UsefulBufC DateString; |
| 3258 | QCBORDecode_Init(&DCtx, |
| 3259 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3260 | QCBOR_DECODE_MODE_NORMAL); |
| 3261 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3262 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3263 | // tagged date string |
| 3264 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3265 | // untagged date string |
| 3266 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3267 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3268 | return 100; |
| 3269 | } |
| 3270 | // untagged byte string |
| 3271 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3272 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3273 | return 101; |
| 3274 | } |
| 3275 | // tagged regex |
| 3276 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3277 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3278 | return 102; |
| 3279 | } |
| 3280 | // tagged date string with a byte string |
| 3281 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3282 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3283 | return 103; |
| 3284 | } |
| 3285 | QCBORDecode_ExitArray(&DCtx); |
| 3286 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3287 | return 104; |
| 3288 | } |
| 3289 | |
| 3290 | |
| 3291 | QCBORDecode_Init(&DCtx, |
| 3292 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3293 | QCBOR_DECODE_MODE_NORMAL); |
| 3294 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3295 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3296 | // tagged date string |
| 3297 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3298 | // untagged date string |
| 3299 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3300 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) { |
| 3301 | return 200; |
| 3302 | } |
| 3303 | // untagged byte string |
| 3304 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3305 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3306 | return 201; |
| 3307 | } |
| 3308 | // tagged regex |
| 3309 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3310 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3311 | return 202; |
| 3312 | } |
| 3313 | // tagged date string with a byte string |
| 3314 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString); |
| 3315 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3316 | return 203; |
| 3317 | } |
| 3318 | QCBORDecode_ExitArray(&DCtx); |
| 3319 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3320 | return 204; |
| 3321 | } |
| 3322 | |
| 3323 | QCBORDecode_Init(&DCtx, |
| 3324 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput), |
| 3325 | QCBOR_DECODE_MODE_NORMAL); |
| 3326 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3327 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3328 | // tagged date string |
| 3329 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3330 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3331 | return 300; |
| 3332 | } |
| 3333 | // untagged date string |
| 3334 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3335 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3336 | return 301; |
| 3337 | } |
| 3338 | // untagged byte string |
| 3339 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString); |
| 3340 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3341 | return 302; |
| 3342 | } |
| 3343 | // tagged regex |
| 3344 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3345 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 3346 | return 303; |
| 3347 | } |
| 3348 | // tagged date string with a byte string |
| 3349 | QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString); |
| 3350 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) { |
| 3351 | return 304; |
| 3352 | } |
| 3353 | QCBORDecode_ExitArray(&DCtx); |
| 3354 | if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) { |
| 3355 | return 305; |
| 3356 | } |
| 3357 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3358 | return 0; |
| 3359 | } |
| 3360 | |
| 3361 | |
| 3362 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3363 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3364 | static uint8_t spBigNumInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3365 | 0x83, |
| 3366 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3367 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3368 | 0xA4, |
| 3369 | 0x63, 0x42, 0x4E, 0x2B, |
| 3370 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3371 | 0x18, 0x40, |
| 3372 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3373 | 0x63, 0x42, 0x4E, 0x2D, |
| 3374 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3375 | 0x38, 0x3F, |
| 3376 | 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 3377 | |
| 3378 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3379 | 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] | 3380 | |
| 3381 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3382 | int32_t BignumParseTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3383 | { |
| 3384 | QCBORDecodeContext DCtx; |
| 3385 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3386 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3387 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3388 | QCBORDecode_Init(&DCtx, |
| 3389 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), |
| 3390 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3391 | |
| 3392 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3393 | // |
| 3394 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
| 3395 | return -1; |
| 3396 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3397 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3398 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3399 | |
| 3400 | // |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3401 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3402 | return -3; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3403 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3404 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3405 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | // |
| 3409 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3410 | return -5; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3411 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3412 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3413 | return -6; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3414 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3415 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3416 | // |
| 3417 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3418 | return -7; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3419 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3420 | return -8; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3421 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3422 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3423 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3424 | return -9; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3425 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3426 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3427 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3428 | return -10; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3432 | return -11; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3433 | if(Item.uDataType != QCBOR_TYPE_POSBIGNUM || |
| 3434 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3435 | Item.label.int64 != 64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3436 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3437 | return -12; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3438 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3439 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3440 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3441 | return -13; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3442 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3443 | Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3444 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3445 | return -14; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3446 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3447 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3448 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3449 | return -15; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3450 | if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM || |
| 3451 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 3452 | Item.label.int64 != -64 || |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3453 | UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 3454 | return -16; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3455 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3456 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3457 | return 0; |
| 3458 | } |
| 3459 | |
| 3460 | |
| 3461 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3462 | static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3463 | uint8_t uDataType, |
| 3464 | uint8_t uNestingLevel, |
| 3465 | uint8_t uNextNest, |
| 3466 | int64_t nLabel, |
| 3467 | QCBORItem *pItem) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3468 | { |
| 3469 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3470 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3471 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3472 | if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1; |
| 3473 | if(Item.uDataType != uDataType) return -1; |
| 3474 | if(uNestingLevel > 0) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3475 | if(Item.uLabelType != QCBOR_TYPE_INT64 && |
| 3476 | Item.uLabelType != QCBOR_TYPE_UINT64) { |
| 3477 | return -1; |
| 3478 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3479 | if(Item.uLabelType == QCBOR_TYPE_INT64) { |
| 3480 | if(Item.label.int64 != nLabel) return -1; |
| 3481 | } else { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3482 | if(Item.label.uint64 != (uint64_t)nLabel) return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3483 | } |
| 3484 | } |
| 3485 | if(Item.uNestingLevel != uNestingLevel) return -1; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3486 | if(Item.uNextNestLevel != uNextNest) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3487 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3488 | if(pItem) { |
| 3489 | *pItem = Item; |
| 3490 | } |
| 3491 | return 0; |
| 3492 | } |
| 3493 | |
| 3494 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3495 | // Same code checks definite and indefinite length versions of the map |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3496 | static int32_t CheckCSRMaps(QCBORDecodeContext *pDC) |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3497 | { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 3498 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3499 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3500 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3501 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3502 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3503 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3504 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4; |
| 3505 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5; |
| 3506 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6; |
| 3507 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7; |
| 3508 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3509 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3510 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9; |
| 3511 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3512 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3513 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11; |
| 3514 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3515 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3516 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13; |
| 3517 | if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3518 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3519 | if(QCBORDecode_Finish(pDC)) return -20; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3520 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3521 | return 0; |
| 3522 | } |
| 3523 | |
| 3524 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3525 | /* |
| 3526 | // cbor.me decoded output |
| 3527 | { |
| 3528 | -23: { |
| 3529 | -20: { |
| 3530 | -18: "Organization", |
| 3531 | -17: "SSG", |
| 3532 | -15: "Confusion", |
| 3533 | -16: "San Diego", |
| 3534 | -14: "US" |
| 3535 | }, |
| 3536 | -19: { |
| 3537 | -11: { |
| 3538 | -9: -7 |
| 3539 | }, |
| 3540 | -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n' |
| 3541 | } |
| 3542 | }, |
| 3543 | -22: { |
| 3544 | -5: -3 |
| 3545 | } |
| 3546 | } |
| 3547 | */ |
| 3548 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3549 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3550 | static uint8_t spCSRInput[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3551 | 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f, |
| 3552 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3553 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3554 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3555 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3556 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
| 3557 | 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26, |
| 3558 | 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 3559 | 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22}; |
| 3560 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3561 | int32_t NestedMapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3562 | { |
| 3563 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3564 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3565 | QCBORDecode_Init(&DCtx, |
| 3566 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3567 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3568 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3569 | return CheckCSRMaps(&DCtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3570 | } |
| 3571 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3572 | |
| 3573 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3574 | int32_t StringDecoderModeFailTest() |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3575 | { |
| 3576 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3577 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3578 | QCBORDecode_Init(&DCtx, |
| 3579 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 3580 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3581 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3582 | QCBORItem Item; |
| 3583 | QCBORError nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3584 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3585 | if(QCBORDecode_GetNext(&DCtx, &Item)) { |
| 3586 | return -1; |
| 3587 | } |
| 3588 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 3589 | return -2; |
| 3590 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3591 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 3592 | nCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 3593 | if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) { |
| 3594 | return -3; |
| 3595 | } |
| 3596 | |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
| 3600 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3601 | // Same map as above, but using indefinite lengths |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3602 | static uint8_t spCSRInputIndefLen[] = { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3603 | 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f, |
| 3604 | 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, |
| 3605 | 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47, |
| 3606 | 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, |
| 3607 | 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e, |
| 3608 | 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62, |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3609 | 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28, |
| 3610 | 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, |
| 3611 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff, |
| 3612 | 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff}; |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3613 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3614 | int32_t NestedMapTestIndefLen() |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3615 | { |
| 3616 | QCBORDecodeContext DCtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3617 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3618 | QCBORDecode_Init(&DCtx, |
| 3619 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), |
| 3620 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3621 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 3622 | return CheckCSRMaps(&DCtx); |
| 3623 | } |
| 3624 | |
| 3625 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 3626 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3627 | static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage) |
| 3628 | { |
| 3629 | UsefulOutBuf UOB; |
| 3630 | UsefulOutBuf_Init(&UOB, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3631 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3632 | int i; |
| 3633 | for(i = 0; i < n; i++) { |
| 3634 | UsefulOutBuf_AppendByte(&UOB, 0x9f); |
| 3635 | } |
| 3636 | |
| 3637 | for(i = 0; i < n; i++) { |
| 3638 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3639 | } |
| 3640 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3641 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3642 | |
| 3643 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3644 | static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel) |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3645 | { |
| 3646 | QCBORDecodeContext DC; |
| 3647 | QCBORDecode_Init(&DC, Nested, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3648 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3649 | int j; |
| 3650 | for(j = 0; j < nNestLevel; j++) { |
| 3651 | QCBORItem Item; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3652 | QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3653 | if(j >= QCBOR_MAX_ARRAY_NESTING) { |
| 3654 | // Should be in error |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3655 | if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3656 | return -4; |
| 3657 | } else { |
| 3658 | return 0; // Decoding doesn't recover after an error |
| 3659 | } |
| 3660 | } else { |
| 3661 | // Should be no error |
| 3662 | if(nReturn) { |
| 3663 | return -9; // Should not have got an error |
| 3664 | } |
| 3665 | } |
| 3666 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3667 | return -7; |
| 3668 | } |
| 3669 | } |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3670 | QCBORError nReturn = QCBORDecode_Finish(&DC); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3671 | if(nReturn) { |
| 3672 | return -3; |
| 3673 | } |
| 3674 | return 0; |
| 3675 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3676 | |
| 3677 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3678 | int32_t IndefiniteLengthNestTest() |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3679 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 3680 | UsefulBuf_MAKE_STACK_UB(Storage, 50); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3681 | int i; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3682 | for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 3683 | const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage); |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3684 | int nReturn = parse_indeflen_nested(Nested, i); |
| 3685 | if(nReturn) { |
| 3686 | return nReturn; |
| 3687 | } |
| 3688 | } |
| 3689 | return 0; |
| 3690 | } |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3691 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3692 | // [1, [2, 3]] |
| 3693 | static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; |
| 3694 | // No closing break |
| 3695 | static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; |
| 3696 | // Not enough closing breaks |
| 3697 | static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; |
| 3698 | // Too many closing breaks |
| 3699 | static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; |
| 3700 | // Unclosed indeflen inside def len |
| 3701 | static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; |
| 3702 | // confused tag |
| 3703 | static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3704 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3705 | int32_t IndefiniteLengthArrayMapTest() |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3706 | { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 3707 | QCBORError nResult; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3708 | // --- first test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3709 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3710 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3711 | // Decode it and see if it is OK |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3712 | QCBORDecodeContext DC; |
| 3713 | QCBORItem Item; |
| 3714 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3715 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3716 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3717 | |
| 3718 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3719 | Item.uNestingLevel != 0 || |
| 3720 | Item.uNextNestLevel != 1) { |
| 3721 | return -111; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3722 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3723 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3724 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3725 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
| 3726 | Item.uNestingLevel != 1 || |
| 3727 | Item.uNextNestLevel != 1) { |
| 3728 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3729 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3730 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3731 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3732 | if(Item.uDataType != QCBOR_TYPE_ARRAY || |
| 3733 | Item.uNestingLevel != 1 || |
| 3734 | Item.uNextNestLevel != 2) { |
| 3735 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3736 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3737 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3738 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3739 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3740 | Item.uNestingLevel != 2 || |
| 3741 | Item.uNextNestLevel != 2) { |
| 3742 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3743 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3744 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3745 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 12b495d | 2018-12-17 11:15:54 -0800 | [diff] [blame] | 3746 | if(Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3747 | Item.uNestingLevel != 2 || |
| 3748 | Item.uNextNestLevel != 0) { |
| 3749 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3750 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3751 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3752 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3753 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3754 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3755 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3756 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3757 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3758 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3759 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3760 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3761 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3762 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3763 | return -7; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3764 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3765 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3766 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3767 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3768 | return -8; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3769 | } |
| 3770 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3771 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3772 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3773 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3774 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3775 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3776 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3777 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3778 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3779 | return -9; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3783 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3784 | return -10; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3785 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3786 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3787 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3788 | if(nResult || Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3789 | return -11; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3790 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3791 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3792 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3793 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3794 | return -12; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3795 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3796 | |
| 3797 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3798 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3799 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3800 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3801 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3802 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3803 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3804 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3805 | return -13; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3806 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3807 | |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3808 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3809 | if(nResult != QCBOR_SUCCESS) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3810 | return -14; |
Laurence Lundblade | 19e0c80 | 2018-10-13 12:19:55 +0800 | [diff] [blame] | 3811 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3812 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 3813 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3814 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3815 | return -140; |
| 3816 | } |
| 3817 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3818 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3819 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3820 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3821 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3822 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3823 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3824 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3825 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3826 | return -15; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3827 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3828 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3829 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3830 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3831 | return -16; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3832 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3833 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3834 | nResult = QCBORDecode_Finish(&DC); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3835 | if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3836 | return -17; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 3837 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3838 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3839 | // --- next test ----- |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3840 | IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3841 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3842 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3843 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3844 | nResult = QCBORDecode_GetNext(&DC, &Item); |
| 3845 | if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3846 | return -18; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3847 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3848 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3849 | nResult = QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 3850 | if(nResult != QCBOR_ERR_BAD_BREAK) { |
| 3851 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3852 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3853 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 3854 | return 0; |
| 3855 | } |
| 3856 | |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3857 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 3858 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 3859 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3860 | static const uint8_t spIndefiniteLenString[] = { |
Laurence Lundblade | 17ede40 | 2018-10-13 11:43:07 +0800 | [diff] [blame] | 3861 | 0x81, // Array of length one |
| 3862 | 0x7f, // text string marked with indefinite length |
| 3863 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3864 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3865 | 0xff // ending break |
| 3866 | }; |
| 3867 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3868 | static const uint8_t spIndefiniteLenStringBad2[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3869 | 0x81, // Array of length one |
| 3870 | 0x7f, // text string marked with indefinite length |
| 3871 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3872 | 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type |
| 3873 | 0xff // ending break |
| 3874 | }; |
| 3875 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3876 | static const uint8_t spIndefiniteLenStringBad3[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3877 | 0x81, // Array of length one |
| 3878 | 0x7f, // text string marked with indefinite length |
| 3879 | 0x01, 0x02, // Not a string |
| 3880 | 0xff // ending break |
| 3881 | }; |
| 3882 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3883 | static const uint8_t spIndefiniteLenStringBad4[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3884 | 0x81, // Array of length one |
| 3885 | 0x7f, // text string marked with indefinite length |
| 3886 | 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment |
| 3887 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3888 | // missing end of string |
| 3889 | }; |
| 3890 | |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3891 | static const uint8_t spIndefiniteLenStringLabel[] = { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3892 | 0xa1, // Array of length one |
| 3893 | 0x7f, // text string marked with indefinite length |
| 3894 | 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment |
| 3895 | 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment |
| 3896 | 0xff, // ending break |
| 3897 | 0x01 // integer being labeled. |
| 3898 | }; |
| 3899 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3900 | /** |
| 3901 | Make an indefinite length string |
| 3902 | |
| 3903 | @param Storage Storage for string, must be 144 bytes in size |
| 3904 | @return The indefinite length string |
| 3905 | |
| 3906 | This makes an array with one indefinite length string that has 7 chunks |
| 3907 | from size of 1 byte up to 64 bytes. |
| 3908 | */ |
| 3909 | static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3910 | { |
| 3911 | UsefulOutBuf UOB; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3912 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3913 | UsefulOutBuf_Init(&UOB, Storage); |
| 3914 | UsefulOutBuf_AppendByte(&UOB, 0x81); |
| 3915 | UsefulOutBuf_AppendByte(&UOB, 0x5f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3916 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3917 | uint8_t uStringByte = 0; |
| 3918 | // Use of type int is intentional |
| 3919 | for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) { |
| 3920 | // Not using preferred encoding here, but that is OK. |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3921 | UsefulOutBuf_AppendByte(&UOB, 0x58); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3922 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize); |
| 3923 | for(int j = 0; j < uChunkSize; j++) { |
| 3924 | UsefulOutBuf_AppendByte(&UOB, uStringByte); |
| 3925 | uStringByte++; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3926 | } |
| 3927 | } |
| 3928 | UsefulOutBuf_AppendByte(&UOB, 0xff); |
| 3929 | |
| 3930 | return UsefulOutBuf_OutUBuf(&UOB); |
| 3931 | } |
| 3932 | |
| 3933 | static int CheckBigString(UsefulBufC BigString) |
| 3934 | { |
| 3935 | if(BigString.len != 255) { |
| 3936 | return 1; |
| 3937 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3938 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3939 | for(uint8_t i = 0; i < 255; i++){ |
| 3940 | if(((const uint8_t *)BigString.ptr)[i] != i) { |
| 3941 | return 1; |
| 3942 | } |
| 3943 | } |
| 3944 | return 0; |
| 3945 | } |
| 3946 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3947 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 3948 | int32_t IndefiniteLengthStringTest() |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3949 | { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3950 | QCBORDecodeContext DC; |
| 3951 | QCBORItem Item; |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3952 | // big enough for MakeIndefiniteBigBstr() + MemPool overhead |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 3953 | UsefulBuf_MAKE_STACK_UB(MemPool, 350); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3954 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3955 | // --- Simple normal indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 3956 | UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString); |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3957 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3958 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 3959 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3960 | return -1; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3961 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3962 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3963 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3964 | return -2; |
| 3965 | } |
| 3966 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
| 3967 | return -3; |
| 3968 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3969 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3970 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3971 | return -4; |
| 3972 | } |
| 3973 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) { |
| 3974 | return -5; |
| 3975 | } |
| 3976 | if(QCBORDecode_Finish(&DC)) { |
| 3977 | return -6; |
| 3978 | } |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 3979 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3980 | // ----- types mismatch --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3981 | QCBORDecode_Init(&DC, |
| 3982 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), |
| 3983 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3984 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3985 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 3986 | return -7; |
| 3987 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3988 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3989 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 3990 | return -8; |
| 3991 | } |
| 3992 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 3993 | return -9; |
| 3994 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3995 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3996 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 3997 | return -10; |
| 3998 | } |
| 3999 | |
| 4000 | // ----- not a string --- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4001 | QCBORDecode_Init(&DC, |
| 4002 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), |
| 4003 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4004 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4005 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4006 | return -11; |
| 4007 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4008 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4009 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4010 | return -12; |
| 4011 | } |
| 4012 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4013 | return -13; |
| 4014 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4015 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4016 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4017 | return -14; |
| 4018 | } |
| 4019 | |
| 4020 | // ----- no end ----- |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4021 | QCBORDecode_Init(&DC, |
| 4022 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), |
| 4023 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4024 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4025 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4026 | return -15; |
| 4027 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4028 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4029 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4030 | return -16; |
| 4031 | } |
| 4032 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4033 | return -17; |
| 4034 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4035 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4036 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) { |
| 4037 | return -18; |
| 4038 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4039 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4040 | // ------ Don't set a string allocator and see an error ----- |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4041 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4042 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4043 | QCBORDecode_GetNext(&DC, &Item); |
| 4044 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4045 | return -19; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4046 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4047 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4048 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4049 | return -20; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4050 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4051 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4052 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4053 | UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4054 | |
| 4055 | QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL); |
| 4056 | if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4057 | return -21; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4058 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4059 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4060 | // ----- Mempool is way too small ----- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 4061 | UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290); |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 4062 | const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4063 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4064 | // 80 is big enough for MemPool overhead, but not BigIndefBStr |
| 4065 | UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4066 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4067 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4068 | if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4069 | return -22; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4070 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4071 | |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4072 | QCBORDecode_GetNext(&DC, &Item); |
| 4073 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4074 | return -23; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4075 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4076 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4077 | return -24; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 4078 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4079 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4080 | // ---- big bstr ----- |
| 4081 | QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4082 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4083 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4084 | return -25; |
| 4085 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4086 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4087 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4088 | return -26; |
| 4089 | } |
| 4090 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4091 | return -26; |
| 4092 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4093 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4094 | if(QCBORDecode_GetNext(&DC, &Item)) { |
| 4095 | return -27; |
| 4096 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 4097 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) { |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4098 | return -28; |
| 4099 | } |
| 4100 | if(CheckBigString(Item.val.string)) { |
| 4101 | return -3; |
| 4102 | } |
| 4103 | if(QCBORDecode_Finish(&DC)) { |
| 4104 | return -29; |
| 4105 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4106 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4107 | // --- label is an indefinite length string ------ |
Laurence Lundblade | b836efb | 2018-10-28 20:09:58 +0700 | [diff] [blame] | 4108 | 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] | 4109 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4110 | if(QCBORDecode_SetMemPool(&DC, MemPool, false)) { |
| 4111 | return -30; |
| 4112 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4113 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4114 | QCBORDecode_GetNext(&DC, &Item); |
| 4115 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 4116 | return -31; |
| 4117 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4118 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4119 | if(QCBORDecode_GetNext(&DC, &Item)){ |
| 4120 | return -32; |
| 4121 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4122 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 4123 | Item.uDataType != QCBOR_TYPE_INT64 || |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4124 | Item.uDataAlloc || !Item.uLabelAlloc || |
| 4125 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) { |
| 4126 | return -33; |
| 4127 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4128 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 4129 | if(QCBORDecode_Finish(&DC)) { |
| 4130 | return -34; |
| 4131 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4132 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4133 | return 0; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4137 | int32_t AllocAllStringsTest() |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4138 | { |
| 4139 | QCBORDecodeContext DC; |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4140 | QCBORError nCBORError; |
| 4141 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4142 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4143 | // First test, use the "CSRMap" as easy input and checking |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4144 | QCBORDecode_Init(&DC, |
| 4145 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), |
| 4146 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4147 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4148 | 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] | 4149 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4150 | nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
| 4151 | if(nCBORError) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4152 | return -1; |
| 4153 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4154 | |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4155 | if(CheckCSRMaps(&DC)) { |
| 4156 | return -2; |
| 4157 | } |
| 4158 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 4159 | // Next parse, save pointers to a few strings, destroy original and |
| 4160 | // see all is OK. |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4161 | 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] | 4162 | const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 4163 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4164 | QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 1f8b5b0 | 2019-01-01 22:27:38 -0800 | [diff] [blame] | 4165 | UsefulBuf_Set(Pool, '/'); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4166 | QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4167 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4168 | QCBORItem Item1, Item2, Item3, Item4; |
| 4169 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4170 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4171 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
| 4172 | Item1.val.uCount != 3) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4173 | return -3; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4174 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4175 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4176 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item2))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4177 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4178 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item3))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4179 | return (int32_t)nCBORError; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4180 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item4))) |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 4181 | return (int32_t)nCBORError; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4182 | |
Laurence Lundblade | 05ec57b | 2018-10-21 01:50:03 +0530 | [diff] [blame] | 4183 | UsefulBuf_Set(CopyOfStorage, '_'); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4184 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4185 | if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4186 | Item1.uDataType != QCBOR_TYPE_INT64 || |
| 4187 | Item1.val.int64 != 42 || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4188 | Item1.uDataAlloc != 0 || |
| 4189 | Item1.uLabelAlloc == 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4190 | UsefulBufCompareToSZ(Item1.label.string, "first integer")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4191 | return -4; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4192 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4193 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4194 | |
| 4195 | if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4196 | UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4197 | Item2.uDataType != QCBOR_TYPE_ARRAY || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4198 | Item2.uDataAlloc != 0 || |
| 4199 | Item2.uLabelAlloc == 0 || |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4200 | Item2.val.uCount != 2) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4201 | return -5; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4202 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4203 | if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4204 | Item3.uDataAlloc == 0 || |
| 4205 | Item3.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4206 | UsefulBufCompareToSZ(Item3.val.string, "string1")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4207 | return -6; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4208 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4209 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4210 | if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4211 | Item4.uDataAlloc == 0 || |
| 4212 | Item4.uLabelAlloc != 0 || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4213 | UsefulBufCompareToSZ(Item4.val.string, "string2")) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4214 | return -7; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4215 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4216 | |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4217 | // Next parse with a pool that is too small |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4218 | UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4219 | QCBORDecode_Init(&DC, |
| 4220 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), |
| 4221 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4222 | QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying. |
| 4223 | if((nCBORError = QCBORDecode_GetNext(&DC, &Item1))) |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4224 | return -8; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4225 | if(Item1.uDataType != QCBOR_TYPE_MAP || |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4226 | Item1.val.uCount != 3) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4227 | return -9; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 4228 | } |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4229 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){ |
| 4230 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) { |
| 4231 | if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) { |
| 4232 | nCBORError = QCBORDecode_GetNext(&DC, &Item4); |
| 4233 | } |
| 4234 | } |
| 4235 | } |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 4236 | if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) { |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4237 | return -10; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | return 0; |
| 4241 | } |
| 4242 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4243 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4244 | int32_t MemPoolTest(void) |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 4245 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4246 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4247 | // nothing can be done with it unless that is set up. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4248 | QCBORDecodeContext DC; |
| 4249 | const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map |
| 4250 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4251 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4252 | // Set up an memory pool of 100 bytes |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4253 | // Then fish into the internals of the decode context |
| 4254 | // to get the allocator function so it can be called directly. |
| 4255 | // Also figure out how much pool is available for use |
| 4256 | // buy subtracting out the overhead. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4257 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | df2836b | 2018-12-19 18:13:29 -0800 | [diff] [blame] | 4258 | QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0); |
| 4259 | if(nError) { |
| 4260 | return -9; |
| 4261 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4262 | QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator; |
| 4263 | void *pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4264 | size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4265 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4266 | // First test -- ask for one more byte than available and see failure |
| 4267 | UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4268 | if(!UsefulBuf_IsNULL(Allocated)) { |
| 4269 | return -1; |
| 4270 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4271 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4272 | // Re do the set up for the next test that will do a successful alloc, |
| 4273 | // a fail, a free and then success |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4274 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4275 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4276 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
| 4277 | uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4278 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4279 | // Allocate one byte less than available and see success |
| 4280 | Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4281 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4282 | return -2; |
| 4283 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4284 | // Ask for some more and see failure |
| 4285 | UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4286 | if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail |
| 4287 | return -3; |
| 4288 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4289 | // Free the first allocate, retry the second and see success |
| 4290 | (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free |
| 4291 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4292 | if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free |
| 4293 | return -4; |
| 4294 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4295 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4296 | // Re do set up for next test that involves a successful alloc, |
| 4297 | // and a successful realloc and a failed realloc |
| 4298 | QCBORDecode_SetMemPool(&DC, Pool, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4299 | pAlloc = DC.StringAllocator.pfAllocator; |
| 4300 | pAllocCtx = DC.StringAllocator.pAllocateCxt; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4301 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4302 | // Allocate half the pool and see success |
| 4303 | Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4304 | if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed |
| 4305 | return -5; |
| 4306 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4307 | // Reallocate to take up the whole pool and see success |
| 4308 | Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool); |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4309 | if(UsefulBuf_IsNULL(Allocated2)) { |
| 4310 | return -6; |
| 4311 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4312 | // Make sure its the same pointer and the size is right |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4313 | if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) { |
| 4314 | return -7; |
| 4315 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4316 | // Try to allocate more to be sure there is failure after a realloc |
| 4317 | UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1); |
| 4318 | if(!UsefulBuf_IsNULL(Allocated3)) { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4319 | return -8; |
| 4320 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4321 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 4322 | return 0; |
| 4323 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 4324 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4325 | |
| 4326 | /* Just enough of an allocator to test configuration of one */ |
| 4327 | static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize) |
| 4328 | { |
| 4329 | (void)pOldMem; // unused variable |
| 4330 | |
| 4331 | if(uNewSize) { |
| 4332 | // Assumes the context pointer is the buffer and |
| 4333 | // nothing too big will ever be asked for. |
| 4334 | // This is only good for this basic test! |
| 4335 | return (UsefulBuf) {pCtx, uNewSize}; |
| 4336 | } else { |
| 4337 | return NULLUsefulBuf; |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4342 | int32_t SetUpAllocatorTest(void) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4343 | { |
| 4344 | // Set up the decoder with a tiny bit of CBOR to parse because |
| 4345 | // nothing can be done with it unless that is set up. |
| 4346 | QCBORDecodeContext DC; |
| 4347 | const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi" |
| 4348 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0); |
| 4349 | |
| 4350 | uint8_t pAllocatorBuffer[50]; |
| 4351 | |
| 4352 | // This is really just to test that this call works. |
| 4353 | // The full functionality of string allocators is tested |
| 4354 | // elsewhere with the MemPool internal allocator. |
| 4355 | QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1); |
| 4356 | |
| 4357 | QCBORItem Item; |
| 4358 | if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) { |
| 4359 | return -1; |
| 4360 | } |
| 4361 | |
| 4362 | if(Item.uDataAlloc == 0 || |
| 4363 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 4364 | Item.val.string.ptr != pAllocatorBuffer) { |
| 4365 | return -2; |
| 4366 | } |
| 4367 | |
| 4368 | if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) { |
| 4369 | return -3; |
| 4370 | } |
| 4371 | |
| 4372 | return 0; |
| 4373 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4374 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 4375 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 4376 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4377 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4378 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4379 | /* exponent, mantissa |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4380 | [ |
| 4381 | 4([-1, 3]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4382 | 4([-20, 4759477275222530853136]), |
| 4383 | 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4384 | 5([300, 100]), |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4385 | 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4386 | 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4387 | 5([ 9223372036854775806, -4759477275222530853137]) |
| 4388 | 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4389 | ] |
| 4390 | */ |
| 4391 | |
| 4392 | static const uint8_t spExpectedExponentsAndMantissas[] = { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4393 | 0x88, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4394 | 0xC4, 0x82, 0x20, |
| 4395 | 0x03, |
| 4396 | 0xC4, 0x82, 0x33, |
| 4397 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4398 | 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4399 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4400 | 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 4401 | 0x18, 0x64, |
| 4402 | 0xC5, 0x82, 0x33, |
| 4403 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 4404 | 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4405 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4406 | 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4407 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4408 | 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 4409 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE |
| 4410 | }; |
| 4411 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4412 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4413 | int32_t ExponentAndMantissaDecodeTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4414 | { |
| 4415 | QCBORDecodeContext DC; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4416 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4417 | QCBORItem item; |
| 4418 | |
Laurence Lundblade | 17af490 | 2020-01-07 19:11:55 -0800 | [diff] [blame] | 4419 | static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 4420 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 4421 | UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4422 | |
| 4423 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4424 | QCBORDecode_Init(&DC, |
| 4425 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4426 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4427 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4428 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4429 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4430 | return 1; |
| 4431 | } |
| 4432 | |
| 4433 | if(item.uDataType != QCBOR_TYPE_ARRAY) { |
| 4434 | return 2; |
| 4435 | } |
| 4436 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4437 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4438 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4439 | return 3; |
| 4440 | } |
| 4441 | |
| 4442 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4443 | item.val.expAndMantissa.Mantissa.nInt != 3 || |
| 4444 | item.val.expAndMantissa.nExponent != -1) { |
| 4445 | return 4; |
| 4446 | } |
| 4447 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4448 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4449 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4450 | return 5; |
| 4451 | } |
| 4452 | |
| 4453 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4454 | item.val.expAndMantissa.nExponent != -20 || |
| 4455 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4456 | return 6; |
| 4457 | } |
| 4458 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4459 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4460 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4461 | return 7; |
| 4462 | } |
| 4463 | |
| 4464 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM || |
| 4465 | item.val.expAndMantissa.nExponent != 9223372036854775807 || |
| 4466 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4467 | return 8; |
| 4468 | } |
| 4469 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4470 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4471 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4472 | return 9; |
| 4473 | } |
| 4474 | |
| 4475 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4476 | item.val.expAndMantissa.Mantissa.nInt != 100 || |
| 4477 | item.val.expAndMantissa.nExponent != 300) { |
| 4478 | return 10; |
| 4479 | } |
| 4480 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4481 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4482 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4483 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4484 | return 11; |
| 4485 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4486 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM || |
| 4487 | item.val.expAndMantissa.nExponent != -20 || |
| 4488 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4489 | return 12; |
| 4490 | } |
| 4491 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4492 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4493 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4494 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4495 | return 13; |
| 4496 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4497 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4498 | item.val.expAndMantissa.nExponent != -9223372036854775807 || |
| 4499 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
| 4500 | return 14; |
| 4501 | } |
| 4502 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4503 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4504 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4505 | if(uErr != QCBOR_SUCCESS) { |
| 4506 | return 15; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4507 | } |
| 4508 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM || |
| 4509 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4510 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4511 | return 16; |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4512 | } |
| 4513 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4514 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4515 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4516 | if(uErr != QCBOR_SUCCESS) { |
| 4517 | return 17; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4518 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4519 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4520 | item.val.expAndMantissa.nExponent != 9223372036854775806 || |
| 4521 | item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4522 | return 18; |
| 4523 | } |
| 4524 | |
| 4525 | uErr = QCBORDecode_Finish(&DC); |
| 4526 | if(uErr != QCBOR_SUCCESS) { |
| 4527 | return 18; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | /* Now encode some stuff and then decode it */ |
| 4531 | uint8_t pBuf[40]; |
| 4532 | QCBOREncodeContext EC; |
| 4533 | UsefulBufC Encoded; |
| 4534 | |
| 4535 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
| 4536 | QCBOREncode_OpenArray(&EC); |
| 4537 | QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000) |
| 4538 | QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN); |
| 4539 | QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX); |
| 4540 | QCBOREncode_CloseArray(&EC); |
| 4541 | QCBOREncode_Finish(&EC, &Encoded); |
| 4542 | |
| 4543 | |
| 4544 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4545 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4546 | if(uErr != QCBOR_SUCCESS) { |
| 4547 | return 100; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4548 | } |
| 4549 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4550 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4551 | if(uErr != QCBOR_SUCCESS) { |
| 4552 | return 101; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION || |
| 4556 | item.val.expAndMantissa.nExponent != 1000 || |
| 4557 | item.val.expAndMantissa.Mantissa.nInt != 999) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4558 | return 102; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4559 | } |
| 4560 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4561 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4562 | if(uErr != QCBOR_SUCCESS) { |
| 4563 | return 103; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | if(item.uDataType != QCBOR_TYPE_BIGFLOAT || |
| 4567 | item.val.expAndMantissa.nExponent != INT32_MIN || |
| 4568 | item.val.expAndMantissa.Mantissa.nInt != 100) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4569 | return 104; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4570 | } |
| 4571 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4572 | uErr = QCBORDecode_GetNext(&DC, &item); |
| 4573 | if(uErr != QCBOR_SUCCESS) { |
| 4574 | return 105; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM || |
| 4578 | item.val.expAndMantissa.nExponent != INT32_MAX || |
| 4579 | UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4580 | return 106; |
| 4581 | } |
| 4582 | |
| 4583 | |
| 4584 | int64_t nExp, nMant; |
| 4585 | UsefulBuf_MAKE_STACK_UB( MantBuf, 20); |
| 4586 | UsefulBufC Mant; |
| 4587 | bool bIsNeg; |
| 4588 | |
| 4589 | QCBORDecode_Init(&DC, |
| 4590 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), |
| 4591 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4592 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4593 | |
| 4594 | // 4([-1, 3]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4595 | QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4596 | |
| 4597 | // 4([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4598 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, |
| 4599 | &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4600 | |
| 4601 | // 4([9223372036854775807, -4759477275222530853137]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4602 | QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, |
| 4603 | MantBuf, &Mant, &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4604 | |
| 4605 | // 5([300, 100]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4606 | QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4607 | |
| 4608 | // 5([-20, 4759477275222530853136]), |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4609 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4610 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4611 | |
| 4612 | // 5([-9223372036854775807, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4613 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4614 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4615 | |
| 4616 | // 5([ 9223372036854775806, -4759477275222530853137]) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4617 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4618 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4619 | |
| 4620 | // 5([ 9223372036854775806, 9223372036854775806])] |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4621 | QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant, |
| 4622 | &bIsNeg, &nExp); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4623 | |
| 4624 | QCBORDecode_ExitArray(&DC); |
| 4625 | |
| 4626 | uErr = QCBORDecode_Finish(&DC); |
| 4627 | if(uErr != QCBOR_SUCCESS) { |
| 4628 | return 200; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | return 0; |
| 4632 | } |
| 4633 | |
| 4634 | |
| 4635 | static struct FailInput ExponentAndMantissaFailures[] = { |
| 4636 | // Exponent > INT64_MAX |
| 4637 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4638 | 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4639 | 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4640 | // Mantissa > INT64_MAX |
| 4641 | { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 4642 | 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 4643 | 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4644 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4645 | { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4646 | // End of input |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4647 | { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS}, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4648 | // bad content for big num |
| 4649 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG}, |
| 4650 | // bad content for big num |
| 4651 | { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT}, |
| 4652 | // Bad integer for exponent |
| 4653 | { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT}, |
| 4654 | // Bad integer for mantissa |
| 4655 | { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT}, |
| 4656 | // 3 items in array |
| 4657 | { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4658 | // unterminated indefinite length array |
| 4659 | { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4660 | // Empty array |
| 4661 | { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS}, |
| 4662 | // Second is not an integer |
| 4663 | { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4664 | // First is not an integer |
| 4665 | { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}, |
| 4666 | // Not an array |
| 4667 | { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA} |
| 4668 | }; |
| 4669 | |
| 4670 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 4671 | int32_t ExponentAndMantissaDecodeFailTests() |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4672 | { |
| 4673 | return ProcessFailures(ExponentAndMantissaFailures, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 4674 | C_ARRAY_COUNT(ExponentAndMantissaFailures, |
| 4675 | struct FailInput)); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4679 | |
| 4680 | |
| 4681 | |
| 4682 | /* |
| 4683 | Some basic CBOR with map and array used in a lot of tests. |
| 4684 | The map labels are all strings |
| 4685 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 4686 | { |
| 4687 | "first integer": 42, |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4688 | "an array of two strings": [ |
| 4689 | "string1", "string2" |
| 4690 | ], |
| 4691 | "map in a map": { |
| 4692 | "bytes 1": h'78787878', |
| 4693 | "bytes 2": h'79797979', |
| 4694 | "another int": 98, |
| 4695 | "text 2": "lies, damn lies and statistics" |
| 4696 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4697 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4698 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4699 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4700 | int32_t SpiffyDecodeBasicMap(UsefulBufC input) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4701 | { |
| 4702 | QCBORItem Item1, Item2, Item3; |
| 4703 | int64_t nDecodedInt1, nDecodedInt2; |
| 4704 | UsefulBufC B1, B2, S1, S2, S3; |
| 4705 | |
| 4706 | QCBORDecodeContext DCtx; |
| 4707 | QCBORError nCBORError; |
| 4708 | |
| 4709 | QCBORDecode_Init(&DCtx, input, 0); |
| 4710 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4711 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4712 | |
| 4713 | QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1); |
| 4714 | |
| 4715 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 4716 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4717 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1); |
| 4718 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2); |
| 4719 | QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4720 | QCBORDecode_ExitMap(&DCtx); |
| 4721 | |
| 4722 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 4723 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 4724 | QCBORDecode_GetNext(&DCtx, &Item2); |
| 4725 | if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4726 | return -400; |
| 4727 | } |
| 4728 | QCBORDecode_ExitArray(&DCtx); |
| 4729 | |
| 4730 | // Parse the same array again using GetText() instead of GetItem() |
| 4731 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4732 | QCBORDecode_GetTextString(&DCtx, &S2); |
| 4733 | QCBORDecode_GetTextString(&DCtx, &S3); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4734 | if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) { |
| 4735 | return 5000; |
| 4736 | } |
| 4737 | /* QCBORDecode_GetText(&DCtx, &S3); |
| 4738 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4739 | return 5001; |
| 4740 | } */ |
| 4741 | |
| 4742 | QCBORDecode_ExitArray(&DCtx); |
| 4743 | |
| 4744 | QCBORDecode_ExitMap(&DCtx); |
| 4745 | |
| 4746 | nCBORError = QCBORDecode_Finish(&DCtx); |
| 4747 | |
| 4748 | if(nCBORError) { |
| 4749 | return (int32_t)nCBORError; |
| 4750 | } |
| 4751 | |
| 4752 | if(nDecodedInt1 != 42) { |
| 4753 | return 1001; |
| 4754 | } |
| 4755 | |
| 4756 | if(nDecodedInt2 != 98) { |
| 4757 | return 1002; |
| 4758 | } |
| 4759 | |
| 4760 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4761 | UsefulBufCompareToSZ(Item1.val.string, "string1")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4762 | return 1003; |
| 4763 | } |
| 4764 | |
| 4765 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING || |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4766 | UsefulBufCompareToSZ(Item2.val.string, "string2")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4767 | return 1004; |
| 4768 | } |
| 4769 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4770 | if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 4771 | return 1005; |
| 4772 | } |
| 4773 | |
| 4774 | if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){ |
| 4775 | return 1006; |
| 4776 | } |
| 4777 | |
| 4778 | if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){ |
| 4779 | return 1007; |
| 4780 | } |
| 4781 | |
| 4782 | if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){ |
| 4783 | return 1008; |
| 4784 | } |
| 4785 | |
| 4786 | if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){ |
| 4787 | return 1009; |
| 4788 | } |
| 4789 | |
| 4790 | return 0; |
| 4791 | } |
| 4792 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4793 | /* |
| 4794 | { |
| 4795 | -75008: h'05083399', |
| 4796 | 88: [ |
| 4797 | ], |
| 4798 | 100100: { |
| 4799 | "sub1": { |
| 4800 | 10: [ |
| 4801 | 0 |
| 4802 | ], |
| 4803 | -75009: h'A46823990001', |
| 4804 | 100100: { |
| 4805 | "json": "{ \"ueid\", \"xyz\"}", |
| 4806 | "subsub": { |
| 4807 | 100002: h'141813191001' |
| 4808 | } |
| 4809 | } |
| 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | */ |
| 4814 | |
| 4815 | static const uint8_t spNestedCBOR[] = { |
| 4816 | 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00, |
| 4817 | 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01, |
| 4818 | 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64, |
| 4819 | 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22, |
| 4820 | 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01, |
| 4821 | 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, 0x01 |
| 4822 | }; |
| 4823 | |
| 4824 | /* Get item in multi-level nesting in spNestedCBOR */ |
| 4825 | static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx) |
| 4826 | { |
| 4827 | UsefulBufC String; |
| 4828 | |
| 4829 | uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01}; |
| 4830 | const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes); |
| 4831 | |
| 4832 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4833 | QCBORDecode_EnterMap(pDCtx, NULL); |
| 4834 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4835 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub"); |
| 4836 | QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String); |
| 4837 | if(QCBORDecode_GetError(pDCtx)) { |
| 4838 | return 4001; |
| 4839 | } |
| 4840 | if(UsefulBuf_Compare(String, test_oemid)) { |
| 4841 | return 4002; |
| 4842 | } |
| 4843 | QCBORDecode_ExitMap(pDCtx); |
| 4844 | QCBORDecode_ExitMap(pDCtx); |
| 4845 | QCBORDecode_ExitMap(pDCtx); |
| 4846 | QCBORDecode_ExitMap(pDCtx); |
| 4847 | |
| 4848 | return 0; |
| 4849 | } |
| 4850 | |
| 4851 | /* Iterations on the zero-length array in spNestedCBOR */ |
| 4852 | static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx) |
| 4853 | { |
| 4854 | QCBORItem Item; |
| 4855 | QCBORError uErr; |
| 4856 | |
| 4857 | QCBORDecode_EnterArrayFromMapN(pDCtx, 88); |
| 4858 | for(int x = 0; x < 20; x++) { |
| 4859 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 4860 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4861 | return 4100; |
| 4862 | |
| 4863 | } |
| 4864 | } |
| 4865 | QCBORDecode_ExitArray(pDCtx); |
| 4866 | if(QCBORDecode_GetError(pDCtx)) { |
| 4867 | return 4101; |
| 4868 | } |
| 4869 | |
| 4870 | return 0; |
| 4871 | } |
| 4872 | |
| 4873 | /* Various iterations on the array that contains a zero in spNestedCBOR */ |
| 4874 | static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx) |
| 4875 | { |
| 4876 | QCBORError uErr; |
| 4877 | |
| 4878 | QCBORDecode_EnterMapFromMapN(pDCtx, 100100); |
| 4879 | QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1"); |
| 4880 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 4881 | int64_t nInt = 99; |
| 4882 | QCBORDecode_GetInt64(pDCtx, &nInt); |
| 4883 | if(nInt != 0) { |
| 4884 | return 4200; |
| 4885 | } |
| 4886 | for(int x = 0; x < 20; x++) { |
| 4887 | QCBORItem Item; |
| 4888 | uErr = QCBORDecode_GetNext(pDCtx, &Item); |
| 4889 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4890 | return 4201; |
| 4891 | |
| 4892 | } |
| 4893 | } |
| 4894 | QCBORDecode_ExitArray(pDCtx); |
| 4895 | if(QCBORDecode_GetAndResetError(pDCtx)) { |
| 4896 | return 4202; |
| 4897 | } |
| 4898 | QCBORDecode_EnterArrayFromMapN(pDCtx, 10); |
| 4899 | UsefulBufC dD; |
| 4900 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 4901 | if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4902 | return 4203; |
| 4903 | } |
| 4904 | for(int x = 0; x < 20; x++) { |
| 4905 | QCBORDecode_GetByteString(pDCtx, &dD); |
| 4906 | uErr = QCBORDecode_GetAndResetError(pDCtx); |
| 4907 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 4908 | return 4204; |
| 4909 | } |
| 4910 | } |
| 4911 | QCBORDecode_ExitArray(pDCtx); |
| 4912 | QCBORDecode_ExitMap(pDCtx); |
| 4913 | QCBORDecode_ExitMap(pDCtx); |
| 4914 | |
| 4915 | return 0; |
| 4916 | } |
| 4917 | |
| 4918 | /* Repeatedly enter and exit maps and arrays, go off the end of maps |
| 4919 | and arrays and such. */ |
| 4920 | static int32_t DecodeNestedIterate() |
| 4921 | { |
| 4922 | QCBORDecodeContext DCtx; |
| 4923 | int32_t nReturn; |
| 4924 | QCBORError uErr; |
| 4925 | |
| 4926 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0); |
| 4927 | QCBORDecode_EnterMap(&DCtx, NULL); |
| 4928 | |
| 4929 | for(int j = 0; j < 5; j++) { |
| 4930 | for(int i = 0; i < 20; i++) { |
| 4931 | nReturn = DecodeNestedGetSubSub(&DCtx); |
| 4932 | if(nReturn) { |
| 4933 | return nReturn; |
| 4934 | } |
| 4935 | } |
| 4936 | |
| 4937 | for(int i = 0; i < 20; i++) { |
| 4938 | nReturn = DecodeNestedGetEmpty(&DCtx); |
| 4939 | if(nReturn ) { |
| 4940 | return nReturn; |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | for(int i = 0; i < 20; i++) { |
| 4945 | nReturn = DecodeNestedGetZero(&DCtx); |
| 4946 | if(nReturn ) { |
| 4947 | return nReturn; |
| 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | QCBORDecode_ExitMap(&DCtx); |
| 4953 | uErr = QCBORDecode_Finish(&DCtx); |
| 4954 | if(uErr) { |
| 4955 | return (int32_t)uErr + 4100; |
| 4956 | } |
| 4957 | |
| 4958 | return 0; |
| 4959 | } |
| 4960 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4961 | |
| 4962 | /* |
| 4963 | [23, |
| 4964 | 6000, |
| 4965 | h'67616C6163746963', |
| 4966 | h'686176656E20746F6B656E' |
| 4967 | ] |
| 4968 | */ |
| 4969 | static const uint8_t spSimpleArray[] = { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4970 | 0x84, |
| 4971 | 0x17, |
| 4972 | 0x19, 0x17, 0x70, |
| 4973 | 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, |
| 4974 | 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4975 | |
| 4976 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4977 | static const uint8_t spEmptyMap[] = {0xa0}; |
| 4978 | |
| 4979 | static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff}; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4980 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4981 | static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00}; |
| 4982 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 4983 | /* |
| 4984 | { |
| 4985 | 0: [], |
| 4986 | 9: [ |
| 4987 | [], |
| 4988 | [] |
| 4989 | ], |
| 4990 | 8: { |
| 4991 | 1: [], |
| 4992 | 2: {}, |
| 4993 | 3: [] |
| 4994 | }, |
| 4995 | 4: {}, |
| 4996 | 5: [], |
| 4997 | 6: [ |
| 4998 | [], |
| 4999 | [] |
| 5000 | ] |
Laurence Lundblade | 4408063 | 2020-08-06 21:43:50 -0700 | [diff] [blame] | 5001 | } |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5002 | */ |
Laurence Lundblade | 4408063 | 2020-08-06 21:43:50 -0700 | [diff] [blame] | 5003 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5004 | static const uint8_t spMapOfEmpty[] = { |
| 5005 | 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01, |
| 5006 | 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff, |
| 5007 | 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff}; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5008 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5009 | /* |
| 5010 | Too many tags |
| 5011 | Invalid tag content |
| 5012 | Duplicate label |
| 5013 | Integer overflow |
| 5014 | Date overflow |
| 5015 | |
| 5016 | {1: 224(225(226(227(4(0))))), |
| 5017 | 2: 1(h''), |
| 5018 | 3: -18446744073709551616, |
| 5019 | 4: 1(1.0e+300), |
| 5020 | 5: 0, 8: 8} |
| 5021 | */ |
| 5022 | static const uint8_t spRecoverableMapErrors[] = { |
| 5023 | 0xbf, |
| 5024 | 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00, |
| 5025 | 0x02, 0xc1, 0x40, |
| 5026 | 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5027 | 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c, |
| 5028 | 0x05, 0x00, |
| 5029 | 0x05, 0x00, |
| 5030 | 0x08, 0x08, |
| 5031 | 0xff |
| 5032 | }; |
| 5033 | |
| 5034 | // Bad break |
| 5035 | static const uint8_t spUnRecoverableMapError1[] = { |
| 5036 | 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5037 | }; |
| 5038 | |
| 5039 | // No more items |
| 5040 | static const uint8_t spUnRecoverableMapError2[] = { |
| 5041 | 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00 |
| 5042 | }; |
| 5043 | |
| 5044 | // Hit end because string is too long |
| 5045 | static const uint8_t spUnRecoverableMapError3[] = { |
| 5046 | 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff |
| 5047 | }; |
| 5048 | |
| 5049 | // Hit end because string is too long |
| 5050 | static const uint8_t spUnRecoverableMapError4[] = { |
| 5051 | 0xbf, |
| 5052 | 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5053 | 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, |
| 5054 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5055 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 5056 | 0xff |
| 5057 | }; |
| 5058 | |
| 5059 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5060 | int32_t EnterMapTest() |
| 5061 | { |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5062 | QCBORItem Item1; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5063 | QCBORItem ArrayItem; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5064 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5065 | int32_t nReturn; |
| 5066 | QCBORError uErr; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5067 | |
| 5068 | |
| 5069 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5070 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5071 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 5072 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5073 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 0 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5074 | QCBORDecode_ExitArray(&DCtx); |
| 5075 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5076 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 9 |
| 5077 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5078 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5079 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5080 | QCBORDecode_ExitArray(&DCtx); |
| 5081 | QCBORDecode_ExitArray(&DCtx); |
| 5082 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5083 | QCBORDecode_EnterMap(&DCtx, NULL); // Label 8 |
| 5084 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5085 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5086 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5087 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5088 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5089 | QCBORDecode_ExitArray(&DCtx); |
| 5090 | QCBORDecode_ExitMap(&DCtx); |
| 5091 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5092 | QCBORDecode_EnterMap(&DCtx, NULL); // Label4 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5093 | QCBORDecode_ExitMap(&DCtx); |
| 5094 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5095 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 5 |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5096 | QCBORDecode_ExitArray(&DCtx); |
| 5097 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5098 | QCBORDecode_EnterArray(&DCtx, NULL); // Label 6 |
| 5099 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5100 | QCBORDecode_ExitArray(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5101 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5102 | QCBORDecode_ExitArray(&DCtx); |
| 5103 | QCBORDecode_ExitArray(&DCtx); |
| 5104 | |
| 5105 | QCBORDecode_ExitMap(&DCtx); |
| 5106 | |
| 5107 | uErr = QCBORDecode_Finish(&DCtx); |
| 5108 | if(uErr != QCBOR_SUCCESS){ |
| 5109 | return 3011; |
| 5110 | } |
| 5111 | |
| 5112 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5113 | (void)pValidMapIndefEncoded; |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5114 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 5115 | if(nReturn) { |
| 5116 | return nReturn + 20000; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5117 | } |
| 5118 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5119 | nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded)); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5120 | if(nReturn) { |
| 5121 | return nReturn; |
| 5122 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 5123 | |
Laurence Lundblade | 8ffdb74 | 2020-05-07 02:49:18 -0700 | [diff] [blame] | 5124 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5125 | |
Laurence Lundblade | 2f467f9 | 2020-10-09 17:50:11 -0700 | [diff] [blame] | 5126 | // These tests confirm the cursor is at the right place after entering |
| 5127 | // a map or array |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5128 | const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5129 | |
| 5130 | // Confirm cursor is at right place |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5131 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5132 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5133 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5134 | if(Item1.uDataType != QCBOR_TYPE_INT64) { |
| 5135 | return 2001; |
| 5136 | } |
| 5137 | |
| 5138 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5139 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5140 | QCBORDecode_VGetNext(&DCtx, &Item1); |
| 5141 | QCBORDecode_VGetNext(&DCtx, &Item1); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5142 | QCBORDecode_EnterArray(&DCtx, &ArrayItem); |
| 5143 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5144 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5145 | UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) { |
| 5146 | return 2051; |
| 5147 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5148 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5149 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 5150 | return 2002; |
| 5151 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5152 | QCBORDecode_ExitArray(&DCtx); |
| 5153 | QCBORDecode_EnterMap(&DCtx, &ArrayItem); |
| 5154 | if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 5155 | UsefulBuf_Compare(ArrayItem.label.string, |
| 5156 | UsefulBuf_FROM_SZ_LITERAL("map in a map"))) { |
| 5157 | return 2052; |
| 5158 | } |
| 5159 | |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5160 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5161 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5162 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5163 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5164 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5165 | QCBORDecode_GetNext(&DCtx, &Item1); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5166 | QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map"); |
| 5167 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5168 | if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 5169 | return 2003; |
| 5170 | } |
| 5171 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5172 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5173 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5174 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5175 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5176 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5177 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5178 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5179 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5180 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5181 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5182 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5183 | if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 5184 | return 2004; |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5185 | } |
| 5186 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5187 | QCBORDecode_Init(&DCtx, ValidEncodedMap, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5188 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 5189 | QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings"); |
| 5190 | QCBORDecode_ExitArray(&DCtx); |
| 5191 | QCBORDecode_GetNext(&DCtx, &Item1); |
| 5192 | if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) { |
| 5193 | return 2006; |
| 5194 | } |
| 5195 | QCBORDecode_ExitMap(&DCtx); |
| 5196 | if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5197 | return 2007; |
| 5198 | } |
| 5199 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5200 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5201 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5202 | int64_t nDecodedInt2; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5203 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5204 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5205 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5206 | return 2008; |
| 5207 | } |
| 5208 | UsefulBufC String; |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5209 | QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 5210 | if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5211 | return 2009; |
| 5212 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 5213 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5214 | |
| 5215 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5216 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5217 | // This will fail because the map is empty. |
| 5218 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5219 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5220 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5221 | return 2010; |
| 5222 | } |
| 5223 | QCBORDecode_ExitMap(&DCtx); |
| 5224 | uErr = QCBORDecode_Finish(&DCtx); |
| 5225 | if(uErr != QCBOR_SUCCESS){ |
| 5226 | return 2011; |
| 5227 | } |
| 5228 | |
| 5229 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5230 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5231 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5232 | // This will fail because the map is empty. |
| 5233 | QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2); |
| 5234 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 5235 | if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5236 | return 2012; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5237 | } |
| 5238 | QCBORDecode_ExitMap(&DCtx); |
| 5239 | uErr = QCBORDecode_Finish(&DCtx); |
| 5240 | if(uErr != QCBOR_SUCCESS){ |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 5241 | return 2013; |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 5242 | } |
| 5243 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5244 | |
| 5245 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5246 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 5247 | QCBORDecode_GetByteString(&DCtx, &String); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5248 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5249 | QCBORDecode_ExitMap(&DCtx); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5250 | QCBORDecode_EnterArray(&DCtx, NULL); |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5251 | QCBORDecode_ExitArray(&DCtx); |
| 5252 | QCBORDecode_GetInt64(&DCtx, &nDecodedInt2); |
| 5253 | QCBORDecode_ExitArray(&DCtx); |
| 5254 | uErr = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5255 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 5256 | return 2014; |
| 5257 | } |
| 5258 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5259 | int64_t nInt; |
| 5260 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5261 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5262 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5263 | uErr = QCBORDecode_GetError(&DCtx); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5264 | if(uErr != QCBOR_ERR_TOO_MANY_TAGS) { |
| 5265 | return 2021; |
| 5266 | } |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 5267 | if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) { |
| 5268 | return 2121; |
| 5269 | } |
| 5270 | (void)QCBORDecode_GetAndResetError(&DCtx); |
| 5271 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5272 | |
| 5273 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 5274 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5275 | if(uErr != QCBOR_ERR_BAD_OPT_TAG) { |
| 5276 | return 2022; |
| 5277 | } |
| 5278 | |
| 5279 | QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt); |
| 5280 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5281 | if(uErr != QCBOR_ERR_INT_OVERFLOW) { |
| 5282 | return 2023; |
| 5283 | } |
| 5284 | |
| 5285 | QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt); |
| 5286 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5287 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5288 | if(uErr != QCBOR_ERR_DATE_OVERFLOW) { |
| 5289 | return 2024; |
| 5290 | } |
| 5291 | #else |
| 5292 | if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) { |
| 5293 | return 2027; |
| 5294 | } |
| 5295 | #endif |
| 5296 | |
| 5297 | QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt); |
| 5298 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5299 | if(uErr != QCBOR_ERR_DUPLICATE_LABEL) { |
| 5300 | return 2025; |
| 5301 | } |
| 5302 | |
| 5303 | QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt); |
| 5304 | |
| 5305 | QCBORDecode_ExitMap(&DCtx); |
| 5306 | uErr = QCBORDecode_Finish(&DCtx); |
| 5307 | if(uErr != QCBOR_SUCCESS) { |
| 5308 | return 2026; |
| 5309 | } |
| 5310 | |
| 5311 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5312 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5313 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5314 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5315 | if(uErr != QCBOR_ERR_BAD_BREAK) { |
| 5316 | return 2030; |
| 5317 | } |
| 5318 | |
| 5319 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5320 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5321 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5322 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5323 | if(uErr != QCBOR_ERR_NO_MORE_ITEMS) { |
| 5324 | return 2031; |
| 5325 | } |
| 5326 | |
| 5327 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5328 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5329 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5330 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5331 | if(uErr != QCBOR_ERR_HIT_END) { |
| 5332 | return 2032; |
| 5333 | } |
| 5334 | |
| 5335 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 5336 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 5337 | QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt); |
| 5338 | uErr = QCBORDecode_GetAndResetError(&DCtx); |
| 5339 | if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) { |
| 5340 | return 2033; |
| 5341 | } |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 5342 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 5343 | nReturn = DecodeNestedIterate(); |
| 5344 | |
| 5345 | return nReturn; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5349 | struct NumberConversion { |
| 5350 | char *szDescription; |
| 5351 | UsefulBufC CBOR; |
| 5352 | int64_t nConvertedToInt64; |
| 5353 | QCBORError uErrorInt64; |
| 5354 | uint64_t uConvertToUInt64; |
| 5355 | QCBORError uErrorUint64; |
| 5356 | double dConvertToDouble; |
| 5357 | QCBORError uErrorDouble; |
| 5358 | }; |
| 5359 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5360 | static const struct NumberConversion NumberConversions[] = { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5361 | { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5362 | "too large to fit into int64_t", |
| 5363 | {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10}, |
| 5364 | 0, |
| 5365 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5366 | 0, |
| 5367 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5368 | ((double)INT64_MIN) + 1 , |
| 5369 | QCBOR_SUCCESS |
| 5370 | }, |
| 5371 | { |
| 5372 | "largest negative int that fits in int64_t", |
| 5373 | {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10}, |
| 5374 | INT64_MIN, |
| 5375 | QCBOR_SUCCESS, |
| 5376 | 0, |
| 5377 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5378 | (double)INT64_MIN, |
| 5379 | QCBOR_SUCCESS |
| 5380 | }, |
| 5381 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5382 | "negative bignum -1", |
| 5383 | {(uint8_t[]){0xc3, 0x41, 0x00}, 3}, |
| 5384 | -1, |
| 5385 | QCBOR_SUCCESS, |
| 5386 | 0, |
| 5387 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5388 | -1.0, |
| 5389 | QCBOR_SUCCESS |
| 5390 | }, |
| 5391 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5392 | "Decimal Fraction with positive bignum 257 * 10e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5393 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5394 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5395 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5396 | 257000, |
| 5397 | QCBOR_SUCCESS, |
| 5398 | 257000, |
| 5399 | QCBOR_SUCCESS, |
| 5400 | 257000.0, |
| 5401 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5402 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5403 | 0, |
| 5404 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5405 | 0, |
| 5406 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5407 | 0.0, |
| 5408 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5409 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5410 | }, |
| 5411 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5412 | "bigfloat with negative bignum -258 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5413 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5414 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5415 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5416 | -2064, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5417 | QCBOR_SUCCESS, |
| 5418 | 0, |
| 5419 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5420 | -2064.0, |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5421 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5422 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5423 | 0, |
| 5424 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5425 | 0, |
| 5426 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5427 | 0.0, |
| 5428 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5429 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5430 | }, |
| 5431 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5432 | "bigfloat with positive bignum 257 * 2e3", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5433 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5434 | 0xC2, 0x42, 0x01, 0x01}, 15}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5435 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5436 | 2056, |
| 5437 | QCBOR_SUCCESS, |
| 5438 | 2056, |
| 5439 | QCBOR_SUCCESS, |
| 5440 | 2056.0, |
| 5441 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5442 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5443 | 0, |
| 5444 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5445 | 0, |
| 5446 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5447 | 0.0, |
| 5448 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5449 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5450 | }, |
| 5451 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5452 | "negative bignum 0xc349010000000000000000 -18446744073709551617", |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5453 | {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11}, |
| 5454 | 0, |
| 5455 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5456 | 0, |
| 5457 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5458 | -18446744073709551617.0, |
| 5459 | QCBOR_SUCCESS |
| 5460 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5461 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5462 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5463 | "Positive bignum 0x01020304 indefinite length string", |
| 5464 | {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10}, |
| 5465 | 0x01020304, |
| 5466 | QCBOR_SUCCESS, |
| 5467 | 0x01020304, |
| 5468 | QCBOR_SUCCESS, |
| 5469 | 16909060.0, |
| 5470 | QCBOR_SUCCESS |
| 5471 | }, |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5472 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5473 | { |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 5474 | "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]", |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5475 | {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 5476 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5477 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5478 | 0, |
| 5479 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5480 | 0, |
| 5481 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5482 | -INFINITY, |
| 5483 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5484 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5485 | 0, |
| 5486 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5487 | 0, |
| 5488 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5489 | 0.0, |
| 5490 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5491 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5492 | }, |
| 5493 | { |
| 5494 | "big float [9223372036854775806, 9223372036854775806]", |
| 5495 | {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 5496 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5497 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5498 | 0, |
| 5499 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5500 | 0, |
| 5501 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5502 | INFINITY, |
| 5503 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5504 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5505 | 0, |
| 5506 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5507 | 0, |
| 5508 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5509 | 0.0, |
| 5510 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5511 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5512 | }, |
| 5513 | { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5514 | "Big float 3 * 2^^2", |
| 5515 | {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5516 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5517 | 12, |
| 5518 | QCBOR_SUCCESS, |
| 5519 | 12, |
| 5520 | QCBOR_SUCCESS, |
| 5521 | 12.0, |
| 5522 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5523 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5524 | 0, |
| 5525 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5526 | 0, |
| 5527 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5528 | 0.0, |
| 5529 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5530 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5531 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5532 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5533 | "Positive integer 18446744073709551615", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5534 | {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9}, |
| 5535 | 0, |
| 5536 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5537 | 18446744073709551615ULL, |
| 5538 | QCBOR_SUCCESS, |
| 5539 | 18446744073709551615.0, |
| 5540 | QCBOR_SUCCESS |
| 5541 | }, |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5542 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5543 | "Positive bignum 0xffff", |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5544 | {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4}, |
| 5545 | 65536-1, |
| 5546 | QCBOR_SUCCESS, |
| 5547 | 0xffff, |
| 5548 | QCBOR_SUCCESS, |
| 5549 | 65535.0, |
| 5550 | QCBOR_SUCCESS |
| 5551 | }, |
| 5552 | { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5553 | "Postive integer 0", |
| 5554 | {(uint8_t[]){0x0}, 1}, |
| 5555 | 0LL, |
| 5556 | QCBOR_SUCCESS, |
| 5557 | 0ULL, |
| 5558 | QCBOR_SUCCESS, |
| 5559 | 0.0, |
| 5560 | QCBOR_SUCCESS |
| 5561 | }, |
| 5562 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5563 | "Negative integer -18446744073709551616", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5564 | {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9}, |
| 5565 | -9223372036854775807-1, // INT64_MIN |
| 5566 | QCBOR_SUCCESS, |
| 5567 | 0ULL, |
| 5568 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5569 | -9223372036854775808.0, |
| 5570 | QCBOR_SUCCESS |
| 5571 | }, |
| 5572 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5573 | "Double Floating point value 100.3", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5574 | {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5575 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5576 | 100L, |
| 5577 | QCBOR_SUCCESS, |
| 5578 | 100ULL, |
| 5579 | QCBOR_SUCCESS, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5580 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5581 | 0, |
| 5582 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5583 | 0, |
| 5584 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5585 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5586 | 100.3, |
| 5587 | QCBOR_SUCCESS |
| 5588 | }, |
| 5589 | { |
| 5590 | "Floating point value NaN 0xfa7fc00000", |
| 5591 | {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5592 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5593 | 0, |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5594 | QCBOR_ERR_FLOAT_EXCEPTION, |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5595 | 0, |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5596 | QCBOR_ERR_FLOAT_EXCEPTION, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5597 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5598 | 0, |
| 5599 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5600 | 0, |
| 5601 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5602 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5603 | NAN, |
| 5604 | QCBOR_SUCCESS |
| 5605 | }, |
| 5606 | { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5607 | "half-precision Floating point value -4", |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5608 | {(uint8_t[]){0xf9, 0xc4, 0x00}, 3}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5609 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 5610 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5611 | // Normal case with all enabled. |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5612 | -4, |
| 5613 | QCBOR_SUCCESS, |
| 5614 | 0, |
| 5615 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5616 | -4.0, |
| 5617 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5618 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5619 | // Float HW disabled |
| 5620 | -4, |
| 5621 | QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer |
| 5622 | 0, |
| 5623 | QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer |
| 5624 | -4.0, |
| 5625 | QCBOR_SUCCESS // Uses ieee754.h to conver, not HW |
| 5626 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5627 | #else |
| 5628 | // Half-precision disabled |
| 5629 | -4, |
| 5630 | QCBOR_ERR_HALF_PRECISION_DISABLED, |
| 5631 | 0, |
| 5632 | QCBOR_ERR_HALF_PRECISION_DISABLED, |
| 5633 | -4.0, |
| 5634 | QCBOR_ERR_HALF_PRECISION_DISABLED |
| 5635 | #endif |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5636 | }, |
| 5637 | { |
| 5638 | "Decimal fraction 3/10", |
| 5639 | {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4}, |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5640 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5641 | 0, |
| 5642 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5643 | 0, |
| 5644 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5645 | 0.30000000000000004, |
| 5646 | QCBOR_SUCCESS |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5647 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5648 | 0, |
| 5649 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5650 | 0, |
| 5651 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5652 | 0.0, |
| 5653 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5654 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5655 | }, |
| 5656 | { |
| 5657 | "+inifinity", |
| 5658 | {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5}, |
| 5659 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5660 | 0, |
| 5661 | QCBOR_ERR_FLOAT_EXCEPTION, |
| 5662 | 0, |
| 5663 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5664 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5665 | 0, |
| 5666 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5667 | 0, |
| 5668 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5669 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5670 | INFINITY, |
| 5671 | QCBOR_SUCCESS |
| 5672 | }, |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5673 | |
| 5674 | { |
| 5675 | "extreme pos bignum", |
| 5676 | {(uint8_t[]){0xc2, 0x59, 0x01, 0x90, |
| 5677 | // 50 rows of 8 is 400 digits. |
| 5678 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5679 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5680 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5681 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5682 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5683 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5684 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5685 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5686 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5687 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5688 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5689 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5690 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5691 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5692 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5693 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5694 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5695 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5696 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5697 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5698 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5699 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5700 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5701 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5702 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5703 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5704 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5705 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5706 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5707 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5708 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5709 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5710 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5711 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5712 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5713 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5714 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5715 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5716 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5717 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5718 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5719 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5720 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5721 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5722 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5723 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5724 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5725 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5726 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5727 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
| 5728 | 404}, |
| 5729 | 0, |
| 5730 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5731 | 0, |
| 5732 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5733 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5734 | INFINITY, |
| 5735 | QCBOR_SUCCESS |
| 5736 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5737 | 0, |
| 5738 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5739 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5740 | }, |
| 5741 | |
| 5742 | { |
| 5743 | "extreme neg bignum", |
| 5744 | {(uint8_t[]){0xc3, 0x59, 0x01, 0x90, |
| 5745 | // 50 rows of 8 is 400 digits. |
| 5746 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5747 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5748 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5749 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5750 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5751 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5752 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5753 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5754 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5755 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5756 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5757 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5758 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5759 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5760 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5761 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5762 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5763 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5764 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5765 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5766 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5767 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5768 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5769 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5770 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5771 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5772 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5773 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5774 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5775 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5776 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5777 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5778 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5779 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5780 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5781 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5782 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5783 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5784 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5785 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5786 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5787 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5788 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5789 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5790 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5791 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5792 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5793 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5794 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, |
| 5795 | 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, |
| 5796 | 404}, |
| 5797 | 0, |
| 5798 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5799 | 0, |
| 5800 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5801 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5802 | -INFINITY, |
| 5803 | QCBOR_SUCCESS |
| 5804 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5805 | 0, |
| 5806 | QCBOR_ERR_HW_FLOAT_DISABLED, |
| 5807 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5808 | }, |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5809 | |
| 5810 | { |
| 5811 | "big float underflow [9223372036854775806, -9223372036854775806]", |
| 5812 | {(uint8_t[]){ |
| 5813 | 0xC5, 0x82, |
| 5814 | 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 5815 | 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20}, |
| 5816 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 5817 | 0, |
| 5818 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5819 | 0, |
| 5820 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5821 | 0, |
| 5822 | QCBOR_SUCCESS |
| 5823 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5824 | 0, |
| 5825 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5826 | 0, |
| 5827 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5828 | 0.0, |
| 5829 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5830 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5831 | }, |
| 5832 | |
| 5833 | { |
| 5834 | "bigfloat that evaluates to -INFINITY", |
| 5835 | {(uint8_t[]){ |
| 5836 | 0xC5, 0x82, |
| 5837 | 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 5838 | 0xC3, 0x42, 0x01, 0x01}, 15}, |
| 5839 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 5840 | 0, |
| 5841 | QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, |
| 5842 | 0, |
| 5843 | QCBOR_ERR_NUMBER_SIGN_CONVERSION, |
| 5844 | -INFINITY, |
| 5845 | QCBOR_SUCCESS |
| 5846 | #else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5847 | 0, |
| 5848 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5849 | 0, |
| 5850 | QCBOR_ERR_UNEXPECTED_TYPE, |
| 5851 | 0.0, |
| 5852 | QCBOR_ERR_UNEXPECTED_TYPE |
| 5853 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/ |
| 5854 | }, |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5855 | }; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5856 | |
| 5857 | |
| 5858 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5859 | |
| 5860 | static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool) |
| 5861 | { |
| 5862 | QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL); |
| 5863 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 5864 | if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) { |
| 5865 | return 1; |
| 5866 | } |
| 5867 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5868 | (void)Pool; |
| 5869 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 5870 | return 0; |
| 5871 | } |
| 5872 | |
| 5873 | |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5874 | int32_t IntegerConvertTest() |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5875 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5876 | const int nNumTests = C_ARRAY_COUNT(NumberConversions, |
| 5877 | struct NumberConversion); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5878 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5879 | for(int nIndex = 0; nIndex < nNumTests; nIndex++) { |
| 5880 | const struct NumberConversion *pF = &NumberConversions[nIndex]; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5881 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5882 | // Set up the decoding context including a memory pool so that |
| 5883 | // indefinite length items can be checked |
| 5884 | QCBORDecodeContext DCtx; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5885 | UsefulBuf_MAKE_STACK_UB(Pool, 100); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5886 | |
| 5887 | /* ----- test conversion to int64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5888 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 5889 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | int64_t nInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5893 | QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5894 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5895 | return (int32_t)(2000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5896 | } |
| 5897 | if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5898 | return (int32_t)(3000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5899 | } |
| 5900 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5901 | /* ----- test conversion to uint64_t ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5902 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 5903 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5904 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5905 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5906 | uint64_t uInt; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5907 | QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5908 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5909 | return (int32_t)(4000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5910 | } |
| 5911 | if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5912 | return (int32_t)(5000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5913 | } |
| 5914 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5915 | /* ----- test conversion to double ------ */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 5916 | if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) { |
| 5917 | return (int32_t)(3333+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5918 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5919 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5920 | double d; |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5921 | QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5922 | if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5923 | return (int32_t)(6000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5924 | } |
| 5925 | if(pF->uErrorDouble == QCBOR_SUCCESS) { |
| 5926 | if(isnan(pF->dConvertToDouble)) { |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 5927 | // NaN's can't be compared for equality. A NaN is |
| 5928 | // never equal to anything including another NaN |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5929 | if(!isnan(d)) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5930 | return (int32_t)(7000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5931 | } |
| 5932 | } else { |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5933 | if(pF->dConvertToDouble != d) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5934 | return (int32_t)(8000+nIndex); |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5935 | } |
| 5936 | } |
| 5937 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5938 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | return 0; |
| 5942 | } |
| 5943 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5944 | |
Laurence Lundblade | 97c61bf | 2020-05-02 11:24:06 -0700 | [diff] [blame] | 5945 | |
| 5946 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 5947 | int32_t CBORSequenceDecodeTests(void) |
| 5948 | { |
| 5949 | QCBORDecodeContext DCtx; |
| 5950 | QCBORItem Item; |
| 5951 | QCBORError uCBORError; |
| 5952 | |
| 5953 | // --- Test a sequence with extra bytes --- |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5954 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 5955 | // The input for the date test happens to be a sequence so it |
| 5956 | // is reused. It is a sequence because it doesn't start as |
| 5957 | // an array or map. |
| 5958 | QCBORDecode_Init(&DCtx, |
| 5959 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), |
| 5960 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5961 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 5962 | // Get the first item |
| 5963 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 5964 | if(uCBORError != QCBOR_SUCCESS) { |
| 5965 | return 1; |
| 5966 | } |
| 5967 | if(Item.uDataType != QCBOR_TYPE_DATE_STRING) { |
| 5968 | return 2; |
| 5969 | } |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5970 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 5971 | // Get a second item |
| 5972 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5973 | if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) { |
| 5974 | return 66; |
| 5975 | } |
| 5976 | |
| 5977 | // Get a third item |
| 5978 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 5979 | if(uCBORError != QCBOR_SUCCESS) { |
| 5980 | return 2; |
| 5981 | } |
| 5982 | if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 5983 | return 3; |
| 5984 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5985 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 5986 | // A sequence can have stuff at the end that may |
| 5987 | // or may not be valid CBOR. The protocol decoder knows |
| 5988 | // when to stop by definition of the protocol, not |
| 5989 | // when the top-level map or array is ended. |
| 5990 | // Finish still has to be called to know that |
| 5991 | // maps and arrays (if there were any) were closed |
| 5992 | // off correctly. When called like this it |
| 5993 | // must return the error QCBOR_ERR_EXTRA_BYTES. |
| 5994 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 5995 | if(uCBORError != QCBOR_ERR_EXTRA_BYTES) { |
| 5996 | return 4; |
| 5997 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5998 | |
| 5999 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6000 | // --- Test an empty input ---- |
| 6001 | uint8_t empty[1]; |
| 6002 | UsefulBufC Empty = {empty, 0}; |
| 6003 | QCBORDecode_Init(&DCtx, |
| 6004 | Empty, |
| 6005 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6006 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6007 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6008 | if(uCBORError != QCBOR_SUCCESS) { |
| 6009 | return 5; |
| 6010 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6011 | |
| 6012 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6013 | // --- Sequence with unclosed indefinite length array --- |
| 6014 | static const uint8_t xx[] = {0x01, 0x9f, 0x02}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6015 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6016 | QCBORDecode_Init(&DCtx, |
| 6017 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx), |
| 6018 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6019 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6020 | // Get the first item |
| 6021 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6022 | if(uCBORError != QCBOR_SUCCESS) { |
| 6023 | return 7; |
| 6024 | } |
| 6025 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6026 | return 8; |
| 6027 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6028 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6029 | // Get a second item |
| 6030 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6031 | if(uCBORError != QCBOR_SUCCESS) { |
| 6032 | return 9; |
| 6033 | } |
| 6034 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6035 | return 10; |
| 6036 | } |
| 6037 | |
| 6038 | // Try to finish before consuming all bytes to confirm |
| 6039 | // that the still-open error is returned. |
| 6040 | uCBORError = QCBORDecode_Finish(&DCtx); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6041 | if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) { |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6042 | return 11; |
| 6043 | } |
| 6044 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6045 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6046 | // --- Sequence with a closed indefinite length array --- |
| 6047 | static const uint8_t yy[] = {0x01, 0x9f, 0xff}; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6048 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6049 | QCBORDecode_Init(&DCtx, |
| 6050 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy), |
| 6051 | QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6052 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6053 | // Get the first item |
| 6054 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6055 | if(uCBORError != QCBOR_SUCCESS) { |
| 6056 | return 12; |
| 6057 | } |
| 6058 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 6059 | return 13; |
| 6060 | } |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6061 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6062 | // Get a second item |
| 6063 | uCBORError = QCBORDecode_GetNext(&DCtx, &Item); |
| 6064 | if(uCBORError != QCBOR_SUCCESS) { |
| 6065 | return 14; |
| 6066 | } |
| 6067 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 6068 | return 15; |
| 6069 | } |
| 6070 | |
| 6071 | // Try to finish before consuming all bytes to confirm |
| 6072 | // that the still-open error is returned. |
| 6073 | uCBORError = QCBORDecode_Finish(&DCtx); |
| 6074 | if(uCBORError != QCBOR_SUCCESS) { |
| 6075 | return 16; |
| 6076 | } |
| 6077 | |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 6078 | |
Laurence Lundblade | e355342 | 2020-05-02 11:11:17 -0700 | [diff] [blame] | 6079 | return 0; |
| 6080 | } |
| 6081 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6082 | |
Laurence Lundblade | 70ecead | 2020-06-15 19:40:06 -0700 | [diff] [blame] | 6083 | |
Laurence Lundblade | e15326f | 2020-06-15 15:50:23 -0700 | [diff] [blame] | 6084 | int32_t IntToTests() |
| 6085 | { |
| 6086 | int nErrCode; |
| 6087 | int32_t n32; |
| 6088 | int16_t n16; |
| 6089 | int8_t n8; |
| 6090 | uint32_t u32; |
| 6091 | uint16_t u16; |
| 6092 | uint8_t u8; |
| 6093 | uint64_t u64; |
| 6094 | |
| 6095 | nErrCode = QCBOR_Int64ToInt32(1, &n32); |
| 6096 | if(nErrCode == -1 || n32 != 1) { |
| 6097 | return 1; |
| 6098 | } |
| 6099 | |
| 6100 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32); |
| 6101 | if(nErrCode == -1 || n32 != INT32_MAX) { |
| 6102 | return 2; |
| 6103 | } |
| 6104 | |
| 6105 | nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32); |
| 6106 | if(nErrCode == -1 || n32 != INT32_MIN) { |
| 6107 | return 3; |
| 6108 | } |
| 6109 | |
| 6110 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32); |
| 6111 | if(nErrCode != -1) { |
| 6112 | return 4; |
| 6113 | } |
| 6114 | |
| 6115 | nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32); |
| 6116 | if(nErrCode != -1) { |
| 6117 | return 5; |
| 6118 | } |
| 6119 | |
| 6120 | |
| 6121 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16); |
| 6122 | if(nErrCode == -1 || n16 != INT16_MAX) { |
| 6123 | return 6; |
| 6124 | } |
| 6125 | |
| 6126 | nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16); |
| 6127 | if(nErrCode == -1 || n16 != INT16_MIN) { |
| 6128 | return 7; |
| 6129 | } |
| 6130 | |
| 6131 | nErrCode = QCBOR_Int64ToInt16(1, &n16); |
| 6132 | if(nErrCode == -1 || n16 != 1) { |
| 6133 | return 8; |
| 6134 | } |
| 6135 | |
| 6136 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16); |
| 6137 | if(nErrCode != -1) { |
| 6138 | return 9; |
| 6139 | } |
| 6140 | |
| 6141 | nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16); |
| 6142 | if(nErrCode != -1) { |
| 6143 | return 10; |
| 6144 | } |
| 6145 | |
| 6146 | |
| 6147 | nErrCode = QCBOR_Int64ToInt8(1, &n8); |
| 6148 | if(nErrCode == -1 || n8 != 1) { |
| 6149 | return 11; |
| 6150 | } |
| 6151 | |
| 6152 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8); |
| 6153 | if(nErrCode == -1 || n8 != INT8_MAX) { |
| 6154 | return 12; |
| 6155 | } |
| 6156 | |
| 6157 | nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8); |
| 6158 | if(nErrCode == -1 || n8 != INT8_MIN) { |
| 6159 | return 13; |
| 6160 | } |
| 6161 | |
| 6162 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8); |
| 6163 | if(nErrCode != -1) { |
| 6164 | return 14; |
| 6165 | } |
| 6166 | |
| 6167 | nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8); |
| 6168 | if(nErrCode != -1) { |
| 6169 | return 15; |
| 6170 | } |
| 6171 | |
| 6172 | |
| 6173 | nErrCode = QCBOR_Int64ToUInt32(1, &u32); |
| 6174 | if(nErrCode == -1 || u32 != 1) { |
| 6175 | return 16; |
| 6176 | } |
| 6177 | |
| 6178 | nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32); |
| 6179 | if(nErrCode == -1 || u32 != UINT32_MAX) { |
| 6180 | return 17; |
| 6181 | } |
| 6182 | |
| 6183 | nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32); |
| 6184 | if(nErrCode == -1 || u32 != 0) { |
| 6185 | return 18; |
| 6186 | } |
| 6187 | |
| 6188 | nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32); |
| 6189 | if(nErrCode != -1) { |
| 6190 | return 19; |
| 6191 | } |
| 6192 | |
| 6193 | nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32); |
| 6194 | if(nErrCode != -1) { |
| 6195 | return 20; |
| 6196 | } |
| 6197 | |
| 6198 | |
| 6199 | nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16); |
| 6200 | if(nErrCode == -1 || u16 != UINT16_MAX) { |
| 6201 | return 21; |
| 6202 | } |
| 6203 | |
| 6204 | nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16); |
| 6205 | if(nErrCode == -1 || u16 != 0) { |
| 6206 | return 22; |
| 6207 | } |
| 6208 | |
| 6209 | nErrCode = QCBOR_Int64UToInt16(1, &u16); |
| 6210 | if(nErrCode == -1 || u16 != 1) { |
| 6211 | return 23; |
| 6212 | } |
| 6213 | |
| 6214 | nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16); |
| 6215 | if(nErrCode != -1) { |
| 6216 | return 24; |
| 6217 | } |
| 6218 | |
| 6219 | nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16); |
| 6220 | if(nErrCode != -1) { |
| 6221 | return 25; |
| 6222 | } |
| 6223 | |
| 6224 | |
| 6225 | nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8); |
| 6226 | if(nErrCode == -1 || u8 != UINT8_MAX) { |
| 6227 | return 26; |
| 6228 | } |
| 6229 | |
| 6230 | nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8); |
| 6231 | if(nErrCode == -1 || u8 != 0) { |
| 6232 | return 27; |
| 6233 | } |
| 6234 | |
| 6235 | nErrCode = QCBOR_Int64ToUInt8(1, &u8); |
| 6236 | if(nErrCode == -1 || u8 != 1) { |
| 6237 | return 28; |
| 6238 | } |
| 6239 | |
| 6240 | nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8); |
| 6241 | if(nErrCode != -1) { |
| 6242 | return 29; |
| 6243 | } |
| 6244 | |
| 6245 | nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8); |
| 6246 | if(nErrCode != -1) { |
| 6247 | return 30; |
| 6248 | } |
| 6249 | |
| 6250 | |
| 6251 | nErrCode = QCBOR_Int64ToUInt64(1, &u64); |
| 6252 | if(nErrCode == -1 || u64 != 1) { |
| 6253 | return 31; |
| 6254 | } |
| 6255 | |
| 6256 | nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64); |
| 6257 | if(nErrCode == -1 || u64 != INT64_MAX) { |
| 6258 | return 32; |
| 6259 | } |
| 6260 | |
| 6261 | nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64); |
| 6262 | if(nErrCode == -1 || u64 != 0) { |
| 6263 | return 33; |
| 6264 | } |
| 6265 | |
| 6266 | nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64); |
| 6267 | if(nErrCode != -1) { |
| 6268 | return 34; |
| 6269 | } |
| 6270 | |
| 6271 | return 0; |
| 6272 | } |
| 6273 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6274 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6275 | |
| 6276 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6277 | /* |
| 6278 | A sequence with |
| 6279 | A wrapping bstr |
| 6280 | containing a map |
| 6281 | 1 |
| 6282 | 2 |
| 6283 | A wrapping bstr |
| 6284 | containing an array |
| 6285 | 3 |
| 6286 | wrapping bstr |
| 6287 | 4 |
| 6288 | 5 |
| 6289 | 6 |
| 6290 | array |
| 6291 | 7 |
| 6292 | 8 |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6293 | */ |
| 6294 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6295 | static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer) |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6296 | { |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6297 | UsefulBufC Encoded; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6298 | QCBOREncodeContext EC; |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6299 | QCBORError uErr; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6300 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6301 | QCBOREncode_Init(&EC, OutputBuffer); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6302 | |
| 6303 | QCBOREncode_BstrWrap(&EC); |
| 6304 | QCBOREncode_OpenMap(&EC); |
| 6305 | QCBOREncode_AddInt64ToMapN(&EC, 100, 1); |
| 6306 | QCBOREncode_AddInt64ToMapN(&EC, 200, 2); |
| 6307 | QCBOREncode_CloseMap(&EC); |
| 6308 | QCBOREncode_BstrWrap(&EC); |
| 6309 | QCBOREncode_OpenArray(&EC); |
| 6310 | QCBOREncode_AddInt64(&EC, 3); |
| 6311 | QCBOREncode_BstrWrap(&EC); |
| 6312 | QCBOREncode_AddInt64(&EC, 4); |
| 6313 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6314 | QCBOREncode_AddInt64(&EC, 5); |
| 6315 | QCBOREncode_CloseArray(&EC); |
| 6316 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6317 | QCBOREncode_AddInt64(&EC, 6); |
| 6318 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 6319 | QCBOREncode_OpenArray(&EC); |
| 6320 | QCBOREncode_AddInt64(&EC, 7); |
| 6321 | QCBOREncode_AddInt64(&EC, 8); |
| 6322 | QCBOREncode_CloseArray(&EC); |
| 6323 | |
| 6324 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 40a0432 | 2020-06-27 22:52:52 -0700 | [diff] [blame] | 6325 | if(uErr) { |
| 6326 | Encoded = NULLUsefulBufC; |
| 6327 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6328 | |
| 6329 | return Encoded; |
| 6330 | } |
| 6331 | |
| 6332 | |
| 6333 | int32_t EnterBstrTest() |
| 6334 | { |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 6335 | UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6336 | |
| 6337 | QCBORDecodeContext DC; |
| 6338 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6339 | QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6340 | |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6341 | int64_t n1, n2, n3, n4, n5, n6, n7, n8; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6342 | |
| 6343 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6344 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6345 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6346 | QCBORDecode_GetInt64InMapN(&DC, 100, &n1); |
| 6347 | QCBORDecode_GetInt64InMapN(&DC, 200, &n2); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6348 | QCBORDecode_ExitMap(&DC); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6349 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6350 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6351 | QCBORDecode_GetInt64(&DC, &n3); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6352 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6353 | QCBORDecode_GetInt64(&DC, &n4); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6354 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6355 | QCBORDecode_GetInt64(&DC, &n5); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6356 | QCBORDecode_ExitArray(&DC); |
| 6357 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6358 | QCBORDecode_GetInt64(&DC, &n6); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6359 | QCBORDecode_ExitBstrWrapped(&DC); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6360 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | 5501364 | 2020-09-23 05:39:22 -0700 | [diff] [blame] | 6361 | QCBORDecode_GetInt64(&DC, &n7); |
| 6362 | QCBORDecode_GetInt64(&DC, &n8); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 6363 | QCBORDecode_ExitArray(&DC); |
| 6364 | |
| 6365 | QCBORError uErr = QCBORDecode_Finish(&DC); |
| 6366 | |
| 6367 | return (int32_t)uErr; |
| 6368 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6369 | |
| 6370 | |
| 6371 | |
| 6372 | |
| 6373 | static const uint8_t spTaggedTypes[] = { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6374 | 0xb2, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6375 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6376 | // Date string |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6377 | 0x00, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6378 | 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, |
| 6379 | 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, |
| 6380 | 0x32, 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6381 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6382 | 0x01, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6383 | 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31, |
| 6384 | 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32, |
| 6385 | 0x5A, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6386 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6387 | // Bignum |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6388 | 10, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6389 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6390 | 0x09, 0x10, |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6391 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6392 | 11, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6393 | 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 6394 | 0x09, 0x10, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6395 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6396 | // URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6397 | 20, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6398 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, |
| 6399 | 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6400 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6401 | 21, |
| 6402 | 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62, |
| 6403 | 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F, |
| 6404 | |
| 6405 | // B64 |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6406 | 0x18, 0x1e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6407 | 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6408 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6409 | |
| 6410 | 0x18, 0x1f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6411 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6412 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6413 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6414 | // B64URL |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6415 | 0x18, 0x28, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6416 | 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, |
| 6417 | 0x31, 0x63, 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6418 | |
| 6419 | 0x18, 0x29, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6420 | 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63, |
| 6421 | 0x6D, 0x55, 0x75, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6422 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6423 | // Regex |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6424 | 0x18, 0x32, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6425 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, |
| 6426 | 0x6B, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6427 | |
| 6428 | 0x18, 0x33, |
| 6429 | 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B, |
| 6430 | |
| 6431 | // MIME |
| 6432 | 0x18, 0x3c, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6433 | 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, |
| 6434 | 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, |
| 6435 | 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6436 | |
| 6437 | 0x18, 0x3d, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6438 | 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6439 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6440 | |
| 6441 | 0x18, 0x3e, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6442 | 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, |
| 6443 | 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, |
| 6444 | 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6445 | |
| 6446 | 0x18, 0x3f, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6447 | 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73, |
| 6448 | 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6449 | |
| 6450 | // UUID |
| 6451 | 0x18, 0x46, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6452 | 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, |
| 6453 | 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6454 | |
| 6455 | 0x18, 0x47, |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6456 | 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, |
| 6457 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32 |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6458 | }; |
| 6459 | |
| 6460 | int32_t DecodeTaggedTypeTests() |
| 6461 | { |
| 6462 | QCBORDecodeContext DC; |
| 6463 | QCBORError uErr; |
| 6464 | |
| 6465 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0); |
| 6466 | |
| 6467 | UsefulBufC String; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6468 | bool bNeg; |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6469 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6470 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6471 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6472 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6473 | if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) { |
| 6474 | return 1; |
| 6475 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6476 | QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6477 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6478 | return 2; |
| 6479 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6480 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6481 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6482 | return 3; |
| 6483 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6484 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6485 | QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6486 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6487 | return 4; |
| 6488 | } |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6489 | QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6490 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6491 | return 5; |
| 6492 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6493 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6494 | QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6495 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6496 | bNeg != false) { |
| 6497 | return 10; |
| 6498 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6499 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6500 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6501 | bNeg != true) { |
| 6502 | return 11; |
| 6503 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6504 | QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6505 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6506 | return 12; |
| 6507 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6508 | QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6509 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6510 | return 13; |
| 6511 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6512 | QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6513 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6514 | return 14; |
| 6515 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6516 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6517 | QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6518 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6519 | return 20; |
| 6520 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6521 | QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6522 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6523 | return 21; |
| 6524 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6525 | QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6526 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6527 | return 22; |
| 6528 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6529 | QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6530 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6531 | return 23; |
| 6532 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6533 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6534 | QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6535 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6536 | return 30; |
| 6537 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6538 | QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6539 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6540 | return 31; |
| 6541 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6542 | QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6543 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6544 | return 32; |
| 6545 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6546 | QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6547 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6548 | return 33; |
| 6549 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6550 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6551 | QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6552 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6553 | return 40; |
| 6554 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6555 | QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6556 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6557 | return 41; |
| 6558 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6559 | QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6560 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6561 | return 42; |
| 6562 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6563 | QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6564 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6565 | return 43; |
| 6566 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6567 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6568 | QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6569 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6570 | return 50; |
| 6571 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6572 | QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6573 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6574 | return 51; |
| 6575 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6576 | QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6577 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6578 | return 52; |
| 6579 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6580 | QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6581 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6582 | return 53; |
| 6583 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6584 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6585 | // MIME |
| 6586 | bool bIsNot7Bit; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6587 | QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6588 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6589 | bIsNot7Bit == true) { |
| 6590 | return 60; |
| 6591 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6592 | QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6593 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6594 | bIsNot7Bit == true) { |
| 6595 | return 61; |
| 6596 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6597 | QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6598 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6599 | bIsNot7Bit == false) { |
| 6600 | return 62; |
| 6601 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6602 | QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6603 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS || |
| 6604 | bIsNot7Bit == false) { |
| 6605 | return 63; |
| 6606 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6607 | QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6608 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6609 | return 64; |
| 6610 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6611 | QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6612 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6613 | return 65; |
| 6614 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6615 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6616 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6617 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6618 | return 70; |
| 6619 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6620 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6621 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) { |
| 6622 | return 71; |
| 6623 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6624 | QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6625 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6626 | return 72; |
| 6627 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6628 | QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 6629 | if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6630 | return 73; |
| 6631 | } |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6632 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 6633 | // Improvement: add some more error test cases |
| 6634 | |
Laurence Lundblade | 37f46e5 | 2020-08-04 03:32:14 -0700 | [diff] [blame] | 6635 | QCBORDecode_ExitMap(&DC); |
| 6636 | |
| 6637 | uErr = QCBORDecode_Finish(&DC); |
| 6638 | if(uErr != QCBOR_SUCCESS) { |
| 6639 | return 100; |
| 6640 | } |
| 6641 | |
| 6642 | return 0; |
| 6643 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6644 | |
| 6645 | |
| 6646 | |
| 6647 | |
| 6648 | /* |
| 6649 | [ |
| 6650 | "aaaaaaaaaa", |
| 6651 | {} |
| 6652 | ] |
| 6653 | */ |
| 6654 | static const uint8_t spTooLarge1[] = { |
| 6655 | 0x9f, |
| 6656 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6657 | 0xa0, |
| 6658 | 0xff |
| 6659 | }; |
| 6660 | |
| 6661 | /* |
| 6662 | [ |
| 6663 | { |
| 6664 | 0: "aaaaaaaaaa" |
| 6665 | } |
| 6666 | ] |
| 6667 | */ |
| 6668 | static const uint8_t spTooLarge2[] = { |
| 6669 | 0x9f, |
| 6670 | 0xa1, |
| 6671 | 0x00, |
| 6672 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6673 | 0xff |
| 6674 | }; |
| 6675 | |
| 6676 | /* |
| 6677 | h'A1006A61616161616161616161' |
| 6678 | |
| 6679 | { |
| 6680 | 0: "aaaaaaaaaa" |
| 6681 | } |
| 6682 | */ |
| 6683 | static const uint8_t spTooLarge3[] = { |
| 6684 | 0x4d, |
| 6685 | 0xa1, |
| 6686 | 0x00, |
| 6687 | 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, |
| 6688 | }; |
| 6689 | |
| 6690 | int32_t TooLargeInputTest(void) |
| 6691 | { |
| 6692 | QCBORDecodeContext DC; |
| 6693 | QCBORError uErr; |
| 6694 | UsefulBufC String; |
| 6695 | |
| 6696 | // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set |
| 6697 | // to 10 There's not really any way to test this error |
| 6698 | // condition. The error condition is not complex, so setting |
| 6699 | // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test. |
| 6700 | |
| 6701 | // The input CBOR is only too large because the |
| 6702 | // QCBOR_MAX_DECODE_INPUT_SIZE is 10. |
| 6703 | // |
| 6704 | // This test is disabled for the normal test runs because of the |
| 6705 | // special build requirement. |
| 6706 | |
| 6707 | |
| 6708 | // Tests the start of a map being too large |
| 6709 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6710 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6711 | QCBORDecode_GetTextString(&DC, &String); |
| 6712 | uErr = QCBORDecode_GetError(&DC); |
| 6713 | if(uErr != QCBOR_SUCCESS) { |
| 6714 | return 1; |
| 6715 | } |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6716 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6717 | uErr = QCBORDecode_GetError(&DC); |
| 6718 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 6719 | return 2; |
| 6720 | } |
| 6721 | |
| 6722 | // Tests the end of a map being too large |
| 6723 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6724 | QCBORDecode_EnterArray(&DC, NULL); |
| 6725 | QCBORDecode_EnterMap(&DC, NULL); |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 6726 | uErr = QCBORDecode_GetError(&DC); |
| 6727 | if(uErr != QCBOR_SUCCESS) { |
| 6728 | return 3; |
| 6729 | } |
| 6730 | QCBORDecode_ExitMap(&DC); |
| 6731 | uErr = QCBORDecode_GetError(&DC); |
| 6732 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 6733 | return 4; |
| 6734 | } |
| 6735 | |
| 6736 | // Tests the entire input CBOR being too large when processing bstr wrapping |
| 6737 | QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL); |
| 6738 | QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 6739 | uErr = QCBORDecode_GetError(&DC); |
| 6740 | if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) { |
| 6741 | return 5; |
| 6742 | } |
| 6743 | |
| 6744 | return 0; |
| 6745 | } |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6746 | |
| 6747 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6748 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 6749 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6750 | static const uint8_t spMapWithIndefLenStrings[] = { |
| 6751 | 0xbf, |
| 6752 | 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff, |
| 6753 | 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff, |
| 6754 | 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff, |
| 6755 | 0x03, |
| 6756 | 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff, |
| 6757 | 0xc3, |
| 6758 | 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff, |
| 6759 | 0xff |
| 6760 | }; |
| 6761 | |
| 6762 | int32_t SpiffyIndefiniteLengthStringsTests() |
| 6763 | { |
| 6764 | QCBORDecodeContext DCtx; |
| 6765 | |
| 6766 | QCBORDecode_Init(&DCtx, |
| 6767 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings), |
| 6768 | QCBOR_DECODE_MODE_NORMAL); |
| 6769 | |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 6770 | UsefulBuf_MAKE_STACK_UB(StringBuf, 200); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6771 | QCBORDecode_SetMemPool(&DCtx, StringBuf, false); |
| 6772 | |
| 6773 | UsefulBufC ByteString; |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 6774 | QCBORDecode_EnterMap(&DCtx, NULL); |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6775 | QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString); |
| 6776 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 6777 | return 1; |
| 6778 | } |
| 6779 | |
| 6780 | const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 6781 | if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) { |
| 6782 | return 2; |
| 6783 | } |
| 6784 | |
| 6785 | uint64_t uInt; |
| 6786 | QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt); |
| 6787 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 6788 | return 3; |
| 6789 | } |
| 6790 | if(uInt != 3) { |
| 6791 | return 4; |
| 6792 | } |
| 6793 | |
| 6794 | double uDouble; |
| 6795 | QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx, |
| 6796 | "label2", |
| 6797 | 0xff, |
| 6798 | &uDouble); |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 6799 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6800 | if(QCBORDecode_GetAndResetError(&DCtx)) { |
| 6801 | return 5; |
| 6802 | } |
| 6803 | if(uDouble != -16777474) { |
| 6804 | return 6; |
| 6805 | } |
Laurence Lundblade | b8e19aa | 2020-10-07 20:59:11 -0700 | [diff] [blame] | 6806 | #else |
| 6807 | if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) { |
| 6808 | return 7; |
| 6809 | } |
| 6810 | #endif |
| 6811 | |
Laurence Lundblade | b6d1c69 | 2020-10-07 18:37:48 -0700 | [diff] [blame] | 6812 | |
| 6813 | QCBORDecode_ExitMap(&DCtx); |
| 6814 | |
| 6815 | if(QCBORDecode_Finish(&DCtx)) { |
| 6816 | return 99; |
| 6817 | } |
| 6818 | |
| 6819 | return 0; |
| 6820 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 6821 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 6822 | |
| 6823 | |
| 6824 | |
| 6825 | int32_t PeekTest() |
| 6826 | { |
| 6827 | QCBORItem Item; |
| 6828 | QCBORError nCBORError; |
| 6829 | QCBORDecodeContext DCtx; |
| 6830 | |
| 6831 | QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0); |
| 6832 | |
| 6833 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6834 | return 100+(int32_t)nCBORError; |
| 6835 | } |
| 6836 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 6837 | return 200; |
| 6838 | } |
| 6839 | |
| 6840 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6841 | return (int32_t)nCBORError; |
| 6842 | } |
| 6843 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 6844 | return 300; |
| 6845 | } |
| 6846 | |
| 6847 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6848 | return 400 + (int32_t)nCBORError; |
| 6849 | } |
| 6850 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 6851 | return 500; |
| 6852 | } |
| 6853 | |
| 6854 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6855 | return (int32_t)nCBORError; |
| 6856 | } |
| 6857 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) { |
| 6858 | return 600; |
| 6859 | } |
| 6860 | |
| 6861 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6862 | return 900 + (int32_t)nCBORError; |
| 6863 | } |
| 6864 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6865 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 6866 | Item.val.int64 != 42 || |
| 6867 | Item.uDataAlloc || |
| 6868 | Item.uLabelAlloc || |
| 6869 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 6870 | return 1000; |
| 6871 | } |
| 6872 | |
| 6873 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6874 | return 1100 + (int32_t)nCBORError; |
| 6875 | } |
| 6876 | |
| 6877 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6878 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 6879 | Item.val.int64 != 42 || |
| 6880 | Item.uDataAlloc || |
| 6881 | Item.uLabelAlloc || |
| 6882 | UsefulBufCompareToSZ(Item.label.string, "first integer")) { |
| 6883 | return 1200; |
| 6884 | } |
| 6885 | |
| 6886 | |
| 6887 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6888 | return 1300 + (int32_t)nCBORError; |
| 6889 | } |
| 6890 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6891 | Item.uDataAlloc || |
| 6892 | Item.uLabelAlloc || |
| 6893 | UsefulBufCompareToSZ(Item.label.string, "an array of two strings") || |
| 6894 | Item.uDataType != QCBOR_TYPE_ARRAY || |
| 6895 | Item.val.uCount != 2) |
| 6896 | return 1400; |
| 6897 | |
| 6898 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6899 | return 1500 + (int32_t)nCBORError; |
| 6900 | } |
| 6901 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 6902 | Item.uDataAlloc || |
| 6903 | Item.uLabelAlloc || |
| 6904 | UsefulBufCompareToSZ(Item.val.string, "string1")) { |
| 6905 | return 1600; |
| 6906 | } |
| 6907 | |
| 6908 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6909 | return 1700 + (int32_t)nCBORError; |
| 6910 | } |
| 6911 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 6912 | Item.uDataAlloc || |
| 6913 | Item.uLabelAlloc || |
| 6914 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 6915 | return 1800; |
| 6916 | } |
| 6917 | |
| 6918 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6919 | return (int32_t)nCBORError; |
| 6920 | } |
| 6921 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 6922 | Item.uDataAlloc || |
| 6923 | Item.uLabelAlloc || |
| 6924 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 6925 | return 1900; |
| 6926 | } |
| 6927 | |
| 6928 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6929 | return (int32_t)nCBORError; |
| 6930 | } |
| 6931 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 6932 | Item.uDataAlloc || |
| 6933 | Item.uLabelAlloc || |
| 6934 | UsefulBufCompareToSZ(Item.val.string, "string2")) { |
| 6935 | return 2000; |
| 6936 | } |
| 6937 | |
| 6938 | |
| 6939 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6940 | return 2100 + (int32_t)nCBORError; |
| 6941 | } |
| 6942 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6943 | Item.uDataAlloc || |
| 6944 | Item.uLabelAlloc || |
| 6945 | UsefulBufCompareToSZ(Item.label.string, "map in a map") || |
| 6946 | Item.uDataType != QCBOR_TYPE_MAP || |
| 6947 | Item.val.uCount != 4) { |
| 6948 | return 2100; |
| 6949 | } |
| 6950 | |
| 6951 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6952 | return 2200 + (int32_t)nCBORError; |
| 6953 | } |
| 6954 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6955 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))|| |
| 6956 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 6957 | Item.uDataAlloc || |
| 6958 | Item.uLabelAlloc || |
| 6959 | UsefulBufCompareToSZ(Item.val.string, "xxxx")) { |
| 6960 | return 2300; |
| 6961 | } |
| 6962 | |
| 6963 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6964 | return 2400 + (int32_t)nCBORError; |
| 6965 | } |
| 6966 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6967 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 6968 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 6969 | Item.uDataAlloc || |
| 6970 | Item.uLabelAlloc || |
| 6971 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 6972 | return 2500; |
| 6973 | } |
| 6974 | |
| 6975 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6976 | return 2600 + (int32_t)nCBORError; |
| 6977 | } |
| 6978 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6979 | UsefulBufCompareToSZ(Item.label.string, "bytes 2") || |
| 6980 | Item.uDataType != QCBOR_TYPE_BYTE_STRING || |
| 6981 | Item.uDataAlloc || |
| 6982 | Item.uLabelAlloc || |
| 6983 | UsefulBufCompareToSZ(Item.val.string, "yyyy")) { |
| 6984 | return 2700; |
| 6985 | } |
| 6986 | |
| 6987 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 6988 | return 2800 + (int32_t)nCBORError; |
| 6989 | } |
| 6990 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 6991 | Item.uDataAlloc || |
| 6992 | Item.uLabelAlloc || |
| 6993 | UsefulBufCompareToSZ(Item.label.string, "another int") || |
| 6994 | Item.uDataType != QCBOR_TYPE_INT64 || |
| 6995 | Item.val.int64 != 98) |
| 6996 | return 2900; |
| 6997 | |
| 6998 | if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) { |
| 6999 | return 3000 + (int32_t)nCBORError; |
| 7000 | } |
| 7001 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7002 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7003 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7004 | Item.uDataAlloc || |
| 7005 | Item.uLabelAlloc || |
| 7006 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7007 | return 3100; |
| 7008 | } |
| 7009 | |
| 7010 | if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) { |
| 7011 | return 3200 + (int32_t)nCBORError; |
| 7012 | } |
| 7013 | if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || |
| 7014 | UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))|| |
| 7015 | Item.uDataType != QCBOR_TYPE_TEXT_STRING || |
| 7016 | Item.uDataAlloc || |
| 7017 | Item.uLabelAlloc || |
| 7018 | UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) { |
| 7019 | return 3300; |
| 7020 | } |
| 7021 | |
| 7022 | return 0; |
| 7023 | } |