Blame test/testutil/driver.c

Packit c4476c
/*
Packit c4476c
 * Copyright 2016-2019 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 <string.h>
Packit c4476c
#include <assert.h>
Packit c4476c
Packit c4476c
#include "internal/nelem.h"
Packit c4476c
#include <openssl/bio.h>
Packit c4476c
Packit c4476c
#ifdef _WIN32
Packit c4476c
# define strdup _strdup
Packit c4476c
#endif
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Declares the structures needed to register each test case function.
Packit c4476c
 */
Packit c4476c
typedef struct test_info {
Packit c4476c
    const char *test_case_name;
Packit c4476c
    int (*test_fn) (void);
Packit c4476c
    int (*param_test_fn)(int idx);
Packit c4476c
    int num;
Packit c4476c
Packit c4476c
    /* flags */
Packit c4476c
    int subtest:1;
Packit c4476c
} TEST_INFO;
Packit c4476c
Packit c4476c
static TEST_INFO all_tests[1024];
Packit c4476c
static int num_tests = 0;
Packit c4476c
static int seed = 0;
Packit c4476c
/*
Packit c4476c
 * A parameterised tests runs a loop of test cases.
Packit c4476c
 * |num_test_cases| counts the total number of test cases
Packit c4476c
 * across all tests.
Packit c4476c
 */
Packit c4476c
static int num_test_cases = 0;
Packit c4476c
Packit c4476c
void add_test(const char *test_case_name, int (*test_fn) (void))
Packit c4476c
{
Packit c4476c
    assert(num_tests != OSSL_NELEM(all_tests));
Packit c4476c
    all_tests[num_tests].test_case_name = test_case_name;
Packit c4476c
    all_tests[num_tests].test_fn = test_fn;
Packit c4476c
    all_tests[num_tests].num = -1;
Packit c4476c
    ++num_tests;
Packit c4476c
    ++num_test_cases;
Packit c4476c
}
Packit c4476c
Packit c4476c
void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
Packit c4476c
                   int num, int subtest)
Packit c4476c
{
Packit c4476c
    assert(num_tests != OSSL_NELEM(all_tests));
Packit c4476c
    all_tests[num_tests].test_case_name = test_case_name;
Packit c4476c
    all_tests[num_tests].param_test_fn = test_fn;
Packit c4476c
    all_tests[num_tests].num = num;
Packit c4476c
    all_tests[num_tests].subtest = subtest;
Packit c4476c
    ++num_tests;
Packit c4476c
    num_test_cases += num;
Packit c4476c
}
Packit c4476c
Packit c4476c
static int level = 0;
Packit c4476c
Packit c4476c
int subtest_level(void)
Packit c4476c
{
Packit c4476c
    return level;
Packit c4476c
}
Packit c4476c
Packit c4476c
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Packit c4476c
static int should_report_leaks(void)
Packit c4476c
{
Packit c4476c
    /*
Packit c4476c
     * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
Packit c4476c
     * can be used to disable leak checking at runtime.
Packit c4476c
     * Note this only works when running the test binary manually;
Packit c4476c
     * the test harness always enables OPENSSL_DEBUG_MEMORY.
Packit c4476c
     */
Packit c4476c
    char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
Packit c4476c
Packit c4476c
    return mem_debug_env == NULL
Packit c4476c
        || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
Packit c4476c
}
Packit c4476c
#endif
Packit c4476c
Packit c4476c
static int gcd(int a, int b)
Packit c4476c
{
Packit c4476c
    while (b != 0) {
Packit c4476c
        int t = b;
Packit c4476c
        b = a % b;
Packit c4476c
        a = t;
Packit c4476c
    }
Packit c4476c
    return a;
Packit c4476c
}
Packit c4476c
Packit c4476c
void setup_test_framework()
Packit c4476c
{
Packit c4476c
    char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
Packit c4476c
    char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
Packit c4476c
Packit c4476c
    level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
Packit c4476c
Packit c4476c
    if (test_seed != NULL) {
Packit c4476c
        seed = atoi(test_seed);
Packit c4476c
        if (seed <= 0)
Packit c4476c
            seed = (int)time(NULL);
Packit c4476c
        test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed);
Packit c4476c
        test_flush_stdout();
Packit c4476c
        test_random_seed(seed);
Packit c4476c
    }
