]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/bus-proxyd/test-bus-xml-policy.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / bus-proxyd / test-bus-xml-policy.c
CommitLineData
20725d92
DM
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
20725d92 22#include <errno.h>
20725d92 23#include <stddef.h>
07630cea 24#include <unistd.h>
20725d92 25
20725d92 26#include "sd-bus.h"
07630cea 27
3c70e3bb 28#include "bus-xml-policy.h"
07630cea
LP
29#include "log.h"
30#include "string-util.h"
31#include "strv.h"
32#include "util.h"
20725d92 33
3c70e3bb 34static int test_policy_load(Policy *p, const char *name) {
5e90b6a9 35 _cleanup_free_ char *path = NULL;
45f1b67a
DM
36 int r = 0;
37
38 path = strjoin(TEST_DIR, "/bus-policy/", name, NULL);
5e90b6a9 39 assert_se(path);
45f1b67a
DM
40
41 if (access(path, R_OK) == 0)
48aae6d6 42 r = policy_load(p, STRV_MAKE(path));
45f1b67a
DM
43 else
44 r = -ENOENT;
45
45f1b67a
DM
46 return r;
47}
48
48aae6d6
LP
49static int show_policy(const char *fn) {
50 Policy p = {};
51 int r;
52
53 r = policy_load(&p, STRV_MAKE(fn));
54 if (r < 0) {
55 log_error_errno(r, "Failed to load policy %s: %m", fn);
56 return r;
57 }
58
59 policy_dump(&p);
60 policy_free(&p);
61
62 return 0;
63}
64
20725d92
DM
65int main(int argc, char *argv[]) {
66
67 Policy p = {};
20725d92 68
48aae6d6
LP
69 printf("Showing session policy BEGIN\n");
70 show_policy("/etc/dbus-1/session.conf");
71 printf("Showing session policy END\n");
72
73 printf("Showing system policy BEGIN\n");
74 show_policy("/etc/dbus-1/system.conf");
75 printf("Showing system policy END\n");
76
20725d92 77 /* Ownership tests */
45f1b67a 78 assert_se(test_policy_load(&p, "ownerships.conf") == 0);
20725d92 79
78f9b196
LP
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);
20725d92 82
78f9b196
LP
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);
20725d92 85
78f9b196
LP
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);
20725d92 88
78f9b196
LP
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);
20725d92
DM
91
92 policy_free(&p);
93
078ef7b8 94 /* Signaltest */
45f1b67a 95 assert_se(test_policy_load(&p, "signals.conf") == 0);
20725d92 96
7447362c
DH
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);
20725d92
DM
99
100 policy_free(&p);
101
102 /* Method calls */
45f1b67a 103 assert_se(test_policy_load(&p, "methods.conf") == 0);
078ef7b8 104 policy_dump(&p);
20725d92 105
7447362c
DH
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);
20725d92 110
7447362c 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);
20725d92
DM
112
113 policy_free(&p);
114
115 /* User and groups */
45f1b67a 116 assert_se(test_policy_load(&p, "hello.conf") == 0);
20725d92
DM
117 policy_dump(&p);
118
78f9b196
LP
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);
20725d92
DM
122
123 policy_free(&p);
124
3a9cca11
LP
125 /* dbus1 test file: ownership */
126
127 assert_se(test_policy_load(&p, "check-own-rules.conf") >= 0);
128 policy_dump(&p);
129
78f9b196
LP
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);
3a9cca11
LP
138
139 policy_free(&p);
140
55e18900
LP
141 /* dbus1 test file: many rules */
142
143 assert_se(test_policy_load(&p, "many-rules.conf") >= 0);
144 policy_dump(&p);
145 policy_free(&p);
146
147 /* dbus1 test file: generic test */
148
149 assert_se(test_policy_load(&p, "test.conf") >= 0);
150 policy_dump(&p);
278ebf8d 151
78f9b196
LP
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);
7447362c
DH
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);
78f9b196
LP
159
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);
7447362c
DH
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);
278ebf8d 167
55e18900
LP
168 policy_free(&p);
169
20725d92
DM
170 return EXIT_SUCCESS;
171}