Skip to content

Commit 0948a1e

Browse files
committed
tests/encode: add streaming encode coverage
- Add STREAMING=1 variants for test1_suit, test2_simple, test3_corner_cases, and test4_senml. - Exercise streaming encode paths via stream_write helpers and cbor_stream_encode_* entrypoints.
1 parent 1d3720a commit 0948a1e

13 files changed

Lines changed: 459 additions & 2 deletions

File tree

tests/encode/test1_suit/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ set(py_command
2323
${bit_arg}
2424
)
2525

26+
if (STREAMING)
27+
list(APPEND py_command --stream-encode-functions)
28+
endif()
29+
2630
execute_process(
2731
COMMAND ${py_command}
2832
COMMAND_ERROR_IS_FATAL ANY

tests/encode/test1_suit/src/main.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,70 @@
99
#include "manifest3_encode.h"
1010
#include "zcbor_print.h"
1111

12+
#ifdef STREAMING
13+
#include <string.h>
14+
15+
struct stream_ctx {
16+
uint8_t *buf;
17+
size_t size;
18+
size_t pos;
19+
};
20+
21+
static int stream_write(void *user_data, const uint8_t *data, size_t len)
22+
{
23+
struct stream_ctx *ctx = (struct stream_ctx *)user_data;
24+
25+
if (!ctx || !data) {
26+
return -1;
27+
}
28+
if (len == 0) {
29+
return 0;
30+
}
31+
if (ctx->pos + len > ctx->size) {
32+
return -1;
33+
}
34+
35+
memcpy(&ctx->buf[ctx->pos], data, len);
36+
ctx->pos += len;
37+
return (int)len;
38+
}
39+
40+
static int stream_encode_SUIT_Command_Sequence(uint8_t *payload, size_t payload_len,
41+
const struct SUIT_Command_Sequence *input, size_t *out_len)
42+
{
43+
struct stream_ctx ctx = {
44+
.buf = payload,
45+
.size = payload_len,
46+
.pos = 0,
47+
};
48+
int rc = cbor_stream_encode_SUIT_Command_Sequence(stream_write, &ctx, input, NULL, out_len);
49+
50+
if (rc == ZCBOR_SUCCESS) {
51+
zassert_equal(*out_len, ctx.pos, NULL);
52+
}
53+
return rc;
54+
}
55+
56+
static int stream_encode_SUIT_Outer_Wrapper(uint8_t *payload, size_t payload_len,
57+
const struct SUIT_Outer_Wrapper *input, size_t *out_len)
58+
{
59+
struct stream_ctx ctx = {
60+
.buf = payload,
61+
.size = payload_len,
62+
.pos = 0,
63+
};
64+
int rc = cbor_stream_encode_SUIT_Outer_Wrapper(stream_write, &ctx, input, NULL, out_len);
65+
66+
if (rc == ZCBOR_SUCCESS) {
67+
zassert_equal(*out_len, ctx.pos, NULL);
68+
}
69+
return rc;
70+
}
71+
72+
#define cbor_encode_SUIT_Command_Sequence stream_encode_SUIT_Command_Sequence
73+
#define cbor_encode_SUIT_Outer_Wrapper stream_encode_SUIT_Outer_Wrapper
74+
#endif /* STREAMING */
75+
1276
/* draft-ietf-suit-manifest-02 Example 0 */
1377
uint8_t test_vector0_02[] = {
1478
0xa2, 0x01, 0x58, 0x54, 0xd2, 0x84, 0x43, 0xa1, 0x01,

tests/encode/test1_suit/testcase.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ tests:
77
- qemu_malta/qemu_malta/be
88
tags: zcbor encode canonical test1
99
extra_args: CANONICAL=CANONICAL
10+
11+
zcbor.encode.test1_suit.streaming_canonical:
12+
platform_allow:
13+
- native_sim
14+
- native_sim/native/64
15+
- mps2/an521/cpu0
16+
- qemu_malta/qemu_malta/be
17+
tags: zcbor encode streaming canonical test1
18+
extra_args: STREAMING=1 CANONICAL=CANONICAL

tests/encode/test2_simple/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ set(py_command
2222
--file-header ${CMAKE_CURRENT_LIST_DIR}/file_header_copyright.txt
2323
)
2424

25+
if (STREAMING)
26+
list(APPEND py_command --stream-encode-functions)
27+
endif()
28+
2529
execute_process(
2630
COMMAND ${py_command}
2731
COMMAND_ERROR_IS_FATAL ANY
@@ -31,4 +35,3 @@ include(${PROJECT_BINARY_DIR}/pet.cmake)
3135

3236
target_link_libraries(pet PRIVATE zephyr_interface)
3337
target_link_libraries(app PRIVATE pet)
34-

tests/encode/test2_simple/src/main.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,65 @@
1313
#endif
1414
#include <common_test.h>
1515

16+
#ifdef STREAMING
17+
#include <string.h>
18+
#include <errno.h>
19+
20+
struct stream_ctx {
21+
uint8_t *buf;
22+
size_t size;
23+
size_t pos;
24+
};
25+
26+
static int stream_write(void *user_data, const uint8_t *data, size_t len)
27+
{
28+
struct stream_ctx *ctx = (struct stream_ctx *)user_data;
29+
30+
if (!ctx) {
31+
return -1;
32+
}
33+
if (len == 0) {
34+
return 0;
35+
}
36+
if (!data) {
37+
return -1;
38+
}
39+
if (ctx->pos + len > ctx->size) {
40+
return -1;
41+
}
42+
43+
memcpy(&ctx->buf[ctx->pos], data, len);
44+
ctx->pos += len;
45+
return (int)len;
46+
}
47+
#endif /* STREAMING */
48+
49+
#ifdef STREAMING
50+
struct bstr_chunk_ctx {
51+
int idx;
52+
const uint8_t *chunks[2];
53+
size_t lens[2];
54+
};
55+
56+
static int bstr_next_chunk(void *user_ctx, const uint8_t **ptr, size_t *len)
57+
{
58+
struct bstr_chunk_ctx *c = (struct bstr_chunk_ctx *)user_ctx;
59+
60+
if (!c || !ptr || !len) {
61+
return -EINVAL;
62+
}
63+
64+
if (c->idx >= 2) {
65+
return 0;
66+
}
67+
68+
*ptr = c->chunks[c->idx];
69+
*len = c->lens[c->idx];
70+
c->idx++;
71+
return 1;
72+
}
73+
#endif /* STREAMING */
74+
1675

1776
/* This test uses generated code to encode a 'Pet' instance. It populates the
1877
* generated struct, and runs the generated encoding function, then checks that
@@ -41,7 +100,19 @@ ZTEST(cbor_encode_test2, test_pet)
41100
size_t out_len;
42101

43102
/* Check that encoding succeeded. */
103+
#ifdef STREAMING
104+
struct stream_ctx ctx = {
105+
.buf = output,
106+
.size = sizeof(output),
107+
.pos = 0,
108+
};
109+
110+
int rc = cbor_stream_encode_Pet(stream_write, &ctx, &pet, NULL, &out_len);
111+
zassert_equal(ZCBOR_SUCCESS, rc, NULL);
112+
zassert_equal(out_len, ctx.pos, NULL);
113+
#else
44114
zassert_equal(ZCBOR_SUCCESS, cbor_encode_Pet(output, sizeof(output), &pet, &out_len), NULL);
115+
#endif
45116

46117
/* Check that the resulting length is correct. */
47118
zassert_equal(sizeof(exp_output), out_len, NULL);
@@ -59,7 +130,18 @@ ZTEST(cbor_encode_test2, test_pet)
59130
ZTEST(cbor_encode_test2, test_pet_raw)
60131
{
61132
uint8_t payload[100] = {0};
133+
#ifdef STREAMING
134+
struct stream_ctx ctx = {
135+
.buf = payload,
136+
.size = sizeof(payload),
137+
.pos = 0,
138+
};
139+
zcbor_state_t states[4];
140+
zcbor_new_encode_state_streaming(states, 4, stream_write, &ctx, 1);
141+
zcbor_state_t *state = states;
142+
#else
62143
ZCBOR_STATE_E(state, 4, payload, sizeof(payload), 1);
144+
#endif
63145

64146
uint8_t exp_output[] = {
65147
LIST(3),
@@ -97,10 +179,69 @@ ZTEST(cbor_encode_test2, test_pet_raw)
97179
/* Check that encoding succeeded. */
98180
zassert_true(res, NULL);
99181
/* Check that the resulting length is correct. */
182+
#ifdef STREAMING
183+
zassert_equal(sizeof(exp_output), ctx.pos, "%d != %d\r\n",
184+
sizeof(exp_output), ctx.pos);
185+
#else
100186
zassert_equal(sizeof(exp_output), state->payload - payload, "%d != %d\r\n",
101187
sizeof(exp_output), state->payload - payload);
188+
#endif
102189
/* Check the payload contents. */
103190
zassert_mem_equal(exp_output, payload, sizeof(exp_output), NULL);
104191
}
105192

193+
#ifdef STREAMING
194+
ZTEST(cbor_encode_test2, test_bstr_indefinite_chunks)
195+
{
196+
/* Expect: 0x5f (bstr indefinite), 0x43 01 02 03, 0x42 04 05, 0xff (break) */
197+
uint8_t output[16];
198+
struct stream_ctx ctx = {
199+
.buf = output,
200+
.size = sizeof(output),
201+
.pos = 0,
202+
};
203+
204+
const uint8_t c1[] = { 0x01, 0x02, 0x03 };
205+
const uint8_t c2[] = { 0x04, 0x05 };
206+
struct bstr_chunk_ctx chunks = {
207+
.idx = 0,
208+
.chunks = { c1, c2 },
209+
.lens = { sizeof(c1), sizeof(c2) },
210+
};
211+
212+
zcbor_state_t s[4];
213+
zcbor_new_encode_state_streaming(s, 4, stream_write, &ctx, 1);
214+
215+
zassert_true(zcbor_bstr_encode_indefinite_chunks(s, bstr_next_chunk, &chunks), NULL);
216+
zassert_equal(ctx.pos, 1 + 1 + sizeof(c1) + 1 + sizeof(c2) + 1, NULL);
217+
218+
const uint8_t exp[] = { 0x5f, 0x43, 0x01, 0x02, 0x03, 0x42, 0x04, 0x05, 0xff };
219+
zassert_mem_equal(output, exp, ctx.pos, NULL);
220+
}
221+
#endif /* STREAMING */
222+
223+
#ifdef STREAMING
224+
ZTEST(cbor_encode_test2, test_streaming_container_headers)
225+
{
226+
/* In streaming mode, lists/maps must be indefinite-length (0x9f/0xbf ... 0xff). */
227+
uint8_t output[32];
228+
struct stream_ctx ctx = {
229+
.buf = output,
230+
.size = sizeof(output),
231+
.pos = 0,
232+
};
233+
234+
zcbor_state_t s[4];
235+
zcbor_new_encode_state_streaming(s, 4, stream_write, &ctx, 1);
236+
237+
zassert_true(zcbor_list_start_encode(s, 3), NULL);
238+
zassert_true(zcbor_uint32_put(s, 1), NULL);
239+
zassert_true(zcbor_list_end_encode(s, 3), NULL);
240+
241+
zassert_true(ctx.pos >= 2, NULL);
242+
zassert_equal(output[0], 0x9f, NULL);
243+
zassert_equal(output[ctx.pos - 1], 0xff, NULL);
244+
}
245+
#endif /* STREAMING */
246+
106247
ZTEST_SUITE(cbor_encode_test2, NULL, NULL, NULL, NULL, NULL);

tests/encode/test2_simple/testcase.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ tests:
66
- mps2/an521/cpu0
77
- qemu_malta/qemu_malta/be
88
tags: zcbor encode test
9+
10+
zcbor.encode.test2_simple.streaming:
11+
platform_allow:
12+
- native_sim
13+
- native_sim/native/64
14+
- mps2/an521/cpu0
15+
- qemu_malta/qemu_malta/be
16+
tags: zcbor encode streaming test
17+
extra_args: STREAMING=1
18+
19+
zcbor.encode.test2_simple.streaming_canonical:
20+
platform_allow:
21+
- native_sim
22+
- native_sim/native/64
23+
- mps2/an521/cpu0
24+
- qemu_malta/qemu_malta/be
25+
tags: zcbor encode streaming canonical test
26+
extra_args: STREAMING=1 CANONICAL=CANONICAL
27+
928
zcbor.encode.test2_simple.canonical:
1029
platform_allow:
1130
- native_sim

tests/encode/test3_corner_cases/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ set(py_command
5959
--short-names
6060
)
6161

62+
if (STREAMING)
63+
list(APPEND py_command --stream-encode-functions)
64+
endif()
65+
6266
execute_process(
6367
COMMAND ${py_command}
6468
COMMAND_ERROR_IS_FATAL ANY

0 commit comments

Comments
 (0)