chiark / gitweb /
bus: add sd_bus_emit_object_{added/removed}()
[elogind.git] / src / libsystemd / sd-bus / test-bus-zero-copy.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 <fcntl.h>
23 #include <sys/mman.h>
24
25 #include "util.h"
26 #include "log.h"
27 #include "memfd-util.h"
28
29 #include "sd-bus.h"
30 #include "bus-message.h"
31 #include "bus-error.h"
32 #include "bus-kernel.h"
33 #include "bus-dump.h"
34
35 #define FIRST_ARRAY 17
36 #define SECOND_ARRAY 33
37
38 #define STRING_SIZE 123
39
40 int main(int argc, char *argv[]) {
41         _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
42         uint8_t *p;
43         sd_bus *a, *b;
44         int r, bus_ref;
45         sd_bus_message *m;
46         int f;
47         uint64_t sz;
48         uint32_t u32;
49         size_t i, l;
50         char *s;
51         _cleanup_close_ int sfd = -1;
52
53         log_set_max_level(LOG_DEBUG);
54
55         assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
56
57         bus_ref = bus_kernel_create_bus(name, false, &bus_name);
58         if (bus_ref == -ENOENT)
59                 return EXIT_TEST_SKIP;
60
61         assert_se(bus_ref >= 0);
62
63         address = strappend("kernel:path=", bus_name);
64         assert_se(address);
65
66         r = sd_bus_new(&a);
67         assert_se(r >= 0);
68
69         r = sd_bus_new(&b);
70         assert_se(r >= 0);
71
72         r = sd_bus_set_address(a, address);
73         assert_se(r >= 0);
74
75         r = sd_bus_set_address(b, address);
76         assert_se(r >= 0);
77
78         r = sd_bus_start(a);
79         assert_se(r >= 0);
80
81         r = sd_bus_start(b);
82         assert_se(r >= 0);
83
84         r = sd_bus_message_new_method_call(b, &m, ":1.1", "/a/path", "an.inter.face", "AMethod");
85         assert_se(r >= 0);
86
87         r = sd_bus_message_open_container(m, 'r', "aysay");
88         assert_se(r >= 0);
89
90         r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
91         assert_se(r >= 0);
92
93         p[0] = '<';
94         memset(p+1, 'L', FIRST_ARRAY-2);
95         p[FIRST_ARRAY-1] = '>';
96
97         f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
98         assert_se(f >= 0);
99
100         s[0] = '<';
101         for (i = 1; i < STRING_SIZE-2; i++)
102                 s[i] = '0' + (i % 10);
103         s[STRING_SIZE-2] = '>';
104         s[STRING_SIZE-1] = 0;
105         munmap(s, STRING_SIZE);
106
107         r = memfd_get_size(f, &sz);
108         assert_se(r >= 0);
109         assert_se(sz == STRING_SIZE);
110
111         r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
112         assert_se(r >= 0);
113
114         close(f);
115
116         f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
117         assert_se(f >= 0);
118
119         p[0] = '<';
120         memset(p+1, 'P', SECOND_ARRAY-2);
121         p[SECOND_ARRAY-1] = '>';
122         munmap(p, SECOND_ARRAY);
123
124         r = memfd_get_size(f, &sz);
125         assert_se(r >= 0);
126         assert_se(sz == SECOND_ARRAY);
127
128         r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
129         assert_se(r >= 0);
130
131         close(f);
132
133         r = sd_bus_message_close_container(m);
134         assert_se(r >= 0);
135
136         r = sd_bus_message_append(m, "u", 4711);
137         assert_se(r >= 0);
138
139         assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
140         memcpy(p, "abcd\0", 6);
141         munmap(p, 6);
142         assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);
143
144         r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
145         assert_se(r >= 0);
146
147         bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
148
149         r = sd_bus_send(b, m, NULL);
150         assert_se(r >= 0);
151
152         sd_bus_message_unref(m);
153
154         r = sd_bus_process(a, &m);
155         assert_se(r > 0);
156
157         bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
158         sd_bus_message_rewind(m, true);
159
160         r = sd_bus_message_enter_container(m, 'r', "aysay");
161         assert_se(r > 0);
162
163         r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
164         assert_se(r > 0);
165         assert_se(l == FIRST_ARRAY);
166
167         assert_se(p[0] == '<');
168         for (i = 1; i < l-1; i++)
169                 assert_se(p[i] == 'L');
170         assert_se(p[l-1] == '>');
171
172         r = sd_bus_message_read(m, "s", &s);
173         assert_se(r > 0);
174
175         assert_se(s[0] == '<');
176         for (i = 1; i < STRING_SIZE-2; i++)
177                 assert_se(s[i] == (char) ('0' + (i % 10)));
178         assert_se(s[STRING_SIZE-2] == '>');
179         assert_se(s[STRING_SIZE-1] == 0);
180
181         r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
182         assert_se(r > 0);
183         assert_se(l == SECOND_ARRAY);
184
185         assert_se(p[0] == '<');
186         for (i = 1; i < l-1; i++)
187                 assert_se(p[i] == 'P');
188         assert_se(p[l-1] == '>');
189
190         r = sd_bus_message_exit_container(m);
191         assert_se(r > 0);
192
193         r = sd_bus_message_read(m, "u", &u32);
194         assert_se(r > 0);
195         assert_se(u32 == 4711);
196
197         r = sd_bus_message_read(m, "s", &s);
198         assert_se(r > 0);
199         assert_se(streq_ptr(s, "bcd"));
200
201         sd_bus_message_unref(m);
202
203         sd_bus_unref(a);
204         sd_bus_unref(b);
205
206         return 0;
207 }