From 094c1ee0ffeb5f13d879d9215ce98c871ed7c211 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 17 Oct 2019 16:50:10 +0200 Subject: [PATCH] tests: Add a test authenticator This is a trivial implementation of org.freedesktop.Flatpak.Authenticator that just reads the contents of the "required-token" file and returns that as the tokens for all refs. --- tests/Makefile.am.inc | 21 +- .../org.flatpak.Authenticator.test.service.in | 3 + tests/test-authenticator.c | 215 ++++++++++++++++++ 3 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 tests/org.flatpak.Authenticator.test.service.in create mode 100644 tests/test-authenticator.c diff --git a/tests/Makefile.am.inc b/tests/Makefile.am.inc index f15457c8..1355a714 100644 --- a/tests/Makefile.am.inc +++ b/tests/Makefile.am.inc @@ -79,6 +79,12 @@ tests_test_portal_impl_CFLAGS = $(AM_CFLAGS) $(BASE_CFLAGS) \ tests_test_portal_impl_LDADD = $(AM_LDADD) $(BASE_LIBS) tests_test_portal_impl_SOURCES = tests/test-portal-impl.c +tests_test_authenticator_CFLAGS = $(AM_CFLAGS) $(BASE_CFLAGS) \ + -DFLATPAK_COMPILATION \ + -DLOCALEDIR=\"$(localedir)\" +tests_test_authenticator_LDADD = $(AM_LDADD) $(BASE_LIBS) libflatpak-common.la libflatpak-common-base.la libglnx.la +tests_test_authenticator_SOURCES = tests/test-authenticator.c + tests/services/org.freedesktop.Flatpak.service: session-helper/org.freedesktop.Flatpak.service.in mkdir -p tests/services $(AM_V_GEN) $(SED) -e "s|\@libexecdir\@|$(abs_top_builddir)|" $< > $@ @@ -95,11 +101,15 @@ tests/services/org.freedesktop.impl.portal.desktop.test.service: tests/org.freed mkdir -p tests/services $(AM_V_GEN) $(SED) -e "s|\@libexecdir\@|$(abs_top_builddir)/tests|" $< > $@ +tests/services/org.flatpak.Authenticator.test.service: tests/org.flatpak.Authenticator.test.service.in + mkdir -p tests/services + $(AM_V_GEN) $(SED) -e "s|\@libexecdir\@|$(abs_top_builddir)/tests|" $< > $@ + tests/share/xdg-desktop-portal/portals/test.portal: tests/test.portal.in mkdir -p tests/share/xdg-desktop-portal/portals $(AM_V_GEN) install -m644 $< $@ -tests/libtest.sh: tests/services/org.freedesktop.Flatpak.service tests/services/org.freedesktop.Flatpak.SystemHelper.service tests/services/org.freedesktop.portal.Flatpak.service tests/share/xdg-desktop-portal/portals/test.portal tests/services/org.freedesktop.impl.portal.desktop.test.service +tests/libtest.sh: tests/services/org.freedesktop.Flatpak.service tests/services/org.freedesktop.Flatpak.SystemHelper.service tests/services/org.freedesktop.portal.Flatpak.service tests/share/xdg-desktop-portal/portals/test.portal tests/services/org.freedesktop.impl.portal.desktop.test.service tests/services/org.flatpak.Authenticator.test.service install-test-data-hook: if ENABLE_INSTALLED_TESTS @@ -108,6 +118,7 @@ if ENABLE_INSTALLED_TESTS $(AM_V_GEN) $(SED) -e "s|\@libexecdir\@|$(libexecdir)|" -e "s|\@extraargs\@| --poll-timeout=1|" $(top_srcdir)/portal/org.freedesktop.portal.Flatpak.service.in > $(DESTDIR)$(installed_testdir)/services/org.freedesktop.portal.Flatpak.service $(AM_V_GEN) $(SED) -e "s|\@libexecdir\@|$(libexecdir)|" -e "s|\@extraargs\@| --session --no-idle-exit|" $(top_srcdir)/system-helper/org.freedesktop.Flatpak.SystemHelper.service.in > $(DESTDIR)$(installed_testdir)/services/org.freedesktop.Flatpak.SystemHelper.service $(AM_V_GEN) $(SED) -e "s|\@libexecdir\@|$(installed_testdir)|" $(top_srcdir)/tests/org.freedesktop.impl.portal.desktop.test.service.in > $(DESTDIR)$(installed_testdir)/services/org.freedesktop.impl.portal.desktop.test.service + $(AM_V_GEN) $(SED) -e "s|\@libexecdir\@|$(installed_testdir)|" $(top_srcdir)/tests/tests/services/org.flatpak.Authenticator.test.service.in > $(DESTDIR)$(installed_testdir)/services/org.flatpak.Authenticator.test.service.service mkdir -p $(DESTDIR)$(installed_testdir)/share/xdg-desktop-portal/portals install -m644 $(top_srcdir)/tests/test.portal.in $(DESTDIR)$(installed_testdir)/share/xdg-desktop-portal/portals/test.portal endif @@ -135,8 +146,9 @@ dist_installed_test_data = \ tests/package_version.txt \ tests/session.conf.in \ tests/test.filter \ - tests/test.portal.in \ - tests/org.freedesktop.impl.portal.desktop.test.service.in \ + tests/test.portal.in \ + tests/org.freedesktop.impl.portal.desktop.test.service.in \ + tests/org.flatpak.Authenticator.test.service.in \ $(NULL) installed_test_keyringdir = $(installed_testdir)/test-keyring @@ -199,7 +211,7 @@ dist_test_scripts = ${TEST_MATRIX_DIST} dist_installed_test_extra_scripts += ${TEST_MATRIX_EXTRA_DIST} test_programs = testlibrary testcommon -test_extra_programs = tests/httpcache tests/test-update-portal tests/test-portal-impl +test_extra_programs = tests/httpcache tests/test-update-portal tests/test-portal-impl tests/test-authenticator @VALGRIND_CHECK_RULES@ VALGRIND_SUPPRESSIONS_FILES=tests/flatpak.supp tests/glib.supp @@ -209,6 +221,7 @@ DISTCLEANFILES += \ tests/services/org.freedesktop.Flatpak.SystemHelper.service \ tests/services/org.freedesktop.portal.Flatpak.service \ tests/services/org.freedesktop.impl.portal.desktop.test.service \ + tests/services/org.flatpak.Authenticator.test.service \ tests/share/xdg-desktop-portal/portals/test.portal \ tests/package_version.txt \ $(NULL) diff --git a/tests/org.flatpak.Authenticator.test.service.in b/tests/org.flatpak.Authenticator.test.service.in new file mode 100644 index 00000000..c29ce564 --- /dev/null +++ b/tests/org.flatpak.Authenticator.test.service.in @@ -0,0 +1,3 @@ +[D-BUS Service] +Name=org.flatpak.Authenticator.test +Exec=@libexecdir@/test-authenticator diff --git a/tests/test-authenticator.c b/tests/test-authenticator.c new file mode 100644 index 00000000..f6ed73bb --- /dev/null +++ b/tests/test-authenticator.c @@ -0,0 +1,215 @@ +/* + * Copyright © 2019 Red Hat, Inc + * + * This program is free software; you can redistribute it and/or + * modify it 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. + * + * This library 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 this library. If not, see . + * + * Authors: + * Alexander Larsson + */ + +#include "config.h" +#include + +#include "flatpak-auth-private.h" +#include "flatpak-dbus-generated.h" + +static GMainLoop *main_loop; +static guint name_owner_id = 0; +FlatpakAuthenticator *authenticator; + +static gboolean +handle_request_ref_tokens (FlatpakAuthenticator *authenticator, + GDBusMethodInvocation *invocation, + const gchar *arg_handle_token, + GVariant *arg_authenticator_option, + const gchar *const *arg_refs) +{ + g_autoptr(GError) error = NULL; + GVariantBuilder results; + GVariantBuilder tokens; + g_autofree char *required_token = NULL; + g_autofree char *required_token_file = NULL; + g_autofree char *request_path = flatpak_auth_create_request_path (g_dbus_method_invocation_get_sender (invocation), + arg_handle_token, + NULL); + g_autoptr(AutoFlatpakAuthenticatorRequest) request = NULL; + + if (request_path == NULL) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_INVALID_ARGS, + "Invalid token"); + return TRUE; + } + + request = flatpak_authenticator_request_skeleton_new (); + + if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (request), + g_dbus_method_invocation_get_connection (invocation), + request_path, + &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } + + flatpak_authenticator_complete_request_ref_tokens (authenticator, invocation, request_path); + + g_variant_builder_init (&results, G_VARIANT_TYPE ("a{sv}")); + + required_token_file = g_build_filename (g_get_user_runtime_dir (), "required-token", NULL); + if (!g_file_get_contents (required_token_file, &required_token, NULL, NULL)) + required_token = g_strdup ("default-token"); + + g_variant_builder_init (&tokens, G_VARIANT_TYPE ("a{sas}")); + g_variant_builder_add (&tokens, "{s^as}", required_token, arg_refs); + g_variant_builder_add (&results, "{sv}", "tokens", g_variant_builder_end (&tokens)); + + flatpak_auth_request_emit_response (request, + g_dbus_method_invocation_get_sender (invocation), + FLATPAK_AUTH_RESPONSE_OK, + g_variant_builder_end (&results)); + + return TRUE; +} + +static void +on_bus_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + GError *error = NULL; + + g_debug ("Bus acquired, creating skeleton"); + + g_dbus_connection_set_exit_on_close (connection, FALSE); + + authenticator = flatpak_authenticator_skeleton_new (); + flatpak_authenticator_set_version (authenticator, 0); + + g_signal_connect (authenticator, "handle-request-ref-tokens", G_CALLBACK (handle_request_ref_tokens), NULL); + + if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (authenticator), + connection, + FLATPAK_AUTHENTICATOR_OBJECT_PATH, + &error)) + { + g_warning ("error: %s", error->message); + g_error_free (error); + } +} + +static void +on_name_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + g_debug ("Name acquired"); +} + +static void +on_name_lost (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + g_debug ("Name lost"); +} + + +static void +message_handler (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + /* Make this look like normal console output */ + if (log_level & G_LOG_LEVEL_DEBUG) + g_printerr ("F: %s\n", message); + else + g_printerr ("%s: %s\n", g_get_prgname (), message); +} + +int +main (int argc, + char **argv) +{ + gboolean replace; + gboolean opt_verbose; + GOptionContext *context; + GDBusConnection *session_bus; + GBusNameOwnerFlags flags; + g_autoptr(GError) error = NULL; + const GOptionEntry options[] = { + { "replace", 'r', 0, G_OPTION_ARG_NONE, &replace, "Replace old daemon.", NULL }, + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose, "Enable debug output.", NULL }, + { NULL } + }; + + setlocale (LC_ALL, ""); + + g_setenv ("GIO_USE_VFS", "local", TRUE); + + g_set_prgname (argv[0]); + + g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, message_handler, NULL); + + context = g_option_context_new (""); + + replace = FALSE; + opt_verbose = FALSE; + + g_option_context_set_summary (context, "Flatpak portal"); + g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE); + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + g_printerr ("%s: %s", g_get_application_name (), error->message); + g_printerr ("\n"); + g_printerr ("Try \"%s --help\" for more information.", + g_get_prgname ()); + g_printerr ("\n"); + g_option_context_free (context); + return 1; + } + + if (opt_verbose) + g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, message_handler, NULL); + + g_debug ("Started test-authenticator"); + + session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); + if (session_bus == NULL) + { + g_printerr ("Can't find bus: %s\n", error->message); + return 1; + } + + flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT; + if (replace) + flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE; + + name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, + "org.flatpak.Authenticator.test", + flags, + on_bus_acquired, + on_name_acquired, + on_name_lost, + NULL, + NULL); + + main_loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (main_loop); + + return 0; +}