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 int change_path(const char *file)
42 char *s = OPENSSL_strdup(file);
50 while ((p = strpbrk(p, DIRSEP)) != NULL) {
55 last[DIRSEP_PRESERVE] = 0;
57 TEST_note("changing path to %s", s);
64 * This test program checks the operation of the .include directive.
69 static int expect_failure = 0;
71 static int test_load_config(void)
78 if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
79 || !TEST_int_eq(err = ERR_peek_error(), 0)) {
82 TEST_note("Failure loading the configuration at line %ld", errline);
86 TEST_note("Failure expected but did not happen");
90 if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
91 TEST_note("Failed in CONF_modules_load");
95 /* verify whether CA_default/default_days is set */
97 if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1)
98 || !TEST_int_eq(val, 365)) {
99 TEST_note("default_days incorrect");
103 /* verify whether req/default_bits is set */
105 if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1)
106 || !TEST_int_eq(val, 2048)) {
107 TEST_note("default_bits incorrect");
111 /* verify whether countryName_default is set correctly */
112 str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default");
113 if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) {
114 TEST_note("countryName_default incorrect");
121 static int test_check_null_numbers(void)
123 #if defined(_BSD_SOURCE) \
124 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
125 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
128 /* Verify that a NULL config with a present environment variable returns
129 * success and the value.
131 if (!TEST_int_eq(setenv("FNORD", "123", 1), 0)
132 || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
133 || !TEST_long_eq(val, 123)) {
134 TEST_note("environment variable with NULL conf failed");
139 * Verify that a NULL config with a missing environment variable returns
142 if (!TEST_int_eq(unsetenv("FNORD"), 0)
143 || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) {
144 TEST_note("missing environment variable with NULL conf failed");
151 static int test_check_overflow(void)
153 #if defined(_BSD_SOURCE) \
154 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
155 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
157 char max[(sizeof(long) * 8) / 3 + 3];
160 p = max + sprintf(max, "0%ld", LONG_MAX) - 1;
161 setenv("FNORD", max, 1);
162 if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
163 || !TEST_long_eq(val, LONG_MAX))
169 setenv("FNORD", max, 1);
170 if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
176 typedef enum OPTION_choice {
183 const OPTIONS *test_get_options(void)
185 static const OPTIONS test_options[] = {
186 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
187 { "f", OPT_FAIL, '-', "A failure is expected" },
193 int setup_tests(void)
195 const char *conf_file;
198 if (!TEST_ptr(conf = NCONF_new(NULL)))
201 while ((o = opt_next()) != OPT_EOF) {
213 conf_file = test_get_argument(0);
214 if (!TEST_ptr(conf_file)
215 || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
216 TEST_note("Unable to open the file argument");
221 * For this test we need to chdir as we use relative
222 * path names in the config files.
224 change_path(conf_file);
226 ADD_TEST(test_load_config);
227 ADD_TEST(test_check_null_numbers);
228 ADD_TEST(test_check_overflow);
232 void cleanup_tests(void)
236 CONF_modules_unload(1);