Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1 | /*============================================================================== |
| 2 | Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are |
| 6 | met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above |
| 10 | copyright notice, this list of conditions and the following |
| 11 | disclaimer in the documentation and/or other materials provided |
| 12 | with the distribution. |
| 13 | * Neither the name of The Linux Foundation nor the names of its |
| 14 | contributors may be used to endorse or promote products derived |
| 15 | from this software without specific prior written permission. |
| 16 | |
| 17 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | ==============================================================================*/ |
| 29 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 30 | /*============================================================================== |
| 31 | Modifications beyond the version released on CAF are under the MIT license: |
| 32 | |
| 33 | Copyright 2018 Laurence Lundblade |
| 34 | |
| 35 | Permission is hereby granted, free of charge, to any person obtaining |
| 36 | a copy of this software and associated documentation files (the |
| 37 | "Software"), to deal in the Software without restriction, including |
| 38 | without limitation the rights to use, copy, modify, merge, publish, |
| 39 | distribute, sublicense, and/or sell copies of the Software, and to |
| 40 | permit persons to whom the Software is furnished to do so, subject to |
| 41 | the following conditions: |
| 42 | |
| 43 | The above copyright notice and this permission notice shall be included |
| 44 | in all copies or substantial portions of the Software. |
| 45 | |
| 46 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 47 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 48 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 49 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 50 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 51 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 52 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 53 | SOFTWARE. |
| 54 | ==============================================================================*/ |
| 55 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 56 | #include "qcbor.h" |
| 57 | #include "qcbor_encode_tests.h" |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 58 | #include <strings.h> |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 59 | #include <stdlib.h> |
| 60 | |
| 61 | |
| 62 | |
| 63 | // TODO: -- test on a 32-bit machine) |
| 64 | |
| 65 | // TODO: test QCBOR_MAX_ITEMS_IN_ARRAY (this is very large...) |
| 66 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 67 | #if PRINT_FUNCTIONS_FOR_DEBUGGINGXX |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 68 | #include <stdio.h> |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 69 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 70 | // ifdef these out to not have compiler warnings |
| 71 | static void printencoded(const uint8_t *pEncoded, size_t nLen) |
| 72 | { |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 73 | size_t i; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 74 | for(i = 0; i < nLen; i++) { |
| 75 | uint8_t Z = pEncoded[i]; |
| 76 | printf("%02x ", Z); |
| 77 | } |
| 78 | printf("\n"); |
| 79 | |
| 80 | fflush(stdout); |
| 81 | } |
| 82 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 83 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 84 | #include <stdio.h> |
| 85 | |
| 86 | int Compare(UsefulBufC U1, UsefulBufC U2) { |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 87 | size_t i; |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 88 | for(i = 0; i < U1.len; i++) { |
| 89 | if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) { |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 90 | 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] | 91 | return 1; |
| 92 | } |
| 93 | } |
| 94 | return 0; |
| 95 | |
| 96 | } |
| 97 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 98 | #define CheckResults(Enc, Expected) \ |
| 99 | Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)}) |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 100 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 101 | #else |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 102 | |
| 103 | #define CheckResults(Enc, Expected) \ |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 104 | UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)}) |
| 105 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 106 | #endif |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 111 | |
| 112 | |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | Some very minimal tests. |
| 116 | */ |
| 117 | int basic_encode_test() |
| 118 | { |
| 119 | // Very simple CBOR, a map with one boolean that is true in it |
| 120 | UsefulBuf_MakeStackUB(MemoryForEncoded, 100); |
| 121 | QCBOREncodeContext EC; |
| 122 | |
| 123 | QCBOREncode_Init(&EC, MemoryForEncoded); |
| 124 | |
| 125 | QCBOREncode_OpenMap(&EC); |
| 126 | QCBOREncode_AddBoolToMapN(&EC, 66, true); |
| 127 | QCBOREncode_CloseMap(&EC); |
| 128 | |
| 129 | UsefulBufC Encoded; |
| 130 | if(QCBOREncode_Finish2(&EC, &Encoded)) { |
| 131 | return -3; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // Decode it and see that is right |
| 136 | QCBORDecodeContext DC; |
| 137 | QCBORItem Item; |
| 138 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
| 139 | |
| 140 | QCBORDecode_GetNext(&DC, &Item); |
| 141 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | QCBORDecode_GetNext(&DC, &Item); |
| 146 | if(Item.uDataType != QCBOR_TYPE_TRUE) { |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | if(QCBORDecode_Finish(&DC)) { |
| 151 | return -2; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | // Make another encoded message with the CBOR from the previous put into this one |
| 156 | UsefulBuf_MakeStackUB(MemoryForEncoded2, 100); |
| 157 | QCBOREncode_Init(&EC, MemoryForEncoded2); |
| 158 | QCBOREncode_OpenArray(&EC); |
| 159 | QCBOREncode_AddUInt64(&EC, 451); |
| 160 | QCBOREncode_AddEncoded(&EC, Encoded); |
| 161 | QCBOREncode_OpenMap(&EC); |
| 162 | QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded); |
| 163 | QCBOREncode_CloseMap(&EC); |
| 164 | QCBOREncode_CloseArray(&EC); |
| 165 | |
| 166 | UsefulBufC Encoded2; |
| 167 | if(QCBOREncode_Finish2(&EC, &Encoded2)) { |
| 168 | return -3; |
| 169 | } |
| 170 | /* |
| 171 | [ // 0 1:3 |
| 172 | 451, // 1 1:2 |
| 173 | { // 1 1:2 2:1 |
| 174 | 66: true // 2 1:1 |
| 175 | }, |
| 176 | { // 1 1:1 2:1 |
| 177 | -70000: { // 2 1:1 2:1 3:1 |
| 178 | 66: true // 3 XXXXXX |
| 179 | } |
| 180 | } |
| 181 | ] |
| 182 | |
| 183 | |
| 184 | |
| 185 | 83 # array(3) |
| 186 | 19 01C3 # unsigned(451) |
| 187 | A1 # map(1) |
| 188 | 18 42 # unsigned(66) |
| 189 | F5 # primitive(21) |
| 190 | A1 # map(1) |
| 191 | 3A 0001116F # negative(69999) |
| 192 | A1 # map(1) |
| 193 | 18 42 # unsigned(66) |
| 194 | F5 # primitive(21) |
| 195 | */ |
| 196 | |
| 197 | // Decode it and see if it is OK |
| 198 | QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL); |
| 199 | |
| 200 | // 0 1:3 |
| 201 | QCBORDecode_GetNext(&DC, &Item); |
| 202 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) { |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | // 1 1:2 |
| 207 | QCBORDecode_GetNext(&DC, &Item); |
| 208 | if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) { |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | // 1 1:2 2:1 |
| 213 | QCBORDecode_GetNext(&DC, &Item); |
| 214 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) { |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | // 2 1:1 |
| 219 | QCBORDecode_GetNext(&DC, &Item); |
| 220 | if(Item.uDataType != QCBOR_TYPE_TRUE) { |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | // 1 1:1 2:1 |
| 225 | QCBORDecode_GetNext(&DC, &Item); |
| 226 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) { |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | // 2 1:1 2:1 3:1 |
| 231 | QCBORDecode_GetNext(&DC, &Item); |
| 232 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1 || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != -70000) { |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | // 3 XXXXXX |
| 237 | QCBORDecode_GetNext(&DC, &Item); |
| 238 | if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) { |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | if(QCBORDecode_Finish(&DC)) { |
| 243 | return -2; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 251 | static const uint8_t pExpectedEncodedAll[] = { |
| 252 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 253 | 0x98, 0x29, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8, |
| 254 | 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54, |
| 255 | 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16, |
| 256 | 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42, |
| 257 | 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45, |
| 258 | 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49, |
| 259 | 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20, |
| 260 | 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a, |
| 261 | 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f, |
| 262 | 0xaf, 0x07, 0xff, 0x65, 0x4a, 0x61, 0x69, 0x6d, 0x65, 0xd8, |
| 263 | 0x58, 0xfa, 0x40, 0x49, 0x0f, 0xd0, 0x66, 0x53, 0x74, 0x72, |
| 264 | 0x65, 0x65, 0x74, 0xd8, 0x63, 0xfb, 0x40, 0x21, 0x4f, 0x01, |
| 265 | 0x96, 0xd8, 0xf4, 0xf9, 0xfa, 0x3f, 0x80, 0x00, 0x00, 0xfb, |
| 266 | 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x63, |
| 267 | 0x66, 0x6f, 0x6f, 0xfa, 0x45, 0x98, 0xb8, 0x00, 0x39, 0x03, |
| 268 | 0xe6, 0xfa, 0x44, 0x79, 0xc0, 0x00, 0x66, 0x66, 0x6f, 0x6f, |
| 269 | 0x66, 0x6f, 0x6f, 0xfb, 0x41, 0x58, 0xf7, 0x7d, 0xc0, 0x00, |
| 270 | 0x00, 0x00, 0x39, 0xaf, 0xc6, 0xfb, 0x40, 0xf5, 0xba, 0x70, |
| 271 | 0x00, 0x00, 0x00, 0x00, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a, |
| 272 | 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65, |
| 273 | 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68, |
| 274 | 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74, |
| 275 | 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41, |
| 276 | 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76, |
| 277 | 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69, |
| 278 | 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62, |
| 279 | 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00, |
| 280 | 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20, |
| 281 | 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, |
| 282 | 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b, |
| 283 | 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61, |
| 284 | 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, |
| 285 | 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74, |
| 286 | 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39, |
| 287 | 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f, |
| 288 | 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d, |
| 289 | 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65, |
| 290 | 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65, |
| 291 | 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c, |
| 292 | 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64, |
| 293 | 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78, |
| 294 | 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63, |
| 295 | 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56, |
| 296 | 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8, |
| 297 | 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8, |
| 298 | 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, |
| 299 | 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, |
| 300 | 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, |
| 301 | 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, |
| 302 | 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, |
| 303 | 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, |
| 304 | 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, |
| 305 | 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, |
| 306 | 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, |
| 307 | 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, |
| 308 | 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, |
| 309 | 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, |
| 310 | 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, |
| 311 | 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, |
| 312 | 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, |
| 313 | 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, |
| 314 | 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, |
| 315 | 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, |
| 316 | 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, |
| 317 | 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, |
| 318 | 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, |
| 319 | 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, |
| 320 | 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, |
| 321 | 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, |
| 322 | 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, |
| 323 | 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, |
| 324 | 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, |
| 325 | 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, |
| 326 | 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, |
| 327 | 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, |
| 328 | 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, |
| 329 | 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, |
| 330 | 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, |
| 331 | 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, |
| 332 | 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23, |
| 333 | 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62, |
| 334 | 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, |
| 335 | 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20, |
| 336 | 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29, |
| 337 | 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61, |
| 338 | 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f, |
| 339 | 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, |
| 340 | 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f, |
| 341 | 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66, |
| 342 | 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, |
| 343 | 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68, |
| 344 | 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f, |
| 345 | 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72, |
| 346 | 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63, |
| 347 | 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69, |
| 348 | 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89, |
| 349 | 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, |
| 350 | 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77, |
| 351 | 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c, |
| 352 | 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d, |
| 353 | 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56, |
| 354 | 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f, |
| 355 | 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d, |
| 356 | 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c, |
| 357 | 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01, |
| 358 | 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, |
| 359 | 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, |
| 360 | 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, |
| 361 | 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, |
| 362 | 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, |
| 363 | 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, |
| 364 | 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, |
| 365 | 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, |
| 366 | 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, |
| 367 | 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, |
| 368 | 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, |
| 369 | 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, |
| 370 | 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, |
| 371 | 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, |
| 372 | 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, |
| 373 | 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, |
| 374 | 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, |
| 375 | 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, |
| 376 | 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, |
| 377 | 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, |
| 378 | 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, |
| 379 | 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, |
| 380 | 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, |
| 381 | 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, |
| 382 | 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, |
| 383 | 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, |
| 384 | 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, |
| 385 | 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, |
| 386 | 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, |
| 387 | 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, |
| 388 | 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, |
| 389 | 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, |
| 390 | 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, |
| 391 | 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, |
| 392 | 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57, |
| 393 | 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, |
| 394 | 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f, |
| 395 | 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, |
| 396 | 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, |
| 397 | 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62, |
| 398 | 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58, |
| 399 | 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, |
| 400 | 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54, |
| 401 | 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d, |
| 402 | 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d, |
| 403 | 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, |
| 404 | 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, |
| 405 | 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, |
| 406 | 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, |
| 407 | 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, |
| 408 | 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, |
| 409 | 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a, |
| 410 | 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, |
| 411 | 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78, |
| 412 | 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, |
| 413 | 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, |
| 414 | 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, |
| 415 | 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, |
| 416 | 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43, |
| 417 | 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73, |
| 418 | 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, |
| 419 | 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, |
| 420 | 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, |
| 421 | 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, |
| 422 | 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, |
| 423 | 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, |
| 424 | 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, |
| 425 | 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, |
| 426 | 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, |
| 427 | 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d, |
| 428 | 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, |
| 429 | 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64, |
| 430 | 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30, |
| 431 | 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, |
| 432 | 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35, |
| 433 | 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78, |
| 434 | 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, |
| 435 | 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, |
| 436 | 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7, |
| 437 | 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62, |
| 438 | 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80, |
| 439 | 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61, |
| 440 | 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20, |
| 441 | 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61, |
| 442 | 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62, |
| 443 | 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20, |
| 444 | 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1, |
| 445 | 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61, |
| 446 | 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5, |
| 447 | 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32, |
| 448 | 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05, |
| 449 | 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8, |
| 450 | 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, |
| 451 | 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, |
| 452 | 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50, |
| 453 | 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54, |
| 454 | 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25, |
| 455 | 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, |
| 456 | 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2, |
| 457 | 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73, |
| 458 | 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19, |
| 459 | 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 460 | 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, |
| 461 | 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B, |
| 462 | 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 463 | 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 464 | 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49, |
| 465 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, |
| 466 | 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 467 | 0x00, 0x00 |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | |
| 471 | static const char *szMIME = "\ |
| 472 | MIME-Version: 1.0\n\ |
| 473 | Content-Type: multipart/mixed;\n\ |
| 474 | boundary=\"XXXXboundary text\"\n\ |
| 475 | \n\ |
| 476 | This is a multipart message in MIME format.\n\ |
| 477 | \n\ |
| 478 | --XXXXboundary text\n\ |
| 479 | Content-Type: text/plain\n\ |
| 480 | \n\ |
| 481 | this is the body text\n\ |
| 482 | \n\ |
| 483 | --XXXXboundary text\n\ |
| 484 | Content-Type: text/plain;\n\ |
| 485 | Content-Disposition: attachment;\n\ |
| 486 | filename=\"test.txt\"\n\ |
| 487 | \n\ |
| 488 | this is the attachment text\n\ |
| 489 | \n\ |
| 490 | --XXXXboundary text--"; |
| 491 | |
| 492 | |
| 493 | int AllAddMethodsTest() |
| 494 | { |
| 495 | QCBOREncodeContext ECtx; |
| 496 | int nReturn = 0; |
| 497 | |
| 498 | uint8_t pEncoded[3000]; |
| 499 | size_t nEncodedLen = sizeof(pEncoded); |
| 500 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 501 | QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 502 | |
| 503 | QCBOREncode_OpenArray(&ECtx); |
| 504 | |
| 505 | // Non-map ints |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 506 | QCBOREncode_AddTag(&ECtx, 100); |
| 507 | QCBOREncode_AddUInt64_2(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 89989909); |
| 508 | QCBOREncode_AddTag(&ECtx, 76); |
| 509 | QCBOREncode_AddInt64_2(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 77689989909); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 510 | QCBOREncode_AddUInt64(&ECtx,0); |
| 511 | QCBOREncode_AddInt64(&ECtx, -44); |
| 512 | |
| 513 | // ints that go in maps |
| 514 | QCBOREncode_OpenMap(&ECtx); |
| 515 | QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77); |
| 516 | QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88); |
| 517 | QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238); |
| 518 | QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000); |
| 519 | QCBOREncode_CloseMap(&ECtx); |
| 520 | |
| 521 | // floats and doubles |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 522 | QCBOREncode_AddTag(&ECtx, 88); |
| 523 | QCBOREncode_AddFloat_2(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 3.14159); |
| 524 | QCBOREncode_AddTag(&ECtx, 99); |
| 525 | QCBOREncode_AddDouble_2(&ECtx, "Street", QCBOR_NO_INT_LABEL, 8.654309); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 526 | QCBOREncode_AddFloat(&ECtx, 1); |
| 527 | QCBOREncode_AddDouble(&ECtx, 1); |
| 528 | |
| 529 | // floats and doubles that go in map |
| 530 | QCBOREncode_OpenMap(&ECtx); |
| 531 | QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887); |
| 532 | QCBOREncode_AddFloatToMapN(&ECtx, -999, 999); |
| 533 | QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887); |
| 534 | QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999); |
| 535 | QCBOREncode_CloseMap(&ECtx); |
| 536 | |
| 537 | // Epoch Date |
| 538 | QCBOREncode_AddDateEpoch(&ECtx, 2383748234); |
| 539 | |
| 540 | // Epoch date with labels |
| 541 | QCBOREncode_OpenMap(&ECtx); |
| 542 | QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000); |
| 543 | QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730); |
| 544 | QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222); |
| 545 | QCBOREncode_CloseMap(&ECtx); |
| 546 | |
| 547 | // Binary blobs |
| 548 | QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2})); |
| 549 | |
| 550 | // binary blobs in maps |
| 551 | QCBOREncode_OpenMap(&ECtx); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 552 | QCBOREncode_AddTag(&ECtx, 100000); |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame^] | 553 | QCBOREncode_AddBytes_2(&ECtx, "binbin", QCBOR_NO_INT_LABEL, ((UsefulBufC) {(uint8_t []){0x00}, 1})); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 554 | QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3})); |
| 555 | QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4})); |
| 556 | QCBOREncode_CloseMap(&ECtx); |
| 557 | |
| 558 | // text blobs |
| 559 | QCBOREncode_AddText(&ECtx, SZLiteralToUsefulBufC("bar bar foo bar")); |
| 560 | QCBOREncode_AddSZString(&ECtx, "oof\n"); |
| 561 | QCBOREncode_AddURI(&ECtx, SZLiteralToUsefulBufC("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8")); |
| 562 | QCBOREncode_AddB64Text(&ECtx, SZLiteralToUsefulBufC("YW55IGNhcm5hbCBwbGVhc3VyZQ==")); |
| 563 | QCBOREncode_AddRegex(&ECtx, SZLiteralToUsefulBufC("[^abc]+")); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 564 | QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 565 | |
| 566 | // text blobs in maps |
| 567 | QCBOREncode_OpenMap(&ECtx); |
| 568 | QCBOREncode_AddTextToMap(&ECtx, "#####", SZLiteralToUsefulBufC("foo bar foo foo")); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 569 | QCBOREncode_AddTextToMap(&ECtx, "____", SZLiteralToUsefulBufC("foo bar")); // TODO _2? |
| 570 | QCBOREncode_AddTag(&ECtx, 1000); |
| 571 | QCBOREncode_AddSZString_2(&ECtx, "()()()", QCBOR_NO_INT_LABEL, "rab rab oof"); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 572 | QCBOREncode_AddTextToMapN(&ECtx,22, SZLiteralToUsefulBufC("foo foo foo foo")); |
| 573 | QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof"); |
| 574 | QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof"); |
| 575 | QCBOREncode_AddURIToMap(&ECtx, "RFC", SZLiteralToUsefulBufC("https://tools.ietf.org/html/rfc7049#section-2.4.5")); |
| 576 | QCBOREncode_AddURIToMapN(&ECtx, 0x89, SZLiteralToUsefulBufC("http://cbor.me/")); |
| 577 | QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", SZLiteralToUsefulBufC("cGxlYXN1cmUu")); |
| 578 | QCBOREncode_AddB64TextToMapN(&ECtx, 64, SZLiteralToUsefulBufC("c3VyZS4=")); |
| 579 | QCBOREncode_AddRegexToMap(&ECtx, "popo", SZLiteralToUsefulBufC("100\\s*mk")); // x code string literal bug |
| 580 | QCBOREncode_AddRegexToMapN(&ECtx, -51, SZLiteralToUsefulBufC("perl\\B")); // x code string literal bug |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 581 | QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME)); |
| 582 | QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME)); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 583 | QCBOREncode_CloseMap(&ECtx); |
| 584 | |
| 585 | // Date strings |
| 586 | QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z"); |
| 587 | QCBOREncode_OpenMap(&ECtx); |
| 588 | QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00"); |
| 589 | QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00"); |
| 590 | QCBOREncode_CloseMap(&ECtx); |
| 591 | |
| 592 | // true / false ... |
| 593 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF); |
| 594 | QCBOREncode_OpenMap(&ECtx); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 595 | QCBOREncode_AddTag(&ECtx, 66); |
| 596 | QCBOREncode_AddSimple_2(&ECtx, "dare", QCBOR_NO_INT_LABEL, CBOR_SIMPLEV_TRUE); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 597 | QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE); |
| 598 | QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL); |
| 599 | QCBOREncode_CloseMap(&ECtx); |
| 600 | |
| 601 | // opening an array |
| 602 | QCBOREncode_OpenArray(&ECtx); |
| 603 | QCBOREncode_CloseArray(&ECtx); |
| 604 | |
| 605 | // opening arrays in a map |
| 606 | QCBOREncode_OpenMap(&ECtx); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 607 | QCBOREncode_AddTag(&ECtx, 1093); |
| 608 | QCBOREncode_OpenArray_2(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 609 | QCBOREncode_CloseArray(&ECtx); |
| 610 | QCBOREncode_OpenArrayInMap(&ECtx, "alabl"); |
| 611 | QCBOREncode_CloseArray(&ECtx); |
| 612 | QCBOREncode_OpenArrayInMapN(&ECtx, 42); |
| 613 | QCBOREncode_CloseArray(&ECtx); |
| 614 | QCBOREncode_CloseMap(&ECtx); |
| 615 | |
| 616 | // opening maps with labels and tagging |
| 617 | QCBOREncode_OpenMap(&ECtx); |
| 618 | QCBOREncode_OpenMapInMap(&ECtx, "in a map"); |
| 619 | QCBOREncode_OpenMapInMapN(&ECtx, 5556); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 620 | QCBOREncode_AddTag(&ECtx, 9087); |
| 621 | QCBOREncode_OpenMap_2(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 622 | QCBOREncode_CloseMap(&ECtx); |
| 623 | QCBOREncode_CloseMap(&ECtx); |
| 624 | QCBOREncode_CloseMap(&ECtx); |
| 625 | QCBOREncode_CloseMap(&ECtx); |
| 626 | |
| 627 | // Extended simple values (these are not standard...) |
| 628 | QCBOREncode_OpenMap(&ECtx); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 629 | QCBOREncode_AddTag(&ECtx, 88); |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame^] | 630 | QCBOREncode_AddType7_2(&ECtx, "s1", QCBOR_NO_INT_LABEL, 0, 255); |
| 631 | QCBOREncode_AddType7_2(&ECtx, "s2", QCBOR_NO_INT_LABEL, 0, 0); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 632 | QCBOREncode_AddTag(&ECtx, 88); |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame^] | 633 | QCBOREncode_AddType7_2(&ECtx, "s3", QCBOR_NO_INT_LABEL, 0, 33); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 634 | QCBOREncode_AddTag(&ECtx, 88); |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame^] | 635 | QCBOREncode_AddType7_2(&ECtx, NULL, 88378374, 0, 255); |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 636 | QCBOREncode_AddTag(&ECtx, 88); |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame^] | 637 | QCBOREncode_AddType7_2(&ECtx, NULL, 89, 0, 19); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 638 | QCBOREncode_CloseMap(&ECtx); |
| 639 | |
| 640 | |
| 641 | // UUIDs |
| 642 | static uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32}; |
| 643 | UsefulBufC XXUUID = ByteArrayLiteralToUsefulBufC(ppppUUID); |
| 644 | QCBOREncode_AddBinaryUUID(&ECtx, XXUUID); |
| 645 | QCBOREncode_OpenMap(&ECtx); |
| 646 | QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID); |
| 647 | QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID); |
| 648 | QCBOREncode_CloseMap(&ECtx); |
| 649 | |
| 650 | |
| 651 | // Bool |
| 652 | QCBOREncode_AddBool(&ECtx, true); |
| 653 | QCBOREncode_AddBool(&ECtx, false); |
| 654 | QCBOREncode_OpenMap(&ECtx); |
| 655 | QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true); |
| 656 | QCBOREncode_AddBoolToMapN(&ECtx, 010101, true); |
| 657 | QCBOREncode_CloseMap(&ECtx); |
| 658 | |
| 659 | |
| 660 | static uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 661 | UsefulBufC BIGNUM = ByteArrayLiteralToUsefulBufC(pBignum); |
| 662 | QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM); |
| 663 | QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM); |
| 664 | QCBOREncode_OpenMap(&ECtx); |
| 665 | QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM); |
| 666 | QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM); |
| 667 | QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM); |
| 668 | QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM); |
| 669 | QCBOREncode_CloseMap(&ECtx); |
| 670 | |
| 671 | QCBOREncode_CloseArray(&ECtx); |
| 672 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 673 | UsefulBufC Enc; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 674 | |
| 675 | if(QCBOREncode_Finish2(&ECtx, &Enc)) { |
| 676 | nReturn = -1; |
| 677 | goto Done; |
| 678 | } |
| 679 | |
| 680 | //printencodedE(Enc); |
| 681 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 682 | if(CheckResults(Enc, pExpectedEncodedAll)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 683 | nReturn = -1; |
| 684 | |
| 685 | Done: |
| 686 | return nReturn; |
| 687 | } |
| 688 | |
| 689 | // todo -- add a test for counting the top level items and adding it back in with AddRaw() |
| 690 | |
| 691 | |
| 692 | static const uint8_t pExpectedEncodedInts[] = { |
| 693 | 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, |
| 694 | 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, |
| 695 | 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, |
| 696 | 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, |
| 697 | 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, |
| 698 | 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, |
| 699 | 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, |
| 700 | 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, |
| 701 | 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, |
| 702 | 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, |
| 703 | 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, |
| 704 | 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, |
| 705 | 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, |
| 706 | 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, |
| 707 | 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, |
| 708 | 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, |
| 709 | 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, |
| 710 | 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, |
| 711 | 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, |
| 712 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, |
| 713 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 714 | 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 715 | 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 716 | 0xff, 0xff}; |
| 717 | |
| 718 | /* |
| 719 | |
| 720 | Test the generation of integers. This also ends up testing |
| 721 | encoding of all the different lengths. It encodes integers |
| 722 | of many lengths and values, especially around the boundaries |
| 723 | for different types of integers. It compares the output |
| 724 | to expected values generated from http://cbor.me. |
| 725 | |
| 726 | */ |
| 727 | int IntegerValuesTest1() |
| 728 | { |
| 729 | QCBOREncodeContext ECtx; |
| 730 | int nReturn = 0; |
| 731 | |
| 732 | uint8_t pEncoded[1000]; |
| 733 | size_t nEncodedLen = sizeof(pEncoded); |
| 734 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 735 | QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 736 | QCBOREncode_OpenArray(&ECtx); |
| 737 | |
| 738 | QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1); |
| 739 | QCBOREncode_AddInt64(&ECtx, -4294967297); |
| 740 | QCBOREncode_AddInt64(&ECtx, -4294967296); |
| 741 | QCBOREncode_AddInt64(&ECtx, -4294967295); |
| 742 | QCBOREncode_AddInt64(&ECtx, -4294967294); |
| 743 | QCBOREncode_AddInt64(&ECtx, -2147483648); |
| 744 | QCBOREncode_AddInt64(&ECtx, -2147483647); |
| 745 | QCBOREncode_AddInt64(&ECtx, -65538); |
| 746 | QCBOREncode_AddInt64(&ECtx, -65537); |
| 747 | QCBOREncode_AddInt64(&ECtx, -65536); |
| 748 | QCBOREncode_AddInt64(&ECtx, -65535); |
| 749 | QCBOREncode_AddInt64(&ECtx, -65534); |
| 750 | QCBOREncode_AddInt64(&ECtx, -257); |
| 751 | QCBOREncode_AddInt64(&ECtx, -256); |
| 752 | QCBOREncode_AddInt64(&ECtx, -255); |
| 753 | QCBOREncode_AddInt64(&ECtx, -254); |
| 754 | QCBOREncode_AddInt64(&ECtx, -25); |
| 755 | QCBOREncode_AddInt64(&ECtx, -24); |
| 756 | QCBOREncode_AddInt64(&ECtx, -23); |
| 757 | QCBOREncode_AddInt64(&ECtx, -1); |
| 758 | QCBOREncode_AddInt64(&ECtx, 0); |
| 759 | QCBOREncode_AddUInt64(&ECtx, 0ULL); |
| 760 | QCBOREncode_AddInt64(&ECtx, 1); |
| 761 | QCBOREncode_AddInt64(&ECtx, 22); |
| 762 | QCBOREncode_AddInt64(&ECtx, 23); |
| 763 | QCBOREncode_AddInt64(&ECtx, 24); |
| 764 | QCBOREncode_AddInt64(&ECtx, 25); |
| 765 | QCBOREncode_AddInt64(&ECtx, 26); |
| 766 | QCBOREncode_AddInt64(&ECtx, 254); |
| 767 | QCBOREncode_AddInt64(&ECtx, 255); |
| 768 | QCBOREncode_AddInt64(&ECtx, 256); |
| 769 | QCBOREncode_AddInt64(&ECtx, 257); |
| 770 | QCBOREncode_AddInt64(&ECtx, 65534); |
| 771 | QCBOREncode_AddInt64(&ECtx, 65535); |
| 772 | QCBOREncode_AddInt64(&ECtx, 65536); |
| 773 | QCBOREncode_AddInt64(&ECtx, 65537); |
| 774 | QCBOREncode_AddInt64(&ECtx, 65538); |
| 775 | QCBOREncode_AddInt64(&ECtx, 2147483647); |
| 776 | QCBOREncode_AddInt64(&ECtx, 2147483647); |
| 777 | QCBOREncode_AddInt64(&ECtx, 2147483648); |
| 778 | QCBOREncode_AddInt64(&ECtx, 2147483649); |
| 779 | QCBOREncode_AddInt64(&ECtx, 4294967294); |
| 780 | QCBOREncode_AddInt64(&ECtx, 4294967295); |
| 781 | QCBOREncode_AddInt64(&ECtx, 4294967296); |
| 782 | QCBOREncode_AddInt64(&ECtx, 4294967297); |
| 783 | QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL); |
| 784 | QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL); |
| 785 | |
| 786 | QCBOREncode_CloseArray(&ECtx); |
| 787 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 788 | UsefulBufC Enc; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 789 | if(QCBOREncode_Finish2(&ECtx, &Enc)) { |
| 790 | nReturn = -1; |
| 791 | } |
| 792 | |
| 793 | if(CheckResults(Enc, pExpectedEncodedInts)) |
| 794 | return -1; |
| 795 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 796 | if(Enc.len != sizeof(pExpectedEncodedInts) || memcmp(pEncoded, pExpectedEncodedInts, Enc.len)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 797 | nReturn = -1; |
| 798 | |
| 799 | //printencoded(pEncoded, nEncodedLen); |
| 800 | |
| 801 | return(nReturn); |
| 802 | } |
| 803 | |
| 804 | |
| 805 | |
| 806 | static uint8_t pExpectedEncodedSimple[] = { |
| 807 | 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7}; |
| 808 | |
| 809 | |
| 810 | int SimpleValuesTest1() |
| 811 | { |
| 812 | QCBOREncodeContext ECtx; |
| 813 | int nReturn = 0; |
| 814 | |
| 815 | uint8_t pEncoded[100]; |
| 816 | size_t nEncodedLen = sizeof(pEncoded); |
| 817 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 818 | QCBOREncode_Init(&ECtx, (UsefulBuf) {pEncoded, nEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 819 | QCBOREncode_OpenArray(&ECtx); |
| 820 | |
| 821 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE); |
| 822 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE); |
| 823 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL); |
| 824 | QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF); |
| 825 | |
| 826 | QCBOREncode_OpenMap(&ECtx); |
| 827 | |
| 828 | QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF); |
| 829 | QCBOREncode_CloseMap(&ECtx); |
| 830 | |
| 831 | QCBOREncode_CloseArray(&ECtx); |
| 832 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 833 | UsefulBufC ECBOR; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 834 | if(QCBOREncode_Finish2(&ECtx, &ECBOR)) { |
| 835 | nReturn = -1; |
| 836 | } |
| 837 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 838 | if(ECBOR.len != sizeof(pExpectedEncodedSimple) || memcmp(pEncoded, pExpectedEncodedSimple, ECBOR.len)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 839 | nReturn = -1; |
| 840 | |
| 841 | // printencoded(pEncoded, nEncodedLen); |
| 842 | |
| 843 | return(nReturn); |
| 844 | } |
| 845 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 846 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 847 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 848 | static uint8_t pExpectedEncodedDates[] = { |
| 849 | 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54, |
| 850 | 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30, |
| 851 | 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2, |
| 852 | 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, |
| 853 | 0x20, 0x44, 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, |
| 854 | 0x6f, 0x6d, 0x20, 0x52, 0x46, 0x43, 0x20, 0x33, |
| 855 | 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31, 0x39, 0x38, |
| 856 | 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54, |
| 857 | 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, |
| 858 | 0x2e, 0x35, 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, |
| 859 | 0x19, 0x03, 0xe7 |
| 860 | }; |
| 861 | |
| 862 | int EncodeDateTest() |
| 863 | { |
| 864 | QCBOREncodeContext ECtx; |
| 865 | int nReturn = 0; |
| 866 | |
| 867 | uint8_t pEncoded[1000]; |
| 868 | size_t nEncodedLen = sizeof(pEncoded); |
| 869 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 870 | QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 871 | |
| 872 | QCBOREncode_OpenArray(&ECtx); |
| 873 | |
| 874 | |
| 875 | QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC |
| 876 | QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC |
| 877 | |
| 878 | |
| 879 | QCBOREncode_OpenMap(&ECtx); |
| 880 | |
| 881 | QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z"); |
| 882 | |
| 883 | |
| 884 | QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999); |
| 885 | |
| 886 | QCBOREncode_CloseMap(&ECtx); |
| 887 | |
| 888 | QCBOREncode_CloseArray(&ECtx); |
| 889 | |
| 890 | |
| 891 | if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) { |
| 892 | nReturn = -1; |
| 893 | } |
| 894 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 895 | if(nEncodedLen != sizeof(pExpectedEncodedDates) || memcmp(pEncoded, pExpectedEncodedDates, nEncodedLen)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 896 | nReturn = -1; |
| 897 | |
| 898 | //printencoded(pEncoded, nEncodedLen); |
| 899 | |
| 900 | return(nReturn); |
| 901 | |
| 902 | } |
| 903 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 904 | int ArrayNestingTest1() |
| 905 | { |
| 906 | QCBOREncodeContext ECtx; |
| 907 | int i; |
| 908 | int nReturn = 0; |
| 909 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 910 | UsefulBuf_MakeStackUB(Encode, 100); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 911 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 912 | QCBOREncode_Init(&ECtx, Encode); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 913 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 914 | QCBOREncode_OpenArray(&ECtx); |
| 915 | } |
| 916 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 917 | QCBOREncode_CloseArray(&ECtx); |
| 918 | } |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 919 | size_t nEncodedLen; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 920 | if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 921 | nReturn = -1; |
| 922 | } |
| 923 | //printencoded(pEncoded, nEncodedLen); |
| 924 | |
| 925 | return(nReturn); |
| 926 | } |
| 927 | |
| 928 | |
| 929 | |
| 930 | int ArrayNestingTest2() |
| 931 | { |
| 932 | QCBOREncodeContext ECtx; |
| 933 | int i; |
| 934 | int nReturn = 0; |
| 935 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 936 | UsefulBuf_MakeStackUB(Encode, 100); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 937 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 938 | QCBOREncode_Init(&ECtx, Encode); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 939 | for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) { |
| 940 | QCBOREncode_OpenArray(&ECtx); |
| 941 | } |
| 942 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 943 | QCBOREncode_CloseArray(&ECtx); |
| 944 | } |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 945 | |
| 946 | size_t nEncodedLen; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 947 | if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
| 948 | nReturn = -1; |
| 949 | } |
| 950 | |
| 951 | return(nReturn); |
| 952 | } |
| 953 | |
| 954 | |
| 955 | |
| 956 | int ArrayNestingTest3() |
| 957 | { |
| 958 | QCBOREncodeContext ECtx; |
| 959 | int i; |
| 960 | int nReturn = 0; |
| 961 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 962 | UsefulBuf_MakeStackUB(Encode, 100); |
| 963 | |
| 964 | QCBOREncode_Init(&ECtx, Encode); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 965 | for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) { |
| 966 | QCBOREncode_OpenArray(&ECtx); |
| 967 | } |
| 968 | for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) { |
| 969 | QCBOREncode_CloseArray(&ECtx); |
| 970 | } |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 971 | size_t nEncodedLen; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 972 | if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) { |
| 973 | nReturn = -1; |
| 974 | } |
| 975 | |
| 976 | return(nReturn); |
| 977 | } |
| 978 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 979 | |
| 980 | static uint8_t s_pFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80}; |
| 981 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 982 | // Validated at http://cbor.me and by manually examining its output |
| 983 | static uint8_t s_pEncodeRawExpected[] = { |
| 984 | 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f, |
| 985 | 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 986 | 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 987 | 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a, |
| 988 | 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff, |
| 989 | 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f, |
| 990 | 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01, |
| 991 | 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff, |
| 992 | 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01, |
| 993 | 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38, |
| 994 | 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16, |
| 995 | 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18, |
| 996 | 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01, |
| 997 | 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a, |
| 998 | 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00, |
| 999 | 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f, |
| 1000 | 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff, |
| 1001 | 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00, |
| 1002 | 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a, |
| 1003 | 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00, |
| 1004 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, |
| 1005 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f, |
| 1006 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b, |
| 1007 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 1008 | |
| 1009 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1010 | int EncodeRawTest() |
| 1011 | { |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1012 | UsefulBuf_MakeStackUB(RawTestStorage, 220); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1013 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1014 | QCBOREncodeContext ECtx; |
| 1015 | |
| 1016 | QCBOREncode_Init(&ECtx, RawTestStorage); |
| 1017 | QCBOREncode_OpenArray(&ECtx); |
| 1018 | QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FromByteArrayLiteral(s_pFiveArrarys)); |
| 1019 | QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FromByteArrayLiteral(pExpectedEncodedInts)); |
| 1020 | QCBOREncode_CloseArray(&ECtx); |
| 1021 | |
| 1022 | UsefulBufC EncodedRawTest; |
| 1023 | |
| 1024 | if(QCBOREncode_Finish2(&ECtx, &EncodedRawTest)) { |
| 1025 | return -4; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1026 | } |
| 1027 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1028 | if(UsefulBuf_Compare(EncodedRawTest, UsefulBuf_FromByteArrayLiteral(s_pEncodeRawExpected))) { |
| 1029 | return -5; |
| 1030 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1031 | |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 1032 | return 0; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1035 | |
| 1036 | |
| 1037 | |
| 1038 | static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen) |
| 1039 | { |
| 1040 | QCBOREncodeContext ECtx; |
| 1041 | int nReturn = -1; |
| 1042 | |
| 1043 | *pEncoded = NULL; |
| 1044 | *pEncodedLen = INT32_MAX; |
| 1045 | |
| 1046 | // loop runs CBOR encoding twice. First with no buffer to |
| 1047 | // calucate the length so buffer can be allocated correctly, |
| 1048 | // and last with the buffer to do the actual encoding |
| 1049 | do { |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1050 | QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1051 | QCBOREncode_OpenMap(&ECtx); |
| 1052 | QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42); |
| 1053 | QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings"); |
| 1054 | QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7})); |
| 1055 | QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7})); |
| 1056 | QCBOREncode_CloseArray(&ECtx); |
| 1057 | QCBOREncode_OpenMapInMap(&ECtx, "map in a map"); |
| 1058 | QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4})); |
| 1059 | QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4})); |
| 1060 | QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98); |
| 1061 | QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30})); |
| 1062 | QCBOREncode_CloseMap(&ECtx); |
| 1063 | QCBOREncode_CloseMap(&ECtx); |
| 1064 | |
| 1065 | |
| 1066 | if(QCBOREncode_Finish(&ECtx, pEncodedLen)) |
| 1067 | goto Done; |
| 1068 | if(*pEncoded != NULL) { |
| 1069 | nReturn = 0; |
| 1070 | goto Done; |
| 1071 | } |
| 1072 | *pEncoded = malloc(*pEncodedLen); |
| 1073 | } while(1); |
| 1074 | |
| 1075 | Done: |
| 1076 | return(nReturn); |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | static uint8_t pValidMapEncoded[] = { |
| 1081 | 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, |
| 1082 | 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, 0x6f, 0x20, |
| 1083 | 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x67, |
| 1084 | 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, |
| 1085 | 0x6d, 0x61, 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, |
| 1086 | 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, |
| 1087 | 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, |
| 1088 | 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, |
| 1089 | 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 } ; |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | int MapEncodeTest() |
| 1094 | { |
| 1095 | uint8_t *pEncodedMaps; |
| 1096 | size_t nEncodedMapLen; |
| 1097 | |
| 1098 | if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) { |
| 1099 | return(-1); |
| 1100 | } |
| 1101 | |
| 1102 | //printencoded(pEncodedMaps, nEncodedMapLen); |
| 1103 | |
| 1104 | int nReturn = 0; |
| 1105 | if(memcmp(pValidMapEncoded, pEncodedMaps, sizeof(pValidMapEncoded))) |
| 1106 | nReturn = 1; |
| 1107 | |
| 1108 | return(nReturn); |
| 1109 | } |
| 1110 | |
| 1111 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1112 | /* |
| 1113 | @brief Encode the RTIC results |
| 1114 | |
| 1115 | @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL |
| 1116 | @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp |
| 1117 | @param[in] szAlexString Diagnostic code. |
| 1118 | @param[in[ pOut Buffer to put the result in |
| 1119 | @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return |
| 1120 | |
| 1121 | @return |
| 1122 | One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error. |
| 1123 | |
| 1124 | The size of pOut should be 30 bytes plus the length of pnLen. If you make it too |
| 1125 | short an error will be returned. This function will never write off the end |
| 1126 | of the buffer passed to it. |
| 1127 | |
| 1128 | If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the |
| 1129 | length of the encoded CBOR. |
| 1130 | |
| 1131 | */ |
| 1132 | |
| 1133 | int FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, uint8_t *pOut, size_t *pnLen) |
| 1134 | { |
| 1135 | // Buffer that the result will be written in to |
| 1136 | // It is fixed size and small that a stack variable will be fine |
| 1137 | // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error. |
| 1138 | |
| 1139 | // Context for the encoder |
| 1140 | QCBOREncodeContext ECtx; |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1141 | QCBOREncode_Init(&ECtx, (UsefulBuf){pOut, *pnLen}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1142 | |
| 1143 | // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object |
| 1144 | // Contents are label / value pairs |
| 1145 | QCBOREncode_OpenMap(&ECtx); |
| 1146 | |
| 1147 | { // Brace / indention just to show CBOR encoding nesting |
| 1148 | |
| 1149 | // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen |
| 1150 | QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult); |
| 1151 | |
| 1152 | // Add the diagnostic code |
| 1153 | QCBOREncode_AddSZStringToMap(&ECtx, "type", szType); |
| 1154 | |
| 1155 | // Add a time stamp |
| 1156 | if(time) { |
| 1157 | QCBOREncode_AddDateEpochToMap(&ECtx, "time", time); |
| 1158 | } |
| 1159 | |
| 1160 | // Add the diagnostic code |
| 1161 | QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString); |
| 1162 | |
| 1163 | // Open a subordinate map for telemtry data |
| 1164 | QCBOREncode_OpenMapInMap(&ECtx, "telemetry"); |
| 1165 | |
| 1166 | { // Brace / indention just to show CBOR encoding nesting |
| 1167 | |
| 1168 | // Add a few fake integers and buffers for now. |
| 1169 | QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12); |
| 1170 | |
| 1171 | // Add a few fake integers and buffers for now. |
| 1172 | QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff); |
| 1173 | |
| 1174 | // Add a few fake integers and buffers for now. |
| 1175 | const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01}; |
| 1176 | UsefulBufC WSPV = {pPV, sizeof(pPV)}; |
| 1177 | |
| 1178 | QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | // Close the telemetry map |
| 1183 | QCBOREncode_CloseMap(&ECtx); |
| 1184 | |
| 1185 | // Close the map |
| 1186 | QCBOREncode_CloseMap(&ECtx); |
| 1187 | |
| 1188 | return QCBOREncode_Finish(&ECtx, pnLen); |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | static const uint8_t pExpectedRTIC[] = {0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74, 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1, 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67, 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30, 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72, 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69, 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61, 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01}; |
| 1193 | |
| 1194 | |
| 1195 | int RTICResultsTest() |
| 1196 | { |
| 1197 | uint8_t out[200]; |
| 1198 | size_t len = sizeof(out); |
| 1199 | |
| 1200 | int nResult = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, "recent", "0xA1eC5001", out, &len); |
| 1201 | if(nResult) |
| 1202 | return -1; |
| 1203 | |
| 1204 | if(memcmp(pExpectedRTIC, out, sizeof(pExpectedRTIC))) |
| 1205 | return 1; |
| 1206 | |
| 1207 | //printencoded(out, len); |
| 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1212 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 1213 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1214 | /* |
| 1215 | Very basic bstr wrapping test |
| 1216 | */ |
| 1217 | int bstrwraptest() |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1218 | { |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1219 | UsefulBuf_MakeStackUB(MemoryForEncoded, 100); |
| 1220 | QCBOREncodeContext EC; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1221 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1222 | QCBOREncode_Init(&EC, MemoryForEncoded); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1223 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1224 | QCBOREncode_OpenArray(&EC); |
| 1225 | QCBOREncode_AddUInt64(&EC, 451); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1226 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1227 | QCBOREncode_BstrWrap(&EC); |
| 1228 | QCBOREncode_AddUInt64(&EC, 466); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1229 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1230 | UsefulBufC Wrapped; |
| 1231 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1232 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1233 | QCBOREncode_CloseArray(&EC); |
| 1234 | |
| 1235 | UsefulBufC Encoded; |
| 1236 | if(QCBOREncode_Finish2(&EC, &Encoded)) { |
| 1237 | return -1; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1238 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1239 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1240 | const uint8_t pExpected[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2}; |
| 1241 | if(UsefulBuf_Compare(UsefulBuf_FromByteArrayLiteral(pExpected), Encoded)) { |
| 1242 | return -2; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1248 | |
| 1249 | |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1250 | int bstr_wrap_error_test() |
| 1251 | { |
| 1252 | // -------------- Test closing a bstrwrap when it is an array that is open ----------- |
| 1253 | UsefulBuf_MakeStackUB(MemoryForEncoded, 100); |
| 1254 | QCBOREncodeContext EC; |
| 1255 | |
| 1256 | QCBOREncode_Init(&EC, MemoryForEncoded); |
| 1257 | |
| 1258 | QCBOREncode_OpenArray(&EC); |
| 1259 | QCBOREncode_AddUInt64(&EC, 451); |
| 1260 | |
| 1261 | QCBOREncode_BstrWrap(&EC); |
| 1262 | QCBOREncode_AddUInt64(&EC, 466); |
| 1263 | QCBOREncode_OpenArray(&EC); |
| 1264 | |
| 1265 | UsefulBufC Wrapped; |
| 1266 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
| 1267 | |
| 1268 | QCBOREncode_CloseArray(&EC); |
| 1269 | |
| 1270 | UsefulBufC Encoded2; |
| 1271 | if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) { |
| 1272 | return -1; |
| 1273 | } |
| 1274 | |
| 1275 | // ----------- test closing a bstrwrap when nothing is open --------------------- |
| 1276 | QCBOREncode_Init(&EC, MemoryForEncoded); |
| 1277 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
| 1278 | if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) { |
| 1279 | return -2; |
| 1280 | } |
| 1281 | |
| 1282 | // --------------- test nesting too deep ---------------------------------- |
| 1283 | QCBOREncode_Init(&EC, MemoryForEncoded); |
| 1284 | for(int i = 1; i < 18; i++) { |
| 1285 | QCBOREncode_BstrWrap(&EC); |
| 1286 | } |
| 1287 | QCBOREncode_AddBool(&EC, true); |
| 1288 | |
| 1289 | for(int i = 1; i < 18; i++) { |
| 1290 | QCBOREncode_CloseBstrWrap(&EC, &Wrapped); |
| 1291 | } |
| 1292 | |
| 1293 | if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) { |
| 1294 | return -3; |
| 1295 | } |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | // Part of bstr_wrap_nest_test |
| 1303 | /* |
| 1304 | 83 array with three |
| 1305 | 53 byte string with 19 bytes |
| 1306 | 01 #1 |
| 1307 | 50 byte string with 16 bytes |
| 1308 | 02 |
| 1309 | 4D byte string with 13 bytes |
| 1310 | 03 |
| 1311 | 4A byte string with 10 bytes |
| 1312 | 04 |
| 1313 | 47 byte string with 7 bytes |
| 1314 | 05 |
| 1315 | 44 byte string with 4 bytes |
| 1316 | 06 |
| 1317 | 41 byte string with 1 byte |
| 1318 | 07 |
| 1319 | 01 |
| 1320 | 02 |
| 1321 | 03 |
| 1322 | 04 |
| 1323 | 05 |
| 1324 | 06 |
| 1325 | 07 |
| 1326 | A2 map with two items |
| 1327 | 18 20 label for byte string |
| 1328 | 54 byte string of length 20 |
| 1329 | 82 Array with two items |
| 1330 | 10 The integer value 10 |
| 1331 | A2 map with two items |
| 1332 | 18 21 label for byte string |
| 1333 | 44 byte string with 4 bytes |
| 1334 | 81 array with 1 item |
| 1335 | 11 integer value 11 |
| 1336 | 18 30 integer value 30 |
| 1337 | 18 40 integer label 40 |
| 1338 | 65 68 65 6C 6C 6F text string hello |
| 1339 | 18 31 integer value 31 |
| 1340 | 18 41 integer label 41 |
| 1341 | 65 68 65 6C 6C 6F text string hello |
| 1342 | |
| 1343 | |
| 1344 | */ |
| 1345 | static const uint8_t sExpectedDeepBstr[] = |
| 1346 | { |
| 1347 | 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D, |
| 1348 | 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41, |
| 1349 | 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 1350 | 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2, |
| 1351 | 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18, |
| 1352 | 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18, |
| 1353 | 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C, |
| 1354 | 0x6F |
| 1355 | }; |
| 1356 | |
| 1357 | // Part of bstr_wrap_nest_test |
| 1358 | static int decode_next_nested(UsefulBufC Wrapped) |
| 1359 | { |
| 1360 | int nReturn; |
| 1361 | QCBORDecodeContext DC; |
| 1362 | QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL); |
| 1363 | |
| 1364 | QCBORItem Item; |
| 1365 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1366 | if(nReturn) { |
| 1367 | return -11; |
| 1368 | } |
| 1369 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1370 | return -12; |
| 1371 | } |
| 1372 | |
| 1373 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1374 | if(nReturn == QCBOR_ERR_HIT_END) { |
| 1375 | return 0; |
| 1376 | } |
| 1377 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1378 | return -13; |
| 1379 | } |
| 1380 | nReturn = decode_next_nested(Item.val.string); |
| 1381 | if(nReturn) { |
| 1382 | return nReturn; |
| 1383 | } |
| 1384 | |
| 1385 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1386 | if(nReturn) { |
| 1387 | return -14; |
| 1388 | } |
| 1389 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1390 | return -15; |
| 1391 | } |
| 1392 | |
| 1393 | if(QCBORDecode_Finish(&DC)) { |
| 1394 | return -16; |
| 1395 | } |
| 1396 | |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | // Part of bstr_wrap_nest_test |
| 1401 | static int decode_next_nested2(UsefulBufC Wrapped) |
| 1402 | { |
| 1403 | int nReturn; |
| 1404 | QCBORDecodeContext DC; |
| 1405 | QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL); |
| 1406 | |
| 1407 | QCBORItem Item; |
| 1408 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1409 | if(nReturn) { |
| 1410 | return -11; |
| 1411 | } |
| 1412 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 1413 | return -12; |
| 1414 | } |
| 1415 | |
| 1416 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1417 | if(nReturn) { |
| 1418 | return -11; |
| 1419 | } |
| 1420 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1421 | return -12; |
| 1422 | } |
| 1423 | |
| 1424 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1425 | if(nReturn) { |
| 1426 | return -11; |
| 1427 | } |
| 1428 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1433 | if(nReturn) { |
| 1434 | return -11; |
| 1435 | } |
| 1436 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1437 | return -13; |
| 1438 | } |
| 1439 | nReturn = decode_next_nested2(Item.val.string); |
| 1440 | if(nReturn) { |
| 1441 | return nReturn; |
| 1442 | } |
| 1443 | |
| 1444 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1445 | if(nReturn) { |
| 1446 | return -11; |
| 1447 | } |
| 1448 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1449 | return -12; |
| 1450 | } |
| 1451 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1452 | if(nReturn) { |
| 1453 | return -11; |
| 1454 | } |
| 1455 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1456 | return -12; |
| 1457 | } |
| 1458 | |
| 1459 | if(QCBORDecode_Finish(&DC)) { |
| 1460 | return -16; |
| 1461 | } |
| 1462 | |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | |
| 1467 | int bstr_wrap_nest_test() |
| 1468 | { |
| 1469 | UsefulBuf_MakeStackUB(MemoryForEncoded, 300); |
| 1470 | QCBOREncodeContext EC; |
| 1471 | QCBOREncode_Init(&EC, MemoryForEncoded); |
| 1472 | |
| 1473 | // ---- Make a complicated nested CBOR structure --- |
| 1474 | QCBOREncode_OpenArray(&EC); |
| 1475 | |
| 1476 | for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) { |
| 1477 | QCBOREncode_BstrWrap(&EC); |
| 1478 | QCBOREncode_AddUInt64(&EC, i); |
| 1479 | } |
| 1480 | |
| 1481 | for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) { |
| 1482 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 1483 | QCBOREncode_AddUInt64(&EC, i); |
| 1484 | } |
| 1485 | |
| 1486 | for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) { |
| 1487 | QCBOREncode_OpenMap(&EC); |
| 1488 | QCBOREncode_BstrWrapMapN(&EC, i+0x20); |
| 1489 | QCBOREncode_OpenArray(&EC); |
| 1490 | QCBOREncode_AddUInt64(&EC, i+0x10); |
| 1491 | } |
| 1492 | |
| 1493 | for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) { |
| 1494 | QCBOREncode_CloseArray(&EC); |
| 1495 | QCBOREncode_AddUInt64(&EC, i+0x30); |
| 1496 | QCBOREncode_CloseBstrWrap(&EC, NULL); |
| 1497 | QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello"); |
| 1498 | QCBOREncode_CloseMap(&EC); |
| 1499 | } |
| 1500 | QCBOREncode_CloseArray(&EC); |
| 1501 | |
| 1502 | UsefulBufC Encoded; |
| 1503 | if(QCBOREncode_Finish2(&EC, &Encoded)) { |
| 1504 | return -1; |
| 1505 | } |
| 1506 | |
| 1507 | // ---Compare it to expected. Expected was hand checked with use of CBOR playground ---- |
| 1508 | if(UsefulBuf_Compare(UsefulBuf_FromByteArrayLiteral(sExpectedDeepBstr), Encoded)) { |
| 1509 | return -25; |
| 1510 | } |
| 1511 | |
| 1512 | |
| 1513 | // ---- Decode it and see if it is OK ------ |
| 1514 | QCBORDecodeContext DC; |
| 1515 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
| 1516 | |
| 1517 | QCBORItem Item; |
| 1518 | QCBORDecode_GetNext(&DC, &Item); |
| 1519 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) { |
| 1520 | return -2; |
| 1521 | } |
| 1522 | |
| 1523 | QCBORDecode_GetNext(&DC, &Item); |
| 1524 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1525 | return -3; |
| 1526 | } |
| 1527 | |
| 1528 | int nReturn = decode_next_nested(Item.val.string); |
| 1529 | if(nReturn) { |
| 1530 | return nReturn; |
| 1531 | } |
| 1532 | |
| 1533 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1534 | if(nReturn) { |
| 1535 | return -11; |
| 1536 | } |
| 1537 | if(Item.uDataType != QCBOR_TYPE_INT64) { |
| 1538 | return -12; |
| 1539 | } |
| 1540 | |
| 1541 | QCBORDecode_GetNext(&DC, &Item); |
| 1542 | if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) { |
| 1543 | return -2; |
| 1544 | } |
| 1545 | |
| 1546 | QCBORDecode_GetNext(&DC, &Item); |
| 1547 | if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1548 | return -3; |
| 1549 | } |
| 1550 | nReturn = decode_next_nested2(Item.val.string); |
| 1551 | if(nReturn) { |
| 1552 | return nReturn; |
| 1553 | } |
| 1554 | |
| 1555 | nReturn = QCBORDecode_GetNext(&DC, &Item); |
| 1556 | if(nReturn) { |
| 1557 | return -11; |
| 1558 | } |
| 1559 | if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1560 | return -12; |
| 1561 | } |
| 1562 | |
| 1563 | if(QCBORDecode_Finish(&DC)) { |
| 1564 | return -16; |
| 1565 | } |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | |
| 1571 | /* |
| 1572 | this corresponds exactly to the example in RFC 8152 |
| 1573 | section C.2.1. This doesn't actually verify the signature |
| 1574 | though that would be nice as it would make the test |
| 1575 | really good. That would require bring in ECDSA crypto |
| 1576 | to this test. |
| 1577 | */ |
| 1578 | int cose_sign1_tbs_test() |
| 1579 | { |
| 1580 | // All of this is from RFC 8152 C.2.1 |
| 1581 | const char *szKid = "11"; |
| 1582 | UsefulBufC Kid = UsefulBuf_FromSZ(szKid); |
| 1583 | const char *szPayload = "This is the content."; |
| 1584 | UsefulBufC Payload = UsefulBuf_FromSZ(szPayload); |
| 1585 | const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26}; |
| 1586 | UsefulBufC ProtectedHeaders = UsefulBuf_FromByteArrayLiteral(pProtectedHeaders); |
| 1587 | const uint8_t sSignature[] = { |
| 1588 | 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0, |
| 1589 | 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c, |
| 1590 | 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0, |
| 1591 | 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a, |
| 1592 | 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54, |
| 1593 | 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3, |
| 1594 | 0x45, 0xca, 0xcb, 0x36}; |
| 1595 | // It would be good to compare this to the output from |
| 1596 | // a COSE implementation like COSE-C. It has been checked |
| 1597 | // against the CBOR playground. |
| 1598 | UsefulBufC Signature = UsefulBuf_FromByteArrayLiteral(sSignature); |
| 1599 | const uint8_t sExpected[] = { |
| 1600 | 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31, |
| 1601 | 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, |
| 1602 | 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E, |
| 1603 | 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D, |
| 1604 | 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23, |
| 1605 | 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A, |
| 1606 | 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91, |
| 1607 | 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A, |
| 1608 | 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09, |
| 1609 | 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36}; |
| 1610 | UsefulBufC Expected = UsefulBuf_FromByteArrayLiteral(sExpected); |
| 1611 | |
| 1612 | UsefulBuf_MakeStackUB(MemoryForEncoded, 98); |
| 1613 | QCBOREncodeContext EC; |
| 1614 | QCBOREncode_Init(&EC, MemoryForEncoded); |
| 1615 | |
| 1616 | // top level array for cose sign1, 18 is the tag for COSE sign |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 1617 | QCBOREncode_AddTag(&EC, 18); // TODO: replace with constant |
| 1618 | QCBOREncode_OpenArray_2(&EC, NULL, QCBOR_NO_INT_LABEL); // TODO: _2 |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 1619 | |
| 1620 | // Add protected headers |
| 1621 | QCBOREncode_AddBytes(&EC, ProtectedHeaders); |
| 1622 | |
| 1623 | // Empty map with unprotected headers |
| 1624 | QCBOREncode_OpenMap(&EC); |
| 1625 | QCBOREncode_AddBytesToMapN(&EC, 4, Kid); |
| 1626 | QCBOREncode_CloseMap(&EC); |
| 1627 | |
| 1628 | // The payload |
| 1629 | UsefulBufC WrappedPayload; |
| 1630 | QCBOREncode_BstrWrap(&EC); |
| 1631 | QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1 |
| 1632 | QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload); |
| 1633 | |
| 1634 | // Check we got back the actual payload expected |
| 1635 | if(UsefulBuf_Compare(WrappedPayload, Payload)) { |
| 1636 | return -1; |
| 1637 | } |
| 1638 | |
| 1639 | // The signature |
| 1640 | QCBOREncode_AddBytes(&EC, Signature); |
| 1641 | QCBOREncode_CloseArray(&EC); |
| 1642 | |
| 1643 | // Finish and check the results |
| 1644 | UsefulBufC COSE_Sign1; |
| 1645 | if(QCBOREncode_Finish2(&EC, &COSE_Sign1)) { |
| 1646 | return -2; |
| 1647 | } |
| 1648 | |
| 1649 | // 98 is the size from RFC 8152 C.2.1 |
| 1650 | if(COSE_Sign1.len != 98) { |
| 1651 | return -3; |
| 1652 | } |
| 1653 | |
| 1654 | if(UsefulBuf_Compare(COSE_Sign1, Expected)) { |
| 1655 | return -4; |
| 1656 | } |
| 1657 | |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |