diff options
author | David Benjamin <davidben@google.com> | 2016-08-25 17:45:20 -0400 |
---|---|---|
committer | Rich Salz <rsalz@openssl.org> | 2016-09-26 11:03:37 -0400 |
commit | 8ff70f3326983360c6f6306a1cd2238ef92d1f26 (patch) | |
tree | 17ffc2d6e15e07e82a102ae225964fbab1d57353 /test | |
parent | 98c1f5b429d036c0370de15f4d6851eed41fa5b3 (diff) |
Add a basic test for BN_bn2dec.
This would have caught 099e2968ed3c7d256cda048995626664082b1b30. This is
a port of the test added in
https://boringssl.googlesource.com/boringssl/+/7c040756178e14a4d181b6d93abb3827c93189c4
Reviewed-by: Emilia Käsper <emilia@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/1496)
Diffstat (limited to 'test')
-rw-r--r-- | test/bntest.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/bntest.c b/test/bntest.c index 3507b31e1d..51b75d3ed0 100644 --- a/test/bntest.c +++ b/test/bntest.c @@ -83,6 +83,7 @@ int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx); int test_kron(BIO *bp, BN_CTX *ctx); int test_sqrt(BIO *bp, BN_CTX *ctx); int test_small_prime(BIO *bp, BN_CTX *ctx); +int test_bn2dec(BIO *bp); int rand_neg(void); static int results = 0; @@ -260,6 +261,11 @@ int main(int argc, char *argv[]) goto err; (void)BIO_flush(out); + message(out, "BN_bn2dec"); + if (!test_bn2dec(out)) + goto err; + (void)BIO_flush(out); + #ifndef OPENSSL_NO_EC2M message(out, "BN_GF2m_add"); if (!test_gf2m_add(out)) @@ -1839,6 +1845,52 @@ int test_small_prime(BIO *bp, BN_CTX *ctx) return ret; } +int test_bn2dec(BIO *bp) +{ + static const char *bn2dec_tests[] = { + "0", + "1", + "-1", + "100", + "-100", + "123456789012345678901234567890", + "-123456789012345678901234567890", + "123456789012345678901234567890123456789012345678901234567890", + "-123456789012345678901234567890123456789012345678901234567890", + }; + int ret = 0; + size_t i; + BIGNUM *bn = NULL; + char *dec = NULL; + + for (i = 0; i < OSSL_NELEM(bn2dec_tests); i++) { + if (!BN_dec2bn(&bn, bn2dec_tests[i])) + goto err; + + dec = BN_bn2dec(bn); + if (dec == NULL) { + fprintf(stderr, "BN_bn2dec failed on %s.\n", bn2dec_tests[i]); + goto err; + } + + if (strcmp(dec, bn2dec_tests[i]) != 0) { + fprintf(stderr, "BN_bn2dec gave %s, wanted %s.\n", dec, + bn2dec_tests[i]); + goto err; + } + + OPENSSL_free(dec); + dec = NULL; + } + + ret = 1; + +err: + BN_free(bn); + OPENSSL_free(dec); + return ret; +} + int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_) { BIGNUM *a, *b, *c, *d; |