Revert "Add GTestDBus object"
[platform/upstream/glib.git] / gio / tests / gdbus-connection-slow.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include <sys/types.h>
28 #include <sys/wait.h>
29
30 #include "gdbus-tests.h"
31
32 /* all tests rely on a shared mainloop */
33 static GMainLoop *loop = NULL;
34
35 /* ---------------------------------------------------------------------------------------------------- */
36
37 static void
38 test_connection_flush_signal_handler (GDBusConnection  *connection,
39                                       const gchar      *sender_name,
40                                       const gchar      *object_path,
41                                       const gchar      *interface_name,
42                                       const gchar      *signal_name,
43                                       GVariant         *parameters,
44                                       gpointer         user_data)
45 {
46   g_main_loop_quit (loop);
47 }
48
49 static gboolean
50 test_connection_flush_on_timeout (gpointer user_data)
51 {
52   guint iteration = GPOINTER_TO_UINT (user_data);
53   g_printerr ("Timeout waiting 1000 msec on iteration %d\n", iteration);
54   g_assert_not_reached ();
55   return FALSE;
56 }
57
58 static void
59 test_connection_flush (void)
60 {
61   GDBusConnection *connection;
62   GError *error;
63   guint n;
64   guint signal_handler_id;
65
66   session_bus_up ();
67
68   error = NULL;
69   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
70   g_assert_no_error (error);
71   g_assert (connection != NULL);
72
73   signal_handler_id = g_dbus_connection_signal_subscribe (connection,
74                                                           NULL, /* sender */
75                                                           "org.gtk.GDBus.FlushInterface",
76                                                           "SomeSignal",
77                                                           "/org/gtk/GDBus/FlushObject",
78                                                           NULL,
79                                                           G_DBUS_SIGNAL_FLAGS_NONE,
80                                                           test_connection_flush_signal_handler,
81                                                           NULL,
82                                                           NULL);
83   g_assert_cmpint (signal_handler_id, !=, 0);
84
85   for (n = 0; n < 50; n++)
86     {
87       gboolean ret;
88       gint exit_status;
89       guint timeout_mainloop_id;
90
91       error = NULL;
92       ret = g_spawn_command_line_sync ("./gdbus-connection-flush-helper",
93                                        NULL, /* stdout */
94                                        NULL, /* stderr */
95                                        &exit_status,
96                                        &error);
97       g_assert_no_error (error);
98       g_assert (WIFEXITED (exit_status));
99       g_assert_cmpint (WEXITSTATUS (exit_status), ==, 0);
100       g_assert (ret);
101
102       timeout_mainloop_id = g_timeout_add (1000, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
103       g_main_loop_run (loop);
104       g_source_remove (timeout_mainloop_id);
105     }
106
107   g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
108   _g_object_wait_for_single_ref (connection);
109   g_object_unref (connection);
110
111   session_bus_down ();
112 }
113
114 /* ---------------------------------------------------------------------------------------------------- */
115
116 /* Message size > 20MiB ... should be enough to make sure the message
117  * is fragmented when shoved across any transport
118  */
119 #define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
120
121 static void
122 large_message_on_name_appeared (GDBusConnection *connection,
123                                 const gchar *name,
124                                 const gchar *name_owner,
125                                 gpointer user_data)
126 {
127   GError *error;
128   gchar *request;
129   const gchar *reply;
130   GVariant *result;
131   guint n;
132
133   request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
134   for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
135     request[n] = '0' + (n%10);
136   request[n] = '\0';
137
138   error = NULL;
139   result = g_dbus_connection_call_sync (connection,
140                                         "com.example.TestService",      /* bus name */
141                                         "/com/example/TestObject",      /* object path */
142                                         "com.example.Frob",             /* interface name */
143                                         "HelloWorld",                   /* method name */
144                                         g_variant_new ("(s)", request), /* parameters */
145                                         G_VARIANT_TYPE ("(s)"),         /* return type */
146                                         G_DBUS_CALL_FLAGS_NONE,
147                                         G_MAXINT,
148                                         NULL,
149                                         &error);
150   g_assert_no_error (error);
151   g_assert (result != NULL);
152   g_variant_get (result, "(&s)", &reply);
153   g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
154   g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
155   g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
156   g_variant_unref (result);
157
158   g_free (request);
159
160   g_main_loop_quit (loop);
161 }
162
163 static void
164 large_message_on_name_vanished (GDBusConnection *connection,
165                                 const gchar *name,
166                                 gpointer user_data)
167 {
168 }
169
170 static void
171 test_connection_large_message (void)
172 {
173   guint watcher_id;
174
175   session_bus_up ();
176
177   /* this is safe; testserver will exit once the bus goes away */
178   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
179
180   watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
181                                  "com.example.TestService",
182                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
183                                  large_message_on_name_appeared,
184                                  large_message_on_name_vanished,
185                                  NULL,  /* user_data */
186                                  NULL); /* GDestroyNotify */
187   g_main_loop_run (loop);
188   g_bus_unwatch_name (watcher_id);
189
190   session_bus_down ();
191 }
192
193 /* ---------------------------------------------------------------------------------------------------- */
194
195 int
196 main (int   argc,
197       char *argv[])
198 {
199   g_type_init ();
200   g_test_init (&argc, &argv, NULL);
201
202   /* all the tests rely on a shared main loop */
203   loop = g_main_loop_new (NULL, FALSE);
204
205   /* all the tests use a session bus with a well-known address that we can bring up and down
206    * using session_bus_up() and session_bus_down().
207    */
208   g_unsetenv ("DISPLAY");
209   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
210
211   g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
212   g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
213   return g_test_run();
214 }