test-bus-zero-copy.c revision 4531a9bc206c0c22e29b56ac4a7528afba2e9c83
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <fcntl.h>
#include "util.h"
#include "log.h"
#include "memfd.h"
#include "sd-bus.h"
#include "bus-message.h"
#include "bus-error.h"
#include "bus-kernel.h"
#include "bus-dump.h"
#define FIRST_ARRAY 17
#define SECOND_ARRAY 33
#define STRING_SIZE 123
uint8_t *p;
sd_bus *a, *b;
int r, bus_ref;
sd_bus_message *m;
int f;
size_t i, l;
char *s;
return EXIT_TEST_SKIP;
r = sd_bus_new(&a);
assert_se(r >= 0);
r = sd_bus_new(&b);
assert_se(r >= 0);
r = sd_bus_set_address(a, address);
assert_se(r >= 0);
r = sd_bus_set_address(b, address);
assert_se(r >= 0);
r = sd_bus_start(a);
assert_se(r >= 0);
r = sd_bus_start(b);
assert_se(r >= 0);
assert_se(r >= 0);
assert_se(r >= 0);
assert_se(r >= 0);
p[0] = '<';
assert_se(f >= 0);
s[0] = '<';
s[i] = '0' + (i % 10);
s[STRING_SIZE-1] = 0;
munmap(s, STRING_SIZE);
r = memfd_get_size(f, &sz);
assert_se(r >= 0);
r = sd_bus_message_append_string_memfd(m, f);
assert_se(r >= 0);
close(f);
assert_se(f >= 0);
p[0] = '<';
munmap(p, SECOND_ARRAY);
r = memfd_get_size(f, &sz);
assert_se(r >= 0);
r = sd_bus_message_append_array_memfd(m, 'y', f);
assert_se(r >= 0);
close(f);
r = sd_bus_message_close_container(m);
assert_se(r >= 0);
assert_se(r >= 0);
assert_se(r >= 0);
bus_message_dump(m, stdout, true);
r = sd_bus_send(b, m, NULL);
assert_se(r >= 0);
r = sd_bus_process(a, &m);
assert_se(r > 0);
bus_message_dump(m, stdout, true);
sd_bus_message_rewind(m, true);
assert_se(r > 0);
r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
assert_se(r > 0);
assert_se(l == FIRST_ARRAY);
assert_se(p[0] == '<');
for (i = 1; i < l-1; i++)
assert_se(p[i] == 'L');
r = sd_bus_message_read(m, "s", &s);
assert_se(r > 0);
assert_se(s[0] == '<');
r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
assert_se(r > 0);
assert_se(l == SECOND_ARRAY);
assert_se(p[0] == '<');
for (i = 1; i < l-1; i++)
assert_se(p[i] == 'P');
r = sd_bus_message_exit_container(m);
assert_se(r > 0);
assert_se(r > 0);
sd_bus_unref(a);
sd_bus_unref(b);
return 0;
}