Update copyright year
[openssl.git] / test / conf_include_test.c
1 /*
2  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <openssl/conf.h>
13 #include <openssl/err.h>
14 #include "testutil.h"
15
16 #ifdef _WIN32
17 # include <direct.h>
18 # define DIRSEP "/\\"
19 # ifndef __BORLANDC__
20 #  define chdir _chdir
21 # endif
22 # define DIRSEP_PRESERVE 0
23 #elif !defined(OPENSSL_NO_POSIX_IO)
24 # include <unistd.h>
25 # ifndef OPENSSL_SYS_VMS
26 #  define DIRSEP "/"
27 #  define DIRSEP_PRESERVE 0
28 # else
29 #  define DIRSEP "/]:"
30 #  define DIRSEP_PRESERVE 1
31 # endif
32 #else
33 /* the test does not work without chdir() */
34 # define chdir(x) (-1);
35 # define DIRSEP "/"
36 #  define DIRSEP_PRESERVE 0
37 #endif
38
39 /* changes path to that of the filename */
40 static int change_path(const char *file)
41 {
42     char *s = OPENSSL_strdup(file);
43     char *p = s;
44     char *last = NULL;
45     int ret;
46
47     if (s == NULL)
48         return -1;
49
50     while ((p = strpbrk(p, DIRSEP)) != NULL) {
51         last = p++;
52     }
53     if (last == NULL)
54         return 0;
55     last[DIRSEP_PRESERVE] = 0;
56
57     TEST_note("changing path to %s", s);
58     ret = chdir(s);
59     OPENSSL_free(s);
60     return ret;
61 }
62
63 /*
64  * This test program checks the operation of the .include directive.
65  */
66
67 static CONF *conf;
68 static BIO *in;
69 static int expect_failure = 0;
70
71 static int test_load_config(void)
72 {
73     long errline;
74     long val;
75     char *str;
76     long err;
77
78     if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
79         || !TEST_int_eq(err = ERR_peek_error(), 0)) {
80         if (expect_failure)
81             return 1;
82         TEST_note("Failure loading the configuration at line %ld", errline);
83         return 0;
84     }
85     if (expect_failure) {
86         TEST_note("Failure expected but did not happen");
87         return 0;
88     }
89
90     if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
91         TEST_note("Failed in CONF_modules_load");
92         return 0;
93     }
94
95     /* verify whether CA_default/default_days is set */
96     val = 0;
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");
100         return 0;
101     }
102
103     /* verify whether req/default_bits is set */
104     val = 0;
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");
108         return 0;
109     }
110
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");
115         return 0;
116     }
117
118     return 1;
119 }
120
121 static int test_check_null_numbers(void)
122 {
123 #if defined(_BSD_SOURCE) \
124         || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
125         || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
126     long val = 0;
127
128     /* Verify that a NULL config with a present environment variable returns
129      * success and the value.
130      */
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");
135         return 0;
136     }
137
138     /*
139      * Verify that a NULL config with a missing environment variable returns
140      * a failure code.
141      */
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");
145         return 0;
146     }
147 #endif
148     return 1;
149 }
150
151 static int test_check_overflow(void)
152 {
153 #if defined(_BSD_SOURCE) \
154         || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
155         || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
156     long val = 0;
157     char max[(sizeof(long) * 8) / 3 + 3];
158     char *p;
159
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))
164         return 0;
165
166     while (++*p > '9')
167         *p-- = '0';
168
169     setenv("FNORD", max, 1);
170     if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
171         return 0;
172 #endif
173     return 1;
174 }
175
176 typedef enum OPTION_choice {
177     OPT_ERR = -1,
178     OPT_EOF = 0,
179     OPT_FAIL,
180     OPT_TEST_ENUM
181 } OPTION_CHOICE;
182
183 const OPTIONS *test_get_options(void)
184 {
185     static const OPTIONS test_options[] = {
186         OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
187         { "f", OPT_FAIL, '-', "A failure is expected" },
188         { NULL }
189     };
190     return test_options;
191 }
192
193 int setup_tests(void)
194 {
195     const char *conf_file;
196     OPTION_CHOICE o;
197
198     if (!TEST_ptr(conf = NCONF_new(NULL)))
199         return 0;
200
201     while ((o = opt_next()) != OPT_EOF) {
202         switch (o) {
203         case OPT_FAIL:
204             expect_failure = 1;
205             break;
206         case OPT_TEST_CASES:
207             break;
208         default:
209             return 0;
210         }
211     }
212
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");
217         return 0;
218     }
219
220     /*
221      * For this test we need to chdir as we use relative
222      * path names in the config files.
223      */
224     change_path(conf_file);
225
226     ADD_TEST(test_load_config);
227     ADD_TEST(test_check_null_numbers);
228     ADD_TEST(test_check_overflow);
229     return 1;
230 }
231
232 void cleanup_tests(void)
233 {
234     BIO_vfree(in);
235     NCONF_free(conf);
236     CONF_modules_unload(1);
237 }