* dbus/dbus-auth.c, dbus/dbus-connection.c, dbus/dbus-keyring.c,
[platform/upstream/dbus.git] / test / glib / test-dbus-glib.c
index cac774c..03a1a73 100644 (file)
@@ -26,6 +26,13 @@ static gboolean proxy_destroy_and_nameowner_complete = FALSE;
 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
 
+static void
+unset_and_free_gvalue (gpointer val)
+{
+  g_value_unset (val);
+  g_free (val);
+}
+
 static gboolean
 timed_exit (gpointer loop)
 {
@@ -40,6 +47,7 @@ proxy_destroyed_cb (DBusGProxy *proxy, gpointer user_data)
   proxy_destroyed = TRUE;
   if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && await_terminating_service == NULL)
     {
+      g_source_remove (exit_timeout);
       g_main_loop_quit (loop);
       proxy_destroy_and_nameowner_complete = TRUE;
     } 
@@ -62,11 +70,13 @@ name_owner_changed (DBusGProxy *proxy,
       await_terminating_service = NULL;
       if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && proxy_destroyed)
        {
+         g_source_remove (exit_timeout);
          g_main_loop_quit (loop);
          proxy_destroy_and_nameowner_complete = TRUE;
        } 
       else if (!proxy_destroy_and_nameowner)
        {
+         g_source_remove (exit_timeout);
          g_main_loop_quit (loop);
        }
     }
@@ -194,7 +204,8 @@ echo_received_cb (DBusGProxy *proxy,
                              G_TYPE_INVALID))
     lose_gerror ("Failed to complete async Echo", error);
   g_assert (echo_data != NULL);
-  g_print ("Async echo gave \"%s\"", echo_data); 
+  g_print ("Async echo gave \"%s\"\n", echo_data); 
+  g_free (echo_data);
   g_main_loop_quit (loop);
   g_source_remove (exit_timeout);
 }
@@ -205,13 +216,11 @@ increment_received_cb (DBusGProxy *proxy,
                       gpointer data)
 {
   GError *error;
-  char *echo_data;
   guint val;
 
   g_assert (!strcmp (data, "moo"));
 
   error = NULL;
-  echo_data = NULL;
   if (!dbus_g_proxy_end_call (proxy, call, &error,
                              G_TYPE_UINT, &val,
                              G_TYPE_INVALID))
@@ -220,11 +229,28 @@ increment_received_cb (DBusGProxy *proxy,
   if (val != 43)
     lose ("Increment call returned %d, should be 43", val);
   
-  g_print ("Async increment gave \"%d\"", val); 
+  g_print ("Async increment gave \"%d\"\n", val); 
+  g_main_loop_quit (loop);
+  g_source_remove (exit_timeout);
+}
+
+static void
+increment_async_cb (DBusGProxy *proxy, guint val, GError *error, gpointer data)
+{
+  if (error)
+    lose_gerror ("Failed to complete (wrapped async) Increment call", error);
+
+  if (data != NULL)
+    lose ("(wrapped async) Increment call gave unexpected data");
+  if (val != 43)
+    lose ("(wrapped async) Increment call returned %d, should be 43", val);
+
+  g_print ("(wrapped async) increment gave \"%d\"\n", val); 
   g_main_loop_quit (loop);
   g_source_remove (exit_timeout);
 }
 
+
 static void
 lose (const char *str, ...)
 {
@@ -476,11 +502,11 @@ main (int argc, char **argv)
                          G_TYPE_UINT, &v_UINT32_2,
                          G_TYPE_INVALID))
     lose_gerror ("Failed to complete Increment call", error);
-  g_assert (n_times_echo_cb_entered == 1);
-
   if (v_UINT32_2 != 43)
     lose ("Increment call returned %d, should be 43", v_UINT32_2);
 
