1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 #include "test_cborattr.h"
20 
TEST_CASE_SELF(test_cborattr_encode_simple)21 TEST_CASE_SELF(test_cborattr_encode_simple)
22 {
23     const struct cbor_out_attr_t attrs[] = {
24         {
25             .attribute = "null",
26             .val = {
27                 .type = CborAttrNullType,
28             },
29         },
30         {
31             .attribute = "bool",
32             .val = {
33                 .type = CborAttrBooleanType,
34                 .boolean = true,
35             },
36         },
37         {
38             .attribute = "int",
39             .val = {
40                 .type = CborAttrIntegerType,
41                 .integer = -99,
42             },
43         },
44         {
45             .attribute = "uint",
46             .val = {
47                 .type = CborAttrUnsignedIntegerType,
48                 .integer = 8442,
49             },
50         },
51         {
52             .attribute = "float",
53             .val = {
54                 .type = CborAttrFloatType,
55                 .fval = 8.0,
56             },
57         },
58         {
59             .attribute = "double",
60             .val = {
61                 .type = CborAttrDoubleType,
62                 .real = 16.0,
63             },
64         },
65         {
66             .attribute = "bytes",
67             .val = {
68                 .type = CborAttrByteStringType,
69                 .bytestring.data = (uint8_t[]) {1, 2, 3},
70                 .bytestring.len = 3,
71             },
72         },
73         {
74             .attribute = "str",
75             .val = {
76                 .type = CborAttrTextStringType,
77                 .string = "mystr",
78             },
79         },
80         {
81             .attribute = "arr",
82             .val = {
83                 .type = CborAttrArrayType,
84                 .array = {
85                     .elems = (struct cbor_out_val_t[]) {
86                         [0] = {
87                             .type = CborAttrUnsignedIntegerType,
88                             .integer = 4355,
89                         },
90                         [1] = {
91                             .type = CborAttrBooleanType,
92                             .integer = false,
93                         },
94                     },
95                     .len = 2,
96                 },
97             },
98         },
99         {
100             .attribute = "obj",
101             .val = {
102                 .type = CborAttrObjectType,
103                 .obj = (struct cbor_out_attr_t[]) {
104                     {
105                         .attribute = "inner_str",
106                         .val = {
107                             .type = CborAttrTextStringType,
108                             .string = "mystr2",
109                         },
110                     },
111                     {
112                         .attribute = "inner_int",
113                         .val = {
114                             .type = CborAttrIntegerType,
115                             .integer = 123,
116                         },
117                     },
118                     { 0 }
119                 },
120             },
121         },
122         { 0 }
123     };
124     const uint8_t exp[] = {
125         0xbf, 0x64, 0x6e, 0x75, 0x6c, 0x6c, 0xf6, 0x64,
126         0x62, 0x6f, 0x6f, 0x6c, 0xf5, 0x63, 0x69, 0x6e,
127         0x74, 0x38, 0x62, 0x64, 0x75, 0x69, 0x6e, 0x74,
128         0x19, 0x20, 0xfa, 0x65, 0x66, 0x6c, 0x6f, 0x61,
129         0x74, 0xfa, 0x41, 0x00, 0x00, 0x00, 0x66, 0x64,
130         0x6f, 0x75, 0x62, 0x6c, 0x65, 0xfb, 0x40, 0x30,
131         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x62,
132         0x79, 0x74, 0x65, 0x73, 0x43, 0x01, 0x02, 0x03,
133         0x63, 0x73, 0x74, 0x72, 0x65, 0x6d, 0x79, 0x73,
134         0x74, 0x72, 0x63, 0x61, 0x72, 0x72, 0x82, 0x19,
135         0x11, 0x03, 0xf4, 0x63, 0x6f, 0x62, 0x6a, 0xbf,
136         0x69, 0x69, 0x6e, 0x6e, 0x65, 0x72, 0x5f, 0x73,
137         0x74, 0x72, 0x66, 0x6d, 0x79, 0x73, 0x74, 0x72,
138         0x32, 0x69, 0x69, 0x6e, 0x6e, 0x65, 0x72, 0x5f,
139         0x69, 0x6e, 0x74, 0x18, 0x7b, 0xff, 0xff,
140     };
141 
142     cborattr_test_util_encode(attrs, exp, sizeof exp);
143 }
144