2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
12 * Ideally, CONF should offer standard parsing methods and cover them
13 * in tests. But since we have no CONF tests, we use a custom test for now.
19 #include "ssl_test_ctx.h"
21 #include <openssl/e_os2.h>
22 #include <openssl/err.h>
23 #include <openssl/conf.h>
24 #include <openssl/ssl.h>
26 static CONF *conf = NULL;
28 typedef struct ssl_test_ctx_test_fixture {
29 const char *test_case_name;
30 const char *test_section;
31 /* Expected parsed configuration. */
32 SSL_TEST_CTX *expected_ctx;
33 } SSL_TEST_CTX_TEST_FIXTURE;
35 /* Returns 1 if the contexts are equal, 0 otherwise. */
36 static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
38 if (ctx->expected_result != ctx2->expected_result) {
39 fprintf(stderr, "ExpectedResult mismatch: %s vs %s.\n",
40 ssl_test_result_name(ctx->expected_result),
41 ssl_test_result_name(ctx2->expected_result));
44 if (ctx->client_alert != ctx2->client_alert) {
45 fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n",
46 ssl_alert_name(ctx->client_alert),
47 ssl_alert_name(ctx2->client_alert));
50 if (ctx->server_alert != ctx2->server_alert) {
51 fprintf(stderr, "ServerAlert mismatch: %s vs %s.\n",
52 ssl_alert_name(ctx->server_alert),
53 ssl_alert_name(ctx2->server_alert));
56 if (ctx->protocol != ctx2->protocol) {
57 fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n",
58 ssl_protocol_name(ctx->protocol),
59 ssl_protocol_name(ctx2->protocol));
62 if (ctx->client_verify_callback != ctx2->client_verify_callback) {
63 fprintf(stderr, "ClientVerifyCallback mismatch: %s vs %s.\n",
64 ssl_verify_callback_name(ctx->client_verify_callback),
65 ssl_verify_callback_name(ctx2->client_verify_callback));
72 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
74 SSL_TEST_CTX_TEST_FIXTURE fixture;
75 fixture.test_case_name = test_case_name;
76 fixture.expected_ctx = SSL_TEST_CTX_new();
77 OPENSSL_assert(fixture.expected_ctx != NULL);
81 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
85 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
88 fprintf(stderr, "Failed to parse good configuration %s.\n",
89 fixture.test_section);
93 if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
98 SSL_TEST_CTX_free(ctx);
102 static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
104 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
107 fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
108 fixture.test_section);
109 SSL_TEST_CTX_free(ctx);
116 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
118 SSL_TEST_CTX_free(fixture.expected_ctx);
119 ERR_print_errors_fp(stderr);
122 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
123 SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
124 #define EXECUTE_SSL_TEST_CTX_TEST() \
125 EXECUTE_TEST(execute_test, tear_down)
126 #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST() \
127 EXECUTE_TEST(execute_failure_test, tear_down)
129 static int test_empty_configuration()
131 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
132 fixture.test_section = "ssltest_default";
133 fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
134 EXECUTE_SSL_TEST_CTX_TEST();
137 static int test_good_configuration()
139 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
140 fixture.test_section = "ssltest_good";
141 fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
142 fixture.expected_ctx->client_alert = SSL_AD_UNKNOWN_CA;
143 fixture.expected_ctx->server_alert = 0; /* No alert. */
144 fixture.expected_ctx->protocol = TLS1_1_VERSION;
145 fixture.expected_ctx->client_verify_callback = SSL_TEST_VERIFY_REJECT_ALL,
146 EXECUTE_SSL_TEST_CTX_TEST();
149 static const char *bad_configurations[] = {
150 "ssltest_unknown_option",
151 "ssltest_unknown_expected_result",
152 "ssltest_unknown_alert",
153 "ssltest_unknown_protocol",
154 "ssltest_unknown_verify_callback",
157 static int test_bad_configuration(int idx)
159 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
160 fixture.test_section = bad_configurations[idx];
161 EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
164 int main(int argc, char **argv)
171 conf = NCONF_new(NULL);
172 OPENSSL_assert(conf != NULL);
174 /* argv[1] should point to test/ssl_test_ctx_test.conf */
175 OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
178 ADD_TEST(test_empty_configuration);
179 ADD_TEST(test_good_configuration);
180 ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
182 result = run_tests(argv[0]);