From aaa2ace04d7e90bd94852f3eea3ea202b2320dd7 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 20 Jun 2017 17:37:33 +0100 Subject: [PATCH 14/49] test/containers: New test So far it only exercises SupportedArguments. Signed-off-by: Simon McVittie --- test/Makefile.am | 7 +++ test/containers.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 test/containers.c diff --git a/test/Makefile.am b/test/Makefile.am index 13640374..8d1d824f 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -186,6 +186,7 @@ installable_helpers += \ $(NULL) installable_tests += \ + test-containers \ test-sd-activation \ $(NULL) @@ -287,6 +288,12 @@ test_apparmor_activation_LDADD = \ $(NULL) endif +test_containers_SOURCES = containers.c +test_containers_LDADD = \ + libdbus-testutils.la \ + $(GLIB_LIBS) \ + $(NULL) + test_corrupt_SOURCES = corrupt.c test_corrupt_LDADD = \ libdbus-testutils.la \ diff --git a/test/containers.c b/test/containers.c new file mode 100644 index 00000000..4499092f --- /dev/null +++ b/test/containers.c @@ -0,0 +1,154 @@ +/* Integration tests for restricted sockets for containers + * + * Copyright © 2017 Collabora Ltd. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#include + +#include +#include +#include + +#if defined(DBUS_ENABLE_CONTAINERS) && defined(HAVE_GIO_UNIX) +#define HAVE_CONTAINERS_TEST +#include +#include +#endif + +#include "test-utils-glib.h" + +typedef struct { + gboolean skip; + gchar *bus_address; + GPid daemon_pid; + GError *error; + + GDBusProxy *proxy; + + GDBusConnection *unconfined_conn; +} Fixture; + +static void +setup (Fixture *f, + gconstpointer context) +{ + f->bus_address = test_get_dbus_daemon (NULL, TEST_USER_ME, NULL, + &f->daemon_pid); + + if (f->bus_address == NULL) + { + f->skip = TRUE; + return; + } + + f->unconfined_conn = g_dbus_connection_new_for_address_sync (f->bus_address, + (G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION | + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT), + NULL, NULL, &f->error); + g_assert_no_error (f->error); +} + +static void +test_get_supported_arguments (Fixture *f, + gconstpointer context) +{ + GVariant *v; +#ifdef DBUS_ENABLE_CONTAINERS + const gchar **args; + gsize len; +#endif + + if (f->skip) + return; + + f->proxy = g_dbus_proxy_new_sync (f->unconfined_conn, G_DBUS_PROXY_FLAGS_NONE, + NULL, DBUS_SERVICE_DBUS, + DBUS_PATH_DBUS, DBUS_INTERFACE_CONTAINERS1, + NULL, &f->error); + + /* This one is DBUS_ENABLE_CONTAINERS rather than HAVE_CONTAINERS_TEST + * because we can still test whether the interface appears or not, even + * if we were not able to detect gio-unix-2.0 */ +#ifdef DBUS_ENABLE_CONTAINERS + g_assert_no_error (f->error); + + v = g_dbus_proxy_get_cached_property (f->proxy, "SupportedArguments"); + g_assert_cmpstr (g_variant_get_type_string (v), ==, "as"); + args = g_variant_get_strv (v, &len); + + /* No arguments are defined yet */ + g_assert_cmpuint (len, ==, 0); + + g_free (args); + g_variant_unref (v); +#else /* !DBUS_ENABLE_CONTAINERS */ + g_assert_no_error (f->error); + v = g_dbus_proxy_get_cached_property (f->proxy, "SupportedArguments"); + g_assert_null (v); +#endif /* !DBUS_ENABLE_CONTAINERS */ +} + +static void +teardown (Fixture *f, + gconstpointer context G_GNUC_UNUSED) +{ + g_clear_object (&f->proxy); + + if (f->unconfined_conn != NULL) + { + GError *error = NULL; + + g_dbus_connection_close_sync (f->unconfined_conn, NULL, &error); + + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CLOSED)) + g_clear_error (&error); + else + g_assert_no_error (error); + } + + g_clear_object (&f->unconfined_conn); + + if (f->daemon_pid != 0) + { + test_kill_pid (f->daemon_pid); + g_spawn_close_pid (f->daemon_pid); + f->daemon_pid = 0; + } + + g_free (f->bus_address); + g_clear_error (&f->error); +} + +int +main (int argc, + char **argv) +{ + test_init (&argc, &argv); + + g_test_add ("/containers/get-supported-arguments", Fixture, NULL, + setup, test_get_supported_arguments, teardown); + + return g_test_run (); +} -- 2.11.0