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