test-bus-zero-copy.c revision f2769777c8e8ad32b0b19cd540d2f742bda4457f
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/***
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering This file is part of systemd.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Copyright 2013 Lennart Poettering
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is free software; you can redistribute it and/or modify it
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering under the terms of the GNU Lesser General Public License as published by
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering (at your option) any later version.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is distributed in the hope that it will be useful, but
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Lesser General Public License for more details.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering You should have received a copy of the GNU Lesser General Public License
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering***/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include <fcntl.h>
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include <sys/mman.h>
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "util.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "log.h"
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "sd-bus.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "sd-memfd.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "bus-message.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen#include "bus-error.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen#include "bus-kernel.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen#include "bus-dump.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#define FIRST_ARRAY 17
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#define SECOND_ARRAY 33
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#define STRING_SIZE 123
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringint main(int argc, char *argv[]) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _cleanup_free_ char *bus_name = NULL, *address = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint8_t *p;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering sd_bus *a, *b;
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace int r, bus_ref;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering sd_bus_message *m;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering sd_memfd *f;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint64_t sz;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t u32;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering size_t i, l;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char *s;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer log_set_max_level(LOG_DEBUG);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering bus_ref = bus_kernel_create_bus("deine-mutter", false, &bus_name);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (bus_ref == -ENOENT)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return EXIT_TEST_SKIP;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(bus_ref >= 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering address = strappend("kernel:path=", bus_name);
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering assert_se(address);
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering r = sd_bus_new(&a);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(r >= 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_new(&b);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(r >= 0);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers r = sd_bus_set_address(a, address);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert_se(r >= 0);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = sd_bus_set_address(b, address);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert_se(r >= 0);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = sd_bus_start(a);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert_se(r >= 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_start(b);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers assert_se(r >= 0);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = sd_bus_message_new_method_call(b, ":1.1", "/a/path", "an.inter.face", "AMethod", &m);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers assert_se(r >= 0);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = sd_bus_message_open_container(m, 'r', "aysay");
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers assert_se(r >= 0);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers assert_se(r >= 0);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers p[0] = '<';
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers memset(p+1, 'L', FIRST_ARRAY-2);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers p[FIRST_ARRAY-1] = '>';
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = sd_memfd_new_and_map(&f, STRING_SIZE, (void**) &s);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert_se(r >= 0);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering s[0] = '<';
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers for (i = 1; i < STRING_SIZE-2; i++)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering s[i] = '0' + (i % 10);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering s[STRING_SIZE-2] = '>';
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden s[STRING_SIZE-1] = 0;
480a61ae742378a6a9a0eb84cf29c6c1e4ea22ffLennart Poettering munmap(s, STRING_SIZE);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
480a61ae742378a6a9a0eb84cf29c6c1e4ea22ffLennart Poettering r = sd_memfd_get_size(f, &sz);
480a61ae742378a6a9a0eb84cf29c6c1e4ea22ffLennart Poettering assert_se(r >= 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(sz == STRING_SIZE);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering r = sd_bus_message_append_string_memfd(m, f);
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering assert_se(r >= 0);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers sd_memfd_free(f);
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = sd_memfd_new_and_map(&f, SECOND_ARRAY, (void**) &p);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers assert_se(r >= 0);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden p[0] = '<';
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden memset(p+1, 'P', SECOND_ARRAY-2);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden p[SECOND_ARRAY-1] = '>';
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden munmap(p, SECOND_ARRAY);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden r = sd_memfd_get_size(f, &sz);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden assert_se(r >= 0);
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John assert_se(sz == SECOND_ARRAY);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden r = sd_bus_message_append_array_memfd(m, 'y', f);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden assert_se(r >= 0);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden sd_memfd_free(f);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_message_close_container(m);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden assert_se(r >= 0);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden r = sd_bus_message_append(m, "u", 4711);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden assert_se(r >= 0);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden assert_se(r >= 0);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering bus_message_dump(m, stdout, true);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = sd_bus_send(b, m, NULL);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(r >= 0);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers sd_bus_message_unref(m);
7f35b7bc4a241e9aa3b1512fd345cbf5b2e5a782Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers r = sd_bus_process(a, &m);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers assert_se(r > 0);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden bus_message_dump(m, stdout, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering sd_bus_message_rewind(m, true);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen r = sd_bus_message_enter_container(m, 'r', "aysay");
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(r > 0);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(r > 0);
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John assert_se(l == FIRST_ARRAY);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(p[0] == '<');
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering for (i = 1; i < l-1; i++)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(p[i] == 'L');
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert_se(p[l-1] == '>');
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = sd_bus_message_read(m, "s", &s);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(r > 0);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(s[0] == '<');
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen for (i = 1; i < STRING_SIZE-2; i++)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(s[i] == (char) ('0' + (i % 10)));
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(s[STRING_SIZE-2] == '>');
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(s[STRING_SIZE-1] == 0);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(r > 0);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(l == SECOND_ARRAY);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(p[0] == '<');
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen for (i = 1; i < l-1; i++)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(p[i] == 'P');
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(p[l-1] == '>');
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen r = sd_bus_message_exit_container(m);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(r > 0);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen r = sd_bus_message_read(m, "u", &u32);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(r > 0);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen assert_se(u32 == 4711);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen sd_bus_message_unref(m);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen sd_bus_unref(a);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen sd_bus_unref(b);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen return 0;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen}
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen