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

55-freebsd-126-freebsd-12
Last change on this file since 4437573 was 4437573, checked in by Chris Johns <chrisj@…>, on 04/27/17 at 03:57:28

rc_conf: Add support for ifconfig_<interface>_alias[0-9]+.

The interface alias allows extra IP addresses to be set on and
interface and it also allows the MAC address to be set.

Examples:

ifconfig_em0="inet 10.10.5.33 netmask 255.255.255.0"
ifconfig_em0_alias0="ether 10:22:33:44:55:66"
ifconfig_em0_alias1="inet 10.1.1.111 netmask 0xffffffff"

  • Property mode set to 100644
File size: 9.1 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  /*
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
149static int
150test_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
158static int
159test_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
167static int
168test_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
176static int
177test_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
185static int
186test_service_bad(rtems_bsd_rc_conf* rc_conf)
187{
188  puts("test_service_bad");
189  return -1;
190}
191
192static void
193make_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
202static void
203test_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
211static void
212test_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
219static void
220test_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
229static void
230test_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
239static void
240test_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
248static void
249test_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
257static void
258setup(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
293static void
294test_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>
Note: See TracBrowser for help on using the repository browser.