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