1 #include "stream_expectations.h"
2
3 // TODO: The saved keystrokes are not worth the complexity. Get rid of this
4 // file to prevent confusion, the fundamental structure is unlikely to change
5 // in the future.
6
7 /* Ordered from 0 to queue_size - 1 */
8 struct test_assertion assertions_queue[MAX_QUEUE_ITEMS];
9 int queue_size = 0;
10 int current_expectation = 0;
11 decoder_t *decoder;
12
set_decoder(decoder_t * dec)13 void set_decoder(decoder_t *dec) { decoder = dec; }
14
clear_stream_assertions(void ** state)15 int clear_stream_assertions(void **state) {
16 if (queue_size != current_expectation) {
17 return 1; // We have not matched all expectations correctly
18 }
19 queue_size = current_expectation = 0;
20 free(*state);
21 return 0;
22 }
23
24 /* Callbacks */
current()25 struct test_assertion current() {
26 return assertions_queue[current_expectation];
27 }
28
29 /* Assertions builders and matcher callbacks */
30
assert_uint8_eq(uint8_t actual)31 void assert_uint8_eq(uint8_t actual) {
32 assertions_queue[queue_size++] = (struct test_assertion){
33 UINT8_EQ, (union test_expectation_data){.int8 = actual}};
34 }
35
uint8_callback(void * context,uint8_t actual)36 void uint8_callback(void *context, uint8_t actual) {
37 assert_true(current().expectation == UINT8_EQ);
38 assert_true(current().data.int8 == actual);
39 current_expectation++;
40 }
41
assert_uint16_eq(uint16_t actual)42 void assert_uint16_eq(uint16_t actual) {
43 assertions_queue[queue_size++] = (struct test_assertion){
44 UINT16_EQ, (union test_expectation_data){.int16 = actual}};
45 }
46
uint16_callback(void * context,uint16_t actual)47 void uint16_callback(void *context, uint16_t actual) {
48 assert_true(current().expectation == UINT16_EQ);
49 assert_true(current().data.int16 == actual);
50 current_expectation++;
51 }
52
assert_uint32_eq(uint32_t actual)53 void assert_uint32_eq(uint32_t actual) {
54 assertions_queue[queue_size++] = (struct test_assertion){
55 UINT32_EQ, (union test_expectation_data){.int32 = actual}};
56 }
57
uint32_callback(void * context,uint32_t actual)58 void uint32_callback(void *context, uint32_t actual) {
59 assert_true(current().expectation == UINT32_EQ);
60 assert_true(current().data.int32 == actual);
61 current_expectation++;
62 }
63
assert_uint64_eq(uint64_t actual)64 void assert_uint64_eq(uint64_t actual) {
65 assertions_queue[queue_size++] = (struct test_assertion){
66 UINT64_EQ, (union test_expectation_data){.int64 = actual}};
67 }
68
uint64_callback(void * context,uint64_t actual)69 void uint64_callback(void *context, uint64_t actual) {
70 assert_true(current().expectation == UINT64_EQ);
71 assert_true(current().data.int64 == actual);
72 current_expectation++;
73 }
74
assert_negint8_eq(uint8_t actual)75 void assert_negint8_eq(uint8_t actual) {
76 assertions_queue[queue_size++] = (struct test_assertion){
77 NEGINT8_EQ, (union test_expectation_data){.int8 = actual}};
78 }
79
negint8_callback(void * context,uint8_t actual)80 void negint8_callback(void *context, uint8_t actual) {
81 assert_true(current().expectation == NEGINT8_EQ);
82 assert_true(current().data.int8 == actual);
83 current_expectation++;
84 }
85
assert_negint16_eq(uint16_t actual)86 void assert_negint16_eq(uint16_t actual) {
87 assertions_queue[queue_size++] = (struct test_assertion){
88 NEGINT16_EQ, (union test_expectation_data){.int16 = actual}};
89 }
90
negint16_callback(void * context,uint16_t actual)91 void negint16_callback(void *context, uint16_t actual) {
92 assert_true(current().expectation == NEGINT16_EQ);
93 assert_true(current().data.int16 == actual);
94 current_expectation++;
95 }
96
assert_negint32_eq(uint32_t actual)97 void assert_negint32_eq(uint32_t actual) {
98 assertions_queue[queue_size++] = (struct test_assertion){
99 NEGINT32_EQ, (union test_expectation_data){.int32 = actual}};
100 }
101
negint32_callback(void * context,uint32_t actual)102 void negint32_callback(void *context, uint32_t actual) {
103 assert_true(current().expectation == NEGINT32_EQ);
104 assert_true(current().data.int32 == actual);
105 current_expectation++;
106 }
107
assert_negint64_eq(uint64_t actual)108 void assert_negint64_eq(uint64_t actual) {
109 assertions_queue[queue_size++] = (struct test_assertion){
110 NEGINT64_EQ, (union test_expectation_data){.int64 = actual}};
111 }
112
negint64_callback(void * context,uint64_t actual)113 void negint64_callback(void *context, uint64_t actual) {
114 assert_true(current().expectation == NEGINT64_EQ);
115 assert_true(current().data.int64 == actual);
116 current_expectation++;
117 }
118
assert_bstring_mem_eq(cbor_data address,size_t length)119 void assert_bstring_mem_eq(cbor_data address, size_t length) {
120 assertions_queue[queue_size++] = (struct test_assertion){
121 BSTRING_MEM_EQ,
122 (union test_expectation_data){.string = {address, length}}};
123 }
124
byte_string_callback(void * context,cbor_data address,size_t length)125 void byte_string_callback(void *context, cbor_data address, size_t length) {
126 assert_true(current().expectation == BSTRING_MEM_EQ);
127 assert_true(current().data.string.address == address);
128 assert_true(current().data.string.length == length);
129 current_expectation++;
130 }
131
assert_bstring_indef_start()132 void assert_bstring_indef_start() {
133 assertions_queue[queue_size++] =
134 (struct test_assertion){.expectation = BSTRING_INDEF_START};
135 }
136
byte_string_start_callback(void * context)137 void byte_string_start_callback(void *context) {
138 assert_true(current().expectation == BSTRING_INDEF_START);
139 current_expectation++;
140 }
141
assert_indef_break()142 void assert_indef_break() {
143 assertions_queue[queue_size++] =
144 (struct test_assertion){.expectation = INDEF_BREAK};
145 }
146
indef_break_callback(void * context)147 void indef_break_callback(void *context) {
148 assert_true(current().expectation == INDEF_BREAK);
149 current_expectation++;
150 }
151
assert_array_start(size_t length)152 void assert_array_start(size_t length) {
153 assertions_queue[queue_size++] =
154 (struct test_assertion){ARRAY_START, {.length = length}};
155 }
156
array_start_callback(void * context,size_t length)157 void array_start_callback(void *context, size_t length) {
158 assert_true(current().expectation == ARRAY_START);
159 assert_true(current().data.length == length);
160 current_expectation++;
161 }
162
assert_indef_array_start()163 void assert_indef_array_start() {
164 assertions_queue[queue_size++] =
165 (struct test_assertion){.expectation = ARRAY_INDEF_START};
166 }
167
indef_array_start_callback(void * context)168 void indef_array_start_callback(void *context) {
169 assert_true(current().expectation == ARRAY_INDEF_START);
170 current_expectation++;
171 }
172
assert_map_start(size_t length)173 void assert_map_start(size_t length) {
174 assertions_queue[queue_size++] =
175 (struct test_assertion){MAP_START, {.length = length}};
176 }
177
map_start_callback(void * context,size_t length)178 void map_start_callback(void *context, size_t length) {
179 assert_true(current().expectation == MAP_START);
180 assert_true(current().data.length == length);
181 current_expectation++;
182 }
183
assert_indef_map_start()184 void assert_indef_map_start() {
185 assertions_queue[queue_size++] =
186 (struct test_assertion){.expectation = MAP_INDEF_START};
187 }
188
indef_map_start_callback(void * context)189 void indef_map_start_callback(void *context) {
190 assert_true(current().expectation == MAP_INDEF_START);
191 current_expectation++;
192 }
193
assert_tag_eq(uint64_t value)194 void assert_tag_eq(uint64_t value) {
195 assertions_queue[queue_size++] =
196 (struct test_assertion){TAG_EQ, {.int64 = value}};
197 }
198
tag_callback(void * context,uint64_t value)199 void tag_callback(void *context, uint64_t value) {
200 assert_true(current().expectation == TAG_EQ);
201 assert_true(current().data.int64 == value);
202 current_expectation++;
203 }
204
assert_half(float value)205 void assert_half(float value) {
206 assertions_queue[queue_size++] =
207 (struct test_assertion){HALF_EQ, {.float2 = value}};
208 }
209
half_callback(void * context,float actual)210 void half_callback(void *context, float actual) {
211 assert_true(current().expectation == HALF_EQ);
212 assert_true(current().data.float2 == actual);
213 current_expectation++;
214 }
215
assert_float(float value)216 void assert_float(float value) {
217 assertions_queue[queue_size++] =
218 (struct test_assertion){FLOAT_EQ, {.float4 = value}};
219 }
220
float_callback(void * context,float actual)221 void float_callback(void *context, float actual) {
222 assert_true(current().expectation == FLOAT_EQ);
223 assert_true(current().data.float4 == actual);
224 current_expectation++;
225 }
226
assert_double(double value)227 void assert_double(double value) {
228 assertions_queue[queue_size++] =
229 (struct test_assertion){DOUBLE_EQ, {.float8 = value}};
230 }
231
double_callback(void * context,double actual)232 void double_callback(void *context, double actual) {
233 assert_true(current().expectation == DOUBLE_EQ);
234 assert_true(current().data.float8 == actual);
235 current_expectation++;
236 }
237
assert_bool(bool value)238 void assert_bool(bool value) {
239 assertions_queue[queue_size++] =
240 (struct test_assertion){BOOL_EQ, {.boolean = value}};
241 }
242
assert_nil()243 void assert_nil() {
244 assertions_queue[queue_size++] = (struct test_assertion){.expectation = NIL};
245 }
246
assert_undef()247 void assert_undef() {
248 assertions_queue[queue_size++] =
249 (struct test_assertion){.expectation = UNDEF};
250 }
251
bool_callback(void * context,bool actual)252 void bool_callback(void *context, bool actual) {
253 assert_true(current().expectation == BOOL_EQ);
254 assert_true(current().data.boolean == actual);
255 current_expectation++;
256 }
257
null_callback(void * context)258 void null_callback(void *context) {
259 assert_true(current().expectation == NIL);
260 current_expectation++;
261 }
262
undef_callback(void * context)263 void undef_callback(void *context) {
264 assert_true(current().expectation == UNDEF);
265 current_expectation++;
266 }
267
268 const struct cbor_callbacks asserting_callbacks = {
269
270 .uint8 = &uint8_callback,
271
272 .uint16 = &uint16_callback,
273
274 .uint32 = &uint32_callback,
275
276 .uint64 = &uint64_callback,
277
278 .negint8 = &negint8_callback,
279
280 .negint16 = &negint16_callback,
281
282 .negint32 = &negint32_callback,
283
284 .negint64 = &negint64_callback,
285
286 .byte_string = &byte_string_callback,
287 .byte_string_start = &byte_string_start_callback,
288
289 .array_start = &array_start_callback,
290 .indef_array_start = &indef_array_start_callback,
291
292 .map_start = &map_start_callback,
293 .indef_map_start = &indef_map_start_callback,
294
295 .tag = &tag_callback,
296
297 .float2 = &half_callback,
298
299 .float4 = &float_callback,
300
301 .float8 = &double_callback,
302
303 .undefined = &undef_callback,
304 .boolean = &bool_callback,
305 .null = &null_callback,
306 .indef_break = &indef_break_callback};
307
decode(cbor_data source,size_t source_size)308 struct cbor_decoder_result decode(cbor_data source, size_t source_size) {
309 int last_expectation = current_expectation;
310 struct cbor_decoder_result result =
311 decoder(source, source_size, &asserting_callbacks, NULL);
312 if (result.status == CBOR_DECODER_FINISHED) {
313 // Check that we have matched an expectation from the queue
314 assert_true(last_expectation + 1 == current_expectation);
315 }
316 return result;
317 }
318