X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=test%2Fbntest.c;h=da07446adb1034dfb367ae9a82fce4cb532c23ac;hp=ccedd7a7ec93c9e6c3a9b9716b8b50260767bef3;hb=9ef73a6fd9a43a79783d2c68339c96532c3209f9;hpb=281313e511381f3734f03233f8189d52771b2f6c diff --git a/test/bntest.c b/test/bntest.c index ccedd7a7ec..da07446adb 100644 --- a/test/bntest.c +++ b/test/bntest.c @@ -1,5 +1,5 @@ /* - * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the OpenSSL license (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -12,7 +12,8 @@ #include #include -#include "e_os.h" +#include +#include "../e_os.h" #include #include #include @@ -35,26 +36,12 @@ BIGNUM *bn_expand2(BIGNUM *b, int words); BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; } #include "../crypto/bn/bn_lcl.h" -#define MAXPAIRS 20 - /* * Things in boring, not in openssl. TODO we should add them. */ #define HAVE_BN_PADDED 0 #define HAVE_BN_SQRT 0 -typedef struct pair_st { - char *key; - char *value; -} PAIR; - -typedef struct stanza_st { - int curr; /* Current line in file */ - int start; /* Line where test starts */ - int numpairs; - PAIR pairs[MAXPAIRS]; -} STANZA; - typedef struct filetest_st { const char *name; int (*func)(STANZA *s); @@ -68,15 +55,15 @@ typedef struct mpitest_st { static const int NUM0 = 100; /* number of tests */ static const int NUM1 = 50; /* additional tests for some functions */ -static BIO *fp; static BN_CTX *ctx; /* * Polynomial coefficients used in GFM tests. */ +#ifndef OPENSSL_NO_EC2M static int p0[] = { 163, 7, 6, 3, 0, -1 }; static int p1[] = { 193, 15, 0, -1 }; - +#endif /* * Look for |key| in the stanza and return it or NULL if not found. @@ -113,7 +100,7 @@ static BIGNUM *getBN(STANZA *s, const char *attribute) BIGNUM *ret = NULL; if ((hex = findattr(s, attribute)) == NULL) { - TEST_error("Can't find %s in test at line %d", attribute, s->start); + TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute); return NULL; } @@ -164,7 +151,7 @@ static int rand_neg(void) } -static int test_sub() +static int test_sub(void) { BIGNUM *a = NULL, *b = NULL, *c = NULL; int i, st = 0; @@ -201,7 +188,7 @@ err: } -static int test_div_recip() +static int test_div_recip(void) { BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; BN_RECP_CTX *recp = NULL; @@ -245,7 +232,7 @@ err: } -static int test_mod() +static int test_mod(void) { BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; int st = 0, i; @@ -337,7 +324,7 @@ static char *glue(const char *list[]) * Test constant-time modular exponentiation with 1024-bit inputs, which on * x86_64 cause a different code branch to be taken. */ -static int test_modexp_mont5() +static int test_modexp_mont5(void) { BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL; BIGNUM *b = NULL, *n = NULL, *c = NULL; @@ -445,7 +432,7 @@ err: } #ifndef OPENSSL_NO_EC2M -static int test_gf2m_add() +static int test_gf2m_add(void) { BIGNUM *a = NULL, *b = NULL, *c = NULL; int i, st = 0; @@ -478,7 +465,7 @@ static int test_gf2m_add() return st; } -static int test_gf2m_mod() +static int test_gf2m_mod(void) { BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL; int i, j, st = 0; @@ -516,7 +503,7 @@ static int test_gf2m_mod() return st; } -static int test_gf2m_mul() +static int test_gf2m_mul(void) { BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL; BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL; @@ -567,7 +554,7 @@ static int test_gf2m_mul() return st; } -static int test_gf2m_sqr() +static int test_gf2m_sqr(void) { BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; int i, j, st = 0; @@ -604,7 +591,7 @@ static int test_gf2m_sqr() return st; } -static int test_gf2m_modinv() +static int test_gf2m_modinv(void) { BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; int i, j, st = 0; @@ -639,7 +626,7 @@ static int test_gf2m_modinv() return st; } -static int test_gf2m_moddiv() +static int test_gf2m_moddiv(void) { BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; BIGNUM *e = NULL, *f = NULL; @@ -681,7 +668,7 @@ static int test_gf2m_moddiv() return st; } -static int test_gf2m_modexp() +static int test_gf2m_modexp(void) { BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; BIGNUM *e = NULL, *f = NULL; @@ -727,7 +714,7 @@ static int test_gf2m_modexp() return st; } -static int test_gf2m_modsqrt() +static int test_gf2m_modsqrt(void) { BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; BIGNUM *e = NULL, *f = NULL; @@ -769,7 +756,7 @@ static int test_gf2m_modsqrt() return st; } -static int test_gf2m_modsolvequad() +static int test_gf2m_modsolvequad(void) { BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; BIGNUM *e = NULL; @@ -820,7 +807,7 @@ static int test_gf2m_modsolvequad() } #endif -static int test_kronecker() +static int test_kronecker(void) { BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL; int i, legendre, kronecker, st = 0; @@ -1382,9 +1369,9 @@ static int file_modexp(STANZA *s) "0000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000001"); - BN_mod_exp(d, a, b, c, ctx); - BN_mul(e, a, a, ctx); - if (!TEST_BN_eq(d, e)) + if (!TEST_true(BN_mod_exp(d, a, b, c, ctx)) + || !TEST_true(BN_mul(e, a, a, ctx)) + || !TEST_BN_eq(d, e)) goto err; st = 1; @@ -1456,7 +1443,7 @@ err: return st; } -static int test_bn2padded() +static int test_bn2padded(void) { #if HAVE_BN_PADDED uint8_t zeros[256], out[256], reference[128]; @@ -1517,7 +1504,7 @@ err: #endif } -static int test_dec2bn() +static int test_dec2bn(void) { BIGNUM *bn = NULL; int st = 0; @@ -1586,7 +1573,7 @@ err: return st; } -static int test_hex2bn() +static int test_hex2bn(void) { BIGNUM *bn = NULL; int st = 0; @@ -1652,7 +1639,7 @@ err: return st; } -static int test_asc2bn() +static int test_asc2bn(void) { BIGNUM *bn = NULL; int st = 0; @@ -1750,7 +1737,7 @@ err: return st; } -static int test_rand() +static int test_rand(void) { BIGNUM *bn = NULL; int st = 0; @@ -1776,7 +1763,7 @@ err: return st; } -static int test_negzero() +static int test_negzero(void) { BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL; BIGNUM *numerator = NULL, *denominator = NULL; @@ -1845,7 +1832,7 @@ err: return st; } -static int test_badmod() +static int test_badmod(void) { BIGNUM *a = NULL, *b = NULL, *zero = NULL; BN_MONT_CTX *mont = NULL; @@ -1911,7 +1898,7 @@ err: return st; } -static int test_expmodzero() +static int test_expmodzero(void) { BIGNUM *a = NULL, *r = NULL, *zero = NULL; int st = 0; @@ -1944,7 +1931,7 @@ err: return st; } -static int test_smallprime() +static int test_smallprime(void) { static const int kBits = 10; BIGNUM *r; @@ -1962,7 +1949,7 @@ err: return st; } -static int test_3_is_prime() +static int test_3_is_prime(void) { int ret = 0; BIGNUM *r = NULL; @@ -1986,85 +1973,6 @@ err: return ret; } - -/* Delete leading and trailing spaces from a string */ -static char *strip_spaces(char *p) -{ - char *q; - - /* Skip over leading spaces */ - while (*p && isspace(*p)) - p++; - if (!*p) - return NULL; - - for (q = p + strlen(p) - 1; q != p && isspace(*q); ) - *q-- = '\0'; - return *p ? p : NULL; -} - -/* - * Read next test stanza; return 1 if found, 0 on EOF or error. - */ -static int readstanza(STANZA *s) -{ - PAIR *pp = s->pairs; - char *p, *equals, *key, *value; - char buff[1024]; - - while (BIO_gets(fp, buff, sizeof(buff))) { - s->curr++; - if (!TEST_ptr(p = strchr(buff, '\n'))) { - TEST_info("Line %d too long", s->curr); - return 0; - } - *p = '\0'; - - /* Blank line marks end of tests. */ - if (buff[0] == '\0') - break; - - /* Lines starting with a pound sign are ignored. */ - if (buff[0] == '#') - continue; - - if (!TEST_ptr(equals = strchr(buff, '='))) { - TEST_info("Missing = at line %d\n", s->curr); - return 0; - } - *equals++ = '\0'; - if (s->numpairs == 0) - s->start = s->curr; - - if (!TEST_ptr(key = strip_spaces(buff)) - || !TEST_ptr(value = strip_spaces(equals)) - || !TEST_int_lt(s->numpairs++, MAXPAIRS) - || !TEST_ptr(pp->key = OPENSSL_strdup(key)) - || !TEST_ptr(pp->value = OPENSSL_strdup(value))) - return 0; - pp++; - } - - /* If we read anything, return ok. */ - return 1; -} - -static void clearstanza(STANZA *s) -{ - PAIR *pp = s->pairs; - int i = s->numpairs; - int start = s->start; - int curr = s->curr; - - for ( ; --i >= 0; pp++) { - OPENSSL_free(pp->key); - OPENSSL_free(pp->value); - } - memset(s, 0, sizeof(*s)); - s->curr = curr; - s->start = start; -} - static int file_test_run(STANZA *s) { static const FILETEST filetests[] = { @@ -2086,55 +1994,55 @@ static int file_test_run(STANZA *s) for ( ; --numtests >= 0; tp++) { if (findattr(s, tp->name) != NULL) { if (!tp->func(s)) { - TEST_info("Failed %s test at %d", tp->name, s->start); + TEST_info("%s:%d: Failed %s test", + s->test_file, s->start, tp->name); return 0; } return 1; } } - TEST_info("Unknown test at %d", s->start); + TEST_info("%s:%d: Unknown test", s->test_file, s->start); return 0; } -static char * const *testfiles; - static int run_file_tests(int i) { - STANZA s; - int errcnt = 0; + STANZA *s = NULL; + char *testfile = test_get_argument(i); + int c; - if (!TEST_ptr(fp = BIO_new_file(testfiles[i], "rb"))) + if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s)))) + return 0; + if (!test_start_file(s, testfile)) { + OPENSSL_free(s); return 0; + } /* Read test file. */ - set_test_title(testfiles[i]); - memset(&s, 0, sizeof(s)); - while (!BIO_eof(fp) && readstanza(&s)) { - if (s.numpairs == 0) + while (!BIO_eof(s->fp) && test_readstanza(s)) { + if (s->numpairs == 0) continue; - if (!file_test_run(&s)) { - errcnt++; - } - clearstanza(&s); + if (!file_test_run(s)) + s->errors++; + s->numtests++; + test_clearstanza(s); } - BIO_free(fp); + test_end_file(s); + c = s->errors; + OPENSSL_free(s); - return errcnt == 0; + return c == 0; } -int test_main(int argc, char *argv[]) +int setup_tests(void) { - static const char rnd_seed[] = - "If not seeded, BN_generate_prime might fail"; - int result = EXIT_FAILURE; - + int n = test_get_argument_count(); - RAND_seed(rnd_seed, sizeof rnd_seed); if (!TEST_ptr(ctx = BN_CTX_new())) - goto end; + return 0; - if (argc < 2) { + if (n == 0) { ADD_TEST(test_sub); ADD_TEST(test_div_recip); ADD_TEST(test_mod); @@ -2163,13 +2071,12 @@ int test_main(int argc, char *argv[]) #endif ADD_TEST(test_3_is_prime); } else { - testfiles = &argv[1]; - ADD_ALL_TESTS(run_file_tests, argc - 1); + ADD_ALL_TESTS(run_file_tests, n); } + return 1; +} - result = run_tests(argv[0]); - -end: +void cleanup_tests(void) +{ BN_CTX_free(ctx); - return result; }