test_get_dbus_daemon: Take a custom XDG_RUNTIME_DIR as an argument
[platform/upstream/dbus.git] / test / monitor.c
1 /* Integration tests for monitor-mode D-Bus connections
2  *
3  * Copyright © 2010-2011 Nokia Corporation
4  * Copyright © 2015 Collabora Ltd.
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 <string.h>
30
31 #include "test-utils-glib.h"
32
33 typedef struct {
34     const char *config_file;
35     const char * const *match_rules;
36     gboolean care_about_our_names;
37 } Config;
38
39 typedef struct {
40     const Config *config;
41     TestMainContext *ctx;
42     DBusError e;
43     GError *ge;
44
45     gchar *address;
46     GPid daemon_pid;
47
48     DBusConnection *monitor;
49     DBusConnection *sender;
50     DBusConnection *recipient;
51
52     GQueue monitored;
53
54     const char *monitor_name;
55     const char *sender_name;
56     const char *recipient_name;
57
58     DBusConnection *systemd;
59     const char *systemd_name;
60     DBusMessage *systemd_message;
61     DBusConnection *activated;
62     const char *activated_name;
63     DBusMessage *activated_message;
64 } Fixture;
65
66 static const char * const no_match_rules[] = {
67     NULL
68 };
69
70 static const char * const wildcard_match_rules[] = {
71     "",
72     NULL,
73     FALSE
74 };
75
76 static const char * const eavesdrop_match_rules[] = {
77     "eavesdrop=true",
78     NULL,
79     FALSE
80 };
81
82 static const char * const no_eavesdrop_match_rules[] = {
83     "eavesdrop=false",
84     NULL,
85     FALSE
86 };
87
88 static const char * const selective_match_rules[] = {
89     "interface='com.example.Interesting'",
90     "interface='com.example.Fun'",
91     NULL,
92     FALSE
93 };
94
95 static const char * const well_known_destination_match_rules[] = {
96     "destination='com.example.Recipient'",
97     NULL
98 };
99
100 static Config forbidding_config = {
101     "valid-config-files/forbidding.conf",
102     NULL,
103     FALSE
104 };
105
106 static Config wildcard_config = {
107     NULL,
108     wildcard_match_rules,
109     FALSE
110 };
111
112 static Config selective_config = {
113     NULL,
114     selective_match_rules,
115     FALSE
116 };
117
118 static Config well_known_destination_config = {
119     NULL,
120     well_known_destination_match_rules,
121     FALSE
122 };
123
124 static Config no_rules_config = {
125     NULL,
126     no_match_rules,
127     FALSE
128 };
129
130 static Config eavesdrop_config = {
131     NULL,
132     eavesdrop_match_rules,
133     FALSE
134 };
135
136 static Config no_eavesdrop_config = {
137     NULL,
138     no_eavesdrop_match_rules,
139     FALSE
140 };
141
142 #ifdef DBUS_UNIX
143 static Config fake_systemd_config = {
144     "valid-config-files/systemd-activation.conf",
145     NULL,
146     FALSE
147 };
148 #endif
149
150 static Config side_effects_config = {
151     NULL,
152     NULL,
153     TRUE
154 };
155
156 static inline const char *
157 not_null2 (const char *x,
158     const char *fallback)
159 {
160   if (x == NULL)
161     return fallback;
162
163   return x;
164 }
165
166 static inline const char *
167 not_null (const char *x)
168 {
169   return not_null2 (x, "(null)");
170 }
171
172 #define log_message(m) _log_message (m, __FILE__, __LINE__)
173
174 G_GNUC_UNUSED
175 static void
176 _log_message (DBusMessage *m,
177     const char *file,
178     int line)
179 {
180   g_test_message ("%s:%d: message type %d (%s)", file, line,
181       dbus_message_get_type (m),
182       dbus_message_type_to_string (dbus_message_get_type (m)));
183   g_test_message ("\tfrom: %s",
184       not_null2 (dbus_message_get_sender (m), "(dbus-daemon)"));
185   g_test_message ("\tto: %s",
186       not_null2 (dbus_message_get_destination (m), "(broadcast)"));
187   g_test_message ("\tpath: %s",
188       not_null (dbus_message_get_path (m)));
189   g_test_message ("\tinterface: %s",
190       not_null (dbus_message_get_interface (m)));
191   g_test_message ("\tmember: %s",
192       not_null (dbus_message_get_member (m)));
193   g_test_message ("\tsignature: %s",
194       not_null (dbus_message_get_signature (m)));
195   g_test_message ("\terror name: %s",
196       not_null (dbus_message_get_error_name (m)));
197
198   if (strcmp ("s", dbus_message_get_signature (m)) == 0)
199     {
200       DBusError e = DBUS_ERROR_INIT;
201       const char *s;
202
203       dbus_message_get_args (m, &e,
204             DBUS_TYPE_STRING, &s,
205             DBUS_TYPE_INVALID);
206       test_assert_no_error (&e);
207       g_test_message ("\tstring payload: %s", s);
208     }
209 }
210
211 /* these are macros so they get the right line number */
212
213 #define assert_hello(m) \
214 do { \
215   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
216       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
217   g_assert_cmpstr (dbus_message_get_destination (m), ==, DBUS_SERVICE_DBUS); \
218   g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
219   g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
220   g_assert_cmpstr (dbus_message_get_member (m), ==, "Hello"); \
221   g_assert_cmpstr (dbus_message_get_signature (m), ==, ""); \
222   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
223   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
224 } while (0)
225
226 #define assert_hello_reply(m) \
227 do { \
228   DBusError _e = DBUS_ERROR_INIT; \
229   const char *_s; \
230     \
231   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
232       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
233   g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
234   g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
235   g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
236   g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
237   g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
238   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
239   g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
240     \
241   dbus_message_get_args (m, &_e, \
242         DBUS_TYPE_STRING, &_s, \
243         DBUS_TYPE_INVALID); \
244   test_assert_no_error (&_e); \
245   g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
246 } while (0)
247
248 #define assert_name_acquired(m) \
249 do { \
250   DBusError _e = DBUS_ERROR_INIT; \
251   const char *_s; \
252     \
253   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
254       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
255   g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
256   g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
257   g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
258   g_assert_cmpstr (dbus_message_get_member (m), ==, "NameAcquired"); \
259   g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
260   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
261   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
262     \
263   dbus_message_get_args (m, &_e, \
264         DBUS_TYPE_STRING, &_s, \
265         DBUS_TYPE_INVALID); \
266   test_assert_no_error (&_e); \
267   g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
268 } while (0)
269
270 #define assert_method_call(m, sender, \
271     destination, path, iface, method, signature) \
272 do { \
273   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
274       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
275   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
276   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
277   g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
278   g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
279   g_assert_cmpstr (dbus_message_get_member (m), ==, method); \
280   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
281   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
282   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
283 } while (0)
284
285 #define assert_signal(m, \
286     sender, path, iface, member, signature, \
287     destination) \
288 do { \
289   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
290       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
291   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
292   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
293   g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
294   g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
295   g_assert_cmpstr (dbus_message_get_member (m), ==, member); \
296   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
297   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
298   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
299 } while (0)
300
301 #define assert_method_reply(m, sender, destination, signature) \
302 do { \
303   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
304       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
305   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
306   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
307   g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
308   g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
309   g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
310   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
311   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
312   g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
313 } while (0)
314
315 #define assert_error_reply(m, sender, destination, error_name) \
316 do { \
317   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
318       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_ERROR)); \
319   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
320   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
321   g_assert_cmpstr (dbus_message_get_error_name (m), ==, error_name); \
322   g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
323   g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
324   g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
325   g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
326   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
327   g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
328 } while (0)
329
330 /* This is called after processing pending replies to our own method
331  * calls, but before anything else.
332  */
333 static DBusHandlerResult
334 monitor_filter (DBusConnection *connection,
335     DBusMessage *message,
336     void *user_data)
337 {
338   Fixture *f = user_data;
339
340   g_assert_cmpstr (dbus_message_get_interface (message), !=,
341       "com.example.Tedious");
342
343   /* we are not interested in the monitor getting NameAcquired or NameLost
344    * for most tests */
345   if (f->config == NULL || !f->config->care_about_our_names)
346     {
347       if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
348             "NameAcquired") ||
349           dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
350             "NameLost"))
351         {
352           DBusError e = DBUS_ERROR_INIT;
353           const char *s;
354
355           dbus_message_get_args (message, &e,
356                 DBUS_TYPE_STRING, &s,
357                 DBUS_TYPE_INVALID);
358           test_assert_no_error (&e);
359
360           if (strcmp (s, f->monitor_name) == 0)
361             {
362               /* ignore */
363               return DBUS_HANDLER_RESULT_HANDLED;
364             }
365         }
366     }
367
368   g_queue_push_tail (&f->monitored, dbus_message_ref (message));
369
370   return DBUS_HANDLER_RESULT_HANDLED;
371 }
372
373 static DBusHandlerResult
374 recipient_filter (DBusConnection *connection,
375     DBusMessage *message,
376     void *user_data)
377 {
378   g_assert_cmpstr (dbus_message_get_interface (message), !=,
379       "com.example.CannotSend");
380   g_assert_cmpstr (dbus_message_get_interface (message), !=,
381       "com.example.CannotReceive");
382
383   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
384 }
385
386 static DBusHandlerResult
387 systemd_filter (DBusConnection *connection,
388     DBusMessage *message,
389     void *user_data)
390 {
391   Fixture *f = user_data;
392
393   if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
394         "NameAcquired") ||
395       dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
396         "NameLost"))
397     {
398       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
399     }
400
401   g_assert (f->systemd_message == NULL);
402   f->systemd_message = dbus_message_ref (message);
403
404   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
405 }
406
407 static DBusHandlerResult
408 activated_filter (DBusConnection *connection,
409     DBusMessage *message,
410     void *user_data)
411 {
412   Fixture *f = user_data;
413
414   if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
415         "NameAcquired") ||
416       dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
417         "NameLost"))
418     {
419       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
420     }
421
422   g_assert (f->activated_message == NULL);
423   f->activated_message = dbus_message_ref (message);
424
425   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
426 }
427
428 static void
429 take_well_known_name (Fixture *f,
430     DBusConnection *connection,
431     const char *name)
432 {
433   int ret;
434
435   ret = dbus_bus_request_name (connection, name,
436       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
437   test_assert_no_error (&f->e);
438   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
439 }
440
441 static void
442 setup (Fixture *f,
443     gconstpointer context)
444 {
445   f->config = context;
446
447   f->ctx = test_main_context_get ();
448
449   f->ge = NULL;
450   dbus_error_init (&f->e);
451
452   f->address = test_get_dbus_daemon (f->config ? f->config->config_file : NULL,
453       TEST_USER_ME, NULL, &f->daemon_pid);
454
455   if (f->address == NULL)
456     return;
457
458   f->monitor = test_connect_to_bus (f->ctx, f->address);
459   f->monitor_name = dbus_bus_get_unique_name (f->monitor);
460   f->sender = test_connect_to_bus (f->ctx, f->address);
461   f->sender_name = dbus_bus_get_unique_name (f->sender);
462   f->recipient = test_connect_to_bus (f->ctx, f->address);
463   f->recipient_name = dbus_bus_get_unique_name (f->recipient);
464
465   if (!dbus_connection_add_filter (f->monitor, monitor_filter, f, NULL))
466     g_error ("OOM");
467
468   if (!dbus_connection_add_filter (f->recipient, recipient_filter, f, NULL))
469     g_error ("OOM");
470 }
471
472 static void
473 become_monitor (Fixture *f,
474     const Config *config)
475 {
476   DBusMessage *m;
477   DBusPendingCall *pc;
478   dbus_bool_t ok;
479   DBusMessageIter appender, array_appender;
480   const char * const *match_rules;
481   int i;
482   dbus_uint32_t zero = 0;
483
484   dbus_connection_set_route_peer_messages (f->monitor, TRUE);
485
486   if (config == NULL)
487     config = f->config;
488
489   if (config != NULL && config->match_rules != NULL)
490     match_rules = config->match_rules;
491   else
492     match_rules = wildcard_match_rules;
493
494   m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
495       DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
496
497   if (m == NULL)
498     g_error ("OOM");
499
500   dbus_message_iter_init_append (m, &appender);
501
502   if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
503         &array_appender))
504     g_error ("OOM");
505
506   for (i = 0; match_rules[i] != NULL; i++)
507     {
508       if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
509             &match_rules[i]))
510         g_error ("OOM");
511     }
512
513   if (!dbus_message_iter_close_container (&appender, &array_appender) ||
514       !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
515     g_error ("OOM");
516
517   if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
518         DBUS_TIMEOUT_USE_DEFAULT) ||
519       pc == NULL)
520     g_error ("OOM");
521
522   dbus_message_unref (m);
523   m = NULL;
524
525   if (dbus_pending_call_get_completed (pc))
526     test_pending_call_store_reply (pc, &m);
527   else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
528         &m, NULL))
529     g_error ("OOM");
530
531   while (m == NULL)
532     test_main_context_iterate (f->ctx, TRUE);
533
534   ok = dbus_message_get_args (m, &f->e,
535       DBUS_TYPE_INVALID);
536   test_assert_no_error (&f->e);
537   g_assert (ok);
538
539   dbus_pending_call_unref (pc);
540   dbus_message_unref (m);
541   m = NULL;
542 }
543
544 /*
545  * Test what happens if the method call arguments are invalid.
546  */
547 static void
548 test_invalid (Fixture *f,
549     gconstpointer context)
550 {
551   DBusMessage *m;
552   DBusPendingCall *pc;
553   dbus_bool_t ok;
554   DBusMessageIter appender, array_appender;
555   dbus_uint32_t zero = 0;
556   dbus_uint32_t invalid_flags = G_MAXUINT32;
557   const char *s;
558
559   if (f->address == NULL)
560     return;
561
562   dbus_connection_set_route_peer_messages (f->monitor, TRUE);
563
564   /* Try to become a monitor but specify nonzero flags - not allowed */
565
566   m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
567       DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
568
569   if (m == NULL)
570     g_error ("OOM");
571
572   dbus_message_iter_init_append (m, &appender);
573
574   if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
575         &array_appender))
576     g_error ("OOM");
577
578   if (!dbus_message_iter_close_container (&appender, &array_appender) ||
579       !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32,
580         &invalid_flags))
581     g_error ("OOM");
582
583   if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
584         DBUS_TIMEOUT_USE_DEFAULT) ||
585       pc == NULL)
586     g_error ("OOM");
587
588   dbus_message_unref (m);
589   m = NULL;
590
591   if (dbus_pending_call_get_completed (pc))
592     test_pending_call_store_reply (pc, &m);
593   else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
594         &m, NULL))
595     g_error ("OOM");
596
597   while (m == NULL)
598     test_main_context_iterate (f->ctx, TRUE);
599
600   g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
601   g_assert_cmpstr (dbus_message_get_error_name (m), ==,
602       DBUS_ERROR_INVALID_ARGS);
603
604   /* Try to become a monitor but specify a bad match rule -
605    * also not allowed */
606
607   dbus_pending_call_unref (pc);
608   dbus_message_unref (m);
609
610   m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
611       DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
612
613   if (m == NULL)
614     g_error ("OOM");
615
616   dbus_message_iter_init_append (m, &appender);
617
618   if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
619         &array_appender))
620     g_error ("OOM");
621
622   /* Syntactically incorrect match rule taken from #92298 - was probably
623    * intended to be path='/modules/...'
624    */
625   s = "interface='org.kde.walletd',member='/modules/kwalletd/org.kde.KWallet/walletOpened'";
626
627   if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
628         &s) ||
629       !dbus_message_iter_close_container (&appender, &array_appender) ||
630       !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero) ||
631       !dbus_connection_send_with_reply (f->monitor, m, &pc,
632         DBUS_TIMEOUT_USE_DEFAULT) ||
633       pc == NULL)
634     g_error ("OOM");
635
636   dbus_message_unref (m);
637   m = NULL;
638
639   if (dbus_pending_call_get_completed (pc))
640     test_pending_call_store_reply (pc, &m);
641   else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
642         &m, NULL))
643     g_error ("OOM");
644
645   while (m == NULL)
646     test_main_context_iterate (f->ctx, TRUE);
647
648   g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
649   g_assert_cmpstr (dbus_message_get_error_name (m), ==,
650       DBUS_ERROR_MATCH_RULE_INVALID);
651
652   dbus_pending_call_unref (pc);
653   dbus_message_unref (m);
654
655   /* We did not become a monitor, so we can still call methods. */
656
657   pc = NULL;
658   m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
659       DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetId");
660
661   if (m == NULL)
662     g_error ("OOM");
663
664   if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
665         DBUS_TIMEOUT_USE_DEFAULT) ||
666       pc == NULL)
667     g_error ("OOM");
668
669   dbus_message_unref (m);
670   m = NULL;
671
672   if (dbus_pending_call_get_completed (pc))
673     test_pending_call_store_reply (pc, &m);
674   else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
675         &m, NULL))
676     g_error ("OOM");
677
678   while (m == NULL)
679     test_main_context_iterate (f->ctx, TRUE);
680
681   if (dbus_set_error_from_message (&f->e, m))
682     g_error ("%s: %s", f->e.name, f->e.message);
683
684   ok = dbus_message_get_args (m, &f->e,
685       DBUS_TYPE_STRING, &s,
686       DBUS_TYPE_INVALID);
687   test_assert_no_error (&f->e);
688   g_assert (ok);
689   g_assert_cmpstr (s, !=, NULL);
690   g_assert_cmpstr (s, !=, "");
691
692   dbus_pending_call_unref (pc);
693   dbus_message_unref (m);
694 }
695
696 /*
697  * Test the side-effects of becoming a monitor.
698  */
699 static void
700 test_become_monitor (Fixture *f,
701     gconstpointer context)
702 {
703   DBusMessage *m;
704   int ret;
705   dbus_bool_t got_unique = FALSE, got_a = FALSE, got_b = FALSE, got_c = FALSE;
706   dbus_bool_t lost_unique = FALSE, lost_a = FALSE, lost_b = FALSE, lost_c = FALSE;
707
708   if (f->address == NULL)
709     return;
710
711   ret = dbus_bus_request_name (f->monitor, "com.example.A",
712       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
713   test_assert_no_error (&f->e);
714   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
715
716   ret = dbus_bus_request_name (f->monitor, "com.example.B",
717       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
718   test_assert_no_error (&f->e);
719   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
720
721   ret = dbus_bus_request_name (f->monitor, "com.example.C",
722       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
723   test_assert_no_error (&f->e);
724   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
725
726   while (!got_unique || !got_a || !got_b || !got_c)
727     {
728       if (g_queue_is_empty (&f->monitored))
729         test_main_context_iterate (f->ctx, TRUE);
730
731       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
732         {
733           if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
734                 "NameAcquired"))
735             {
736               const char *name;
737               dbus_bool_t ok = dbus_message_get_args (m, &f->e,
738                   DBUS_TYPE_STRING, &name,
739                   DBUS_TYPE_INVALID);
740
741               g_assert_cmpstr (dbus_message_get_path (m), ==,
742                   DBUS_PATH_DBUS);
743
744               test_assert_no_error (&f->e);
745               g_assert (ok);
746
747               if (g_str_equal (name, f->monitor_name))
748                 {
749                   g_assert (!got_unique);
750                   got_unique = TRUE;
751                 }
752               else if (g_str_equal (name, "com.example.A"))
753                 {
754                   g_assert (!got_a);
755                   got_a = TRUE;
756                 }
757               else if (g_str_equal (name, "com.example.B"))
758                 {
759                   g_assert (!got_b);
760                   got_b = TRUE;
761                 }
762               else
763                 {
764                   g_assert_cmpstr (name, ==, "com.example.C");
765                   g_assert (!got_c);
766                   got_c = TRUE;
767                 }
768             }
769           else
770             {
771               g_error ("unexpected message %s.%s",
772                   dbus_message_get_interface (m),
773                   dbus_message_get_member (m));
774             }
775
776           dbus_message_unref (m);
777         }
778     }
779
780   become_monitor (f, NULL);
781
782   while (!lost_unique || !lost_a || !lost_b || !lost_c)
783     {
784       if (g_queue_is_empty (&f->monitored))
785         test_main_context_iterate (f->ctx, TRUE);
786
787       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
788         {
789           if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
790                 "NameLost"))
791             {
792               const char *name;
793               dbus_bool_t ok = dbus_message_get_args (m, &f->e,
794                   DBUS_TYPE_STRING, &name,
795                   DBUS_TYPE_INVALID);
796
797               test_assert_no_error (&f->e);
798               g_assert (ok);
799
800               if (g_str_equal (name, f->monitor_name))
801                 {
802                   g_assert (!lost_unique);
803                   lost_unique = TRUE;
804                 }
805               else if (g_str_equal (name, "com.example.A"))
806                 {
807                   g_assert (!lost_a);
808                   lost_a = TRUE;
809                 }
810               else if (g_str_equal (name, "com.example.B"))
811                 {
812                   g_assert (!lost_b);
813                   lost_b = TRUE;
814                 }
815               else
816                 {
817                   g_assert_cmpstr (name, ==, "com.example.C");
818                   g_assert (!lost_c);
819                   lost_c = TRUE;
820                 }
821             }
822           else
823             {
824               g_error ("unexpected message %s.%s",
825                   dbus_message_get_interface (m),
826                   dbus_message_get_member (m));
827             }
828
829           dbus_message_unref (m);
830         }
831     }
832
833   /* Calling methods is forbidden; we get disconnected. */
834   dbus_bus_add_match (f->monitor, "", &f->e);
835   g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);
836   g_assert (!dbus_connection_get_is_connected (f->monitor));
837
838   while (TRUE)
839     {
840       if (g_queue_is_empty (&f->monitored))
841         test_main_context_iterate (f->ctx, TRUE);
842
843       /* When we iterate all the connection's messages, we see ourselves
844        * losing all our names, then we're disconnected. */
845       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
846         {
847           if (dbus_message_is_signal (m, DBUS_INTERFACE_LOCAL, "Disconnected"))
848             {
849               dbus_message_unref (m);
850               goto disconnected;
851             }
852           else
853             {
854               g_error ("unexpected message %s.%s",
855                   dbus_message_get_interface (m),
856                   dbus_message_get_member (m));
857             }
858
859           dbus_message_unref (m);
860         }
861     }
862
863 disconnected:
864
865   g_assert (lost_a);
866   g_assert (lost_b);
867   g_assert (lost_c);
868 }
869
870 static void
871 test_broadcast (Fixture *f,
872     gconstpointer context)
873 {
874   DBusMessage *m;
875
876   if (f->address == NULL)
877     return;
878
879   dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
880   test_assert_no_error (&f->e);
881
882   become_monitor (f, NULL);
883
884   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal1");
885   dbus_connection_send (f->sender, m, NULL);
886   dbus_message_unref (m);
887
888   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal2");
889   dbus_connection_send (f->sender, m, NULL);
890   dbus_message_unref (m);
891
892   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal3");
893   dbus_connection_send (f->sender, m, NULL);
894   dbus_message_unref (m);
895
896   while (g_queue_get_length (&f->monitored) < 3)
897     test_main_context_iterate (f->ctx, TRUE);
898
899   m = g_queue_pop_head (&f->monitored);
900   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
901       "BroadcastSignal1", "", NULL);
902   dbus_message_unref (m);
903
904   m = g_queue_pop_head (&f->monitored);
905   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
906       "BroadcastSignal2", "", NULL);
907   dbus_message_unref (m);
908
909   m = g_queue_pop_head (&f->monitored);
910   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
911       "BroadcastSignal3", "", NULL);
912   dbus_message_unref (m);
913
914   m = g_queue_pop_head (&f->monitored);
915   g_assert (m == NULL);
916 }
917
918 static void
919 test_forbidden_broadcast (Fixture *f,
920     gconstpointer context)
921 {
922   DBusMessage *m;
923
924   if (f->address == NULL)
925     return;
926
927   dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
928   test_assert_no_error (&f->e);
929
930   become_monitor (f, NULL);
931
932   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
933       "BroadcastSignal1");
934   dbus_connection_send (f->sender, m, NULL);
935   dbus_message_unref (m);
936
937   m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
938       "BroadcastSignal2");
939   dbus_connection_send (f->sender, m, NULL);
940   dbus_message_unref (m);
941
942   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
943       "BroadcastSignal3");
944   dbus_connection_send (f->sender, m, NULL);
945   dbus_message_unref (m);
946
947   while (g_queue_get_length (&f->monitored) < 6)
948     test_main_context_iterate (f->ctx, TRUE);
949
950   m = g_queue_pop_head (&f->monitored);
951   assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
952       "BroadcastSignal1", "", NULL);
953   dbus_message_unref (m);
954
955   m = g_queue_pop_head (&f->monitored);
956   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
957       DBUS_ERROR_ACCESS_DENIED);
958   dbus_message_unref (m);
959
960   m = g_queue_pop_head (&f->monitored);
961   assert_signal (m, f->sender_name, "/foo", "com.example.CannotReceive",
962       "BroadcastSignal2", "", NULL);
963   dbus_message_unref (m);
964
965   m = g_queue_pop_head (&f->monitored);
966   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
967       DBUS_ERROR_ACCESS_DENIED);
968   dbus_message_unref (m);
969
970   m = g_queue_pop_head (&f->monitored);
971   assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
972       "BroadcastSignal3", "", NULL);
973   dbus_message_unref (m);
974
975   m = g_queue_pop_head (&f->monitored);
976   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
977       DBUS_ERROR_ACCESS_DENIED);
978   dbus_message_unref (m);
979
980   m = g_queue_pop_head (&f->monitored);
981   g_assert (m == NULL);
982 }
983
984 static void
985 test_unicast_signal (Fixture *f,
986     gconstpointer context)
987 {
988   DBusMessage *m;
989
990   if (f->address == NULL)
991     return;
992
993   become_monitor (f, NULL);
994
995   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
996   if (!dbus_message_set_destination (m, f->recipient_name))
997     g_error ("OOM");
998   dbus_connection_send (f->sender, m, NULL);
999   dbus_message_unref (m);
1000
1001   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1002   if (!dbus_message_set_destination (m, f->recipient_name))
1003     g_error ("OOM");
1004   dbus_connection_send (f->sender, m, NULL);
1005   dbus_message_unref (m);
1006
1007   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1008   if (!dbus_message_set_destination (m, f->recipient_name))
1009     g_error ("OOM");
1010   dbus_connection_send (f->sender, m, NULL);
1011   dbus_message_unref (m);
1012
1013   while (g_queue_get_length (&f->monitored) < 3)
1014     test_main_context_iterate (f->ctx, TRUE);
1015
1016   m = g_queue_pop_head (&f->monitored);
1017   assert_signal (m, f->sender_name, "/foo",
1018       "com.example.bar", "UnicastSignal1", "", f->recipient_name);
1019   dbus_message_unref (m);
1020
1021   m = g_queue_pop_head (&f->monitored);
1022   assert_signal (m, f->sender_name, "/foo",
1023       "com.example.bar", "UnicastSignal2", "", f->recipient_name);
1024   dbus_message_unref (m);
1025
1026   m = g_queue_pop_head (&f->monitored);
1027   assert_signal (m, f->sender_name, "/foo",
1028       "com.example.bar", "UnicastSignal3", "", f->recipient_name);
1029   dbus_message_unref (m);
1030
1031   m = g_queue_pop_head (&f->monitored);
1032   g_assert (m == NULL);
1033 }
1034
1035 static void
1036 test_forbidden (Fixture *f,
1037     gconstpointer context)
1038 {
1039   DBusMessage *m;
1040
1041   if (f->address == NULL)
1042     return;
1043
1044   become_monitor (f, NULL);
1045
1046   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1047       "UnicastSignal1");
1048   if (!dbus_message_set_destination (m, f->recipient_name))
1049     g_error ("OOM");
1050   dbus_connection_send (f->sender, m, NULL);
1051   dbus_message_unref (m);
1052
1053   m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
1054       "UnicastSignal2");
1055   if (!dbus_message_set_destination (m, f->recipient_name))
1056     g_error ("OOM");
1057   dbus_connection_send (f->sender, m, NULL);
1058   dbus_message_unref (m);
1059
1060   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1061       "UnicastSignal3");
1062   if (!dbus_message_set_destination (m, f->recipient_name))
1063     g_error ("OOM");
1064   dbus_connection_send (f->sender, m, NULL);
1065   dbus_message_unref (m);
1066
1067   while (g_queue_get_length (&f->monitored) < 6)
1068     test_main_context_iterate (f->ctx, TRUE);
1069
1070   m = g_queue_pop_head (&f->monitored);
1071   assert_signal (m, f->sender_name, "/foo",
1072       "com.example.CannotSend", "UnicastSignal1", "", f->recipient_name);
1073   dbus_message_unref (m);
1074
1075   m = g_queue_pop_head (&f->monitored);
1076   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1077       DBUS_ERROR_ACCESS_DENIED);
1078   dbus_message_unref (m);
1079
1080   m = g_queue_pop_head (&f->monitored);
1081   assert_signal (m, f->sender_name, "/foo",
1082       "com.example.CannotReceive", "UnicastSignal2", "", f->recipient_name);
1083   dbus_message_unref (m);
1084
1085   m = g_queue_pop_head (&f->monitored);
1086   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1087       DBUS_ERROR_ACCESS_DENIED);
1088   dbus_message_unref (m);
1089
1090   m = g_queue_pop_head (&f->monitored);
1091   assert_signal (m, f->sender_name, "/foo",
1092       "com.example.CannotSend", "UnicastSignal3", "", f->recipient_name);
1093   dbus_message_unref (m);
1094
1095   m = g_queue_pop_head (&f->monitored);
1096   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1097       DBUS_ERROR_ACCESS_DENIED);
1098   dbus_message_unref (m);
1099
1100   m = g_queue_pop_head (&f->monitored);
1101   g_assert (m == NULL);
1102 }
1103
1104 static void
1105 test_method_call (Fixture *f,
1106     gconstpointer context)
1107 {
1108   DBusMessage *m;
1109
1110   if (f->address == NULL)
1111     return;
1112
1113   become_monitor (f, NULL);
1114
1115   /* regression test for
1116    * https://bugs.freedesktop.org/show_bug.cgi?id=90952 */
1117   m = dbus_message_new_method_call (f->recipient_name, "/foo",
1118       DBUS_INTERFACE_PEER, "Ping");
1119   dbus_connection_send (f->sender, m, NULL);
1120   dbus_message_unref (m);
1121
1122   while (g_queue_get_length (&f->monitored) < 2)
1123     test_main_context_iterate (f->ctx, TRUE);
1124
1125   m = g_queue_pop_head (&f->monitored);
1126   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1127       DBUS_INTERFACE_PEER, "Ping", "");
1128   dbus_message_unref (m);
1129
1130   m = g_queue_pop_head (&f->monitored);
1131   assert_method_reply (m, f->recipient_name, f->sender_name, "");
1132   dbus_message_unref (m);
1133
1134   m = g_queue_pop_head (&f->monitored);
1135   g_assert (m == NULL);
1136
1137   m = dbus_message_new_method_call (f->recipient_name, "/foo", "com.example.bar",
1138       "Call1");
1139   dbus_connection_send (f->sender, m, NULL);
1140   dbus_message_unref (m);
1141
1142   while (g_queue_get_length (&f->monitored) < 2)
1143     test_main_context_iterate (f->ctx, TRUE);
1144
1145   m = g_queue_pop_head (&f->monitored);
1146   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1147       "com.example.bar", "Call1", "");
1148   dbus_message_unref (m);
1149
1150   m = g_queue_pop_head (&f->monitored);
1151   assert_error_reply (m, f->recipient_name, f->sender_name,
1152       DBUS_ERROR_UNKNOWN_METHOD);
1153   dbus_message_unref (m);
1154
1155   m = g_queue_pop_head (&f->monitored);
1156   g_assert (m == NULL);
1157 }
1158
1159 static void
1160 test_forbidden_method_call (Fixture *f,
1161     gconstpointer context)
1162 {
1163   DBusMessage *m;
1164
1165   if (f->address == NULL)
1166     return;
1167
1168   become_monitor (f, NULL);
1169
1170   m = dbus_message_new_method_call (f->recipient_name, "/foo",
1171       "com.example.CannotSend", "Call1");
1172   dbus_connection_send (f->sender, m, NULL);
1173   dbus_message_unref (m);
1174
1175   while (g_queue_get_length (&f->monitored) < 2)
1176     test_main_context_iterate (f->ctx, TRUE);
1177
1178   m = g_queue_pop_head (&f->monitored);
1179   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1180       "com.example.CannotSend", "Call1", "");
1181   dbus_message_unref (m);
1182
1183   m = g_queue_pop_head (&f->monitored);
1184   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1185       DBUS_ERROR_ACCESS_DENIED);
1186   dbus_message_unref (m);
1187
1188   m = g_queue_pop_head (&f->monitored);
1189   g_assert (m == NULL);
1190
1191   m = dbus_message_new_method_call (f->recipient_name, "/foo",
1192       "com.example.CannotReceive", "Call2");
1193   dbus_connection_send (f->sender, m, NULL);
1194   dbus_message_unref (m);
1195
1196   while (g_queue_get_length (&f->monitored) < 2)
1197     test_main_context_iterate (f->ctx, TRUE);
1198
1199   m = g_queue_pop_head (&f->monitored);
1200   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1201       "com.example.CannotReceive", "Call2", "");
1202   dbus_message_unref (m);
1203
1204   m = g_queue_pop_head (&f->monitored);
1205   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1206       DBUS_ERROR_ACCESS_DENIED);
1207   dbus_message_unref (m);
1208
1209   m = g_queue_pop_head (&f->monitored);
1210   g_assert (m == NULL);
1211 }
1212
1213 static void
1214 test_dbus_daemon (Fixture *f,
1215     gconstpointer context)
1216 {
1217   DBusMessage *m;
1218   int res;
1219
1220   if (f->address == NULL)
1221     return;
1222
1223   become_monitor (f, NULL);
1224
1225   res = dbus_bus_request_name (f->sender, "com.example.Sender",
1226       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1227   test_assert_no_error (&f->e);
1228   g_assert_cmpint (res, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1229
1230   res = dbus_bus_release_name (f->sender, "com.example.Sender", &f->e);
1231   test_assert_no_error (&f->e);
1232   g_assert_cmpint (res, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
1233
1234   while (g_queue_get_length (&f->monitored) < 8)
1235     test_main_context_iterate (f->ctx, TRUE);
1236
1237   m = g_queue_pop_head (&f->monitored);
1238   assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1239       DBUS_INTERFACE_DBUS, "RequestName", "su");
1240   dbus_message_unref (m);
1241
1242   m = g_queue_pop_head (&f->monitored);
1243   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1244       "NameOwnerChanged", "sss", NULL);
1245   dbus_message_unref (m);
1246
1247   /* FIXME: should we get this? */
1248   m = g_queue_pop_head (&f->monitored);
1249   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1250       "NameAcquired", "s", f->sender_name);
1251   dbus_message_unref (m);
1252
1253   m = g_queue_pop_head (&f->monitored);
1254   assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1255   dbus_message_unref (m);
1256
1257   m = g_queue_pop_head (&f->monitored);
1258   assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1259       DBUS_INTERFACE_DBUS, "ReleaseName", "s");
1260   dbus_message_unref (m);
1261
1262   /* FIXME: should we get this? */
1263   m = g_queue_pop_head (&f->monitored);
1264   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1265       "NameLost", "s", f->sender_name);
1266   dbus_message_unref (m);
1267
1268   m = g_queue_pop_head (&f->monitored);
1269   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1270       "NameOwnerChanged", "sss", NULL);
1271   dbus_message_unref (m);
1272
1273   m = g_queue_pop_head (&f->monitored);
1274   assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1275   dbus_message_unref (m);
1276
1277   m = g_queue_pop_head (&f->monitored);
1278   g_assert (m == NULL);
1279 }
1280
1281 static void
1282 test_selective (Fixture *f,
1283     gconstpointer context)
1284 {
1285   DBusMessage *m;
1286
1287   if (f->address == NULL)
1288     return;
1289
1290   /* Match rules added before becoming a monitor should be cleared:
1291    * if they weren't, this test would get Interesting twice, then Tedious,
1292    * and only see Fun after that. */
1293   dbus_bus_add_match (f->monitor,
1294       "eavesdrop='true',interface='com.example.Interesting'", &f->e);
1295   test_assert_no_error (&f->e);
1296   dbus_bus_add_match (f->monitor,
1297       "eavesdrop='true',interface='com.example.Tedious'", &f->e);
1298   test_assert_no_error (&f->e);
1299
1300   become_monitor (f, NULL);
1301
1302   m = dbus_message_new_signal ("/foo", "com.example.Interesting",
1303       "UnicastSignal1");
1304   if (!dbus_message_set_destination (m, f->recipient_name))
1305     g_error ("OOM");
1306   dbus_connection_send (f->sender, m, NULL);
1307   dbus_message_unref (m);
1308
1309   m = dbus_message_new_signal ("/foo", "com.example.Tedious",
1310       "UnicastSignal2");
1311   if (!dbus_message_set_destination (m, f->recipient_name))
1312     g_error ("OOM");
1313   dbus_connection_send (f->sender, m, NULL);
1314   dbus_message_unref (m);
1315
1316   m = dbus_message_new_signal ("/foo", "com.example.Fun",
1317       "UnicastSignal3");
1318   if (!dbus_message_set_destination (m, f->recipient_name))
1319     g_error ("OOM");
1320   dbus_connection_send (f->sender, m, NULL);
1321   dbus_message_unref (m);
1322
1323   while (g_queue_get_length (&f->monitored) < 2)
1324     test_main_context_iterate (f->ctx, TRUE);
1325
1326   /* We get the interesting signal and the fun signal, but not the tedious
1327    * signal. */
1328
1329   m = g_queue_pop_head (&f->monitored);
1330   assert_signal (m, f->sender_name, "/foo",
1331       "com.example.Interesting", "UnicastSignal1", "", f->recipient_name);
1332   dbus_message_unref (m);
1333
1334   m = g_queue_pop_head (&f->monitored);
1335   assert_signal (m, f->sender_name, "/foo",
1336       "com.example.Fun", "UnicastSignal3", "", f->recipient_name);
1337   dbus_message_unref (m);
1338
1339   m = g_queue_pop_head (&f->monitored);
1340   g_assert (m == NULL);
1341 }
1342
1343 static void
1344 test_well_known_destination (Fixture *f,
1345     gconstpointer context)
1346 {
1347   DBusMessage *m;
1348
1349   if (f->address == NULL)
1350     return;
1351
1352   take_well_known_name (f, f->recipient, "com.example.Recipient");
1353   /* we don't expect_take_well_known_name here because the
1354    * monitor isn't up yet */
1355
1356   become_monitor (f, NULL);
1357
1358   /* The sender sends a message to itself. It will not be observed. */
1359   m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1360   if (!dbus_message_set_destination (m, f->sender_name))
1361     g_error ("OOM");
1362   dbus_connection_send (f->sender, m, NULL);
1363   dbus_message_unref (m);
1364
1365   /* The sender sends a message to the recipient by well-known name.
1366    * It will be observed. */
1367   m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1368   if (!dbus_message_set_destination (m, "com.example.Recipient"))
1369     g_error ("OOM");
1370   dbus_connection_send (f->sender, m, NULL);
1371   dbus_message_unref (m);
1372
1373   /* The sender sends a message to the recipient by unique name.
1374    * It will still be observed. */
1375   m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1376   if (!dbus_message_set_destination (m, f->recipient_name))
1377     g_error ("OOM");
1378   dbus_connection_send (f->sender, m, NULL);
1379   dbus_message_unref (m);
1380
1381   while (g_queue_get_length (&f->monitored) < 2)
1382     test_main_context_iterate (f->ctx, TRUE);
1383
1384   m = g_queue_pop_head (&f->monitored);
1385   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1386       "Observed1", "", "com.example.Recipient");
1387   dbus_message_unref (m);
1388
1389   m = g_queue_pop_head (&f->monitored);
1390   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1391       "Observed2", "", f->recipient_name);
1392   dbus_message_unref (m);
1393
1394   m = g_queue_pop_head (&f->monitored);
1395   g_assert (m == NULL);
1396 }
1397
1398 static void
1399 test_unique_destination (Fixture *f,
1400     gconstpointer context)
1401 {
1402   DBusMessage *m;
1403   Config config = {
1404     NULL,
1405     NULL, /* match rules */
1406     FALSE
1407   };
1408   const gchar *match_rules[2] = { NULL, NULL };
1409   gchar *rule;
1410
1411   if (f->address == NULL)
1412     return;
1413
1414   take_well_known_name (f, f->recipient, "com.example.Recipient");
1415   /* we don't expect_take_well_known_name here because the
1416    * monitor isn't up yet */
1417
1418   rule = g_strdup_printf ("destination='%s'", f->recipient_name);
1419   /* free it later */
1420   g_test_queue_free (rule);
1421   match_rules[0] = rule;
1422   config.match_rules = match_rules;
1423
1424   become_monitor (f, &config);
1425
1426   /* The sender sends a message to itself. It will not be observed. */
1427   m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1428   if (!dbus_message_set_destination (m, f->sender_name))
1429     g_error ("OOM");
1430   dbus_connection_send (f->sender, m, NULL);
1431   dbus_message_unref (m);
1432
1433   /* The sender sends a message to the recipient by well-known name.
1434    * It will be observed. */
1435   m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1436   if (!dbus_message_set_destination (m, "com.example.Recipient"))
1437     g_error ("OOM");
1438   dbus_connection_send (f->sender, m, NULL);
1439   dbus_message_unref (m);
1440
1441   /* The sender sends a message to the recipient by unique name.
1442    * It will still be observed. */
1443   m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1444   if (!dbus_message_set_destination (m, f->recipient_name))
1445     g_error ("OOM");
1446   dbus_connection_send (f->sender, m, NULL);
1447   dbus_message_unref (m);
1448
1449   while (g_queue_get_length (&f->monitored) < 2)
1450     test_main_context_iterate (f->ctx, TRUE);
1451
1452   m = g_queue_pop_head (&f->monitored);
1453   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1454       "Observed1", "", "com.example.Recipient");
1455   dbus_message_unref (m);
1456
1457   m = g_queue_pop_head (&f->monitored);
1458   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1459       "Observed2", "", f->recipient_name);
1460   dbus_message_unref (m);
1461
1462   m = g_queue_pop_head (&f->monitored);
1463   g_assert (m == NULL);
1464 }
1465
1466 #ifdef DBUS_UNIX
1467 /* currently only used for the systemd activation test */
1468 static void
1469 expect_new_connection (Fixture *f)
1470 {
1471   DBusMessage *m;
1472
1473   while (g_queue_get_length (&f->monitored) < 4)
1474     test_main_context_iterate (f->ctx, TRUE);
1475
1476   m = g_queue_pop_head (&f->monitored);
1477   assert_hello (m);
1478   dbus_message_unref (m);
1479
1480   m = g_queue_pop_head (&f->monitored);
1481   assert_hello_reply (m);
1482   dbus_message_unref (m);
1483
1484   m = g_queue_pop_head (&f->monitored);
1485   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1486       "NameOwnerChanged", "sss", NULL);
1487   dbus_message_unref (m);
1488
1489   m = g_queue_pop_head (&f->monitored);
1490   assert_name_acquired (m);
1491   dbus_message_unref (m);
1492 }
1493
1494 /* currently only used for the systemd activation test */
1495 static void
1496 expect_take_well_known_name (Fixture *f,
1497     DBusConnection *connection,
1498     const char *name)
1499 {
1500   DBusMessage *m;
1501   const char *connection_name = dbus_bus_get_unique_name (connection);
1502
1503   while (g_queue_get_length (&f->monitored) < 4)
1504     test_main_context_iterate (f->ctx, TRUE);
1505
1506   m = g_queue_pop_head (&f->monitored);
1507   assert_method_call (m, connection_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1508       DBUS_INTERFACE_DBUS, "RequestName", "su");
1509   dbus_message_unref (m);
1510
1511   m = g_queue_pop_head (&f->monitored);
1512   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1513       "NameOwnerChanged", "sss", NULL);
1514   dbus_message_unref (m);
1515
1516   m = g_queue_pop_head (&f->monitored);
1517   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1518       "NameAcquired", "s", connection_name);
1519   dbus_message_unref (m);
1520
1521   m = g_queue_pop_head (&f->monitored);
1522   assert_method_reply (m, DBUS_SERVICE_DBUS, connection_name, "u");
1523   dbus_message_unref (m);
1524 }
1525
1526 static void
1527 test_activation (Fixture *f,
1528     gconstpointer context)
1529 {
1530   DBusMessage *m;
1531
1532   if (f->address == NULL)
1533     return;
1534
1535   become_monitor (f, NULL);
1536
1537   /* The sender sends a message to an activatable service. */
1538   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1539   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
1540     g_error ("OOM");
1541   dbus_connection_send (f->sender, m, NULL);
1542   dbus_message_unref (m);
1543
1544   /* We observe the activation request, and the message that caused it,
1545    * before systemd has even joined the bus. */
1546   while (g_queue_get_length (&f->monitored) < 2)
1547     test_main_context_iterate (f->ctx, TRUE);
1548
1549   m = g_queue_pop_head (&f->monitored);
1550   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1551       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1552       "org.freedesktop.systemd1");
1553   dbus_message_unref (m);
1554   m = g_queue_pop_head (&f->monitored);
1555   assert_signal (m, f->sender_name, "/foo",
1556       "com.example.bar", "UnicastSignal1", "",
1557       "com.example.SystemdActivatable1");
1558   dbus_message_unref (m);
1559
1560   /* The fake systemd connects to the bus. */
1561   f->systemd = test_connect_to_bus (f->ctx, f->address);
1562   if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
1563     g_error ("OOM");
1564   f->systemd_name = dbus_bus_get_unique_name (f->systemd);
1565
1566   expect_new_connection (f);
1567   take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1568   expect_take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1569
1570   /* It gets its activation request. */
1571   while (f->systemd_message == NULL)
1572     test_main_context_iterate (f->ctx, TRUE);
1573
1574   m = f->systemd_message;
1575   f->systemd_message = NULL;
1576   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1577       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1578       "org.freedesktop.systemd1");
1579   dbus_message_unref (m);
1580
1581   /* systemd starts the activatable service. */
1582   f->activated = test_connect_to_bus (f->ctx, f->address);
1583   if (!dbus_connection_add_filter (f->activated, activated_filter,
1584         f, NULL))
1585     g_error ("OOM");
1586   f->activated_name = dbus_bus_get_unique_name (f->activated);
1587
1588   expect_new_connection (f);
1589   take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
1590   expect_take_well_known_name (f, f->activated,
1591       "com.example.SystemdActivatable1");
1592
1593   /* The message is delivered to the activatable service. */
1594   while (f->activated_message == NULL)
1595     test_main_context_iterate (f->ctx, TRUE);
1596
1597   m = f->activated_message;
1598   f->activated_message = NULL;
1599   assert_signal (m, f->sender_name, "/foo",
1600       "com.example.bar", "UnicastSignal1", "",
1601       "com.example.SystemdActivatable1");
1602   dbus_message_unref (m);
1603
1604   /* The sender sends a message to a different activatable service. */
1605   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1606   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
1607     g_error ("OOM");
1608   dbus_connection_send (f->sender, m, NULL);
1609   dbus_message_unref (m);
1610
1611   /* This time systemd is already ready for it. */
1612   while (g_queue_get_length (&f->monitored) < 2 ||
1613       f->systemd_message == NULL)
1614     test_main_context_iterate (f->ctx, TRUE);
1615
1616   m = f->systemd_message;
1617   f->systemd_message = NULL;
1618   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1619       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1620       "org.freedesktop.systemd1");
1621   dbus_message_unref (m);
1622
1623   /* The monitor sees the activation request and the signal that
1624    * prompted it.*/
1625   m = g_queue_pop_head (&f->monitored);
1626   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1627       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1628       "org.freedesktop.systemd1");
1629   dbus_message_unref (m);
1630   m = g_queue_pop_head (&f->monitored);
1631   assert_signal (m, f->sender_name, "/foo",
1632       "com.example.bar", "UnicastSignal2", "",
1633       "com.example.SystemdActivatable2");
1634   dbus_message_unref (m);
1635
1636   /* The activatable service takes its name. Here I'm faking it by using
1637    * an existing connection. */
1638   take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
1639
1640   /* The message is delivered to the activatable service.
1641    * Implementation detail: the monitor sees this happen before it even
1642    * sees that the name request happened, which is pretty odd. */
1643   while (f->activated_message == NULL)
1644     test_main_context_iterate (f->ctx, TRUE);
1645
1646   m = f->activated_message;
1647   f->activated_message = NULL;
1648   assert_signal (m, f->sender_name, "/foo",
1649       "com.example.bar", "UnicastSignal2", "",
1650       "com.example.SystemdActivatable2");
1651   dbus_message_unref (m);
1652
1653   expect_take_well_known_name (f, f->activated,
1654       "com.example.SystemdActivatable2");
1655
1656   /* A third activation. */
1657   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1658   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
1659     g_error ("OOM");
1660   dbus_connection_send (f->sender, m, NULL);
1661   dbus_message_unref (m);
1662
1663   /* Once again, we see the activation request and the reason. */
1664   while (g_queue_get_length (&f->monitored) < 2)
1665     test_main_context_iterate (f->ctx, TRUE);
1666
1667   m = g_queue_pop_head (&f->monitored);
1668   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1669       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1670       "org.freedesktop.systemd1");
1671   dbus_message_unref (m);
1672   m = g_queue_pop_head (&f->monitored);
1673   assert_signal (m, f->sender_name, "/foo",
1674       "com.example.bar", "UnicastSignal3", "",
1675       "com.example.SystemdActivatable3");
1676   dbus_message_unref (m);
1677
1678   /* systemd gets the request too. */
1679   while (f->systemd_message == NULL)
1680     test_main_context_iterate (f->ctx, TRUE);
1681
1682   m = f->systemd_message;
1683   f->systemd_message = NULL;
1684   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1685       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1686       "org.freedesktop.systemd1");
1687   dbus_message_unref (m);
1688
1689   /* This time activation fails */
1690   m = dbus_message_new_signal ("/org/freedesktop/systemd1",
1691       "org.freedesktop.systemd1.Activator", "ActivationFailure");
1692
1693   do
1694     {
1695       const char *unit = "dbus-com.example.SystemdActivatable3.service";
1696       const char *error_name = "com.example.Nope";
1697       const char *error_message = "Computer says no";
1698
1699       if (!dbus_message_append_args (m,
1700             DBUS_TYPE_STRING, &unit,
1701             DBUS_TYPE_STRING, &error_name,
1702             DBUS_TYPE_STRING, &error_message,
1703             DBUS_TYPE_INVALID))
1704         g_error ("OOM");
1705     }
1706   while (0);
1707
1708   if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
1709     g_error ("OOM");
1710   dbus_connection_send (f->systemd, m, NULL);
1711   dbus_message_unref (m);
1712
1713   /* The monitor sees activation fail */
1714
1715   /* Once again, we see the activation request and the reason. */
1716   while (g_queue_get_length (&f->monitored) < 1)
1717     test_main_context_iterate (f->ctx, TRUE);
1718
1719   m = g_queue_pop_head (&f->monitored);
1720   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1721       "com.example.Nope");
1722   dbus_message_unref (m);
1723 }
1724 #endif /* DBUS_UNIX */
1725
1726 static void
1727 teardown (Fixture *f,
1728     gconstpointer context G_GNUC_UNUSED)
1729 {
1730   dbus_error_free (&f->e);
1731   g_clear_error (&f->ge);
1732
1733   if (f->monitor != NULL)
1734     {
1735       dbus_connection_remove_filter (f->monitor, monitor_filter, f);
1736       dbus_connection_close (f->monitor);
1737       dbus_connection_unref (f->monitor);
1738       f->monitor = NULL;
1739     }
1740
1741   if (f->sender != NULL)
1742     {
1743       dbus_connection_close (f->sender);
1744       dbus_connection_unref (f->sender);
1745       f->sender = NULL;
1746     }
1747
1748   if (f->recipient != NULL)
1749     {
1750       dbus_connection_remove_filter (f->recipient, recipient_filter, f);
1751       dbus_connection_close (f->recipient);
1752       dbus_connection_unref (f->recipient);
1753       f->recipient = NULL;
1754     }
1755
1756   if (f->systemd != NULL)
1757     {
1758       dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1759       dbus_connection_close (f->systemd);
1760       dbus_connection_unref (f->systemd);
1761       f->systemd = NULL;
1762     }
1763
1764   if (f->activated != NULL)
1765     {
1766       dbus_connection_remove_filter (f->activated, activated_filter, f);
1767       dbus_connection_close (f->activated);
1768       dbus_connection_unref (f->activated);
1769       f->activated = NULL;
1770     }
1771
1772   test_kill_pid (f->daemon_pid);
1773   g_spawn_close_pid (f->daemon_pid);
1774
1775   test_main_context_unref (f->ctx);
1776
1777   g_queue_foreach (&f->monitored, (GFunc) dbus_message_unref, NULL);
1778   g_queue_clear (&f->monitored);
1779
1780   g_free (f->address);
1781 }
1782
1783 int
1784 main (int argc,
1785     char **argv)
1786 {
1787   test_init (&argc, &argv);
1788
1789   g_test_add ("/monitor/invalid", Fixture, NULL,
1790       setup, test_invalid, teardown);
1791   g_test_add ("/monitor/become", Fixture, &side_effects_config,
1792       setup, test_become_monitor, teardown);
1793   g_test_add ("/monitor/broadcast", Fixture, NULL,
1794       setup, test_broadcast, teardown);
1795   g_test_add ("/monitor/forbidden-broadcast", Fixture, &forbidding_config,
1796       setup, test_forbidden_broadcast, teardown);
1797   g_test_add ("/monitor/unicast-signal", Fixture, NULL,
1798       setup, test_unicast_signal, teardown);
1799   g_test_add ("/monitor/forbidden", Fixture, &forbidding_config,
1800       setup, test_forbidden, teardown);
1801   g_test_add ("/monitor/method-call", Fixture, NULL,
1802       setup, test_method_call, teardown);
1803   g_test_add ("/monitor/forbidden-method", Fixture, &forbidding_config,
1804       setup, test_forbidden_method_call, teardown);
1805   g_test_add ("/monitor/dbus-daemon", Fixture, NULL,
1806       setup, test_dbus_daemon, teardown);
1807   g_test_add ("/monitor/selective", Fixture, &selective_config,
1808       setup, test_selective, teardown);
1809   g_test_add ("/monitor/well-known-destination",
1810       Fixture, &well_known_destination_config,
1811       setup, test_well_known_destination, teardown);
1812   g_test_add ("/monitor/unique-destination",
1813       Fixture, NULL,
1814       setup, test_unique_destination, teardown);
1815   g_test_add ("/monitor/wildcard", Fixture, &wildcard_config,
1816       setup, test_unicast_signal, teardown);
1817   g_test_add ("/monitor/no-rule", Fixture, &no_rules_config,
1818       setup, test_unicast_signal, teardown);
1819   g_test_add ("/monitor/eavesdrop", Fixture, &eavesdrop_config,
1820       setup, test_unicast_signal, teardown);
1821   g_test_add ("/monitor/no-eavesdrop", Fixture, &no_eavesdrop_config,
1822       setup, test_unicast_signal, teardown);
1823
1824 #ifdef DBUS_UNIX
1825   /* this relies on the systemd activation code path */
1826   g_test_add ("/monitor/activation", Fixture, &fake_systemd_config,
1827       setup, test_activation, teardown);
1828 #endif
1829
1830   return g_test_run ();
1831 }