1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <pet_encode.h>
9 #include <zcbor_encode.h>
10 
11 #ifndef ZCBOR_CANONICAL
12 #define TEST_INDEFINITE_LENGTH_ARRAYS
13 #endif
14 #include <common_test.h>
15 
16 
17 /* This test uses generated code to encode a 'Pet' instance. It populates the
18  * generated struct, and runs the generated encoding function, then checks that
19  * everything is correct.
20  */
ZTEST(cbor_encode_test2,test_pet)21 ZTEST(cbor_encode_test2, test_pet)
22 {
23 	struct Pet pet = {
24 		.names = {{.value = "foo", .len = 3}, {.value = "bar", .len = 3}},
25 		.names_count = 2,
26 		.birthday = {.value = (uint8_t[]){1,2,3,4,5,6,7,8}, .len = 8},
27 		.species_choice = Pet_species_dog_c
28 	};
29 	uint8_t exp_output[] = {
30 		LIST(3),
31 		LIST(2),
32 			0x63, 0x66, 0x6f, 0x6f, /* foo */
33 			0x63, 0x62, 0x61, 0x72, /* bar */
34 		END
35 		0x48, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
36 		0x02, /* 2: dog */
37 		END
38 	};
39 
40 	uint8_t output[25];
41 	size_t out_len;
42 
43 	/* Check that encoding succeeded. */
44 	zassert_equal(ZCBOR_SUCCESS, cbor_encode_Pet(output, sizeof(output), &pet, &out_len), NULL);
45 
46 	/* Check that the resulting length is correct. */
47 	zassert_equal(sizeof(exp_output), out_len, NULL);
48 	/* Check the payload contents. */
49 	zassert_mem_equal(exp_output, output, sizeof(exp_output), NULL);
50 }
51 
52 
53 /* This test uses the CBOR encoding library directly, i.e. no generated code.
54  * It has no checking against a CDDL schema, but follows the "Pet" structure.
55  * It sets up the zcbor_state_t variable.
56  * It then makes a number of calls to functions in zcbor_encode.h and checks the
57  * resulting payload agains the expected output.
58  */
ZTEST(cbor_encode_test2,test_pet_raw)59 ZTEST(cbor_encode_test2, test_pet_raw)
60 {
61 	uint8_t payload[100] = {0};
62 	ZCBOR_STATE_E(state, 4, payload, sizeof(payload), 1);
63 
64 	uint8_t exp_output[] = {
65 		LIST(3),
66 		LIST(2),
67 			0x65, 0x66, 0x69, 0x72, 0x73, 0x74, /* first */
68 			0x66, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, /* second */
69 		END
70 		0x48, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
71 		0x02, /* 2: dog */
72 		END
73 	};
74 
75 	bool res = zcbor_list_start_encode(state, 0);
76 	zassert_true(res, NULL);
77 
78 	res = res && zcbor_list_start_encode(state, 0);
79 	zassert_true(res, NULL);
80 	res = res && zcbor_tstr_put_lit(state, "first");
81 	zassert_true(res, NULL);
82 	res = res && zcbor_tstr_put_lit(state, "second");
83 	zassert_true(res, NULL);
84 	res = res && zcbor_list_end_encode(state, 0);
85 	zassert_true(res, NULL);
86 	uint8_t timestamp[8] = {1, 2, 3, 4, 5, 6, 7, 8};
87 	struct zcbor_string timestamp_str = {
88 		.value = timestamp,
89 		.len = sizeof(timestamp),
90 	};
91 	res = res && zcbor_bstr_encode(state, &timestamp_str);
92 	zassert_true(res, NULL);
93 	res = res && zcbor_uint32_put(state, 2 /* dog */);
94 	zassert_true(res, NULL);
95 	res = res && zcbor_list_end_encode(state, 0);
96 
97 	/* Check that encoding succeeded. */
98 	zassert_true(res, NULL);
99 	/* Check that the resulting length is correct. */
100 	zassert_equal(sizeof(exp_output), state->payload - payload, "%d != %d\r\n",
101 		sizeof(exp_output), state->payload - payload);
102 	/* Check the payload contents. */
103 	zassert_mem_equal(exp_output, payload, sizeof(exp_output), NULL);
104 }
105 
106 ZTEST_SUITE(cbor_encode_test2, NULL, NULL, NULL, NULL, NULL);
107