From 5e30e26c6d7ae33e23557bc18def36f98be4e1e6 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Sep 2016 09:44:57 -0400 Subject: [PATCH] Chop another ~93 RSA key generations out of the unit tests We have a mock for our RSA key generation function, so we now wire it to pk_generate(). This covers all the cases that were not using pk_generate() before -- all ~93 of them. --- src/common/crypto.c | 14 ++++++++++++++ src/common/crypto.h | 4 ++++ src/test/test_shared_random.c | 4 +--- src/test/test_tortls.c | 4 +++- src/test/testing_common.c | 18 +++++++++++++++++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/common/crypto.c b/src/common/crypto.c index b87023f071..bf682ff330 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -984,6 +984,20 @@ crypto_pk_dup_key(crypto_pk_t *env) return env; } +#ifdef TOR_UNIT_TESTS +/** For testing: replace dest with src. (Dest must have a refcount + * of 1) */ +void +crypto_pk_assign_(crypto_pk_t *dest, const crypto_pk_t *src) +{ + tor_assert(dest); + tor_assert(dest->refs == 1); + tor_assert(src); + RSA_free(dest->key); + dest->key = RSAPrivateKey_dup(src->key); +} +#endif + /** Make a real honest-to-goodness copy of env, and return it. * Returns NULL on failure. */ crypto_pk_t * diff --git a/src/common/crypto.h b/src/common/crypto.h index f8fb0daa81..8f1217e2bd 100644 --- a/src/common/crypto.h +++ b/src/common/crypto.h @@ -326,5 +326,9 @@ extern int break_strongest_rng_fallback; #endif #endif +#ifdef TOR_UNIT_TESTS +void crypto_pk_assign_(crypto_pk_t *dest, const crypto_pk_t *src); +#endif + #endif diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c index 19f2e2d95e..056f199b94 100644 --- a/src/test/test_shared_random.c +++ b/src/test/test_shared_random.c @@ -1110,11 +1110,9 @@ test_keep_commit(void *arg) trusteddirserver_get_by_v3_auth_digest_m); { - k = crypto_pk_new(); + k = pk_generate(1); /* Setup a minimal dirauth environment for this test */ /* Have a key that is not the one from our commit. */ - tt_int_op(0, ==, crypto_pk_generate_key(k)); - tt_int_op(0, ==, crypto_pk_get_fingerprint(k, fp, 0)); init_authority_state(); state = get_sr_state(); } diff --git a/src/test/test_tortls.c b/src/test/test_tortls.c index 8502e8aa3f..8efcac242f 100644 --- a/src/test/test_tortls.c +++ b/src/test/test_tortls.c @@ -2422,6 +2422,8 @@ test_tortls_context_new(void *ignored) ret = tor_tls_context_new(NULL, 0, 0, 0); tt_assert(!ret); + /* note: we already override this in testing_common.c, so we + * run this unit test in a subprocess. */ MOCK(crypto_pk_generate_key_with_bits, fixed_crypto_pk_generate_key_with_bits); fixed_crypto_pk_new_result_index = 0; @@ -2808,7 +2810,7 @@ struct testcase_t tortls_tests[] = { INTRUSIVE_TEST_CASE(find_cipher_by_id, 0), INTRUSIVE_TEST_CASE(session_secret_cb, 0), INTRUSIVE_TEST_CASE(debug_state_callback, 0), - INTRUSIVE_TEST_CASE(context_new, 0), + INTRUSIVE_TEST_CASE(context_new, TT_FORK /* redundant */), LOCAL_TEST_CASE(create_certificate, 0), LOCAL_TEST_CASE(cert_new, 0), LOCAL_TEST_CASE(cert_is_valid, 0), diff --git a/src/test/testing_common.c b/src/test/testing_common.c index 2858c9ec99..d5f2c00923 100644 --- a/src/test/testing_common.c +++ b/src/test/testing_common.c @@ -181,12 +181,26 @@ pk_generate(int idx) crypto_pk_t *result; int res; result = crypto_pk_new(); - res = crypto_pk_generate_key(result); + res = crypto_pk_generate_key__real(result); tor_assert(!res); return result; #endif } +#ifdef CACHE_GENERATED_KEYS +static int +crypto_pk_generate_key_with_bits__get_cached(crypto_pk_t *env, int bits) +{ + if (bits != 1024) + return crypto_pk_generate_key_with_bits__real(env, bits); + + crypto_pk_t *newkey = pk_generate(0); + crypto_pk_assign_(env, newkey); + crypto_pk_free(newkey); + return 0; +} +#endif + /** Free all storage used for the cached key optimization. */ static void free_pregenerated_keys(void) @@ -332,6 +346,8 @@ main(int c, const char **v) int r = crypto_pk_generate_key(pregen_keys[i]); tor_assert(r == 0); } + MOCK(crypto_pk_generate_key_with_bits, + crypto_pk_generate_key_with_bits__get_cached); #endif atexit(remove_directory);