2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
7 * https://www.openssl.org/source/license.html
12 #include <openssl/conf.h>
13 #include <openssl/err.h>
22 # define DIRSEP_PRESERVE 0
23 #elif !defined(OPENSSL_NO_POSIX_IO)
25 # ifndef OPENSSL_SYS_VMS
27 # define DIRSEP_PRESERVE 0
30 # define DIRSEP_PRESERVE 1
33 /* the test does not work without chdir() */
34 # define chdir(x) (-1);
36 # define DIRSEP_PRESERVE 0
39 /* changes path to that of the filename */
40 static char *change_path(const char *file)
42 char *s = OPENSSL_strdup(file);
46 char *new_config_name = NULL;
51 while ((p = strpbrk(p, DIRSEP)) != NULL) {
57 last[DIRSEP_PRESERVE] = 0;
58 TEST_note("changing path to %s", s);
62 new_config_name = strdup(last + DIRSEP_PRESERVE + 1);
65 return new_config_name;
69 * This test program checks the operation of the .include directive.
74 static int expect_failure = 0;
75 static int test_providers = 0;
76 static OSSL_LIB_CTX *libctx = NULL;
77 static char *rel_conf_file = NULL;
79 static int test_load_config(void)
86 if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
87 || !TEST_int_eq(err = ERR_peek_error(), 0)) {
90 TEST_note("Failure loading the configuration at line %ld", errline);
94 TEST_note("Failure expected but did not happen");
98 if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
99 TEST_note("Failed in CONF_modules_load");
103 /* verify whether CA_default/default_days is set */
105 if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1)
106 || !TEST_int_eq(val, 365)) {
107 TEST_note("default_days incorrect");
111 /* verify whether req/default_bits is set */
113 if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1)
114 || !TEST_int_eq(val, 2048)) {
115 TEST_note("default_bits incorrect");
119 /* verify whether countryName_default is set correctly */
120 str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default");
121 if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) {
122 TEST_note("countryName_default incorrect");
126 if (test_providers != 0) {
127 /* test for `active` directive in configuration file */
129 if (!TEST_int_eq(NCONF_get_number(conf, "null_sect", "activate", &val), 1)
130 || !TEST_int_eq(val, 1)) {
131 TEST_note("null provider not activated");
135 if (!TEST_int_eq(NCONF_get_number(conf, "default_sect", "activate", &val), 1)
136 || !TEST_int_eq(val, 1)) {
137 TEST_note("default provider not activated");
141 if (!TEST_int_eq(NCONF_get_number(conf, "legacy_sect", "activate", &val), 1)
142 || !TEST_int_eq(val, 1)) {
143 TEST_note("legacy provider not activated");
150 static int test_check_null_numbers(void)
152 #if defined(_BSD_SOURCE) \
153 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
154 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
157 /* Verify that a NULL config with a present environment variable returns
158 * success and the value.
160 if (!TEST_int_eq(setenv("FNORD", "123", 1), 0)
161 || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
162 || !TEST_long_eq(val, 123)) {
163 TEST_note("environment variable with NULL conf failed");
168 * Verify that a NULL config with a missing environment variable returns
171 if (!TEST_int_eq(unsetenv("FNORD"), 0)
172 || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) {
173 TEST_note("missing environment variable with NULL conf failed");
180 static int test_check_overflow(void)
182 #if defined(_BSD_SOURCE) \
183 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
184 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
186 char max[(sizeof(long) * 8) / 3 + 3];
189 p = max + sprintf(max, "0%ld", LONG_MAX) - 1;
190 setenv("FNORD", max, 1);
191 if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
192 || !TEST_long_eq(val, LONG_MAX))
198 setenv("FNORD", max, 1);
199 if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
205 static int test_available_providers(void)
207 libctx = OSSL_LIB_CTX_new();
208 if (!TEST_ptr(libctx))
211 if (!TEST_ptr(rel_conf_file) || !OSSL_LIB_CTX_load_config(libctx, rel_conf_file)) {
212 TEST_note("Failed to load config");
216 if (OSSL_PROVIDER_available(libctx, "default") != 1) {
217 TEST_note("Default provider is missing");
220 if (OSSL_PROVIDER_available(libctx, "legacy") != 1) {
221 TEST_note("Legacy provider is missing");
227 typedef enum OPTION_choice {
235 const OPTIONS *test_get_options(void)
237 static const OPTIONS test_options[] = {
238 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
239 { "f", OPT_FAIL, '-', "A failure is expected" },
240 { "providers", OPT_TEST_PROV, '-',
241 "Test for activated default and legacy providers"},
247 int setup_tests(void)
249 char *conf_file = NULL;
252 if (!TEST_ptr(conf = NCONF_new(NULL)))
255 while ((o = opt_next()) != OPT_EOF) {
269 conf_file = test_get_argument(0);
270 if (!TEST_ptr(conf_file)
271 || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
272 TEST_note("Unable to open the file argument");
277 * For this test we need to chdir as we use relative
278 * path names in the config files.
280 rel_conf_file = change_path(conf_file);
281 if (!TEST_ptr(rel_conf_file)) {
282 TEST_note("Unable to change path");
286 ADD_TEST(test_load_config);
287 ADD_TEST(test_check_null_numbers);
288 ADD_TEST(test_check_overflow);
289 if (test_providers != 0)
290 ADD_TEST(test_available_providers);
295 void cleanup_tests(void)
297 OPENSSL_free(rel_conf_file);
300 CONF_modules_unload(1);