Imported Upstream version 2.67.0
[platform/upstream/glib.git] / gio / tests / gdbus-exit-on-close.c
index 14d3a8c..4241fc7 100644 (file)
@@ -5,7 +5,7 @@
  * This library 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 of the License, or (at your option) any later version.
+ * 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
@@ -13,9 +13,7 @@
  * 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, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: David Zeuthen <davidz@redhat.com>
  */
@@ -59,11 +57,9 @@ quit_later_cb (gpointer data G_GNUC_UNUSED)
 {
   g_main_loop_quit (loop);
 
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
-#define VANISHED_PATTERN "*Remote peer vanished with error: Underlying GIOStream returned 0 bytes on an async read (g-io-error-quark, 0). Exiting.*"
-
 static void
 closed_cb (GDBusConnection  *c G_GNUC_UNUSED,
            gboolean          remote_peer_vanished,
@@ -109,90 +105,90 @@ close_async_cb (GObject *source G_GNUC_UNUSED,
 }
 
 static void
-test_exit_on_close (gconstpointer test_data)
+test_exit_on_close_subprocess (gconstpointer test_data)
 {
   const TestData *td = test_data;
-  GTestTrapFlags silence;
+  GDBusConnection *c;
 
-  /* all the tests rely on a shared main loop */
   loop = g_main_loop_new (NULL, FALSE);
 
-  /* all the tests use a session bus with a well-known address that we can bring up and down
-   * using session_bus_up() and session_bus_down().
-   */
-  g_unsetenv ("DISPLAY");
-  g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
+  session_bus_up ();
+  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
 
-  if (g_test_verbose ())
-    silence = 0;
-  else
-    silence = G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR;
+  g_assert (c != NULL);
 
-  if (g_test_trap_fork (0, silence))
-    {
-      GDBusConnection *c;
-
-      session_bus_up ();
-      c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
-
-      g_assert (c != NULL);
-
-      /* the default is meant to be TRUE */
-      if (td->exit_on_close != IMPLICITLY_TRUE)
-        g_dbus_connection_set_exit_on_close (c, td->exit_on_close);
-
-      g_assert_cmpint (g_dbus_connection_get_exit_on_close (c), ==,
-                       (td->exit_on_close != EXPLICITLY_FALSE));
-      g_assert (!g_dbus_connection_is_closed (c));
-
-      g_timeout_add (50, quit_later_cb, NULL);
-      g_main_loop_run (loop);
-
-      g_signal_connect (c, "closed", G_CALLBACK (closed_cb), (gpointer) td);
-
-      if (td->who_closes == LOCAL)
-        {
-          GVariant *v;
-          GError *error = NULL;
-
-          v = g_dbus_connection_call_sync (c, "org.freedesktop.DBus",
-                                           "/org/freedesktop/DBus",
-                                           "org.freedesktop.DBus",
-                                           "ListNames",
-                                           NULL,
-                                           G_VARIANT_TYPE ("(as)"),
-                                           G_DBUS_CALL_FLAGS_NONE,
-                                           -1,
-                                           NULL,
-                                           &error);
-          g_assert_no_error (error);
-          g_assert (v != NULL);
-          g_variant_unref (v);
-
-          g_dbus_connection_close (c, NULL, close_async_cb, NULL);
-        }
-      else
-        {
-          session_bus_down ();
-        }
-
-      g_main_loop_run (loop);
-      /* this is only reached when we turn off exit-on-close */
-      g_main_loop_unref (loop);
-      exit (0);
-    }
+  /* the default is meant to be TRUE */
+  if (td->exit_on_close != IMPLICITLY_TRUE)
+    g_dbus_connection_set_exit_on_close (c, td->exit_on_close);
 
-  if (td->exit_on_close == EXPLICITLY_FALSE ||
-      td->who_closes == LOCAL)
+  g_assert_cmpint (g_dbus_connection_get_exit_on_close (c), ==,
+                   (td->exit_on_close != EXPLICITLY_FALSE));
+  g_assert (!g_dbus_connection_is_closed (c));
+
+  g_timeout_add (50, quit_later_cb, NULL);
+  g_main_loop_run (loop);
+
+  g_signal_connect (c, "closed", G_CALLBACK (closed_cb), (gpointer) td);
+
+  if (td->who_closes == LOCAL)
     {
-      g_test_trap_assert_stdout_unmatched (VANISHED_PATTERN);
-      g_test_trap_assert_passed ();
+      GVariant *v;
+      GError *error = NULL;
+
+      v = g_dbus_connection_call_sync (c, "org.freedesktop.DBus",
+                                       "/org/freedesktop/DBus",
+                                       "org.freedesktop.DBus",
+                                       "ListNames",
+                                       NULL,
+                                       G_VARIANT_TYPE ("(as)"),
+                                       G_DBUS_CALL_FLAGS_NONE,
+                                       -1,
+                                       NULL,
+                                       &error);
+      g_assert_no_error (error);
+      g_assert (v != NULL);
+      g_variant_unref (v);
+
+      g_dbus_connection_close (c, NULL, close_async_cb, NULL);
     }
   else
     {
-      g_test_trap_assert_stdout (VANISHED_PATTERN);
-      g_test_trap_assert_failed();
+      session_bus_stop ();
     }
+
+  g_main_loop_run (loop);
+  /* this is only reached when we turn off exit-on-close */
+  g_main_loop_unref (loop);
+  g_object_unref (c);
+
+  session_bus_down ();
+
+  exit (0);
+}
+
+static void
+test_exit_on_close (gconstpointer test_data)
+{
+  const TestData *td = test_data;
+  GTestSubprocessFlags flags;
+  char *child_name;
+
+  g_test_dbus_unset ();
+
+  if (g_test_verbose ())
+    flags = G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;
+  else
+    flags = 0;
+
+  child_name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", td->name);
+  g_test_trap_subprocess (child_name, 0, flags);
+  g_free (child_name);
+
+  if (td->exit_on_close == EXPLICITLY_FALSE ||
+      td->who_closes == LOCAL)
+    g_test_trap_assert_passed ();
+  else
+    g_test_trap_assert_failed();
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -203,15 +199,19 @@ main (int   argc,
 {
   gint i;
 
-  g_type_init ();
   g_test_init (&argc, &argv, NULL);
 
   for (i = 0; cases[i].name != NULL; i++)
     {
-      gchar *name = g_strdup_printf ("/gdbus/exit-on-close/%s", cases[i].name);
+      gchar *name;
 
+      name = g_strdup_printf ("/gdbus/exit-on-close/%s", cases[i].name);
       g_test_add_data_func (name, &cases[i], test_exit_on_close);
       g_free (name);
+
+      name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", cases[i].name);
+      g_test_add_data_func (name, &cases[i], test_exit_on_close_subprocess);
+      g_free (name);
     }
 
   return g_test_run();