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