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