Blame test/testutil/tests.c

Packit c4476c
/*
Packit c4476c
 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
#include "../testutil.h"
Packit c4476c
#include "output.h"
Packit c4476c
#include "tu_local.h"
Packit c4476c
Packit c4476c
#include <errno.h>
Packit c4476c
#include <string.h>
Packit c4476c
#include <ctype.h>
Packit c4476c
#include "internal/nelem.h"
Packit c4476c
#include <openssl/asn1.h>
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Output a failed test first line.
Packit c4476c
 * All items are optional are generally not preinted if passed as NULL.
Packit c4476c
 * The special cases are for prefix where "ERROR" is assumed and for left
Packit c4476c
 * and right where a non-failure message is produced if either is NULL.
Packit c4476c
 */
Packit c4476c
void test_fail_message_prefix(const char *prefix, const char *file,
Packit c4476c
                              int line, const char *type,
Packit c4476c
                              const char *left, const char *right,
Packit c4476c
                              const char *op)
Packit c4476c
{
Packit c4476c
    test_printf_stderr("%s: ", prefix != NULL ? prefix : "ERROR");
Packit c4476c
    if (type)
Packit c4476c
        test_printf_stderr("(%s) ", type);
Packit c4476c
    if (op != NULL) {
Packit c4476c
        if (left != NULL && right != NULL)
Packit c4476c
            test_printf_stderr("'%s %s %s' failed", left, op, right);
Packit c4476c
        else
Packit c4476c
            test_printf_stderr("'%s'", op);
Packit c4476c
    }
Packit c4476c
    if (file != NULL) {
Packit c4476c
        test_printf_stderr(" @ %s:%d", file, line);
Packit c4476c
    }
Packit c4476c
    test_printf_stderr("\n");
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * A common routine to output test failure messages.  Generally this should not
Packit c4476c
 * be called directly, rather it should be called by the following functions.
Packit c4476c
 *
Packit c4476c
 * |desc| is a printf formatted description with arguments |args| that is
Packit c4476c
 * supplied by the user and |desc| can be NULL.  |type| is the data type
Packit c4476c
 * that was tested (int, char, ptr, ...).  |fmt| is a system provided
Packit c4476c
 * printf format with following arguments that spell out the failure
Packit c4476c
 * details i.e. the actual values compared and the operator used.
Packit c4476c
 *
Packit c4476c
 * The typical use for this is from an utility test function:
Packit c4476c
 *
Packit c4476c
 * int test6(const char *file, int line, int n) {
Packit c4476c
 *     if (n != 6) {
Packit c4476c
 *         test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
Packit c4476c
 *         return 0;
Packit c4476c
 *     }
Packit c4476c
 *     return 1;
Packit c4476c
 * }
Packit c4476c
 *
Packit c4476c
 * calling test6(3, "oops") will return 0 and produce out along the lines of:
Packit c4476c
 *      FAIL oops: (int) value 3 is not 6\n
Packit c4476c
 */
Packit c4476c
static void test_fail_message(const char *prefix, const char *file, int line,
Packit c4476c
                              const char *type, const char *left,
Packit c4476c
                              const char *right, const char *op,
Packit c4476c
                              const char *fmt, ...)
Packit c4476c
            PRINTF_FORMAT(8, 9);
Packit c4476c
Packit c4476c
static void test_fail_message_va(const char *prefix, const char *file,
Packit c4476c
                                 int line, const char *type,
Packit c4476c
                                 const char *left, const char *right,
Packit c4476c
                                 const char *op, const char *fmt, va_list ap)
Packit c4476c
{
Packit c4476c
    test_fail_message_prefix(prefix, file, line, type, left, right, op);
Packit c4476c
    if (fmt != NULL) {
Packit c4476c
        test_vprintf_stderr(fmt, ap);
Packit c4476c
        test_printf_stderr("\n");
Packit c4476c
    }
Packit c4476c
    test_flush_stderr();
Packit c4476c
}
Packit c4476c
Packit c4476c
static void test_fail_message(const char *prefix, const char *file,
Packit c4476c
                              int line, const char *type,
Packit c4476c
                              const char *left, const char *right,
Packit c4476c
                              const char *op, const char *fmt, ...)
Packit c4476c
{
Packit c4476c
    va_list ap;
Packit c4476c
Packit c4476c
    va_start(ap, fmt);
Packit c4476c
    test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap);
Packit c4476c
    va_end(ap);
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_info_c90(const char *desc, ...)
Packit c4476c
{
Packit c4476c
    va_list ap;
Packit c4476c
Packit c4476c
    va_start(ap, desc);
Packit c4476c
    test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
Packit c4476c
    va_end(ap);
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_info(const char *file, int line, const char *desc, ...)
Packit c4476c
{
Packit c4476c
    va_list ap;
Packit c4476c
Packit c4476c
    va_start(ap, desc);
Packit c4476c
    test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap);
Packit c4476c
    va_end(ap);
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_error_c90(const char *desc, ...)
Packit c4476c
{
Packit c4476c
    va_list ap;
Packit c4476c
Packit c4476c
    va_start(ap, desc);
Packit c4476c
    test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
Packit c4476c
    va_end(ap);
Packit c4476c
    test_printf_stderr("\n");
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_error(const char *file, int line, const char *desc, ...)
Packit c4476c
{
Packit c4476c
    va_list ap;
Packit c4476c
Packit c4476c
    va_start(ap, desc);
Packit c4476c
    test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap);
Packit c4476c
    va_end(ap);
Packit c4476c
    test_printf_stderr("\n");
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_perror(const char *s)
Packit c4476c
{
Packit c4476c
    /*
Packit c4476c
     * Using openssl_strerror_r causes linking issues since it isn't
Packit c4476c
     * exported from libcrypto.so
Packit c4476c
     */
Packit c4476c
    TEST_error("%s: %s", s, strerror(errno));
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_note(const char *fmt, ...)
Packit c4476c
{
Packit c4476c
    if (fmt != NULL) {
Packit c4476c
        va_list ap;
Packit c4476c
Packit c4476c
        va_start(ap, fmt);
Packit c4476c
        test_vprintf_stderr(fmt, ap);
Packit c4476c
        va_end(ap);
Packit c4476c
        test_printf_stderr("\n");
Packit c4476c
    }
Packit c4476c
    test_flush_stderr();
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_openssl_errors(void)
Packit c4476c
{
Packit c4476c
    ERR_print_errors_cb(openssl_error_cb, NULL);
Packit c4476c
    ERR_clear_error();
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Define some comparisons between pairs of various types.
Packit c4476c
 * These functions return 1 if the test is true.
Packit c4476c
 * Otherwise, they return 0 and pretty-print diagnostics.
Packit c4476c
 *
Packit c4476c
 * In each case the functions produced are:
Packit c4476c
 *  int test_name_eq(const type t1, const type t2, const char *desc, ...);
Packit c4476c
 *  int test_name_ne(const type t1, const type t2, const char *desc, ...);
Packit c4476c
 *  int test_name_lt(const type t1, const type t2, const char *desc, ...);
Packit c4476c
 *  int test_name_le(const type t1, const type t2, const char *desc, ...);
Packit c4476c
 *  int test_name_gt(const type t1, const type t2, const char *desc, ...);
Packit c4476c
 *  int test_name_ge(const type t1, const type t2, const char *desc, ...);
Packit c4476c
 *
Packit c4476c
 * The t1 and t2 arguments are to be compared for equality, inequality,
Packit c4476c
 * less than, less than or equal to, greater than and greater than or
Packit c4476c
 * equal to respectively.  If the specified condition holds, the functions
Packit c4476c
 * return 1.  If the condition does not hold, the functions print a diagnostic
Packit c4476c
 * message and return 0.
Packit c4476c
 *
Packit c4476c
 * The desc argument is a printf format string followed by its arguments and
Packit c4476c
 * this is included in the output if the condition being tested for is false.
Packit c4476c
 */
Packit c4476c
#define DEFINE_COMPARISON(type, name, opname, op, fmt)                  \
Packit c4476c
    int test_ ## name ## _ ## opname(const char *file, int line,        \
Packit c4476c
                                     const char *s1, const char *s2,    \
Packit c4476c
                                     const type t1, const type t2)      \
Packit c4476c
    {                                                                   \
Packit c4476c
        if (t1 op t2)                                                   \
Packit c4476c
            return 1;                                                   \
Packit c4476c
        test_fail_message(NULL, file, line, #type, s1, s2, #op,         \
Packit c4476c
                          "[" fmt "] compared to [" fmt "]",            \
Packit c4476c
                          t1, t2);                                      \
Packit c4476c
        return 0;                                                       \
Packit c4476c
    }
Packit c4476c
Packit c4476c
#define DEFINE_COMPARISONS(type, name, fmt)                             \
Packit c4476c
    DEFINE_COMPARISON(type, name, eq, ==, fmt)                          \
Packit c4476c
    DEFINE_COMPARISON(type, name, ne, !=, fmt)                          \
Packit c4476c
    DEFINE_COMPARISON(type, name, lt, <, fmt)                           \
Packit c4476c
    DEFINE_COMPARISON(type, name, le, <=, fmt)                          \
Packit c4476c
    DEFINE_COMPARISON(type, name, gt, >, fmt)                           \
Packit c4476c
    DEFINE_COMPARISON(type, name, ge, >=, fmt)
Packit c4476c
Packit c4476c
DEFINE_COMPARISONS(int, int, "%d")
Packit c4476c
DEFINE_COMPARISONS(unsigned int, uint, "%u")
Packit c4476c
DEFINE_COMPARISONS(char, char, "%c")
Packit c4476c
DEFINE_COMPARISONS(unsigned char, uchar, "%u")
Packit c4476c
DEFINE_COMPARISONS(long, long, "%ld")
Packit c4476c
DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
Packit c4476c
DEFINE_COMPARISONS(size_t, size_t, "%zu")
Packit c4476c
Packit c4476c
DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
Packit c4476c
DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
Packit c4476c
Packit c4476c
int test_ptr_null(const char *file, int line, const char *s, const void *p)
Packit c4476c
{
Packit c4476c
    if (p == NULL)
Packit c4476c
        return 1;
Packit c4476c
    test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_ptr(const char *file, int line, const char *s, const void *p)
Packit c4476c
{
Packit c4476c
    if (p != NULL)
Packit c4476c
        return 1;
Packit c4476c
    test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_true(const char *file, int line, const char *s, int b)
Packit c4476c
{
Packit c4476c
    if (b)
Packit c4476c
        return 1;
Packit c4476c
    test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_false(const char *file, int line, const char *s, int b)
Packit c4476c
{
Packit c4476c
    if (!b)
Packit c4476c
        return 1;
Packit c4476c
    test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_str_eq(const char *file, int line, const char *st1, const char *st2,
Packit c4476c
                const char *s1, const char *s2)
Packit c4476c
{
Packit c4476c
    if (s1 == NULL && s2 == NULL)
Packit c4476c
      return 1;
Packit c4476c
    if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
Packit c4476c
        test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
Packit c4476c
                                 s1, s1 == NULL ? 0 : strlen(s1),
Packit c4476c
                                 s2, s2 == NULL ? 0 : strlen(s2));
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_str_ne(const char *file, int line, const char *st1, const char *st2,
Packit c4476c
                const char *s1, const char *s2)
Packit c4476c
{
Packit c4476c
    if ((s1 == NULL) ^ (s2 == NULL))
Packit c4476c
      return 1;
Packit c4476c
    if (s1 == NULL || strcmp(s1, s2) == 0) {
Packit c4476c
        test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
Packit c4476c
                                 s1, s1 == NULL ? 0 : strlen(s1),
Packit c4476c
                                 s2, s2 == NULL ? 0 : strlen(s2));
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
Packit c4476c
                 const char *s1, const char *s2, size_t len)
Packit c4476c
{
Packit c4476c
    if (s1 == NULL && s2 == NULL)
Packit c4476c
      return 1;
Packit c4476c
    if (s1 == NULL || s2 == NULL || strncmp(s1, s2, len) != 0) {
Packit c4476c
        test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
Packit c4476c
                                 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
Packit c4476c
                                 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
Packit c4476c
                 const char *s1, const char *s2, size_t len)
Packit c4476c
{
Packit c4476c
    if ((s1 == NULL) ^ (s2 == NULL))
Packit c4476c
      return 1;
Packit c4476c
    if (s1 == NULL || strncmp(s1, s2, len) == 0) {
Packit c4476c
        test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
Packit c4476c
                                 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
Packit c4476c
                                 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
Packit c4476c
                const void *s1, size_t n1, const void *s2, size_t n2)
Packit c4476c
{
Packit c4476c
    if (s1 == NULL && s2 == NULL)
Packit c4476c
        return 1;
Packit c4476c
    if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
Packit c4476c
        test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
Packit c4476c
                                 s1, n1, s2, n2);
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
Packit c4476c
                const void *s1, size_t n1, const void *s2, size_t n2)
Packit c4476c
{
Packit c4476c
    if ((s1 == NULL) ^ (s2 == NULL))
Packit c4476c
        return 1;
Packit c4476c
    if (n1 != n2)
Packit c4476c
        return 1;
Packit c4476c
    if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
Packit c4476c
        test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
Packit c4476c
                                 s1, n1, s2, n2);
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
#define DEFINE_BN_COMPARISONS(opname, op, zero_cond)                    \
Packit c4476c
    int test_BN_ ## opname(const char *file, int line,                  \
Packit c4476c
                           const char *s1, const char *s2,              \
Packit c4476c
                           const BIGNUM *t1, const BIGNUM *t2)          \
Packit c4476c
    {                                                                   \
Packit c4476c
        if (BN_cmp(t1, t2) op 0)                                        \
Packit c4476c
            return 1;                                                   \
Packit c4476c
        test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2,    \
Packit c4476c
                                 #op, t1, t2);                          \
Packit c4476c
        return 0;                                                       \
Packit c4476c
    }                                                                   \
Packit c4476c
    int test_BN_ ## opname ## _zero(const char *file, int line,         \
Packit c4476c
                                    const char *s, const BIGNUM *a)     \
Packit c4476c
    {                                                                   \
Packit c4476c
        if (a != NULL &&(zero_cond))                                    \
Packit c4476c
            return 1;                                                   \
Packit c4476c
        test_fail_bignum_mono_message(NULL, file, line, "BIGNUM",       \
Packit c4476c
                                      s, "0", #op, a);                  \
Packit c4476c
        return 0;                                                       \
Packit c4476c
    }
Packit c4476c
Packit c4476c
DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
Packit c4476c
DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
Packit c4476c
DEFINE_BN_COMPARISONS(gt, >,  !BN_is_negative(a) && !BN_is_zero(a))
Packit c4476c
DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
Packit c4476c
DEFINE_BN_COMPARISONS(lt, <,  BN_is_negative(a) && !BN_is_zero(a))
Packit c4476c
DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
Packit c4476c
Packit c4476c
int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
Packit c4476c
{
Packit c4476c
    if (a != NULL && BN_is_one(a))
Packit c4476c
        return 1;
Packit c4476c
    test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
Packit c4476c
{
Packit c4476c
    if (a != NULL && BN_is_odd(a))
Packit c4476c
        return 1;
Packit c4476c
    test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
Packit c4476c
{
Packit c4476c
    if (a != NULL && !BN_is_odd(a))
Packit c4476c
        return 1;
Packit c4476c
    test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
Packit c4476c
                                  a);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
Packit c4476c
                    const BIGNUM *a, BN_ULONG w)
Packit c4476c
{
Packit c4476c
    BIGNUM *bw;
Packit c4476c
Packit c4476c
    if (a != NULL && BN_is_word(a, w))
Packit c4476c
        return 1;
Packit c4476c
    bw = BN_new();
Packit c4476c
    BN_set_word(bw, w);
Packit c4476c
    test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
Packit c4476c
    BN_free(bw);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
int test_BN_abs_eq_word(const char *file, int line, const char *bns,
Packit c4476c
                        const char *ws, const BIGNUM *a, BN_ULONG w)
Packit c4476c
{
Packit c4476c
    BIGNUM *bw, *aa;
Packit c4476c
Packit c4476c
    if (a != NULL && BN_abs_is_word(a, w))
Packit c4476c
        return 1;
Packit c4476c
    bw = BN_new();
Packit c4476c
    aa = BN_dup(a);
Packit c4476c
    BN_set_negative(aa, 0);
Packit c4476c
    BN_set_word(bw, w);
Packit c4476c
    test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
Packit c4476c
                             aa, bw);
Packit c4476c
    BN_free(bw);
Packit c4476c
    BN_free(aa);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
static const char *print_time(const ASN1_TIME *t)
Packit c4476c
{
Packit c4476c
    return t == NULL ? "<null>" : (char *)ASN1_STRING_get0_data(t);
Packit c4476c
}
Packit c4476c
Packit c4476c
#define DEFINE_TIME_T_COMPARISON(opname, op)                            \
Packit c4476c
    int test_time_t_ ## opname(const char *file, int line,              \
Packit c4476c
                               const char *s1, const char *s2,          \
Packit c4476c
                               const time_t t1, const time_t t2)        \
Packit c4476c
    {                                                                   \
Packit c4476c
        ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1);                       \
Packit c4476c
        ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2);                       \
Packit c4476c
        int r = at1 != NULL && at2 != NULL                              \
Packit c4476c
                && ASN1_TIME_compare(at1, at2) op 0;                    \
Packit c4476c
        if (!r)                                                         \
Packit c4476c
            test_fail_message(NULL, file, line, "time_t", s1, s2, #op,  \
Packit c4476c
                              "[%s] compared to [%s]",                  \
Packit c4476c
                              print_time(at1), print_time(at2));        \
Packit c4476c
        ASN1_STRING_free(at1);                                          \
Packit c4476c
        ASN1_STRING_free(at2);                                          \
Packit c4476c
        return r;                                                       \
Packit c4476c
    }
Packit c4476c
DEFINE_TIME_T_COMPARISON(eq, ==)
Packit c4476c
DEFINE_TIME_T_COMPARISON(ne, !=)
Packit c4476c
DEFINE_TIME_T_COMPARISON(gt, >)
Packit c4476c
DEFINE_TIME_T_COMPARISON(ge, >=)
Packit c4476c
DEFINE_TIME_T_COMPARISON(lt, <)
Packit c4476c
DEFINE_TIME_T_COMPARISON(le, <=)