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 "corner_cases_encode.h"
9 
10 #ifndef ZCBOR_CANONICAL
11 #define TEST_INDEFINITE_LENGTH_ARRAYS
12 #endif
13 #include <common_test.h>
14 
15 
ZTEST(cbor_encode_test3,test_numbers)16 ZTEST(cbor_encode_test3, test_numbers)
17 {
18 	const uint8_t exp_payload_numbers1[] = {
19 		LIST(A), // List start
20 			0x01, // 1
21 			0x21, // -2
22 			0x05, // 5
23 			0x19, 0x01, 0x00, // 256
24 			0x1A, 0x01, 0x02, 0x03, 0x04, // 0x01020304
25 			0x39, 0x13, 0x87, // -5000
26 			0x1A, 0xEE, 0x6B, 0x28, 0x00, // 4000000000
27 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -2^31
28 			0x00, // 0
29 			0xD9, 0xFF, 0xFF, 0x01, // 1
30 		END
31 	};
32 
33 	struct Numbers numbers = {0};
34 	uint8_t output[100];
35 	size_t out_len;
36 
37 	numbers.fourtoten = 3; // Invalid
38 	numbers.twobytes = 256;
39 	numbers.onetofourbytes = 0x01020304;
40 	numbers.minusfivektoplustwohundred = -5000;
41 	numbers.negint = -2147483648;
42 	numbers.posint = 0;
43 	numbers.tagged_int = 1;
44 
45 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Numbers(output,
46 		sizeof(output), &numbers, &out_len), NULL);
47 
48 	numbers.fourtoten = 11; // Invalid
49 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Numbers(output,
50 		sizeof(output), &numbers, &out_len), NULL);
51 
52 	numbers.fourtoten = 5; // Valid
53 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Numbers(output,
54 		sizeof(output), &numbers, &out_len), NULL);
55 	zassert_equal(sizeof(exp_payload_numbers1), out_len, "%d != %d\r\n", sizeof(exp_payload_numbers1), out_len);
56 	zassert_mem_equal(exp_payload_numbers1, output, sizeof(exp_payload_numbers1), NULL);
57 
58 	numbers.negint = 1; // Invalid
59 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Numbers(output,
60 		sizeof(output), &numbers, &out_len), NULL);
61 
62 	numbers.negint = -1; // Valid
63 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Numbers(output,
64 		sizeof(output), &numbers, &out_len), NULL);
65 
66 	numbers.minusfivektoplustwohundred = -5001; // Invalid
67 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Numbers(output,
68 		sizeof(output), &numbers, &out_len), NULL);
69 
70 	numbers.minusfivektoplustwohundred = 201; // Invalid
71 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Numbers(output,
72 		sizeof(output), &numbers, &out_len), NULL);
73 
74 	numbers.minusfivektoplustwohundred = 200; // Valid
75 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Numbers(output,
76 		sizeof(output), &numbers, &out_len), NULL);
77 }
78 
ZTEST(cbor_encode_test3,test_numbers2)79 ZTEST(cbor_encode_test3, test_numbers2)
80 {
81 	const uint8_t exp_payload_numbers2[] = {
82 		LIST(8),
83 			0x1A, 0x00, 0x12, 0x34, 0x56, // 0x123456
84 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
85 			0x1B, 0x01, 2, 3, 4, 5, 6, 7, 8, // 0x0102030405060708
86 			0x1B, 0x11, 2, 3, 4, 5, 6, 7, 9, // 0x1102030405060709
87 			0x00, // 0
88 			0x3A, 0x80, 0x00, 0x00, 0x00, // -0x8000_0001
89 			0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -0x8000_0000
90 			0xD9, 0x04, 0xD2, 0x03, // #6.1234(3)
91 		END
92 	};
93 	struct Numbers2 numbers2 = {
94 		.threebytes = 0x123456,
95 		.int32 =  -0x80000000L,
96 		.big_int = 0x0102030405060708,
97 		.big_uint = 0x1102030405060709,
98 		.big_uint2 = 0,
99 		.tagged_int = 3,
100 	};
101 	uint8_t output[100];
102 	size_t out_len;
103 
104 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Numbers2(output,
105 		sizeof(output), &numbers2, &out_len), NULL);
106 	zassert_equal(sizeof(exp_payload_numbers2), out_len, "%d != %d\r\n",
107 		sizeof(exp_payload_numbers2), out_len);
108 	zassert_mem_equal(exp_payload_numbers2, output, sizeof(exp_payload_numbers2), NULL);
109 }
110 
ZTEST(cbor_encode_test3,test_tagged_union)111 ZTEST(cbor_encode_test3, test_tagged_union)
112 {
113 	size_t encode_len;
114 	const uint8_t exp_payload_tagged_union1[] = {0xD9, 0x10, 0xE1, 0xF5};
115 	const uint8_t exp_payload_tagged_union2[] = {0xD9, 0x09, 0x29, 0x10};
116 
117 	uint8_t output[5];
118 
119 	struct TaggedUnion_r input;
120 	input.TaggedUnion_choice = TaggedUnion_bool_c;
121 	input.Bool = true;
122 
123 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_TaggedUnion(output,
124 		sizeof(output), &input, &encode_len), "%d\r\n");
125 
126 	zassert_equal(sizeof(exp_payload_tagged_union1), encode_len, NULL);
127 	zassert_mem_equal(exp_payload_tagged_union1, output, sizeof(exp_payload_tagged_union1), NULL);
128 
129 	input.TaggedUnion_choice = TaggedUnion_uint_c;
130 	input.uint = 0x10;
131 
132 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_TaggedUnion(output,
133 		sizeof(output), &input, &encode_len), NULL);
134 
135 	zassert_equal(sizeof(exp_payload_tagged_union2), encode_len, NULL);
136 	zassert_mem_equal(exp_payload_tagged_union2, output, sizeof(exp_payload_tagged_union2), NULL);
137 }
138 
ZTEST(cbor_encode_test3,test_number_map)139 ZTEST(cbor_encode_test3, test_number_map)
140 {
141 	size_t encode_len = 0xFFFFFFFF;
142 	const uint8_t exp_payload_number_map1[] = {
143 		MAP(3),
144 			0x64, 'b', 'y', 't', 'e',
145 			0x18, 42,
146 			0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't',
147 			0x19, 0x12, 0x34,
148 			0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r',
149 			0x45, 0x1A, 0x12, 0x34, 0x56, 0x78,
150 		END
151 	};
152 
153 	const uint8_t exp_payload_number_map2[] = {
154 		MAP(2),
155 			0x64, 'b', 'y', 't', 'e',
156 			0x18, 42,
157 			0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r',
158 			0x45, 0x1A, 0x12, 0x34, 0x56, 0x78,
159 		END
160 	};
161 
162 	uint8_t payload[80];
163 
164 	struct NumberMap number_map1 = {
165 		.byte = 42,
166 		.opt_short_present = true,
167 		.opt_short.opt_short = 0x1234,
168 		.opt_cbor_present = true,
169 		.opt_cbor.opt_cbor_cbor = 0x12345678,
170 	};
171 
172 	int res = cbor_encode_NumberMap(payload,
173 		sizeof(payload), &number_map1, &encode_len);
174 	zassert_equal(ZCBOR_SUCCESS, res, "%d\r\n", res);
175 	zassert_equal(encode_len, sizeof(exp_payload_number_map1), NULL);
176 	zassert_mem_equal(payload, exp_payload_number_map1, encode_len, NULL);
177 
178 	struct NumberMap number_map2 = {
179 		.byte = 42,
180 		.opt_short_present = false,
181 		.opt_cbor_present = true,
182 		.opt_cbor.opt_cbor_cbor = 0x12345678,
183 	};
184 
185 	res = cbor_encode_NumberMap(payload,
186 		sizeof(payload), &number_map2, &encode_len);
187 	zassert_equal(ZCBOR_SUCCESS, res, "%d\r\n", res);
188 	zassert_equal(encode_len, sizeof(exp_payload_number_map2), NULL);
189 	zassert_mem_equal(payload, exp_payload_number_map2, encode_len, NULL);
190 
191 	struct NumberMap number_map3 = {
192 		.byte = 42,
193 		.opt_short_present = false,
194 		.opt_cbor_present = true,
195 		.opt_cbor.opt_cbor.value = (uint8_t[]){0x1A, 0x12, 0x34, 0x56, 0x78},
196 		.opt_cbor.opt_cbor.len = 5,
197 	};
198 
199 	res = cbor_encode_NumberMap(payload,
200 		sizeof(payload), &number_map3, &encode_len);
201 	zassert_equal(ZCBOR_SUCCESS, res, "%d\r\n", res);
202 	zassert_equal(encode_len, sizeof(exp_payload_number_map2), NULL);
203 	zassert_mem_equal(payload, exp_payload_number_map2, encode_len, NULL);
204 
205 	struct NumberMap number_map4_inv = {
206 		.byte = 42,
207 		.opt_short_present = false,
208 		.opt_cbor_present = true,
209 		.opt_cbor.opt_cbor.value = (uint8_t[]){0x19, 0x12, 0x34},
210 		.opt_cbor.opt_cbor.len = 3,
211 	};
212 
213 	res = cbor_encode_NumberMap(payload,
214 		sizeof(payload), &number_map4_inv, &encode_len);
215 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, res, "%d\r\n", res);
216 }
217 
218 
ZTEST(cbor_encode_test3,test_strings)219 ZTEST(cbor_encode_test3, test_strings)
220 {
221 	const uint8_t exp_payload_strings1[] = {
222 		LIST(6),
223 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
224 		0x59, 0x01, 0x2c, // 300 bytes
225 		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,
226 		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,
227 		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,
228 		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,
229 		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,
230 		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,
231 		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,
232 		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,
233 		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,
234 		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,
235 		0xC0, 0x78, 0x1E, // 30 bytes
236 		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,
237 #ifndef ZCBOR_CANONICAL
238 		0x58, 30, // Numbers (len: 30)
239 #else
240 		0x58, 29, // Numbers (len: 29)
241 #endif
242 			LIST(A), // List start
243 				0x01, // 1
244 				0x21, // -2
245 				0x05, // 5
246 				0x19, 0xFF, 0xFF, // 0xFFFF
247 				0x18, 0x18, // 24
248 				0x00, // 0
249 				0x1A, 0xEE, 0x6B, 0x28, 0x00, // 4000000000
250 				0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -2^31
251 				0x1A, 0xFF, 0xFF, 0xFF, 0xFF, // 0xFFFFFFFF
252 				0xD9, 0xFF, 0xFF, 9, // 9
253 				END
254 #ifndef ZCBOR_CANONICAL
255 		0x55, // Simples (len: 21)
256 #else
257 		0x52, // Simples (len: 18)
258 #endif
259 			LIST(5), // List start
260 				0xF5, // True
261 				0xF4, // False
262 				0xF4, // False
263 				0xF6, // Nil
264 				0xF7, // Undefined
265 				END
266 			LIST(5), // List start
267 				0xF5, // True
268 				0xF4, // False
269 				0xF5, // True
270 				0xF6, // Nil
271 				0xF7, // Undefined
272 				END
273 			LIST(5), // List start
274 				0xF5, // True
275 				0xF4, // False
276 				0xF4, // False
277 				0xF6, // Nil
278 				0xF7, // Undefined
279 				END
280 #ifndef ZCBOR_CANONICAL
281 		0x59, 0x01, 0x6B, // Strings (len: 363)
282 #else
283 		0x59, 0x01, 0x68, // Strings (len: 360)
284 #endif
285 			LIST(5),
286 			0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
287 			0x59, 0x01, 0x2c, // 300 bytes
288 			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,
289 			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,
290 			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,
291 			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,
292 			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,
293 			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,
294 			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,
295 			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,
296 			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,
297 			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,
298 			0xC0, 0x6A, // 10 bytes
299 			0,1,2,3,4,5,6,7,8,9,
300 #ifndef ZCBOR_CANONICAL
301 			0x58, 30, // Numbers (len: 30)
302 #else
303 			0x58, 29, // Numbers (len: 29)
304 #endif
305 				LIST(A), // List start
306 					0x01, // 1
307 					0x21, // -2
308 					0x05, // 5
309 					0x19, 0xFF, 0xFF, // 0xFFFF
310 					0x18, 0x18, // 24
311 					0x00, // 0
312 					0x1A, 0xEE, 0x6B, 0x28, 0x00, // 4000000000
313 					0x3A, 0x7F, 0xFF, 0xFF, 0xFF, // -2^31
314 					0x1A, 0xFF, 0xFF, 0xFF, 0xFF, // 0xFFFFFFFF
315 					0xD9, 0xFF, 0xFF, 0x29, // -10
316 					END
317 #ifndef ZCBOR_CANONICAL
318 			0x47, // Simples (len: 7)
319 #else
320 			0x46, // Simples (len: 6)
321 #endif
322 				LIST(5), // List start
323 					0xF5, // True
324 					0xF4, // False
325 					0xF4, // False
326 					0xF6, // Nil
327 					0xF7, // Undefined
328 					END
329 			END
330 		END
331 	};
332 
333 	const uint8_t bytes300[] = {
334 		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,
335 		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,
336 		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,
337 		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,
338 		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,
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 	};
345 
346 	struct Strings strings1 = {0};
347 	struct Strings strings2 = {0};
348 	struct Numbers numbers1 = {0};
349 	uint8_t output1[100];
350 	uint8_t output2[100];
351 	uint8_t output3[400];
352 	uint8_t output4[800];
353 	size_t out_len;
354 
355 	numbers1.fourtoten = 5;
356 	numbers1.twobytes = 0xFFFF;
357 	numbers1.onetofourbytes = 24;
358 	numbers1.minusfivektoplustwohundred = 0;
359 	numbers1.negint = -2147483648;
360 	numbers1.posint = 0xFFFFFFFF;
361 	numbers1.tagged_int = 9;
362 
363 	strings1.optCborStrings_present = true;
364 	strings1.threehundrebytebstr.len = 300;
365 	strings1.threehundrebytebstr.value = bytes300;
366 	strings1.tentothirtybytetstr.len = 30;
367 	strings1.tentothirtybytetstr.value = bytes300;
368 	strings1.cborseqSimples_cbor_count = 3;
369 	strings1.cborseqSimples_cbor[0].boolval = false;
370 	strings1.cborseqSimples_cbor[1].boolval = true;
371 	strings1.cborseqSimples_cbor[2].boolval = false;
372 
373 	strings2.threehundrebytebstr.len = 300;
374 	strings2.threehundrebytebstr.value = bytes300;
375 	strings2.tentothirtybytetstr.len = 9; // Invalid
376 	strings2.tentothirtybytetstr.value = bytes300;
377 	strings2.cborseqSimples_cbor_count = 1;
378 	strings2.cborseqSimples_cbor[0].boolval = false;
379 
380 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Numbers(output2, sizeof(output2), &numbers1, &out_len), NULL);
381 	strings1.cborNumbers.value = output2;
382 	strings1.cborNumbers.len = out_len;
383 	numbers1.tagged_int = -10;
384 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Numbers(output1, sizeof(output1), &numbers1, &out_len), NULL);
385 	strings2.cborNumbers.value = output1;
386 	strings2.cborNumbers.len = out_len;
387 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Strings(output3, sizeof(output3), &strings2, &out_len), NULL);
388 	strings2.tentothirtybytetstr.len = 31; // Invalid
389 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Strings(output3, sizeof(output3), &strings2, &out_len), NULL);
390 	strings2.tentothirtybytetstr.len = 10; // Valid
391 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Strings(output3, sizeof(output3), &strings2, &out_len), NULL);
392 	strings1.optCborStrings.value = output3;
393 	strings1.optCborStrings.len = out_len;
394 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Strings(output4, sizeof(output4), &strings1, &out_len), NULL);
395 	zassert_equal(sizeof(exp_payload_strings1), out_len, "expected: %d, actual: %d\r\n", sizeof(exp_payload_strings1), out_len);
396 
397 	zassert_mem_equal(exp_payload_strings1, output4, sizeof(exp_payload_strings1), NULL);
398 }
399 
ZTEST(cbor_encode_test3,test_simples)400 ZTEST(cbor_encode_test3, test_simples)
401 {
402 	uint8_t exp_payload_simple1[] = {LIST(5), 0xF5, 0xF4, 0xF4, 0xF6, 0xF7, END};
403 	uint8_t exp_payload_simple2[] = {LIST(5), 0xF5, 0xF4, 0xF5, 0xF6, 0xF7, END};
404 
405 	struct Simples input;
406 	size_t len_encode;
407 	uint8_t output[10];
408 
409 	input.boolval = false;
410 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Simple2(output, sizeof(output), &input, &len_encode), NULL);
411 	zassert_equal(len_encode, sizeof(exp_payload_simple1), NULL);
412 	zassert_mem_equal(exp_payload_simple1, output, sizeof(exp_payload_simple1), NULL);
413 
414 	input.boolval = true;
415 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Simple2(output, sizeof(output), &input, &len_encode), NULL);
416 	zassert_equal(len_encode, sizeof(exp_payload_simple2), NULL);
417 	zassert_mem_equal(exp_payload_simple2, output, sizeof(exp_payload_simple2), NULL);
418 }
419 
ZTEST(cbor_encode_test3,test_optional)420 ZTEST(cbor_encode_test3, test_optional)
421 {
422 	const uint8_t exp_payload_optional1[] = {
423 		LIST(3) /* List start */, 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x03, END
424 	};
425 	const uint8_t exp_payload_optional2[] = {
426 		LIST(2) /* List start */, 0xCA /* tag */, 0xF4 /* False */, 0x03, END
427 	};
428 	const uint8_t exp_payload_optional3[] = {
429 		LIST(3) /* List start */, 0xCA /* tag */, 0xF4 /* False */, 0x02, 0x01, END
430 	};
431 	const uint8_t exp_payload_optional4[] = {
432 		LIST(3) /* List start */, 0xCA /* tag */, 0xF5 /* True */, 0x02, 0x02, END
433 	};
434 	const uint8_t exp_payload_optional5[] = {
435 		LIST(4) /* List start */, 0xCA /* tag */, 0xF5 /* True */, 0xF4 /* False */, 0x02, 0x02, END
436 	};
437 	const uint8_t exp_payload_optional6[] = {
438 		LIST(5) /* List start */, 0xCA /* tag */, 0xF5 /* True */, 0xF4 /* False */, 0x02, 0x02, 0x08, END
439 	};
440 	const uint8_t exp_payload_optional7[] = {
441 		LIST(7) /* List start */, 0xCA /* tag */, 0xF5 /* True */, 0xF4 /* False */, 0x02, 0x02, 0x08, 0x08, 0x08, END
442 	};
443 
444 	struct Optional optional1 = {.opttwo_present = true, .manduint = 3};
445 	struct Optional optional2 = {.manduint = 3};
446 	struct Optional optional3 = {.opttwo_present = true, .manduint = 1};
447 	struct Optional optional4 = {.boolval = true, .opttwo_present = true,
448 				.manduint = 2};
449 	struct Optional optional5 = {.boolval = true, .optbool_present = true,
450 				.opttwo_present = true, .manduint = 2};
451 	struct Optional optional6 = {.boolval = true, .optbool_present = true,
452 				.opttwo_present = true, .manduint = 2,
453 				.multi8_count = 1};
454 	struct Optional optional7 = {.boolval = true, .optbool_present = true,
455 				.opttwo_present = true, .manduint = 2,
456 				.multi8_count = 3};
457 	uint8_t output[10];
458 	size_t out_len;
459 
460 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Optional(output,
461 			sizeof(output), &optional1, &out_len), NULL);
462 	zassert_equal(sizeof(exp_payload_optional1), out_len, NULL);
463 	zassert_mem_equal(exp_payload_optional1, output, sizeof(exp_payload_optional1), NULL);
464 
465 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Optional(output,
466 			sizeof(output), &optional2, &out_len), NULL);
467 	zassert_equal(sizeof(exp_payload_optional2), out_len, NULL);
468 	zassert_mem_equal(exp_payload_optional2, output, sizeof(exp_payload_optional2), NULL);
469 
470 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Optional(output,
471 			sizeof(output), &optional3, &out_len), NULL);
472 	zassert_equal(sizeof(exp_payload_optional3), out_len, NULL);
473 	zassert_mem_equal(exp_payload_optional3, output, sizeof(exp_payload_optional3), NULL);
474 
475 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Optional(output,
476 			sizeof(output), &optional4, &out_len), NULL);
477 	zassert_equal(sizeof(exp_payload_optional4), out_len, NULL);
478 	zassert_mem_equal(exp_payload_optional4, output, sizeof(exp_payload_optional4), NULL);
479 
480 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Optional(output,
481 			sizeof(output), &optional5, &out_len), NULL);
482 	zassert_equal(sizeof(exp_payload_optional5), out_len, NULL);
483 	zassert_mem_equal(exp_payload_optional5, output, sizeof(exp_payload_optional5), NULL);
484 
485 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Optional(output,
486 			sizeof(output), &optional6, &out_len), NULL);
487 	zassert_equal(sizeof(exp_payload_optional6), out_len, NULL);
488 	zassert_mem_equal(exp_payload_optional6, output, sizeof(exp_payload_optional6), NULL);
489 
490 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Optional(output,
491 			sizeof(output), &optional7, &out_len), NULL);
492 	zassert_equal(sizeof(exp_payload_optional7), out_len, NULL);
493 	zassert_mem_equal(exp_payload_optional7, output, sizeof(exp_payload_optional7), NULL);
494 }
495 
ZTEST(cbor_encode_test3,test_union)496 ZTEST(cbor_encode_test3, test_union)
497 {
498 	const uint8_t exp_payload_union1[] = {0x01, 0x21};
499 	const uint8_t exp_payload_union2[] = {0x03, 0x23};
500 	const uint8_t exp_payload_union3[] = {0x03, 0x04};
501 	const uint8_t exp_payload_union4[] = {
502 		0x67, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22 // "\"hello\""
503 	};
504 	const uint8_t exp_payload_union5[] = {
505 		0x03, 0x23, 0x03, 0x23, 0x03, 0x23,
506 		0x03, 0x23, 0x03, 0x23, 0x03, 0x23
507 	};
508 
509 	struct Union_r union1 = {.Union_choice = Union_Group_m_c};
510 	struct Union_r union2 = {.Union_choice = Union_MultiGroup_m_c, .MultiGroup_m.MultiGroup_count = 1};
511 	struct Union_r union3 = {.Union_choice = Union_uint3_l_c};
512 	struct Union_r union4 = {.Union_choice = Union_hello_tstr_c};
513 	struct Union_r union5 = {.Union_choice = Union_MultiGroup_m_c, .MultiGroup_m.MultiGroup_count = 6};
514 	struct Union_r union6_inv = {.Union_choice = Union_MultiGroup_m_c, .MultiGroup_m.MultiGroup_count = 7};
515 
516 	uint8_t output[15];
517 	size_t out_len;
518 
519 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Union(output, sizeof(output),
520 				&union1, &out_len), NULL);
521 	zassert_equal(sizeof(exp_payload_union1), out_len, NULL);
522 	zassert_mem_equal(exp_payload_union1, output, sizeof(exp_payload_union1), NULL);
523 
524 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Union(output, sizeof(output),
525 				&union2, &out_len), NULL);
526 	zassert_equal(sizeof(exp_payload_union2), out_len, NULL);
527 	zassert_mem_equal(exp_payload_union2, output, sizeof(exp_payload_union2), NULL);
528 
529 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Union(output, sizeof(output),
530 				&union3, &out_len), NULL);
531 	zassert_equal(sizeof(exp_payload_union3), out_len, NULL);
532 	zassert_mem_equal(exp_payload_union3, output, sizeof(exp_payload_union3), NULL);
533 
534 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Union(output, sizeof(output),
535 				&union4, &out_len), NULL);
536 	zassert_equal(sizeof(exp_payload_union4), out_len, NULL);
537 	zassert_mem_equal(exp_payload_union4, output, sizeof(exp_payload_union4), NULL);
538 
539 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Union(output, sizeof(output),
540 				&union5, &out_len), NULL);
541 	zassert_equal(sizeof(exp_payload_union5), out_len, NULL);
542 	zassert_mem_equal(exp_payload_union5, output, sizeof(exp_payload_union5), NULL);
543 
544 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_encode_Union(output, sizeof(output),
545 				&union6_inv, &out_len), NULL);
546 }
547 
ZTEST(cbor_encode_test3,test_levels)548 ZTEST(cbor_encode_test3, test_levels)
549 {
550 	const uint8_t exp_payload_levels1[] = {
551 		LIST(1), // Level1
552 		LIST(2), // Level2
553 		LIST(4), // Level3 no 1
554 		LIST(1), 0x00, END // Level4 no 1
555 		LIST(1), 0x00, END // Level4 no 2
556 		LIST(1), 0x00, END // Level4 no 3
557 		LIST(1), 0x00, END // Level4 no 4
558 		END
559 		LIST(4), // Level3 no 2
560 		LIST(1), 0x00, END // Level4 no 1
561 		LIST(1), 0x00, END // Level4 no 2
562 		LIST(1), 0x00, END // Level4 no 3
563 		LIST(1), 0x00, END // Level4 no 4
564 		END END END
565 	};
566 	uint8_t output[32];
567 	size_t out_len;
568 
569 	struct Level2 level1 = {.Level3_m_count = 2, .Level3_m = {
570 		{.Level4_m_count = 4}, {.Level4_m_count = 4}
571 	}};
572 	_Static_assert(sizeof(exp_payload_levels1) <= sizeof(output),
573 		"Payload is larger than output");
574 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, cbor_encode_Level1(output,
575 		sizeof(exp_payload_levels1)-1, &level1, &out_len), NULL);
576 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Level1(output,
577 		sizeof(output), &level1, &out_len), NULL);
578 
579 	zassert_equal(sizeof(exp_payload_levels1), out_len, "%d != %d", sizeof(exp_payload_levels1), out_len);
580 	zassert_mem_equal(exp_payload_levels1, output, sizeof(exp_payload_levels1), NULL);
581 }
582 
583 
ZTEST(cbor_encode_test3,test_map)584 ZTEST(cbor_encode_test3, test_map)
585 {
586 	const uint8_t exp_payload_map1[] = {
587 		MAP(4), LIST(2), 0x05, 0x06, END 0xF4, // [5,6] => false
588 		0x07, 0x01, // 7 => 1
589 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
590 		0xf6, 0x40, // nil => ""
591 		END
592 	};
593 	const uint8_t exp_payload_map2[] = {
594 		MAP(5), LIST(2), 0x05, 0x06, END 0xF5, // [5,6] => true
595 		0x07, 0x01, // 7 => 1
596 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
597 		0xf6, 0x40, // nil => ""
598 		0xf6, 0x40, // nil => ""
599 		END
600 	};
601 	const uint8_t exp_payload_map3[] = {
602 		MAP(4), LIST(2), 0x05, 0x06, END 0xF4, // [5,6] => false
603 		0x27, 0x01, // -8 => 1
604 		0xf6, 0x45, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // nil => "hello"
605 		0xf6, 0x40, // nil => ""
606 		END
607 	};
608 
609 	struct Map map1 = {
610 		.Union_choice = union_uint7uint_c,
611 		.uint7uint = 1,
612 		.twotothree_count = 2,
613 		.twotothree = {
614 			{.twotothree = {.value = "hello", .len = 5}},
615 			{.twotothree = {.len = 0}},
616 		}
617 	};
618 	struct Map map2 = {
619 		.listkey = true,
620 		.Union_choice = union_uint7uint_c,
621 		.uint7uint = 1,
622 		.twotothree_count = 3,
623 		.twotothree = {
624 			{.twotothree = {.value = "hello", .len = 5}},
625 			{.twotothree = {.len = 0}},
626 			{.twotothree = {.len = 0}},
627 		}
628 	};
629 	struct Map map3 = {
630 		.Union_choice = union_nint8uint_c,
631 		.nint8uint = 1,
632 		.twotothree_count = 2,
633 		.twotothree = {
634 			{.twotothree = {.value = "hello", .len = 5}},
635 			{.twotothree = {.len = 0}},
636 		}
637 	};
638 
639 	uint8_t output[25];
640 	size_t out_len;
641 
642 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Map(output, sizeof(output),
643 			&map1, &out_len), NULL);
644 	zassert_equal(sizeof(exp_payload_map1), out_len, NULL);
645 	zassert_mem_equal(exp_payload_map1, output, sizeof(exp_payload_map1), NULL);
646 
647 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Map(output, sizeof(output),
648 			&map2, &out_len), NULL);
649 	zassert_equal(sizeof(exp_payload_map2), out_len, NULL);
650 	zassert_mem_equal(exp_payload_map2, output, sizeof(exp_payload_map2), NULL);
651 
652 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Map(output, sizeof(output),
653 			&map3, &out_len), NULL);
654 	zassert_equal(sizeof(exp_payload_map3), out_len, NULL);
655 	zassert_mem_equal(exp_payload_map3, output, sizeof(exp_payload_map3), NULL);
656 }
657 
ZTEST(cbor_encode_test3,test_nested_list_map)658 ZTEST(cbor_encode_test3, test_nested_list_map)
659 {
660 	const uint8_t exp_payload_nested_lm1[] = {LIST(0), END};
661 	const uint8_t exp_payload_nested_lm2[] = {LIST(1), MAP(0), END END};
662 	const uint8_t exp_payload_nested_lm3[] = {LIST(1), MAP(1), 0x01, 0x04, END END};
663 	const uint8_t exp_payload_nested_lm4[] = {LIST(2), MAP(0), END MAP(1), 0x01, 0x04, END END};
664 	const uint8_t exp_payload_nested_lm5[] = {LIST(3), MAP(0), END MAP(0), END MAP(0), END END};
665 	struct NestedListMap listmap1 = {
666 		.map_count = 0,
667 	};
668 	struct NestedListMap listmap2 = {
669 		.map_count = 1,
670 		.map = {
671 			{.uint4_present = false},
672 		}
673 	};
674 	struct NestedListMap listmap3 = {
675 		.map_count = 1,
676 		.map = {
677 			{.uint4_present = true},
678 		}
679 	};
680 	struct NestedListMap listmap4 = {
681 		.map_count = 2,
682 		.map = {
683 			{.uint4_present = false},
684 			{.uint4_present = true},
685 		}
686 	};
687 	struct NestedListMap listmap5 = {
688 		.map_count = 3,
689 		.map = {
690 			{.uint4_present = false},
691 			{.uint4_present = false},
692 			{.uint4_present = false},
693 		}
694 	};
695 	uint8_t output[40];
696 	size_t out_len;
697 
698 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedListMap(output,
699 			sizeof(output), &listmap1, &out_len), NULL);
700 
701 	zassert_equal(sizeof(exp_payload_nested_lm1), out_len, NULL);
702 	zassert_mem_equal(exp_payload_nested_lm1, output, sizeof(exp_payload_nested_lm1), NULL);
703 
704 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedListMap(output,
705 			sizeof(output), &listmap2, &out_len), NULL);
706 
707 	zassert_equal(sizeof(exp_payload_nested_lm2), out_len, NULL);
708 	zassert_mem_equal(exp_payload_nested_lm2, output, sizeof(exp_payload_nested_lm2), NULL);
709 
710 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedListMap(output,
711 			sizeof(output), &listmap3, &out_len), NULL);
712 
713 	zassert_equal(sizeof(exp_payload_nested_lm3), out_len, NULL);
714 	zassert_mem_equal(exp_payload_nested_lm3, output, sizeof(exp_payload_nested_lm3), NULL);
715 
716 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedListMap(output,
717 			sizeof(output), &listmap4, &out_len), NULL);
718 
719 	zassert_equal(sizeof(exp_payload_nested_lm4), out_len, NULL);
720 	zassert_mem_equal(exp_payload_nested_lm4, output, sizeof(exp_payload_nested_lm4), NULL);
721 
722 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedListMap(output,
723 			sizeof(output), &listmap5, &out_len), NULL);
724 
725 	zassert_equal(sizeof(exp_payload_nested_lm5), out_len, "%d != %d", sizeof(exp_payload_nested_lm5), out_len);
726 	zassert_mem_equal(exp_payload_nested_lm5, output, sizeof(exp_payload_nested_lm5), NULL);
727 }
728 
ZTEST(cbor_encode_test3,test_nested_map_list_map)729 ZTEST(cbor_encode_test3, test_nested_map_list_map)
730 {
731 	const uint8_t exp_payload_nested_mlm1[] = {MAP(1), LIST(0), END LIST(0), END END};
732 	const uint8_t exp_payload_nested_mlm2[] = {MAP(1), LIST(0), END LIST(1), MAP(0), END END END};
733 	const uint8_t exp_payload_nested_mlm3[] = {MAP(1), LIST(0), END LIST(2), MAP(0), END MAP(0), END END END};
734 	const uint8_t exp_payload_nested_mlm4[] = {MAP(2), LIST(0), END LIST(0), END LIST(0), END LIST(0), END END};
735 	const uint8_t exp_payload_nested_mlm5[] = {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};
736 	struct NestedMapListMap maplistmap1 = {
737 		.map_l_count = 1,
738 		.map_l = {{0}}
739 	};
740 	struct NestedMapListMap maplistmap2 = {
741 		.map_l_count = 1,
742 		.map_l = {
743 			{.map_count = 1}
744 		}
745 	};
746 	struct NestedMapListMap maplistmap3 = {
747 		.map_l_count = 1,
748 		.map_l = {
749 			{.map_count = 2}
750 		}
751 	};
752 	struct NestedMapListMap maplistmap4 = {
753 		.map_l_count = 2,
754 		.map_l = {
755 			{.map_count = 0},
756 			{.map_count = 0},
757 		}
758 	};
759 	struct NestedMapListMap maplistmap5 = {
760 		.map_l_count = 3,
761 		.map_l = {
762 			{.map_count = 0},
763 			{.map_count = 0},
764 			{.map_count = 2},
765 		}
766 	};
767 	uint8_t output[30];
768 	size_t out_len;
769 
770 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedMapListMap(output,
771 			sizeof(output), &maplistmap1, &out_len), NULL);
772 
773 	zassert_equal(sizeof(exp_payload_nested_mlm1), out_len, NULL);
774 	zassert_mem_equal(exp_payload_nested_mlm1, output, sizeof(exp_payload_nested_mlm1), NULL);
775 
776 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedMapListMap(output,
777 			sizeof(output), &maplistmap2, &out_len), NULL);
778 
779 	zassert_equal(sizeof(exp_payload_nested_mlm2), out_len, "%d != %d", sizeof(exp_payload_nested_mlm2), out_len);
780 	zassert_mem_equal(exp_payload_nested_mlm2, output, sizeof(exp_payload_nested_mlm2), NULL);
781 
782 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedMapListMap(output,
783 			sizeof(output), &maplistmap3, &out_len), NULL);
784 
785 	zassert_equal(sizeof(exp_payload_nested_mlm3), out_len, "%d != %d", sizeof(exp_payload_nested_mlm3), out_len);
786 	zassert_mem_equal(exp_payload_nested_mlm3, output, sizeof(exp_payload_nested_mlm3), NULL);
787 
788 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedMapListMap(output,
789 			sizeof(output), &maplistmap4, &out_len), NULL);
790 
791 	zassert_equal(sizeof(exp_payload_nested_mlm4), out_len, NULL);
792 	zassert_mem_equal(exp_payload_nested_mlm4, output, sizeof(exp_payload_nested_mlm4), NULL);
793 
794 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_NestedMapListMap(output,
795 			sizeof(output), &maplistmap5, &out_len), NULL);
796 
797 	zassert_equal(sizeof(exp_payload_nested_mlm5), out_len, "%d != %d", sizeof(exp_payload_nested_mlm5), out_len);
798 	zassert_mem_equal(exp_payload_nested_mlm5, output, sizeof(exp_payload_nested_mlm5), NULL);
799 }
800 
801 
ZTEST(cbor_encode_test3,test_range)802 ZTEST(cbor_encode_test3, test_range)
803 {
804 	const uint8_t exp_payload_range1[] = {LIST(3),
805 		0x08,
806 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
807 		0x0,
808 		END
809 	};
810 
811 	const uint8_t exp_payload_range2[] = {LIST(6),
812 		0x05,
813 		0x08, 0x08,
814 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
815 		0x00, 0x0A,
816 		END
817 	};
818 
819 	const uint8_t exp_payload_range3[] = {LIST(5),
820 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
821 		0x08,
822 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
823 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
824 		0x07,
825 		END
826 	};
827 	const uint8_t exp_payload_range4[] = {LIST(4),
828 		0x28,
829 		0x08,
830 		0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
831 		0x0,
832 		END
833 	};
834 
835 	struct Range input1 = {
836 		.optMinus5to5_present = false,
837 		.optStr3to6_present = false,
838 		.optMinus9toMinus6excl_present = false,
839 		.multi8_count = 1,
840 		.multiHello_count = 1,
841 		.multi0to10_count = 1,
842 		.multi0to10 = {0},
843 	};
844 	struct Range input2 = {
845 		.optMinus5to5_present = true,
846 		.optMinus5to5 = 5,
847 		.optStr3to6_present = false,
848 		.optMinus9toMinus6excl_present = false,
849 		.multi8_count = 2,
850 		.multiHello_count = 1,
851 		.multi0to10_count = 2,
852 		.multi0to10 = {0, 10},
853 	};
854 	struct Range input3 = {
855 		.optMinus5to5_present = false,
856 		.optStr3to6_present = true,
857 		.optStr3to6 = {
858 			.value = "hello",
859 			.len = 5,
860 		},
861 		.optMinus9toMinus6excl_present = false,
862 		.multi8_count = 1,
863 		.multiHello_count = 2,
864 		.multi0to10_count = 1,
865 		.multi0to10 = {7},
866 	};
867 	struct Range input4 = {
868 		.optMinus5to5_present = false,
869 		.optStr3to6_present = false,
870 		.optMinus9toMinus6excl_present = true,
871 		.optMinus9toMinus6excl = -9,
872 		.multi8_count = 1,
873 		.multiHello_count = 1,
874 		.multi0to10_count = 1,
875 		.multi0to10 = {0},
876 	};
877 	struct Range input5_inv = {
878 		.optMinus5to5_present = false,
879 		.optStr3to6_present = false,
880 		.optMinus9toMinus6excl_present = true,
881 		.optMinus9toMinus6excl = -6,
882 		.multi8_count = 1,
883 		.multiHello_count = 1,
884 		.multi0to10_count = 1,
885 		.multi0to10 = {0},
886 	};
887 
888 	uint8_t output[25];
889 	size_t out_len;
890 
891 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Range(output, sizeof(output), &input1,
892 				&out_len), NULL);
893 	zassert_equal(sizeof(exp_payload_range1), out_len, NULL);
894 	zassert_mem_equal(exp_payload_range1, output, sizeof(exp_payload_range1), NULL);
895 
896 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Range(output, sizeof(output), &input2,
897 				&out_len), NULL);
898 	zassert_equal(sizeof(exp_payload_range2), out_len, NULL);
899 	zassert_mem_equal(exp_payload_range2, output, sizeof(exp_payload_range2), NULL);
900 
901 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Range(output, sizeof(output), &input3,
902 				&out_len), NULL);
903 	zassert_equal(sizeof(exp_payload_range3), out_len, NULL);
904 	zassert_mem_equal(exp_payload_range3, output, sizeof(exp_payload_range3), NULL);
905 
906 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Range(output, sizeof(output), &input4,
907 				&out_len), NULL);
908 	zassert_equal(sizeof(exp_payload_range4), out_len, NULL);
909 	zassert_mem_equal(exp_payload_range4, output, sizeof(exp_payload_range4), NULL);
910 
911 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_Range(output, sizeof(output), &input5_inv,
912 				&out_len), NULL);
913 }
914 
ZTEST(cbor_encode_test3,test_value_range)915 ZTEST(cbor_encode_test3, test_value_range)
916 {
917 	const uint8_t exp_payload_value_range1[] = {LIST(6),
918 		11,
919 		0x19, 0x03, 0xe7, // 999
920 		0x29, // -10
921 		1,
922 		0x18, 42, // 42
923 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
924 		END
925 	};
926 
927 	const uint8_t exp_payload_value_range2[] = {LIST(6),
928 		0x18, 100, // 100
929 		0x39, 0x03, 0xe8, // -1001
930 		0x18, 100, // 100
931 		0,
932 		0x18, 42, // 42
933 		0x65, 'w', 'o', 'r', 'l', 'd', // "world"
934 		END
935 	};
936 
937 	struct ValueRange input1 = {
938 		.greater10 = 11,
939 		.less1000 = 999,
940 		.greatereqmin10 = -10,
941 		.lesseq1 = 1,
942 	};
943 	struct ValueRange input2 = {
944 		.greater10 = 100,
945 		.less1000 = -1001,
946 		.greatereqmin10 = 100,
947 		.lesseq1 = 0,
948 	};
949 	struct ValueRange input3_inval = {
950 		.greater10 = 10,
951 		.less1000 = 999,
952 		.greatereqmin10 = -10,
953 		.lesseq1 = 1,
954 	};
955 	struct ValueRange input4_inval = {
956 		.greater10 = 11,
957 		.less1000 = 1000,
958 		.greatereqmin10 = -10,
959 		.lesseq1 = 1,
960 	};
961 	struct ValueRange input5_inval = {
962 		.greater10 = 11,
963 		.less1000 = 999,
964 		.greatereqmin10 = -11,
965 		.lesseq1 = 1,
966 	};
967 	struct ValueRange input6_inval = {
968 		.greater10 = 11,
969 		.less1000 = 999,
970 		.greatereqmin10 = -10,
971 		.lesseq1 = 2,
972 	};
973 	struct ValueRange input7_inval = {
974 		.greater10 = 1,
975 		.less1000 = 999,
976 		.greatereqmin10 = -10,
977 		.lesseq1 = 1,
978 	};
979 	struct ValueRange input8_inval = {
980 		.greater10 = 11,
981 		.less1000 = 10000,
982 		.greatereqmin10 = -10,
983 		.lesseq1 = 1,
984 	};
985 	struct ValueRange input9_inval = {
986 		.greater10 = 11,
987 		.less1000 = 999,
988 		.greatereqmin10 = -100,
989 		.lesseq1 = 1,
990 	};
991 	struct ValueRange input10_inval = {
992 		.greater10 = 11,
993 		.less1000 = 999,
994 		.greatereqmin10 = -10,
995 		.lesseq1 = 21,
996 	};
997 
998 	uint8_t output[25];
999 	size_t out_len;
1000 
1001 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_ValueRange(output, sizeof(output), &input1,
1002 				&out_len), NULL);
1003 	zassert_equal(sizeof(exp_payload_value_range1), out_len, "%d != %d\n", sizeof(exp_payload_value_range1), out_len);
1004 	zassert_mem_equal(exp_payload_value_range1, output, sizeof(exp_payload_value_range1), NULL);
1005 
1006 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_ValueRange(output, sizeof(output), &input2,
1007 				&out_len), NULL);
1008 	zassert_equal(sizeof(exp_payload_value_range2), out_len, NULL);
1009 	zassert_mem_equal(exp_payload_value_range2, output, sizeof(exp_payload_value_range2), NULL);
1010 
1011 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input3_inval,
1012 				&out_len), NULL);
1013 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input4_inval,
1014 				&out_len), NULL);
1015 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input5_inval,
1016 				&out_len), NULL);
1017 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input6_inval,
1018 				&out_len), NULL);
1019 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input7_inval,
1020 				&out_len), NULL);
1021 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input8_inval,
1022 				&out_len), NULL);
1023 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input9_inval,
1024 				&out_len), NULL);
1025 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_ValueRange(output, sizeof(output), &input10_inval,
1026 				&out_len), NULL);
1027 }
1028 
ZTEST(cbor_encode_test3,test_single)1029 ZTEST(cbor_encode_test3, test_single)
1030 {
1031 	uint8_t exp_payload_single0[] = {0x45, 'h', 'e', 'l', 'l', 'o'};
1032 	uint8_t exp_payload_single1[] = {0x18, 52,};
1033 	uint8_t exp_payload_single2[] = {9};
1034 	uint8_t output[10];
1035 	size_t out_len;
1036 	struct zcbor_string input_single0 = {
1037 		.value = "hello",
1038 		.len = 5
1039 	};
1040 	size_t input_single1 = 52;
1041 	size_t input_single2_ign = 53;
1042 	size_t input_single3 = 9;
1043 	size_t input_single4_inv = 10;
1044 
1045 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_SingleBstr(output, sizeof(output), &input_single0, &out_len), NULL);
1046 	zassert_equal(sizeof(exp_payload_single0), out_len, NULL);
1047 	zassert_mem_equal(exp_payload_single0, output, sizeof(exp_payload_single0), NULL);
1048 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, cbor_encode_SingleBstr(output, 5, &input_single0, &out_len), NULL);
1049 
1050 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_SingleInt_uint52(output, sizeof(output), &input_single1, &out_len), NULL);
1051 	zassert_equal(sizeof(exp_payload_single1), out_len, NULL);
1052 	zassert_mem_equal(exp_payload_single1, output, sizeof(exp_payload_single1), NULL);
1053 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, cbor_encode_SingleInt_uint52(output, 1, &input_single1, &out_len), NULL);
1054 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_SingleInt_uint52(output, sizeof(output), &input_single2_ign, &out_len), NULL);
1055 	zassert_equal(sizeof(exp_payload_single1), out_len, NULL);
1056 	zassert_mem_equal(exp_payload_single1, output, sizeof(exp_payload_single1), NULL);
1057 
1058 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_SingleInt2(output, sizeof(output), &input_single3, &out_len), NULL);
1059 	zassert_equal(sizeof(exp_payload_single2), out_len, NULL);
1060 	zassert_mem_equal(exp_payload_single2, output, sizeof(exp_payload_single2), NULL);
1061 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_SingleInt2(output, sizeof(output), &input_single4_inv, &out_len), NULL);
1062 }
1063 
ZTEST(cbor_encode_test3,test_unabstracted)1064 ZTEST(cbor_encode_test3, test_unabstracted)
1065 {
1066 	uint8_t exp_payload_unabstracted0[] = {LIST(2), 0x01, 0x03, END};
1067 	uint8_t exp_payload_unabstracted1[] = {LIST(2), 0x02, 0x04, END};
1068 	struct Unabstracted result_unabstracted0 = {
1069 		.unabstractedunion1_choice = Unabstracted_unabstractedunion1_choice1_c,
1070 		.unabstractedunion2_choice = Unabstracted_unabstractedunion2_uint3_c,
1071 	};
1072 	struct Unabstracted result_unabstracted1 = {
1073 		.unabstractedunion1_choice = Unabstracted_unabstractedunion1_choice2_c,
1074 		.unabstractedunion2_choice = Unabstracted_unabstractedunion2_choice4_c,
1075 	};
1076 	uint8_t output[4];
1077 	size_t out_len;
1078 
1079 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Unabstracted(output, sizeof(output),
1080 					&result_unabstracted0, &out_len), NULL);
1081 	zassert_equal(sizeof(exp_payload_unabstracted0), out_len, "was %d\n", out_len);
1082 	zassert_mem_equal(exp_payload_unabstracted0, output, sizeof(exp_payload_unabstracted0), NULL);
1083 
1084 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Unabstracted(output, sizeof(output),
1085 					&result_unabstracted1, &out_len), NULL);
1086 	zassert_equal(sizeof(exp_payload_unabstracted1), out_len, NULL);
1087 	zassert_mem_equal(exp_payload_unabstracted1, output, sizeof(exp_payload_unabstracted1), NULL);
1088 }
1089 
ZTEST(cbor_encode_test3,test_string_overflow)1090 ZTEST(cbor_encode_test3, test_string_overflow)
1091 {
1092 	struct zcbor_string input_overflow0 = {
1093 		.value = "",
1094 		.len = 0xFFFFFF00, /* overflows to before this object. */
1095 	};
1096 	uint8_t output[10];
1097 	size_t out_len;
1098 
1099 	zassert_equal(ZCBOR_ERR_NO_PAYLOAD, cbor_encode_SingleBstr(output, sizeof(output), &input_overflow0, &out_len), NULL);
1100 }
1101 
ZTEST(cbor_encode_test3,test_quantity_range)1102 ZTEST(cbor_encode_test3, test_quantity_range)
1103 {
1104 	uint8_t exp_payload_qty_range1[] = {0xF5, 0xF5, 0xF5};
1105 	uint8_t exp_payload_qty_range2[] = {0xF6, 0xF6, 0xF6, 0xF6, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5};
1106 	struct QuantityRange result_qty_range1 = {
1107 		.upto4nils_count = 0,
1108 		.from3true_count = 3,
1109 	};
1110 	struct QuantityRange result_qty_range2 = {
1111 		.upto4nils_count = 4,
1112 		.from3true_count = 6,
1113 	};
1114 	struct QuantityRange result_qty_range3_inv = {
1115 		.upto4nils_count = 5,
1116 		.from3true_count = 3
1117 	};
1118 	struct QuantityRange result_qty_range4_inv = {
1119 		.upto4nils_count = 0,
1120 		.from3true_count = 2
1121 	};
1122 	uint8_t output[12];
1123 	size_t out_len;
1124 
1125 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_QuantityRange(output, sizeof(output),
1126 			&result_qty_range1, &out_len), NULL);
1127 	zassert_equal(sizeof(exp_payload_qty_range1), out_len, "was %d\n", out_len);
1128 	zassert_mem_equal(exp_payload_qty_range1, output, sizeof(exp_payload_qty_range1), NULL);
1129 
1130 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_QuantityRange(output, sizeof(output),
1131 			&result_qty_range2, &out_len), NULL);
1132 	zassert_equal(sizeof(exp_payload_qty_range2), out_len, "was %d\n", out_len);
1133 	zassert_mem_equal(exp_payload_qty_range2, output, sizeof(exp_payload_qty_range2), NULL);
1134 
1135 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_encode_QuantityRange(output, sizeof(output),
1136 			&result_qty_range3_inv, &out_len), NULL);
1137 
1138 	zassert_equal(ZCBOR_ERR_ITERATIONS, cbor_encode_QuantityRange(output, sizeof(output),
1139 			&result_qty_range4_inv, &out_len), NULL);
1140 }
1141 
ZTEST(cbor_encode_test3,test_doublemap)1142 ZTEST(cbor_encode_test3, test_doublemap)
1143 {
1144 	uint8_t exp_payload_doublemap0[] = {MAP(2), 0x01, MAP(1), 0x01, 0x01, END 0x02, MAP(1), 0x02, 0x02, END END};
1145 	struct DoubleMap result_doublemap = {
1146 		.uintmap_count = 2,
1147 		.uintmap = {
1148 			{
1149 				.uintmap_key = 1,
1150 				.MyKeys_m = {
1151 					.uint1int_present = true,
1152 					.uint1int = {.uint1int = 1},
1153 				}
1154 			},
1155 			{
1156 				.uintmap_key = 2,
1157 				.MyKeys_m = {
1158 					.uint2int_present = true,
1159 					.uint2int = {.uint2int = 2},
1160 				}
1161 			},
1162 		}
1163 	};
1164 	uint8_t output[20];
1165 	size_t out_len;
1166 
1167 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_DoubleMap(output,
1168 					sizeof(output),
1169 					&result_doublemap, &out_len), NULL);
1170 	zassert_equal(out_len, sizeof(exp_payload_doublemap0), "%d != %d\n",
1171 			out_len, sizeof(exp_payload_doublemap0));
1172 	zassert_mem_equal(exp_payload_doublemap0, output, out_len, NULL);
1173 }
1174 
ZTEST(cbor_encode_test3,test_floats)1175 ZTEST(cbor_encode_test3, test_floats)
1176 {
1177 	uint8_t exp_floats_payload1[] = {LIST(5), 0xF9, 0, 0, /* 0.0 */
1178 			0xFA, 0, 0, 0, 0 /* 0.0 */,
1179 			0xFB, 0, 0, 0, 0, 0, 0, 0, 0 /* 0.0 */,
1180 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1181 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1182 			END
1183 	};
1184 	uint8_t exp_floats_payload2[] = {LIST(5), 0xF9, 0x42, 0x48, /* 3.1415 */
1185 			0xFA, 0xc7, 0xc0, 0xe6, 0xb7 /* -98765.4321 */,
1186 			0xFB, 0x41, 0x32, 0xd6, 0x87, 0xe3, 0xd7, 0xa, 0x3d /* 1234567.89 */,
1187 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1188 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1189 			END
1190 	};
1191 
1192 	uint8_t exp_floats_payload3[] = {LIST(5), 0xF9, 0xfc, 0x0, /* -98765.4321 (-infinity) */
1193 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1194 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1195 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1196 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1197 			END
1198 	};
1199 
1200 	uint8_t exp_floats_payload4[] = {LIST(8), 0xF9, 0x42, 0x48, /* 3.1415 */
1201 			0xFA, 0x49, 0x96, 0xb4, 0x3f /* 1234567.89 */,
1202 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1203 			0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1204 			0xFB, 0x40, 0x5, 0xbf, 0x9, 0x95, 0xaa, 0xf7, 0x90 /* 2.71828 */,
1205 			0xFB, 0x3f, 0x31, 0xa5, 0x9d, 0xe0, 0x0, 0x0, 0x0 /* 123/456789 */,
1206 			0xFB, 0x3f, 0x31, 0xa5, 0x9d, 0xd9, 0x57, 0x14, 0x64 /* 123/456789 */,
1207 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1208 			END
1209 	};
1210 
1211 	struct Floats input;
1212 	size_t num_encode;
1213 	uint8_t output[70];
1214 
1215 	input.float_16 = (float)0.0;
1216 	input.float_32 = (float)0.0;
1217 	input.float_64 = (double)0.0;
1218 	input.floats_count = 0;
1219 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Floats(
1220 		output, sizeof(output), &input, &num_encode), NULL);
1221 
1222 	zassert_equal(sizeof(exp_floats_payload1), num_encode, NULL);
1223 	zassert_mem_equal(exp_floats_payload1, output, num_encode, NULL);
1224 
1225 	input.float_16 = (float)3.1415;
1226 	input.float_32 = (float)-98765.4321;
1227 	input.float_64 = (double)1234567.89;
1228 	input.floats_count = 0;
1229 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Floats(
1230 		output, sizeof(output), &input, &num_encode), NULL);
1231 	zassert_equal(sizeof(exp_floats_payload2), num_encode, NULL);
1232 	zassert_mem_equal(exp_floats_payload2, output, num_encode, NULL);
1233 
1234 	input.float_16 = (float)-98765.4321;
1235 	input.float_32 = (float)1234567.89;
1236 	input.float_64 = (double)-98765.4321;
1237 	input.floats_count = 0;
1238 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Floats(
1239 		output, sizeof(output), &input, &num_encode), NULL);
1240 	zassert_equal(sizeof(exp_floats_payload3), num_encode, NULL);
1241 	zassert_mem_equal(exp_floats_payload3, output, num_encode, NULL);
1242 
1243 	input.float_16 = (float)3.1415;
1244 	input.float_32 = (float)1234567.89;
1245 	input.float_64 = (double)-98765.4321;
1246 	input.floats_count = 3;
1247 	input.floats[0] = (float)(123.0/456789.0);
1248 	input.floats[1] = (double)(123.0/456789.0);
1249 	input.floats[2] = (double)(-1.0/(1LL << 42));
1250 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Floats(
1251 		output, sizeof(output), &input, &num_encode), NULL);
1252 	zassert_equal(sizeof(exp_floats_payload4), num_encode, NULL);
1253 	zassert_mem_equal(exp_floats_payload4, output, num_encode, NULL);
1254 
1255 }
1256 
1257 
1258 /* Test using ranges (greater/less than) on floats. */
ZTEST(cbor_encode_test3,test_floats2)1259 ZTEST(cbor_encode_test3, test_floats2)
1260 {
1261 	uint8_t exp_floats2_payload1[] = {LIST(2),
1262 			0xFB, 0xc0, 0xf8, 0x1c, 0xd6, 0xe9, 0xe1, 0xb0, 0x8a /* -98765.4321 */,
1263 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1264 			END
1265 	};
1266 	uint8_t exp_floats2_payload2[] = {LIST(2),
1267 			0xFB, 0xbd, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* -2^(-42) */,
1268 			0xFB, 0xc0, 0xc3, 0x88, 0x0, 0x0, 0x0, 0x0, 0x0 /* -10000 */,
1269 			END
1270 	};
1271 	size_t num_encode;
1272 	struct Floats2 input;
1273 	uint8_t output[40];
1274 
1275 	input.float_lt_1 = -98765.4321;
1276 	input.float_ge_min_10000 = (-1.0/(1LL << 42));
1277 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Floats2(
1278 		output, sizeof(output), &input, &num_encode), NULL);
1279 	zassert_equal(sizeof(exp_floats2_payload1), num_encode, NULL);
1280 	zassert_mem_equal(exp_floats2_payload1, output, num_encode, NULL);
1281 
1282 	input.float_lt_1 = (-1.0/(1LL << 42));
1283 	input.float_ge_min_10000 = -10000;
1284 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Floats2(
1285 		output, sizeof(output), &input, &num_encode), NULL);
1286 	zassert_equal(sizeof(exp_floats2_payload2), num_encode, NULL);
1287 	zassert_mem_equal(exp_floats2_payload2, output, num_encode, NULL);
1288 }
1289 
ZTEST(cbor_encode_test3,test_cbor_bstr)1290 ZTEST(cbor_encode_test3, test_cbor_bstr)
1291 {
1292 	uint8_t exp_cbor_bstr_payload1[] = {
1293 #ifdef ZCBOR_CANONICAL
1294 		0x58, 33,
1295 #else
1296 		0x58, 34,
1297 #endif
1298 			LIST(4),
1299 				0x46, 0x65, 'H', 'e', 'l', 'l', 'o',
1300 				0x49, 0xFB, 0x40, 0x9, 0x21, 0xca, 0xc0, 0x83, 0x12, 0x6f /* 3.1415 */,
1301 				0x4C, 0xC2, 0x4A, 0x42, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* 0x4202030405060708090A */,
1302 				0x41, 0xF6 /* nil */,
1303 			END
1304 	};
1305 
1306 	struct CBORBstr input = {0};
1307 	size_t num_encode;
1308 	uint8_t output[70];
1309 
1310 	input.big_uint_bstr_cbor.value = (uint8_t []){0x42, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1311 	input.big_uint_bstr_cbor.len = 10;
1312 
1313 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_CBORBstr(output, sizeof(output), &input, &num_encode), NULL);
1314 
1315 	zassert_equal(sizeof(exp_cbor_bstr_payload1), num_encode, "%d != %d\r\n", sizeof(exp_cbor_bstr_payload1), num_encode);
1316 	zassert_mem_equal(exp_cbor_bstr_payload1, output, num_encode, NULL);
1317 }
1318 
1319 
ZTEST(cbor_encode_test3,test_map_length)1320 ZTEST(cbor_encode_test3, test_map_length)
1321 {
1322 	uint8_t exp_map_length_payload1[] = {MAP(2),
1323 		0x61, 'r', 0x01,
1324 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6, END
1325 	};
1326 	uint8_t exp_map_length_payload2[] = {MAP(3),
1327 		0x61, 'r', 0x01,
1328 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1329 		0x61, 'e', LIST(0), END END
1330 	};
1331 	uint8_t exp_map_length_payload3[] = {MAP(3),
1332 		0x61, 'r', 0x01,
1333 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1334 		0x61, 'e', LIST(1), 0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, END END
1335 	};
1336 	uint8_t exp_map_length_payload4[] = {MAP(3),
1337 		0x61, 'r', 0x01,
1338 		0x61, 'm', 0x46, 1, 2, 3, 4, 5, 6,
1339 		0x61, 'e', LIST(2), 0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1,
1340 			0x50, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, END END
1341 	};
1342 
1343 	struct MapLength input;
1344 	size_t num_encode;
1345 	uint8_t output[60];
1346 
1347 	uint8_t mac[] = {1, 2, 3, 4, 5, 6};
1348 	uint8_t uuid_m[] = {8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, 0};
1349 
1350 	input.result = 1;
1351 	input.mac_addr.len = 6;
1352 	input.mac_addr.value = mac;
1353 	input.end_device_array_present = false;
1354 
1355 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapLength(output,
1356 		sizeof(output), &input, &num_encode), NULL);
1357 	zassert_equal(sizeof(exp_map_length_payload1), num_encode, "%d != %d\r\n", sizeof(exp_map_length_payload1), num_encode);
1358 	zassert_mem_equal(exp_map_length_payload1, output, num_encode, NULL);
1359 
1360 	input.end_device_array_present = true;
1361 	input.end_device_array.uuid_m_count = 0;
1362 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapLength(output,
1363 		sizeof(output), &input, &num_encode), NULL);
1364 	zassert_equal(sizeof(exp_map_length_payload2), num_encode, "%d != %d\r\n", sizeof(exp_map_length_payload2), num_encode);
1365 	zassert_mem_equal(exp_map_length_payload2, output, num_encode, NULL);
1366 
1367 	input.end_device_array.uuid_m_count = 1;
1368 	input.end_device_array.uuid_m[0].len = 16;
1369 	input.end_device_array.uuid_m[0].value = uuid_m;
1370 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapLength(output,
1371 		sizeof(output), &input, &num_encode), NULL);
1372 	zassert_equal(sizeof(exp_map_length_payload3), num_encode, "%d != %d\r\n", sizeof(exp_map_length_payload3), num_encode);
1373 	zassert_mem_equal(exp_map_length_payload3, output, num_encode, NULL);
1374 
1375 	input.end_device_array.uuid_m_count = 2;
1376 	input.end_device_array.uuid_m[1].len = 16;
1377 	input.end_device_array.uuid_m[1].value = uuid_m;
1378 	int err = cbor_encode_MapLength(output,
1379 		sizeof(output), &input, &num_encode);
1380 	zassert_equal(ZCBOR_SUCCESS, err, "%d\r\b", err);
1381 	zassert_equal(sizeof(exp_map_length_payload4), num_encode, "%d != %d\r\n", sizeof(exp_map_length_payload4), num_encode);
1382 	zassert_mem_equal(exp_map_length_payload4, output, num_encode, NULL);
1383 
1384 	input.mac_addr.len--;
1385 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_encode_MapLength(output,
1386 		sizeof(output), &input, &num_encode), NULL);
1387 
1388 	input.mac_addr.len++;
1389 	input.end_device_array.uuid_m[1].len++;
1390 	err = cbor_encode_MapLength(output,
1391 		sizeof(output), &input, &num_encode);
1392 	zassert_equal(ZCBOR_ERR_WRONG_RANGE, err, "%d\r\b", err);
1393 }
1394 
1395 
ZTEST(cbor_encode_test3,test_union_int)1396 ZTEST(cbor_encode_test3, test_union_int)
1397 {
1398 	uint8_t exp_union_int_payload1[] = {LIST(2),
1399 		0x05, 0x6E, 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'f', 'i', 'v', 'e',
1400 		END
1401 	};
1402 	uint8_t exp_union_int_payload2[] = {LIST(2),
1403 		0x19, 0x03, 0xE8, 0x50, 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'h', 'o', 'u', 's', 'a', 'n', 'd',
1404 		END
1405 	};
1406 	uint8_t exp_union_int_payload3[] = {LIST(3),
1407 		0x3A, 0x00, 0x01, 0x86, 0x9F, 0xF6, 0x01,
1408 		END
1409 	};
1410 	uint8_t exp_union_int_payload4[] = {LIST(2),
1411 		0x01, 0x42, 'h', 'i',
1412 		END
1413 	};
1414 	struct UnionInt2 input;
1415 	size_t num_encode;
1416 	uint8_t output[60];
1417 
1418 	input.Union_choice = union_uint5_l_c;
1419 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_UnionInt2(output, sizeof(output),
1420 			&input, &num_encode), NULL);
1421 	zassert_equal(sizeof(exp_union_int_payload1), num_encode, NULL);
1422 	zassert_mem_equal(exp_union_int_payload1, output, num_encode, NULL);
1423 
1424 	input.Union_choice = union_uint1000_l_c;
1425 	input.bstr.len = 16;
1426 	input.bstr.value = (uint8_t *)&"This is thousand";
1427 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_UnionInt2(output, sizeof(output),
1428 			&input, &num_encode), NULL);
1429 	zassert_equal(sizeof(exp_union_int_payload2), num_encode, NULL);
1430 	zassert_mem_equal(exp_union_int_payload2, output, num_encode, NULL);
1431 
1432 	input.Union_choice = union_nint100000_l_c;
1433 	input.number_m.number_choice = number_int_c;
1434 	input.number_m.Int = 1;
1435 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_UnionInt2(output, sizeof(output),
1436 			&input, &num_encode), NULL);
1437 	zassert_equal(sizeof(exp_union_int_payload3), num_encode, NULL);
1438 	zassert_mem_equal(exp_union_int_payload3, output, num_encode, NULL);
1439 
1440 	input.Union_choice = UnionInt2_union_Structure_One_m_c;
1441 	input.Structure_One_m.some_array.value = (uint8_t *)&"hi";
1442 	input.Structure_One_m.some_array.len = 2;
1443 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_UnionInt2(output, sizeof(output),
1444 			&input, &num_encode), NULL);
1445 	zassert_equal(sizeof(exp_union_int_payload4), num_encode, NULL);
1446 	zassert_mem_equal(exp_union_int_payload4, output, num_encode, NULL);
1447 }
1448 
1449 
ZTEST(cbor_encode_test3,test_intmax)1450 ZTEST(cbor_encode_test3, test_intmax)
1451 {
1452 	uint8_t exp_intmax1_payload1[] = {LIST(C),
1453 		0x38, 0x7F, 0x18, 0x7F, 0x18, 0xFF,
1454 		0x39, 0x7F, 0xFF,
1455 		0x19, 0x7F, 0xFF,
1456 		0x19, 0xFF, 0xFF,
1457 		0x3A, 0x7F, 0xFF, 0xFF, 0xFF,
1458 		0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
1459 		0x1A, 0xFF, 0xFF, 0xFF, 0xFF,
1460 		0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1461 		0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1462 		0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1463 		END
1464 	};
1465 	uint8_t exp_intmax2_payload1[] = {LIST(8),
1466 		0x38, 0x7F, 0x0,
1467 		0x39, 0x7F, 0xFF,
1468 		0x0,
1469 		0x3A, 0x7F, 0xFF, 0xFF, 0xFF,
1470 		0x0,
1471 		0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1472 		0x0,
1473 		END
1474 	};
1475 	uint8_t exp_intmax2_payload2[] = {LIST(8),
1476 		0x18, 0x7F, 0x18, 0xFF,
1477 		0x19, 0x7F, 0xFF,
1478 		0x19, 0xFF, 0xFF,
1479 		0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
1480 		0x1A, 0xFF, 0xFF, 0xFF, 0xFF,
1481 		0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1482 		0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1483 		END
1484 	};
1485 	struct Intmax2 intput2;
1486 	size_t num_encode;
1487 	uint8_t output[60];
1488 
1489 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Intmax1(output,
1490 		sizeof(output), NULL, &num_encode), NULL);
1491 	zassert_equal(sizeof(exp_intmax1_payload1), num_encode, NULL);
1492 	zassert_mem_equal(exp_intmax1_payload1, output, num_encode, NULL);
1493 
1494 	intput2.INT_8 = INT8_MIN;
1495 	intput2.UINT_8 = 0;
1496 	intput2.INT_16 = INT16_MIN;
1497 	intput2.UINT_16 = 0;
1498 	intput2.INT_32 = INT32_MIN;
1499 	intput2.UINT_32 = 0;
1500 	intput2.INT_64 = INT64_MIN;
1501 	intput2.UINT_64 = 0;
1502 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Intmax2(output,
1503 		sizeof(output), &intput2, &num_encode), NULL);
1504 	zassert_equal(sizeof(exp_intmax2_payload1), num_encode, NULL);
1505 	zassert_mem_equal(exp_intmax2_payload1, output, num_encode, NULL);
1506 
1507 	intput2.INT_8 = INT8_MAX;
1508 	intput2.UINT_8 = UINT8_MAX;
1509 	intput2.INT_16 = INT16_MAX;
1510 	intput2.UINT_16 = UINT16_MAX;
1511 	intput2.INT_32 = INT32_MAX;
1512 	intput2.UINT_32 = UINT32_MAX;
1513 	intput2.INT_64 = INT64_MAX;
1514 	intput2.UINT_64 = UINT64_MAX;
1515 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Intmax2(output,
1516 		sizeof(output), &intput2, &num_encode), NULL);
1517 	zassert_equal(sizeof(exp_intmax2_payload2), num_encode, NULL);
1518 	zassert_mem_equal(exp_intmax2_payload2, output, num_encode, NULL);
1519 }
1520 
1521 
1522 /* Test that zcbor generates variable names that don't contain unsupported characters. */
ZTEST(cbor_encode_test3,test_invalid_identifiers)1523 ZTEST(cbor_encode_test3, test_invalid_identifiers)
1524 {
1525 	uint8_t exp_invalid_identifiers_payload1[] = {
1526 		LIST(3),
1527 		0x64, '1', 'o', 'n', 'e',
1528 		0x02, /* Ø */
1529 		0x67, '{', '[', 'a', '-', 'z', ']', '}',
1530 		END
1531 	};
1532 	struct InvalidIdentifiers input;
1533 	size_t num_encode;
1534 	uint8_t output[100];
1535 
1536 	input._1one_tstr_present = true;
1537 	input.__present = true;
1538 	input.a_z_tstr_present = true;
1539 
1540 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_InvalidIdentifiers(output,
1541 		sizeof(output), &input, &num_encode), NULL);
1542 	zassert_equal(sizeof(exp_invalid_identifiers_payload1), num_encode, NULL);
1543 	zassert_mem_equal(exp_invalid_identifiers_payload1, output, num_encode, NULL);
1544 }
1545 
1546 
1547 /** Regression test for a bug where some decoding functions were not called
1548  *  because of the union_int optimization */
ZTEST(cbor_encode_test3,test_map_union_prim_alias)1549 ZTEST(cbor_encode_test3, test_map_union_prim_alias)
1550 {
1551 	uint8_t exp_map_union_prim_alias_payload0[] = {MAP(1), 0, 0, END};
1552 	uint8_t exp_map_union_prim_alias_payload1[] = {MAP(1), 1, 1, END};
1553 	uint8_t exp_map_union_prim_alias_payload2[] = {MAP(1), 2, 0xf6, END};
1554 	uint8_t exp_map_union_prim_alias_payload3[] = {MAP(1), 3, 0xf6, END};
1555 	uint8_t exp_map_union_prim_alias_payload4[] = {MAP(1), 4, 0xf6, END};
1556 	uint8_t exp_map_union_prim_alias_payload5[] = {MAP(1), 5, 5, END};
1557 	uint8_t exp_map_union_prim_alias_payload6[] = {MAP(1), 6, 6, END};
1558 	uint8_t payload[10];
1559 
1560 	struct MapUnionPrimAlias input;
1561 	size_t num_encode;
1562 
1563 	input.Union_choice = union_uint0_c;
1564 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapUnionPrimAlias(payload,
1565 		sizeof(payload), &input, &num_encode), NULL);
1566 	zassert_equal(num_encode, sizeof(exp_map_union_prim_alias_payload0));
1567 	zassert_mem_equal(exp_map_union_prim_alias_payload0, payload, num_encode);
1568 
1569 	input.Union_choice = union_uint1int_c;
1570 	input.uint1int = 1;
1571 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapUnionPrimAlias(payload,
1572 		sizeof(payload), &input, &num_encode), NULL);
1573 	zassert_equal(num_encode, sizeof(exp_map_union_prim_alias_payload1));
1574 	zassert_mem_equal(exp_map_union_prim_alias_payload1, payload, num_encode);
1575 
1576 	input.Union_choice = union_uint2nil_c;
1577 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapUnionPrimAlias(payload,
1578 		sizeof(payload), &input, &num_encode), NULL);
1579 	zassert_equal(num_encode, sizeof(exp_map_union_prim_alias_payload2));
1580 	zassert_mem_equal(exp_map_union_prim_alias_payload2, payload, num_encode);
1581 
1582 	input.Union_choice = union_m_nil_m_c;
1583 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapUnionPrimAlias(payload,
1584 		sizeof(payload), &input, &num_encode), NULL);
1585 	zassert_equal(num_encode, sizeof(exp_map_union_prim_alias_payload3));
1586 	zassert_mem_equal(exp_map_union_prim_alias_payload3, payload, num_encode);
1587 
1588 	input.Union_choice = union_mm_nil_m_c;
1589 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapUnionPrimAlias(payload,
1590 		sizeof(payload), &input, &num_encode), NULL);
1591 	zassert_equal(num_encode, sizeof(exp_map_union_prim_alias_payload4));
1592 	zassert_mem_equal(exp_map_union_prim_alias_payload4, payload, num_encode);
1593 
1594 	input.Union_choice = union_m_int_m_c;
1595 	input.m_int_m = 5;
1596 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapUnionPrimAlias(payload,
1597 		sizeof(payload), &input, &num_encode), NULL);
1598 	zassert_equal(num_encode, sizeof(exp_map_union_prim_alias_payload5));
1599 	zassert_mem_equal(exp_map_union_prim_alias_payload5, payload, num_encode);
1600 
1601 	input.Union_choice = union_m_6_m_c;
1602 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_MapUnionPrimAlias(payload,
1603 		sizeof(payload), &input, &num_encode), NULL);
1604 	zassert_equal(num_encode, sizeof(exp_map_union_prim_alias_payload6));
1605 	zassert_mem_equal(exp_map_union_prim_alias_payload6, payload, num_encode);
1606 }
1607 
1608 
ZTEST(cbor_encode_test3,test_empty_group)1609 ZTEST(cbor_encode_test3, test_empty_group)
1610 {
1611 	uint8_t exp_empty_group_payload0[] = {LIST(3), 0, LIST(0), END MAP(0), END END};
1612 	uint8_t payload[10];
1613 
1614 	struct EmptyContainer input;
1615 	size_t num_encode;
1616 
1617 	input.Int = 0;
1618 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_EmptyContainer(payload,
1619 		sizeof(payload), &input, &num_encode), NULL);
1620 	zassert_equal(sizeof(exp_empty_group_payload0), num_encode, NULL);
1621 	zassert_mem_equal(exp_empty_group_payload0, payload, num_encode);
1622 }
1623 
1624 
ZTEST(cbor_encode_test3,test_single_elem_list)1625 ZTEST(cbor_encode_test3, test_single_elem_list)
1626 {
1627 	uint8_t exp_single_elem_list_payload1[] = {LIST(4),
1628 		LIST(1), 0x61, 's', END
1629 		LIST(1), LIST(1), 0, END END
1630 		LIST(1), 0xf5, END
1631 		MAP(1), 1, 0x41, 0x02, END
1632 		END
1633 	};
1634 
1635 	struct SingleElemList input = {
1636 		.tstr.len = 1,
1637 		.tstr.value = &(uint8_t){'s'},
1638 		.MyInt_m = 0,
1639 		.uint1bstr.len = 1,
1640 		.uint1bstr.value = &(uint8_t){2},
1641 	};
1642 	size_t num_encode;
1643 	uint8_t payload[20];
1644 
1645 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_SingleElemList(payload,
1646 		sizeof(payload), &input, &num_encode), NULL);
1647 	zassert_equal(num_encode, sizeof(exp_single_elem_list_payload1));
1648 	zassert_mem_equal(exp_single_elem_list_payload1, payload, num_encode);
1649 }
1650 
1651 
ZTEST(cbor_encode_test3,test_nested_choices)1652 ZTEST(cbor_encode_test3, test_nested_choices)
1653 {
1654 	uint8_t nested_choices_exp_payload1[] = {0xf6};
1655 	uint8_t nested_choices_exp_payload2[] = {LIST(2),
1656 			LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
1657 			LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
1658 		END
1659 	};
1660 	uint8_t nested_choices_exp_payload3[] = {LIST(3),
1661 			LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
1662 			0xf6,
1663 			LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
1664 		END
1665 	};
1666 	uint8_t nested_choices_exp_payload4[] = {LIST(2),
1667 			LIST(1), LIST(1),
1668 				LIST(3),
1669 					LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
1670 					0xf6,
1671 					LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
1672 				END
1673 			END END
1674 			0xf6,
1675 		END
1676 	};
1677 	uint8_t nested_choices_exp_payload5[] = {LIST(2),
1678 			MAP(2),
1679 				0, LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
1680 				1, LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
1681 			END
1682 			0xf6,
1683 		END
1684 	};
1685 	struct Choice1_r input1 = {.Choice1_choice = Choice1_nil_c};
1686 	struct Choice2_r input2 = {.Choice2_choice = Choice2_nil_c};
1687 	struct Choice3_r input3 = {.Choice3_choice = Choice3_nil_c};
1688 	struct Choice4_r input4 = {.Choice4_choice = Choice4_nil_c};
1689 	struct Choice5_r input5 = {.Choice5_choice = Choice5_nil_c};
1690 
1691 	uint8_t payload[50];
1692 
1693 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice1(payload,
1694 		sizeof(payload), &input1, NULL), NULL);
1695 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice2(payload,
1696 		sizeof(payload), &input2, NULL), NULL);
1697 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice3(payload,
1698 		sizeof(payload), &input3, NULL), NULL);
1699 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice4(payload,
1700 		sizeof(payload), &input4, NULL), NULL);
1701 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice5(payload,
1702 		sizeof(payload), &input5, NULL), NULL);
1703 	zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
1704 	zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
1705 	zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
1706 	zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
1707 	zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
1708 
1709 	input1.Choice1_choice = Choice1_tstr_l_l_c;
1710 	input1.tstr_count = 2;
1711 	input1.tstr[0].value = "hello";
1712 	input1.tstr[0].len = 5;
1713 	input1.tstr[1].value = "world";
1714 	input1.tstr[1].len = 5;
1715 
1716 	input2.Choice2_choice = Choice2_tstr_l_l_c;
1717 	input2.tstr_count = 2;
1718 	input2.tstr[0].value = "hello";
1719 	input2.tstr[0].len = 5;
1720 	input2.tstr[1].value = "world";
1721 	input2.tstr[1].len = 5;
1722 
1723 	input3.Choice3_choice = Choice3_union_l_c;
1724 	input3.Union_count = 2;
1725 	input3.Union[0].Union_choice = union_tstr_l_tstr_c;
1726 	input3.Union[1].Union_choice = union_tstr_l_tstr_c;
1727 	input3.Union[0].tstr.value = "hello";
1728 	input3.Union[0].tstr.len = 5;
1729 	input3.Union[1].tstr.value = "world";
1730 	input3.Union[1].tstr.len = 5;
1731 
1732 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice1(payload,
1733 		sizeof(payload), &input1, NULL), NULL);
1734 	zassert_mem_equal(payload, nested_choices_exp_payload2, sizeof(nested_choices_exp_payload2));
1735 
1736 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice2(payload,
1737 		sizeof(payload), &input2, NULL), NULL);
1738 	zassert_mem_equal(payload, nested_choices_exp_payload2, sizeof(nested_choices_exp_payload2));
1739 
1740 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice3(payload,
1741 		sizeof(payload), &input3, NULL), NULL);
1742 	zassert_mem_equal(payload, nested_choices_exp_payload2, sizeof(nested_choices_exp_payload2));
1743 
1744 	input3.Choice3_choice = Choice3_union_l_c;
1745 	input3.Union_count = 3;
1746 	input3.Union[0].Union_choice = union_tstr_l_tstr_c;
1747 	input3.Union[1].Union_choice = Choice3_union_l_union_nil_c;
1748 	input3.Union[2].Union_choice = union_tstr_l_tstr_c;
1749 	input3.Union[0].tstr.value = "hello";
1750 	input3.Union[0].tstr.len = 5;
1751 	input3.Union[2].tstr.value = "world";
1752 	input3.Union[2].tstr.len = 5;
1753 
1754 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice3(payload,
1755 		sizeof(payload), &input3, NULL), NULL);
1756 	zassert_mem_equal(payload, nested_choices_exp_payload3, sizeof(nested_choices_exp_payload3));
1757 
1758 	input4.Choice4_choice = Choice4_union_l_c;
1759 	input4.Union_count = 2;
1760 	input4.Union[0].Union_choice = Choice3_m_l_l_Choice3_m_l_Choice3_m_c;
1761 
1762 	input4.Union[0].Choice3_m.Choice3_choice = Choice3_union_l_c;
1763 	input4.Union[0].Choice3_m.Union_count = 3;
1764 	input4.Union[0].Choice3_m.Union[0].Union_choice = union_tstr_l_tstr_c;
1765 	input4.Union[0].Choice3_m.Union[1].Union_choice = Choice3_union_l_union_nil_c;
1766 	input4.Union[0].Choice3_m.Union[2].Union_choice = union_tstr_l_tstr_c;
1767 	input4.Union[0].Choice3_m.Union[0].tstr.value = "hello";
1768 	input4.Union[0].Choice3_m.Union[0].tstr.len = 5;
1769 	input4.Union[0].Choice3_m.Union[2].tstr.value = "world";
1770 	input4.Union[0].Choice3_m.Union[2].tstr.len = 5;
1771 
1772 	input4.Union[1].Union_choice = Choice4_union_l_union_nil_c;
1773 
1774 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice4(payload,
1775 		sizeof(payload), &input4, NULL), NULL);
1776 
1777 	zassert_mem_equal(payload, nested_choices_exp_payload4, sizeof(nested_choices_exp_payload4));
1778 
1779 	input5.Choice5_choice = Choice5_union_l_c;
1780 	input5.Union_count = 2;
1781 	input5.Union[0].Union_choice = union_map_c;
1782 	input5.Union[0].tstr_l_count = 2;
1783 	input5.Union[0].tstr_l[0].tstr_l_key = 0;
1784 	input5.Union[0].tstr_l[0].tstr.value = "hello";
1785 	input5.Union[0].tstr_l[0].tstr.len = 5;
1786 	input5.Union[0].tstr_l[1].tstr_l_key = 1;
1787 	input5.Union[0].tstr_l[1].tstr.value = "world";
1788 	input5.Union[0].tstr_l[1].tstr.len = 5;
1789 	input5.Union[1].Union_choice = Choice5_union_l_union_nil_c;
1790 
1791 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice5(payload,
1792 		sizeof(payload), &input5, NULL), NULL);
1793 	zassert_mem_equal(payload, nested_choices_exp_payload5, sizeof(nested_choices_exp_payload5));
1794 }
1795 
1796 
ZTEST(cbor_encode_test3,test_optlist)1797 ZTEST(cbor_encode_test3, test_optlist)
1798 {
1799 	uint8_t optlist_exp_payload1[] = {LIST(1),
1800 			10,
1801 		END
1802 	};
1803 	uint8_t optlist_exp_payload2[] = {LIST(2),
1804 			11,
1805 			LIST(1), 0x18, 100, END
1806 		END
1807 	};
1808 	struct OptList input;
1809 	uint8_t payload[20];
1810 
1811 	input.uint_present = false;
1812 	input.optlist_int= 10;
1813 
1814 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_OptList(payload,
1815 		sizeof(payload), &input, NULL));
1816 	zassert_mem_equal(payload, optlist_exp_payload1, sizeof(optlist_exp_payload1));
1817 
1818 	input.uint_present = true;
1819 	input.optlist_int = 11;
1820 	input.uint = 100;
1821 
1822 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_OptList(payload,
1823 		sizeof(payload), &input, NULL));
1824 	zassert_mem_equal(payload, optlist_exp_payload2, sizeof(optlist_exp_payload2));
1825 }
1826 
1827 
1828 
1829 ZTEST_SUITE(cbor_encode_test3, NULL, NULL, NULL, NULL, NULL);
1830