1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
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.
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.
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/>.
26 #include "memfd-util.h"
29 #include "bus-message.h"
30 #include "bus-kernel.h"
33 #define FIRST_ARRAY 17
34 #define SECOND_ARRAY 33
36 #define STRING_SIZE 123
38 int main(int argc, char *argv[]) {
39 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
50 _cleanup_close_ int sfd = -1;
52 log_set_max_level(LOG_DEBUG);
54 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
56 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
57 if (bus_ref == -ENOENT)
58 return EXIT_TEST_SKIP;
60 assert_se(bus_ref >= 0);
62 address = strappend("kernel:path=", bus_name);
71 r = sd_bus_set_address(a, address);
74 r = sd_bus_set_address(b, address);
83 r = sd_bus_get_unique_name(a, &unique);
86 r = sd_bus_message_new_method_call(b, &m, unique, "/a/path", "an.inter.face", "AMethod");
89 r = sd_bus_message_open_container(m, 'r', "aysay");
92 r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
96 memset(p+1, 'L', FIRST_ARRAY-2);
97 p[FIRST_ARRAY-1] = '>';
99 f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
103 for (i = 1; i < STRING_SIZE-2; i++)
104 s[i] = '0' + (i % 10);
105 s[STRING_SIZE-2] = '>';
106 s[STRING_SIZE-1] = 0;
107 munmap(s, STRING_SIZE);
109 r = memfd_get_size(f, &sz);
111 assert_se(sz == STRING_SIZE);
113 r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
118 f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
122 memset(p+1, 'P', SECOND_ARRAY-2);
123 p[SECOND_ARRAY-1] = '>';
124 munmap(p, SECOND_ARRAY);
126 r = memfd_get_size(f, &sz);
128 assert_se(sz == SECOND_ARRAY);
130 r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
135 r = sd_bus_message_close_container(m);
138 r = sd_bus_message_append(m, "u", 4711);
141 assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
142 memcpy(p, "abcd\0", 6);
144 assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);
146 r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
149 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
151 r = sd_bus_send(b, m, NULL);
154 sd_bus_message_unref(m);
156 r = sd_bus_process(a, &m);
159 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
160 sd_bus_message_rewind(m, true);
162 r = sd_bus_message_enter_container(m, 'r', "aysay");
165 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
167 assert_se(l == FIRST_ARRAY);
169 assert_se(p[0] == '<');
170 for (i = 1; i < l-1; i++)
171 assert_se(p[i] == 'L');
172 assert_se(p[l-1] == '>');
174 r = sd_bus_message_read(m, "s", &s);
177 assert_se(s[0] == '<');
178 for (i = 1; i < STRING_SIZE-2; i++)
179 assert_se(s[i] == (char) ('0' + (i % 10)));
180 assert_se(s[STRING_SIZE-2] == '>');
181 assert_se(s[STRING_SIZE-1] == 0);
183 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
185 assert_se(l == SECOND_ARRAY);
187 assert_se(p[0] == '<');
188 for (i = 1; i < l-1; i++)
189 assert_se(p[i] == 'P');
190 assert_se(p[l-1] == '>');
192 r = sd_bus_message_exit_container(m);
195 r = sd_bus_message_read(m, "u", &u32);
197 assert_se(u32 == 4711);
199 r = sd_bus_message_read(m, "s", &s);
201 assert_se(streq_ptr(s, "bcd"));
203 sd_bus_message_unref(m);