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;
36 static int SSL_TEST_CLIENT_CONF_equal(SSL_TEST_CLIENT_CONF *client,
37 SSL_TEST_CLIENT_CONF *client2)
39 if (!TEST_int_eq(client->verify_callback, client2->verify_callback)) {
40 TEST_info("ClientVerifyCallback mismatch: %s vs %s.",
41 ssl_verify_callback_name(client->verify_callback),
42 ssl_verify_callback_name(client2->verify_callback));
45 if (!TEST_int_eq(client->servername, client2->servername)) {
46 TEST_info("ServerName mismatch: %s vs %s.",
47 ssl_servername_name(client->servername),
48 ssl_servername_name(client2->servername));
51 if (!TEST_str_eq(client->npn_protocols, client2->npn_protocols))
53 if (!TEST_str_eq(client->alpn_protocols, client2->alpn_protocols))
55 if (!TEST_int_eq(client->ct_validation, client2->ct_validation)) {
56 TEST_info("CTValidation mismatch: %s vs %s.",
57 ssl_ct_validation_name(client->ct_validation),
58 ssl_ct_validation_name(client2->ct_validation));
64 static int SSL_TEST_SERVER_CONF_equal(SSL_TEST_SERVER_CONF *server,
65 SSL_TEST_SERVER_CONF *server2)
67 if (!TEST_int_eq(server->servername_callback,
68 server2->servername_callback)) {
69 TEST_info("ServerNameCallback mismatch: %s vs %s.",
70 ssl_servername_callback_name(server->servername_callback),
71 ssl_servername_callback_name(server2->servername_callback));
74 if (!TEST_str_eq(server->npn_protocols, server2->npn_protocols))
76 if (!TEST_str_eq(server->alpn_protocols, server2->alpn_protocols))
78 if (!TEST_int_eq(server->broken_session_ticket,
79 server2->broken_session_ticket))
81 if (!TEST_int_eq(server->cert_status, server2->cert_status)) {
82 TEST_info("CertStatus mismatch: %s vs %s.",
83 ssl_certstatus_name(server->cert_status),
84 ssl_certstatus_name(server2->cert_status));
90 static int SSL_TEST_EXTRA_CONF_equal(SSL_TEST_EXTRA_CONF *extra,
91 SSL_TEST_EXTRA_CONF *extra2)
93 return SSL_TEST_CLIENT_CONF_equal(&extra->client, &extra2->client)
94 && SSL_TEST_SERVER_CONF_equal(&extra->server, &extra2->server)
95 && SSL_TEST_SERVER_CONF_equal(&extra->server2, &extra2->server2);
98 /* Returns 1 if the contexts are equal, 0 otherwise. */
99 static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
101 if (!TEST_int_eq(ctx->method, ctx2->method)) {
102 TEST_info("Method mismatch: %s vs %s.",
103 ssl_test_method_name(ctx->method),
104 ssl_test_method_name(ctx2->method));
107 if (!TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode)) {
108 TEST_info("HandshakeMode mismatch: %s vs %s.",
109 ssl_handshake_mode_name(ctx->handshake_mode),
110 ssl_handshake_mode_name(ctx2->handshake_mode));
113 if (!TEST_int_eq(ctx->app_data_size, ctx2->app_data_size))
116 if (!TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size))
119 if (!SSL_TEST_EXTRA_CONF_equal(&ctx->extra, &ctx2->extra))
121 if (!SSL_TEST_EXTRA_CONF_equal(&ctx->resume_extra, &ctx2->resume_extra))
124 if (!TEST_int_eq(ctx->expected_result, ctx2->expected_result)) {
125 TEST_info("ExpectedResult mismatch: %s vs %s.",
126 ssl_test_result_name(ctx->expected_result),
127 ssl_test_result_name(ctx2->expected_result));
130 if (!TEST_int_eq(ctx->expected_client_alert, ctx2->expected_client_alert)) {
131 TEST_info("ClientAlert mismatch: %s vs %s.",
132 ssl_alert_name(ctx->expected_client_alert),
133 ssl_alert_name(ctx2->expected_client_alert));
136 if (!TEST_int_eq(ctx->expected_server_alert, ctx2->expected_server_alert)) {
137 TEST_info("ServerAlert mismatch: %s vs %s.",
138 ssl_alert_name(ctx->expected_server_alert),
139 ssl_alert_name(ctx2->expected_server_alert));
142 if (!TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol)) {
143 TEST_info("ClientAlert mismatch: %s vs %s.",
144 ssl_protocol_name(ctx->expected_protocol),
145 ssl_protocol_name(ctx2->expected_protocol));
148 if (!TEST_int_eq(ctx->expected_servername, ctx2->expected_servername)) {
149 TEST_info("ExpectedServerName mismatch: %s vs %s.",
150 ssl_servername_name(ctx->expected_servername),
151 ssl_servername_name(ctx2->expected_servername));
154 if (!TEST_int_eq(ctx->session_ticket_expected,
155 ctx2->session_ticket_expected)) {
156 TEST_info("SessionTicketExpected mismatch: %s vs %s.",
157 ssl_session_ticket_name(ctx->session_ticket_expected),
158 ssl_session_ticket_name(ctx2->session_ticket_expected));
161 if (!TEST_int_eq(ctx->compression_expected, ctx2->compression_expected)) {
162 TEST_info("ComrpessionExpected mismatch: %d vs %d.",
163 ctx->compression_expected,
164 ctx2->compression_expected);
167 if (!TEST_str_eq(ctx->expected_npn_protocol, ctx2->expected_npn_protocol))
169 if (!TEST_str_eq(ctx->expected_alpn_protocol, ctx2->expected_alpn_protocol))
171 if (!TEST_int_eq(ctx->resumption_expected, ctx2->resumption_expected))
176 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
178 SSL_TEST_CTX_TEST_FIXTURE fixture;
179 fixture.test_case_name = test_case_name;
180 fixture.expected_ctx = SSL_TEST_CTX_new();
181 TEST_check(fixture.expected_ctx != NULL);
185 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
189 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
191 if (!TEST_ptr(ctx)) {
192 TEST_info("Failed to parse good configuration %s.",
193 fixture.test_section);
197 if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
202 SSL_TEST_CTX_free(ctx);
206 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
208 SSL_TEST_CTX_free(fixture.expected_ctx);
211 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
212 SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
213 #define EXECUTE_SSL_TEST_CTX_TEST() \
214 EXECUTE_TEST(execute_test, tear_down)
216 static int test_empty_configuration()
218 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
219 fixture.test_section = "ssltest_default";
220 fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
221 EXECUTE_SSL_TEST_CTX_TEST();
224 static int test_good_configuration()
226 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
227 fixture.test_section = "ssltest_good";
228 fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
229 fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
230 fixture.expected_ctx->app_data_size = 1024;
231 fixture.expected_ctx->max_fragment_size = 2048;
233 fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
234 fixture.expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
235 fixture.expected_ctx->expected_server_alert = 0; /* No alert. */
236 fixture.expected_ctx->expected_protocol = TLS1_1_VERSION;
237 fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
238 fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
239 fixture.expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
240 fixture.expected_ctx->resumption_expected = 1;
242 fixture.expected_ctx->extra.client.verify_callback =
243 SSL_TEST_VERIFY_REJECT_ALL;
244 fixture.expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
245 fixture.expected_ctx->extra.client.npn_protocols =
246 OPENSSL_strdup("foo,bar");
247 TEST_check(fixture.expected_ctx->extra.client.npn_protocols != NULL);
249 fixture.expected_ctx->extra.server.servername_callback =
250 SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
251 fixture.expected_ctx->extra.server.broken_session_ticket = 1;
253 fixture.expected_ctx->resume_extra.server2.alpn_protocols =
254 OPENSSL_strdup("baz");
256 fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL);
258 fixture.expected_ctx->resume_extra.client.ct_validation =
259 SSL_TEST_CT_VALIDATION_STRICT;
261 EXECUTE_SSL_TEST_CTX_TEST();
264 static const char *bad_configurations[] = {
265 "ssltest_unknown_option",
266 "ssltest_wrong_section",
267 "ssltest_unknown_expected_result",
268 "ssltest_unknown_alert",
269 "ssltest_unknown_protocol",
270 "ssltest_unknown_verify_callback",
271 "ssltest_unknown_servername",
272 "ssltest_unknown_servername_callback",
273 "ssltest_unknown_session_ticket_expected",
274 "ssltest_unknown_compression_expected",
275 "ssltest_unknown_method",
276 "ssltest_unknown_handshake_mode",
277 "ssltest_unknown_resumption_expected",
278 "ssltest_unknown_ct_validation",
281 static int test_bad_configuration(int idx)
283 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, bad_configurations[idx]);
285 if (!TEST_ptr_null(ctx)) {
286 TEST_info("Parsing bad configuration %s succeeded.",
287 bad_configurations[idx]);
288 SSL_TEST_CTX_free(ctx);
295 int test_main(int argc, char **argv)
302 conf = NCONF_new(NULL);
303 TEST_check(conf != NULL);
305 /* argv[1] should point to test/ssl_test_ctx_test.conf */
306 TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
308 ADD_TEST(test_empty_configuration);
309 ADD_TEST(test_good_configuration);
310 ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
312 result = run_tests(argv[0]);