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