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