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