110ff414cSEd Maste #include "stream_expectations.h"
210ff414cSEd Maste
310ff414cSEd Maste struct test_assertion assertions_queue[MAX_QUEUE_ITEMS];
410ff414cSEd Maste int queue_size = 0;
510ff414cSEd Maste int current_expectation = 0;
610ff414cSEd Maste
clean_up_stream_assertions(void ** state)7*5d3e7166SEd Maste int clean_up_stream_assertions(void **state) {
810ff414cSEd Maste if (queue_size != current_expectation) {
910ff414cSEd Maste return 1; // We have not matched all expectations correctly
1010ff414cSEd Maste }
1110ff414cSEd Maste queue_size = current_expectation = 0;
1210ff414cSEd Maste free(*state);
1310ff414cSEd Maste return 0;
1410ff414cSEd Maste }
1510ff414cSEd Maste
1610ff414cSEd Maste /* Callbacks */
current(void)17*5d3e7166SEd Maste struct test_assertion current(void) {
1810ff414cSEd Maste return assertions_queue[current_expectation];
1910ff414cSEd Maste }
2010ff414cSEd Maste
2110ff414cSEd Maste /* Assertions builders and matcher callbacks */
2210ff414cSEd Maste
assert_uint8_eq(uint8_t actual)2310ff414cSEd Maste void assert_uint8_eq(uint8_t actual) {
2410ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
2510ff414cSEd Maste UINT8_EQ, (union test_expectation_data){.int8 = actual}};
2610ff414cSEd Maste }
2710ff414cSEd Maste
uint8_callback(void * _CBOR_UNUSED (_context),uint8_t actual)28*5d3e7166SEd Maste void uint8_callback(void *_CBOR_UNUSED(_context), uint8_t actual) {
2910ff414cSEd Maste assert_true(current().expectation == UINT8_EQ);
3010ff414cSEd Maste assert_true(current().data.int8 == actual);
3110ff414cSEd Maste current_expectation++;
3210ff414cSEd Maste }
3310ff414cSEd Maste
assert_uint16_eq(uint16_t actual)3410ff414cSEd Maste void assert_uint16_eq(uint16_t actual) {
3510ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
3610ff414cSEd Maste UINT16_EQ, (union test_expectation_data){.int16 = actual}};
3710ff414cSEd Maste }
3810ff414cSEd Maste
uint16_callback(void * _CBOR_UNUSED (_context),uint16_t actual)39*5d3e7166SEd Maste void uint16_callback(void *_CBOR_UNUSED(_context), uint16_t actual) {
4010ff414cSEd Maste assert_true(current().expectation == UINT16_EQ);
4110ff414cSEd Maste assert_true(current().data.int16 == actual);
4210ff414cSEd Maste current_expectation++;
4310ff414cSEd Maste }
4410ff414cSEd Maste
assert_uint32_eq(uint32_t actual)4510ff414cSEd Maste void assert_uint32_eq(uint32_t actual) {
4610ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
4710ff414cSEd Maste UINT32_EQ, (union test_expectation_data){.int32 = actual}};
4810ff414cSEd Maste }
4910ff414cSEd Maste
uint32_callback(void * _CBOR_UNUSED (_context),uint32_t actual)50*5d3e7166SEd Maste void uint32_callback(void *_CBOR_UNUSED(_context), uint32_t actual) {
5110ff414cSEd Maste assert_true(current().expectation == UINT32_EQ);
5210ff414cSEd Maste assert_true(current().data.int32 == actual);
5310ff414cSEd Maste current_expectation++;
5410ff414cSEd Maste }
5510ff414cSEd Maste
assert_uint64_eq(uint64_t actual)5610ff414cSEd Maste void assert_uint64_eq(uint64_t actual) {
5710ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
5810ff414cSEd Maste UINT64_EQ, (union test_expectation_data){.int64 = actual}};
5910ff414cSEd Maste }
6010ff414cSEd Maste
uint64_callback(void * _CBOR_UNUSED (_context),uint64_t actual)61*5d3e7166SEd Maste void uint64_callback(void *_CBOR_UNUSED(_context), uint64_t actual) {
6210ff414cSEd Maste assert_true(current().expectation == UINT64_EQ);
6310ff414cSEd Maste assert_true(current().data.int64 == actual);
6410ff414cSEd Maste current_expectation++;
6510ff414cSEd Maste }
6610ff414cSEd Maste
assert_negint8_eq(uint8_t actual)6710ff414cSEd Maste void assert_negint8_eq(uint8_t actual) {
6810ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
6910ff414cSEd Maste NEGINT8_EQ, (union test_expectation_data){.int8 = actual}};
7010ff414cSEd Maste }
7110ff414cSEd Maste
negint8_callback(void * _CBOR_UNUSED (_context),uint8_t actual)72*5d3e7166SEd Maste void negint8_callback(void *_CBOR_UNUSED(_context), uint8_t actual) {
7310ff414cSEd Maste assert_true(current().expectation == NEGINT8_EQ);
7410ff414cSEd Maste assert_true(current().data.int8 == actual);
7510ff414cSEd Maste current_expectation++;
7610ff414cSEd Maste }
7710ff414cSEd Maste
assert_negint16_eq(uint16_t actual)7810ff414cSEd Maste void assert_negint16_eq(uint16_t actual) {
7910ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
8010ff414cSEd Maste NEGINT16_EQ, (union test_expectation_data){.int16 = actual}};
8110ff414cSEd Maste }
8210ff414cSEd Maste
negint16_callback(void * _CBOR_UNUSED (_context),uint16_t actual)83*5d3e7166SEd Maste void negint16_callback(void *_CBOR_UNUSED(_context), uint16_t actual) {
8410ff414cSEd Maste assert_true(current().expectation == NEGINT16_EQ);
8510ff414cSEd Maste assert_true(current().data.int16 == actual);
8610ff414cSEd Maste current_expectation++;
8710ff414cSEd Maste }
8810ff414cSEd Maste
assert_negint32_eq(uint32_t actual)8910ff414cSEd Maste void assert_negint32_eq(uint32_t actual) {
9010ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
9110ff414cSEd Maste NEGINT32_EQ, (union test_expectation_data){.int32 = actual}};
9210ff414cSEd Maste }
9310ff414cSEd Maste
negint32_callback(void * _CBOR_UNUSED (_context),uint32_t actual)94*5d3e7166SEd Maste void negint32_callback(void *_CBOR_UNUSED(_context), uint32_t actual) {
9510ff414cSEd Maste assert_true(current().expectation == NEGINT32_EQ);
9610ff414cSEd Maste assert_true(current().data.int32 == actual);
9710ff414cSEd Maste current_expectation++;
9810ff414cSEd Maste }
9910ff414cSEd Maste
assert_negint64_eq(uint64_t actual)10010ff414cSEd Maste void assert_negint64_eq(uint64_t actual) {
10110ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
10210ff414cSEd Maste NEGINT64_EQ, (union test_expectation_data){.int64 = actual}};
10310ff414cSEd Maste }
10410ff414cSEd Maste
negint64_callback(void * _CBOR_UNUSED (_context),uint64_t actual)105*5d3e7166SEd Maste void negint64_callback(void *_CBOR_UNUSED(_context), uint64_t actual) {
10610ff414cSEd Maste assert_true(current().expectation == NEGINT64_EQ);
10710ff414cSEd Maste assert_true(current().data.int64 == actual);
10810ff414cSEd Maste current_expectation++;
10910ff414cSEd Maste }
11010ff414cSEd Maste
assert_bstring_mem_eq(cbor_data address,size_t length)11110ff414cSEd Maste void assert_bstring_mem_eq(cbor_data address, size_t length) {
11210ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){
11310ff414cSEd Maste BSTRING_MEM_EQ,
11410ff414cSEd Maste (union test_expectation_data){.string = {address, length}}};
11510ff414cSEd Maste }
11610ff414cSEd Maste
byte_string_callback(void * _CBOR_UNUSED (_context),cbor_data address,uint64_t length)117*5d3e7166SEd Maste void byte_string_callback(void *_CBOR_UNUSED(_context), cbor_data address,
118*5d3e7166SEd Maste uint64_t length) {
11910ff414cSEd Maste assert_true(current().expectation == BSTRING_MEM_EQ);
12010ff414cSEd Maste assert_true(current().data.string.address == address);
12110ff414cSEd Maste assert_true(current().data.string.length == length);
12210ff414cSEd Maste current_expectation++;
12310ff414cSEd Maste }
12410ff414cSEd Maste
assert_bstring_indef_start(void)125*5d3e7166SEd Maste void assert_bstring_indef_start(void) {
12610ff414cSEd Maste assertions_queue[queue_size++] =
12710ff414cSEd Maste (struct test_assertion){.expectation = BSTRING_INDEF_START};
12810ff414cSEd Maste }
12910ff414cSEd Maste
byte_string_start_callback(void * _CBOR_UNUSED (_context))130*5d3e7166SEd Maste void byte_string_start_callback(void *_CBOR_UNUSED(_context)) {
13110ff414cSEd Maste assert_true(current().expectation == BSTRING_INDEF_START);
13210ff414cSEd Maste current_expectation++;
13310ff414cSEd Maste }
13410ff414cSEd Maste
assert_string_mem_eq(cbor_data address,size_t length)135*5d3e7166SEd Maste void assert_string_mem_eq(cbor_data address, size_t length) {
136*5d3e7166SEd Maste assertions_queue[queue_size++] = (struct test_assertion){
137*5d3e7166SEd Maste STRING_MEM_EQ,
138*5d3e7166SEd Maste (union test_expectation_data){.string = {address, length}}};
139*5d3e7166SEd Maste }
140*5d3e7166SEd Maste
string_callback(void * _CBOR_UNUSED (_context),cbor_data address,uint64_t length)141*5d3e7166SEd Maste void string_callback(void *_CBOR_UNUSED(_context), cbor_data address,
142*5d3e7166SEd Maste uint64_t length) {
143*5d3e7166SEd Maste assert_true(current().expectation == STRING_MEM_EQ);
144*5d3e7166SEd Maste assert_true(current().data.string.address == address);
145*5d3e7166SEd Maste assert_true(current().data.string.length == length);
146*5d3e7166SEd Maste current_expectation++;
147*5d3e7166SEd Maste }
148*5d3e7166SEd Maste
assert_string_indef_start(void)149*5d3e7166SEd Maste void assert_string_indef_start(void) {
150*5d3e7166SEd Maste assertions_queue[queue_size++] =
151*5d3e7166SEd Maste (struct test_assertion){.expectation = STRING_INDEF_START};
152*5d3e7166SEd Maste }
153*5d3e7166SEd Maste
string_start_callback(void * _CBOR_UNUSED (_context))154*5d3e7166SEd Maste void string_start_callback(void *_CBOR_UNUSED(_context)) {
155*5d3e7166SEd Maste assert_true(current().expectation == STRING_INDEF_START);
156*5d3e7166SEd Maste current_expectation++;
157*5d3e7166SEd Maste }
158*5d3e7166SEd Maste
assert_indef_break(void)159*5d3e7166SEd Maste void assert_indef_break(void) {
16010ff414cSEd Maste assertions_queue[queue_size++] =
16110ff414cSEd Maste (struct test_assertion){.expectation = INDEF_BREAK};
16210ff414cSEd Maste }
16310ff414cSEd Maste
indef_break_callback(void * _CBOR_UNUSED (_context))164*5d3e7166SEd Maste void indef_break_callback(void *_CBOR_UNUSED(_context)) {
16510ff414cSEd Maste assert_true(current().expectation == INDEF_BREAK);
16610ff414cSEd Maste current_expectation++;
16710ff414cSEd Maste }
16810ff414cSEd Maste
assert_array_start(size_t length)16910ff414cSEd Maste void assert_array_start(size_t length) {
17010ff414cSEd Maste assertions_queue[queue_size++] =
17110ff414cSEd Maste (struct test_assertion){ARRAY_START, {.length = length}};
17210ff414cSEd Maste }
17310ff414cSEd Maste
array_start_callback(void * _CBOR_UNUSED (_context),uint64_t length)174*5d3e7166SEd Maste void array_start_callback(void *_CBOR_UNUSED(_context), uint64_t length) {
17510ff414cSEd Maste assert_true(current().expectation == ARRAY_START);
17610ff414cSEd Maste assert_true(current().data.length == length);
17710ff414cSEd Maste current_expectation++;
17810ff414cSEd Maste }
17910ff414cSEd Maste
assert_indef_array_start(void)180*5d3e7166SEd Maste void assert_indef_array_start(void) {
18110ff414cSEd Maste assertions_queue[queue_size++] =
18210ff414cSEd Maste (struct test_assertion){.expectation = ARRAY_INDEF_START};
18310ff414cSEd Maste }
18410ff414cSEd Maste
indef_array_start_callback(void * _CBOR_UNUSED (_context))185*5d3e7166SEd Maste void indef_array_start_callback(void *_CBOR_UNUSED(_context)) {
18610ff414cSEd Maste assert_true(current().expectation == ARRAY_INDEF_START);
18710ff414cSEd Maste current_expectation++;
18810ff414cSEd Maste }
18910ff414cSEd Maste
assert_map_start(size_t length)19010ff414cSEd Maste void assert_map_start(size_t length) {
19110ff414cSEd Maste assertions_queue[queue_size++] =
19210ff414cSEd Maste (struct test_assertion){MAP_START, {.length = length}};
19310ff414cSEd Maste }
19410ff414cSEd Maste
map_start_callback(void * _CBOR_UNUSED (_context),uint64_t length)195*5d3e7166SEd Maste void map_start_callback(void *_CBOR_UNUSED(_context), uint64_t length) {
19610ff414cSEd Maste assert_true(current().expectation == MAP_START);
19710ff414cSEd Maste assert_true(current().data.length == length);
19810ff414cSEd Maste current_expectation++;
19910ff414cSEd Maste }
20010ff414cSEd Maste
assert_indef_map_start(void)201*5d3e7166SEd Maste void assert_indef_map_start(void) {
20210ff414cSEd Maste assertions_queue[queue_size++] =
20310ff414cSEd Maste (struct test_assertion){.expectation = MAP_INDEF_START};
20410ff414cSEd Maste }
20510ff414cSEd Maste
indef_map_start_callback(void * _CBOR_UNUSED (_context))206*5d3e7166SEd Maste void indef_map_start_callback(void *_CBOR_UNUSED(_context)) {
20710ff414cSEd Maste assert_true(current().expectation == MAP_INDEF_START);
20810ff414cSEd Maste current_expectation++;
20910ff414cSEd Maste }
21010ff414cSEd Maste
assert_tag_eq(uint64_t value)21110ff414cSEd Maste void assert_tag_eq(uint64_t value) {
21210ff414cSEd Maste assertions_queue[queue_size++] =
21310ff414cSEd Maste (struct test_assertion){TAG_EQ, {.int64 = value}};
21410ff414cSEd Maste }
21510ff414cSEd Maste
tag_callback(void * _CBOR_UNUSED (_context),uint64_t value)216*5d3e7166SEd Maste void tag_callback(void *_CBOR_UNUSED(_context), uint64_t value) {
21710ff414cSEd Maste assert_true(current().expectation == TAG_EQ);
21810ff414cSEd Maste assert_true(current().data.int64 == value);
21910ff414cSEd Maste current_expectation++;
22010ff414cSEd Maste }
22110ff414cSEd Maste
assert_half(float value)22210ff414cSEd Maste void assert_half(float value) {
22310ff414cSEd Maste assertions_queue[queue_size++] =
22410ff414cSEd Maste (struct test_assertion){HALF_EQ, {.float2 = value}};
22510ff414cSEd Maste }
22610ff414cSEd Maste
half_callback(void * _CBOR_UNUSED (_context),float actual)227*5d3e7166SEd Maste void half_callback(void *_CBOR_UNUSED(_context), float actual) {
22810ff414cSEd Maste assert_true(current().expectation == HALF_EQ);
22910ff414cSEd Maste assert_true(current().data.float2 == actual);
23010ff414cSEd Maste current_expectation++;
23110ff414cSEd Maste }
23210ff414cSEd Maste
assert_float(float value)23310ff414cSEd Maste void assert_float(float value) {
23410ff414cSEd Maste assertions_queue[queue_size++] =
23510ff414cSEd Maste (struct test_assertion){FLOAT_EQ, {.float4 = value}};
23610ff414cSEd Maste }
23710ff414cSEd Maste
float_callback(void * _CBOR_UNUSED (_context),float actual)238*5d3e7166SEd Maste void float_callback(void *_CBOR_UNUSED(_context), float actual) {
23910ff414cSEd Maste assert_true(current().expectation == FLOAT_EQ);
24010ff414cSEd Maste assert_true(current().data.float4 == actual);
24110ff414cSEd Maste current_expectation++;
24210ff414cSEd Maste }
24310ff414cSEd Maste
assert_double(double value)24410ff414cSEd Maste void assert_double(double value) {
24510ff414cSEd Maste assertions_queue[queue_size++] =
24610ff414cSEd Maste (struct test_assertion){DOUBLE_EQ, {.float8 = value}};
24710ff414cSEd Maste }
24810ff414cSEd Maste
double_callback(void * _CBOR_UNUSED (_context),double actual)249*5d3e7166SEd Maste void double_callback(void *_CBOR_UNUSED(_context), double actual) {
25010ff414cSEd Maste assert_true(current().expectation == DOUBLE_EQ);
25110ff414cSEd Maste assert_true(current().data.float8 == actual);
25210ff414cSEd Maste current_expectation++;
25310ff414cSEd Maste }
25410ff414cSEd Maste
assert_bool(bool value)25510ff414cSEd Maste void assert_bool(bool value) {
25610ff414cSEd Maste assertions_queue[queue_size++] =
25710ff414cSEd Maste (struct test_assertion){BOOL_EQ, {.boolean = value}};
25810ff414cSEd Maste }
25910ff414cSEd Maste
assert_nil(void)260*5d3e7166SEd Maste void assert_nil(void) {
26110ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){.expectation = NIL};
26210ff414cSEd Maste }
26310ff414cSEd Maste
assert_undef(void)264*5d3e7166SEd Maste void assert_undef(void) {
26510ff414cSEd Maste assertions_queue[queue_size++] =
26610ff414cSEd Maste (struct test_assertion){.expectation = UNDEF};
26710ff414cSEd Maste }
26810ff414cSEd Maste
bool_callback(void * _CBOR_UNUSED (_context),bool actual)269*5d3e7166SEd Maste void bool_callback(void *_CBOR_UNUSED(_context), bool actual) {
27010ff414cSEd Maste assert_true(current().expectation == BOOL_EQ);
27110ff414cSEd Maste assert_true(current().data.boolean == actual);
27210ff414cSEd Maste current_expectation++;
27310ff414cSEd Maste }
27410ff414cSEd Maste
null_callback(void * _CBOR_UNUSED (_context))275*5d3e7166SEd Maste void null_callback(void *_CBOR_UNUSED(_context)) {
27610ff414cSEd Maste assert_true(current().expectation == NIL);
27710ff414cSEd Maste current_expectation++;
27810ff414cSEd Maste }
27910ff414cSEd Maste
undef_callback(void * _CBOR_UNUSED (_context))280*5d3e7166SEd Maste void undef_callback(void *_CBOR_UNUSED(_context)) {
28110ff414cSEd Maste assert_true(current().expectation == UNDEF);
28210ff414cSEd Maste current_expectation++;
28310ff414cSEd Maste }
28410ff414cSEd Maste
28510ff414cSEd Maste const struct cbor_callbacks asserting_callbacks = {
28610ff414cSEd Maste .uint8 = &uint8_callback,
28710ff414cSEd Maste .uint16 = &uint16_callback,
28810ff414cSEd Maste .uint32 = &uint32_callback,
28910ff414cSEd Maste .uint64 = &uint64_callback,
29010ff414cSEd Maste
29110ff414cSEd Maste .negint8 = &negint8_callback,
29210ff414cSEd Maste .negint16 = &negint16_callback,
29310ff414cSEd Maste .negint32 = &negint32_callback,
29410ff414cSEd Maste .negint64 = &negint64_callback,
29510ff414cSEd Maste
29610ff414cSEd Maste .byte_string = &byte_string_callback,
29710ff414cSEd Maste .byte_string_start = &byte_string_start_callback,
29810ff414cSEd Maste
299*5d3e7166SEd Maste .string = &string_callback,
300*5d3e7166SEd Maste .string_start = &string_start_callback,
301*5d3e7166SEd Maste
30210ff414cSEd Maste .array_start = &array_start_callback,
30310ff414cSEd Maste .indef_array_start = &indef_array_start_callback,
30410ff414cSEd Maste
30510ff414cSEd Maste .map_start = &map_start_callback,
30610ff414cSEd Maste .indef_map_start = &indef_map_start_callback,
30710ff414cSEd Maste
30810ff414cSEd Maste .tag = &tag_callback,
30910ff414cSEd Maste
31010ff414cSEd Maste .float2 = &half_callback,
31110ff414cSEd Maste .float4 = &float_callback,
31210ff414cSEd Maste .float8 = &double_callback,
31310ff414cSEd Maste
31410ff414cSEd Maste .undefined = &undef_callback,
31510ff414cSEd Maste .boolean = &bool_callback,
31610ff414cSEd Maste .null = &null_callback,
31710ff414cSEd Maste .indef_break = &indef_break_callback};
31810ff414cSEd Maste
decode(cbor_data source,size_t source_size)31910ff414cSEd Maste struct cbor_decoder_result decode(cbor_data source, size_t source_size) {
32010ff414cSEd Maste int last_expectation = current_expectation;
32110ff414cSEd Maste struct cbor_decoder_result result =
322*5d3e7166SEd Maste cbor_stream_decode(source, source_size, &asserting_callbacks, NULL);
32310ff414cSEd Maste if (result.status == CBOR_DECODER_FINISHED) {
32410ff414cSEd Maste // Check that we have matched an expectation from the queue
32510ff414cSEd Maste assert_true(last_expectation + 1 == current_expectation);
32610ff414cSEd Maste }
32710ff414cSEd Maste return result;
32810ff414cSEd Maste }
329