1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <math.h>
9 #include <corner_cases.h>
10 #include <zcbor_decode.h>
11 #include <zcbor_print.h>
12 
13 #define CONCAT_BYTE(a,b) a ## b
14 
15 #ifdef TEST_INDEFINITE_LENGTH_ARRAYS
16 #define LIST(num) 0x9F /* Use short count 31 (indefinite-length). Note that the 'num' argument is ignored */
17 #define LIST2(num) 0x9F /* Use short count 31 (indefinite-length). Note that the 'num' argument is ignored */
18 #define LIST3(num) 0x9F /* Use short count 31 (indefinite-length). Note that the 'num' argument is ignored */
19 #define MAP(num) 0xBF  /* Use short count 31 (indefinite-length). Note that the 'num' argument is ignored */
20 #define ARR_ERR1 ZCBOR_ERR_WRONG_TYPE
21 #define ARR_ERR2 ZCBOR_ERR_NO_PAYLOAD
22 #define ARR_ERR3 ZCBOR_ERR_WRONG_TYPE
23 #define ARR_ERR4 ZCBOR_ERR_PAYLOAD_NOT_CONSUMED
24 #define END 0xFF,
25 #define STR_LEN(len, lists) (len + lists)
26 #else
27 #define LIST(num) CONCAT_BYTE(0x8, num)
28 #define LIST2(num) CONCAT_BYTE(0x9, num)
29 #define LIST3(num) 0x98, num
30 #define MAP(num) CONCAT_BYTE(0xA, num)
31 #define ARR_ERR1 ZCBOR_ERR_HIGH_ELEM_COUNT
32 #define ARR_ERR2 ZCBOR_ERR_HIGH_ELEM_COUNT
33 #define ARR_ERR3 ZCBOR_ERR_NO_PAYLOAD
34 #define ARR_ERR4 ZCBOR_ERR_NO_PAYLOAD
35 #define END
36 #define STR_LEN(len, lists) (len)
37 #endif
38 
39 
ZTEST(cbor_decode_test5,test_numbers)40 ZTEST(cbor_decode_test5, test_numbers)
41 {
42 	size_t decode_len = 0xFFFFFFFF;
43 	const uint8_t payload_numbers1[] = {
44 		LIST(A),
45 			0x01, // 1
46 			0x21, // -2
47 			0x05, // 5
48 			0x19, 0x01, 0x00, // 256
49 			0x1A, 0x01, 0x02, 0x03, 0x04, // 0x01020304
50 			0x39, 0x13, 0x87, // -5000
51 			0x1A, 0xEE, 0x6B, 0x28, 0x00, // 4000000000
52 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -2^31
53 			0x00, // 0
54 			0xD9, 0xFF, 0xFF, 0x01, // 1 tagged (0xFFFF)
55 		END
56 	};
57 	int ret;
58 
59 	struct Numbers numbers;
60 	ret = cbor_decode_Numbers(payload_numbers1, sizeof(payload_numbers1) - 1, &numbers, &decode_len);
61 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, ret, "%d\r\n", ret); // Payload too small.
62 	zassert_equal(0xFFFFFFFF, decode_len, NULL); // Should be untouched
63 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Numbers(payload_numbers1,
64 		sizeof(payload_numbers1) + 1, &numbers, &decode_len), NULL); // Payload too large
65 	zassert_equal(sizeof(payload_numbers1), decode_len, NULL);
66 
67 	zassert_equal(5, numbers.fourtoten, NULL);
68 	zassert_equal(256, numbers.twobytes, NULL);
69 	zassert_equal(0x01020304, numbers.onetofourbytes, NULL);
70 	zassert_equal(-5000, numbers.minusfivektoplustwohundred, "%d", numbers.minusfivektoplustwohundred);
71 	zassert_equal(-2147483648, numbers.negint, NULL);
72 	zassert_equal(0, numbers.posint, NULL);
73 	zassert_equal(1, numbers.tagged_int, NULL);
74 }
75 
76 
ZTEST(cbor_decode_test5,test_numbers2)77 ZTEST(cbor_decode_test5, test_numbers2)
78 {
79 	size_t decode_len = 0xFFFFFFFF;
80 	const uint8_t payload_numbers2[] = {
81 		LIST(8),
82 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
83 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x80000000
84 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
85 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
86 			0x00, // 0
87 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
88 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
89 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
90 		END
91 	};
92 	const uint8_t payload_numbers2_1[] = {
93 		LIST(8),
94 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
95 			0x1A, 0x7F, 0xFF, 0xFF, 0xFF, // 0x7FFFFFFF
96 			0x3B, 0x01, 2, 3, 4, 5, 6, 7, 8, // -0x0102030405060709
97 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
98 			0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xFFFFFFFFFFFFFFFF
99 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
100 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
101 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
102 		END
103 	};
104 	const uint8_t payload_numbers2_inv2[] = {
105 		LIST(8),
106 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
107 			0x1A, 0x7F, 0xFF, 0xFF, 0xFF, // 0x7FFFFFFF
108 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
109 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
110 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
111 			0x3A, 0x80, 0x00, 0x00, 0x01, // -0x8000_0002 INV
112 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
113 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
114 		END
115 	};
116 	const uint8_t payload_numbers2_inv3[] = {
117 		LIST(8),
118 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
119 			0x1A, 0x7F, 0xFF, 0xFF, 0xFF, // 0x7FFFFFFF
120 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
121 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
122 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
123 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000 INV
124 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
125 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
126 		END
127 	};
128 	const uint8_t payload_numbers2_inv4[] = {
129 		LIST(8),
130 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
131 			0x1A, 0x7F, 0xFF, 0xFF, 0xFF, // 0x7FFFFFFF
132 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
133 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456 INV
134 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
135 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
136 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
137 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
138 		END
139 	};
140 	const uint8_t payload_numbers2_inv5[] = {
141 		LIST(8),
142 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
143 			0x1A, 0x7F, 0xFF, 0xFF, 0xFF, // 0x7FFFFFFF
144 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
145 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
146 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
147 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
148 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001 INV
149 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
150 		END
151 	};
152 	const uint8_t payload_numbers2_inv6[] = {
153 		LIST(8),
154 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
155 			0x1A, 0x7F, 0xFF, 0xFF, 0xFF, // 0x7FFFFFFF
156 			0x3B, 0x01, 2, 3, 4, 5, 6, 7, 8, // -0x0102030405060709
157 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
158 			0x20, // -1 INV
159 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
160 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
161 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
162 		END
163 	};
164 	const uint8_t payload_numbers2_inv7[] = {
165 		LIST(8),
166 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
167 			0x1A, 0x7F, 0xFF, 0xFF, 0xFF, // 0x7FFFFFFF
168 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
169 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
170 			0x00, // 0
171 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
172 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
173 			0xD9, 0x04, 0xD3, 0x03, // #6.1235(3) INV
174 		END
175 	};
176 	const uint8_t payload_numbers2_inv8[] = {
177 		LIST(8),
178 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
179 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x80000001 INV
180 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
181 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
182 			0x00, // 0
183 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
184 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
185 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
186 		END
187 	};
188 	const uint8_t payload_numbers2_inv9[] = {
189 		LIST(8),
190 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
191 			0x1A, 0x80, 0x00, 0x00, 0x00, // 0x80000000 INV
192 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
193 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
194 			0x00, // 0
195 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
196 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
197 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
198 		END
199 	};
200 	struct Numbers2 numbers2;
201 	int ret;
202 
203 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Numbers2(payload_numbers2,
204 		sizeof(payload_numbers2), &numbers2, &decode_len), NULL);
205 
206 	zassert_equal(0x123456, numbers2.threebytes, NULL);
207 	zassert_equal(-0x80000000L, numbers2.int32, NULL);
208 	zassert_equal(0x0102030405060708, numbers2.big_int, NULL);
209 	zassert_equal(0x1102030405060709, numbers2.big_uint, NULL);
210 	zassert_equal(0, numbers2.big_uint2, NULL);
211 	zassert_equal(3, numbers2.tagged_int, NULL);
212 
213 	ret = cbor_decode_Numbers2(payload_numbers2_1,
214 		sizeof(payload_numbers2_1), &numbers2, &decode_len);
215 	zassert_equal(ZCBOR_SUCCESS, ret, "%d\r\n", ret);
216 
217 	zassert_equal(0x123456, numbers2.threebytes, NULL);
218 	zassert_equal(0x7FFFFFFFL, numbers2.int32, NULL);
219 	zassert_equal(-0x0102030405060709, numbers2.big_int, NULL);
220 	zassert_equal(0x1102030405060709, numbers2.big_uint, NULL);
221 	zassert_equal(0xFFFFFFFFFFFFFFFF, numbers2.big_uint2, NULL);
222 	zassert_equal(3, numbers2.tagged_int, NULL);
223 
224 	ret = cbor_decode_Numbers2(payload_numbers2_inv2,
225 		sizeof(payload_numbers2_inv2), &numbers2, &decode_len);
226 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, ret, "%d\r\n", ret);
227 
228 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Numbers2(payload_numbers2_inv3,
229 		sizeof(payload_numbers2_inv3), &numbers2, &decode_len), NULL);
230 
231 	ret = cbor_decode_Numbers2(payload_numbers2_inv4,
232 		sizeof(payload_numbers2_inv4), &numbers2, &decode_len);
233 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, ret, "%d\r\n", ret);
234 
235 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Numbers2(payload_numbers2_inv5,
236 		sizeof(payload_numbers2_inv5), &numbers2, &decode_len), NULL);
237 
238 	zassert_equal(ZCBOR_ERR_WRONG_TYPE, cbor_decode_Numbers2(payload_numbers2_inv6,
239 		sizeof(payload_numbers2_inv6), &numbers2, &decode_len), NULL);
240 
241 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Numbers2(payload_numbers2_inv7,
242 		sizeof(payload_numbers2_inv7), &numbers2, &decode_len), NULL);
243 
244 	ret = cbor_decode_Numbers2(payload_numbers2_inv8,
245 		sizeof(payload_numbers2_inv8), &numbers2, &decode_len);
246 	zassert_equal(sizeof(numbers2.int32) == 4 ? ZCBOR_ERR_INT_SIZE : ZCBOR_ERR_WRONG_RANGE,
247 		ret, "%d\r\n", ret);
248 
249 	zassert_equal(sizeof(numbers2.int32) == 4 ? ZCBOR_ERR_INT_SIZE : ZCBOR_ERR_WRONG_RANGE,
250 		cbor_decode_Numbers2(payload_numbers2_inv9,
251 		sizeof(payload_numbers2_inv9), &numbers2, &decode_len), NULL);
252 }
253 
254 
255 /** Test that when unions contain tagged elements (with #6.x), without
256  *  indirection, the tags are enforced correctly.
257 */
ZTEST(cbor_decode_test5,test_tagged_union)258 ZTEST(cbor_decode_test5, test_tagged_union)
259 {
260 	size_t decode_len;
261 	const uint8_t payload_tagged_union1[] = {0xD9, 0x10, 0xE1, 0xF5};
262 	const uint8_t payload_tagged_union2[] = {0xD9, 0x09, 0x29, 0x10};
263 	const uint8_t payload_tagged_union3_inv[] = {0xD9, 0x10, 0xE1, 0x10};
264 
265 	struct TaggedUnion_r result;
266 
267 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_TaggedUnion(payload_tagged_union1,
268 		sizeof(payload_tagged_union1), &result, &decode_len), "%d\r\n");
269 
270 	zassert_equal(sizeof(payload_tagged_union1), decode_len, NULL);
271 	zassert_equal(TaggedUnion_bool_c, result.TaggedUnion_choice, NULL);
272 	zassert_true(result.Bool, NULL);
273 
274 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_TaggedUnion(payload_tagged_union2,
275 		sizeof(payload_tagged_union2), &result, &decode_len), NULL);
276 
277 	zassert_equal(TaggedUnion_uint_c, result.TaggedUnion_choice, NULL);
278 	zassert_equal(0x10, result.uint, NULL);
279 
280 	zassert_equal(ZCBOR_ERR_WRONG_TYPE, cbor_decode_TaggedUnion(payload_tagged_union3_inv,
281 		sizeof(payload_tagged_union3_inv), &result, &decode_len), NULL);
282 }
283 
ZTEST(cbor_decode_test5,test_number_map)284 ZTEST(cbor_decode_test5, test_number_map)
285 {
286 	size_t decode_len = 0xFFFFFFFF;
287 	const uint8_t payload_number_map1[] = {
288 		MAP(3),
289 			0x64, 'b', 'y', 't', 'e',
290 			0x18, 42,
291 			0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't',
292 			0x19, 0x12, 0x34,
293 			0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r',
294 			0x45, 0x1A, 0x12, 0x34, 0x56, 0x78,
295 		END
296 	};
297 	const uint8_t payload_number_map2[] = {
298 		MAP(1),
299 			0x64, 'b', 'y', 't', 'e',
300 			0x04,
301 		END
302 	};
303 	const uint8_t payload_number_map3[] = {
304 		MAP(2),
305 			0x64, 'b', 'y', 't', 'e',
306 			0x18, 42,
307 			0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't',
308 			0x12,
309 		END
310 	};
311 	const uint8_t payload_number_map4_inv[] = {
312 		MAP(2),
313 			0x64, 'b', 'y', 't', 'e',
314 			0x19, 42, 42,
315 		END
316 	};
317 	const uint8_t payload_number_map5_inv[] = {
318 		MAP(2),
319 			0x64, 'b', 'y', 't', 'e',
320 			0x18, 42,
321 			0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't',
322 			0x1A, 0x12, 0x34, 0x56, 0x78,
323 		END
324 	};
325 	const uint8_t payload_number_map6_inv[] = {
326 		MAP(2),
327 			0x64, 'b', 'y', 't', 'e',
328 			0x18, 42,
329 			0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r',
330 			0x43, 0x19, 0x12, 0x34,
331 		END
332 	};
333 	const uint8_t payload_number_map7_inv[] = {
334 		MAP(1),
335 			0x64, 'B', 'y', 't', 'e',
336 			0x04,
337 		END
338 	};
339 
340 	struct NumberMap number_map;
341 
342 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NumberMap(payload_number_map1,
343 		sizeof(payload_number_map1), &number_map, &decode_len), NULL);
344 	zassert_equal(42, number_map.byte, NULL);
345 	zassert_true(number_map.opt_short_present, NULL);
346 	zassert_equal(0x1234, number_map.opt_short.opt_short, NULL);
347 	zassert_true(number_map.opt_cbor_present, NULL);
348 	zassert_equal(0x12345678, number_map.opt_cbor.opt_cbor_cbor, NULL);
349 
350 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NumberMap(payload_number_map2,
351 		sizeof(payload_number_map2), &number_map, &decode_len), NULL);
352 	zassert_equal(4, number_map.byte, NULL);
353 	zassert_false(number_map.opt_short_present, NULL);
354 	zassert_false(number_map.opt_cbor_present, NULL);
355 
356 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NumberMap(payload_number_map3,
357 		sizeof(payload_number_map3), &number_map, &decode_len), NULL);
358 	zassert_equal(42, number_map.byte, NULL);
359 	zassert_true(number_map.opt_short_present, NULL);
360 	zassert_equal(0x12, number_map.opt_short.opt_short, NULL);
361 	zassert_false(number_map.opt_cbor_present, NULL);
362 
363 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_NumberMap(payload_number_map4_inv,
364 		sizeof(payload_number_map4_inv), &number_map, &decode_len), NULL);
365 
366 	int res = cbor_decode_NumberMap(payload_number_map5_inv,
367 		sizeof(payload_number_map5_inv), &number_map, &decode_len);
368 	zassert_equal(ARR_ERR1, res, "%d\r\n", res);
369 
370 	zassert_equal(ARR_ERR1, cbor_decode_NumberMap(payload_number_map6_inv,
371 		sizeof(payload_number_map6_inv), &number_map, &decode_len), NULL);
372 
373 	res = cbor_decode_NumberMap(payload_number_map7_inv,
374 		sizeof(payload_number_map7_inv), &number_map, &decode_len);
375 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, res, "%d\r\n", res);
376 }
377 
ZTEST(cbor_decode_test5,test_strings)378 ZTEST(cbor_decode_test5, test_strings)
379 {
380 	const uint8_t payload_strings1[] = {
381 		LIST(6),
382 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
383 		0x59, 0x01, 0x2c, // 300 bytes
384 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
385 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
386 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
387 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
388 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
389 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
390 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
391 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
392 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
393 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
394 		0xC0, 0x78, 0x1E, // 30 bytes
395 		0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
396 		0x58, STR_LEN(29, 1), // Numbers (len: 29)
397 			LIST(A),
398 				0x01, // 1
399 				0x21, // -2
400 				0x05, // 5
401 				0x19, 0xFF, 0xFF, // 0xFFFF
402 				0x18, 0x18, // 24
403 				0x00, // 0
404 				0x1A, 0xEE, 0x6B, 0x28, 0x00, // 4000000000
405 				0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -2^31
406 				0x1A, 0xFF, 0xFF, 0xFF, 0xFF, // 0xFFFFFFFF
407 				0xD9, 0xFF, 0xFF, 0x09, // 9, tagged (0xFFFF)
408 			END
409 		STR_LEN(0x52, 3), // Simples (len: 18)
410 			LIST(5),
411 				0xF5, // True
412 				0xF4, // False
413 				0xF4, // False
414 				0xF6, // Nil
415 				0xF7, // Undefined
416 			END
417 			LIST(5),
418 				0xF5, // True
419 				0xF4, // False
420 				0xF5, // True
421 				0xF6, // Nil
422 				0xF7, // Undefined
423 			END
424 			LIST(5),
425 				0xF5, // True
426 				0xF4, // False
427 				0xF4, // False
428 				0xF6, // Nil
429 				0xF7, // Undefined
430 			END
431 		0x59, 0x01, STR_LEN(0x68, 3), // Strings (len: 360)
432 			LIST(5),
433 			0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
434 			0x59, 0x01, 0x2c, // 300 bytes
435 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
436 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
437 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
438 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
439 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
440 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
441 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
442 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
443 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
444 			0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
445 			0xC0, 0x6A, // 10 bytes
446 			0,1,2,3,4,5,6,7,8,9,
447 			0x58, STR_LEN(29, 1), // Numbers (len: 29)
448 				LIST(A),
449 					0x01, // 1
450 					0x21, // -2
451 					0x05, // 5
452 					0x19, 0xFF, 0xFF, // 0xFFFF
453 					0x18, 0x18, // 24
454 					0x00, // 0
455 					0x1A, 0xEE, 0x6B, 0x28, 0x00, // 4000000000
456 					0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -2^31
457 					0x1A, 0xFF, 0xFF, 0xFF, 0xFF, // 0xFFFFFFFF
458 					0xD9, 0xFF, 0xFF, 0x29, // -10, tagged (0xFFFF)
459 				END
460 			STR_LEN(0x46, 1), // Simples (len: 6)
461 				LIST(5),
462 					0xF5, // True
463 					0xF4, // False
464 					0xF4, // False
465 					0xF6, // Nil
466 					0xF7, // Undefined
467 				END
468 			END
469 		END
470 	};
471 
472 	struct Strings strings1;
473 	struct Strings strings2;
474 	struct Numbers numbers1;
475 	struct Numbers numbers2;
476 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Strings(payload_strings1,
477 		sizeof(payload_strings1), &strings1, NULL), NULL);
478 	zassert_true(strings1.optCborStrings_present, NULL);
479 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Strings(strings1.optCborStrings.value,
480 		strings1.optCborStrings.len, &strings2, NULL), NULL);
481 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Numbers(strings1.cborNumbers.value,
482 		strings1.cborNumbers.len, &numbers1, NULL), NULL);
483 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Numbers(strings2.cborNumbers.value,
484 		strings2.cborNumbers.len, &numbers2, NULL), NULL);
485 
486 	zassert_equal(300, strings1.threehundrebytebstr.len, NULL);
487 	zassert_equal(0, strings1.threehundrebytebstr.value[0], NULL);
488 	zassert_equal(9, strings1.threehundrebytebstr.value[299], NULL);
489 	zassert_equal(30, strings1.tentothirtybytetstr.len, NULL);
490 	zassert_equal(STR_LEN(29, 1), strings1.cborNumbers.len, NULL);
491 	zassert_equal(3, strings1.cborseqSimples_cbor_count, NULL);
492 	zassert_false(strings1.cborseqSimples_cbor[0].boolval, NULL);
493 	zassert_true(strings1.cborseqSimples_cbor[1].boolval, NULL);
494 	zassert_false(strings1.cborseqSimples_cbor[2].boolval, NULL);
495 
496 	zassert_equal(300, strings2.threehundrebytebstr.len, NULL);
497 	zassert_equal(10, strings2.tentothirtybytetstr.len, NULL);
498 	zassert_equal(5, numbers2.fourtoten, NULL);
499 	zassert_equal(0xFFFF, numbers2.twobytes, NULL);
500 	zassert_equal(24, numbers2.onetofourbytes, NULL);
501 	zassert_equal(0, numbers2.minusfivektoplustwohundred, NULL);
502 	zassert_equal(-2147483648, numbers2.negint, NULL);
503 	zassert_equal(0xFFFFFFFF, numbers2.posint, NULL);
504 	zassert_equal(-10, numbers2.tagged_int, NULL);
505 	zassert_equal(1, strings2.cborseqSimples_cbor_count, NULL);
506 	zassert_false(strings2.cborseqSimples_cbor[0].boolval, NULL);
507 }
508 
ZTEST(cbor_decode_test5,test_simples)509 ZTEST(cbor_decode_test5, test_simples)
510 {
511 	uint8_t payload_simple1[] = {LIST(5), 0xF5, 0xF4, 0xF4, 0xF6, 0xF7, END};
512 	uint8_t payload_simple2[] = {LIST(5), 0xF5, 0xF4, 0xF5, 0xF6, 0xF7, END};
513 	uint8_t payload_simple_inv3[] = {LIST(5), 0xF7, 0xF4, 0xF4, 0xF6, 0xF7, END};
514 	uint8_t payload_simple_inv4[] = {LIST(5), 0xF5, 0xF7, 0xF4, 0xF6, 0xF7, END};
515 	uint8_t payload_simple_inv5[] = {LIST(5), 0xF5, 0xF4, 0xF7, 0xF6, 0xF7, END};
516 	uint8_t payload_simple_inv6[] = {LIST(5), 0xF5, 0xF4, 0xF5, 0xF7, 0xF7, END};
517 	uint8_t payload_simple_inv7[] = {LIST(5), 0xF5, 0xF4, 0xF5, 0xF6, 0xF6, END};
518 	uint8_t payload_simple_inv8[] = {LIST(5), 0xF5, 0xF4, 0xF5, 0xF6, 0xF5, END};
519 	uint8_t payload_simple_inv9[] = {LIST(5), 0xF5, 0xF4, 0xF6, 0xF6, 0xF7, END};
520 	uint8_t payload_simple_inv10[] = {LIST(5), 0xF5, 0xF5, 0xF5, 0xF6, 0xF7, END};
521 	uint8_t payload_simple_inv11[] = {LIST(5), 0xF4, 0xF4, 0xF5, 0xF6, 0xF7, END};
522 
523 	struct Simples result;
524 	size_t len_decode;
525 
526 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Simple2(payload_simple1, sizeof(payload_simple1), &result, &len_decode), NULL);
527 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Simple2(payload_simple2, sizeof(payload_simple2), &result, &len_decode), NULL);
528 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Simple2(payload_simple_inv3, sizeof(payload_simple_inv3), &result, &len_decode), NULL);
529 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Simple2(payload_simple_inv4, sizeof(payload_simple_inv4), &result, &len_decode), NULL);
530 	zassert_equal(ZCBOR_ERR_WRONG_TYPE, cbor_decode_Simple2(payload_simple_inv5, sizeof(payload_simple_inv5), &result, &len_decode), NULL);
531 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Simple2(payload_simple_inv6, sizeof(payload_simple_inv6), &result, &len_decode), NULL);
532 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Simple2(payload_simple_inv7, sizeof(payload_simple_inv7), &result, &len_decode), NULL);
533 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Simple2(payload_simple_inv8, sizeof(payload_simple_inv8), &result, &len_decode), NULL);
534 	zassert_equal(ZCBOR_ERR_WRONG_TYPE, cbor_decode_Simple2(payload_simple_inv9, sizeof(payload_simple_inv9), &result, &len_decode), NULL);
535 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Simple2(payload_simple_inv10, sizeof(payload_simple_inv10), &result, &len_decode), NULL);
536 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Simple2(payload_simple_inv11, sizeof(payload_simple_inv11), &result, &len_decode), NULL);
537 }
538 
ZTEST(cbor_decode_test5,test_string_overflow)539 ZTEST(cbor_decode_test5, test_string_overflow)
540 {
541 	const uint8_t payload_overflow[] = {
542 		0x5A, 0xFF, 0xFF, 0xF0, 0x00, /* overflows to before this string. */
543 	};
544 
545 	struct zcbor_string result_overflow;
546 	size_t out_len;
547 
548 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, cbor_decode_SingleBstr(payload_overflow, sizeof(payload_overflow), &result_overflow, &out_len), NULL);
549 }
550 
ZTEST(cbor_decode_test5,test_optional)551 ZTEST(cbor_decode_test5, test_optional)
552 {
553 	const uint8_t payload_optional1[] = {
554 		LIST(3), 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x03,
555 		END
556 	};
557 	const uint8_t payload_optional2[] = {
558 		LIST(2), 0xCA /* tag */, 0xF4 /* False */, 0x03,
559 		END
560 	};
561 	const uint8_t payload_optional3_inv[] = {
562 		LIST(2), 0xCA /* tag */, 0xF4 /* False */, 0x02,
563 		END
564 	};
565 	const uint8_t payload_optional4[] = {
566 		LIST(3), 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x01,
567 		END
568 	};
569 	const uint8_t payload_optional5[] = {
570 		LIST(3), 0xCA /* tag */, 0xF5 /* True */, 0x02, 0x02,
571 		END
572 	};
573 	const uint8_t payload_optional6[] = {
574 		LIST(4), 0xCA /* tag */, 0xF5 /* True */, 0xF4 /* False */, 0x02, 0x02,
575 		END
576 	};
577 	const uint8_t payload_optional7_inv[] = {
578 		LIST(2), 0xCB /* wrong tag */, 0xF4 /* False */, 0x03,
579 		END
580 	};
581 	const uint8_t payload_optional8_inv[] = {
582 		LIST(2), 0xF4 /* False (no tag first) */, 0x03,
583 		END
584 	};
585 	const uint8_t payload_optional9[] = {
586 		LIST(4), 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x03, 0x08,
587 		END
588 	};
589 	const uint8_t payload_optional10[] = {
590 		LIST(6), 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x03, 0x08, 0x08, 0x08,
591 		END
592 	};
593 	const uint8_t payload_optional11_inv[] = {
594 		LIST(6), 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x03, 0x08, 0x08, 0x09,
595 		END
596 	};
597 	const uint8_t payload_optional12[] = {
598 		LIST(3), 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x08,
599 		END
600 	};
601 
602 	struct Optional optional;
603 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Optional(payload_optional1,
604 			sizeof(payload_optional1), &optional, NULL), NULL);
605 	zassert_false(optional.boolval, NULL);
606 	zassert_false(optional.optbool_present, NULL);
607 	zassert_true(optional.opttwo_present, NULL);
608 	zassert_equal(3, optional.manduint, NULL);
609 	zassert_equal(0, optional.multi8_count, NULL);
610 
611 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Optional(payload_optional2,
612 			sizeof(payload_optional2), &optional, NULL), NULL);
613 	zassert_false(optional.boolval, NULL);
614 	zassert_false(optional.optbool_present, NULL);
615 	zassert_false(optional.opttwo_present, NULL);
616 	zassert_equal(3, optional.manduint, NULL);
617 	zassert_equal(0, optional.multi8_count, NULL);
618 
619 	int ret = cbor_decode_Optional(payload_optional3_inv,
620 			sizeof(payload_optional3_inv), &optional, NULL);
621 	zassert_equal(ARR_ERR3, ret, "%d\r\n", ret);
622 
623 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Optional(payload_optional4,
624 			sizeof(payload_optional4), &optional, NULL), NULL);
625 	zassert_false(optional.boolval, NULL);
626 	zassert_false(optional.optbool_present, NULL);
627 	zassert_true(optional.opttwo_present, NULL);
628 	zassert_equal(1, optional.manduint, NULL);
629 	zassert_equal(0, optional.multi8_count, NULL);
630 
631 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Optional(payload_optional5,
632 			sizeof(payload_optional5), &optional, NULL), NULL);
633 	zassert_true(optional.boolval, NULL);
634 	zassert_false(optional.optbool_present, NULL);
635 	zassert_true(optional.opttwo_present, NULL);
636 	zassert_equal(2, optional.manduint, NULL);
637 	zassert_equal(0, optional.multi8_count, NULL);
638 
639 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Optional(payload_optional6,
640 			sizeof(payload_optional6), &optional, NULL), NULL);
641 	zassert_true(optional.boolval, NULL);
642 	zassert_true(optional.optbool_present, NULL);
643 	zassert_false(optional.optbool, NULL);
644 	zassert_true(optional.opttwo_present, NULL);
645 	zassert_equal(2, optional.manduint, NULL);
646 	zassert_equal(0, optional.multi8_count, NULL);
647 
648 	ret = cbor_decode_Optional(payload_optional7_inv,
649 			sizeof(payload_optional7_inv), &optional, NULL);
650 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, ret, "%d\r\n", ret);
651 
652 	zassert_equal(ZCBOR_ERR_WRONG_TYPE, cbor_decode_Optional(payload_optional8_inv,
653 			sizeof(payload_optional8_inv), &optional, NULL), NULL);
654 
655 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Optional(payload_optional9,
656 			sizeof(payload_optional9), &optional, NULL), NULL);
657 	zassert_false(optional.boolval, NULL);
658 	zassert_false(optional.optbool_present, NULL);
659 	zassert_true(optional.opttwo_present, NULL);
660 	zassert_equal(3, optional.manduint, NULL);
661 	zassert_equal(1, optional.multi8_count, NULL);
662 
663 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Optional(payload_optional10,
664 			sizeof(payload_optional10), &optional, NULL), NULL);
665 	zassert_false(optional.boolval, NULL);
666 	zassert_false(optional.optbool_present, NULL);
667 	zassert_true(optional.opttwo_present, NULL);
668 	zassert_equal(3, optional.manduint, NULL);
669 	zassert_equal(3, optional.multi8_count, NULL);
670 
671 	ret = cbor_decode_Optional(payload_optional11_inv,
672 			sizeof(payload_optional11_inv), &optional, NULL);
673 	zassert_equal(ARR_ERR1, ret, "%d\r\n", ret);
674 
675 	ret = cbor_decode_Optional(payload_optional12,
676 			sizeof(payload_optional12), &optional, NULL);
677 	zassert_equal(ZCBOR_SUCCESS, ret, "%d\r\n", ret);
678 	zassert_false(optional.boolval, NULL);
679 	zassert_false(optional.optbool_present, NULL);
680 	zassert_true(optional.opttwo_present, NULL);
681 	zassert_equal(8, optional.manduint, NULL);
682 	zassert_equal(0, optional.multi8_count, NULL);
683 
684 }
685 
ZTEST(cbor_decode_test5,test_union)686 ZTEST(cbor_decode_test5, test_union)
687 {
688 	const uint8_t payload_union1[] = {0x01, 0x21};
689 	const uint8_t payload_union2[] = {0x03, 0x23};
690 	const uint8_t payload_union3[] = {0x03, 0x04};
691 	const uint8_t payload_union4[] = {
692 		0x67, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22 // "hello"
693 	};
694 	const uint8_t payload_union6[] = {
695 		0x03, 0x23, 0x03, 0x23, 0x03, 0x23,
696 		0x03, 0x23, 0x03, 0x23, 0x03, 0x23
697 	};
698 	const uint8_t payload_union7_long[] = {0x03, 0x23, 0x03, 0x04};
699 	const uint8_t payload_union8_long[] = {0x03, 0x23, 0x03};
700 	const uint8_t payload_union9_long[] = {0x03, 0x04, 0x03, 0x04};
701 	const uint8_t payload_union10_inv[] = {
702 		0x03, 0x23, 0x03, 0x23, 0x03, 0x23, 0x03, 0x23,
703 		0x03, 0x23, 0x03, 0x23, 0x03, 0x23}; /* Too many */
704 	size_t decode_len;
705 
706 	struct Union_r union_r;
707 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union1, sizeof(payload_union1),
708 				&union_r, NULL), NULL);
709 	zassert_equal(Union_Group_m_c, union_r.Union_choice, NULL);
710 
711 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union2, sizeof(payload_union2),
712 				&union_r, NULL), NULL);
713 	zassert_equal(Union_MultiGroup_m_c, union_r.Union_choice, NULL);
714 	zassert_equal(1, union_r.MultiGroup_m.MultiGroup_count, "was %d\r\n", union_r.MultiGroup_m.MultiGroup_count);
715 
716 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union3, sizeof(payload_union3),
717 				&union_r, NULL), NULL);
718 	zassert_equal(Union_uint3_l_c, union_r.Union_choice, NULL);
719 
720 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union4, sizeof(payload_union4),
721 				&union_r, NULL), NULL);
722 	zassert_equal(Union_hello_tstr_c, union_r.Union_choice, NULL);
723 
724 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union6, sizeof(payload_union6),
725 				&union_r, &decode_len), NULL);
726 	zassert_equal(Union_MultiGroup_m_c, union_r.Union_choice, NULL);
727 	zassert_equal(6, union_r.MultiGroup_m.MultiGroup_count, NULL);
728 	zassert_equal(12, decode_len, NULL);
729 
730 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union7_long, sizeof(payload_union7_long),
731 				&union_r, &decode_len), NULL);
732 	zassert_equal(2, decode_len, NULL);
733 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union8_long, sizeof(payload_union8_long),
734 				&union_r, &decode_len), NULL);
735 	zassert_equal(2, decode_len, NULL);
736 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union9_long, sizeof(payload_union9_long),
737 				&union_r, &decode_len), NULL);
738 	zassert_equal(2, decode_len, NULL);
739 
740 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Union(payload_union10_inv, sizeof(payload_union10_inv),
741 				&union_r, &decode_len), NULL);
742 	zassert_equal(6, union_r.MultiGroup_m.MultiGroup_count, NULL);
743 	zassert_equal(12, decode_len, NULL);
744 }
745 
ZTEST(cbor_decode_test5,test_levels)746 ZTEST(cbor_decode_test5, test_levels)
747 {
748 	const uint8_t payload_levels1[] = {
749 		LIST(1), // Level1
750 		LIST(2), // Level2
751 		LIST(4), // Level3 no 1
752 		LIST(1), 0x00, END // Level4 no 1
753 		LIST(1), 0x00, END // Level4 no 2
754 		LIST(1), 0x00, END // Level4 no 3
755 		LIST(1), 0x00, END // Level4 no 4
756 		END
757 		LIST(4), // Level3 no 2
758 		LIST(1), 0x00, END // Level4 no 1
759 		LIST(1), 0x00, END // Level4 no 2
760 		LIST(1), 0x00, END // Level4 no 3
761 		LIST(1), 0x00, END // Level4 no 4
762 		END END END
763 	};
764 
765 	struct Level2 level1;
766 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Level1(payload_levels1,
767 		sizeof(payload_levels1), &level1, NULL), NULL);
768 
769 	zassert_equal(2, level1.Level3_m_count, NULL);
770 	zassert_equal(4, level1.Level3_m[0].Level4_m_count, NULL);
771 	zassert_equal(4, level1.Level3_m[1].Level4_m_count, NULL);
772 }
773 
774 
ZTEST(cbor_decode_test5,test_map)775 ZTEST(cbor_decode_test5, test_map)
776 {
777 	const uint8_t payload_map1[] = {
778 		MAP(4), LIST(2), 0x05, 0x06, END 0xF4, // [5,6] => false
779 		0x07, 0x01, // 7 => 1
780 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
781 		0xf6, 0x40, // nil => ""
782 		END
783 	};
784 	const uint8_t payload_map2_inv[] = {
785 		MAP(4), LIST(2), 0x05, 0x06, END 0xF4, // [5,6] => false
786 		0x07, 0x01, // 7 => 1
787 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
788 		END
789 	};
790 	const uint8_t payload_map3[] = {
791 		MAP(5), LIST(2), 0x05, 0x06, END 0xF5, // [5,6] => true
792 		0x07, 0x01, // 7 => 1
793 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
794 		0xf6, 0x40, // nil => ""
795 		0xf6, 0x40, // nil => ""
796 		END
797 	};
798 
799 	/* Wrong list length. Will not fail for indefinite length arrays. */
800 	const uint8_t payload_map4_inv[] = {
801 		MAP(6), LIST(2), 0x05, 0x06, END 0xF4, // [5,6] => false
802 		0x07, 0x01, // 7 => 1
803 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
804 		0xf6, 0x40, // nil => ""
805 		0xf6, 0x40, // nil => ""
806 		END
807 	};
808 	const uint8_t payload_map5[] = {
809 		MAP(4), LIST(2), 0x05, 0x06, END 0xF4, // [5,6] => false
810 		0x27, 0x01, // -8 => 1
811 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
812 		0xf6, 0x40, // nil => ""
813 		END
814 	};
815 
816 	struct Map map;
817 
818 	zassert_equal(union_nintuint_c, -8,
819 		"The union_int optimization seems to not be working.\r\n");
820 
821 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Map(payload_map1, sizeof(payload_map1),
822 			&map, NULL), NULL);
823 	zassert_false(map.listkey, NULL);
824 	zassert_equal(union_uint7uint_c, map.union_choice, NULL);
825 	zassert_equal(1, map.uint7uint, NULL);
826 	zassert_equal(2, map.twotothree_count, NULL);
827 	zassert_equal(5, map.twotothree[0].twotothree.len, NULL);
828 	zassert_mem_equal("hello", map.twotothree[0].twotothree.value,
829 			5, NULL);
830 	zassert_equal(0, map.twotothree[1].twotothree.len, NULL);
831 
832 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_decode_Map(payload_map2_inv, sizeof(payload_map2_inv),
833 			&map, NULL), NULL);
834 
835 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Map(payload_map3, sizeof(payload_map3),
836 			&map, NULL), NULL);
837 	zassert_true(map.listkey, NULL);
838 	zassert_equal(union_uint7uint_c, map.union_choice, NULL);
839 	zassert_equal(1, map.uint7uint, NULL);
840 	zassert_equal(3, map.twotothree_count, NULL);
841 	zassert_equal(5, map.twotothree[0].twotothree.len, NULL);
842 	zassert_mem_equal("hello", map.twotothree[0].twotothree.value,
843 			5, NULL);
844 	zassert_equal(0, map.twotothree[1].twotothree.len, NULL);
845 	zassert_equal(0, map.twotothree[2].twotothree.len, NULL);
846 
847 #ifdef TEST_INDEFINITE_LENGTH_ARRAYS
848 	zassert_equal(ZCBOR_SUCCESS,
849 #else
850 	zassert_equal(ARR_ERR1,
851 #endif
852 		cbor_decode_Map(payload_map4_inv, sizeof(payload_map4_inv),
853 			&map, NULL), NULL);
854 
855 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Map(payload_map5, sizeof(payload_map5),
856 			&map, NULL), NULL);
857 	zassert_false(map.listkey, NULL);
858 	zassert_equal(union_nintuint_c, map.union_choice, NULL);
859 	zassert_equal(1, map.nintuint, NULL);
860 	zassert_equal(2, map.twotothree_count, NULL);
861 }
862 
863 
my_decode_EmptyMap(zcbor_state_t * state,void * unused)864 static bool my_decode_EmptyMap(zcbor_state_t *state, void *unused)
865 {
866 	size_t payload_len_out;
867 	int res = cbor_decode_EmptyMap(state->payload,
868 		state->payload_end - state->payload, NULL, &payload_len_out);
869 
870 	if (!res) {
871 		state->payload += payload_len_out;
872 	}
873 	return !res;
874 }
875 
876 
ZTEST(cbor_decode_test5,test_empty_map)877 ZTEST(cbor_decode_test5, test_empty_map)
878 {
879 	const uint8_t payload1[] = {MAP(0), END};
880 	const uint8_t payload2_inv[] = {MAP(1), END};
881 	const uint8_t payload3_inv[] = {MAP(1), 0, END};
882 	const uint8_t payload4[] = {MAP(0), END MAP(0), END MAP(0), END};
883 	size_t num_decode;
884 
885 	ZCBOR_STATE_D(state, 0, payload4, sizeof(payload4), 3, 0);
886 
887 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_EmptyMap(payload1, sizeof(payload1), NULL, NULL), NULL);
888 #ifdef TEST_INDEFINITE_LENGTH_ARRAYS
889 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_EmptyMap(payload2_inv, sizeof(payload2_inv), NULL, NULL), NULL);
890 #else
891 	zassert_equal(ARR_ERR1, cbor_decode_EmptyMap(payload2_inv, sizeof(payload2_inv), NULL, NULL), NULL);
892 #endif
893 	zassert_equal(ARR_ERR1, cbor_decode_EmptyMap(payload3_inv, sizeof(payload3_inv), NULL, NULL), NULL);
894 	zassert_true(zcbor_multi_decode(3, 3, &num_decode, my_decode_EmptyMap, state, NULL, 0), NULL);
895 	zassert_equal(3, num_decode, NULL);
896 }
897 
898 
ZTEST(cbor_decode_test5,test_nested_list_map)899 ZTEST(cbor_decode_test5, test_nested_list_map)
900 {
901 	const uint8_t payload_nested_lm1[] = {LIST(0), END};
902 	const uint8_t payload_nested_lm2[] = {LIST(1), MAP(0), END END};
903 	const uint8_t payload_nested_lm3[] = {LIST(1), MAP(1), 0x01, 0x04, END END};
904 	const uint8_t payload_nested_lm4_inv[] = {LIST(1), MAP(2), 0x01, 0x04, 0x1, 0x4, END END};
905 	const uint8_t payload_nested_lm5[] = {LIST(2), MAP(0), END MAP(1), 0x01, 0x04, END END};
906 	const uint8_t payload_nested_lm6_inv[] = {LIST(2), MAP(0), END MAP(1), 0x04, END END};
907 	const uint8_t payload_nested_lm7[] = {LIST(3), MAP(0), END MAP(0), END MAP(0), END END};
908 	struct NestedListMap listmap;
909 
910 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedListMap(payload_nested_lm1,
911 			sizeof(payload_nested_lm1), &listmap, NULL), NULL);
912 	zassert_equal(0, listmap.map_count, NULL);
913 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedListMap(payload_nested_lm2,
914 			sizeof(payload_nested_lm2), &listmap, NULL), NULL);
915 	zassert_equal(1, listmap.map_count, NULL);
916 	zassert_false(listmap.map[0].uint4_present, NULL);
917 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedListMap(payload_nested_lm3,
918 			sizeof(payload_nested_lm3), &listmap, NULL), NULL);
919 	zassert_equal(1, listmap.map_count, NULL);
920 	zassert_true(listmap.map[0].uint4_present, NULL);
921 	zassert_equal(1, listmap.map[0].uint4_present, NULL);
922 	zassert_equal(ARR_ERR1, cbor_decode_NestedListMap(payload_nested_lm4_inv,
923 			sizeof(payload_nested_lm4_inv), &listmap, NULL), NULL);
924 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedListMap(payload_nested_lm5,
925 			sizeof(payload_nested_lm5), &listmap, NULL), NULL);
926 	zassert_equal(2, listmap.map_count, NULL);
927 	zassert_false(listmap.map[0].uint4_present, NULL);
928 	zassert_true(listmap.map[1].uint4_present, NULL);
929 	zassert_equal(ARR_ERR1, cbor_decode_NestedListMap(payload_nested_lm6_inv,
930 			sizeof(payload_nested_lm6_inv), &listmap, NULL), NULL);
931 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedListMap(payload_nested_lm7,
932 			sizeof(payload_nested_lm7), &listmap, NULL), NULL);
933 	zassert_equal(3, listmap.map_count, NULL);
934 	zassert_false(listmap.map[0].uint4_present, NULL);
935 	zassert_false(listmap.map[1].uint4_present, NULL);
936 	zassert_false(listmap.map[2].uint4_present, NULL);
937 }
938 
ZTEST(cbor_decode_test5,test_nested_map_list_map)939 ZTEST(cbor_decode_test5, test_nested_map_list_map)
940 {
941 	const uint8_t payload_nested_mlm1[] = {MAP(1), LIST(0), END LIST(0), END END};
942 	const uint8_t payload_nested_mlm2[] = {MAP(1), LIST(0), END LIST(1), MAP(0), END END END};
943 	const uint8_t payload_nested_mlm3[] = {MAP(1), LIST(0), END LIST(2), MAP(0), END MAP(0), END END END};
944 	const uint8_t payload_nested_mlm4_inv[] = {MAP(1), LIST(0), END LIST(1), MAP(1), MAP(0), END END END END};
945 	const uint8_t payload_nested_mlm5[] = {MAP(2), LIST(0), END LIST(0), END LIST(0), END LIST(0), END END};
946 	const uint8_t payload_nested_mlm6[] = {MAP(3), LIST(0), END LIST(0), END LIST(0), END LIST(0), END LIST(0), END LIST(2), MAP(0), END MAP(0), END END END};
947 	struct NestedMapListMap maplistmap;
948 
949 	int ret = cbor_decode_NestedMapListMap(payload_nested_mlm1,
950 			sizeof(payload_nested_mlm1), &maplistmap, NULL);
951 	zassert_equal(ZCBOR_SUCCESS, ret, "%d\r\n", ret);
952 	zassert_equal(1, maplistmap.map_l_count, NULL);
953 	zassert_equal(0, maplistmap.map_l[0].map_count, NULL);
954 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedMapListMap(payload_nested_mlm2,
955 			sizeof(payload_nested_mlm2), &maplistmap, NULL), NULL);
956 	zassert_equal(1, maplistmap.map_l_count, NULL);
957 	zassert_equal(1, maplistmap.map_l[0].map_count, NULL);
958 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedMapListMap(payload_nested_mlm3,
959 			sizeof(payload_nested_mlm3), &maplistmap, NULL), NULL);
960 	zassert_equal(1, maplistmap.map_l_count, NULL);
961 	zassert_equal(2, maplistmap.map_l[0].map_count, NULL);
962 	ret = cbor_decode_NestedMapListMap(payload_nested_mlm4_inv,
963 			sizeof(payload_nested_mlm4_inv), &maplistmap, NULL);
964 	zassert_equal(ZCBOR_ERR_ITERATIONS, ret, "%d\r\n", ret);
965 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedMapListMap(payload_nested_mlm5,
966 			sizeof(payload_nested_mlm5), &maplistmap, NULL), NULL);
967 	zassert_equal(2, maplistmap.map_l_count, NULL);
968 	zassert_equal(0, maplistmap.map_l[0].map_count, NULL);
969 	zassert_equal(0, maplistmap.map_l[1].map_count, NULL);
970 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_NestedMapListMap(payload_nested_mlm6,
971 			sizeof(payload_nested_mlm6), &maplistmap, NULL), NULL);
972 	zassert_equal(3, maplistmap.map_l_count, NULL);
973 	zassert_equal(0, maplistmap.map_l[0].map_count, NULL);
974 	zassert_equal(0, maplistmap.map_l[1].map_count, NULL);
975 	zassert_equal(2, maplistmap.map_l[2].map_count, NULL);
976 }
977 
978 
ZTEST(cbor_decode_test5,test_range)979 ZTEST(cbor_decode_test5, test_range)
980 {
981 	const uint8_t payload_range1[] = {LIST(3),
982 		0x08,
983 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
984 		0x00,
985 		END
986 	};
987 
988 	const uint8_t payload_range2[] = {LIST(6),
989 		0x05,
990 		0x08, 0x08,
991 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
992 		0x00, 0x0A,
993 		END
994 	};
995 
996 	const uint8_t payload_range3_inv[] = {LIST(4),
997 		0x06, // outside range
998 		0x08,
999 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1000 		0x00,
1001 		END
1002 	};
1003 
1004 	const uint8_t payload_range4_inv[] = {LIST(4),
1005 		0x00,
1006 		0x08,
1007 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1008 		0x0B, //outside range
1009 		END
1010 	};
1011 
1012 	const uint8_t payload_range5[] = {LIST(5),
1013 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1014 		0x08,
1015 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1016 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1017 		0x07,
1018 		END
1019 	};
1020 
1021 	const uint8_t payload_range6_inv[] = {LIST(4),
1022 		0x67, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x6f, 0x6f, // "hellooo" -> too long
1023 		0x08,
1024 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1025 		0x07,
1026 		END
1027 	};
1028 
1029 	const uint8_t payload_range7_inv[] = {LIST(5),
1030 		0x22,
1031 		0x62, 0x68, 0x65, // "he" -> too short
1032 		0x08,
1033 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1034 		0x07,
1035 		END
1036 	};
1037 
1038 	const uint8_t payload_range8_inv[] = {LIST(5),
1039 		0x08,
1040 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1041 		0x07, 0x08, 0x18, // Last too large
1042 		END
1043 	};
1044 
1045 	const uint8_t payload_range9[] = {0x84,
1046 		0x28,
1047 		0x08,
1048 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1049 		0x0
1050 	};
1051 
1052 	const uint8_t payload_range10_inv[] = {0x84,
1053 		0x25,
1054 		0x08,
1055 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
1056 		0x0
1057 	};
1058 
1059 	struct Range output;
1060 
1061 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Range(payload_range1, sizeof(payload_range1),
1062 				&output, NULL), NULL);
1063 	zassert_false(output.optMinus5to5_present, NULL);
1064 	zassert_false(output.optStr3to6_present, NULL);
1065 	zassert_false(output.optMinus9toMinus6excl_present, NULL);
1066 	zassert_equal(1, output.multi8_count, NULL);
1067 	zassert_equal(1, output.multiHello_count, NULL);
1068 	zassert_equal(1, output.multi0to10_count, NULL);
1069 	zassert_equal(0, output.multi0to10[0], NULL);
1070 
1071 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Range(payload_range2, sizeof(payload_range2),
1072 				&output, NULL), NULL);
1073 	zassert_true(output.optMinus5to5_present, NULL);
1074 	zassert_equal(5, output.optMinus5to5, "was %d", output.optMinus5to5);
1075 	zassert_false(output.optStr3to6_present, NULL);
1076 	zassert_false(output.optMinus9toMinus6excl_present, NULL);
1077 	zassert_equal(2, output.multi8_count, NULL);
1078 	zassert_equal(1, output.multiHello_count, NULL);
1079 	zassert_equal(2, output.multi0to10_count, NULL);
1080 	zassert_equal(0, output.multi0to10[0], NULL);
1081 	zassert_equal(10, output.multi0to10[1], NULL);
1082 
1083 	int ret = cbor_decode_Range(payload_range3_inv, sizeof(payload_range3_inv),
1084 				&output, NULL);
1085 	zassert_equal(ZCBOR_ERR_ITERATIONS, ret, "%d\r\n", ret);
1086 
1087 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_decode_Range(payload_range4_inv, sizeof(payload_range4_inv),
1088 				&output, NULL), NULL);
1089 
1090 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Range(payload_range5, sizeof(payload_range5),
1091 				&output, NULL), NULL);
1092 	zassert_false(output.optMinus5to5_present, NULL);
1093 	zassert_true(output.optStr3to6_present, NULL);
1094 	zassert_equal(5, output.optStr3to6.len, NULL);
1095 	zassert_mem_equal("hello", output.optStr3to6.value, 5, NULL);
1096 	zassert_false(output.optMinus9toMinus6excl_present, NULL);
1097 	zassert_equal(1, output.multi8_count, NULL);
1098 	zassert_equal(2, output.multiHello_count, NULL);
1099 	zassert_equal(1, output.multi0to10_count, NULL);
1100 	zassert_equal(7, output.multi0to10[0], NULL);
1101 
1102 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_decode_Range(payload_range6_inv, sizeof(payload_range6_inv),
1103 				&output, NULL), NULL);
1104 
1105 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_decode_Range(payload_range7_inv, sizeof(payload_range7_inv),
1106 				&output, NULL), NULL);
1107 
1108 	ret = cbor_decode_Range(payload_range8_inv, sizeof(payload_range8_inv),
1109 				&output, NULL);
1110 	zassert_equal(ARR_ERR1, ret, "%d\r\n", ret);
1111 
1112 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Range(payload_range9, sizeof(payload_range9),
1113 				&output, NULL), NULL);
1114 	zassert_false(output.optMinus5to5_present, NULL);
1115 	zassert_false(output.optStr3to6_present, NULL);
1116 	zassert_true(output.optMinus9toMinus6excl_present, NULL);
1117 	zassert_equal(-9, output.optMinus9toMinus6excl, NULL);
1118 	zassert_equal(1, output.multi8_count, NULL);
1119 	zassert_equal(1, output.multiHello_count, NULL);
1120 	zassert_equal(1, output.multi0to10_count, NULL);
1121 	zassert_equal(0, output.multi0to10[0], NULL);
1122 
1123 	ret = cbor_decode_Range(payload_range10_inv, sizeof(payload_range10_inv),
1124 				&output, NULL);
1125 	zassert_equal(ZCBOR_ERR_ITERATIONS, ret, "%d\r\n", ret);
1126 }
1127 
ZTEST(cbor_decode_test5,test_value_range)1128 ZTEST(cbor_decode_test5, test_value_range)
1129 {
1130 	const uint8_t payload_value_range1[] = {LIST(6),
1131 		11,
1132 		0x19, 0x03, 0xe7, // 999
1133 		0x29, // -10
1134 		1,
1135 		0x18, 42, // 42
1136 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1137 		END
1138 	};
1139 
1140 	const uint8_t payload_value_range2[] = {LIST(6),
1141 		0x18, 100, // 100
1142 		0x39, 0x03, 0xe8, // -1001
1143 		0x18, 100, // 100
1144 		0,
1145 		0x18, 42, // 42
1146 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1147 		END
1148 	};
1149 
1150 	const uint8_t payload_value_range3_inv[] = {LIST(6),
1151 		10,
1152 		0x19, 0x03, 0xe7, // 999
1153 		0x29, // -10
1154 		1,
1155 		0x18, 42, // 42
1156 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1157 		END
1158 	};
1159 
1160 	const uint8_t payload_value_range4_inv[] = {LIST(6),
1161 		11,
1162 		0x19, 0x03, 0xe8, // 1000
1163 		0x29, // -10
1164 		1,
1165 		0x18, 42, // 42
1166 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1167 		END
1168 	};
1169 
1170 	const uint8_t payload_value_range5_inv[] = {LIST(6),
1171 		11,
1172 		0x19, 0x03, 0xe7, // 999
1173 		0x2a, // -11
1174 		1,
1175 		0x18, 42, // 42
1176 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1177 		END
1178 	};
1179 
1180 	const uint8_t payload_value_range6_inv[] = {LIST(6),
1181 		11,
1182 		0x19, 0x03, 0xe7, // 999
1183 		0x29, // -10
1184 		2,
1185 		0x18, 42, // 42
1186 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1187 		END
1188 	};
1189 
1190 	const uint8_t payload_value_range7_inv[] = {LIST(6),
1191 		11,
1192 		0x19, 0x03, 0xe7, // 999
1193 		0x29, // -10
1194 		1,
1195 		0x18, 43, // 42
1196 		0x65, 'w', 'o', 'r', 'l', 'e', // "worle"
1197 		END
1198 	};
1199 
1200 	const uint8_t payload_value_range8_inv[] = {LIST(6),
1201 		11,
1202 		0x19, 0x03, 0xe7, // 999
1203 		0x29, // -10
1204 		1,
1205 		0x18, 42, // 42
1206 		0x66, 'w', 'o', 'r', 'l', 'd', 'd', // "worldd"
1207 		END
1208 	};
1209 
1210 	const uint8_t payload_value_range9_inv[] = {LIST(6),
1211 		11,
1212 		0x19, 0x03, 0xe7, // 999
1213 		0x29, // -10
1214 		1,
1215 		0x18, 42, // 42
1216 		0x64, 'w', 'o', 'r', 'l', // "worl"
1217 		END
1218 	};
1219 
1220 	const uint8_t payload_value_range10_inv[] = {LIST(6),
1221 		11,
1222 		0x39, 0x03, 0xe6, // -999
1223 		0x39, // -10
1224 		1,
1225 		0x18, 42, // 42
1226 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1227 		END
1228 	};
1229 
1230 	const uint8_t payload_value_range11_inv[] = {LIST(6),
1231 		11,
1232 		0x1a, 0x10, 0x00, 0x00, 0x00, // 0x10000000
1233 		0x39, 0x03, 0xe8, // -1001
1234 		1,
1235 		0x18, 42, // 42
1236 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
1237 		END
1238 	};
1239 
1240 	struct ValueRange exp_output_value_range1 = {
1241 		.greater10 = 11,
1242 		.less1000 = 999,
1243 		.greatereqmin10 = -10,
1244 		.lesseq1 = 1,
1245 	};
1246 	struct ValueRange exp_output_value_range2 = {
1247 		.greater10 = 100,
1248 		.less1000 = -1001,
1249 		.greatereqmin10 = 100,
1250 		.lesseq1 = 0,
1251 	};
1252 
1253 	struct ValueRange output;
1254 	size_t out_len;
1255 
1256 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_ValueRange(payload_value_range1, sizeof(payload_value_range1),
1257 					&output, &out_len), NULL);
1258 	zassert_equal(sizeof(payload_value_range1), out_len, NULL);
1259 	zassert_equal(exp_output_value_range1.greater10,
1260 			output.greater10, NULL);
1261 	zassert_equal(exp_output_value_range1.less1000,
1262 			output.less1000, NULL);
1263 	zassert_equal(exp_output_value_range1.greatereqmin10,
1264 			output.greatereqmin10, NULL);
1265 	zassert_equal(exp_output_value_range1.lesseq1,
1266 			output.lesseq1, NULL);
1267 
1268 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_ValueRange(payload_value_range2, sizeof(payload_value_range2),
1269 					&output, &out_len), NULL);
1270 	zassert_equal(sizeof(payload_value_range2), out_len, NULL);
1271 	zassert_equal(exp_output_value_range2.greater10,
1272 			output.greater10, NULL);
1273 	zassert_equal(exp_output_value_range2.less1000,
1274 			output.less1000, NULL);
1275 	zassert_equal(exp_output_value_range2.greatereqmin10,
1276 			output.greatereqmin10, NULL);
1277 	zassert_equal(exp_output_value_range2.lesseq1,
1278 			output.lesseq1, NULL);
1279 
1280 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_ValueRange(payload_value_range3_inv,
1281 				sizeof(payload_value_range3_inv), &output, &out_len), NULL);
1282 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_ValueRange(payload_value_range4_inv,
1283 				sizeof(payload_value_range4_inv), &output, &out_len), NULL);
1284 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_ValueRange(payload_value_range5_inv,
1285 				sizeof(payload_value_range5_inv), &output, &out_len), NULL);
1286 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_ValueRange(payload_value_range6_inv,
1287 				sizeof(payload_value_range6_inv), &output, &out_len), NULL);
1288 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_ValueRange(payload_value_range7_inv,
1289 				sizeof(payload_value_range7_inv), &output, &out_len), NULL);
1290 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_ValueRange(payload_value_range8_inv,
1291 				sizeof(payload_value_range8_inv), &output, &out_len), NULL);
1292 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_ValueRange(payload_value_range9_inv,
1293 				sizeof(payload_value_range9_inv), &output, &out_len), NULL);
1294 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_ValueRange(payload_value_range10_inv,
1295 				sizeof(payload_value_range10_inv), &output, &out_len), NULL);
1296 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_ValueRange(payload_value_range11_inv,
1297 				sizeof(payload_value_range11_inv), &output, &out_len), NULL);
1298 }
1299 
ZTEST(cbor_decode_test5,test_single)1300 ZTEST(cbor_decode_test5, test_single)
1301 {
1302 	uint8_t payload_single0[] = {0x45, 'h', 'e', 'l', 'l', 'o'};
1303 	uint8_t payload_single1[] = {0x18, 52};
1304 	uint8_t payload_single2_inv[] = {0x18, 53};
1305 	uint8_t payload_single3[] = {9};
1306 	uint8_t payload_single4_inv[] = {10};
1307 	struct zcbor_string result_bstr;
1308 	size_t result_int;
1309 	size_t out_len;
1310 
1311 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_SingleBstr(payload_single0, sizeof(payload_single0), &result_bstr, &out_len), NULL);
1312 	zassert_equal(sizeof(payload_single0), out_len, NULL);
1313 	zassert_equal(5, result_bstr.len, NULL);
1314 	zassert_mem_equal(result_bstr.value, "hello", result_bstr.len, NULL);
1315 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, cbor_decode_SingleBstr(payload_single0, 5, &result_bstr, &out_len), NULL);
1316 
1317 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_SingleInt_uint52(payload_single1, sizeof(payload_single1), NULL, &out_len), NULL); // Result pointer not needed.
1318 	zassert_equal(sizeof(payload_single1), out_len, NULL);
1319 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, cbor_decode_SingleInt_uint52(payload_single1, 1, NULL, &out_len), NULL);
1320 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_SingleInt_uint52(payload_single2_inv, sizeof(payload_single2_inv), NULL, &out_len), NULL);
1321 
1322 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_SingleInt2(payload_single3, sizeof(payload_single3), &result_int, &out_len), NULL);
1323 	zassert_equal(sizeof(payload_single3), out_len, NULL);
1324 	zassert_equal(9, result_int, NULL);
1325 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_SingleInt2(payload_single4_inv, sizeof(payload_single4_inv), &result_int, &out_len), NULL);
1326 }
1327 
ZTEST(cbor_decode_test5,test_unabstracted)1328 ZTEST(cbor_decode_test5, test_unabstracted)
1329 {
1330 	uint8_t payload_unabstracted0[] = {LIST(2), 0x01, 0x03, END};
1331 	uint8_t payload_unabstracted1[] = {LIST(2), 0x02, 0x04, END};
1332 	uint8_t payload_unabstracted2_inv[] = {LIST(2), 0x03, 0x03, END};
1333 	uint8_t payload_unabstracted3_inv[] = {LIST(2), 0x01, 0x01, END};
1334 	struct Unabstracted result_unabstracted;
1335 	size_t out_len;
1336 
1337 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Unabstracted(payload_unabstracted0,
1338 					sizeof(payload_unabstracted0),
1339 					&result_unabstracted, &out_len), NULL);
1340 	zassert_equal(result_unabstracted.unabstractedunion1_choice,
1341 			Unabstracted_unabstractedunion1_choice1_c, NULL);
1342 	zassert_equal(result_unabstracted.unabstractedunion2_choice,
1343 			Unabstracted_unabstractedunion2_uint3_c, NULL);
1344 	zassert_equal(sizeof(payload_unabstracted0), out_len, NULL);
1345 
1346 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Unabstracted(payload_unabstracted1,
1347 					sizeof(payload_unabstracted1),
1348 					&result_unabstracted, &out_len), NULL);
1349 	zassert_equal(result_unabstracted.unabstractedunion1_choice,
1350 			Unabstracted_unabstractedunion1_choice2_c, NULL);
1351 	zassert_equal(result_unabstracted.unabstractedunion2_choice,
1352 			Unabstracted_unabstractedunion2_choice4_c, NULL);
1353 	zassert_equal(sizeof(payload_unabstracted1), out_len, NULL);
1354 
1355 	int ret = cbor_decode_Unabstracted(payload_unabstracted2_inv,
1356 					sizeof(payload_unabstracted2_inv),
1357 					&result_unabstracted, &out_len);
1358 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, ret, "%d\r\n", ret);
1359 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Unabstracted(payload_unabstracted3_inv,
1360 					sizeof(payload_unabstracted3_inv),
1361 					&result_unabstracted, &out_len), NULL);
1362 }
1363 
ZTEST(cbor_decode_test5,test_quantity_range)1364 ZTEST(cbor_decode_test5, test_quantity_range)
1365 {
1366 	uint8_t payload_qty_range1[] = {0xF5, 0xF5, 0xF5};
1367 	uint8_t payload_qty_range2_inv[] = {0xF5, 0xF5};
1368 	uint8_t payload_qty_range3[] = {0xF6, 0xF6, 0xF6, 0xF6, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5};
1369 	uint8_t payload_qty_range4_inv[] = {0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF5, 0xF5, 0xF5};
1370 	struct QuantityRange result_qty_range;
1371 	size_t out_len;
1372 
1373 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_QuantityRange(payload_qty_range1,
1374 					sizeof(payload_qty_range1),
1375 					&result_qty_range, &out_len), NULL);
1376 	zassert_equal(0, result_qty_range.upto4nils_count, NULL);
1377 	zassert_equal(3, result_qty_range.from3true_count, NULL);
1378 	zassert_equal(sizeof(payload_qty_range1), out_len, NULL);
1379 
1380 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_decode_QuantityRange(payload_qty_range2_inv,
1381 					sizeof(payload_qty_range2_inv),
1382 					&result_qty_range, &out_len), NULL);
1383 
1384 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_QuantityRange(payload_qty_range3,
1385 					sizeof(payload_qty_range3),
1386 					&result_qty_range, &out_len), NULL);
1387 	zassert_equal(4, result_qty_range.upto4nils_count, NULL);
1388 	zassert_equal(6, result_qty_range.from3true_count, NULL);
1389 	zassert_equal(sizeof(payload_qty_range3), out_len, NULL);
1390 
1391 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_decode_QuantityRange(payload_qty_range4_inv,
1392 					sizeof(payload_qty_range4_inv),
1393 					&result_qty_range, &out_len), NULL);
1394 }
1395 
ZTEST(cbor_decode_test5,test_doublemap)1396 ZTEST(cbor_decode_test5, test_doublemap)
1397 {
1398 	uint8_t payload_doublemap0[] = {MAP(2), 0x01, 0xA1, 0x01, 0x01, 0x02, 0xA1, 0x02, 0x02, END};
1399 	uint8_t payload_doublemap1_inv[] = {MAP(2), 0x01, 0xA1, 0x01, 0x01, 0x02, 0xA1, 0x03, 0x02, END};
1400 	struct DoubleMap result_doublemap;
1401 	size_t out_len;
1402 
1403 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_DoubleMap(payload_doublemap0,
1404 					sizeof(payload_doublemap0),
1405 					&result_doublemap, &out_len), NULL);
1406 	zassert_equal(result_doublemap.uintmap_count, 2, NULL);
1407 	zassert_equal(result_doublemap.uintmap[0].uintmap_key, 1, NULL);
1408 	zassert_true(result_doublemap.uintmap[0].MyKeys_m.uint1int_present, NULL);
1409 	zassert_equal(result_doublemap.uintmap[0].MyKeys_m.uint1int.uint1int, 1, NULL);
1410 	zassert_false(result_doublemap.uintmap[0].MyKeys_m.uint2int_present, NULL);
1411 	zassert_equal(result_doublemap.uintmap[1].uintmap_key, 2, NULL);
1412 	zassert_false(result_doublemap.uintmap[1].MyKeys_m.uint1int_present, NULL);
1413 	zassert_true(result_doublemap.uintmap[1].MyKeys_m.uint2int_present, NULL);
1414 	zassert_equal(result_doublemap.uintmap[1].MyKeys_m.uint2int.uint2int, 2, NULL);
1415 
1416 	int ret = cbor_decode_DoubleMap(payload_doublemap1_inv,
1417 					sizeof(payload_doublemap1_inv),
1418 					&result_doublemap, &out_len);
1419 	zassert_equal(ARR_ERR1, ret, "%d\r\n", ret);
1420 }
1421 
1422 
1423 #ifndef INFINITY
1424 #define INFINITY (__builtin_inff())
1425 #endif
1426 
1427 
ZTEST(cbor_decode_test5,test_floats)1428 ZTEST(cbor_decode_test5, test_floats)
1429 {
1430 
1431 	uint8_t floats_payload1[] = {LIST(7), 0xF9, 0x42, 0x48, /* 3.1415 */
1432 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1433 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1434 			0xFA, 0x40, 0x49, 0xe, 0x56 /* 3.1415 */,
1435 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1436 			0xFA, 0x39, 0x8d, 0x2c, 0xef /* 123/456789 */,
1437 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1438 			END
1439 	};
1440 
1441 	uint8_t floats_payload2[] = {LIST(5), 0xF9, 0xfc, 0x0, /* -infinity */
1442 			0xFA, 0xc7, 0xc0, 0xe6, 0xb7 /* -98765.4321 */,
1443 			0xFB, 0x41, 0x32, 0xd6, 0x87, 0xe3, 0xd7, 0xa, 0x3d /* 1234567.89 */,
1444 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1445 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1446 			END
1447 	};
1448 
1449 	uint8_t floats_payload3[] = {LIST(5), 0xF9, 0x0, 0x1, /* 0.000000059604645 */
1450 			0xFA, 0, 0, 0, 0 /* 0.0 */,
1451 			0xFB, 0, 0, 0, 0, 0, 0, 0, 0 /* 0.0 */,
1452 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1453 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1454 			END
1455 	};
1456 
1457 	uint8_t floats_payload4[] = {LIST(6), 0xF9, 0x42, 0x48, /* 3.1415 */
1458 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1459 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1460 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1461 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1462 			0xFB, 0x3f, 0x31, 0xa5, 0x9d, 0xe0, 0x0, 0x0, 0x0 /* 123/456789 */,
1463 			END
1464 	};
1465 
1466 	uint8_t floats_payload5[] = {LIST(6), 0xF9, 0x42, 0x48, /* 3.1415 */
1467 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1468 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1469 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1470 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1471 			0xF9, 0xc, 0x69 /* 123/456789 UNSUPPORTED SIZE */,
1472 			END
1473 	};
1474 
1475 	uint8_t floats_payload6_inv[] = {LIST(5), 0xF9, 0x42, 0x48, /* 3.1415 */
1476 			0xFB, 0x41, 0x32, 0xd6, 0x87, 0xe0, 0x0, 0x0, 0x0 /* 1234567.89 WRONG SIZE */,
1477 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1478 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1479 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1480 			END
1481 	};
1482 
1483 	uint8_t floats_payload7_inv[] = {LIST(5), 0xF9, 0x42, 0x48, /* 3.1415 */
1484 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1485 			0xFA, 0xc7, 0xc0, 0xe6, 0xb7 /* -98765.4321 WRONG SIZE */,
1486 			0xFA, 0x40, 0x49, 0xe, 0x56 /* 3.1415 */,
1487 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1488 			END
1489 	};
1490 
1491 	uint8_t floats_payload8_inv[] = {LIST(5), 0xF9, 0x42, 0x48, /* 3.1415 */
1492 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1493 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1494 			0xFA, 0x40, 0x49, 0xe, 0x56 /* 3.1415 */,
1495 			0xFA, 0x40, 0x2d, 0xf8, 0x4d /* 2.71828 WRONG SIZE */,
1496 			END
1497 	};
1498 
1499 	uint8_t floats_payload9_inv[] = {LIST(5), 0xF9, 0x42, 0x48, /* 3.1415 */
1500 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1501 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1502 			0xFB, 0x40, 0x9, 0x21, 0xca, 0, 0, 0, 0 /* 3.1415 */,
1503 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1504 			END
1505 	};
1506 
1507 	size_t num_decode;
1508 	struct Floats result;
1509 	int ret;
1510 	float expected;
1511 
1512 	ret = cbor_decode_Floats(floats_payload1, sizeof(floats_payload1), &result, &num_decode);
1513 	zassert_equal(ZCBOR_SUCCESS, ret, "%d\n", ret);
1514 	zassert_equal(sizeof(floats_payload1), num_decode, NULL);
1515 	zassert_equal((float)3.140625, result.float_16, NULL);
1516 	zassert_equal((float)1234567.89, result.float_32, NULL);
1517 	zassert_equal((double)-98765.4321, result.float_64, NULL);
1518 	zassert_equal(2, result.floats_count, NULL);
1519 	zassert_equal((float)(123.0/456789.0), result.floats[0], NULL);
1520 	zassert_equal((double)(-1.0/(1LL << 42)), result.floats[1], NULL);
1521 
1522 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats(
1523 		floats_payload2, sizeof(floats_payload2), &result, &num_decode), NULL);
1524 	zassert_equal(sizeof(floats_payload2), num_decode, NULL);
1525 	zassert_equal((float)-INFINITY, result.float_16, NULL);
1526 	zassert_equal((float)-98765.4321, result.float_32, NULL);
1527 	zassert_equal((double)1234567.89, result.float_64, NULL);
1528 	zassert_equal(0, result.floats_count, NULL);
1529 
1530 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats(
1531 		floats_payload3, sizeof(floats_payload3), &result, &num_decode), NULL);
1532 	zassert_equal(sizeof(floats_payload3), num_decode, NULL);
1533 	zassert_equal((float)0.000000059604645, result.float_16, NULL);
1534 	zassert_equal((float)0.0, result.float_32, NULL);
1535 	zassert_equal((double)0.0, result.float_64, NULL);
1536 	zassert_equal(0, result.floats_count, NULL);
1537 
1538 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats(
1539 		floats_payload4, sizeof(floats_payload4), &result, &num_decode), NULL);
1540 	zassert_equal(sizeof(floats_payload4), num_decode, NULL);
1541 	zassert_equal((float)3.140625, result.float_16, NULL);
1542 	zassert_equal((float)1234567.89, result.float_32, NULL);
1543 	zassert_equal((double)-98765.4321, result.float_64, NULL);
1544 	zassert_equal(1, result.floats_count, NULL);
1545 	zassert_false((double)(123.0/456789.0) == result.floats[0], NULL);
1546 	zassert_equal((float)(123.0/456789.0), result.floats[0], NULL);
1547 
1548 	ret = cbor_decode_Floats(
1549 		floats_payload5, sizeof(floats_payload5), &result, &num_decode);
1550 	zassert_equal(ZCBOR_SUCCESS, ret, "%d\r\n", ret);
1551 	zassert_equal(sizeof(floats_payload5), num_decode, NULL);
1552 	zassert_equal((float)3.140625, result.float_16, NULL);
1553 	zassert_equal((float)1234567.89, result.float_32, NULL);
1554 	zassert_equal((double)-98765.4321, result.float_64, NULL);
1555 	zassert_equal(1, result.floats_count, NULL);
1556 	zassert_false((double)(123.0/456789.0) == result.floats[0], NULL);
1557 	expected = 0.00026917458f; /* 0x398d2000 */
1558 	zassert_equal(expected, result.floats[0], NULL);
1559 
1560 	zassert_equal(ZCBOR_ERR_FLOAT_SIZE, cbor_decode_Floats(
1561 		floats_payload6_inv, sizeof(floats_payload6_inv), &result, &num_decode), NULL);
1562 
1563 	zassert_equal(ZCBOR_ERR_FLOAT_SIZE, cbor_decode_Floats(
1564 		floats_payload7_inv, sizeof(floats_payload7_inv), &result, &num_decode), NULL);
1565 
1566 	ret = cbor_decode_Floats(
1567 		floats_payload8_inv, sizeof(floats_payload8_inv), &result, &num_decode);
1568 	zassert_equal(ZCBOR_ERR_FLOAT_SIZE, ret, "%d\r\n", ret);
1569 
1570 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, cbor_decode_Floats(
1571 		floats_payload9_inv, sizeof(floats_payload9_inv), &result, &num_decode), NULL);
1572 }
1573 
1574 
1575 /* Test using ranges (greater/less than) on floats. */
ZTEST(cbor_decode_test5,test_floats2)1576 ZTEST(cbor_decode_test5, test_floats2)
1577 {
1578 	uint8_t floats2_payload1[] = {LIST(2),
1579 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1580 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1581 			END
1582 	};
1583 	uint8_t floats2_payload2[] = {LIST(2),
1584 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1585 			0xFB, 0xc0, 0xc3, 0x88, 0x0, 0x0, 0x0, 0x0, 0x0 /* -10000 */,
1586 			END
1587 	};
1588 	uint8_t floats2_payload3[] = {LIST(2),
1589 			0xF9, 0xf0, 0xe2 /* -10000 */,
1590 			0xFA, 0x39, 0x8d, 0x2c, 0xef /* 123/456789 */,
1591 			END
1592 	};
1593 	uint8_t floats2_payload4_inv[] = {LIST(2),
1594 			0xFA, 0x3f, 0x80, 0x0, 0x0 /* 1.0 */,
1595 			0xFB, 0xc0, 0xc3, 0x88, 0x0, 0x0, 0x0, 0x0, 0x0 /* -10000 */,
1596 			END
1597 	};
1598 	uint8_t floats2_payload5_inv[] = {LIST(2),
1599 			0xF9, 0x3c, 0x0 /* 1.0 */,
1600 			0xF9, 0xf0, 0xe2 /* -10000 */,
1601 			END
1602 	};
1603 	uint8_t floats2_payload6_inv[] = {LIST(2),
1604 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1605 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1606 			END
1607 	};
1608 
1609 	size_t num_decode;
1610 	struct Floats2 result;
1611 
1612 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats2(
1613 		floats2_payload1, sizeof(floats2_payload1), &result, &num_decode), NULL);
1614 	zassert_equal(sizeof(floats2_payload1), num_decode, NULL);
1615 	zassert_equal((double)-98765.4321, result.float_lt_1, NULL);
1616 	zassert_equal((double)(-1.0/(1LL << 42)), result.float_ge_min_10000, NULL);
1617 
1618 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats2(
1619 		floats2_payload2, sizeof(floats2_payload2), &result, &num_decode), NULL);
1620 	zassert_equal(sizeof(floats2_payload2), num_decode, NULL);
1621 	zassert_equal((double)(-1.0/(1LL << 42)), result.float_lt_1, NULL);
1622 	zassert_equal((double)-10000, result.float_ge_min_10000, NULL);
1623 
1624 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats2(
1625 		floats2_payload3, sizeof(floats2_payload3), &result, &num_decode), NULL);
1626 	zassert_equal(sizeof(floats2_payload3), num_decode, NULL);
1627 	zassert_equal((double)(-10000), result.float_lt_1, NULL);
1628 	zassert_equal((double)(float)(123.0/456789.0), result.float_ge_min_10000, NULL);
1629 
1630 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_Floats2(
1631 		floats2_payload4_inv, sizeof(floats2_payload4_inv), &result, &num_decode), NULL);
1632 
1633 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_Floats2(
1634 		floats2_payload5_inv, sizeof(floats2_payload5_inv), &result, &num_decode), NULL);
1635 
1636 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_Floats2(
1637 		floats2_payload6_inv, sizeof(floats2_payload6_inv), &result, &num_decode), NULL);
1638 }
1639 
1640 
1641 /* Test float16-32 and float32-64 */
ZTEST(cbor_decode_test5,test_floats3)1642 ZTEST(cbor_decode_test5, test_floats3)
1643 {
1644 	uint8_t floats3_payload1[] = {LIST(2),
1645 			0xF9, 0xf0, 0xe2 /* -10000 */,
1646 			0xFA, 0x39, 0x8d, 0x2c, 0xef /* 123/456789 */,
1647 			END
1648 	};
1649 	uint8_t floats3_payload2[] = {LIST(2),
1650 			0xFA, 0x3f, 0x80, 0x0, 0x0 /* 1.0 */,
1651 			0xFB, 0xc0, 0xc3, 0x88, 0x0, 0x0, 0x0, 0x0, 0x0 /* -10000 */,
1652 			END
1653 	};
1654 	uint8_t floats3_payload3_inv[] = {LIST(2),
1655 			0xF9, 0x3c, 0x0 /* 1.0 */,
1656 			0xF9, 0xf0, 0xe2 /* -10000 */,
1657 			END
1658 	};
1659 	uint8_t floats3_payload4_inv[] = {LIST(2),
1660 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1661 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1662 			END
1663 	};
1664 
1665 	size_t num_decode;
1666 	struct Floats3 result;
1667 
1668 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats3(
1669 		floats3_payload1, sizeof(floats3_payload1), &result, &num_decode), NULL);
1670 	zassert_equal(sizeof(floats3_payload1), num_decode, NULL);
1671 	zassert_equal((float)(-10000), result.float_16_32, NULL);
1672 	zassert_equal((double)(float)(123.0/456789.0), result.float_32_64, NULL);
1673 
1674 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Floats3(
1675 		floats3_payload2, sizeof(floats3_payload2), &result, &num_decode), NULL);
1676 	zassert_equal(sizeof(floats3_payload2), num_decode, NULL);
1677 	zassert_equal((float)(1.0), result.float_16_32, NULL);
1678 	zassert_equal((double)(-10000), result.float_32_64, NULL);
1679 
1680 	zassert_equal(ZCBOR_ERR_FLOAT_SIZE, cbor_decode_Floats3(
1681 		floats3_payload3_inv, sizeof(floats3_payload3_inv), &result, &num_decode), NULL);
1682 
1683 	zassert_equal(ZCBOR_ERR_FLOAT_SIZE, cbor_decode_Floats3(
1684 		floats3_payload4_inv, sizeof(floats3_payload4_inv), &result, &num_decode), NULL);
1685 }
1686 
ZTEST(cbor_decode_test5,test_prelude)1687 ZTEST(cbor_decode_test5, test_prelude)
1688 {
1689 	uint8_t prelude_payload1[] = {
1690 		LIST3(25), 0x40 /* empty bstr */, 0x60 /* empty tstr */,
1691 		0xC0, 0x6A, '2', '0', '2', '2', '-', '0', '2', '-', '2', '2' /* tdate */,
1692 		0xC1, 0x1A, 0x62, 0x15, 0x5d, 0x6c /* 1645567340 */,
1693 		0xFA, 0x40, 0x49, 0xe, 0x56 /* 3.1415 */,
1694 		0xC2, 0x4A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* 0x0102030405060708090A */,
1695 		0xC3, 0x4A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9 /* -0x0102030405060708090A */,
1696 		0xC3, 0x4A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9 /* -0x0102030405060708090A */,
1697 		0 /* 0 (integer) */, 0,
1698 		0xC4, LIST(2), 0x01, 0xC2, 0x4C, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, END /* decfrac: [1, 0x010000000000000000000001] */
1699 		0xC5, LIST(2), 0x04, 0xC3, 0x4C, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, END /* bigfloat: [4, -0x020000000000000000000001] */
1700 		0xD5, LIST(2), LIST(1), 0x41, 'a', END LIST(1), 0x41, 'b', END END /* eb64url: [['a']['b']] */
1701 		0xD6, 0xF6, 0xD7, 0xF6,
1702 		0xD8, 24, 0x41, 0x0 /* encoded-cbor: 0x0 */,
1703 		0xD8, 32, 0x71,  'f', 't', 'p', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm',
1704 		0xD8, 33, 0x60, 0xD8, 34, 0x60,
1705 		0xD8, 35, 0x62, '.', '*' /* regexp: ".*" */,
1706 		0xD8, 36, 0x60, 0xD9, 0xd9, 0xf7, 0xF6,
1707 		0xFA, 0x40, 0x49, 0xe, 0x56 /* 3.1415 */,
1708 		0xFA, 0x40, 0x49, 0xe, 0x56 /* 3.1415 */,
1709 		0xF6,
1710 		END
1711 	};
1712 
1713 	struct Prelude result;
1714 	size_t num_decode;
1715 
1716 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Prelude(prelude_payload1, sizeof(prelude_payload1), &result, &num_decode), NULL);
1717 }
1718 
1719 /** Testing that the generated code correctly enforces the restrictions that are
1720  *  specified inside the .cbor statements. I.e. that it checks that the string and
1721  *  float values are correct.
1722 */
ZTEST(cbor_decode_test5,test_cbor_bstr)1723 ZTEST(cbor_decode_test5, test_cbor_bstr)
1724 {
1725 
1726 #ifdef TEST_INDEFINITE_LENGTH_ARRAYS
1727 #define CBOR_BSTR_LEN(len) (len + 1)
1728 #else
1729 #define CBOR_BSTR_LEN(len) len
1730 #endif
1731 	uint8_t cbor_bstr_payload1[] = {
1732 		0x58, CBOR_BSTR_LEN(33),
1733 			LIST(4),
1734 				0x46, 0x65, 'H', 'e', 'l', 'l', 'o',
1735 				0x49, 0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1736 				0x4C, 0xC2, 0x4A, 0x42, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* 0x4202030405060708090A */,
1737 				0x41, 0xF6, /* nil */
1738 			END
1739 	};
1740 
1741 	uint8_t cbor_bstr_payload2_inv[] = {
1742 		0x58, CBOR_BSTR_LEN(33),
1743 			LIST(4),
1744 				0x46, 0x65, 'Y', 'e', 'l', 'l', 'o',
1745 				0x49, 0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1746 				0x4C, 0xC2, 0x4A, 0x42, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* 0x4202030405060708090A */,
1747 				0x41, 0xF6, /* nil */
1748 			END
1749 	};
1750 
1751 	uint8_t cbor_bstr_payload3_inv[] = {
1752 		0x58, CBOR_BSTR_LEN(33),
1753 			LIST(4),
1754 				0x46, 0x65, 'H', 'e', 'l', 'l', 'o',
1755 				0x49, 0xFB, 0x40, 0x9, 0x21, 0xca, 0, 0, 0, 0 /* 3.1415 */,
1756 				0x4C, 0xC2, 0x4A, 0x42, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* 0x4202030405060708090A */,
1757 				0x41, 0xF6, /* nil */
1758 			END
1759 	};
1760 
1761 	uint8_t cbor_bstr_payload4_inv[] = {
1762 		0x58, CBOR_BSTR_LEN(18),
1763 			LIST(3),
1764 				0x46, 0x65, 'H', 'e', 'l', 'l', 'o',
1765 				0x49, 0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1766 			END
1767 	};
1768 
1769 	uint8_t cbor_bstr_payload5_inv[] = {
1770 		0x58, CBOR_BSTR_LEN(18),
1771 			LIST(2),
1772 				0x46, 0x65, 'H', 'e', 'l', 'l', 'o',
1773 				0x49, 0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1774 			END
1775 	};
1776 
1777 	uint8_t cbor_bstr_payload6_inv[] = {
1778 		0x58, CBOR_BSTR_LEN(33),
1779 			LIST(4),
1780 				0x46, 0x65, 'H', 'e', 'l', 'l', 'o',
1781 				0x49, 0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1782 				0x4C, 0xC2, 0x4A, 0x42, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* 0x4202030405060708090A */,
1783 				0x41, 0xF5, /* true (wrong) */
1784 			END
1785 	};
1786 
1787 	struct CBORBstr result;
1788 	size_t num_decode;
1789 
1790 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_CBORBstr(cbor_bstr_payload1, sizeof(cbor_bstr_payload1), &result, &num_decode), NULL);
1791 	zassert_equal(&cbor_bstr_payload1[2], result.CBORBstr.value, NULL);
1792 	zassert_equal(&cbor_bstr_payload1[21], result.big_uint_bstr.value, NULL);
1793 	zassert_equal(&cbor_bstr_payload1[23], result.big_uint_bstr_cbor.value, NULL);
1794 
1795 	int res = cbor_decode_CBORBstr(cbor_bstr_payload2_inv, sizeof(cbor_bstr_payload2_inv), &result, &num_decode);
1796 	zassert_equal(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED, res, "%d\r\n", res);
1797 
1798 	zassert_equal(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED, cbor_decode_CBORBstr(cbor_bstr_payload3_inv, sizeof(cbor_bstr_payload3_inv), &result, &num_decode), NULL);
1799 
1800 	res = cbor_decode_CBORBstr(cbor_bstr_payload4_inv, sizeof(cbor_bstr_payload4_inv), &result, &num_decode);
1801 	zassert_equal(ARR_ERR4, res, "%d\r\n", res);
1802 
1803 	res = cbor_decode_CBORBstr(cbor_bstr_payload5_inv, sizeof(cbor_bstr_payload5_inv), &result, &num_decode);
1804 	zassert_equal(ARR_ERR4, res, "%d\r\n", res);
1805 
1806 	res = cbor_decode_CBORBstr(cbor_bstr_payload6_inv, sizeof(cbor_bstr_payload6_inv), &result, &num_decode);
1807 	zassert_equal(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED, res, "%d\r\n", res);
1808 }
1809 
1810 
ZTEST(cbor_decode_test5,test_map_length)1811 ZTEST(cbor_decode_test5, test_map_length)
1812 {
1813 	uint8_t map_length_payload1[] = {MAP(2),
1814 		0x61, 'r', 0x01,
1815 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6, END
1816 	};
1817 	uint8_t map_length_payload2[] = {MAP(3),
1818 		0x61, 'r', 0x01,
1819 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1820 		0x61, 'e', LIST(0), END END
1821 	};
1822 	uint8_t map_length_payload3[] = {MAP(3),
1823 		0x61, 'r', 0x01,
1824 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1825 		0x61, 'e', LIST(1), 0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, END END
1826 	};
1827 	uint8_t map_length_payload4[] = {MAP(3),
1828 		0x61, 'r', 0x01,
1829 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1830 		0x61, 'e', LIST(2), 0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1,
1831 			0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, END END
1832 	};
1833 	uint8_t map_length_payload5_inv[] = {MAP(2),
1834 		0x61, 'r', 0x01,
1835 		0x61, 'm', 0x45, 1, 2, 3, 4, 5, END
1836 	};
1837 	uint8_t map_length_payload6_inv[] = {MAP(3),
1838 		0x61, 'r', 0x01,
1839 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1840 		0x61, 'e', LIST(2), 0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1,
1841 			0x49, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, END END
1842 	};
1843 	uint8_t map_length_payload7_inv[] = {MAP(3),
1844 		0x61, 'r', 0x01,
1845 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1846 		0x61, 'e', LIST(2), 0x51, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, 0,
1847 			0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, END END
1848 	};
1849 
1850 	struct MapLength result;
1851 	size_t num_decode;
1852 
1853 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_MapLength(map_length_payload1,
1854 		sizeof(map_length_payload1), &result, &num_decode), NULL);
1855 	zassert_equal(sizeof(map_length_payload1), num_decode, NULL);
1856 	zassert_false(result.end_device_array_present, NULL);
1857 
1858 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_MapLength(map_length_payload2,
1859 		sizeof(map_length_payload2), &result, &num_decode), NULL);
1860 	zassert_equal(sizeof(map_length_payload2), num_decode, NULL);
1861 	zassert_true(result.end_device_array_present, NULL);
1862 	zassert_equal(0, result.end_device_array.uuid_m_count, NULL);
1863 
1864 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_MapLength(map_length_payload3,
1865 		sizeof(map_length_payload3), &result, &num_decode), NULL);
1866 	zassert_equal(sizeof(map_length_payload3), num_decode, NULL);
1867 	zassert_true(result.end_device_array_present, NULL);
1868 	zassert_equal(1, result.end_device_array.uuid_m_count, NULL);
1869 
1870 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_MapLength(map_length_payload4,
1871 		sizeof(map_length_payload4), &result, &num_decode), NULL);
1872 	zassert_equal(sizeof(map_length_payload4), num_decode, NULL);
1873 	zassert_true(result.end_device_array_present, NULL);
1874 	zassert_equal(2, result.end_device_array.uuid_m_count, NULL);
1875 	zassert_equal(16, result.end_device_array.uuid_m[0].len, NULL);
1876 	zassert_equal(16, result.end_device_array.uuid_m[1].len, NULL);
1877 	zassert_equal(8, result.end_device_array.uuid_m[1].value[8], NULL);
1878 
1879 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_MapLength(map_length_payload5_inv,
1880 		sizeof(map_length_payload5_inv), &result, &num_decode), NULL);
1881 
1882 	int err = cbor_decode_MapLength(map_length_payload6_inv,
1883 		sizeof(map_length_payload6_inv), &result, &num_decode);
1884 	zassert_equal(ARR_ERR1, err, "%d\r\n", err);
1885 
1886 	err = cbor_decode_MapLength(map_length_payload7_inv,
1887 		sizeof(map_length_payload7_inv), &result, &num_decode);
1888 	zassert_equal(ARR_ERR1, err, "%d\r\n", err);
1889 }
1890 
1891 
1892 /* UnionInt1 will be optimized, while UnionInt2 won't because it contains a separate type
1893    (Structure_One) which prevents it from being optimized. */
ZTEST(cbor_decode_test5,test_union_int)1894 ZTEST(cbor_decode_test5, test_union_int)
1895 {
1896 	uint8_t union_int_payload1[] = {LIST(2),
1897 		0x05, 0x6E, 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'f', 'i', 'v', 'e',
1898 		END
1899 	};
1900 	uint8_t union_int_payload2[] = {LIST(2),
1901 		0x19, 0x03, 0xE8, 0x50, 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'h', 'o', 'u', 's', 'a', 'n', 'd',
1902 		END
1903 	};
1904 	uint8_t union_int_payload3[] = {LIST(3),
1905 		0x3A, 0x00, 0x01, 0x86, 0x9F, 0xF6, 0x01,
1906 		END
1907 	};
1908 	uint8_t union_int_payload4_inv[] = {LIST(3),
1909 		0x3A, 0x00, 0x01, 0x86, 0x9F, 0xF7, 0x01,
1910 		END
1911 	};
1912 	uint8_t union_int_payload5_inv[] = {LIST(2),
1913 		0x24, 0x6E, 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'f', 'i', 'v', 'e',
1914 		END
1915 	};
1916 	uint8_t union_int_payload6[] = {LIST(2),
1917 		0x01, 0x42, 'h', 'i',
1918 		END
1919 	};
1920 	struct UnionInt1 result1;
1921 	struct UnionInt2 result2;
1922 	size_t num_decode;
1923 
1924 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_UnionInt1(union_int_payload1,
1925 		sizeof(union_int_payload1), &result1, &num_decode), NULL);
1926 	zassert_equal(sizeof(union_int_payload1), num_decode, NULL);
1927 	zassert_equal(result1.union_choice, UnionInt1_union_uint1_l_c, NULL);
1928 
1929 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_UnionInt2(union_int_payload1,
1930 		sizeof(union_int_payload1), &result2, &num_decode), NULL);
1931 	zassert_equal(sizeof(union_int_payload1), num_decode, NULL);
1932 	zassert_equal(result2.union_choice, union_uint5_l_c, NULL);
1933 
1934 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_UnionInt1(union_int_payload2,
1935 		sizeof(union_int_payload2), &result1, &num_decode), NULL);
1936 	zassert_equal(sizeof(union_int_payload2), num_decode, NULL);
1937 	zassert_equal(result1.union_choice, UnionInt1_union_uint2_l_c, NULL);
1938 	zassert_equal(result1.bstr.len, 16, NULL);
1939 	zassert_mem_equal(result1.bstr.value, "This is thousand", 16, NULL);
1940 
1941 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_UnionInt2(union_int_payload2,
1942 		sizeof(union_int_payload2), &result2, &num_decode), NULL);
1943 	zassert_equal(sizeof(union_int_payload2), num_decode, NULL);
1944 	zassert_equal(result2.union_choice, union_uint1000_l_c, NULL);
1945 	zassert_equal(result2.bstr.len, 16, NULL);
1946 	zassert_mem_equal(result2.bstr.value, "This is thousand", 16, NULL);
1947 
1948 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_UnionInt1(union_int_payload3,
1949 		sizeof(union_int_payload3), &result1, &num_decode), NULL);
1950 	zassert_equal(sizeof(union_int_payload3), num_decode, NULL);
1951 	zassert_equal(result1.union_choice, UnionInt1_union_uint3_l_c, NULL);
1952 	zassert_equal(result1.number_m.number_choice, number_int_c, NULL);
1953 	zassert_equal(result1.number_m.Int, 1, NULL);
1954 
1955 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_UnionInt2(union_int_payload3,
1956 		sizeof(union_int_payload3), &result2, &num_decode), NULL);
1957 	zassert_equal(sizeof(union_int_payload3), num_decode, NULL);
1958 	zassert_equal(result2.union_choice, union_nint_l_c, NULL);
1959 	zassert_equal(result2.number_m.number_choice, number_int_c, NULL);
1960 	zassert_equal(result2.number_m.Int, 1, NULL);
1961 
1962 	int err = cbor_decode_UnionInt2(union_int_payload6,
1963 		sizeof(union_int_payload6), &result2, &num_decode);
1964 	zassert_equal(ZCBOR_SUCCESS, err, "%d\r\n", err);
1965 	zassert_equal(sizeof(union_int_payload6), num_decode, NULL);
1966 	zassert_equal(result2.union_choice, UnionInt2_union_Structure_One_m_c, NULL);
1967 	zassert_equal(result2.Structure_One_m.some_array.len, 2, NULL);
1968 	zassert_mem_equal(result2.Structure_One_m.some_array.value, "hi", 2, NULL);
1969 
1970 	err = cbor_decode_UnionInt1(union_int_payload4_inv,
1971 		sizeof(union_int_payload4_inv), &result1, &num_decode);
1972 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, err, "%d\r\n", err);
1973 
1974 	err = cbor_decode_UnionInt2(union_int_payload4_inv,
1975 		sizeof(union_int_payload4_inv), &result2, &num_decode);
1976 	zassert_equal(ZCBOR_ERR_WRONG_TYPE, err, "%d\r\n", err);
1977 
1978 	err = cbor_decode_UnionInt1(union_int_payload5_inv,
1979 		sizeof(union_int_payload5_inv), &result1, &num_decode);
1980 	zassert_equal(ZCBOR_ERR_WRONG_VALUE, err, "%d\r\n", err);
1981 
1982 	err = cbor_decode_UnionInt2(union_int_payload5_inv,
1983 		sizeof(union_int_payload5_inv), &result2, &num_decode);
1984 	zassert_equal(ZCBOR_ERR_WRONG_TYPE, err, "%d\r\n", err);
1985 }
1986 
1987 
ZTEST(cbor_decode_test5,test_intmax)1988 ZTEST(cbor_decode_test5, test_intmax)
1989 {
1990 	uint8_t intmax1_payload1[] = {LIST(C),
1991 		0x38, 0x7F, 0x18, 0x7F, 0x18, 0xFF,
1992 		0x39, 0x7F, 0xFF,
1993 		0x19, 0x7F, 0xFF,
1994 		0x19, 0xFF, 0xFF,
1995 		0x3A, 0x7F, 0xFF, 0xFF, 0xFF,
1996 		0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
1997 		0x1A, 0xFF, 0xFF, 0xFF, 0xFF,
1998 		0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1999 		0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2000 		0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2001 		END
2002 	};
2003 	uint8_t intmax2_payload1[] = {LIST(8),
2004 		0x38, 0x7F, 0x0,
2005 		0x39, 0x7F, 0xFF,
2006 		0x0,
2007 		0x3A, 0x7F, 0xFF, 0xFF, 0xFF,
2008 		0x0,
2009 		0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2010 		0x0,
2011 		END
2012 	};
2013 	uint8_t intmax2_payload2[] = {LIST(8),
2014 		0x18, 0x7F, 0x18, 0xFF,
2015 		0x19, 0x7F, 0xFF,
2016 		0x19, 0xFF, 0xFF,
2017 		0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2018 		0x1A, 0xFF, 0xFF, 0xFF, 0xFF,
2019 		0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2020 		0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2021 		END
2022 	};
2023 	uint8_t intmax3_payload1[] = {LIST(3),
2024 		0x18, 254,
2025 		0x19, 0xFF, 0xFE,
2026 		MAP(1),
2027 			0x38, 127,
2028 			0x58, 127,
2029 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2030 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2031 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2032 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2033 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2034 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2035 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2036 			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2037 		END
2038 		END
2039 	};
2040 
2041 
2042 	struct Intmax2 result2;
2043 	struct Intmax3 result3;
2044 	size_t num_decode;
2045 
2046 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Intmax1(intmax1_payload1,
2047 		sizeof(intmax1_payload1), NULL, &num_decode), NULL);
2048 	zassert_equal(sizeof(intmax1_payload1), num_decode, NULL);
2049 
2050 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Intmax2(intmax2_payload1,
2051 		sizeof(intmax2_payload1), &result2, &num_decode), NULL);
2052 	zassert_equal(sizeof(intmax2_payload1), num_decode, NULL);
2053 	zassert_equal(result2.INT_8, INT8_MIN, NULL);
2054 	zassert_equal(result2.UINT_8, 0, NULL);
2055 	zassert_equal(result2.INT_16, INT16_MIN, NULL);
2056 	zassert_equal(result2.UINT_16, 0, NULL);
2057 	zassert_equal(result2.INT_32, INT32_MIN, NULL);
2058 	zassert_equal(result2.UINT_32, 0, NULL);
2059 	zassert_equal(result2.INT_64, INT64_MIN, NULL);
2060 	zassert_equal(result2.UINT_64, 0, NULL);
2061 
2062 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Intmax2(intmax2_payload2,
2063 		sizeof(intmax2_payload2), &result2, &num_decode), NULL);
2064 	zassert_equal(sizeof(intmax2_payload2), num_decode, NULL);
2065 	zassert_equal(result2.INT_8, INT8_MAX, NULL);
2066 	zassert_equal(result2.UINT_8, UINT8_MAX, NULL);
2067 	zassert_equal(result2.INT_16, INT16_MAX, NULL);
2068 	zassert_equal(result2.UINT_16, UINT16_MAX, NULL);
2069 	zassert_equal(result2.INT_32, INT32_MAX, NULL);
2070 	zassert_equal(result2.UINT_32, UINT32_MAX, NULL);
2071 	zassert_equal(result2.INT_64, INT64_MAX, NULL);
2072 	zassert_equal(result2.UINT_64, UINT64_MAX, NULL);
2073 
2074 	int res = cbor_decode_Intmax3(intmax3_payload1,
2075 		sizeof(intmax3_payload1), &result3, &num_decode);
2076 	zassert_equal(ZCBOR_SUCCESS, res, "%s\n", zcbor_error_str(res));
2077 	zassert_equal(sizeof(intmax3_payload1), num_decode, NULL);
2078 	zassert_equal(result3.union_choice, Intmax3_union_uint254_c, NULL);
2079 	zassert_equal(result3.Int, 65534, NULL);
2080 	zassert_equal(result3.nintbstr.len, 127, NULL);
2081 }
2082 
2083 
2084 /* Test that zcbor generates variable names that don't contain unsupported characters. */
ZTEST(cbor_decode_test5,test_invalid_identifiers)2085 ZTEST(cbor_decode_test5, test_invalid_identifiers)
2086 {
2087 	struct InvalidIdentifiers result;
2088 	uint8_t invalid_identifiers_payload1[] = {
2089 		LIST(3),
2090 		0x64, '1', 'o', 'n', 'e',
2091 		0x02, /* Ø */
2092 		0x67, '{', '[', 'a', '-', 'z', ']', '}',
2093 		END
2094 	};
2095 
2096 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_InvalidIdentifiers(invalid_identifiers_payload1,
2097 		sizeof(invalid_identifiers_payload1), &result, NULL), NULL);
2098 	zassert_true(result._1one_tstr_present, NULL);
2099 	zassert_true(result.__present, NULL);
2100 	zassert_true(result.a_z_tstr_present, NULL);
2101 }
2102 
2103 
ZTEST(cbor_decode_test5,test_uint64_list)2104 ZTEST(cbor_decode_test5, test_uint64_list)
2105 {
2106 	uint8_t uint64_list_payload1[] = {LIST(8),
2107 		0x10,
2108 		0x18, 0x20,
2109 		0x19, 0x12, 0x34,
2110 		0x1a, 0x12, 0x34, 0x56, 0x78,
2111 		0x1b, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
2112 		0x3b, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
2113 		0x1b, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2114 		0x3b, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee,
2115 		END
2116 	};
2117 
2118 	struct Uint64List result;
2119 	size_t num_decode;
2120 
2121 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_Uint64List(uint64_list_payload1,
2122 		sizeof(uint64_list_payload1), &result, &num_decode), NULL);
2123 	zassert_equal(sizeof(uint64_list_payload1), num_decode, NULL);
2124 
2125 	zassert_equal(5, result.uint64_count, NULL);
2126 	zassert_equal(0x10, result.uint64[0], NULL);
2127 	zassert_equal(0x20, result.uint64[1], NULL);
2128 	zassert_equal(0x1234, result.uint64[2], NULL);
2129 	zassert_equal(0x12345678, result.uint64[3], NULL);
2130 	zassert_equal(0x123456789abcdef0, result.uint64[4], NULL);
2131 }
2132 
2133 
ZTEST(cbor_decode_test5,test_bstr_size)2134 ZTEST(cbor_decode_test5, test_bstr_size)
2135 {
2136 	uint8_t bstr_size_payload1[] = {MAP(1),
2137 		0x61, 's',
2138 		0x4c, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
2139 		0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
2140 		END
2141 	};
2142 
2143 	struct BstrSize result;
2144 	size_t num_decode;
2145 
2146 	zassert_equal(ZCBOR_SUCCESS, cbor_decode_BstrSize(bstr_size_payload1,
2147 		sizeof(bstr_size_payload1), &result, &num_decode), NULL);
2148 	zassert_equal(sizeof(bstr_size_payload1), num_decode, NULL);
2149 
2150 	zassert_equal(12, result.bstr12_m.s.len, NULL);
2151 }
2152 
2153 
2154 ZTEST_SUITE(cbor_decode_test5, NULL, NULL, NULL, NULL, NULL);
2155