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)
59130ZTEST (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+
106247ZTEST_SUITE (cbor_encode_test2 , NULL , NULL , NULL , NULL , NULL );
0 commit comments