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 (2000)
20 #define MAX_STRING_WIDTH (80)
21 #define BN_OUTPUT_SIZE (8)
23 /* Output a failed test first line */
24 static void test_fail_message_prefix(const char *prefix, const char *file,
25 int line, const char *type,
26 const char *left, const char *right,
29 test_printf_stderr("%*s# %s: ", subtest_level(), "",
30 prefix != NULL ? prefix : "ERROR");
32 test_printf_stderr("(%s) ", type);
34 test_printf_stderr("'%s %s %s' failed", left, op, right);
36 test_printf_stderr(" @ %s:%d", file, line);
38 test_printf_stderr("\n");
41 /* Output a diff header */
42 static void test_diff_header(const char *left, const char *right)
44 test_printf_stderr("%*s# --- %s\n", subtest_level(), "", left);
45 test_printf_stderr("%*s# +++ %s\n", subtest_level(), "", right);
49 * A common routine to output test failure messages. Generally this should not
50 * be called directly, rather it should be called by the following functions.
52 * |desc| is a printf formatted description with arguments |args| that is
53 * supplied by the user and |desc| can be NULL. |type| is the data type
54 * that was tested (int, char, ptr, ...). |fmt| is a system provided
55 * printf format with following arguments that spell out the failure
56 * details i.e. the actual values compared and the operator used.
58 * The typical use for this is from an utility test function:
60 * int test6(const char *file, int line, int n) {
62 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
68 * calling test6(3, "oops") will return 0 and produce out along the lines of:
69 * FAIL oops: (int) value 3 is not 6\n
71 static void test_fail_message(const char *prefix, const char *file, int line,
72 const char *type, const char *left,
73 const char *right, const char *op,
77 static void test_fail_message_va(const char *prefix, const char *file,
78 int line, const char *type,
79 const char *left, const char *right,
80 const char *op, const char *fmt, va_list ap)
82 test_fail_message_prefix(prefix, file, line, type, left, right, op);
84 test_printf_stderr("%*s# ", subtest_level(), "");
85 test_vprintf_stderr(fmt, ap);
86 test_printf_stderr("\n");
88 test_printf_stderr("\n");
92 static void test_fail_string_message(const char *prefix, const char *file,
93 int line, const char *type,
94 const char *left, const char *right,
95 const char *op, const char *m1, size_t l1,
96 const char *m2, size_t l2)
98 const int indent = subtest_level();
99 const size_t width = (MAX_STRING_WIDTH - indent - 12) / 16 * 16;
100 char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
101 char bdiff[MAX_STRING_WIDTH + 1];
103 unsigned int cnt = 0, diff;
105 test_fail_message_prefix(prefix, file, line, type, left, right, op);
110 if (l1 == 0 && l2 == 0) {
111 if ((m1 == NULL) == (m2 == NULL)) {
112 test_printf_stderr("%*s# % 4s %s\n", indent, "", "",
113 m1 == NULL ? "NULL" : "''");
115 test_diff_header(left, right);
116 test_printf_stderr("%*s# % 4s - %s\n", indent, "", "",
117 m1 == NULL ? "NULL" : "''");
118 test_printf_stderr("%*s# % 4s + %s\n", indent, "", "",
119 m2 == NULL ? "NULL" : "''");
124 if (l1 != l2 || strcmp(m1, m2) != 0)
125 test_diff_header(left, right);
127 while (l1 > 0 || l2 > 0) {
130 b1[n1 = l1 > width ? width : l1] = 0;
131 for (i = 0; i < n1; i++)
132 b1[i] = isprint(m1[i]) ? m1[i] : '.';
135 b2[n2 = l2 > width ? width : l2] = 0;
136 for (i = 0; i < n2; i++)
137 b2[i] = isprint(m2[i]) ? m2[i] : '.';
141 if (n1 > 0 && n2 > 0) {
142 const size_t j = n1 < n2 ? n1 : n2;
143 const size_t k = n1 > n2 ? n1 : n2;
146 if (m1[i] == m2[i]) {
157 test_printf_stderr("%*s# % 4u: '%s'\n", indent, "", cnt, b1);
159 if (cnt == 0 && m1 == NULL)
160 test_printf_stderr("%*s# % 4s - NULL\n", indent, "", "");
161 else if (cnt == 0 && *m1 == '\0')
162 test_printf_stderr("%*s# % 4s - ''\n", indent, "", "");
164 test_printf_stderr("%*s# % 4u:- '%s'\n", indent, "", cnt, b1);
165 if (cnt == 0 && m2 == NULL)
166 test_printf_stderr("%*s# % 4s + NULL\n", indent, "", "");
167 else if (cnt == 0 && *m2 == '\0')
168 test_printf_stderr("%*s# % 4s + ''\n", indent, "", "");
170 test_printf_stderr("%*s# % 4u:+ '%s'\n", indent, "", cnt, b2);
172 test_printf_stderr("%*s# % 4s %s\n", indent, "", "", bdiff);
181 test_printf_stderr("\n");
185 static void hex_convert_memory(const unsigned char *m, size_t n, char *b,
190 for (i = 0; i < n; i++) {
191 const unsigned char c = *m++;
193 *b++ = "0123456789abcdef"[c >> 4];
194 *b++ = "0123456789abcdef"[c & 15];
195 if (i % width == width - 1 && i != n - 1)
201 static const int bn_bytes = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
203 static const int bn_chars = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
204 * (BN_OUTPUT_SIZE * 2 + 1) - 1;
206 static void test_bignum_header_line(void)
208 test_printf_stderr("%*s# %*s\n", subtest_level(), "", bn_chars + 6,
212 static void test_bignum_zero_print(const BIGNUM *bn, char sep)
214 const char *v = "NULL", *suf = "";
217 v = BN_is_negative(bn) ? "-0" : "0";
219 test_printf_stderr("%*s# %c%*s%s\n", subtest_level(), "", sep, bn_chars,
223 static int convert_bn_memory(const unsigned char *in, size_t bytes,
224 char *out, int *lz, const BIGNUM *bn)
226 int n = bytes * 2, i;
227 char *p = out, *q = NULL;
229 if (bn != NULL && !BN_is_zero(bn)) {
230 hex_convert_memory(in, bytes, out, BN_OUTPUT_SIZE);
232 for (; *p == '0' || *p == ' '; p++)
240 * in[bytes] is defined because we're converting a non-zero
241 * number and we've not seen a non-zero yet.
243 if ((in[bytes] & 0xf0) != 0 && BN_is_negative(bn)) {
250 if (BN_is_negative(bn)) {
252 * This is valid because we always convert more digits than
263 for (i = 0; i < n; i++) {
265 if (i % (2 * BN_OUTPUT_SIZE) == 2 * BN_OUTPUT_SIZE - 1 && i != n - 1)
272 q = BN_is_negative(bn) ? "-0" : "0";
273 strcpy(p - strlen(q), q);
277 static void test_fail_bignum_common(const char *prefix, const char *file,
278 int line, const char *type,
279 const char *left, const char *right,
281 const BIGNUM *bn1, const BIGNUM *bn2)
283 const int indent = subtest_level();
284 const size_t bytes = bn_bytes;
285 char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
286 char *p, bdiff[MAX_STRING_WIDTH + 1];
287 size_t l1, l2, n1, n2, i, len;
288 unsigned int cnt, diff, real_diff;
289 unsigned char *m1 = NULL, *m2 = NULL;
290 int lz1 = 1, lz2 = 1;
291 unsigned char buffer[MEM_BUFFER_SIZE * 2], *bufp = buffer;
293 l1 = bn1 == NULL ? 0 : (BN_num_bytes(bn1) + (BN_is_negative(bn1) ? 1 : 0));
294 l2 = bn2 == NULL ? 0 : (BN_num_bytes(bn2) + (BN_is_negative(bn2) ? 1 : 0));
295 if (l1 == 0 && l2 == 0) {
296 if ((bn1 == NULL) == (bn2 == NULL)) {
297 test_bignum_header_line();
298 test_bignum_zero_print(bn1, ' ');
300 test_diff_header(left, right);
301 test_bignum_header_line();
302 test_bignum_zero_print(bn1, '-');
303 test_bignum_zero_print(bn2, '+');
308 if (l1 != l2 || bn1 == NULL || bn2 == NULL || BN_cmp(bn1, bn2) != 0)
309 test_diff_header(left, right);
310 test_bignum_header_line();
312 len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes;
314 if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) {
316 len = MEM_BUFFER_SIZE;
317 test_printf_stderr("%*s# WARNING: these BIGNUMs have been truncated",
323 BN_bn2binpad(bn1, m1, len);
327 BN_bn2binpad(bn2, m2, len);
331 cnt = 8 * (len - bytes);
332 n1 = convert_bn_memory(m1, bytes, b1, &lz1, bn1);
333 n2 = convert_bn_memory(m2, bytes, b2, &lz2, bn2);
335 diff = real_diff = 0;
338 for (i=0; b1[i] != '\0'; i++)
339 if (b1[i] == b2[i] || b1[i] == ' ' || b2[i] == ' ') {
341 diff |= b1[i] != b2[i];
344 real_diff = diff = 1;
348 test_printf_stderr("%*s# %s:% 5d\n", indent, "",
349 n2 > n1 ? b2 : b1, cnt);
351 if (cnt == 0 && bn1 == NULL)
352 test_printf_stderr("%*s# -%s\n", indent, "", b1);
353 else if (cnt == 0 || n1 > 0)
354 test_printf_stderr("%*s# -%s:% 5d\n", indent, "", b1, cnt);
355 if (cnt == 0 && bn2 == NULL)
356 test_printf_stderr("%*s# +%s\n", indent, "", b2);
357 else if (cnt == 0 || n2 > 0)
358 test_printf_stderr("%*s# +%s:% 5d\n", indent, "", b2, cnt);
359 if (real_diff && (cnt == 0 || (n1 > 0 && n2 > 0))
360 && bn1 != NULL && bn2 != NULL)
361 test_printf_stderr("%*s# %s\n", indent, "", bdiff);
370 test_printf_stderr("\n");
376 static void test_fail_bignum_message(const char *prefix, const char *file,
377 int line, const char *type,
378 const char *left, const char *right,
380 const BIGNUM *bn1, const BIGNUM *bn2)
382 test_fail_message_prefix(prefix, file, line, type, left, right, op);
383 test_fail_bignum_common(prefix, file, line, type, left, right, op, bn1, bn2);
386 static void test_fail_bignum_mono_message(const char *prefix, const char *file,
387 int line, const char *type,
388 const char *left, const char *right,
389 const char *op, const BIGNUM *bn)
391 test_fail_message_prefix(prefix, file, line, type, left, right, op);
392 test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn);
395 static void test_memory_null_empty(const unsigned char *m, int indent, char c)
398 test_printf_stderr("%*s# % 4s %c%s\n", indent, "", "", c, "NULL");
400 test_printf_stderr("%*s# %04x %c%s\n", indent, "", 0u, c, "empty");
403 static void test_fail_memory_message(const char *prefix, const char *file,
404 int line, const char *type,
405 const char *left, const char *right,
407 const unsigned char *m1, size_t l1,
408 const unsigned char *m2, size_t l2)
410 const int indent = subtest_level();
411 const size_t bytes = (MAX_STRING_WIDTH - 9) / 17 * 8;
412 char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
413 char *p, bdiff[MAX_STRING_WIDTH + 1];
415 unsigned int cnt = 0, diff;
417 test_fail_message_prefix(prefix, file, line, type, left, right, op);
422 if (l1 == 0 && l2 == 0) {
423 if ((m1 == NULL) == (m2 == NULL)) {
424 test_memory_null_empty(m1, indent, ' ');
426 test_diff_header(left, right);
427 test_memory_null_empty(m1, indent, '-');
428 test_memory_null_empty(m2, indent, '+');
433 if (l1 != l2 || memcmp(m1, m2, l1) != 0)
434 test_diff_header(left, right);
436 while (l1 > 0 || l2 > 0) {
439 n1 = l1 > bytes ? bytes : l1;
440 hex_convert_memory(m1, n1, b1, 8);
443 n2 = l2 > bytes ? bytes : l2;
444 hex_convert_memory(m2, n2, b2, 8);
450 if (n1 > 0 && n2 > 0) {
451 const size_t j = n1 < n2 ? n1 : n2;
454 if (m1[i] == m2[i]) {
462 if (i % 8 == 7 && i != j - 1)
468 if (n1 == n2 && !diff) {
469 test_printf_stderr("%*s# %04x: %s\n", indent, "", cnt, b1);
471 if (cnt == 0 && (m1 == NULL || l1 == 0))
472 test_memory_null_empty(m1, indent, '-');
474 test_printf_stderr("%*s# %04x:-%s\n", indent, "", cnt, b1);
475 if (cnt == 0 && (m2 == NULL || l2 == 0))
476 test_memory_null_empty(m2, indent, '+');
478 test_printf_stderr("%*s# %04x:+%s\n", indent, "", cnt, b2);
480 test_printf_stderr("%*s# % 4s %s\n", indent, "", "", bdiff);
489 test_printf_stderr("\n");
493 static void test_fail_message(const char *prefix, const char *file,
494 int line, const char *type,
495 const char *left, const char *right,
496 const char *op, const char *fmt, ...)
501 test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap);
505 void test_info_c90(const char *desc, ...)
510 test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
514 void test_info(const char *file, int line, const char *desc, ...)
519 test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap);
523 void test_error_c90(const char *desc, ...)
528 test_fail_message(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
532 void test_error(const char *file, int line, const char *desc, ...)
537 test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap);
541 void test_openssl_errors(void)
543 ERR_print_errors_cb(openssl_error_cb, NULL);
547 * Define some comparisons between pairs of various types.
548 * These functions return 1 if the test is true.
549 * Otherwise, they return 0 and pretty-print diagnostics.
551 * In each case the functions produced are:
552 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
553 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
554 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
555 * int test_name_le(const type t1, const type t2, const char *desc, ...);
556 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
557 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
559 * The t1 and t2 arguments are to be compared for equality, inequality,
560 * less than, less than or equal to, greater than and greater than or
561 * equal to respectively. If the specified condition holds, the functions
562 * return 1. If the condition does not hold, the functions print a diagnostic
563 * message and return 0.
565 * The desc argument is a printf format string followed by its arguments and
566 * this is included in the output if the condition being tested for is false.
568 #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
569 int test_ ## name ## _ ## opname(const char *file, int line, \
570 const char *s1, const char *s2, \
571 const type t1, const type t2) \
575 test_fail_message(NULL, file, line, #type, s1, s2, #op, \
576 "[" fmt "] compared to [" fmt "]", \
581 #define DEFINE_COMPARISONS(type, name, fmt) \
582 DEFINE_COMPARISON(type, name, eq, ==, fmt) \
583 DEFINE_COMPARISON(type, name, ne, !=, fmt) \
584 DEFINE_COMPARISON(type, name, lt, <, fmt) \
585 DEFINE_COMPARISON(type, name, le, <=, fmt) \
586 DEFINE_COMPARISON(type, name, gt, >, fmt) \
587 DEFINE_COMPARISON(type, name, ge, >=, fmt)
589 DEFINE_COMPARISONS(int, int, "%d")
590 DEFINE_COMPARISONS(unsigned int, uint, "%u")
591 DEFINE_COMPARISONS(char, char, "%c")
592 DEFINE_COMPARISONS(unsigned char, uchar, "%u")
593 DEFINE_COMPARISONS(long, long, "%ld")
594 DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
595 DEFINE_COMPARISONS(size_t, size_t, "%zu")
597 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
598 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
600 int test_ptr_null(const char *file, int line, const char *s, const void *p)
604 test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
608 int test_ptr(const char *file, int line, const char *s, const void *p)
612 test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
616 int test_true(const char *file, int line, const char *s, int b)
620 test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
624 int test_false(const char *file, int line, const char *s, int b)
628 test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
632 int test_str_eq(const char *file, int line, const char *st1, const char *st2,
633 const char *s1, const char *s2)
635 if (s1 == NULL && s2 == NULL)
637 if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
638 test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
639 s1, s1 == NULL ? 0 : strlen(s1),
640 s2, s2 == NULL ? 0 : strlen(s2));
646 int test_str_ne(const char *file, int line, const char *st1, const char *st2,
647 const char *s1, const char *s2)
649 if ((s1 == NULL) ^ (s2 == NULL))
651 if (s1 == NULL || strcmp(s1, s2) == 0) {
652 test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
653 s1, s1 == NULL ? 0 : strlen(s1),
654 s2, s2 == NULL ? 0 : strlen(s2));
660 int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
661 const char *s1, const char *s2, size_t len)
663 if (s1 == NULL && s2 == NULL)
665 if (s1 == NULL || s2 == NULL || strncmp(s1, s2, len) != 0) {
666 test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
667 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
668 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
674 int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
675 const char *s1, const char *s2, size_t len)
677 if ((s1 == NULL) ^ (s2 == NULL))
679 if (s1 == NULL || strncmp(s1, s2, len) == 0) {
680 test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
681 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
682 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
688 int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
689 const void *s1, size_t n1, const void *s2, size_t n2)
691 if (s1 == NULL && s2 == NULL)
693 if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
694 test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
701 int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
702 const void *s1, size_t n1, const void *s2, size_t n2)
704 if ((s1 == NULL) ^ (s2 == NULL))
708 if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
709 test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
716 #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
717 int test_BN_ ## opname(const char *file, int line, \
718 const char *s1, const char *s2, \
719 const BIGNUM *t1, const BIGNUM *t2) \
721 if (BN_cmp(t1, t2) op 0) \
723 test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
727 int test_BN_ ## opname ## _zero(const char *file, int line, \
728 const char *s, const BIGNUM *a) \
730 if (a != NULL &&(zero_cond)) \
732 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
737 DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
738 DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
739 DEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a))
740 DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
741 DEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a))
742 DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
744 int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
746 if (a != NULL && BN_is_one(a))
748 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
752 int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
754 if (a != NULL && BN_is_odd(a))
756 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
760 int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
762 if (a != NULL && !BN_is_odd(a))
764 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
769 int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
770 const BIGNUM *a, BN_ULONG w)
774 if (a != NULL && BN_is_word(a, w))
778 test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
783 int test_BN_abs_eq_word(const char *file, int line, const char *bns,
784 const char *ws, const BIGNUM *a, BN_ULONG w)
788 if (a != NULL && BN_abs_is_word(a, w))
792 BN_set_negative(aa, 0);
794 test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",