testutil: Add commodity printing functions test_printf_std{out,err}
[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 "../../e_os.h"
16
17 /* The size of memory buffers to display on failure */
18 #define MEM_BUFFER_SIZE     (21)
19
20 /*
21  * A common routine to output test failure messages.  Generally this should not
22  * be called directly, rather it should be called by the following functions.
23  *
24  * |desc| is a printf formatted description with arguments |args| that is
25  * supplied by the user and |desc| can be NULL.  |type| is the data type
26  * that was tested (int, char, ptr, ...).  |fmt| is a system provided
27  * printf format with following arguments that spell out the failure
28  * details i.e. the actual values compared and the operator used.
29  *
30  * The typical use for this is from an utility test function:
31  *
32  * int test6(const char *file, int line, int n) {
33  *     if (n != 6) {
34  *         test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
35  *         return 0;
36  *     }
37  *     return 1;
38  * }
39  *
40  * calling test6(3, "oops") will return 0 and produce out along the lines of:
41  *      FAIL oops: (int) value 3 is not 6\n
42  *
43  * It general, test_fail_message should not be called directly.
44  */
45 static void test_fail_message(const char *prefix, const char *file, int line,
46                               const char *type, const char *fmt, ...)
47             PRINTF_FORMAT(5, 6);
48
49 static void test_fail_message_va(const char *prefix, const char *file, int line,
50                                  const char *type, const char *fmt, va_list ap)
51 {
52     test_printf_stderr("%*s# ", subtest_level(), "");
53     test_puts_stderr(prefix != NULL ? prefix : "ERROR");
54     test_puts_stderr(":");
55     if (type)
56         test_printf_stderr(" (%s)", type);
57     if (fmt != NULL) {
58         test_puts_stderr(" ");
59         test_vprintf_stderr(fmt, ap);
60     }
61     if (file != NULL) {
62         test_printf_stderr(" @ %s:%d", file, line);
63     }
64     test_puts_stderr("\n");
65     test_flush_stderr();
66 }
67
68 static void test_fail_message(const char *prefix, const char *file, int line,
69                               const char *type, const char *fmt, ...)
70 {
71     va_list ap;
72
73     va_start(ap, fmt);
74     test_fail_message_va(prefix, file, line, type, fmt, ap);
75     va_end(ap);
76 }
77
78 void test_info_c90(const char *desc, ...)
79 {
80     va_list ap;
81
82     va_start(ap, desc);
83     test_fail_message_va("INFO", NULL, -1, NULL, desc, ap);
84     va_end(ap);
85 }
86
87 void test_info(const char *file, int line, const char *desc, ...)
88 {
89     va_list ap;
90
91     va_start(ap, desc);
92     test_fail_message_va("INFO", file, line, NULL, desc, ap);
93     va_end(ap);
94 }
95
96 void test_error_c90(const char *desc, ...)
97 {
98     va_list ap;
99
100     va_start(ap, desc);
101     test_fail_message(NULL, NULL, -1, NULL, desc, ap);
102     va_end(ap);
103 }
104
105 void test_error(const char *file, int line, const char *desc, ...)
106 {
107     va_list ap;
108
109     va_start(ap, desc);
110     test_fail_message_va(NULL, file, line, NULL, desc, ap);
111     va_end(ap);
112 }
113
114 /*
115  * Define some comparisons between pairs of various types.
116  * These functions return 1 if the test is true.
117  * Otherwise, they return 0 and pretty-print diagnostics.
118  *
119  * In each case the functions produced are:
120  *  int test_name_eq(const type t1, const type t2, const char *desc, ...);
121  *  int test_name_ne(const type t1, const type t2, const char *desc, ...);
122  *  int test_name_lt(const type t1, const type t2, const char *desc, ...);
123  *  int test_name_le(const type t1, const type t2, const char *desc, ...);
124  *  int test_name_gt(const type t1, const type t2, const char *desc, ...);
125  *  int test_name_ge(const type t1, const type t2, const char *desc, ...);
126  *
127  * The t1 and t2 arguments are to be compared for equality, inequality,
128  * less than, less than or equal to, greater than and greater than or
129  * equal to respectively.  If the specified condition holds, the functions
130  * return 1.  If the condition does not hold, the functions print a diagnostic
131  * message and return 0.
132  *
133  * The desc argument is a printf format string followed by its arguments and
134  * this is included in the output if the condition being tested for is false.
135  */
136 #define DEFINE_COMPARISON(type, name, opname, op, fmt)                  \
137     int test_ ## name ## _ ## opname(const char *file, int line,        \
138                                      const char *s1, const char *s2,    \
139                                      const type t1, const type t2)      \
140     {                                                                   \
141         if (t1 op t2)                                                   \
142             return 1;                                                   \
143         test_fail_message(NULL, file, line, #type,                      \
144                           "%s [" fmt "] " #op " %s [" fmt "]",          \
145                           s1, t1, s2, t2);                              \
146         return 0;                                                       \
147     }
148
149 #define DEFINE_COMPARISONS(type, name, fmt)                             \
150     DEFINE_COMPARISON(type, name, eq, ==, fmt)                          \
151     DEFINE_COMPARISON(type, name, ne, !=, fmt)                          \
152     DEFINE_COMPARISON(type, name, lt, <, fmt)                           \
153     DEFINE_COMPARISON(type, name, le, <=, fmt)                          \
154     DEFINE_COMPARISON(type, name, gt, >, fmt)                           \
155     DEFINE_COMPARISON(type, name, ge, >=, fmt)
156
157 DEFINE_COMPARISONS(int, int, "%d")
158 DEFINE_COMPARISONS(unsigned int, uint, "%u")
159 DEFINE_COMPARISONS(char, char, "%c")
160 DEFINE_COMPARISONS(unsigned char, uchar, "%u")
161 DEFINE_COMPARISONS(long, long, "%ld")
162 DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
163 DEFINE_COMPARISONS(size_t, size_t, "%zu")
164
165 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
166 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
167
168 int test_ptr_null(const char *file, int line, const char *s, const void *p)
169 {
170     if (p == NULL)
171         return 1;
172     test_fail_message(NULL, file, line, "ptr", "%s [%p] == NULL", s, p);
173     return 0;
174 }
175
176 int test_ptr(const char *file, int line, const char *s, const void *p)
177 {
178     if (p != NULL)
179         return 1;
180     test_fail_message(NULL, file, line, "ptr", "%s [%p] != NULL", s, p);
181     return 0;
182 }
183
184 int test_true(const char *file, int line, const char *s, int b)
185 {
186     if (b)
187         return 1;
188     test_fail_message(NULL, file, line, "bool", "%s [false] == true", s);
189     return 0;
190 }
191
192 int test_false(const char *file, int line, const char *s, int b)
193 {
194     if (!b)
195         return 1;
196     test_fail_message(NULL, file, line, "bool", "%s [true] == false", s);
197     return 0;
198 }
199
200 static const char *print_string_maybe_null(const char *s)
201 {
202     return s == NULL ? "(NULL)" : s;
203 }
204
205 int test_str_eq(const char *file, int line, const char *st1, const char *st2,
206                 const char *s1, const char *s2)
207 {
208     if (s1 == NULL && s2 == NULL)
209       return 1;
210     if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
211         test_fail_message(NULL, file, line, "string", "%s [%s] == %s [%s]",
212                           st1, print_string_maybe_null(s1),
213                           st2, print_string_maybe_null(s2));
214         return 0;
215     }
216     return 1;
217 }
218
219 int test_str_ne(const char *file, int line, const char *st1, const char *st2,
220                 const char *s1, const char *s2)
221 {
222     if ((s1 == NULL) ^ (s2 == NULL))
223       return 1;
224     if (s1 == NULL || strcmp(s1, s2) == 0) {
225         test_fail_message(NULL, file, line, "string", "%s [%s] != %s [%s]",
226                           st1, print_string_maybe_null(s1),
227                           st2, print_string_maybe_null(s2));
228         return 0;
229     }
230     return 1;
231 }
232
233 int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
234                  const char *s1, const char *s2, size_t len)
235 {
236     int prec = (int)len;
237
238     if (s1 == NULL && s2 == NULL)
239       return 1;
240     if (s1 == NULL || s2 == NULL || strncmp(s1, s2, len) != 0) {
241         test_fail_message(NULL, file, line, "string", "%.s [%.*s] == %s [%.*s]",
242                           st1, prec, print_string_maybe_null(s1),
243                           st2, prec, print_string_maybe_null(s2));
244         return 0;
245     }
246     return 1;
247 }
248
249 int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
250                  const char *s1, const char *s2, size_t len)
251 {
252     int prec = (int)len;
253
254     if ((s1 == NULL) ^ (s2 == NULL))
255       return 1;
256     if (s1 == NULL || strncmp(s1, s2, len) == 0) {
257         test_fail_message(NULL, file, line, "string", "%s [%.*s] != %s [%.*s]",
258                           st1, prec, print_string_maybe_null(s1),
259                           st2, prec, print_string_maybe_null(s2));
260         return 0;
261     }
262     return 1;
263 }
264
265 /*
266  * We could use OPENSSL_buf2hexstr() to do this but trying to allocate memory
267  * in a failure state isn't generally a great idea.
268  */
269 static const char *print_mem_maybe_null(const void *s, size_t n,
270                                         char out[MEM_BUFFER_SIZE])
271 {
272     size_t i;
273     const unsigned char *p = (const unsigned char *)s;
274     int pad = 2*n >= MEM_BUFFER_SIZE;
275
276     if (s == NULL)
277         return "(NULL)";
278     if (pad)
279         n = MEM_BUFFER_SIZE-4;
280     
281     for (i=0; i<2*n; i++) {
282         unsigned char c = (i & 1) != 0 ? p[i / 2] & 15 : p[i / 2] >> 4;
283         out[i] = "0123456789abcdef"[c];
284     }
285     if (pad) {
286         out[i++] = '.';
287         out[i++] = '.';
288         out[i++] = '.';
289     }
290     out[i] = '\0';
291         
292     return out;
293 }
294
295 int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
296                 const void *s1, size_t n1, const void *s2, size_t n2)
297 {
298     char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
299
300     if (s1 == NULL && s2 == NULL)
301         return 1;
302     if (n1 != n2) {
303         test_fail_message(NULL, file, line, "memory",
304                           "size mismatch %s %s [%zu] != %s %s [%zu]",
305                           st1, print_mem_maybe_null(s1, n1, b1), n1,
306                           st2, print_mem_maybe_null(s2, n2, b2), n2);
307         return 0;
308     }
309     if (s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
310         test_fail_message(NULL, file, line, "memory",
311                           "%s %s [%zu] != %s %s [%zu]",
312                           st1, print_mem_maybe_null(s1, n1, b1), n1,
313                           st2, print_mem_maybe_null(s2, n2, b2), n2);
314         return 0;
315     }
316     return 1;
317 }
318
319 int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
320                 const void *s1, size_t n1, const void *s2, size_t n2)
321 {
322     char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
323
324     if ((s1 == NULL) ^ (s2 == NULL))
325       return 1;
326     if (n1 != n2)
327         return 1;
328     if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
329         test_fail_message(NULL, file, line, "memory",
330                           "%s %s [%zu] != %s %s [%zu]",
331                           st1, print_mem_maybe_null(s1, n1, b1), n1,
332                           st2, print_mem_maybe_null(s2, n2, b2), n2);
333         return 0;
334     }
335     return 1;
336 }