diff options
author | Marcin Cieślak <saper@saper.info> | 2018-05-11 00:16:52 +0000 |
---|---|---|
committer | Marcin Cieślak <saper@saper.info> | 2018-05-11 01:44:09 +0000 |
commit | 308eec7532691f6a352446020aeb7839a6af927e (patch) | |
tree | 3fda4b311fcfc539e7a1e36433a7909ff164bfc1 /src | |
parent | 382beb93cb4110b2bcc82ab69c8e72c274a51ca2 (diff) | |
download | tor-308eec7532691f6a352446020aeb7839a6af927e.tar.gz tor-308eec7532691f6a352446020aeb7839a6af927e.zip |
testing: X509 certificate structure needs to be initialized
We alloc/free X.509 structures in three ways:
1) X509 structure allocated with X509_new() and X509_free()
2) Fake X509 structure allocated with fake_x509_malloc() and fake_x509_free()
May contain valid pointers inside.
3) Empty X509 structure shell allocated with tor_malloc_zero() and
freed with tor_free()
Diffstat (limited to 'src')
-rw-r--r-- | src/test/test_tortls.c | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/src/test/test_tortls.c b/src/test/test_tortls.c index 896e093968..388f6df325 100644 --- a/src/test/test_tortls.c +++ b/src/test/test_tortls.c @@ -522,7 +522,7 @@ test_tortls_x509_cert_free(void *ignored) tor_x509_cert_free(cert); cert = tor_malloc_zero(sizeof(tor_x509_cert_t)); - cert->cert = tor_malloc_zero(sizeof(X509)); + cert->cert = X509_new(); cert->encoded = tor_malloc_zero(1); tor_x509_cert_free(cert); } @@ -560,6 +560,15 @@ fixed_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) return 1; } +/* + * Use only for the matching fake_x509_free() call + */ +static X509 * +fake_x509_malloc(void) +{ + return tor_malloc_zero(sizeof(X509)); +} + static void fake_x509_free(X509 *cert) { @@ -590,9 +599,9 @@ test_tortls_cert_matches_key(void *ignored) tls = tor_malloc_zero(sizeof(tor_tls_t)); cert = tor_malloc_zero(sizeof(tor_x509_cert_t)); - one = tor_malloc_zero(sizeof(X509)); + one = fake_x509_malloc(); one->references = 1; - two = tor_malloc_zero(sizeof(X509)); + two = fake_x509_malloc(); two->references = 1; res = tor_tls_cert_matches_key(tls, cert); @@ -648,7 +657,7 @@ test_tortls_cert_get_key(void *ignored) crypto_pk_t *res = NULL; cert = tor_malloc_zero(sizeof(tor_x509_cert_t)); X509 *key = NULL; - key = tor_malloc_zero(sizeof(X509)); + key = fake_x509_malloc(); key->references = 1; res = tor_tls_cert_get_key(cert); @@ -2475,8 +2484,8 @@ test_tortls_context_new(void *ignored) fixed_crypto_pk_generate_key_with_bits_result[1] = 0; fixed_tor_tls_create_certificate_result_index = 0; fixed_tor_tls_create_certificate_result[0] = NULL; - fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[1] = X509_new(); + fixed_tor_tls_create_certificate_result[2] = X509_new(); ret = tor_tls_context_new(NULL, 0, 0, 0); tt_assert(!ret); @@ -2486,9 +2495,9 @@ test_tortls_context_new(void *ignored) fixed_crypto_pk_new_result[2] = NULL; fixed_crypto_pk_generate_key_with_bits_result_index = 0; fixed_tor_tls_create_certificate_result_index = 0; - fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[0] = X509_new(); fixed_tor_tls_create_certificate_result[1] = NULL; - fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[2] = X509_new(); ret = tor_tls_context_new(NULL, 0, 0, 0); tt_assert(!ret); @@ -2498,8 +2507,8 @@ test_tortls_context_new(void *ignored) fixed_crypto_pk_new_result[2] = NULL; fixed_crypto_pk_generate_key_with_bits_result_index = 0; fixed_tor_tls_create_certificate_result_index = 0; - fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[0] = X509_new(); + fixed_tor_tls_create_certificate_result[1] = X509_new(); fixed_tor_tls_create_certificate_result[2] = NULL; ret = tor_tls_context_new(NULL, 0, 0, 0); tt_assert(!ret); @@ -2511,9 +2520,9 @@ test_tortls_context_new(void *ignored) fixed_crypto_pk_new_result[2] = NULL; fixed_crypto_pk_generate_key_with_bits_result_index = 0; fixed_tor_tls_create_certificate_result_index = 0; - fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[0] = X509_new(); + fixed_tor_tls_create_certificate_result[1] = X509_new(); + fixed_tor_tls_create_certificate_result[2] = X509_new(); fixed_tor_x509_cert_new_result_index = 0; fixed_tor_x509_cert_new_result[0] = NULL; fixed_tor_x509_cert_new_result[1] = NULL; @@ -2527,9 +2536,9 @@ test_tortls_context_new(void *ignored) fixed_crypto_pk_new_result[2] = NULL; fixed_crypto_pk_generate_key_with_bits_result_index = 0; fixed_tor_tls_create_certificate_result_index = 0; - fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[0] = X509_new(); + fixed_tor_tls_create_certificate_result[1] = X509_new(); + fixed_tor_tls_create_certificate_result[2] = X509_new(); fixed_tor_x509_cert_new_result_index = 0; fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t)); fixed_tor_x509_cert_new_result[1] = NULL; @@ -2543,9 +2552,9 @@ test_tortls_context_new(void *ignored) fixed_crypto_pk_new_result[2] = NULL; fixed_crypto_pk_generate_key_with_bits_result_index = 0; fixed_tor_tls_create_certificate_result_index = 0; - fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[0] = X509_new(); + fixed_tor_tls_create_certificate_result[1] = X509_new(); + fixed_tor_tls_create_certificate_result[2] = X509_new(); fixed_tor_x509_cert_new_result_index = 0; fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t)); fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t)); @@ -2559,9 +2568,9 @@ test_tortls_context_new(void *ignored) fixed_crypto_pk_new_result[2] = NULL; fixed_crypto_pk_generate_key_with_bits_result_index = 0; fixed_tor_tls_create_certificate_result_index = 0; - fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509)); - fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509)); + fixed_tor_tls_create_certificate_result[0] = X509_new(); + fixed_tor_tls_create_certificate_result[1] = X509_new(); + fixed_tor_tls_create_certificate_result[2] = X509_new(); fixed_tor_x509_cert_new_result_index = 0; fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t)); fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t)); |