]>
Commit | Line | Data |
---|---|---|
453dfd8d EK |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
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> | |
16 | ||
17 | #include "e_os.h" | |
18 | #include "ssl_test_ctx.h" | |
19 | #include "testutil.h" | |
20 | #include <openssl/e_os2.h> | |
21 | #include <openssl/err.h> | |
22 | #include <openssl/conf.h> | |
23 | #include <openssl/ssl.h> | |
24 | ||
25 | static CONF *conf = NULL; | |
26 | ||
27 | typedef struct ssl_test_ctx_test_fixture { | |
28 | const char *test_case_name; | |
29 | const char *test_section; | |
30 | /* Expected parsed configuration. */ | |
31 | SSL_TEST_CTX *expected_ctx; | |
32 | } SSL_TEST_CTX_TEST_FIXTURE; | |
33 | ||
34 | /* Returns 1 if the contexts are equal, 0 otherwise. */ | |
35 | static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2) | |
36 | { | |
37 | if (ctx->expected_result != ctx2->expected_result) { | |
38 | fprintf(stderr, "ExpectedResult mismatch: %s vs %s.\n", | |
a263f320 EK |
39 | ssl_test_result_name(ctx->expected_result), |
40 | ssl_test_result_name(ctx2->expected_result)); | |
453dfd8d EK |
41 | return 0; |
42 | } | |
43 | if (ctx->client_alert != ctx2->client_alert) { | |
44 | fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n", | |
a263f320 EK |
45 | ssl_alert_name(ctx->client_alert), |
46 | ssl_alert_name(ctx2->client_alert)); | |
453dfd8d EK |
47 | return 0; |
48 | } | |
49 | if (ctx->server_alert != ctx2->server_alert) { | |
50 | fprintf(stderr, "ServerAlert mismatch: %s vs %s.\n", | |
a263f320 EK |
51 | ssl_alert_name(ctx->server_alert), |
52 | ssl_alert_name(ctx2->server_alert)); | |
453dfd8d EK |
53 | return 0; |
54 | } | |
55 | if (ctx->protocol != ctx2->protocol) { | |
56 | fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n", | |
a263f320 EK |
57 | ssl_protocol_name(ctx->protocol), |
58 | ssl_protocol_name(ctx2->protocol)); | |
59 | return 0; | |
60 | } | |
61 | if (ctx->client_verify_callback != ctx2->client_verify_callback) { | |
62 | fprintf(stderr, "ClientVerifyCallback mismatch: %s vs %s.\n", | |
63 | ssl_verify_callback_name(ctx->client_verify_callback), | |
64 | ssl_verify_callback_name(ctx2->client_verify_callback)); | |
453dfd8d EK |
65 | return 0; |
66 | } | |
5c753de6 TS |
67 | if (ctx->servername != ctx2->servername) { |
68 | fprintf(stderr, "ServerName mismatch: %s vs %s.\n", | |
69 | ssl_servername_name(ctx->servername), | |
70 | ssl_servername_name(ctx2->servername)); | |
71 | return 0; | |
72 | } | |
73 | if (ctx->session_ticket_expected != ctx2->session_ticket_expected) { | |
74 | fprintf(stderr, "SessionTicketExpected mismatch: %s vs %s.\n", | |
81fc33c9 EK |
75 | ssl_session_ticket_name(ctx->session_ticket_expected), |
76 | ssl_session_ticket_name(ctx2->session_ticket_expected)); | |
5c753de6 TS |
77 | return 0; |
78 | } | |
453dfd8d EK |
79 | |
80 | return 1; | |
81 | } | |
82 | ||
83 | static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name) | |
84 | { | |
85 | SSL_TEST_CTX_TEST_FIXTURE fixture; | |
86 | fixture.test_case_name = test_case_name; | |
87 | fixture.expected_ctx = SSL_TEST_CTX_new(); | |
88 | OPENSSL_assert(fixture.expected_ctx != NULL); | |
89 | return fixture; | |
90 | } | |
91 | ||
92 | static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
93 | { | |
ababe86b | 94 | int success = 0; |
453dfd8d EK |
95 | |
96 | SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section); | |
97 | ||
98 | if (ctx == NULL) { | |
99 | fprintf(stderr, "Failed to parse good configuration %s.\n", | |
100 | fixture.test_section); | |
101 | goto err; | |
102 | } | |
103 | ||
104 | if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx)) | |
105 | goto err; | |
106 | ||
ababe86b | 107 | success = 1; |
453dfd8d EK |
108 | err: |
109 | SSL_TEST_CTX_free(ctx); | |
ababe86b | 110 | return success; |
453dfd8d EK |
111 | } |
112 | ||
113 | static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
114 | { | |
115 | SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section); | |
116 | ||
117 | if (ctx != NULL) { | |
118 | fprintf(stderr, "Parsing bad configuration %s succeeded.\n", | |
119 | fixture.test_section); | |
120 | SSL_TEST_CTX_free(ctx); | |
ababe86b | 121 | return 0; |
453dfd8d EK |
122 | } |
123 | ||
ababe86b | 124 | return 1; |
453dfd8d EK |
125 | } |
126 | ||
127 | static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
128 | { | |
129 | SSL_TEST_CTX_free(fixture.expected_ctx); | |
130 | ERR_print_errors_fp(stderr); | |
131 | } | |
132 | ||
133 | #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \ | |
134 | SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up) | |
135 | #define EXECUTE_SSL_TEST_CTX_TEST() \ | |
136 | EXECUTE_TEST(execute_test, tear_down) | |
137 | #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST() \ | |
138 | EXECUTE_TEST(execute_failure_test, tear_down) | |
139 | ||
140 | static int test_empty_configuration() | |
141 | { | |
142 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
143 | fixture.test_section = "ssltest_default"; | |
144 | fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS; | |
145 | EXECUTE_SSL_TEST_CTX_TEST(); | |
146 | } | |
147 | ||
148 | static int test_good_configuration() | |
149 | { | |
150 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
151 | fixture.test_section = "ssltest_good"; | |
152 | fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL; | |
153 | fixture.expected_ctx->client_alert = SSL_AD_UNKNOWN_CA; | |
154 | fixture.expected_ctx->server_alert = 0; /* No alert. */ | |
155 | fixture.expected_ctx->protocol = TLS1_1_VERSION; | |
5c753de6 TS |
156 | fixture.expected_ctx->client_verify_callback = SSL_TEST_VERIFY_REJECT_ALL; |
157 | fixture.expected_ctx->servername = SSL_TEST_SERVERNAME_SERVER2; | |
158 | fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES; | |
453dfd8d EK |
159 | EXECUTE_SSL_TEST_CTX_TEST(); |
160 | } | |
161 | ||
162 | static const char *bad_configurations[] = { | |
163 | "ssltest_unknown_option", | |
164 | "ssltest_unknown_expected_result", | |
165 | "ssltest_unknown_alert", | |
166 | "ssltest_unknown_protocol", | |
a263f320 | 167 | "ssltest_unknown_verify_callback", |
5c753de6 TS |
168 | "ssltest_unknown_servername", |
169 | "ssltest_unknown_session_ticket_expected", | |
453dfd8d EK |
170 | }; |
171 | ||
172 | static int test_bad_configuration(int idx) | |
173 | { | |
174 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
175 | fixture.test_section = bad_configurations[idx]; | |
176 | EXECUTE_SSL_TEST_CTX_FAILURE_TEST(); | |
177 | } | |
178 | ||
179 | int main(int argc, char **argv) | |
180 | { | |
181 | int result = 0; | |
182 | ||
183 | if (argc != 2) | |
184 | return 1; | |
185 | ||
186 | conf = NCONF_new(NULL); | |
187 | OPENSSL_assert(conf != NULL); | |
188 | ||
189 | /* argv[1] should point to test/ssl_test_ctx_test.conf */ | |
190 | OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0); | |
191 | ||
192 | ||
193 | ADD_TEST(test_empty_configuration); | |
194 | ADD_TEST(test_good_configuration); | |
195 | ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations)); | |
196 | ||
197 | result = run_tests(argv[0]); | |
198 | ||
199 | NCONF_free(conf); | |
200 | ||
201 | return result; | |
202 | } |