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