tests/gsubprocess: Only compare basename of /tmp
[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
29 #include "gdbus-tests.h"
30
31 /* all tests rely on a shared mainloop */
32 static GMainLoop *loop = NULL;
33
34 /* ---------------------------------------------------------------------------------------------------- */
35
36 static void
37 test_connection_flush_signal_handler (GDBusConnection  *connection,
38                                       const gchar      *sender_name,
39                                       const gchar      *object_path,
40                                       const gchar      *interface_name,
41                                       const gchar      *signal_name,
42                                       GVariant         *parameters,
43                                       gpointer         user_data)
44 {
45   g_main_loop_quit (loop);
46 }
47
48 static gboolean
49 test_connection_flush_on_timeout (gpointer user_data)
50 {
51   guint iteration = GPOINTER_TO_UINT (user_data);
52   g_printerr ("Timeout waiting 1000 msec on iteration %d\n", iteration);
53   g_assert_not_reached ();
54   return FALSE;
55 }
56
57 static void
58 test_connection_flush (void)
59 {
60   GDBusConnection *connection;
61   GError *error;
62   guint n;
63   guint signal_handler_id;
64   const gchar *flush_helper;
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   flush_helper = g_test_get_filename (G_TEST_BUILT, "gdbus-connection-flush-helper", NULL);
86   for (n = 0; n < 50; n++)
87     {
88       gboolean ret;
89       gint exit_status;
90       guint timeout_mainloop_id;
91
92       error = NULL;
93       ret = g_spawn_command_line_sync (flush_helper,
94                                        NULL, /* stdout */
95                                        NULL, /* stderr */
96                                        &exit_status,
97                                        &error);
98       g_assert_no_error (error);
99       g_spawn_check_exit_status (exit_status, &error);
100       g_assert_no_error (error);
101       g_assert (ret);
102
103       timeout_mainloop_id = g_timeout_add (1000, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
104       g_main_loop_run (loop);
105       g_source_remove (timeout_mainloop_id);
106     }
107
108   g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
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 /* the test will fail if the service name has not appeared after this amount of seconds */
121 #define LARGE_MESSAGE_TIMEOUT_SECONDS 10
122
123 static gboolean
124 large_message_timeout_cb (gpointer data)
125 {
126   (void)data;
127
128   g_error ("Error: timeout waiting for dbus name to appear\n");
129
130   return FALSE;
131 }
132
133 static void
134 large_message_on_name_appeared (GDBusConnection *connection,
135                                 const gchar *name,
136                                 const gchar *name_owner,
137                                 gpointer user_data)
138 {
139   GError *error;
140   gchar *request;
141   const gchar *reply;
142   GVariant *result;
143   guint n;
144
145   g_assert (g_source_remove (GPOINTER_TO_UINT (user_data)));
146
147   request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
148   for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
149     request[n] = '0' + (n%10);
150   request[n] = '\0';
151
152   error = NULL;
153   result = g_dbus_connection_call_sync (connection,
154                                         "com.example.TestService",      /* bus name */
155                                         "/com/example/TestObject",      /* object path */
156                                         "com.example.Frob",             /* interface name */
157                                         "HelloWorld",                   /* method name */
158                                         g_variant_new ("(s)", request), /* parameters */
159                                         G_VARIANT_TYPE ("(s)"),         /* return type */
160                                         G_DBUS_CALL_FLAGS_NONE,
161                                         G_MAXINT,
162                                         NULL,
163                                         &error);
164   g_assert_no_error (error);
165   g_assert (result != NULL);
166   g_variant_get (result, "(&s)", &reply);
167   g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
168   g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
169   g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
170   g_variant_unref (result);
171
172   g_free (request);
173
174   g_main_loop_quit (loop);
175 }
176
177 static void
178 large_message_on_name_vanished (GDBusConnection *connection,
179                                 const gchar *name,
180                                 gpointer user_data)
181 {
182 }
183
184 static void
185 test_connection_large_message (void)
186 {
187   guint watcher_id;
188   guint timeout_id;
189
190   session_bus_up ();
191
192   /* this is safe; testserver will exit once the bus goes away */
193   g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
194
195   timeout_id = g_timeout_add_seconds (LARGE_MESSAGE_TIMEOUT_SECONDS,
196                                       large_message_timeout_cb,
197                                       NULL);
198
199   watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
200                                  "com.example.TestService",
201                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
202                                  large_message_on_name_appeared,
203                                  large_message_on_name_vanished,
204                                  GUINT_TO_POINTER (timeout_id),  /* user_data */
205                                  NULL); /* GDestroyNotify */
206   g_main_loop_run (loop);
207   g_bus_unwatch_name (watcher_id);
208
209   session_bus_down ();
210 }
211
212 /* ---------------------------------------------------------------------------------------------------- */
213
214 int
215 main (int   argc,
216       char *argv[])
217 {
218   gint ret;
219
220   g_test_init (&argc, &argv, NULL);
221
222   /* all the tests rely on a shared main loop */
223   loop = g_main_loop_new (NULL, FALSE);
224
225   g_test_dbus_unset ();
226
227   g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
228   g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
229
230   ret = g_test_run();
231   g_main_loop_unref (loop);
232   return ret;
233 }