test-bus-zero-copy.c revision 151b9b9662a90455262ce575a8a8ae74bf4ff336
/*-*- 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 "sd-bus.h"
#include "sd-memfd.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;
sd_memfd *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(r >= 0);
s[0] = '<';
s[i] = '0' + (i % 10);
s[STRING_SIZE-1] = 0;
munmap(s, STRING_SIZE);
r = sd_memfd_get_size(f, &sz);
assert_se(r >= 0);
r = sd_bus_message_append_string_memfd(m, f);
assert_se(r >= 0);
sd_memfd_free(f);
assert_se(r >= 0);
p[0] = '<';
munmap(p, SECOND_ARRAY);
r = sd_memfd_get_size(f, &sz);
assert_se(r >= 0);
r = sd_bus_message_append_array_memfd(m, 'y', f);
assert_se(r >= 0);
sd_memfd_free(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;
}