2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "../testutil.h"
15 #include "../../e_os.h"
17 /* The size of memory buffers to display on failure */
18 #define MEM_BUFFER_SIZE (21)
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.
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.
30 * The typical use for this is from an utility test function:
32 * int test6(const char *file, int line, int n) {
34 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
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
43 * It general, test_fail_message should not be called directly.
45 static void test_fail_message(const char *prefix, const char *file, int line,
46 const char *type, const char *fmt, ...)
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)
52 test_printf_stderr("%*s# %s: ", subtest_level(), "",
53 prefix != NULL ? prefix : "ERROR");
55 test_printf_stderr("(%s)", type);
57 test_vprintf_stderr(fmt, ap);
60 test_printf_stderr(" @ %s:%d", file, line);
62 test_printf_stderr("\n");
66 static void test_fail_message(const char *prefix, const char *file, int line,
67 const char *type, const char *fmt, ...)
72 test_fail_message_va(prefix, file, line, type, fmt, ap);
76 void test_info_c90(const char *desc, ...)
81 test_fail_message_va("INFO", NULL, -1, NULL, desc, ap);
85 void test_info(const char *file, int line, const char *desc, ...)
90 test_fail_message_va("INFO", file, line, NULL, desc, ap);
94 void test_error_c90(const char *desc, ...)
99 test_fail_message(NULL, NULL, -1, NULL, desc, ap);
103 void test_error(const char *file, int line, const char *desc, ...)
108 test_fail_message_va(NULL, file, line, NULL, desc, ap);
112 void test_openssl_errors(void)
114 ERR_print_errors_cb(openssl_error_cb, NULL);
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.
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, ...);
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.
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.
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) \
146 test_fail_message(NULL, file, line, #type, \
147 "%s [" fmt "] " #op " %s [" fmt "]", \
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)
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")
168 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
169 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
171 int test_ptr_null(const char *file, int line, const char *s, const void *p)
175 test_fail_message(NULL, file, line, "ptr", "%s [%p] == NULL", s, p);
179 int test_ptr(const char *file, int line, const char *s, const void *p)
183 test_fail_message(NULL, file, line, "ptr", "%s [%p] != NULL", s, p);
187 int test_true(const char *file, int line, const char *s, int b)
191 test_fail_message(NULL, file, line, "bool", "%s [false] == true", s);
195 int test_false(const char *file, int line, const char *s, int b)
199 test_fail_message(NULL, file, line, "bool", "%s [true] == false", s);
203 static const char *print_string_maybe_null(const char *s)
205 return s == NULL ? "(NULL)" : s;
208 int test_str_eq(const char *file, int line, const char *st1, const char *st2,
209 const char *s1, const char *s2)
211 if (s1 == NULL && s2 == NULL)
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));
222 int test_str_ne(const char *file, int line, const char *st1, const char *st2,
223 const char *s1, const char *s2)
225 if ((s1 == NULL) ^ (s2 == NULL))
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));
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)
241 if (s1 == NULL && s2 == NULL)
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));
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)
257 if ((s1 == NULL) ^ (s2 == NULL))
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));
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.
272 static const char *print_mem_maybe_null(const void *s, size_t n,
273 char out[MEM_BUFFER_SIZE])
276 const unsigned char *p = (const unsigned char *)s;
277 int pad = 2*n >= MEM_BUFFER_SIZE;
282 n = MEM_BUFFER_SIZE-4;
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];
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)
301 char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
303 if (s1 == NULL && s2 == NULL)
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);
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);
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)
325 char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
327 if ((s1 == NULL) ^ (s2 == NULL))
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);