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 if (!strings_equal("ClientNPNProtocols", ctx->client_npn_protocols,
93 ctx2->client_npn_protocols))
95 if (ctx->method != ctx2->method) {
96 fprintf(stderr, "Method mismatch: %s vs %s.\n",
97 ssl_test_method_name(ctx->method),
98 ssl_test_method_name(ctx2->method));
101 if (!strings_equal("ServerNPNProtocols", ctx->server_npn_protocols,
102 ctx2->server_npn_protocols))
104 if (!strings_equal("Server2NPNProtocols", ctx->server_npn_protocols,
105 ctx2->server_npn_protocols))
107 if (!strings_equal("ExpectedNPNProtocol", ctx->expected_npn_protocol,
108 ctx2->expected_npn_protocol))
110 if (!strings_equal("ClientALPNProtocols", ctx->client_alpn_protocols,
111 ctx2->client_alpn_protocols))
114 if (!strings_equal("ServerALPNProtocols", ctx->server_alpn_protocols,
115 ctx2->server_alpn_protocols))
117 if (!strings_equal("Server2ALPNProtocols", ctx->server_alpn_protocols,
118 ctx2->server_alpn_protocols))
120 if (!strings_equal("ExpectedALPNProtocol", ctx->expected_alpn_protocol,
121 ctx2->expected_alpn_protocol))
123 if (ctx->handshake_mode != ctx2->handshake_mode) {
124 fprintf(stderr, "HandshakeMode mismatch: %s vs %s.\n",
125 ssl_handshake_mode_name(ctx->handshake_mode),
126 ssl_handshake_mode_name(ctx2->handshake_mode));
129 if (ctx->resumption_expected != ctx2->resumption_expected) {
130 fprintf(stderr, "ResumptionExpected mismatch: %d vs %d.\n",
131 ctx->resumption_expected, ctx2->resumption_expected);
137 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
139 SSL_TEST_CTX_TEST_FIXTURE fixture;
140 fixture.test_case_name = test_case_name;
141 fixture.expected_ctx = SSL_TEST_CTX_new();
142 OPENSSL_assert(fixture.expected_ctx != NULL);
146 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
150 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
153 fprintf(stderr, "Failed to parse good configuration %s.\n",
154 fixture.test_section);
158 if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
163 SSL_TEST_CTX_free(ctx);
167 static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
169 SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
172 fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
173 fixture.test_section);
174 SSL_TEST_CTX_free(ctx);
181 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
183 SSL_TEST_CTX_free(fixture.expected_ctx);
184 ERR_print_errors_fp(stderr);
187 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
188 SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
189 #define EXECUTE_SSL_TEST_CTX_TEST() \
190 EXECUTE_TEST(execute_test, tear_down)
191 #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST() \
192 EXECUTE_TEST(execute_failure_test, tear_down)
194 static int test_empty_configuration()
196 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
197 fixture.test_section = "ssltest_default";
198 fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
199 EXECUTE_SSL_TEST_CTX_TEST();
202 static int test_good_configuration()
204 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
205 fixture.test_section = "ssltest_good";
206 fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
207 fixture.expected_ctx->client_alert = SSL_AD_UNKNOWN_CA;
208 fixture.expected_ctx->server_alert = 0; /* No alert. */
209 fixture.expected_ctx->protocol = TLS1_1_VERSION;
210 fixture.expected_ctx->client_verify_callback = SSL_TEST_VERIFY_REJECT_ALL;
211 fixture.expected_ctx->servername = SSL_TEST_SERVERNAME_SERVER2;
212 fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
213 fixture.expected_ctx->servername_callback =
214 SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
215 fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
216 fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
217 fixture.expected_ctx->client_npn_protocols = OPENSSL_strdup("foo,bar");
218 fixture.expected_ctx->server2_alpn_protocols = OPENSSL_strdup("baz");
219 OPENSSL_assert(fixture.expected_ctx->client_npn_protocols != NULL);
220 OPENSSL_assert(fixture.expected_ctx->server2_alpn_protocols != NULL);
221 fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
222 fixture.expected_ctx->resumption_expected = 1;
223 EXECUTE_SSL_TEST_CTX_TEST();
226 static const char *bad_configurations[] = {
227 "ssltest_unknown_option",
228 "ssltest_unknown_expected_result",
229 "ssltest_unknown_alert",
230 "ssltest_unknown_protocol",
231 "ssltest_unknown_verify_callback",
232 "ssltest_unknown_servername",
233 "ssltest_unknown_servername_callback",
234 "ssltest_unknown_session_ticket_expected",
235 "ssltest_unknown_method",
236 "ssltest_unknown_handshake_mode",
237 "ssltest_unknown_resumption_expected",
240 static int test_bad_configuration(int idx)
242 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
243 fixture.test_section = bad_configurations[idx];
244 EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
247 int main(int argc, char **argv)
254 conf = NCONF_new(NULL);
255 OPENSSL_assert(conf != NULL);
257 /* argv[1] should point to test/ssl_test_ctx_test.conf */
258 OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
261 ADD_TEST(test_empty_configuration);
262 ADD_TEST(test_good_configuration);
263 ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
265 result = run_tests(argv[0]);