Discard a dead option
[openssl.git] / test / ssl_test_ctx_test.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
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.
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "e_os.h"
19 #include "ssl_test_ctx.h"
20 #include "testutil.h"
21 #include <openssl/e_os2.h>
22 #include <openssl/err.h>
23 #include <openssl/conf.h>
24 #include <openssl/ssl.h>
25
26 static CONF *conf = NULL;
27
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;
34
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)
37 {
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));
42         return 0;
43     }
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));
48         return 0;
49     }
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));
54         return 0;
55     }
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));
60         return 0;
61     }
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));
66         return 0;
67     }
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));
72         return 0;
73     }
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));
78         return 0;
79     }
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));
84         return 0;
85     }
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));
90         return 0;
91     }
92     if (!strings_equal("ClientNPNProtocols", ctx->client_npn_protocols,
93                        ctx2->client_npn_protocols))
94         return 0;
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));
99         return 0;
100     }
101     if (!strings_equal("ServerNPNProtocols", ctx->server_npn_protocols,
102                        ctx2->server_npn_protocols))
103         return 0;
104     if (!strings_equal("Server2NPNProtocols", ctx->server_npn_protocols,
105                        ctx2->server_npn_protocols))
106         return 0;
107     if (!strings_equal("ExpectedNPNProtocol", ctx->expected_npn_protocol,
108                        ctx2->expected_npn_protocol))
109         return 0;
110     if (!strings_equal("ClientALPNProtocols", ctx->client_alpn_protocols,
111                        ctx2->client_alpn_protocols))
112         return 0;
113
114     if (!strings_equal("ServerALPNProtocols", ctx->server_alpn_protocols,
115                        ctx2->server_alpn_protocols))
116         return 0;
117     if (!strings_equal("Server2ALPNProtocols", ctx->server_alpn_protocols,
118                        ctx2->server_alpn_protocols))
119         return 0;
120     if (!strings_equal("ExpectedALPNProtocol", ctx->expected_alpn_protocol,
121                        ctx2->expected_alpn_protocol))
122         return 0;
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));
127         return 0;
128     }
129     if (ctx->resumption_expected != ctx2->resumption_expected) {
130         fprintf(stderr, "ResumptionExpected mismatch: %d vs %d.\n",
131                 ctx->resumption_expected, ctx2->resumption_expected);
132         return 0;
133     }
134     return 1;
135 }
136
137 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
138 {
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);
143     return fixture;
144 }
145
146 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
147 {
148     int success = 0;
149
150     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
151
152     if (ctx == NULL) {
153         fprintf(stderr, "Failed to parse good configuration %s.\n",
154                 fixture.test_section);
155         goto err;
156     }
157
158     if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
159         goto err;
160
161     success = 1;
162  err:
163     SSL_TEST_CTX_free(ctx);
164     return success;
165 }
166
167 static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
168 {
169     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
170
171     if (ctx != NULL) {
172         fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
173                 fixture.test_section);
174         SSL_TEST_CTX_free(ctx);
175         return 0;
176     }
177
178     return 1;
179 }
180
181 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
182 {
183     SSL_TEST_CTX_free(fixture.expected_ctx);
184     ERR_print_errors_fp(stderr);
185 }
186
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)
193
194 static int test_empty_configuration()
195 {
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();
200 }
201
202 static int test_good_configuration()
203 {
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();
224 }
225
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",
238 };
239
240 static int test_bad_configuration(int idx)
241 {
242         SETUP_SSL_TEST_CTX_TEST_FIXTURE();
243         fixture.test_section = bad_configurations[idx];
244         EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
245 }
246
247 int main(int argc, char **argv)
248 {
249     int result = 0;
250
251     if (argc != 2)
252         return 1;
253
254     conf = NCONF_new(NULL);
255     OPENSSL_assert(conf != NULL);
256
257     /* argv[1] should point to test/ssl_test_ctx_test.conf */
258     OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
259
260
261     ADD_TEST(test_empty_configuration);
262     ADD_TEST(test_good_configuration);
263     ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
264
265     result = run_tests(argv[0]);
266
267     NCONF_free(conf);
268
269     return result;
270 }