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