Make test marcos for true/false checks reliable for all integral types.
[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 "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>
26
27 static CONF *conf = NULL;
28
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;
35
36
37 static int SSL_TEST_CLIENT_CONF_equal(SSL_TEST_CLIENT_CONF *client,
38                                       SSL_TEST_CLIENT_CONF *client2)
39 {
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));
44         return 0;
45     }
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));
50         return 0;
51     }
52     if (!TEST_str_eq(client->npn_protocols, client2->npn_protocols))
53         return 0;
54     if (!TEST_str_eq(client->alpn_protocols, client2->alpn_protocols))
55         return 0;
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));
60         return 0;
61     }
62     return 1;
63 }
64
65 static int SSL_TEST_SERVER_CONF_equal(SSL_TEST_SERVER_CONF *server,
66                                       SSL_TEST_SERVER_CONF *server2)
67 {
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));
73         return 0;
74     }
75     if (!TEST_str_eq(server->npn_protocols, server2->npn_protocols))
76         return 0;
77     if (!TEST_str_eq(server->alpn_protocols, server2->alpn_protocols))
78         return 0;
79     if (!TEST_int_eq(server->broken_session_ticket,
80                      server2->broken_session_ticket))
81         return 0;
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));
86         return 0;
87     }
88     return 1;
89 }
90
91 static int SSL_TEST_EXTRA_CONF_equal(SSL_TEST_EXTRA_CONF *extra,
92                                      SSL_TEST_EXTRA_CONF *extra2)
93 {
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);
97 }
98
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)
101 {
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));
106         return 0;
107     }
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));
112         return 0;
113     }
114     if (!TEST_int_eq(ctx->app_data_size, ctx2->app_data_size))
115         return 0;
116
117     if (!TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size))
118         return 0;
119
120     if (!SSL_TEST_EXTRA_CONF_equal(&ctx->extra, &ctx2->extra))
121         return 0;
122     if (!SSL_TEST_EXTRA_CONF_equal(&ctx->resume_extra, &ctx2->resume_extra))
123         return 0;
124
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));
129         return 0;
130     }
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));
135         return 0;
136     }
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));
141         return 0;
142     }
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));
147         return 0;
148     }
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));
153         return 0;
154     }
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));
160         return 0;
161     }
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);
166         return 0;
167     }
168     if (!TEST_str_eq(ctx->expected_npn_protocol, ctx2->expected_npn_protocol))
169         return 0;
170     if (!TEST_str_eq(ctx->expected_alpn_protocol, ctx2->expected_alpn_protocol))
171         return 0;
172     if (!TEST_int_eq(ctx->resumption_expected, ctx2->resumption_expected))
173         return 0;
174     return 1;
175 }
176
177 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
178 {
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);
183     return fixture;
184 }
185
186 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
187 {
188     int success = 0;
189
190     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
191
192     if (!TEST_ptr(ctx)) {
193         TEST_info("Failed to parse good configuration %s.",
194                   fixture.test_section);
195         goto err;
196     }
197
198     if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
199         goto err;
200
201     success = 1;
202  err:
203     SSL_TEST_CTX_free(ctx);
204     return success;
205 }
206
207 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
208 {
209     SSL_TEST_CTX_free(fixture.expected_ctx);
210 }
211
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)
216
217 static int test_empty_configuration()
218 {
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();
223 }
224
225 static int test_good_configuration()
226 {
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;
233
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;
242
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);
249
250     fixture.expected_ctx->extra.server.servername_callback =
251         SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
252     fixture.expected_ctx->extra.server.broken_session_ticket = 1;
253
254     fixture.expected_ctx->resume_extra.server2.alpn_protocols =
255         OPENSSL_strdup("baz");
256     TEST_check(
257         fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL);
258
259     fixture.expected_ctx->resume_extra.client.ct_validation =
260         SSL_TEST_CT_VALIDATION_STRICT;
261
262     EXECUTE_SSL_TEST_CTX_TEST();
263 }
264
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",
280 };
281
282 static int test_bad_configuration(int idx)
283 {
284     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, bad_configurations[idx]);
285
286     if (!TEST_ptr_null(ctx)) {
287         TEST_info("Parsing bad configuration %s succeeded.",
288                   bad_configurations[idx]);
289         SSL_TEST_CTX_free(ctx);
290         return 0;
291     }
292
293     return 1;
294 }
295
296 int test_main(int argc, char **argv)
297 {
298     int result = 0;
299
300     if (argc != 2)
301         return 1;
302
303     conf = NCONF_new(NULL);
304     TEST_check(conf != NULL);
305
306     /* argv[1] should point to test/ssl_test_ctx_test.conf */
307     TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
308
309     ADD_TEST(test_empty_configuration);
310     ADD_TEST(test_good_configuration);
311     ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
312
313     result = run_tests(argv[0]);
314
315     NCONF_free(conf);
316
317     return result;
318 }