1 | /* |
---|
2 | * Copyright 2016 Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * Redistribution and use in source and binary forms, with or without |
---|
5 | * modification, are permitted provided that the following conditions |
---|
6 | * are met: |
---|
7 | * 1. Redistributions of source code must retain the above copyright |
---|
8 | * notice, this list of conditions and the following disclaimer. |
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer in the |
---|
11 | * documentation and/or other materials provided with the distribution. |
---|
12 | * |
---|
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
23 | * SUCH DAMAGE. |
---|
24 | */ |
---|
25 | |
---|
26 | #include <rtems/bsd/sys/param.h> |
---|
27 | |
---|
28 | #include <assert.h> |
---|
29 | #include <ctype.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <string.h> |
---|
32 | #include <stdio.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <sys/stat.h> |
---|
35 | #include <sysexits.h> |
---|
36 | #include <unistd.h> |
---|
37 | |
---|
38 | #include <machine/rtems-bsd-rc-conf.h> |
---|
39 | #include <machine/rtems-bsd-rc-conf-services.h> |
---|
40 | |
---|
41 | #define TEST_NAME "LIBBSD RC.CONF 1" |
---|
42 | |
---|
43 | static const char* rc_conf_regex = \ |
---|
44 | "\n" /* empty line */ \ |
---|
45 | "#\n" /* comment then nothing */ \ |
---|
46 | "# comment\n" /* comment */ |
---|
47 | " # \n" /* whitespace (ws), commend, ws */ \ |
---|
48 | "test_regex_1=\n" /* name, empty value */ \ |
---|
49 | "test_regex_2=\"\"\n" /* name, quoted empty value */ \ |
---|
50 | "test_regex_3=\"1 2 3\"\n" /* name, 3 args */ \ |
---|
51 | " test_regex_4=\"04\"\n" /* ws, name, 1 arg */ \ |
---|
52 | "\ttest_regex_5=\"05\" \n" /* ws, name, 1 arg, ws */ \ |
---|
53 | "test_regex_6=\" 1 2\t 3 \"\n"; /* name then ws and 3 args */ |
---|
54 | |
---|
55 | #define NUM_OF_TEST_REGEX_ 6 |
---|
56 | static bool test_regex_results[NUM_OF_TEST_REGEX_]; |
---|
57 | static int test_regex_last_num; |
---|
58 | static int test_service_last_num; |
---|
59 | |
---|
60 | static const char* rc_conf_not_found = \ |
---|
61 | "# invalid directive.\n" \ |
---|
62 | "abc_def_0=\"not found\"\n"; |
---|
63 | |
---|
64 | static int |
---|
65 | test_service(rtems_bsd_rc_conf* rc_conf) |
---|
66 | { |
---|
67 | rtems_bsd_rc_conf_argc_argv* aa; |
---|
68 | int r; |
---|
69 | |
---|
70 | test_service_last_num = 1; |
---|
71 | |
---|
72 | /* |
---|
73 | * Fill match of anything. |
---|
74 | */ |
---|
75 | puts("test_service regex: 'test_regex_.*'"); |
---|
76 | test_regex_last_num = 0; |
---|
77 | |
---|
78 | assert((aa = rtems_bsd_rc_conf_argc_argv_create()) != NULL); |
---|
79 | r = rtems_bsd_rc_conf_find(rc_conf, "test_regex_.*", aa); |
---|
80 | assert(r == 0 || (r < 0 && errno == ENOENT)); |
---|
81 | if (r < 0 && errno == ENOENT) |
---|
82 | return -1; |
---|
83 | |
---|
84 | while (r == 0) { |
---|
85 | int num; |
---|
86 | int arg; |
---|
87 | rtems_bsd_rc_conf_print_cmd(rc_conf, "test_service[1]", aa->argc, aa->argv); |
---|
88 | assert(strncasecmp(aa->argv[0], "test_regex_", strlen("test_regex_")) == 0); |
---|
89 | num = atoi(aa->argv[0] + strlen("test_regex_")); |
---|
90 | assert(num == (test_regex_last_num + 1)); |
---|
91 | assert((num - 1) < NUM_OF_TEST_REGEX_); |
---|
92 | for (arg = 0; arg < aa->argc; ++arg) { |
---|
93 | const char* a = aa->argv[arg]; |
---|
94 | size_t l = strlen(a); |
---|
95 | if (l > 0) { |
---|
96 | assert(!isspace(a[0])); |
---|
97 | assert(!isspace(a[l - 1])); |
---|
98 | assert(a[0] != '"'); |
---|
99 | assert(a[l - 1] != '"'); |
---|
100 | } |
---|
101 | } |
---|
102 | test_regex_results[num - 1] = true; |
---|
103 | ++test_regex_last_num; |
---|
104 | r = rtems_bsd_rc_conf_find_next(rc_conf, aa); |
---|
105 | assert(r == 0 || (r < 0 && errno == ENOENT)); |
---|
106 | } |
---|
107 | |
---|
108 | /* |
---|
109 | * Specific match of only numbers. |
---|
110 | */ |
---|
111 | puts("test_service regex: 'test_regex_[0-9]+'"); |
---|
112 | test_regex_last_num = 0; |
---|
113 | |
---|
114 | assert((aa = rtems_bsd_rc_conf_argc_argv_create()) != NULL); |
---|
115 | r = rtems_bsd_rc_conf_find(rc_conf, "test_regex_[0-9]+", aa); |
---|
116 | assert(r == 0 || (r < 0 && errno == ENOENT)); |
---|
117 | if (r < 0 && errno == ENOENT) |
---|
118 | return -1; |
---|
119 | |
---|
120 | while (r == 0) { |
---|
121 | int num; |
---|
122 | int arg; |
---|
123 | rtems_bsd_rc_conf_print_cmd(rc_conf, "test_service[2]", aa->argc, aa->argv); |
---|
124 | assert(strncasecmp(aa->argv[0], "test_regex_", strlen("test_regex_")) == 0); |
---|
125 | num = atoi(aa->argv[0] + strlen("test_regex_")); |
---|
126 | assert(num == (test_regex_last_num + 1)); |
---|
127 | assert((num - 1) < NUM_OF_TEST_REGEX_); |
---|
128 | for (arg = 0; arg < aa->argc; ++arg) { |
---|
129 | const char* a = aa->argv[arg]; |
---|
130 | size_t l = strlen(a); |
---|
131 | if (l > 0) { |
---|
132 | assert(!isspace(a[0])); |
---|
133 | assert(!isspace(a[l - 1])); |
---|
134 | assert(a[0] != '"'); |
---|
135 | assert(a[l - 1] != '"'); |
---|
136 | } |
---|
137 | } |
---|
138 | test_regex_results[num - 1] = true; |
---|
139 | ++test_regex_last_num; |
---|
140 | r = rtems_bsd_rc_conf_find_next(rc_conf, aa); |
---|
141 | assert(r == 0 || (r < 0 && errno == ENOENT)); |
---|
142 | } |
---|
143 | |
---|
144 | rtems_bsd_rc_conf_argc_argv_destroy(aa); |
---|
145 | puts("test_service done"); |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | static int |
---|
150 | test_service_2(rtems_bsd_rc_conf* rc_conf) |
---|
151 | { |
---|
152 | puts("test_service_2"); |
---|
153 | assert(test_service_last_num == 1); |
---|
154 | test_service_last_num = 2; |
---|
155 | return 0; |
---|
156 | } |
---|
157 | |
---|
158 | static int |
---|
159 | test_service_3(rtems_bsd_rc_conf* rc_conf) |
---|
160 | { |
---|
161 | puts("test_service_3"); |
---|
162 | assert(test_service_last_num == 2); |
---|
163 | test_service_last_num = 3; |
---|
164 | return 0; |
---|
165 | } |
---|
166 | |
---|
167 | static int |
---|
168 | test_service_4(rtems_bsd_rc_conf* rc_conf) |
---|
169 | { |
---|
170 | puts("test_service_4"); |
---|
171 | assert(test_service_last_num == 3); |
---|
172 | test_service_last_num = 4; |
---|
173 | return 0; |
---|
174 | } |
---|
175 | |
---|
176 | static int |
---|
177 | test_service_5(rtems_bsd_rc_conf* rc_conf) |
---|
178 | { |
---|
179 | puts("test_service_5"); |
---|
180 | assert(test_service_last_num == 4); |
---|
181 | test_service_last_num = 5; |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | static int |
---|
186 | test_service_bad(rtems_bsd_rc_conf* rc_conf) |
---|
187 | { |
---|
188 | puts("test_service_bad"); |
---|
189 | return -1; |
---|
190 | } |
---|
191 | |
---|
192 | static void |
---|
193 | make_rc_conf(const char* rc_conf) |
---|
194 | { |
---|
195 | FILE* f; |
---|
196 | unlink(rc_conf); /* Ignore any errors */ |
---|
197 | assert((f = fopen(rc_conf, "w")) != NULL); |
---|
198 | assert(fwrite(rc_conf_regex, strlen(rc_conf_regex), 1, f) == 1); |
---|
199 | assert(fclose(f) == 0); |
---|
200 | } |
---|
201 | |
---|
202 | static void |
---|
203 | test_regex_check(void) |
---|
204 | { |
---|
205 | int i; |
---|
206 | assert(test_regex_last_num == NUM_OF_TEST_REGEX_); /* all items found? */ |
---|
207 | for (i = 0; i < NUM_OF_TEST_REGEX_; ++i) |
---|
208 | assert(test_regex_results[i]); |
---|
209 | } |
---|
210 | |
---|
211 | static void |
---|
212 | test_regex_reset(void) |
---|
213 | { |
---|
214 | memset(&test_regex_results[0], 0, sizeof(test_regex_results)); |
---|
215 | test_regex_last_num = 0; |
---|
216 | test_service_last_num = 0; |
---|
217 | } |
---|
218 | |
---|
219 | static void |
---|
220 | test_etc_rc_conf(void) |
---|
221 | { |
---|
222 | puts("test_etc_rc_conf"); |
---|
223 | make_rc_conf("/etc/rc.conf"); |
---|
224 | test_regex_reset(); |
---|
225 | assert(rtems_bsd_run_etc_rc_conf(0, true) == 0); |
---|
226 | test_regex_check(); |
---|
227 | } |
---|
228 | |
---|
229 | static void |
---|
230 | test_rc_conf(void) |
---|
231 | { |
---|
232 | puts("test_rc_conf"); |
---|
233 | make_rc_conf("/my_rc.conf"); |
---|
234 | test_regex_reset(); |
---|
235 | assert(rtems_bsd_run_rc_conf("/my_rc.conf", 0, true) == 0); |
---|
236 | test_regex_check(); |
---|
237 | } |
---|
238 | |
---|
239 | static void |
---|
240 | test_rc_conf_script(void) |
---|
241 | { |
---|
242 | puts("test_rc_conf_conf"); |
---|
243 | test_regex_reset(); |
---|
244 | assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_regex, 0, true) == 0); |
---|
245 | test_regex_check(); |
---|
246 | } |
---|
247 | |
---|
248 | static void |
---|
249 | test_rc_conf_script_not_found(void) |
---|
250 | { |
---|
251 | puts("test_rc_conf_conf_not_found"); |
---|
252 | test_regex_reset(); |
---|
253 | assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_not_found, 0, true) < 0); |
---|
254 | assert(test_regex_last_num == 0); |
---|
255 | } |
---|
256 | |
---|
257 | static void |
---|
258 | setup(void) |
---|
259 | { |
---|
260 | struct stat sb; |
---|
261 | mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO); /* ignore errors, check the dir after. */ |
---|
262 | assert(stat("/etc", &sb) == 0); |
---|
263 | assert(S_ISDIR(sb.st_mode)); |
---|
264 | assert(rtems_bsd_rc_conf_service_add("test_service_2", |
---|
265 | "before:last;", |
---|
266 | test_service_2) == 0); |
---|
267 | assert(rtems_bsd_rc_conf_service_add("test_service_5", |
---|
268 | "after:test_service_2;", |
---|
269 | test_service_5) == 0); |
---|
270 | assert(rtems_bsd_rc_conf_service_add("test_service_4", |
---|
271 | "before:test_service_5;", |
---|
272 | test_service_4) == 0); |
---|
273 | assert(rtems_bsd_rc_conf_service_add("test_service_3", |
---|
274 | "before:test_service_4;after:test_service_2;", |
---|
275 | test_service_3) == 0); |
---|
276 | assert(rtems_bsd_rc_conf_service_add("test_service_bad", |
---|
277 | "before:first;", |
---|
278 | test_service_bad) < 0); |
---|
279 | assert(rtems_bsd_rc_conf_service_add("test_service_bad", |
---|
280 | "after:last;", |
---|
281 | test_service_bad) < 0); |
---|
282 | assert(rtems_bsd_rc_conf_service_add("test_service_bad", |
---|
283 | "after:xxxx,xxxx", |
---|
284 | test_service_bad) < 0); |
---|
285 | assert(rtems_bsd_rc_conf_service_add("test_service_bad", |
---|
286 | "yyyy:xxxx;", |
---|
287 | test_service_bad) < 0); |
---|
288 | assert(rtems_bsd_rc_conf_service_add("test_service", |
---|
289 | "after:first;", |
---|
290 | test_service) == 0); |
---|
291 | } |
---|
292 | |
---|
293 | static void |
---|
294 | test_main(void) |
---|
295 | { |
---|
296 | setup(); |
---|
297 | |
---|
298 | test_etc_rc_conf(); |
---|
299 | test_rc_conf(); |
---|
300 | test_rc_conf_script(); |
---|
301 | test_rc_conf_script_not_found(); |
---|
302 | |
---|
303 | exit(0); |
---|
304 | } |
---|
305 | |
---|
306 | #include <rtems/bsd/test/default-init.h> |
---|