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