source: rtems-libbsd/testsuite/rcconf01/test_main.c @ 55c564a

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 55c564a was 55c564a, checked in by Chris Johns <chrisj@…>, on 06/27/16 at 03:24:43

Refactor rc.conf processing to be services based.

  • Property mode set to 100644
File size: 7.9 KB
Line 
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
43static 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
56static bool test_regex_results[NUM_OF_TEST_REGEX_];
57static int  test_regex_last_num;
58static int  test_service_last_num;
59
60static const char* rc_conf_not_found = \
61  "# invalid directive.\n" \
62  "abc_def_0=\"not found\"\n";
63
64static int
65test_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  assert((aa = rtems_bsd_rc_conf_argc_argv_create()) != NULL);
73  r = rtems_bsd_rc_conf_find(rc_conf, "test_regex_.*", aa);
74  assert(r == 0 || (r < 0 && errno == ENOENT));
75  if (r < 0 && errno == ENOENT)
76    return -1;
77
78  while (r == 0) {
79    int num;
80    int arg;
81    rtems_bsd_rc_conf_print_cmd(rc_conf, "test_service", aa->argc, aa->argv);
82    assert(strncasecmp(aa->argv[0], "test_regex_", strlen("test_regex_")) == 0);
83    num = atoi(aa->argv[0] + strlen("test_regex_"));
84    assert(num == (test_regex_last_num + 1));
85    assert((num - 1) < NUM_OF_TEST_REGEX_);
86    for (arg = 0; arg < aa->argc; ++arg) {
87      const char* a = aa->argv[arg];
88      size_t      l = strlen(a);
89      if (l > 0) {
90        assert(!isspace(a[0]));
91        assert(!isspace(a[l - 1]));
92        assert(a[0] != '"');
93        assert(a[l - 1] != '"');
94      }
95    }
96    test_regex_results[num - 1] = true;
97    ++test_regex_last_num;
98    r = rtems_bsd_rc_conf_find_next(rc_conf, aa);
99    assert(r == 0 || (r < 0 && errno == ENOENT));
100  }
101  rtems_bsd_rc_conf_argc_argv_destroy(aa);
102  puts("test_service done");
103  return 0;
104}
105
106static int
107test_service_2(rtems_bsd_rc_conf* rc_conf)
108{
109  puts("test_service_2");
110  assert(test_service_last_num == 1);
111  test_service_last_num = 2;
112  return 0;
113}
114
115static int
116test_service_3(rtems_bsd_rc_conf* rc_conf)
117{
118  puts("test_service_3");
119  assert(test_service_last_num == 2);
120  test_service_last_num = 3;
121  return 0;
122}
123
124static int
125test_service_4(rtems_bsd_rc_conf* rc_conf)
126{
127  puts("test_service_4");
128  assert(test_service_last_num == 3);
129  test_service_last_num = 4;
130  return 0;
131}
132
133static int
134test_service_5(rtems_bsd_rc_conf* rc_conf)
135{
136  puts("test_service_5");
137  assert(test_service_last_num == 4);
138  test_service_last_num = 5;
139  return 0;
140}
141
142static int
143test_service_bad(rtems_bsd_rc_conf* rc_conf)
144{
145  puts("test_service_bad");
146  return -1;
147}
148
149static void
150make_rc_conf(const char* rc_conf)
151{
152  FILE* f;
153  unlink(rc_conf); /* Ignore any errors */
154  assert((f = fopen(rc_conf, "w")) != NULL);
155  assert(fwrite(rc_conf_regex, strlen(rc_conf_regex), 1, f) == 1);
156  assert(fclose(f) == 0);
157}
158
159static void
160test_regex_check(void)
161{
162  int i;
163  assert(test_regex_last_num == NUM_OF_TEST_REGEX_); /* all items found? */
164  for (i = 0; i < NUM_OF_TEST_REGEX_; ++i)
165    assert(test_regex_results[i]);
166}
167
168static void
169test_regex_reset(void)
170{
171  memset(&test_regex_results[0], 0, sizeof(test_regex_results));
172  test_regex_last_num = 0;
173  test_service_last_num = 0;
174 }
175
176static void
177test_etc_rc_conf(void)
178{
179  puts("test_etc_rc_conf");
180  make_rc_conf("/etc/rc.conf");
181  test_regex_reset();
182  assert(rtems_bsd_run_etc_rc_conf(0, true) == 0);
183  test_regex_check();
184}
185
186static void
187test_rc_conf(void)
188{
189  puts("test_rc_conf");
190  make_rc_conf("/my_rc.conf");
191  test_regex_reset();
192  assert(rtems_bsd_run_rc_conf("/my_rc.conf", 0, true) == 0);
193  test_regex_check();
194}
195
196static void
197test_rc_conf_script(void)
198{
199  puts("test_rc_conf_conf");
200  test_regex_reset();
201  assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_regex, 0, true) == 0);
202  test_regex_check();
203}
204
205static void
206test_rc_conf_script_not_found(void)
207{
208  puts("test_rc_conf_conf_not_found");
209  test_regex_reset();
210  assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_not_found, 0, true) < 0);
211  assert(test_regex_last_num == 0);
212}
213
214static void
215setup(void)
216{
217  struct stat sb;
218  mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO); /* ignore errors, check the dir after. */
219  assert(stat("/etc", &sb) == 0);
220  assert(S_ISDIR(sb.st_mode));
221  assert(rtems_bsd_rc_conf_service_add("test_service_2",
222                                       "before:last;",
223                                       test_service_2) == 0);
224  assert(rtems_bsd_rc_conf_service_add("test_service_5",
225                                       "after:test_service_2;",
226                                       test_service_5) == 0);
227  assert(rtems_bsd_rc_conf_service_add("test_service_4",
228                                       "before:test_service_5;",
229                                       test_service_4) == 0);
230  assert(rtems_bsd_rc_conf_service_add("test_service_3",
231                                       "before:test_service_4;after:test_service_2;",
232                                       test_service_3) == 0);
233  assert(rtems_bsd_rc_conf_service_add("test_service_bad",
234                                       "before:first;",
235                                       test_service_bad) < 0);
236  assert(rtems_bsd_rc_conf_service_add("test_service_bad",
237                                       "after:last;",
238                                       test_service_bad) < 0);
239  assert(rtems_bsd_rc_conf_service_add("test_service_bad",
240                                       "after:xxxx,xxxx",
241                                       test_service_bad) < 0);
242  assert(rtems_bsd_rc_conf_service_add("test_service_bad",
243                                       "yyyy:xxxx;",
244                                       test_service_bad) < 0);
245  assert(rtems_bsd_rc_conf_service_add("test_service",
246                                       "after:first;",
247                                       test_service) == 0);
248}
249
250static void
251test_main(void)
252{
253  setup();
254
255  test_etc_rc_conf();
256  test_rc_conf();
257  test_rc_conf_script();
258  test_rc_conf_script_not_found();
259
260  exit(0);
261}
262
263#include <rtems/bsd/test/default-init.h>
Note: See TracBrowser for help on using the repository browser.