]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/conf_include_test.c
Fix various typos, repeated words, align some spelling to LDP.
[thirdparty/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 = 0;
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 goto err;
55 last[DIRSEP_PRESERVE] = 0;
56
57 TEST_note("changing path to %s", s);
58 ret = chdir(s);
59 err:
60 OPENSSL_free(s);
61 return ret;
62 }
63
64 /*
65 * This test program checks the operation of the .include directive.
66 */
67
68 static CONF *conf;
69 static BIO *in;
70 static int expect_failure = 0;
71
72 static int test_load_config(void)
73 {
74 long errline;
75 long val;
76 char *str;
77 long err;
78
79 if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
80 || !TEST_int_eq(err = ERR_peek_error(), 0)) {
81 if (expect_failure)
82 return 1;
83 TEST_note("Failure loading the configuration at line %ld", errline);
84 return 0;
85 }
86 if (expect_failure) {
87 TEST_note("Failure expected but did not happen");
88 return 0;
89 }
90
91 if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
92 TEST_note("Failed in CONF_modules_load");
93 return 0;
94 }
95
96 /* verify whether CA_default/default_days is set */
97 val = 0;
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");
101 return 0;
102 }
103
104 /* verify whether req/default_bits is set */
105 val = 0;
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");
109 return 0;
110 }
111
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");
116 return 0;
117 }
118
119 return 1;
120 }
121
122 static int test_check_null_numbers(void)
123 {
124 #if defined(_BSD_SOURCE) \
125 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
126 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
127 long val = 0;
128
129 /* Verify that a NULL config with a present environment variable returns
130 * success and the value.
131 */
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");
136 return 0;
137 }
138
139 /*
140 * Verify that a NULL config with a missing environment variable returns
141 * a failure code.
142 */
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");
146 return 0;
147 }
148 #endif
149 return 1;
150 }
151
152 static int test_check_overflow(void)
153 {
154 #if defined(_BSD_SOURCE) \
155 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
156 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
157 long val = 0;
158 char max[(sizeof(long) * 8) / 3 + 3];
159 char *p;
160
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))
165 return 0;
166
167 while (++*p > '9')
168 *p-- = '0';
169
170 setenv("FNORD", max, 1);
171 if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
172 return 0;
173 #endif
174 return 1;
175 }
176
177 typedef enum OPTION_choice {
178 OPT_ERR = -1,
179 OPT_EOF = 0,
180 OPT_FAIL,
181 OPT_TEST_ENUM
182 } OPTION_CHOICE;
183
184 const OPTIONS *test_get_options(void)
185 {
186 static const OPTIONS test_options[] = {
187 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
188 { "f", OPT_FAIL, '-', "A failure is expected" },
189 { NULL }
190 };
191 return test_options;
192 }
193
194 int setup_tests(void)
195 {
196 const char *conf_file;
197 OPTION_CHOICE o;
198
199 if (!TEST_ptr(conf = NCONF_new(NULL)))
200 return 0;
201
202 while ((o = opt_next()) != OPT_EOF) {
203 switch (o) {
204 case OPT_FAIL:
205 expect_failure = 1;
206 break;
207 case OPT_TEST_CASES:
208 break;
209 default:
210 return 0;
211 }
212 }
213
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");
218 return 0;
219 }
220
221 /*
222 * For this test we need to chdir as we use relative
223 * path names in the config files.
224 */
225 change_path(conf_file);
226
227 ADD_TEST(test_load_config);
228 ADD_TEST(test_check_null_numbers);
229 ADD_TEST(test_check_overflow);
230 return 1;
231 }
232
233 void cleanup_tests(void)
234 {
235 BIO_vfree(in);
236 NCONF_free(conf);
237 CONF_modules_unload(1);
238 }