From 57983d20f17a0af3c8e28dae890cc71a32172816 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 20 Jun 2017 17:37:33 +0100 Subject: [PATCH] test/containers: New test So far it only exercises SupportedArguments. Signed-off-by: Simon McVittie --- Add a brief doc-comment for the test https://bugs.freedesktop.org/show_bug.cgi?id=101354 --- test/Makefile.am | 7 +++ test/containers.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 test/containers.c diff --git a/test/Makefile.am b/test/Makefile.am index f3b70dcd..ca4727b4 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -187,6 +187,7 @@ installable_helpers += \ $(NULL) installable_tests += \ + test-containers \ test-sd-activation \ $(NULL) @@ -290,6 +291,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..5e2387e5 --- /dev/null +++ b/test/containers.c @@ -0,0 +1,157 @@ +/* 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); +} + +/* + * Assert that Get(SupportedArguments) contains what we expect it to. + */ +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.13.3