Fix distcheck
[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
65   session_bus_up ();
66
67   error = NULL;
68   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
69   g_assert_no_error (error);
70   g_assert (connection != NULL);
71
72   signal_handler_id = g_dbus_connection_signal_subscribe (connection,
73                                                           NULL, /* sender */
74                                                           "org.gtk.GDBus.FlushInterface",
75                                                           "SomeSignal",
76                                                           "/org/gtk/GDBus/FlushObject",
77                                                           NULL,
78                                                           G_DBUS_SIGNAL_FLAGS_NONE,
79                                                           test_connection_flush_signal_handler,
80                                                           NULL,
81                                                           NULL);
82   g_assert_cmpint (signal_handler_id, !=, 0);
83
84   for (n = 0; n < 50; n++)
85     {
86       gboolean ret;
87       gint exit_status;
88       guint timeout_mainloop_id;
89
90       error = NULL;
91       ret = g_spawn_command_line_sync ("./gdbus-connection-flush-helper",
92                                        NULL, /* stdout */
93                                        NULL, /* stderr */
94                                        &exit_status,
95                                        &error);
96       g_assert_no_error (error);
97       g_spawn_check_exit_status (exit_status, &error);
98       g_assert_no_error (error);
99       g_assert (ret);
100
101       timeout_mainloop_id = g_timeout_add (1000, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
102       g_main_loop_run (loop);
103       g_source_remove (timeout_mainloop_id);
104     }
105
106   g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
107   g_object_unref (connection);
108
109   session_bus_down ();
110 }
111
112 /* ---------------------------------------------------------------------------------------------------- */
113
114 /* Message size > 20MiB ... should be enough to make sure the message
115  * is fragmented when shoved across any transport
116  */
117 #define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
118
119 static void
120 large_message_on_name_appeared (GDBusConnection *connection,
121                                 const gchar *name,
122                                 const gchar *name_owner,
123                                 gpointer user_data)
124 {
125   GError *error;
126   gchar *request;
127   const gchar *reply;
128   GVariant *result;
129   guint n;
130
131   request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
132   for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
133     request[n] = '0' + (n%10);
134   request[n] = '\0';
135
136   error = NULL;
137   result = g_dbus_connection_call_sync (connection,
138                                         "com.example.TestService",      /* bus name */
139                                         "/com/example/TestObject",      /* object path */
140                                         "com.example.Frob",             /* interface name */
141                                         "HelloWorld",                   /* method name */
142                                         g_variant_new ("(s)", request), /* parameters */
143                                         G_VARIANT_TYPE ("(s)"),         /* return type */
144                                         G_DBUS_CALL_FLAGS_NONE,
145                                         G_MAXINT,
146                                         NULL,
147                                         &error);
148   g_assert_no_error (error);
149   g_assert (result != NULL);
150   g_variant_get (result, "(&s)", &reply);
151   g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
152   g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
153   g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
154   g_variant_unref (result);
155
156   g_free (request);
157
158   g_main_loop_quit (loop);
159 }
160
161 static void
162 large_message_on_name_vanished (GDBusConnection *connection,
163                                 const gchar *name,
164                                 gpointer user_data)
165 {
166 }
167
168 static void
169 test_connection_large_message (void)
170 {
171   guint watcher_id;
172
173   session_bus_up ();
174
175   /* this is safe; testserver will exit once the bus goes away */
176   g_assert (g_spawn_command_line_async ("./gdbus-testserver", NULL));
177
178   watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
179                                  "com.example.TestService",
180                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
181                                  large_message_on_name_appeared,
182                                  large_message_on_name_vanished,
183                                  NULL,  /* user_data */
184                                  NULL); /* GDestroyNotify */
185   g_main_loop_run (loop);
186   g_bus_unwatch_name (watcher_id);
187
188   session_bus_down ();
189 }
190
191 /* ---------------------------------------------------------------------------------------------------- */
192
193 int
194 main (int   argc,
195       char *argv[])
196 {
197   g_test_init (&argc, &argv, NULL);
198
199   /* all the tests rely on a shared main loop */
200   loop = g_main_loop_new (NULL, FALSE);
201
202   g_test_dbus_unset ();
203
204   g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
205   g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
206   return g_test_run();
207 }