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