[Telepathy-commits] [telepathy-glib/master] Add a regression test for fd.o #19101

Will Thompson will.thompson at collabora.co.uk
Wed Jan 7 07:24:03 PST 2009


---
 .gitignore                      |    1 +
 tests/dbus/Makefile.am          |    3 +
 tests/dbus/contacts-bug-19101.c |  199 +++++++++++++++++++++++++++++++++++++++
 tests/lib/Makefile.am           |    2 +
 tests/lib/bug-19101-conn.c      |   74 +++++++++++++++
 tests/lib/bug-19101-conn.h      |   51 ++++++++++
 6 files changed, 330 insertions(+), 0 deletions(-)
 create mode 100644 tests/dbus/contacts-bug-19101.c
 create mode 100644 tests/lib/bug-19101-conn.c
 create mode 100644 tests/lib/bug-19101-conn.h

diff --git a/.gitignore b/.gitignore
index 0a30bf9..48e0b3d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -72,6 +72,7 @@ tests/dbus/test-connection-getinterfaces-failure
 tests/dbus/test-connection-handles
 tests/dbus/test-connection-inject-bug16307
 tests/dbus/test-contacts
+tests/dbus/test-contacts-bug-19101
 tests/dbus/test-contacts-mixin
 tests/dbus/test-dbus
 tests/dbus/test-disconnection
diff --git a/tests/dbus/Makefile.am b/tests/dbus/Makefile.am
index b914d76..6c013c6 100644
--- a/tests/dbus/Makefile.am
+++ b/tests/dbus/Makefile.am
@@ -7,6 +7,7 @@ noinst_PROGRAMS = \
     test-connection-inject-bug16307 \
     test-connection-getinterfaces-failure \
     test-contacts \
+    test-contacts-bug-19101 \
     test-contacts-mixin \
     test-dbus \
     test-disconnection \
@@ -41,6 +42,8 @@ test_connection_getinterfaces_failure_SOURCES = \
 
 test_contacts_SOURCES = contacts.c
 
+test_contacts_bug_19101_SOURCES = contacts-bug-19101.c
+
 test_contacts_mixin_SOURCES = contacts-mixin.c
 
 test_finalized_in_invalidated_handler_SOURCES = \
