]>
Commit | Line | Data |
---|---|---|
453dfd8d EK |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the OpenSSL licenses, (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * https://www.openssl.org/source/license.html | |
8 | * or in the file LICENSE in the source distribution. | |
9 | */ | |
10 | ||
11 | /* | |
12 | * Ideally, CONF should offer standard parsing methods and cover them | |
13 | * in tests. But since we have no CONF tests, we use a custom test for now. | |
14 | */ | |
15 | ||
16 | #include <stdio.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_t_name(ctx->expected_result), | |
41 | ssl_test_result_t_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->expected_result), | |
47 | ssl_alert_name(ctx2->expected_result)); | |
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->expected_result), | |
53 | ssl_alert_name(ctx2->expected_result)); | |
54 | return 0; | |
55 | } | |
56 | if (ctx->protocol != ctx2->protocol) { | |
57 | fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n", | |
58 | ssl_protocol_name(ctx->expected_result), | |
59 | ssl_protocol_name(ctx2->expected_result)); | |
60 | return 0; | |
61 | } | |
62 | ||
63 | return 1; | |
64 | } | |
65 | ||
66 | static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name) | |
67 | { | |
68 | SSL_TEST_CTX_TEST_FIXTURE fixture; | |
69 | fixture.test_case_name = test_case_name; | |
70 | fixture.expected_ctx = SSL_TEST_CTX_new(); | |
71 | OPENSSL_assert(fixture.expected_ctx != NULL); | |
72 | return fixture; | |
73 | } | |
74 | ||
75 | static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
76 | { | |
77 | int ret = 1; | |
78 | ||
79 | SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section); | |
80 | ||
81 | if (ctx == NULL) { | |
82 | fprintf(stderr, "Failed to parse good configuration %s.\n", | |
83 | fixture.test_section); | |
84 | goto err; | |
85 | } | |
86 | ||
87 | if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx)) | |
88 | goto err; | |
89 | ||
90 | ret = 0; | |
91 | err: | |
92 | SSL_TEST_CTX_free(ctx); | |
93 | return ret; | |
94 | } | |
95 | ||
96 | static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
97 | { | |
98 | SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section); | |
99 | ||
100 | if (ctx != NULL) { | |
101 | fprintf(stderr, "Parsing bad configuration %s succeeded.\n", | |
102 | fixture.test_section); | |
103 | SSL_TEST_CTX_free(ctx); | |
104 | return 1; | |
105 | } | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
110 | static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
111 | { | |
112 | SSL_TEST_CTX_free(fixture.expected_ctx); | |
113 | ERR_print_errors_fp(stderr); | |
114 | } | |
115 | ||
116 | #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \ | |
117 | SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up) | |
118 | #define EXECUTE_SSL_TEST_CTX_TEST() \ | |
119 | EXECUTE_TEST(execute_test, tear_down) | |
120 | #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST() \ | |
121 | EXECUTE_TEST(execute_failure_test, tear_down) | |
122 | ||
123 | static int test_empty_configuration() | |
124 | { | |
125 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
126 | fixture.test_section = "ssltest_default"; | |
127 | fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS; | |
128 | EXECUTE_SSL_TEST_CTX_TEST(); | |
129 | } | |
130 | ||
131 | static int test_good_configuration() | |
132 | { | |
133 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
134 | fixture.test_section = "ssltest_good"; | |
135 | fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL; | |
136 | fixture.expected_ctx->client_alert = SSL_AD_UNKNOWN_CA; | |
137 | fixture.expected_ctx->server_alert = 0; /* No alert. */ | |
138 | fixture.expected_ctx->protocol = TLS1_1_VERSION; | |
139 | EXECUTE_SSL_TEST_CTX_TEST(); | |
140 | } | |
141 | ||
142 | static const char *bad_configurations[] = { | |
143 | "ssltest_unknown_option", | |
144 | "ssltest_unknown_expected_result", | |
145 | "ssltest_unknown_alert", | |
146 | "ssltest_unknown_protocol", | |
147 | }; | |
148 | ||
149 | static int test_bad_configuration(int idx) | |
150 | { | |
151 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
152 | fixture.test_section = bad_configurations[idx]; | |
153 | EXECUTE_SSL_TEST_CTX_FAILURE_TEST(); | |
154 | } | |
155 | ||
156 | int main(int argc, char **argv) | |
157 | { | |
158 | int result = 0; | |
159 | ||
160 | if (argc != 2) | |
161 | return 1; | |
162 | ||
163 | conf = NCONF_new(NULL); | |
164 | OPENSSL_assert(conf != NULL); | |
165 | ||
166 | /* argv[1] should point to test/ssl_test_ctx_test.conf */ | |
167 | OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0); | |
168 | ||
169 | ||
170 | ADD_TEST(test_empty_configuration); | |
171 | ADD_TEST(test_good_configuration); | |
172 | ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations)); | |
173 | ||
174 | result = run_tests(argv[0]); | |
175 | ||
176 | NCONF_free(conf); | |
177 | ||
178 | return result; | |
179 | } |