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

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f5c6651 was f5c6651, checked in by Chris Johns <chrisj@…>, on 05/18/16 at 03:49:17

Add support for rc.conf(5) initialisation.

Provide user support for rc.conf(5) so a user can create a suitable
/etc/rc.conf file to initialise libbsd.

This patch by default adds basic networking support:

cloned_interfaces
ifconfig_'interface'
defaultrouter
hostname

Refer to FreeBSD documentation for examples.

Users can make a single call to have /etc/rc.conf processed, or pass a
file name to a specific configuration file or a text string with line
feeds can be passed to the scripting version of the interface.

The rc.conf support is implemented in terms of directive handlers that
are called based on a regular expression. The design allows new handlers
to be added as needed.

Line concatenation is still to be implemented.

  • Property mode set to 100644
File size: 4.8 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 <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
39static 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
52static bool test_regex_results[NUM_OF_TEST_REGEX_];
53static int  test_regex_last_num;
54
55static const char* rc_conf_not_found = \
56  "# invalid directive.\n" \
57  "abc_def_0=\"not found\"\n";
58
59static int
60test_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
87static void
88make_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
97static void
98test_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
106static void
107test_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
116static void
117test_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
126static void
127test_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
135static void
136test_rc_conf_script_not_found(void)
137{
138  assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_not_found, true) < 0);
139}
140
141static void
142setup(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
151static void
152test_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>
Note: See TracBrowser for help on using the repository browser.