xref: /freebsd/contrib/libcbor/test/stream_expectations.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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 
13 void set_decoder(decoder_t *dec) { decoder = dec; }
14 
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 */
25 struct test_assertion current() {
26   return assertions_queue[current_expectation];
27 }
28 
29 /* Assertions builders and matcher callbacks */
30 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
132 void assert_bstring_indef_start() {
133   assertions_queue[queue_size++] =
134       (struct test_assertion){.expectation = BSTRING_INDEF_START};
135 }
136 
137 void byte_string_start_callback(void *context) {
138   assert_true(current().expectation == BSTRING_INDEF_START);
139   current_expectation++;
140 }
141 
142 void assert_indef_break() {
143   assertions_queue[queue_size++] =
144       (struct test_assertion){.expectation = INDEF_BREAK};
145 }
146 
147 void indef_break_callback(void *context) {
148   assert_true(current().expectation == INDEF_BREAK);
149   current_expectation++;
150 }
151 
152 void assert_array_start(size_t length) {
153   assertions_queue[queue_size++] =
154       (struct test_assertion){ARRAY_START, {.length = length}};
155 }
156 
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 
163 void assert_indef_array_start() {
164   assertions_queue[queue_size++] =
165       (struct test_assertion){.expectation = ARRAY_INDEF_START};
166 }
167 
168 void indef_array_start_callback(void *context) {
169   assert_true(current().expectation == ARRAY_INDEF_START);
170   current_expectation++;
171 }
172 
173 void assert_map_start(size_t length) {
174   assertions_queue[queue_size++] =
175       (struct test_assertion){MAP_START, {.length = length}};
176 }
177 
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 
184 void assert_indef_map_start() {
185   assertions_queue[queue_size++] =
186       (struct test_assertion){.expectation = MAP_INDEF_START};
187 }
188 
189 void indef_map_start_callback(void *context) {
190   assert_true(current().expectation == MAP_INDEF_START);
191   current_expectation++;
192 }
193 
194 void assert_tag_eq(uint64_t value) {
195   assertions_queue[queue_size++] =
196       (struct test_assertion){TAG_EQ, {.int64 = value}};
197 }
198 
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 
205 void assert_half(float value) {
206   assertions_queue[queue_size++] =
207       (struct test_assertion){HALF_EQ, {.float2 = value}};
208 }
209 
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 
216 void assert_float(float value) {
217   assertions_queue[queue_size++] =
218       (struct test_assertion){FLOAT_EQ, {.float4 = value}};
219 }
220 
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 
227 void assert_double(double value) {
228   assertions_queue[queue_size++] =
229       (struct test_assertion){DOUBLE_EQ, {.float8 = value}};
230 }
231 
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 
238 void assert_bool(bool value) {
239   assertions_queue[queue_size++] =
240       (struct test_assertion){BOOL_EQ, {.boolean = value}};
241 }
242 
243 void assert_nil() {
244   assertions_queue[queue_size++] = (struct test_assertion){.expectation = NIL};
245 }
246 
247 void assert_undef() {
248   assertions_queue[queue_size++] =
249       (struct test_assertion){.expectation = UNDEF};
250 }
251 
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 
258 void null_callback(void *context) {
259   assert_true(current().expectation == NIL);
260   current_expectation++;
261 }
262 
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 
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