xref: /freebsd/contrib/libcbor/test/stream_expectations.h (revision 3078531de10dcae44b253a35125c949ff4235284)
1 /*
2  * This file provides testing tools for the streaming decoder. The intended
3  * usage is as follows: 1) SE API wrapper is initialized 2) Client builds
4  * (ordered) series of expectations 3) The decoder is executed 4) SE checks all
5  * assertions 5) Go to 2) if desired
6  */
7 
8 #ifndef STREAM_EXPECTATIONS_H_
9 #define STREAM_EXPECTATIONS_H_
10 
11 #include <setjmp.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 
15 #include <cmocka.h>
16 
17 #include <stdint.h>
18 #include "cbor.h"
19 
20 // TODO: This setup is overengineered, we currently use one assertion at a time
21 // TOOD: We never ensure that the queue is empty
22 #define MAX_QUEUE_ITEMS 30
23 
24 enum test_expectation {
25   UINT8_EQ,
26   UINT16_EQ,
27   UINT32_EQ,
28   UINT64_EQ,
29 
30   NEGINT8_EQ,
31   NEGINT16_EQ,
32   NEGINT32_EQ,
33   NEGINT64_EQ,
34 
35   BSTRING_MEM_EQ, /* Matches length and memory address for definite byte strings
36                    */
37   BSTRING_INDEF_START,
38 
39   ARRAY_START, /* Definite arrays only */
40   ARRAY_INDEF_START,
41 
42   MAP_START, /* Definite maps only */
43   MAP_INDEF_START,
44 
45   TAG_EQ,
46 
47   HALF_EQ,
48   FLOAT_EQ,
49   DOUBLE_EQ,
50   BOOL_EQ,
51   NIL,
52   UNDEF,
53   INDEF_BREAK /* Expect "Break" */
54 };
55 
56 union test_expectation_data {
57   uint8_t int8;
58   uint16_t int16;
59   uint32_t int32;
60   uint64_t int64;
61   struct string {
62     cbor_data address;
63     size_t length;
64   } string;
65   size_t length;
66   float float2;
67   float float4;
68   double float8;
69   bool boolean;
70 };
71 
72 struct test_assertion {
73   enum test_expectation expectation;
74   union test_expectation_data data;
75 };
76 
77 /* Tested function */
78 // TODO: This looks overengineered, we only ever use one (?) in the testsuite
79 typedef struct cbor_decoder_result decoder_t(cbor_data, size_t,
80                                              const struct cbor_callbacks *,
81                                              void *);
82 void set_decoder(decoder_t *);
83 struct cbor_decoder_result decode(cbor_data, size_t);
84 
85 /* Test setup */
86 int clear_stream_assertions(void **);
87 
88 /* Assertions builders */
89 void assert_uint8_eq(uint8_t);
90 void assert_uint16_eq(uint16_t);
91 void assert_uint32_eq(uint32_t);
92 void assert_uint64_eq(uint64_t);
93 
94 void assert_negint8_eq(uint8_t);
95 void assert_negint16_eq(uint16_t);
96 void assert_negint32_eq(uint32_t);
97 void assert_negint64_eq(uint64_t);
98 
99 void assert_bstring_mem_eq(cbor_data, size_t);
100 void assert_bstring_indef_start();
101 
102 void assert_array_start(size_t);
103 void assert_indef_array_start();
104 
105 void assert_map_start(size_t);
106 void assert_indef_map_start();
107 
108 void assert_tag_eq(uint64_t);
109 
110 void assert_half(float);
111 void assert_float(float);
112 void assert_double(double);
113 
114 void assert_bool(bool);
115 void assert_nil(); /* assert_null already exists */
116 void assert_undef();
117 
118 void assert_indef_break();
119 
120 /* Assertions verifying callbacks */
121 void uint8_callback(void *, uint8_t);
122 void uint16_callback(void *, uint16_t);
123 void uint32_callback(void *, uint32_t);
124 void uint64_callback(void *, uint64_t);
125 
126 void negint8_callback(void *, uint8_t);
127 void negint16_callback(void *, uint16_t);
128 void negint32_callback(void *, uint32_t);
129 void negint64_callback(void *, uint64_t);
130 
131 void byte_string_callback(void *, cbor_data, size_t);
132 void byte_string_start_callback(void *);
133 
134 void array_start_callback(void *, size_t);
135 void indef_array_start_callback(void *);
136 
137 void map_start_callback(void *, size_t);
138 void indef_map_start_callback(void *);
139 
140 void tag_callback(void *, uint64_t);
141 
142 void half_callback(void *, float);
143 void float_callback(void *, float);
144 void double_callback(void *, double);
145 void indef_break_callback(void *);
146 
147 void bool_callback(void *, bool);
148 void null_callback(void *);
149 void undef_callback(void *);
150 
151 #endif
152