]>
Commit | Line | Data |
---|---|---|
453dfd8d | 1 | /* |
019e47ce | 2 | * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved. |
453dfd8d | 3 | * |
440e5d80 RS |
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 | |
453dfd8d | 7 | * https://www.openssl.org/source/license.html |
453dfd8d EK |
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> | |
ce2cdac2 | 16 | #include <string.h> |
453dfd8d | 17 | |
b99fe5f4 | 18 | #include <internal/nelem.h> |
453dfd8d EK |
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 | ||
9f48bbac | 35 | |
1f9d203d RS |
36 | static int clientconf_eq(SSL_TEST_CLIENT_CONF *conf1, |
37 | SSL_TEST_CLIENT_CONF *conf2) | |
9f48bbac | 38 | { |
1f9d203d RS |
39 | if (!TEST_int_eq(conf1->verify_callback, conf2->verify_callback) |
40 | || !TEST_int_eq(conf1->servername, conf2->servername) | |
41 | || !TEST_str_eq(conf1->npn_protocols, conf2->npn_protocols) | |
42 | || !TEST_str_eq(conf1->alpn_protocols, conf2->alpn_protocols) | |
43 | || !TEST_int_eq(conf1->ct_validation, conf2->ct_validation)) | |
9f48bbac | 44 | return 0; |
9f48bbac EK |
45 | return 1; |
46 | } | |
47 | ||
1f9d203d RS |
48 | static int serverconf_eq(SSL_TEST_SERVER_CONF *serv, |
49 | SSL_TEST_SERVER_CONF *serv2) | |
9f48bbac | 50 | { |
1f9d203d RS |
51 | if (!TEST_int_eq(serv->servername_callback, serv2->servername_callback) |
52 | || !TEST_str_eq(serv->npn_protocols, serv2->npn_protocols) | |
53 | || !TEST_str_eq(serv->alpn_protocols, serv2->alpn_protocols) | |
54 | || !TEST_int_eq(serv->broken_session_ticket, | |
55 | serv2->broken_session_ticket) | |
56 | || !TEST_int_eq(serv->cert_status, serv2->cert_status)) | |
9f48bbac | 57 | return 0; |
9f48bbac EK |
58 | return 1; |
59 | } | |
60 | ||
1f9d203d RS |
61 | static int extraconf_eq(SSL_TEST_EXTRA_CONF *extra, |
62 | SSL_TEST_EXTRA_CONF *extra2) | |
9f48bbac | 63 | { |
1f9d203d RS |
64 | if (!TEST_true(clientconf_eq(&extra->client, &extra2->client)) |
65 | || !TEST_true(serverconf_eq(&extra->server, &extra2->server)) | |
66 | || !TEST_true(serverconf_eq(&extra->server2, &extra2->server2))) | |
67 | return 0; | |
68 | return 1; | |
9f48bbac EK |
69 | } |
70 | ||
1f9d203d | 71 | static int testctx_eq(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2) |
453dfd8d | 72 | { |
1f9d203d RS |
73 | if (!TEST_int_eq(ctx->method, ctx2->method) |
74 | || !TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode) | |
75 | || !TEST_int_eq(ctx->app_data_size, ctx2->app_data_size) | |
76 | || !TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size) | |
77 | || !extraconf_eq(&ctx->extra, &ctx2->extra) | |
78 | || !extraconf_eq(&ctx->resume_extra, &ctx2->resume_extra) | |
79 | || !TEST_int_eq(ctx->expected_result, ctx2->expected_result) | |
80 | || !TEST_int_eq(ctx->expected_client_alert, | |
81 | ctx2->expected_client_alert) | |
82 | || !TEST_int_eq(ctx->expected_server_alert, | |
83 | ctx2->expected_server_alert) | |
84 | || !TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol) | |
85 | || !TEST_int_eq(ctx->expected_servername, ctx2->expected_servername) | |
86 | || !TEST_int_eq(ctx->session_ticket_expected, | |
87 | ctx2->session_ticket_expected) | |
88 | || !TEST_int_eq(ctx->compression_expected, | |
89 | ctx2->compression_expected) | |
90 | || !TEST_str_eq(ctx->expected_npn_protocol, | |
91 | ctx2->expected_npn_protocol) | |
92 | || !TEST_str_eq(ctx->expected_alpn_protocol, | |
93 | ctx2->expected_alpn_protocol) | |
94 | || !TEST_int_eq(ctx->resumption_expected, | |
95 | ctx2->resumption_expected)) | |
590ed3d7 | 96 | return 0; |
453dfd8d EK |
97 | return 1; |
98 | } | |
99 | ||
2326bba0 | 100 | static SSL_TEST_CTX_TEST_FIXTURE *set_up(const char *const test_case_name) |
453dfd8d | 101 | { |
2326bba0 P |
102 | SSL_TEST_CTX_TEST_FIXTURE *fixture; |
103 | ||
104 | if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) | |
105 | return NULL; | |
106 | fixture->test_case_name = test_case_name; | |
107 | if (!TEST_ptr(fixture->expected_ctx = SSL_TEST_CTX_new())) { | |
108 | OPENSSL_free(fixture); | |
109 | return NULL; | |
110 | } | |
453dfd8d EK |
111 | return fixture; |
112 | } | |
113 | ||
2326bba0 | 114 | static int execute_test(SSL_TEST_CTX_TEST_FIXTURE *fixture) |
453dfd8d | 115 | { |
ababe86b | 116 | int success = 0; |
1f9d203d | 117 | SSL_TEST_CTX *ctx; |
453dfd8d | 118 | |
2326bba0 P |
119 | if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture->test_section)) |
120 | || !testctx_eq(ctx, fixture->expected_ctx)) | |
453dfd8d EK |
121 | goto err; |
122 | ||
ababe86b | 123 | success = 1; |
453dfd8d EK |
124 | err: |
125 | SSL_TEST_CTX_free(ctx); | |
ababe86b | 126 | return success; |
453dfd8d EK |
127 | } |
128 | ||
2326bba0 | 129 | static void tear_down(SSL_TEST_CTX_TEST_FIXTURE *fixture) |
453dfd8d | 130 | { |
2326bba0 P |
131 | SSL_TEST_CTX_free(fixture->expected_ctx); |
132 | OPENSSL_free(fixture); | |
453dfd8d EK |
133 | } |
134 | ||
1f9d203d | 135 | #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \ |
99801878 | 136 | SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up); |
1f9d203d | 137 | #define EXECUTE_SSL_TEST_CTX_TEST() \ |
453dfd8d | 138 | EXECUTE_TEST(execute_test, tear_down) |
453dfd8d | 139 | |
31a80694 | 140 | static int test_empty_configuration(void) |
453dfd8d EK |
141 | { |
142 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
99801878 P |
143 | if (fixture == NULL) |
144 | return 0; | |
2326bba0 P |
145 | fixture->test_section = "ssltest_default"; |
146 | fixture->expected_ctx->expected_result = SSL_TEST_SUCCESS; | |
453dfd8d | 147 | EXECUTE_SSL_TEST_CTX_TEST(); |
99801878 | 148 | return result; |
453dfd8d EK |
149 | } |
150 | ||
31a80694 | 151 | static int test_good_configuration(void) |
453dfd8d EK |
152 | { |
153 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
99801878 P |
154 | if (fixture == NULL) |
155 | return 0; | |
2326bba0 P |
156 | fixture->test_section = "ssltest_good"; |
157 | fixture->expected_ctx->method = SSL_TEST_METHOD_DTLS; | |
158 | fixture->expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME; | |
159 | fixture->expected_ctx->app_data_size = 1024; | |
160 | fixture->expected_ctx->max_fragment_size = 2048; | |
161 | ||
162 | fixture->expected_ctx->expected_result = SSL_TEST_SERVER_FAIL; | |
163 | fixture->expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA; | |
164 | fixture->expected_ctx->expected_server_alert = 0; /* No alert. */ | |
165 | fixture->expected_ctx->expected_protocol = TLS1_1_VERSION; | |
166 | fixture->expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2; | |
167 | fixture->expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES; | |
168 | fixture->expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO; | |
169 | fixture->expected_ctx->resumption_expected = 1; | |
170 | ||
171 | fixture->expected_ctx->extra.client.verify_callback = | |
9f48bbac | 172 | SSL_TEST_VERIFY_REJECT_ALL; |
2326bba0 P |
173 | fixture->expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2; |
174 | fixture->expected_ctx->extra.client.npn_protocols = | |
9f48bbac | 175 | OPENSSL_strdup("foo,bar"); |
2326bba0 | 176 | if (!TEST_ptr(fixture->expected_ctx->extra.client.npn_protocols)) |
019e47ce | 177 | goto err; |
9f48bbac | 178 | |
2326bba0 | 179 | fixture->expected_ctx->extra.server.servername_callback = |
9f48bbac | 180 | SSL_TEST_SERVERNAME_IGNORE_MISMATCH; |
2326bba0 | 181 | fixture->expected_ctx->extra.server.broken_session_ticket = 1; |
9f48bbac | 182 | |
2326bba0 | 183 | fixture->expected_ctx->resume_extra.server2.alpn_protocols = |
9f48bbac | 184 | OPENSSL_strdup("baz"); |
2326bba0 | 185 | if (!TEST_ptr(fixture->expected_ctx->resume_extra.server2.alpn_protocols)) |
019e47ce | 186 | goto err; |
9f48bbac | 187 | |
2326bba0 | 188 | fixture->expected_ctx->resume_extra.client.ct_validation = |
da085d27 EK |
189 | SSL_TEST_CT_VALIDATION_STRICT; |
190 | ||
453dfd8d | 191 | EXECUTE_SSL_TEST_CTX_TEST(); |
99801878 | 192 | return result; |
019e47ce P |
193 | |
194 | err: | |
195 | tear_down(fixture); | |
196 | return 0; | |
453dfd8d EK |
197 | } |
198 | ||
199 | static const char *bad_configurations[] = { | |
200 | "ssltest_unknown_option", | |
6bd3379a | 201 | "ssltest_wrong_section", |
453dfd8d EK |
202 | "ssltest_unknown_expected_result", |
203 | "ssltest_unknown_alert", | |
204 | "ssltest_unknown_protocol", | |
a263f320 | 205 | "ssltest_unknown_verify_callback", |
5c753de6 | 206 | "ssltest_unknown_servername", |
d2b23cd2 | 207 | "ssltest_unknown_servername_callback", |
5c753de6 | 208 | "ssltest_unknown_session_ticket_expected", |
439db0c9 | 209 | "ssltest_unknown_compression_expected", |
74726750 | 210 | "ssltest_unknown_method", |
590ed3d7 EK |
211 | "ssltest_unknown_handshake_mode", |
212 | "ssltest_unknown_resumption_expected", | |
da085d27 | 213 | "ssltest_unknown_ct_validation", |
453dfd8d EK |
214 | }; |
215 | ||
216 | static int test_bad_configuration(int idx) | |
217 | { | |
1f9d203d | 218 | SSL_TEST_CTX *ctx; |
bd91e3c8 | 219 | |
1f9d203d RS |
220 | if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf, |
221 | bad_configurations[idx]))) { | |
d836d71b EK |
222 | SSL_TEST_CTX_free(ctx); |
223 | return 0; | |
224 | } | |
225 | ||
226 | return 1; | |
453dfd8d EK |
227 | } |
228 | ||
ad887416 | 229 | int setup_tests(void) |
453dfd8d | 230 | { |
ad887416 P |
231 | if (!TEST_ptr(conf = NCONF_new(NULL))) |
232 | return 0; | |
233 | /* argument should point to test/ssl_test_ctx_test.conf */ | |
234 | if (!TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)) { | |
235 | TEST_note("Missing file argument"); | |
236 | return 0; | |
1f9d203d | 237 | } |
453dfd8d EK |
238 | |
239 | ADD_TEST(test_empty_configuration); | |
240 | ADD_TEST(test_good_configuration); | |
241 | ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations)); | |
ad887416 P |
242 | return 1; |
243 | } | |
453dfd8d | 244 | |
ad887416 P |
245 | void cleanup_tests(void) |
246 | { | |
453dfd8d | 247 | NCONF_free(conf); |
453dfd8d | 248 | } |