xref: /freebsd/crypto/openssh/regress/unittests/test_helper/test_helper.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*	$OpenBSD: test_helper.h,v 1.8 2018/02/08 08:46:20 djm 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 "includes.h"
24 
25 #include <sys/types.h>
26 #ifdef HAVE_STDINT_H
27 # include <stdint.h>
28 #endif
29 
30 #include <openssl/bn.h>
31 #include <openssl/err.h>
32 
33 enum test_predicate {
34 	TEST_EQ, TEST_NE, TEST_LT, TEST_LE, TEST_GT, TEST_GE
35 };
36 typedef void (test_onerror_func_t)(void *);
37 
38 /* Supplied by test suite */
39 void tests(void);
40 
41 const char *test_data_file(const char *name);
42 void test_start(const char *n);
43 void test_info(char *s, size_t len);
44 void set_onerror_func(test_onerror_func_t *f, void *ctx);
45 void test_done(void);
46 int test_is_verbose(void);
47 int test_is_quiet(void);
48 void test_subtest_info(const char *fmt, ...)
49     __attribute__((format(printf, 1, 2)));
50 void ssl_err_check(const char *file, int line);
51 void assert_bignum(const char *file, int line,
52     const char *a1, const char *a2,
53     const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred);
54 void assert_string(const char *file, int line,
55     const char *a1, const char *a2,
56     const char *aa1, const char *aa2, enum test_predicate pred);
57 void assert_mem(const char *file, int line,
58     const char *a1, const char *a2,
59     const void *aa1, const void *aa2, size_t l, enum test_predicate pred);
60 void assert_mem_filled(const char *file, int line,
61     const char *a1,
62     const void *aa1, u_char v, size_t l, enum test_predicate pred);
63 void assert_int(const char *file, int line,
64     const char *a1, const char *a2,
65     int aa1, int aa2, enum test_predicate pred);
66 void assert_size_t(const char *file, int line,
67     const char *a1, const char *a2,
68     size_t aa1, size_t aa2, enum test_predicate pred);
69 void assert_u_int(const char *file, int line,
70     const char *a1, const char *a2,
71     u_int aa1, u_int aa2, enum test_predicate pred);
72 void assert_long(const char *file, int line,
73     const char *a1, const char *a2,
74     long aa1, long aa2, enum test_predicate pred);
75 void assert_long_long(const char *file, int line,
76     const char *a1, const char *a2,
77     long long aa1, long long aa2, enum test_predicate pred);
78 void assert_char(const char *file, int line,
79     const char *a1, const char *a2,
80     char aa1, char aa2, enum test_predicate pred);
81 void assert_ptr(const char *file, int line,
82     const char *a1, const char *a2,
83     const void *aa1, const void *aa2, enum test_predicate pred);
84 void assert_u8(const char *file, int line,
85     const char *a1, const char *a2,
86     u_int8_t aa1, u_int8_t aa2, enum test_predicate pred);
87 void assert_u16(const char *file, int line,
88     const char *a1, const char *a2,
89     u_int16_t aa1, u_int16_t aa2, enum test_predicate pred);
90 void assert_u32(const char *file, int line,
91     const char *a1, const char *a2,
92     u_int32_t aa1, u_int32_t aa2, enum test_predicate pred);
93 void assert_u64(const char *file, int line,
94     const char *a1, const char *a2,
95     u_int64_t aa1, u_int64_t aa2, enum test_predicate pred);
96 
97 #define TEST_START(n)			test_start(n)
98 #define TEST_DONE()			test_done()
99 #define TEST_ONERROR(f, c)		set_onerror_func(f, c)
100 #define SSL_ERR_CHECK() 		ssl_err_check(__FILE__, __LINE__)
101 
102 #define ASSERT_BIGNUM_EQ(a1, a2) \
103 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
104 #define ASSERT_STRING_EQ(a1, a2) \
105 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
106 #define ASSERT_MEM_EQ(a1, a2, l) \
107 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_EQ)
108 #define ASSERT_MEM_FILLED_EQ(a1, c, l) \
109 	assert_mem_filled(__FILE__, __LINE__, #a1, a1, c, l, TEST_EQ)
110 #define ASSERT_MEM_ZERO_EQ(a1, l) \
111 	assert_mem_filled(__FILE__, __LINE__, #a1, a1, '\0', l, TEST_EQ)
112 #define ASSERT_INT_EQ(a1, a2) \
113 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
114 #define ASSERT_SIZE_T_EQ(a1, a2) \
115 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
116 #define ASSERT_U_INT_EQ(a1, a2) \
117 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
118 #define ASSERT_LONG_EQ(a1, a2) \
119 	assert_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
120 #define ASSERT_LONG_LONG_EQ(a1, a2) \
121 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
122 #define ASSERT_CHAR_EQ(a1, a2) \
123 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
124 #define ASSERT_PTR_EQ(a1, a2) \
125 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
126 #define ASSERT_U8_EQ(a1, a2) \
127 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
128 #define ASSERT_U16_EQ(a1, a2) \
129 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
130 #define ASSERT_U32_EQ(a1, a2) \
131 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
132 #define ASSERT_U64_EQ(a1, a2) \
133 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ)
134 
135 #define ASSERT_BIGNUM_NE(a1, a2) \
136 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
137 #define ASSERT_STRING_NE(a1, a2) \
138 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
139 #define ASSERT_MEM_NE(a1, a2, l) \
140 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_NE)
141 #define ASSERT_MEM_ZERO_NE(a1, l) \
142 	assert_mem_filled(__FILE__, __LINE__, #a1, a1, '\0', l, TEST_NE)
143 #define ASSERT_INT_NE(a1, a2) \
144 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
145 #define ASSERT_SIZE_T_NE(a1, a2) \
146 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
147 #define ASSERT_U_INT_NE(a1, a2) \
148 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
149 #define ASSERT_LONG_NE(a1, a2) \
150 	assert_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
151 #define ASSERT_LONG_LONG_NE(a1, a2) \
152 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
153 #define ASSERT_CHAR_NE(a1, a2) \
154 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
155 #define ASSERT_PTR_NE(a1, a2) \
156 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
157 #define ASSERT_U8_NE(a1, a2) \
158 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
159 #define ASSERT_U16_NE(a1, a2) \
160 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
161 #define ASSERT_U32_NE(a1, a2) \
162 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
163 #define ASSERT_U64_NE(a1, a2) \
164 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE)
165 
166 #define ASSERT_BIGNUM_LT(a1, a2) \
167 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
168 #define ASSERT_STRING_LT(a1, a2) \
169 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
170 #define ASSERT_MEM_LT(a1, a2, l) \
171 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_LT)
172 #define ASSERT_INT_LT(a1, a2) \
173 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
174 #define ASSERT_SIZE_T_LT(a1, a2) \
175 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
176 #define ASSERT_U_INT_LT(a1, a2) \
177 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
178 #define ASSERT_LONG_LT(a1, a2) \
179 	assert_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
180 #define ASSERT_LONG_LONG_LT(a1, a2) \
181 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
182 #define ASSERT_CHAR_LT(a1, a2) \
183 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
184 #define ASSERT_PTR_LT(a1, a2) \
185 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
186 #define ASSERT_U8_LT(a1, a2) \
187 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
188 #define ASSERT_U16_LT(a1, a2) \
189 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
190 #define ASSERT_U32_LT(a1, a2) \
191 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
192 #define ASSERT_U64_LT(a1, a2) \
193 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT)
194 
195 #define ASSERT_BIGNUM_LE(a1, a2) \
196 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
197 #define ASSERT_STRING_LE(a1, a2) \
198 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
199 #define ASSERT_MEM_LE(a1, a2, l) \
200 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_LE)
201 #define ASSERT_INT_LE(a1, a2) \
202 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
203 #define ASSERT_SIZE_T_LE(a1, a2) \
204 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
205 #define ASSERT_U_INT_LE(a1, a2) \
206 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
207 #define ASSERT_LONG_LE(a1, a2) \
208 	assert_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
209 #define ASSERT_LONG_LONG_LE(a1, a2) \
210 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
211 #define ASSERT_CHAR_LE(a1, a2) \
212 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
213 #define ASSERT_PTR_LE(a1, a2) \
214 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
215 #define ASSERT_U8_LE(a1, a2) \
216 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
217 #define ASSERT_U16_LE(a1, a2) \
218 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
219 #define ASSERT_U32_LE(a1, a2) \
220 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
221 #define ASSERT_U64_LE(a1, a2) \
222 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE)
223 
224 #define ASSERT_BIGNUM_GT(a1, a2) \
225 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
226 #define ASSERT_STRING_GT(a1, a2) \
227 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
228 #define ASSERT_MEM_GT(a1, a2, l) \
229 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_GT)
230 #define ASSERT_INT_GT(a1, a2) \
231 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
232 #define ASSERT_SIZE_T_GT(a1, a2) \
233 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
234 #define ASSERT_U_INT_GT(a1, a2) \
235 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
236 #define ASSERT_LONG_GT(a1, a2) \
237 	assert_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
238 #define ASSERT_LONG_LONG_GT(a1, a2) \
239 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
240 #define ASSERT_CHAR_GT(a1, a2) \
241 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
242 #define ASSERT_PTR_GT(a1, a2) \
243 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
244 #define ASSERT_U8_GT(a1, a2) \
245 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
246 #define ASSERT_U16_GT(a1, a2) \
247 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
248 #define ASSERT_U32_GT(a1, a2) \
249 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
250 #define ASSERT_U64_GT(a1, a2) \
251 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT)
252 
253 #define ASSERT_BIGNUM_GE(a1, a2) \
254 	assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
255 #define ASSERT_STRING_GE(a1, a2) \
256 	assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
257 #define ASSERT_MEM_GE(a1, a2, l) \
258 	assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_GE)
259 #define ASSERT_INT_GE(a1, a2) \
260 	assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
261 #define ASSERT_SIZE_T_GE(a1, a2) \
262 	assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
263 #define ASSERT_U_INT_GE(a1, a2) \
264 	assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
265 #define ASSERT_LONG_GE(a1, a2) \
266 	assert_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
267 #define ASSERT_LONG_LONG_GE(a1, a2) \
268 	assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
269 #define ASSERT_CHAR_GE(a1, a2) \
270 	assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
271 #define ASSERT_PTR_GE(a1, a2) \
272 	assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
273 #define ASSERT_U8_GE(a1, a2) \
274 	assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
275 #define ASSERT_U16_GE(a1, a2) \
276 	assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
277 #define ASSERT_U32_GE(a1, a2) \
278 	assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
279 #define ASSERT_U64_GE(a1, a2) \
280 	assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE)
281 
282 /* Fuzzing support */
283 
284 struct fuzz;
285 #define FUZZ_1_BIT_FLIP		0x00000001	/* Flip one bit at a time */
286 #define FUZZ_2_BIT_FLIP		0x00000002	/* Flip two bits at a time */
287 #define FUZZ_1_BYTE_FLIP	0x00000004	/* Flip one byte at a time */
288 #define FUZZ_2_BYTE_FLIP	0x00000008	/* Flip two bytes at a time */
289 #define FUZZ_TRUNCATE_START	0x00000010	/* Truncate from beginning */
290 #define FUZZ_TRUNCATE_END	0x00000020	/* Truncate from end */
291 #define FUZZ_BASE64		0x00000040	/* Try all base64 chars */
292 #define FUZZ_MAX		FUZZ_BASE64
293 
294 /* Start fuzzing a blob of data with selected strategies (bitmask) */
295 struct fuzz *fuzz_begin(u_int strategies, const void *p, size_t l);
296 
297 /* Free a fuzz context */
298 void fuzz_cleanup(struct fuzz *fuzz);
299 
300 /* Prepare the next fuzz case in the series */
301 void fuzz_next(struct fuzz *fuzz);
302 
303 /*
304  * Check whether this fuzz case is identical to the original
305  * This is slow, but useful if the caller needs to ensure that all tests
306  * generated change the input (e.g. when fuzzing signatures).
307  */
308 int fuzz_matches_original(struct fuzz *fuzz);
309 
310 /* Determine whether the current fuzz sequence is exhausted (nonzero = yes) */
311 int fuzz_done(struct fuzz *fuzz);
312 
313 /* Return the length and a pointer to the current fuzzed case */
314 size_t fuzz_len(struct fuzz *fuzz);
315 u_char *fuzz_ptr(struct fuzz *fuzz);
316 
317 /* Dump the current fuzz case to stderr */
318 void fuzz_dump(struct fuzz *fuzz);
319 
320 #endif /* _TEST_HELPER_H */
321