blob: a486614dcc76664899431fd3e8cb8c68dda5e57a [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
2Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
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
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28==============================================================================*/
29
30#include "qcbor.h"
31#include "qcbor_encode_tests.h"
32#include <stdio.h>
33#include <strings.h>
34#include <float.h>
35#include <math.h>
36#include <stdlib.h>
37
38
39
40// TODO: -- test on a 32-bit machine)
41
42// TODO: test QCBOR_MAX_ITEMS_IN_ARRAY (this is very large...)
43
44//
45
46#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
47// ifdef these out to not have compiler warnings
48static void printencoded(const uint8_t *pEncoded, size_t nLen)
49{
50 int i;
51 for(i = 0; i < nLen; i++) {
52 uint8_t Z = pEncoded[i];
53 printf("%02x ", Z);
54 }
55 printf("\n");
56
57 fflush(stdout);
58}
59
60
61static void printencodedE(EncodedCBOR E)
62{
63 printf("Num Items: %d\n", E.uItems);
64 printencoded(E.Bytes.ptr, E.Bytes.len);
65}
66#endif
67
68
69#define CheckResults(Enc, Expected) \
70 UsefulBuf_Compare(UsefulBufConst(Enc.Bytes), (UsefulBufC){Expected, sizeof(Expected)})
71
72
73
74static const uint8_t pExpectedEncodedAll[] = {
75
76 0x98, 0x29, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8, 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54, 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16, 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42, 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45, 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49, 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20, 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a, 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f, 0xaf, 0x07, 0xff, 0x65, 0x4a, 0x61, 0x69, 0x6d, 0x65, 0xd8, 0x58, 0xfa, 0x40, 0x49, 0x0f, 0xd0, 0x66, 0x53, 0x74, 0x72, 0x65, 0x65, 0x74, 0xd8, 0x63, 0xfb, 0x40, 0x21, 0x4f, 0x01, 0x96, 0xd8, 0xf4, 0xf9, 0xfa, 0x3f, 0x80, 0x00, 0x00, 0xfb, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x63, 0x66, 0x6f, 0x6f, 0xfa, 0x45, 0x98, 0xb8, 0x00, 0x39, 0x03, 0xe6, 0xfa, 0x44, 0x79, 0xc0, 0x00, 0x66, 0x66, 0x6f, 0x6f, 0x66, 0x6f, 0x6f, 0xfb, 0x41, 0x58, 0xf7, 0x7d, 0xc0, 0x00, 0x00, 0x00, 0x39, 0xaf, 0xc6, 0xfb, 0x40, 0xf5, 0xba, 0x70, 0x00, 0x00, 0x00, 0x00, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a, 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65, 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68, 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74, 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76, 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69, 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00, 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20, 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39, 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f, 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d, 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65, 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78, 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63, 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56, 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8, 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8, 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23, 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29, 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61, 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66, 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f, 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63, 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89, 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77, 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d, 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f, 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d, 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c, 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64, 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78, 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7, 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62, 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80, 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62, 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1, 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5, 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32, 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05, 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8, 0x58, 0xf3,
77
78 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
79
80 0xf5, 0xf4, 0xa2, 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19, 0x10, 0x41, 0xf5,
81
82 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
83
84};
85
86
87static const char *szMIME = "\
88MIME-Version: 1.0\n\
89Content-Type: multipart/mixed;\n\
90boundary=\"XXXXboundary text\"\n\
91\n\
92This is a multipart message in MIME format.\n\
93\n\
94--XXXXboundary text\n\
95Content-Type: text/plain\n\
96\n\
97this is the body text\n\
98\n\
99--XXXXboundary text\n\
100Content-Type: text/plain;\n\
101Content-Disposition: attachment;\n\
102filename=\"test.txt\"\n\
103\n\
104this is the attachment text\n\
105\n\
106--XXXXboundary text--";
107
108
109int AllAddMethodsTest()
110{
111 QCBOREncodeContext ECtx;
112 int nReturn = 0;
113
114 uint8_t pEncoded[3000];
115 size_t nEncodedLen = sizeof(pEncoded);
116
117 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
118
119 QCBOREncode_OpenArray(&ECtx);
120
121 // Non-map ints
122 QCBOREncode_AddUInt64_3(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 100, 89989909);
123 QCBOREncode_AddInt64_3(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 76, 77689989909);
124 QCBOREncode_AddUInt64(&ECtx,0);
125 QCBOREncode_AddInt64(&ECtx, -44);
126
127 // ints that go in maps
128 QCBOREncode_OpenMap(&ECtx);
129 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
130 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
131 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
132 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
133 QCBOREncode_CloseMap(&ECtx);
134
135 // floats and doubles
136 QCBOREncode_AddFloat_3(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 88, 3.14159);
137 QCBOREncode_AddDouble_3(&ECtx, "Street", QCBOR_NO_INT_LABEL, 99, 8.654309);
138 QCBOREncode_AddFloat(&ECtx, 1);
139 QCBOREncode_AddDouble(&ECtx, 1);
140
141 // floats and doubles that go in map
142 QCBOREncode_OpenMap(&ECtx);
143 QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887);
144 QCBOREncode_AddFloatToMapN(&ECtx, -999, 999);
145 QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887);
146 QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999);
147 QCBOREncode_CloseMap(&ECtx);
148
149 // Epoch Date
150 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
151
152 // Epoch date with labels
153 QCBOREncode_OpenMap(&ECtx);
154 QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000);
155 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
156 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
157 QCBOREncode_CloseMap(&ECtx);
158
159 // Binary blobs
160 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
161
162 // binary blobs in maps
163 QCBOREncode_OpenMap(&ECtx);
164 QCBOREncode_AddBytes_3(&ECtx, "binbin", QCBOR_NO_INT_LABEL, 100000, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
165 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
166 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
167 QCBOREncode_CloseMap(&ECtx);
168
169 // text blobs
170 QCBOREncode_AddText(&ECtx, SZLiteralToUsefulBufC("bar bar foo bar"));
171 QCBOREncode_AddSZString(&ECtx, "oof\n");
172 QCBOREncode_AddURI(&ECtx, SZLiteralToUsefulBufC("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
173 QCBOREncode_AddB64Text(&ECtx, SZLiteralToUsefulBufC("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
174 QCBOREncode_AddRegex(&ECtx, SZLiteralToUsefulBufC("[^abc]+"));
175 QCBOREncode_AddMIMEData(&ECtx, SZToUsefulBufC(szMIME));
176
177 // text blobs in maps
178 QCBOREncode_OpenMap(&ECtx);
179 QCBOREncode_AddTextToMap(&ECtx, "#####", SZLiteralToUsefulBufC("foo bar foo foo"));
180 QCBOREncode_AddText_3(&ECtx, "____", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, SZLiteralToUsefulBufC("foo bar"));
181 QCBOREncode_AddSZString_3(&ECtx, "()()()", QCBOR_NO_INT_LABEL, 1000, "rab rab oof");
182 QCBOREncode_AddTextToMapN(&ECtx,22, SZLiteralToUsefulBufC("foo foo foo foo"));
183 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
184 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
185 QCBOREncode_AddURIToMap(&ECtx, "RFC", SZLiteralToUsefulBufC("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
186 QCBOREncode_AddURIToMapN(&ECtx, 0x89, SZLiteralToUsefulBufC("http://cbor.me/"));
187 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", SZLiteralToUsefulBufC("cGxlYXN1cmUu"));
188 QCBOREncode_AddB64TextToMapN(&ECtx, 64, SZLiteralToUsefulBufC("c3VyZS4="));
189 QCBOREncode_AddRegexToMap(&ECtx, "popo", SZLiteralToUsefulBufC("100\\s*mk")); // x code string literal bug
190 QCBOREncode_AddRegexToMapN(&ECtx, -51, SZLiteralToUsefulBufC("perl\\B")); // x code string literal bug
191 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", SZToUsefulBufC(szMIME));
192 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, SZToUsefulBufC(szMIME));
193 QCBOREncode_CloseMap(&ECtx);
194
195 // Date strings
196 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
197 QCBOREncode_OpenMap(&ECtx);
198 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
199 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
200 QCBOREncode_CloseMap(&ECtx);
201
202 // true / false ...
203 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
204 QCBOREncode_OpenMap(&ECtx);
205 QCBOREncode_AddSimple_3(&ECtx, "dare", QCBOR_NO_INT_LABEL, 66, CBOR_SIMPLEV_TRUE);
206 QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
207 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
208 QCBOREncode_CloseMap(&ECtx);
209
210 // opening an array
211 QCBOREncode_OpenArray(&ECtx);
212 QCBOREncode_CloseArray(&ECtx);
213
214 // opening arrays in a map
215 QCBOREncode_OpenMap(&ECtx);
216 QCBOREncode_OpenArray_3(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL, 1093, 0);
217 QCBOREncode_CloseArray(&ECtx);
218 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
219 QCBOREncode_CloseArray(&ECtx);
220 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
221 QCBOREncode_CloseArray(&ECtx);
222 QCBOREncode_CloseMap(&ECtx);
223
224 // opening maps with labels and tagging
225 QCBOREncode_OpenMap(&ECtx);
226 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
227 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
228 QCBOREncode_OpenMap_3(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL, 9087, 0);
229 QCBOREncode_CloseMap(&ECtx);
230 QCBOREncode_CloseMap(&ECtx);
231 QCBOREncode_CloseMap(&ECtx);
232 QCBOREncode_CloseMap(&ECtx);
233
234 // Extended simple values (these are not standard...)
235 QCBOREncode_OpenMap(&ECtx);
236 QCBOREncode_AddRawSimple_3(&ECtx, "s1", QCBOR_NO_INT_LABEL, 88, 255);
237 QCBOREncode_AddRawSimple_3(&ECtx, "s2", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 0);
238 QCBOREncode_AddRawSimple_3(&ECtx, "s3", QCBOR_NO_INT_LABEL, 88, 33);
239 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 88378374, 88, 255);
240 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 89, 88, 19);
241 QCBOREncode_CloseMap(&ECtx);
242
243
244 // UUIDs
245 static uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
246 UsefulBufC XXUUID = ByteArrayLiteralToUsefulBufC(ppppUUID);
247 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
248 QCBOREncode_OpenMap(&ECtx);
249 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
250 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
251 QCBOREncode_CloseMap(&ECtx);
252
253
254 // Bool
255 QCBOREncode_AddBool(&ECtx, true);
256 QCBOREncode_AddBool(&ECtx, false);
257 QCBOREncode_OpenMap(&ECtx);
258 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
259 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
260 QCBOREncode_CloseMap(&ECtx);
261
262
263 static uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
264 UsefulBufC BIGNUM = ByteArrayLiteralToUsefulBufC(pBignum);
265 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
266 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
267 QCBOREncode_OpenMap(&ECtx);
268 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
269 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
270 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
271 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
272 QCBOREncode_CloseMap(&ECtx);
273
274 QCBOREncode_CloseArray(&ECtx);
275
276 EncodedCBOR Enc;
277
278 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
279 nReturn = -1;
280 goto Done;
281 }
282
283 //printencodedE(Enc);
284
285 if(CheckResults(Enc,pExpectedEncodedAll))
286 nReturn = -1;
287
288Done:
289 return nReturn;
290}
291
292// todo -- add a test for counting the top level items and adding it back in with AddRaw()
293
294
295static const uint8_t pExpectedEncodedInts[] = {
296 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
297 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
298 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
299 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
300 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
301 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
302 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
303 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
304 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
305 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
306 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
307 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
308 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
309 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
310 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
311 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
312 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
313 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
314 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
315 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
316 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
317 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
318 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
319 0xff, 0xff};
320
321/*
322
323 Test the generation of integers. This also ends up testing
324 encoding of all the different lengths. It encodes integers
325 of many lengths and values, especially around the boundaries
326 for different types of integers. It compares the output
327 to expected values generated from http://cbor.me.
328
329 */
330int IntegerValuesTest1()
331{
332 QCBOREncodeContext ECtx;
333 int nReturn = 0;
334
335 uint8_t pEncoded[1000];
336 size_t nEncodedLen = sizeof(pEncoded);
337
338 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
339 QCBOREncode_OpenArray(&ECtx);
340
341 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
342 QCBOREncode_AddInt64(&ECtx, -4294967297);
343 QCBOREncode_AddInt64(&ECtx, -4294967296);
344 QCBOREncode_AddInt64(&ECtx, -4294967295);
345 QCBOREncode_AddInt64(&ECtx, -4294967294);
346 QCBOREncode_AddInt64(&ECtx, -2147483648);
347 QCBOREncode_AddInt64(&ECtx, -2147483647);
348 QCBOREncode_AddInt64(&ECtx, -65538);
349 QCBOREncode_AddInt64(&ECtx, -65537);
350 QCBOREncode_AddInt64(&ECtx, -65536);
351 QCBOREncode_AddInt64(&ECtx, -65535);
352 QCBOREncode_AddInt64(&ECtx, -65534);
353 QCBOREncode_AddInt64(&ECtx, -257);
354 QCBOREncode_AddInt64(&ECtx, -256);
355 QCBOREncode_AddInt64(&ECtx, -255);
356 QCBOREncode_AddInt64(&ECtx, -254);
357 QCBOREncode_AddInt64(&ECtx, -25);
358 QCBOREncode_AddInt64(&ECtx, -24);
359 QCBOREncode_AddInt64(&ECtx, -23);
360 QCBOREncode_AddInt64(&ECtx, -1);
361 QCBOREncode_AddInt64(&ECtx, 0);
362 QCBOREncode_AddUInt64(&ECtx, 0ULL);
363 QCBOREncode_AddInt64(&ECtx, 1);
364 QCBOREncode_AddInt64(&ECtx, 22);
365 QCBOREncode_AddInt64(&ECtx, 23);
366 QCBOREncode_AddInt64(&ECtx, 24);
367 QCBOREncode_AddInt64(&ECtx, 25);
368 QCBOREncode_AddInt64(&ECtx, 26);
369 QCBOREncode_AddInt64(&ECtx, 254);
370 QCBOREncode_AddInt64(&ECtx, 255);
371 QCBOREncode_AddInt64(&ECtx, 256);
372 QCBOREncode_AddInt64(&ECtx, 257);
373 QCBOREncode_AddInt64(&ECtx, 65534);
374 QCBOREncode_AddInt64(&ECtx, 65535);
375 QCBOREncode_AddInt64(&ECtx, 65536);
376 QCBOREncode_AddInt64(&ECtx, 65537);
377 QCBOREncode_AddInt64(&ECtx, 65538);
378 QCBOREncode_AddInt64(&ECtx, 2147483647);
379 QCBOREncode_AddInt64(&ECtx, 2147483647);
380 QCBOREncode_AddInt64(&ECtx, 2147483648);
381 QCBOREncode_AddInt64(&ECtx, 2147483649);
382 QCBOREncode_AddInt64(&ECtx, 4294967294);
383 QCBOREncode_AddInt64(&ECtx, 4294967295);
384 QCBOREncode_AddInt64(&ECtx, 4294967296);
385 QCBOREncode_AddInt64(&ECtx, 4294967297);
386 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
387 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
388
389 QCBOREncode_CloseArray(&ECtx);
390
391 EncodedCBOR Enc;
392 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
393 nReturn = -1;
394 }
395
396 if(CheckResults(Enc, pExpectedEncodedInts))
397 return -1;
398
399 if(Enc.Bytes.len != sizeof(pExpectedEncodedInts) || bcmp(pEncoded, pExpectedEncodedInts, Enc.Bytes.len))
400 nReturn = -1;
401
402 //printencoded(pEncoded, nEncodedLen);
403
404 return(nReturn);
405}
406
407
408
409static uint8_t pExpectedEncodedSimple[] = {
410 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
411
412
413int SimpleValuesTest1()
414{
415 QCBOREncodeContext ECtx;
416 int nReturn = 0;
417
418 uint8_t pEncoded[100];
419 size_t nEncodedLen = sizeof(pEncoded);
420
421 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
422 QCBOREncode_OpenArray(&ECtx);
423
424 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
425 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
426 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
427 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
428
429 QCBOREncode_OpenMap(&ECtx);
430
431 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
432 QCBOREncode_CloseMap(&ECtx);
433
434 QCBOREncode_CloseArray(&ECtx);
435
436 EncodedCBOR ECBOR;
437 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
438 nReturn = -1;
439 }
440
441 if(ECBOR.Bytes.len != sizeof(pExpectedEncodedSimple) || bcmp(pEncoded, pExpectedEncodedSimple, ECBOR.Bytes.len))
442 nReturn = -1;
443
444 // printencoded(pEncoded, nEncodedLen);
445
446 return(nReturn);
447}
448
449
450static uint8_t pExpectedEncodedFloat[] = {
451 0x98, 0x1e, 0xfa, 0x00, 0x00, 0x00, 0x00, 0xfa,
452 0x3f, 0x80, 0x00, 0x00, 0xfa, 0x3f, 0x8c, 0xcc,
453 0xcd, 0xfa, 0x3f, 0xc0, 0x00, 0x00, 0xfa, 0x47,
454 0x7f, 0xe0, 0x00, 0xfa, 0x47, 0xc3, 0x50, 0x00,
455 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0xfa, 0x7f, 0x80,
456 0x00, 0x00, 0xfa, 0x33, 0x80, 0x00, 0x00, 0xfa,
457 0x38, 0x80, 0x00, 0x00, 0xfa, 0xc0, 0x80, 0x00,
458 0x00, 0xfa, 0xc0, 0x83, 0x33, 0x33, 0xfa, 0x7f,
459 0xc0, 0x00, 0x00, 0xfa, 0x7f, 0x80, 0x00, 0x00,
460 0xfa, 0xff, 0x80, 0x00, 0x00, 0xfb, 0x00, 0x00,
461 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb, 0x3f,
462 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb,
463 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a,
464 0xfb, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
465 0x00, 0xfb, 0x40, 0xef, 0xfc, 0x00, 0x00, 0x00,
466 0x00, 0x00, 0xfb, 0x40, 0xf8, 0x6a, 0x00, 0x00,
467 0x00, 0x00, 0x00, 0xfb, 0x47, 0xef, 0xff, 0xff,
468 0xe0, 0x00, 0x00, 0x00, 0xfb, 0x7e, 0x37, 0xe4,
469 0x3c, 0x88, 0x00, 0x75, 0x9c, 0xfb, 0x3e, 0x70,
470 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb, 0x3f,
471 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb,
472 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
473 0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66,
474 0x66, 0xfb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00,
475 0x00, 0x00, 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00,
476 0x00, 0x00, 0x00, 0xfb, 0xff, 0xf0, 0x00, 0x00,
477 0x00, 0x00, 0x00, 0x00};
478
479
480int FloatValuesTest1()
481{
482 QCBOREncodeContext ECtx;
483 int nReturn = 0;
484
485 uint8_t pEncoded[1000];
486 size_t nEncodedLen = sizeof(pEncoded);
487
488 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
489 QCBOREncode_OpenArray(&ECtx);
490
491 // These are all samples published
492 // in RFC 7049.
493 QCBOREncode_AddFloat(&ECtx, 0.0);
494 QCBOREncode_AddFloat(&ECtx, 1.0);
495 QCBOREncode_AddFloat(&ECtx, 1.1); // appx
496 QCBOREncode_AddFloat(&ECtx, 1.5);
497 QCBOREncode_AddFloat(&ECtx, 65504.0);
498 QCBOREncode_AddFloat(&ECtx, 100000.0);
499 QCBOREncode_AddFloat(&ECtx, 3.4028234663852886e+38);
500 QCBOREncode_AddFloat(&ECtx, 1.0e+300); // Infinity?
501 QCBOREncode_AddFloat(&ECtx, 5.960464477539063e-8);
502 QCBOREncode_AddFloat(&ECtx, 0.00006103515625);
503 QCBOREncode_AddFloat(&ECtx, -4.0);
504 QCBOREncode_AddFloat(&ECtx, -4.1); // appx
505
506 QCBOREncode_AddFloat(&ECtx, NAN);
507 QCBOREncode_AddFloat(&ECtx, INFINITY);
508 QCBOREncode_AddFloat(&ECtx, -INFINITY);
509
510
511 QCBOREncode_AddDouble(&ECtx, 0.0);
512 QCBOREncode_AddDouble(&ECtx, 1.0);
513 QCBOREncode_AddDouble(&ECtx, 1.1); // appx
514 QCBOREncode_AddDouble(&ECtx, 1.5);
515 QCBOREncode_AddDouble(&ECtx, 65504.0);
516 QCBOREncode_AddDouble(&ECtx, 100000.0);
517 QCBOREncode_AddDouble(&ECtx, 3.4028234663852886e+38);
518 QCBOREncode_AddDouble(&ECtx, 1.0e+300); // Infinity?
519 QCBOREncode_AddDouble(&ECtx, 5.960464477539063e-8);
520 QCBOREncode_AddDouble(&ECtx, 0.00006103515625);
521 QCBOREncode_AddDouble(&ECtx, -4.0);
522 QCBOREncode_AddDouble(&ECtx, -4.1); // appx
523
524 QCBOREncode_AddDouble(&ECtx, NAN);
525 QCBOREncode_AddDouble(&ECtx, INFINITY);
526 QCBOREncode_AddDouble(&ECtx, -INFINITY);
527
528 QCBOREncode_CloseArray(&ECtx);
529 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
530 nReturn = -1;
531 }
532
533 if(nEncodedLen != sizeof(pExpectedEncodedFloat) || bcmp(pEncoded, pExpectedEncodedFloat, nEncodedLen))
534 nReturn = -1;
535
536 //printencoded(pEncoded, nEncodedLen);
537
538 return(nReturn);
539}
540
541static uint8_t pExpectedEncodedDates[] = {
542 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54,
543 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30,
544 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
545 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65,
546 0x20, 0x44, 0x61, 0x74, 0x65, 0x20, 0x66, 0x72,
547 0x6f, 0x6d, 0x20, 0x52, 0x46, 0x43, 0x20, 0x33,
548 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31, 0x39, 0x38,
549 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
550 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30,
551 0x2e, 0x35, 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1,
552 0x19, 0x03, 0xe7
553};
554
555int EncodeDateTest()
556{
557 QCBOREncodeContext ECtx;
558 int nReturn = 0;
559
560 uint8_t pEncoded[1000];
561 size_t nEncodedLen = sizeof(pEncoded);
562
563 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
564
565 QCBOREncode_OpenArray(&ECtx);
566
567
568 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
569 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
570
571
572 QCBOREncode_OpenMap(&ECtx);
573
574 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
575
576
577 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
578
579 QCBOREncode_CloseMap(&ECtx);
580
581 QCBOREncode_CloseArray(&ECtx);
582
583
584 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
585 nReturn = -1;
586 }
587
588 if(nEncodedLen != sizeof(pExpectedEncodedDates) || bcmp(pEncoded, pExpectedEncodedDates, nEncodedLen))
589 nReturn = -1;
590
591 //printencoded(pEncoded, nEncodedLen);
592
593 return(nReturn);
594
595}
596
597
598int ArrayNestingTest1()
599{
600 QCBOREncodeContext ECtx;
601 int i;
602 int nReturn = 0;
603
604 uint8_t pEncoded[100];
605 size_t nEncodedLen = sizeof(pEncoded);
606
607 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
608 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
609 QCBOREncode_OpenArray(&ECtx);
610 }
611 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
612 QCBOREncode_CloseArray(&ECtx);
613 }
614 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
615 printf("ArrayNestingTest1 Failed\n");
616 nReturn = -1;
617 }
618 //printencoded(pEncoded, nEncodedLen);
619
620 return(nReturn);
621}
622
623
624
625int ArrayNestingTest2()
626{
627 QCBOREncodeContext ECtx;
628 int i;
629 int nReturn = 0;
630
631 uint8_t pEncoded[100];
632 size_t nEncodedLen = sizeof(pEncoded);
633
634 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
635 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
636 QCBOREncode_OpenArray(&ECtx);
637 }
638 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
639 QCBOREncode_CloseArray(&ECtx);
640 }
641 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
642 nReturn = -1;
643 }
644
645 return(nReturn);
646}
647
648
649
650int ArrayNestingTest3()
651{
652 QCBOREncodeContext ECtx;
653 int i;
654 int nReturn = 0;
655
656 uint8_t pEncoded[100];
657 size_t nEncodedLen = sizeof(pEncoded);
658
659 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
660 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
661 QCBOREncode_OpenArray(&ECtx);
662 }
663 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
664 QCBOREncode_CloseArray(&ECtx);
665 }
666 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
667 nReturn = -1;
668 }
669
670 return(nReturn);
671}
672
673
674
675static uint8_t s_pFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
676
677static int EncodeRaw(uint8_t **pEncoded, size_t *pEncodedLen)
678{
679 QCBOREncodeContext ECtx;
680 int nReturn = -1;
681
682 *pEncoded = NULL;
683 *pEncodedLen = INT32_MAX; // largest buffer this CBOR implementation will deal with
684
685 // loop runs CBOR encoding twice. First with no buffer to
686 // calucate the length so buffer can be allocated correctly,
687 // and last with the buffer to do the actual encoding
688 do {
689 QCBOREncode_Init(&ECtx, *pEncoded, *pEncodedLen);
690 QCBOREncode_OpenArray(&ECtx);
691 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {s_pFiveArrarys, 5}), 1});
692 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {pExpectedEncodedInts, sizeof(pExpectedEncodedInts)}), 1});
693 QCBOREncode_CloseArray(&ECtx);
694
695 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
696 goto Done;
697 if(*pEncoded != NULL) {
698 nReturn = 0;
699 goto Done;
700 }
701 *pEncoded = malloc(*pEncodedLen);
702 } while(1);
703
704Done:
705 return(nReturn);
706}
707
708
709// Validated at http://cbor.me and by manually examining its output
710static uint8_t s_pEncodeRawExpected[] = {
711 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
712 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
713 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
714 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
715 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
716 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
717 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
718 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
719 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
720 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
721 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
722 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
723 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
724 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
725 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
726 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
727 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
728 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
729 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
730 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
731 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
732 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
733 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
734 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
735
736
737
738int EncodeRawTest()
739{
740 uint8_t *pEncodedRaw;
741 size_t nEncodedRawLen;
742
743 if(EncodeRaw(&pEncodedRaw, &nEncodedRawLen)) {
744 return(-1);
745 }
746
747 //printencoded(pEncodedRaw, nEncodedRawLen);
748
749 int nReturn = 0;
750 if(nEncodedRawLen != sizeof(s_pEncodeRawExpected) || memcmp(s_pEncodeRawExpected, pEncodedRaw, sizeof(s_pEncodeRawExpected)))
751 nReturn = 1;
752
753 return(nReturn);
754}
755
756
757
758
759
760static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
761{
762 QCBOREncodeContext ECtx;
763 int nReturn = -1;
764
765 *pEncoded = NULL;
766 *pEncodedLen = INT32_MAX;
767
768 // loop runs CBOR encoding twice. First with no buffer to
769 // calucate the length so buffer can be allocated correctly,
770 // and last with the buffer to do the actual encoding
771 do {
772 QCBOREncode_Init(&ECtx, *pEncoded, *pEncodedLen);
773 QCBOREncode_OpenMap(&ECtx);
774 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
775 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
776 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
777 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
778 QCBOREncode_CloseArray(&ECtx);
779 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
780 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
781 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
782 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
783 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
784 QCBOREncode_CloseMap(&ECtx);
785 QCBOREncode_CloseMap(&ECtx);
786
787
788 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
789 goto Done;
790 if(*pEncoded != NULL) {
791 nReturn = 0;
792 goto Done;
793 }
794 *pEncoded = malloc(*pEncodedLen);
795 } while(1);
796
797 Done:
798 return(nReturn);
799}
800
801
802static uint8_t pValidMapEncoded[] = {
803 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a,
804 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, 0x6f, 0x20,
805 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x67,
806 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20,
807 0x6d, 0x61, 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, 0x44, 0x78, 0x78, 0x78, 0x78,
808 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f,
809 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
810 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
811 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 } ;
812
813
814
815int MapEncodeTest()
816{
817 uint8_t *pEncodedMaps;
818 size_t nEncodedMapLen;
819
820 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
821 return(-1);
822 }
823
824 //printencoded(pEncodedMaps, nEncodedMapLen);
825
826 int nReturn = 0;
827 if(memcmp(pValidMapEncoded, pEncodedMaps, sizeof(pValidMapEncoded)))
828 nReturn = 1;
829
830 return(nReturn);
831}
832
833
834
835/*
836 @brief Encode the RTIC results
837
838 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
839 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
840 @param[in] szAlexString Diagnostic code.
841 @param[in[ pOut Buffer to put the result in
842 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
843
844 @return
845 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
846
847 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
848 short an error will be returned. This function will never write off the end
849 of the buffer passed to it.
850
851 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
852 length of the encoded CBOR.
853
854 */
855
856int FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, uint8_t *pOut, size_t *pnLen)
857{
858 // Buffer that the result will be written in to
859 // It is fixed size and small that a stack variable will be fine
860 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
861
862 // Context for the encoder
863 QCBOREncodeContext ECtx;
864 QCBOREncode_Init(&ECtx, pOut, *pnLen);
865
866 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
867 // Contents are label / value pairs
868 QCBOREncode_OpenMap(&ECtx);
869
870 { // Brace / indention just to show CBOR encoding nesting
871
872 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
873 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
874
875 // Add the diagnostic code
876 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
877
878 // Add a time stamp
879 if(time) {
880 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
881 }
882
883 // Add the diagnostic code
884 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
885
886 // Open a subordinate map for telemtry data
887 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
888
889 { // Brace / indention just to show CBOR encoding nesting
890
891 // Add a few fake integers and buffers for now.
892 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
893
894 // Add a few fake integers and buffers for now.
895 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
896
897 // Add a few fake integers and buffers for now.
898 const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
899 UsefulBufC WSPV = {pPV, sizeof(pPV)};
900
901 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
902 }
903 }
904
905 // Close the telemetry map
906 QCBOREncode_CloseMap(&ECtx);
907
908 // Close the map
909 QCBOREncode_CloseMap(&ECtx);
910
911 return QCBOREncode_Finish(&ECtx, pnLen);
912}
913
914
915static 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};
916
917
918int RTICResultsTest()
919{
920 uint8_t out[200];
921 size_t len = sizeof(out);
922
923 int nResult = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, "recent", "0xA1eC5001", out, &len);
924 if(nResult)
925 return -1;
926
927 if(memcmp(pExpectedRTIC, out, sizeof(pExpectedRTIC)))
928 return 1;
929
930 //printencoded(out, len);
931
932 return 0;
933}
934
935static const uint8_t pBStrArrayExpected[] = { 0x45, 0x81, 0x63, 0x66, 0x6F, 0x6F };
936
937static const uint8_t pBStrMapExpected[] = {
938 0x81, // array of 1
939 0x58, 0x1e, // bstr wrapper
940 0xA1, // map of 1
941 0x13, // label 19
942 0x78, 0x1a, // text string of 26
943 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
944 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a};
945
946int BStrWrapTests()
947{
948 QCBOREncodeContext ECtx;
949
950 uint8_t pEncoded[100];
951 size_t nEncodedLen = sizeof(pEncoded);
952
953 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
954
955 QCBOREncode_OpenArray_3(&ECtx, NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 1);
956 QCBOREncode_AddSZString(&ECtx, "foo");
957 QCBOREncode_CloseArray(&ECtx);
958
959 EncodedCBOR E;
960 QCBOREncode_Finish2(&ECtx, &E);
961
962 if(UsefulBuf_Compare(UsefulBuf_Const(E.Bytes), ByteArrayLiteralToUsefulBufC(pBStrArrayExpected) )) {
963 return 1;
964 }
965
966
967 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
968
969 QCBOREncode_OpenArray(&ECtx);
970
971 QCBOREncode_OpenMap_3(&ECtx, NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 1);
972 QCBOREncode_AddSZStringToMapN(&ECtx, 19, "abcdefghijklmnopqrstuvwxyz");
973 QCBOREncode_CloseMap(&ECtx);
974 QCBOREncode_CloseArray(&ECtx);
975
976 QCBOREncode_Finish2(&ECtx, &E);
977
978 if(UsefulBuf_Compare(UsefulBuf_Const(E.Bytes), ByteArrayLiteralToUsefulBufC(pBStrMapExpected) )) {
979 return 2;
980 }
981
982 return 0;
983}
984
985
986
987