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 | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3 | Copyright (c) 2018-2021, Laurence Lundblade. |
adam2809 | 46eefce | 2022-08-13 21:48:07 +0200 | [diff] [blame] | 4 | Copyright (c) 2022, Arm Limited. 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 | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 32 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 33 | #include "qcbor/qcbor_encode.h" |
| 34 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 35 | #include "qcbor_encode_tests.h" |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 36 | |
| 37 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 38 | /* |
| 39 | This is the test set for CBOR encoding. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 40 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 41 | This is largely complete for the implemented. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 42 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 43 | A few more things to do include: |
| 44 | - Add a test for counting the top level items and adding it back in with AddRaw() |
| 45 | - Run on some different CPUs like 32-bit and maybe even 16-bit |
| 46 | - Test the large array count limit |
| 47 | - Add the CBOR diagnostic output for every expected |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 48 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 49 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 50 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 51 | //#define PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 52 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 53 | #ifdef PRINT_FUNCTIONS_FOR_DEBUGGING |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 54 | #include <stdio.h> |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 55 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 56 | #if 0 |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 57 | // ifdef these out to not have compiler warnings |
| 58 | static void printencoded(const uint8_t *pEncoded, size_t nLen) |
| 59 | { |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 60 | size_t i; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 61 | for(i = 0; i < nLen; i++) { |
| 62 | uint8_t Z = pEncoded[i]; |
| 63 | printf("%02x ", Z); |
| 64 | } |
| 65 | printf("\n"); |
| 66 | |
| 67 | fflush(stdout); |
| 68 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 69 | #endif |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 70 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 71 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 72 | // Do the comparison and print out where it fails |
| 73 | static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) { |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 74 | size_t i; |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 75 | for(i = 0; i < U1.len; i++) { |
| 76 | if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) { |
escherstair | b556342 | 2023-02-13 18:36:45 +0100 | [diff] [blame] | 77 | printf("Position: %u Actual: 0x%x Expected: 0x%x\n", |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 78 | (uint32_t)i, |
| 79 | ((uint8_t *)U1.ptr)[i], |
| 80 | ((uint8_t *)U2.ptr)[i]); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | } |
| 84 | return 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 85 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 86 | } |
| 87 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 88 | #define CheckResults(Enc, Expected) \ |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 89 | UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)}) |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 90 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 91 | #else |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 92 | |
| 93 | #define CheckResults(Enc, Expected) \ |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 94 | UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)}) |
| 95 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 99 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 100 | /* |
| 101 | Returns 0 if UsefulBufs are equal |
| 102 | Returns 1000000 + offeset if they are not equal. |
| 103 | |
| 104 | |
| 105 | |
| 106 | */ |
| 107 | struct UBCompareDiagnostic { |
| 108 | uint8_t uActual; |
| 109 | uint8_t uExpected; |
| 110 | size_t uOffset; |
| 111 | }; |
| 112 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 113 | static int32_t |
| 114 | UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual, |
| 115 | UsefulBufC Expected, |
| 116 | struct UBCompareDiagnostic *pDiag) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 117 | size_t i; |
| 118 | for(i = 0; i < Actual.len; i++) { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 119 | if(((const uint8_t *)Actual.ptr)[i] != ((const uint8_t *)Expected.ptr)[i]) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 120 | if(pDiag) { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 121 | pDiag->uActual = ((const uint8_t *)Actual.ptr)[i]; |
| 122 | pDiag->uExpected = ((const uint8_t *)Expected.ptr)[i]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 123 | pDiag->uOffset = i; |
| 124 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 125 | // Cast to int is OK as this is only a diagnostic and the sizes |
| 126 | // here are never over a few KB. |
| 127 | return (int32_t)i + 1000000; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | return 0; |
| 131 | |
| 132 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 133 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 134 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 135 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 136 | // One big buffer that is used by all the tests to encode into |
| 137 | // Putting it in uninitialized data is better than using a lot |
| 138 | // of stack. The tests should run on small devices too. |
| 139 | static uint8_t spBigBuf[2200]; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 140 | |
| 141 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | Some very minimal tests. |
| 145 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 146 | int32_t BasicEncodeTest(void) |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 147 | { |
| 148 | // Very simple CBOR, a map with one boolean that is true in it |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 149 | QCBOREncodeContext EC; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 150 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 151 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 152 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 153 | QCBOREncode_OpenMap(&EC); |
| 154 | QCBOREncode_AddBoolToMapN(&EC, 66, true); |
| 155 | QCBOREncode_CloseMap(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 156 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 157 | UsefulBufC Encoded; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 158 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 159 | return -1; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 160 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 161 | |
| 162 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 163 | // Decode it and see that is right |
| 164 | QCBORDecodeContext DC; |
| 165 | QCBORItem Item; |
| 166 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 167 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 168 | QCBORDecode_GetNext(&DC, &Item); |
| 169 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 170 | return -2; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 171 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 172 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 173 | QCBORDecode_GetNext(&DC, &Item); |
| 174 | if(Item.uDataType != QCBOR_TYPE_TRUE) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 175 | return -3; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 176 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 177 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 178 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 179 | return -4; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 180 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 181 | |
| 182 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 183 | // Make another encoded message with the CBOR from the previous |
| 184 | // put into this one |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 185 | UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 186 | QCBOREncode_Init(&EC, MemoryForEncoded2); |
| 187 | QCBOREncode_OpenArray(&EC); |
| 188 | QCBOREncode_AddUInt64(&EC, 451); |
| 189 | QCBOREncode_AddEncoded(&EC, Encoded); |
| 190 | QCBOREncode_OpenMap(&EC); |
| 191 | QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded); |
| 192 | QCBOREncode_CloseMap(&EC); |
| 193 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 194 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 195 | UsefulBufC Encoded2; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 196 | if(QCBOREncode_Finish(&EC, &Encoded2)) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 197 | return -5; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 198 | } |
| 199 | /* |
| 200 | [ // 0 1:3 |
| 201 | 451, // 1 1:2 |
| 202 | { // 1 1:2 2:1 |
| 203 | 66: true // 2 1:1 |
| 204 | }, |
| 205 | { // 1 1:1 2:1 |
| 206 | -70000: { // 2 1:1 2:1 3:1 |
| 207 | 66: true // 3 XXXXXX |
| 208 | } |
| 209 | } |
| 210 | ] |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 211 | |
| 212 | |
| 213 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 214 | 83 # array(3) |
| 215 | 19 01C3 # unsigned(451) |
| 216 | A1 # map(1) |
| 217 | 18 42 # unsigned(66) |
| 218 | F5 # primitive(21) |
| 219 | A1 # map(1) |
| 220 | 3A 0001116F # negative(69999) |
| 221 | A1 # map(1) |
| 222 | 18 42 # unsigned(66) |
| 223 | F5 # primitive(21) |
| 224 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 225 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 226 | // Decode it and see if it is OK |
| 227 | QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 228 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 229 | // 0 1:3 |
| 230 | QCBORDecode_GetNext(&DC, &Item); |
| 231 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 232 | return -6; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 233 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 234 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 235 | // 1 1:2 |
| 236 | QCBORDecode_GetNext(&DC, &Item); |
| 237 | if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 238 | return -7; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 239 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 240 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 241 | // 1 1:2 2:1 |
| 242 | QCBORDecode_GetNext(&DC, &Item); |
| 243 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 244 | return -8; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 245 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 246 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 247 | // 2 1:1 |
| 248 | QCBORDecode_GetNext(&DC, &Item); |
| 249 | if(Item.uDataType != QCBOR_TYPE_TRUE) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 250 | return -9; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 251 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 252 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 253 | // 1 1:1 2:1 |
| 254 | QCBORDecode_GetNext(&DC, &Item); |
| 255 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 256 | return -10; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 257 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 258 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 259 | // 2 1:1 2:1 3:1 |
| 260 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 261 | if(Item.uDataType != QCBOR_TYPE_MAP || |
| 262 | Item.val.uCount != 1 || |
| 263 | Item.uLabelType != QCBOR_TYPE_INT64 || |
| 264 | Item.label.int64 != -70000) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 265 | return -11; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 266 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 267 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 268 | // 3 XXXXXX |
| 269 | QCBORDecode_GetNext(&DC, &Item); |
| 270 | if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 271 | return -12; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 272 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 273 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 274 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 275 | return -13; |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 276 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 277 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 283 | static const uint8_t spExpectedEncodedAll[] = { |
Laurence Lundblade | 5a6fec5 | 2022-12-25 11:28:43 -0700 | [diff] [blame] | 284 | 0x98, 0x23, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8, |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 285 | 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54, |
| 286 | 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16, |
| 287 | 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42, |
| 288 | 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45, |
| 289 | 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49, |
| 290 | 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20, |
| 291 | 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a, |
| 292 | 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f, |
Laurence Lundblade | 3df8c7e | 2018-11-02 13:12:41 +0700 | [diff] [blame] | 293 | 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a, |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 294 | 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65, |
| 295 | 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68, |
| 296 | 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74, |
| 297 | 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41, |
| 298 | 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76, |
Laurence Lundblade | 5a6fec5 | 2022-12-25 11:28:43 -0700 | [diff] [blame] | 299 | 0x42, 0xff, 0x00, 0xa4, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69, |
| 300 | 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, |
| 301 | 0x65, 0x65, 0x6D, 0x70, 0x74, 0x79, 0x40, |
| 302 | 0x66, 0x62, |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 303 | 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00, |
| 304 | 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20, |
| 305 | 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, |
Laurence Lundblade | 5a6fec5 | 2022-12-25 11:28:43 -0700 | [diff] [blame] | 306 | 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0x60, 0xd8, 0x20, 0x78, 0x6b, |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 307 | 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61, |
| 308 | 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, |
| 309 | 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74, |
| 310 | 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39, |
| 311 | 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f, |
| 312 | 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d, |
| 313 | 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65, |
| 314 | 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65, |
| 315 | 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c, |
| 316 | 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64, |
| 317 | 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78, |
| 318 | 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63, |
| 319 | 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56, |
| 320 | 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 321 | 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9, |
| 322 | 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 323 | 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, |
| 324 | 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, |
| 325 | 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, |
| 326 | 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, |
| 327 | 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, |
| 328 | 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, |
| 329 | 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, |
| 330 | 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, |
| 331 | 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, |
| 332 | 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, |
| 333 | 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, |
| 334 | 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, |
| 335 | 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, |
| 336 | 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, |
| 337 | 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, |
| 338 | 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, |
| 339 | 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, |
| 340 | 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, |
| 341 | 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, |
| 342 | 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, |
| 343 | 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, |
| 344 | 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, |
| 345 | 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, |
| 346 | 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, |
| 347 | 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, |
| 348 | 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, |
| 349 | 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, |
| 350 | 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, |
| 351 | 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, |
| 352 | 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, |
| 353 | 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, |
| 354 | 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, |
| 355 | 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, |
| 356 | 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23, |
| 357 | 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62, |
| 358 | 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, |
| 359 | 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20, |
| 360 | 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29, |
| 361 | 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61, |
| 362 | 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f, |
| 363 | 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, |
| 364 | 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f, |
| 365 | 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66, |
| 366 | 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, |
| 367 | 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68, |
| 368 | 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f, |
| 369 | 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72, |
| 370 | 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63, |
| 371 | 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69, |
| 372 | 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89, |
| 373 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, |
| 374 | 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77, |
| 375 | 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c, |
| 376 | 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d, |
| 377 | 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56, |
| 378 | 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f, |
| 379 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d, |
| 380 | 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 381 | 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01, |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 382 | 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, |
| 383 | 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, |
| 384 | 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, |
| 385 | 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, |
| 386 | 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, |
| 387 | 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, |
| 388 | 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, |
| 389 | 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, |
| 390 | 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, |
| 391 | 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, |
| 392 | 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, |
| 393 | 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, |
| 394 | 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, |
| 395 | 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, |
| 396 | 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, |
| 397 | 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, |
| 398 | 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, |
| 399 | 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, |
| 400 | 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, |
| 401 | 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, |
| 402 | 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, |
| 403 | 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, |
| 404 | 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, |
| 405 | 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, |
| 406 | 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, |
| 407 | 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, |
| 408 | 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, |
| 409 | 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, |
| 410 | 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, |
| 411 | 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, |
| 412 | 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, |
| 413 | 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, |
| 414 | 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, |
| 415 | 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 416 | 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57, |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 417 | 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, |
| 418 | 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f, |
| 419 | 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, |
| 420 | 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, |
| 421 | 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62, |
| 422 | 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58, |
| 423 | 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, |
| 424 | 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54, |
| 425 | 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d, |
| 426 | 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d, |
| 427 | 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, |
| 428 | 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, |
| 429 | 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, |
| 430 | 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, |
| 431 | 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, |
| 432 | 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, |
| 433 | 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a, |
| 434 | 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, |
| 435 | 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78, |
| 436 | 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, |
| 437 | 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, |
| 438 | 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, |
| 439 | 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, |
| 440 | 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43, |
| 441 | 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73, |
| 442 | 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, |
| 443 | 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, |
| 444 | 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, |
| 445 | 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, |
| 446 | 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, |
| 447 | 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, |
| 448 | 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, |
| 449 | 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, |
| 450 | 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, |
| 451 | 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d, |
| 452 | 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, |
| 453 | 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64, |
| 454 | 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30, |
| 455 | 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, |
| 456 | 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35, |
| 457 | 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78, |
| 458 | 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, |
| 459 | 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, |
| 460 | 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7, |
| 461 | 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62, |
| 462 | 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80, |
| 463 | 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61, |
| 464 | 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20, |
| 465 | 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61, |
| 466 | 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62, |
| 467 | 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20, |
| 468 | 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1, |
| 469 | 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61, |
| 470 | 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5, |
| 471 | 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32, |
| 472 | 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05, |
| 473 | 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8, |
| 474 | 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, |
| 475 | 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, |
| 476 | 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50, |
| 477 | 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54, |
| 478 | 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25, |
| 479 | 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, |
| 480 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2, |
| 481 | 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73, |
| 482 | 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19, |
| 483 | 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 484 | 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, |
| 485 | 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B, |
| 486 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 487 | 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 488 | 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49, |
| 489 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, |
| 490 | 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 491 | 0x00, 0x00 |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | |
| 495 | static const char *szMIME = "\ |
| 496 | MIME-Version: 1.0\n\ |
| 497 | Content-Type: multipart/mixed;\n\ |
| 498 | boundary=\"XXXXboundary text\"\n\ |
| 499 | \n\ |
| 500 | This is a multipart message in MIME format.\n\ |
| 501 | \n\ |
| 502 | --XXXXboundary text\n\ |
| 503 | Content-Type: text/plain\n\ |
| 504 | \n\ |
| 505 | this is the body text\n\ |
| 506 | \n\ |
| 507 | --XXXXboundary text\n\ |
| 508 | Content-Type: text/plain;\n\ |
| 509 | Content-Disposition: attachment;\n\ |
| 510 | filename=\"test.txt\"\n\ |
| 511 | \n\ |
| 512 | this is the attachment text\n\ |
| 513 | \n\ |
| 514 | --XXXXboundary text--"; |
| 515 | |
| 516 | |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 517 | static void AddAll(QCBOREncodeContext *pECtx) |
| 518 | { |
| 519 | QCBOREncode_OpenArray(pECtx); |
| 520 | |
| 521 | /* Some ints that are tagged and have strings preceeding them |
| 522 | * (not labels becase it is not a map) */ |
| 523 | QCBOREncode_AddSZString(pECtx, "UINT62"); |
| 524 | QCBOREncode_AddTag(pECtx, 100); |
| 525 | QCBOREncode_AddUInt64(pECtx, 89989909); |
| 526 | QCBOREncode_AddSZString(pECtx, "INT64"); |
| 527 | QCBOREncode_AddTag(pECtx, 76); |
| 528 | QCBOREncode_AddInt64(pECtx, 77689989909); |
| 529 | QCBOREncode_AddUInt64(pECtx,0); |
| 530 | QCBOREncode_AddInt64(pECtx, -44); |
| 531 | |
| 532 | /* ints that go in maps */ |
| 533 | QCBOREncode_OpenMap(pECtx); |
| 534 | QCBOREncode_AddUInt64ToMap(pECtx, "LBL", 77); |
| 535 | QCBOREncode_AddUInt64ToMapN(pECtx, -4, 88); |
| 536 | QCBOREncode_AddInt64ToMap(pECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238); |
| 537 | QCBOREncode_AddInt64ToMapN(pECtx, -100000000, -800000000); |
| 538 | QCBOREncode_CloseMap(pECtx); |
| 539 | |
| 540 | /* Epoch Date */ |
| 541 | QCBOREncode_AddDateEpoch(pECtx, 2383748234); |
| 542 | |
| 543 | /* Epoch date with labels */ |
| 544 | QCBOREncode_OpenMap(pECtx); |
| 545 | QCBOREncode_AddDateEpochToMap(pECtx, "LongLiveDenisRitchie", 1400000000); |
| 546 | QCBOREncode_AddDateEpochToMap(pECtx, "time()", 1477263730); |
| 547 | QCBOREncode_AddDateEpochToMapN(pECtx, -1969, 1477263222); |
| 548 | QCBOREncode_CloseMap(pECtx); |
| 549 | |
| 550 | /* Binary blobs */ |
| 551 | QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2})); |
| 552 | |
| 553 | /* binary blobs in maps */ |
| 554 | QCBOREncode_OpenMap(pECtx); |
| 555 | QCBOREncode_AddSZString(pECtx, "binbin"); |
| 556 | QCBOREncode_AddTag(pECtx, 100000); |
| 557 | QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1})); |
Laurence Lundblade | 5a6fec5 | 2022-12-25 11:28:43 -0700 | [diff] [blame] | 558 | QCBOREncode_AddBytesToMap(pECtx, "empty", NULLUsefulBufC); // Empty string |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 559 | QCBOREncode_AddBytesToMap(pECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3})); |
| 560 | QCBOREncode_AddBytesToMapN(pECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4})); |
| 561 | QCBOREncode_CloseMap(pECtx); |
| 562 | |
| 563 | /* text blobs */ |
| 564 | QCBOREncode_AddText(pECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar")); |
| 565 | QCBOREncode_AddSZString(pECtx, "oof\n"); |
Laurence Lundblade | 5a6fec5 | 2022-12-25 11:28:43 -0700 | [diff] [blame] | 566 | QCBOREncode_AddText(pECtx, NULLUsefulBufC); // Empty string |
| 567 | |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 568 | const char *szURL = |
| 569 | "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"; |
| 570 | QCBOREncode_AddURI(pECtx, UsefulBuf_FromSZ(szURL)); |
| 571 | QCBOREncode_AddB64Text(pECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ==")); |
| 572 | QCBOREncode_AddRegex(pECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+")); |
| 573 | QCBOREncode_AddMIMEData(pECtx, UsefulBuf_FromSZ(szMIME)); |
| 574 | |
| 575 | /* text blobs in maps */ |
| 576 | QCBOREncode_OpenMap(pECtx); |
| 577 | QCBOREncode_AddTextToMap(pECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo")); |
| 578 | QCBOREncode_AddTextToMap(pECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar")); |
| 579 | QCBOREncode_AddSZString(pECtx, "()()()"); |
| 580 | QCBOREncode_AddTag(pECtx, 1000); |
| 581 | QCBOREncode_AddSZString(pECtx, "rab rab oof"); |
| 582 | QCBOREncode_AddTextToMapN(pECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo")); |
| 583 | QCBOREncode_AddSZStringToMap(pECtx, "^^", "oooooooof"); |
| 584 | QCBOREncode_AddSZStringToMapN(pECtx, 99, "ffffoooooooof"); |
| 585 | QCBOREncode_AddURIToMap(pECtx, |
| 586 | "RFC", |
| 587 | UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5")); |
| 588 | QCBOREncode_AddURIToMapN(pECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/")); |
| 589 | QCBOREncode_AddB64TextToMap(pECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu")); |
| 590 | QCBOREncode_AddB64TextToMapN(pECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4=")); |
| 591 | QCBOREncode_AddRegexToMap(pECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug |
| 592 | QCBOREncode_AddRegexToMapN(pECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug |
| 593 | QCBOREncode_AddMIMEDataToMap(pECtx, "Ned", UsefulBuf_FromSZ(szMIME)); |
| 594 | QCBOREncode_AddMIMEDataToMapN(pECtx, 10, UsefulBuf_FromSZ(szMIME)); |
| 595 | QCBOREncode_CloseMap(pECtx); |
| 596 | |
| 597 | /* Date strings */ |
| 598 | QCBOREncode_AddDateString(pECtx, "2003-12-13T18:30:02Z"); |
| 599 | QCBOREncode_OpenMap(pECtx); |
| 600 | QCBOREncode_AddDateStringToMap(pECtx, "Bed time", "2003-12-13T18:30:02.25+01:00"); |
| 601 | QCBOREncode_AddDateStringToMapN(pECtx, 88, "2003-12-13T18:30:02.25+01:00"); |
| 602 | QCBOREncode_CloseMap(pECtx); |
| 603 | |
| 604 | /* true / false ... */ |
| 605 | QCBOREncode_AddSimple(pECtx, CBOR_SIMPLEV_UNDEF); |
| 606 | QCBOREncode_OpenMap(pECtx); |
| 607 | QCBOREncode_AddSZString(pECtx, "dare"); |
| 608 | QCBOREncode_AddTag(pECtx, 66); |
| 609 | QCBOREncode_AddBool(pECtx, true); |
| 610 | QCBOREncode_AddBoolToMap(pECtx, "uu", false); |
| 611 | QCBOREncode_AddSimpleToMapN(pECtx, 737634, CBOR_SIMPLEV_NULL); |
| 612 | QCBOREncode_CloseMap(pECtx); |
| 613 | |
| 614 | /* opening an array */ |
| 615 | QCBOREncode_OpenArray(pECtx); |
| 616 | QCBOREncode_CloseArray(pECtx); |
| 617 | |
| 618 | /* opening arrays in a map */ |
| 619 | QCBOREncode_OpenMap(pECtx); |
| 620 | QCBOREncode_AddSZString(pECtx, "label and tagged empty array"); |
| 621 | QCBOREncode_AddTag(pECtx, 1093); |
| 622 | QCBOREncode_OpenArray(pECtx); |
| 623 | QCBOREncode_CloseArray(pECtx); |
| 624 | QCBOREncode_OpenArrayInMap(pECtx, "alabl"); |
| 625 | QCBOREncode_CloseArray(pECtx); |
| 626 | QCBOREncode_OpenArrayInMapN(pECtx, 42); |
| 627 | QCBOREncode_CloseArray(pECtx); |
| 628 | QCBOREncode_CloseMap(pECtx); |
| 629 | |
| 630 | /* opening maps with labels and tagging */ |
| 631 | QCBOREncode_OpenMap(pECtx); |
| 632 | QCBOREncode_OpenMapInMap(pECtx, "in a map"); |
| 633 | QCBOREncode_OpenMapInMapN(pECtx, 5556); |
| 634 | QCBOREncode_AddSZString(pECtx, "in a in a in a"); |
| 635 | QCBOREncode_AddTag(pECtx, 9087); |
| 636 | QCBOREncode_OpenMap(pECtx); |
| 637 | QCBOREncode_CloseMap(pECtx); |
| 638 | QCBOREncode_CloseMap(pECtx); |
| 639 | QCBOREncode_CloseMap(pECtx); |
| 640 | QCBOREncode_CloseMap(pECtx); |
| 641 | |
| 642 | /* Extended simple values (these are not standard...) */ |
| 643 | QCBOREncode_OpenMap(pECtx); |
| 644 | QCBOREncode_AddSZString(pECtx, "s1"); |
| 645 | QCBOREncode_AddTag(pECtx, 88); |
| 646 | QCBOREncode_AddSimple(pECtx, 255); |
| 647 | QCBOREncode_AddSimpleToMap(pECtx, "s2", 0); |
| 648 | QCBOREncode_AddSZString(pECtx, "s3"); |
| 649 | QCBOREncode_AddTag(pECtx, 88); |
| 650 | QCBOREncode_AddSimple(pECtx, 33); |
| 651 | QCBOREncode_AddInt64(pECtx, 88378374); // label before tag |
| 652 | QCBOREncode_AddTag(pECtx, 88); |
| 653 | QCBOREncode_AddSimple(pECtx, 255); |
| 654 | QCBOREncode_AddInt64(pECtx, 89); // label before tag |
| 655 | QCBOREncode_AddTag(pECtx, 88); |
| 656 | QCBOREncode_AddSimple(pECtx, 19); |
| 657 | QCBOREncode_CloseMap(pECtx); |
| 658 | |
| 659 | /* UUIDs */ |
| 660 | static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, |
| 661 | 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, |
| 662 | 0x49, 0x43, 0x41, 0x32}; |
| 663 | const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID); |
| 664 | QCBOREncode_AddBinaryUUID(pECtx, XXUUID); |
| 665 | QCBOREncode_OpenMap(pECtx); |
| 666 | QCBOREncode_AddBinaryUUIDToMap(pECtx, "UUUU", XXUUID); |
| 667 | QCBOREncode_AddBinaryUUIDToMapN(pECtx, 99, XXUUID); |
| 668 | QCBOREncode_CloseMap(pECtx); |
| 669 | |
| 670 | /* Bool */ |
| 671 | QCBOREncode_AddBool(pECtx, true); |
| 672 | QCBOREncode_AddBool(pECtx, false); |
| 673 | QCBOREncode_OpenMap(pECtx); |
| 674 | QCBOREncode_AddBoolToMap(pECtx, "George is the man", true); |
| 675 | QCBOREncode_AddBoolToMapN(pECtx, 010101, true); |
| 676 | QCBOREncode_CloseMap(pECtx); |
| 677 | |
| 678 | /* Big numbers */ |
| 679 | static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 680 | const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum); |
| 681 | QCBOREncode_AddPositiveBignum(pECtx, BIGNUM); |
| 682 | QCBOREncode_AddNegativeBignum(pECtx, BIGNUM); |
| 683 | QCBOREncode_OpenMap(pECtx); |
| 684 | QCBOREncode_AddPositiveBignumToMap(pECtx, "BN+", BIGNUM); |
| 685 | QCBOREncode_AddPositiveBignumToMapN(pECtx, 64, BIGNUM); |
| 686 | QCBOREncode_AddNegativeBignumToMap(pECtx, "BN-", BIGNUM); |
| 687 | QCBOREncode_AddNegativeBignumToMapN(pECtx, -64, BIGNUM); |
| 688 | QCBOREncode_CloseMap(pECtx); |
| 689 | |
| 690 | QCBOREncode_CloseArray(pECtx); |
| 691 | } |
| 692 | |
| 693 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 694 | int32_t AllAddMethodsTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 695 | { |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 696 | /* Improvement: this test should be broken down into several so it is more |
| 697 | * managable. Tags and labels could be more sensible */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 698 | QCBOREncodeContext ECtx; |
| 699 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 700 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 701 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 702 | |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 703 | AddAll (&ECtx); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 704 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 705 | UsefulBufC Enc; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 706 | if(QCBOREncode_Finish(&ECtx, &Enc)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 707 | nReturn = -1; |
| 708 | goto Done; |
| 709 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 710 | |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 711 | if(CheckResults(Enc, spExpectedEncodedAll)) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 712 | nReturn = -2; |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | |
| 716 | /* Also test size calculation */ |
| 717 | QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf); |
| 718 | |
| 719 | AddAll (&ECtx); |
| 720 | |
| 721 | size_t size; |
| 722 | if(QCBOREncode_FinishGetSize(&ECtx, &size)) { |
| 723 | nReturn = -10; |
| 724 | goto Done; |
| 725 | } |
| 726 | |
| 727 | if(size != sizeof(spExpectedEncodedAll)) { |
| 728 | nReturn = -11; |
| 729 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 730 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 731 | Done: |
| 732 | return nReturn; |
| 733 | } |
| 734 | |
Laurence Lundblade | 8ece373 | 2021-09-21 21:47:23 -0700 | [diff] [blame] | 735 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 736 | /* |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 737 | 98 30 # array(48) |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 738 | 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807) |
| 739 | 3B 0000000100000000 # negative(4294967296) |
| 740 | 3A FFFFFFFF # negative(4294967295) |
| 741 | 3A FFFFFFFE # negative(4294967294) |
| 742 | 3A FFFFFFFD # negative(4294967293) |
| 743 | 3A 7FFFFFFF # negative(2147483647) |
| 744 | 3A 7FFFFFFE # negative(2147483646) |
| 745 | 3A 00010001 # negative(65537) |
| 746 | 3A 00010000 # negative(65536) |
| 747 | 39 FFFF # negative(65535) |
| 748 | 39 FFFE # negative(65534) |
| 749 | 39 FFFD # negative(65533) |
| 750 | 39 0100 # negative(256) |
| 751 | 38 FF # negative(255) |
| 752 | 38 FE # negative(254) |
| 753 | 38 FD # negative(253) |
| 754 | 38 18 # negative(24) |
| 755 | 37 # negative(23) |
| 756 | 36 # negative(22) |
| 757 | 20 # negative(0) |
| 758 | 00 # unsigned(0) |
| 759 | 00 # unsigned(0) |
| 760 | 01 # unsigned(1) |
| 761 | 16 # unsigned(22) |
| 762 | 17 # unsigned(23) |
| 763 | 18 18 # unsigned(24) |
| 764 | 18 19 # unsigned(25) |
| 765 | 18 1A # unsigned(26) |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 766 | 18 1F # unsigned(31) |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 767 | 18 FE # unsigned(254) |
| 768 | 18 FF # unsigned(255) |
| 769 | 19 0100 # unsigned(256) |
| 770 | 19 0101 # unsigned(257) |
| 771 | 19 FFFE # unsigned(65534) |
| 772 | 19 FFFF # unsigned(65535) |
| 773 | 1A 00010000 # unsigned(65536) |
| 774 | 1A 00010001 # unsigned(65537) |
| 775 | 1A 00010002 # unsigned(65538) |
| 776 | 1A 7FFFFFFF # unsigned(2147483647) |
| 777 | 1A 7FFFFFFF # unsigned(2147483647) |
| 778 | 1A 80000000 # unsigned(2147483648) |
| 779 | 1A 80000001 # unsigned(2147483649) |
| 780 | 1A FFFFFFFE # unsigned(4294967294) |
| 781 | 1A FFFFFFFF # unsigned(4294967295) |
| 782 | 1B 0000000100000000 # unsigned(4294967296) |
| 783 | 1B 0000000100000001 # unsigned(4294967297) |
| 784 | 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807) |
| 785 | 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615) |
| 786 | */ |
| 787 | static const uint8_t spExpectedEncodedInts[] = { |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 788 | 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 789 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 790 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 791 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 792 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 793 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 794 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 795 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 796 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 797 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 798 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 799 | 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, |
| 800 | 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, |
| 801 | 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, |
| 802 | 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, |
| 803 | 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, |
| 804 | 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, |
| 805 | 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, |
| 806 | 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, |
| 807 | 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 808 | 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 809 | 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 810 | 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, |
| 811 | 0xff, 0xff, 0xff, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 812 | |
| 813 | /* |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 814 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 815 | Test the generation of integers. This also ends up testing |
| 816 | encoding of all the different lengths. It encodes integers |
| 817 | of many lengths and values, especially around the boundaries |
| 818 | for different types of integers. It compares the output |
| 819 | to expected values generated from http://cbor.me. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 820 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 821 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 822 | int32_t IntegerValuesTest1(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 823 | { |
| 824 | QCBOREncodeContext ECtx; |
| 825 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 826 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 827 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 828 | QCBOREncode_OpenArray(&ECtx); |
| 829 | |
| 830 | QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1); |
| 831 | QCBOREncode_AddInt64(&ECtx, -4294967297); |
| 832 | QCBOREncode_AddInt64(&ECtx, -4294967296); |
| 833 | QCBOREncode_AddInt64(&ECtx, -4294967295); |
| 834 | QCBOREncode_AddInt64(&ECtx, -4294967294); |
| 835 | QCBOREncode_AddInt64(&ECtx, -2147483648); |
| 836 | QCBOREncode_AddInt64(&ECtx, -2147483647); |
| 837 | QCBOREncode_AddInt64(&ECtx, -65538); |
| 838 | QCBOREncode_AddInt64(&ECtx, -65537); |
| 839 | QCBOREncode_AddInt64(&ECtx, -65536); |
| 840 | QCBOREncode_AddInt64(&ECtx, -65535); |
| 841 | QCBOREncode_AddInt64(&ECtx, -65534); |
| 842 | QCBOREncode_AddInt64(&ECtx, -257); |
| 843 | QCBOREncode_AddInt64(&ECtx, -256); |
| 844 | QCBOREncode_AddInt64(&ECtx, -255); |
| 845 | QCBOREncode_AddInt64(&ECtx, -254); |
| 846 | QCBOREncode_AddInt64(&ECtx, -25); |
| 847 | QCBOREncode_AddInt64(&ECtx, -24); |
| 848 | QCBOREncode_AddInt64(&ECtx, -23); |
| 849 | QCBOREncode_AddInt64(&ECtx, -1); |
| 850 | QCBOREncode_AddInt64(&ECtx, 0); |
| 851 | QCBOREncode_AddUInt64(&ECtx, 0ULL); |
| 852 | QCBOREncode_AddInt64(&ECtx, 1); |
| 853 | QCBOREncode_AddInt64(&ECtx, 22); |
| 854 | QCBOREncode_AddInt64(&ECtx, 23); |
| 855 | QCBOREncode_AddInt64(&ECtx, 24); |
| 856 | QCBOREncode_AddInt64(&ECtx, 25); |
| 857 | QCBOREncode_AddInt64(&ECtx, 26); |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 858 | QCBOREncode_AddInt64(&ECtx, 31); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 859 | QCBOREncode_AddInt64(&ECtx, 254); |
| 860 | QCBOREncode_AddInt64(&ECtx, 255); |
| 861 | QCBOREncode_AddInt64(&ECtx, 256); |
| 862 | QCBOREncode_AddInt64(&ECtx, 257); |
| 863 | QCBOREncode_AddInt64(&ECtx, 65534); |
| 864 | QCBOREncode_AddInt64(&ECtx, 65535); |
| 865 | QCBOREncode_AddInt64(&ECtx, 65536); |
| 866 | QCBOREncode_AddInt64(&ECtx, 65537); |
| 867 | QCBOREncode_AddInt64(&ECtx, 65538); |
| 868 | QCBOREncode_AddInt64(&ECtx, 2147483647); |
| 869 | QCBOREncode_AddInt64(&ECtx, 2147483647); |
| 870 | QCBOREncode_AddInt64(&ECtx, 2147483648); |
| 871 | QCBOREncode_AddInt64(&ECtx, 2147483649); |
| 872 | QCBOREncode_AddInt64(&ECtx, 4294967294); |
| 873 | QCBOREncode_AddInt64(&ECtx, 4294967295); |
| 874 | QCBOREncode_AddInt64(&ECtx, 4294967296); |
| 875 | QCBOREncode_AddInt64(&ECtx, 4294967297); |
| 876 | QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL); |
| 877 | QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 878 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 879 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 880 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 881 | UsefulBufC Enc; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 882 | if(QCBOREncode_Finish(&ECtx, &Enc)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 883 | nReturn = -1; |
| 884 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 885 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 886 | if(CheckResults(Enc, spExpectedEncodedInts)) |
| 887 | return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 888 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 889 | return(nReturn); |
| 890 | } |
| 891 | |
| 892 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 893 | /* |
| 894 | 85 # array(5) |
| 895 | F5 # primitive(21) |
| 896 | F4 # primitive(20) |
| 897 | F6 # primitive(22) |
| 898 | F7 # primitive(23) |
| 899 | A1 # map(1) |
| 900 | 65 # text(5) |
| 901 | 554E446566 # "UNDef" |
| 902 | F7 # primitive(23) |
| 903 | */ |
| 904 | static const uint8_t spExpectedEncodedSimple[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 905 | 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7}; |
| 906 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 907 | int32_t SimpleValuesTest1(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 908 | { |
| 909 | QCBOREncodeContext ECtx; |
| 910 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 911 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 912 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 913 | QCBOREncode_OpenArray(&ECtx); |
Laurence Lundblade | c6ec7ef | 2018-12-04 10:45:06 +0900 | [diff] [blame] | 914 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 915 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE); |
| 916 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE); |
| 917 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL); |
| 918 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 919 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 920 | QCBOREncode_OpenMap(&ECtx); |
| 921 | |
| 922 | QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF); |
| 923 | QCBOREncode_CloseMap(&ECtx); |
| 924 | |
| 925 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 926 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 927 | UsefulBufC ECBOR; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 928 | if(QCBOREncode_Finish(&ECtx, &ECBOR)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 929 | nReturn = -1; |
| 930 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 931 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 932 | if(CheckResults(ECBOR, spExpectedEncodedSimple)) |
| 933 | return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 934 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 935 | return(nReturn); |
| 936 | } |
| 937 | |
Jan Jongboom | 47d86c5 | 2019-07-25 08:54:16 +0200 | [diff] [blame] | 938 | /* |
| 939 | 9F # array(5) |
| 940 | F5 # primitive(21) |
| 941 | F4 # primitive(20) |
| 942 | F6 # primitive(22) |
| 943 | F7 # primitive(23) |
| 944 | BF # map(1) |
| 945 | 65 # text(5) |
| 946 | 554E446566 # "UNDef" |
| 947 | F7 # primitive(23) |
| 948 | FF # break |
| 949 | FF # break |
| 950 | */ |
| 951 | static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = { |
| 952 | 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff}; |
| 953 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 954 | int32_t SimpleValuesIndefiniteLengthTest1(void) |
Jan Jongboom | 47d86c5 | 2019-07-25 08:54:16 +0200 | [diff] [blame] | 955 | { |
| 956 | QCBOREncodeContext ECtx; |
| 957 | int nReturn = 0; |
| 958 | |
| 959 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 960 | QCBOREncode_OpenArrayIndefiniteLength(&ECtx); |
| 961 | |
| 962 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE); |
| 963 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE); |
| 964 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL); |
| 965 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF); |
| 966 | |
| 967 | QCBOREncode_OpenMapIndefiniteLength(&ECtx); |
| 968 | |
| 969 | QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF); |
| 970 | QCBOREncode_CloseMapIndefiniteLength(&ECtx); |
| 971 | |
| 972 | QCBOREncode_CloseArrayIndefiniteLength(&ECtx); |
| 973 | |
| 974 | UsefulBufC ECBOR; |
| 975 | if(QCBOREncode_Finish(&ECtx, &ECBOR)) { |
| 976 | nReturn = -1; |
| 977 | } |
| 978 | |
| 979 | if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength)) |
| 980 | return -2; |
| 981 | |
| 982 | return(nReturn); |
| 983 | } |
| 984 | |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 985 | /* |
| 986 | A5 # map(5) |
| 987 | 63 # text(3) |
| 988 | 617272 # "arr" |
| 989 | 98 1F # array(31) |
| 990 | 00 # unsigned(0) |
| 991 | 01 # unsigned(1) |
| 992 | 02 # unsigned(2) |
| 993 | 03 # unsigned(3) |
| 994 | 04 # unsigned(4) |
| 995 | 05 # unsigned(5) |
| 996 | 06 # unsigned(6) |
| 997 | 07 # unsigned(7) |
| 998 | 08 # unsigned(8) |
| 999 | 09 # unsigned(9) |
| 1000 | 0A # unsigned(10) |
| 1001 | 0B # unsigned(11) |
| 1002 | 0C # unsigned(12) |
| 1003 | 0D # unsigned(13) |
| 1004 | 0E # unsigned(14) |
| 1005 | 0F # unsigned(15) |
| 1006 | 10 # unsigned(16) |
| 1007 | 11 # unsigned(17) |
| 1008 | 12 # unsigned(18) |
| 1009 | 13 # unsigned(19) |
| 1010 | 14 # unsigned(20) |
| 1011 | 15 # unsigned(21) |
| 1012 | 16 # unsigned(22) |
| 1013 | 17 # unsigned(23) |
| 1014 | 18 18 # unsigned(24) |
| 1015 | 18 19 # unsigned(25) |
| 1016 | 18 1A # unsigned(26) |
| 1017 | 18 1B # unsigned(27) |
| 1018 | 18 1C # unsigned(28) |
| 1019 | 18 1D # unsigned(29) |
| 1020 | 18 1E # unsigned(30) |
| 1021 | 63 # text(3) |
| 1022 | 6D6170 # "map" |
| 1023 | B8 1F # map(31) |
| 1024 | 61 # text(1) |
| 1025 | 61 # "a" |
| 1026 | 00 # unsigned(0) |
| 1027 | 61 # text(1) |
| 1028 | 62 # "b" |
| 1029 | 01 # unsigned(1) |
| 1030 | 61 # text(1) |
| 1031 | 63 # "c" |
| 1032 | 02 # unsigned(2) |
| 1033 | 61 # text(1) |
| 1034 | 64 # "d" |
| 1035 | 03 # unsigned(3) |
| 1036 | 61 # text(1) |
| 1037 | 65 # "e" |
| 1038 | 04 # unsigned(4) |
| 1039 | 61 # text(1) |
| 1040 | 66 # "f" |
| 1041 | 05 # unsigned(5) |
| 1042 | 61 # text(1) |
| 1043 | 67 # "g" |
| 1044 | 06 # unsigned(6) |
| 1045 | 61 # text(1) |
| 1046 | 68 # "h" |
| 1047 | 07 # unsigned(7) |
| 1048 | 61 # text(1) |
| 1049 | 69 # "i" |
| 1050 | 08 # unsigned(8) |
| 1051 | 61 # text(1) |
| 1052 | 6A # "j" |
| 1053 | 09 # unsigned(9) |
| 1054 | 61 # text(1) |
| 1055 | 6B # "k" |
| 1056 | 0A # unsigned(10) |
| 1057 | 61 # text(1) |
| 1058 | 6C # "l" |
| 1059 | 0B # unsigned(11) |
| 1060 | 61 # text(1) |
| 1061 | 6D # "m" |
| 1062 | 0C # unsigned(12) |
| 1063 | 61 # text(1) |
| 1064 | 6E # "n" |
| 1065 | 0D # unsigned(13) |
| 1066 | 61 # text(1) |
| 1067 | 6F # "o" |
| 1068 | 0E # unsigned(14) |
| 1069 | 61 # text(1) |
| 1070 | 70 # "p" |
| 1071 | 0F # unsigned(15) |
| 1072 | 61 # text(1) |
| 1073 | 71 # "q" |
| 1074 | 10 # unsigned(16) |
| 1075 | 61 # text(1) |
| 1076 | 72 # "r" |
| 1077 | 11 # unsigned(17) |
| 1078 | 61 # text(1) |
| 1079 | 73 # "s" |
| 1080 | 12 # unsigned(18) |
| 1081 | 61 # text(1) |
| 1082 | 74 # "t" |
| 1083 | 13 # unsigned(19) |
| 1084 | 61 # text(1) |
| 1085 | 75 # "u" |
| 1086 | 14 # unsigned(20) |
| 1087 | 61 # text(1) |
| 1088 | 76 # "v" |
| 1089 | 15 # unsigned(21) |
| 1090 | 61 # text(1) |
| 1091 | 77 # "w" |
| 1092 | 16 # unsigned(22) |
| 1093 | 61 # text(1) |
| 1094 | 78 # "x" |
| 1095 | 17 # unsigned(23) |
| 1096 | 61 # text(1) |
| 1097 | 79 # "y" |
| 1098 | 18 18 # unsigned(24) |
| 1099 | 61 # text(1) |
| 1100 | 7A # "z" |
| 1101 | 18 19 # unsigned(25) |
| 1102 | 61 # text(1) |
| 1103 | 41 # "A" |
| 1104 | 18 1A # unsigned(26) |
| 1105 | 61 # text(1) |
| 1106 | 42 # "B" |
| 1107 | 18 1B # unsigned(27) |
| 1108 | 61 # text(1) |
| 1109 | 43 # "C" |
| 1110 | 18 1C # unsigned(28) |
| 1111 | 61 # text(1) |
| 1112 | 44 # "D" |
| 1113 | 18 1D # unsigned(29) |
| 1114 | 61 # text(1) |
| 1115 | 45 # "E" |
| 1116 | 18 1E # unsigned(30) |
| 1117 | 65 # text(5) |
| 1118 | 6D696E3331 # "min31" |
| 1119 | 38 1E # negative(30) |
| 1120 | 66 # text(6) |
| 1121 | 706C75733331 # "plus31" |
| 1122 | 18 1F # unsigned(31) |
| 1123 | 63 # text(3) |
| 1124 | 737472 # "str" |
| 1125 | 78 1F # text(31) |
| 1126 | 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11" |
| 1127 | */ |
| 1128 | static const uint8_t EncodeLengthThirtyone[] = { |
| 1129 | 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04, |
| 1130 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 1131 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 1132 | 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61, |
| 1133 | 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02, |
| 1134 | 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06, |
| 1135 | 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a, |
| 1136 | 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e, |
| 1137 | 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12, |
| 1138 | 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16, |
| 1139 | 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61, |
| 1140 | 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61, |
| 1141 | 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33, |
| 1142 | 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f, |
| 1143 | 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, |
| 1144 | 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, |
| 1145 | 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31, |
| 1146 | 0x31 |
| 1147 | }; |
| 1148 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1149 | int32_t EncodeLengthThirtyoneTest(void) |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1150 | { |
| 1151 | QCBOREncodeContext ECtx; |
| 1152 | int nReturn = 0; |
| 1153 | |
| 1154 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 1155 | QCBOREncode_OpenMap(&ECtx); |
| 1156 | |
| 1157 | // add array with 31 items |
| 1158 | QCBOREncode_OpenArrayInMap(&ECtx, "arr"); |
| 1159 | for (size_t ix = 0; ix < 31; ix++) { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1160 | QCBOREncode_AddInt64(&ECtx, (int64_t)ix); |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1161 | } |
| 1162 | QCBOREncode_CloseArray(&ECtx); |
| 1163 | |
| 1164 | // add map with 31 items |
| 1165 | QCBOREncode_OpenMapInMap(&ECtx, "map"); |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1166 | for (int ix = 0; ix < 31; ix++) { |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1167 | // make sure we have unique keys in the map (a-z then follow by A-Z) |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1168 | int c = 'a'; |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1169 | if (ix < 26) c = c + ix; |
| 1170 | else c = 'A' + (ix - 26); |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1171 | char buffer[2] = { (char)c, 0 }; |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1172 | QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix); |
| 1173 | } |
| 1174 | QCBOREncode_CloseMap(&ECtx); |
| 1175 | |
| 1176 | // add -31 and +31 |
| 1177 | QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31); |
| 1178 | QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31); |
| 1179 | |
| 1180 | // add string with length 31 |
| 1181 | const char *str = "testtesttesttesttesttestqcbor11"; |
| 1182 | UsefulBufC str_b = { str, 31 }; |
| 1183 | QCBOREncode_AddTextToMap(&ECtx, "str", str_b); |
| 1184 | |
| 1185 | QCBOREncode_CloseMap(&ECtx); |
| 1186 | |
| 1187 | UsefulBufC ECBOR; |
| 1188 | if(QCBOREncode_Finish(&ECtx, &ECBOR)) { |
| 1189 | nReturn = -1; |
| 1190 | } |
| 1191 | |
| 1192 | if(CheckResults(ECBOR, EncodeLengthThirtyone)) |
| 1193 | return -2; |
| 1194 | |
| 1195 | return(nReturn); |
| 1196 | } |
| 1197 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1198 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1199 | /* |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1200 | * [ "2013-03-21T20:04:00Z", |
| 1201 | * 0("2013-03-21T20:04:00Z"), |
| 1202 | * 1363896240, |
| 1203 | * 1(1363896240), |
| 1204 | * 100(-10676), |
| 1205 | * 3994, |
| 1206 | * 1004("1940-10-09"), |
| 1207 | * "1980-12-08", |
| 1208 | * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"), |
| 1209 | * "SD": 1(999), |
| 1210 | * "Sample Date from RFC 8943": "1985-04-12", |
| 1211 | * 42: 1004("1985-04-12T23:20:50.52Z"), |
| 1212 | * "SY": 100(-10676), |
| 1213 | * 45: 3994 |
| 1214 | * } |
| 1215 | * ] |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1216 | */ |
| 1217 | static const uint8_t spExpectedEncodedDates[] = { |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1218 | 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D, |
| 1219 | 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30, |
| 1220 | 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, |
| 1221 | 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, |
| 1222 | 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1, |
| 1223 | 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3, |
| 1224 | 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34, |
| 1225 | 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39, |
| 1226 | 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78, |
| 1227 | 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, |
| 1228 | 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, |
| 1229 | 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39, |
| 1230 | 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32, |
| 1231 | 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32, |
| 1232 | 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19, |
| 1233 | 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74, |
| 1234 | 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43, |
| 1235 | 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35, |
| 1236 | 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03, |
| 1237 | 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, |
| 1238 | 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, |
| 1239 | 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64, |
| 1240 | 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1241 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1242 | int32_t EncodeDateTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1243 | { |
| 1244 | QCBOREncodeContext ECtx; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1245 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1246 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1247 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1248 | QCBOREncode_OpenArray(&ECtx); |
| 1249 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1250 | /* The values are taken from the CBOR RFCs */ |
| 1251 | QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z"); |
| 1252 | QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); |
| 1253 | QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240); |
| 1254 | QCBOREncode_AddDateEpoch(&ECtx, 1363896240); |
| 1255 | QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676); |
| 1256 | QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994); |
| 1257 | QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09"); |
| 1258 | QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08"); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1259 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1260 | QCBOREncode_OpenMap(&ECtx); |
| 1261 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1262 | QCBOREncode_AddDateStringToMap(&ECtx, |
| 1263 | "Sample Date from RFC 3339", |
| 1264 | "1985-04-12T23:20:50.52Z"); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1265 | QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1266 | QCBOREncode_AddTDaysStringToMapSZ(&ECtx, |
| 1267 | "Sample Date from RFC 8943", |
| 1268 | QCBOR_ENCODE_AS_BORROWED, |
| 1269 | "1985-04-12"); |
| 1270 | QCBOREncode_AddTDaysStringToMapN(&ECtx, |
| 1271 | 42, |
| 1272 | QCBOR_ENCODE_AS_TAG, |
| 1273 | "1985-04-12T23:20:50.52Z"); |
| 1274 | QCBOREncode_AddTDaysEpochToMapSZ(&ECtx, |
| 1275 | "SY", |
| 1276 | QCBOR_ENCODE_AS_TAG, |
| 1277 | -10676); |
| 1278 | QCBOREncode_AddTDaysEpochToMapN(&ECtx, |
| 1279 | 45, |
| 1280 | QCBOR_ENCODE_AS_BORROWED, |
| 1281 | 3994); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1282 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1283 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1284 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1285 | QCBOREncode_CloseArray(&ECtx); |
| 1286 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1287 | UsefulBufC ECBOR; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1288 | if(QCBOREncode_Finish(&ECtx, &ECBOR)) { |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1289 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1291 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1292 | if(CheckResults(ECBOR, spExpectedEncodedDates)) |
| 1293 | return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1294 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1295 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1298 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1299 | int32_t ArrayNestingTest1(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1300 | { |
| 1301 | QCBOREncodeContext ECtx; |
| 1302 | int i; |
| 1303 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1304 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1305 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1306 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 1307 | QCBOREncode_OpenArray(&ECtx); |
| 1308 | } |
| 1309 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 1310 | QCBOREncode_CloseArray(&ECtx); |
| 1311 | } |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1312 | UsefulBufC Encoded; |
| 1313 | if(QCBOREncode_Finish(&ECtx, &Encoded)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1314 | nReturn = -1; |
| 1315 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1316 | |
| 1317 | return(nReturn); |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1322 | int32_t ArrayNestingTest2(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1323 | { |
| 1324 | QCBOREncodeContext ECtx; |
| 1325 | int i; |
| 1326 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1327 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1328 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1329 | for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) { |
| 1330 | QCBOREncode_OpenArray(&ECtx); |
| 1331 | } |
| 1332 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 1333 | QCBOREncode_CloseArray(&ECtx); |
| 1334 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1335 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1336 | UsefulBufC Encoded; |
| 1337 | if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1338 | nReturn = -1; |
| 1339 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1340 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1341 | return(nReturn); |
| 1342 | } |
| 1343 | |
| 1344 | |
| 1345 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1346 | int32_t ArrayNestingTest3(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1347 | { |
| 1348 | QCBOREncodeContext ECtx; |
| 1349 | int i; |
| 1350 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1351 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1352 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1353 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 1354 | QCBOREncode_OpenArray(&ECtx); |
| 1355 | } |
| 1356 | for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) { |
| 1357 | QCBOREncode_CloseArray(&ECtx); |
| 1358 | } |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1359 | UsefulBufC Encoded; |
| 1360 | if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1361 | nReturn = -1; |
| 1362 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1363 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1364 | return(nReturn); |
| 1365 | } |
| 1366 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1367 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1368 | /* |
| 1369 | 81 # array(1) |
| 1370 | 81 # array(1) |
| 1371 | 81 # array(1) |
| 1372 | 81 # array(1) |
| 1373 | 80 # array(0) |
| 1374 | */ |
| 1375 | static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1376 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1377 | // Validated at http://cbor.me and by manually examining its output |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1378 | /* |
| 1379 | 82 # array(2) |
| 1380 | 81 # array(1) |
| 1381 | 81 # array(1) |
| 1382 | 81 # array(1) |
| 1383 | 81 # array(1) |
| 1384 | 80 # array(0) |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1385 | 98 30 # array(48) |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1386 | 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807) |
| 1387 | 3B 0000000100000000 # negative(4294967296) |
| 1388 | 3A FFFFFFFF # negative(4294967295) |
| 1389 | 3A FFFFFFFE # negative(4294967294) |
| 1390 | 3A FFFFFFFD # negative(4294967293) |
| 1391 | 3A 7FFFFFFF # negative(2147483647) |
| 1392 | 3A 7FFFFFFE # negative(2147483646) |
| 1393 | 3A 00010001 # negative(65537) |
| 1394 | 3A 00010000 # negative(65536) |
| 1395 | 39 FFFF # negative(65535) |
| 1396 | 39 FFFE # negative(65534) |
| 1397 | 39 FFFD # negative(65533) |
| 1398 | 39 0100 # negative(256) |
| 1399 | 38 FF # negative(255) |
| 1400 | 38 FE # negative(254) |
| 1401 | 38 FD # negative(253) |
| 1402 | 38 18 # negative(24) |
| 1403 | 37 # negative(23) |
| 1404 | 36 # negative(22) |
| 1405 | 20 # negative(0) |
| 1406 | 00 # unsigned(0) |
| 1407 | 00 # unsigned(0) |
| 1408 | 01 # unsigned(1) |
| 1409 | 16 # unsigned(22) |
| 1410 | 17 # unsigned(23) |
| 1411 | 18 18 # unsigned(24) |
| 1412 | 18 19 # unsigned(25) |
| 1413 | 18 1A # unsigned(26) |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1414 | 18 1F # unsigned(31) |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1415 | 18 FE # unsigned(254) |
| 1416 | 18 FF # unsigned(255) |
| 1417 | 19 0100 # unsigned(256) |
| 1418 | 19 0101 # unsigned(257) |
| 1419 | 19 FFFE # unsigned(65534) |
| 1420 | 19 FFFF # unsigned(65535) |
| 1421 | 1A 00010000 # unsigned(65536) |
| 1422 | 1A 00010001 # unsigned(65537) |
| 1423 | 1A 00010002 # unsigned(65538) |
| 1424 | 1A 7FFFFFFF # unsigned(2147483647) |
| 1425 | 1A 7FFFFFFF # unsigned(2147483647) |
| 1426 | 1A 80000000 # unsigned(2147483648) |
| 1427 | 1A 80000001 # unsigned(2147483649) |
| 1428 | 1A FFFFFFFE # unsigned(4294967294) |
| 1429 | 1A FFFFFFFF # unsigned(4294967295) |
| 1430 | 1B 0000000100000000 # unsigned(4294967296) |
| 1431 | 1B 0000000100000001 # unsigned(4294967297) |
| 1432 | 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807) |
| 1433 | 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615) |
| 1434 | */ |
| 1435 | static const uint8_t spEncodeRawExpected[] = { |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1436 | 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30, |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1437 | 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 1438 | 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 1439 | 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a, |
| 1440 | 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff, |
| 1441 | 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f, |
| 1442 | 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01, |
| 1443 | 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff, |
| 1444 | 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01, |
| 1445 | 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38, |
| 1446 | 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16, |
| 1447 | 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18, |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 1448 | 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 1449 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 1450 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 1451 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 1452 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 1453 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 1454 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 1455 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 1456 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 1457 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 1458 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 1459 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 1460 | 0xff, 0xff}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1461 | |
| 1462 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1463 | int32_t EncodeRawTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1464 | { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1465 | QCBOREncodeContext ECtx; |
| 1466 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1467 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1468 | QCBOREncode_OpenArray(&ECtx); |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1469 | QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys)); |
| 1470 | QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts)); |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1471 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1472 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1473 | UsefulBufC EncodedRawTest; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1474 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1475 | if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1476 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1477 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1478 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1479 | if(CheckResults(EncodedRawTest, spEncodeRawExpected)) { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1480 | return -5; |
| 1481 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1482 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1483 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1484 | } |
| 1485 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1486 | /* |
| 1487 | This returns a pointer to spBigBuf |
| 1488 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1489 | static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1490 | { |
| 1491 | QCBOREncodeContext ECtx; |
| 1492 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1493 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1494 | *pEncoded = NULL; |
| 1495 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1496 | size_t uFirstSizeEstimate = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1497 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1498 | // loop runs CBOR encoding twice. First with no buffer to |
| 1499 | // calucate the length so buffer can be allocated correctly, |
| 1500 | // and last with the buffer to do the actual encoding |
| 1501 | do { |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1502 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1503 | QCBOREncode_OpenMap(&ECtx); |
| 1504 | QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42); |
| 1505 | QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings"); |
| 1506 | QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7})); |
| 1507 | QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7})); |
| 1508 | QCBOREncode_CloseArray(&ECtx); |
| 1509 | QCBOREncode_OpenMapInMap(&ECtx, "map in a map"); |
| 1510 | QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4})); |
| 1511 | QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4})); |
| 1512 | QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98); |
| 1513 | QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30})); |
| 1514 | QCBOREncode_CloseMap(&ECtx); |
| 1515 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1516 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1517 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1518 | goto Done; |
| 1519 | if(*pEncoded != NULL) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1520 | if(uFirstSizeEstimate != *pEncodedLen) { |
| 1521 | nReturn = 1; |
| 1522 | } else { |
| 1523 | nReturn = 0; |
| 1524 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1525 | goto Done; |
| 1526 | } |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1527 | *pEncoded = spBigBuf; |
| 1528 | uFirstSizeEstimate = *pEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1529 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1530 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1531 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1532 | Done: |
| 1533 | return(nReturn); |
| 1534 | } |
| 1535 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1536 | /* |
| 1537 | A3 # map(3) |
| 1538 | 6D # text(13) |
| 1539 | 666972737420696E7465676572 # "first integer" |
| 1540 | 18 2A # unsigned(42) |
| 1541 | 77 # text(23) |
| 1542 | 616E206172726179206F662074776F20737472696E6773 # "an array of two strings" |
| 1543 | 82 # array(2) |
| 1544 | 67 # text(7) |
| 1545 | 737472696E6731 # "string1" |
| 1546 | 67 # text(7) |
| 1547 | 737472696E6732 # "string2" |
| 1548 | 6C # text(12) |
| 1549 | 6D617020696E2061206D6170 # "map in a map" |
| 1550 | A4 # map(4) |
| 1551 | 67 # text(7) |
| 1552 | 62797465732031 # "bytes 1" |
| 1553 | 44 # bytes(4) |
| 1554 | 78787878 # "xxxx" |
| 1555 | 67 # text(7) |
| 1556 | 62797465732032 # "bytes 2" |
| 1557 | 44 # bytes(4) |
| 1558 | 79797979 # "yyyy" |
| 1559 | 6B # text(11) |
| 1560 | 616E6F7468657220696E74 # "another int" |
| 1561 | 18 62 # unsigned(98) |
| 1562 | 66 # text(6) |
| 1563 | 746578742032 # "text 2" |
| 1564 | 78 1E # text(30) |
| 1565 | 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics" |
| 1566 | */ |
| 1567 | static const uint8_t spValidMapEncoded[] = { |
| 1568 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 1569 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 1570 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 1571 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 1572 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 1573 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 1574 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 1575 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 1576 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 1577 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 1578 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 1579 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 1580 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 1581 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 1582 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 1583 | 0x73 } ; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1584 | |
| 1585 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1586 | int32_t MapEncodeTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1587 | { |
| 1588 | uint8_t *pEncodedMaps; |
| 1589 | size_t nEncodedMapLen; |
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(CreateMap(&pEncodedMaps, &nEncodedMapLen)) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1592 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1593 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1594 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1595 | int nReturn = 0; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1596 | if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded))) |
| 1597 | nReturn = 2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1598 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1599 | return(nReturn); |
| 1600 | } |
| 1601 | |
| 1602 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1603 | /* |
| 1604 | @brief Encode the RTIC results |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1605 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1606 | @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or |
| 1607 | CBOR_SIMPLEV_NULL |
| 1608 | @param[in] time Time stamp in UNIX epoch time or 0 for none |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1609 | @param[in] szAlexString Diagnostic code. |
| 1610 | @param[in[ pOut Buffer to put the result in |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1611 | @param[in/out] pnLen Size of pOut buffer when called; length of data |
| 1612 | output in buffer on return |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1613 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1614 | @return |
| 1615 | One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1616 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1617 | The size of pOut should be 30 bytes plus the length of pnLen. If you make it too |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1618 | short an error will be returned. This function will never write off the end |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1619 | of the buffer passed to it. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1620 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1621 | If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the |
| 1622 | length of the encoded CBOR. |
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 | */ |
| 1625 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1626 | static UsefulBufC |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1627 | FormatRTICResults(uint8_t uRResult, |
| 1628 | int64_t time, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1629 | const char *szType, |
| 1630 | const char *szAlexString, |
| 1631 | UsefulBuf Storage) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1632 | { |
| 1633 | // Buffer that the result will be written in to |
| 1634 | // It is fixed size and small that a stack variable will be fine |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1635 | // QCBOREncode will never write off the end of this buffer. If it won't |
| 1636 | // fit QCBOREncode_Finish will return an error. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1637 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1638 | // Context for the encoder |
| 1639 | QCBOREncodeContext ECtx; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1640 | QCBOREncode_Init(&ECtx, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1641 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1642 | // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object |
| 1643 | // Contents are label / value pairs |
| 1644 | QCBOREncode_OpenMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1645 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1646 | { // Brace / indention just to show CBOR encoding nesting |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1647 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1648 | // The result: 0 if scan happened and found nothing; 1 if it happened and |
| 1649 | // found something wrong; 2 if it didn't happen |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1650 | QCBOREncode_AddSimpleToMap(&ECtx, "integrity", uRResult); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1651 | |
| 1652 | // Add the diagnostic code |
| 1653 | QCBOREncode_AddSZStringToMap(&ECtx, "type", szType); |
| 1654 | |
| 1655 | // Add a time stamp |
| 1656 | if(time) { |
| 1657 | QCBOREncode_AddDateEpochToMap(&ECtx, "time", time); |
| 1658 | } |
| 1659 | |
| 1660 | // Add the diagnostic code |
| 1661 | QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString); |
| 1662 | |
| 1663 | // Open a subordinate map for telemtry data |
| 1664 | QCBOREncode_OpenMapInMap(&ECtx, "telemetry"); |
| 1665 | |
| 1666 | { // Brace / indention just to show CBOR encoding nesting |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1667 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1668 | // Add a few fake integers and buffers for now. |
| 1669 | QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12); |
| 1670 | |
| 1671 | // Add a few fake integers and buffers for now. |
| 1672 | QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff); |
| 1673 | |
| 1674 | // Add a few fake integers and buffers for now. |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1675 | static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01}; |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 1676 | const UsefulBufC WSPV = {pPV, sizeof(pPV)}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1677 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1678 | QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV); |
| 1679 | } |
| 1680 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1681 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1682 | // Close the telemetry map |
| 1683 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1684 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1685 | // Close the map |
| 1686 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1687 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1688 | UsefulBufC Result; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1689 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1690 | QCBOREncode_Finish(&ECtx, &Result); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1691 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1692 | return Result; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1696 | /* |
| 1697 | A5 # map(5) |
| 1698 | 69 # text(9) |
| 1699 | 696E74656772697479 # "integrity" |
| 1700 | F4 # primitive(20) |
| 1701 | 64 # text(4) |
| 1702 | 74797065 # "type" |
| 1703 | 66 # text(6) |
| 1704 | 726563656E74 # "recent" |
| 1705 | 64 # text(4) |
| 1706 | 74696D65 # "time" |
| 1707 | C1 # tag(1) |
| 1708 | 1A 580D4172 # unsigned(1477263730) |
| 1709 | 64 # text(4) |
| 1710 | 64696167 # "diag" |
| 1711 | 6A # text(10) |
| 1712 | 30784131654335303031 # "0xA1eC5001" |
| 1713 | 69 # text(9) |
| 1714 | 74656C656D65747279 # "telemetry" |
| 1715 | A3 # map(3) |
| 1716 | 69 # text(9) |
| 1717 | 53686F652053697A65 # "Shoe Size" |
| 1718 | 0C # unsigned(12) |
| 1719 | 62 # text(2) |
| 1720 | 4951 # "IQ" |
| 1721 | 1A FFFFFFFF # unsigned(4294967295) |
| 1722 | 77 # text(23) |
| 1723 | 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector" |
| 1724 | 48 # bytes(8) |
| 1725 | 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01" |
| 1726 | */ |
| 1727 | static const uint8_t spExpectedRTIC[] = { |
| 1728 | 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74, |
| 1729 | 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65, |
| 1730 | 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1, |
| 1731 | 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67, |
| 1732 | 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30, |
| 1733 | 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72, |
| 1734 | 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69, |
| 1735 | 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff, |
| 1736 | 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61, |
| 1737 | 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56, |
| 1738 | 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56, |
| 1739 | 0xaa, 0xbb, 0x01, 0x01}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1740 | |
| 1741 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1742 | int32_t RTICResultsTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1743 | { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 1744 | const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1745 | "recent", "0xA1eC5001", |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1746 | UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1747 | if(UsefulBuf_IsNULLC(Encoded)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1748 | return -1; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1749 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1750 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1751 | if(CheckResults(Encoded, spExpectedRTIC)) { |
| 1752 | return -2; |
| 1753 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1754 | |
| 1755 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1756 | } |
| 1757 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1758 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1759 | /* |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1760 | The expected encoding for first test in BstrWrapTest() |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 1761 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1762 | 82 # array(2) |
| 1763 | 19 01C3 # unsigned(451) |
| 1764 | 43 # bytes(3) |
| 1765 | 1901D2 # "\x19\x01\xD2" |
| 1766 | */ |
| 1767 | static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2}; |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1768 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1769 | /* |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 1770 | 81 #array(1) |
| 1771 | 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes") |
| 1772 | */ |
| 1773 | static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25}; |
| 1774 | |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1775 | static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A}; |
| 1776 | |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 1777 | /* |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1778 | * bstr wrapping test |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1779 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1780 | int32_t BstrWrapTest(void) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1781 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1782 | QCBOREncodeContext EC; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1783 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1784 | // First test - make some wrapped CBOR and see that it is as expected |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1785 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1786 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1787 | QCBOREncode_OpenArray(&EC); |
| 1788 | QCBOREncode_AddUInt64(&EC, 451); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1789 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1790 | QCBOREncode_BstrWrap(&EC); |
| 1791 | QCBOREncode_AddUInt64(&EC, 466); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1792 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1793 | UsefulBufC Wrapped; |
| 1794 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1795 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1796 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1797 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1798 | UsefulBufC Encoded; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1799 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1800 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1801 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1802 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1803 | if(CheckResults(Encoded, spExpectedBstrWrap)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1804 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1805 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 1806 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1807 | // Second test - see if the length of the wrapped |
| 1808 | // bstr is correct. Also tests bstr wrapping |
| 1809 | // in length calculation only mode. |
Laurence Lundblade | 7412f81 | 2019-01-01 18:49:36 -0800 | [diff] [blame] | 1810 | QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX}); |
| 1811 | QCBOREncode_OpenArray(&EC); |
| 1812 | QCBOREncode_BstrWrap(&EC); |
| 1813 | QCBOREncode_OpenArray(&EC); |
| 1814 | QCBOREncode_AddNULL(&EC); |
| 1815 | QCBOREncode_CloseArray(&EC); |
| 1816 | UsefulBufC BStr; |
| 1817 | QCBOREncode_CloseBstrWrap(&EC, &BStr); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1818 | // 3 is one byte for the wrapping bstr, 1 for an array of length 1, |
| 1819 | // and 1 byte for a NULL |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1820 | if(BStr.ptr != NULL || BStr.len != 3) { |
Laurence Lundblade | 7412f81 | 2019-01-01 18:49:36 -0800 | [diff] [blame] | 1821 | return -5; |
| 1822 | } |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 1823 | |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 1824 | // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the |
| 1825 | // bstr wrapping use cases. |
| 1826 | UsefulBuf_MAKE_STACK_UB(StuffBuf, 50); |
| 1827 | QCBOREncode_Init(&EC, StuffBuf); |
| 1828 | QCBOREncode_OpenArray(&EC); |
| 1829 | QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes")); |
| 1830 | QCBOREncode_CloseArray(&EC); |
| 1831 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
| 1832 | return -6; |
| 1833 | } |
| 1834 | if(CheckResults(Encoded, spExpectedTypeAndLen)) { |
| 1835 | return -7; |
| 1836 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1837 | |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1838 | // Fourth test, cancelling a byte string |
| 1839 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 1840 | |
| 1841 | QCBOREncode_OpenArray(&EC); |
| 1842 | QCBOREncode_AddUInt64(&EC, 451); |
| 1843 | |
| 1844 | QCBOREncode_BstrWrap(&EC); |
| 1845 | QCBOREncode_CancelBstrWrap(&EC); |
| 1846 | |
| 1847 | |
| 1848 | QCBOREncode_AddUInt64(&EC, 42); |
| 1849 | QCBOREncode_CloseArray(&EC); |
| 1850 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
| 1851 | return -8; |
| 1852 | } |
| 1853 | if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) { |
| 1854 | return -9; |
| 1855 | } |
| 1856 | |
| 1857 | QCBORError uErr; |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1858 | // Fifth test, failed cancelling |
| 1859 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 1860 | |
| 1861 | QCBOREncode_OpenArray(&EC); |
| 1862 | QCBOREncode_AddUInt64(&EC, 451); |
| 1863 | |
| 1864 | QCBOREncode_BstrWrap(&EC); |
| 1865 | QCBOREncode_AddUInt64(&EC, 99); |
| 1866 | QCBOREncode_CancelBstrWrap(&EC); |
| 1867 | |
| 1868 | QCBOREncode_AddUInt64(&EC, 42); |
| 1869 | QCBOREncode_CloseArray(&EC); |
| 1870 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1871 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1872 | if(uErr != QCBOR_ERR_CANNOT_CANCEL) { |
| 1873 | return -10; |
| 1874 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1875 | #else |
| 1876 | if(uErr != QCBOR_SUCCESS) { |
| 1877 | return -110; |
| 1878 | } |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1879 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 1880 | |
| 1881 | // Sixth test, another cancel, but the error is not caught |
| 1882 | // This use will produce unintended CBOR. The error |
| 1883 | // is not caught because it would require tracking state |
| 1884 | // for QCBOREncode_BstrWrapInMapN. |
| 1885 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 1886 | |
| 1887 | QCBOREncode_OpenMap(&EC); |
| 1888 | QCBOREncode_AddUInt64ToMapN(&EC, 451, 88); |
| 1889 | |
| 1890 | QCBOREncode_BstrWrapInMapN(&EC, 55); |
| 1891 | QCBOREncode_CancelBstrWrap(&EC); |
| 1892 | |
| 1893 | QCBOREncode_CloseMap(&EC); |
| 1894 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
| 1895 | if(uErr != QCBOR_SUCCESS) { |
| 1896 | return -11; |
| 1897 | } |
| 1898 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1899 | return 0; |
| 1900 | } |
| 1901 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1902 | |
| 1903 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1904 | int32_t BstrWrapErrorTest(void) |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1905 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1906 | QCBOREncodeContext EC; |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1907 | UsefulBufC Wrapped; |
| 1908 | UsefulBufC Encoded2; |
| 1909 | QCBORError uError; |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 1910 | |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 1911 | // ---- Test closing a bstrwrap when it is an array that is open --------- |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1912 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1913 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1914 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1915 | QCBOREncode_OpenArray(&EC); |
| 1916 | QCBOREncode_AddUInt64(&EC, 451); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1917 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1918 | QCBOREncode_BstrWrap(&EC); |
| 1919 | QCBOREncode_AddUInt64(&EC, 466); |
| 1920 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1921 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1922 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1923 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1924 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1925 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1926 | uError = QCBOREncode_Finish(&EC, &Encoded2); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1927 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1928 | if(uError != QCBOR_ERR_CLOSE_MISMATCH) { |
Laurence Lundblade | b19ad28 | 2020-12-11 16:40:19 -0800 | [diff] [blame] | 1929 | return (int32_t)(100 + uError); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1930 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1931 | #else |
| 1932 | /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 1933 | * is set and not to be sure to excerice all the relavant code in |
| 1934 | * both conditions. When the guards are disabled, there is no |
| 1935 | * error returned, but the code path is still covered. |
| 1936 | */ |
| 1937 | if(uError != QCBOR_SUCCESS) { |
| 1938 | return (int32_t)(600 + uError); |
| 1939 | } |
| 1940 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1941 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1942 | // -------- test closing a bstrwrap when nothing is open ---------------- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1943 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1944 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1945 | uError = QCBOREncode_Finish(&EC, &Encoded2); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1946 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1947 | if(uError != QCBOR_ERR_TOO_MANY_CLOSES) { |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1948 | return (int32_t)(700 + uError); |
| 1949 | } |
| 1950 | #else |
| 1951 | if(uError != QCBOR_SUCCESS) { |
| 1952 | return (int32_t)(800 + uError); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1953 | } |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 1954 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1955 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1956 | // --------------- test nesting too deep ---------------------------------- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1957 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1958 | for(int i = 1; i < 18; i++) { |
| 1959 | QCBOREncode_BstrWrap(&EC); |
| 1960 | } |
| 1961 | QCBOREncode_AddBool(&EC, true); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1962 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1963 | for(int i = 1; i < 18; i++) { |
| 1964 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
| 1965 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1966 | |
Laurence Lundblade | 4bf6e71 | 2020-12-10 11:53:21 -0800 | [diff] [blame] | 1967 | uError = QCBOREncode_Finish(&EC, &Encoded2); |
| 1968 | if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
Laurence Lundblade | b19ad28 | 2020-12-11 16:40:19 -0800 | [diff] [blame] | 1969 | return (int32_t)(300 + uError); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1970 | } |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 1971 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1976 | /* |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1977 | This is bstr wrapped CBOR in 6 levels. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1978 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1979 | [ |
| 1980 | h'82004E82014B8202488203458204428105', |
| 1981 | { |
| 1982 | 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121 |
| 1983 | 8426568656C6C6F18234BA2131318436568656C6C6F' |
| 1984 | } |
| 1985 | ] |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1986 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1987 | Unwrapping the first byte string in the above gives |
| 1988 | [0, h'82014B8202488203458204428105'] |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1989 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1990 | Unwrapping again, the byte string immediately above gives |
| 1991 | [1, h'8202488203458204428105'] |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1992 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1993 | ... |
| 1994 | |
| 1995 | Unrapping the second byte string in the top-level CBOR above gives |
| 1996 | {16: 16, |
| 1997 | 64: "hello", |
| 1998 | 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2.... |
| 1999 | } |
| 2000 | |
| 2001 | Unwrapping again, the byte string immediately above gives |
| 2002 | {17: 17, |
| 2003 | 65: "hello", |
| 2004 | 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F' |
| 2005 | } |
| 2006 | |
| 2007 | ... |
| 2008 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 2009 | */ |
| 2010 | static const uint8_t spExpectedDeepBstr[] = |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2011 | { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2012 | 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B, |
| 2013 | 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04, |
| 2014 | 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37, |
| 2015 | 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65, |
| 2016 | 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3, |
| 2017 | 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, |
| 2018 | 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12, |
| 2019 | 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C, |
| 2020 | 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18, |
| 2021 | 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2022 | }; |
| 2023 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2024 | |
| 2025 | /* |
| 2026 | Get an int64 out of the decoder or fail. |
| 2027 | */ |
| 2028 | static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt) |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2029 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2030 | QCBORItem Item; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2031 | int32_t nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2032 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2033 | nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2034 | if(nReturn) { |
| 2035 | return nReturn; |
| 2036 | } |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2037 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2038 | return -1; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2039 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2040 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2041 | *pInt = Item.val.int64; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2042 | return 0; |
| 2043 | } |
| 2044 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2045 | /* |
| 2046 | Get an array out of the decoder or fail. |
| 2047 | */ |
| 2048 | static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt) |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2049 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2050 | QCBORItem Item; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2051 | int32_t nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2052 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2053 | nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2054 | if(nReturn) { |
| 2055 | return nReturn; |
| 2056 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2057 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2058 | return -1; |
| 2059 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2060 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2061 | *pInt = Item.val.uCount; |
| 2062 | return 0; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | Get a map out of the decoder or fail. |
| 2067 | */ |
| 2068 | static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt) |
| 2069 | { |
| 2070 | QCBORItem Item; |
| 2071 | int32_t nReturn; |
| 2072 | |
| 2073 | nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2074 | if(nReturn) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2075 | return nReturn; |
| 2076 | } |
| 2077 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 2078 | return -1; |
| 2079 | } |
| 2080 | |
| 2081 | *pInt = Item.val.uCount; |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | /* |
| 2086 | Get a byte string out of the decoder or fail. |
| 2087 | */ |
| 2088 | static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr) |
| 2089 | { |
| 2090 | QCBORItem Item; |
| 2091 | int32_t nReturn; |
| 2092 | |
| 2093 | nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item); |
| 2094 | if(nReturn) { |
| 2095 | return nReturn; |
| 2096 | } |
| 2097 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2098 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | *pBstr = Item.val.string; |
| 2102 | return 0; |
| 2103 | } |
| 2104 | |
| 2105 | /* |
| 2106 | Get a byte string out of the decoder or fail. |
| 2107 | */ |
| 2108 | static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr) |
| 2109 | { |
| 2110 | QCBORItem Item; |
| 2111 | int nReturn; |
| 2112 | |
| 2113 | nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item); |
| 2114 | if(nReturn) { |
| 2115 | return nReturn; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2116 | } |
| 2117 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2118 | return -1; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2119 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2120 | |
| 2121 | *pTstr = Item.val.string; |
| 2122 | return 0; |
| 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | /* |
| 2127 | Recursively decode array containing a little CBOR and a bstr wrapped array |
| 2128 | with a little CBOR and a bstr wrapped array... |
| 2129 | |
| 2130 | Part of bstr_wrap_nest_test. |
| 2131 | */static int32_t DecodeNextNested(UsefulBufC Wrapped) |
| 2132 | { |
| 2133 | int64_t nInt; |
| 2134 | UsefulBufC Bstr; |
| 2135 | uint16_t nArrayCount; |
| 2136 | QCBORDecodeContext DC; |
| 2137 | int32_t nResult; |
| 2138 | |
| 2139 | QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL); |
| 2140 | |
| 2141 | if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) { |
| 2142 | return -10; |
| 2143 | } |
| 2144 | |
| 2145 | if(GetInt64(&DC, &nInt)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2146 | return -11; |
| 2147 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2148 | |
| 2149 | nResult = GetByteString(&DC, &Bstr); |
| 2150 | if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) { |
| 2151 | if(nArrayCount != 1) { |
| 2152 | return -12; |
| 2153 | } else { |
| 2154 | // successful exit |
| 2155 | return 0; |
| 2156 | } |
| 2157 | } |
| 2158 | if(nResult) { |
| 2159 | return -13; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2160 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2161 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2162 | // tail recursion; good compilers will reuse the stack frame |
| 2163 | return DecodeNextNested(Bstr); |
| 2164 | } |
| 2165 | |
| 2166 | |
| 2167 | /* |
| 2168 | Recursively decode map containing a little CBOR and a bstr wrapped map |
| 2169 | with a little CBOR and a bstr wrapped map... |
| 2170 | |
| 2171 | Part of bstr_wrap_nest_test. |
| 2172 | */ |
| 2173 | static int32_t DecodeNextNested2(UsefulBufC Wrapped) |
| 2174 | { |
| 2175 | int32_t nResult; |
| 2176 | uint16_t nMapCount; |
| 2177 | int64_t nInt; |
| 2178 | UsefulBufC Bstr; |
| 2179 | QCBORDecodeContext DC; |
| 2180 | |
| 2181 | QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL); |
| 2182 | |
| 2183 | if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) { |
| 2184 | return -20; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2185 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2186 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2187 | if(GetInt64(&DC, &nInt)) { |
| 2188 | return -21; |
| 2189 | } |
| 2190 | |
| 2191 | // The "hello" |
| 2192 | if(GetTextString(&DC, &Bstr)) { |
| 2193 | return -22; |
| 2194 | } |
| 2195 | |
| 2196 | nResult = GetByteString(&DC, &Bstr); |
| 2197 | if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) { |
| 2198 | if(nMapCount == 2) { |
| 2199 | // successful exit |
| 2200 | return 0; |
| 2201 | } else { |
| 2202 | return -23; |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | if(nResult) { |
| 2207 | return -24; |
| 2208 | } |
| 2209 | |
| 2210 | // tail recursion; good compilers will reuse the stack frame |
| 2211 | return DecodeNextNested2(Bstr); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2215 | int32_t BstrWrapNestTest(void) |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2216 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2217 | QCBOREncodeContext EC; |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2218 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2219 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2220 | // ---- Make a complicated nested CBOR structure --- |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2221 | #define BSTR_TEST_DEPTH 6 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2222 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 2223 | QCBOREncode_OpenArray(&EC); |
| 2224 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2225 | for(int i = 0; i < BSTR_TEST_DEPTH; i++) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2226 | QCBOREncode_BstrWrap(&EC); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2227 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2228 | QCBOREncode_AddInt64(&EC, i); |
| 2229 | } |
| 2230 | for(int i = 0; i < BSTR_TEST_DEPTH; i++) { |
| 2231 | QCBOREncode_CloseArray(&EC); |
| 2232 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2233 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2234 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2235 | QCBOREncode_OpenMap(&EC); |
| 2236 | for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) { |
| 2237 | QCBOREncode_BstrWrapInMapN(&EC, i+0x20); |
| 2238 | QCBOREncode_OpenMap(&EC); |
| 2239 | QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2240 | QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello"); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2241 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2242 | |
| 2243 | for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) { |
| 2244 | QCBOREncode_CloseMap(&EC); |
| 2245 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 2246 | } |
| 2247 | QCBOREncode_CloseMap(&EC); |
| 2248 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2249 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2250 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2251 | UsefulBufC Encoded; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 2252 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2253 | return -1; |
| 2254 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2255 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2256 | // ---Compare it to expected. Expected was hand checked with use of CBOR playground ---- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2257 | if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2258 | return -2; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2260 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2261 | // ---- Decode it and see if it is OK ------ |
| 2262 | QCBORDecodeContext DC; |
| 2263 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2264 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2265 | UsefulBufC Bstr; |
| 2266 | uint16_t nArrayCount; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2267 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2268 | // Array surrounding the the whole thing |
| 2269 | if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2270 | return -3; |
| 2271 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2272 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2273 | // Get the byte string wrapping some array stuff |
| 2274 | if(GetByteString(&DC, &Bstr)) { |
| 2275 | return -4; |
| 2276 | } |
| 2277 | |
| 2278 | // Decode the wrapped nested structure |
| 2279 | int nReturn = DecodeNextNested(Bstr); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2280 | if(nReturn) { |
| 2281 | return nReturn; |
| 2282 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2283 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2284 | // A map enclosing some map-oriented bstr wraps |
| 2285 | if(GetMap(&DC, &nArrayCount)) { |
| 2286 | return -5; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2287 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2288 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2289 | // Get the byte string wrapping some array stuff |
| 2290 | if(GetByteString(&DC, &Bstr)) { |
| 2291 | return -6; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2292 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2293 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2294 | // Decode the wrapped nested structure |
| 2295 | nReturn = DecodeNextNested2(Bstr); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2296 | if(nReturn) { |
| 2297 | return nReturn; |
| 2298 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2299 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2300 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2301 | return -7; |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2302 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2303 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2304 | return 0; |
| 2305 | } |
| 2306 | |
| 2307 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2308 | static const uint8_t spCoseSign1Signature[] = { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 2309 | 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0, |
| 2310 | 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c, |
| 2311 | 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0, |
| 2312 | 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a, |
| 2313 | 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54, |
| 2314 | 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3, |
| 2315 | 0x45, 0xca, 0xcb, 0x36}; |
| 2316 | |
| 2317 | /* |
| 2318 | D2 # tag(18) |
| 2319 | 84 # array(4) |
| 2320 | 43 # bytes(3) |
| 2321 | A10126 # "\xA1\x01&" |
| 2322 | A1 # map(1) |
| 2323 | 04 # unsigned(4) |
| 2324 | 42 # bytes(2) |
| 2325 | 3131 # "11" |
| 2326 | 54 # bytes(20) |
| 2327 | 546869732069732074686520636F6E74656E742E # "This is the content." |
| 2328 | 58 40 # bytes(64) |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2329 | 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25 |
| 2330 | A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E |
| 2331 | 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4 |
| 2332 | \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z |
| 2333 | \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91 |
| 2334 | \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01 |
| 2335 | \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA |
| 2336 | \xCB6" |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 2337 | */ |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2338 | static const uint8_t spCoseSign1TBSExpected[] = { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 2339 | 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31, |
| 2340 | 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, |
| 2341 | 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E, |
| 2342 | 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D, |
| 2343 | 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23, |
| 2344 | 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A, |
| 2345 | 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91, |
| 2346 | 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A, |
| 2347 | 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09, |
| 2348 | 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36}; |
| 2349 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2350 | static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26}; |
| 2351 | |
| 2352 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2353 | /* |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2354 | This corresponds exactly to the example in RFC 8152 section |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2355 | C.2.1. This doesn't actually verify the signature (however |
| 2356 | the t_cose implementation does). |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2357 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2358 | int32_t CoseSign1TBSTest(void) |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2359 | { |
| 2360 | // All of this is from RFC 8152 C.2.1 |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2361 | const char *szKid = "11"; |
| 2362 | const UsefulBufC Kid = UsefulBuf_FromSZ(szKid); |
| 2363 | const char *szPayload = "This is the content."; |
| 2364 | const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload); |
| 2365 | const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders); |
| 2366 | const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2367 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2368 | QCBOREncodeContext EC; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2369 | |
| 2370 | // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ---------------------- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 2371 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2372 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2373 | // top level array for cose sign1, 18 is the tag for COSE sign |
Laurence Lundblade | c6ec7ef | 2018-12-04 10:45:06 +0900 | [diff] [blame] | 2374 | QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 2375 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2376 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2377 | // Add protected headers |
| 2378 | QCBOREncode_AddBytes(&EC, ProtectedHeaders); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2379 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2380 | // Empty map with unprotected headers |
| 2381 | QCBOREncode_OpenMap(&EC); |
| 2382 | QCBOREncode_AddBytesToMapN(&EC, 4, Kid); |
| 2383 | QCBOREncode_CloseMap(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2384 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2385 | // The payload |
| 2386 | UsefulBufC WrappedPayload; |
| 2387 | QCBOREncode_BstrWrap(&EC); |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2388 | // Payload is not actually CBOR in example C.2.1 like it would be |
| 2389 | // for a CWT or EAT. It is just a text string. |
| 2390 | QCBOREncode_AddEncoded(&EC, Payload); |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2391 | QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2392 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2393 | // Check we got back the actual payload expected |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2394 | // The extra "T" is 0x54, which is the initial byte a bstr of length 20. |
| 2395 | if(UsefulBuf_Compare(WrappedPayload, |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2396 | UsefulBuf_FROM_SZ_LITERAL("This is the content."))) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2397 | return -1; |
| 2398 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2399 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2400 | /* if(UsefulBuf_Compare(WrappedPayload, |
| 2401 | UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) { |
| 2402 | return -1; |
| 2403 | } */ |
| 2404 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2405 | // The signature |
| 2406 | QCBOREncode_AddBytes(&EC, Signature); |
| 2407 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2408 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2409 | // Finish and check the results |
| 2410 | UsefulBufC COSE_Sign1; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 2411 | if(QCBOREncode_Finish(&EC, &COSE_Sign1)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2412 | return -2; |
| 2413 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2414 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2415 | // 98 is the size from RFC 8152 C.2.1 |
| 2416 | if(COSE_Sign1.len != 98) { |
| 2417 | return -3; |
| 2418 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2419 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 2420 | // It would be good to compare this to the output from a COSE |
| 2421 | // implementation like COSE-C. This has been checked against the |
| 2422 | // CBOR playground. |
| 2423 | if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2424 | return -4; |
| 2425 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2426 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2427 | |
| 2428 | // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------ |
| 2429 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 2430 | |
| 2431 | // top level array for cose sign1, 18 is the tag for COSE sign |
| 2432 | QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1); |
| 2433 | QCBOREncode_OpenArray(&EC); |
| 2434 | |
| 2435 | // Add protected headers |
| 2436 | QCBOREncode_AddBytes(&EC, ProtectedHeaders); |
| 2437 | |
| 2438 | // Empty map with unprotected headers |
| 2439 | QCBOREncode_OpenMap(&EC); |
| 2440 | QCBOREncode_AddBytesToMapN(&EC, 4, Kid); |
| 2441 | QCBOREncode_CloseMap(&EC); |
| 2442 | |
| 2443 | // The payload |
| 2444 | QCBOREncode_BstrWrap(&EC); |
| 2445 | // Payload is not actually CBOR in example C.2.1 like it would be |
| 2446 | // for a CWT or EAT. It is just a text string. |
| 2447 | QCBOREncode_AddEncoded(&EC, Payload); |
| 2448 | QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload); |
| 2449 | |
| 2450 | // Check we got back the actual payload expected |
| 2451 | // The extra "T" is 0x54, which is the initial byte a bstr of length 20. |
| 2452 | if(UsefulBuf_Compare(WrappedPayload, |
| 2453 | UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) { |
| 2454 | return -11; |
| 2455 | } |
| 2456 | |
| 2457 | // The signature |
| 2458 | QCBOREncode_AddBytes(&EC, Signature); |
| 2459 | QCBOREncode_CloseArray(&EC); |
| 2460 | |
| 2461 | // Finish and check the results |
| 2462 | if(QCBOREncode_Finish(&EC, &COSE_Sign1)) { |
| 2463 | return -12; |
| 2464 | } |
| 2465 | |
| 2466 | // 98 is the size from RFC 8152 C.2.1 |
| 2467 | if(COSE_Sign1.len != 98) { |
| 2468 | return -13; |
| 2469 | } |
| 2470 | |
| 2471 | // It would be good to compare this to the output from a COSE |
| 2472 | // implementation like COSE-C. This has been checked against the |
| 2473 | // CBOR playground. |
| 2474 | if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) { |
| 2475 | return -14; |
| 2476 | } |
| 2477 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 2478 | return 0; |
| 2479 | } |
| 2480 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2481 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2482 | int32_t EncodeErrorTests(void) |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2483 | { |
| 2484 | QCBOREncodeContext EC; |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2485 | QCBORError uErr; |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2486 | |
| 2487 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2488 | // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2489 | // Do all of these tests with NULL buffers so no actual |
| 2490 | // large allocations are neccesary |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2491 | const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX}; |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2492 | |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 2493 | // First verify no error from a big buffer |
| 2494 | QCBOREncode_Init(&EC, Buffer); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2495 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 2496 | // 6 is the CBOR overhead for opening the array and encodng the length |
| 2497 | // This exactly fills the buffer. |
| 2498 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6}); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2499 | QCBOREncode_CloseArray(&EC); |
| 2500 | size_t xx; |
| 2501 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 2502 | return -1; |
| 2503 | } |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2504 | |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 2505 | // Second verify error from an array in encoded output too large |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2506 | // Also test fetching the error code before finish |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 2507 | QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX}); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2508 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 2509 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10}); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 2510 | QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2511 | if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) { |
| 2512 | // Error fetch failed. |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 2513 | return -122; |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2514 | } |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2515 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2516 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) { |
| 2517 | return -2; |
| 2518 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2519 | |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 2520 | // Third, fit an array in exactly at max position allowed |
| 2521 | QCBOREncode_Init(&EC, Buffer); |
| 2522 | QCBOREncode_OpenArray(&EC); |
| 2523 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6}); |
| 2524 | QCBOREncode_OpenArray(&EC); |
| 2525 | QCBOREncode_CloseArray(&EC); |
| 2526 | QCBOREncode_CloseArray(&EC); |
| 2527 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 2528 | return -10; |
| 2529 | } |
| 2530 | |
| 2531 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2532 | // ----- QCBOR_ERR_BUFFER_TOO_SMALL -------------- |
| 2533 | // Work close to the 4GB size limit for a better test |
| 2534 | const uint32_t uLargeSize = UINT32_MAX - 1024; |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2535 | const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize}; |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2536 | |
| 2537 | QCBOREncode_Init(&EC, Large); |
| 2538 | QCBOREncode_OpenArray(&EC); |
| 2539 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1}); |
| 2540 | QCBOREncode_CloseArray(&EC); |
| 2541 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 2542 | // Making sure it succeeds when it should first |
| 2543 | return -3; |
| 2544 | } |
| 2545 | |
| 2546 | QCBOREncode_Init(&EC, Large); |
| 2547 | QCBOREncode_OpenArray(&EC); |
| 2548 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1}); |
| 2549 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2}); |
| 2550 | QCBOREncode_CloseArray(&EC); |
| 2551 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) { |
| 2552 | // Now just 1 byte over, see that it fails |
| 2553 | return -4; |
| 2554 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2555 | |
| 2556 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2557 | // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP ------- |
| 2558 | QCBOREncode_Init(&EC, Large); |
| 2559 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2560 | QCBOREncode_OpenArray(&EC); |
| 2561 | } |
| 2562 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2563 | QCBOREncode_CloseArray(&EC); |
| 2564 | } |
| 2565 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 2566 | // Making sure it succeeds when it should first |
| 2567 | return -5; |
| 2568 | } |
| 2569 | |
| 2570 | QCBOREncode_Init(&EC, Large); |
| 2571 | for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) { |
| 2572 | QCBOREncode_OpenArray(&EC); |
| 2573 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2574 | /* +1 level to cause error */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2575 | for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) { |
| 2576 | QCBOREncode_CloseArray(&EC); |
| 2577 | } |
| 2578 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2579 | return -6; |
| 2580 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2581 | |
| 2582 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2583 | /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2584 | QCBOREncode_Init(&EC, Large); |
| 2585 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2586 | QCBOREncode_OpenArray(&EC); |
| 2587 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2588 | /* +1 level to cause error */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2589 | for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) { |
| 2590 | QCBOREncode_CloseArray(&EC); |
| 2591 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2592 | uErr = QCBOREncode_FinishGetSize(&EC, &xx); |
| 2593 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 2594 | if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) { |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2595 | return -7; |
| 2596 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2597 | #else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 2598 | if(uErr != QCBOR_SUCCESS) { |
| 2599 | return -107; |
| 2600 | } |
| 2601 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2602 | |
| 2603 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2604 | /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2605 | QCBOREncode_Init(&EC, Large); |
| 2606 | QCBOREncode_OpenArray(&EC); |
| 2607 | UsefulBufC Wrap; |
| 2608 | QCBOREncode_CloseBstrWrap(&EC, &Wrap); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2609 | uErr = QCBOREncode_FinishGetSize(&EC, &xx); |
| 2610 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 2611 | if(uErr != QCBOR_ERR_CLOSE_MISMATCH) { |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2612 | return -8; |
| 2613 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2614 | #else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 2615 | if(uErr != QCBOR_SUCCESS) { |
| 2616 | return -108; |
| 2617 | } |
| 2618 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2619 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2620 | /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2621 | QCBOREncode_Init(&EC, Large); |
| 2622 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2623 | QCBOREncode_OpenArray(&EC); |
| 2624 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2625 | /* -1 level to cause error */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2626 | for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) { |
| 2627 | QCBOREncode_CloseArray(&EC); |
| 2628 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2629 | |
| 2630 | uErr = QCBOREncode_FinishGetSize(&EC, &xx); |
| 2631 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 2632 | if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2633 | return -9; |
| 2634 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2635 | #else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 2636 | if(uErr != QCBOR_SUCCESS) { |
| 2637 | return -109; |
| 2638 | } |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 2639 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2640 | |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 2641 | /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as |
| 2642 | it would require a 64KB of RAM to test */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2643 | |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2644 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2645 | /* ----- Test the check for NULL buffer ------ */ |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2646 | QCBOREncode_Init(&EC, Buffer); |
| 2647 | if(QCBOREncode_IsBufferNULL(&EC) == 0) { |
| 2648 | return -11; |
| 2649 | } |
| 2650 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2651 | /* ------ QCBOR_ERR_UNSUPPORTED -------- */ |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2652 | QCBOREncode_Init(&EC, Large); |
| 2653 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2654 | QCBOREncode_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */ |
| 2655 | uErr = QCBOREncode_FinishGetSize(&EC, &xx); |
| 2656 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 2657 | if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2658 | return -12; |
| 2659 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2660 | #else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 2661 | if(uErr != QCBOR_SUCCESS) { |
| 2662 | return -112; |
| 2663 | } |
| 2664 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 2665 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2666 | |
| 2667 | QCBOREncode_Init(&EC, Large); |
| 2668 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2669 | QCBOREncode_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */ |
| 2670 | uErr = QCBOREncode_FinishGetSize(&EC, &xx); |
| 2671 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 2672 | if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2673 | return -13; |
| 2674 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 2675 | #else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 2676 | if(uErr != QCBOR_SUCCESS) { |
| 2677 | return -113; |
| 2678 | } |
| 2679 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 2680 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2681 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2682 | return 0; |
| 2683 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2684 | |
| 2685 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2686 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2687 | /* |
| 2688 | [ |
| 2689 | 4([-1, 3]), |
| 2690 | 4([-20, 4759477275222530853136]), |
| 2691 | 4([9223372036854775807, -4759477275222530853137]), |
| 2692 | 5([300, 100]), |
| 2693 | 5([-20, 4759477275222530853136]), |
| 2694 | 5([-9223372036854775808, -4759477275222530853137]) |
| 2695 | ] |
| 2696 | */ |
| 2697 | static const uint8_t spExpectedExponentAndMantissaArray[] = { |
| 2698 | 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33, |
| 2699 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 2700 | 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F, |
| 2701 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, |
| 2702 | 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 2703 | 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 2704 | 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01, |
| 2705 | 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, |
| 2706 | 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, |
| 2707 | 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, |
| 2708 | 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}; |
| 2709 | |
| 2710 | |
| 2711 | /* |
| 2712 | { |
| 2713 | "decimal fraction": 4([-1, 3]), |
| 2714 | 300: 4([-1, 3]), |
| 2715 | "decimal fraction bignum postive": 4([-200, 4759477275222530853136]), |
| 2716 | 400: 4([2147483647, 4759477275222530853136]), |
| 2717 | "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]), |
| 2718 | 500: 4([9223372036854775807, -4759477275222530853137]), |
| 2719 | "big float": 5([300, 100]), |
| 2720 | 600: 5([300, 100]), |
| 2721 | "big float bignum positive": 5([-20, 4759477275222530853136]), |
| 2722 | 700: 5([-20, 4759477275222530853136]), |
| 2723 | "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]), |
| 2724 | 800: 5([-9223372036854775808, -4759477275222530853137]) |
| 2725 | } |
| 2726 | */ |
| 2727 | static const uint8_t spExpectedExponentAndMantissaMap[] = { |
| 2728 | 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61, |
| 2729 | 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69, |
| 2730 | 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01, |
| 2731 | 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64, |
| 2732 | 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, |
| 2733 | 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, |
| 2734 | 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, |
| 2735 | 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82, |
| 2736 | 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, |
| 2737 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, |
| 2738 | 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF, |
| 2739 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 2740 | 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65, |
| 2741 | 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72, |
| 2742 | 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62, |
| 2743 | 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65, |
| 2744 | 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82, |
| 2745 | 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 2746 | 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 2747 | 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4, |
| 2748 | 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, |
| 2749 | 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, |
| 2750 | 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69, |
| 2751 | 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61, |
| 2752 | 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64, |
| 2753 | 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C, |
| 2754 | 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20, |
| 2755 | 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69, |
| 2756 | 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73, |
| 2757 | 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33, |
| 2758 | 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 2759 | 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5, |
| 2760 | 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, |
| 2761 | 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19, |
| 2762 | 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61, |
| 2763 | 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, |
| 2764 | 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, |
| 2765 | 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, |
| 2766 | 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, |
| 2767 | 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 2768 | 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, |
| 2769 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, |
| 2770 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 2771 | 0x09, 0x10 |
| 2772 | }; |
| 2773 | |
| 2774 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2775 | int32_t ExponentAndMantissaEncodeTests(void) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2776 | { |
| 2777 | QCBOREncodeContext EC; |
| 2778 | UsefulBufC EncodedExponentAndMantissa; |
| 2779 | |
| 2780 | // Constant for the big number used in all the tests. |
| 2781 | static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05, |
| 2782 | 0x06, 0x07, 0x08, 0x09, 0x010}; |
| 2783 | const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum); |
| 2784 | |
| 2785 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 2786 | QCBOREncode_OpenArray(&EC); |
| 2787 | QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1) |
| 2788 | QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20); |
| 2789 | QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX); |
| 2790 | QCBOREncode_AddBigFloat(&EC, 100, 300); |
| 2791 | QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20); |
| 2792 | QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN); |
| 2793 | QCBOREncode_CloseArray(&EC); |
| 2794 | |
| 2795 | if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) { |
| 2796 | return -2; |
| 2797 | } |
| 2798 | |
| 2799 | int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa, |
| 2800 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray), |
| 2801 | NULL); |
| 2802 | if(nReturn) { |
| 2803 | return nReturn; |
| 2804 | } |
| 2805 | |
| 2806 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 2807 | QCBOREncode_OpenMap(&EC); |
| 2808 | |
| 2809 | QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1); |
| 2810 | |
| 2811 | QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1); |
| 2812 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2813 | QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2814 | "decimal fraction bignum postive", |
| 2815 | BigNum, |
| 2816 | false, |
| 2817 | -200); |
| 2818 | |
| 2819 | QCBOREncode_AddDecimalFractionBigNumToMapN(&EC, |
| 2820 | 400, |
| 2821 | BigNum, |
| 2822 | false, |
| 2823 | INT32_MAX); |
| 2824 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2825 | QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC, |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2826 | "decimal fraction bignum negative", |
| 2827 | BigNum, |
| 2828 | true, |
| 2829 | INT64_MAX); |
| 2830 | |
| 2831 | QCBOREncode_AddDecimalFractionBigNumToMapN(&EC, |
| 2832 | 500, |
| 2833 | BigNum, |
| 2834 | true, |
| 2835 | INT64_MAX); |
| 2836 | |
| 2837 | QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300); |
| 2838 | |
| 2839 | QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300); |
| 2840 | |
| 2841 | QCBOREncode_AddBigFloatBigNumToMap(&EC, |
| 2842 | "big float bignum positive", |
| 2843 | BigNum, |
| 2844 | false, |
| 2845 | -20); |
| 2846 | |
| 2847 | QCBOREncode_AddBigFloatBigNumToMapN(&EC, |
| 2848 | 700, |
| 2849 | BigNum, |
| 2850 | false, |
| 2851 | -20); |
| 2852 | |
| 2853 | QCBOREncode_AddBigFloatBigNumToMap(&EC, |
| 2854 | "big float bignum negative", |
| 2855 | BigNum, |
| 2856 | true, |
| 2857 | INT64_MIN); |
| 2858 | |
| 2859 | QCBOREncode_AddBigFloatBigNumToMapN(&EC, |
| 2860 | 800, |
| 2861 | BigNum, |
| 2862 | true, |
| 2863 | INT64_MIN); |
| 2864 | |
| 2865 | QCBOREncode_CloseMap(&EC); |
| 2866 | |
| 2867 | if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) { |
| 2868 | return -3; |
| 2869 | } |
| 2870 | |
| 2871 | |
| 2872 | struct UBCompareDiagnostic Diag; |
| 2873 | |
| 2874 | nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa, |
| 2875 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap), |
| 2876 | &Diag); |
| 2877 | if(nReturn) { |
| 2878 | return nReturn + 1000000; // +1000000 to distinguish from first test above |
| 2879 | } |
| 2880 | |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2884 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2885 | |
| 2886 | |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 2887 | int32_t QCBORHeadTest(void) |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2888 | { |
| 2889 | /* This test doesn't have to be extensive, because just about every |
| 2890 | * other test exercises QCBOREncode_EncodeHead(). |
| 2891 | */ |
| 2892 | // ---- basic test to encode a zero ---- |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2893 | UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2894 | |
| 2895 | UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize, |
| 2896 | CBOR_MAJOR_TYPE_POSITIVE_INT, |
| 2897 | 0, |
| 2898 | 0); |
| 2899 | |
| 2900 | static const uint8_t expectedZero[] = {0x00}; |
| 2901 | |
| 2902 | if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) { |
| 2903 | return -1; |
| 2904 | } |
| 2905 | |
| 2906 | // ---- Encode a zero padded out to an 8 byte integer ---- |
| 2907 | encoded = QCBOREncode_EncodeHead(RightSize, |
| 2908 | CBOR_MAJOR_TYPE_POSITIVE_INT, |
| 2909 | 8, // uMinSize is 8 bytes |
| 2910 | 0); |
| 2911 | |
| 2912 | static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00, |
| 2913 | 0x00, 0x00, 0x00, 0x00}; |
| 2914 | |
| 2915 | if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) { |
| 2916 | return -2; |
| 2917 | } |
| 2918 | |
| 2919 | |
| 2920 | // ---- Try to encode into too-small a buffer ---- |
Laurence Lundblade | 8510f8c | 2020-12-01 11:31:16 -0800 | [diff] [blame] | 2921 | UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1); |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 2922 | |
| 2923 | encoded = QCBOREncode_EncodeHead(TooSmall, |
| 2924 | CBOR_MAJOR_TYPE_POSITIVE_INT, |
| 2925 | 0, |
| 2926 | 0); |
| 2927 | |
| 2928 | if(!UsefulBuf_IsNULLC(encoded)) { |
| 2929 | return -3; |
| 2930 | } |
| 2931 | |
| 2932 | return 0; |
| 2933 | } |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 2934 | |
| 2935 | |
| 2936 | static const uint8_t spExpectedForOpenBytes[] = { |
| 2937 | 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, |
| 2938 | 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, |
| 2939 | 0x78 |
| 2940 | }; |
| 2941 | |
| 2942 | static const uint8_t spExpectedForOpenBytes2[] = { |
| 2943 | 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66, |
| 2944 | 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79, |
| 2945 | 0x79, 0x79, 0x18, 0x28, 0x81, 0x40 |
| 2946 | }; |
| 2947 | |
| 2948 | int32_t |
| 2949 | OpenCloseBytesTest(void) |
| 2950 | { |
| 2951 | UsefulBuf_MAKE_STACK_UB( TestBuf, 20); |
| 2952 | UsefulBuf_MAKE_STACK_UB( TestBuf2, 30); |
| 2953 | QCBOREncodeContext EC; |
| 2954 | UsefulBuf Place; |
| 2955 | UsefulBufC Encoded; |
| 2956 | QCBORError uErr; |
| 2957 | |
| 2958 | /* Normal use case -- add a byte string that fits */ |
| 2959 | QCBOREncode_Init(&EC, TestBuf); |
| 2960 | QCBOREncode_OpenBytes(&EC, &Place); |
| 2961 | if(Place.ptr != TestBuf.ptr || |
| 2962 | Place.len != TestBuf.len) { |
| 2963 | return 1; |
| 2964 | } |
| 2965 | Place.len -= 4; |
| 2966 | UsefulBuf_Set(Place, 'x'); |
| 2967 | QCBOREncode_CloseBytes(&EC, Place.len); |
| 2968 | QCBOREncode_Finish(&EC, &Encoded); |
| 2969 | if(UsefulBuf_Compare(Encoded, |
| 2970 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) { |
| 2971 | return 2; |
| 2972 | } |
| 2973 | |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 2974 | /* Run the same test but with a NULL buffer */ |
| 2975 | QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20}); |
| 2976 | QCBOREncode_OpenBytes(&EC, &Place); |
| 2977 | if(!UsefulBuf_IsNULL(Place)) { |
| 2978 | return 3; |
| 2979 | } |
| 2980 | Place.len -= 4; |
| 2981 | /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */ |
| 2982 | QCBOREncode_CloseBytes(&EC, Place.len); |
| 2983 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
| 2984 | if(uErr != QCBOR_SUCCESS || |
| 2985 | Encoded.len != sizeof(spExpectedForOpenBytes)) { |
| 2986 | return 4; |
| 2987 | } |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 2988 | |
| 2989 | /* Open a byte string with no room left */ |
| 2990 | QCBOREncode_Init(&EC, TestBuf); |
| 2991 | QCBOREncode_AddSZString(&EC, "0123456789012345678"); |
| 2992 | QCBOREncode_OpenBytes(&EC, &Place); |
| 2993 | if(Place.ptr != NULL || |
| 2994 | Place.len != 0) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 2995 | return 5; |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | /* Try to extend byte string past end of encoding output buffer */ |
| 2999 | QCBOREncode_Init(&EC, TestBuf); |
| 3000 | QCBOREncode_AddSZString(&EC, "012345678901234567"); |
| 3001 | QCBOREncode_OpenBytes(&EC, &Place); |
| 3002 | /* Don't bother to write any bytes*/ |
| 3003 | QCBOREncode_CloseBytes(&EC, Place.len+1); |
| 3004 | uErr = QCBOREncode_GetErrorState(&EC); |
| 3005 | if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3006 | return 6; |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3007 | } |
| 3008 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3009 | /* Close a byte string without opening one. */ |
| 3010 | QCBOREncode_Init(&EC, TestBuf); |
| 3011 | QCBOREncode_AddSZString(&EC, "012345678"); |
| 3012 | QCBOREncode_CloseBytes(&EC, 1); |
| 3013 | uErr = QCBOREncode_GetErrorState(&EC); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3014 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3015 | if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3016 | return 7; |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3017 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3018 | #else |
| 3019 | if(uErr != QCBOR_SUCCESS) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3020 | return 107; |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3021 | } |
| 3022 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3023 | |
| 3024 | /* Forget to close a byte string */ |
| 3025 | QCBOREncode_Init(&EC, TestBuf); |
| 3026 | QCBOREncode_AddSZString(&EC, "012345678"); |
| 3027 | QCBOREncode_OpenBytes(&EC, &Place); |
| 3028 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3029 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3030 | if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3031 | return 8; |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3032 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3033 | #else |
| 3034 | if(uErr != QCBOR_SUCCESS) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3035 | return 108; |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3036 | } |
| 3037 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3038 | |
| 3039 | /* Try to open a byte string in a byte string */ |
| 3040 | QCBOREncode_Init(&EC, TestBuf); |
| 3041 | QCBOREncode_AddSZString(&EC, "012345678"); |
| 3042 | QCBOREncode_OpenBytes(&EC, &Place); |
| 3043 | QCBOREncode_OpenBytes(&EC, &Place); |
| 3044 | uErr = QCBOREncode_GetErrorState(&EC); |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3045 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3046 | if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3047 | return 9; |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3048 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3049 | #else |
| 3050 | if(uErr != QCBOR_SUCCESS) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3051 | return 109; |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 3052 | } |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3053 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 3054 | |
| 3055 | /* A successful case with a little complexity */ |
| 3056 | QCBOREncode_Init(&EC, TestBuf2); |
| 3057 | QCBOREncode_OpenMap(&EC); |
| 3058 | QCBOREncode_AddInt64ToMapN(&EC, 10, 22); |
| 3059 | QCBOREncode_OpenBytesInMapN(&EC, 20, &Place); |
| 3060 | Place.len = 2; |
| 3061 | UsefulBuf_Set(Place, 'x'); |
| 3062 | QCBOREncode_CloseBytes(&EC, 2); |
| 3063 | QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place); |
| 3064 | Place.len = 3; |
| 3065 | UsefulBuf_Set(Place, 'y'); |
| 3066 | QCBOREncode_CloseBytes(&EC, 3); |
| 3067 | QCBOREncode_OpenArrayInMapN(&EC, 40); |
| 3068 | QCBOREncode_OpenBytes(&EC, &Place); |
| 3069 | QCBOREncode_CloseBytes(&EC, 0); |
| 3070 | QCBOREncode_CloseArray(&EC); |
| 3071 | QCBOREncode_CloseMap(&EC); |
| 3072 | uErr = QCBOREncode_Finish(&EC, &Encoded); |
| 3073 | if(uErr != QCBOR_SUCCESS) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3074 | return 10; |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3075 | } |
| 3076 | if(UsefulBuf_Compare(Encoded, |
| 3077 | UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) { |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 3078 | return 11; |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | return 0; |
| 3082 | } |