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