1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include "../testutil.h"
11*e0c4386eSCy Schubert #include "output.h"
12*e0c4386eSCy Schubert #include "tu_local.h"
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert #include <errno.h>
15*e0c4386eSCy Schubert #include <string.h>
16*e0c4386eSCy Schubert #include <ctype.h>
17*e0c4386eSCy Schubert #include "internal/nelem.h"
18*e0c4386eSCy Schubert #include <openssl/asn1.h>
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubert /*
21*e0c4386eSCy Schubert * Output a failed test first line.
22*e0c4386eSCy Schubert * All items are optional are generally not preinted if passed as NULL.
23*e0c4386eSCy Schubert * The special cases are for prefix where "ERROR" is assumed and for left
24*e0c4386eSCy Schubert * and right where a non-failure message is produced if either is NULL.
25*e0c4386eSCy Schubert */
test_fail_message_prefix(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op)26*e0c4386eSCy Schubert void test_fail_message_prefix(const char *prefix, const char *file,
27*e0c4386eSCy Schubert int line, const char *type,
28*e0c4386eSCy Schubert const char *left, const char *right,
29*e0c4386eSCy Schubert const char *op)
30*e0c4386eSCy Schubert {
31*e0c4386eSCy Schubert test_printf_stderr("%s: ", prefix != NULL ? prefix : "ERROR");
32*e0c4386eSCy Schubert if (type)
33*e0c4386eSCy Schubert test_printf_stderr("(%s) ", type);
34*e0c4386eSCy Schubert if (op != NULL) {
35*e0c4386eSCy Schubert if (left != NULL && right != NULL)
36*e0c4386eSCy Schubert test_printf_stderr("'%s %s %s' failed", left, op, right);
37*e0c4386eSCy Schubert else
38*e0c4386eSCy Schubert test_printf_stderr("'%s'", op);
39*e0c4386eSCy Schubert }
40*e0c4386eSCy Schubert if (file != NULL) {
41*e0c4386eSCy Schubert test_printf_stderr(" @ %s:%d", file, line);
42*e0c4386eSCy Schubert }
43*e0c4386eSCy Schubert test_printf_stderr("\n");
44*e0c4386eSCy Schubert }
45*e0c4386eSCy Schubert
46*e0c4386eSCy Schubert /*
47*e0c4386eSCy Schubert * A common routine to output test failure messages. Generally this should not
48*e0c4386eSCy Schubert * be called directly, rather it should be called by the following functions.
49*e0c4386eSCy Schubert *
50*e0c4386eSCy Schubert * |desc| is a printf formatted description with arguments |args| that is
51*e0c4386eSCy Schubert * supplied by the user and |desc| can be NULL. |type| is the data type
52*e0c4386eSCy Schubert * that was tested (int, char, ptr, ...). |fmt| is a system provided
53*e0c4386eSCy Schubert * printf format with following arguments that spell out the failure
54*e0c4386eSCy Schubert * details i.e. the actual values compared and the operator used.
55*e0c4386eSCy Schubert *
56*e0c4386eSCy Schubert * The typical use for this is from an utility test function:
57*e0c4386eSCy Schubert *
58*e0c4386eSCy Schubert * int test6(const char *file, int line, int n) {
59*e0c4386eSCy Schubert * if (n != 6) {
60*e0c4386eSCy Schubert * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
61*e0c4386eSCy Schubert * return 0;
62*e0c4386eSCy Schubert * }
63*e0c4386eSCy Schubert * return 1;
64*e0c4386eSCy Schubert * }
65*e0c4386eSCy Schubert *
66*e0c4386eSCy Schubert * calling test6(3, "oops") will return 0 and produce out along the lines of:
67*e0c4386eSCy Schubert * FAIL oops: (int) value 3 is not 6\n
68*e0c4386eSCy Schubert */
69*e0c4386eSCy Schubert static void test_fail_message(const char *prefix, const char *file, int line,
70*e0c4386eSCy Schubert const char *type, const char *left,
71*e0c4386eSCy Schubert const char *right, const char *op,
72*e0c4386eSCy Schubert const char *fmt, ...)
73*e0c4386eSCy Schubert PRINTF_FORMAT(8, 9);
74*e0c4386eSCy Schubert
test_fail_message_va(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const char * fmt,va_list ap)75*e0c4386eSCy Schubert static void test_fail_message_va(const char *prefix, const char *file,
76*e0c4386eSCy Schubert int line, const char *type,
77*e0c4386eSCy Schubert const char *left, const char *right,
78*e0c4386eSCy Schubert const char *op, const char *fmt, va_list ap)
79*e0c4386eSCy Schubert {
80*e0c4386eSCy Schubert test_fail_message_prefix(prefix, file, line, type, left, right, op);
81*e0c4386eSCy Schubert if (fmt != NULL) {
82*e0c4386eSCy Schubert test_vprintf_stderr(fmt, ap);
83*e0c4386eSCy Schubert test_printf_stderr("\n");
84*e0c4386eSCy Schubert }
85*e0c4386eSCy Schubert test_flush_stderr();
86*e0c4386eSCy Schubert }
87*e0c4386eSCy Schubert
test_fail_message(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const char * fmt,...)88*e0c4386eSCy Schubert static void test_fail_message(const char *prefix, const char *file,
89*e0c4386eSCy Schubert int line, const char *type,
90*e0c4386eSCy Schubert const char *left, const char *right,
91*e0c4386eSCy Schubert const char *op, const char *fmt, ...)
92*e0c4386eSCy Schubert {
93*e0c4386eSCy Schubert va_list ap;
94*e0c4386eSCy Schubert
95*e0c4386eSCy Schubert va_start(ap, fmt);
96*e0c4386eSCy Schubert test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap);
97*e0c4386eSCy Schubert va_end(ap);
98*e0c4386eSCy Schubert }
99*e0c4386eSCy Schubert
test_info_c90(const char * desc,...)100*e0c4386eSCy Schubert void test_info_c90(const char *desc, ...)
101*e0c4386eSCy Schubert {
102*e0c4386eSCy Schubert va_list ap;
103*e0c4386eSCy Schubert
104*e0c4386eSCy Schubert va_start(ap, desc);
105*e0c4386eSCy Schubert test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
106*e0c4386eSCy Schubert va_end(ap);
107*e0c4386eSCy Schubert }
108*e0c4386eSCy Schubert
test_info(const char * file,int line,const char * desc,...)109*e0c4386eSCy Schubert void test_info(const char *file, int line, const char *desc, ...)
110*e0c4386eSCy Schubert {
111*e0c4386eSCy Schubert va_list ap;
112*e0c4386eSCy Schubert
113*e0c4386eSCy Schubert va_start(ap, desc);
114*e0c4386eSCy Schubert test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap);
115*e0c4386eSCy Schubert va_end(ap);
116*e0c4386eSCy Schubert }
117*e0c4386eSCy Schubert
test_error_c90(const char * desc,...)118*e0c4386eSCy Schubert void test_error_c90(const char *desc, ...)
119*e0c4386eSCy Schubert {
120*e0c4386eSCy Schubert va_list ap;
121*e0c4386eSCy Schubert
122*e0c4386eSCy Schubert va_start(ap, desc);
123*e0c4386eSCy Schubert test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
124*e0c4386eSCy Schubert va_end(ap);
125*e0c4386eSCy Schubert test_printf_stderr("\n");
126*e0c4386eSCy Schubert }
127*e0c4386eSCy Schubert
test_error(const char * file,int line,const char * desc,...)128*e0c4386eSCy Schubert void test_error(const char *file, int line, const char *desc, ...)
129*e0c4386eSCy Schubert {
130*e0c4386eSCy Schubert va_list ap;
131*e0c4386eSCy Schubert
132*e0c4386eSCy Schubert va_start(ap, desc);
133*e0c4386eSCy Schubert test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap);
134*e0c4386eSCy Schubert va_end(ap);
135*e0c4386eSCy Schubert test_printf_stderr("\n");
136*e0c4386eSCy Schubert }
137*e0c4386eSCy Schubert
test_perror(const char * s)138*e0c4386eSCy Schubert void test_perror(const char *s)
139*e0c4386eSCy Schubert {
140*e0c4386eSCy Schubert /*
141*e0c4386eSCy Schubert * Using openssl_strerror_r causes linking issues since it isn't
142*e0c4386eSCy Schubert * exported from libcrypto.so
143*e0c4386eSCy Schubert */
144*e0c4386eSCy Schubert TEST_error("%s: %s", s, strerror(errno));
145*e0c4386eSCy Schubert }
146*e0c4386eSCy Schubert
test_note(const char * fmt,...)147*e0c4386eSCy Schubert void test_note(const char *fmt, ...)
148*e0c4386eSCy Schubert {
149*e0c4386eSCy Schubert if (fmt != NULL) {
150*e0c4386eSCy Schubert va_list ap;
151*e0c4386eSCy Schubert
152*e0c4386eSCy Schubert va_start(ap, fmt);
153*e0c4386eSCy Schubert test_vprintf_stderr(fmt, ap);
154*e0c4386eSCy Schubert va_end(ap);
155*e0c4386eSCy Schubert test_printf_stderr("\n");
156*e0c4386eSCy Schubert }
157*e0c4386eSCy Schubert test_flush_stderr();
158*e0c4386eSCy Schubert }
159*e0c4386eSCy Schubert
160*e0c4386eSCy Schubert
test_skip(const char * file,int line,const char * desc,...)161*e0c4386eSCy Schubert int test_skip(const char *file, int line, const char *desc, ...)
162*e0c4386eSCy Schubert {
163*e0c4386eSCy Schubert va_list ap;
164*e0c4386eSCy Schubert
165*e0c4386eSCy Schubert va_start(ap, desc);
166*e0c4386eSCy Schubert test_fail_message_va("SKIP", file, line, NULL, NULL, NULL, NULL, desc, ap);
167*e0c4386eSCy Schubert va_end(ap);
168*e0c4386eSCy Schubert return TEST_SKIP_CODE;
169*e0c4386eSCy Schubert }
170*e0c4386eSCy Schubert
test_skip_c90(const char * desc,...)171*e0c4386eSCy Schubert int test_skip_c90(const char *desc, ...)
172*e0c4386eSCy Schubert {
173*e0c4386eSCy Schubert va_list ap;
174*e0c4386eSCy Schubert
175*e0c4386eSCy Schubert va_start(ap, desc);
176*e0c4386eSCy Schubert test_fail_message_va("SKIP", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
177*e0c4386eSCy Schubert va_end(ap);
178*e0c4386eSCy Schubert test_printf_stderr("\n");
179*e0c4386eSCy Schubert return TEST_SKIP_CODE;
180*e0c4386eSCy Schubert }
181*e0c4386eSCy Schubert
182*e0c4386eSCy Schubert
test_openssl_errors(void)183*e0c4386eSCy Schubert void test_openssl_errors(void)
184*e0c4386eSCy Schubert {
185*e0c4386eSCy Schubert ERR_print_errors_cb(openssl_error_cb, NULL);
186*e0c4386eSCy Schubert ERR_clear_error();
187*e0c4386eSCy Schubert }
188*e0c4386eSCy Schubert
189*e0c4386eSCy Schubert /*
190*e0c4386eSCy Schubert * Define some comparisons between pairs of various types.
191*e0c4386eSCy Schubert * These functions return 1 if the test is true.
192*e0c4386eSCy Schubert * Otherwise, they return 0 and pretty-print diagnostics.
193*e0c4386eSCy Schubert *
194*e0c4386eSCy Schubert * In each case the functions produced are:
195*e0c4386eSCy Schubert * int test_name_eq(const type t1, const type t2, const char *desc, ...);
196*e0c4386eSCy Schubert * int test_name_ne(const type t1, const type t2, const char *desc, ...);
197*e0c4386eSCy Schubert * int test_name_lt(const type t1, const type t2, const char *desc, ...);
198*e0c4386eSCy Schubert * int test_name_le(const type t1, const type t2, const char *desc, ...);
199*e0c4386eSCy Schubert * int test_name_gt(const type t1, const type t2, const char *desc, ...);
200*e0c4386eSCy Schubert * int test_name_ge(const type t1, const type t2, const char *desc, ...);
201*e0c4386eSCy Schubert *
202*e0c4386eSCy Schubert * The t1 and t2 arguments are to be compared for equality, inequality,
203*e0c4386eSCy Schubert * less than, less than or equal to, greater than and greater than or
204*e0c4386eSCy Schubert * equal to respectively. If the specified condition holds, the functions
205*e0c4386eSCy Schubert * return 1. If the condition does not hold, the functions print a diagnostic
206*e0c4386eSCy Schubert * message and return 0.
207*e0c4386eSCy Schubert *
208*e0c4386eSCy Schubert * The desc argument is a printf format string followed by its arguments and
209*e0c4386eSCy Schubert * this is included in the output if the condition being tested for is false.
210*e0c4386eSCy Schubert */
211*e0c4386eSCy Schubert #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
212*e0c4386eSCy Schubert int test_ ## name ## _ ## opname(const char *file, int line, \
213*e0c4386eSCy Schubert const char *s1, const char *s2, \
214*e0c4386eSCy Schubert const type t1, const type t2) \
215*e0c4386eSCy Schubert { \
216*e0c4386eSCy Schubert if (t1 op t2) \
217*e0c4386eSCy Schubert return 1; \
218*e0c4386eSCy Schubert test_fail_message(NULL, file, line, #type, s1, s2, #op, \
219*e0c4386eSCy Schubert "[" fmt "] compared to [" fmt "]", \
220*e0c4386eSCy Schubert t1, t2); \
221*e0c4386eSCy Schubert return 0; \
222*e0c4386eSCy Schubert }
223*e0c4386eSCy Schubert
224*e0c4386eSCy Schubert #define DEFINE_COMPARISONS(type, name, fmt) \
225*e0c4386eSCy Schubert DEFINE_COMPARISON(type, name, eq, ==, fmt) \
226*e0c4386eSCy Schubert DEFINE_COMPARISON(type, name, ne, !=, fmt) \
227*e0c4386eSCy Schubert DEFINE_COMPARISON(type, name, lt, <, fmt) \
228*e0c4386eSCy Schubert DEFINE_COMPARISON(type, name, le, <=, fmt) \
229*e0c4386eSCy Schubert DEFINE_COMPARISON(type, name, gt, >, fmt) \
230*e0c4386eSCy Schubert DEFINE_COMPARISON(type, name, ge, >=, fmt)
231*e0c4386eSCy Schubert
232*e0c4386eSCy Schubert DEFINE_COMPARISONS(int, int, "%d")
233*e0c4386eSCy Schubert DEFINE_COMPARISONS(unsigned int, uint, "%u")
234*e0c4386eSCy Schubert DEFINE_COMPARISONS(char, char, "%c")
235*e0c4386eSCy Schubert DEFINE_COMPARISONS(unsigned char, uchar, "%u")
236*e0c4386eSCy Schubert DEFINE_COMPARISONS(long, long, "%ld")
237*e0c4386eSCy Schubert DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
238*e0c4386eSCy Schubert DEFINE_COMPARISONS(size_t, size_t, "%zu")
239*e0c4386eSCy Schubert DEFINE_COMPARISONS(double, double, "%g")
240*e0c4386eSCy Schubert
241*e0c4386eSCy Schubert DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
242*e0c4386eSCy Schubert DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
243*e0c4386eSCy Schubert
test_ptr_null(const char * file,int line,const char * s,const void * p)244*e0c4386eSCy Schubert int test_ptr_null(const char *file, int line, const char *s, const void *p)
245*e0c4386eSCy Schubert {
246*e0c4386eSCy Schubert if (p == NULL)
247*e0c4386eSCy Schubert return 1;
248*e0c4386eSCy Schubert test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
249*e0c4386eSCy Schubert return 0;
250*e0c4386eSCy Schubert }
251*e0c4386eSCy Schubert
test_ptr(const char * file,int line,const char * s,const void * p)252*e0c4386eSCy Schubert int test_ptr(const char *file, int line, const char *s, const void *p)
253*e0c4386eSCy Schubert {
254*e0c4386eSCy Schubert if (p != NULL)
255*e0c4386eSCy Schubert return 1;
256*e0c4386eSCy Schubert test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
257*e0c4386eSCy Schubert return 0;
258*e0c4386eSCy Schubert }
259*e0c4386eSCy Schubert
test_true(const char * file,int line,const char * s,int b)260*e0c4386eSCy Schubert int test_true(const char *file, int line, const char *s, int b)
261*e0c4386eSCy Schubert {
262*e0c4386eSCy Schubert if (b)
263*e0c4386eSCy Schubert return 1;
264*e0c4386eSCy Schubert test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
265*e0c4386eSCy Schubert return 0;
266*e0c4386eSCy Schubert }
267*e0c4386eSCy Schubert
test_false(const char * file,int line,const char * s,int b)268*e0c4386eSCy Schubert int test_false(const char *file, int line, const char *s, int b)
269*e0c4386eSCy Schubert {
270*e0c4386eSCy Schubert if (!b)
271*e0c4386eSCy Schubert return 1;
272*e0c4386eSCy Schubert test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
273*e0c4386eSCy Schubert return 0;
274*e0c4386eSCy Schubert }
275*e0c4386eSCy Schubert
test_str_eq(const char * file,int line,const char * st1,const char * st2,const char * s1,const char * s2)276*e0c4386eSCy Schubert int test_str_eq(const char *file, int line, const char *st1, const char *st2,
277*e0c4386eSCy Schubert const char *s1, const char *s2)
278*e0c4386eSCy Schubert {
279*e0c4386eSCy Schubert if (s1 == NULL && s2 == NULL)
280*e0c4386eSCy Schubert return 1;
281*e0c4386eSCy Schubert if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
282*e0c4386eSCy Schubert test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
283*e0c4386eSCy Schubert s1, s1 == NULL ? 0 : strlen(s1),
284*e0c4386eSCy Schubert s2, s2 == NULL ? 0 : strlen(s2));
285*e0c4386eSCy Schubert return 0;
286*e0c4386eSCy Schubert }
287*e0c4386eSCy Schubert return 1;
288*e0c4386eSCy Schubert }
289*e0c4386eSCy Schubert
test_str_ne(const char * file,int line,const char * st1,const char * st2,const char * s1,const char * s2)290*e0c4386eSCy Schubert int test_str_ne(const char *file, int line, const char *st1, const char *st2,
291*e0c4386eSCy Schubert const char *s1, const char *s2)
292*e0c4386eSCy Schubert {
293*e0c4386eSCy Schubert if ((s1 == NULL) ^ (s2 == NULL))
294*e0c4386eSCy Schubert return 1;
295*e0c4386eSCy Schubert if (s1 == NULL || strcmp(s1, s2) == 0) {
296*e0c4386eSCy Schubert test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
297*e0c4386eSCy Schubert s1, s1 == NULL ? 0 : strlen(s1),
298*e0c4386eSCy Schubert s2, s2 == NULL ? 0 : strlen(s2));
299*e0c4386eSCy Schubert return 0;
300*e0c4386eSCy Schubert }
301*e0c4386eSCy Schubert return 1;
302*e0c4386eSCy Schubert }
303*e0c4386eSCy Schubert
test_strn_eq(const char * file,int line,const char * st1,const char * st2,const char * s1,size_t n1,const char * s2,size_t n2)304*e0c4386eSCy Schubert int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
305*e0c4386eSCy Schubert const char *s1, size_t n1, const char *s2, size_t n2)
306*e0c4386eSCy Schubert {
307*e0c4386eSCy Schubert if (s1 == NULL && s2 == NULL)
308*e0c4386eSCy Schubert return 1;
309*e0c4386eSCy Schubert if (n1 != n2 || s1 == NULL || s2 == NULL || strncmp(s1, s2, n1) != 0) {
310*e0c4386eSCy Schubert test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
311*e0c4386eSCy Schubert s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1),
312*e0c4386eSCy Schubert s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2));
313*e0c4386eSCy Schubert return 0;
314*e0c4386eSCy Schubert }
315*e0c4386eSCy Schubert return 1;
316*e0c4386eSCy Schubert }
317*e0c4386eSCy Schubert
test_strn_ne(const char * file,int line,const char * st1,const char * st2,const char * s1,size_t n1,const char * s2,size_t n2)318*e0c4386eSCy Schubert int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
319*e0c4386eSCy Schubert const char *s1, size_t n1, const char *s2, size_t n2)
320*e0c4386eSCy Schubert {
321*e0c4386eSCy Schubert if ((s1 == NULL) ^ (s2 == NULL))
322*e0c4386eSCy Schubert return 1;
323*e0c4386eSCy Schubert if (n1 != n2 || s1 == NULL || strncmp(s1, s2, n1) == 0) {
324*e0c4386eSCy Schubert test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
325*e0c4386eSCy Schubert s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1),
326*e0c4386eSCy Schubert s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2));
327*e0c4386eSCy Schubert return 0;
328*e0c4386eSCy Schubert }
329*e0c4386eSCy Schubert return 1;
330*e0c4386eSCy Schubert }
331*e0c4386eSCy Schubert
test_mem_eq(const char * file,int line,const char * st1,const char * st2,const void * s1,size_t n1,const void * s2,size_t n2)332*e0c4386eSCy Schubert int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
333*e0c4386eSCy Schubert const void *s1, size_t n1, const void *s2, size_t n2)
334*e0c4386eSCy Schubert {
335*e0c4386eSCy Schubert if (s1 == NULL && s2 == NULL)
336*e0c4386eSCy Schubert return 1;
337*e0c4386eSCy Schubert if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
338*e0c4386eSCy Schubert test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
339*e0c4386eSCy Schubert s1, n1, s2, n2);
340*e0c4386eSCy Schubert return 0;
341*e0c4386eSCy Schubert }
342*e0c4386eSCy Schubert return 1;
343*e0c4386eSCy Schubert }
344*e0c4386eSCy Schubert
test_mem_ne(const char * file,int line,const char * st1,const char * st2,const void * s1,size_t n1,const void * s2,size_t n2)345*e0c4386eSCy Schubert int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
346*e0c4386eSCy Schubert const void *s1, size_t n1, const void *s2, size_t n2)
347*e0c4386eSCy Schubert {
348*e0c4386eSCy Schubert if ((s1 == NULL) ^ (s2 == NULL))
349*e0c4386eSCy Schubert return 1;
350*e0c4386eSCy Schubert if (n1 != n2)
351*e0c4386eSCy Schubert return 1;
352*e0c4386eSCy Schubert if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
353*e0c4386eSCy Schubert test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
354*e0c4386eSCy Schubert s1, n1, s2, n2);
355*e0c4386eSCy Schubert return 0;
356*e0c4386eSCy Schubert }
357*e0c4386eSCy Schubert return 1;
358*e0c4386eSCy Schubert }
359*e0c4386eSCy Schubert
360*e0c4386eSCy Schubert #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
361*e0c4386eSCy Schubert int test_BN_ ## opname(const char *file, int line, \
362*e0c4386eSCy Schubert const char *s1, const char *s2, \
363*e0c4386eSCy Schubert const BIGNUM *t1, const BIGNUM *t2) \
364*e0c4386eSCy Schubert { \
365*e0c4386eSCy Schubert if (BN_cmp(t1, t2) op 0) \
366*e0c4386eSCy Schubert return 1; \
367*e0c4386eSCy Schubert test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
368*e0c4386eSCy Schubert #op, t1, t2); \
369*e0c4386eSCy Schubert return 0; \
370*e0c4386eSCy Schubert } \
371*e0c4386eSCy Schubert int test_BN_ ## opname ## _zero(const char *file, int line, \
372*e0c4386eSCy Schubert const char *s, const BIGNUM *a) \
373*e0c4386eSCy Schubert { \
374*e0c4386eSCy Schubert if (a != NULL &&(zero_cond)) \
375*e0c4386eSCy Schubert return 1; \
376*e0c4386eSCy Schubert test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
377*e0c4386eSCy Schubert s, "0", #op, a); \
378*e0c4386eSCy Schubert return 0; \
379*e0c4386eSCy Schubert }
380*e0c4386eSCy Schubert
381*e0c4386eSCy Schubert DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
382*e0c4386eSCy Schubert DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
383*e0c4386eSCy Schubert DEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a))
384*e0c4386eSCy Schubert DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
385*e0c4386eSCy Schubert DEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a))
386*e0c4386eSCy Schubert DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
387*e0c4386eSCy Schubert
test_BN_eq_one(const char * file,int line,const char * s,const BIGNUM * a)388*e0c4386eSCy Schubert int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
389*e0c4386eSCy Schubert {
390*e0c4386eSCy Schubert if (a != NULL && BN_is_one(a))
391*e0c4386eSCy Schubert return 1;
392*e0c4386eSCy Schubert test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
393*e0c4386eSCy Schubert return 0;
394*e0c4386eSCy Schubert }
395*e0c4386eSCy Schubert
test_BN_odd(const char * file,int line,const char * s,const BIGNUM * a)396*e0c4386eSCy Schubert int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
397*e0c4386eSCy Schubert {
398*e0c4386eSCy Schubert if (a != NULL && BN_is_odd(a))
399*e0c4386eSCy Schubert return 1;
400*e0c4386eSCy Schubert test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
401*e0c4386eSCy Schubert return 0;
402*e0c4386eSCy Schubert }
403*e0c4386eSCy Schubert
test_BN_even(const char * file,int line,const char * s,const BIGNUM * a)404*e0c4386eSCy Schubert int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
405*e0c4386eSCy Schubert {
406*e0c4386eSCy Schubert if (a != NULL && !BN_is_odd(a))
407*e0c4386eSCy Schubert return 1;
408*e0c4386eSCy Schubert test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
409*e0c4386eSCy Schubert a);
410*e0c4386eSCy Schubert return 0;
411*e0c4386eSCy Schubert }
412*e0c4386eSCy Schubert
test_BN_eq_word(const char * file,int line,const char * bns,const char * ws,const BIGNUM * a,BN_ULONG w)413*e0c4386eSCy Schubert int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
414*e0c4386eSCy Schubert const BIGNUM *a, BN_ULONG w)
415*e0c4386eSCy Schubert {
416*e0c4386eSCy Schubert BIGNUM *bw;
417*e0c4386eSCy Schubert
418*e0c4386eSCy Schubert if (a != NULL && BN_is_word(a, w))
419*e0c4386eSCy Schubert return 1;
420*e0c4386eSCy Schubert if ((bw = BN_new()) != NULL)
421*e0c4386eSCy Schubert BN_set_word(bw, w);
422*e0c4386eSCy Schubert test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
423*e0c4386eSCy Schubert BN_free(bw);
424*e0c4386eSCy Schubert return 0;
425*e0c4386eSCy Schubert }
426*e0c4386eSCy Schubert
test_BN_abs_eq_word(const char * file,int line,const char * bns,const char * ws,const BIGNUM * a,BN_ULONG w)427*e0c4386eSCy Schubert int test_BN_abs_eq_word(const char *file, int line, const char *bns,
428*e0c4386eSCy Schubert const char *ws, const BIGNUM *a, BN_ULONG w)
429*e0c4386eSCy Schubert {
430*e0c4386eSCy Schubert BIGNUM *bw, *aa;
431*e0c4386eSCy Schubert
432*e0c4386eSCy Schubert if (a != NULL && BN_abs_is_word(a, w))
433*e0c4386eSCy Schubert return 1;
434*e0c4386eSCy Schubert if ((aa = BN_dup(a)) != NULL)
435*e0c4386eSCy Schubert BN_set_negative(aa, 0);
436*e0c4386eSCy Schubert if ((bw = BN_new()) != NULL)
437*e0c4386eSCy Schubert BN_set_word(bw, w);
438*e0c4386eSCy Schubert test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
439*e0c4386eSCy Schubert aa, bw);
440*e0c4386eSCy Schubert BN_free(bw);
441*e0c4386eSCy Schubert BN_free(aa);
442*e0c4386eSCy Schubert return 0;
443*e0c4386eSCy Schubert }
444*e0c4386eSCy Schubert
print_time(const ASN1_TIME * t)445*e0c4386eSCy Schubert static const char *print_time(const ASN1_TIME *t)
446*e0c4386eSCy Schubert {
447*e0c4386eSCy Schubert return t == NULL ? "<null>" : (const char *)ASN1_STRING_get0_data(t);
448*e0c4386eSCy Schubert }
449*e0c4386eSCy Schubert
450*e0c4386eSCy Schubert #define DEFINE_TIME_T_COMPARISON(opname, op) \
451*e0c4386eSCy Schubert int test_time_t_ ## opname(const char *file, int line, \
452*e0c4386eSCy Schubert const char *s1, const char *s2, \
453*e0c4386eSCy Schubert const time_t t1, const time_t t2) \
454*e0c4386eSCy Schubert { \
455*e0c4386eSCy Schubert ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \
456*e0c4386eSCy Schubert ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \
457*e0c4386eSCy Schubert int r = at1 != NULL && at2 != NULL \
458*e0c4386eSCy Schubert && ASN1_TIME_compare(at1, at2) op 0; \
459*e0c4386eSCy Schubert if (!r) \
460*e0c4386eSCy Schubert test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \
461*e0c4386eSCy Schubert "[%s] compared to [%s]", \
462*e0c4386eSCy Schubert print_time(at1), print_time(at2)); \
463*e0c4386eSCy Schubert ASN1_STRING_free(at1); \
464*e0c4386eSCy Schubert ASN1_STRING_free(at2); \
465*e0c4386eSCy Schubert return r; \
466*e0c4386eSCy Schubert }
467*e0c4386eSCy Schubert DEFINE_TIME_T_COMPARISON(eq, ==)
468*e0c4386eSCy Schubert DEFINE_TIME_T_COMPARISON(ne, !=)
469*e0c4386eSCy Schubert DEFINE_TIME_T_COMPARISON(gt, >)
470*e0c4386eSCy Schubert DEFINE_TIME_T_COMPARISON(ge, >=)
471*e0c4386eSCy Schubert DEFINE_TIME_T_COMPARISON(lt, <)
472*e0c4386eSCy Schubert DEFINE_TIME_T_COMPARISON(le, <=)
473