diff --git a/tests/dbus/contacts-bug-19101.c b/tests/dbus/contacts-bug-19101.c
new file mode 100644
index 0000000..50c1109
--- /dev/null
+++ b/tests/dbus/contacts-bug-19101.c
@@ -0,0 +1,199 @@
+/* Regression test for fd.o bug #19101. */
+
+#include <telepathy-glib/connection.h>
+#include <telepathy-glib/contact.h>
+#include <telepathy-glib/dbus.h>
+#include <telepathy-glib/debug.h>
+
+#include "tests/lib/bug-19101-conn.h"
+#include "tests/lib/debug.h"
+#include "tests/lib/myassert.h"
+#include "tests/lib/util.h"
+
+static int fail = 0;
+
+static void
+myassert_failed (void)
+{
+  fail = 1;
+}
+
+typedef struct {
+    GMainLoop *loop;
+    GError *error /* initialized to 0 */;
+    GPtrArray *contacts;
+    gchar **good_ids;
+    GHashTable *bad_ids;
+} Result;
+
+static void
+finish (gpointer r)
+{
+  Result *result = r;
+
+  g_main_loop_quit (result->loop);
+}
+
+static void
+by_id_cb (TpConnection *connection,
+          guint n_contacts,
+          TpContact * const *contacts,
+          const gchar * const *good_ids,
+          GHashTable *bad_ids,
+          const GError *error,
+          gpointer user_data,
+          GObject *weak_object)
+{
+  Result *result = user_data;
+
+  g_assert (result->contacts == NULL);
+  g_assert (result->error == NULL);
+  g_assert (result->good_ids == NULL);
+  g_assert (result->bad_ids == NULL);
+
+  if (error == NULL)
+    {
+      GHashTableIter iter;
+      gpointer key, value;
+      guint i;
+
+      DEBUG ("got %u contacts and %u bad IDs", n_contacts,
+          g_hash_table_size (bad_ids));
+
+      result->bad_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
+          g_free, (GDestroyNotify) g_error_free);
+      tp_g_hash_table_update (result->bad_ids, bad_ids,
+          (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) g_error_copy);
+
+      g_hash_table_iter_init (&iter, result->bad_ids);
+
+      while (g_hash_table_iter_next (&iter, &key, &value))
+        {
+          gchar *id = key;
+          GError *e = value;
+
+          DEBUG ("bad ID %s: %s %u: %s", id, g_quark_to_string (e->domain),
+              e->code, e->message);
+        }
+
+      result->good_ids = g_strdupv ((GStrv) good_ids);
+
+      result->contacts = g_ptr_array_sized_new (n_contacts);
+
+      for (i = 0; i < n_contacts; i++)
+        {
+          TpContact *contact = contacts[i];
+
+          DEBUG ("contact #%u: %p", i, contact);
+          DEBUG ("contact #%u we asked for ID %s", i, good_ids[i]);
+          DEBUG ("contact #%u we got ID %s", i,
+              tp_contact_get_identifier (contact));
+          DEBUG ("contact #%u alias: %s", i, tp_contact_get_alias (contact));
+          DEBUG ("contact #%u avatar token: %s", i,
+              tp_contact_get_avatar_token (contact));
+          DEBUG ("contact #%u presence type: %u", i,
+              tp_contact_get_presence_type (contact));
+          DEBUG ("contact #%u presence status: %s", i,
+              tp_contact_get_presence_status (contact));
+          DEBUG ("contact #%u presence message: %s", i,
+              tp_contact_get_presence_message (contact));
+          g_ptr_array_add (result->contacts, g_object_ref (contact));
+        }
+    }
+  else
+    {
+      DEBUG ("got an error: %s %u: %s", g_quark_to_string (error->domain),
+          error->code, error->message);
+      result->error = g_error_copy (error);
+    }
+}
+
+static void
+test_by_id (TpConnection *client_conn)
+{
+  Result result = { g_main_loop_new (NULL, FALSE) };
+  static const gchar * const ids[] = { "Alice", "Bob", "Not valid", "Chris",
+      "not valid either", NULL };
+
+  tp_connection_get_contacts_by_id (client_conn,
+      2, ids,
+      0, NULL,
+      by_id_cb,
+      &result, finish, NULL);
+
+  g_main_loop_run (result.loop);
+
+  MYASSERT (result.error != NULL, ": should fail as the CM is broken");
+  MYASSERT_SAME_UINT (result.error->domain, TP_DBUS_ERRORS);
+  MYASSERT (result.error->code == TP_DBUS_ERROR_INCONSISTENT,
+      ": %i != %i", result.error->code, TP_DBUS_ERROR_INCONSISTENT);
+
+  MYASSERT (result.contacts == NULL, "");
+  MYASSERT (result.good_ids == NULL, "");
+  MYASSERT (result.bad_ids == NULL, "");
+
+  /* clean up */
+  g_main_loop_unref (result.loop);
+  g_error_free (result.error);
+}
+
+int
+main (int argc,
+      char **argv)
+{
+  TpDBusDaemon *dbus;
+  ContactsConnection *service_conn;
+  TpBaseConnection *service_conn_as_base;
+  gchar *name;
+  gchar *conn_path;
+  GError *error = NULL;
+  TpConnection *client_conn;
+
+  /* Setup */
+
+  g_type_init ();
+  tp_debug_set_flags ("all");
+  dbus = tp_dbus_daemon_new (tp_get_bus ());
+
+  DEBUG ("a");
+  service_conn = CONTACTS_CONNECTION (g_object_new (
+        BUG_19101_TYPE_CONNECTION,
+        "account", "me at example.com",
+        "protocol", "simple",
+        NULL));
+  DEBUG ("b");
+  service_conn_as_base = TP_BASE_CONNECTION (service_conn);
+  MYASSERT (service_conn != NULL, "");
+  MYASSERT (service_conn_as_base != NULL, "");
+
+  MYASSERT (tp_base_connection_register (service_conn_as_base, "simple",
+        &name, &conn_path, &error), "");
+  MYASSERT_NO_ERROR (error);
+
+  client_conn = tp_connection_new (dbus, name, conn_path, &error);
+  MYASSERT (client_conn != NULL, "");
+  MYASSERT_NO_ERROR (error);
+  MYASSERT (tp_connection_run_until_ready (client_conn, TRUE, &error, NULL),
+      "");
+  MYASSERT_NO_ERROR (error);
+
+  /* Tests */
+
+  test_by_id (client_conn);
+
+  /* Teardown */
+
+  MYASSERT (tp_cli_connection_run_disconnect (client_conn, -1, &error, NULL),
+      "");
+  MYASSERT_NO_ERROR (error);
+  g_object_unref (client_conn);
+
+  service_conn_as_base = NULL;
+  g_object_unref (service_conn);
+  g_free (name);
+  g_free (conn_path);
+
+  g_object_unref (dbus);
+
+  return fail;
+}
diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am
index fe25a37..d2509ca 100644
--- a/tests/lib/Makefile.am
+++ b/tests/lib/Makefile.am
@@ -1,6 +1,8 @@
 noinst_LTLIBRARIES = libtp-glib-tests.la
 
 libtp_glib_tests_la_SOURCES = \
+    bug-19101-conn.c \
+    bug-19101-conn.h \
     bug16307-conn.c \
     bug16307-conn.h \
     contacts-conn.c \
