]>
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 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"
16 #include "../../e_os.h"
18 /* The size of memory buffers to display on failure */
19 #define MEM_BUFFER_SIZE (33)
20 #define MAX_STRING_WIDTH (80)
22 /* Special representation of -0 */
23 static char BN_minus_zero
[] = "-0";
25 /* Output a failed test first line */
26 static 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# %s: ", subtest_level(), "",
32 prefix
!= NULL
? prefix
: "ERROR");
34 test_printf_stderr("(%s) ", type
);
36 test_printf_stderr("%s %s %s", left
, op
, right
);
38 test_printf_stderr(" @ %s:%d", file
, line
);
40 test_printf_stderr("\n");
44 * A common routine to output test failure messages. Generally this should not
45 * be called directly, rather it should be called by the following functions.
47 * |desc| is a printf formatted description with arguments |args| that is
48 * supplied by the user and |desc| can be NULL. |type| is the data type
49 * that was tested (int, char, ptr, ...). |fmt| is a system provided
50 * printf format with following arguments that spell out the failure
51 * details i.e. the actual values compared and the operator used.
53 * The typical use for this is from an utility test function:
55 * int test6(const char *file, int line, int n) {
57 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
63 * calling test6(3, "oops") will return 0 and produce out along the lines of:
64 * FAIL oops: (int) value 3 is not 6\n
66 static void test_fail_message(const char *prefix
, const char *file
, int line
,
67 const char *type
, const char *left
,
68 const char *right
, const char *op
,
72 static void test_fail_message_va(const char *prefix
, const char *file
,
73 int line
, const char *type
,
74 const char *left
, const char *right
,
75 const char *op
, const char *fmt
, va_list ap
)
77 test_fail_message_prefix(prefix
, file
, line
, type
, left
, right
, op
);
79 test_printf_stderr("%*s# ", subtest_level(), "");
80 test_vprintf_stderr(fmt
, ap
);
81 test_printf_stderr("\n");
83 test_printf_stderr("\n");
87 static void test_fail_string_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 *m1
, size_t l1
,
91 const char *m2
, size_t l2
)
93 const int indent
= subtest_level();
94 const size_t width
= (MAX_STRING_WIDTH
- indent
- 12) / 16 * 16;
95 char b1
[MAX_STRING_WIDTH
+ 1], b2
[MAX_STRING_WIDTH
+ 1];
96 char bdiff
[MAX_STRING_WIDTH
+ 1];
98 unsigned int cnt
= 0, diff
;
100 test_fail_message_prefix(prefix
, file
, line
, type
, left
, right
, op
);
105 if (l1
== 0 && l2
== 0) {
106 if ((m1
== NULL
) == (m2
== NULL
)) {
107 test_printf_stderr("%*s# % 4s %s\n", indent
, "", "",
108 m1
== NULL
? "NULL" : "''");
110 test_printf_stderr("%*s# % 4s - %s\n", indent
, "", "",
111 m1
== NULL
? "NULL" : "''");
112 test_printf_stderr("%*s# % 4s + %s\n", indent
, "", "",
113 m2
== NULL
? "NULL" : "''");
118 while (l1
> 0 || l2
> 0) {
121 b1
[n1
= l1
> width
? width
: l1
] = 0;
122 for (i
= 0; i
< n1
; i
++)
123 b1
[i
] = isprint(m1
[i
]) ? m1
[i
] : '.';
126 b2
[n2
= l2
> width
? width
: l2
] = 0;
127 for (i
= 0; i
< n2
; i
++)
128 b2
[i
] = isprint(m2
[i
]) ? m2
[i
] : '.';
132 if (n1
> 0 && n2
> 0) {
133 const size_t j
= n1
< n2
? n1
: n2
;
134 const size_t k
= n1
> n2
? n1
: n2
;
137 if (m1
[i
] == m2
[i
]) {
148 test_printf_stderr("%*s# % 4u: '%s'\n", indent
, "", cnt
, b1
);
150 if (cnt
== 0 && m1
== NULL
)
151 test_printf_stderr("%*s# % 4s - NULL\n", indent
, "", "");
152 else if (cnt
== 0 && *m1
== '\0')
153 test_printf_stderr("%*s# % 4s - ''\n", indent
, "", "");
155 test_printf_stderr("%*s# % 4u:- '%s'\n", indent
, "", cnt
, b1
);
156 if (cnt
== 0 && m2
== NULL
)
157 test_printf_stderr("%*s# % 4s + NULL\n", indent
, "", "");
158 else if (cnt
== 0 && *m2
== '\0')
159 test_printf_stderr("%*s# % 4s + ''\n", indent
, "", "");
161 test_printf_stderr("%*s# % 4u:+ '%s'\n", indent
, "", cnt
, b2
);
163 test_printf_stderr("%*s# % 4s %s\n", indent
, "", "", bdiff
);
172 test_printf_stderr("\n");
176 static char *convertBN(const BIGNUM
*b
)
180 if (BN_is_zero(b
) && BN_is_negative(b
))
181 return BN_minus_zero
;
185 static void test_fail_bignum_message(const char *prefix
, const char *file
,
186 int line
, const char *type
,
187 const char *left
, const char *right
,
189 const BIGNUM
*bn1
, const BIGNUM
*bn2
)
191 char *s1
= convertBN(bn1
), *s2
= convertBN(bn2
);
192 size_t l1
= s1
!= NULL
? strlen(s1
) : 0;
193 size_t l2
= s2
!= NULL
? strlen(s2
) : 0;
195 test_fail_string_message(prefix
, file
, line
, type
, left
, right
, op
,
197 if (s1
!= BN_minus_zero
)
199 if (s2
!= BN_minus_zero
)
203 static void test_fail_bignum_mono_message(const char *prefix
, const char *file
,
204 int line
, const char *type
,
205 const char *left
, const char *right
,
206 const char *op
, const BIGNUM
*bn
)
208 char *s
= convertBN(bn
);
209 size_t l
= s
!= NULL
? strlen(s
) : 0;
211 test_fail_string_message(prefix
, file
, line
, type
, left
, right
, op
,
213 if (s
!= BN_minus_zero
)
217 static void hex_convert_memory(const char *m
, size_t n
, char *b
)
221 for (i
= 0; i
< n
; i
++) {
222 const unsigned char c
= *m
++;
224 *b
++ = "0123456789abcdef"[c
>> 4];
225 *b
++ = "0123456789abcdef"[c
& 15];
226 if ((i
% 8) == 7 && i
!= n
- 1)
232 static void test_fail_memory_message(const char *prefix
, const char *file
,
233 int line
, const char *type
,
234 const char *left
, const char *right
,
235 const char *op
, const char *m1
, size_t l1
,
236 const char *m2
, size_t l2
)
238 const int indent
= subtest_level();
239 const size_t bytes
= (MAX_STRING_WIDTH
- 9) / 17 * 8;
240 char b1
[MAX_STRING_WIDTH
+ 1], b2
[MAX_STRING_WIDTH
+ 1];
241 char *p
, bdiff
[MAX_STRING_WIDTH
+ 1];
243 unsigned int cnt
= 0, diff
;
245 test_fail_message_prefix(prefix
, file
, line
, type
, left
, right
, op
);
250 if (l1
== 0 && l2
== 0) {
251 if ((m1
== NULL
) == (m2
== NULL
)) {
252 test_printf_stderr("%*s# %04s %s\n", indent
, "", "",
253 m1
== NULL
? "NULL" : "empty");
255 test_printf_stderr("%*s# %04s -%s\n", indent
, "", "",
256 m1
== NULL
? "NULL" : "empty");
257 test_printf_stderr("%*s# %04s +%s\n", indent
, "", "",
258 m2
== NULL
? "NULL" : "empty");
263 while (l1
> 0 || l2
> 0) {
266 n1
= l1
> bytes
? bytes
: l1
;
267 hex_convert_memory(m1
, n1
, b1
);
270 n2
= l2
> bytes
? bytes
: l2
;
271 hex_convert_memory(m2
, n2
, b2
);
277 if (n1
> 0 && n2
> 0) {
278 const size_t j
= n1
< n2
? n1
: n2
;
279 const size_t k
= n1
> n2
? n1
: n2
;
282 if (m1
[i
] == m2
[i
]) {
290 if ((i
% 8) == 7 && (i
!= j
- 1 || j
!= k
))
297 if ((i
% 8) == 7 && i
!= k
- 1)
304 test_printf_stderr("%*s# %04x: %s\n", indent
, "", cnt
, b1
);
306 if (cnt
== 0 && m1
== NULL
)
307 test_printf_stderr("%*s# %04s -NULL\n", indent
, "", "");
308 else if (cnt
== 0 && l1
== 0)
309 test_printf_stderr("%*s# %04s -empty\n", indent
, "", "");
311 test_printf_stderr("%*s# %04x:-%s\n", indent
, "", cnt
, b1
);
312 if (cnt
== 0 && m2
== NULL
)
313 test_printf_stderr("%*s# %04s +NULL\n", indent
, "", "");
314 else if (cnt
== 0 && l2
== 0)
315 test_printf_stderr("%*s# %04s +empty\n", indent
, "", "");
317 test_printf_stderr("%*s# %04x:+%s\n", indent
, "", cnt
, b2
);
319 test_printf_stderr("%*s# % 4s %s\n", indent
, "", "", bdiff
);
328 test_printf_stderr("\n");
332 static void test_fail_message(const char *prefix
, const char *file
,
333 int line
, const char *type
,
334 const char *left
, const char *right
,
335 const char *op
, const char *fmt
, ...)
340 test_fail_message_va(prefix
, file
, line
, type
, left
, right
, op
, fmt
, ap
);
344 void test_info_c90(const char *desc
, ...)
349 test_fail_message_va("INFO", NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
353 void test_info(const char *file
, int line
, const char *desc
, ...)
358 test_fail_message_va("INFO", file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
362 void test_error_c90(const char *desc
, ...)
367 test_fail_message(NULL
, NULL
, -1, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
371 void test_error(const char *file
, int line
, const char *desc
, ...)
376 test_fail_message_va(NULL
, file
, line
, NULL
, NULL
, NULL
, NULL
, desc
, ap
);
380 void test_openssl_errors(void)
382 ERR_print_errors_cb(openssl_error_cb
, NULL
);
386 * Define some comparisons between pairs of various types.
387 * These functions return 1 if the test is true.
388 * Otherwise, they return 0 and pretty-print diagnostics.
390 * In each case the functions produced are:
391 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
392 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
393 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
394 * int test_name_le(const type t1, const type t2, const char *desc, ...);
395 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
396 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
398 * The t1 and t2 arguments are to be compared for equality, inequality,
399 * less than, less than or equal to, greater than and greater than or
400 * equal to respectively. If the specified condition holds, the functions
401 * return 1. If the condition does not hold, the functions print a diagnostic
402 * message and return 0.
404 * The desc argument is a printf format string followed by its arguments and
405 * this is included in the output if the condition being tested for is false.
407 #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
408 int test_ ## name ## _ ## opname(const char *file, int line, \
409 const char *s1, const char *s2, \
410 const type t1, const type t2) \
414 test_fail_message(NULL, file, line, #type, s1, s2, #op, \
415 "[" fmt "] compared to [" fmt "]", \
420 #define DEFINE_COMPARISONS(type, name, fmt) \
421 DEFINE_COMPARISON(type, name, eq, ==, fmt) \
422 DEFINE_COMPARISON(type, name, ne, !=, fmt) \
423 DEFINE_COMPARISON(type, name, lt, <, fmt) \
424 DEFINE_COMPARISON(type, name, le, <=, fmt) \
425 DEFINE_COMPARISON(type, name, gt, >, fmt) \
426 DEFINE_COMPARISON(type, name, ge, >=, fmt)
428 DEFINE_COMPARISONS(int, int, "%d")
429 DEFINE_COMPARISONS(unsigned int, uint
, "%u")
430 DEFINE_COMPARISONS(char, char, "%c")
431 DEFINE_COMPARISONS(unsigned char, uchar
, "%u")
432 DEFINE_COMPARISONS(long, long, "%ld")
433 DEFINE_COMPARISONS(unsigned long, ulong
, "%lu")
434 DEFINE_COMPARISONS(size_t, size_t, "%zu")
436 DEFINE_COMPARISON(void *, ptr
, eq
, ==, "%p")
437 DEFINE_COMPARISON(void *, ptr
, ne
, !=, "%p")
439 int test_ptr_null(const char *file
, int line
, const char *s
, const void *p
)
443 test_fail_message(NULL
, file
, line
, "ptr", s
, "NULL", "==", "%p", p
);
447 int test_ptr(const char *file
, int line
, const char *s
, const void *p
)
451 test_fail_message(NULL
, file
, line
, "ptr", s
, "NULL", "!=", "%p", p
);
455 int test_true(const char *file
, int line
, const char *s
, int b
)
459 test_fail_message(NULL
, file
, line
, "bool", s
, "true", "==", "false");
463 int test_false(const char *file
, int line
, const char *s
, int b
)
467 test_fail_message(NULL
, file
, line
, "bool", s
, "false", "==", "true");
471 int test_str_eq(const char *file
, int line
, const char *st1
, const char *st2
,
472 const char *s1
, const char *s2
)
474 if (s1
== NULL
&& s2
== NULL
)
476 if (s1
== NULL
|| s2
== NULL
|| strcmp(s1
, s2
) != 0) {
477 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "==",
478 s1
, s1
== NULL
? 0 : strlen(s1
),
479 s2
, s2
== NULL
? 0 : strlen(s2
));
485 int test_str_ne(const char *file
, int line
, const char *st1
, const char *st2
,
486 const char *s1
, const char *s2
)
488 if ((s1
== NULL
) ^ (s2
== NULL
))
490 if (s1
== NULL
|| strcmp(s1
, s2
) == 0) {
491 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "!=",
492 s1
, s1
== NULL
? 0 : strlen(s1
),
493 s2
, s2
== NULL
? 0 : strlen(s2
));
499 int test_strn_eq(const char *file
, int line
, const char *st1
, const char *st2
,
500 const char *s1
, const char *s2
, size_t len
)
502 if (s1
== NULL
&& s2
== NULL
)
504 if (s1
== NULL
|| s2
== NULL
|| strncmp(s1
, s2
, len
) != 0) {
505 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "==",
506 s1
, s1
== NULL
? 0 : OPENSSL_strnlen(s1
, len
),
507 s2
, s2
== NULL
? 0 : OPENSSL_strnlen(s2
, len
));
513 int test_strn_ne(const char *file
, int line
, const char *st1
, const char *st2
,
514 const char *s1
, const char *s2
, size_t len
)
516 if ((s1
== NULL
) ^ (s2
== NULL
))
518 if (s1
== NULL
|| strncmp(s1
, s2
, len
) == 0) {
519 test_fail_string_message(NULL
, file
, line
, "string", st1
, st2
, "!=",
520 s1
, s1
== NULL
? 0 : OPENSSL_strnlen(s1
, len
),
521 s2
, s2
== NULL
? 0 : OPENSSL_strnlen(s2
, len
));
527 int test_mem_eq(const char *file
, int line
, const char *st1
, const char *st2
,
528 const void *s1
, size_t n1
, const void *s2
, size_t n2
)
530 if (s1
== NULL
&& s2
== NULL
)
532 if (n1
!= n2
|| s1
== NULL
|| s2
== NULL
|| memcmp(s1
, s2
, n1
) != 0) {
533 test_fail_memory_message(NULL
, file
, line
, "memory", st1
, st2
, "==",
540 int test_mem_ne(const char *file
, int line
, const char *st1
, const char *st2
,
541 const void *s1
, size_t n1
, const void *s2
, size_t n2
)
543 if ((s1
== NULL
) ^ (s2
== NULL
))
547 if (s1
== NULL
|| memcmp(s1
, s2
, n1
) == 0) {
548 test_fail_memory_message(NULL
, file
, line
, "memory", st1
, st2
, "!=",
555 #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
556 int test_BN_ ## opname(const char *file, int line, \
557 const char *s1, const char *s2, \
558 const BIGNUM *t1, const BIGNUM *t2) \
560 if (BN_cmp(t1, t2) op 0) \
562 test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
566 int test_BN_ ## opname ## _zero(const char *file, int line, \
567 const char *s, const BIGNUM *a) \
569 if (a != NULL &&(zero_cond)) \
571 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
576 DEFINE_BN_COMPARISONS(eq
, ==, BN_is_zero(a
))
577 DEFINE_BN_COMPARISONS(ne
, !=, !BN_is_zero(a
))
578 DEFINE_BN_COMPARISONS(gt
, >, !BN_is_negative(a
) && !BN_is_zero(a
))
579 DEFINE_BN_COMPARISONS(ge
, >=, !BN_is_negative(a
) || BN_is_zero(a
))
580 DEFINE_BN_COMPARISONS(lt
, <, BN_is_negative(a
) && !BN_is_zero(a
))
581 DEFINE_BN_COMPARISONS(le
, <=, BN_is_negative(a
) || BN_is_zero(a
))
583 int test_BN_eq_one(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
585 if (a
!= NULL
&& BN_is_one(a
))
587 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", s
, "1", "==", a
);
591 int test_BN_odd(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
593 if (a
!= NULL
&& BN_is_odd(a
))
595 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", "ODD(", ")", s
,
600 int test_BN_even(const char *file
, int line
, const char *s
, const BIGNUM
*a
)
602 if (a
!= NULL
&& !BN_is_odd(a
))
604 test_fail_bignum_mono_message(NULL
, file
, line
, "BIGNUM", "EVEN(", ")", s
,
609 int test_BN_eq_word(const char *file
, int line
, const char *bns
, const char *ws
,
610 const BIGNUM
*a
, BN_ULONG w
)
614 if (a
!= NULL
&& BN_is_word(a
, w
))
618 test_fail_bignum_message(NULL
, file
, line
, "BIGNUM", bns
, ws
, "==", a
, bw
);
623 int test_BN_abs_eq_word(const char *file
, int line
, const char *bns
,
624 const char *ws
, const BIGNUM
*a
, BN_ULONG w
)
628 if (a
!= NULL
&& BN_abs_is_word(a
, w
))
632 BN_set_negative(aa
, 0);
634 test_fail_bignum_message(NULL
, file
, line
, "BIGNUM", bns
, ws
, "abs==",