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 <errno.h> |
---|
30 | #include <stdio.h> |
---|
31 | #include <stdlib.h> |
---|
32 | #include <sys/stat.h> |
---|
33 | #include <sysexits.h> |
---|
34 | |
---|
35 | #include <machine/rtems-bsd-rc-conf.h> |
---|
36 | |
---|
37 | #define TEST_NAME "LIBBSD RC.CONF 1" |
---|
38 | |
---|
39 | static const char* rc_conf_regex = \ |
---|
40 | "\n" /* empty line */ \ |
---|
41 | "#\n" /* comment then nothing */ \ |
---|
42 | "# comment\n" /* comment */ |
---|
43 | " # \n" /* whitespace (ws), commend, ws */ \ |
---|
44 | "test_regex_1=\n" /* name, empty value */ \ |
---|
45 | "test_regex_2=\"\"\n" /* name, quoted empty value */ \ |
---|
46 | "test_regex_3=\"1 2 3\"\n" /* name, 3 args */ \ |
---|
47 | " test_regex_4=\"04\"\n" /* ws, name, 1 arg */ \ |
---|
48 | "\ttest_regex_5=\"05\" \n" /* ws, name, 1 arg, ws */ \ |
---|
49 | "test_regex_6=\" 1 2\t 3 \"\n"; /* name then ws and 3 args */ |
---|
50 | |
---|
51 | #define NUM_OF_TEST_REGEX_ 6 |
---|
52 | static bool test_regex_results[NUM_OF_TEST_REGEX_]; |
---|
53 | static int test_regex_last_num; |
---|
54 | |
---|
55 | static const char* rc_conf_not_found = \ |
---|
56 | "# invalid directive.\n" \ |
---|
57 | "abc_def_0=\"not found\"\n"; |
---|
58 | |
---|
59 | static int |
---|
60 | test_regex_(rtems_bsd_rc_conf* rc_conf, int argc, const char** argv) |
---|
61 | { |
---|
62 | int num; |
---|
63 | int arg; |
---|
64 | |
---|
65 | rtems_bsd_rc_conf_print_cmd(rc_conf, "test_regex_", argc, argv); |
---|
66 | |
---|
67 | assert(strncasecmp(argv[0], "test_regex_", strlen("test_regex_")) == 0); |
---|
68 | num = atoi(argv[0] + strlen("test_regex_")); |
---|
69 | assert(num == (test_regex_last_num + 1)); |
---|
70 | assert((num - 1) < NUM_OF_TEST_REGEX_); |
---|
71 | for (arg = 0; arg < argc; ++arg) { |
---|
72 | const char* a = argv[arg]; |
---|
73 | size_t l = strlen(a); |
---|
74 | if (l > 0) { |
---|
75 | assert(!isspace(a[0])); |
---|
76 | assert(!isspace(a[l - 1])); |
---|
77 | assert(a[0] != '"'); |
---|
78 | assert(a[l - 1] != '"'); |
---|
79 | } |
---|
80 | } |
---|
81 | test_regex_results[num - 1] = true; |
---|
82 | ++test_regex_last_num; |
---|
83 | |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | static void |
---|
88 | make_rc_conf(const char* rc_conf) |
---|
89 | { |
---|
90 | FILE* f; |
---|
91 | unlink(rc_conf); /* Ignore any errors */ |
---|
92 | assert((f = fopen(rc_conf, "w")) != NULL); |
---|
93 | assert(fwrite(rc_conf_regex, strlen(rc_conf_regex), 1, f) == 1); |
---|
94 | assert(fclose(f) == 0); |
---|
95 | } |
---|
96 | |
---|
97 | static void |
---|
98 | test_regex_check(void) |
---|
99 | { |
---|
100 | int i; |
---|
101 | assert(test_regex_last_num == NUM_OF_TEST_REGEX_); /* all items found? */ |
---|
102 | for (i = 0; i < NUM_OF_TEST_REGEX_; ++i) |
---|
103 | assert(test_regex_results[i]); |
---|
104 | } |
---|
105 | |
---|
106 | static void |
---|
107 | test_etc_rc_conf(void) |
---|
108 | { |
---|
109 | memset(&test_regex_results[0], 0, sizeof(test_regex_results)); |
---|
110 | test_regex_last_num = 0; |
---|
111 | make_rc_conf("/etc/rc.conf"); |
---|
112 | assert(rtems_bsd_run_etc_rc_conf(true) == 0); |
---|
113 | test_regex_check(); |
---|
114 | } |
---|
115 | |
---|
116 | static void |
---|
117 | test_rc_conf(void) |
---|
118 | { |
---|
119 | memset(&test_regex_results[0], 0, sizeof(test_regex_results)); |
---|
120 | test_regex_last_num = 0; |
---|
121 | make_rc_conf("/my_rc.conf"); |
---|
122 | assert(rtems_bsd_run_rc_conf("/my_rc.conf", true) == 0); |
---|
123 | test_regex_check(); |
---|
124 | } |
---|
125 | |
---|
126 | static void |
---|
127 | test_rc_conf_script(void) |
---|
128 | { |
---|
129 | memset(&test_regex_results[0], 0, sizeof(test_regex_results)); |
---|
130 | test_regex_last_num = 0; |
---|
131 | assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_regex, true) == 0); |
---|
132 | test_regex_check(); |
---|
133 | } |
---|
134 | |
---|
135 | static void |
---|
136 | test_rc_conf_script_not_found(void) |
---|
137 | { |
---|
138 | assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_not_found, true) < 0); |
---|
139 | } |
---|
140 | |
---|
141 | static void |
---|
142 | setup(void) |
---|
143 | { |
---|
144 | struct stat sb; |
---|
145 | mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO); /* ignore errors, check the dir after. */ |
---|
146 | assert(stat("/etc", &sb) == 0); |
---|
147 | assert(S_ISDIR(sb.st_mode)); |
---|
148 | assert(rtems_bsd_rc_conf_directive_add("test_regex_.*", test_regex_) == 0); |
---|
149 | } |
---|
150 | |
---|
151 | static void |
---|
152 | test_main(void) |
---|
153 | { |
---|
154 | setup(); |
---|
155 | |
---|
156 | test_etc_rc_conf(); |
---|
157 | test_rc_conf(); |
---|
158 | test_rc_conf_script(); |
---|
159 | test_rc_conf_script_not_found(); |
---|
160 | |
---|
161 | exit(0); |
---|
162 | } |
---|
163 | |
---|
164 | #include <rtems/bsd/test/default-init.h> |
---|