Debian Codegraph @

src/launch/test-config.c

raw
1 /*
2 * Test Config Parser
3 */
4
5 #undef NDEBUG
6 #include <c-list.h>
7 #include <c-stdaux.h>
8 #include <stdlib.h>
9 #include "launch/config.h"
10 #include "launch/nss-cache.h"
11 #include "util/dirwatch.h"
12 #include "util/misc.h"
13
14 static const char *test_type2str[_CONFIG_NODE_N] = {
15 [CONFIG_NODE_BUSCONFIG] = "busconfig",
16 [CONFIG_NODE_USER] = "user",
17 [CONFIG_NODE_TYPE] = "type",
18 [CONFIG_NODE_FORK] = "fork",
19 [CONFIG_NODE_SYSLOG] = "syslog",
20 [CONFIG_NODE_KEEP_UMASK] = "keep_umask",
21 [CONFIG_NODE_LISTEN] = "listen",
22 [CONFIG_NODE_PIDFILE] = "pidfile",
23 [CONFIG_NODE_INCLUDEDIR] = "includedir",
24 [CONFIG_NODE_STANDARD_SESSION_SERVICEDIRS] = "standard_session_servicedirs",
25 [CONFIG_NODE_STANDARD_SYSTEM_SERVICEDIRS] = "standard_system_servicedirs",
26 [CONFIG_NODE_SERVICEDIR] = "servicedir",
27 [CONFIG_NODE_SERVICEHELPER] = "servicehelper",
28 [CONFIG_NODE_AUTH] = "auth",
29 [CONFIG_NODE_INCLUDE] = "include",
30 [CONFIG_NODE_POLICY] = "policy",
31 [CONFIG_NODE_LIMIT] = "limit",
32 [CONFIG_NODE_SELINUX] = "selinux",
33 [CONFIG_NODE_APPARMOR] = "apparmor",
34 [CONFIG_NODE_ALLOW] = "allow",
35 [CONFIG_NODE_DENY] = "deny",
36 [CONFIG_NODE_ASSOCIATE] = "associate",
37 };
38
39 static int config_memfd(const char *data) {
40 ssize_t n;
41 int fd;
42
43 fd = misc_memfd("dbus-broker-test-config", MISC_MFD_NOEXEC_SEAL, 0);
44 c_assert(fd >= 0);
45 n = write(fd, data, strlen(data));
46 c_assert(n == (ssize_t)strlen(data));
47
48 return fd;
49 }
50
51 static int parse_config(ConfigRoot **rootp, const char *path) {
52 _c_cleanup_(config_parser_deinit) ConfigParser parser = CONFIG_PARSER_NULL(parser);
53 _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
54 _c_cleanup_(nss_cache_deinit) NSSCache nss_cache = NSS_CACHE_INIT;
55 _c_cleanup_(dirwatch_freep) Dirwatch *dirwatch = NULL;
56 int r;
57
58 r = dirwatch_new(&dirwatch);
59 c_assert(!r);
60
61 config_parser_init(&parser);
62
63 r = config_parser_read(&parser, &root, path, &nss_cache, dirwatch);
64 if (r)
65 return r;
66
67 *rootp = root;
68 root = NULL;
69 return 0;
70 }
71
72 static int parse_config_inline(ConfigRoot **rootp, const char *data) {
73 _c_cleanup_(c_closep) int fd = -1;
74 _c_cleanup_(c_freep) char *path = NULL;
75 int r;
76
77 fd = config_memfd(data);
78 r = asprintf(&path, "/proc/self/fd/%d", fd);
79 c_assert(r > 0);
80
81 return parse_config(rootp, path);
82 }
83
84 static void print_config(const char *path) {
85 _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
86 ConfigNode *i_node;
87 int r;
88
89 r = parse_config(&root, path);
90 c_assert(!r);
91
92 c_list_for_each_entry(i_node, &root->node_list, root_link) {
93 fprintf(stderr, "<%s>\n", test_type2str[i_node->type]);
94 }
95 }
96
97 static void test_config_base(void) {
98 _c_cleanup_(config_parser_deinit) ConfigParser parser = CONFIG_PARSER_NULL(parser);
99
100 config_parser_init(&parser);
101 config_parser_deinit(&parser);
102 }
103
104 static void test_config_sample0(void) {
105 _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
106 const char *data;
107 int r;
108
109 data =
110 "<?xml version=\"1.0\"?> <!--*-nxml-*-->\
111 <!DOCTYPE g PUBLIC \"-/N\"\
112 \"htt\">\
113 <busconfig>\
114 <policy user=\"root\">\
115 <allow own_prefix=\"oramd\"/>\
116 <allow send_interface=\"d\"/>\
117 </policy>\
118 <user ix=\"d\"/>\
119 </cy>";
120
121 r = parse_config_inline(&root, data);
122 c_assert(r == CONFIG_E_INVALID);
123 }
124
125 static void test_config_sample1(void) {
126 _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
127 const char *data;
128 int r;
129
130 data =
131 "<?xml version=\"1.0\"?> <!--*-nxml-*-->\
132 <!DOCTYPE g PUBLIC \"-/N\"\
133 \"htt\">\
134 <busconfig>\
135 <policy user=\"root\">\
136 <allow own_prefix=\"oramd\"/>\
137 <allow send_interface=\"d\"/>\
138 </policy>\
139 <policy context=\"default\"/> <user ix=\"d\"/>\
140 </policy>\
141 </busconfig>";
142
143 r = parse_config_inline(&root, data);
144 c_assert(r == CONFIG_E_INVALID);
145 }
146
147 int main(int argc, char **argv) {
148 if (argc > 1) {
149 print_config(argv[1]);
150 return 0;
151 }
152
153 test_config_base();
154 test_config_sample0();
155 test_config_sample1();
156
157 return 0;
158 }