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