Revert "Add GTestDBus object"
[platform/upstream/glib.git] / gio / tests / gdbus-export.c
index 297fb23..21e9fda 100644 (file)
@@ -343,7 +343,6 @@ get_nodes_at (GDBusConnection  *c,
 
   error = NULL;
   proxy = g_dbus_proxy_new_sync (c,
-                                 G_TYPE_DBUS_PROXY,
                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                                  NULL,
@@ -400,7 +399,6 @@ has_interface (GDBusConnection *c,
 
   error = NULL;
   proxy = g_dbus_proxy_new_sync (c,
-                                 G_TYPE_DBUS_PROXY,
                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                                  NULL,
@@ -450,7 +448,6 @@ count_interfaces (GDBusConnection *c,
 
   error = NULL;
   proxy = g_dbus_proxy_new_sync (c,
-                                 G_TYPE_DBUS_PROXY,
                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                                  NULL,
@@ -522,7 +519,6 @@ dyna_create (GDBusConnection *c,
 
   error = NULL;
   proxy = g_dbus_proxy_new_sync (c,
-                                 G_TYPE_DBUS_PROXY,
                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                                  NULL,
@@ -618,37 +614,38 @@ subtree_enumerate (GDBusConnection       *connection,
 }
 
 /* Only allows certain objects, and aborts on unknowns */
-static GPtrArray *
+static GDBusInterfaceInfo **
 subtree_introspect (GDBusConnection       *connection,
                     const gchar           *sender,
                     const gchar           *object_path,
                     const gchar           *node,
                     gpointer               user_data)
 {
-  GPtrArray *interfaces;
+  const GDBusInterfaceInfo *interfaces[2] = {
+     NULL /* filled in below */, NULL
+  };
 
   /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
    * does not implement any interfaces
    */
-  interfaces = g_ptr_array_new ();
-  if (g_str_has_prefix (node, "vp"))
+  if (node == NULL)
     {
-      g_ptr_array_add (interfaces, (gpointer) &foo_interface_info);
+      return NULL;
     }
-  else if (g_str_has_prefix (node, "evp"))
+  else if (g_str_has_prefix (node, "vp"))
     {
-      g_ptr_array_add (interfaces, (gpointer) &bar_interface_info);
+      interfaces[0] = &foo_interface_info;
     }
-  else if (g_strcmp0 (node, "/") == 0)
+  else if (g_str_has_prefix (node, "evp"))
     {
-      /* do nothing */
+      interfaces[0] = &bar_interface_info;
     }
   else
     {
       g_assert_not_reached ();
     }
 
-  return interfaces;
+  return g_memdup (interfaces, 2 * sizeof (void *));
 }
 
 static const GDBusInterfaceVTable *
@@ -695,20 +692,16 @@ dynamic_subtree_enumerate (GDBusConnection       *connection,
 }
 
 /* Allow all objects to be introspected */
-static GPtrArray *
+static GDBusInterfaceInfo **
 dynamic_subtree_introspect (GDBusConnection       *connection,
                             const gchar           *sender,
                             const gchar           *object_path,
                             const gchar           *node,
                             gpointer               user_data)
 {
-  GPtrArray *interfaces;
+  const GDBusInterfaceInfo *interfaces[2] = { &dyna_interface_info, NULL };
 
-  /* All nodes (including the root node) implements the Dyna interface */
-  interfaces = g_ptr_array_new ();
-  g_ptr_array_add (interfaces, (gpointer) &dyna_interface_info);
-
-  return interfaces;
+  return g_memdup (interfaces, 2 * sizeof (void *));
 }
 
 static const GDBusInterfaceVTable *
@@ -745,7 +738,6 @@ test_dispatch_thread_func (gpointer user_data)
   const gchar *value_str;
 
   foo_proxy = g_dbus_proxy_new_sync (c,
-                                     G_TYPE_DBUS_PROXY,
                                      G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
                                      NULL,
@@ -944,16 +936,11 @@ static void
 test_dispatch (const gchar *object_path)
 {
   GThread *thread;
-  GError *error;
 
   /* run this in a thread to avoid deadlocks */
-  error = NULL;
-  thread = g_thread_create (test_dispatch_thread_func,
-                            (gpointer) object_path,
-                            TRUE,
-                            &error);
-  g_assert_no_error (error);
-  g_assert (thread != NULL);
+  thread = g_thread_new ("test_dispatch",
+                         test_dispatch_thread_func,
+                         (gpointer) object_path);
   g_main_loop_run (loop);
   g_thread_join (thread);
 }
@@ -968,6 +955,7 @@ test_object_registration (void)
   guint boss_foo_reg_id;
   guint boss_bar_reg_id;
   guint worker1_foo_reg_id;
+  guint worker1p1_foo_reg_id;
   guint worker2_bar_reg_id;
   guint intern1_foo_reg_id;
   guint intern2_bar_reg_id;
@@ -993,7 +981,7 @@ test_object_registration (void)
 
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss",
-                                                       &foo_interface_info,
+                                                       (GDBusInterfaceInfo *) &foo_interface_info,
                                                        &foo_vtable,
                                                        &data,
                                                        on_object_unregistered,
@@ -1005,7 +993,7 @@ test_object_registration (void)
 
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss",
-                                                       &bar_interface_info,
+                                                       (GDBusInterfaceInfo *) &bar_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1017,7 +1005,7 @@ test_object_registration (void)
 
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/worker1",
-                                                       &foo_interface_info,
+                                                       (GDBusInterfaceInfo *) &foo_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1028,8 +1016,20 @@ test_object_registration (void)
   num_successful_registrations++;
 
   registration_id = g_dbus_connection_register_object (c,
+                                                       "/foo/boss/worker1p1",
+                                                       (GDBusInterfaceInfo *) &foo_interface_info,
+                                                       NULL,
+                                                       &data,
+                                                       on_object_unregistered,
+                                                       &error);
+  g_assert_no_error (error);
+  g_assert (registration_id > 0);
+  worker1p1_foo_reg_id = registration_id;
+  num_successful_registrations++;
+
+  registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/worker2",
-                                                       &bar_interface_info,
+                                                       (GDBusInterfaceInfo *) &bar_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1041,7 +1041,7 @@ test_object_registration (void)
 
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/interns/intern1",
-                                                       &foo_interface_info,
+                                                       (GDBusInterfaceInfo *) &foo_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1054,7 +1054,7 @@ test_object_registration (void)
   /* ... and try again at another path */
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/interns/intern2",
-                                                       &bar_interface_info,
+                                                       (GDBusInterfaceInfo *) &bar_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1067,7 +1067,7 @@ test_object_registration (void)
   /* register at the same path/interface - this should fail */
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/interns/intern2",
-                                                       &bar_interface_info,
+                                                       (GDBusInterfaceInfo *) &bar_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1081,7 +1081,7 @@ test_object_registration (void)
   /* register at different interface - shouldn't fail */
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/interns/intern2",
-                                                       &foo_interface_info,
+                                                       (GDBusInterfaceInfo *) &foo_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1093,13 +1093,14 @@ test_object_registration (void)
 
   /* unregister it via the id */
   g_assert (g_dbus_connection_unregister_object (c, registration_id));
+  g_main_context_iteration (NULL, FALSE);
   g_assert_cmpint (data.num_unregistered_calls, ==, 1);
   intern2_foo_reg_id = 0;
 
   /* register it back */
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/interns/intern2",
-                                                       &foo_interface_info,
+                                                       (GDBusInterfaceInfo *) &foo_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1111,7 +1112,7 @@ test_object_registration (void)
 
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/interns/intern3",
-                                                       &bar_interface_info,
+                                                       (GDBusInterfaceInfo *) &bar_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1148,6 +1149,7 @@ test_object_registration (void)
   /* unregister it, then register it again */
   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
+  g_main_context_iteration (NULL, FALSE);
   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
   subtree_registration_id = g_dbus_connection_register_subtree (c,
                                                                 "/foo/boss/executives",
@@ -1167,7 +1169,7 @@ test_object_registration (void)
    */
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/executives/non_subtree_object",
-                                                       &bar_interface_info,
+                                                       (GDBusInterfaceInfo *) &bar_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1178,7 +1180,7 @@ test_object_registration (void)
   num_successful_registrations++;
   registration_id = g_dbus_connection_register_object (c,
                                                        "/foo/boss/executives/non_subtree_object",
-                                                       &foo_interface_info,
+                                                       (GDBusInterfaceInfo *) &foo_interface_info,
                                                        NULL,
                                                        &data,
                                                        on_object_unregistered,
@@ -1255,8 +1257,9 @@ test_object_registration (void)
 
   nodes = get_nodes_at (c, "/foo/boss");
   g_assert (nodes != NULL);
-  g_assert_cmpint (g_strv_length (nodes), ==, 4);
+  g_assert_cmpint (g_strv_length (nodes), ==, 5);
   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
+  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1p1"));
   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
@@ -1316,6 +1319,17 @@ test_object_registration (void)
   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
   g_strfreev (nodes);
 
+  /* This is to check that a bug (rather, class of bugs) in gdbusconnection.c's
+   *
+   *  g_dbus_connection_list_registered_unlocked()
+   *
+   * where /foo/boss/worker1 reported a child '1', is now fixed.
+   */
+  nodes = get_nodes_at (c, "/foo/boss/worker1");
+  g_assert (nodes != NULL);
+  g_assert_cmpint (g_strv_length (nodes), ==, 0);
+  g_strfreev (nodes);
+
   /* check that calls are properly dispatched to the functions in foo_vtable for objects
    * implementing the org.example.Foo interface
    *
@@ -1334,6 +1348,7 @@ test_object_registration (void)
   /* check that unregistering the subtree handler works */
   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
+  g_main_context_iteration (NULL, FALSE);
   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
   nodes = get_nodes_at (c, "/foo/boss/executives");
   g_assert (nodes != NULL);
@@ -1344,6 +1359,7 @@ test_object_registration (void)
   g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
   g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
   g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
+  g_assert (g_dbus_connection_unregister_object (c, worker1p1_foo_reg_id));
   g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
   g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
   g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
@@ -1352,6 +1368,7 @@ test_object_registration (void)
   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
 
+  g_main_context_iteration (NULL, FALSE);
   g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
 
   /* check that we no longer export any objects - TODO: it looks like there's a bug in
@@ -1367,6 +1384,144 @@ test_object_registration (void)
   g_object_unref (c);
 }
 
+static const GDBusInterfaceInfo test_interface_info1 =
+{
+  -1,
+  "org.example.Foo",
+  (GDBusMethodInfo **) NULL,
+  (GDBusSignalInfo **) NULL,
+  (GDBusPropertyInfo **) NULL,
+  NULL,
+};
+
+static const GDBusInterfaceInfo test_interface_info2 =
+{
+  -1,
+  "org.freedesktop.DBus.Properties",
+  (GDBusMethodInfo **) NULL,
+  (GDBusSignalInfo **) NULL,
+  (GDBusPropertyInfo **) NULL,
+  NULL,
+};
+
+static void
+check_interfaces (GDBusConnection  *c,
+                  const gchar      *object_path,
+                  const gchar     **interfaces)
+{
+  GError *error;
+  GDBusProxy *proxy;
+  gchar *xml_data;
+  GDBusNodeInfo *node_info;
+  gint i, j;
+
+  error = NULL;
+  proxy = g_dbus_proxy_new_sync (c,
+                                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+                                 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+                                 NULL,
+                                 g_dbus_connection_get_unique_name (c),
+                                 object_path,
+                                 "org.freedesktop.DBus.Introspectable",
+                                 NULL,
+                                 &error);
+  g_assert_no_error (error);
+  g_assert (proxy != NULL);
+
+  /* do this async to avoid libdbus-1 deadlocks */
+  xml_data = NULL;
+  g_dbus_proxy_call (proxy,
+                     "Introspect",
+                     NULL,
+                     G_DBUS_CALL_FLAGS_NONE,
+                     -1,
+                     NULL,
+                     (GAsyncReadyCallback) introspect_callback,
+                     &xml_data);
+  g_main_loop_run (loop);
+  g_assert (xml_data != NULL);
+
+  node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
+  g_assert_no_error (error);
+  g_assert (node_info != NULL);
+
+  g_assert (node_info->interfaces != NULL);
+  for (i = 0; node_info->interfaces[i]; i++) ;
+#if 0
+  if (g_strv_length ((gchar**)interfaces) != i - 1)
+    {
+      g_print ("expected ");
+      for (i = 0; interfaces[i]; i++)
+        g_print ("%s ", interfaces[i]);
+      g_print ("\ngot ");
+      for (i = 0; node_info->interfaces[i]; i++)
+        g_print ("%s ", node_info->interfaces[i]->name);
+      g_print ("\n");
+    }
+#endif
+  g_assert_cmpint (g_strv_length ((gchar**)interfaces), ==, i - 1);
+
+  for (i = 0; interfaces[i]; i++)
+    {
+      for (j = 0; node_info->interfaces[j]; j++)
+        {
+          if (strcmp (interfaces[i], node_info->interfaces[j]->name) == 0)
+            goto found;
+        }
+
+      g_assert_not_reached ();
+
+ found: ;
+    }
+
+  g_object_unref (proxy);
+  g_free (xml_data);
+  g_dbus_node_info_unref (node_info);
+}
+
+static void
+test_registered_interfaces (void)
+{
+  GError *error;
+  guint id1, id2;
+  const gchar *interfaces[] = {
+    "org.example.Foo",
+    "org.freedesktop.DBus.Properties",
+    "org.freedesktop.DBus.Introspectable",
+    NULL,
+  };
+
+  error = NULL;
+  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+  g_assert_no_error (error);
+  g_assert (c != NULL);
+
+  id1 = g_dbus_connection_register_object (c,
+                                           "/test",
+                                           (GDBusInterfaceInfo *) &test_interface_info1,
+                                           NULL,
+                                           NULL,
+                                           NULL,
+                                           &error);
+  g_assert_no_error (error);
+  g_assert (id1 > 0);
+  id2 = g_dbus_connection_register_object (c,
+                                           "/test",
+                                           (GDBusInterfaceInfo *) &test_interface_info2,
+                                           NULL,
+                                           NULL,
+                                           NULL,
+                                           &error);
+  g_assert_no_error (error);
+  g_assert (id2 > 0);
+
+  check_interfaces (c, "/test", interfaces);
+
+  g_assert (g_dbus_connection_unregister_object (c, id1));
+  g_assert (g_dbus_connection_unregister_object (c, id2));
+  g_object_unref (c);
+}
+
 
 /* ---------------------------------------------------------------------------------------------------- */
 
@@ -1396,6 +1551,8 @@ main (int   argc,
   usleep (500 * 1000);
 
   g_test_add_func ("/gdbus/object-registration", test_object_registration);
+  g_test_add_func ("/gdbus/registered-interfaces", test_registered_interfaces);
+
   /* TODO: check that we spit out correct introspection data */
   /* TODO: check that registering a whole subtree works */