xref: /freebsd/contrib/libcbor/test/cbor_stream_decode_test.c (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include "assertions.h"
9 #include "cbor.h"
10 #include "stream_expectations.h"
11 
12 static void test_no_data(void **_CBOR_UNUSED(_state)) {
13   assert_decoder_result_nedata(1, decode(NULL, 0));
14 }
15 
16 unsigned char embedded_uint8_data[] = {0x00, 0x01, 0x05, 0x17};
17 static void test_uint8_embedded_decoding(void **_CBOR_UNUSED(_state)) {
18   assert_uint8_eq(0);
19   assert_decoder_result(1, CBOR_DECODER_FINISHED,
20                         decode(embedded_uint8_data, 1));
21 
22   assert_uint8_eq(1);
23   assert_decoder_result(1, CBOR_DECODER_FINISHED,
24                         decode(embedded_uint8_data + 1, 1));
25 
26   assert_uint8_eq(5);
27   assert_decoder_result(1, CBOR_DECODER_FINISHED,
28                         decode(embedded_uint8_data + 2, 1));
29 
30   assert_uint8_eq(23);
31   assert_decoder_result(1, CBOR_DECODER_FINISHED,
32                         decode(embedded_uint8_data + 3, 1));
33 }
34 
35 unsigned char uint8_data[] = {0x18, 0x83, 0x18, 0xFF};
36 static void test_uint8_decoding(void **_CBOR_UNUSED(_state)) {
37   assert_uint8_eq(0x83);
38   assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(uint8_data, 2));
39 
40   assert_uint8_eq(0xFF);
41   assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(uint8_data + 2, 2));
42 
43   assert_minimum_input_size(2, uint8_data);
44 }
45 
46 unsigned char uint16_data[] = {0x19, 0x01, 0xf4};
47 static void test_uint16_decoding(void **_CBOR_UNUSED(_state)) {
48   assert_uint16_eq(500);
49   assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(uint16_data, 3));
50 
51   assert_minimum_input_size(3, uint16_data);
52 }
53 
54 unsigned char uint32_data[] = {0x1a, 0xa5, 0xf7, 0x02, 0xb3};
55 static void test_uint32_decoding(void **_CBOR_UNUSED(_state)) {
56   assert_uint32_eq((uint32_t)2784428723UL);
57   assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(uint32_data, 5));
58 
59   assert_minimum_input_size(5, uint32_data);
60 }
61 
62 unsigned char uint64_data[] = {0x1b, 0xa5, 0xf7, 0x02, 0xb3,
63                                0xa5, 0xf7, 0x02, 0xb3};
64 static void test_uint64_decoding(void **_CBOR_UNUSED(_state)) {
65   assert_uint64_eq(11959030306112471731ULL);
66   assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(uint64_data, 9));
67 
68   assert_minimum_input_size(9, uint64_data);
69 }
70 
71 unsigned char embedded_negint8_data[] = {0x20, 0x21, 0x25, 0x37};
72 static void test_negint8_embedded_decoding(void **_CBOR_UNUSED(_state)) {
73   assert_negint8_eq(0);
74   assert_decoder_result(1, CBOR_DECODER_FINISHED,
75                         decode(embedded_negint8_data, 1));
76 
77   assert_negint8_eq(1);
78   assert_decoder_result(1, CBOR_DECODER_FINISHED,
79                         decode(embedded_negint8_data + 1, 1));
80 
81   assert_negint8_eq(5);
82   assert_decoder_result(1, CBOR_DECODER_FINISHED,
83                         decode(embedded_negint8_data + 2, 1));
84 
85   assert_negint8_eq(23);
86   assert_decoder_result(1, CBOR_DECODER_FINISHED,
87                         decode(embedded_negint8_data + 3, 1));
88 }
89 
90 unsigned char negint8_data[] = {0x38, 0x83, 0x38, 0xFF};
91 static void test_negint8_decoding(void **_CBOR_UNUSED(_state)) {
92   assert_negint8_eq(0x83);
93   assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(negint8_data, 2));
94 
95   assert_negint8_eq(0xFF);
96   assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(negint8_data + 2, 2));
97 
98   assert_minimum_input_size(2, negint8_data);
99 }
100 
101 unsigned char negint16_data[] = {0x39, 0x01, 0xf4};
102 static void test_negint16_decoding(void **_CBOR_UNUSED(_state)) {
103   assert_negint16_eq(500);
104   assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(negint16_data, 3));
105 
106   assert_minimum_input_size(3, negint16_data);
107 }
108 
109 unsigned char negint32_data[] = {0x3a, 0xa5, 0xf7, 0x02, 0xb3};
110 static void test_negint32_decoding(void **_CBOR_UNUSED(_state)) {
111   assert_negint32_eq((uint32_t)2784428723UL);
112   assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(negint32_data, 5));
113 
114   assert_minimum_input_size(5, negint32_data);
115 }
116 
117 unsigned char negint64_data[] = {0x3b, 0xa5, 0xf7, 0x02, 0xb3,
118                                  0xa5, 0xf7, 0x02, 0xb3};
119 static void test_negint64_decoding(void **_CBOR_UNUSED(_state)) {
120   assert_negint64_eq(11959030306112471731ULL);
121   assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(negint64_data, 9));
122 
123   assert_minimum_input_size(9, negint64_data);
124 }
125 
126 unsigned char bstring_embedded_int8_data[] = {0x41, 0xFF};
127 static void test_bstring_embedded_int8_decoding(void **_CBOR_UNUSED(_state)) {
128   assert_bstring_mem_eq(bstring_embedded_int8_data + 1, 1);
129   assert_decoder_result(2, CBOR_DECODER_FINISHED,
130                         decode(bstring_embedded_int8_data, 2));
131 
132   assert_minimum_input_size(2, bstring_embedded_int8_data);
133 }
134 
135 // The callback returns a *pointer* to the the start of the data segment (after
136 // the second byte of input); the data is never read, so we never run into
137 // memory issues despite not allocating and initializing all the data.
138 unsigned char bstring_int8_data[] = {0x58, 0x02 /*, [2 bytes] */};
139 static void test_bstring_int8_decoding(void **_CBOR_UNUSED(_state)) {
140   assert_bstring_mem_eq(bstring_int8_data + 2, 2);
141   assert_decoder_result(4, CBOR_DECODER_FINISHED, decode(bstring_int8_data, 4));
142 
143   assert_minimum_input_size(2, bstring_int8_data);
144   assert_decoder_result_nedata(/* expected_bytes_required= */ 2 + 2,
145                                decode(bstring_int8_data, 2));
146 }
147 
148 unsigned char bstring_int8_empty_data[] = {0x58, 0x00};
149 static void test_bstring_int8_empty_decoding(void **_CBOR_UNUSED(_state)) {
150   assert_bstring_mem_eq(bstring_int8_empty_data + 2, 0);
151   assert_decoder_result(2, CBOR_DECODER_FINISHED,
152                         decode(bstring_int8_empty_data, 2));
153 
154   assert_minimum_input_size(2, bstring_int8_empty_data);
155 }
156 
157 unsigned char bstring_int16_data[] = {0x59, 0x01, 0x5C /*, [348 bytes] */};
158 static void test_bstring_int16_decoding(void **_CBOR_UNUSED(_state)) {
159   assert_bstring_mem_eq(bstring_int16_data + 3, 348);
160   assert_decoder_result(3 + 348, CBOR_DECODER_FINISHED,
161                         decode(bstring_int16_data, 3 + 348));
162 
163   assert_minimum_input_size(3, bstring_int16_data);
164   assert_decoder_result_nedata(/* expected_bytes_required= */ 3 + 348,
165                                decode(bstring_int16_data, 3));
166 }
167 
168 unsigned char bstring_int32_data[] = {0x5A, 0x00, 0x10, 0x10,
169                                       0x10 /*, [1052688 bytes] */};
170 static void test_bstring_int32_decoding(void **_CBOR_UNUSED(_state)) {
171   assert_bstring_mem_eq(bstring_int32_data + 5, 1052688);
172   assert_decoder_result(5 + 1052688, CBOR_DECODER_FINISHED,
173                         decode(bstring_int32_data, 5 + 1052688));
174 
175   assert_minimum_input_size(5, bstring_int32_data);
176   assert_decoder_result_nedata(/* expected_bytes_required= */ 5 + 1052688,
177                                decode(bstring_int32_data, 5));
178 }
179 
180 #ifdef EIGHT_BYTE_SIZE_T
181 unsigned char bstring_int64_data[] = {
182     0x5B, 0x00, 0x00, 0x00, 0x01,
183     0x00, 0x00, 0x00, 0x00 /*, [4294967296 bytes] */};
184 static void test_bstring_int64_decoding(void **_CBOR_UNUSED(_state)) {
185   assert_bstring_mem_eq(bstring_int64_data + 9, 4294967296);
186   assert_decoder_result(9 + 4294967296, CBOR_DECODER_FINISHED,
187                         decode(bstring_int64_data, 9 + 4294967296));
188 
189   assert_minimum_input_size(9, bstring_int64_data);
190   assert_decoder_result_nedata(/* expected_bytes_required= */ 9 + 4294967296,
191                                decode(bstring_int64_data, 9));
192 }
193 #endif
194 
195 unsigned char bstring_indef_1_data[] = {0x5F, 0x40 /* Empty byte string */,
196                                         0xFF};
197 static void test_bstring_indef_decoding_1(void **_CBOR_UNUSED(_state)) {
198   assert_bstring_indef_start();
199   assert_decoder_result(1, CBOR_DECODER_FINISHED,
200                         decode(bstring_indef_1_data, 3));
201 
202   assert_bstring_mem_eq(bstring_indef_1_data + 2, 0);
203   assert_decoder_result(1, CBOR_DECODER_FINISHED,
204                         decode(bstring_indef_1_data + 1, 2));
205 
206   assert_indef_break();
207   assert_decoder_result(1, CBOR_DECODER_FINISHED,
208                         decode(bstring_indef_1_data + 2, 1));
209 }
210 
211 unsigned char bstring_indef_2_data[] = {0x5F, 0xFF};
212 static void test_bstring_indef_decoding_2(void **_CBOR_UNUSED(_state)) {
213   assert_bstring_indef_start();
214   assert_decoder_result(1, CBOR_DECODER_FINISHED,
215                         decode(bstring_indef_2_data, 2));
216 
217   assert_indef_break();
218   assert_decoder_result(1, CBOR_DECODER_FINISHED,
219                         decode(bstring_indef_2_data + 1, 1));
220 }
221 
222 unsigned char bstring_indef_3_data[] = {0x5F,
223                                         // Empty byte string
224                                         0x40,
225                                         // 1B, 1 character byte string
226                                         0x58, 0x01, 0x00,
227                                         // Break
228                                         0xFF};
229 static void test_bstring_indef_decoding_3(void **_CBOR_UNUSED(_state)) {
230   assert_bstring_indef_start();
231   assert_decoder_result(1, CBOR_DECODER_FINISHED,
232                         decode(bstring_indef_3_data, 6));
233 
234   assert_bstring_mem_eq(bstring_indef_3_data + 2, 0);
235   assert_decoder_result(1, CBOR_DECODER_FINISHED,
236                         decode(bstring_indef_3_data + 1, 5));
237 
238   assert_bstring_mem_eq(bstring_indef_3_data + 4, 1);
239   assert_decoder_result(3, CBOR_DECODER_FINISHED,
240                         decode(bstring_indef_3_data + 2, 4));
241 
242   assert_indef_break();
243   assert_decoder_result(1, CBOR_DECODER_FINISHED,
244                         decode(bstring_indef_3_data + 5, 1));
245 }
246 
247 unsigned char string_embedded_int8_data[] = {0x61, 0xFF};
248 static void test_string_embedded_int8_decoding(void **_CBOR_UNUSED(_state)) {
249   assert_string_mem_eq(string_embedded_int8_data + 1, 1);
250   assert_decoder_result(2, CBOR_DECODER_FINISHED,
251                         decode(string_embedded_int8_data, 2));
252 
253   assert_minimum_input_size(2, string_embedded_int8_data);
254 }
255 
256 // The callback returns a *pointer* to the the start of the data segment (after
257 // the second byte of input); the data is never read, so we never run into
258 // memory issues despite not allocating and initializing all the data.
259 unsigned char string_int8_data[] = {0x78, 0x02 /*, [2 bytes] */};
260 static void test_string_int8_decoding(void **_CBOR_UNUSED(_state)) {
261   assert_string_mem_eq(string_int8_data + 2, 2);
262   assert_decoder_result(4, CBOR_DECODER_FINISHED, decode(string_int8_data, 4));
263 
264   assert_minimum_input_size(2, string_int8_data);
265   assert_decoder_result_nedata(/* expected_bytes_required= */ 2 + 2,
266                                decode(string_int8_data, 2));
267 }
268 
269 unsigned char string_int8_empty_data[] = {0x78, 0x00};
270 static void test_string_int8_empty_decoding(void **_CBOR_UNUSED(_state)) {
271   assert_string_mem_eq(string_int8_empty_data + 2, 0);
272   assert_decoder_result(2, CBOR_DECODER_FINISHED,
273                         decode(string_int8_empty_data, 2));
274 
275   assert_minimum_input_size(2, string_int8_empty_data);
276 }
277 
278 unsigned char string_int16_data[] = {0x79, 0x01, 0x5C /*, [348 bytes] */};
279 static void test_string_int16_decoding(void **_CBOR_UNUSED(_state)) {
280   assert_string_mem_eq(string_int16_data + 3, 348);
281   assert_decoder_result(3 + 348, CBOR_DECODER_FINISHED,
282                         decode(string_int16_data, 3 + 348));
283 
284   assert_minimum_input_size(3, string_int16_data);
285   assert_decoder_result_nedata(/* expected_bytes_required= */ 3 + 348,
286                                decode(string_int16_data, 3));
287 }
288 
289 unsigned char string_int32_data[] = {0x7A, 0x00, 0x10, 0x10,
290                                      0x10 /*, [1052688 bytes] */};
291 static void test_string_int32_decoding(void **_CBOR_UNUSED(_state)) {
292   assert_string_mem_eq(string_int32_data + 5, 1052688);
293   assert_decoder_result(5 + 1052688, CBOR_DECODER_FINISHED,
294                         decode(string_int32_data, 5 + 1052688));
295 
296   assert_minimum_input_size(5, string_int32_data);
297   assert_decoder_result_nedata(/* expected_bytes_required= */ 5 + 1052688,
298                                decode(string_int32_data, 5));
299 }
300 
301 #ifdef EIGHT_BYTE_SIZE_T
302 unsigned char string_int64_data[] = {
303     0x7B, 0x00, 0x00, 0x00, 0x01,
304     0x00, 0x00, 0x00, 0x00 /*, [4294967296 bytes] */};
305 static void test_string_int64_decoding(void **_CBOR_UNUSED(_state)) {
306   assert_string_mem_eq(string_int64_data + 9, 4294967296);
307   assert_decoder_result(9 + 4294967296, CBOR_DECODER_FINISHED,
308                         decode(string_int64_data, 9 + 4294967296));
309 
310   assert_minimum_input_size(9, string_int64_data);
311   assert_decoder_result_nedata(/* expected_bytes_required= */ 9 + 4294967296,
312                                decode(string_int64_data, 9));
313 }
314 #endif
315 
316 unsigned char string_indef_1_data[] = {0x7F, 0x60 /* Empty string */, 0xFF};
317 static void test_string_indef_decoding_1(void **_CBOR_UNUSED(_state)) {
318   assert_string_indef_start();
319   assert_decoder_result(1, CBOR_DECODER_FINISHED,
320                         decode(string_indef_1_data, 3));
321 
322   assert_string_mem_eq(string_indef_1_data + 2, 0);
323   assert_decoder_result(1, CBOR_DECODER_FINISHED,
324                         decode(string_indef_1_data + 1, 2));
325 
326   assert_indef_break();
327   assert_decoder_result(1, CBOR_DECODER_FINISHED,
328                         decode(string_indef_1_data + 2, 1));
329 }
330 
331 unsigned char string_indef_2_data[] = {0x7F, 0xFF};
332 static void test_string_indef_decoding_2(void **_CBOR_UNUSED(_state)) {
333   assert_string_indef_start();
334   assert_decoder_result(1, CBOR_DECODER_FINISHED,
335                         decode(string_indef_2_data, 2));
336 
337   assert_indef_break();
338   assert_decoder_result(1, CBOR_DECODER_FINISHED,
339                         decode(string_indef_2_data + 1, 1));
340 }
341 
342 unsigned char string_indef_3_data[] = {0x7F,
343                                        // Empty string
344                                        0x60,
345                                        // 1B, 1 character byte string
346                                        0x78, 0x01, 0x00,
347                                        // Break
348                                        0xFF};
349 static void test_string_indef_decoding_3(void **_CBOR_UNUSED(_state)) {
350   assert_string_indef_start();
351   assert_decoder_result(1, CBOR_DECODER_FINISHED,
352                         decode(string_indef_3_data, 6));
353 
354   assert_string_mem_eq(string_indef_3_data + 2, 0);
355   assert_decoder_result(1, CBOR_DECODER_FINISHED,
356                         decode(string_indef_3_data + 1, 5));
357 
358   assert_string_mem_eq(string_indef_3_data + 4, 1);
359   assert_decoder_result(3, CBOR_DECODER_FINISHED,
360                         decode(string_indef_3_data + 2, 4));
361 
362   assert_indef_break();
363   assert_decoder_result(1, CBOR_DECODER_FINISHED,
364                         decode(string_indef_3_data + 5, 1));
365 }
366 
367 unsigned char array_embedded_int8_data[] = {0x80};
368 static void test_array_embedded_int8_decoding(void **_CBOR_UNUSED(_state)) {
369   assert_array_start(0);
370   assert_decoder_result(1, CBOR_DECODER_FINISHED,
371                         decode(array_embedded_int8_data, 1));
372 }
373 
374 unsigned char array_int8_data[] = {0x98, 0x02, 0x00, 0x01};
375 static void test_array_int8_decoding(void **_CBOR_UNUSED(_state)) {
376   assert_array_start(2);
377   assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(array_int8_data, 4));
378 
379   assert_uint8_eq(0);
380   assert_decoder_result(1, CBOR_DECODER_FINISHED,
381                         decode(array_int8_data + 2, 2));
382 
383   assert_uint8_eq(1);
384   assert_decoder_result(1, CBOR_DECODER_FINISHED,
385                         decode(array_int8_data + 3, 1));
386 
387   assert_minimum_input_size(2, array_int8_data);
388 }
389 
390 unsigned char array_int16_data[] = {0x99, 0x00, 0x02, 0x00, 0x01};
391 static void test_array_int16_decoding(void **_CBOR_UNUSED(_state)) {
392   assert_array_start(2);
393   assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(array_int16_data, 5));
394 
395   assert_uint8_eq(0);
396   assert_decoder_result(1, CBOR_DECODER_FINISHED,
397                         decode(array_int16_data + 3, 2));
398 
399   assert_uint8_eq(1);
400   assert_decoder_result(1, CBOR_DECODER_FINISHED,
401                         decode(array_int16_data + 4, 1));
402 
403   assert_minimum_input_size(3, array_int16_data);
404 }
405 
406 unsigned char array_int32_data[] = {0x9A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01};
407 static void test_array_int32_decoding(void **_CBOR_UNUSED(_state)) {
408   assert_array_start(2);
409   assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(array_int32_data, 7));
410 
411   assert_uint8_eq(0);
412   assert_decoder_result(1, CBOR_DECODER_FINISHED,
413                         decode(array_int32_data + 5, 2));
414 
415   assert_uint8_eq(1);
416   assert_decoder_result(1, CBOR_DECODER_FINISHED,
417                         decode(array_int32_data + 6, 1));
418 
419   assert_minimum_input_size(5, array_int32_data);
420 }
421 
422 unsigned char array_int64_data[] = {0x9B, 0x00, 0x00, 0x00, 0x00, 0x00,
423                                     0x00, 0x00, 0x02, 0x00, 0x01};
424 static void test_array_int64_decoding(void **_CBOR_UNUSED(_state)) {
425   assert_array_start(2);
426   assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(array_int64_data, 11));
427 
428   assert_uint8_eq(0);
429   assert_decoder_result(1, CBOR_DECODER_FINISHED,
430                         decode(array_int64_data + 9, 2));
431 
432   assert_uint8_eq(1);
433   assert_decoder_result(1, CBOR_DECODER_FINISHED,
434                         decode(array_int64_data + 10, 1));
435 
436   assert_minimum_input_size(9, array_int64_data);
437 }
438 
439 unsigned char array_of_arrays_data[] = {0x82, 0x80, 0x80};
440 static void test_array_of_arrays_decoding(void **_CBOR_UNUSED(_state)) {
441   assert_array_start(2);
442   assert_decoder_result(1, CBOR_DECODER_FINISHED,
443                         decode(array_of_arrays_data, 3));
444 
445   assert_array_start(0);
446   assert_decoder_result(1, CBOR_DECODER_FINISHED,
447                         decode(array_of_arrays_data + 1, 2));
448 
449   assert_array_start(0);
450   assert_decoder_result(1, CBOR_DECODER_FINISHED,
451                         decode(array_of_arrays_data + 2, 1));
452 }
453 
454 unsigned char indef_array_data_1[] = {0x9F, 0x00, 0x18, 0xFF, 0x9F, 0xFF, 0xFF};
455 static void test_indef_array_decoding_1(void **_CBOR_UNUSED(_state)) {
456   assert_indef_array_start();
457   assert_decoder_result(1, CBOR_DECODER_FINISHED,
458                         decode(indef_array_data_1, 7));
459 
460   assert_uint8_eq(0);
461   assert_decoder_result(1, CBOR_DECODER_FINISHED,
462                         decode(indef_array_data_1 + 1, 6));
463 
464   assert_uint8_eq(255);
465   assert_decoder_result(2, CBOR_DECODER_FINISHED,
466                         decode(indef_array_data_1 + 2, 4));
467 
468   assert_indef_array_start();
469   assert_decoder_result(1, CBOR_DECODER_FINISHED,
470                         decode(indef_array_data_1 + 4, 3));
471 
472   assert_indef_break();
473   assert_decoder_result(1, CBOR_DECODER_FINISHED,
474                         decode(indef_array_data_1 + 5, 2));
475 
476   assert_indef_break();
477   assert_decoder_result(1, CBOR_DECODER_FINISHED,
478                         decode(indef_array_data_1 + 6, 1));
479 }
480 
481 unsigned char map_embedded_int8_data[] = {0xa1, 0x01, 0x00};
482 static void test_map_embedded_int8_decoding(void **_CBOR_UNUSED(_state)) {
483   assert_map_start(1);
484   assert_decoder_result(1, CBOR_DECODER_FINISHED,
485                         decode(map_embedded_int8_data, 3));
486 
487   assert_uint8_eq(1);
488   assert_decoder_result(1, CBOR_DECODER_FINISHED,
489                         decode(map_embedded_int8_data + 1, 2));
490 
491   assert_uint8_eq(0);
492   assert_decoder_result(1, CBOR_DECODER_FINISHED,
493                         decode(map_embedded_int8_data + 2, 1));
494 }
495 
496 unsigned char map_int8_data[] = {0xB8, 0x01, 0x00, 0x01};
497 static void test_map_int8_decoding(void **_CBOR_UNUSED(_state)) {
498   assert_map_start(1);
499   assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(map_int8_data, 4));
500 
501   assert_uint8_eq(0);
502   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(map_int8_data + 2, 2));
503 
504   assert_uint8_eq(1);
505   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(map_int8_data + 3, 1));
506 
507   assert_minimum_input_size(2, map_int8_data);
508 }
509 
510 unsigned char map_int16_data[] = {0xB9, 0x00, 0x01, 0x00, 0x01};
511 static void test_map_int16_decoding(void **_CBOR_UNUSED(_state)) {
512   assert_map_start(1);
513   assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(map_int16_data, 5));
514 
515   assert_uint8_eq(0);
516   assert_decoder_result(1, CBOR_DECODER_FINISHED,
517                         decode(map_int16_data + 3, 2));
518 
519   assert_uint8_eq(1);
520   assert_decoder_result(1, CBOR_DECODER_FINISHED,
521                         decode(map_int16_data + 4, 1));
522 
523   assert_minimum_input_size(3, map_int16_data);
524 }
525 
526 unsigned char map_int32_data[] = {0xBA, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01};
527 static void test_map_int32_decoding(void **_CBOR_UNUSED(_state)) {
528   assert_map_start(1);
529   assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(map_int32_data, 7));
530 
531   assert_uint8_eq(0);
532   assert_decoder_result(1, CBOR_DECODER_FINISHED,
533                         decode(map_int32_data + 5, 2));
534 
535   assert_uint8_eq(1);
536   assert_decoder_result(1, CBOR_DECODER_FINISHED,
537                         decode(map_int32_data + 6, 1));
538 
539   assert_minimum_input_size(5, map_int32_data);
540 }
541 
542 unsigned char map_int64_data[] = {0xBB, 0x00, 0x00, 0x00, 0x00, 0x00,
543                                   0x00, 0x00, 0x01, 0x00, 0x01};
544 static void test_map_int64_decoding(void **_CBOR_UNUSED(_state)) {
545   assert_map_start(1);
546   assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(map_int64_data, 11));
547 
548   assert_uint8_eq(0);
549   assert_decoder_result(1, CBOR_DECODER_FINISHED,
550                         decode(map_int64_data + 9, 2));
551 
552   assert_uint8_eq(1);
553   assert_decoder_result(1, CBOR_DECODER_FINISHED,
554                         decode(map_int64_data + 10, 1));
555 
556   assert_minimum_input_size(9, map_int64_data);
557 }
558 
559 unsigned char indef_map_data_1[] = {0xBF, 0x00, 0x18, 0xFF, 0xFF};
560 static void test_indef_map_decoding_1(void **_CBOR_UNUSED(_state)) {
561   assert_indef_map_start();
562   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(indef_map_data_1, 5));
563 
564   assert_uint8_eq(0);
565   assert_decoder_result(1, CBOR_DECODER_FINISHED,
566                         decode(indef_map_data_1 + 1, 4));
567 
568   assert_uint8_eq(255);
569   assert_decoder_result(2, CBOR_DECODER_FINISHED,
570                         decode(indef_map_data_1 + 2, 3));
571 
572   assert_indef_break();
573   assert_decoder_result(1, CBOR_DECODER_FINISHED,
574                         decode(indef_map_data_1 + 4, 1));
575 }
576 
577 unsigned char embedded_tag_data[] = {0xC1};
578 static void test_embedded_tag_decoding(void **_CBOR_UNUSED(_state)) {
579   assert_tag_eq(1);
580   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(embedded_tag_data, 1));
581 }
582 
583 unsigned char int8_tag_data[] = {0xD8, 0xFE};
584 static void test_int8_tag_decoding(void **_CBOR_UNUSED(_state)) {
585   assert_tag_eq(254);
586   assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(int8_tag_data, 2));
587 
588   assert_minimum_input_size(2, int8_tag_data);
589 }
590 
591 unsigned char int16_tag_data[] = {0xD9, 0xFE, 0xFD};
592 static void test_int16_tag_decoding(void **_CBOR_UNUSED(_state)) {
593   assert_tag_eq(65277);
594   assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(int16_tag_data, 3));
595 
596   assert_minimum_input_size(3, int16_tag_data);
597 }
598 
599 unsigned char int32_tag_data[] = {0xDA, 0xFE, 0xFD, 0xFC, 0xFB};
600 static void test_int32_tag_decoding(void **_CBOR_UNUSED(_state)) {
601   assert_tag_eq(4278058235ULL);
602   assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(int32_tag_data, 5));
603 
604   assert_minimum_input_size(5, int32_tag_data);
605 }
606 
607 unsigned char int64_tag_data[] = {0xDB, 0xFE, 0xFD, 0xFC, 0xFB,
608                                   0xFA, 0xF9, 0xF8, 0xF7};
609 static void test_int64_tag_decoding(void **_CBOR_UNUSED(_state)) {
610   assert_tag_eq(18374120213919168759ULL);
611   assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(int64_tag_data, 9));
612 
613   assert_minimum_input_size(9, int64_tag_data);
614 }
615 
616 unsigned char reserved_byte_data[] = {0xDC};
617 static void test_reserved_byte_decoding(void **_CBOR_UNUSED(_state)) {
618   assert_decoder_result(0, CBOR_DECODER_ERROR, decode(reserved_byte_data, 1));
619 }
620 
621 unsigned char float2_data[] = {0xF9, 0x7B, 0xFF};
622 static void test_float2_decoding(void **_CBOR_UNUSED(_state)) {
623   assert_half(65504.0f);
624   assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(float2_data, 3));
625 
626   assert_minimum_input_size(3, float2_data);
627 }
628 
629 unsigned char float4_data[] = {0xFA, 0x47, 0xC3, 0x50, 0x00};
630 static void test_float4_decoding(void **_CBOR_UNUSED(_state)) {
631   assert_float(100000.0f);
632   assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(float4_data, 5));
633 
634   assert_minimum_input_size(5, float4_data);
635 }
636 
637 unsigned char float8_data[] = {0xFB, 0xC0, 0x10, 0x66, 0x66,
638                                0x66, 0x66, 0x66, 0x66};
639 static void test_float8_decoding(void **_CBOR_UNUSED(_state)) {
640   assert_double(-4.1);
641   assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(float8_data, 9));
642 
643   assert_minimum_input_size(0, float8_data);
644 }
645 
646 unsigned char false_data[] = {0xF4};
647 static void test_false_decoding(void **_CBOR_UNUSED(_state)) {
648   assert_bool(false);
649   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(false_data, 1));
650 }
651 
652 unsigned char true_data[] = {0xF5};
653 static void test_true_decoding(void **_CBOR_UNUSED(_state)) {
654   assert_bool(true);
655   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(true_data, 1));
656 }
657 
658 unsigned char null_data[] = {0xF6};
659 static void test_null_decoding(void **_CBOR_UNUSED(_state)) {
660   assert_nil();
661   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(null_data, 1));
662 }
663 
664 unsigned char undef_data[] = {0xF7};
665 static void test_undef_decoding(void **_CBOR_UNUSED(_state)) {
666   assert_undef();
667   assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(undef_data, 1));
668 }
669 
670 #define stream_test(f) cmocka_unit_test_teardown(f, clean_up_stream_assertions)
671 
672 int main(void) {
673   const struct CMUnitTest tests[] = {
674       stream_test(test_no_data),
675 
676       stream_test(test_uint8_embedded_decoding),
677       stream_test(test_uint8_decoding),
678       stream_test(test_uint16_decoding),
679       stream_test(test_uint32_decoding),
680       stream_test(test_uint64_decoding),
681 
682       stream_test(test_negint8_embedded_decoding),
683       stream_test(test_negint8_decoding),
684       stream_test(test_negint16_decoding),
685       stream_test(test_negint32_decoding),
686       stream_test(test_negint64_decoding),
687 
688       stream_test(test_bstring_embedded_int8_decoding),
689       stream_test(test_bstring_int8_decoding),
690       stream_test(test_bstring_int8_empty_decoding),
691       stream_test(test_bstring_int16_decoding),
692       stream_test(test_bstring_int32_decoding),
693 #ifdef EIGHT_BYTE_SIZE_T
694       stream_test(test_bstring_int64_decoding),
695 #endif
696       stream_test(test_bstring_indef_decoding_1),
697       stream_test(test_bstring_indef_decoding_2),
698       stream_test(test_bstring_indef_decoding_3),
699 
700       stream_test(test_string_embedded_int8_decoding),
701       stream_test(test_string_int8_decoding),
702       stream_test(test_string_int8_empty_decoding),
703       stream_test(test_string_int16_decoding),
704       stream_test(test_string_int32_decoding),
705 #ifdef EIGHT_BYTE_SIZE_T
706       stream_test(test_string_int64_decoding),
707 #endif
708       stream_test(test_string_indef_decoding_1),
709       stream_test(test_string_indef_decoding_2),
710       stream_test(test_string_indef_decoding_3),
711 
712       stream_test(test_array_embedded_int8_decoding),
713       stream_test(test_array_int8_decoding),
714       stream_test(test_array_int16_decoding),
715       stream_test(test_array_int32_decoding),
716       stream_test(test_array_int64_decoding),
717       stream_test(test_array_of_arrays_decoding),
718       stream_test(test_indef_array_decoding_1),
719 
720       stream_test(test_map_embedded_int8_decoding),
721       stream_test(test_map_int8_decoding),
722       stream_test(test_map_int16_decoding),
723       stream_test(test_map_int32_decoding),
724       stream_test(test_map_int64_decoding),
725       stream_test(test_indef_map_decoding_1),
726 
727       stream_test(test_embedded_tag_decoding),
728       stream_test(test_int8_tag_decoding),
729       stream_test(test_int16_tag_decoding),
730       stream_test(test_int32_tag_decoding),
731       stream_test(test_int64_tag_decoding),
732       stream_test(test_reserved_byte_decoding),
733 
734       stream_test(test_float2_decoding),
735       stream_test(test_float4_decoding),
736       stream_test(test_float8_decoding),
737 
738       stream_test(test_false_decoding),
739       stream_test(test_true_decoding),
740       stream_test(test_null_decoding),
741       stream_test(test_undef_decoding)};
742   return cmocka_run_group_tests(tests, NULL, NULL);
743 }
744