chiark / gitweb /
bus-policy: split API for bus-proxyd
[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 "bus-internal.h"
40 #include "build.h"
41 #include "strv.h"
42 #include "def.h"
43 #include "capability.h"
44
45 #include <bus-proxyd/bus-policy.h>
46
47 int main(int argc, char *argv[]) {
48
49         Policy p = {};
50         struct ucred ucred = {};
51         char **names_strv;
52         Hashmap *names_hash;
53
54         /* Ownership tests */
55         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/ownerships.conf")) == 0);
56
57         ucred.uid = 0;
58         assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);
59         ucred.uid = 1;
60         assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);
61
62         ucred.uid = 0;
63         assert_se(policy_check_own(&p, &ucred, "org.test.test2") == true);
64         ucred.uid = 1;
65         assert_se(policy_check_own(&p, &ucred, "org.test.test2") == false);
66
67         ucred.uid = 0;
68         assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);
69         ucred.uid = 1;
70         assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);
71
72         ucred.uid = 0;
73         assert_se(policy_check_own(&p, &ucred, "org.test.test4") == false);
74         ucred.uid = 1;
75         assert_se(policy_check_own(&p, &ucred, "org.test.test4") == true);
76
77         policy_free(&p);
78
79         /* Signaltest */
80         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/signals.conf")) == 0);
81         names_strv = STRV_MAKE("bli.bla.blubb");
82
83         ucred.uid = 0;
84         assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == true);
85
86         ucred.uid = 1;
87         assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == false);
88
89         policy_free(&p);
90
91         /* Method calls */
92         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/methods.conf")) == 0);
93         names_strv = STRV_MAKE("org.test.test1");
94         policy_dump(&p);
95
96         ucred.uid = 0;
97
98         assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
99         assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
100         assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int1", "Member") == true);
101         assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int2", "Member") == true);
102
103         names_hash = hashmap_new(&string_hash_ops);
104         assert(names_hash != NULL);
105         assert_se(hashmap_put(names_hash, "org.test.test3", NULL) >= 0);
106         assert_se(policy_check_recv(&p, &ucred, names_hash, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int3", "Member111") == true);
107
108         policy_free(&p);
109
110         /* User and groups */
111         assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/hello.conf")) == 0);
112         policy_dump(&p);
113
114         ucred.uid = 0;
115         assert_se(policy_check_hello(&p, &ucred) == true);
116
117         ucred.uid = 1;
118         assert_se(policy_check_hello(&p, &ucred) == false);
119
120         ucred.uid = 0;
121         ucred.gid = 1;
122         assert_se(policy_check_hello(&p, &ucred) == false);
123
124         policy_free(&p);
125
126         return EXIT_SUCCESS;
127 }