xref: /netbsd-src/external/mit/libcbor/dist/test/stream_expectations.h (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
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 #define MAX_QUEUE_ITEMS 30
21 
22 enum test_expectation {
23   UINT8_EQ,
24   UINT16_EQ,
25   UINT32_EQ,
26   UINT64_EQ,
27 
28   NEGINT8_EQ,
29   NEGINT16_EQ,
30   NEGINT32_EQ,
31   NEGINT64_EQ,
32 
33   BSTRING_MEM_EQ, /* Matches length and memory address for definite byte strings
34                    */
35   BSTRING_INDEF_START,
36 
37   ARRAY_START, /* Definite arrays only */
38   ARRAY_INDEF_START,
39 
40   MAP_START, /* Definite maps only */
41   MAP_INDEF_START,
42 
43   TAG_EQ,
44 
45   HALF_EQ,
46   FLOAT_EQ,
47   DOUBLE_EQ,
48   BOOL_EQ,
49   NIL,
50   UNDEF,
51   INDEF_BREAK /* Expect "Break" */
52 };
53 
54 union test_expectation_data {
55   uint8_t int8;
56   uint16_t int16;
57   uint32_t int32;
58   uint64_t int64;
59   struct string {
60     cbor_data address;
61     size_t length;
62   } string;
63   size_t length;
64   float float2;
65   float float4;
66   double float8;
67   bool boolean;
68 };
69 
70 struct test_assertion {
71   enum test_expectation expectation;
72   union test_expectation_data data;
73 };
74 
75 /* Tested function */
76 typedef struct cbor_decoder_result decoder_t(cbor_data, size_t,
77                                              const struct cbor_callbacks *,
78                                              void *);
79 
80 void set_decoder(decoder_t *);
81 struct cbor_decoder_result decode(cbor_data, size_t);
82 
83 /* Assertions builders */
84 
85 void assert_uint8_eq(uint8_t);
86 void assert_uint16_eq(uint16_t);
87 void assert_uint32_eq(uint32_t);
88 void assert_uint64_eq(uint64_t);
89 
90 void assert_negint8_eq(uint8_t);
91 void assert_negint16_eq(uint16_t);
92 void assert_negint32_eq(uint32_t);
93 void assert_negint64_eq(uint64_t);
94 
95 void assert_bstring_mem_eq(cbor_data, size_t);
96 void assert_bstring_indef_start();
97 
98 void assert_array_start(size_t);
99 void assert_indef_array_start();
100 
101 void assert_map_start(size_t);
102 void assert_indef_map_start();
103 
104 void assert_tag_eq(uint64_t);
105 
106 void assert_half(float);
107 void assert_float(float);
108 void assert_double(double);
109 
110 void assert_bool(bool);
111 void assert_nil(); /* assert_null already exists */
112 void assert_undef();
113 
114 void assert_indef_break();
115 
116 /* Assertions verifying callbacks */
117 void uint8_callback(void *, uint8_t);
118 void uint16_callback(void *, uint16_t);
119 void uint32_callback(void *, uint32_t);
120 void uint64_callback(void *, uint64_t);
121 
122 void negint8_callback(void *, uint8_t);
123 void negint16_callback(void *, uint16_t);
124 void negint32_callback(void *, uint32_t);
125 void negint64_callback(void *, uint64_t);
126 
127 void byte_string_callback(void *, cbor_data, size_t);
128 void byte_string_start_callback(void *);
129 
130 void array_start_callback(void *, size_t);
131 void indef_array_start_callback(void *);
132 
133 void map_start_callback(void *, size_t);
134 void indef_map_start_callback(void *);
135 
136 void tag_callback(void *, uint64_t);
137 
138 void half_callback(void *, float);
139 void float_callback(void *, float);
140 void double_callback(void *, double);
141 void indef_break_callback(void *);
142 
143 void bool_callback(void *, bool);
144 void null_callback(void *);
145 void undef_callback(void *);
146 
147 #endif
148