chiark / gitweb /
a594ce3157f529e9690a1a7f1fe7dc4fc0238fce
[elogind.git] / src / libsystemd-bus / test-bus-server.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
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 <assert.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "log.h"
29 #include "util.h"
30 #include "macro.h"
31
32 #include "sd-bus.h"
33 #include "bus-internal.h"
34 #include "bus-message.h"
35
36 struct context {
37         int fds[2];
38
39         bool client_negotiate_unix_fds;
40         bool server_negotiate_unix_fds;
41
42         bool client_anonymous_auth;
43         bool server_anonymous_auth;
44 };
45
46 static void *server(void *p) {
47         struct context *c = p;
48         sd_bus *bus = NULL;
49         sd_id128_t id;
50         bool quit = false;
51         int r;
52
53         assert_se(sd_id128_randomize(&id) >= 0);
54
55         assert_se(sd_bus_new(&bus) >= 0);
56         assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
57         assert_se(sd_bus_set_server(bus, 1, id) >= 0);
58         assert_se(sd_bus_set_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
59         assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
60         assert_se(sd_bus_start(bus) >= 0);
61
62         while (!quit) {
63                 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
64
65                 r = sd_bus_process(bus, &m);
66                 if (r < 0) {
67                         log_error("Failed to process requests: %s", strerror(-r));
68                         goto fail;
69                 }
70
71                 if (r == 0) {
72                         r = sd_bus_wait(bus, (uint64_t) -1);
73                         if (r < 0) {
74                                 log_error("Failed to wait: %s", strerror(-r));
75                                 goto fail;
76                         }
77
78                         continue;
79                 }
80
81                 if (!m)
82                         continue;
83
84                 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
85
86                 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
87
88                         assert_se((sd_bus_can_send(bus, 'h') >= 1) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
89
90                         r = sd_bus_message_new_method_return(bus, m, &reply);
91                         if (r < 0) {
92                                 log_error("Failed to allocate return: %s", strerror(-r));
93                                 goto fail;
94                         }
95
96                         quit = true;
97
98                 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
99                         const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownMethod", "Unknown method.");
100
101                         r = sd_bus_message_new_method_error(bus, m, &e, &reply);
102                         if (r < 0) {
103                                 log_error("Failed to allocate return: %s", strerror(-r));
104                                 goto fail;
105                         }
106                 }
107
108                 if (reply) {
109                         r = sd_bus_send(bus, reply, NULL);
110                         if (r < 0) {
111                                 log_error("Failed to send reply: %s", strerror(-r));
112                                 goto fail;
113                         }
114                 }
115         }
116
117         r = 0;
118
119 fail:
120         if (bus) {
121                 sd_bus_flush(bus);
122                 sd_bus_unref(bus);
123         }
124
125         return INT_TO_PTR(r);
126 }
127
128 static int client(struct context *c) {
129         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
130         _cleanup_bus_unref_ sd_bus *bus = NULL;
131         sd_bus_error error = SD_BUS_ERROR_INIT;
132         int r;
133
134         assert_se(sd_bus_new(&bus) >= 0);
135         assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
136         assert_se(sd_bus_set_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
137         assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
138         assert_se(sd_bus_start(bus) >= 0);
139
140         r = sd_bus_message_new_method_call(
141                         bus,
142                         "org.freedesktop.systemd.test",
143                         "/",
144                         "org.freedesktop.systemd.test",
145                         "Exit",
146                         &m);
147         if (r < 0) {
148                 log_error("Failed to allocate method call: %s", strerror(-r));
149                 return r;
150         }
151
152         r = sd_bus_send_with_reply_and_block(bus, m, 0, &error, &reply);
153         if (r < 0) {
154                 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
155                 return r;
156         }
157
158         return 0;
159 }
160
161 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
162                     bool client_anonymous_auth, bool server_anonymous_auth) {
163
164         struct context c;
165         pthread_t s;
166         void *p;
167         int r, q;
168
169         zero(c);
170
171         assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
172
173         c.client_negotiate_unix_fds = client_negotiate_unix_fds;
174         c.server_negotiate_unix_fds = server_negotiate_unix_fds;
175         c.client_anonymous_auth = client_anonymous_auth;
176         c.server_anonymous_auth = server_anonymous_auth;
177
178         r = pthread_create(&s, NULL, server, &c);
179         if (r != 0)
180                 return -r;
181
182         r = client(&c);
183
184         q = pthread_join(s, &p);
185         if (q != 0)
186                 return -q;
187
188         if (r < 0)
189                 return r;
190
191         if (PTR_TO_INT(p) < 0)
192                 return PTR_TO_INT(p);
193
194         return 0;
195 }
196
197 int main(int argc, char *argv[]) {
198         int r;
199
200         r = test_one(true, true, false, false);
201         assert_se(r >= 0);
202
203         r = test_one(true, false, false, false);
204         assert_se(r >= 0);
205
206         r = test_one(false, true, false, false);
207         assert_se(r >= 0);
208
209         r = test_one(false, false, false, false);
210         assert_se(r >= 0);
211
212         r = test_one(true, true, true, true);
213         assert_se(r >= 0);
214
215         r = test_one(true, true, false, true);
216         assert_se(r >= 0);
217
218         r = test_one(true, true, true, false);
219         assert_se(r == -EPERM);
220
221         return EXIT_SUCCESS;
222 }