chiark / gitweb /
bus-proxy-test: show parsed system/session policy
[elogind.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 <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/poll.h>
30 #include <stddef.h>
31 #include <getopt.h>
32
33 #include "log.h"
34 #include "util.h"
35 #include "sd-bus.h"
36 #include "bus-internal.h"
37 #include "bus-message.h"
38 #include "bus-util.h"
39 #include "build.h"
40 #include "strv.h"
41 #include "def.h"
42 #include "capability.h"
43 #include "bus-xml-policy.h"
44
45 static int test_policy_load(Policy *p, const char *name) {
46         _cleanup_free_ char *path = NULL;
47         int r = 0;
48
49         path = strjoin(TEST_DIR, "/bus-policy/", name, NULL);
50         assert_se(path);
51
52         if (access(path, R_OK) == 0)
53                 r = policy_load(p, STRV_MAKE(path));
54         else
55                 r = -ENOENT;
56
57         return r;
58 }
59
60 static int show_policy(const char *fn) {
61         Policy p = {};
62         int r;
63
64         r = policy_load(&p, STRV_MAKE(fn));
65         if (r < 0) {
66                 log_error_errno(r, "Failed to load policy %s: %m", fn);
67                 return r;
68         }
69
70         policy_dump(&p);
71         policy_free(&p);
72
73         return 0;
74 }
75
76 int main(int argc, char *argv[]) {
77
78         Policy p = {};
79
80         printf("Showing session policy BEGIN\n");
81         show_policy("/etc/dbus-1/session.conf");
82         printf("Showing session policy END\n");
83
84         printf("Showing system policy BEGIN\n");
85         show_policy("/etc/dbus-1/system.conf");
86         printf("Showing system policy END\n");
87
88         /* Ownership tests */
89         assert_se(test_policy_load(&p, "ownerships.conf") == 0);
90
91         assert_se(policy_check_own(&p, 0, 0, "org.test.test1") == true);
92         assert_se(policy_check_own(&p, 1, 0, "org.test.test1") == true);
93
94         assert_se(policy_check_own(&p, 0, 0, "org.test.test2") == true);
95         assert_se(policy_check_own(&p, 1, 0, "org.test.test2") == false);
96
97         assert_se(policy_check_own(&p, 0, 0, "org.test.test3") == false);
98         assert_se(policy_check_own(&p, 1, 0, "org.test.test3") == false);
99
100         assert_se(policy_check_own(&p, 0, 0, "org.test.test4") == false);
101         assert_se(policy_check_own(&p, 1, 0, "org.test.test4") == true);
102
103         policy_free(&p);
104
105         /* Signaltest */
106         assert_se(test_policy_load(&p, "signals.conf") == 0);
107
108         assert_se(policy_check_send(&p, 0, 0, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == true);
109         assert_se(policy_check_send(&p, 1, 0, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == false);
110
111         policy_free(&p);
112
113         /* Method calls */
114         assert_se(test_policy_load(&p, "methods.conf") == 0);
115         policy_dump(&p);
116
117         assert_se(policy_check_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
118         assert_se(policy_check_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
119         assert_se(policy_check_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == true);
120         assert_se(policy_check_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == true);
121
122         assert_se(policy_check_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test3", "/an/object/path", "org.test.int3", "Member111") == true);
123
124         policy_free(&p);
125
126         /* User and groups */
127         assert_se(test_policy_load(&p, "hello.conf") == 0);
128         policy_dump(&p);
129
130         assert_se(policy_check_hello(&p, 0, 0) == true);
131         assert_se(policy_check_hello(&p, 1, 0) == false);
132         assert_se(policy_check_hello(&p, 0, 1) == false);
133
134         policy_free(&p);
135
136         /* dbus1 test file: ownership */
137
138         assert_se(test_policy_load(&p, "check-own-rules.conf") >= 0);
139         policy_dump(&p);
140
141         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop") == false);
142         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystem") == false);
143         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems") == true);
144         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems.foo") == true);
145         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems.foo.bar") == true);
146         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems2") == false);
147         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems2.foo") == false);
148         assert_se(policy_check_own(&p, 0, 0, "org.freedesktop.ManySystems2.foo.bar") == false);
149
150         policy_free(&p);
151
152         /* dbus1 test file: many rules */
153
154         assert_se(test_policy_load(&p, "many-rules.conf") >= 0);
155         policy_dump(&p);
156         policy_free(&p);
157
158         /* dbus1 test file: generic test */
159
160         assert_se(test_policy_load(&p, "test.conf") >= 0);
161         policy_dump(&p);
162
163         assert_se(policy_check_own(&p, 0, 0, "org.foo.FooService") == true);
164         assert_se(policy_check_own(&p, 0, 0, "org.foo.FooService2") == false);
165         assert_se(policy_check_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
166         assert_se(policy_check_send(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
167         assert_se(policy_check_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
168         assert_se(policy_check_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
169         assert_se(policy_check_recv(&p, 0, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService2", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
170
171         assert_se(policy_check_own(&p, 100, 0, "org.foo.FooService") == false);
172         assert_se(policy_check_own(&p, 100, 0, "org.foo.FooService2") == false);
173         assert_se(policy_check_send(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
174         assert_se(policy_check_send(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
175         assert_se(policy_check_recv(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
176         assert_se(policy_check_recv(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
177         assert_se(policy_check_recv(&p, 100, 0, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService2", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
178
179         policy_free(&p);
180
181         return EXIT_SUCCESS;
182 }