[daemon-fix] Fixed sending daemon match rules for kdbus broadcasts
[platform/upstream/dbus.git] / test / loopback.c
1 /* Simple sanity-check for loopback through TCP and Unix sockets.
2  *
3  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4  * Copyright © 2010-2012 Nokia Corporation
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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
24  * SOFTWARE.
25  */
26
27 #include <config.h>
28
29 #include <glib.h>
30
31 #include <dbus/dbus.h>
32
33 #include <string.h>
34
35 #include "test-utils.h"
36
37 typedef struct {
38     TestMainContext *ctx;
39     DBusError e;
40
41     DBusServer *server;
42     DBusConnection *server_conn;
43     /* queue of DBusMessage */
44     GQueue server_messages;
45
46     DBusConnection *client_conn;
47 } Fixture;
48
49 static void
50 assert_no_error (const DBusError *e)
51 {
52   if (G_UNLIKELY (dbus_error_is_set (e)))
53     g_error ("expected success but got error: %s: %s", e->name, e->message);
54 }
55
56 static DBusHandlerResult
57 server_message_cb (DBusConnection *server_conn,
58     DBusMessage *message,
59     void *data)
60 {
61   Fixture *f = data;
62
63   g_assert (server_conn == f->server_conn);
64   g_queue_push_tail (&f->server_messages, dbus_message_ref (message));
65
66   return DBUS_HANDLER_RESULT_HANDLED;
67 }
68
69 static void
70 new_conn_cb (DBusServer *server,
71     DBusConnection *server_conn,
72     void *data)
73 {
74   Fixture *f = data;
75   dbus_bool_t have_mem;
76
77   g_assert (f->server_conn == NULL);
78   f->server_conn = dbus_connection_ref (server_conn);
79   test_connection_setup (f->ctx, server_conn);
80
81   have_mem = dbus_connection_add_filter (server_conn,
82       server_message_cb, f, NULL);
83   g_assert (have_mem);
84 }
85
86 static void
87 setup (Fixture *f,
88     gconstpointer addr)
89 {
90   f->ctx = test_main_context_get ();
91   dbus_error_init (&f->e);
92   g_queue_init (&f->server_messages);
93
94   f->server = dbus_server_listen (addr, &f->e);
95   assert_no_error (&f->e);
96   g_assert (f->server != NULL);
97
98   dbus_server_set_new_connection_function (f->server,
99       new_conn_cb, f, NULL);
100   test_server_setup (f->ctx, f->server);
101 }
102
103 static void
104 test_connect (Fixture *f,
105     gconstpointer addr G_GNUC_UNUSED)
106 {
107   g_assert (f->server_conn == NULL);
108
109   f->client_conn = dbus_connection_open_private (
110       dbus_server_get_address (f->server), &f->e);
111   assert_no_error (&f->e);
112   g_assert (f->client_conn != NULL);
113   test_connection_setup (f->ctx, f->client_conn);
114
115   while (f->server_conn == NULL)
116     {
117       g_print (".");
118       test_main_context_iterate (f->ctx, TRUE);
119     }
120 }
121
122 static void
123 test_bad_guid (Fixture *f,
124     gconstpointer addr G_GNUC_UNUSED)
125 {
126   DBusMessage *incoming;
127   gchar *address = g_strdup (dbus_server_get_address (f->server));
128   gchar *guid;
129
130   g_test_bug ("39720");
131
132   g_assert (f->server_conn == NULL);
133
134   g_assert (strstr (address, "guid=") != NULL);
135   guid = strstr (address, "guid=");
136   g_assert_cmpuint (strlen (guid), >=, 5 + 32);
137
138   /* Change the first char of the guid to something different */
139   if (guid[5] == '0')
140     guid[5] = 'f';
141   else
142     guid[5] = '0';
143
144   f->client_conn = dbus_connection_open_private (address, &f->e);
145   assert_no_error (&f->e);
146   g_assert (f->client_conn != NULL);
147   test_connection_setup (f->ctx, f->client_conn);
148
149   while (f->server_conn == NULL)
150     {
151       g_print (".");
152       test_main_context_iterate (f->ctx, TRUE);
153     }
154
155   /* We get disconnected */
156
157   while (g_queue_is_empty (&f->server_messages))
158     {
159       g_print (".");
160       test_main_context_iterate (f->ctx, TRUE);
161     }
162
163   g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
164
165   incoming = g_queue_pop_head (&f->server_messages);
166
167   g_assert (!dbus_message_contains_unix_fds (incoming));
168   g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
169   g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
170   g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
171       DBUS_INTERFACE_LOCAL);
172   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
173   g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
174   g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
175   g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL);
176
177   dbus_message_unref (incoming);
178
179   g_free (address);
180 }
181
182 static void
183 test_message (Fixture *f,
184     gconstpointer addr)
185 {
186   dbus_bool_t have_mem;
187   dbus_uint32_t serial;
188   DBusMessage *outgoing, *incoming;
189
190   test_connect (f, addr);
191
192   outgoing = dbus_message_new_signal ("/com/example/Hello",
193       "com.example.Hello", "Greeting");
194   g_assert (outgoing != NULL);
195
196   have_mem = dbus_connection_send (f->client_conn, outgoing, &serial);
197   g_assert (have_mem);
198   g_assert (serial != 0);
199
200   while (g_queue_is_empty (&f->server_messages))
201     {
202       g_print (".");
203       test_main_context_iterate (f->ctx, TRUE);
204     }
205
206   g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
207
208   incoming = g_queue_pop_head (&f->server_messages);
209
210   g_assert (!dbus_message_contains_unix_fds (incoming));
211   g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
212   g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
213   g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
214       "com.example.Hello");
215   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
216   g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
217   g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
218   g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
219   g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
220
221   dbus_message_unref (incoming);
222
223   dbus_message_unref (outgoing);
224 }
225
226 static void
227 teardown (Fixture *f,
228     gconstpointer addr G_GNUC_UNUSED)
229 {
230   if (f->client_conn != NULL)
231     {
232       dbus_connection_close (f->client_conn);
233       dbus_connection_unref (f->client_conn);
234       f->client_conn = NULL;
235     }
236
237   if (f->server_conn != NULL)
238     {
239       dbus_connection_close (f->server_conn);
240       dbus_connection_unref (f->server_conn);
241       f->server_conn = NULL;
242     }
243
244   if (f->server != NULL)
245     {
246       dbus_server_disconnect (f->server);
247       dbus_server_unref (f->server);
248       f->server = NULL;
249     }
250
251   test_main_context_unref (f->ctx);
252 }
253
254 int
255 main (int argc,
256     char **argv)
257 {
258   g_test_init (&argc, &argv, NULL);
259   g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
260
261   g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
262       test_connect, teardown);
263   g_test_add ("/message/tcp", Fixture, "tcp:host=127.0.0.1", setup,
264       test_message, teardown);
265
266   g_test_add ("/connect/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
267       test_connect, teardown);
268   g_test_add ("/message/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
269       test_message, teardown);
270
271 #ifdef DBUS_UNIX
272   g_test_add ("/connect/unix", Fixture, "unix:tmpdir=/tmp", setup,
273       test_connect, teardown);
274   g_test_add ("/message/unix", Fixture, "unix:tmpdir=/tmp", setup,
275       test_message, teardown);
276 #endif
277
278   g_test_add ("/message/bad-guid", Fixture, "tcp:host=127.0.0.1", setup,
279       test_bad_guid, teardown);
280
281   return g_test_run ();
282 }