Small MSVC build fixes.
[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 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_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
69     return 1;
70 }
71
72 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
73 {
74     SSL_TEST_CTX_TEST_FIXTURE fixture;
75     fixture.test_case_name = test_case_name;
76     fixture.expected_ctx = SSL_TEST_CTX_new();
77     OPENSSL_assert(fixture.expected_ctx != NULL);
78     return fixture;
79 }
80
81 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
82 {
83     int success = 0;
84
85     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
86
87     if (ctx == NULL) {
88         fprintf(stderr, "Failed to parse good configuration %s.\n",
89                 fixture.test_section);
90         goto err;
91     }
92
93     if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
94         goto err;
95
96     success = 1;
97  err:
98     SSL_TEST_CTX_free(ctx);
99     return success;
100 }
101
102 static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
103 {
104     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
105
106     if (ctx != NULL) {
107         fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
108                 fixture.test_section);
109         SSL_TEST_CTX_free(ctx);
110         return 0;
111     }
112
113     return 1;
114 }
115
116 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
117 {
118     SSL_TEST_CTX_free(fixture.expected_ctx);
119     ERR_print_errors_fp(stderr);
120 }
121
122 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE()                       \
123     SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
124 #define EXECUTE_SSL_TEST_CTX_TEST()             \
125     EXECUTE_TEST(execute_test, tear_down)
126 #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST()             \
127     EXECUTE_TEST(execute_failure_test, tear_down)
128
129 static int test_empty_configuration()
130 {
131     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
132     fixture.test_section = "ssltest_default";
133     fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
134     EXECUTE_SSL_TEST_CTX_TEST();
135 }
136
137 static int test_good_configuration()
138 {
139     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
140     fixture.test_section = "ssltest_good";
141     fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
142     fixture.expected_ctx->client_alert = SSL_AD_UNKNOWN_CA;
143     fixture.expected_ctx->server_alert = 0;  /* No alert. */
144     fixture.expected_ctx->protocol = TLS1_1_VERSION;
145     fixture.expected_ctx->client_verify_callback = SSL_TEST_VERIFY_REJECT_ALL,
146     EXECUTE_SSL_TEST_CTX_TEST();
147 }
148
149 static const char *bad_configurations[] = {
150     "ssltest_unknown_option",
151     "ssltest_unknown_expected_result",
152     "ssltest_unknown_alert",
153     "ssltest_unknown_protocol",
154     "ssltest_unknown_verify_callback",
155 };
156
157 static int test_bad_configuration(int idx)
158 {
159         SETUP_SSL_TEST_CTX_TEST_FIXTURE();
160         fixture.test_section = bad_configurations[idx];
161         EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
162 }
163
164 int main(int argc, char **argv)
165 {
166     int result = 0;
167
168     if (argc != 2)
169         return 1;
170
171     conf = NCONF_new(NULL);
172     OPENSSL_assert(conf != NULL);
173
174     /* argv[1] should point to test/ssl_test_ctx_test.conf */
175     OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
176
177
178     ADD_TEST(test_empty_configuration);
179     ADD_TEST(test_good_configuration);
180     ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
181
182     result = run_tests(argv[0]);
183
184     NCONF_free(conf);
185
186     return result;
187 }