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/>.
27 #include "memfd-util.h"
30 #include "bus-message.h"
31 #include "bus-error.h"
32 #include "bus-kernel.h"
35 #define FIRST_ARRAY 17
36 #define SECOND_ARRAY 33
38 #define STRING_SIZE 123
40 int main(int argc, char *argv[]) {
41 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
51 _cleanup_close_ int sfd = -1;
53 log_set_max_level(LOG_DEBUG);
55 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
57 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
58 if (bus_ref == -ENOENT)
59 return EXIT_TEST_SKIP;
61 assert_se(bus_ref >= 0);
63 address = strappend("kernel:path=", bus_name);
72 r = sd_bus_set_address(a, address);
75 r = sd_bus_set_address(b, address);
84 r = sd_bus_message_new_method_call(b, &m, ":1.1", "/a/path", "an.inter.face", "AMethod");
87 r = sd_bus_message_open_container(m, 'r', "aysay");
90 r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
94 memset(p+1, 'L', FIRST_ARRAY-2);
95 p[FIRST_ARRAY-1] = '>';
97 f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
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);
107 r = memfd_get_size(f, &sz);
109 assert_se(sz == STRING_SIZE);
111 r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
116 f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
120 memset(p+1, 'P', SECOND_ARRAY-2);
121 p[SECOND_ARRAY-1] = '>';
122 munmap(p, SECOND_ARRAY);
124 r = memfd_get_size(f, &sz);
126 assert_se(sz == SECOND_ARRAY);
128 r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
133 r = sd_bus_message_close_container(m);
136 r = sd_bus_message_append(m, "u", 4711);
139 assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
140 memcpy(p, "abcd\0", 6);
142 assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);
144 r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
147 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
149 r = sd_bus_send(b, m, NULL);
152 sd_bus_message_unref(m);
154 r = sd_bus_process(a, &m);
157 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
158 sd_bus_message_rewind(m, true);
160 r = sd_bus_message_enter_container(m, 'r', "aysay");
163 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
165 assert_se(l == FIRST_ARRAY);
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] == '>');
172 r = sd_bus_message_read(m, "s", &s);
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);
181 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
183 assert_se(l == SECOND_ARRAY);
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] == '>');
190 r = sd_bus_message_exit_container(m);
193 r = sd_bus_message_read(m, "u", &u32);
195 assert_se(u32 == 4711);
197 r = sd_bus_message_read(m, "s", &s);
199 assert_se(streq_ptr(s, "bcd"));
201 sd_bus_message_unref(m);