]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/tests.c
2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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"
17 #include "internal/nelem.h"
18 #include <openssl/asn1.h>
21 * Output a failed test first line.
22 * All items are optional are generally not preinted if passed as NULL.
23 * The special cases are for prefix where "ERROR" is assumed and for left
24 * and right where a non-failure message is produced if either is NULL.
26 void test_fail_message_prefix(const char *prefix
, const char *file
,
27 int line
, const char *type
,
28 const char *left
, const char *right
,
31 test_printf_stderr("%s: ", prefix
!= NULL
? prefix
: "ERROR");
33 test_printf_stderr("(%s) ", type
);
35 if (left
!= NULL
&& right
!= NULL
)
36 test_printf_stderr("'%s %s %s' failed", left
, op
, right
);
38 test_printf_stderr("'%s'", op
);
41 test_printf_stderr(" @ %s:%d", file
, line
);
43 test_printf_stderr("\n");
47 * A common routine to output test failure messages. Generally this should not
48 * be called directly, rather it should be called by the following functions.
50 * |desc| is a printf formatted description with arguments |args| that is
51 * supplied by the user and |desc| can be NULL. |type| is the data type
52 * that was tested (int, char, ptr, ...). |fmt| is a system provided
53 * printf format with following arguments that spell out the failure
54 * details i.e. the actual values compared and the operator used.
56 * The typical use for this is from an utility test function:
58 * int test6(const char *file, int line, int n) {
60 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
66 * calling test6(3, "oops") will return 0 and produce out along the lines of:
67 * FAIL oops: (int) value 3 is not 6\n
69 static void test_fail_message(const char *prefix
, const char *file
, int line
,
70 const char *type
, const char *left
,
71 const char *right
, const char *op
,
75 static void test_fail_message_va(const char *prefix
, const char *file
,
76 int line
, const char *type
,
77 const char *left
, const char *right
,
78 const char *op
, const char *fmt
, va_list ap
)
80 test_fail_message_prefix(prefix
, file
, line
, type
, left
, right
, op
);
82 test_vprintf_stderr(fmt
, ap
);
83 test_printf_stderr("\n");
88 static void test_fail_message(const char *prefix
, const char *file
,
89 int line
, const char *type
,
90 const char *left
, const char *right
,
91 const char *op
, const char *fmt
, ...)
96 test_fail_message_va(prefix
, file
, line
, type
, left
, right
, op
, fmt
, ap
);
100 void test_info_c90(const char *desc
, ...)
105 test_fail_message_va("INFO", NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
109 void test_info(const char *file
, int line
, const char *desc
, ...)
114 test_fail_message_va("INFO", file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
118 void test_error_c90(const char *desc
, ...)
123 test_fail_message_va(NULL
, NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
125 test_printf_stderr("\n");
128 void test_error(const char *file
, int line
, const char *desc
, ...)
133 test_fail_message_va(NULL
, file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
135 test_printf_stderr("\n");
138 void test_perror(const char *s
)
141 * Using openssl_strerror_r causes linking issues since it isn't
142 * exported from libcrypto.so
144 TEST_error("%s: %s", s
, strerror(errno
));
147 void test_note(const char *fmt
, ...)
153 test_vprintf_stderr(fmt
, ap
);
155 test_printf_stderr("\n");
161 int test_skip(const char *file
, int line
, const char *desc
, ...)
166 test_fail_message_va("SKIP", file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
168 return TEST_SKIP_CODE
;
171 int test_skip_c90(const char *desc
, ...)
176 test_fail_message_va("SKIP", NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
178 test_printf_stderr("\n");
179 return TEST_SKIP_CODE
;
183 void test_openssl_errors(void)
185 ERR_print_errors_cb(openssl_error_cb
, NULL
);
190 * Define some comparisons between pairs of various types.
191 * These functions return 1 if the test is true.
192 * Otherwise, they return 0 and pretty-print diagnostics.
194 * In each case the functions produced are:
195 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
196 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
197 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
198 * int test_name_le(const type t1, const type t2, const char *desc, ...);
199 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
200 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
202 * The t1 and t2 arguments are to be compared for equality, inequality,
203 * less than, less than or equal to, greater than and greater than or
204 * equal to respectively. If the specified condition holds, the functions
205 * return 1. If the condition does not hold, the functions print a diagnostic
206 * message and return 0.
208 * The desc argument is a printf format string followed by its arguments and
209 * this is included in the output if the condition being tested for is false.
211 #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
212 int test_ ## name ## _ ## opname(const char *file, int line, \
213 const char *s1, const char *s2, \
214 const type t1, const type t2) \
218 test_fail_message(NULL, file, line, #type, s1, s2, #op, \
219 "[" fmt "] compared to [" fmt "]", \
224 #define DEFINE_COMPARISONS(type, name, fmt) \
225 DEFINE_COMPARISON(type, name, eq, ==, fmt) \
226 DEFINE_COMPARISON(type, name, ne, !=, fmt) \
227 DEFINE_COMPARISON(type, name, lt, <, fmt) \
228 DEFINE_COMPARISON(type, name, le, <=, fmt) \
229 DEFINE_COMPARISON(type, name, gt, >, fmt) \
230 DEFINE_COMPARISON(type, name, ge, >=, fmt)
232 DEFINE_COMPARISONS(int, int, "%d")
233 DEFINE_COMPARISONS(unsigned int, uint
, "%u")
234 DEFINE_COMPARISONS(char, char, "%c")
235 DEFINE_COMPARISONS(unsigned char, uchar
, "%u")
236 DEFINE_COMPARISONS(long, long, "%ld")
237 DEFINE_COMPARISONS(unsigned long, ulong
, "%lu")
238 DEFINE_COMPARISONS(size_t, size_t, "%zu")
239 DEFINE_COMPARISONS(double, double, "%g")
241 DEFINE_COMPARISON(void *, ptr
, eq
, ==, "%p")
242 DEFINE_COMPARISON(void *, ptr
, ne
, !=, "%p")
244 int test_ptr_null(const char *file
, int line
, const char *s
, const void *p
)
248 test_fail_message(NULL
, file
, line
, "ptr", s
, "NULL", "==", "%p", p
);
252 int test_ptr(const char *file
, int line
, const char *s
, const void *p
)
256 test_fail_message(NULL
, file
, line
, "ptr", s
, "NULL", "!=", "%p", p
);
260 int test_true(const char *file
, int line
, const char *s
, int b
)
264 test_fail_message(NULL
, file
, line
, "bool", s
, "true", "==", "false");
268 int test_false(const char *file
, int line
, const char *s
, int b
)
272 test_fail_message(NULL
, file
, line
, "bool", s
, "false", "==", "true");
276 int test_str_eq(const char *file
, int line
, const char *st1
, const char *st2
,
277 const char *s1
, const char *s2
)
279 if (s1
== NULL
&& s2
== NULL
)
281 if (s1
== NULL
|| s2
== NULL
|| strcmp(s1
, s2
) != 0) {
282 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "==",
283 s1
, s1
== NULL
? 0 : strlen(s1
),
284 s2
, s2
== NULL
? 0 : strlen(s2
));
290 int test_str_ne(const char *file
, int line
, const char *st1
, const char *st2
,
291 const char *s1
, const char *s2
)
293 if ((s1
== NULL
) ^ (s2
== NULL
))
295 if (s1
== NULL
|| strcmp(s1
, s2
) == 0) {
296 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "!=",
297 s1
, s1
== NULL
? 0 : strlen(s1
),
298 s2
, s2
== NULL
? 0 : strlen(s2
));
304 int test_strn_eq(const char *file
, int line
, const char *st1
, const char *st2
,
305 const char *s1
, const char *s2
, size_t len
)
307 if (s1
== NULL
&& s2
== NULL
)
309 if (s1
== NULL
|| s2
== NULL
|| strncmp(s1
, s2
, len
) != 0) {
310 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "==",
311 s1
, s1
== NULL
? 0 : OPENSSL_strnlen(s1
, len
),
312 s2
, s2
== NULL
? 0 : OPENSSL_strnlen(s2
, len
));
318 int test_strn_ne(const char *file
, int line
, const char *st1
, const char *st2
,
319 const char *s1
, const char *s2
, size_t len
)
321 if ((s1
== NULL
) ^ (s2
== NULL
))
323 if (s1
== NULL
|| strncmp(s1
, s2
, len
) == 0) {
324 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "!=",
325 s1
, s1
== NULL
? 0 : OPENSSL_strnlen(s1
, len
),
326 s2
, s2
== NULL
? 0 : OPENSSL_strnlen(s2
, len
));
332 int test_mem_eq(const char *file
, int line
, const char *st1
, const char *st2
,
333 const void *s1
, size_t n1
, const void *s2
, size_t n2
)
335 if (s1
== NULL
&& s2
== NULL
)
337 if (n1
!= n2
|| s1
== NULL
|| s2
== NULL
|| memcmp(s1
, s2
, n1
) != 0) {
338 test_fail_memory_message(NULL
, file
, line
, "memory", st1
, st2
, "==",
345 int test_mem_ne(const char *file
, int line
, const char *st1
, const char *st2
,
346 const void *s1
, size_t n1
, const void *s2
, size_t n2
)
348 if ((s1
== NULL
) ^ (s2
== NULL
))
352 if (s1
== NULL
|| memcmp(s1
, s2
, n1
) == 0) {
353 test_fail_memory_message(NULL
, file
, line
, "memory", st1
, st2
, "!=",
360 #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
361 int test_BN_ ## opname(const char *file, int line, \
362 const char *s1, const char *s2, \
363 const BIGNUM *t1, const BIGNUM *t2) \
365 if (BN_cmp(t1, t2) op 0) \
367 test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
371 int test_BN_ ## opname ## _zero(const char *file, int line, \
372 const char *s, const BIGNUM *a) \
374 if (a != NULL &&(zero_cond)) \
376 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
381 DEFINE_BN_COMPARISONS(eq
, ==, BN_is_zero(a
))
382 DEFINE_BN_COMPARISONS(ne
, !=, !BN_is_zero(a
))
383 DEFINE_BN_COMPARISONS(gt
, >, !BN_is_negative(a
) && !BN_is_zero(a
))
384 DEFINE_BN_COMPARISONS(ge
, >=, !BN_is_negative(a
) || BN_is_zero(a
))
385 DEFINE_BN_COMPARISONS(lt
, <, BN_is_negative(a
) && !BN_is_zero(a
))
386 DEFINE_BN_COMPARISONS(le
, <=, BN_is_negative(a
) || BN_is_zero(a
))
388 int test_BN_eq_one(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
390 if (a
!= NULL
&& BN_is_one(a
))
392 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", s
, "1", "==", a
);
396 int test_BN_odd(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
398 if (a
!= NULL
&& BN_is_odd(a
))
400 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", "ODD(", ")", s
, a
);
404 int test_BN_even(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
406 if (a
!= NULL
&& !BN_is_odd(a
))
408 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", "EVEN(", ")", s
,
413 int test_BN_eq_word(const char *file
, int line
, const char *bns
, const char *ws
,
414 const BIGNUM
*a
, BN_ULONG w
)
418 if (a
!= NULL
&& BN_is_word(a
, w
))
422 test_fail_bignum_message(NULL
, file
, line
, "BIGNUM", bns
, ws
, "==", a
, bw
);
427 int test_BN_abs_eq_word(const char *file
, int line
, const char *bns
,
428 const char *ws
, const BIGNUM
*a
, BN_ULONG w
)
432 if (a
!= NULL
&& BN_abs_is_word(a
, w
))
436 BN_set_negative(aa
, 0);
438 test_fail_bignum_message(NULL
, file
, line
, "BIGNUM", bns
, ws
, "abs==",
445 static const char *print_time(const ASN1_TIME
*t
)
447 return t
== NULL
? "<null>" : (const char *)ASN1_STRING_get0_data(t
);
450 #define DEFINE_TIME_T_COMPARISON(opname, op) \
451 int test_time_t_ ## opname(const char *file, int line, \
452 const char *s1, const char *s2, \
453 const time_t t1, const time_t t2) \
455 ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \
456 ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \
457 int r = at1 != NULL && at2 != NULL \
458 && ASN1_TIME_compare(at1, at2) op 0; \
460 test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \
461 "[%s] compared to [%s]", \
462 print_time(at1), print_time(at2)); \
463 ASN1_STRING_free(at1); \
464 ASN1_STRING_free(at2); \
467 DEFINE_TIME_T_COMPARISON(eq
, ==)
468 DEFINE_TIME_T_COMPARISON(ne
, !=)
469 DEFINE_TIME_T_COMPARISON(gt
, >)
470 DEFINE_TIME_T_COMPARISON(ge
, >=)
471 DEFINE_TIME_T_COMPARISON(lt
, <)
472 DEFINE_TIME_T_COMPARISON(le
, <=)