xref: /freebsd/contrib/libcbor/test/stream_expectations.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 #ifndef STREAM_EXPECTATIONS_H_
2 #define STREAM_EXPECTATIONS_H_
3 
4 #include "assertions.h"
5 #include "cbor.h"
6 
7 #define MAX_QUEUE_ITEMS 30
8 
9 // Utilities to test `cbor_stream_decode`.  See `cbor_stream_decode_test.cc`.
10 //
11 // Usage:
12 // - The `assert_` helpers build a queue of `test_assertion`s
13 //   (`assertions_queue` in the implementation file), specifying
14 //  - Which callback is expected (`test_expectation`)
15 //  - And what is the expected argument value (if applicable,
16 //    `test_expectation_data`)
17 // - `decode` will invoke `cbor_stream_decode` (test subject)
18 // - `cbor_stream_decode` will invoke one of the `_callback` functions, which
19 //   will check the passed data against the `assertions_queue`
20 
21 enum test_expectation {
22   UINT8_EQ,
23   UINT16_EQ,
24   UINT32_EQ,
25   UINT64_EQ,
26 
27   NEGINT8_EQ,
28   NEGINT16_EQ,
29   NEGINT32_EQ,
30   NEGINT64_EQ,
31 
32   // Matches length and memory address for definite strings
33   BSTRING_MEM_EQ,
34   BSTRING_INDEF_START,
35 
36   STRING_MEM_EQ,
37   STRING_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 /* Test harness -- calls `cbor_stream_decode` and checks assertions */
78 struct cbor_decoder_result decode(cbor_data, size_t);
79 
80 /* Verify all assertions were applied and clean up */
81 int clean_up_stream_assertions(void **);
82 
83 /* Assertions builders */
84 void assert_uint8_eq(uint8_t);
85 void assert_uint16_eq(uint16_t);
86 void assert_uint32_eq(uint32_t);
87 void assert_uint64_eq(uint64_t);
88 
89 void assert_negint8_eq(uint8_t);
90 void assert_negint16_eq(uint16_t);
91 void assert_negint32_eq(uint32_t);
92 void assert_negint64_eq(uint64_t);
93 
94 void assert_bstring_mem_eq(cbor_data, size_t);
95 void assert_bstring_indef_start(void);
96 
97 void assert_string_mem_eq(cbor_data, size_t);
98 void assert_string_indef_start(void);
99 
100 void assert_array_start(size_t);
101 void assert_indef_array_start(void);
102 
103 void assert_map_start(size_t);
104 void assert_indef_map_start(void);
105 
106 void assert_tag_eq(uint64_t);
107 
108 void assert_half(float);
109 void assert_float(float);
110 void assert_double(double);
111 
112 void assert_bool(bool);
113 void assert_nil(void); /* assert_null already exists */
114 void assert_undef(void);
115 
116 void assert_indef_break(void);
117 
118 /* Assertions verifying callbacks */
119 void uint8_callback(void *, uint8_t);
120 void uint16_callback(void *, uint16_t);
121 void uint32_callback(void *, uint32_t);
122 void uint64_callback(void *, uint64_t);
123 
124 void negint8_callback(void *, uint8_t);
125 void negint16_callback(void *, uint16_t);
126 void negint32_callback(void *, uint32_t);
127 void negint64_callback(void *, uint64_t);
128 
129 void byte_string_callback(void *, cbor_data, uint64_t);
130 void byte_string_start_callback(void *);
131 
132 void string_callback(void *, cbor_data, uint64_t);
133 void string_start_callback(void *);
134 
135 void array_start_callback(void *, uint64_t);
136 void indef_array_start_callback(void *);
137 
138 void map_start_callback(void *, uint64_t);
139 void indef_map_start_callback(void *);
140 
141 void tag_callback(void *, uint64_t);
142 
143 void half_callback(void *, float);
144 void float_callback(void *, float);
145 void double_callback(void *, double);
146 void indef_break_callback(void *);
147 
148 void bool_callback(void *, bool);
149 void null_callback(void *);
150 void undef_callback(void *);
151 
152 #endif
153