Add a common test_init() for GLib tests which prevents hanging forever
[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 Config forbidding_config = {
96     "valid-config-files/forbidding.conf",
97     NULL,
98     FALSE
99 };
100
101 static Config wildcard_config = {
102     NULL,
103     wildcard_match_rules,
104     FALSE
105 };
106
107 static Config selective_config = {
108     NULL,
109     selective_match_rules,
110     FALSE
111 };
112
113 static Config no_rules_config = {
114     NULL,
115     no_match_rules,
116     FALSE
117 };
118
119 static Config eavesdrop_config = {
120     NULL,
121     eavesdrop_match_rules,
122     FALSE
123 };
124
125 static Config no_eavesdrop_config = {
126     NULL,
127     no_eavesdrop_match_rules,
128     FALSE
129 };
130
131 static Config fake_systemd_config = {
132     "valid-config-files/systemd-activation.conf",
133     NULL,
134     FALSE
135 };
136
137 static Config side_effects_config = {
138     NULL,
139     NULL,
140     TRUE
141 };
142
143 static inline const char *
144 not_null2 (const char *x,
145     const char *fallback)
146 {
147   if (x == NULL)
148     return fallback;
149
150   return x;
151 }
152
153 static inline const char *
154 not_null (const char *x)
155 {
156   return not_null2 (x, "(null)");
157 }
158
159 #define log_message(m) _log_message (m, __FILE__, __LINE__)
160
161 G_GNUC_UNUSED
162 static void
163 _log_message (DBusMessage *m,
164     const char *file,
165     int line)
166 {
167   g_message ("%s:%d: message type %d (%s)", file, line,
168       dbus_message_get_type (m),
169       dbus_message_type_to_string (dbus_message_get_type (m)));
170   g_message ("\tfrom: %s",
171       not_null2 (dbus_message_get_sender (m), "(dbus-daemon)"));
172   g_message ("\tto: %s",
173       not_null2 (dbus_message_get_destination (m), "(broadcast)"));
174   g_message ("\tpath: %s",
175       not_null (dbus_message_get_path (m)));
176   g_message ("\tinterface: %s",
177       not_null (dbus_message_get_interface (m)));
178   g_message ("\tmember: %s",
179       not_null (dbus_message_get_member (m)));
180   g_message ("\tsignature: %s",
181       not_null (dbus_message_get_signature (m)));
182   g_message ("\terror name: %s",
183       not_null (dbus_message_get_error_name (m)));
184
185   if (strcmp ("s", dbus_message_get_signature (m)) == 0)
186     {
187       DBusError e = DBUS_ERROR_INIT;
188       const char *s;
189
190       dbus_message_get_args (m, &e,
191             DBUS_TYPE_STRING, &s,
192             DBUS_TYPE_INVALID);
193       test_assert_no_error (&e);
194       g_message ("\tstring payload: %s", s);
195     }
196 }
197
198 /* these are macros so they get the right line number */
199
200 #define assert_hello(m) \
201 do { \
202   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
203       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
204   g_assert_cmpstr (dbus_message_get_destination (m), ==, DBUS_SERVICE_DBUS); \
205   g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
206   g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
207   g_assert_cmpstr (dbus_message_get_member (m), ==, "Hello"); \
208   g_assert_cmpstr (dbus_message_get_signature (m), ==, ""); \
209   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
210   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
211 } while (0)
212
213 #define assert_hello_reply(m) \
214 do { \
215   DBusError _e = DBUS_ERROR_INIT; \
216   const char *_s; \
217     \
218   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
219       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
220   g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
221   g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
222   g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
223   g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
224   g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
225   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
226   g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
227     \
228   dbus_message_get_args (m, &_e, \
229         DBUS_TYPE_STRING, &_s, \
230         DBUS_TYPE_INVALID); \
231   test_assert_no_error (&_e); \
232   g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
233 } while (0)
234
235 #define assert_name_acquired(m) \
236 do { \
237   DBusError _e = DBUS_ERROR_INIT; \
238   const char *_s; \
239     \
240   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
241       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
242   g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
243   g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
244   g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
245   g_assert_cmpstr (dbus_message_get_member (m), ==, "NameAcquired"); \
246   g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
247   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
248   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
249     \
250   dbus_message_get_args (m, &_e, \
251         DBUS_TYPE_STRING, &_s, \
252         DBUS_TYPE_INVALID); \
253   test_assert_no_error (&_e); \
254   g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
255 } while (0)
256
257 #define assert_method_call(m, sender, \
258     destination, path, iface, method, signature) \
259 do { \
260   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
261       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
262   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
263   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
264   g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
265   g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
266   g_assert_cmpstr (dbus_message_get_member (m), ==, method); \
267   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
268   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
269   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
270 } while (0)
271
272 #define assert_signal(m, \
273     sender, path, iface, member, signature, \
274     destination) \
275 do { \
276   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
277       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
278   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
279   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
280   g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
281   g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
282   g_assert_cmpstr (dbus_message_get_member (m), ==, member); \
283   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
284   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
285   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
286 } while (0)
287
288 #define assert_method_reply(m, sender, destination, signature) \
289 do { \
290   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
291       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
292   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
293   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
294   g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
295   g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
296   g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
297   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
298   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
299   g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
300 } while (0)
301
302 #define assert_error_reply(m, sender, destination, error_name) \
303 do { \
304   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
305       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_ERROR)); \
306   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
307   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
308   g_assert_cmpstr (dbus_message_get_error_name (m), ==, error_name); \
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), ==, "s"); \
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 /* This is called after processing pending replies to our own method
318  * calls, but before anything else.
319  */
320 static DBusHandlerResult
321 monitor_filter (DBusConnection *connection,
322     DBusMessage *message,
323     void *user_data)
324 {
325   Fixture *f = user_data;
326
327   g_assert_cmpstr (dbus_message_get_interface (message), !=,
328       "com.example.Tedious");
329
330   /* we are not interested in the monitor getting NameAcquired or NameLost
331    * for most tests */
332   if (f->config == NULL || !f->config->care_about_our_names)
333     {
334       if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
335             "NameAcquired") ||
336           dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
337             "NameLost"))
338         {
339           DBusError e = DBUS_ERROR_INIT;
340           const char *s;
341
342           dbus_message_get_args (message, &e,
343                 DBUS_TYPE_STRING, &s,
344                 DBUS_TYPE_INVALID);
345           test_assert_no_error (&e);
346
347           if (strcmp (s, f->monitor_name) == 0)
348             {
349               /* ignore */
350               return DBUS_HANDLER_RESULT_HANDLED;
351             }
352         }
353     }
354
355   g_queue_push_tail (&f->monitored, dbus_message_ref (message));
356
357   return DBUS_HANDLER_RESULT_HANDLED;
358 }
359
360 static DBusHandlerResult
361 recipient_filter (DBusConnection *connection,
362     DBusMessage *message,
363     void *user_data)
364 {
365   g_assert_cmpstr (dbus_message_get_interface (message), !=,
366       "com.example.CannotSend");
367   g_assert_cmpstr (dbus_message_get_interface (message), !=,
368       "com.example.CannotReceive");
369
370   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
371 }
372
373 static DBusHandlerResult
374 systemd_filter (DBusConnection *connection,
375     DBusMessage *message,
376     void *user_data)
377 {
378   Fixture *f = user_data;
379
380   if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
381         "NameAcquired") ||
382       dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
383         "NameLost"))
384     {
385       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
386     }
387
388   g_assert (f->systemd_message == NULL);
389   f->systemd_message = dbus_message_ref (message);
390
391   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
392 }
393
394 static DBusHandlerResult
395 activated_filter (DBusConnection *connection,
396     DBusMessage *message,
397     void *user_data)
398 {
399   Fixture *f = user_data;
400
401   if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
402         "NameAcquired") ||
403       dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
404         "NameLost"))
405     {
406       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
407     }
408
409   g_assert (f->activated_message == NULL);
410   f->activated_message = dbus_message_ref (message);
411
412   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
413 }
414
415 static void
416 setup (Fixture *f,
417     gconstpointer context)
418 {
419   f->config = context;
420
421   f->ctx = test_main_context_get ();
422
423   f->ge = NULL;
424   dbus_error_init (&f->e);
425
426   f->address = test_get_dbus_daemon (f->config ? f->config->config_file : NULL,
427       TEST_USER_ME, &f->daemon_pid);
428
429   if (f->address == NULL)
430     return;
431
432   f->monitor = test_connect_to_bus (f->ctx, f->address);
433   f->monitor_name = dbus_bus_get_unique_name (f->monitor);
434   f->sender = test_connect_to_bus (f->ctx, f->address);
435   f->sender_name = dbus_bus_get_unique_name (f->sender);
436   f->recipient = test_connect_to_bus (f->ctx, f->address);
437   f->recipient_name = dbus_bus_get_unique_name (f->recipient);
438
439   if (!dbus_connection_add_filter (f->monitor, monitor_filter, f, NULL))
440     g_error ("OOM");
441
442   if (!dbus_connection_add_filter (f->recipient, recipient_filter, f, NULL))
443     g_error ("OOM");
444 }
445
446 static void
447 become_monitor (Fixture *f)
448 {
449   DBusMessage *m;
450   DBusPendingCall *pc;
451   dbus_bool_t ok;
452   DBusMessageIter appender, array_appender;
453   const char * const *match_rules;
454   int i;
455   dbus_uint32_t zero = 0;
456
457   if (f->config != NULL && f->config->match_rules != NULL)
458     match_rules = f->config->match_rules;
459   else
460     match_rules = wildcard_match_rules;
461
462   m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
463       DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
464
465   if (m == NULL)
466     g_error ("OOM");
467
468   dbus_message_iter_init_append (m, &appender);
469
470   if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
471         &array_appender))
472     g_error ("OOM");
473
474   for (i = 0; match_rules[i] != NULL; i++)
475     {
476       if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
477             &match_rules[i]))
478         g_error ("OOM");
479     }
480
481   if (!dbus_message_iter_close_container (&appender, &array_appender) ||
482       !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
483     g_error ("OOM");
484
485   if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
486         DBUS_TIMEOUT_USE_DEFAULT) ||
487       pc == NULL)
488     g_error ("OOM");
489
490   dbus_message_unref (m);
491   m = NULL;
492
493   if (dbus_pending_call_get_completed (pc))
494     test_pending_call_store_reply (pc, &m);
495   else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
496         &m, NULL))
497     g_error ("OOM");
498
499   while (m == NULL)
500     test_main_context_iterate (f->ctx, TRUE);
501
502   ok = dbus_message_get_args (m, &f->e,
503       DBUS_TYPE_INVALID);
504   test_assert_no_error (&f->e);
505   g_assert (ok);
506
507   dbus_pending_call_unref (pc);
508   dbus_message_unref (m);
509   m = NULL;
510 }
511
512 /*
513  * Test the side-effects of becoming a monitor.
514  */
515 static void
516 test_become_monitor (Fixture *f,
517     gconstpointer context)
518 {
519   DBusMessage *m;
520   int ret;
521   dbus_bool_t got_unique = FALSE, got_a = FALSE, got_b = FALSE, got_c = FALSE;
522   dbus_bool_t lost_unique = FALSE, lost_a = FALSE, lost_b = FALSE, lost_c = FALSE;
523
524   if (f->address == NULL)
525     return;
526
527   ret = dbus_bus_request_name (f->monitor, "com.example.A",
528       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
529   test_assert_no_error (&f->e);
530   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
531
532   ret = dbus_bus_request_name (f->monitor, "com.example.B",
533       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
534   test_assert_no_error (&f->e);
535   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
536
537   ret = dbus_bus_request_name (f->monitor, "com.example.C",
538       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
539   test_assert_no_error (&f->e);
540   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
541
542   while (!got_unique || !got_a || !got_b || !got_c)
543     {
544       test_main_context_iterate (f->ctx, TRUE);
545
546       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
547         {
548           if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
549                 "NameAcquired"))
550             {
551               const char *name;
552               dbus_bool_t ok = dbus_message_get_args (m, &f->e,
553                   DBUS_TYPE_STRING, &name,
554                   DBUS_TYPE_INVALID);
555
556               g_assert_cmpstr (dbus_message_get_path (m), ==,
557                   DBUS_PATH_DBUS);
558
559               test_assert_no_error (&f->e);
560               g_assert (ok);
561
562               if (g_str_equal (name, f->monitor_name))
563                 {
564                   g_assert (!got_unique);
565                   got_unique = TRUE;
566                 }
567               else if (g_str_equal (name, "com.example.A"))
568                 {
569                   g_assert (!got_a);
570                   got_a = TRUE;
571                 }
572               else if (g_str_equal (name, "com.example.B"))
573                 {
574                   g_assert (!got_b);
575                   got_b = TRUE;
576                 }
577               else
578                 {
579                   g_assert_cmpstr (name, ==, "com.example.C");
580                   g_assert (!got_c);
581                   got_c = TRUE;
582                 }
583             }
584           else
585             {
586               g_error ("unexpected message %s.%s",
587                   dbus_message_get_interface (m),
588                   dbus_message_get_member (m));
589             }
590
591           dbus_message_unref (m);
592         }
593     }
594
595   become_monitor (f);
596
597   while (!lost_unique || !lost_a || !lost_b || !lost_c)
598     {
599       test_main_context_iterate (f->ctx, TRUE);
600
601       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
602         {
603           if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
604                 "NameLost"))
605             {
606               const char *name;
607               dbus_bool_t ok = dbus_message_get_args (m, &f->e,
608                   DBUS_TYPE_STRING, &name,
609                   DBUS_TYPE_INVALID);
610
611               test_assert_no_error (&f->e);
612               g_assert (ok);
613
614               if (g_str_equal (name, f->monitor_name))
615                 {
616                   g_assert (!lost_unique);
617                   lost_unique = TRUE;
618                 }
619               else if (g_str_equal (name, "com.example.A"))
620                 {
621                   g_assert (!lost_a);
622                   lost_a = TRUE;
623                 }
624               else if (g_str_equal (name, "com.example.B"))
625                 {
626                   g_assert (!lost_b);
627                   lost_b = TRUE;
628                 }
629               else
630                 {
631                   g_assert_cmpstr (name, ==, "com.example.C");
632                   g_assert (!lost_c);
633                   lost_c = TRUE;
634                 }
635             }
636           else
637             {
638               g_error ("unexpected message %s.%s",
639                   dbus_message_get_interface (m),
640                   dbus_message_get_member (m));
641             }
642
643           dbus_message_unref (m);
644         }
645     }
646
647   /* Calling methods is forbidden; we get disconnected. */
648   dbus_bus_add_match (f->monitor, "", &f->e);
649   g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);
650   g_assert (!dbus_connection_get_is_connected (f->monitor));
651
652   while (TRUE)
653     {
654       test_main_context_iterate (f->ctx, TRUE);
655
656       /* When we iterate all the connection's messages, we see ourselves
657        * losing all our names, then we're disconnected. */
658       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
659         {
660           if (dbus_message_is_signal (m, DBUS_INTERFACE_LOCAL, "Disconnected"))
661             {
662               dbus_message_unref (m);
663               goto disconnected;
664             }
665           else
666             {
667               g_error ("unexpected message %s.%s",
668                   dbus_message_get_interface (m),
669                   dbus_message_get_member (m));
670             }
671
672           dbus_message_unref (m);
673         }
674     }
675
676 disconnected:
677
678   g_assert (lost_a);
679   g_assert (lost_b);
680   g_assert (lost_c);
681 }
682
683 static void
684 test_broadcast (Fixture *f,
685     gconstpointer context)
686 {
687   DBusMessage *m;
688
689   if (f->address == NULL)
690     return;
691
692   dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
693   test_assert_no_error (&f->e);
694
695   become_monitor (f);
696
697   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal1");
698   dbus_connection_send (f->sender, m, NULL);
699   dbus_message_unref (m);
700
701   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal2");
702   dbus_connection_send (f->sender, m, NULL);
703   dbus_message_unref (m);
704
705   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal3");
706   dbus_connection_send (f->sender, m, NULL);
707   dbus_message_unref (m);
708
709   while (g_queue_get_length (&f->monitored) < 3)
710     test_main_context_iterate (f->ctx, TRUE);
711
712   m = g_queue_pop_head (&f->monitored);
713   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
714       "BroadcastSignal1", "", NULL);
715   dbus_message_unref (m);
716
717   m = g_queue_pop_head (&f->monitored);
718   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
719       "BroadcastSignal2", "", NULL);
720   dbus_message_unref (m);
721
722   m = g_queue_pop_head (&f->monitored);
723   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
724       "BroadcastSignal3", "", NULL);
725   dbus_message_unref (m);
726
727   m = g_queue_pop_head (&f->monitored);
728   g_assert (m == NULL);
729 }
730
731 static void
732 test_forbidden_broadcast (Fixture *f,
733     gconstpointer context)
734 {
735   DBusMessage *m;
736
737   if (f->address == NULL)
738     return;
739
740   dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
741   test_assert_no_error (&f->e);
742
743   become_monitor (f);
744
745   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
746       "BroadcastSignal1");
747   dbus_connection_send (f->sender, m, NULL);
748   dbus_message_unref (m);
749
750   m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
751       "BroadcastSignal2");
752   dbus_connection_send (f->sender, m, NULL);
753   dbus_message_unref (m);
754
755   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
756       "BroadcastSignal3");
757   dbus_connection_send (f->sender, m, NULL);
758   dbus_message_unref (m);
759
760   while (g_queue_get_length (&f->monitored) < 6)
761     test_main_context_iterate (f->ctx, TRUE);
762
763   m = g_queue_pop_head (&f->monitored);
764   assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
765       "BroadcastSignal1", "", NULL);
766   dbus_message_unref (m);
767
768   m = g_queue_pop_head (&f->monitored);
769   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
770       DBUS_ERROR_ACCESS_DENIED);
771   dbus_message_unref (m);
772
773   m = g_queue_pop_head (&f->monitored);
774   assert_signal (m, f->sender_name, "/foo", "com.example.CannotReceive",
775       "BroadcastSignal2", "", NULL);
776   dbus_message_unref (m);
777
778   m = g_queue_pop_head (&f->monitored);
779   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
780       DBUS_ERROR_ACCESS_DENIED);
781   dbus_message_unref (m);
782
783   m = g_queue_pop_head (&f->monitored);
784   assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
785       "BroadcastSignal3", "", NULL);
786   dbus_message_unref (m);
787
788   m = g_queue_pop_head (&f->monitored);
789   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
790       DBUS_ERROR_ACCESS_DENIED);
791   dbus_message_unref (m);
792
793   m = g_queue_pop_head (&f->monitored);
794   g_assert (m == NULL);
795 }
796
797 static void
798 test_unicast_signal (Fixture *f,
799     gconstpointer context)
800 {
801   DBusMessage *m;
802
803   if (f->address == NULL)
804     return;
805
806   become_monitor (f);
807
808   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
809   if (!dbus_message_set_destination (m, f->recipient_name))
810     g_error ("OOM");
811   dbus_connection_send (f->sender, m, NULL);
812   dbus_message_unref (m);
813
814   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
815   if (!dbus_message_set_destination (m, f->recipient_name))
816     g_error ("OOM");
817   dbus_connection_send (f->sender, m, NULL);
818   dbus_message_unref (m);
819
820   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
821   if (!dbus_message_set_destination (m, f->recipient_name))
822     g_error ("OOM");
823   dbus_connection_send (f->sender, m, NULL);
824   dbus_message_unref (m);
825
826   while (g_queue_get_length (&f->monitored) < 3)
827     test_main_context_iterate (f->ctx, TRUE);
828
829   m = g_queue_pop_head (&f->monitored);
830   assert_signal (m, f->sender_name, "/foo",
831       "com.example.bar", "UnicastSignal1", "", f->recipient_name);
832   dbus_message_unref (m);
833
834   m = g_queue_pop_head (&f->monitored);
835   assert_signal (m, f->sender_name, "/foo",
836       "com.example.bar", "UnicastSignal2", "", f->recipient_name);
837   dbus_message_unref (m);
838
839   m = g_queue_pop_head (&f->monitored);
840   assert_signal (m, f->sender_name, "/foo",
841       "com.example.bar", "UnicastSignal3", "", f->recipient_name);
842   dbus_message_unref (m);
843
844   m = g_queue_pop_head (&f->monitored);
845   g_assert (m == NULL);
846 }
847
848 static void
849 test_forbidden (Fixture *f,
850     gconstpointer context)
851 {
852   DBusMessage *m;
853
854   if (f->address == NULL)
855     return;
856
857   become_monitor (f);
858
859   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
860       "UnicastSignal1");
861   if (!dbus_message_set_destination (m, f->recipient_name))
862     g_error ("OOM");
863   dbus_connection_send (f->sender, m, NULL);
864   dbus_message_unref (m);
865
866   m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
867       "UnicastSignal2");
868   if (!dbus_message_set_destination (m, f->recipient_name))
869     g_error ("OOM");
870   dbus_connection_send (f->sender, m, NULL);
871   dbus_message_unref (m);
872
873   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
874       "UnicastSignal3");
875   if (!dbus_message_set_destination (m, f->recipient_name))
876     g_error ("OOM");
877   dbus_connection_send (f->sender, m, NULL);
878   dbus_message_unref (m);
879
880   while (g_queue_get_length (&f->monitored) < 6)
881     test_main_context_iterate (f->ctx, TRUE);
882
883   m = g_queue_pop_head (&f->monitored);
884   assert_signal (m, f->sender_name, "/foo",
885       "com.example.CannotSend", "UnicastSignal1", "", f->recipient_name);
886   dbus_message_unref (m);
887
888   m = g_queue_pop_head (&f->monitored);
889   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
890       DBUS_ERROR_ACCESS_DENIED);
891   dbus_message_unref (m);
892
893   m = g_queue_pop_head (&f->monitored);
894   assert_signal (m, f->sender_name, "/foo",
895       "com.example.CannotReceive", "UnicastSignal2", "", f->recipient_name);
896   dbus_message_unref (m);
897
898   m = g_queue_pop_head (&f->monitored);
899   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
900       DBUS_ERROR_ACCESS_DENIED);
901   dbus_message_unref (m);
902
903   m = g_queue_pop_head (&f->monitored);
904   assert_signal (m, f->sender_name, "/foo",
905       "com.example.CannotSend", "UnicastSignal3", "", f->recipient_name);
906   dbus_message_unref (m);
907
908   m = g_queue_pop_head (&f->monitored);
909   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
910       DBUS_ERROR_ACCESS_DENIED);
911   dbus_message_unref (m);
912
913   m = g_queue_pop_head (&f->monitored);
914   g_assert (m == NULL);
915 }
916
917 static void
918 test_method_call (Fixture *f,
919     gconstpointer context)
920 {
921   DBusMessage *m;
922
923   if (f->address == NULL)
924     return;
925
926   become_monitor (f);
927
928   m = dbus_message_new_method_call (f->recipient_name, "/foo", "com.example.bar",
929       "Call1");
930   dbus_connection_send (f->sender, m, NULL);
931   dbus_message_unref (m);
932
933   while (g_queue_get_length (&f->monitored) < 2)
934     test_main_context_iterate (f->ctx, TRUE);
935
936   m = g_queue_pop_head (&f->monitored);
937   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
938       "com.example.bar", "Call1", "");
939   dbus_message_unref (m);
940
941   m = g_queue_pop_head (&f->monitored);
942   assert_error_reply (m, f->recipient_name, f->sender_name,
943       DBUS_ERROR_UNKNOWN_METHOD);
944   dbus_message_unref (m);
945
946   m = g_queue_pop_head (&f->monitored);
947   g_assert (m == NULL);
948 }
949
950 static void
951 test_forbidden_method_call (Fixture *f,
952     gconstpointer context)
953 {
954   DBusMessage *m;
955
956   if (f->address == NULL)
957     return;
958
959   become_monitor (f);
960
961   m = dbus_message_new_method_call (f->recipient_name, "/foo",
962       "com.example.CannotSend", "Call1");
963   dbus_connection_send (f->sender, m, NULL);
964   dbus_message_unref (m);
965
966   while (g_queue_get_length (&f->monitored) < 2)
967     test_main_context_iterate (f->ctx, TRUE);
968
969   m = g_queue_pop_head (&f->monitored);
970   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
971       "com.example.CannotSend", "Call1", "");
972   dbus_message_unref (m);
973
974   m = g_queue_pop_head (&f->monitored);
975   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
976       DBUS_ERROR_ACCESS_DENIED);
977   dbus_message_unref (m);
978
979   m = g_queue_pop_head (&f->monitored);
980   g_assert (m == NULL);
981
982   m = dbus_message_new_method_call (f->recipient_name, "/foo",
983       "com.example.CannotReceive", "Call2");
984   dbus_connection_send (f->sender, m, NULL);
985   dbus_message_unref (m);
986
987   while (g_queue_get_length (&f->monitored) < 2)
988     test_main_context_iterate (f->ctx, TRUE);
989
990   m = g_queue_pop_head (&f->monitored);
991   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
992       "com.example.CannotReceive", "Call2", "");
993   dbus_message_unref (m);
994
995   m = g_queue_pop_head (&f->monitored);
996   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
997       DBUS_ERROR_ACCESS_DENIED);
998   dbus_message_unref (m);
999
1000   m = g_queue_pop_head (&f->monitored);
1001   g_assert (m == NULL);
1002 }
1003
1004 static void
1005 test_dbus_daemon (Fixture *f,
1006     gconstpointer context)
1007 {
1008   DBusMessage *m;
1009   int res;
1010
1011   if (f->address == NULL)
1012     return;
1013
1014   become_monitor (f);
1015
1016   res = dbus_bus_request_name (f->sender, "com.example.Sender",
1017       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1018   test_assert_no_error (&f->e);
1019   g_assert_cmpint (res, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1020
1021   res = dbus_bus_release_name (f->sender, "com.example.Sender", &f->e);
1022   test_assert_no_error (&f->e);
1023   g_assert_cmpint (res, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
1024
1025   while (g_queue_get_length (&f->monitored) < 8)
1026     test_main_context_iterate (f->ctx, TRUE);
1027
1028   m = g_queue_pop_head (&f->monitored);
1029   assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1030       DBUS_INTERFACE_DBUS, "RequestName", "su");
1031   dbus_message_unref (m);
1032
1033   m = g_queue_pop_head (&f->monitored);
1034   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1035       "NameOwnerChanged", "sss", NULL);
1036   dbus_message_unref (m);
1037
1038   /* FIXME: should we get this? */
1039   m = g_queue_pop_head (&f->monitored);
1040   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1041       "NameAcquired", "s", f->sender_name);
1042   dbus_message_unref (m);
1043
1044   m = g_queue_pop_head (&f->monitored);
1045   assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1046   dbus_message_unref (m);
1047
1048   m = g_queue_pop_head (&f->monitored);
1049   assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1050       DBUS_INTERFACE_DBUS, "ReleaseName", "s");
1051   dbus_message_unref (m);
1052
1053   /* FIXME: should we get this? */
1054   m = g_queue_pop_head (&f->monitored);
1055   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1056       "NameLost", "s", f->sender_name);
1057   dbus_message_unref (m);
1058
1059   m = g_queue_pop_head (&f->monitored);
1060   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1061       "NameOwnerChanged", "sss", NULL);
1062   dbus_message_unref (m);
1063
1064   m = g_queue_pop_head (&f->monitored);
1065   assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1066   dbus_message_unref (m);
1067
1068   m = g_queue_pop_head (&f->monitored);
1069   g_assert (m == NULL);
1070 }
1071
1072 static void
1073 test_selective (Fixture *f,
1074     gconstpointer context)
1075 {
1076   DBusMessage *m;
1077
1078   if (f->address == NULL)
1079     return;
1080
1081   /* Match rules added before becoming a monitor should be cleared:
1082    * if they weren't, this test would get Interesting twice, then Tedious,
1083    * and only see Fun after that. */
1084   dbus_bus_add_match (f->monitor,
1085       "eavesdrop='true',interface='com.example.Interesting'", &f->e);
1086   test_assert_no_error (&f->e);
1087   dbus_bus_add_match (f->monitor,
1088       "eavesdrop='true',interface='com.example.Tedious'", &f->e);
1089   test_assert_no_error (&f->e);
1090
1091   become_monitor (f);
1092
1093   m = dbus_message_new_signal ("/foo", "com.example.Interesting",
1094       "UnicastSignal1");
1095   if (!dbus_message_set_destination (m, f->recipient_name))
1096     g_error ("OOM");
1097   dbus_connection_send (f->sender, m, NULL);
1098   dbus_message_unref (m);
1099
1100   m = dbus_message_new_signal ("/foo", "com.example.Tedious",
1101       "UnicastSignal2");
1102   if (!dbus_message_set_destination (m, f->recipient_name))
1103     g_error ("OOM");
1104   dbus_connection_send (f->sender, m, NULL);
1105   dbus_message_unref (m);
1106
1107   m = dbus_message_new_signal ("/foo", "com.example.Fun",
1108       "UnicastSignal3");
1109   if (!dbus_message_set_destination (m, f->recipient_name))
1110     g_error ("OOM");
1111   dbus_connection_send (f->sender, m, NULL);
1112   dbus_message_unref (m);
1113
1114   while (g_queue_get_length (&f->monitored) < 2)
1115     test_main_context_iterate (f->ctx, TRUE);
1116
1117   /* We get the interesting signal and the fun signal, but not the tedious
1118    * signal. */
1119
1120   m = g_queue_pop_head (&f->monitored);
1121   assert_signal (m, f->sender_name, "/foo",
1122       "com.example.Interesting", "UnicastSignal1", "", f->recipient_name);
1123   dbus_message_unref (m);
1124
1125   m = g_queue_pop_head (&f->monitored);
1126   assert_signal (m, f->sender_name, "/foo",
1127       "com.example.Fun", "UnicastSignal3", "", f->recipient_name);
1128   dbus_message_unref (m);
1129
1130   m = g_queue_pop_head (&f->monitored);
1131   g_assert (m == NULL);
1132 }
1133
1134 static void
1135 expect_new_connection (Fixture *f)
1136 {
1137   DBusMessage *m;
1138
1139   while (g_queue_get_length (&f->monitored) < 4)
1140     test_main_context_iterate (f->ctx, TRUE);
1141
1142   m = g_queue_pop_head (&f->monitored);
1143   assert_hello (m);
1144   dbus_message_unref (m);
1145
1146   m = g_queue_pop_head (&f->monitored);
1147   assert_hello_reply (m);
1148   dbus_message_unref (m);
1149
1150   m = g_queue_pop_head (&f->monitored);
1151   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1152       "NameOwnerChanged", "sss", NULL);
1153   dbus_message_unref (m);
1154
1155   m = g_queue_pop_head (&f->monitored);
1156   assert_name_acquired (m);
1157   dbus_message_unref (m);
1158 }
1159
1160 static void
1161 take_well_known_name (Fixture *f,
1162     DBusConnection *connection,
1163     const char *name)
1164 {
1165   int ret;
1166
1167   ret = dbus_bus_request_name (connection, name,
1168       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1169   test_assert_no_error (&f->e);
1170   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1171 }
1172
1173 static void
1174 expect_take_well_known_name (Fixture *f,
1175     DBusConnection *connection,
1176     const char *name)
1177 {
1178   DBusMessage *m;
1179   const char *connection_name = dbus_bus_get_unique_name (connection);
1180
1181   while (g_queue_get_length (&f->monitored) < 4)
1182     test_main_context_iterate (f->ctx, TRUE);
1183
1184   m = g_queue_pop_head (&f->monitored);
1185   assert_method_call (m, connection_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1186       DBUS_INTERFACE_DBUS, "RequestName", "su");
1187   dbus_message_unref (m);
1188
1189   m = g_queue_pop_head (&f->monitored);
1190   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1191       "NameOwnerChanged", "sss", NULL);
1192   dbus_message_unref (m);
1193
1194   m = g_queue_pop_head (&f->monitored);
1195   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1196       "NameAcquired", "s", connection_name);
1197   dbus_message_unref (m);
1198
1199   m = g_queue_pop_head (&f->monitored);
1200   assert_method_reply (m, DBUS_SERVICE_DBUS, connection_name, "u");
1201   dbus_message_unref (m);
1202 }
1203
1204 static void
1205 test_activation (Fixture *f,
1206     gconstpointer context)
1207 {
1208   DBusMessage *m;
1209
1210   if (f->address == NULL)
1211     return;
1212
1213   become_monitor (f);
1214
1215   /* The sender sends a message to an activatable service. */
1216   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1217   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
1218     g_error ("OOM");
1219   dbus_connection_send (f->sender, m, NULL);
1220   dbus_message_unref (m);
1221
1222   /* We observe the activation request, and the message that caused it,
1223    * before systemd has even joined the bus. */
1224   while (g_queue_get_length (&f->monitored) < 2)
1225     test_main_context_iterate (f->ctx, TRUE);
1226
1227   m = g_queue_pop_head (&f->monitored);
1228   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1229       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1230       "org.freedesktop.systemd1");
1231   dbus_message_unref (m);
1232   m = g_queue_pop_head (&f->monitored);
1233   assert_signal (m, f->sender_name, "/foo",
1234       "com.example.bar", "UnicastSignal1", "",
1235       "com.example.SystemdActivatable1");
1236   dbus_message_unref (m);
1237
1238   /* The fake systemd connects to the bus. */
1239   f->systemd = test_connect_to_bus (f->ctx, f->address);
1240   if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
1241     g_error ("OOM");
1242   f->systemd_name = dbus_bus_get_unique_name (f->systemd);
1243
1244   expect_new_connection (f);
1245   take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1246   expect_take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1247
1248   /* It gets its activation request. */
1249   while (f->systemd_message == NULL)
1250     test_main_context_iterate (f->ctx, TRUE);
1251
1252   m = f->systemd_message;
1253   f->systemd_message = NULL;
1254   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1255       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1256       "org.freedesktop.systemd1");
1257   dbus_message_unref (m);
1258
1259   /* systemd starts the activatable service. */
1260   f->activated = test_connect_to_bus (f->ctx, f->address);
1261   if (!dbus_connection_add_filter (f->activated, activated_filter,
1262         f, NULL))
1263     g_error ("OOM");
1264   f->activated_name = dbus_bus_get_unique_name (f->activated);
1265
1266   expect_new_connection (f);
1267   take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
1268   expect_take_well_known_name (f, f->activated,
1269       "com.example.SystemdActivatable1");
1270
1271   /* The message is delivered to the activatable service. */
1272   while (f->activated_message == NULL)
1273     test_main_context_iterate (f->ctx, TRUE);
1274
1275   m = f->activated_message;
1276   f->activated_message = NULL;
1277   assert_signal (m, f->sender_name, "/foo",
1278       "com.example.bar", "UnicastSignal1", "",
1279       "com.example.SystemdActivatable1");
1280   dbus_message_unref (m);
1281
1282   /* The sender sends a message to a different activatable service. */
1283   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1284   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
1285     g_error ("OOM");
1286   dbus_connection_send (f->sender, m, NULL);
1287   dbus_message_unref (m);
1288
1289   /* This time systemd is already ready for it. */
1290   while (g_queue_get_length (&f->monitored) < 2 ||
1291       f->systemd_message == NULL)
1292     test_main_context_iterate (f->ctx, TRUE);
1293
1294   m = f->systemd_message;
1295   f->systemd_message = NULL;
1296   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1297       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1298       "org.freedesktop.systemd1");
1299   dbus_message_unref (m);
1300
1301   /* The monitor sees the activation request and the signal that
1302    * prompted it.*/
1303   m = g_queue_pop_head (&f->monitored);
1304   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1305       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1306       "org.freedesktop.systemd1");
1307   dbus_message_unref (m);
1308   m = g_queue_pop_head (&f->monitored);
1309   assert_signal (m, f->sender_name, "/foo",
1310       "com.example.bar", "UnicastSignal2", "",
1311       "com.example.SystemdActivatable2");
1312   dbus_message_unref (m);
1313
1314   /* The activatable service takes its name. Here I'm faking it by using
1315    * an existing connection. */
1316   take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
1317
1318   /* The message is delivered to the activatable service.
1319    * Implementation detail: the monitor sees this happen before it even
1320    * sees that the name request happened, which is pretty odd. */
1321   while (f->activated_message == NULL)
1322     test_main_context_iterate (f->ctx, TRUE);
1323
1324   m = f->activated_message;
1325   f->activated_message = NULL;
1326   assert_signal (m, f->sender_name, "/foo",
1327       "com.example.bar", "UnicastSignal2", "",
1328       "com.example.SystemdActivatable2");
1329   dbus_message_unref (m);
1330
1331   expect_take_well_known_name (f, f->activated,
1332       "com.example.SystemdActivatable2");
1333
1334   /* A third activation. */
1335   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1336   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
1337     g_error ("OOM");
1338   dbus_connection_send (f->sender, m, NULL);
1339   dbus_message_unref (m);
1340
1341   /* Once again, we see the activation request and the reason. */
1342   while (g_queue_get_length (&f->monitored) < 2)
1343     test_main_context_iterate (f->ctx, TRUE);
1344
1345   m = g_queue_pop_head (&f->monitored);
1346   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1347       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1348       "org.freedesktop.systemd1");
1349   dbus_message_unref (m);
1350   m = g_queue_pop_head (&f->monitored);
1351   assert_signal (m, f->sender_name, "/foo",
1352       "com.example.bar", "UnicastSignal3", "",
1353       "com.example.SystemdActivatable3");
1354   dbus_message_unref (m);
1355
1356   /* systemd gets the request too. */
1357   while (f->systemd_message == NULL)
1358     test_main_context_iterate (f->ctx, TRUE);
1359
1360   m = f->systemd_message;
1361   f->systemd_message = NULL;
1362   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1363       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1364       "org.freedesktop.systemd1");
1365   dbus_message_unref (m);
1366
1367   /* This time activation fails */
1368   m = dbus_message_new_signal ("/org/freedesktop/systemd1",
1369       "org.freedesktop.systemd1.Activator", "ActivationFailure");
1370
1371   do
1372     {
1373       const char *unit = "dbus-com.example.SystemdActivatable3.service";
1374       const char *error_name = "com.example.Nope";
1375       const char *error_message = "Computer says no";
1376
1377       if (!dbus_message_append_args (m,
1378             DBUS_TYPE_STRING, &unit,
1379             DBUS_TYPE_STRING, &error_name,
1380             DBUS_TYPE_STRING, &error_message,
1381             DBUS_TYPE_INVALID))
1382         g_error ("OOM");
1383     }
1384   while (0);
1385
1386   if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
1387     g_error ("OOM");
1388   dbus_connection_send (f->systemd, m, NULL);
1389   dbus_message_unref (m);
1390
1391   /* The monitor sees activation fail */
1392
1393   /* Once again, we see the activation request and the reason. */
1394   while (g_queue_get_length (&f->monitored) < 1)
1395     test_main_context_iterate (f->ctx, TRUE);
1396
1397   m = g_queue_pop_head (&f->monitored);
1398   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1399       "com.example.Nope");
1400   dbus_message_unref (m);
1401 }
1402
1403 static void
1404 teardown (Fixture *f,
1405     gconstpointer context G_GNUC_UNUSED)
1406 {
1407   dbus_error_free (&f->e);
1408   g_clear_error (&f->ge);
1409
1410   if (f->monitor != NULL)
1411     {
1412       dbus_connection_remove_filter (f->monitor, monitor_filter, f);
1413       dbus_connection_close (f->monitor);
1414       dbus_connection_unref (f->monitor);
1415       f->monitor = NULL;
1416     }
1417
1418   if (f->sender != NULL)
1419     {
1420       dbus_connection_close (f->sender);
1421       dbus_connection_unref (f->sender);
1422       f->sender = NULL;
1423     }
1424
1425   if (f->recipient != NULL)
1426     {
1427       dbus_connection_remove_filter (f->recipient, recipient_filter, f);
1428       dbus_connection_close (f->recipient);
1429       dbus_connection_unref (f->recipient);
1430       f->recipient = NULL;
1431     }
1432
1433   if (f->systemd != NULL)
1434     {
1435       dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1436       dbus_connection_close (f->systemd);
1437       dbus_connection_unref (f->systemd);
1438       f->systemd = NULL;
1439     }
1440
1441   if (f->activated != NULL)
1442     {
1443       dbus_connection_remove_filter (f->activated, activated_filter, f);
1444       dbus_connection_close (f->activated);
1445       dbus_connection_unref (f->activated);
1446       f->activated = NULL;
1447     }
1448
1449   test_kill_pid (f->daemon_pid);
1450   g_spawn_close_pid (f->daemon_pid);
1451
1452   test_main_context_unref (f->ctx);
1453
1454   g_queue_foreach (&f->monitored, (GFunc) dbus_message_unref, NULL);
1455   g_queue_clear (&f->monitored);
1456
1457   g_free (f->address);
1458 }
1459
1460 int
1461 main (int argc,
1462     char **argv)
1463 {
1464   test_init (&argc, &argv);
1465
1466   g_test_add ("/monitor/become", Fixture, &side_effects_config,
1467       setup, test_become_monitor, teardown);
1468   g_test_add ("/monitor/broadcast", Fixture, NULL,
1469       setup, test_broadcast, teardown);
1470   g_test_add ("/monitor/forbidden-broadcast", Fixture, &forbidding_config,
1471       setup, test_forbidden_broadcast, teardown);
1472   g_test_add ("/monitor/unicast-signal", Fixture, NULL,
1473       setup, test_unicast_signal, teardown);
1474   g_test_add ("/monitor/forbidden", Fixture, &forbidding_config,
1475       setup, test_forbidden, teardown);
1476   g_test_add ("/monitor/method-call", Fixture, NULL,
1477       setup, test_method_call, teardown);
1478   g_test_add ("/monitor/forbidden-method", Fixture, &forbidding_config,
1479       setup, test_forbidden_method_call, teardown);
1480   g_test_add ("/monitor/dbus-daemon", Fixture, NULL,
1481       setup, test_dbus_daemon, teardown);
1482   g_test_add ("/monitor/selective", Fixture, &selective_config,
1483       setup, test_selective, teardown);
1484   g_test_add ("/monitor/wildcard", Fixture, &wildcard_config,
1485       setup, test_unicast_signal, teardown);
1486   g_test_add ("/monitor/no-rule", Fixture, &no_rules_config,
1487       setup, test_unicast_signal, teardown);
1488   g_test_add ("/monitor/eavesdrop", Fixture, &eavesdrop_config,
1489       setup, test_unicast_signal, teardown);
1490   g_test_add ("/monitor/no-eavesdrop", Fixture, &no_eavesdrop_config,
1491       setup, test_unicast_signal, teardown);
1492   g_test_add ("/monitor/activation", Fixture, &fake_systemd_config,
1493       setup, test_activation, teardown);
1494
1495   return g_test_run ();
1496 }