Packit c4476c
Packit c4476c
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Packit c4476c
    if (should_report_leaks()) {
Packit c4476c
        CRYPTO_set_mem_debug(1);
Packit c4476c
        CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
Packit c4476c
    }
Packit c4476c
#endif
Packit c4476c
}
Packit c4476c
Packit c4476c
int pulldown_test_framework(int ret)
Packit c4476c
{
Packit c4476c
    set_test_title(NULL);
Packit c4476c
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Packit c4476c
    if (should_report_leaks()
Packit c4476c
        && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0)
Packit c4476c
        return EXIT_FAILURE;
Packit c4476c
#endif
Packit c4476c
Packit c4476c
    return ret;
Packit c4476c
}
Packit c4476c
Packit c4476c
static void finalize(int success)
Packit c4476c
{
Packit c4476c
    if (success)
Packit c4476c
        ERR_clear_error();
Packit c4476c
    else
Packit c4476c
        ERR_print_errors_cb(openssl_error_cb, NULL);
Packit c4476c
}
Packit c4476c
Packit c4476c
static char *test_title = NULL;
Packit c4476c
Packit c4476c
void set_test_title(const char *title)
Packit c4476c
{
Packit c4476c
    free(test_title);
Packit c4476c
    test_title = title == NULL ? NULL : strdup(title);
Packit c4476c
}
Packit c4476c
Packit c4476c
PRINTF_FORMAT(2, 3) static void test_verdict(int pass, const char *extra, ...)
Packit c4476c
{
Packit c4476c
    va_list ap;
Packit c4476c
Packit c4476c
    test_flush_stdout();
Packit c4476c
    test_flush_stderr();
Packit c4476c
Packit c4476c
    test_printf_stdout("%*s%s", level, "", pass ? "ok" : "not ok");
Packit c4476c
    if (extra != NULL) {
Packit c4476c
        test_printf_stdout(" ");
Packit c4476c
        va_start(ap, extra);
Packit c4476c
        test_vprintf_stdout(extra, ap);
Packit c4476c
        va_end(ap);
Packit c4476c
    }
Packit c4476c
    test_printf_stdout("\n");
Packit c4476c
    test_flush_stdout();
Packit c4476c
}
Packit c4476c
Packit c4476c
int run_tests(const char *test_prog_name)
Packit c4476c
{
Packit c4476c
    int num_failed = 0;
Packit c4476c
    int verdict = 1;
Packit c4476c
    int ii, i, jj, j, jstep;
Packit c4476c
    int permute[OSSL_NELEM(all_tests)];
Packit c4476c
Packit c4476c
    if (num_tests < 1) {
Packit c4476c
        test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
Packit c4476c
                           test_prog_name);
Packit c4476c
    } else {
Packit c4476c
        if (level > 0)
Packit c4476c
            test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
Packit c4476c
        test_printf_stdout("%*s1..%d\n", level, "", num_tests);
Packit c4476c
    }
Packit c4476c
    test_flush_stdout();
Packit c4476c
Packit c4476c
    for (i = 0; i < num_tests; i++)
Packit c4476c
        permute[i] = i;
Packit c4476c
    if (seed != 0)
Packit c4476c
        for (i = num_tests - 1; i >= 1; i--) {
Packit c4476c
            j = test_random() % (1 + i);
Packit c4476c
            ii = permute[j];
Packit c4476c
            permute[j] = permute[i];
Packit c4476c
            permute[i] = ii;
Packit c4476c
        }
