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 "test_main_custom.h"
22 #include <openssl/e_os2.h>
23 #include <openssl/err.h>
24 #include <openssl/conf.h>
25 #include <openssl/ssl.h>
27 static CONF *conf = NULL;
29 typedef struct ssl_test_ctx_test_fixture {
30 const char *test_case_name;
31 const char *test_section;
32 /* Expected parsed configuration. */
33 SSL_TEST_CTX *expected_ctx;
34 } SSL_TEST_CTX_TEST_FIXTURE;
37 static int SSL_TEST_CLIENT_CONF_equal(SSL_TEST_CLIENT_CONF *client,
38 SSL_TEST_CLIENT_CONF *client2)
40 if (!TEST_int_eq(client->verify_callback, client2->verify_callback)) {
41 TEST_info("ClientVerifyCallback mismatch: %s vs %s.",
42 ssl_verify_callback_name(client->verify_callback),
43 ssl_verify_callback_name(client2->verify_callback));
46 if (!TEST_int_eq(client->servername, client2->servername)) {
47 TEST_info("ServerName mismatch: %s vs %s.",
48 ssl_servername_name(client->servername),
49 ssl_servername_name(client2->servername));
52 if (!TEST_str_eq(client->npn_protocols, client2->npn_protocols))
54 if (!TEST_str_eq(client->alpn_protocols, client2->alpn_protocols))
56 if (!TEST_int_eq(client->ct_validation, client2->ct_validation)) {
57 TEST_info("CTValidation mismatch: %s vs %s.",
58 ssl_ct_validation_name(client->ct_validation),
59 ssl_ct_validation_name(client2->ct_validation));
65 static int SSL_TEST_SERVER_CONF_equal(SSL_TEST_SERVER_CONF *server,
66 SSL_TEST_SERVER_CONF *server2)
68 if (!TEST_int_eq(server->servername_callback,
69 server2->servername_callback)) {
70 TEST_info("ServerNameCallback mismatch: %s vs %s.",
71 ssl_servername_callback_name(server->servername_callback),
72 ssl_servername_callback_name(server2->servername_callback));
75 if (!TEST_str_eq(server->npn_protocols, server2->npn_protocols))
77 if (!TEST_str_eq(server->alpn_protocols, server2->alpn_protocols))
79 if (!TEST_int_eq(server->broken_session_ticket,
80 server2->broken_session_ticket))
82 if (!TEST_int_eq(server->cert_status, server2->cert_status)) {
83 TEST_info("CertStatus mismatch: %s vs %s.",
84 ssl_certstatus_name(server->cert_status),
85 ssl_certstatus_name(server2->cert_status));
91 static int SSL_TEST_EXTRA_CONF_equal(SSL_TEST_EXTRA_CONF *extra,
92 SSL_TEST_EXTRA_CONF *extra2)
94 return SSL_TEST_CLIENT_CONF_equal(&extra->client, &extra2->client)
95 && SSL_TEST_SERVER_CONF_equal(&extra->server, &extra2->server)
96 && SSL_TEST_SERVER_CONF_equal(&extra->server2, &extra2->server2);
99 /* Returns 1 if the contexts are equal, 0 otherwise. */
100 static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
102 if (!TEST_int_eq(ctx->method, ctx2->method)) {
103 TEST_info("Method mismatch: %s vs %s.",
104 ssl_test_method_name(ctx->method),
105 ssl_test_method_name(ctx2->method));
108 if (!TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode)) {
109 TEST_info("HandshakeMode mismatch: %s vs %s.",
110 ssl_handshake_mode_name(ctx->handshake_mode),
111 ssl_handshake_mode_name(ctx2->handshake_mode));
114 if (!TEST_int_eq(ctx->app_data_size, ctx2->app_data_size))
117 if (!TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size))
120 if (!SSL_TEST_EXTRA_CONF_equal(&ctx->extra, &ctx2->extra))
122 if (!SSL_TEST_EXTRA_CONF_equal(&ctx->resume_extra, &ctx2->resume_extra))
125 if (!TEST_int_eq(ctx->expected_result, ctx2->expected_result)) {
126 TEST_info("ExpectedResult mismatch: %s vs %s.",
127 ssl_test_result_name(ctx->expected_result),
128 ssl_test_result_name(ctx2->expected_result));
131 if (!TEST_int_eq(ctx->expected_client_alert, ctx2->expected_client_alert)) {
132 TEST_info("ClientAlert mismatch: %s vs %s.",
133 ssl_alert_name(ctx->expected_client_alert),
134 ssl_alert_name(ctx2->expected_client_alert));
137 if (!TEST_int_eq(ctx->expected_server_alert, ctx2->expected_server_alert)) {
138 TEST_info("ServerAlert mismatch: %s vs %s.",
139 ssl_alert_name(ctx->expected_server_alert),
140 ssl_alert_name(ctx2->expected_server_alert));
143 if (!TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol)) {
144 TEST_info("ClientAlert mismatch: %s vs %s.",
145 ssl_protocol_name(ctx->expected_protocol),
146 ssl_protocol_name(ctx2->expected_protocol));
149 if (!TEST_int_eq(ctx->expected_servername, ctx2->expected_servername)) {
150 TEST_info("ExpectedServerName mismatch: %s vs %s.",
151 ssl_servername_name(ctx->expected_servername),
152 ssl_servername_name(ctx2->expected_servername));
155 if (!TEST_int_eq(ctx->session_ticket_expected,
156 ctx2->session_ticket_expected)) {
157 TEST_info("SessionTicketExpected mismatch: %s vs %s.",
158 ssl_session_ticket_name(ctx->session_ticket_expected),
159 ssl_session_ticket_name(ctx2->session_ticket_expected));
162 if (!TEST_int_eq(ctx->compression_expected, ctx2->compression_expected)) {
163 TEST_info("ComrpessionExpected mismatch: %d vs %d.",
164 ctx->compression_expected,
165 ctx2->compression_expected);
168 if (!TEST_str_eq(ctx->expected_npn_protocol, ctx2->expected_npn_protocol))
170 if (!TEST_str_eq(ctx->expected_alpn_protocol, ctx2->expected_alpn_protocol))
172 if (!TEST_int_eq(ctx->resumption_expected, ctx2->resumption_expected))
177 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
179 SSL_TEST_CTX_TEST_FIXTURE fixture;
180 fixture.test_case_name = test_case_name;
181 fixture.expected_ctx = SSL_TEST_CTX_new();
182 TEST_check(fixture.expected_ctx != NULL);
186 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
190 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
192 if (!TEST_ptr(ctx)) {
193 TEST_info("Failed to parse good configuration %s.",
194 fixture.test_section);
198 if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
203 SSL_TEST_CTX_free(ctx);
207 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
209 SSL_TEST_CTX_free(fixture.expected_ctx);
212 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
213 SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
214 #define EXECUTE_SSL_TEST_CTX_TEST() \
215 EXECUTE_TEST(execute_test, tear_down)
217 static int test_empty_configuration()
219 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
220 fixture.test_section = "ssltest_default";
221 fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
222 EXECUTE_SSL_TEST_CTX_TEST();
225 static int test_good_configuration()
227 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
228 fixture.test_section = "ssltest_good";
229 fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
230 fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
231 fixture.expected_ctx->app_data_size = 1024;
232 fixture.expected_ctx->max_fragment_size = 2048;
234 fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
235 fixture.expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
236 fixture.expected_ctx->expected_server_alert = 0; /* No alert. */
237 fixture.expected_ctx->expected_protocol = TLS1_1_VERSION;
238 fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
239 fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
240 fixture.expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
241 fixture.expected_ctx->resumption_expected = 1;
243 fixture.expected_ctx->extra.client.verify_callback =
244 SSL_TEST_VERIFY_REJECT_ALL;
245 fixture.expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
246 fixture.expected_ctx->extra.client.npn_protocols =
247 OPENSSL_strdup("foo,bar");
248 TEST_check(fixture.expected_ctx->extra.client.npn_protocols != NULL);
250 fixture.expected_ctx->extra.server.servername_callback =
251 SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
252 fixture.expected_ctx->extra.server.broken_session_ticket = 1;
254 fixture.expected_ctx->resume_extra.server2.alpn_protocols =
255 OPENSSL_strdup("baz");
257 fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL);
259 fixture.expected_ctx->resume_extra.client.ct_validation =
260 SSL_TEST_CT_VALIDATION_STRICT;
262 EXECUTE_SSL_TEST_CTX_TEST();
265 static const char *bad_configurations[] = {
266 "ssltest_unknown_option",
267 "ssltest_wrong_section",
268 "ssltest_unknown_expected_result",
269 "ssltest_unknown_alert",
270 "ssltest_unknown_protocol",
271 "ssltest_unknown_verify_callback",
272 "ssltest_unknown_servername",
273 "ssltest_unknown_servername_callback",
274 "ssltest_unknown_session_ticket_expected",
275 "ssltest_unknown_compression_expected",
276 "ssltest_unknown_method",
277 "ssltest_unknown_handshake_mode",
278 "ssltest_unknown_resumption_expected",
279 "ssltest_unknown_ct_validation",
282 static int test_bad_configuration(int idx)
284 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, bad_configurations[idx]);
286 if (!TEST_ptr_null(ctx)) {
287 TEST_info("Parsing bad configuration %s succeeded.",
288 bad_configurations[idx]);
289 SSL_TEST_CTX_free(ctx);
296 int test_main(int argc, char **argv)
303 conf = NCONF_new(NULL);
304 TEST_check(conf != NULL);
306 /* argv[1] should point to test/ssl_test_ctx_test.conf */
307 TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
309 ADD_TEST(test_empty_configuration);
310 ADD_TEST(test_good_configuration);
311 ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
313 result = run_tests(argv[0]);