2016-05-18 02:20:24 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2014-06-20 00:27:54 +08:00
|
|
|
*
|
2016-05-18 02:20:24 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2014-06-20 00:27:54 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testutil.h"
|
|
|
|
|
2014-07-18 07:08:04 +08:00
|
|
|
#include <assert.h>
|
2014-06-20 00:27:54 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2016-07-05 02:16:14 +08:00
|
|
|
#include <string.h>
|
2015-05-04 00:32:05 +08:00
|
|
|
#include "e_os.h"
|
2014-06-20 00:27:54 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Declares the structures needed to register each test case function.
|
2014-06-20 00:27:54 +08:00
|
|
|
*/
|
2015-01-22 11:40:55 +08:00
|
|
|
typedef struct test_info {
|
|
|
|
const char *test_case_name;
|
|
|
|
int (*test_fn) ();
|
2016-03-17 22:14:30 +08:00
|
|
|
int (*param_test_fn)(int idx);
|
|
|
|
int num;
|
2015-01-22 11:40:55 +08:00
|
|
|
} TEST_INFO;
|
2014-06-20 00:27:54 +08:00
|
|
|
|
|
|
|
static TEST_INFO all_tests[1024];
|
|
|
|
static int num_tests = 0;
|
2016-03-17 22:14:30 +08:00
|
|
|
/*
|
|
|
|
* A parameterised tests runs a loop of test cases.
|
|
|
|
* |num_test_cases| counts the total number of test cases
|
|
|
|
* across all tests.
|
|
|
|
*/
|
|
|
|
static int num_test_cases = 0;
|
2014-06-20 00:27:54 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
void add_test(const char *test_case_name, int (*test_fn) ())
|
|
|
|
{
|
2015-05-04 00:32:05 +08:00
|
|
|
assert(num_tests != OSSL_NELEM(all_tests));
|
2015-01-22 11:40:55 +08:00
|
|
|
all_tests[num_tests].test_case_name = test_case_name;
|
|
|
|
all_tests[num_tests].test_fn = test_fn;
|
2016-03-17 22:14:30 +08:00
|
|
|
all_tests[num_tests].num = -1;
|
|
|
|
++num_test_cases;
|
|
|
|
++num_tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
|
|
|
|
int num)
|
|
|
|
{
|
|
|
|
assert(num_tests != OSSL_NELEM(all_tests));
|
|
|
|
all_tests[num_tests].test_case_name = test_case_name;
|
|
|
|
all_tests[num_tests].param_test_fn = test_fn;
|
|
|
|
all_tests[num_tests].num = num;
|
2015-01-22 11:40:55 +08:00
|
|
|
++num_tests;
|
2016-03-17 22:14:30 +08:00
|
|
|
num_test_cases += num;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2014-06-20 00:27:54 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
int run_tests(const char *test_prog_name)
|
|
|
|
{
|
|
|
|
int num_failed = 0;
|
2014-06-20 00:27:54 +08:00
|
|
|
|
2016-03-17 22:14:30 +08:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
printf("%s: %d test case%s\n", test_prog_name, num_test_cases,
|
|
|
|
num_test_cases == 1 ? "" : "s");
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (i = 0; i != num_tests; ++i) {
|
2016-03-17 22:14:30 +08:00
|
|
|
if (all_tests[i].num == -1) {
|
2016-04-05 20:29:06 +08:00
|
|
|
if (!all_tests[i].test_fn()) {
|
2016-03-17 22:14:30 +08:00
|
|
|
printf("** %s failed **\n--------\n",
|
|
|
|
all_tests[i].test_case_name);
|
|
|
|
++num_failed;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < all_tests[i].num; j++) {
|
2016-04-05 20:29:06 +08:00
|
|
|
if (!all_tests[i].param_test_fn(j)) {
|
2016-03-17 22:14:30 +08:00
|
|
|
printf("** %s failed test %d\n--------\n",
|
|
|
|
all_tests[i].test_case_name, j);
|
|
|
|
++num_failed;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2014-06-20 00:27:54 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (num_failed != 0) {
|
|
|
|
printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
|
2016-03-17 22:14:30 +08:00
|
|
|
num_failed, num_failed != 1 ? "s" : "", num_test_cases);
|
2015-01-22 11:40:55 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
printf(" All tests passed.\n");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2016-07-05 02:16:14 +08:00
|
|
|
|
|
|
|
static const char *print_string_maybe_null(const char *s)
|
|
|
|
{
|
|
|
|
return s == NULL ? "(NULL)" : s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int strings_equal(const char *desc, const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
if (s1 == NULL && s2 == NULL)
|
|
|
|
return 1;
|
|
|
|
if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
|
|
|
|
fprintf(stderr, "%s mismatch: %s vs %s\n", desc, print_string_maybe_null(s1),
|
|
|
|
print_string_maybe_null(s2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|