1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
25 #include <sys/types.h>
27 #include "gdbus-tests.h"
29 /* all tests rely on a shared mainloop */
30 static GMainLoop
*loop
= NULL
;
32 /* ---------------------------------------------------------------------------------------------------- */
35 test_connection_flush_signal_handler (GDBusConnection
*connection
,
36 const gchar
*sender_name
,
37 const gchar
*object_path
,
38 const gchar
*interface_name
,
39 const gchar
*signal_name
,
43 g_main_loop_quit (loop
);
47 test_connection_flush_on_timeout (gpointer user_data
)
49 guint iteration
= GPOINTER_TO_UINT (user_data
);
50 g_printerr ("Timeout waiting 1000 msec on iteration %d\n", iteration
);
51 g_assert_not_reached ();
56 test_connection_flush (void)
58 GDBusConnection
*connection
;
61 guint signal_handler_id
;
62 const gchar
*flush_helper
;
67 connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, &error
);
68 g_assert_no_error (error
);
69 g_assert (connection
!= NULL
);
71 signal_handler_id
= g_dbus_connection_signal_subscribe (connection
,
73 "org.gtk.GDBus.FlushInterface",
75 "/org/gtk/GDBus/FlushObject",
77 G_DBUS_SIGNAL_FLAGS_NONE
,
78 test_connection_flush_signal_handler
,
81 g_assert_cmpint (signal_handler_id
, !=, 0);
83 flush_helper
= g_test_get_filename (G_TEST_BUILT
, "gdbus-connection-flush-helper", NULL
);
84 for (n
= 0; n
< 50; n
++)
88 guint timeout_mainloop_id
;
91 ret
= g_spawn_command_line_sync (flush_helper
,
96 g_assert_no_error (error
);
97 g_spawn_check_exit_status (exit_status
, &error
);
98 g_assert_no_error (error
);
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
);
106 g_dbus_connection_signal_unsubscribe (connection
, signal_handler_id
);
107 g_object_unref (connection
);
112 /* ---------------------------------------------------------------------------------------------------- */
114 /* Message size > 20MiB ... should be enough to make sure the message
115 * is fragmented when shoved across any transport
117 #define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
118 /* the test will fail if the service name has not appeared after this amount of seconds */
119 #define LARGE_MESSAGE_TIMEOUT_SECONDS 10
122 large_message_timeout_cb (gpointer data
)
126 g_error ("Error: timeout waiting for dbus name to appear");
132 large_message_on_name_appeared (GDBusConnection
*connection
,
134 const gchar
*name_owner
,
143 g_assert (g_source_remove (GPOINTER_TO_UINT (user_data
)));
145 request
= g_new (gchar
, LARGE_MESSAGE_STRING_LENGTH
+ 1);
146 for (n
= 0; n
< LARGE_MESSAGE_STRING_LENGTH
; n
++)
147 request
[n
] = '0' + (n
%10);
151 result
= g_dbus_connection_call_sync (connection
,
152 "com.example.TestService", /* bus name */
153 "/com/example/TestObject", /* object path */
154 "com.example.Frob", /* interface name */
155 "HelloWorld", /* method name */
156 g_variant_new ("(s)", request
), /* parameters */
157 G_VARIANT_TYPE ("(s)"), /* return type */
158 G_DBUS_CALL_FLAGS_NONE
,
162 g_assert_no_error (error
);
163 g_assert (result
!= NULL
);
164 g_variant_get (result
, "(&s)", &reply
);
165 g_assert_cmpint (strlen (reply
), >, LARGE_MESSAGE_STRING_LENGTH
);
166 g_assert (g_str_has_prefix (reply
, "You greeted me with '01234567890123456789012"));
167 g_assert (g_str_has_suffix (reply
, "6789'. Thanks!"));
168 g_variant_unref (result
);
172 g_main_loop_quit (loop
);
176 large_message_on_name_vanished (GDBusConnection
*connection
,
183 test_connection_large_message (void)
190 /* this is safe; testserver will exit once the bus goes away */
191 g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT
, "gdbus-testserver", NULL
), NULL
));
193 timeout_id
= g_timeout_add_seconds (LARGE_MESSAGE_TIMEOUT_SECONDS
,
194 large_message_timeout_cb
,
197 watcher_id
= g_bus_watch_name (G_BUS_TYPE_SESSION
,
198 "com.example.TestService",
199 G_BUS_NAME_WATCHER_FLAGS_NONE
,
200 large_message_on_name_appeared
,
201 large_message_on_name_vanished
,
202 GUINT_TO_POINTER (timeout_id
), /* user_data */
203 NULL
); /* GDestroyNotify */
204 g_main_loop_run (loop
);
205 g_bus_unwatch_name (watcher_id
);
210 /* ---------------------------------------------------------------------------------------------------- */
218 g_test_init (&argc
, &argv
, NULL
);
220 /* all the tests rely on a shared main loop */
221 loop
= g_main_loop_new (NULL
, FALSE
);
223 g_test_dbus_unset ();
225 g_test_add_func ("/gdbus/connection/flush", test_connection_flush
);
226 g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message
);
229 g_main_loop_unref (loop
);