X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=test%2Fcipherlist_test.c;h=0297b0d2fab09cd16adbe98efaa912ca532c0991;hp=70ebd835660ec017901a23a7327cd918e054d95a;hb=6aa907a6bf4d533da657dbb7176012fab40a0544;hpb=582a17d662d123eafbb70c9eaaa140a2559b7cdb diff --git a/test/cipherlist_test.c b/test/cipherlist_test.c index 70ebd83566..0297b0d2fa 100644 --- a/test/cipherlist_test.c +++ b/test/cipherlist_test.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the OpenSSL licenses, (the "License"); * you may not use this file except in compliance with the License. @@ -9,6 +9,7 @@ */ #include +#include #include #include @@ -27,13 +28,28 @@ typedef struct cipherlist_test_fixture { } CIPHERLIST_TEST_FIXTURE; -static CIPHERLIST_TEST_FIXTURE set_up(const char *const test_case_name) +static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture) { - CIPHERLIST_TEST_FIXTURE fixture; - fixture.test_case_name = test_case_name; - fixture.server = SSL_CTX_new(TLS_server_method()); - fixture.client = SSL_CTX_new(TLS_client_method()); - OPENSSL_assert(fixture.client != NULL && fixture.server != NULL); + if (fixture != NULL) { + SSL_CTX_free(fixture->server); + SSL_CTX_free(fixture->client); + fixture->server = fixture->client = NULL; + OPENSSL_free(fixture); + } +} + +static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name) +{ + CIPHERLIST_TEST_FIXTURE *fixture; + + if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) + return NULL; + fixture->test_case_name = test_case_name; + if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method())) + || !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) { + tear_down(fixture); + return NULL; + } return fixture; } @@ -109,6 +125,8 @@ static const uint32_t default_ciphers_in_order[] = { TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, #endif #ifndef OPENSSL_NO_TLS1_3 + TLS1_3_CK_AES_256_GCM_SHA384, + TLS1_3_CK_CHACHA20_POLY1305_SHA256, TLS1_3_CK_AES_128_GCM_SHA256, #endif #ifndef OPENSSL_NO_TLS1_2 @@ -121,30 +139,28 @@ static const uint32_t default_ciphers_in_order[] = { static int test_default_cipherlist(SSL_CTX *ctx) { - STACK_OF(SSL_CIPHER) *ciphers; - SSL *ssl; + STACK_OF(SSL_CIPHER) *ciphers = NULL; + SSL *ssl = NULL; int i, ret = 0, num_expected_ciphers, num_ciphers; uint32_t expected_cipher_id, cipher_id; - ssl = SSL_new(ctx); - OPENSSL_assert(ssl != NULL); + if (ctx == NULL) + return 0; + + if (!TEST_ptr(ssl = SSL_new(ctx)) + || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl))) + goto err; - ciphers = SSL_get1_supported_ciphers(ssl); - OPENSSL_assert(ciphers != NULL); num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order); num_ciphers = sk_SSL_CIPHER_num(ciphers); - if (num_ciphers != num_expected_ciphers) { - fprintf(stderr, "Expected %d supported ciphers, got %d.\n", - num_expected_ciphers, num_ciphers); + if (!TEST_int_eq(num_ciphers, num_expected_ciphers)) goto err; - } for (i = 0; i < num_ciphers; i++) { expected_cipher_id = default_ciphers_in_order[i]; cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i)); - if (cipher_id != expected_cipher_id) { - fprintf(stderr, "Wrong cipher at position %d: expected %x, " - "got %x\n", i, expected_cipher_id, cipher_id); + if (!TEST_int_eq(cipher_id, expected_cipher_id)) { + TEST_info("Wrong cipher at position %d", i); goto err; } } @@ -157,17 +173,11 @@ static int test_default_cipherlist(SSL_CTX *ctx) return ret; } -static int execute_test(CIPHERLIST_TEST_FIXTURE fixture) +static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture) { - return test_default_cipherlist(fixture.server) - && test_default_cipherlist(fixture.client); -} - -static void tear_down(CIPHERLIST_TEST_FIXTURE fixture) -{ - SSL_CTX_free(fixture.server); - SSL_CTX_free(fixture.client); - ERR_print_errors_fp(stderr); + return fixture != NULL + && test_default_cipherlist(fixture->server) + && test_default_cipherlist(fixture->client); } #define SETUP_CIPHERLIST_TEST_FIXTURE() \ @@ -179,25 +189,27 @@ static void tear_down(CIPHERLIST_TEST_FIXTURE fixture) static int test_default_cipherlist_implicit() { SETUP_CIPHERLIST_TEST_FIXTURE(); + if (fixture == NULL) + return 0; EXECUTE_CIPHERLIST_TEST(); + return result; } static int test_default_cipherlist_explicit() { SETUP_CIPHERLIST_TEST_FIXTURE(); - OPENSSL_assert(SSL_CTX_set_cipher_list(fixture.server, "DEFAULT")); - OPENSSL_assert(SSL_CTX_set_cipher_list(fixture.client, "DEFAULT")); + if (fixture == NULL) + return 0; + if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT")) + || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT"))) + tear_down(fixture); EXECUTE_CIPHERLIST_TEST(); + return result; } -int main(int argc, char **argv) +int setup_tests() { - int result = 0; - ADD_TEST(test_default_cipherlist_implicit); ADD_TEST(test_default_cipherlist_explicit); - - result = run_tests(argv[0]); - - return result; + return 1; }