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_debug.h"
11
ZTEST(zcbor_unit_tests,test_int64)12 ZTEST(zcbor_unit_tests, test_int64)
13 {
14 uint8_t payload[100] = {0};
15 int64_t int64;
16 int32_t int32;
17
18 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
19 ZCBOR_STATE_D(state_d, 0, payload, sizeof(payload), 10);
20
21 zassert_true(zcbor_int64_put(state_e, 5), NULL);
22 zassert_false(zcbor_int64_expect(state_d, 4), NULL);
23 zassert_false(zcbor_int64_expect(state_d, 6), NULL);
24 zassert_false(zcbor_int64_expect(state_d, -5), NULL);
25 zassert_false(zcbor_int64_expect(state_d, -6), NULL);
26 zassert_true(zcbor_int64_expect(state_d, 5), NULL);
27
28 zassert_true(zcbor_int32_put(state_e, 5), NULL);
29 zassert_true(zcbor_int64_expect(state_d, 5), NULL);
30
31 zassert_true(zcbor_int64_put(state_e, 5), NULL);
32 zassert_true(zcbor_int32_expect(state_d, 5), NULL);
33
34 zassert_true(zcbor_int64_put(state_e, 5000000000), NULL);
35 zassert_false(zcbor_int32_decode(state_d, &int32), NULL);
36 zassert_true(zcbor_int64_decode(state_d, &int64), NULL);
37 zassert_equal(int64, 5000000000, NULL);
38
39 zassert_true(zcbor_int64_encode(state_e, &int64), NULL);
40 zassert_false(zcbor_int64_expect(state_d, 5000000001), NULL);
41 zassert_true(zcbor_int64_expect(state_d, 5000000000), NULL);
42
43 zassert_true(zcbor_int64_put(state_e, 0x80000000), NULL);
44 zassert_false(zcbor_int32_decode(state_d, &int32), NULL);
45 zassert_true(zcbor_uint32_expect(state_d, 0x80000000), NULL);
46
47 zassert_true(zcbor_int32_put(state_e, -505), NULL);
48 zassert_true(zcbor_int64_expect(state_d, -505), NULL);
49
50 zassert_true(zcbor_int64_put(state_e, -5000000000000), NULL);
51 zassert_false(zcbor_int64_expect(state_d, -5000000000001), NULL);
52 zassert_false(zcbor_int64_expect(state_d, -4999999999999), NULL);
53 zassert_false(zcbor_int64_expect(state_d, 5000000000000), NULL);
54 zassert_false(zcbor_int64_expect(state_d, 4999999999999), NULL);
55 zassert_true(zcbor_int64_expect(state_d, -5000000000000), NULL);
56
57 zassert_true(zcbor_uint64_put(state_e, ((uint64_t)INT64_MAX + 1)), NULL);
58 zassert_false(zcbor_int64_decode(state_d, &int64), NULL);
59 zassert_true(zcbor_uint64_expect(state_d, ((uint64_t)INT64_MAX + 1)), NULL);
60
61 }
62
63
ZTEST(zcbor_unit_tests,test_uint64)64 ZTEST(zcbor_unit_tests, test_uint64)
65 {
66 uint8_t payload[100] = {0};
67 uint64_t uint64;
68 uint32_t uint32;
69
70 zcbor_state_t state_e;
71 zcbor_new_state(&state_e, 1, payload, sizeof(payload), 0);
72 zcbor_state_t state_d;
73 zcbor_new_state(&state_d, 1, payload, sizeof(payload), 10);
74
75 zassert_true(zcbor_uint64_put(&state_e, 5), NULL);
76 zassert_false(zcbor_uint64_expect(&state_d, 4), NULL);
77 zassert_false(zcbor_uint64_expect(&state_d, 6), NULL);
78 zassert_false(zcbor_uint64_expect(&state_d, -5), NULL);
79 zassert_false(zcbor_uint64_expect(&state_d, -6), NULL);
80 zassert_true(zcbor_uint64_expect(&state_d, 5), NULL);
81
82 zassert_true(zcbor_uint32_put(&state_e, 5), NULL);
83 zassert_true(zcbor_uint64_expect(&state_d, 5), NULL);
84
85 zassert_true(zcbor_uint64_put(&state_e, 5), NULL);
86 zassert_true(zcbor_uint32_expect(&state_d, 5), NULL);
87
88 zassert_true(zcbor_uint64_put(&state_e, UINT64_MAX), NULL);
89 zassert_false(zcbor_uint32_decode(&state_d, &uint32), NULL);
90 zassert_true(zcbor_uint64_decode(&state_d, &uint64), NULL);
91 zassert_equal(uint64, UINT64_MAX, NULL);
92
93 zassert_true(zcbor_uint64_encode(&state_e, &uint64), NULL);
94 zassert_false(zcbor_uint64_expect(&state_d, (UINT64_MAX - 1)), NULL);
95 zassert_true(zcbor_uint64_expect(&state_d, UINT64_MAX), NULL);
96 }
97
ZTEST(zcbor_unit_tests,test_size)98 ZTEST(zcbor_unit_tests, test_size)
99 {
100 uint8_t payload[100] = {0};
101 size_t read;
102
103 zcbor_state_t state_e;
104 zcbor_new_state(&state_e, 1, payload, sizeof(payload), 0);
105 zcbor_state_t state_d;
106 zcbor_new_state(&state_d, 1, payload, sizeof(payload), 10);
107
108 zassert_true(zcbor_size_put(&state_e, 5), NULL);
109 zassert_false(zcbor_size_expect(&state_d, 4), NULL);
110 zassert_false(zcbor_size_expect(&state_d, 6), NULL);
111 zassert_false(zcbor_size_expect(&state_d, -5), NULL);
112 zassert_false(zcbor_size_expect(&state_d, -6), NULL);
113 zassert_true(zcbor_size_expect(&state_d, 5), NULL);
114
115 zassert_true(zcbor_int32_put(&state_e, -7), NULL);
116 zassert_false(zcbor_size_expect(&state_d, -7), NULL);
117 zassert_false(zcbor_size_decode(&state_d, &read), NULL); // Negative number not supported.
118 zassert_true(zcbor_int32_expect(&state_d, -7), NULL);
119
120 zassert_true(zcbor_uint32_put(&state_e, 5), NULL);
121 zassert_true(zcbor_size_expect(&state_d, 5), NULL);
122
123 zassert_true(zcbor_uint64_put(&state_e, 5), NULL);
124 zassert_true(zcbor_size_expect(&state_d, 5), NULL);
125
126 zassert_true(zcbor_uint32_put(&state_e, UINT32_MAX), NULL);
127 zassert_true(zcbor_size_decode(&state_d, &read), NULL);
128 zassert_equal(read, UINT32_MAX, NULL);
129
130 #if SIZE_MAX == UINT64_MAX
131 zassert_true(zcbor_uint64_put(&state_e, UINT64_MAX), NULL);
132 zassert_true(zcbor_size_decode(&state_d, &read), NULL);
133 zassert_equal(read, UINT64_MAX, NULL);
134 #endif
135
136 #if SIZE_MAX == UINT32_MAX
137 zassert_true(zcbor_uint64_put(&state_e, UINT64_MAX), NULL);
138 zassert_false(zcbor_size_decode(&state_d, &read), NULL);
139 #endif
140 }
141
142 #if SIZE_MAX == UINT64_MAX
143 /* Only runs on 64-bit builds. */
144 #include <stdlib.h>
145
146 #define PAYL_SIZE 0x100000100
147 #define STR_SIZE 0x100000010
148
ZTEST(zcbor_unit_tests,test_size64)149 ZTEST(zcbor_unit_tests, test_size64)
150 {
151 uint8_t *large_payload = malloc(PAYL_SIZE);
152 uint8_t *large_string = malloc(STR_SIZE);
153 struct zcbor_string tstr = {.value = large_string, .len = STR_SIZE};
154 struct zcbor_string tstr_res;
155
156 for (int i = 0; i < 1000; i++) {
157 large_string[i] = i % 256;
158 large_payload[i + 9] = 0;
159 }
160 for (size_t i = STR_SIZE - 1001; i < STR_SIZE; i++) {
161 large_string[i] = i % 256;
162 large_payload[i + 9] = 0;
163 }
164
165 ZCBOR_STATE_E(state_e, 0, large_payload, PAYL_SIZE, 0);
166 ZCBOR_STATE_D(state_d, 0, large_payload, PAYL_SIZE, 10);
167
168 zassert_true(zcbor_tstr_encode(state_e, &tstr), NULL);
169 zassert_false(zcbor_bstr_decode(state_d, &tstr_res), NULL);
170 zassert_true(zcbor_tstr_decode(state_d, &tstr_res), NULL);
171 zassert_equal(tstr_res.len, tstr.len, NULL);
172 zassert_equal_ptr(tstr_res.value, &large_payload[9], NULL);
173 zassert_mem_equal(tstr_res.value, large_string, tstr.len, NULL);
174 }
175
176
177 #else
ZTEST(zcbor_unit_tests,test_size64)178 ZTEST(zcbor_unit_tests, test_size64)
179 {
180 printk("Skip on non-64-bit builds.\n");
181 }
182 #endif
183
184
ZTEST(zcbor_unit_tests,test_string_macros)185 ZTEST(zcbor_unit_tests, test_string_macros)
186 {
187 uint8_t payload[100];
188 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
189 char world[] = {'w', 'o', 'r', 'l', 'd'};
190
191 zassert_true(zcbor_bstr_put_lit(state_e, "Hello"), NULL);
192 zassert_true(zcbor_bstr_put_term(state_e, "Hello"), NULL);
193 zassert_true(zcbor_bstr_put_arr(state_e, world), NULL);
194 zassert_true(zcbor_tstr_put_lit(state_e, "Hello"), NULL);
195 zassert_true(zcbor_tstr_put_term(state_e, "Hello"), NULL);
196 zassert_true(zcbor_tstr_put_arr(state_e, world), NULL);
197
198 ZCBOR_STATE_D(state_d, 0, payload, sizeof(payload), 6);
199
200 zassert_false(zcbor_bstr_expect_lit(state_d, "Yello"), NULL);
201 zassert_false(zcbor_tstr_expect_lit(state_d, "Hello"), NULL);
202 zassert_true(zcbor_bstr_expect_lit(state_d, "Hello"), NULL);
203 zassert_false(zcbor_bstr_expect_term(state_d, "Hello!"), NULL);
204 zassert_true(zcbor_bstr_expect_term(state_d, "Hello"), NULL);
205 world[3]++;
206 zassert_false(zcbor_bstr_expect_arr(state_d, world), NULL);
207 world[3]--;
208 zassert_true(zcbor_bstr_expect_arr(state_d, world), NULL);
209 zassert_false(zcbor_tstr_expect_lit(state_d, "hello"), NULL);
210 zassert_true(zcbor_tstr_expect_lit(state_d, "Hello"), NULL);
211 zassert_false(zcbor_tstr_expect_term(state_d, "Helo"), NULL);
212 zassert_true(zcbor_tstr_expect_term(state_d, "Hello"), NULL);
213 world[2]++;
214 zassert_false(zcbor_tstr_expect_arr(state_d, world), NULL);
215 world[2]--;
216 zassert_false(zcbor_bstr_expect_arr(state_d, world), NULL);
217 zassert_true(zcbor_tstr_expect_arr(state_d, world), NULL);
218 }
219
220
ZTEST(zcbor_unit_tests,test_stop_on_error)221 ZTEST(zcbor_unit_tests, test_stop_on_error)
222 {
223 uint8_t payload[100];
224 ZCBOR_STATE_E(state_e, 3, payload, sizeof(payload), 0);
225 struct zcbor_string failing_string = {.value = NULL, .len = 1000};
226 struct zcbor_string dummy_string;
227 zcbor_state_t state_backup;
228 struct zcbor_state_constant constant_state_backup;
229
230 state_e->constant_state->stop_on_error = true;
231
232 zassert_false(zcbor_tstr_encode(state_e, &failing_string), NULL);
233 zassert_equal(ZCBOR_ERR_NO_PAYLOAD, state_e->constant_state->error, "%d\r\n", state_e->constant_state->error);
234 memcpy(&state_backup, state_e, sizeof(state_backup));
235 memcpy(&constant_state_backup, state_e->constant_state, sizeof(constant_state_backup));
236
237 /* All fail because of ZCBOR_ERR_NO_PAYLOAD */
238 zassert_false(zcbor_int32_put(state_e, 1), NULL);
239 zassert_false(zcbor_int64_put(state_e, 2), NULL);
240 zassert_false(zcbor_uint32_put(state_e, 3), NULL);
241 zassert_false(zcbor_uint64_put(state_e, 4), NULL);
242 zassert_false(zcbor_size_put(state_e, 10), NULL);
243 zassert_false(zcbor_int32_encode(state_e, &(int32_t){5}), NULL);
244 zassert_false(zcbor_int64_encode(state_e, &(int64_t){6}), NULL);
245 zassert_false(zcbor_uint32_encode(state_e, &(uint32_t){7}), NULL);
246 zassert_false(zcbor_uint64_encode(state_e, &(uint64_t){8}), NULL);
247 zassert_false(zcbor_size_encode(state_e, &(size_t){9}), NULL);
248 zassert_false(zcbor_bstr_put_lit(state_e, "Hello"), NULL);
249 zassert_false(zcbor_tstr_put_lit(state_e, "World"), NULL);
250 zassert_false(zcbor_tag_encode(state_e, 9), NULL);
251 zassert_false(zcbor_bool_put(state_e, true), NULL);
252 zassert_false(zcbor_bool_encode(state_e, &(bool){false}), NULL);
253 zassert_false(zcbor_float32_put(state_e, 10.5), NULL);
254 zassert_false(zcbor_float32_encode(state_e, &(float){11.6}), NULL);
255 zassert_false(zcbor_float64_put(state_e, 12.7), NULL);
256 zassert_false(zcbor_float64_encode(state_e, &(double){13.8}), NULL);
257 zassert_false(zcbor_nil_put(state_e, NULL), NULL);
258 zassert_false(zcbor_undefined_put(state_e, NULL), NULL);
259 zassert_false(zcbor_bstr_start_encode(state_e), NULL);
260 zassert_false(zcbor_bstr_end_encode(state_e, NULL), NULL);
261 zassert_false(zcbor_list_start_encode(state_e, 1), NULL);
262 zassert_false(zcbor_map_start_encode(state_e, 0), NULL);
263 zassert_false(zcbor_map_end_encode(state_e, 0), NULL);
264 zassert_false(zcbor_list_end_encode(state_e, 1), NULL);
265 zassert_false(zcbor_multi_encode(1, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)14, 0), NULL);
266 zassert_false(zcbor_multi_encode_minmax(1, 1, &(size_t){1}, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)15, 0), NULL);
267 zassert_false(zcbor_present_encode(&(bool){true}, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)16), NULL);
268
269
270 zassert_mem_equal(&state_backup, state_e, sizeof(state_backup), NULL);
271 zassert_mem_equal(&constant_state_backup, state_e->constant_state, sizeof(constant_state_backup), NULL);
272
273 zassert_equal(ZCBOR_ERR_NO_PAYLOAD, zcbor_peek_error(state_e), NULL);
274 zassert_equal(ZCBOR_ERR_NO_PAYLOAD, zcbor_pop_error(state_e), NULL);
275 zassert_equal(ZCBOR_SUCCESS, zcbor_peek_error(state_e), NULL);
276
277 /* All succeed since the error has been popped. */
278 zassert_true(zcbor_int32_put(state_e, 1), NULL);
279 zassert_true(zcbor_int64_put(state_e, 2), NULL);
280 zassert_true(zcbor_uint32_put(state_e, 3), NULL);
281 zassert_true(zcbor_uint64_put(state_e, 4), NULL);
282 zassert_true(zcbor_size_put(state_e, 10), NULL);
283 zassert_true(zcbor_int32_encode(state_e, &(int32_t){5}), NULL);
284 zassert_true(zcbor_int64_encode(state_e, &(int64_t){6}), NULL);
285 zassert_true(zcbor_uint32_encode(state_e, &(uint32_t){7}), NULL);
286 zassert_true(zcbor_uint64_encode(state_e, &(uint64_t){8}), NULL);
287 zassert_true(zcbor_size_encode(state_e, &(size_t){9}), NULL);
288 zassert_true(zcbor_bstr_put_lit(state_e, "Hello"), NULL);
289 zassert_true(zcbor_tstr_put_lit(state_e, "World"), NULL);
290 zassert_true(zcbor_tag_encode(state_e, 9), NULL);
291 zassert_true(zcbor_tag_encode(state_e, 10), NULL);
292 zassert_true(zcbor_bool_put(state_e, true), NULL);
293 zassert_true(zcbor_bool_encode(state_e, &(bool){false}), NULL);
294 zassert_true(zcbor_float32_put(state_e, 10.5), NULL);
295 zassert_true(zcbor_float32_encode(state_e, &(float){11.6}), NULL);
296 zassert_true(zcbor_float64_put(state_e, 12.7), NULL);
297 zassert_true(zcbor_float64_encode(state_e, &(double){13.8}), NULL);
298 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
299 zassert_true(zcbor_undefined_put(state_e, NULL), NULL);
300 zassert_true(zcbor_bstr_start_encode(state_e), NULL);
301 zassert_true(zcbor_bstr_end_encode(state_e, NULL), NULL);
302 zassert_true(zcbor_list_start_encode(state_e, 1), NULL);
303 zassert_true(zcbor_map_start_encode(state_e, 0), NULL);
304 zassert_true(zcbor_map_end_encode(state_e, 0), NULL);
305 zassert_true(zcbor_list_end_encode(state_e, 1), NULL);
306 zassert_true(zcbor_multi_encode(1, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)14, 0), NULL);
307 zassert_true(zcbor_multi_encode_minmax(1, 1, &(size_t){1}, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)15, 0), NULL);
308 zassert_true(zcbor_present_encode(&(bool){true}, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)16), NULL);
309
310 ZCBOR_STATE_D(state_d, 3, payload, sizeof(payload), 30);
311 state_d->constant_state->stop_on_error = true;
312
313 zassert_false(zcbor_int32_expect(state_d, 2), NULL);
314 zassert_equal(ZCBOR_ERR_WRONG_VALUE, state_d->constant_state->error, "%d\r\n", state_d->constant_state->error);
315 memcpy(&state_backup, state_d, sizeof(state_backup));
316 memcpy(&constant_state_backup, state_d->constant_state, sizeof(constant_state_backup));
317
318 /* All fail because of ZCBOR_ERR_WRONG_VALUE */
319 zassert_false(zcbor_int32_expect(state_d, 1), NULL);
320 zassert_false(zcbor_int64_expect(state_d, 2), NULL);
321 zassert_false(zcbor_uint32_expect(state_d, 3), NULL);
322 zassert_false(zcbor_uint64_expect(state_d, 4), NULL);
323 zassert_false(zcbor_size_expect(state_d, 10), NULL);
324 zassert_false(zcbor_int32_decode(state_d, &(int32_t){5}), NULL);
325 zassert_false(zcbor_int64_decode(state_d, &(int64_t){6}), NULL);
326 zassert_false(zcbor_uint32_decode(state_d, &(uint32_t){7}), NULL);
327 zassert_false(zcbor_uint64_decode(state_d, &(uint64_t){8}), NULL);
328 zassert_false(zcbor_size_decode(state_d, &(size_t){9}), NULL);
329 zassert_false(zcbor_bstr_expect_lit(state_d, "Hello"), NULL);
330 zassert_false(zcbor_tstr_expect_lit(state_d, "World"), NULL);
331 zassert_false(zcbor_tag_decode(state_d, &(uint32_t){9}), NULL);
332 zassert_false(zcbor_tag_expect(state_d, 10), NULL);
333 zassert_false(zcbor_bool_expect(state_d, true), NULL);
334 zassert_false(zcbor_bool_decode(state_d, &(bool){false}), NULL);
335 zassert_false(zcbor_float32_expect(state_d, 10.5), NULL);
336 zassert_false(zcbor_float32_decode(state_d, &(float){11.6}), NULL);
337 #ifndef CONFIG_BOARD_QEMU_MALTA_BE
338 zassert_false(zcbor_float64_expect(state_d, 12.7), NULL);
339 #endif
340 zassert_false(zcbor_float64_decode(state_d, &(double){13.8}), NULL);
341 zassert_false(zcbor_nil_expect(state_d, NULL), NULL);
342 zassert_false(zcbor_undefined_expect(state_d, NULL), NULL);
343 zassert_false(zcbor_bstr_start_decode(state_d, &dummy_string), NULL);
344 zassert_false(zcbor_bstr_end_decode(state_d), NULL);
345 zassert_false(zcbor_list_start_decode(state_d), NULL);
346 zassert_false(zcbor_map_start_decode(state_d), NULL);
347 zassert_false(zcbor_map_end_decode(state_d), NULL);
348 zassert_false(zcbor_list_end_decode(state_d), NULL);
349 zassert_false(zcbor_multi_decode(1, 1, &(size_t){1}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)14, 0), NULL);
350 zassert_false(zcbor_int32_expect(state_d, 15), NULL);
351 zassert_false(zcbor_present_decode(&(bool){true}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)16), 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 #ifdef CONFIG_BOARD_QEMU_MALTA_BE
378 zassert_true(zcbor_float64_decode(state_d, &(double){12.7}), NULL);
379 #else
380 zassert_true(zcbor_float64_expect(state_d, 12.7), NULL);
381 #endif
382 zassert_true(zcbor_float64_decode(state_d, &(double){13.8}), NULL);
383 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
384 zassert_true(zcbor_undefined_expect(state_d, NULL), NULL);
385 zassert_true(zcbor_bstr_start_decode(state_d, &dummy_string), NULL);
386 zassert_true(zcbor_bstr_end_decode(state_d), NULL);
387 zassert_true(zcbor_list_start_decode(state_d), NULL);
388 zassert_true(zcbor_map_start_decode(state_d), NULL);
389 zassert_true(zcbor_map_end_decode(state_d), NULL);
390 zassert_true(zcbor_list_end_decode(state_d), NULL);
391 zassert_true(zcbor_multi_decode(1, 1, &(size_t){1}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)14, 0), NULL);
392 zassert_true(zcbor_int32_expect(state_d, 15), NULL);
393 zassert_true(zcbor_present_decode(&(bool){1}, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)16), NULL);
394
395 /* Everything has been decoded. */
396 zassert_equal(state_e->payload, state_d->payload, NULL);
397 }
398
399
400 /** The string "HelloWorld" is split into 2 fragments, and a number of different
401 * functions are called to test that they respond correctly to the situation.
402 **/
ZTEST(zcbor_unit_tests,test_fragments)403 ZTEST(zcbor_unit_tests, test_fragments)
404 {
405 uint8_t payload[100];
406 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
407 struct zcbor_string output;
408 struct zcbor_string_fragment output_frags[2];
409
410 zassert_true(zcbor_bstr_put_lit(state_e, "HelloWorld"), NULL);
411
412 ZCBOR_STATE_D(state_d, 0, payload, 8, 1);
413 ZCBOR_STATE_D(state_d2, 0, payload, sizeof(payload), 1);
414
415 zassert_true(zcbor_bstr_decode(state_d2, &output), NULL);
416 zassert_false(zcbor_payload_at_end(state_d2), NULL);
417 zassert_false(zcbor_bstr_decode(state_d, &output), NULL);
418 zassert_false(zcbor_payload_at_end(state_d), NULL);
419 zassert_true(zcbor_bstr_decode_fragment(state_d, &output_frags[0]), NULL);
420 zassert_equal_ptr(&payload[1], output_frags[0].fragment.value, NULL);
421 zassert_equal(7, output_frags[0].fragment.len, NULL);
422 zassert_equal(10, output_frags[0].total_len, "%d != %d\r\n", 10, output_frags[0].total_len);
423 zassert_equal(0, output_frags[0].offset, NULL);
424 zassert_false(zcbor_is_last_fragment(&output_frags[0]), NULL);
425
426 zassert_true(zcbor_payload_at_end(state_d), NULL);
427 zcbor_update_state(state_d, &payload[8], sizeof(payload) - 8);
428 zassert_false(zcbor_bstr_decode_fragment(state_d, &output_frags[1]), NULL);
429 zcbor_next_fragment(state_d, &output_frags[0], &output_frags[1]);
430 zassert_equal_ptr(&payload[8], output_frags[1].fragment.value, NULL);
431 zassert_equal(3, output_frags[1].fragment.len, "%d != %d\r\n", 3, output_frags[1].fragment.len);
432 zassert_equal(10, output_frags[1].total_len, NULL);
433 zassert_equal(7, output_frags[1].offset, NULL);
434 zassert_true(zcbor_is_last_fragment(&output_frags[1]), NULL);
435
436 uint8_t spliced[11];
437 output.value = spliced;
438 output.len = sizeof(spliced);
439
440 zassert_true(zcbor_validate_string_fragments(output_frags, 2), NULL);
441 zassert_true(zcbor_splice_string_fragments(output_frags, 2, spliced, &output.len), NULL);
442
443 zassert_equal(10, output.len, NULL);
444 zassert_mem_equal(output.value, "HelloWorld", 10, NULL);
445 }
446
447
448 /** The long string "HelloWorld1HelloWorld2..." is split into 18 fragments.
449 *
450 * First, zcbor_validate_string_fragments() is checked to be true, then various
451 * errors are introduced one by one and zcbor_validate_string_fragments() is
452 * checked to be false for all of them in turn.
453 */
ZTEST(zcbor_unit_tests,test_validate_fragments)454 ZTEST(zcbor_unit_tests, test_validate_fragments)
455 {
456 uint8_t payload[200];
457 uint8_t frag_payload[15];
458
459 ZCBOR_STATE_E(state_e, 0, payload, sizeof(payload), 0);
460 struct zcbor_string output;
461 struct zcbor_string_fragment output_frags[18];
462 struct zcbor_string_fragment output_frags_backup;
463
464 // Start positive test
465
466 zassert_true(zcbor_bstr_put_lit(state_e,
467 "HelloWorld1HelloWorld2HelloWorld3HelloWorld4HelloWorld5HelloWorld6" \
468 "HelloWorld7HelloWorld8HelloWorld9HelloWorldAHelloWorldBHelloWorldC" \
469 "HelloWorldDHelloWorldEHelloWorldFHelloWorldGHelloWorldHHelloWorldI"),
470 NULL);
471
472 ZCBOR_STATE_D(state_d, 0, payload, 13, 1);
473 ZCBOR_STATE_D(state_d2, 0, payload, sizeof(payload), 1);
474
475 zassert_true(zcbor_bstr_decode(state_d2, &output), NULL);
476 zassert_true(zcbor_bstr_decode_fragment(state_d, &output_frags[0]), NULL);
477
478 for (int i = 1; i < 18; i++) {
479 zassert_true(zcbor_payload_at_end(state_d), NULL);
480 zassert_false(zcbor_is_last_fragment(&output_frags[i - 1]), NULL);
481 memcpy(frag_payload, &payload[11 * i + 2], 11); // + 2 because of the CBOR header
482 zcbor_update_state(state_d, frag_payload, 11);
483 zcbor_next_fragment(state_d, &output_frags[i - 1], &output_frags[i]);
484 }
485 zassert_true(zcbor_payload_at_end(state_d), NULL);
486 zassert_true(zcbor_is_last_fragment(&output_frags[17]), NULL);
487
488 uint8_t spliced[200];
489 size_t out_len = sizeof(spliced);
490
491 zassert_true(zcbor_validate_string_fragments(output_frags, 18), NULL);
492 zassert_true(zcbor_splice_string_fragments(output_frags, 18, spliced,
493 &out_len), NULL);
494
495 zassert_equal(198, output.len, NULL);
496 zassert_mem_equal(output.value,
497 "HelloWorld1HelloWorld2HelloWorld3HelloWorld4HelloWorld5HelloWorld6" \
498 "HelloWorld7HelloWorld8HelloWorld9HelloWorldAHelloWorldBHelloWorldC" \
499 "HelloWorldDHelloWorldEHelloWorldFHelloWorldGHelloWorldHHelloWorldI",
500 198, NULL);
501
502 // Start negative tests
503
504 zassert_false(zcbor_validate_string_fragments(output_frags, 17), NULL); // Last fragment missing
505
506 output_frags[2].total_len--;
507 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Mismatch total_len
508 output_frags[2].total_len += 2;
509 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Mismatch total_len
510 output_frags[2].total_len--;
511
512 memcpy(&output_frags_backup, &output_frags[16], sizeof(output_frags[0]));
513 memcpy(&output_frags[16], &output_frags[17], sizeof(output_frags[0])); // Move last fragment
514
515 zassert_false(zcbor_validate_string_fragments(output_frags, 17), NULL); // Second-to-last fragment missing
516 memcpy(&output_frags[16], &output_frags_backup, sizeof(output_frags[0])); // Restore
517
518 output_frags[6].offset++;
519 output_frags[7].offset--;
520 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Slight error in offset vs. fragment lengths.
521 output_frags[6].offset--;
522 output_frags[7].offset++;
523
524 for (int i = 0; i < 18; i++) {
525 output_frags[i].total_len -= output_frags[17].fragment.len;
526 }
527
528 zassert_true(zcbor_validate_string_fragments(output_frags, 17), NULL); // Should work with 17 fragments now.
529 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Last fragment will extend past total_len.
530
531 for (int i = 0; i < 18; i++) {
532 output_frags[i].total_len += (output_frags[17].fragment.len - 1);
533 }
534 zassert_false(zcbor_validate_string_fragments(output_frags, 18), NULL); // Last fragment will extend past total_len by a single byte.
535 output_frags[17].fragment.len--;
536 zassert_true(zcbor_validate_string_fragments(output_frags, 18), NULL); // Should work with shorter last fragment
537 output_frags[17].fragment.len++; // Restore
538 for (int i = 0; i < 18; i++) {
539 output_frags[i].total_len += 1; // Restore
540 }
541
542 zassert_true(zcbor_validate_string_fragments(output_frags, 18), NULL); // Check that all errors were restored correctly.
543 }
544
545
546 /** This test creates the following structure, wrapped in a BSTR:
547 *
548 * (
549 * 42,
550 * "Hello World",
551 * [
552 * true,
553 * nil,
554 * ]
555 * )
556 *
557 * This structures is then split in three fragments (output_frags) like so:
558 * 1st fragment (8 bytes):
559 * (
560 * 42,
561 * "Hell
562 *
563 * 2nd fragment (8 bytes):
564 * o World",
565 * [
566 *
567 * 3rd fragment (3 bytes or 2 bytes if ZCBOR_CANONICAL is defined):
568 * true,
569 * nil,
570 * ]
571 * )
572 *
573 * This means that the TSTR has to be handled using fragments (tstr_frags)
574 * as well.
575 */
ZTEST(zcbor_unit_tests,test_bstr_cbor_fragments)576 ZTEST(zcbor_unit_tests, test_bstr_cbor_fragments)
577 {
578 uint8_t payload[100];
579 ZCBOR_STATE_E(state_e, 2, payload, sizeof(payload), 0);
580 struct zcbor_string output;
581 struct zcbor_string_fragment output_frags[3];
582 struct zcbor_string_fragment tstr_frags[2];
583
584 zassert_true(zcbor_bstr_start_encode(state_e), NULL); // 1 B
585 zassert_true(zcbor_uint32_put(state_e, 42), NULL); // 2 B
586 zassert_true(zcbor_tstr_put_lit(state_e, "Hello World"), NULL); // 12 B
587 zassert_true(zcbor_list_start_encode(state_e, 2), NULL); // 1 B
588 zassert_true(zcbor_bool_put(state_e, true), NULL); // 1 B
589 zassert_true(zcbor_nil_put(state_e, NULL), NULL); // 1 B
590 zassert_true(zcbor_list_end_encode(state_e, 2), NULL); // 1 B
591 zassert_true(zcbor_bstr_end_encode(state_e, NULL), NULL); // 0 B
592
593 ZCBOR_STATE_D(state_d, 2, payload, 8, 1);
594 ZCBOR_STATE_D(state_d2, 0, payload, sizeof(payload), 1);
595
596 #ifdef ZCBOR_CANONICAL
597 #define EXP_TOTAL_LEN 17
598 #else
599 #define EXP_TOTAL_LEN 18
600 #endif
601
602 zassert_true(zcbor_bstr_decode(state_d2, &output), NULL);
603 zassert_false(zcbor_bstr_start_decode(state_d, &output), NULL);
604 zassert_true(zcbor_bstr_start_decode_fragment(state_d, &output_frags[0]), NULL);
605 zassert_equal_ptr(&payload[1], output_frags[0].fragment.value, NULL);
606 zassert_equal(7, output_frags[0].fragment.len, NULL);
607 zassert_equal(EXP_TOTAL_LEN, output_frags[0].total_len, "%d != %d\r\n", EXP_TOTAL_LEN, output_frags[0].total_len);
608 zassert_equal(0, output_frags[0].offset, NULL);
609 zassert_false(zcbor_is_last_fragment(&output_frags[0]), NULL);
610 zassert_true(zcbor_uint32_expect(state_d, 42), NULL);
611 zassert_false(zcbor_tstr_expect_lit(state_d, "Hello World"), NULL);
612 zassert_true(zcbor_tstr_decode_fragment(state_d, &tstr_frags[0]), NULL);
613 zassert_equal_ptr(&payload[4], tstr_frags[0].fragment.value, NULL);
614 zassert_equal(4, tstr_frags[0].fragment.len, NULL);
615 zassert_equal(11, tstr_frags[0].total_len, NULL);
616 zassert_equal(0, tstr_frags[0].offset, NULL);
617
618 zassert_true(zcbor_payload_at_end(state_d), NULL);
619 zcbor_update_state(state_d, &payload[8], 8);
620 zassert_false(zcbor_bstr_decode_fragment(state_d, &output_frags[1]), NULL);
621 zcbor_bstr_next_fragment(state_d, &output_frags[0], &output_frags[1]);
622 zassert_equal_ptr(&payload[8], output_frags[1].fragment.value, NULL);
623 zassert_equal(8, output_frags[1].fragment.len, "%d != %d\r\n", 3, output_frags[1].fragment.len);
624 zassert_equal(EXP_TOTAL_LEN, output_frags[1].total_len, "%d != %d\r\n", EXP_TOTAL_LEN, output_frags[1].total_len);
625 zassert_equal(7, output_frags[1].offset, NULL);
626 zassert_false(zcbor_is_last_fragment(&output_frags[1]), NULL);
627 zcbor_next_fragment(state_d, &tstr_frags[0], &tstr_frags[1]);
628 zassert_equal_ptr(&payload[8], tstr_frags[1].fragment.value, NULL);
629 zassert_equal(7, tstr_frags[1].fragment.len, "%d != %d\r\n", 7, tstr_frags[1].fragment.len);
630 zassert_equal(11, tstr_frags[1].total_len, NULL);
631 zassert_equal(4, tstr_frags[1].offset, NULL);
632 zassert_true(zcbor_is_last_fragment(&tstr_frags[1]), NULL);
633 zassert_true(zcbor_list_start_decode(state_d), NULL);
634
635 zassert_true(zcbor_payload_at_end(state_d), NULL);
636 zcbor_update_state(state_d, &payload[16], sizeof(payload) - 16);
637 zassert_false(zcbor_bstr_decode_fragment(state_d, &output_frags[2]), NULL);
638 zcbor_bstr_next_fragment(state_d, &output_frags[1], &output_frags[2]);
639 zassert_equal_ptr(&payload[16], output_frags[2].fragment.value, NULL);
640 zassert_equal(EXP_TOTAL_LEN - 15,
641 output_frags[2].fragment.len, NULL);
642 zassert_equal(EXP_TOTAL_LEN, output_frags[2].total_len, NULL);
643 zassert_equal(15, output_frags[2].offset, NULL);
644 zassert_true(zcbor_is_last_fragment(&output_frags[2]), NULL);
645 zassert_true(zcbor_bool_expect(state_d, true), NULL);
646 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
647 zassert_true(zcbor_list_end_decode(state_d), NULL);
648
649 uint8_t spliced[19];
650 output.value = spliced;
651 output.len = sizeof(spliced);
652
653 zassert_true(zcbor_validate_string_fragments(output_frags, 3), NULL);
654 zassert_true(zcbor_splice_string_fragments(output_frags, 3, spliced, &output.len), NULL);
655
656 zassert_equal(EXP_TOTAL_LEN, output.len, NULL);
657 zassert_mem_equal(output.value, &payload[1], EXP_TOTAL_LEN, NULL);
658
659 output.len = sizeof(spliced);
660
661 zassert_true(zcbor_validate_string_fragments(tstr_frags, 2), NULL);
662 zassert_true(zcbor_splice_string_fragments(tstr_frags, 2, spliced, &output.len), NULL);
663
664 zassert_equal(11, output.len, NULL);
665 zassert_mem_equal(output.value, &payload[4], 11, NULL);
666 }
667
668
ZTEST(zcbor_unit_tests,test_canonical_list)669 ZTEST(zcbor_unit_tests, test_canonical_list)
670 {
671 #ifndef ZCBOR_CANONICAL
672 printk("Skip on non-canonical builds.\n");
673 #else
674 uint8_t payload1[100];
675 uint8_t payload2[100];
676 uint8_t exp_payload[] = {0x8A, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
677 ZCBOR_STATE_E(state_e1, 1, payload1, sizeof(payload1), 0);
678 ZCBOR_STATE_E(state_e2, 1, payload2, sizeof(payload2), 0);
679
680 zassert_true(zcbor_list_start_encode(state_e1, 10), "%d\r\n", state_e1->constant_state->error);
681 for (int i = 0; i < 30; i++) {
682 zassert_true(zcbor_uint32_put(state_e1, i), NULL);
683 }
684 zassert_false(zcbor_list_end_encode(state_e1, 10), NULL);
685 zassert_equal(ZCBOR_ERR_HIGH_ELEM_COUNT, zcbor_pop_error(state_e1), NULL);
686
687 zassert_true(zcbor_list_start_encode(state_e2, 1000), NULL);
688 for (int i = 0; i < 10; i++) {
689 zassert_true(zcbor_uint32_put(state_e2, i), NULL);
690 }
691 zassert_true(zcbor_list_end_encode(state_e2, 1000), NULL);
692 zassert_equal(sizeof(exp_payload), state_e2->payload - payload2, NULL);
693 zassert_mem_equal(exp_payload, payload2, sizeof(exp_payload), NULL);
694 #endif
695 }
696
697
ZTEST(zcbor_unit_tests,test_int)698 ZTEST(zcbor_unit_tests, test_int)
699 {
700 uint8_t payload1[100];
701 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
702 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16);
703
704 /* Encode all numbers */
705 /* Arbitrary positive numbers in each size */
706 int8_t int8 = 12;
707 int16_t int16 = 1234;
708 int32_t int32 = 12345678;
709 int64_t int64 = 1234567812345678;
710
711 zassert_true(zcbor_int_encode(state_e, &int8, sizeof(int8)), NULL);
712 zassert_true(zcbor_int_encode(state_e, &int16, sizeof(int16)), NULL);
713 zassert_true(zcbor_int_encode(state_e, &int32, sizeof(int32)), NULL);
714 zassert_true(zcbor_int_encode(state_e, &int64, sizeof(int64)), NULL);
715
716 /* Arbitrary negative numbers in each size */
717 int8 = -12;
718 int16 = -1234;
719 int32 = -12345678;
720 int64 = -1234567812345678;
721
722 zassert_true(zcbor_int_encode(state_e, &int8, sizeof(int8)), NULL);
723 zassert_true(zcbor_int_encode(state_e, &int16, sizeof(int16)), NULL);
724 zassert_true(zcbor_int_encode(state_e, &int32, sizeof(int32)), NULL);
725 zassert_true(zcbor_int_encode(state_e, &int64, sizeof(int64)), NULL);
726
727 /* Check against overflow (negative). */
728 zassert_true(zcbor_int32_put(state_e, INT8_MIN - 1), NULL);
729 zassert_true(zcbor_int32_put(state_e, INT16_MIN - 1), NULL);
730 zassert_true(zcbor_int64_put(state_e, (int64_t)INT32_MIN - 1), NULL);
731 /* Check absolute minimum number. */
732 zassert_true(zcbor_int64_put(state_e, INT64_MIN), NULL);
733
734 /* Check against overflow (positive). */
735 zassert_true(zcbor_int32_put(state_e, INT8_MAX + 1), NULL);
736 zassert_true(zcbor_int32_put(state_e, INT16_MAX + 1), NULL);
737 zassert_true(zcbor_int64_put(state_e, (int64_t)INT32_MAX + 1), NULL);
738 /* Check absolute maximum number. */
739 zassert_true(zcbor_int64_put(state_e, INT64_MAX), NULL);
740
741 /* Decode all numbers */
742 /* Arbitrary positive numbers in each size */
743 zassert_true(zcbor_int_decode(state_d, &int8, sizeof(int8)), NULL);
744 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
745 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
746 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
747 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
748 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
749 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
750 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
751 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
752 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
753
754 zassert_equal(int8, 12, "%d\r\n", int8);
755 zassert_equal(int16, 1234, "%d\r\n", int16);
756 zassert_equal(int32, 12345678, "%d\r\n", int32);
757 zassert_equal(int64, 1234567812345678, "%d\r\n", int64);
758
759 /* Arbitrary negative numbers in each size */
760 zassert_true(zcbor_int_decode(state_d, &int8, sizeof(int8)), NULL);
761 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
762 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
763 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
764 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
765 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
766 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
767 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
768 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
769 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
770
771 zassert_equal(int8, -12, NULL);
772 zassert_equal(int16, -1234, NULL);
773 zassert_equal(int32, -12345678, NULL);
774 zassert_equal(int64, -1234567812345678, NULL);
775
776 /* Check against overflow (negative). */
777 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
778 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
779 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
780 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
781 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
782 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
783 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
784 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
785 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
786
787 zassert_equal(int16, INT8_MIN - 1, NULL);
788 zassert_equal(int32, INT16_MIN - 1, NULL);
789 zassert_equal(int64, (int64_t)INT32_MIN - 1, NULL);
790
791 /* Check absolute minimum number. */
792 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
793
794 zassert_equal(int64, INT64_MIN, NULL);
795
796 /* Check against overflow (positive). */
797 zassert_false(zcbor_int_decode(state_d, &int16, sizeof(int8)), NULL);
798 zassert_true(zcbor_int_decode(state_d, &int16, sizeof(int16)), NULL);
799 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int8)), NULL);
800 zassert_false(zcbor_int_decode(state_d, &int32, sizeof(int16)), NULL);
801 zassert_true(zcbor_int_decode(state_d, &int32, sizeof(int32)), NULL);
802 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int8)), NULL);
803 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int16)), NULL);
804 zassert_false(zcbor_int_decode(state_d, &int64, sizeof(int32)), NULL);
805 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
806
807 zassert_equal(int16, INT8_MAX + 1, NULL);
808 zassert_equal(int32, INT16_MAX + 1, NULL);
809 zassert_equal(int64, (int64_t)INT32_MAX + 1, NULL);
810
811 /* Check absolute maximum number. */
812 zassert_true(zcbor_int_decode(state_d, &int64, sizeof(int64)), NULL);
813
814 zassert_equal(int64, INT64_MAX, NULL);
815 }
816
817
ZTEST(zcbor_unit_tests,test_uint)818 ZTEST(zcbor_unit_tests, test_uint)
819 {
820 uint8_t payload1[100];
821 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
822 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16);
823
824 /* Encode all numbers */
825 /* Arbitrary positive numbers in each size */
826 uint8_t uint8 = 12;
827 uint16_t uint16 = 1234;
828 uint32_t uint32 = 12345678;
829 uint64_t uint64 = 1234567812345678;
830
831 zassert_true(zcbor_uint_encode(state_e, &uint8, sizeof(uint8)), NULL);
832 zassert_true(zcbor_uint_encode(state_e, &uint16, sizeof(uint16)), NULL);
833 zassert_true(zcbor_uint_encode(state_e, &uint32, sizeof(uint32)), NULL);
834 zassert_true(zcbor_uint_encode(state_e, &uint64, sizeof(uint64)), NULL);
835
836 /* Check absolute maximum number. */
837 zassert_true(zcbor_uint64_put(state_e, UINT64_MAX), NULL);
838
839 /* Decode all numbers */
840 /* Arbitrary positive numbers in each size */
841 zassert_true(zcbor_uint_decode(state_d, &uint8, sizeof(uint8)), NULL);
842 zassert_false(zcbor_uint_decode(state_d, &uint16, sizeof(uint8)), NULL);
843 zassert_true(zcbor_uint_decode(state_d, &uint16, sizeof(uint16)), NULL);
844 zassert_false(zcbor_uint_decode(state_d, &uint32, sizeof(uint8)), NULL);
845 zassert_false(zcbor_uint_decode(state_d, &uint32, sizeof(uint16)), NULL);
846 zassert_true(zcbor_uint_decode(state_d, &uint32, sizeof(uint32)), NULL);
847 zassert_false(zcbor_uint_decode(state_d, &uint64, sizeof(uint8)), NULL);
848 zassert_false(zcbor_uint_decode(state_d, &uint64, sizeof(uint16)), NULL);
849 zassert_false(zcbor_uint_decode(state_d, &uint64, sizeof(uint32)), NULL);
850 zassert_true(zcbor_uint_decode(state_d, &uint64, sizeof(uint64)), NULL);
851
852 zassert_equal(uint8, 12, "%d\r\n", uint8);
853 zassert_equal(uint16, 1234, "%d\r\n", uint16);
854 zassert_equal(uint32, 12345678, "%d\r\n", uint32);
855 zassert_equal(uint64, 1234567812345678, "%d\r\n", uint64);
856
857 /* Check absolute maximum number. */
858 zassert_true(zcbor_uint_decode(state_d, &uint64, sizeof(uint64)), NULL);
859
860 zassert_equal(uint64, UINT64_MAX, NULL);
861 }
862
863
864 /** This tests a regression in big-endian encoding, where small numbers (like 0)
865 * where handled incorrectly (1-off), because of an error in get_result().
866 */
ZTEST(zcbor_unit_tests,test_encode_int_0)867 ZTEST(zcbor_unit_tests, test_encode_int_0)
868 {
869 uint8_t payload1[100];
870 uint32_t values_32[2] = {0, UINT32_MAX};
871 uint64_t values_64[2] = {0, UINT64_MAX};
872 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
873 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16);
874
875 zassert_true(zcbor_uint32_put(state_e, values_32[0]));
876 zassert_true(zcbor_uint64_put(state_e, values_64[0]));
877 zassert_true(zcbor_uint32_expect(state_d, 0));
878 zassert_true(zcbor_uint64_expect(state_d, 0));
879 }
880
881
ZTEST(zcbor_unit_tests,test_simple)882 ZTEST(zcbor_unit_tests, test_simple)
883 {
884 uint8_t payload1[100];
885 ZCBOR_STATE_E(state_e, 1, payload1, sizeof(payload1), 0);
886 ZCBOR_STATE_D(state_d, 1, payload1, sizeof(payload1), 16);
887 uint8_t simple1 = 0;
888 uint8_t simple2 = 2;
889
890 zassert_true(zcbor_simple_encode(state_e, &simple1), NULL);
891 zassert_true(zcbor_simple_put(state_e, 14), NULL);
892 zassert_true(zcbor_simple_put(state_e, 22), NULL);
893 zassert_true(zcbor_simple_put(state_e, 24), NULL);
894 zassert_true(zcbor_simple_put(state_e, 255), NULL);
895
896 zassert_true(zcbor_simple_decode(state_d, &simple2), NULL);
897 zassert_true(zcbor_simple_expect(state_d, 14), NULL);
898 zassert_true(zcbor_nil_expect(state_d, NULL), NULL);
899 zassert_false(zcbor_undefined_expect(state_d, NULL), NULL);
900 zassert_true(zcbor_simple_expect(state_d, 24), NULL);
901 zassert_false(zcbor_simple_expect(state_d, 254), NULL);
902 zassert_true(zcbor_simple_decode(state_d, &simple1), NULL);
903 zassert_equal(0, simple2, NULL);
904 zassert_equal(255, simple1, NULL);
905 }
906
907
ZTEST(zcbor_unit_tests,test_header_len)908 ZTEST(zcbor_unit_tests, test_header_len)
909 {
910 zassert_equal(1, zcbor_header_len(0), NULL);
911 zassert_equal(1, zcbor_header_len(23), NULL);
912 zassert_equal(2, zcbor_header_len(24), NULL);
913 zassert_equal(2, zcbor_header_len(0xFF), NULL);
914 zassert_equal(3, zcbor_header_len(0x100), NULL);
915 zassert_equal(3, zcbor_header_len(0xFFFF), NULL);
916 zassert_equal(5, zcbor_header_len(0x10000), NULL);
917 zassert_equal(5, zcbor_header_len(0xFFFFFFFF), NULL);
918 #if SIZE_MAX >= 0x100000000ULL
919 zassert_equal(9, zcbor_header_len(0x100000000), NULL);
920 zassert_equal(9, zcbor_header_len(0xFFFFFFFFFFFFFFFF), NULL);
921 #endif
922 }
923
924
ZTEST(zcbor_unit_tests,test_compare_strings)925 ZTEST(zcbor_unit_tests, test_compare_strings)
926 {
927 const uint8_t hello[] = "hello";
928 const uint8_t hello2[] = "hello";
929 const uint8_t long_str[] = "This is a very long string. This is a very long string. "
930 "This is a very long string. This is a very long string. This is a very long string. "
931 "This is a very long string. This is a very long string. This is a very long string. "
932 "This is a very long string. This is a very long string. This is a very long string. "
933 "This is a very long string. This is a very long string. This is a very long string. "
934 "This is a very long string. This is a very long string. This is a very long string. "
935 "This is a very long string. This is a very long string. This is a very long string. "
936 "This is a very long string. This is a very long string. This is a very long string. "
937 "This is a very long string. This is a very long string. This is a very long string. "
938 "This is a very long string. This is a very long string. This is a very long string. "
939 "This is a very long string. This is a very long string. This is a very long string. "
940 "This is a very long string. This is a very long string. This is a very long string. ";
941 const uint8_t long_str2[] = "This is a very long string. This is a very long string. "
942 "This is a very long string. This is a very long string. This is a very long string. "
943 "This is a very long string. This is a very long string. This is a very long string. "
944 "This is a very long string. This is a very long string. This is a very long string. "
945 "This is a very long string. This is a very long string. This is a very long string. "
946 "This is a very long string. This is a very long string. This is a very long string. "
947 "This is a very long string. This is a very long string. This is a very long string. "
948 "This is a very long string. This is a very long string. This is a very long string. "
949 "This is a very long string. This is a very long string. This is a very long string. "
950 "This is a very long string. This is a very long string. This is a very long string. "
951 "This is a very long string. 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 struct zcbor_string test_str1_hello = {hello, 5};
954 struct zcbor_string test_str2_hello_short = {hello, 4};
955 struct zcbor_string test_str3_hello_offset = {&hello[1], 4};
956 struct zcbor_string test_str4_long = {long_str, sizeof(long_str)};
957 struct zcbor_string test_str5_long2 = {long_str2, sizeof(long_str2)};
958 struct zcbor_string test_str6_empty = {hello, 0};
959 struct zcbor_string test_str7_hello2 = {hello2, 5};
960 struct zcbor_string test_str8_empty = {hello2, 0};
961 struct zcbor_string test_str9_NULL = {NULL, 0};
962
963 zassert_true(zcbor_compare_strings(&test_str1_hello, &test_str1_hello), NULL);
964 zassert_true(zcbor_compare_strings(&test_str1_hello, &test_str7_hello2), NULL);
965 zassert_true(zcbor_compare_strings(&test_str4_long, &test_str5_long2), NULL);
966 zassert_true(zcbor_compare_strings(&test_str6_empty, &test_str8_empty), NULL);
967
968 zassert_false(zcbor_compare_strings(&test_str2_hello_short, &test_str7_hello2), NULL);
969 zassert_false(zcbor_compare_strings(&test_str3_hello_offset, &test_str7_hello2), NULL);
970 zassert_false(zcbor_compare_strings(&test_str1_hello, NULL), NULL);
971 zassert_false(zcbor_compare_strings(NULL, &test_str5_long2), NULL);
972 zassert_false(zcbor_compare_strings(&test_str6_empty, NULL), NULL);
973 zassert_false(zcbor_compare_strings(&test_str1_hello, &test_str9_NULL), NULL);
974 zassert_false(zcbor_compare_strings(&test_str9_NULL, &test_str5_long2), NULL);
975 }
976
977
ZTEST(zcbor_unit_tests,test_any_skip)978 ZTEST(zcbor_unit_tests, test_any_skip)
979 {
980 uint8_t payload[200];
981 ZCBOR_STATE_E(state_e, 1, payload, sizeof(payload), 0);
982 ZCBOR_STATE_D(state_d, 0, payload, sizeof(payload), 10);
983 size_t exp_elem_count = 10;
984
985 zassert_true(zcbor_uint32_put(state_e, 10), NULL);
986 zassert_true(zcbor_any_skip(state_d, NULL));
987 zassert_equal(state_d->payload, state_e->payload, NULL);
988 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
989
990 zassert_true(zcbor_int64_put(state_e, -10000000000000), NULL);
991 zassert_true(zcbor_any_skip(state_d, NULL));
992 zassert_equal(state_d->payload, state_e->payload, NULL);
993 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
994
995 zassert_true(zcbor_bstr_put_term(state_e, "hello"), NULL);
996 zassert_true(zcbor_any_skip(state_d, NULL));
997 zassert_equal(state_d->payload, state_e->payload, NULL);
998 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
999
1000 zassert_true(zcbor_tstr_put_term(state_e, "world"), NULL);
1001 zassert_true(zcbor_any_skip(state_d, NULL));
1002 zassert_equal(state_d->payload, state_e->payload, NULL);
1003 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1004
1005 zassert_true(zcbor_nil_put(state_e, NULL), NULL);
1006 zassert_true(zcbor_any_skip(state_d, NULL));
1007 zassert_equal(state_d->payload, state_e->payload, NULL);
1008 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1009
1010 zassert_true(zcbor_float64_put(state_e, 3.14), NULL);
1011 zassert_true(zcbor_any_skip(state_d, NULL));
1012 zassert_equal(state_d->payload, state_e->payload, NULL);
1013 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1014
1015 zassert_true(zcbor_list_start_encode(state_e, 6), NULL);
1016 zassert_true(zcbor_uint32_put(state_e, 10), NULL);
1017 zassert_true(zcbor_int64_put(state_e, -10000000000000), NULL);
1018 zassert_true(zcbor_bstr_put_term(state_e, "hello"), NULL);
1019 zassert_true(zcbor_tstr_put_term(state_e, "world"), NULL);
1020 zassert_true(zcbor_bool_put(state_e, true), NULL);
1021 zassert_true(zcbor_float64_put(state_e, 3.14), NULL);
1022 zassert_true(zcbor_list_end_encode(state_e, 6), NULL);
1023 zassert_true(zcbor_any_skip(state_d, NULL));
1024 zassert_equal(state_d->payload, state_e->payload, NULL);
1025 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1026
1027 zassert_true(zcbor_tag_encode(state_e, 1), NULL);
1028 zassert_true(zcbor_tag_encode(state_e, 200), NULL);
1029 zassert_true(zcbor_tag_encode(state_e, 3000), NULL);
1030 zassert_true(zcbor_map_start_encode(state_e, 6), NULL);
1031 zassert_true(zcbor_uint32_put(state_e, 10), NULL);
1032 zassert_true(zcbor_int64_put(state_e, -10000000000000), NULL);
1033 zassert_true(zcbor_bstr_put_term(state_e, "hello"), NULL);
1034 zassert_true(zcbor_tstr_put_term(state_e, "world"), NULL);
1035 zassert_true(zcbor_undefined_put(state_e, NULL), NULL);
1036 zassert_true(zcbor_float64_put(state_e, 3.14), NULL);
1037 zassert_true(zcbor_map_end_encode(state_e, 6), NULL);
1038 zassert_true(zcbor_any_skip(state_d, NULL));
1039 zassert_equal(state_d->payload, state_e->payload, "0x%x != 0x%x\n",
1040 state_d->payload, state_e->payload);
1041 zassert_equal(state_d->elem_count, --exp_elem_count, NULL);
1042 }
1043
1044 ZTEST_SUITE(zcbor_unit_tests, NULL, NULL, NULL, NULL, NULL);
1045