]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/tests.c
2 * Copyright 2017-2023 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 <openssl/asn1.h>
20 * Output a failed test first line.
21 * All items are optional are generally not printed if passed as NULL.
22 * The special cases are for prefix where "ERROR" is assumed and for left
23 * and right where a non-failure message is produced if either is NULL.
25 void test_fail_message_prefix(const char *prefix
, const char *file
,
26 int line
, const char *type
,
27 const char *left
, const char *right
,
30 test_printf_stderr("%s: ", prefix
!= NULL
? prefix
: "ERROR");
32 test_printf_stderr("(%s) ", type
);
34 if (left
!= NULL
&& right
!= NULL
)
35 test_printf_stderr("'%s %s %s' failed", left
, op
, right
);
37 test_printf_stderr("'%s'", op
);
40 test_printf_stderr(" @ %s:%d", file
, line
);
42 test_printf_stderr("\n");
46 * A common routine to output test failure messages. Generally this should not
47 * be called directly, rather it should be called by the following functions.
49 * |desc| is a printf formatted description with arguments |args| that is
50 * supplied by the user and |desc| can be NULL. |type| is the data type
51 * that was tested (int, char, ptr, ...). |fmt| is a system provided
52 * printf format with following arguments that spell out the failure
53 * details i.e. the actual values compared and the operator used.
55 * The typical use for this is from an utility test function:
57 * int test6(const char *file, int line, int n) {
59 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
65 * calling test6(3, "oops") will return 0 and produce out along the lines of:
66 * FAIL oops: (int) value 3 is not 6\n
68 static void test_fail_message(const char *prefix
, const char *file
, int line
,
69 const char *type
, const char *left
,
70 const char *right
, const char *op
,
74 static void test_fail_message_va(const char *prefix
, const char *file
,
75 int line
, const char *type
,
76 const char *left
, const char *right
,
77 const char *op
, const char *fmt
, va_list ap
)
79 test_fail_message_prefix(prefix
, file
, line
, type
, left
, right
, op
);
81 test_vprintf_stderr(fmt
, ap
);
82 test_printf_stderr("\n");
87 static void test_fail_message(const char *prefix
, const char *file
,
88 int line
, const char *type
,
89 const char *left
, const char *right
,
90 const char *op
, const char *fmt
, ...)
95 test_fail_message_va(prefix
, file
, line
, type
, left
, right
, op
, fmt
, ap
);
99 void test_info_c90(const char *desc
, ...)
104 test_fail_message_va("INFO", NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
108 void test_info(const char *file
, int line
, const char *desc
, ...)
113 test_fail_message_va("INFO", file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
117 void test_error_c90(const char *desc
, ...)
122 test_fail_message_va(NULL
, NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
124 test_printf_stderr("\n");
127 void test_error(const char *file
, int line
, const char *desc
, ...)
132 test_fail_message_va(NULL
, file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
134 test_printf_stderr("\n");
137 void test_perror(const char *s
)
140 * Using openssl_strerror_r causes linking issues since it isn't
141 * exported from libcrypto.so
143 TEST_error("%s: %s", s
, strerror(errno
));
146 void test_note(const char *fmt
, ...)
152 test_vprintf_stderr(fmt
, ap
);
154 test_printf_stderr("\n");
160 int test_skip(const char *file
, int line
, const char *desc
, ...)
165 test_fail_message_va("SKIP", file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
167 return TEST_SKIP_CODE
;
170 int test_skip_c90(const char *desc
, ...)
175 test_fail_message_va("SKIP", NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
177 test_printf_stderr("\n");
178 return TEST_SKIP_CODE
;
182 void test_openssl_errors(void)
184 ERR_print_errors_cb(openssl_error_cb
, NULL
);
189 * Define some comparisons between pairs of various types.
190 * These functions return 1 if the test is true.
191 * Otherwise, they return 0 and pretty-print diagnostics.
193 * In each case the functions produced are:
194 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
195 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
196 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
197 * int test_name_le(const type t1, const type t2, const char *desc, ...);
198 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
199 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
201 * The t1 and t2 arguments are to be compared for equality, inequality,
202 * less than, less than or equal to, greater than and greater than or
203 * equal to respectively. If the specified condition holds, the functions
204 * return 1. If the condition does not hold, the functions print a diagnostic
205 * message and return 0.
207 * The desc argument is a printf format string followed by its arguments and
208 * this is included in the output if the condition being tested for is false.
210 #define DEFINE_COMPARISON(type, name, opname, op, fmt, cast) \
211 int test_ ## name ## _ ## opname(const char *file, int line, \
212 const char *s1, const char *s2, \
213 const type t1, const type t2) \
217 test_fail_message(NULL, file, line, #type, s1, s2, #op, \
218 "[" fmt "] compared to [" fmt "]", \
219 (cast)t1, (cast)t2); \
223 #define DEFINE_COMPARISONS(type, name, fmt, cast) \
224 DEFINE_COMPARISON(type, name, eq, ==, fmt, cast) \
225 DEFINE_COMPARISON(type, name, ne, !=, fmt, cast) \
226 DEFINE_COMPARISON(type, name, lt, <, fmt, cast) \
227 DEFINE_COMPARISON(type, name, le, <=, fmt, cast) \
228 DEFINE_COMPARISON(type, name, gt, >, fmt, cast) \
229 DEFINE_COMPARISON(type, name, ge, >=, fmt, cast)
231 DEFINE_COMPARISONS(int, int, "%d", int)
232 DEFINE_COMPARISONS(unsigned int, uint
, "%u", unsigned int)
233 DEFINE_COMPARISONS(char, char, "%c", char)
234 DEFINE_COMPARISONS(unsigned char, uchar
, "%u", unsigned char)
235 DEFINE_COMPARISONS(long, long, "%ld", long)
236 DEFINE_COMPARISONS(unsigned long, ulong
, "%lu", unsigned long)
237 DEFINE_COMPARISONS(int64_t, int64_t, "%lld", long long)
238 DEFINE_COMPARISONS(uint64_t, uint64_t, "%llu", unsigned long long)
239 DEFINE_COMPARISONS(size_t, size_t, "%zu", size_t)
240 DEFINE_COMPARISONS(double, double, "%g", double)
242 DEFINE_COMPARISON(void *, ptr
, eq
, ==, "%p", void *)
243 DEFINE_COMPARISON(void *, ptr
, ne
, !=, "%p", void *)
245 int test_ptr_null(const char *file
, int line
, const char *s
, const void *p
)
249 test_fail_message(NULL
, file
, line
, "ptr", s
, "NULL", "==", "%p", p
);
253 int test_ptr(const char *file
, int line
, const char *s
, const void *p
)
257 test_fail_message(NULL
, file
, line
, "ptr", s
, "NULL", "!=", "%p", p
);
261 int test_true(const char *file
, int line
, const char *s
, int b
)
265 test_fail_message(NULL
, file
, line
, "bool", s
, "true", "==", "false");
269 int test_false(const char *file
, int line
, const char *s
, int b
)
273 test_fail_message(NULL
, file
, line
, "bool", s
, "false", "==", "true");
277 int test_str_eq(const char *file
, int line
, const char *st1
, const char *st2
,
278 const char *s1
, const char *s2
)
280 if (s1
== NULL
&& s2
== NULL
)
282 if (s1
== NULL
|| s2
== NULL
|| strcmp(s1
, s2
) != 0) {
283 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "==",
284 s1
, s1
== NULL
? 0 : strlen(s1
),
285 s2
, s2
== NULL
? 0 : strlen(s2
));
291 int test_str_ne(const char *file
, int line
, const char *st1
, const char *st2
,
292 const char *s1
, const char *s2
)
294 if ((s1
== NULL
) ^ (s2
== NULL
))
296 if (s1
== NULL
|| strcmp(s1
, s2
) == 0) {
297 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "!=",
298 s1
, s1
== NULL
? 0 : strlen(s1
),
299 s2
, s2
== NULL
? 0 : strlen(s2
));
305 int test_strn_eq(const char *file
, int line
, const char *st1
, const char *st2
,
306 const char *s1
, size_t n1
, const char *s2
, size_t n2
)
308 if (s1
== NULL
&& s2
== NULL
)
310 if (n1
!= n2
|| s1
== NULL
|| s2
== NULL
|| strncmp(s1
, s2
, n1
) != 0) {
311 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "==",
312 s1
, s1
== NULL
? 0 : OPENSSL_strnlen(s1
, n1
),
313 s2
, s2
== NULL
? 0 : OPENSSL_strnlen(s2
, n2
));
319 int test_strn_ne(const char *file
, int line
, const char *st1
, const char *st2
,
320 const char *s1
, size_t n1
, const char *s2
, size_t n2
)
322 if ((s1
== NULL
) ^ (s2
== NULL
))
324 if (n1
!= n2
|| s1
== NULL
|| strncmp(s1
, s2
, n1
) == 0) {
325 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "!=",
326 s1
, s1
== NULL
? 0 : OPENSSL_strnlen(s1
, n1
),
327 s2
, s2
== NULL
? 0 : OPENSSL_strnlen(s2
, n2
));
333 int test_mem_eq(const char *file
, int line
, const char *st1
, const char *st2
,
334 const void *s1
, size_t n1
, const void *s2
, size_t n2
)
336 if (s1
== NULL
&& s2
== NULL
)
338 if (n1
!= n2
|| s1
== NULL
|| s2
== NULL
|| memcmp(s1
, s2
, n1
) != 0) {
339 test_fail_memory_message(NULL
, file
, line
, "memory", st1
, st2
, "==",
346 int test_mem_ne(const char *file
, int line
, const char *st1
, const char *st2
,
347 const void *s1
, size_t n1
, const void *s2
, size_t n2
)
349 if ((s1
== NULL
) ^ (s2
== NULL
))
353 if (s1
== NULL
|| memcmp(s1
, s2
, n1
) == 0) {
354 test_fail_memory_message(NULL
, file
, line
, "memory", st1
, st2
, "!=",
361 #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
362 int test_BN_ ## opname(const char *file, int line, \
363 const char *s1, const char *s2, \
364 const BIGNUM *t1, const BIGNUM *t2) \
366 if (BN_cmp(t1, t2) op 0) \
368 test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
372 int test_BN_ ## opname ## _zero(const char *file, int line, \
373 const char *s, const BIGNUM *a) \
375 if (a != NULL &&(zero_cond)) \
377 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
382 DEFINE_BN_COMPARISONS(eq
, ==, BN_is_zero(a
))
383 DEFINE_BN_COMPARISONS(ne
, !=, !BN_is_zero(a
))
384 DEFINE_BN_COMPARISONS(gt
, >, !BN_is_negative(a
) && !BN_is_zero(a
))
385 DEFINE_BN_COMPARISONS(ge
, >=, !BN_is_negative(a
) || BN_is_zero(a
))
386 DEFINE_BN_COMPARISONS(lt
, <, BN_is_negative(a
) && !BN_is_zero(a
))
387 DEFINE_BN_COMPARISONS(le
, <=, BN_is_negative(a
) || BN_is_zero(a
))
389 int test_BN_eq_one(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
391 if (a
!= NULL
&& BN_is_one(a
))
393 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", s
, "1", "==", a
);
397 int test_BN_odd(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
399 if (a
!= NULL
&& BN_is_odd(a
))
401 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", "ODD(", ")", s
, a
);
405 int test_BN_even(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
407 if (a
!= NULL
&& !BN_is_odd(a
))
409 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", "EVEN(", ")", s
,
414 int test_BN_eq_word(const char *file
, int line
, const char *bns
, const char *ws
,
415 const BIGNUM
*a
, BN_ULONG w
)
419 if (a
!= NULL
&& BN_is_word(a
, w
))
421 if ((bw
= BN_new()) != NULL
)
423 test_fail_bignum_message(NULL
, file
, line
, "BIGNUM", bns
, ws
, "==", a
, bw
);
428 int test_BN_abs_eq_word(const char *file
, int line
, const char *bns
,
429 const char *ws
, const BIGNUM
*a
, BN_ULONG w
)
433 if (a
!= NULL
&& BN_abs_is_word(a
, w
))
435 if ((aa
= BN_dup(a
)) != NULL
)
436 BN_set_negative(aa
, 0);
437 if ((bw
= BN_new()) != NULL
)
439 test_fail_bignum_message(NULL
, file
, line
, "BIGNUM", bns
, ws
, "abs==",
446 static const char *print_time(const ASN1_TIME
*t
)
448 return t
== NULL
? "<null>" : (const char *)ASN1_STRING_get0_data(t
);
451 #define DEFINE_TIME_T_COMPARISON(opname, op) \
452 int test_time_t_ ## opname(const char *file, int line, \
453 const char *s1, const char *s2, \
454 const time_t t1, const time_t t2) \
456 ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \
457 ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \
458 int r = at1 != NULL && at2 != NULL \
459 && ASN1_TIME_compare(at1, at2) op 0; \
461 test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \
462 "[%s] compared to [%s]", \
463 print_time(at1), print_time(at2)); \
464 ASN1_STRING_free(at1); \
465 ASN1_STRING_free(at2); \
468 DEFINE_TIME_T_COMPARISON(eq
, ==)
469 DEFINE_TIME_T_COMPARISON(ne
, !=)
470 DEFINE_TIME_T_COMPARISON(gt
, >)
471 DEFINE_TIME_T_COMPARISON(ge
, >=)
472 DEFINE_TIME_T_COMPARISON(lt
, <)
473 DEFINE_TIME_T_COMPARISON(le
, <=)