a5538e7d9f2a8ac95eef80d0ec867177e636c35f
[openssl.git] / test / testutil / tests.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "../testutil.h"
11 #include "output.h"
12 #include "tu_local.h"
13
14 #include <string.h>
15 #include <ctype.h>
16 #include "../../e_os.h"
17
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)
22
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,
27                                      const char *op)
28 {
29     test_printf_stderr("%*s# %s: ", subtest_level(), "",
30                        prefix != NULL ? prefix : "ERROR");
31     if (type)
32         test_printf_stderr("(%s) ", type);
33     if (op != NULL)
34         test_printf_stderr("'%s %s %s' failed", left, op, right);
35     if (file != NULL) {
36         test_printf_stderr(" @ %s:%d", file, line);
37     }
38     test_printf_stderr("\n");
39 }
40
41 /* Output a diff header */
42 static void test_diff_header(const char *left, const char *right)
43 {
44     test_printf_stderr("%*s# --- %s\n", subtest_level(), "", left);
45     test_printf_stderr("%*s# +++ %s\n", subtest_level(), "", right);
46 }
47
48 /*
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.
51  *
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.
57  *
58  * The typical use for this is from an utility test function:
59  *
60  * int test6(const char *file, int line, int n) {
61  *     if (n != 6) {
62  *         test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
63  *         return 0;
64  *     }
65  *     return 1;
66  * }
67  *
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
70  */
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,
74                               const char *fmt, ...)
75             PRINTF_FORMAT(8, 9);
76
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)
81 {
82     test_fail_message_prefix(prefix, file, line, type, left, right, op);
83     if (fmt != NULL) {
84         test_printf_stderr("%*s# ", subtest_level(), "");
85         test_vprintf_stderr(fmt, ap);
86         test_printf_stderr("\n");
87     }
88     test_printf_stderr("\n");
89     test_flush_stderr();
90 }
91
92 static void test_string_null_empty(const char *m, int indent, char c)
93 {
94     if (m == NULL)
95         test_printf_stderr("%*s# % 4s %c NULL\n", indent, "", "", c);
96     else
97         test_printf_stderr("%*s# % 4u:%c ''\n", indent, "", 0u, c);
98 }
99
100 static void test_fail_string_message(const char *prefix, const char *file,
101                                      int line, const char *type,
102                                      const char *left, const char *right,
103                                      const char *op, const char *m1, size_t l1,
104                                      const char *m2, size_t l2)
105 {
106     const int indent = subtest_level();
107     const size_t width = (MAX_STRING_WIDTH - indent - 12) / 16 * 16;
108     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
109     char bdiff[MAX_STRING_WIDTH + 1];
110     size_t n1, n2, i;
111     unsigned int cnt = 0, diff;
112
113     test_fail_message_prefix(prefix, file, line, type, left, right, op);
114     if (m1 == NULL)
115         l1 = 0;
116     if (m2 == NULL)
117         l2 = 0;
118     if (l1 == 0 && l2 == 0) {
119         if ((m1 == NULL) == (m2 == NULL)) {
120             test_string_null_empty(m1, indent, ' ');
121         } else {
122             test_diff_header(left, right);
123             test_string_null_empty(m1, indent, '-');
124             test_string_null_empty(m2, indent, '+');
125         }
126         goto fin;
127     }
128
129     if (l1 != l2 || strcmp(m1, m2) != 0)
130         test_diff_header(left, right);
131
132     while (l1 > 0 || l2 > 0) {
133         n1 = n2 = 0;
134         if (l1 > 0) {
135             b1[n1 = l1 > width ? width : l1] = 0;
136             for (i = 0; i < n1; i++)
137                 b1[i] = isprint(m1[i]) ? m1[i] : '.';
138         }
139         if (l2 > 0) {
140             b2[n2 = l2 > width ? width : l2] = 0;
141             for (i = 0; i < n2; i++)
142                 b2[i] = isprint(m2[i]) ? m2[i] : '.';
143         }
144         diff = 0;
145         i = 0;
146         if (n1 > 0 && n2 > 0) {
147             const size_t j = n1 < n2 ? n1 : n2;
148
149             for (; i < j; i++)
150                 if (m1[i] == m2[i]) {
151                     bdiff[i] = ' ';
152                 } else {
153                     bdiff[i] = '^';
154                     diff = 1;
155                 }
156             bdiff[i] = '\0';
157         }
158         if (n1 == n2 && !diff) {
159             test_printf_stderr("%*s# % 4u:  '%s'\n", indent, "", cnt,
160                                n2 > n1 ? b2 : b1);
161         } else {
162             if (cnt == 0 && (m1 == NULL || *m1 == '\0'))
163                 test_string_null_empty(m1, indent, '-');
164             else if (n1 > 0)
165                 test_printf_stderr("%*s# % 4u:- '%s'\n", indent, "", cnt, b1);
166             if (cnt == 0 && (m2 == NULL || *m2 == '\0'))
167                test_string_null_empty(m2, indent, '+');
168             else if (n2 > 0)
169                 test_printf_stderr("%*s# % 4u:+ '%s'\n", indent, "", cnt, b2);
170             if (diff && i > 0)
171                 test_printf_stderr("%*s# % 4s    %s\n", indent, "", "", bdiff);
172         }
173         m1 += n1;
174         m2 += n2;
175         l1 -= n1;
176         l2 -= n2;
177         cnt += width;
178     }
179 fin:
180     test_printf_stderr("\n");
181     test_flush_stderr();
182 }
183
184 static void hex_convert_memory(const unsigned char *m, size_t n, char *b,
185                                size_t width)
186 {
187     size_t i;
188
189     for (i = 0; i < n; i++) {
190         const unsigned char c = *m++;
191
192         *b++ = "0123456789abcdef"[c >> 4];
193         *b++ = "0123456789abcdef"[c & 15];
194         if (i % width == width - 1 && i != n - 1)
195             *b++ = ' ';
196     }
197     *b = '\0';
198 }
199
200 static const int bn_bytes = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
201                             * BN_OUTPUT_SIZE;
202 static const int bn_chars = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
203                             * (BN_OUTPUT_SIZE * 2 + 1) - 1;
204
205 static void test_bignum_header_line(void)
206 {
207     test_printf_stderr("%*s#  %*s\n", subtest_level(), "", bn_chars + 6,
208                        "bit position");
209 }
210
211 static void test_bignum_zero_print(const BIGNUM *bn, char sep)
212 {
213     const char *v = "NULL", *suf = "";
214     if (bn != NULL) {
215         suf = ":    0";
216         v = BN_is_negative(bn) ? "-0" : "0";
217     }
218     test_printf_stderr("%*s# %c%*s%s\n", subtest_level(), "", sep, bn_chars,
219                        v, suf);
220 }
221
222 static int convert_bn_memory(const unsigned char *in, size_t bytes,
223                              char *out, int *lz, const BIGNUM *bn)
224 {
225     int n = bytes * 2, i;
226     char *p = out, *q = NULL;
227
228     if (bn != NULL && !BN_is_zero(bn)) {
229         hex_convert_memory(in, bytes, out, BN_OUTPUT_SIZE);
230         if (*lz) {
231             for (; *p == '0' || *p == ' '; p++)
232                 if (*p == '0') {
233                     q = p;
234                     *p = ' ';
235                     n--;
236                 }
237             if (*p == '\0') {
238                 /*
239                  * in[bytes] is defined because we're converting a non-zero
240                  * number and we've not seen a non-zero yet.
241                  */
242                 if ((in[bytes] & 0xf0) != 0 && BN_is_negative(bn)) {
243                     *lz = 0;
244                     *q = '-';
245                     n++;
246                 }
247             } else {
248                 *lz = 0;
249                 if (BN_is_negative(bn)) {
250                     /*
251                      * This is valid because we always convert more digits than
252                      * the number holds.
253                      */
254                     *q = '-';
255                     n++;
256                 }
257             }
258         }
259        return n;
260     }
261
262     for (i = 0; i < n; i++) {
263         *p++ = ' ';
264         if (i % (2 * BN_OUTPUT_SIZE) == 2 * BN_OUTPUT_SIZE - 1 && i != n - 1)
265             *p++ = ' ';
266     }
267     *p = '\0';
268     if (bn == NULL)
269         q = "NULL";
270     else
271         q = BN_is_negative(bn) ? "-0" : "0";
272     strcpy(p - strlen(q), q);
273     return 0;
274 }
275
276 static void test_fail_bignum_common(const char *prefix, const char *file,
277                                     int line, const char *type,
278                                     const char *left, const char *right,
279                                     const char *op,
280                                     const BIGNUM *bn1, const BIGNUM *bn2)
281 {
282     const int indent = subtest_level();
283     const size_t bytes = bn_bytes;
284     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
285     char *p, bdiff[MAX_STRING_WIDTH + 1];
286     size_t l1, l2, n1, n2, i, len;
287     unsigned int cnt, diff, real_diff;
288     unsigned char *m1 = NULL, *m2 = NULL;
289     int lz1 = 1, lz2 = 1;
290     unsigned char buffer[MEM_BUFFER_SIZE * 2], *bufp = buffer;
291
292     l1 = bn1 == NULL ? 0 : (BN_num_bytes(bn1) + (BN_is_negative(bn1) ? 1 : 0));
293     l2 = bn2 == NULL ? 0 : (BN_num_bytes(bn2) + (BN_is_negative(bn2) ? 1 : 0));
294     if (l1 == 0 && l2 == 0) {
295         if ((bn1 == NULL) == (bn2 == NULL)) {
296             test_bignum_header_line();
297             test_bignum_zero_print(bn1, ' ');
298         } else {
299             test_diff_header(left, right);
300             test_bignum_header_line();
301             test_bignum_zero_print(bn1, '-');
302             test_bignum_zero_print(bn2, '+');
303         }
304         goto fin;
305     }
306
307     if (l1 != l2 || bn1 == NULL || bn2 == NULL || BN_cmp(bn1, bn2) != 0)
308         test_diff_header(left, right);
309     test_bignum_header_line();
310
311     len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes;
312
313     if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) {
314         bufp = buffer;
315         len = MEM_BUFFER_SIZE;
316         test_printf_stderr("%*s# WARNING: these BIGNUMs have been truncated",
317                            indent, "");
318     }
319
320     if (bn1 != NULL) {
321         m1 = bufp;
322         BN_bn2binpad(bn1, m1, len);
323     }
324     if (bn2 != NULL) {
325         m2 = bufp + len;
326         BN_bn2binpad(bn2, m2, len);
327     }
328
329     while (len > 0) {
330         cnt = 8 * (len - bytes);
331         n1 = convert_bn_memory(m1, bytes, b1, &lz1, bn1);
332         n2 = convert_bn_memory(m2, bytes, b2, &lz2, bn2);
333
334         diff = real_diff = 0;
335         i = 0;
336         p = bdiff;
337         for (i=0; b1[i] != '\0'; i++)
338             if (b1[i] == b2[i] || b1[i] == ' ' || b2[i] == ' ') {
339                 *p++ = ' ';
340                 diff |= b1[i] != b2[i];
341             } else {
342                 *p++ = '^';
343                 real_diff = diff = 1;
344             }
345         *p++ = '\0';
346         if (!diff) {
347             test_printf_stderr("%*s#  %s:% 5d\n", indent, "",
348                                n2 > n1 ? b2 : b1, cnt);
349         } else {
350             if (cnt == 0 && bn1 == NULL)
351                 test_printf_stderr("%*s# -%s\n", indent, "", b1);
352             else if (cnt == 0 || n1 > 0)
353                 test_printf_stderr("%*s# -%s:% 5d\n", indent, "", b1, cnt);
354             if (cnt == 0 && bn2 == NULL)
355                 test_printf_stderr("%*s# +%s\n", indent, "", b2);
356             else if (cnt == 0 || n2 > 0)
357                 test_printf_stderr("%*s# +%s:% 5d\n", indent, "", b2, cnt);
358             if (real_diff && (cnt == 0 || (n1 > 0 && n2 > 0))
359                     && bn1 != NULL && bn2 != NULL)
360                 test_printf_stderr("%*s#  %s\n", indent, "", bdiff);
361         }
362         if (m1 != NULL)
363             m1 += bytes;
364         if (m2 != NULL)
365             m2 += bytes;
366         len -= bytes;
367     }
368 fin:
369     test_printf_stderr("\n");
370     test_flush_stderr();
371     if (bufp != buffer)
372         OPENSSL_free(bufp);
373 }
374
375 static void test_fail_bignum_message(const char *prefix, const char *file,
376                                      int line, const char *type,
377                                      const char *left, const char *right,
378                                      const char *op,
379                                      const BIGNUM *bn1, const BIGNUM *bn2)
380 {
381     test_fail_message_prefix(prefix, file, line, type, left, right, op);
382     test_fail_bignum_common(prefix, file, line, type, left, right, op, bn1, bn2);
383 }
384
385 static void test_fail_bignum_mono_message(const char *prefix, const char *file,
386                                           int line, const char *type,
387                                           const char *left, const char *right,
388                                           const char *op, const BIGNUM *bn)
389 {
390     test_fail_message_prefix(prefix, file, line, type, left, right, op);
391     test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn);
392 }
393
394 static void test_memory_null_empty(const unsigned char *m, int indent, char c)
395 {
396     if (m == NULL)
397         test_printf_stderr("%*s# % 4s %c%s\n", indent, "", "", c, "NULL");
398     else
399         test_printf_stderr("%*s# %04x %c%s\n", indent, "", 0u, c, "empty");
400 }
401
402 static void test_fail_memory_message(const char *prefix, const char *file,
403                                      int line, const char *type,
404                                      const char *left, const char *right,
405                                      const char *op,
406                                      const unsigned char *m1, size_t l1,
407                                      const unsigned char *m2, size_t l2)
408 {
409     const int indent = subtest_level();
410     const size_t bytes = (MAX_STRING_WIDTH - 9) / 17 * 8;
411     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
412     char *p, bdiff[MAX_STRING_WIDTH + 1];
413     size_t n1, n2, i;
414     unsigned int cnt = 0, diff;
415
416     test_fail_message_prefix(prefix, file, line, type, left, right, op);
417     if (m1 == NULL)
418         l1 = 0;
419     if (m2 == NULL)
420         l2 = 0;
421     if (l1 == 0 && l2 == 0) {
422         if ((m1 == NULL) == (m2 == NULL)) {
423             test_memory_null_empty(m1, indent, ' ');
424         } else {
425             test_diff_header(left, right);
426             test_memory_null_empty(m1, indent, '-');
427             test_memory_null_empty(m2, indent, '+');
428         }
429         goto fin;
430     }
431
432     if (l1 != l2 || memcmp(m1, m2, l1) != 0)
433         test_diff_header(left, right);
434
435     while (l1 > 0 || l2 > 0) {
436         n1 = n2 = 0;
437         if (l1 > 0) {
438             n1 = l1 > bytes ? bytes : l1;
439             hex_convert_memory(m1, n1, b1, 8);
440         }
441         if (l2 > 0) {
442             n2 = l2 > bytes ? bytes : l2;
443             hex_convert_memory(m2, n2, b2, 8);
444         }
445
446         diff = 0;
447         i = 0;
448         p = bdiff;
449         if (n1 > 0 && n2 > 0) {
450             const size_t j = n1 < n2 ? n1 : n2;
451
452             for (; i < j; i++) {
453                 if (m1[i] == m2[i]) {
454                     *p++ = ' ';
455                     *p++ = ' ';
456                 } else {
457                     *p++ = '^';
458                     *p++ = '^';
459                     diff = 1;
460                 }
461                 if (i % 8 == 7 && i != j - 1)
462                     *p++ = ' ';
463             }
464             *p++ = '\0';
465         }
466
467         if (n1 == n2 && !diff) {
468             test_printf_stderr("%*s# %04x: %s\n", indent, "", cnt, b1);
469         } else {
470             if (cnt == 0 && (m1 == NULL || l1 == 0))
471                 test_memory_null_empty(m1, indent, '-');
472             else if (n1 > 0)
473                 test_printf_stderr("%*s# %04x:-%s\n", indent, "", cnt, b1);
474             if (cnt == 0 && (m2 == NULL || l2 == 0))
475                 test_memory_null_empty(m2, indent, '+');
476             else if (n2 > 0)
477                 test_printf_stderr("%*s# %04x:+%s\n", indent, "", cnt, b2);
478             if (diff && i > 0)
479                 test_printf_stderr("%*s# % 4s  %s\n", indent, "", "", bdiff);
480         }
481         m1 += n1;
482         m2 += n2;
483         l1 -= n1;
484         l2 -= n2;
485         cnt += bytes;
486     }
487 fin:
488     test_printf_stderr("\n");
489     test_flush_stderr();
490 }
491
492 static void test_fail_message(const char *prefix, const char *file,
493                               int line, const char *type,
494                               const char *left, const char *right,
495                               const char *op, const char *fmt, ...)
496 {
497     va_list ap;
498
499     va_start(ap, fmt);
500     test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap);
501     va_end(ap);
502 }
503
504 void test_info_c90(const char *desc, ...)
505 {
506     va_list ap;
507
508     va_start(ap, desc);
509     test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
510     va_end(ap);
511 }
512
513 void test_info(const char *file, int line, const char *desc, ...)
514 {
515     va_list ap;
516
517     va_start(ap, desc);
518     test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap);
519     va_end(ap);
520 }
521
522 void test_error_c90(const char *desc, ...)
523 {
524     va_list ap;
525
526     va_start(ap, desc);
527     test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
528     va_end(ap);
529 }
530
531 void test_error(const char *file, int line, const char *desc, ...)
532 {
533     va_list ap;
534
535     va_start(ap, desc);
536     test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap);
537     va_end(ap);
538 }
539
540 void test_openssl_errors(void)
541 {
542     ERR_print_errors_cb(openssl_error_cb, NULL);
543 }
544
545 /*
546  * Define some comparisons between pairs of various types.
547  * These functions return 1 if the test is true.
548  * Otherwise, they return 0 and pretty-print diagnostics.
549  *
550  * In each case the functions produced are:
551  *  int test_name_eq(const type t1, const type t2, const char *desc, ...);
552  *  int test_name_ne(const type t1, const type t2, const char *desc, ...);
553  *  int test_name_lt(const type t1, const type t2, const char *desc, ...);
554  *  int test_name_le(const type t1, const type t2, const char *desc, ...);
555  *  int test_name_gt(const type t1, const type t2, const char *desc, ...);
556  *  int test_name_ge(const type t1, const type t2, const char *desc, ...);
557  *
558  * The t1 and t2 arguments are to be compared for equality, inequality,
559  * less than, less than or equal to, greater than and greater than or
560  * equal to respectively.  If the specified condition holds, the functions
561  * return 1.  If the condition does not hold, the functions print a diagnostic
562  * message and return 0.
563  *
564  * The desc argument is a printf format string followed by its arguments and
565  * this is included in the output if the condition being tested for is false.
566  */
567 #define DEFINE_COMPARISON(type, name, opname, op, fmt)                  \
568     int test_ ## name ## _ ## opname(const char *file, int line,        \
569                                      const char *s1, const char *s2,    \
570                                      const type t1, const type t2)      \
571     {                                                                   \
572         if (t1 op t2)                                                   \
573             return 1;                                                   \
574         test_fail_message(NULL, file, line, #type, s1, s2, #op,         \
575                           "[" fmt "] compared to [" fmt "]",            \
576                           t1, t2);                                      \
577         return 0;                                                       \
578     }
579
580 #define DEFINE_COMPARISONS(type, name, fmt)                             \
581     DEFINE_COMPARISON(type, name, eq, ==, fmt)                          \
582     DEFINE_COMPARISON(type, name, ne, !=, fmt)                          \
583     DEFINE_COMPARISON(type, name, lt, <, fmt)                           \
584     DEFINE_COMPARISON(type, name, le, <=, fmt)                          \
585     DEFINE_COMPARISON(type, name, gt, >, fmt)                           \
586     DEFINE_COMPARISON(type, name, ge, >=, fmt)
587
588 DEFINE_COMPARISONS(int, int, "%d")
589 DEFINE_COMPARISONS(unsigned int, uint, "%u")
590 DEFINE_COMPARISONS(char, char, "%c")
591 DEFINE_COMPARISONS(unsigned char, uchar, "%u")
592 DEFINE_COMPARISONS(long, long, "%ld")
593 DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
594 DEFINE_COMPARISONS(size_t, size_t, "%zu")
595
596 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
597 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
598
599 int test_ptr_null(const char *file, int line, const char *s, const void *p)
600 {
601     if (p == NULL)
602         return 1;
603     test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
604     return 0;
605 }
606
607 int test_ptr(const char *file, int line, const char *s, const void *p)
608 {
609     if (p != NULL)
610         return 1;
611     test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
612     return 0;
613 }
614
615 int test_true(const char *file, int line, const char *s, int b)
616 {
617     if (b)
618         return 1;
619     test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
620     return 0;
621 }
622
623 int test_false(const char *file, int line, const char *s, int b)
624 {
625     if (!b)
626         return 1;
627     test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
628     return 0;
629 }
630
631 int test_str_eq(const char *file, int line, const char *st1, const char *st2,
632                 const char *s1, const char *s2)
633 {
634     if (s1 == NULL && s2 == NULL)
635       return 1;
636     if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
637         test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
638                                  s1, s1 == NULL ? 0 : strlen(s1),
639                                  s2, s2 == NULL ? 0 : strlen(s2));
640         return 0;
641     }
642     return 1;
643 }
644
645 int test_str_ne(const char *file, int line, const char *st1, const char *st2,
646                 const char *s1, const char *s2)
647 {
648     if ((s1 == NULL) ^ (s2 == NULL))
649       return 1;
650     if (s1 == NULL || strcmp(s1, s2) == 0) {
651         test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
652                                  s1, s1 == NULL ? 0 : strlen(s1),
653                                  s2, s2 == NULL ? 0 : strlen(s2));
654         return 0;
655     }
656     return 1;
657 }
658
659 int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
660                  const char *s1, const char *s2, size_t len)
661 {
662     if (s1 == NULL && s2 == NULL)
663       return 1;
664     if (s1 == NULL || s2 == NULL || strncmp(s1, s2, len) != 0) {
665         test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
666                                  s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
667                                  s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
668         return 0;
669     }
670     return 1;
671 }
672
673 int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
674                  const char *s1, const char *s2, size_t len)
675 {
676     if ((s1 == NULL) ^ (s2 == NULL))
677       return 1;
678     if (s1 == NULL || strncmp(s1, s2, len) == 0) {
679         test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
680                                  s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
681                                  s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
682         return 0;
683     }
684     return 1;
685 }
686
687 int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
688                 const void *s1, size_t n1, const void *s2, size_t n2)
689 {
690     if (s1 == NULL && s2 == NULL)
691         return 1;
692     if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
693         test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
694                                  s1, n1, s2, n2);
695         return 0;
696     }
697     return 1;
698 }
699
700 int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
701                 const void *s1, size_t n1, const void *s2, size_t n2)
702 {
703     if ((s1 == NULL) ^ (s2 == NULL))
704         return 1;
705     if (n1 != n2)
706         return 1;
707     if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
708         test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
709                                  s1, n1, s2, n2);
710         return 0;
711     }
712     return 1;
713 }
714
715 #define DEFINE_BN_COMPARISONS(opname, op, zero_cond)                    \
716     int test_BN_ ## opname(const char *file, int line,                  \
717                            const char *s1, const char *s2,              \
718                            const BIGNUM *t1, const BIGNUM *t2)          \
719     {                                                                   \
720         if (BN_cmp(t1, t2) op 0)                                        \
721             return 1;                                                   \
722         test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2,    \
723                                  #op, t1, t2);                          \
724         return 0;                                                       \
725     }                                                                   \
726     int test_BN_ ## opname ## _zero(const char *file, int line,         \
727                                     const char *s, const BIGNUM *a)     \
728     {                                                                   \
729         if (a != NULL &&(zero_cond))                                    \
730             return 1;                                                   \
731         test_fail_bignum_mono_message(NULL, file, line, "BIGNUM",       \
732                                       s, "0", #op, a);                  \
733         return 0;                                                       \
734     }
735
736 DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
737 DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
738 DEFINE_BN_COMPARISONS(gt, >,  !BN_is_negative(a) && !BN_is_zero(a))
739 DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
740 DEFINE_BN_COMPARISONS(lt, <,  BN_is_negative(a) && !BN_is_zero(a))
741 DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
742
743 int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
744 {
745     if (a != NULL && BN_is_one(a))
746         return 1;
747     test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
748     return 0;
749 }
750
751 int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
752 {
753     if (a != NULL && BN_is_odd(a))
754         return 1;
755     test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
756     return 0;
757 }
758
759 int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
760 {
761     if (a != NULL && !BN_is_odd(a))
762         return 1;
763     test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
764                                   a);
765     return 0;
766 }
767
768 int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
769                     const BIGNUM *a, BN_ULONG w)
770 {
771     BIGNUM *bw;
772
773     if (a != NULL && BN_is_word(a, w))
774         return 1;
775     bw = BN_new();
776     BN_set_word(bw, w);
777     test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
778     BN_free(bw);
779     return 0;
780 }
781
782 int test_BN_abs_eq_word(const char *file, int line, const char *bns,
783                         const char *ws, const BIGNUM *a, BN_ULONG w)
784 {
785     BIGNUM *bw, *aa;
786
787     if (a != NULL && BN_abs_is_word(a, w))
788         return 1;
789     bw = BN_new();
790     aa = BN_dup(a);
791     BN_set_negative(aa, 0);
792     BN_set_word(bw, w);
793     test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
794                              aa, bw);
795     BN_free(bw);
796     BN_free(aa);
797     return 0;
798 }