1 /*	$OpenBSD: test_helper.h,v 1.3 2014/05/02 09:41:32 andre Exp $	*/
2 /*
3  * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* Utility functions/framework for regress tests */
19 
20 #ifndef _TEST_HELPER_H
21 #define _TEST_HELPER_H
22 
23 #include <sys/types.h>
24 #include <stdint.h>
25 
26 #include <openssl/bn.h>
27 #include <openssl/err.h>
28 
29 enum test_predicate {
30 	TEST_EQ, TEST_NE, TEST_LT, TEST_LE, TEST_GT, TEST_GE
31 };
32 typedef void (test_onerror_func_t)(void *);
33 
34 /* Supplied by test suite */
35 void tests(void);
36 
37 const char *test_data_file(const char *name);
38 void test_start(const char *n);
39 void set_onerror_func(test_onerror_func_t *f, void *ctx);
40 void test_done(void);
41 void ssl_err_check(const char *file, int line);
42 void assert_bignum(const char *file, int line,
43     const char *a1, const char *a2,
44     const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred);
45 void assert_string(const char *file, int line,
46     const char *a1, const char *a2,
47     const char *aa1, const char *aa2, enum test_predicate pred);
48 void assert_mem(const char *file, int line,
49     const char *a1, const char *a2,
50     const void *aa1, const void *aa2, size_t l, enum test_predicate pred);
51 void assert_mem_filled(const char *file, int line,
52     const char *a1,
53     const void *aa1, u_char v, size_t l, enum test_predicate pred);
54 void assert_int(const char *file, int line,
55     const char *a1, const char *a2,
56     int aa1, int aa2, enum test_predicate pred);
57 void assert_size_t(const char *file, int line,
58     const char *a1, const char *a2,
59     size_t aa1, size_t aa2, enum test_predicate pred);
60 void assert_u_int(const char *file, int line,
61     const char *a1, const char *a2,
62     u_int aa1, u_int aa2, enum test_predicate pred);
63 void assert_long_long(const char *file, int line,
64     const char *a1, const char *a2,
65     long long aa1, long long aa2, enum test_predicate pred);
66 void assert_char(const char *file, int line,
67     const char *a1, const char *a2,
68     char aa1, char aa2, enum test_predicate pred);
69 void assert_ptr(const char *file, int line,
70     const char *a1, const char *a2,
71     const void *aa1, const void *aa2, enum test_predicate pred);
72 void assert_u8(const char *file, int line,
73     const char *a1, const char *a2,
74     u_int8_t aa1, u_int8_t aa2, enum test_predicate pred);
75 void assert_u16(const char *file, int line,
76     const char *a1, const char *a2,
77     u_int16_t aa1, u_int16_t aa2, enum test_predicate pred);
78 void assert_u32(const char *file, int line,
79     const char *a1, const char *a2,
80     u_int32_t aa1, u_int32_t aa2, enum test_predicate pred);
81 void assert_u64(const char *file, int line,
82     const char *a1, const char *a2,
83     u_int64_t aa1, u_int64_t aa2, enum test_predicate pred);
84 
85 #define TEST_START(n)			test_start(n)
86 #define TEST_DONE()			test_done()
87 #define TEST_ONERROR(f, c)		set_onerror_func(f, c)
88 #define SSL_ERR_CHECK() 		ssl_err_check(__FILE__, __LINE__)
89 
90 #define ASSERT_BIGNUM_EQ(a1, a2) \
91 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
92 #define ASSERT_STRING_EQ(a1, a2) \
93 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
94 #define ASSERT_MEM_EQ(a1, a2, l) \
95 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_EQ)
96 #define ASSERT_MEM_FILLED_EQ(a1, c, l) \
97 	assert_mem_filled(__FILE__, __LINE__, #a1, a1, c, l, TEST_EQ)
98 #define ASSERT_MEM_ZERO_EQ(a1, l) \
99 	assert_mem_filled(__FILE__, __LINE__, #a1, a1, '\0', l, TEST_EQ)
100 #define ASSERT_INT_EQ(a1, a2) \
101 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
102 #define ASSERT_SIZE_T_EQ(a1, a2) \
103 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
104 #define ASSERT_U_INT_EQ(a1, a2) \
105 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
106 #define ASSERT_LONG_LONG_EQ(a1, a2) \
107 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
108 #define ASSERT_CHAR_EQ(a1, a2) \
109 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
110 #define ASSERT_PTR_EQ(a1, a2) \
111 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
112 #define ASSERT_U8_EQ(a1, a2) \
113 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
114 #define ASSERT_U16_EQ(a1, a2) \
115 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
116 #define ASSERT_U32_EQ(a1, a2) \
117 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
118 #define ASSERT_U64_EQ(a1, a2) \
119 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
120 
121 #define ASSERT_BIGNUM_NE(a1, a2) \
122 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
123 #define ASSERT_STRING_NE(a1, a2) \
124 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
125 #define ASSERT_MEM_NE(a1, a2, l) \
126 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_NE)
127 #define ASSERT_MEM_ZERO_NE(a1, l) \
128 	assert_mem_filled(__FILE__, __LINE__, #a1, a1, '\0', l, TEST_NE)
129 #define ASSERT_INT_NE(a1, a2) \
130 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
131 #define ASSERT_SIZE_T_NE(a1, a2) \
132 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
133 #define ASSERT_U_INT_NE(a1, a2) \
134 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
135 #define ASSERT_LONG_LONG_NE(a1, a2) \
136 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
137 #define ASSERT_CHAR_NE(a1, a2) \
138 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
139 #define ASSERT_PTR_NE(a1, a2) \
140 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
141 #define ASSERT_U8_NE(a1, a2) \
142 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
143 #define ASSERT_U16_NE(a1, a2) \
144 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
145 #define ASSERT_U32_NE(a1, a2) \
146 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
147 #define ASSERT_U64_NE(a1, a2) \
148 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
149 
150 #define ASSERT_BIGNUM_LT(a1, a2) \
151 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
152 #define ASSERT_STRING_LT(a1, a2) \
153 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
154 #define ASSERT_MEM_LT(a1, a2, l) \
155 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_LT)
156 #define ASSERT_INT_LT(a1, a2) \
157 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
158 #define ASSERT_SIZE_T_LT(a1, a2) \
159 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
160 #define ASSERT_U_INT_LT(a1, a2) \
161 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
162 #define ASSERT_LONG_LONG_LT(a1, a2) \
163 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
164 #define ASSERT_CHAR_LT(a1, a2) \
165 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
166 #define ASSERT_PTR_LT(a1, a2) \
167 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
168 #define ASSERT_U8_LT(a1, a2) \
169 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
170 #define ASSERT_U16_LT(a1, a2) \
171 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
172 #define ASSERT_U32_LT(a1, a2) \
173 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
174 #define ASSERT_U64_LT(a1, a2) \
175 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
176 
177 #define ASSERT_BIGNUM_LE(a1, a2) \
178 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
179 #define ASSERT_STRING_LE(a1, a2) \
180 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
181 #define ASSERT_MEM_LE(a1, a2, l) \
182 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_LE)
183 #define ASSERT_INT_LE(a1, a2) \
184 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
185 #define ASSERT_SIZE_T_LE(a1, a2) \
186 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
187 #define ASSERT_U_INT_LE(a1, a2) \
188 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
189 #define ASSERT_LONG_LONG_LE(a1, a2) \
190 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
191 #define ASSERT_CHAR_LE(a1, a2) \
192 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
193 #define ASSERT_PTR_LE(a1, a2) \
194 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
195 #define ASSERT_U8_LE(a1, a2) \
196 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
197 #define ASSERT_U16_LE(a1, a2) \
198 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
199 #define ASSERT_U32_LE(a1, a2) \
200 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
201 #define ASSERT_U64_LE(a1, a2) \
202 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
203 
204 #define ASSERT_BIGNUM_GT(a1, a2) \
205 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
206 #define ASSERT_STRING_GT(a1, a2) \
207 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
208 #define ASSERT_MEM_GT(a1, a2, l) \
209 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_GT)
210 #define ASSERT_INT_GT(a1, a2) \
211 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
212 #define ASSERT_SIZE_T_GT(a1, a2) \
213 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
214 #define ASSERT_U_INT_GT(a1, a2) \
215 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
216 #define ASSERT_LONG_LONG_GT(a1, a2) \
217 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
218 #define ASSERT_CHAR_GT(a1, a2) \
219 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
220 #define ASSERT_PTR_GT(a1, a2) \
221 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
222 #define ASSERT_U8_GT(a1, a2) \
223 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
224 #define ASSERT_U16_GT(a1, a2) \
225 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
226 #define ASSERT_U32_GT(a1, a2) \
227 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
228 #define ASSERT_U64_GT(a1, a2) \
229 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
230 
231 #define ASSERT_BIGNUM_GE(a1, a2) \
232 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
233 #define ASSERT_STRING_GE(a1, a2) \
234 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
235 #define ASSERT_MEM_GE(a1, a2, l) \
236 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_GE)
237 #define ASSERT_INT_GE(a1, a2) \
238 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
239 #define ASSERT_SIZE_T_GE(a1, a2) \
240 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
241 #define ASSERT_U_INT_GE(a1, a2) \
242 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
243 #define ASSERT_LONG_LONG_GE(a1, a2) \
244 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
245 #define ASSERT_CHAR_GE(a1, a2) \
246 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
247 #define ASSERT_PTR_GE(a1, a2) \
248 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
249 #define ASSERT_U8_GE(a1, a2) \
250 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
251 #define ASSERT_U16_GE(a1, a2) \
252 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
253 #define ASSERT_U32_GE(a1, a2) \
254 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
255 #define ASSERT_U64_GE(a1, a2) \
256 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
257 
258 /* Fuzzing support */
259 
260 struct fuzz;
261 #define FUZZ_1_BIT_FLIP		0x00000001	/* Flip one bit at a time */
262 #define FUZZ_2_BIT_FLIP		0x00000002	/* Flip two bits at a time */
263 #define FUZZ_1_BYTE_FLIP	0x00000004	/* Flip one byte at a time */
264 #define FUZZ_2_BYTE_FLIP	0x00000008	/* Flip two bytes at a time */
265 #define FUZZ_TRUNCATE_START	0x00000010	/* Truncate from beginning */
266 #define FUZZ_TRUNCATE_END	0x00000020	/* Truncate from end */
267 #define FUZZ_BASE64		0x00000040	/* Try all base64 chars */
268 #define FUZZ_MAX		FUZZ_BASE64
269 
270 /* Start fuzzing a blob of data with selected strategies (bitmask) */
271 struct fuzz *fuzz_begin(u_int strategies, const void *p, size_t l);
272 
273 /* Free a fuzz context */
274 void fuzz_cleanup(struct fuzz *fuzz);
275 
276 /* Prepare the next fuzz case in the series */
277 void fuzz_next(struct fuzz *fuzz);
278 
279 /* Determine whether the current fuzz sequence is exhausted (nonzero = yes) */
280 int fuzz_done(struct fuzz *fuzz);
281 
282 /* Return the length and a pointer to the current fuzzed case */
283 size_t fuzz_len(struct fuzz *fuzz);
284 u_char *fuzz_ptr(struct fuzz *fuzz);
285 
286 /* Dump the current fuzz case to stderr */
287 void fuzz_dump(struct fuzz *fuzz);
288 #endif /* _TEST_HELPER_H */
289