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