+  v_UINT32_2 = 0;
+  g_print ("Calling Increment (async)\n");
   call = dbus_g_proxy_begin_call (proxy, "Increment",
                                  increment_received_cb, g_strdup ("moo"), g_free,
                                  G_TYPE_UINT, 42,
@@ -489,6 +515,30 @@ main (int argc, char **argv)
   exit_timeout = g_timeout_add (5000, timed_exit, loop);
   g_main_loop_run (loop);
 
+  g_print ("Calling IncrementRetval\n");
+  error = NULL;
+  v_UINT32_2 = 0;
+  if (!dbus_g_proxy_call (proxy, "IncrementRetval", &error,
+                         G_TYPE_UINT, 42,
+                         G_TYPE_INVALID,
+                         G_TYPE_UINT, &v_UINT32_2,
+                         G_TYPE_INVALID))
+    lose_gerror ("Failed to complete Increment call", error);
+  if (v_UINT32_2 != 43)
+    lose ("IncrementRetval call returned %d, should be 43", v_UINT32_2);
+
+  g_print ("Calling IncrementRetvalError\n");
+  error = NULL;
+  v_UINT32_2 = 0;
+  if (!dbus_g_proxy_call (proxy, "IncrementRetvalError", &error,
+                         G_TYPE_UINT, 5,
+                         G_TYPE_INVALID,
+                         G_TYPE_UINT, &v_UINT32_2,
+                         G_TYPE_INVALID))
+    lose_gerror ("Failed to complete Increment call", error);
+  if (v_UINT32_2 != 6)
+    lose ("IncrementRetval call returned %d, should be 6", v_UINT32_2);
+
   g_print ("Calling ThrowError\n");
   if (dbus_g_proxy_call (proxy, "ThrowError", &error,
                         G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
@@ -501,6 +551,19 @@ main (int argc, char **argv)
   g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
   g_clear_error (&error);
 
+  g_print ("Calling IncrementRetvalError (for error)\n");
+  error = NULL;
+  v_UINT32_2 = 0;
+  if (dbus_g_proxy_call (proxy, "IncrementRetvalError", &error,
+                        G_TYPE_UINT, 20,
+                        G_TYPE_INVALID,
+                        G_TYPE_UINT, &v_UINT32_2,
+                        G_TYPE_INVALID) != FALSE)
+    lose ("IncrementRetvalError call unexpectedly succeeded!");
+  if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
+    lose ("IncrementRetvalError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error), error->message);
+  g_clear_error (&error);
+
   error = NULL;
   g_print ("Calling Uppercase\n");
   if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
@@ -542,6 +605,13 @@ main (int argc, char **argv)
   if (v_UINT32_2 != 43)
     lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
 
+  g_print ("Calling (wrapped async) increment\n");
+  if (!org_freedesktop_DBus_Tests_MyObject_increment_async (proxy, 42, increment_async_cb, NULL))
+    lose_gerror ("Failed to complete (wrapped) Increment call", error);
+  dbus_g_connection_flush (connection);
+  exit_timeout = g_timeout_add (5000, timed_exit, loop);
+  g_main_loop_run (loop);
+
   v_UINT32_2 = 0;
   if (!org_freedesktop_DBus_Tests_MyObject_async_increment (proxy, 42, &v_UINT32_2, &error))
     lose_gerror ("Failed to complete (wrapped) AsyncIncrement call", error);
@@ -813,6 +883,146 @@ main (int argc, char **argv)
   run_mainloop ();
 
   {
+    GValueArray *vals;
+    GValueArray *vals_ret;
+    GValue *val;
+
+    vals = g_value_array_new (3);
+
+    g_value_array_append (vals, NULL);
+    g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_STRING);
+    g_value_set_string (g_value_array_get_nth (vals, 0), "foo");
+
+    g_value_array_append (vals, NULL);
+    g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_UINT);
+    g_value_set_uint (g_value_array_get_nth (vals, vals->n_values - 1), 42);
+
+    g_value_array_append (vals, NULL);
+    g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_VALUE);
+    val = g_new0 (GValue, 1);
+    g_value_init (val, G_TYPE_UCHAR);
+    g_value_set_uchar (val, '!');
+    g_value_set_boxed (g_value_array_get_nth (vals, vals->n_values - 1), val);
+
+    vals_ret = NULL;
+    g_print ("Calling SendCar\n");
+    if (!dbus_g_proxy_call (proxy, "SendCar", &error,
+                           G_TYPE_VALUE_ARRAY, vals,
+                           G_TYPE_INVALID,
+                           G_TYPE_VALUE_ARRAY, &vals_ret,
+                           G_TYPE_INVALID))
+      lose_gerror ("Failed to complete SendCar call", error);
+
+    g_assert (vals_ret != NULL);
+    g_assert (vals_ret->n_values == 2);
+
+    g_assert (G_VALUE_HOLDS_UINT (g_value_array_get_nth (vals_ret, 0)));
+    g_assert (g_value_get_uint (g_value_array_get_nth (vals_ret, 0)) == 43);
+    
+    g_assert (G_VALUE_TYPE (g_value_array_get_nth (vals_ret, 1)) == DBUS_TYPE_G_OBJECT_PATH);
+    g_assert (!strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
+                      g_value_get_boxed (g_value_array_get_nth (vals_ret, 1))));
+
+    g_value_array_free (vals);
+    g_value_array_free (vals_ret);
+  }
+
+  {
+    GValue *val;
+    GHashTable *table;
+    GHashTable *ret_table;
+
+    table = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                  g_free, unset_and_free_gvalue);
+    
+    val = g_new0 (GValue, 1);
+    g_value_init (val, G_TYPE_UINT);
+    g_value_set_uint (val, 42);
+    g_hash_table_insert (table, g_strdup ("foo"), val);
+
+    val = g_new0 (GValue, 1);
+    g_value_init (val, G_TYPE_STRING);
+    g_value_set_string (val, "hello");
+    g_hash_table_insert (table, g_strdup ("bar"), val);
+
+    ret_table = NULL;
+    g_print ("Calling ManyStringify\n");
+    if (!dbus_g_proxy_call (proxy, "ManyStringify", &error,
+                           dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), table,
+                           G_TYPE_INVALID,
+                           dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &ret_table,
+                           G_TYPE_INVALID))
+      lose_gerror ("Failed to complete ManyStringify call", error);
+
+    g_assert (ret_table != NULL);
+    g_assert (g_hash_table_size (ret_table) == 2);
+
+    val = g_hash_table_lookup (ret_table, "foo");
+    g_assert (val != NULL);
+    g_assert (G_VALUE_HOLDS_STRING (val));
+    g_assert (!strcmp ("42", g_value_get_string (val)));
+
+    val = g_hash_table_lookup (ret_table, "bar");
+    g_assert (val != NULL);
+    g_assert (G_VALUE_HOLDS_STRING (val));
+    g_assert (!strcmp ("hello", g_value_get_string (val)));
+
+    g_hash_table_destroy (table);
+    g_hash_table_destroy (ret_table);
+  }
+
+  {
+    GPtrArray *in_array;
+    GPtrArray *out_array;
+    char **strs;
+    GArray *uints;
+
+    in_array = g_ptr_array_new ();
+
+    strs = g_new0 (char *, 3);
+    strs[0] = "foo";
+    strs[1] = "bar";
+    strs[2] = NULL;
+    g_ptr_array_add (in_array, strs);
+
+    strs = g_new0 (char *, 4);
+    strs[0] = "baz";
+    strs[1] = "whee";
+    strs[2] = "moo";
+    strs[3] = NULL;
+    g_ptr_array_add (in_array, strs);
+
+    out_array = NULL;
+    g_print ("Calling RecArrays\n");
+    if (!dbus_g_proxy_call (proxy, "RecArrays", &error,
+                           dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRV), in_array,
+                           G_TYPE_INVALID,
+                           dbus_g_type_get_collection ("GPtrArray",
+                                                       dbus_g_type_get_collection ("GPtrArray",
+                                                                                   G_TYPE_UINT)), &out_array, 
+                           G_TYPE_INVALID))
+      lose_gerror ("Failed to complete RecArrays call", error);
+    g_free (g_ptr_array_index (in_array, 0));
+    g_free (g_ptr_array_index (in_array, 1));
+
+    g_assert (out_array);
+    g_assert (out_array->len == 2);
+    uints = g_ptr_array_index (out_array, 0);
+    g_assert (uints);
+    g_assert (uints->len == 3);
+    g_assert (g_array_index (uints, guint, 0) == 10);
+    g_assert (g_array_index (uints, guint, 1) == 42);
+    g_assert (g_array_index (uints, guint, 2) == 27);
+    g_array_free (uints, TRUE);
+    uints = g_ptr_array_index (out_array, 1);
+    g_assert (uints);
+    g_assert (uints->len == 1);
+    g_assert (g_array_index (uints, guint, 0) == 30);
+    g_array_free (uints, TRUE);
+    g_ptr_array_free (out_array, TRUE);
+  }
+
+  {
     guint val;
     char *ret_path;
     DBusGProxy *ret_proxy;
@@ -899,6 +1109,61 @@ main (int argc, char **argv)
 
   run_mainloop ();
 
+  {
+    GPtrArray *objs;
+    guint i;
+
+    g_print ("Calling GetObjs\n");
+
+    if (!dbus_g_proxy_call (proxy, "GetObjs", &error, G_TYPE_INVALID,
+                           dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH),
+                           &objs,
+                           G_TYPE_INVALID))
+      lose_gerror ("Failed to complete GetObjs call", error);
+    if (objs->len != 2)
+      lose ("GetObjs call returned unexpected number of objects %d, expected 2",
+           objs->len);
+
+    if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject",
+               g_ptr_array_index (objs, 0)) != 0)
+      lose ("GetObjs call returned unexpected path \"%s\" in position 0; expected /org/freedesktop/DBus/Tests/MyTestObject", (char*) g_ptr_array_index (objs, 0));
+
+    if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
+               g_ptr_array_index (objs, 1)) != 0)
+      lose ("GetObjs call returned unexpected path \"%s\" in position 1; expected /org/freedesktop/DBus/Tests/MyTestObject2", (char*) g_ptr_array_index (objs, 1));
+
+    for (i = 0; i < objs->len; i++)
+      g_free (g_ptr_array_index (objs, i));
+    g_ptr_array_free (objs, TRUE);
+  }
+  
+  {
+    GValue *variant;
+    GArray *array;
+    gint i;
+
+    g_print ("Calling ProcessVariantOfArrayOfInts123\n");
+
+    array = g_array_sized_new (FALSE, FALSE, sizeof(gint), 3);
+    i = 1;
+    g_array_append_val (array, i);
+    i++;
+    g_array_append_val (array, i);
+    i++;
+    g_array_append_val (array, i);
+
+    variant = g_new0 (GValue, 1);
+    g_value_init (variant, dbus_g_type_get_collection ("GArray", G_TYPE_INT));
+    g_value_set_boxed_take_ownership (variant, array);
+
+    if (!dbus_g_proxy_call (proxy, "ProcessVariantOfArrayOfInts123", &error,
+                            G_TYPE_VALUE, variant,
+                            G_TYPE_INVALID,
+                           G_TYPE_INVALID))
+      lose_gerror ("Failed to send a vairant of array of ints 1, 2 and 3!", error);
+
+    g_value_unset (variant);
+  }
   /* Signal handling tests */
   
   g_print ("Testing signal handling\n");
@@ -1082,6 +1347,9 @@ main (int argc, char **argv)
     lose_gerror ("Failed to complete Uppercase call", error);
   g_free (v_STRING_2);
 
+  if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION1"))
+    g_usleep (8 * G_USEC_PER_SEC);
+
   dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
   
   dbus_g_proxy_connect_signal (proxy, "Frobnicate",
@@ -1122,6 +1390,9 @@ main (int argc, char **argv)
                          G_TYPE_INVALID, G_TYPE_INVALID))
     lose_gerror ("Failed to complete EmitFrobnicate call", error);
 
+  if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION2"))
+    g_usleep (8 * G_USEC_PER_SEC);
+
   dbus_g_connection_flush (connection);
   exit_timeout = g_timeout_add (5000, timed_exit, loop);
   g_main_loop_run (loop);