1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include "zcbor_decode.h"
9 #include "zcbor_encode.h"
10 #include "zcbor_print.h"
11 #include <math.h>
12
13
ZTEST(zcbor_unit_tests,test_int64)14 ZTEST(zcbor_unit_tests, test_int64)
15 {
16 uint8_t payload[100] = {0};
17 int64_t int64;
18 int32_t int32;
19
20 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
21 ZCBOR_STATE_D(state_d, 0, payload, sizeof(payload), 10, 0);
22
23 zassert_true(zcbor_int64_put(state_e, 5), NULL);
24 zassert_false(zcbor_int64_expect(state_d, 4), NULL);
25 zassert_false(zcbor_int64_expect(state_d, 6), NULL);
26 zassert_false(zcbor_int64_expect(state_d, -5), NULL);
27 zassert_false(zcbor_int64_expect(state_d, -6), NULL);
28 zassert_true(zcbor_int64_expect(state_d, 5), NULL);
29
30 zassert_true(zcbor_int32_put(state_e, 5), NULL);
31 zassert_true(zcbor_int64_expect(state_d, 5), NULL);
32
33 zassert_true(zcbor_int64_put(state_e, 5), NULL);
34 zassert_true(zcbor_int32_expect(state_d, 5), NULL);
35
36 zassert_true(zcbor_int64_put(state_e, 5000000000), NULL);
37 zassert_false(zcbor_int32_decode(state_d, &int32), NULL);
38 zassert_true(zcbor_int64_decode(state_d, &int64), NULL);
39 zassert_equal(int64, 5000000000, NULL);
40
41 zassert_true(zcbor_int64_encode(state_e, &int64), NULL);
42 zassert_false(zcbor_int64_expect(state_d, 5000000001), NULL);
43 zassert_true(zcbor_int64_expect(state_d, 5000000000), NULL);
44
45 zassert_true(zcbor_int64_put(state_e, 0x80000000), NULL);
46 zassert_false(zcbor_int32_decode(state_d, &int32), NULL);
47 zassert_true(zcbor_uint32_expect(state_d, 0x80000000), NULL);
48
49 zassert_true(zcbor_int32_put(state_e, -505), NULL);
50 zassert_true(zcbor_int64_expect(state_d, -505), NULL);
51
52 zassert_true(zcbor_int64_put(state_e, -5000000000000), NULL);
53 zassert_false(zcbor_int64_expect(state_d, -5000000000001), NULL);
54 zassert_false(zcbor_int64_expect(state_d, -4999999999999), NULL);
55 zassert_false(zcbor_int64_expect(state_d, 5000000000000), NULL);
56 zassert_false(zcbor_int64_expect(state_d, 4999999999999), NULL);
57 zassert_true(zcbor_int64_expect(state_d, -5000000000000), NULL);
58
59 zassert_true(zcbor_uint64_put(state_e, ((uint64_t)INT64_MAX + 1)), NULL);
60 zassert_false(zcbor_int64_decode(state_d, &int64), NULL);
61 zassert_true(zcbor_uint64_expect(state_d, ((uint64_t)INT64_MAX + 1)), NULL);
62
63 }
64
65
ZTEST(zcbor_unit_tests,test_uint64)66 ZTEST(zcbor_unit_tests, test_uint64)
67 {
68 uint8_t payload[100] = {0};
69 uint64_t uint64;
70 uint32_t uint32;
71
72 zcbor_state_t state_e;
73 zcbor_new_state(&state_e, 1, payload, sizeof(payload), 0, NULL, 0);
74 zcbor_state_t state_d;
75 zcbor_new_state(&state_d, 1, payload, sizeof(payload), 10, NULL, 0);
76
77 zassert_true(zcbor_uint64_put(&state_e, 5), NULL);
78 zassert_false(zcbor_uint64_expect(&state_d, 4), NULL);
79 zassert_false(zcbor_uint64_expect(&state_d, 6), NULL);
80 zassert_false(zcbor_uint64_expect(&state_d, -5), NULL);
81 zassert_false(zcbor_uint64_expect(&state_d, -6), NULL);
82 zassert_true(zcbor_uint64_expect(&state_d, 5), NULL);
83
84 zassert_true(zcbor_uint32_put(&state_e, 5), NULL);
85 zassert_true(zcbor_uint64_expect(&state_d, 5), NULL);
86
87 zassert_true(zcbor_uint64_put(&state_e, 5), NULL);
88 zassert_true(zcbor_uint32_expect(&state_d, 5), NULL);
89
90 zassert_true(zcbor_uint64_put(&state_e, UINT64_MAX), NULL);
91 zassert_false(zcbor_uint32_decode(&state_d, &uint32), NULL);
92 zassert_true(zcbor_uint64_decode(&state_d, &uint64), NULL);
93 zassert_equal(uint64, UINT64_MAX, NULL);
94
95 zassert_true(zcbor_uint64_encode(&state_e, &uint64), NULL);
96 zassert_false(zcbor_uint64_expect(&state_d, (UINT64_MAX - 1)), NULL);
97 zassert_true(zcbor_uint64_expect(&state_d, UINT64_MAX), NULL);
98 }
99
ZTEST(zcbor_unit_tests,test_size)100 ZTEST(zcbor_unit_tests, test_size)
101 {
102 uint8_t payload[100] = {0};
103 size_t read;
104
105 zcbor_state_t state_e;
106 zcbor_new_state(&state_e, 1, payload, sizeof(payload), 0, NULL, 0);
107 zcbor_state_t state_d;
108 zcbor_new_state(&state_d, 1, payload, sizeof(payload), 10, NULL, 0);
109
110 zassert_true(zcbor_size_put(&state_e, 5), NULL);
111 zassert_false(zcbor_size_expect(&state_d, 4), NULL);
112 zassert_false(zcbor_size_expect(&state_d, 6), NULL);
113 zassert_false(zcbor_size_expect(&state_d, -5), NULL);
114 zassert_false(zcbor_size_expect(&state_d, -6), NULL);
115 zassert_true(zcbor_size_expect(&state_d, 5), NULL);
116
117 zassert_true(zcbor_int32_put(&state_e, -7), NULL);
118 zassert_false(zcbor_size_expect(&state_d, -7), NULL);
119 zassert_false(zcbor_size_decode(&state_d, &read), NULL); // Negative number not supported.
120 zassert_true(zcbor_int32_expect(&state_d, -7), NULL);
121
122 zassert_true(zcbor_uint32_put(&state_e, 5), NULL);
123 zassert_true(zcbor_size_expect(&state_d, 5), NULL);
124
125 zassert_true(zcbor_uint64_put(&state_e, 5), NULL);
126 zassert_true(zcbor_size_expect(&state_d, 5), NULL);
127
128 zassert_true(zcbor_uint32_put(&state_e, UINT32_MAX), NULL);
129 zassert_true(zcbor_size_decode(&state_d, &read), NULL);
130 zassert_equal(read, UINT32_MAX, NULL);
131
132 #if SIZE_MAX == UINT64_MAX
133 zassert_true(zcbor_uint64_put(&state_e, UINT64_MAX), NULL);
134 zassert_true(zcbor_size_decode(&state_d, &read), NULL);
135 zassert_equal(read, UINT64_MAX, NULL);
136 #endif
137
138 #if SIZE_MAX == UINT32_MAX
139 zassert_true(zcbor_uint64_put(&state_e, UINT64_MAX), NULL);
140 zassert_false(zcbor_size_decode(&state_d, &read), NULL);
141 #endif
142 }
143
144 #if SIZE_MAX == UINT64_MAX
145 /* Only runs on 64-bit builds. */
146 #include <stdlib.h>
147
148 #define PAYL_SIZE 0x100000100
149 #define STR_SIZE 0x100000010
150
ZTEST(zcbor_unit_tests,test_size64)151 ZTEST(zcbor_unit_tests, test_size64)
152 {
153 uint8_t *large_payload = malloc(PAYL_SIZE);
154 uint8_t *large_string = malloc(STR_SIZE);
155 struct zcbor_string tstr = {.value = large_string, .len = STR_SIZE};
156 struct zcbor_string tstr_res;
157
158 for (int i = 0; i < 1000; i++) {
159 large_string[i] = i % 256;
160 large_payload[i + 9] = 0;
161 }
162 for (size_t i = STR_SIZE - 1001; i < STR_SIZE; i++) {
163 large_string[i] = i % 256;
164 large_payload[i + 9] = 0;
165 }
166
167 ZCBOR_STATE_E(state_e, 0, large_payload, PAYL_SIZE, 0);
168 ZCBOR_STATE_D(state_d, 0, large_payload, PAYL_SIZE, 10, 0);
169
170 zassert_true(zcbor_tstr_encode(state_e, &tstr), NULL);
171 zassert_false(zcbor_bstr_decode(state_d, &tstr_res), NULL);
172 zassert_true(zcbor_tstr_decode(state_d, &tstr_res), NULL);
173 zassert_equal(tstr_res.len, tstr.len, NULL);
174 zassert_equal_ptr(tstr_res.value, &large_payload[9], NULL);
175 zassert_mem_equal(tstr_res.value, large_string, tstr.len, NULL);
176 }
177
178
179 #else
ZTEST(zcbor_unit_tests,test_size64)180 ZTEST(zcbor_unit_tests, test_size64)
181 {
182 printf("Skip on non-64-bit builds.\n");
183 }
184 #endif
185
186
ZTEST(zcbor_unit_tests,test_string_macros)187 ZTEST(zcbor_unit_tests, test_string_macros)
188 {
189 uint8_t payload[100];
190 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
191 char world[] = {'w', 'o', 'r', 'l', 'd'};
192
193 zassert_true(zcbor_bstr_put_lit(state_e, "Hello"), NULL);
194 zassert_true(zcbor_bstr_put_term(state_e, "Hello world", 5), NULL); /* Check that strnlen cuts off at 5 */
195 zassert_true(zcbor_bstr_put_arr(state_e, world), NULL);
196 zassert_true(zcbor_tstr_put_lit(state_e, "Hello"), NULL);
197 zassert_true(zcbor_tstr_put_term(state_e, "Hellooooo", 5), NULL); /* Check that strnlen cuts off at 5 */
198 zassert_true(zcbor_tstr_put_arr(state_e, world), NULL);
199
200 ZCBOR_STATE_D(state_d, 0, payload, sizeof(payload), 6, 0);
201
202 zassert_false(zcbor_bstr_expect_lit(state_d, "Yello"), NULL);
203 zassert_false(zcbor_tstr_expect_lit(state_d, "Hello"), NULL);
204 zassert_true(zcbor_bstr_expect_lit(state_d, "Hello"), NULL);
205 zassert_false(zcbor_bstr_expect_term(state_d, "Hello!", 10), NULL);
206 zassert_false(zcbor_bstr_expect_term(state_d, "Hello", 4), NULL); /* Check that strnlen cuts off at 4 */
207 zassert_true(zcbor_bstr_expect_term(state_d, "Hello", 5), NULL);
208 world[3]++;
209 zassert_false(zcbor_bstr_expect_arr(state_d, world), NULL);
210 world[3]--;
211 zassert_true(zcbor_bstr_expect_arr(state_d, world), NULL);
212 zassert_false(zcbor_tstr_expect_lit(state_d, "hello"), NULL);
213 zassert_true(zcbor_tstr_expect_lit(state_d, "Hello"), NULL);
214 zassert_false(zcbor_tstr_expect_term(state_d, "Helo", 10), NULL);
215 zassert_false(zcbor_tstr_expect_term(state_d, "Hello", 0), NULL);
216 zassert_true(zcbor_tstr_expect_term(state_d, "Hello", 10), NULL);
217 world[2]++;
218 zassert_false(zcbor_tstr_expect_arr(state_d, world), NULL);
219 world[2]--;
220 zassert_false(zcbor_bstr_expect_arr(state_d, world), NULL);
221 zassert_true(zcbor_tstr_expect_arr(state_d, world), NULL);
222 }
223
224
ZTEST(zcbor_unit_tests,test_stop_on_error)225 ZTEST(zcbor_unit_tests, test_stop_on_error)
226 {
227 uint8_t payload[100];
228 ZCBOR_STATE_E(state_e, 3, payload, sizeof(payload), 0);
229 struct zcbor_string failing_string = {.value = NULL, .len = 1000};
230 struct zcbor_string dummy_string;
231 zcbor_state_t state_backup;
232 struct zcbor_state_constant constant_state_backup;
233
234 state_e->constant_state->stop_on_error = true;
235
236 zassert_false(zcbor_tstr_encode(state_e, &failing_string), NULL);
237 zassert_equal(ZCBOR_ERR_NO_PAYLOAD, state_e->constant_state->error, "%d\r\n", state_e->constant_state->error);
238 memcpy(&state_backup, state_e, sizeof(state_backup));
239 memcpy(&constant_state_backup, state_e->constant_state, sizeof(constant_state_backup));
240
241 /* All fail because of ZCBOR_ERR_NO_PAYLOAD */
242 zassert_false(zcbor_int32_put(state_e, 1), NULL);
243 zassert_false(zcbor_int64_put(state_e, 2), NULL);
244 zassert_false(zcbor_uint32_put(state_e, 3), NULL);
245 zassert_false(zcbor_uint64_put(state_e, 4), NULL);
246 zassert_false(zcbor_size_put(state_e, 10), NULL);
247 zassert_false(zcbor_int32_encode(state_e, &(int32_t){5}), NULL);
248 zassert_false(zcbor_int64_encode(state_e, &(int64_t){6}), NULL);
249 zassert_false(zcbor_uint32_encode(state_e, &(uint32_t){7}), NULL);
250 zassert_false(zcbor_uint64_encode(state_e, &(uint64_t){8}), NULL);
251 zassert_false(zcbor_size_encode(state_e, &(size_t){9}), NULL);
252 zassert_false(zcbor_bstr_put_lit(state_e, "Hello"), NULL);
253 zassert_false(zcbor_tstr_put_lit(state_e, "World"), NULL);
254 zassert_false(zcbor_tag_put(state_e, 9), NULL);
255 zassert_false(zcbor_tag_encode(state_e, &(uint32_t){10}));
256 zassert_false(zcbor_bool_put(state_e, true), NULL);
257 zassert_false(zcbor_bool_encode(state_e, &(bool){false}), NULL);
258 zassert_false(zcbor_float32_put(state_e, 10.5), NULL);
259 zassert_false(zcbor_float32_encode(state_e, &(float){11.6}), NULL);
260 zassert_false(zcbor_float64_put(state_e, 12.7), NULL);
261 zassert_false(zcbor_float64_encode(state_e, &(double){13.8}), NULL);
262 zassert_false(zcbor_nil_put(state_e, NULL), NULL);
263 zassert_false(zcbor_undefined_put(state_e, NULL), NULL);
264 zassert_false(zcbor_bstr_start_encode(state_e), NULL);
265 zassert_false(zcbor_bstr_end_encode(state_e, NULL), NULL);
266 zassert_false(zcbor_list_start_encode(state_e, 1), NULL);
267 zassert_false(zcbor_map_start_encode(state_e, 0), NULL);
268 zassert_false(zcbor_map_end_encode(state_e, 0), NULL);
269 zassert_false(zcbor_list_end_encode(state_e, 1), NULL);
270 zassert_false(zcbor_multi_encode(1, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)14, 0), NULL);
271 zassert_false(zcbor_multi_encode_minmax(1, 1, &(size_t){1}, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)15, 0), NULL);
272
273
274 zassert_mem_equal(&state_backup, state_e, sizeof(state_backup), NULL);
275 zassert_mem_equal(&constant_state_backup, state_e->constant_state, sizeof(constant_state_backup), NULL);
276
277 zassert_equal(ZCBOR_ERR_NO_PAYLOAD, zcbor_peek_error(state_e), NULL);
278 zassert_equal(ZCBOR_ERR_NO_PAYLOAD, zcbor_pop_error(state_e), NULL);
279 zassert_equal(ZCBOR_SUCCESS, zcbor_peek_error(state_e), NULL);
280
281 /* All succeed since the error has been popped. */
282 zassert_true(zcbor_int32_put(state_e, 1), NULL);
283 zassert_true(zcbor_int64_put(state_e, 2), NULL);
284 zassert_true(zcbor_uint32_put(state_e, 3), NULL);
285 zassert_true(zcbor_uint64_put(state_e, 4), NULL);
286 zassert_true(zcbor_size_put(state_e, 10), NULL);
287 zassert_true(zcbor_int32_encode(state_e, &(int32_t){5}), NULL);
288 zassert_true(zcbor_int64_encode(state_e, &(int64_t){6}), NULL);
289 zassert_true(zcbor_uint32_encode(state_e, &(uint32_t){7}), NULL);
290 zassert_true(zcbor_uint64_encode(state_e, &(uint64_t){8}), NULL);
291 zassert_true(zcbor_size_encode(state_e, &(size_t){9}), NULL);
292 zassert_true(zcbor_bstr_put_lit(state_e, "Hello"), NULL);
293 zassert_true(zcbor_tstr_put_lit(state_e, "World"), NULL);
294 zassert_true(zcbor_tag_put(state_e, 9), NULL);
295 zassert_true(zcbor_tag_encode(state_e, &(uint32_t){10}));
296 zassert_true(zcbor_bool_put(state_e, true), NULL);
297 zassert_true(zcbor_bool_encode(state_e, &(bool){false}), NULL);
298 zassert_true(zcbor_float32_put(state_e, 10.5), NULL);
299 zassert_true(zcbor_float32_encode(state_e, &(float){11.6}), NULL);
300 zassert_true(zcbor_float64_put(state_e, 12.7), NULL);
301 zassert_true(zcbor_float64_encode(state_e, &(double){13.8}), NULL);
302 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
303 zassert_true(zcbor_undefined_put(state_e, NULL), NULL);
304 zassert_true(zcbor_bstr_start_encode(state_e), NULL);
305 zassert_true(zcbor_bstr_end_encode(state_e, NULL), NULL);
306 zassert_true(zcbor_list_start_encode(state_e, 1), NULL);
307 zassert_true(zcbor_map_start_encode(state_e, 0), NULL);
308 zassert_true(zcbor_map_end_encode(state_e, 0), NULL);
309 zassert_true(zcbor_list_end_encode(state_e, 1), NULL);
310 zassert_true(zcbor_multi_encode(1, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)14, 0), NULL);
311 zassert_true(zcbor_multi_encode_minmax(1, 1, &(size_t){1}, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)15, 0), NULL);
312
313 ZCBOR_STATE_D(state_d, 3, payload, sizeof(payload), 30, 0);
314 state_d->constant_state->stop_on_error = true;
315
316 zassert_false(zcbor_int32_expect(state_d, 2), NULL);
317 zassert_equal(ZCBOR_ERR_WRONG_VALUE, state_d->constant_state->error, "%d\r\n", state_d->constant_state->error);
318 memcpy(&state_backup, state_d, sizeof(state_backup));
319 memcpy(&constant_state_backup, state_d->constant_state, sizeof(constant_state_backup));
320
321 /* All fail because of ZCBOR_ERR_WRONG_VALUE */
322 zassert_false(zcbor_int32_expect(state_d, 1), NULL);
323 zassert_false(zcbor_int64_expect(state_d, 2), NULL);
324 zassert_false(zcbor_uint32_expect(state_d, 3), NULL);
325 zassert_false(zcbor_uint64_expect(state_d, 4), NULL);
326 zassert_false(zcbor_size_expect(state_d, 10), NULL);
327 zassert_false(zcbor_int32_decode(state_d, &(int32_t){5}), NULL);
328 zassert_false(zcbor_int64_decode(state_d, &(int64_t){6}), NULL);
329 zassert_false(zcbor_uint32_decode(state_d, &(uint32_t){7}), NULL);
330 zassert_false(zcbor_uint64_decode(state_d, &(uint64_t){8}), NULL);
331 zassert_false(zcbor_size_decode(state_d, &(size_t){9}), NULL);
332 zassert_false(zcbor_bstr_expect_lit(state_d, "Hello"), NULL);
333 zassert_false(zcbor_tstr_expect_lit(state_d, "World"), NULL);
334 zassert_false(zcbor_tag_decode(state_d, &(uint32_t){9}), NULL);
335 zassert_false(zcbor_tag_expect(state_d, 10), NULL);
336 zassert_false(zcbor_bool_expect(state_d, true), NULL);
337 zassert_false(zcbor_bool_decode(state_d, &(bool){false}), NULL);
338 zassert_false(zcbor_float32_expect(state_d, 10.5), NULL);
339 zassert_false(zcbor_float32_decode(state_d, &(float){11.6}), NULL);
340 zassert_false(zcbor_float64_expect(state_d, 12.7), NULL);
341 zassert_false(zcbor_float64_decode(state_d, &(double){13.8}), NULL);
342 zassert_false(zcbor_nil_expect(state_d, NULL), NULL);
343 zassert_false(zcbor_undefined_expect(state_d, NULL), NULL);
344 zassert_false(zcbor_bstr_start_decode(state_d, &dummy_string), NULL);
345 zassert_false(zcbor_bstr_end_decode(state_d), NULL);
346 zassert_false(zcbor_list_start_decode(state_d), NULL);
347 zassert_false(zcbor_map_start_decode(state_d), NULL);
348 zassert_false(zcbor_map_end_decode(state_d), NULL);
349 zassert_false(zcbor_list_end_decode(state_d), NULL);
350 zassert_false(zcbor_multi_decode(1, 1, &(size_t){1}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)14, 0), NULL);
351 zassert_false(zcbor_present_decode(&(bool){true}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)15), NULL);
352
353 zassert_mem_equal(&state_backup, state_d, sizeof(state_backup), NULL);
354 zassert_mem_equal(&constant_state_backup, state_d->constant_state, sizeof(constant_state_backup), NULL);
355
356 zassert_equal(ZCBOR_ERR_WRONG_VALUE, zcbor_pop_error(state_d), NULL);
357
358 /* All succeed since the error has been popped. */
359 zassert_true(zcbor_int32_expect(state_d, 1), NULL);
360 zassert_true(zcbor_int64_expect(state_d, 2), NULL);
361 zassert_true(zcbor_uint32_expect(state_d, 3), NULL);
362 zassert_true(zcbor_uint64_expect(state_d, 4), NULL);
363 zassert_true(zcbor_size_expect(state_d, 10), NULL);
364 zassert_true(zcbor_int32_decode(state_d, &(int32_t){5}), NULL);
365 zassert_true(zcbor_int64_decode(state_d, &(int64_t){6}), NULL);
366 zassert_true(zcbor_uint32_decode(state_d, &(uint32_t){7}), NULL);
367 zassert_true(zcbor_uint64_decode(state_d, &(uint64_t){8}), NULL);
368 zassert_true(zcbor_size_decode(state_d, &(size_t){9}), NULL);
369 zassert_true(zcbor_bstr_expect_lit(state_d, "Hello"), NULL);
370 zassert_true(zcbor_tstr_expect_lit(state_d, "World"), NULL);
371 zassert_true(zcbor_tag_decode(state_d, &(uint32_t){9}), NULL);
372 zassert_true(zcbor_tag_expect(state_d, 10), NULL);
373 zassert_true(zcbor_bool_expect(state_d, true), NULL);
374 zassert_true(zcbor_bool_decode(state_d, &(bool){false}), NULL);
375 zassert_true(zcbor_float32_expect(state_d, 10.5), NULL);
376 zassert_true(zcbor_float32_decode(state_d, &(float){11.6}), NULL);
377 zassert_true(zcbor_float64_expect(state_d, 12.7), NULL);
378 zassert_true(zcbor_float64_decode(state_d, &(double){13.8}), NULL);
379 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
380 zassert_true(zcbor_undefined_expect(state_d, NULL), NULL);
381 zassert_true(zcbor_bstr_start_decode(state_d, &dummy_string), NULL);
382 zassert_true(zcbor_bstr_end_decode(state_d), NULL);
383 zassert_true(zcbor_list_start_decode(state_d), NULL);
384 zassert_true(zcbor_map_start_decode(state_d), NULL);
385 zassert_true(zcbor_map_end_decode(state_d), NULL);
386 zassert_true(zcbor_list_end_decode(state_d), NULL);
387 zassert_true(zcbor_multi_decode(1, 1, &(size_t){1}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)14, 0), NULL);
388 zassert_true(zcbor_present_decode(&(bool){1}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)15), NULL);
389
390 /* Everything has been decoded. */
391 zassert_equal(state_e->payload, state_d->payload, NULL);
392 }
393
394
395 /** The string "HelloWorld" is split into 2 fragments, and a number of different
396 * functions are called to test that they respond correctly to the situation.
397 **/
ZTEST(zcbor_unit_tests,test_fragments)398 ZTEST(zcbor_unit_tests, test_fragments)
399 {
400 uint8_t payload[100];
401 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
402 struct zcbor_string output;
403 struct zcbor_string_fragment output_frags[2];
404
405 zassert_true(zcbor_bstr_put_lit(state_e, "HelloWorld"), NULL);
406
407 ZCBOR_STATE_D(state_d, 0, payload, 8, 1, 0);
408 ZCBOR_STATE_D(state_d2, 0, payload, sizeof(payload), 1, 0);
409
410 zassert_true(zcbor_bstr_decode(state_d2, &output), NULL);
411 zassert_false(zcbor_payload_at_end(state_d2), NULL);
412 zassert_false(zcbor_bstr_decode(state_d, &output), NULL);
413 zassert_false(zcbor_payload_at_end(state_d), NULL);
414 zassert_true(zcbor_bstr_decode_fragment(state_d, &output_frags[0]), NULL);
415 zassert_equal_ptr(&payload[1], output_frags[0].fragment.value, NULL);
416 zassert_equal(7, output_frags[0].fragment.len, NULL);
417 zassert_equal(10, output_frags[0].total_len, "%d != %d\r\n", 10, output_frags[0].total_len);
418 zassert_equal(0, output_frags[0].offset, NULL);
419 zassert_false(zcbor_is_last_fragment(&output_frags[0]), NULL);
420
421 zassert_true(zcbor_payload_at_end(state_d), NULL);
422 zcbor_update_state(state_d, &payload[8], sizeof(payload) - 8);
423 zassert_false(zcbor_bstr_decode_fragment(state_d, &output_frags[1]), NULL);
424 zcbor_next_fragment(state_d, &output_frags[0], &output_frags[1]);
425 zassert_equal_ptr(&payload[8], output_frags[1].fragment.value, NULL);
426 zassert_equal(3, output_frags[1].fragment.len, "%d != %d\r\n", 3, output_frags[1].fragment.len);
427 zassert_equal(10, output_frags[1].total_len, NULL);
428 zassert_equal(7, output_frags[1].offset, NULL);
429 zassert_true(zcbor_is_last_fragment(&output_frags[1]), NULL);
430
431 uint8_t spliced[11];
432 output.value = spliced;
433 output.len = sizeof(spliced);
434
435 zassert_true(zcbor_validate_string_fragments(output_frags, 2), NULL);
436 zassert_true(zcbor_splice_string_fragments(output_frags, 2, spliced, &output.len), NULL);
437
438 zassert_equal(10, output.len, NULL);
439 zassert_mem_equal(output.value, "HelloWorld", 10, NULL);
440 }
441
442
443 /** The long string "HelloWorld1HelloWorld2..." is split into 18 fragments.
444 *
445 * First, zcbor_validate_string_fragments() is checked to be true, then various
446 * errors are introduced one by one and zcbor_validate_string_fragments() is
447 * checked to be false for all of them in turn.
448 */
ZTEST(zcbor_unit_tests,test_validate_fragments)449 ZTEST(zcbor_unit_tests, test_validate_fragments)
450 {
451 uint8_t payload[200];
452 uint8_t frag_payload[15];
453
454 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
455 struct zcbor_string output;
456 struct zcbor_string_fragment output_frags[18];
457 struct zcbor_string_fragment output_frags_backup;
458
459 // Start positive test
460
461 zassert_true(zcbor_bstr_put_lit(state_e,
462 "HelloWorld1HelloWorld2HelloWorld3HelloWorld4HelloWorld5HelloWorld6" \
463 "HelloWorld7HelloWorld8HelloWorld9HelloWorldAHelloWorldBHelloWorldC" \
464 "HelloWorldDHelloWorldEHelloWorldFHelloWorldGHelloWorldHHelloWorldI"),
465 NULL);
466
467 ZCBOR_STATE_D(state_d, 0, payload, 13, 1, 0);
468 ZCBOR_STATE_D(state_d2, 0, payload, sizeof(payload), 1, 0);
469
470 zassert_true(zcbor_bstr_decode(state_d2, &output), NULL);
471 zassert_true(zcbor_bstr_decode_fragment(state_d, &output_frags[0]), NULL);
472
473 for (int i = 1; i < 18; i++) {
474 zassert_true(zcbor_payload_at_end(state_d), NULL);
475 zassert_false(zcbor_is_last_fragment(&output_frags[i - 1]), NULL);
476 memcpy(frag_payload, &payload[11 * i + 2], 11); // + 2 because of the CBOR header
477 zcbor_update_state(state_d, frag_payload, 11);
478 zcbor_next_fragment(state_d, &output_frags[i - 1], &output_frags[i]);
479 }
480 zassert_true(zcbor_payload_at_end(state_d), NULL);
481 zassert_true(zcbor_is_last_fragment(&output_frags[17]), NULL);
482
483 uint8_t spliced[200];
484 size_t out_len = sizeof(spliced);
485
486 zassert_true(zcbor_validate_string_fragments(output_frags, 18), NULL);
487 zassert_true(zcbor_splice_string_fragments(output_frags, 18, spliced,
488 &out_len), NULL);
489
490 zassert_equal(198, output.len, NULL);
491 zassert_mem_equal(output.value,
492 "HelloWorld1HelloWorld2HelloWorld3HelloWorld4HelloWorld5HelloWorld6" \
493 "HelloWorld7HelloWorld8HelloWorld9HelloWorldAHelloWorldBHelloWorldC" \
494 "HelloWorldDHelloWorldEHelloWorldFHelloWorldGHelloWorldHHelloWorldI",
495 198, NULL);
496
497 // Start negative tests
498
499 zassert_false(zcbor_validate_string_fragments(output_frags, 17), NULL); // Last fragment missing
500
501 output_frags[2].total_len--;
502 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Mismatch total_len
503 output_frags[2].total_len += 2;
504 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Mismatch total_len
505 output_frags[2].total_len--;
506
507 memcpy(&output_frags_backup, &output_frags[16], sizeof(output_frags[0]));
508 memcpy(&output_frags[16], &output_frags[17], sizeof(output_frags[0])); // Move last fragment
509
510 zassert_false(zcbor_validate_string_fragments(output_frags, 17), NULL); // Second-to-last fragment missing
511 memcpy(&output_frags[16], &output_frags_backup, sizeof(output_frags[0])); // Restore
512
513 output_frags[6].offset++;
514 output_frags[7].offset--;
515 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Slight error in offset vs. fragment lengths.
516 output_frags[6].offset--;
517 output_frags[7].offset++;
518
519 for (int i = 0; i < 18; i++) {
520 output_frags[i].total_len -= output_frags[17].fragment.len;
521 }
522
523 zassert_true(zcbor_validate_string_fragments(output_frags, 17), NULL); // Should work with 17 fragments now.
524 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Last fragment will extend past total_len.
525
526 for (int i = 0; i < 18; i++) {
527 output_frags[i].total_len += (output_frags[17].fragment.len - 1);
528 }
529 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Last fragment will extend past total_len by a single byte.
530 output_frags[17].fragment.len--;
531 zassert_true(zcbor_validate_string_fragments(output_frags, 18), NULL); // Should work with shorter last fragment
532 output_frags[17].fragment.len++; // Restore
533 for (int i = 0; i < 18; i++) {
534 output_frags[i].total_len += 1; // Restore
535 }
536
537 zassert_true(zcbor_validate_string_fragments(output_frags, 18), NULL); // Check that all errors were restored correctly.
538 }
539
540
541 /** This test creates the following structure, wrapped in a BSTR:
542 *
543 * (
544 * 42,
545 * "Hello World",
546 * [
547 * true,
548 * nil,
549 * ]
550 * )
551 *
552 * This structures is then split in three fragments (output_frags) like so:
553 * 1st fragment (8 bytes):
554 * (
555 * 42,
556 * "Hell
557 *
558 * 2nd fragment (8 bytes):
559 * o World",
560 * [
561 *
562 * 3rd fragment (3 bytes or 2 bytes if ZCBOR_CANONICAL is defined):
563 * true,
564 * nil,
565 * ]
566 * )
567 *
568 * This means that the TSTR has to be handled using fragments (tstr_frags)
569 * as well.
570 */
ZTEST(zcbor_unit_tests,test_bstr_cbor_fragments)571 ZTEST(zcbor_unit_tests, test_bstr_cbor_fragments)
572 {
573 uint8_t payload[100];
574 ZCBOR_STATE_E(state_e, 2, payload, sizeof(payload), 0);
575 struct zcbor_string output;
576 struct zcbor_string_fragment output_frags[3];
577 struct zcbor_string_fragment tstr_frags[2];
578
579 zassert_true(zcbor_bstr_start_encode(state_e), NULL); // 1 B
580 zassert_true(zcbor_uint32_put(state_e, 42), NULL); // 2 B
581 zassert_true(zcbor_tstr_put_lit(state_e, "Hello World"), NULL); // 12 B
582 zassert_true(zcbor_list_start_encode(state_e, 2), NULL); // 1 B
583 zassert_true(zcbor_bool_put(state_e, true), NULL); // 1 B
584 zassert_true(zcbor_nil_put(state_e, NULL), NULL); // 1 B
585 zassert_true(zcbor_list_end_encode(state_e, 2), NULL); // 1 B
586 zassert_true(zcbor_bstr_end_encode(state_e, NULL), NULL); // 0 B
587
588 ZCBOR_STATE_D(state_d, 2, payload, 8, 1, 0);
589 ZCBOR_STATE_D(state_d2, 0, payload, sizeof(payload), 1, 0);
590
591 #ifdef ZCBOR_CANONICAL
592 #define EXP_TOTAL_LEN 17
593 #else
594 #define EXP_TOTAL_LEN 18
595 #endif
596
597 zassert_true(zcbor_bstr_decode(state_d2, &output), NULL);
598 zassert_false(zcbor_bstr_start_decode(state_d, &output), NULL);
599 zassert_true(zcbor_bstr_start_decode_fragment(state_d, &output_frags[0]), NULL);
600 zassert_equal_ptr(&payload[1], output_frags[0].fragment.value, NULL);
601 zassert_equal(7, output_frags[0].fragment.len, NULL);
602 zassert_equal(EXP_TOTAL_LEN, output_frags[0].total_len, "%d != %d\r\n", EXP_TOTAL_LEN, output_frags[0].total_len);
603 zassert_equal(0, output_frags[0].offset, NULL);
604 zassert_false(zcbor_is_last_fragment(&output_frags[0]), NULL);
605 zassert_true(zcbor_uint32_expect(state_d, 42), NULL);
606 zassert_false(zcbor_tstr_expect_lit(state_d, "Hello World"), NULL);
607 zassert_true(zcbor_tstr_decode_fragment(state_d, &tstr_frags[0]), NULL);
608 zassert_equal_ptr(&payload[4], tstr_frags[0].fragment.value, NULL);
609 zassert_equal(4, tstr_frags[0].fragment.len, NULL);
610 zassert_equal(11, tstr_frags[0].total_len, NULL);
611 zassert_equal(0, tstr_frags[0].offset, NULL);
612
613 zassert_true(zcbor_payload_at_end(state_d), NULL);
614 zcbor_update_state(state_d, &payload[8], 8);
615 zassert_false(zcbor_bstr_decode_fragment(state_d, &output_frags[1]), NULL);
616 zcbor_bstr_next_fragment(state_d, &output_frags[0], &output_frags[1]);
617 zassert_equal_ptr(&payload[8], output_frags[1].fragment.value, NULL);
618 zassert_equal(8, output_frags[1].fragment.len, "%d != %d\r\n", 3, output_frags[1].fragment.len);
619 zassert_equal(EXP_TOTAL_LEN, output_frags[1].total_len, "%d != %d\r\n", EXP_TOTAL_LEN, output_frags[1].total_len);
620 zassert_equal(7, output_frags[1].offset, NULL);
621 zassert_false(zcbor_is_last_fragment(&output_frags[1]), NULL);
622 zcbor_next_fragment(state_d, &tstr_frags[0], &tstr_frags[1]);
623 zassert_equal_ptr(&payload[8], tstr_frags[1].fragment.value, NULL);
624 zassert_equal(7, tstr_frags[1].fragment.len, "%d != %d\r\n", 7, tstr_frags[1].fragment.len);
625 zassert_equal(11, tstr_frags[1].total_len, NULL);
626 zassert_equal(4, tstr_frags[1].offset, NULL);
627 zassert_true(zcbor_is_last_fragment(&tstr_frags[1]), NULL);
628 zassert_true(zcbor_list_start_decode(state_d), NULL);
629
630 zassert_true(zcbor_payload_at_end(state_d), NULL);
631 zcbor_update_state(state_d, &payload[16], sizeof(payload) - 16);
632 zassert_false(zcbor_bstr_decode_fragment(state_d, &output_frags[2]), NULL);
633 zcbor_bstr_next_fragment(state_d, &output_frags[1], &output_frags[2]);
634 zassert_equal_ptr(&payload[16], output_frags[2].fragment.value, NULL);
635 zassert_equal(EXP_TOTAL_LEN - 15,
636 output_frags[2].fragment.len, NULL);
637 zassert_equal(EXP_TOTAL_LEN, output_frags[2].total_len, NULL);
638 zassert_equal(15, output_frags[2].offset, NULL);
639 zassert_true(zcbor_is_last_fragment(&output_frags[2]), NULL);
640 zassert_true(zcbor_bool_expect(state_d, true), NULL);
641 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
642 zassert_true(zcbor_list_end_decode(state_d), NULL);
643
644 uint8_t spliced[19];
645 output.value = spliced;
646 output.len = sizeof(spliced);
647
648 zassert_true(zcbor_validate_string_fragments(output_frags, 3), NULL);
649 zassert_true(zcbor_splice_string_fragments(output_frags, 3, spliced, &output.len), NULL);
650
651 zassert_equal(EXP_TOTAL_LEN, output.len, NULL);
652 zassert_mem_equal(output.value, &payload[1], EXP_TOTAL_LEN, NULL);
653
654 output.len = sizeof(spliced);
655
656 zassert_true(zcbor_validate_string_fragments(tstr_frags, 2), NULL);
657 zassert_true(zcbor_splice_string_fragments(tstr_frags, 2, spliced, &output.len), NULL);
658
659 zassert_equal(11, output.len, NULL);
660 zassert_mem_equal(output.value, &payload[4], 11, NULL);
661 }
662
663
ZTEST(zcbor_unit_tests,test_canonical_list)664 ZTEST(zcbor_unit_tests, test_canonical_list)
665 {
666 #ifndef ZCBOR_CANONICAL
667 printf("Skip on non-canonical builds.\n");
668 #else
669 uint8_t payload1[100];
670 uint8_t payload2[100];
671 uint8_t exp_payload[] = {0x8A, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
672 ZCBOR_STATE_E(state_e1, 1, payload1, sizeof(payload1), 0);
673 ZCBOR_STATE_E(state_e2, 1, payload2, sizeof(payload2), 0);
674
675 zassert_true(zcbor_list_start_encode(state_e1, 10), "%d\r\n", state_e1->constant_state->error);
676 for (int i = 0; i < 30; i++) {
677 zassert_true(zcbor_uint32_put(state_e1, i), NULL);
678 }
679 zassert_false(zcbor_list_end_encode(state_e1, 10), NULL);
680 zassert_equal(ZCBOR_ERR_HIGH_ELEM_COUNT, zcbor_pop_error(state_e1), NULL);
681
682 zassert_true(zcbor_list_start_encode(state_e2, 1000), NULL);
683 for (int i = 0; i < 10; i++) {
684 zassert_true(zcbor_uint32_put(state_e2, i), NULL);
685 }
686 zassert_true(zcbor_list_end_encode(state_e2, 1000), NULL);
687 zassert_equal(sizeof(exp_payload), state_e2->payload - payload2, NULL);
688 zassert_mem_equal(exp_payload, payload2, sizeof(exp_payload), NULL);
689 #endif
690 }
691
692
ZTEST(zcbor_unit_tests,test_int)693 ZTEST(zcbor_unit_tests, test_int)
694 {
695 uint8_t payload1[100];
696 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
697 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16, 0);
698
699 /* Encode all numbers */
700 /* Arbitrary positive numbers in each size */
701 int8_t int8 = 12;
702 int16_t int16 = 1234;
703 int32_t int32 = 12345678;
704 int64_t int64 = 1234567812345678;
705
706 zassert_true(zcbor_int_encode(state_e, &int8, sizeof(int8)), NULL);
707 zassert_true(zcbor_int_encode(state_e, &int16, sizeof(int16)), NULL);
708 zassert_true(zcbor_int_encode(state_e, &int32, sizeof(int32)), NULL);
709 zassert_true(zcbor_int_encode(state_e, &int64, sizeof(int64)), NULL);
710
711 /* Arbitrary negative numbers in each size */
712 int8 = -12;
713 int16 = -1234;
714 int32 = -12345678;
715 int64 = -1234567812345678;
716
717 zassert_true(zcbor_int_encode(state_e, &int8, sizeof(int8)), NULL);
718 zassert_true(zcbor_int_encode(state_e, &int16, sizeof(int16)), NULL);
719 zassert_true(zcbor_int_encode(state_e, &int32, sizeof(int32)), NULL);
720 zassert_true(zcbor_int_encode(state_e, &int64, sizeof(int64)), NULL);
721
722 /* Check against overflow (negative). */
723 zassert_true(zcbor_int32_put(state_e, INT8_MIN - 1), NULL);
724 zassert_true(zcbor_int32_put(state_e, INT16_MIN - 1), NULL);
725 zassert_true(zcbor_int64_put(state_e, (int64_t)INT32_MIN - 1), NULL);
726 /* Check absolute minimum number. */
727 zassert_true(zcbor_int64_put(state_e, INT64_MIN), NULL);
728
729 /* Check against overflow (positive). */
730 zassert_true(zcbor_int32_put(state_e, INT8_MAX + 1), NULL);
731 zassert_true(zcbor_int32_put(state_e, INT16_MAX + 1), NULL);
732 zassert_true(zcbor_int64_put(state_e, (int64_t)INT32_MAX + 1), NULL);
733 /* Check absolute maximum number. */
734 zassert_true(zcbor_int64_put(state_e, INT64_MAX), NULL);
735
736 /* Decode all numbers */
737 /* Arbitrary positive numbers in each size */
738 zassert_true(zcbor_int_decode(state_d, &int8, sizeof(int8)), NULL);
739 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
740 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
741 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
742 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
743 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
744 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
745 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
746 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
747 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
748
749 zassert_equal(int8, 12, "%d\r\n", int8);
750 zassert_equal(int16, 1234, "%d\r\n", int16);
751 zassert_equal(int32, 12345678, "%d\r\n", int32);
752 zassert_equal(int64, 1234567812345678, "%d\r\n", int64);
753
754 /* Arbitrary negative numbers in each size */
755 zassert_true(zcbor_int_decode(state_d, &int8, sizeof(int8)), NULL);
756 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
757 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
758 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
759 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
760 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
761 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
762 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
763 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
764 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
765
766 zassert_equal(int8, -12, NULL);
767 zassert_equal(int16, -1234, NULL);
768 zassert_equal(int32, -12345678, NULL);
769 zassert_equal(int64, -1234567812345678, NULL);
770
771 /* Check against overflow (negative). */
772 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
773 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
774 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
775 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
776 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
777 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
778 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
779 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
780 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
781
782 zassert_equal(int16, INT8_MIN - 1, NULL);
783 zassert_equal(int32, INT16_MIN - 1, NULL);
784 zassert_equal(int64, (int64_t)INT32_MIN - 1, NULL);
785
786 /* Check absolute minimum number. */
787 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
788
789 zassert_equal(int64, INT64_MIN, NULL);
790
791 /* Check against overflow (positive). */
792 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
793 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
794 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
795 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
796 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
797 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
798 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
799 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
800 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
801
802 zassert_equal(int16, INT8_MAX + 1, NULL);
803 zassert_equal(int32, INT16_MAX + 1, NULL);
804 zassert_equal(int64, (int64_t)INT32_MAX + 1, NULL);
805
806 /* Check absolute maximum number. */
807 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
808
809 zassert_equal(int64, INT64_MAX, NULL);
810 }
811
812
ZTEST(zcbor_unit_tests,test_uint)813 ZTEST(zcbor_unit_tests, test_uint)
814 {
815 uint8_t payload1[100];
816 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
817 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16, 0);
818
819 /* Encode all numbers */
820 /* Arbitrary positive numbers in each size */
821 uint8_t uint8 = 12;
822 uint16_t uint16 = 1234;
823 uint32_t uint32 = 12345678;
824 uint64_t uint64 = 1234567812345678;
825
826 zassert_true(zcbor_uint_encode(state_e, &uint8, sizeof(uint8)), NULL);
827 zassert_true(zcbor_uint_encode(state_e, &uint16, sizeof(uint16)), NULL);
828 zassert_true(zcbor_uint_encode(state_e, &uint32, sizeof(uint32)), NULL);
829 zassert_true(zcbor_uint_encode(state_e, &uint64, sizeof(uint64)), NULL);
830
831 /* Check absolute maximum number. */
832 zassert_true(zcbor_uint64_put(state_e, UINT64_MAX), NULL);
833
834 /* Decode all numbers */
835 /* Arbitrary positive numbers in each size */
836 zassert_true(zcbor_uint_decode(state_d, &uint8, sizeof(uint8)), NULL);
837 zassert_false(zcbor_uint_decode(state_d, &uint16, sizeof(uint8)), NULL);
838 zassert_true(zcbor_uint_decode(state_d, &uint16, sizeof(uint16)), NULL);
839 zassert_false(zcbor_uint_decode(state_d, &uint32, sizeof(uint8)), NULL);
840 zassert_false(zcbor_uint_decode(state_d, &uint32, sizeof(uint16)), NULL);
841 zassert_true(zcbor_uint_decode(state_d, &uint32, sizeof(uint32)), NULL);
842 zassert_false(zcbor_uint_decode(state_d, &uint64, sizeof(uint8)), NULL);
843 zassert_false(zcbor_uint_decode(state_d, &uint64, sizeof(uint16)), NULL);
844 zassert_false(zcbor_uint_decode(state_d, &uint64, sizeof(uint32)), NULL);
845 zassert_true(zcbor_uint_decode(state_d, &uint64, sizeof(uint64)), NULL);
846
847 zassert_equal(uint8, 12, "%d\r\n", uint8);
848 zassert_equal(uint16, 1234, "%d\r\n", uint16);
849 zassert_equal(uint32, 12345678, "%d\r\n", uint32);
850 zassert_equal(uint64, 1234567812345678, "%d\r\n", uint64);
851
852 /* Check absolute maximum number. */
853 zassert_true(zcbor_uint_decode(state_d, &uint64, sizeof(uint64)), NULL);
854
855 zassert_equal(uint64, UINT64_MAX, NULL);
856 }
857
858
859 /** This tests a regression in big-endian encoding, where small numbers (like 0)
860 * where handled incorrectly (1-off), because of an error in get_result().
861 */
ZTEST(zcbor_unit_tests,test_encode_int_0)862 ZTEST(zcbor_unit_tests, test_encode_int_0)
863 {
864 uint8_t payload1[100];
865 uint32_t values_32[2] = {0, UINT32_MAX};
866 uint64_t values_64[2] = {0, UINT64_MAX};
867 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
868 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16, 0);
869
870 zassert_true(zcbor_uint32_put(state_e, values_32[0]));
871 zassert_true(zcbor_uint64_put(state_e, values_64[0]));
872 zassert_true(zcbor_uint32_expect(state_d, 0));
873 zassert_true(zcbor_uint64_expect(state_d, 0));
874 }
875
876
877 bool zcbor_simple_put(zcbor_state_t *state, uint8_t input);
878 bool zcbor_simple_encode(zcbor_state_t *state, uint8_t *input);
879 bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected);
880 bool zcbor_simple_pexpect(zcbor_state_t *state, uint8_t *expected);
881 bool zcbor_simple_decode(zcbor_state_t *state, uint8_t *result);
882
ZTEST(zcbor_unit_tests,test_simple)883 ZTEST(zcbor_unit_tests, test_simple)
884 {
885 uint8_t payload1[100];
886 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
887 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16, 0);
888 uint8_t simple1 = 0;
889 uint8_t simple2 = 2;
890 uint8_t simple3 = 0;
891
892 zassert_true(zcbor_simple_encode(state_e, &simple1), NULL);
893 zassert_true(zcbor_simple_put(state_e, 14), NULL);
894 zassert_true(zcbor_simple_put(state_e, 22), NULL);
895 zassert_false(zcbor_simple_put(state_e, 24), NULL);
896 zassert_equal(ZCBOR_ERR_INVALID_VALUE_ENCODING, zcbor_peek_error(state_e), NULL);
897 zassert_true(zcbor_simple_put(state_e, 32), NULL);
898 zassert_true(zcbor_simple_put(state_e, 255), NULL);
899 state_e->payload_mut[0] = 0xF8;
900 state_e->payload_mut[1] = 24; // Invalid value
901 state_e->payload_end += 2;
902
903 zassert_true(zcbor_simple_decode(state_d, &simple2), NULL);
904 zassert_true(zcbor_simple_expect(state_d, 14), NULL);
905 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
906 zassert_false(zcbor_undefined_expect(state_d, NULL), NULL);
907 zassert_true(zcbor_simple_expect(state_d, 32), NULL);
908 zassert_false(zcbor_simple_expect(state_d, 254), NULL);
909 zassert_true(zcbor_simple_decode(state_d, &simple1), NULL);
910 zassert_equal(0, simple2, NULL);
911 zassert_equal(255, simple1, NULL);
912
913 zassert_false(zcbor_simple_expect(state_d, 24), NULL);
914 zassert_equal(ZCBOR_ERR_INVALID_VALUE_ENCODING, zcbor_peek_error(state_d), "%s\n", zcbor_error_str(zcbor_peek_error(state_d)));
915 zassert_false(zcbor_simple_decode(state_d, &simple3), NULL);
916 zassert_equal(ZCBOR_ERR_INVALID_VALUE_ENCODING, zcbor_peek_error(state_d), "%s\n", zcbor_error_str(zcbor_peek_error(state_d)));
917 }
918
919
ZTEST(zcbor_unit_tests,test_header_len)920 ZTEST(zcbor_unit_tests, test_header_len)
921 {
922 zassert_equal(1, zcbor_header_len(0), NULL);
923 zassert_equal(1, zcbor_header_len_ptr((uint8_t*)&(uint8_t){0}, 1), NULL);
924 zassert_equal(1, zcbor_header_len(23), NULL);
925 zassert_equal(1, zcbor_header_len_ptr((uint8_t*)&(uint8_t){23}, 1), NULL);
926 zassert_equal(2, zcbor_header_len(24), NULL);
927 zassert_equal(2, zcbor_header_len_ptr((uint8_t*)&(uint8_t){24}, 1), NULL);
928 zassert_equal(2, zcbor_header_len(0xFF), NULL);
929 zassert_equal(2, zcbor_header_len_ptr((uint8_t*)&(uint8_t){0xFF}, 1), NULL);
930 zassert_equal(3, zcbor_header_len(0x100), NULL);
931 zassert_equal(3, zcbor_header_len_ptr((uint8_t*)&(uint16_t){0x100}, 2), NULL);
932 zassert_equal(3, zcbor_header_len(0xFFFF), NULL);
933 zassert_equal(3, zcbor_header_len_ptr((uint8_t*)&(uint16_t){0xFFFF}, 2), NULL);
934 zassert_equal(5, zcbor_header_len(0x10000), NULL);
935 zassert_equal(5, zcbor_header_len_ptr((uint8_t*)&(uint32_t){0x10000}, 4), NULL);
936 zassert_equal(5, zcbor_header_len(0xFFFFFFFF), NULL);
937 zassert_equal(5, zcbor_header_len_ptr((uint8_t*)&(uint32_t){0xFFFFFFFF}, 4), NULL);
938 #if SIZE_MAX >= 0x100000000ULL
939 zassert_equal(9, zcbor_header_len(0x100000000), NULL);
940 zassert_equal(9, zcbor_header_len_ptr((uint8_t*)&(uint64_t){0x100000000}, 8), NULL);
941 zassert_equal(9, zcbor_header_len(0xFFFFFFFFFFFFFFFF), NULL);
942 zassert_equal(9, zcbor_header_len_ptr((uint8_t*)&(uint64_t){0xFFFFFFFFFFFFFFFF}, 8), NULL);
943 #endif
944 }
945
946
ZTEST(zcbor_unit_tests,test_compare_strings)947 ZTEST(zcbor_unit_tests, test_compare_strings)
948 {
949 const uint8_t hello[] = "hello";
950 const uint8_t hello2[] = "hello";
951 const uint8_t long_str[] = "This is a very long string. This is a very long string. "
952 "This is a very long string. This is a very long string. This is a very long string. "
953 "This is a very long string. This is a very long string. This is a very long string. "
954 "This is a very long string. This is a very long string. This is a very long string. "
955 "This is a very long string. This is a very long string. This is a very long string. "
956 "This is a very long string. This is a very long string. This is a very long string. "
957 "This is a very long string. This is a very long string. This is a very long string. "
958 "This is a very long string. This is a very long string. This is a very long string. "
959 "This is a very long string. This is a very long string. This is a very long string. "
960 "This is a very long string. This is a very long string. This is a very long string. "
961 "This is a very long string. This is a very long string. This is a very long string. "
962 "This is a very long string. This is a very long string. This is a very long string. ";
963 const uint8_t long_str2[] = "This is a very long string. This is a very long string. "
964 "This is a very long string. This is a very long string. This is a very long string. "
965 "This is a very long string. This is a very long string. This is a very long string. "
966 "This is a very long string. This is a very long string. This is a very long string. "
967 "This is a very long string. This is a very long string. This is a very long string. "
968 "This is a very long string. This is a very long string. This is a very long string. "
969 "This is a very long string. This is a very long string. This is a very long string. "
970 "This is a very long string. This is a very long string. This is a very long string. "
971 "This is a very long string. This is a very long string. This is a very long string. "
972 "This is a very long string. This is a very long string. This is a very long string. "
973 "This is a very long string. This is a very long string. This is a very long string. "
974 "This is a very long string. This is a very long string. This is a very long string. ";
975 struct zcbor_string test_str1_hello = {hello, 5};
976 struct zcbor_string test_str2_hello_short = {hello, 4};
977 struct zcbor_string test_str3_hello_offset = {&hello[1], 4};
978 struct zcbor_string test_str4_long = {long_str, sizeof(long_str)};
979 struct zcbor_string test_str5_long2 = {long_str2, sizeof(long_str2)};
980 struct zcbor_string test_str6_empty = {hello, 0};
981 struct zcbor_string test_str7_hello2 = {hello2, 5};
982 struct zcbor_string test_str8_empty = {hello2, 0};
983 struct zcbor_string test_str9_NULL = {NULL, 0};
984
985 zassert_true(zcbor_compare_strings(&test_str1_hello, &test_str1_hello), NULL);
986 zassert_true(zcbor_compare_strings(&test_str1_hello, &test_str7_hello2), NULL);
987 zassert_true(zcbor_compare_strings(&test_str4_long, &test_str5_long2), NULL);
988 zassert_true(zcbor_compare_strings(&test_str6_empty, &test_str8_empty), NULL);
989
990 zassert_false(zcbor_compare_strings(&test_str2_hello_short, &test_str7_hello2), NULL);
991 zassert_false(zcbor_compare_strings(&test_str3_hello_offset, &test_str7_hello2), NULL);
992 zassert_false(zcbor_compare_strings(&test_str1_hello, NULL), NULL);
993 zassert_false(zcbor_compare_strings(NULL, &test_str5_long2), NULL);
994 zassert_false(zcbor_compare_strings(&test_str6_empty, NULL), NULL);
995 zassert_false(zcbor_compare_strings(&test_str1_hello, &test_str9_NULL), NULL);
996 zassert_false(zcbor_compare_strings(&test_str9_NULL, &test_str5_long2), NULL);
997 }
998
999
ZTEST(zcbor_unit_tests,test_error_str)1000 ZTEST(zcbor_unit_tests, test_error_str)
1001 {
1002 #define test_str(err) zassert_mem_equal(zcbor_error_str(err), #err, sizeof(#err), NULL)
1003
1004 test_str(ZCBOR_SUCCESS);
1005 test_str(ZCBOR_ERR_NO_BACKUP_MEM);
1006 test_str(ZCBOR_ERR_NO_BACKUP_ACTIVE);
1007 test_str(ZCBOR_ERR_LOW_ELEM_COUNT);
1008 test_str(ZCBOR_ERR_HIGH_ELEM_COUNT);
1009 test_str(ZCBOR_ERR_INT_SIZE);
1010 test_str(ZCBOR_ERR_FLOAT_SIZE);
1011 test_str(ZCBOR_ERR_ADDITIONAL_INVAL);
1012 test_str(ZCBOR_ERR_NO_PAYLOAD);
1013 test_str(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED);
1014 test_str(ZCBOR_ERR_WRONG_TYPE);
1015 test_str(ZCBOR_ERR_WRONG_VALUE);
1016 test_str(ZCBOR_ERR_WRONG_RANGE);
1017 test_str(ZCBOR_ERR_ITERATIONS);
1018 test_str(ZCBOR_ERR_ASSERTION);
1019 test_str(ZCBOR_ERR_PAYLOAD_OUTDATED);
1020 test_str(ZCBOR_ERR_ELEM_NOT_FOUND);
1021 test_str(ZCBOR_ERR_MAP_MISALIGNED);
1022 test_str(ZCBOR_ERR_ELEMS_NOT_PROCESSED);
1023 test_str(ZCBOR_ERR_NOT_AT_END);
1024 test_str(ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE);
1025 test_str(ZCBOR_ERR_INVALID_VALUE_ENCODING);
1026 test_str(ZCBOR_ERR_CONSTANT_STATE_MISSING);
1027 test_str(ZCBOR_ERR_UNKNOWN);
1028 zassert_mem_equal(zcbor_error_str(-1), "ZCBOR_ERR_UNKNOWN", sizeof("ZCBOR_ERR_UNKNOWN"), NULL);
1029 zassert_mem_equal(zcbor_error_str(-10), "ZCBOR_ERR_UNKNOWN", sizeof("ZCBOR_ERR_UNKNOWN"), NULL);
1030 zassert_mem_equal(zcbor_error_str(ZCBOR_ERR_CONSTANT_STATE_MISSING + 1), "ZCBOR_ERR_UNKNOWN", sizeof("ZCBOR_ERR_UNKNOWN"), NULL);
1031 zassert_mem_equal(zcbor_error_str(100000), "ZCBOR_ERR_UNKNOWN", sizeof("ZCBOR_ERR_UNKNOWN"), NULL);
1032 }
1033
1034
ZTEST(zcbor_unit_tests,test_any_skip)1035 ZTEST(zcbor_unit_tests, test_any_skip)
1036 {
1037 uint8_t payload[200];
1038 ZCBOR_STATE_E(state_e, 1, payload, sizeof(payload), 0);
1039 ZCBOR_STATE_D(state_d, 0, payload, sizeof(payload), 10, 0);
1040 size_t exp_elem_count = 10;
1041
1042 zassert_true(zcbor_uint32_put(state_e, 10), NULL);
1043 bool ret = zcbor_any_skip(state_d, NULL);
1044 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1045 zassert_equal(state_d->payload, state_e->payload, NULL);
1046 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1047
1048 zassert_true(zcbor_int64_put(state_e, -10000000000000), NULL);
1049 ret = zcbor_any_skip(state_d, NULL);
1050 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1051 zassert_equal(state_d->payload, state_e->payload, NULL);
1052 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1053
1054 zassert_true(zcbor_bstr_put_term(state_e, "hello", 10), NULL);
1055 zassert_true(zcbor_any_skip(state_d, NULL));
1056 zassert_equal(state_d->payload, state_e->payload, NULL);
1057 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1058
1059 zassert_true(zcbor_tstr_put_term(state_e, "world", 5), NULL);
1060 zassert_true(zcbor_any_skip(state_d, NULL));
1061 zassert_equal(state_d->payload, state_e->payload, NULL);
1062 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1063
1064 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
1065 zassert_true(zcbor_any_skip(state_d, NULL));
1066 zassert_equal(state_d->payload, state_e->payload, NULL);
1067 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1068
1069 zassert_true(zcbor_float64_put(state_e, 3.14), NULL);
1070 zassert_true(zcbor_any_skip(state_d, NULL));
1071 zassert_equal(state_d->payload, state_e->payload, NULL);
1072 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1073
1074 zassert_true(zcbor_list_start_encode(state_e, 6), NULL);
1075 zassert_true(zcbor_uint32_put(state_e, 10), NULL);
1076 zassert_true(zcbor_int64_put(state_e, -10000000000000), NULL);
1077 zassert_true(zcbor_bstr_put_term(state_e, "hello", 10), NULL);
1078 zassert_true(zcbor_tstr_put_term(state_e, "world", 10), NULL);
1079 zassert_true(zcbor_bool_put(state_e, true), NULL);
1080 zassert_true(zcbor_float64_put(state_e, 3.14), NULL);
1081 zassert_true(zcbor_list_end_encode(state_e, 6), NULL);
1082 ret = zcbor_any_skip(state_d, NULL);
1083 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1084 zassert_equal(state_d->payload, state_e->payload, NULL);
1085 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1086
1087 zassert_true(zcbor_tag_put(state_e, 1), NULL);
1088 zassert_true(zcbor_tag_put(state_e, 200), NULL);
1089 zassert_true(zcbor_tag_put(state_e, 3000), NULL);
1090 zassert_true(zcbor_map_start_encode(state_e, 6), NULL);
1091 zassert_true(zcbor_uint32_put(state_e, 10), NULL);
1092 zassert_true(zcbor_int64_put(state_e, -10000000000000), NULL);
1093 zassert_true(zcbor_bstr_put_term(state_e, "hello", 10), NULL);
1094 zassert_true(zcbor_tstr_put_term(state_e, "world", 10), NULL);
1095 zassert_true(zcbor_undefined_put(state_e, NULL), NULL);
1096 zassert_true(zcbor_float64_put(state_e, 3.14), NULL);
1097 zassert_true(zcbor_map_end_encode(state_e, 6), NULL);
1098 zassert_true(zcbor_any_skip(state_d, NULL));
1099 zassert_equal(state_d->payload, state_e->payload, "0x%x != 0x%x\n",
1100 state_d->payload, state_e->payload);
1101 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1102 }
1103
1104
ZTEST(zcbor_unit_tests,test_pexpect)1105 ZTEST(zcbor_unit_tests, test_pexpect)
1106 {
1107 uint8_t payload[100];
1108 ZCBOR_STATE_E(state_e, 2, payload, sizeof(payload), 0);
1109 ZCBOR_STATE_D(state_d, 2, payload, sizeof(payload), 20, 0);
1110
1111 zassert_true(zcbor_int32_put(state_e, 1), NULL);
1112 zassert_true(zcbor_int32_put(state_e, 2), NULL);
1113 zassert_true(zcbor_int32_put(state_e, 3), NULL);
1114 zassert_true(zcbor_int32_put(state_e, 4), NULL);
1115 zassert_true(zcbor_int32_put(state_e, 5), NULL);
1116 zassert_true(zcbor_tag_put(state_e, 6), NULL);
1117 zassert_true(zcbor_simple_put(state_e, 7), NULL);
1118 zassert_true(zcbor_bool_put(state_e, true), NULL);
1119 zassert_true(zcbor_float16_bytes_put(state_e, 0x4800), NULL);
1120 zassert_true(zcbor_float16_bytes_put(state_e, 0x4880), NULL);
1121 zassert_true(zcbor_float32_put(state_e, 10.0), NULL);
1122 zassert_true(zcbor_float32_put(state_e, 11.0), NULL);
1123 zassert_true(zcbor_float32_put(state_e, 12.0), NULL);
1124 zassert_true(zcbor_float64_put(state_e, 13.0), NULL);
1125 zassert_true(zcbor_float64_put(state_e, 14.0), NULL);
1126
1127 zassert_true(zcbor_int32_pexpect(state_d, &(int32_t){1}), NULL);
1128 zassert_true(zcbor_int64_pexpect(state_d, &(int64_t){2}), NULL);
1129 zassert_true(zcbor_uint32_pexpect(state_d, &(uint32_t){3}), NULL);
1130 zassert_true(zcbor_uint64_pexpect(state_d, &(uint64_t){4}), NULL);
1131 zassert_true(zcbor_size_pexpect(state_d, &(size_t){5}), NULL);
1132 zassert_true(zcbor_tag_pexpect(state_d, &(uint32_t){6}), NULL);
1133 zassert_true(zcbor_simple_pexpect(state_d, &(uint8_t){7}), NULL);
1134 zassert_true(zcbor_bool_pexpect(state_d, &(bool){true}), NULL);
1135 zassert_true(zcbor_float16_pexpect(state_d, &(float){8.0}), NULL);
1136 zassert_true(zcbor_float16_bytes_pexpect(state_d, &(uint16_t){0x4880}), NULL);
1137 zassert_true(zcbor_float16_32_pexpect(state_d, &(float){10.0}), NULL);
1138 zassert_true(zcbor_float32_pexpect(state_d, &(float){11.0}), NULL);
1139 zassert_true(zcbor_float32_64_pexpect(state_d, &(double){12.0}), NULL);
1140 zassert_true(zcbor_float64_pexpect(state_d, &(double){13.0}), NULL);
1141 zassert_true(zcbor_float_pexpect(state_d, &(double){14.0}), NULL);
1142 }
1143
1144
decode_inner_map(zcbor_state_t * state,void * result)1145 void decode_inner_map(zcbor_state_t *state, void *result)
1146 {
1147 zassert_true(zcbor_unordered_map_start_decode(state), NULL);
1148 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_bool_pexpect, state, &(bool){false}), NULL);
1149 zassert_true(zcbor_undefined_expect(state, NULL), NULL);
1150 zcbor_elem_processed(state);
1151 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_tstr_expect, state, &(struct zcbor_string){"hello", 5}), NULL);
1152 zassert_true(zcbor_tstr_expect(state, &(struct zcbor_string){"world", 5}), NULL);
1153 zcbor_elem_processed(state);
1154 bool ret = zcbor_unordered_map_end_decode(state);
1155 zassert_true(ret, "err: %d\n", zcbor_peek_error(state));
1156 }
1157
1158
ZTEST(zcbor_unit_tests,test_unordered_map)1159 ZTEST(zcbor_unit_tests, test_unordered_map)
1160 {
1161 uint8_t payload[200];
1162 ZCBOR_STATE_E(state_e, 2, payload, sizeof(payload), 0);
1163 ZCBOR_STATE_D(state_d, 2, payload, sizeof(payload), 10, 40);
1164 struct zcbor_string str_result1;
1165 struct zcbor_string str_result2;
1166 struct zcbor_string str_result3;
1167 int32_t int_result1;
1168 uint8_t const *start2, *start3, *start4;
1169 bool ret;
1170
1171 zassert_true(zcbor_map_start_encode(state_e, 0), NULL);
1172 zassert_true(zcbor_map_end_encode(state_e, 0), NULL);
1173 start2 = state_e->payload;
1174
1175 zassert_true(zcbor_map_start_encode(state_e, 0), NULL);
1176 zassert_true(zcbor_uint32_put(state_e, 1), NULL);
1177 zassert_true(zcbor_uint32_put(state_e, 1), NULL);
1178 zassert_true(zcbor_map_end_encode(state_e, 0), NULL);
1179 start3 = state_e->payload;
1180
1181 zassert_true(zcbor_map_start_encode(state_e, 0), NULL);
1182 zassert_true(zcbor_uint32_put(state_e, 1), NULL);
1183 zassert_true(zcbor_uint32_put(state_e, 1), NULL);
1184 zassert_true(zcbor_uint32_put(state_e, 2), NULL);
1185 zassert_true(zcbor_uint32_put(state_e, 2), NULL);
1186 zassert_true(zcbor_map_end_encode(state_e, 0), NULL);
1187 start4 = state_e->payload;
1188
1189 ZCBOR_STATE_D(state_d2, 2, start3, start4 - start3, 10, 2);
1190 ZCBOR_STATE_D(state_d3, 2, start3, start4 - start3, 10, 0); // No flags
1191
1192 zassert_true(zcbor_map_start_encode(state_e, 43), NULL);
1193 zassert_true(zcbor_uint32_put(state_e, 1), NULL);
1194 zassert_true(zcbor_tstr_put_lit(state_e, "hello"), NULL);
1195 zassert_true(zcbor_int32_put(state_e, -1), NULL);
1196 zassert_true(zcbor_tstr_put_lit(state_e, "world"), NULL);
1197 zassert_true(zcbor_bool_put(state_e, true), NULL);
1198 zassert_true(zcbor_tstr_put_lit(state_e, "foo"), NULL);
1199
1200 /* Nested map */
1201 zassert_true(zcbor_tstr_put_lit(state_e, "bar"), NULL);
1202 zassert_true(zcbor_map_start_encode(state_e, 0), NULL);
1203 zassert_true(zcbor_tstr_put_lit(state_e, "hello"), NULL);
1204 zassert_true(zcbor_tstr_put_lit(state_e, "world"), NULL);
1205 zassert_true(zcbor_bool_put(state_e, false), NULL);
1206 zassert_true(zcbor_undefined_put(state_e, NULL), NULL);
1207 zassert_true(zcbor_map_end_encode(state_e, 0), NULL);
1208 /* Nested map end */
1209
1210 for (int i = 2; i < 35; i++) {
1211 zassert_true(zcbor_uint32_put(state_e, i), NULL);
1212 zassert_true(zcbor_int32_put(state_e, -i), NULL);
1213 }
1214
1215 zassert_true(zcbor_tstr_put_lit(state_e, "baz1"), NULL);
1216 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
1217 zassert_true(zcbor_tstr_put_lit(state_e, "baz2"), NULL);
1218 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
1219 zassert_true(zcbor_bstr_put_term(state_e, "baz3", 4), NULL);
1220 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
1221 zassert_true(zcbor_bstr_put_term(state_e, "baz4", 4), NULL);
1222 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
1223 zassert_true(zcbor_bstr_put_term(state_e, "baz5", 4), NULL);
1224 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
1225 zassert_true(zcbor_map_end_encode(state_e, 43), NULL);
1226
1227
1228 /* Test empty map */
1229 zassert_true(zcbor_unordered_map_start_decode(state_d), NULL);
1230 zassert_false(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){2}), NULL);
1231 zassert_equal(ZCBOR_ERR_ELEM_NOT_FOUND, zcbor_peek_error(state_d), "err: %d\n", zcbor_peek_error(state_d));
1232 ret = zcbor_unordered_map_end_decode(state_d);
1233 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1234 zassert_equal(start2, state_d->payload, NULL);
1235
1236 /* Test single entry map */
1237 zassert_true(zcbor_unordered_map_start_decode(state_d), NULL);
1238 ret = zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){1});
1239 zassert_true(ret, "err: %d\n", zcbor_peek_error);
1240 zassert_true(zcbor_int32_expect(state_d, 1), NULL);
1241 ret = zcbor_unordered_map_end_decode(state_d);
1242 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1243 zassert_equal(start3, state_d->payload, NULL);
1244
1245 /* Test that looping stops both if it starts at the very start and very end of the map.
1246 * Also test ZCBOR_ERR_MAP_MISALIGNED. */
1247 zassert_true(zcbor_unordered_map_start_decode(state_d2), NULL);
1248 zassert_false(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d2, &(int32_t){3}), NULL);
1249 ret = zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d2, &(int32_t){2});
1250 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d2));
1251 zassert_false(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d2, &(int32_t){1}), NULL);
1252 zassert_equal(zcbor_peek_error(state_d2), ZCBOR_ERR_MAP_MISALIGNED, NULL);
1253 zassert_true(zcbor_int32_expect(state_d2, 2), NULL);
1254 zassert_true(zcbor_array_at_end(state_d2), NULL);
1255 zassert_false(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d2, &(int32_t){3}), NULL);
1256 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d2, &(int32_t){1}), NULL);
1257 zassert_true(zcbor_int32_expect(state_d2, 1), NULL);
1258 ret = zcbor_unordered_map_end_decode(state_d2);
1259 zassert_true(ret, NULL);
1260
1261 /* Test that state_d3 fails because of missing flags. */
1262 #ifdef ZCBOR_MAP_SMART_SEARCH
1263 #ifdef ZCBOR_CANONICAL
1264 zassert_false(zcbor_unordered_map_start_decode(state_d3), NULL);
1265 #else
1266 zassert_true(zcbor_unordered_map_start_decode(state_d3), NULL);
1267 zassert_false(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d3, &(int32_t){2}), NULL);
1268 #endif
1269 zassert_equal(zcbor_peek_error(state_d3), ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE, NULL);
1270 #endif
1271
1272 /* Test premature map end */
1273 zassert_true(zcbor_unordered_map_start_decode(state_d), NULL);
1274 ret = zcbor_unordered_map_end_decode(state_d);
1275 zassert_false(ret, NULL);
1276 zassert_equal(ZCBOR_ERR_ELEMS_NOT_PROCESSED, zcbor_peek_error(state_d), NULL);
1277 #ifndef ZCBOR_CANONICAL
1278 zcbor_elem_processed(state_d); // Should do nothing because no elements have been discovered.
1279 #endif
1280 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){1}), NULL);
1281 zassert_true(zcbor_int32_expect(state_d, 1), NULL);
1282 ret = zcbor_unordered_map_end_decode(state_d);
1283 zassert_false(ret, NULL);
1284 zassert_equal(ZCBOR_ERR_ELEMS_NOT_PROCESSED, zcbor_peek_error(state_d), NULL);
1285 /* Cause a restart of the map */
1286 zassert_false(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){3}), NULL);
1287 ret = zcbor_unordered_map_end_decode(state_d);
1288 zassert_false(ret, NULL);
1289 zassert_equal(ZCBOR_ERR_ELEMS_NOT_PROCESSED, zcbor_peek_error(state_d), NULL);
1290 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){2}), NULL);
1291 zassert_true(zcbor_int32_expect(state_d, 2), NULL);
1292 ret = zcbor_unordered_map_end_decode(state_d);
1293 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1294 zassert_equal(start4, state_d->payload, NULL);
1295
1296 /* Test a large map, including nesting. */
1297 state_d->constant_state->manually_process_elem = true;
1298 zassert_true(zcbor_unordered_map_start_decode(state_d), NULL);
1299 ret = zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){-1});
1300 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1301 zassert_true(zcbor_tstr_decode(state_d, &str_result1), NULL);
1302 zcbor_elem_processed(state_d);
1303
1304 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_bool_pexpect, state_d, &(bool){true}), NULL);
1305 zassert_true(zcbor_tstr_decode(state_d, &str_result2), NULL);
1306 zcbor_elem_processed(state_d);
1307 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){2}), NULL);
1308 zassert_true(zcbor_int32_decode(state_d, &int_result1), NULL);
1309 zcbor_elem_processed(state_d);
1310 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &(int32_t){1}), NULL);
1311 zassert_true(zcbor_tstr_decode(state_d, &str_result3), NULL);
1312 zcbor_elem_processed(state_d);
1313
1314 char baz4[] = {'b', 'a', 'z', '4'};
1315 char baz3[] = "baz3";
1316 char baz2[] = {'b', 'a', 'z', '2'};
1317 char baz1[] = "baz1";
1318 ret = zcbor_search_key_bstr_lit(state_d, "baz5");
1319 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1320 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
1321 zcbor_elem_processed(state_d);
1322 zassert_true(zcbor_search_key_bstr_arr(state_d, baz4), NULL);
1323 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
1324 zcbor_elem_processed(state_d);
1325 zassert_true(zcbor_search_key_bstr_term(state_d, baz3, 6), NULL);
1326 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
1327 zcbor_elem_processed(state_d);
1328 zassert_true(zcbor_search_key_tstr_arr(state_d, baz2), NULL);
1329 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
1330 zcbor_elem_processed(state_d);
1331 zassert_true(zcbor_search_key_tstr_term(state_d, baz1, 6), NULL);
1332 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
1333 // Don't call zcbor_elem_processed() To check that we can find the element again.
1334 zassert_true(zcbor_search_key_tstr_term(state_d, baz1, 6), NULL);
1335 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
1336 zcbor_elem_processed(state_d);
1337 // Check whether we can find the element again when it has been marked as processed.
1338 #ifdef ZCBOR_MAP_SMART_SEARCH
1339 zassert_false(zcbor_search_key_tstr_term(state_d, baz1, 6), NULL);
1340 #else
1341 zassert_true(zcbor_search_key_tstr_term(state_d, baz1, 6), NULL);
1342 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
1343 #endif
1344
1345 zassert_true(zcbor_search_key_tstr_lit(state_d, "bar"), NULL);
1346 decode_inner_map(state_d, NULL);
1347 zcbor_elem_processed(state_d);
1348
1349 for (size_t i = 34; i > 2; i--) {
1350 zassert_true(zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_int32_pexpect, state_d, &i), "i: %d, err: %d\n", i, zcbor_peek_error(state_d));
1351 zassert_false(zcbor_int32_expect(state_d, i), NULL);
1352 zassert_true(zcbor_int32_expect(state_d, -1 * i), NULL);
1353 zcbor_elem_processed(state_d);
1354 }
1355 ret = zcbor_unordered_map_end_decode(state_d);
1356 zassert_true(ret, "err: %d\n", zcbor_peek_error(state_d));
1357
1358 zassert_equal(int_result1, -2, NULL);
1359 zassert_true(zcbor_compare_strings(&str_result1, &(struct zcbor_string){"world", 5}), "%s (len: %d)\n", &str_result1.value, str_result1.len);
1360 zassert_true(zcbor_compare_strings(&str_result2, &(struct zcbor_string){"foo", 3}), NULL);
1361 zassert_true(zcbor_compare_strings(&str_result3, &(struct zcbor_string){"hello", 5}), NULL);
1362 }
1363
1364
ZTEST(zcbor_unit_tests,test_canonical_check)1365 ZTEST(zcbor_unit_tests, test_canonical_check)
1366 {
1367 uint8_t payload[] = {
1368 0xBF, /* Invalid map start */
1369 0x9F, /* Invalid list start */
1370 0x78, 0x00, /* invalid 0 */
1371 0x78, 0x17, /* invalid 23 */
1372 0x59, 0x00, 0x18, /* invalid 24 */
1373 0x59, 0x00, 0xFF, /* invalid 255 */
1374 0x3A, 0x00, 0x00, 0x01, 0x00, /* invalid 256 */
1375 0x3A, 0x00, 0x00, 0xFF, 0xFF, /* invalid 65535 */
1376 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* invalid 65536 */
1377 0x1B, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, /* invalid 4294967295 */
1378 0xFF, 0xFF,
1379 };
1380 ZCBOR_STATE_D(state_d, 2, payload, sizeof(payload), 20, 0);
1381 uint64_t u64_result;
1382 int64_t i64_result;
1383 struct zcbor_string str_result;
1384
1385 #ifdef ZCBOR_CANONICAL
1386 #define CHECK_ERROR1(state) zassert_equal(ZCBOR_ERR_INVALID_VALUE_ENCODING, zcbor_pop_error(state), "err: %s\n", zcbor_error_str(zcbor_peek_error(state)))
1387
1388 zassert_false(zcbor_map_start_decode(state_d), NULL);
1389 CHECK_ERROR1(state_d);
1390 state_d->payload += 1;
1391 zassert_false(zcbor_list_start_decode(state_d), NULL);
1392 CHECK_ERROR1(state_d);
1393 state_d->payload += 1;
1394 zassert_false(zcbor_tstr_decode(state_d, &str_result), NULL);
1395 CHECK_ERROR1(state_d);
1396 state_d->payload += 2;
1397 zassert_false(zcbor_tstr_decode(state_d, &str_result), NULL);
1398 CHECK_ERROR1(state_d);
1399 state_d->payload += 2;
1400 zassert_false(zcbor_bstr_decode(state_d, &str_result), NULL);
1401 CHECK_ERROR1(state_d);
1402 state_d->payload += 3;
1403 zassert_false(zcbor_bstr_decode(state_d, &str_result), NULL);
1404 CHECK_ERROR1(state_d);
1405 state_d->payload += 3;
1406 zassert_false(zcbor_int64_decode(state_d, &i64_result), NULL);
1407 CHECK_ERROR1(state_d);
1408 state_d->payload += 5;
1409 zassert_false(zcbor_int64_decode(state_d, &i64_result), NULL);
1410 CHECK_ERROR1(state_d);
1411 state_d->payload += 5;
1412 zassert_false(zcbor_uint64_decode(state_d, &u64_result), NULL);
1413 CHECK_ERROR1(state_d);
1414 state_d->payload += 9;
1415 zassert_false(zcbor_uint64_decode(state_d, &u64_result), NULL);
1416 CHECK_ERROR1(state_d);
1417 state_d->payload += 9;
1418
1419 #else
1420 #define CHECK_ERROR1(state) zassert_equal(ZCBOR_ERR_NO_PAYLOAD, zcbor_pop_error(state), "err: %s\n", zcbor_error_str(zcbor_peek_error(state)))
1421
1422 zassert_true(zcbor_map_start_decode(state_d), NULL);
1423 zassert_true(zcbor_list_start_decode(state_d), NULL);
1424 zassert_true(zcbor_tstr_decode(state_d, &str_result), NULL);
1425 zassert_true(zcbor_tstr_decode(state_d, &str_result), NULL);
1426 state_d->payload = state_d->payload_bak + 2; /* Reset since test vector doesn't contain the string value, just the header. */
1427 zassert_true(zcbor_bstr_decode(state_d, &str_result), NULL);
1428 state_d->payload = state_d->payload_bak + 3; /* Reset since test vector doesn't contain the string value, just the header. */
1429 zassert_false(zcbor_bstr_decode(state_d, &str_result), NULL); /* Fails because payload isn't big enough. */
1430 CHECK_ERROR1(state_d);
1431 state_d->payload += 3;
1432 zassert_true(zcbor_int64_decode(state_d, &i64_result), NULL);
1433 zassert_true(zcbor_int64_decode(state_d, &i64_result), NULL);
1434 zassert_true(zcbor_uint64_decode(state_d, &u64_result), NULL);
1435 zassert_true(zcbor_uint64_decode(state_d, &u64_result), NULL);
1436 zassert_true(zcbor_list_end_decode(state_d), NULL);
1437 zassert_true(zcbor_map_end_decode(state_d), NULL);
1438 #endif
1439 }
1440
1441
ZTEST(zcbor_unit_tests,test_zcbor_version)1442 ZTEST(zcbor_unit_tests, test_zcbor_version)
1443 {
1444 const char zcbor_version_str[] = ZCBOR_VERSION_STR;
1445 const char zcbor_version_expected[] = TEST_ZCBOR_VERSION_STR;
1446
1447
1448 zassert_mem_equal(zcbor_version_expected, zcbor_version_str, sizeof(zcbor_version_expected));
1449 zassert_equal(TEST_ZCBOR_VERSION, ZCBOR_VERSION, NULL);
1450 zassert_equal(TEST_ZCBOR_VERSION_MAJOR, ZCBOR_VERSION_MAJOR, NULL);
1451 zassert_equal(TEST_ZCBOR_VERSION_MINOR, ZCBOR_VERSION_MINOR, NULL);
1452 zassert_equal(TEST_ZCBOR_VERSION_BUGFIX, ZCBOR_VERSION_BUGFIX, NULL);
1453 }
1454
1455
1456 /* Test that CBOR-encoded bstrs are encoded with the correct length. */
ZTEST(zcbor_unit_tests,test_cbor_encoded_bstr_len)1457 ZTEST(zcbor_unit_tests, test_cbor_encoded_bstr_len)
1458 {
1459 uint8_t payload[50];
1460
1461 #ifdef ZCBOR_VERBOSE
1462 for (size_t len = 10; len < 0x108; len++)
1463 #else
1464 for (size_t len = 10; len < 0x10010; len++)
1465 #endif /* ZCBOR_VERBOSE */
1466 {
1467 ZCBOR_STATE_E(state_e, 1, payload, len, 0);
1468 ZCBOR_STATE_D(state_d, 1, payload, len, 1, 0);
1469
1470 zassert_true(zcbor_bstr_start_encode(state_e), "len: %d\n", len);
1471 zassert_true(zcbor_size_put(state_e, len), "len: %d\n", len);
1472 zassert_true(zcbor_bstr_end_encode(state_e, NULL), "len: %d\n", len);
1473
1474 zassert_true(zcbor_bstr_start_decode(state_d, NULL), "len: %d\n", len);
1475 zassert_true(zcbor_size_expect(state_d, len), "len: %d\n", len);
1476 zassert_true(zcbor_bstr_end_decode(state_d), "len: %d\n", len);
1477 }
1478
1479 #if SIZE_MAX == UINT64_MAX
1480 for (size_t len = 0xFFFFFF00; len <= 0x100000100; len++) {
1481 ZCBOR_STATE_E(state_e, 1, payload, len, 0);
1482 ZCBOR_STATE_D(state_d, 1, payload, len, 1, 0);
1483
1484 zassert_true(zcbor_bstr_start_encode(state_e), "len: %d\n", len);
1485 zassert_true(zcbor_size_put(state_e, len), "len: %d\n", len);
1486 zassert_true(zcbor_bstr_end_encode(state_e, NULL), "len: %d\n", len);
1487
1488 zassert_true(zcbor_bstr_start_decode(state_d, NULL), "len: %d\n", len);
1489 zassert_true(zcbor_size_expect(state_d, len), "len: %d\n", len);
1490 zassert_true(zcbor_bstr_end_decode(state_d), "len: %d\n", len);
1491 }
1492 #endif /* SIZE_MAX == UINT64_MAX */
1493 }
1494
1495
1496 /* Test zcbor_remaining_str_len().
1497 * Some payload lengths are impossible to fill with a properly encoded string,
1498 * these have special cases. */
ZTEST(zcbor_unit_tests,test_remaining_str_len)1499 ZTEST(zcbor_unit_tests, test_remaining_str_len)
1500 {
1501 uint8_t payload[8];
1502 ZCBOR_STATE_E(state_e, 1, payload, 0, 0);
1503
1504 zassert_equal(zcbor_remaining_str_len(state_e), 0, "i: 0\n");
1505
1506 for (uint64_t i = 1; i <= 0x20000; i++) {
1507 size_t offset;
1508
1509 state_e->payload_end = payload + i;
1510
1511 switch(i) {
1512 case 25:
1513 case 0x102:
1514 offset = 1;
1515 break;
1516 case 0x10003:
1517 case 0x10004:
1518 offset = 2;
1519 break;
1520 default:
1521 offset = 0;
1522 break;
1523 }
1524
1525 size_t total_len = zcbor_remaining_str_len(state_e)
1526 + zcbor_header_len(zcbor_remaining_str_len(state_e));
1527 zassert_equal(i - offset, total_len, "i: %ld, len: %zu\n", i, total_len);
1528 }
1529
1530 #if SIZE_MAX == UINT64_MAX
1531 for (uint64_t i = 0xFFFFFF00; i <= 0x100000100; i++) {
1532 size_t offset;
1533
1534 state_e->payload_end = payload + i;
1535
1536 switch(i) {
1537 case 0x100000005:
1538 case 0x100000006:
1539 case 0x100000007:
1540 case 0x100000008:
1541 offset = 4;
1542 break;
1543 default:
1544 offset = 0;
1545 break;
1546 }
1547
1548 size_t total_len = zcbor_remaining_str_len(state_e)
1549 + zcbor_header_len(zcbor_remaining_str_len(state_e));
1550 zassert_equal(i - offset, total_len, "i: %lx, len: %zx\n", i, total_len);
1551 }
1552 #endif
1553 }
1554
1555 /* Test that CBOR-encoded tstrs are encoded with the correct length, even if the first part of the
1556 number is 0s. */
ZTEST(zcbor_unit_tests,test_float_len)1557 ZTEST(zcbor_unit_tests, test_float_len)
1558 {
1559 uint8_t payload[50];
1560 ZCBOR_STATE_E(state_e, 1, payload, sizeof(payload), 0);
1561 ZCBOR_STATE_D(state_d, 1, payload, sizeof(payload), 20, 0);
1562
1563 zassert_true(zcbor_float16_put(state_e, 0.0));
1564 zassert_true(zcbor_float16_expect(state_d, 0.0));
1565 zassert_true(zcbor_float32_put(state_e, 0.0));
1566 zassert_true(zcbor_float32_expect(state_d, 0.0));
1567 zassert_true(zcbor_float64_put(state_e, 0.0));
1568 zassert_true(zcbor_float64_expect(state_d, 0.0));
1569
1570 zassert_true(zcbor_float16_put(state_e, powf(2, -17)));
1571 zassert_true(zcbor_float16_expect(state_d, powf(2, -17)));
1572 zassert_true(zcbor_float32_put(state_e, 0.000000000000000000000000000000000000000001f));
1573 zassert_true(zcbor_float32_expect(state_d, 0.000000000000000000000000000000000000000001f));
1574 zassert_true(zcbor_float64_put(state_e, pow(10, -315)));
1575 zassert_true(zcbor_float64_expect(state_d, pow(10, -315)));
1576 }
1577
ZTEST(zcbor_unit_tests,test_simple_value_len)1578 ZTEST(zcbor_unit_tests, test_simple_value_len)
1579 {
1580 #ifndef ZCBOR_CANONICAL
1581 printf("Skip on non-canonical builds.\n");
1582 #else
1583 uint8_t payload[] = {0xe1, 0xf4, 0xf5, 0xf6, 0xf7};
1584
1585 /* Simple values under 24 must be encoded as single bytes. */
1586 uint8_t payload_inv[] = {
1587 0xf8, 1,
1588 0xf8, ZCBOR_BOOL_TO_SIMPLE,
1589 0xf8, ZCBOR_BOOL_TO_SIMPLE + 1,
1590 0xf8, ZCBOR_NIL_VAL,
1591 0xf8, ZCBOR_UNDEF_VAL
1592 };
1593 ZCBOR_STATE_D(state_d, 1, payload, sizeof(payload), 20, 0);
1594 ZCBOR_STATE_D(state_d_inv, 1, payload_inv, sizeof(payload_inv), 20, 0);
1595
1596 zassert_true(zcbor_simple_expect(state_d, 1));
1597 zassert_true(zcbor_bool_expect(state_d, false));
1598 zassert_true(zcbor_bool_expect(state_d, true));
1599 zassert_true(zcbor_nil_expect(state_d, NULL));
1600 zassert_true(zcbor_undefined_expect(state_d, NULL));
1601
1602 zassert_false(zcbor_simple_expect(state_d_inv, 1));
1603 zassert_false(zcbor_bool_expect(state_d_inv, false));
1604 zassert_false(zcbor_bool_expect(state_d_inv, true));
1605 zassert_false(zcbor_nil_expect(state_d_inv, NULL));
1606 zassert_false(zcbor_undefined_expect(state_d_inv, NULL));
1607 #endif
1608 }
1609
1610
1611 ZTEST_SUITE(zcbor_unit_tests, NULL, NULL, NULL, NULL, NULL);
1612