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.
13 #include <openssl/e_os2.h>
14 #include <openssl/crypto.h>
17 #include "ssl_test_ctx.h"
19 /* True enums and other test configuration values that map to an int. */
26 __owur static int parse_enum(const test_enum *enums, size_t num_enums,
27 int *value, const char *name)
30 for (i = 0; i < num_enums; i++) {
31 if (strcmp(enums[i].name, name) == 0) {
32 *value = enums[i].value;
39 static const char *enum_name(const test_enum *enums, size_t num_enums,
43 for (i = 0; i < num_enums; i++) {
44 if (enums[i].value == value) {
48 return "InvalidValue";
56 static const test_enum ssl_test_results[] = {
57 {"Success", SSL_TEST_SUCCESS},
58 {"ServerFail", SSL_TEST_SERVER_FAIL},
59 {"ClientFail", SSL_TEST_CLIENT_FAIL},
60 {"InternalError", SSL_TEST_INTERNAL_ERROR},
63 __owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
66 if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
70 test_ctx->expected_result = ret_value;
74 const char *ssl_test_result_t_name(ssl_test_result_t result)
76 return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
79 /******************************/
80 /* ClientAlert / ServerAlert. */
81 /******************************/
83 static const test_enum ssl_alerts[] = {
84 {"UnknownCA", SSL_AD_UNKNOWN_CA},
87 __owur static int parse_alert(int *alert, const char *value)
89 return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
92 __owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
94 return parse_alert(&test_ctx->client_alert, value);
97 __owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
99 return parse_alert(&test_ctx->server_alert, value);
102 const char *ssl_alert_name(int alert)
104 return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
111 static const test_enum ssl_protocols[] = {
112 {"TLSv1.2", TLS1_2_VERSION},
113 {"TLSv1.1", TLS1_1_VERSION},
114 {"TLSv1", TLS1_VERSION},
115 {"SSLv3", SSL3_VERSION},
118 __owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value)
120 return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols),
121 &test_ctx->protocol, value);
124 const char *ssl_protocol_name(int protocol)
126 return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol);
130 /*************************************************************/
131 /* Known test options and their corresponding parse methods. */
132 /*************************************************************/
136 int (*parse)(SSL_TEST_CTX *test_ctx, const char *value);
137 } ssl_test_ctx_option;
139 static const ssl_test_ctx_option ssl_test_ctx_options[] = {
140 { "ExpectedResult", &parse_expected_result },
141 { "ClientAlert", &parse_client_alert },
142 { "ServerAlert", &parse_server_alert },
143 { "Protocol", &parse_protocol },
148 * Since these methods are used to create tests, we use OPENSSL_assert liberally
149 * for malloc failures and other internal errors.
151 SSL_TEST_CTX *SSL_TEST_CTX_new()
154 ret = OPENSSL_zalloc(sizeof(*ret));
155 OPENSSL_assert(ret != NULL);
156 ret->expected_result = SSL_TEST_SUCCESS;
160 void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx)
165 SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)
167 STACK_OF(CONF_VALUE) *sk_conf;
172 sk_conf = NCONF_get_section(conf, test_section);
173 OPENSSL_assert(sk_conf != NULL);
175 ctx = SSL_TEST_CTX_new();
176 OPENSSL_assert(ctx != NULL);
178 for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
180 const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
181 for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {
182 if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {
183 if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {
184 fprintf(stderr, "Bad value %s for option %s\n",
185 option->value, option->name);
193 fprintf(stderr, "Unknown test option: %s\n", option->name);
201 SSL_TEST_CTX_free(ctx);