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);
65 * This test program checks the operation of the .include directive.
70 static int expect_failure = 0;
72 static int test_load_config(void)
79 if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
80 || !TEST_int_eq(err = ERR_peek_error(), 0)) {
83 TEST_note("Failure loading the configuration at line %ld", errline);
87 TEST_note("Failure expected but did not happen");
91 if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
92 TEST_note("Failed in CONF_modules_load");
96 /* verify whether CA_default/default_days is set */
98 if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1)
99 || !TEST_int_eq(val, 365)) {
100 TEST_note("default_days incorrect");
104 /* verify whether req/default_bits is set */
106 if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1)
107 || !TEST_int_eq(val, 2048)) {
108 TEST_note("default_bits incorrect");
112 /* verify whether countryName_default is set correctly */
113 str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default");
114 if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) {
115 TEST_note("countryName_default incorrect");
122 static int test_check_null_numbers(void)
124 #if defined(_BSD_SOURCE) \
125 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
126 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
129 /* Verify that a NULL config with a present environment variable returns
130 * success and the value.
132 if (!TEST_int_eq(setenv("FNORD", "123", 1), 0)
133 || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
134 || !TEST_long_eq(val, 123)) {
135 TEST_note("environment variable with NULL conf failed");
140 * Verify that a NULL config with a missing environment variable returns
143 if (!TEST_int_eq(unsetenv("FNORD"), 0)
144 || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) {
145 TEST_note("missing environment variable with NULL conf failed");
152 static int test_check_overflow(void)
154 #if defined(_BSD_SOURCE) \
155 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
156 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
158 char max[(sizeof(long) * 8) / 3 + 3];
161 p = max + sprintf(max, "0%ld", LONG_MAX) - 1;
162 setenv("FNORD", max, 1);
163 if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
164 || !TEST_long_eq(val, LONG_MAX))
170 setenv("FNORD", max, 1);
171 if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
177 typedef enum OPTION_choice {
184 const OPTIONS *test_get_options(void)
186 static const OPTIONS test_options[] = {
187 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
188 { "f", OPT_FAIL, '-', "A failure is expected" },
194 int setup_tests(void)
196 const char *conf_file;
199 if (!TEST_ptr(conf = NCONF_new(NULL)))
202 while ((o = opt_next()) != OPT_EOF) {
214 conf_file = test_get_argument(0);
215 if (!TEST_ptr(conf_file)
216 || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
217 TEST_note("Unable to open the file argument");
222 * For this test we need to chdir as we use relative
223 * path names in the config files.
225 change_path(conf_file);
227 ADD_TEST(test_load_config);
228 ADD_TEST(test_check_null_numbers);
229 ADD_TEST(test_check_overflow);
233 void cleanup_tests(void)
237 CONF_modules_unload(1);