]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/test-bus-zero-copy.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-zero-copy.c
CommitLineData
453a0c29
LP
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
453a0c29
LP
22#include <sys/mman.h>
23
453a0c29 24#include "sd-bus.h"
07630cea 25
2b5c5383 26#include "bus-dump.h"
07630cea
LP
27#include "bus-kernel.h"
28#include "bus-message.h"
29#include "log.h"
30#include "memfd-util.h"
31#include "string-util.h"
32#include "util.h"
453a0c29 33
66b26c5c
LP
34#define FIRST_ARRAY 17
35#define SECOND_ARRAY 33
36
5cbe7492
LP
37#define STRING_SIZE 123
38
453a0c29 39int main(int argc, char *argv[]) {
79ccff06 40 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
4d2b1e0a 41 const char *unique;
66b26c5c 42 uint8_t *p;
453a0c29
LP
43 sd_bus *a, *b;
44 int r, bus_ref;
45 sd_bus_message *m;
fac9c0d5 46 int f;
a392d361 47 uint64_t sz;
66b26c5c
LP
48 uint32_t u32;
49 size_t i, l;
5cbe7492 50 char *s;
7dcd79c2 51 _cleanup_close_ int sfd = -1;
453a0c29
LP
52
53 log_set_max_level(LOG_DEBUG);
54
79ccff06
ZJS
55 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
56
57 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
453a0c29
LP
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
4d2b1e0a
DH
84 r = sd_bus_get_unique_name(a, &unique);
85 assert_se(r >= 0);
86
87 r = sd_bus_message_new_method_call(b, &m, unique, "/a/path", "an.inter.face", "AMethod");
453a0c29
LP
88 assert_se(r >= 0);
89
5cbe7492 90 r = sd_bus_message_open_container(m, 'r', "aysay");
453a0c29
LP
91 assert_se(r >= 0);
92
66b26c5c 93 r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
453a0c29
LP
94 assert_se(r >= 0);
95
72d87e65
LP
96 p[0] = '<';
97 memset(p+1, 'L', FIRST_ARRAY-2);
98 p[FIRST_ARRAY-1] = '>';
453a0c29 99
4531a9bc
LP
100 f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
101 assert_se(f >= 0);
5cbe7492 102
72d87e65
LP
103 s[0] = '<';
104 for (i = 1; i < STRING_SIZE-2; i++)
5cbe7492 105 s[i] = '0' + (i % 10);
72d87e65 106 s[STRING_SIZE-2] = '>';
5cbe7492
LP
107 s[STRING_SIZE-1] = 0;
108 munmap(s, STRING_SIZE);
109
fac9c0d5 110 r = memfd_get_size(f, &sz);
5cbe7492
LP
111 assert_se(r >= 0);
112 assert_se(sz == STRING_SIZE);
113
7dcd79c2 114 r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
5cbe7492
LP
115 assert_se(r >= 0);
116
fac9c0d5 117 close(f);
5cbe7492 118
4531a9bc
LP
119 f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
120 assert_se(f >= 0);
453a0c29 121
72d87e65
LP
122 p[0] = '<';
123 memset(p+1, 'P', SECOND_ARRAY-2);
124 p[SECOND_ARRAY-1] = '>';
66b26c5c 125 munmap(p, SECOND_ARRAY);
453a0c29 126
fac9c0d5 127 r = memfd_get_size(f, &sz);
453a0c29 128 assert_se(r >= 0);
66b26c5c 129 assert_se(sz == SECOND_ARRAY);
453a0c29 130
7dcd79c2 131 r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
453a0c29
LP
132 assert_se(r >= 0);
133
fac9c0d5 134 close(f);
a392d361 135
453a0c29
LP
136 r = sd_bus_message_close_container(m);
137 assert_se(r >= 0);
138
a392d361
LP
139 r = sd_bus_message_append(m, "u", 4711);
140 assert_se(r >= 0);
141
7dcd79c2
LP
142 assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
143 memcpy(p, "abcd\0", 6);
144 munmap(p, 6);
145 assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);
146
6e41a3e5 147 r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
453a0c29
LP
148 assert_se(r >= 0);
149
d55192ad 150 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
453a0c29
LP
151
152 r = sd_bus_send(b, m, NULL);
153 assert_se(r >= 0);
154
155 sd_bus_message_unref(m);
156
1307c3ff
LP
157 r = sd_bus_process(a, &m);
158 assert_se(r > 0);
159
d55192ad 160 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
66b26c5c
LP
161 sd_bus_message_rewind(m, true);
162
5cbe7492 163 r = sd_bus_message_enter_container(m, 'r', "aysay");
66b26c5c
LP
164 assert_se(r > 0);
165
166 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
167 assert_se(r > 0);
168 assert_se(l == FIRST_ARRAY);
169
72d87e65
LP
170 assert_se(p[0] == '<');
171 for (i = 1; i < l-1; i++)
66b26c5c 172 assert_se(p[i] == 'L');
72d87e65 173 assert_se(p[l-1] == '>');
66b26c5c 174
5cbe7492
LP
175 r = sd_bus_message_read(m, "s", &s);
176 assert_se(r > 0);
177
72d87e65
LP
178 assert_se(s[0] == '<');
179 for (i = 1; i < STRING_SIZE-2; i++)
5cbe7492 180 assert_se(s[i] == (char) ('0' + (i % 10)));
72d87e65 181 assert_se(s[STRING_SIZE-2] == '>');
5cbe7492
LP
182 assert_se(s[STRING_SIZE-1] == 0);
183
66b26c5c
LP
184 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
185 assert_se(r > 0);
186 assert_se(l == SECOND_ARRAY);
187
72d87e65
LP
188 assert_se(p[0] == '<');
189 for (i = 1; i < l-1; i++)
66b26c5c 190 assert_se(p[i] == 'P');
72d87e65 191 assert_se(p[l-1] == '>');
66b26c5c
LP
192
193 r = sd_bus_message_exit_container(m);
194 assert_se(r > 0);
195
196 r = sd_bus_message_read(m, "u", &u32);
197 assert_se(r > 0);
198 assert_se(u32 == 4711);
199
7dcd79c2
LP
200 r = sd_bus_message_read(m, "s", &s);
201 assert_se(r > 0);
202 assert_se(streq_ptr(s, "bcd"));
203
1307c3ff
LP
204 sd_bus_message_unref(m);
205
453a0c29
LP
206 sd_bus_unref(a);
207 sd_bus_unref(b);
453a0c29
LP
208
209 return 0;
210}