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