2 * Copyright 2016-2018 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>
20 # define DIRSEP_PRESERVE 0
21 #elif !defined(OPENSSL_NO_POSIX_IO)
23 # ifndef OPENSSL_SYS_VMS
25 # define DIRSEP_PRESERVE 0
28 # define DIRSEP_PRESERVE 1
31 /* the test does not work without chdir() */
32 # define chdir(x) (-1);
34 # define DIRSEP_PRESERVE 0
37 /* changes path to that of the filename */
38 static int change_path(const char *file)
40 char *s = OPENSSL_strdup(file);
48 while ((p = strpbrk(p, DIRSEP)) != NULL) {
53 last[DIRSEP_PRESERVE] = 0;
55 TEST_note("changing path to %s", s);
62 * This test program checks the operation of the .include directive.
67 static int expect_failure = 0;
69 static int test_load_config(void)
76 if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
77 || !TEST_int_eq(err = ERR_peek_error(), 0)) {
80 TEST_note("Failure loading the configuration at line %ld", errline);
84 TEST_note("Failure expected but did not happen");
88 if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
89 TEST_note("Failed in CONF_modules_load");
93 /* verify whether CA_default/default_days is set */
95 if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1)
96 || !TEST_int_eq(val, 365)) {
97 TEST_note("default_days incorrect");
101 /* verify whether req/default_bits is set */
103 if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1)
104 || !TEST_int_eq(val, 2048)) {
105 TEST_note("default_bits incorrect");
109 /* verify whether countryName_default is set correctly */
110 str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default");
111 if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) {
112 TEST_note("countryName_default incorrect");
119 static int test_check_null_numbers(void)
121 #if defined(_BSD_SOURCE) \
122 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
123 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
126 /* Verify that a NULL config with a present environment variable returns
127 * success and the value.
129 if (!TEST_int_eq(setenv("FNORD", "123", 1), 0)
130 || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
131 || !TEST_long_eq(val, 123)) {
132 TEST_note("environment variable with NULL conf failed");
137 * Verify that a NULL config with a missing environment variable returns
140 if (!TEST_int_eq(unsetenv("FNORD"), 0)
141 || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) {
142 TEST_note("missing environment variable with NULL conf failed");
149 static int test_check_overflow(void)
151 #if defined(_BSD_SOURCE) \
152 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
153 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
155 char max[(sizeof(long) * 8) / 3 + 3];
158 p = max + sprintf(max, "0%ld", LONG_MAX) - 1;
159 setenv("FNORD", max, 1);
160 if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
161 || !TEST_long_eq(val, LONG_MAX))
167 setenv("FNORD", max, 1);
168 if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
174 typedef enum OPTION_choice {
181 const OPTIONS *test_get_options(void)
183 static const OPTIONS test_options[] = {
184 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
185 { "f", OPT_FAIL, '-', "A failure is expected" },
191 int setup_tests(void)
193 const char *conf_file;
196 if (!TEST_ptr(conf = NCONF_new(NULL)))
199 while ((o = opt_next()) != OPT_EOF) {
211 conf_file = test_get_argument(0);
212 if (!TEST_ptr(conf_file)
213 || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
214 TEST_note("Unable to open the file argument");
219 * For this test we need to chdir as we use relative
220 * path names in the config files.
222 change_path(conf_file);
224 ADD_TEST(test_load_config);
225 ADD_TEST(test_check_null_numbers);
226 ADD_TEST(test_check_overflow);
230 void cleanup_tests(void)
234 CONF_modules_unload(1);