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