]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/tests.c
Update tests to avoid printf to stdout/stderr when running as test cases.
[thirdparty/openssl.git] / test / testutil / tests.c
1 /*
2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include "../testutil.h"
11 #include "output.h"
12 #include "tu_local.h"
13
14 #include <errno.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include "../../e_os.h"
18
19 /*
20 * Output a failed test first line.
21 * All items are optional are generally not preinted if passed as NULL.
22 * The special cases are for prefix where "ERROR" is assumed and for left
23 * and right where a non-failure message is produced if either is NULL.
24 */
25 void test_fail_message_prefix(const char *prefix, const char *file,
26 int line, const char *type,
27 const char *left, const char *right,
28 const char *op)
29 {
30 test_printf_stderr("%*s# %s: ", subtest_level(), "",
31 prefix != NULL ? prefix : "ERROR");
32 if (type)
33 test_printf_stderr("(%s) ", type);
34 if (op != NULL) {
35 if (left != NULL && right != NULL)
36 test_printf_stderr("'%s %s %s' failed", left, op, right);
37 else
38 test_printf_stderr("'%s'", op);
39 }
40 if (file != NULL) {
41 test_printf_stderr(" @ %s:%d", file, line);
42 }
43 test_printf_stderr("\n");
44 }
45
46 /*
47 * A common routine to output test failure messages. Generally this should not
48 * be called directly, rather it should be called by the following functions.
49 *
50 * |desc| is a printf formatted description with arguments |args| that is
51 * supplied by the user and |desc| can be NULL. |type| is the data type
52 * that was tested (int, char, ptr, ...). |fmt| is a system provided
53 * printf format with following arguments that spell out the failure
54 * details i.e. the actual values compared and the operator used.
55 *
56 * The typical use for this is from an utility test function:
57 *
58 * int test6(const char *file, int line, int n) {
59 * if (n != 6) {
60 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
61 * return 0;
62 * }
63 * return 1;
64 * }
65 *
66 * calling test6(3, "oops") will return 0 and produce out along the lines of:
67 * FAIL oops: (int) value 3 is not 6\n
68 */
69 static void test_fail_message(const char *prefix, const char *file, int line,
70 const char *type, const char *left,
71 const char *right, const char *op,
72 const char *fmt, ...)
73 PRINTF_FORMAT(8, 9);
74
75 static void test_fail_message_va(const char *prefix, const char *file,
76 int line, const char *type,
77 const char *left, const char *right,
78 const char *op, const char *fmt, va_list ap)
79 {
80 test_fail_message_prefix(prefix, file, line, type, left, right, op);
81 if (fmt != NULL) {
82 test_printf_stderr("%*s# ", subtest_level(), "");
83 test_vprintf_stderr(fmt, ap);
84 test_printf_stderr("\n");
85 }
86 test_flush_stderr();
87 }
88
89 static void test_fail_message(const char *prefix, const char *file,
90 int line, const char *type,
91 const char *left, const char *right,
92 const char *op, const char *fmt, ...)
93 {
94 va_list ap;
95
96 va_start(ap, fmt);
97 test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap);
98 va_end(ap);
99 }
100
101 void test_info_c90(const char *desc, ...)
102 {
103 va_list ap;
104
105 va_start(ap, desc);
106 test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
107 va_end(ap);
108 }
109
110 void test_info(const char *file, int line, const char *desc, ...)
111 {
112 va_list ap;
113
114 va_start(ap, desc);
115 test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap);
116 va_end(ap);
117 }
118
119 void test_error_c90(const char *desc, ...)
120 {
121 va_list ap;
122
123 va_start(ap, desc);
124 test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
125 va_end(ap);
126 test_printf_stderr("\n");
127 }
128
129 void test_error(const char *file, int line, const char *desc, ...)
130 {
131 va_list ap;
132
133 va_start(ap, desc);
134 test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap);
135 va_end(ap);
136 test_printf_stderr("\n");
137 }
138
139 void test_perror(const char *s)
140 {
141 /*
142 * Using openssl_strerror_r causes linking issues since it isn't
143 * exported from libcrypto.so
144 */
145 TEST_error("%s: %s", s, strerror(errno));
146 }
147
148 void test_note(const char *fmt, ...)
149 {
150 if (fmt != NULL) {
151 va_list ap;
152
153 test_printf_stderr("%*s# ", subtest_level(), "");
154 va_start(ap, fmt);
155 test_vprintf_stderr(fmt, ap);
156 va_end(ap);
157 test_printf_stderr("\n");
158 }
159 test_flush_stderr();
160 }
161
162 void test_openssl_errors(void)
163 {
164 ERR_print_errors_cb(openssl_error_cb, NULL);
165 ERR_clear_error();
166 }
167
168 /*
169 * Define some comparisons between pairs of various types.
170 * These functions return 1 if the test is true.
171 * Otherwise, they return 0 and pretty-print diagnostics.
172 *
173 * In each case the functions produced are:
174 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
175 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
176 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
177 * int test_name_le(const type t1, const type t2, const char *desc, ...);
178 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
179 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
180 *
181 * The t1 and t2 arguments are to be compared for equality, inequality,
182 * less than, less than or equal to, greater than and greater than or
183 * equal to respectively. If the specified condition holds, the functions
184 * return 1. If the condition does not hold, the functions print a diagnostic
185 * message and return 0.
186 *
187 * The desc argument is a printf format string followed by its arguments and
188 * this is included in the output if the condition being tested for is false.
189 */
190 #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
191 int test_ ## name ## _ ## opname(const char *file, int line, \
192 const char *s1, const char *s2, \
193 const type t1, const type t2) \
194 { \
195 if (t1 op t2) \
196 return 1; \
197 test_fail_message(NULL, file, line, #type, s1, s2, #op, \
198 "[" fmt "] compared to [" fmt "]", \
199 t1, t2); \
200 return 0; \
201 }
202
203 #define DEFINE_COMPARISONS(type, name, fmt) \
204 DEFINE_COMPARISON(type, name, eq, ==, fmt) \
205 DEFINE_COMPARISON(type, name, ne, !=, fmt) \
206 DEFINE_COMPARISON(type, name, lt, <, fmt) \
207 DEFINE_COMPARISON(type, name, le, <=, fmt) \
208 DEFINE_COMPARISON(type, name, gt, >, fmt) \
209 DEFINE_COMPARISON(type, name, ge, >=, fmt)
210
211 DEFINE_COMPARISONS(int, int, "%d")
212 DEFINE_COMPARISONS(unsigned int, uint, "%u")
213 DEFINE_COMPARISONS(char, char, "%c")
214 DEFINE_COMPARISONS(unsigned char, uchar, "%u")
215 DEFINE_COMPARISONS(long, long, "%ld")
216 DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
217 DEFINE_COMPARISONS(size_t, size_t, "%zu")
218
219 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
220 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
221
222 int test_ptr_null(const char *file, int line, const char *s, const void *p)
223 {
224 if (p == NULL)
225 return 1;
226 test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
227 return 0;
228 }
229
230 int test_ptr(const char *file, int line, const char *s, const void *p)
231 {
232 if (p != NULL)
233 return 1;
234 test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
235 return 0;
236 }
237
238 int test_true(const char *file, int line, const char *s, int b)
239 {
240 if (b)
241 return 1;
242 test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
243 return 0;
244 }
245
246 int test_false(const char *file, int line, const char *s, int b)
247 {
248 if (!b)
249 return 1;
250 test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
251 return 0;
252 }
253
254 int test_str_eq(const char *file, int line, const char *st1, const char *st2,
255 const char *s1, const char *s2)
256 {
257 if (s1 == NULL && s2 == NULL)
258 return 1;
259 if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
260 test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
261 s1, s1 == NULL ? 0 : strlen(s1),
262 s2, s2 == NULL ? 0 : strlen(s2));
263 return 0;
264 }
265 return 1;
266 }
267
268 int test_str_ne(const char *file, int line, const char *st1, const char *st2,
269 const char *s1, const char *s2)
270 {
271 if ((s1 == NULL) ^ (s2 == NULL))
272 return 1;
273 if (s1 == NULL || strcmp(s1, s2) == 0) {
274 test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
275 s1, s1 == NULL ? 0 : strlen(s1),
276 s2, s2 == NULL ? 0 : strlen(s2));
277 return 0;
278 }
279 return 1;
280 }
281
282 int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
283 const char *s1, const char *s2, size_t len)
284 {
285 if (s1 == NULL && s2 == NULL)
286 return 1;
287 if (s1 == NULL || s2 == NULL || strncmp(s1, s2, len) != 0) {
288 test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
289 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
290 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
291 return 0;
292 }
293 return 1;
294 }
295
296 int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
297 const char *s1, const char *s2, size_t len)
298 {
299 if ((s1 == NULL) ^ (s2 == NULL))
300 return 1;
301 if (s1 == NULL || strncmp(s1, s2, len) == 0) {
302 test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
303 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
304 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
305 return 0;
306 }
307 return 1;
308 }
309
310 int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
311 const void *s1, size_t n1, const void *s2, size_t n2)
312 {
313 if (s1 == NULL && s2 == NULL)
314 return 1;
315 if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
316 test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
317 s1, n1, s2, n2);
318 return 0;
319 }
320 return 1;
321 }
322
323 int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
324 const void *s1, size_t n1, const void *s2, size_t n2)
325 {
326 if ((s1 == NULL) ^ (s2 == NULL))
327 return 1;
328 if (n1 != n2)
329 return 1;
330 if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
331 test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
332 s1, n1, s2, n2);
333 return 0;
334 }
335 return 1;
336 }
337
338 #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
339 int test_BN_ ## opname(const char *file, int line, \
340 const char *s1, const char *s2, \
341 const BIGNUM *t1, const BIGNUM *t2) \
342 { \
343 if (BN_cmp(t1, t2) op 0) \
344 return 1; \
345 test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
346 #op, t1, t2); \
347 return 0; \
348 } \
349 int test_BN_ ## opname ## _zero(const char *file, int line, \
350 const char *s, const BIGNUM *a) \
351 { \
352 if (a != NULL &&(zero_cond)) \
353 return 1; \
354 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
355 s, "0", #op, a); \
356 return 0; \
357 }
358
359 DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
360 DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
361 DEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a))
362 DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
363 DEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a))
364 DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
365
366 int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
367 {
368 if (a != NULL && BN_is_one(a))
369 return 1;
370 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
371 return 0;
372 }
373
374 int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
375 {
376 if (a != NULL && BN_is_odd(a))
377 return 1;
378 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
379 return 0;
380 }
381
382 int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
383 {
384 if (a != NULL && !BN_is_odd(a))
385 return 1;
386 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
387 a);
388 return 0;
389 }
390
391 int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
392 const BIGNUM *a, BN_ULONG w)
393 {
394 BIGNUM *bw;
395
396 if (a != NULL && BN_is_word(a, w))
397 return 1;
398 bw = BN_new();
399 BN_set_word(bw, w);
400 test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
401 BN_free(bw);
402 return 0;
403 }
404
405 int test_BN_abs_eq_word(const char *file, int line, const char *bns,
406 const char *ws, const BIGNUM *a, BN_ULONG w)
407 {
408 BIGNUM *bw, *aa;
409
410 if (a != NULL && BN_abs_is_word(a, w))
411 return 1;
412 bw = BN_new();
413 aa = BN_dup(a);
414 BN_set_negative(aa, 0);
415 BN_set_word(bw, w);
416 test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
417 aa, bw);
418 BN_free(bw);
419 BN_free(aa);
420 return 0;
421 }