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