Use "" not <> for internal/ includes
[openssl.git] / test / ssl_test_ctx_test.c
index 5022ce0f6ed54fc1e88036fbc54927a7f566e8b6..194919d1f36c3144d5431717c63e48d72c79f698 100644 (file)
@@ -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 license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -15,7 +15,7 @@
 #include <stdio.h>
 #include <string.h>
 
-#include "e_os.h"
+#include "internal/nelem.h"
 #include "ssl_test_ctx.h"
 #include "testutil.h"
 #include <openssl/e_os2.h>
@@ -97,22 +97,27 @@ static int testctx_eq(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
     return 1;
 }
 
-static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
+static SSL_TEST_CTX_TEST_FIXTURE *set_up(const char *const test_case_name)
 {
-    SSL_TEST_CTX_TEST_FIXTURE fixture;
-
-    fixture.test_case_name = test_case_name;
-    TEST_ptr(fixture.expected_ctx = SSL_TEST_CTX_new());
+    SSL_TEST_CTX_TEST_FIXTURE *fixture;
+
+    if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
+        return NULL;
+    fixture->test_case_name = test_case_name;
+    if (!TEST_ptr(fixture->expected_ctx = SSL_TEST_CTX_new())) {
+        OPENSSL_free(fixture);
+        return NULL;
+    }
     return fixture;
 }
 
-static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
+static int execute_test(SSL_TEST_CTX_TEST_FIXTURE *fixture)
 {
     int success = 0;
     SSL_TEST_CTX *ctx;
 
-    if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture.test_section))
-            || !testctx_eq(ctx, fixture.expected_ctx))
+    if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture->test_section))
+            || !testctx_eq(ctx, fixture->expected_ctx))
         goto err;
 
     success = 1;
@@ -121,62 +126,74 @@ static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
     return success;
 }
 
-static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
+static void tear_down(SSL_TEST_CTX_TEST_FIXTURE *fixture)
 {
-    SSL_TEST_CTX_free(fixture.expected_ctx);
+    SSL_TEST_CTX_free(fixture->expected_ctx);
+    OPENSSL_free(fixture);
 }
 
 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
-    SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
+    SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up);
 #define EXECUTE_SSL_TEST_CTX_TEST() \
     EXECUTE_TEST(execute_test, tear_down)
 
-static int test_empty_configuration()
+static int test_empty_configuration(void)
 {
     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
-    fixture.test_section = "ssltest_default";
-    fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
+    if (fixture == NULL)
+        return 0;
+    fixture->test_section = "ssltest_default";
+    fixture->expected_ctx->expected_result = SSL_TEST_SUCCESS;
     EXECUTE_SSL_TEST_CTX_TEST();
+    return result;
 }
 
-static int test_good_configuration()
+static int test_good_configuration(void)
 {
     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
-    fixture.test_section = "ssltest_good";
-    fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
-    fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
-    fixture.expected_ctx->app_data_size = 1024;
-    fixture.expected_ctx->max_fragment_size = 2048;
-
-    fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
-    fixture.expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
-    fixture.expected_ctx->expected_server_alert = 0;  /* No alert. */
-    fixture.expected_ctx->expected_protocol = TLS1_1_VERSION;
-    fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
-    fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
-    fixture.expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
-    fixture.expected_ctx->resumption_expected = 1;
-
-    fixture.expected_ctx->extra.client.verify_callback =
+    if (fixture == NULL)
+        return 0;
+    fixture->test_section = "ssltest_good";
+    fixture->expected_ctx->method = SSL_TEST_METHOD_DTLS;
+    fixture->expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
+    fixture->expected_ctx->app_data_size = 1024;
+    fixture->expected_ctx->max_fragment_size = 2048;
+
+    fixture->expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
+    fixture->expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
+    fixture->expected_ctx->expected_server_alert = 0;  /* No alert. */
+    fixture->expected_ctx->expected_protocol = TLS1_1_VERSION;
+    fixture->expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
+    fixture->expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
+    fixture->expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
+    fixture->expected_ctx->resumption_expected = 1;
+
+    fixture->expected_ctx->extra.client.verify_callback =
         SSL_TEST_VERIFY_REJECT_ALL;
-    fixture.expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
-    fixture.expected_ctx->extra.client.npn_protocols =
+    fixture->expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
+    fixture->expected_ctx->extra.client.npn_protocols =
         OPENSSL_strdup("foo,bar");
-    TEST_check(fixture.expected_ctx->extra.client.npn_protocols != NULL);
+    if (!TEST_ptr(fixture->expected_ctx->extra.client.npn_protocols))
+        goto err;
 
-    fixture.expected_ctx->extra.server.servername_callback =
+    fixture->expected_ctx->extra.server.servername_callback =
         SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
-    fixture.expected_ctx->extra.server.broken_session_ticket = 1;
+    fixture->expected_ctx->extra.server.broken_session_ticket = 1;
 
-    fixture.expected_ctx->resume_extra.server2.alpn_protocols =
+    fixture->expected_ctx->resume_extra.server2.alpn_protocols =
         OPENSSL_strdup("baz");
-    TEST_check(
-        fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL);
+    if (!TEST_ptr(fixture->expected_ctx->resume_extra.server2.alpn_protocols))
+        goto err;
 
-    fixture.expected_ctx->resume_extra.client.ct_validation =
+    fixture->expected_ctx->resume_extra.client.ct_validation =
         SSL_TEST_CT_VALIDATION_STRICT;
 
     EXECUTE_SSL_TEST_CTX_TEST();
+    return result;
+
+err:
+    tear_down(fixture);
+    return 0;
 }
 
 static const char *bad_configurations[] = {
@@ -199,7 +216,7 @@ static const char *bad_configurations[] = {
 static int test_bad_configuration(int idx)
 {
     SSL_TEST_CTX *ctx;
-    
+
     if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf,
                                                  bad_configurations[idx]))) {
         SSL_TEST_CTX_free(ctx);
@@ -209,25 +226,23 @@ static int test_bad_configuration(int idx)
     return 1;
 }
 
-int test_main(int argc, char **argv)
+int setup_tests(void)
 {
-    int result = 0;
-
-    if (argc != 2) {
-        TEST_info("Missing file argument");
-        goto end;
+    if (!TEST_ptr(conf = NCONF_new(NULL)))
+        return 0;
+    /* argument should point to test/ssl_test_ctx_test.conf */
+    if (!TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)) {
+        TEST_note("Missing file argument");
+        return 0;
     }
-    if (!TEST_ptr(conf = NCONF_new(NULL))
-            /* argv[1] should point to test/ssl_test_ctx_test.conf */
-            || !TEST_int_gt(NCONF_load(conf, argv[1], NULL), 0))
-        goto end;
 
     ADD_TEST(test_empty_configuration);
     ADD_TEST(test_good_configuration);
     ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
-    result = run_tests(argv[0]);
+    return 1;
+}
 
-end:
+void cleanup_tests(void)
+{
     NCONF_free(conf);
-    return result;
 }