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 | /* |
| 656 | 98 2F # array(47) |
| 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) |
| 685 | 18 FE # unsigned(254) |
| 686 | 18 FF # unsigned(255) |
| 687 | 19 0100 # unsigned(256) |
| 688 | 19 0101 # unsigned(257) |
| 689 | 19 FFFE # unsigned(65534) |
| 690 | 19 FFFF # unsigned(65535) |
| 691 | 1A 00010000 # unsigned(65536) |
| 692 | 1A 00010001 # unsigned(65537) |
| 693 | 1A 00010002 # unsigned(65538) |
| 694 | 1A 7FFFFFFF # unsigned(2147483647) |
| 695 | 1A 7FFFFFFF # unsigned(2147483647) |
| 696 | 1A 80000000 # unsigned(2147483648) |
| 697 | 1A 80000001 # unsigned(2147483649) |
| 698 | 1A FFFFFFFE # unsigned(4294967294) |
| 699 | 1A FFFFFFFF # unsigned(4294967295) |
| 700 | 1B 0000000100000000 # unsigned(4294967296) |
| 701 | 1B 0000000100000001 # unsigned(4294967297) |
| 702 | 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807) |
| 703 | 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615) |
| 704 | */ |
| 705 | static const uint8_t spExpectedEncodedInts[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 706 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 707 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 708 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 709 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 710 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 711 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 712 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 713 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 714 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 715 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 716 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 717 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 718 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 719 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 720 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 721 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 722 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 723 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 724 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 725 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 726 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 727 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 728 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 729 | 0xff, 0xff}; |
| 730 | |
| 731 | /* |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 732 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 733 | Test the generation of integers. This also ends up testing |
| 734 | encoding of all the different lengths. It encodes integers |
| 735 | of many lengths and values, especially around the boundaries |
| 736 | for different types of integers. It compares the output |
| 737 | to expected values generated from http://cbor.me. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 738 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 739 | */ |
| 740 | int IntegerValuesTest1() |
| 741 | { |
| 742 | QCBOREncodeContext ECtx; |
| 743 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 744 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 745 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 746 | QCBOREncode_OpenArray(&ECtx); |
| 747 | |
| 748 | QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1); |
| 749 | QCBOREncode_AddInt64(&ECtx, -4294967297); |
| 750 | QCBOREncode_AddInt64(&ECtx, -4294967296); |
| 751 | QCBOREncode_AddInt64(&ECtx, -4294967295); |
| 752 | QCBOREncode_AddInt64(&ECtx, -4294967294); |
| 753 | QCBOREncode_AddInt64(&ECtx, -2147483648); |
| 754 | QCBOREncode_AddInt64(&ECtx, -2147483647); |
| 755 | QCBOREncode_AddInt64(&ECtx, -65538); |
| 756 | QCBOREncode_AddInt64(&ECtx, -65537); |
| 757 | QCBOREncode_AddInt64(&ECtx, -65536); |
| 758 | QCBOREncode_AddInt64(&ECtx, -65535); |
| 759 | QCBOREncode_AddInt64(&ECtx, -65534); |
| 760 | QCBOREncode_AddInt64(&ECtx, -257); |
| 761 | QCBOREncode_AddInt64(&ECtx, -256); |
| 762 | QCBOREncode_AddInt64(&ECtx, -255); |
| 763 | QCBOREncode_AddInt64(&ECtx, -254); |
| 764 | QCBOREncode_AddInt64(&ECtx, -25); |
| 765 | QCBOREncode_AddInt64(&ECtx, -24); |
| 766 | QCBOREncode_AddInt64(&ECtx, -23); |
| 767 | QCBOREncode_AddInt64(&ECtx, -1); |
| 768 | QCBOREncode_AddInt64(&ECtx, 0); |
| 769 | QCBOREncode_AddUInt64(&ECtx, 0ULL); |
| 770 | QCBOREncode_AddInt64(&ECtx, 1); |
| 771 | QCBOREncode_AddInt64(&ECtx, 22); |
| 772 | QCBOREncode_AddInt64(&ECtx, 23); |
| 773 | QCBOREncode_AddInt64(&ECtx, 24); |
| 774 | QCBOREncode_AddInt64(&ECtx, 25); |
| 775 | QCBOREncode_AddInt64(&ECtx, 26); |
| 776 | QCBOREncode_AddInt64(&ECtx, 254); |
| 777 | QCBOREncode_AddInt64(&ECtx, 255); |
| 778 | QCBOREncode_AddInt64(&ECtx, 256); |
| 779 | QCBOREncode_AddInt64(&ECtx, 257); |
| 780 | QCBOREncode_AddInt64(&ECtx, 65534); |
| 781 | QCBOREncode_AddInt64(&ECtx, 65535); |
| 782 | QCBOREncode_AddInt64(&ECtx, 65536); |
| 783 | QCBOREncode_AddInt64(&ECtx, 65537); |
| 784 | QCBOREncode_AddInt64(&ECtx, 65538); |
| 785 | QCBOREncode_AddInt64(&ECtx, 2147483647); |
| 786 | QCBOREncode_AddInt64(&ECtx, 2147483647); |
| 787 | QCBOREncode_AddInt64(&ECtx, 2147483648); |
| 788 | QCBOREncode_AddInt64(&ECtx, 2147483649); |
| 789 | QCBOREncode_AddInt64(&ECtx, 4294967294); |
| 790 | QCBOREncode_AddInt64(&ECtx, 4294967295); |
| 791 | QCBOREncode_AddInt64(&ECtx, 4294967296); |
| 792 | QCBOREncode_AddInt64(&ECtx, 4294967297); |
| 793 | QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL); |
| 794 | QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 795 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 796 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 797 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 798 | UsefulBufC Enc; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 799 | if(QCBOREncode_Finish(&ECtx, &Enc)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 800 | nReturn = -1; |
| 801 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 802 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 803 | if(CheckResults(Enc, spExpectedEncodedInts)) |
| 804 | return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 805 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 806 | return(nReturn); |
| 807 | } |
| 808 | |
| 809 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 810 | /* |
| 811 | 85 # array(5) |
| 812 | F5 # primitive(21) |
| 813 | F4 # primitive(20) |
| 814 | F6 # primitive(22) |
| 815 | F7 # primitive(23) |
| 816 | A1 # map(1) |
| 817 | 65 # text(5) |
| 818 | 554E446566 # "UNDef" |
| 819 | F7 # primitive(23) |
| 820 | */ |
| 821 | static const uint8_t spExpectedEncodedSimple[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 822 | 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7}; |
| 823 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 824 | int SimpleValuesTest1() |
| 825 | { |
| 826 | QCBOREncodeContext ECtx; |
| 827 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 828 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 829 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 830 | QCBOREncode_OpenArray(&ECtx); |
Laurence Lundblade | c6ec7ef | 2018-12-04 10:45:06 +0900 | [diff] [blame] | 831 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 832 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE); |
| 833 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE); |
| 834 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL); |
| 835 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 836 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 837 | QCBOREncode_OpenMap(&ECtx); |
| 838 | |
| 839 | QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF); |
| 840 | QCBOREncode_CloseMap(&ECtx); |
| 841 | |
| 842 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 843 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 844 | UsefulBufC ECBOR; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 845 | if(QCBOREncode_Finish(&ECtx, &ECBOR)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 846 | nReturn = -1; |
| 847 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 848 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 849 | if(CheckResults(ECBOR, spExpectedEncodedSimple)) |
| 850 | return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 851 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 852 | return(nReturn); |
| 853 | } |
| 854 | |
Jan Jongboom | 47d86c5 | 2019-07-25 08:54:16 +0200 | [diff] [blame^] | 855 | /* |
| 856 | 9F # array(5) |
| 857 | F5 # primitive(21) |
| 858 | F4 # primitive(20) |
| 859 | F6 # primitive(22) |
| 860 | F7 # primitive(23) |
| 861 | BF # map(1) |
| 862 | 65 # text(5) |
| 863 | 554E446566 # "UNDef" |
| 864 | F7 # primitive(23) |
| 865 | FF # break |
| 866 | FF # break |
| 867 | */ |
| 868 | static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = { |
| 869 | 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff}; |
| 870 | |
| 871 | int SimpleValuesIndefiniteLengthTest1() |
| 872 | { |
| 873 | QCBOREncodeContext ECtx; |
| 874 | int nReturn = 0; |
| 875 | |
| 876 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
| 877 | QCBOREncode_OpenArrayIndefiniteLength(&ECtx); |
| 878 | |
| 879 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE); |
| 880 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE); |
| 881 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL); |
| 882 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF); |
| 883 | |
| 884 | QCBOREncode_OpenMapIndefiniteLength(&ECtx); |
| 885 | |
| 886 | QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF); |
| 887 | QCBOREncode_CloseMapIndefiniteLength(&ECtx); |
| 888 | |
| 889 | QCBOREncode_CloseArrayIndefiniteLength(&ECtx); |
| 890 | |
| 891 | UsefulBufC ECBOR; |
| 892 | if(QCBOREncode_Finish(&ECtx, &ECBOR)) { |
| 893 | nReturn = -1; |
| 894 | } |
| 895 | |
| 896 | if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength)) |
| 897 | return -2; |
| 898 | |
| 899 | return(nReturn); |
| 900 | } |
| 901 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 902 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 903 | /* |
| 904 | 83 # array(3) |
| 905 | C0 # tag(0) |
| 906 | 74 # text(20) |
| 907 | 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z" |
| 908 | C1 # tag(1) |
| 909 | 1A 514B67B0 # unsigned(1363896240) |
| 910 | A2 # map(2) |
| 911 | 78 19 # text(25) |
| 912 | 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339" |
| 913 | C0 # tag(0) |
| 914 | 77 # text(23) |
| 915 | 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z" |
| 916 | 62 # text(2) |
| 917 | 5344 # "SD" |
| 918 | C1 # tag(1) |
| 919 | 19 03E7 # unsigned(999) |
| 920 | */ |
| 921 | static const uint8_t spExpectedEncodedDates[] = { |
| 922 | 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, |
| 923 | 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, |
| 924 | 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2, |
| 925 | 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44, |
| 926 | 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52, |
| 927 | 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31, |
| 928 | 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54, |
| 929 | 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35, |
| 930 | 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7 |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | int EncodeDateTest() |
| 934 | { |
| 935 | QCBOREncodeContext ECtx; |
| 936 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 937 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 938 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 939 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 940 | QCBOREncode_OpenArray(&ECtx); |
| 941 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 942 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 943 | QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC |
| 944 | QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC |
| 945 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 946 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 947 | QCBOREncode_OpenMap(&ECtx); |
| 948 | |
| 949 | 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] | 950 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 951 | QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 952 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 953 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 954 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 955 | QCBOREncode_CloseArray(&ECtx); |
| 956 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 957 | UsefulBufC ECBOR; |
| 958 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 959 | if(QCBOREncode_Finish(&ECtx, &ECBOR)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 960 | nReturn = -1; |
| 961 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 962 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 963 | if(CheckResults(ECBOR, spExpectedEncodedDates)) |
| 964 | return -2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 965 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 966 | return(nReturn); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 967 | } |
| 968 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 969 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 970 | int ArrayNestingTest1() |
| 971 | { |
| 972 | QCBOREncodeContext ECtx; |
| 973 | int i; |
| 974 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 975 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 976 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 977 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 978 | QCBOREncode_OpenArray(&ECtx); |
| 979 | } |
| 980 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 981 | QCBOREncode_CloseArray(&ECtx); |
| 982 | } |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 983 | UsefulBufC Encoded; |
| 984 | if(QCBOREncode_Finish(&ECtx, &Encoded)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 985 | nReturn = -1; |
| 986 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 987 | |
| 988 | return(nReturn); |
| 989 | } |
| 990 | |
| 991 | |
| 992 | |
| 993 | int ArrayNestingTest2() |
| 994 | { |
| 995 | QCBOREncodeContext ECtx; |
| 996 | int i; |
| 997 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 998 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 999 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1000 | for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) { |
| 1001 | QCBOREncode_OpenArray(&ECtx); |
| 1002 | } |
| 1003 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 1004 | QCBOREncode_CloseArray(&ECtx); |
| 1005 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1006 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1007 | UsefulBufC Encoded; |
| 1008 | if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1009 | nReturn = -1; |
| 1010 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1011 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1012 | return(nReturn); |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | int ArrayNestingTest3() |
| 1018 | { |
| 1019 | QCBOREncodeContext ECtx; |
| 1020 | int i; |
| 1021 | int nReturn = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1022 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1023 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1024 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 1025 | QCBOREncode_OpenArray(&ECtx); |
| 1026 | } |
| 1027 | for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) { |
| 1028 | QCBOREncode_CloseArray(&ECtx); |
| 1029 | } |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1030 | UsefulBufC Encoded; |
| 1031 | if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1032 | nReturn = -1; |
| 1033 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1034 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1035 | return(nReturn); |
| 1036 | } |
| 1037 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1038 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1039 | /* |
| 1040 | 81 # array(1) |
| 1041 | 81 # array(1) |
| 1042 | 81 # array(1) |
| 1043 | 81 # array(1) |
| 1044 | 80 # array(0) |
| 1045 | */ |
| 1046 | static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1047 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1048 | // Validated at http://cbor.me and by manually examining its output |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1049 | /* |
| 1050 | 82 # array(2) |
| 1051 | 81 # array(1) |
| 1052 | 81 # array(1) |
| 1053 | 81 # array(1) |
| 1054 | 81 # array(1) |
| 1055 | 80 # array(0) |
| 1056 | 98 2F # array(47) |
| 1057 | 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807) |
| 1058 | 3B 0000000100000000 # negative(4294967296) |
| 1059 | 3A FFFFFFFF # negative(4294967295) |
| 1060 | 3A FFFFFFFE # negative(4294967294) |
| 1061 | 3A FFFFFFFD # negative(4294967293) |
| 1062 | 3A 7FFFFFFF # negative(2147483647) |
| 1063 | 3A 7FFFFFFE # negative(2147483646) |
| 1064 | 3A 00010001 # negative(65537) |
| 1065 | 3A 00010000 # negative(65536) |
| 1066 | 39 FFFF # negative(65535) |
| 1067 | 39 FFFE # negative(65534) |
| 1068 | 39 FFFD # negative(65533) |
| 1069 | 39 0100 # negative(256) |
| 1070 | 38 FF # negative(255) |
| 1071 | 38 FE # negative(254) |
| 1072 | 38 FD # negative(253) |
| 1073 | 38 18 # negative(24) |
| 1074 | 37 # negative(23) |
| 1075 | 36 # negative(22) |
| 1076 | 20 # negative(0) |
| 1077 | 00 # unsigned(0) |
| 1078 | 00 # unsigned(0) |
| 1079 | 01 # unsigned(1) |
| 1080 | 16 # unsigned(22) |
| 1081 | 17 # unsigned(23) |
| 1082 | 18 18 # unsigned(24) |
| 1083 | 18 19 # unsigned(25) |
| 1084 | 18 1A # unsigned(26) |
| 1085 | 18 FE # unsigned(254) |
| 1086 | 18 FF # unsigned(255) |
| 1087 | 19 0100 # unsigned(256) |
| 1088 | 19 0101 # unsigned(257) |
| 1089 | 19 FFFE # unsigned(65534) |
| 1090 | 19 FFFF # unsigned(65535) |
| 1091 | 1A 00010000 # unsigned(65536) |
| 1092 | 1A 00010001 # unsigned(65537) |
| 1093 | 1A 00010002 # unsigned(65538) |
| 1094 | 1A 7FFFFFFF # unsigned(2147483647) |
| 1095 | 1A 7FFFFFFF # unsigned(2147483647) |
| 1096 | 1A 80000000 # unsigned(2147483648) |
| 1097 | 1A 80000001 # unsigned(2147483649) |
| 1098 | 1A FFFFFFFE # unsigned(4294967294) |
| 1099 | 1A FFFFFFFF # unsigned(4294967295) |
| 1100 | 1B 0000000100000000 # unsigned(4294967296) |
| 1101 | 1B 0000000100000001 # unsigned(4294967297) |
| 1102 | 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807) |
| 1103 | 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615) |
| 1104 | */ |
| 1105 | static const uint8_t spEncodeRawExpected[] = { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1106 | 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f, |
| 1107 | 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 1108 | 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 1109 | 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a, |
| 1110 | 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff, |
| 1111 | 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f, |
| 1112 | 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01, |
| 1113 | 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff, |
| 1114 | 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01, |
| 1115 | 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38, |
| 1116 | 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16, |
| 1117 | 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18, |
| 1118 | 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01, |
| 1119 | 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a, |
| 1120 | 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00, |
| 1121 | 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f, |
| 1122 | 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff, |
| 1123 | 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00, |
| 1124 | 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a, |
| 1125 | 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00, |
| 1126 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, |
| 1127 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f, |
| 1128 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b, |
| 1129 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 1130 | |
| 1131 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1132 | int EncodeRawTest() |
| 1133 | { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1134 | QCBOREncodeContext ECtx; |
| 1135 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1136 | QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1137 | QCBOREncode_OpenArray(&ECtx); |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1138 | QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys)); |
| 1139 | QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts)); |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1140 | QCBOREncode_CloseArray(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1141 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1142 | UsefulBufC EncodedRawTest; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1143 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1144 | if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1145 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1146 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1147 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1148 | if(CheckResults(EncodedRawTest, spEncodeRawExpected)) { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1149 | return -5; |
| 1150 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1151 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1152 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1155 | /* |
| 1156 | This returns a pointer to spBigBuf |
| 1157 | */ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1158 | static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen) |
| 1159 | { |
| 1160 | QCBOREncodeContext ECtx; |
| 1161 | int nReturn = -1; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1162 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1163 | *pEncoded = NULL; |
| 1164 | *pEncodedLen = INT32_MAX; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1165 | size_t uFirstSizeEstimate = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1166 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1167 | // loop runs CBOR encoding twice. First with no buffer to |
| 1168 | // calucate the length so buffer can be allocated correctly, |
| 1169 | // and last with the buffer to do the actual encoding |
| 1170 | do { |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1171 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1172 | QCBOREncode_OpenMap(&ECtx); |
| 1173 | QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42); |
| 1174 | QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings"); |
| 1175 | QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7})); |
| 1176 | QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7})); |
| 1177 | QCBOREncode_CloseArray(&ECtx); |
| 1178 | QCBOREncode_OpenMapInMap(&ECtx, "map in a map"); |
| 1179 | QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4})); |
| 1180 | QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4})); |
| 1181 | QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98); |
| 1182 | QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30})); |
| 1183 | QCBOREncode_CloseMap(&ECtx); |
| 1184 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1185 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1186 | if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1187 | goto Done; |
| 1188 | if(*pEncoded != NULL) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1189 | if(uFirstSizeEstimate != *pEncodedLen) { |
| 1190 | nReturn = 1; |
| 1191 | } else { |
| 1192 | nReturn = 0; |
| 1193 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1194 | goto Done; |
| 1195 | } |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1196 | *pEncoded = spBigBuf; |
| 1197 | uFirstSizeEstimate = *pEncodedLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1198 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1199 | } while(1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1200 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1201 | Done: |
| 1202 | return(nReturn); |
| 1203 | } |
| 1204 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1205 | /* |
| 1206 | A3 # map(3) |
| 1207 | 6D # text(13) |
| 1208 | 666972737420696E7465676572 # "first integer" |
| 1209 | 18 2A # unsigned(42) |
| 1210 | 77 # text(23) |
| 1211 | 616E206172726179206F662074776F20737472696E6773 # "an array of two strings" |
| 1212 | 82 # array(2) |
| 1213 | 67 # text(7) |
| 1214 | 737472696E6731 # "string1" |
| 1215 | 67 # text(7) |
| 1216 | 737472696E6732 # "string2" |
| 1217 | 6C # text(12) |
| 1218 | 6D617020696E2061206D6170 # "map in a map" |
| 1219 | A4 # map(4) |
| 1220 | 67 # text(7) |
| 1221 | 62797465732031 # "bytes 1" |
| 1222 | 44 # bytes(4) |
| 1223 | 78787878 # "xxxx" |
| 1224 | 67 # text(7) |
| 1225 | 62797465732032 # "bytes 2" |
| 1226 | 44 # bytes(4) |
| 1227 | 79797979 # "yyyy" |
| 1228 | 6B # text(11) |
| 1229 | 616E6F7468657220696E74 # "another int" |
| 1230 | 18 62 # unsigned(98) |
| 1231 | 66 # text(6) |
| 1232 | 746578742032 # "text 2" |
| 1233 | 78 1E # text(30) |
| 1234 | 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics" |
| 1235 | */ |
| 1236 | static const uint8_t spValidMapEncoded[] = { |
| 1237 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, |
| 1238 | 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e, |
| 1239 | 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, |
| 1240 | 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 1241 | 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, |
| 1242 | 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, |
| 1243 | 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, |
| 1244 | 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, |
| 1245 | 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65, |
| 1246 | 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, |
| 1247 | 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, |
| 1248 | 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78, |
| 1249 | 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, |
| 1250 | 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, |
| 1251 | 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, |
| 1252 | 0x73 } ; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1253 | |
| 1254 | |
| 1255 | int MapEncodeTest() |
| 1256 | { |
| 1257 | uint8_t *pEncodedMaps; |
| 1258 | size_t nEncodedMapLen; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1259 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1260 | if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1261 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1262 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1263 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1264 | int nReturn = 0; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1265 | if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded))) |
| 1266 | nReturn = 2; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1267 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1268 | return(nReturn); |
| 1269 | } |
| 1270 | |
| 1271 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1272 | /* |
| 1273 | @brief Encode the RTIC results |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1274 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1275 | @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL |
| 1276 | @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp |
| 1277 | @param[in] szAlexString Diagnostic code. |
| 1278 | @param[in[ pOut Buffer to put the result in |
| 1279 | @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] | 1280 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1281 | @return |
| 1282 | 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] | 1283 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1284 | 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] | 1285 | 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] | 1286 | of the buffer passed to it. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1287 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1288 | If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the |
| 1289 | length of the encoded CBOR. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1290 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1291 | */ |
| 1292 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1293 | 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] | 1294 | { |
| 1295 | // Buffer that the result will be written in to |
| 1296 | // It is fixed size and small that a stack variable will be fine |
| 1297 | // 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] | 1298 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1299 | // Context for the encoder |
| 1300 | QCBOREncodeContext ECtx; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1301 | QCBOREncode_Init(&ECtx, Storage); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1302 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1303 | // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object |
| 1304 | // Contents are label / value pairs |
| 1305 | QCBOREncode_OpenMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1306 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1307 | { // Brace / indention just to show CBOR encoding nesting |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1308 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1309 | // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen |
| 1310 | QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult); |
| 1311 | |
| 1312 | // Add the diagnostic code |
| 1313 | QCBOREncode_AddSZStringToMap(&ECtx, "type", szType); |
| 1314 | |
| 1315 | // Add a time stamp |
| 1316 | if(time) { |
| 1317 | QCBOREncode_AddDateEpochToMap(&ECtx, "time", time); |
| 1318 | } |
| 1319 | |
| 1320 | // Add the diagnostic code |
| 1321 | QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString); |
| 1322 | |
| 1323 | // Open a subordinate map for telemtry data |
| 1324 | QCBOREncode_OpenMapInMap(&ECtx, "telemetry"); |
| 1325 | |
| 1326 | { // Brace / indention just to show CBOR encoding nesting |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1327 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1328 | // Add a few fake integers and buffers for now. |
| 1329 | QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12); |
| 1330 | |
| 1331 | // Add a few fake integers and buffers for now. |
| 1332 | QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff); |
| 1333 | |
| 1334 | // Add a few fake integers and buffers for now. |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1335 | 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] | 1336 | const UsefulBufC WSPV = {pPV, sizeof(pPV)}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1337 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1338 | QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV); |
| 1339 | } |
| 1340 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1341 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1342 | // Close the telemetry map |
| 1343 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1344 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1345 | // Close the map |
| 1346 | QCBOREncode_CloseMap(&ECtx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1347 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1348 | UsefulBufC Result; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1349 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1350 | QCBOREncode_Finish(&ECtx, &Result); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1351 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1352 | return Result; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1356 | /* |
| 1357 | A5 # map(5) |
| 1358 | 69 # text(9) |
| 1359 | 696E74656772697479 # "integrity" |
| 1360 | F4 # primitive(20) |
| 1361 | 64 # text(4) |
| 1362 | 74797065 # "type" |
| 1363 | 66 # text(6) |
| 1364 | 726563656E74 # "recent" |
| 1365 | 64 # text(4) |
| 1366 | 74696D65 # "time" |
| 1367 | C1 # tag(1) |
| 1368 | 1A 580D4172 # unsigned(1477263730) |
| 1369 | 64 # text(4) |
| 1370 | 64696167 # "diag" |
| 1371 | 6A # text(10) |
| 1372 | 30784131654335303031 # "0xA1eC5001" |
| 1373 | 69 # text(9) |
| 1374 | 74656C656D65747279 # "telemetry" |
| 1375 | A3 # map(3) |
| 1376 | 69 # text(9) |
| 1377 | 53686F652053697A65 # "Shoe Size" |
| 1378 | 0C # unsigned(12) |
| 1379 | 62 # text(2) |
| 1380 | 4951 # "IQ" |
| 1381 | 1A FFFFFFFF # unsigned(4294967295) |
| 1382 | 77 # text(23) |
| 1383 | 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector" |
| 1384 | 48 # bytes(8) |
| 1385 | 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01" |
| 1386 | */ |
| 1387 | static const uint8_t spExpectedRTIC[] = { |
| 1388 | 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74, |
| 1389 | 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65, |
| 1390 | 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1, |
| 1391 | 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67, |
| 1392 | 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30, |
| 1393 | 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72, |
| 1394 | 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69, |
| 1395 | 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff, |
| 1396 | 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61, |
| 1397 | 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56, |
| 1398 | 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56, |
| 1399 | 0xaa, 0xbb, 0x01, 0x01}; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1400 | |
| 1401 | |
| 1402 | int RTICResultsTest() |
| 1403 | { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 1404 | const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1405 | "recent", "0xA1eC5001", |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1406 | UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1407 | if(UsefulBuf_IsNULLC(Encoded)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1408 | return -1; |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1409 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1410 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1411 | if(CheckResults(Encoded, spExpectedRTIC)) { |
| 1412 | return -2; |
| 1413 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1414 | |
| 1415 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1416 | } |
| 1417 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1418 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1419 | /* |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1420 | The expected encoding for first test in BstrWrapTest() |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 1421 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1422 | 82 # array(2) |
| 1423 | 19 01C3 # unsigned(451) |
| 1424 | 43 # bytes(3) |
| 1425 | 1901D2 # "\x19\x01\xD2" |
| 1426 | */ |
| 1427 | 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] | 1428 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1429 | /* |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 1430 | 81 #array(1) |
| 1431 | 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes") |
| 1432 | */ |
| 1433 | static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25}; |
| 1434 | |
| 1435 | /* |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1436 | Very basic bstr wrapping test |
| 1437 | */ |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1438 | int BstrWrapTest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1439 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1440 | QCBOREncodeContext EC; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1441 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1442 | // 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] | 1443 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1444 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1445 | QCBOREncode_OpenArray(&EC); |
| 1446 | QCBOREncode_AddUInt64(&EC, 451); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1447 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1448 | QCBOREncode_BstrWrap(&EC); |
| 1449 | QCBOREncode_AddUInt64(&EC, 466); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1450 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1451 | UsefulBufC Wrapped; |
| 1452 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1453 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1454 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1455 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1456 | UsefulBufC Encoded; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1457 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1458 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1459 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1460 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1461 | if(CheckResults(Encoded, spExpectedBstrWrap)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1462 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1463 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 1464 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1465 | // Second test - see if the length of the wrapped |
| 1466 | // bstr is correct. Also tests bstr wrapping |
| 1467 | // in length calculation only mode. |
Laurence Lundblade | 7412f81 | 2019-01-01 18:49:36 -0800 | [diff] [blame] | 1468 | QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX}); |
| 1469 | QCBOREncode_OpenArray(&EC); |
| 1470 | QCBOREncode_BstrWrap(&EC); |
| 1471 | QCBOREncode_OpenArray(&EC); |
| 1472 | QCBOREncode_AddNULL(&EC); |
| 1473 | QCBOREncode_CloseArray(&EC); |
| 1474 | UsefulBufC BStr; |
| 1475 | QCBOREncode_CloseBstrWrap(&EC, &BStr); |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1476 | // 3 is one byte for the wrapping bstr, 1 for an array of length 1, and 1 byte for a NULL |
| 1477 | if(BStr.ptr != NULL || BStr.len != 3) { |
Laurence Lundblade | 7412f81 | 2019-01-01 18:49:36 -0800 | [diff] [blame] | 1478 | return -5; |
| 1479 | } |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 1480 | |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 1481 | // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the |
| 1482 | // bstr wrapping use cases. |
| 1483 | UsefulBuf_MAKE_STACK_UB(StuffBuf, 50); |
| 1484 | QCBOREncode_Init(&EC, StuffBuf); |
| 1485 | QCBOREncode_OpenArray(&EC); |
| 1486 | QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes")); |
| 1487 | QCBOREncode_CloseArray(&EC); |
| 1488 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
| 1489 | return -6; |
| 1490 | } |
| 1491 | if(CheckResults(Encoded, spExpectedTypeAndLen)) { |
| 1492 | return -7; |
| 1493 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1494 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1495 | return 0; |
| 1496 | } |
| 1497 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1498 | |
| 1499 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1500 | int BstrWrapErrorTest() |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1501 | { |
| 1502 | // -------------- Test closing a bstrwrap when it is an array that is open ----------- |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1503 | QCBOREncodeContext EC; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1504 | |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1505 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1506 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1507 | QCBOREncode_OpenArray(&EC); |
| 1508 | QCBOREncode_AddUInt64(&EC, 451); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1509 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1510 | QCBOREncode_BstrWrap(&EC); |
| 1511 | QCBOREncode_AddUInt64(&EC, 466); |
| 1512 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1513 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1514 | UsefulBufC Wrapped; |
| 1515 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1516 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1517 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1518 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1519 | UsefulBufC Encoded2; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1520 | if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1521 | return -1; |
| 1522 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1523 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1524 | // ----------- test closing a bstrwrap when nothing is open --------------------- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1525 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1526 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1527 | if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1528 | return -2; |
| 1529 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1530 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1531 | // --------------- test nesting too deep ---------------------------------- |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1532 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1533 | for(int i = 1; i < 18; i++) { |
| 1534 | QCBOREncode_BstrWrap(&EC); |
| 1535 | } |
| 1536 | QCBOREncode_AddBool(&EC, true); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1537 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1538 | for(int i = 1; i < 18; i++) { |
| 1539 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
| 1540 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1541 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1542 | if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1543 | return -3; |
| 1544 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1545 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | |
| 1550 | |
| 1551 | // Part of bstr_wrap_nest_test |
| 1552 | /* |
| 1553 | 83 array with three |
| 1554 | 53 byte string with 19 bytes |
| 1555 | 01 #1 |
| 1556 | 50 byte string with 16 bytes |
| 1557 | 02 |
| 1558 | 4D byte string with 13 bytes |
| 1559 | 03 |
| 1560 | 4A byte string with 10 bytes |
| 1561 | 04 |
| 1562 | 47 byte string with 7 bytes |
| 1563 | 05 |
| 1564 | 44 byte string with 4 bytes |
| 1565 | 06 |
| 1566 | 41 byte string with 1 byte |
| 1567 | 07 |
| 1568 | 01 |
| 1569 | 02 |
| 1570 | 03 |
| 1571 | 04 |
| 1572 | 05 |
| 1573 | 06 |
| 1574 | 07 |
| 1575 | A2 map with two items |
| 1576 | 18 20 label for byte string |
| 1577 | 54 byte string of length 20 |
| 1578 | 82 Array with two items |
| 1579 | 10 The integer value 10 |
| 1580 | A2 map with two items |
| 1581 | 18 21 label for byte string |
| 1582 | 44 byte string with 4 bytes |
| 1583 | 81 array with 1 item |
| 1584 | 11 integer value 11 |
| 1585 | 18 30 integer value 30 |
| 1586 | 18 40 integer label 40 |
| 1587 | 65 68 65 6C 6C 6F text string hello |
| 1588 | 18 31 integer value 31 |
| 1589 | 18 41 integer label 41 |
| 1590 | 65 68 65 6C 6C 6F text string hello |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1591 | |
| 1592 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1593 | */ |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1594 | |
| 1595 | |
| 1596 | /* |
| 1597 | 83 # array(3) |
| 1598 | 56 # bytes(22) |
| 1599 | 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06" |
| 1600 | 07 # unsigned(7) |
| 1601 | A2 # map(2) |
| 1602 | 18 20 # unsigned(32) |
| 1603 | 54 # bytes(20) |
| 1604 | 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181" |
| 1605 | 18 41 # unsigned(65) |
| 1606 | 65 # text(5) |
| 1607 | 68656C6C6F # "hello" |
| 1608 | */ |
| 1609 | static const uint8_t spExpectedDeepBstr[] = |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1610 | { |
| 1611 | 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D, |
| 1612 | 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41, |
| 1613 | 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 1614 | 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2, |
| 1615 | 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18, |
| 1616 | 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18, |
| 1617 | 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C, |
| 1618 | 0x6F |
| 1619 | }; |
| 1620 | |
| 1621 | // Part of bstr_wrap_nest_test |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1622 | static int DecodeNextNested(UsefulBufC Wrapped) |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1623 | { |
| 1624 | int nReturn; |
| 1625 | QCBORDecodeContext DC; |
| 1626 | QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1627 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1628 | QCBORItem Item; |
| 1629 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1630 | if(nReturn) { |
| 1631 | return -11; |
| 1632 | } |
| 1633 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1634 | return -12; |
| 1635 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1636 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1637 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1638 | if(nReturn == QCBOR_ERR_HIT_END) { |
| 1639 | return 0; |
| 1640 | } |
| 1641 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1642 | return -13; |
| 1643 | } |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1644 | nReturn = DecodeNextNested(Item.val.string); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1645 | if(nReturn) { |
| 1646 | return nReturn; |
| 1647 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1648 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1649 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1650 | if(nReturn) { |
| 1651 | return -14; |
| 1652 | } |
| 1653 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1654 | return -15; |
| 1655 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1656 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1657 | if(QCBORDecode_Finish(&DC)) { |
| 1658 | return -16; |
| 1659 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1660 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | // Part of bstr_wrap_nest_test |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1665 | static int DecodeNextNested2(UsefulBufC Wrapped) |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1666 | { |
| 1667 | int nReturn; |
| 1668 | QCBORDecodeContext DC; |
| 1669 | QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL); |
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 | QCBORItem Item; |
| 1672 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1673 | if(nReturn) { |
| 1674 | return -11; |
| 1675 | } |
| 1676 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 1677 | return -12; |
| 1678 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1679 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1680 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1681 | if(nReturn) { |
| 1682 | return -11; |
| 1683 | } |
| 1684 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1685 | return -12; |
| 1686 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1687 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1688 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1689 | if(nReturn) { |
| 1690 | return -11; |
| 1691 | } |
| 1692 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 1693 | return 0; |
| 1694 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1695 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1696 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1697 | if(nReturn) { |
| 1698 | return -11; |
| 1699 | } |
| 1700 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1701 | return -13; |
| 1702 | } |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1703 | nReturn = DecodeNextNested2(Item.val.string); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1704 | if(nReturn) { |
| 1705 | return nReturn; |
| 1706 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1707 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1708 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1709 | if(nReturn) { |
| 1710 | return -11; |
| 1711 | } |
| 1712 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1713 | return -12; |
| 1714 | } |
| 1715 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1716 | if(nReturn) { |
| 1717 | return -11; |
| 1718 | } |
| 1719 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1720 | return -12; |
| 1721 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1722 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1723 | if(QCBORDecode_Finish(&DC)) { |
| 1724 | return -16; |
| 1725 | } |
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 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1731 | int BstrWrapNestTest() |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1732 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1733 | QCBOREncodeContext EC; |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1734 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
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 | // ---- Make a complicated nested CBOR structure --- |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 1737 | #define BSTR_TEST_DEPTH 10 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1738 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 1739 | QCBOREncode_OpenArray(&EC); |
| 1740 | |
| 1741 | for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1742 | QCBOREncode_BstrWrap(&EC); |
| 1743 | QCBOREncode_AddUInt64(&EC, i); |
| 1744 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1745 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 1746 | for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1747 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 1748 | QCBOREncode_AddUInt64(&EC, i); |
| 1749 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1750 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 1751 | for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1752 | QCBOREncode_OpenMap(&EC); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1753 | QCBOREncode_BstrWrapInMapN(&EC, i+0x20); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1754 | QCBOREncode_OpenArray(&EC); |
| 1755 | QCBOREncode_AddUInt64(&EC, i+0x10); |
| 1756 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1757 | |
Laurence Lundblade | 972e59c | 2018-11-11 15:57:23 +0700 | [diff] [blame] | 1758 | for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1759 | QCBOREncode_CloseArray(&EC); |
| 1760 | QCBOREncode_AddUInt64(&EC, i+0x30); |
| 1761 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 1762 | QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello"); |
| 1763 | QCBOREncode_CloseMap(&EC); |
| 1764 | } |
| 1765 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1766 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1767 | UsefulBufC Encoded; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1768 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1769 | return -1; |
| 1770 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1771 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1772 | // ---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] | 1773 | if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1774 | return -25; |
| 1775 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1776 | |
| 1777 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1778 | // ---- Decode it and see if it is OK ------ |
| 1779 | QCBORDecodeContext DC; |
| 1780 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1781 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1782 | QCBORItem Item; |
| 1783 | QCBORDecode_GetNext(&DC, &Item); |
| 1784 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) { |
| 1785 | return -2; |
| 1786 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1787 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1788 | QCBORDecode_GetNext(&DC, &Item); |
| 1789 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1790 | return -3; |
| 1791 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1792 | |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1793 | int nReturn = DecodeNextNested(Item.val.string); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1794 | if(nReturn) { |
| 1795 | return nReturn; |
| 1796 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1797 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1798 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1799 | if(nReturn) { |
| 1800 | return -11; |
| 1801 | } |
| 1802 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1803 | return -12; |
| 1804 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1805 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1806 | QCBORDecode_GetNext(&DC, &Item); |
| 1807 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) { |
| 1808 | return -2; |
| 1809 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1810 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1811 | QCBORDecode_GetNext(&DC, &Item); |
| 1812 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1813 | return -3; |
| 1814 | } |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1815 | nReturn = DecodeNextNested2(Item.val.string); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1816 | if(nReturn) { |
| 1817 | return nReturn; |
| 1818 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1819 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1820 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1821 | if(nReturn) { |
| 1822 | return -11; |
| 1823 | } |
| 1824 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1825 | return -12; |
| 1826 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1827 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1828 | if(QCBORDecode_Finish(&DC)) { |
| 1829 | return -16; |
| 1830 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 1831 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1832 | return 0; |
| 1833 | } |
| 1834 | |
| 1835 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1836 | static const uint8_t spCoseSign1Signature[] = { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1837 | 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0, |
| 1838 | 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c, |
| 1839 | 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0, |
| 1840 | 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a, |
| 1841 | 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54, |
| 1842 | 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3, |
| 1843 | 0x45, 0xca, 0xcb, 0x36}; |
| 1844 | |
| 1845 | /* |
| 1846 | D2 # tag(18) |
| 1847 | 84 # array(4) |
| 1848 | 43 # bytes(3) |
| 1849 | A10126 # "\xA1\x01&" |
| 1850 | A1 # map(1) |
| 1851 | 04 # unsigned(4) |
| 1852 | 42 # bytes(2) |
| 1853 | 3131 # "11" |
| 1854 | 54 # bytes(20) |
| 1855 | 546869732069732074686520636F6E74656E742E # "This is the content." |
| 1856 | 58 40 # bytes(64) |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1857 | 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25 |
| 1858 | A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E |
| 1859 | 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4 |
| 1860 | \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z |
| 1861 | \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91 |
| 1862 | \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01 |
| 1863 | \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA |
| 1864 | \xCB6" |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1865 | */ |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1866 | static const uint8_t spCoseSign1TBSExpected[] = { |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1867 | 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31, |
| 1868 | 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, |
| 1869 | 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E, |
| 1870 | 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D, |
| 1871 | 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23, |
| 1872 | 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A, |
| 1873 | 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91, |
| 1874 | 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A, |
| 1875 | 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09, |
| 1876 | 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36}; |
| 1877 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1878 | static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26}; |
| 1879 | |
| 1880 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1881 | /* |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1882 | This corresponds exactly to the example in RFC 8152 section |
| 1883 | C.2.1. This doesn't actually verify the signature though that would |
| 1884 | be nice as it would make the test really good. That would require |
| 1885 | bring in ECDSA crypto to this test. |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1886 | */ |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 1887 | int CoseSign1TBSTest() |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1888 | { |
| 1889 | // All of this is from RFC 8152 C.2.1 |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1890 | const char *szKid = "11"; |
| 1891 | const UsefulBufC Kid = UsefulBuf_FromSZ(szKid); |
| 1892 | const char *szPayload = "This is the content."; |
| 1893 | const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload); |
| 1894 | const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders); |
| 1895 | const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature); |
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 | QCBOREncodeContext EC; |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 1898 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1899 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1900 | // 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] | 1901 | QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1902 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1903 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1904 | // Add protected headers |
| 1905 | QCBOREncode_AddBytes(&EC, ProtectedHeaders); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1906 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1907 | // Empty map with unprotected headers |
| 1908 | QCBOREncode_OpenMap(&EC); |
| 1909 | QCBOREncode_AddBytesToMapN(&EC, 4, Kid); |
| 1910 | QCBOREncode_CloseMap(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1911 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1912 | // The payload |
| 1913 | UsefulBufC WrappedPayload; |
| 1914 | QCBOREncode_BstrWrap(&EC); |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1915 | // Payload is not actually CBOR in example C.2.1 like it would be |
| 1916 | // for a CWT or EAT. It is just a text string. |
| 1917 | QCBOREncode_AddEncoded(&EC, Payload); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1918 | QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1919 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1920 | // Check we got back the actual payload expected |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1921 | // The extra "T" is 0x54, which is the initial byte a bstr of length 20. |
| 1922 | if(UsefulBuf_Compare(WrappedPayload, |
| 1923 | UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1924 | return -1; |
| 1925 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1926 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1927 | // The signature |
| 1928 | QCBOREncode_AddBytes(&EC, Signature); |
| 1929 | QCBOREncode_CloseArray(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1930 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1931 | // Finish and check the results |
| 1932 | UsefulBufC COSE_Sign1; |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1933 | if(QCBOREncode_Finish(&EC, &COSE_Sign1)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1934 | return -2; |
| 1935 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1936 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1937 | // 98 is the size from RFC 8152 C.2.1 |
| 1938 | if(COSE_Sign1.len != 98) { |
| 1939 | return -3; |
| 1940 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1941 | |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 1942 | // It would be good to compare this to the output from a COSE |
| 1943 | // implementation like COSE-C. This has been checked against the |
| 1944 | // CBOR playground. |
| 1945 | if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1946 | return -4; |
| 1947 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1948 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1949 | return 0; |
| 1950 | } |
| 1951 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 1952 | |
| 1953 | int EncodeErrorTests() |
| 1954 | { |
| 1955 | QCBOREncodeContext EC; |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 1956 | |
| 1957 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 1958 | // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------ |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 1959 | // 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] | 1960 | const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX}; |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 1961 | |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 1962 | // First verify no error from a big buffer |
| 1963 | QCBOREncode_Init(&EC, Buffer); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 1964 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 1965 | // 6 is the CBOR overhead for opening the array and encodng the length |
| 1966 | // This exactly fills the buffer. |
| 1967 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6}); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 1968 | QCBOREncode_CloseArray(&EC); |
| 1969 | size_t xx; |
| 1970 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 1971 | return -1; |
| 1972 | } |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 1973 | |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 1974 | // Second verify error from an array in encoded output too large |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 1975 | // Also test fetching the error code before finish |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 1976 | QCBOREncode_Init(&EC, Buffer); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 1977 | QCBOREncode_OpenArray(&EC); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 1978 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6}); |
| 1979 | QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 1980 | if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) { |
| 1981 | // Error fetch failed. |
| 1982 | return -12; |
| 1983 | } |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 1984 | QCBOREncode_CloseArray(&EC); |
| 1985 | QCBOREncode_CloseArray(&EC); |
| 1986 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) { |
| 1987 | return -2; |
| 1988 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 1989 | |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 1990 | // Third, fit an array in exactly at max position allowed |
| 1991 | QCBOREncode_Init(&EC, Buffer); |
| 1992 | QCBOREncode_OpenArray(&EC); |
| 1993 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6}); |
| 1994 | QCBOREncode_OpenArray(&EC); |
| 1995 | QCBOREncode_CloseArray(&EC); |
| 1996 | QCBOREncode_CloseArray(&EC); |
| 1997 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 1998 | return -10; |
| 1999 | } |
| 2000 | |
| 2001 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2002 | // ----- QCBOR_ERR_BUFFER_TOO_SMALL -------------- |
| 2003 | // Work close to the 4GB size limit for a better test |
| 2004 | const uint32_t uLargeSize = UINT32_MAX - 1024; |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2005 | const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize}; |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2006 | |
| 2007 | QCBOREncode_Init(&EC, Large); |
| 2008 | QCBOREncode_OpenArray(&EC); |
| 2009 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1}); |
| 2010 | QCBOREncode_CloseArray(&EC); |
| 2011 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 2012 | // Making sure it succeeds when it should first |
| 2013 | return -3; |
| 2014 | } |
| 2015 | |
| 2016 | QCBOREncode_Init(&EC, Large); |
| 2017 | QCBOREncode_OpenArray(&EC); |
| 2018 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1}); |
| 2019 | QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2}); |
| 2020 | QCBOREncode_CloseArray(&EC); |
| 2021 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) { |
| 2022 | // Now just 1 byte over, see that it fails |
| 2023 | return -4; |
| 2024 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2025 | |
| 2026 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2027 | // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP ------- |
| 2028 | QCBOREncode_Init(&EC, Large); |
| 2029 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2030 | QCBOREncode_OpenArray(&EC); |
| 2031 | } |
| 2032 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2033 | QCBOREncode_CloseArray(&EC); |
| 2034 | } |
| 2035 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) { |
| 2036 | // Making sure it succeeds when it should first |
| 2037 | return -5; |
| 2038 | } |
| 2039 | |
| 2040 | QCBOREncode_Init(&EC, Large); |
| 2041 | for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) { |
| 2042 | QCBOREncode_OpenArray(&EC); |
| 2043 | } |
| 2044 | for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) { |
| 2045 | QCBOREncode_CloseArray(&EC); |
| 2046 | } |
| 2047 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
| 2048 | // One more level to cause error |
| 2049 | return -6; |
| 2050 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2051 | |
| 2052 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2053 | // ------ QCBOR_ERR_TOO_MANY_CLOSES -------- |
| 2054 | QCBOREncode_Init(&EC, Large); |
| 2055 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2056 | QCBOREncode_OpenArray(&EC); |
| 2057 | } |
| 2058 | for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) { |
| 2059 | QCBOREncode_CloseArray(&EC); |
| 2060 | } |
| 2061 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_TOO_MANY_CLOSES) { |
| 2062 | // One more level to cause error |
| 2063 | return -7; |
| 2064 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2065 | |
| 2066 | |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2067 | // ------ QCBOR_ERR_CLOSE_MISMATCH -------- |
| 2068 | QCBOREncode_Init(&EC, Large); |
| 2069 | QCBOREncode_OpenArray(&EC); |
| 2070 | UsefulBufC Wrap; |
| 2071 | QCBOREncode_CloseBstrWrap(&EC, &Wrap); |
| 2072 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_CLOSE_MISMATCH) { |
| 2073 | return -8; |
| 2074 | } |
| 2075 | |
| 2076 | |
| 2077 | // ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- |
| 2078 | QCBOREncode_Init(&EC, Large); |
| 2079 | for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) { |
| 2080 | QCBOREncode_OpenArray(&EC); |
| 2081 | } |
| 2082 | for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) { |
| 2083 | QCBOREncode_CloseArray(&EC); |
| 2084 | } |
| 2085 | if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { |
| 2086 | // One more level to cause error |
| 2087 | return -9; |
| 2088 | } |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 2089 | |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 2090 | /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as |
| 2091 | it would require a 64KB of RAM to test */ |
Laurence Lundblade | f2a58f6 | 2018-12-16 00:43:34 -0800 | [diff] [blame] | 2092 | |
Laurence Lundblade | 30dd0ba | 2019-05-26 23:37:30 +0300 | [diff] [blame] | 2093 | |
| 2094 | // ----- Test the check for NULL buffer ------ |
| 2095 | QCBOREncode_Init(&EC, Buffer); |
| 2096 | if(QCBOREncode_IsBufferNULL(&EC) == 0) { |
| 2097 | return -11; |
| 2098 | } |
| 2099 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 2100 | return 0; |
| 2101 | } |