From 1a9be3a1c8415f487236fb964b825355cead589c Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 15 Mar 2023 13:23:44 +0000 Subject: [PATCH] tests: Add a test for D-Bus activating an unknown app with a context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should catch regressions in the critical warning fixed in the previous commit. The launch has to have several conditions: - Session bus is running (to avoid the launch happening via the spawn codepath) - Use a non-existent D-Bus name (to trigger a launch error) - Use a launch context (to hit the critical warning code path) - Not have a startup ID specified in the platform data — this implies having an empty launch context - Use an async launch, as that provides an error handling path Signed-off-by: Philip Withnall --- gio/tests/desktop-app-info.c | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/gio/tests/desktop-app-info.c b/gio/tests/desktop-app-info.c index 1df5fb693..594ec945d 100644 --- a/gio/tests/desktop-app-info.c +++ b/gio/tests/desktop-app-info.c @@ -1807,6 +1807,56 @@ test_launch_fail_absolute_path (void) g_clear_object (&app_info); } +static void +async_result_cb (GObject *source_object, + GAsyncResult *result, + gpointer user_data) +{ + GAsyncResult **result_out = user_data; + + g_assert (*result_out == NULL); + *result_out = g_object_ref (result); + g_main_context_wakeup (g_main_context_get_thread_default ()); +} + +static void +test_launch_fail_dbus (void) +{ + GTestDBus *bus = NULL; + GDesktopAppInfo *app_info = NULL; + GAppLaunchContext *context = NULL; + GAsyncResult *result = NULL; + GError *error = NULL; + + /* Set up a test session bus to ensure that launching the app happens using + * D-Bus rather than spawning. */ + bus = g_test_dbus_new (G_TEST_DBUS_NONE); + g_test_dbus_up (bus); + + app_info = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "org.gtk.test.dbusappinfo.desktop", NULL)); + g_assert_nonnull (app_info); + + g_assert_true (g_desktop_app_info_has_key (app_info, "DBusActivatable")); + + context = g_app_launch_context_new (); + + g_app_info_launch_uris_async (G_APP_INFO (app_info), NULL, context, NULL, async_result_cb, &result); + + while (result == NULL) + g_main_context_iteration (NULL, TRUE); + + g_assert_false (g_app_info_launch_uris_finish (G_APP_INFO (app_info), result, &error)); + g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN); + + g_test_dbus_down (bus); + g_clear_object (&bus); + + g_clear_error (&error); + g_clear_object (&result); + g_clear_object (&context); + g_clear_object (&app_info); +} + int main (int argc, char *argv[]) @@ -1849,6 +1899,7 @@ main (int argc, g_test_add_func ("/desktop-app-info/launch/fail", test_launch_fail); g_test_add_func ("/desktop-app-info/launch/fail-absolute-path", test_launch_fail_absolute_path); g_test_add_func ("/desktop-app-info/launch/fail-startup-notify", test_launch_startup_notify_fail); + g_test_add_func ("/desktop-app-info/launch/fail-dbus", test_launch_fail_dbus); g_test_add_func ("/desktop-app-info/launch-as-manager", test_launch_as_manager); g_test_add_func ("/desktop-app-info/launch-as-manager/fail", test_launch_as_manager_fail); g_test_add_func ("/desktop-app-info/launch-default-uri-handler", test_default_uri_handler);