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