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