]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/bus-proxyd/test-bus-xml-policy.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / bus-proxyd / test-bus-xml-policy.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Daniel Mack
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <stddef.h>
24 #include <unistd.h>
25
26 #include "sd-bus.h"
27
28 #include "bus-xml-policy.h"
29 #include "log.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "util.h"
33
34 static int test_policy_load(Policy *p, const char *name) {
35 _cleanup_free_ char *path = NULL;
36 int r = 0;
37
38 path = strjoin(TEST_DIR, "/bus-policy/", name, NULL);
39 assert_se(path);
40
41 if (access(path, R_OK) == 0)
42 r = policy_load(p, STRV_MAKE(path));
43 else
44 r = -ENOENT;
45
46 return r;
47 }
48
49 static int show_policy(const char *fn) {
50 Policy p = {};
51 int r;
52
53 r = policy_load(&p, STRV_MAKE(fn));
54 if (r < 0) {
55 log_error_errno(r, "Failed to load policy %s: %m", fn);
56 return r;
57 }
58
59 policy_dump(&p);
60 policy_free(&p);
61
62 return 0;
63 }
64
65 int main(int argc, char *argv[]) {
66
67 Policy p = {};
68
69 printf("Showing session policy BEGIN\n");
70 show_policy("/etc/dbus-1/session.conf");
71 printf("Showing session policy END\n");
72
73 printf("Showing system policy BEGIN\n");
74 show_policy("/etc/dbus-1/system.conf");
75 printf("Showing system policy END\n");
76
77 /* Ownership tests */
78 assert_se(test_policy_load(&p, "ownerships.conf") == 0);
79
80 assert_se(policy_check_own(&p, 0, 0, "org.test.test1") == true);
81 assert_se(policy_check_own(&p, 1, 0, "org.test.test1") == true);
82
83 assert_se(policy_check_own(&p, 0, 0, "org.test.test2") == true);
84 assert_se(policy_check_own(&p, 1, 0, "org.test.test2") == false);
85
86 assert_se(policy_check_own(&p, 0, 0, "org.test.test3") == false);
87 assert_se(policy_check_own(&p, 1, 0, "org.test.test3") == false);
88
89 assert_se(policy_check_own(&p, 0, 0, "org.test.test4") == false);
90 assert_se(policy_check_own(&p, 1, 0, "org.test.test4") == true);
91
92 policy_free(&p);
93
94 /* Signaltest */
95 assert_se(test_policy_load(&p, "signals.conf") == 0);
96
97 assert_se(policy_check_one_send(&p, 0, 0, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == true);
98 assert_se(policy_check_one_send(&p, 1, 0, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == false);
99
100 policy_free(&p);
101
102 /* Method calls */
103 assert_se(test_policy_load(&p, "methods.conf") == 0);
104 policy_dump(&p);
105
106 assert_se(policy_check_one_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
107 assert_se(policy_check_one_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
108 assert_se(policy_check_one_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == true);
109 assert_se(policy_check_one_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == true);
110
111 assert_se(policy_check_one_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test3", "/an/object/path", "org.test.int3", "Member111") == true);
112
113 policy_free(&p);
114
115 /* User and groups */
116 assert_se(test_policy_load(&p, "hello.conf") == 0);
117 policy_dump(&p);
118
119 assert_se(policy_check_hello(&p, 0, 0) == true);
120 assert_se(policy_check_hello(&p, 1, 0) == false);
121 assert_se(policy_check_hello(&p, 0, 1) == false);
122
123 policy_free(&p);
124
125 /* dbus1 test file: ownership */
126
127 assert_se(test_policy_load(&p, "check-own-rules.conf") >= 0);
128 policy_dump(&p);
129
130 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop") == false);
131 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystem") == false);
132 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems") == true);
133 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems.foo") == true);
134 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems.foo.bar") == true);
135 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems2") == false);
136 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems2.foo") == false);
137 assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems2.foo.bar") == false);
138
139 policy_free(&p);
140
141 /* dbus1 test file: many rules */
142
143 assert_se(test_policy_load(&p, "many-rules.conf") >= 0);
144 policy_dump(&p);
145 policy_free(&p);
146
147 /* dbus1 test file: generic test */
148
149 assert_se(test_policy_load(&p, "test.conf") >= 0);
150 policy_dump(&p);
151
152 assert_se(policy_check_own(&p, 0, 0, "org.foo.FooService") == true);
153 assert_se(policy_check_own(&p, 0, 0, "org.foo.FooService2") == false);
154 assert_se(policy_check_one_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
155 assert_se(policy_check_one_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
156 assert_se(policy_check_one_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
157 assert_se(policy_check_one_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
158 assert_se(policy_check_one_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService2", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
159
160 assert_se(policy_check_own(&p, 100, 0, "org.foo.FooService") == false);
161 assert_se(policy_check_own(&p, 100, 0, "org.foo.FooService2") == false);
162 assert_se(policy_check_one_send(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
163 assert_se(policy_check_one_send(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
164 assert_se(policy_check_one_recv(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
165 assert_se(policy_check_one_recv(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
166 assert_se(policy_check_one_recv(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService2", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
167
168 policy_free(&p);
169
170 return EXIT_SUCCESS;
171 }