1 /* Regression test for https://bugs.freedesktop.org/show_bug.cgi?id=18845
2  *
3  * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Copying and distribution of this file, with or without modification,
7  * are permitted in any medium without royalty provided the copyright
8  * notice and this notice are preserved.
9  */
10 
11 #include "config.h"
12 
13 #include <telepathy-glib/connection.h>
14 #include <telepathy-glib/dbus.h>
15 #include <telepathy-glib/debug.h>
16 #include <telepathy-glib/interfaces.h>
17 #include <telepathy-glib/proxy-subclass.h>
18 
19 #include "tests/lib/myassert.h"
20 #include "tests/lib/simple-conn.h"
21 #include "tests/lib/util.h"
22 
23 static GMainLoop *mainloop;
24 
25 static GError invalidated_for_test = { 0, TP_ERROR_PERMISSION_DENIED,
26       "No connection for you!" };
27 
28 static gboolean
no_more_idling_around(gpointer data)29 no_more_idling_around (gpointer data)
30 {
31   g_main_loop_quit (data);
32   return FALSE;
33 }
34 
35 int
main(int argc,char ** argv)36 main (int argc,
37       char **argv)
38 {
39   TpDBusDaemon *dbus;
40   TpTestsSimpleConnection *service_conn;
41   TpBaseConnection *service_conn_as_base;
42   gchar *name;
43   gchar *conn_path;
44   GError *error = NULL;
45   TpConnection *conn;
46   DBusGProxy *proxy;
47 
48   tp_tests_abort_after (10);
49   invalidated_for_test.domain = TP_ERROR;
50 
51   tp_debug_set_flags ("all");
52   mainloop = g_main_loop_new (NULL, FALSE);
53   dbus = tp_tests_dbus_daemon_dup_or_die ();
54 
55   service_conn = TP_TESTS_SIMPLE_CONNECTION (tp_tests_object_new_static_class (
56         TP_TESTS_TYPE_SIMPLE_CONNECTION,
57         "account", "me@example.com",
58         "protocol", "simple",
59         NULL));
60   service_conn_as_base = TP_BASE_CONNECTION (service_conn);
61   MYASSERT (service_conn != NULL, "");
62   MYASSERT (service_conn_as_base != NULL, "");
63 
64   MYASSERT (tp_base_connection_register (service_conn_as_base, "simple",
65         &name, &conn_path, &error), "");
66   g_assert_no_error (error);
67 
68   conn = tp_connection_new (dbus, name, conn_path, &error);
69   MYASSERT (conn != NULL, "");
70   g_assert_no_error (error);
71   MYASSERT (tp_connection_run_until_ready (conn, TRUE, &error, NULL),
72       "");
73   g_assert_no_error (error);
74 
75   {
76     const gchar *ids[] = {
77         "flarglybadger",
78         NULL
79     };
80     GArray *handles = NULL;
81 
82     MYASSERT (tp_cli_connection_run_request_handles (conn, -1,
83         TP_HANDLE_TYPE_CONTACT, ids, &handles, &error, NULL), "");
84     g_assert_no_error (error);
85 
86     g_array_unref (handles);
87   }
88 
89   /* The bug was in cleaning up handle refs when the CM fell off the bus.
90    * Emitting "destroy" on the proxy simulates the CM falling off the bus.
91    */
92   proxy = tp_proxy_borrow_interface_by_id ((TpProxy *) conn,
93       TP_IFACE_QUARK_CONNECTION, &error);
94   g_assert_no_error (error);
95   g_signal_emit_by_name (proxy, "destroy");
96 
97   g_idle_add_full (G_PRIORITY_LOW, no_more_idling_around, mainloop, NULL);
98 
99   g_main_loop_run (mainloop);
100 
101   g_object_unref (conn);
102 
103   /* Make a new connection proxy so that we can call Disconnect() on the
104    * connection.
105    */
106   conn = tp_connection_new (dbus, name, conn_path, &error);
107   MYASSERT (conn != NULL, "");
108   g_assert_no_error (error);
109   MYASSERT (tp_connection_run_until_ready (conn, TRUE, &error, NULL), "");
110   g_assert_no_error (error);
111 
112   tp_tests_connection_assert_disconnect_succeeds (conn);
113   g_object_unref (conn);
114 
115   service_conn_as_base = NULL;
116   g_object_unref (service_conn);
117   g_free (name);
118   g_free (conn_path);
119 
120   g_object_unref (dbus);
121   g_main_loop_unref (mainloop);
122 
123   return 0;
124 }
125