Packit c4476c
Packit c4476c
    for (ii = 0; ii != num_tests; ++ii) {
Packit c4476c
        i = permute[ii];
Packit c4476c
        if (all_tests[i].num == -1) {
Packit c4476c
            int ret = 0;
Packit c4476c
Packit c4476c
            set_test_title(all_tests[i].test_case_name);
Packit c4476c
            ret = all_tests[i].test_fn();
Packit c4476c
Packit c4476c
            verdict = 1;
Packit c4476c
            if (!ret) {
Packit c4476c
                verdict = 0;
Packit c4476c
                ++num_failed;
Packit c4476c
            }
Packit c4476c
            test_verdict(verdict, "%d - %s", ii + 1, test_title);
Packit c4476c
            finalize(ret);
Packit c4476c
        } else {
Packit c4476c
            int num_failed_inner = 0;
Packit c4476c
Packit c4476c
            level += 4;
Packit c4476c
            if (all_tests[i].subtest) {
Packit c4476c
                test_printf_stdout("%*s# Subtest: %s\n", level, "",
Packit c4476c
                                   all_tests[i].test_case_name);
Packit c4476c
                test_printf_stdout("%*s%d..%d\n", level, "", 1,
Packit c4476c
                                   all_tests[i].num);
Packit c4476c
                test_flush_stdout();
Packit c4476c
            }
Packit c4476c
Packit c4476c
            j = -1;
Packit c4476c
            if (seed == 0 || all_tests[i].num < 3)
Packit c4476c
                jstep = 1;
Packit c4476c
            else
Packit c4476c
                do
Packit c4476c
                    jstep = test_random() % all_tests[i].num;
Packit c4476c
                while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
Packit c4476c
Packit c4476c
            for (jj = 0; jj < all_tests[i].num; jj++) {
Packit c4476c
                int ret;
Packit c4476c
Packit c4476c
                j = (j + jstep) % all_tests[i].num;
Packit c4476c
                set_test_title(NULL);
Packit c4476c
                ret = all_tests[i].param_test_fn(j);
Packit c4476c
Packit c4476c
                if (!ret)
Packit c4476c
                    ++num_failed_inner;
Packit c4476c
Packit c4476c
                finalize(ret);
Packit c4476c
Packit c4476c
                if (all_tests[i].subtest) {
Packit c4476c
                    verdict = 1;
Packit c4476c
                    if (!ret) {
Packit c4476c
                        verdict = 0;
Packit c4476c
                        ++num_failed_inner;
Packit c4476c
                    }
Packit c4476c
                    if (test_title != NULL)
Packit c4476c
                        test_verdict(verdict, "%d - %s", jj + 1, test_title);
Packit c4476c
                    else
Packit c4476c
                        test_verdict(verdict, "%d - iteration %d",
Packit c4476c
                                     jj + 1, j + 1);
Packit c4476c
                }
Packit c4476c
            }
Packit c4476c
Packit c4476c
            level -= 4;
Packit c4476c
            verdict = 1;
Packit c4476c
            if (num_failed_inner) {
Packit c4476c
                verdict = 0;
Packit c4476c
                ++num_failed;
Packit c4476c
            }
Packit c4476c
            test_verdict(verdict, "%d - %s", ii + 1,
Packit c4476c
                         all_tests[i].test_case_name);
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
    if (num_failed != 0)
Packit c4476c
        return EXIT_FAILURE;
Packit c4476c
    return EXIT_SUCCESS;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Glue an array of strings together and return it as an allocated string.
Packit c4476c
 * Optionally return the whole length of this string in |out_len|
Packit c4476c
 */
Packit c4476c
char *glue_strings(const char *list[], size_t *out_len)
Packit c4476c
{
Packit c4476c
    size_t len = 0;
Packit c4476c
    char *p, *ret;
Packit c4476c
    int i;
Packit c4476c
Packit c4476c
    for (i = 0; list[i] != NULL; i++)
Packit c4476c
        len += strlen(list[i]);
Packit c4476c
Packit c4476c
    if (out_len != NULL)
Packit c4476c
        *out_len = len;
Packit c4476c
Packit c4476c
    if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1)))
Packit c4476c
        return NULL;
Packit c4476c
Packit c4476c
    for (i = 0; list[i] != NULL; i++)
Packit c4476c
        p += strlen(strcpy(p, list[i]));
Packit c4476c
Packit c4476c
    return ret;
Packit c4476c
}
Packit c4476c
Packit c4476c
char *test_mk_file_path(const char *dir, const char *file)
Packit c4476c
{
Packit c4476c
# ifndef OPENSSL_SYS_VMS
Packit c4476c
    const char *sep = "/";
Packit c4476c
# else
Packit c4476c
    const char *sep = "";
Packit c4476c
# endif
Packit c4476c
    size_t len = strlen(dir) + strlen(sep) + strlen(file) + 1;
Packit c4476c
    char *full_file = OPENSSL_zalloc(len);
Packit c4476c
Packit c4476c
    if (full_file != NULL) {
Packit c4476c
        OPENSSL_strlcpy(full_file, dir, len);
Packit c4476c
        OPENSSL_strlcat(full_file, sep, len);
Packit c4476c
        OPENSSL_strlcat(full_file, file, len);
Packit c4476c
    }
Packit c4476c
Packit c4476c
    return full_file;
Packit c4476c
}