2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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.
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.
19 #include "ssl_test_ctx.h"
21 #include <openssl/e_os2.h>
22 #include <openssl/err.h>
23 #include <openssl/conf.h>
24 #include <openssl/ssl.h>
26 static CONF
*conf
= NULL
;
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
;
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
)
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
));
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
));
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
));
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
));
66 static SSL_TEST_CTX_TEST_FIXTURE
set_up(const char *const test_case_name
)
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
);
75 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture
)
79 SSL_TEST_CTX
*ctx
= SSL_TEST_CTX_create(conf
, fixture
.test_section
);
82 fprintf(stderr
, "Failed to parse good configuration %s.\n",
83 fixture
.test_section
);
87 if (!SSL_TEST_CTX_equal(ctx
, fixture
.expected_ctx
))
92 SSL_TEST_CTX_free(ctx
);
96 static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture
)
98 SSL_TEST_CTX
*ctx
= SSL_TEST_CTX_create(conf
, fixture
.test_section
);
101 fprintf(stderr
, "Parsing bad configuration %s succeeded.\n",
102 fixture
.test_section
);
103 SSL_TEST_CTX_free(ctx
);
110 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture
)
112 SSL_TEST_CTX_free(fixture
.expected_ctx
);
113 ERR_print_errors_fp(stderr
);
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)
123 static int test_empty_configuration()
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();
131 static int test_good_configuration()
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();
142 static const char *bad_configurations
[] = {
143 "ssltest_unknown_option",
144 "ssltest_unknown_expected_result",
145 "ssltest_unknown_alert",
146 "ssltest_unknown_protocol",
149 static int test_bad_configuration(int idx
)
151 SETUP_SSL_TEST_CTX_TEST_FIXTURE();
152 fixture
.test_section
= bad_configurations
[idx
];
153 EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
156 int main(int argc
, char **argv
)
163 conf
= NCONF_new(NULL
);
164 OPENSSL_assert(conf
!= NULL
);
166 /* argv[1] should point to test/ssl_test_ctx_test.conf */
167 OPENSSL_assert(NCONF_load(conf
, argv
[1], NULL
) > 0);
170 ADD_TEST(test_empty_configuration
);
171 ADD_TEST(test_good_configuration
);
172 ADD_ALL_TESTS(test_bad_configuration
, OSSL_NELEM(bad_configurations
));
174 result
= run_tests(argv
[0]);