1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Daniel Mack
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.
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.
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/>.
28 #include "bus-xml-policy.h"
30 #include "string-util.h"
34 static int test_policy_load(Policy
*p
, const char *name
) {
35 _cleanup_free_
char *path
= NULL
;
38 path
= strjoin(TEST_DIR
, "/bus-policy/", name
, NULL
);
41 if (access(path
, R_OK
) == 0)
42 r
= policy_load(p
, STRV_MAKE(path
));
49 static int show_policy(const char *fn
) {
53 r
= policy_load(&p
, STRV_MAKE(fn
));
55 log_error_errno(r
, "Failed to load policy %s: %m", fn
);
65 int main(int argc
, char *argv
[]) {
69 printf("Showing session policy BEGIN\n");
70 show_policy("/etc/dbus-1/session.conf");
71 printf("Showing session policy END\n");
73 printf("Showing system policy BEGIN\n");
74 show_policy("/etc/dbus-1/system.conf");
75 printf("Showing system policy END\n");
78 assert_se(test_policy_load(&p
, "ownerships.conf") == 0);
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);
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);
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);
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);
95 assert_se(test_policy_load(&p
, "signals.conf") == 0);
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);
103 assert_se(test_policy_load(&p
, "methods.conf") == 0);
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);
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);
115 /* User and groups */
116 assert_se(test_policy_load(&p
, "hello.conf") == 0);
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);
125 /* dbus1 test file: ownership */
127 assert_se(test_policy_load(&p
, "check-own-rules.conf") >= 0);
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);
141 /* dbus1 test file: many rules */
143 assert_se(test_policy_load(&p
, "many-rules.conf") >= 0);
147 /* dbus1 test file: generic test */
149 assert_se(test_policy_load(&p
, "test.conf") >= 0);
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);
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);