diff --git a/tests/lib/bug-19101-conn.c b/tests/lib/bug-19101-conn.c
new file mode 100644
index 0000000..44b00f3
--- /dev/null
+++ b/tests/lib/bug-19101-conn.c
@@ -0,0 +1,74 @@
+/*
+ * bug-19101-conn.c - a broken connection to reproduce bug #19101
+ *
+ * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * Copying and distribution of this file, with or without modification,
+ * are permitted in any medium without royalty provided the copyright
+ * notice and this notice are preserved.
+ */
+
+#include "bug-19101-conn.h"
+
+#include "debug.h"
+
+static void contacts_iface_init (gpointer g_iface, gpointer iface_data);
+
+G_DEFINE_TYPE_WITH_CODE (Bug19101Connection, bug_19101_connection,
+    CONTACTS_TYPE_CONNECTION,
+    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CONNECTION_INTERFACE_CONTACTS,
+        contacts_iface_init);
+    )
+
+static void
+bug_19101_connection_init (Bug19101Connection *self)
+{
+}
+
+static void
+bug_19101_connection_class_init (Bug19101ConnectionClass *klass)
+{
+}
+
+/* A broken implementation of GetContactAttributes, which returns an empty dict
+ * of attributes for each handle.
+ */
+static void
+bug_19101_connection_get_contact_attributes (
+    TpSvcConnectionInterfaceContacts *iface,
+    const GArray *handles,
+    const char **interfaces,
+    gboolean hold,
+    DBusGMethodInvocation *context)
+{
+  GHashTable *result = g_hash_table_new_full (NULL, NULL, NULL,
+      (GDestroyNotify) g_hash_table_destroy);
+  guint i;
+
+  DEBUG ("called; returning rubbish");
+
+  for (i = 0 ; i < handles->len ; i++)
+    {
+      TpHandle h= g_array_index (handles, TpHandle, i);
+      GHashTable *attr_hash = g_hash_table_new_full (NULL, NULL, NULL, NULL);
+
+      g_hash_table_insert (result, GUINT_TO_POINTER(h), attr_hash);
+    }
+
+  tp_svc_connection_interface_contacts_return_from_get_contact_attributes (
+      context, result);
+  g_hash_table_unref (result);
+}
+
+static void
+contacts_iface_init (gpointer g_iface, gpointer iface_data)
+{
+  TpSvcConnectionInterfaceContactsClass *klass =
+    (TpSvcConnectionInterfaceContactsClass *) g_iface;
+
+#define IMPLEMENT(x) tp_svc_connection_interface_contacts_implement_##x ( \
+    klass, bug_19101_connection_##x)
+  IMPLEMENT(get_contact_attributes);
+#undef IMPLEMENT
+}
diff --git a/tests/lib/bug-19101-conn.h b/tests/lib/bug-19101-conn.h
new file mode 100644
index 0000000..e12df4b
--- /dev/null
+++ b/tests/lib/bug-19101-conn.h
@@ -0,0 +1,51 @@
+/*
+ * bug-19101-conn.h - header for a broken connection to reproduce bug #19101
+ *
+ * Copyright (C) 2007-2008 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2007-2008 Nokia Corporation
+ *
+ * Copying and distribution of this file, with or without modification,
+ * are permitted in any medium without royalty provided the copyright
+ * notice and this notice are preserved.
+ */
+
+#ifndef TESTS_LIB_BUG_19101_CONN_H
+#define TESTS_LIB_BUG_19101_CONN_H
+
+#include "contacts-conn.h"
+
+G_BEGIN_DECLS
+
+typedef struct _Bug19101Connection Bug19101Connection;
+typedef struct _Bug19101ConnectionClass Bug19101ConnectionClass;
+
+struct _Bug19101ConnectionClass {
+    ContactsConnectionClass parent_class;
+};
+
+struct _Bug19101Connection {
+    ContactsConnection parent;
+};
+
+GType bug_19101_connection_get_type (void);
+
+/* TYPE MACROS */
+#define BUG_19101_TYPE_CONNECTION \
+  (bug_19101_connection_get_type ())
+#define BUG_19101_CONNECTION(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj), BUG_19101_TYPE_CONNECTION, \
+                              Bug19101Connection))
+#define BUG_19101_CONNECTION_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass), BUG_19101_TYPE_CONNECTION, \
+                           Bug19101ConnectionClass))
+#define BUG_19101_IS_CONNECTION(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj), BUG_19101_TYPE_CONNECTION))
+#define BUG_19101_IS_CONNECTION_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass), BUG_19101_TYPE_CONNECTION))
+#define BUG_19101_CONNECTION_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), BUG_19101_TYPE_CONNECTION, \
+                              Bug19101ConnectionClass))
+
+G_END_DECLS
+
+#endif
-- 
1.5.6.5




More information about the Telepathy-commits mailing list