chiark / gitweb /
bus-policy: actually test messages against the newly added test.conf
[elogind.git] / src / bus-proxyd / test-bus-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
44 #include <bus-proxyd/bus-policy.h>
45
46 static int test_policy_load(Policy *p, const char *name)
47 {
48         _cleanup_free_ char *path = NULL;
49         int r = 0;
50
51         path = strjoin(TEST_DIR, "/bus-policy/", name, NULL);
52         assert_se(path);
53
54         if (access(path, R_OK) == 0)
55                 policy_load(p, STRV_MAKE(path));
56         else
57                 r = -ENOENT;
58
59         return r;
60 }
61
62 int main(int argc, char *argv[]) {
63
64         Policy p = {};
65         struct ucred ucred = {};
66
67         /* Ownership tests */
68         assert_se(test_policy_load(&p, "ownerships.conf") == 0);
69
70         ucred.uid = 0;
71         assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);
72         ucred.uid = 1;
73         assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);
74
75         ucred.uid = 0;
76         assert_se(policy_check_own(&p, &ucred, "org.test.test2") == true);
77         ucred.uid = 1;
78         assert_se(policy_check_own(&p, &ucred, "org.test.test2") == false);
79
80         ucred.uid = 0;
81         assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);
82         ucred.uid = 1;
83         assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);
84
85         ucred.uid = 0;
86         assert_se(policy_check_own(&p, &ucred, "org.test.test4") == false);
87         ucred.uid = 1;
88         assert_se(policy_check_own(&p, &ucred, "org.test.test4") == true);
89
90         policy_free(&p);
91
92         /* Signaltest */
93         assert_se(test_policy_load(&p, "signals.conf") == 0);
94
95         ucred.uid = 0;
96         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == true);
97
98         ucred.uid = 1;
99         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == false);
100
101         policy_free(&p);
102
103         /* Method calls */
104         assert_se(test_policy_load(&p, "methods.conf") == 0);
105         policy_dump(&p);
106
107         ucred.uid = 0;
108
109         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
110         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
111         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == true);
112         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == true);
113
114         assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test3", "/an/object/path", "org.test.int3", "Member111") == true);
115
116         policy_free(&p);
117
118         /* User and groups */
119         assert_se(test_policy_load(&p, "hello.conf") == 0);
120         policy_dump(&p);
121
122         ucred.uid = 0;
123         assert_se(policy_check_hello(&p, &ucred) == true);
124
125         ucred.uid = 1;
126         assert_se(policy_check_hello(&p, &ucred) == false);
127
128         ucred.uid = 0;
129         ucred.gid = 1;
130         assert_se(policy_check_hello(&p, &ucred) == false);
131
132         policy_free(&p);
133
134         /* dbus1 test file: ownership */
135
136         assert_se(test_policy_load(&p, "check-own-rules.conf") >= 0);
137         policy_dump(&p);
138
139         assert_se(policy_check_own(&p, &ucred, "org.freedesktop") == false);
140         assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystem") == false);
141         assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems") == true);
142         assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems.foo") == true);
143         assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems.foo.bar") == true);
144         assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems2") == false);
145         assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems2.foo") == false);
146         assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems2.foo.bar") == false);
147
148         policy_free(&p);
149
150         /* dbus1 test file: many rules */
151
152         assert_se(test_policy_load(&p, "many-rules.conf") >= 0);
153         policy_dump(&p);
154         policy_free(&p);
155
156         /* dbus1 test file: generic test */
157
158         assert_se(test_policy_load(&p, "test.conf") >= 0);
159         policy_dump(&p);
160
161         ucred.uid = 0;
162         assert_se(policy_check_own(&p, &ucred, "org.foo.FooService") == true);
163         assert_se(policy_check_own(&p, &ucred, "org.foo.FooService2") == false);
164         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
165         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
166         assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
167         assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
168         assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService2", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
169
170         ucred.uid = 100;
171         assert_se(policy_check_own(&p, &ucred, "org.foo.FooService") == false);
172         assert_se(policy_check_own(&p, &ucred, "org.foo.FooService2") == false);
173         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
174         assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
175         assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
176         assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
177         assert_se(policy_check_recv(&p, &ucred, 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 }