2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Ideally, CONF should offer standard parsing methods and cover them
12 * 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));
68 if (ctx->servername != ctx2->servername) {
69 fprintf(stderr, "ServerName mismatch: %s vs %s.\n",
70 ssl_servername_name(ctx->servername),
71 ssl_servername_name(ctx2->servername));
74 if (ctx->expected_servername != ctx2->expected_servername) {
75 fprintf(stderr, "ExpectedServerName mismatch: %s vs %s.\n",
76 ssl_servername_name(ctx->expected_servername),
77 ssl_servername_name(ctx2->expected_servername));
80 if (ctx->servername_callback != ctx2->servername_callback) {
81 fprintf(stderr, "ServerNameCallback mismatch: %s vs %s.\n",
82 ssl_servername_callback_name(ctx->servername_callback),
83 ssl_servername_callback_name(ctx2->servername_callback));
86 if (ctx->session_ticket_expected != ctx2->session_ticket_expected) {
87 fprintf(stderr, "SessionTicketExpected mismatch: %s vs %s.\n",
88 ssl_session_ticket_name(ctx->session_ticket_expected),
89 ssl_session_ticket_name(ctx2->session_ticket_expected));
92 #ifndef OPENSSL_NO_NEXTPROTONEG
93 if (!strings_equal("ClientNPNProtocols", ctx->client_npn_protocols,
94 ctx2->client_npn_protocols))
96 if (ctx->method != ctx2->method) {
97 fprintf(stderr, "Method mismatch: %s vs %s.\n",
98 ssl_test_method_name(ctx->method),
99 ssl_test_method_name(ctx2->method));
102 if (!strings_equal("ServerNPNProtocols", ctx->server_npn_protocols,
103 ctx2->server_npn_protocols))
105 if (!strings_equal("Server2NPNProtocols", ctx->server_npn_protocols,
106 ctx2->server_npn_protocols))
108 if (!strings_equal("ExpectedNPNProtocol", ctx->expected_npn_protocol,
109 ctx2->expected_npn_protocol))
111 if (!strings_equal("ClientALPNProtocols", ctx->client_alpn_protocols,
112 ctx2->client_alpn_protocols))
115 if (!strings_equal("ServerALPNProtocols", ctx->server_alpn_protocols,
116 ctx2->server_alpn_protocols))
118 if (!strings_equal("Server2ALPNProtocols", ctx->server_alpn_protocols,
119 ctx2->server_alpn_protocols))
121 if (!strings_equal("ExpectedALPNProtocol", ctx->expected_alpn_protocol,
122 ctx2->expected_alpn_protocol))
125 if (ctx->handshake_mode != ctx2->handshake_mode) {
126 fprintf(stderr, "HandshakeMode mismatch: %s vs %s.\n",
127 ssl_handshake_mode_name(ctx->handshake_mode),
128 ssl_handshake_mode_name(ctx2->handshake_mode));
131 if (ctx->resumption_expected != ctx2->resumption_expected) {
132 fprintf(stderr, "ResumptionExpected mismatch: %d vs %d.\n",
133 ctx->resumption_expected, ctx2->resumption_expected);
139 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
141 SSL_TEST_CTX_TEST_FIXTURE fixture;
142 fixture.test_case_name = test_case_name;
143 fixture.expected_ctx = SSL_TEST_CTX_new();
144 OPENSSL_assert(fixture.expected_ctx != NULL);
148 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
152 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
155 fprintf(stderr, "Failed to parse good configuration %s.\n",
156 fixture.test_section);
160 if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
165 SSL_TEST_CTX_free(ctx);
169 static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
171 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
174 fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
175 fixture.test_section);
176 SSL_TEST_CTX_free(ctx);
183 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
185 SSL_TEST_CTX_free(fixture.expected_ctx);
186 ERR_print_errors_fp(stderr);
189 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
190 SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
191 #define EXECUTE_SSL_TEST_CTX_TEST() \
192 EXECUTE_TEST(execute_test, tear_down)
193 #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST() \
194 EXECUTE_TEST(execute_failure_test, tear_down)
196 static int test_empty_configuration()
198 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
199 fixture.test_section = "ssltest_default";
200 fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
201 EXECUTE_SSL_TEST_CTX_TEST();
204 static int test_good_configuration()
206 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
207 fixture.test_section = "ssltest_good";
208 fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
209 fixture.expected_ctx->client_alert = SSL_AD_UNKNOWN_CA;
210 fixture.expected_ctx->server_alert = 0; /* No alert. */
211 fixture.expected_ctx->protocol = TLS1_1_VERSION;
212 fixture.expected_ctx->client_verify_callback = SSL_TEST_VERIFY_REJECT_ALL;
213 fixture.expected_ctx->servername = SSL_TEST_SERVERNAME_SERVER2;
214 fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
215 fixture.expected_ctx->servername_callback =
216 SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
217 fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
218 fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
219 #ifndef OPENSSL_NO_NEXTPROTONEG
220 fixture.expected_ctx->client_npn_protocols = OPENSSL_strdup("foo,bar");
221 fixture.expected_ctx->server2_alpn_protocols = OPENSSL_strdup("baz");
222 OPENSSL_assert(fixture.expected_ctx->client_npn_protocols != NULL);
223 OPENSSL_assert(fixture.expected_ctx->server2_alpn_protocols != NULL);
225 fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
226 fixture.expected_ctx->resumption_expected = 1;
227 EXECUTE_SSL_TEST_CTX_TEST();
230 static const char *bad_configurations[] = {
231 "ssltest_unknown_option",
232 "ssltest_unknown_expected_result",
233 "ssltest_unknown_alert",
234 "ssltest_unknown_protocol",
235 "ssltest_unknown_verify_callback",
236 "ssltest_unknown_servername",
237 "ssltest_unknown_servername_callback",
238 "ssltest_unknown_session_ticket_expected",
239 "ssltest_unknown_method",
240 "ssltest_unknown_handshake_mode",
241 "ssltest_unknown_resumption_expected",
244 static int test_bad_configuration(int idx)
246 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
247 fixture.test_section = bad_configurations[idx];
248 EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
251 int main(int argc, char **argv)
258 conf = NCONF_new(NULL);
259 OPENSSL_assert(conf != NULL);
261 /* argv[1] should point to test/ssl_test_ctx_test.conf */
262 OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
265 ADD_TEST(test_empty_configuration);
266 ADD_TEST(test_good_configuration);
267 ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
269 result = run_tests(argv[0]);