1 /* Simple sanity-check for loopback through TCP and Unix sockets.
3 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4 * Copyright © 2010-2011 Nokia Corporation
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib-lowlevel.h>
38 DBusConnection *server_conn;
39 /* queue of DBusMessage */
40 GQueue server_messages;
42 DBusConnection *client_conn;
46 assert_no_error (const DBusError *e)
48 if (G_UNLIKELY (dbus_error_is_set (e)))
49 g_error ("expected success but got error: %s: %s", e->name, e->message);
52 static DBusHandlerResult
53 server_message_cb (DBusConnection *server_conn,
59 g_assert (server_conn == f->server_conn);
60 g_queue_push_tail (&f->server_messages, dbus_message_ref (message));
62 return DBUS_HANDLER_RESULT_HANDLED;
66 new_conn_cb (DBusServer *server,
67 DBusConnection *server_conn,
73 g_assert (f->server_conn == NULL);
74 f->server_conn = dbus_connection_ref (server_conn);
75 dbus_connection_setup_with_g_main (server_conn, NULL);
77 have_mem = dbus_connection_add_filter (server_conn,
78 server_message_cb, f, NULL);
86 dbus_error_init (&f->e);
87 g_queue_init (&f->server_messages);
89 f->server = dbus_server_listen (addr, &f->e);
90 assert_no_error (&f->e);
91 g_assert (f->server != NULL);
93 dbus_server_set_new_connection_function (f->server,
94 new_conn_cb, f, NULL);
95 dbus_server_setup_with_g_main (f->server, NULL);
99 test_connect (Fixture *f,
100 gconstpointer addr G_GNUC_UNUSED)
102 g_assert (f->server_conn == NULL);
104 f->client_conn = dbus_connection_open_private (
105 dbus_server_get_address (f->server), &f->e);
106 assert_no_error (&f->e);
107 g_assert (f->client_conn != NULL);
108 dbus_connection_setup_with_g_main (f->client_conn, NULL);
110 while (f->server_conn == NULL)
113 g_main_context_iteration (NULL, TRUE);
118 test_message (Fixture *f,
121 dbus_bool_t have_mem;
122 dbus_uint32_t serial;
123 DBusMessage *outgoing, *incoming;
125 test_connect (f, addr);
127 outgoing = dbus_message_new_signal ("/com/example/Hello",
128 "com.example.Hello", "Greeting");
129 g_assert (outgoing != NULL);
131 have_mem = dbus_connection_send (f->client_conn, outgoing, &serial);
133 g_assert (serial != 0);
135 while (g_queue_is_empty (&f->server_messages))
138 g_main_context_iteration (NULL, TRUE);
141 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
143 incoming = g_queue_pop_head (&f->server_messages);
145 g_assert (!dbus_message_contains_unix_fds (incoming));
146 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
147 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
148 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
149 "com.example.Hello");
150 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
151 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
152 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
153 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
154 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
156 dbus_message_unref (incoming);
158 dbus_message_unref (outgoing);
162 teardown (Fixture *f,
163 gconstpointer addr G_GNUC_UNUSED)
165 if (f->client_conn != NULL)
167 dbus_connection_close (f->client_conn);
168 dbus_connection_unref (f->client_conn);
169 f->client_conn = NULL;
172 if (f->server_conn != NULL)
174 dbus_connection_close (f->server_conn);
175 dbus_connection_unref (f->server_conn);
176 f->server_conn = NULL;
179 if (f->server != NULL)
181 dbus_server_disconnect (f->server);
182 dbus_server_unref (f->server);
191 g_test_init (&argc, &argv, NULL);
193 g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
194 test_connect, teardown);
195 g_test_add ("/message/tcp", Fixture, "tcp:host=127.0.0.1", setup,
196 test_message, teardown);
198 g_test_add ("/connect/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
199 test_connect, teardown);
200 g_test_add ("/message/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
201 test_message, teardown);
204 g_test_add ("/connect/unix", Fixture, "unix:tmpdir=/tmp", setup,
205 test_connect, teardown);
206 g_test_add ("/message/unix", Fixture, "unix:tmpdir=/tmp", setup,
207 test_message, teardown);
210 return g_test_run ();