Merge remote-tracking branch 'origin/dbus-1.8'
[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_test_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_test_message ("\tfrom: %s",
171       not_null2 (dbus_message_get_sender (m), "(dbus-daemon)"));
172   g_test_message ("\tto: %s",
173       not_null2 (dbus_message_get_destination (m), "(broadcast)"));
174   g_test_message ("\tpath: %s",
175       not_null (dbus_message_get_path (m)));
176   g_test_message ("\tinterface: %s",
177       not_null (dbus_message_get_interface (m)));
178   g_test_message ("\tmember: %s",
179       not_null (dbus_message_get_member (m)));
180   g_test_message ("\tsignature: %s",
181       not_null (dbus_message_get_signature (m)));
182   g_test_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_test_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       if (g_queue_is_empty (&f->monitored))
545         test_main_context_iterate (f->ctx, TRUE);
546
547       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
548         {
549           if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
550                 "NameAcquired"))
551             {
552               const char *name;
553               dbus_bool_t ok = dbus_message_get_args (m, &f->e,
554                   DBUS_TYPE_STRING, &name,
555                   DBUS_TYPE_INVALID);
556
557               g_assert_cmpstr (dbus_message_get_path (m), ==,
558                   DBUS_PATH_DBUS);
559
560               test_assert_no_error (&f->e);
561               g_assert (ok);
562
563               if (g_str_equal (name, f->monitor_name))
564                 {
565                   g_assert (!got_unique);
566                   got_unique = TRUE;
567                 }
568               else if (g_str_equal (name, "com.example.A"))
569                 {
570                   g_assert (!got_a);
571                   got_a = TRUE;
572                 }
573               else if (g_str_equal (name, "com.example.B"))
574                 {
575                   g_assert (!got_b);
576                   got_b = TRUE;
577                 }
578               else
579                 {
580                   g_assert_cmpstr (name, ==, "com.example.C");
581                   g_assert (!got_c);
582                   got_c = TRUE;
583                 }
584             }
585           else
586             {
587               g_error ("unexpected message %s.%s",
588                   dbus_message_get_interface (m),
589                   dbus_message_get_member (m));
590             }
591
592           dbus_message_unref (m);
593         }
594     }
595
596   become_monitor (f);
597
598   while (!lost_unique || !lost_a || !lost_b || !lost_c)
599     {
600       if (g_queue_is_empty (&f->monitored))
601         test_main_context_iterate (f->ctx, TRUE);
602
603       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
604         {
605           if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
606                 "NameLost"))
607             {
608               const char *name;
609               dbus_bool_t ok = dbus_message_get_args (m, &f->e,
610                   DBUS_TYPE_STRING, &name,
611                   DBUS_TYPE_INVALID);
612
613               test_assert_no_error (&f->e);
614               g_assert (ok);
615
616               if (g_str_equal (name, f->monitor_name))
617                 {
618                   g_assert (!lost_unique);
619                   lost_unique = TRUE;
620                 }
621               else if (g_str_equal (name, "com.example.A"))
622                 {
623                   g_assert (!lost_a);
624                   lost_a = TRUE;
625                 }
626               else if (g_str_equal (name, "com.example.B"))
627                 {
628                   g_assert (!lost_b);
629                   lost_b = TRUE;
630                 }
631               else
632                 {
633                   g_assert_cmpstr (name, ==, "com.example.C");
634                   g_assert (!lost_c);
635                   lost_c = TRUE;
636                 }
637             }
638           else
639             {
640               g_error ("unexpected message %s.%s",
641                   dbus_message_get_interface (m),
642                   dbus_message_get_member (m));
643             }
644
645           dbus_message_unref (m);
646         }
647     }
648
649   /* Calling methods is forbidden; we get disconnected. */
650   dbus_bus_add_match (f->monitor, "", &f->e);
651   g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);
652   g_assert (!dbus_connection_get_is_connected (f->monitor));
653
654   while (TRUE)
655     {
656       if (g_queue_is_empty (&f->monitored))
657         test_main_context_iterate (f->ctx, TRUE);
658
659       /* When we iterate all the connection's messages, we see ourselves
660        * losing all our names, then we're disconnected. */
661       while ((m = g_queue_pop_head (&f->monitored)) != NULL)
662         {
663           if (dbus_message_is_signal (m, DBUS_INTERFACE_LOCAL, "Disconnected"))
664             {
665               dbus_message_unref (m);
666               goto disconnected;
667             }
668           else
669             {
670               g_error ("unexpected message %s.%s",
671                   dbus_message_get_interface (m),
672                   dbus_message_get_member (m));
673             }
674
675           dbus_message_unref (m);
676         }
677     }
678
679 disconnected:
680
681   g_assert (lost_a);
682   g_assert (lost_b);
683   g_assert (lost_c);
684 }
685
686 static void
687 test_broadcast (Fixture *f,
688     gconstpointer context)
689 {
690   DBusMessage *m;
691
692   if (f->address == NULL)
693     return;
694
695   dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
696   test_assert_no_error (&f->e);
697
698   become_monitor (f);
699
700   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal1");
701   dbus_connection_send (f->sender, m, NULL);
702   dbus_message_unref (m);
703
704   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal2");
705   dbus_connection_send (f->sender, m, NULL);
706   dbus_message_unref (m);
707
708   m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal3");
709   dbus_connection_send (f->sender, m, NULL);
710   dbus_message_unref (m);
711
712   while (g_queue_get_length (&f->monitored) < 3)
713     test_main_context_iterate (f->ctx, TRUE);
714
715   m = g_queue_pop_head (&f->monitored);
716   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
717       "BroadcastSignal1", "", NULL);
718   dbus_message_unref (m);
719
720   m = g_queue_pop_head (&f->monitored);
721   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
722       "BroadcastSignal2", "", NULL);
723   dbus_message_unref (m);
724
725   m = g_queue_pop_head (&f->monitored);
726   assert_signal (m, f->sender_name, "/foo", "com.example.bar",
727       "BroadcastSignal3", "", NULL);
728   dbus_message_unref (m);
729
730   m = g_queue_pop_head (&f->monitored);
731   g_assert (m == NULL);
732 }
733
734 static void
735 test_forbidden_broadcast (Fixture *f,
736     gconstpointer context)
737 {
738   DBusMessage *m;
739
740   if (f->address == NULL)
741     return;
742
743   dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
744   test_assert_no_error (&f->e);
745
746   become_monitor (f);
747
748   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
749       "BroadcastSignal1");
750   dbus_connection_send (f->sender, m, NULL);
751   dbus_message_unref (m);
752
753   m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
754       "BroadcastSignal2");
755   dbus_connection_send (f->sender, m, NULL);
756   dbus_message_unref (m);
757
758   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
759       "BroadcastSignal3");
760   dbus_connection_send (f->sender, m, NULL);
761   dbus_message_unref (m);
762
763   while (g_queue_get_length (&f->monitored) < 6)
764     test_main_context_iterate (f->ctx, TRUE);
765
766   m = g_queue_pop_head (&f->monitored);
767   assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
768       "BroadcastSignal1", "", NULL);
769   dbus_message_unref (m);
770
771   m = g_queue_pop_head (&f->monitored);
772   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
773       DBUS_ERROR_ACCESS_DENIED);
774   dbus_message_unref (m);
775
776   m = g_queue_pop_head (&f->monitored);
777   assert_signal (m, f->sender_name, "/foo", "com.example.CannotReceive",
778       "BroadcastSignal2", "", NULL);
779   dbus_message_unref (m);
780
781   m = g_queue_pop_head (&f->monitored);
782   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
783       DBUS_ERROR_ACCESS_DENIED);
784   dbus_message_unref (m);
785
786   m = g_queue_pop_head (&f->monitored);
787   assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
788       "BroadcastSignal3", "", NULL);
789   dbus_message_unref (m);
790
791   m = g_queue_pop_head (&f->monitored);
792   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
793       DBUS_ERROR_ACCESS_DENIED);
794   dbus_message_unref (m);
795
796   m = g_queue_pop_head (&f->monitored);
797   g_assert (m == NULL);
798 }
799
800 static void
801 test_unicast_signal (Fixture *f,
802     gconstpointer context)
803 {
804   DBusMessage *m;
805
806   if (f->address == NULL)
807     return;
808
809   become_monitor (f);
810
811   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
812   if (!dbus_message_set_destination (m, f->recipient_name))
813     g_error ("OOM");
814   dbus_connection_send (f->sender, m, NULL);
815   dbus_message_unref (m);
816
817   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
818   if (!dbus_message_set_destination (m, f->recipient_name))
819     g_error ("OOM");
820   dbus_connection_send (f->sender, m, NULL);
821   dbus_message_unref (m);
822
823   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
824   if (!dbus_message_set_destination (m, f->recipient_name))
825     g_error ("OOM");
826   dbus_connection_send (f->sender, m, NULL);
827   dbus_message_unref (m);
828
829   while (g_queue_get_length (&f->monitored) < 3)
830     test_main_context_iterate (f->ctx, TRUE);
831
832   m = g_queue_pop_head (&f->monitored);
833   assert_signal (m, f->sender_name, "/foo",
834       "com.example.bar", "UnicastSignal1", "", f->recipient_name);
835   dbus_message_unref (m);
836
837   m = g_queue_pop_head (&f->monitored);
838   assert_signal (m, f->sender_name, "/foo",
839       "com.example.bar", "UnicastSignal2", "", f->recipient_name);
840   dbus_message_unref (m);
841
842   m = g_queue_pop_head (&f->monitored);
843   assert_signal (m, f->sender_name, "/foo",
844       "com.example.bar", "UnicastSignal3", "", f->recipient_name);
845   dbus_message_unref (m);
846
847   m = g_queue_pop_head (&f->monitored);
848   g_assert (m == NULL);
849 }
850
851 static void
852 test_forbidden (Fixture *f,
853     gconstpointer context)
854 {
855   DBusMessage *m;
856
857   if (f->address == NULL)
858     return;
859
860   become_monitor (f);
861
862   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
863       "UnicastSignal1");
864   if (!dbus_message_set_destination (m, f->recipient_name))
865     g_error ("OOM");
866   dbus_connection_send (f->sender, m, NULL);
867   dbus_message_unref (m);
868
869   m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
870       "UnicastSignal2");
871   if (!dbus_message_set_destination (m, f->recipient_name))
872     g_error ("OOM");
873   dbus_connection_send (f->sender, m, NULL);
874   dbus_message_unref (m);
875
876   m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
877       "UnicastSignal3");
878   if (!dbus_message_set_destination (m, f->recipient_name))
879     g_error ("OOM");
880   dbus_connection_send (f->sender, m, NULL);
881   dbus_message_unref (m);
882
883   while (g_queue_get_length (&f->monitored) < 6)
884     test_main_context_iterate (f->ctx, TRUE);
885
886   m = g_queue_pop_head (&f->monitored);
887   assert_signal (m, f->sender_name, "/foo",
888       "com.example.CannotSend", "UnicastSignal1", "", f->recipient_name);
889   dbus_message_unref (m);
890
891   m = g_queue_pop_head (&f->monitored);
892   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
893       DBUS_ERROR_ACCESS_DENIED);
894   dbus_message_unref (m);
895
896   m = g_queue_pop_head (&f->monitored);
897   assert_signal (m, f->sender_name, "/foo",
898       "com.example.CannotReceive", "UnicastSignal2", "", f->recipient_name);
899   dbus_message_unref (m);
900
901   m = g_queue_pop_head (&f->monitored);
902   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
903       DBUS_ERROR_ACCESS_DENIED);
904   dbus_message_unref (m);
905
906   m = g_queue_pop_head (&f->monitored);
907   assert_signal (m, f->sender_name, "/foo",
908       "com.example.CannotSend", "UnicastSignal3", "", f->recipient_name);
909   dbus_message_unref (m);
910
911   m = g_queue_pop_head (&f->monitored);
912   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
913       DBUS_ERROR_ACCESS_DENIED);
914   dbus_message_unref (m);
915
916   m = g_queue_pop_head (&f->monitored);
917   g_assert (m == NULL);
918 }
919
920 static void
921 test_method_call (Fixture *f,
922     gconstpointer context)
923 {
924   DBusMessage *m;
925
926   if (f->address == NULL)
927     return;
928
929   become_monitor (f);
930
931   m = dbus_message_new_method_call (f->recipient_name, "/foo", "com.example.bar",
932       "Call1");
933   dbus_connection_send (f->sender, m, NULL);
934   dbus_message_unref (m);
935
936   while (g_queue_get_length (&f->monitored) < 2)
937     test_main_context_iterate (f->ctx, TRUE);
938
939   m = g_queue_pop_head (&f->monitored);
940   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
941       "com.example.bar", "Call1", "");
942   dbus_message_unref (m);
943
944   m = g_queue_pop_head (&f->monitored);
945   assert_error_reply (m, f->recipient_name, f->sender_name,
946       DBUS_ERROR_UNKNOWN_METHOD);
947   dbus_message_unref (m);
948
949   m = g_queue_pop_head (&f->monitored);
950   g_assert (m == NULL);
951 }
952
953 static void
954 test_forbidden_method_call (Fixture *f,
955     gconstpointer context)
956 {
957   DBusMessage *m;
958
959   if (f->address == NULL)
960     return;
961
962   become_monitor (f);
963
964   m = dbus_message_new_method_call (f->recipient_name, "/foo",
965       "com.example.CannotSend", "Call1");
966   dbus_connection_send (f->sender, m, NULL);
967   dbus_message_unref (m);
968
969   while (g_queue_get_length (&f->monitored) < 2)
970     test_main_context_iterate (f->ctx, TRUE);
971
972   m = g_queue_pop_head (&f->monitored);
973   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
974       "com.example.CannotSend", "Call1", "");
975   dbus_message_unref (m);
976
977   m = g_queue_pop_head (&f->monitored);
978   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
979       DBUS_ERROR_ACCESS_DENIED);
980   dbus_message_unref (m);
981
982   m = g_queue_pop_head (&f->monitored);
983   g_assert (m == NULL);
984
985   m = dbus_message_new_method_call (f->recipient_name, "/foo",
986       "com.example.CannotReceive", "Call2");
987   dbus_connection_send (f->sender, m, NULL);
988   dbus_message_unref (m);
989
990   while (g_queue_get_length (&f->monitored) < 2)
991     test_main_context_iterate (f->ctx, TRUE);
992
993   m = g_queue_pop_head (&f->monitored);
994   assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
995       "com.example.CannotReceive", "Call2", "");
996   dbus_message_unref (m);
997
998   m = g_queue_pop_head (&f->monitored);
999   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1000       DBUS_ERROR_ACCESS_DENIED);
1001   dbus_message_unref (m);
1002
1003   m = g_queue_pop_head (&f->monitored);
1004   g_assert (m == NULL);
1005 }
1006
1007 static void
1008 test_dbus_daemon (Fixture *f,
1009     gconstpointer context)
1010 {
1011   DBusMessage *m;
1012   int res;
1013
1014   if (f->address == NULL)
1015     return;
1016
1017   become_monitor (f);
1018
1019   res = dbus_bus_request_name (f->sender, "com.example.Sender",
1020       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1021   test_assert_no_error (&f->e);
1022   g_assert_cmpint (res, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1023
1024   res = dbus_bus_release_name (f->sender, "com.example.Sender", &f->e);
1025   test_assert_no_error (&f->e);
1026   g_assert_cmpint (res, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
1027
1028   while (g_queue_get_length (&f->monitored) < 8)
1029     test_main_context_iterate (f->ctx, TRUE);
1030
1031   m = g_queue_pop_head (&f->monitored);
1032   assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1033       DBUS_INTERFACE_DBUS, "RequestName", "su");
1034   dbus_message_unref (m);
1035
1036   m = g_queue_pop_head (&f->monitored);
1037   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1038       "NameOwnerChanged", "sss", NULL);
1039   dbus_message_unref (m);
1040
1041   /* FIXME: should we get this? */
1042   m = g_queue_pop_head (&f->monitored);
1043   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1044       "NameAcquired", "s", f->sender_name);
1045   dbus_message_unref (m);
1046
1047   m = g_queue_pop_head (&f->monitored);
1048   assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1049   dbus_message_unref (m);
1050
1051   m = g_queue_pop_head (&f->monitored);
1052   assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1053       DBUS_INTERFACE_DBUS, "ReleaseName", "s");
1054   dbus_message_unref (m);
1055
1056   /* FIXME: should we get this? */
1057   m = g_queue_pop_head (&f->monitored);
1058   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1059       "NameLost", "s", f->sender_name);
1060   dbus_message_unref (m);
1061
1062   m = g_queue_pop_head (&f->monitored);
1063   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1064       "NameOwnerChanged", "sss", NULL);
1065   dbus_message_unref (m);
1066
1067   m = g_queue_pop_head (&f->monitored);
1068   assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1069   dbus_message_unref (m);
1070
1071   m = g_queue_pop_head (&f->monitored);
1072   g_assert (m == NULL);
1073 }
1074
1075 static void
1076 test_selective (Fixture *f,
1077     gconstpointer context)
1078 {
1079   DBusMessage *m;
1080
1081   if (f->address == NULL)
1082     return;
1083
1084   /* Match rules added before becoming a monitor should be cleared:
1085    * if they weren't, this test would get Interesting twice, then Tedious,
1086    * and only see Fun after that. */
1087   dbus_bus_add_match (f->monitor,
1088       "eavesdrop='true',interface='com.example.Interesting'", &f->e);
1089   test_assert_no_error (&f->e);
1090   dbus_bus_add_match (f->monitor,
1091       "eavesdrop='true',interface='com.example.Tedious'", &f->e);
1092   test_assert_no_error (&f->e);
1093
1094   become_monitor (f);
1095
1096   m = dbus_message_new_signal ("/foo", "com.example.Interesting",
1097       "UnicastSignal1");
1098   if (!dbus_message_set_destination (m, f->recipient_name))
1099     g_error ("OOM");
1100   dbus_connection_send (f->sender, m, NULL);
1101   dbus_message_unref (m);
1102
1103   m = dbus_message_new_signal ("/foo", "com.example.Tedious",
1104       "UnicastSignal2");
1105   if (!dbus_message_set_destination (m, f->recipient_name))
1106     g_error ("OOM");
1107   dbus_connection_send (f->sender, m, NULL);
1108   dbus_message_unref (m);
1109
1110   m = dbus_message_new_signal ("/foo", "com.example.Fun",
1111       "UnicastSignal3");
1112   if (!dbus_message_set_destination (m, f->recipient_name))
1113     g_error ("OOM");
1114   dbus_connection_send (f->sender, m, NULL);
1115   dbus_message_unref (m);
1116
1117   while (g_queue_get_length (&f->monitored) < 2)
1118     test_main_context_iterate (f->ctx, TRUE);
1119
1120   /* We get the interesting signal and the fun signal, but not the tedious
1121    * signal. */
1122
1123   m = g_queue_pop_head (&f->monitored);
1124   assert_signal (m, f->sender_name, "/foo",
1125       "com.example.Interesting", "UnicastSignal1", "", f->recipient_name);
1126   dbus_message_unref (m);
1127
1128   m = g_queue_pop_head (&f->monitored);
1129   assert_signal (m, f->sender_name, "/foo",
1130       "com.example.Fun", "UnicastSignal3", "", f->recipient_name);
1131   dbus_message_unref (m);
1132
1133   m = g_queue_pop_head (&f->monitored);
1134   g_assert (m == NULL);
1135 }
1136
1137 static void
1138 expect_new_connection (Fixture *f)
1139 {
1140   DBusMessage *m;
1141
1142   while (g_queue_get_length (&f->monitored) < 4)
1143     test_main_context_iterate (f->ctx, TRUE);
1144
1145   m = g_queue_pop_head (&f->monitored);
1146   assert_hello (m);
1147   dbus_message_unref (m);
1148
1149   m = g_queue_pop_head (&f->monitored);
1150   assert_hello_reply (m);
1151   dbus_message_unref (m);
1152
1153   m = g_queue_pop_head (&f->monitored);
1154   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1155       "NameOwnerChanged", "sss", NULL);
1156   dbus_message_unref (m);
1157
1158   m = g_queue_pop_head (&f->monitored);
1159   assert_name_acquired (m);
1160   dbus_message_unref (m);
1161 }
1162
1163 static void
1164 take_well_known_name (Fixture *f,
1165     DBusConnection *connection,
1166     const char *name)
1167 {
1168   int ret;
1169
1170   ret = dbus_bus_request_name (connection, name,
1171       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1172   test_assert_no_error (&f->e);
1173   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1174 }
1175
1176 static void
1177 expect_take_well_known_name (Fixture *f,
1178     DBusConnection *connection,
1179     const char *name)
1180 {
1181   DBusMessage *m;
1182   const char *connection_name = dbus_bus_get_unique_name (connection);
1183
1184   while (g_queue_get_length (&f->monitored) < 4)
1185     test_main_context_iterate (f->ctx, TRUE);
1186
1187   m = g_queue_pop_head (&f->monitored);
1188   assert_method_call (m, connection_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1189       DBUS_INTERFACE_DBUS, "RequestName", "su");
1190   dbus_message_unref (m);
1191
1192   m = g_queue_pop_head (&f->monitored);
1193   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1194       "NameOwnerChanged", "sss", NULL);
1195   dbus_message_unref (m);
1196
1197   m = g_queue_pop_head (&f->monitored);
1198   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1199       "NameAcquired", "s", connection_name);
1200   dbus_message_unref (m);
1201
1202   m = g_queue_pop_head (&f->monitored);
1203   assert_method_reply (m, DBUS_SERVICE_DBUS, connection_name, "u");
1204   dbus_message_unref (m);
1205 }
1206
1207 static void
1208 test_activation (Fixture *f,
1209     gconstpointer context)
1210 {
1211   DBusMessage *m;
1212
1213   if (f->address == NULL)
1214     return;
1215
1216   become_monitor (f);
1217
1218   /* The sender sends a message to an activatable service. */
1219   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1220   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
1221     g_error ("OOM");
1222   dbus_connection_send (f->sender, m, NULL);
1223   dbus_message_unref (m);
1224
1225   /* We observe the activation request, and the message that caused it,
1226    * before systemd has even joined the bus. */
1227   while (g_queue_get_length (&f->monitored) < 2)
1228     test_main_context_iterate (f->ctx, TRUE);
1229
1230   m = g_queue_pop_head (&f->monitored);
1231   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1232       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1233       "org.freedesktop.systemd1");
1234   dbus_message_unref (m);
1235   m = g_queue_pop_head (&f->monitored);
1236   assert_signal (m, f->sender_name, "/foo",
1237       "com.example.bar", "UnicastSignal1", "",
1238       "com.example.SystemdActivatable1");
1239   dbus_message_unref (m);
1240
1241   /* The fake systemd connects to the bus. */
1242   f->systemd = test_connect_to_bus (f->ctx, f->address);
1243   if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
1244     g_error ("OOM");
1245   f->systemd_name = dbus_bus_get_unique_name (f->systemd);
1246
1247   expect_new_connection (f);
1248   take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1249   expect_take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1250
1251   /* It gets its activation request. */
1252   while (f->systemd_message == NULL)
1253     test_main_context_iterate (f->ctx, TRUE);
1254
1255   m = f->systemd_message;
1256   f->systemd_message = NULL;
1257   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1258       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1259       "org.freedesktop.systemd1");
1260   dbus_message_unref (m);
1261
1262   /* systemd starts the activatable service. */
1263   f->activated = test_connect_to_bus (f->ctx, f->address);
1264   if (!dbus_connection_add_filter (f->activated, activated_filter,
1265         f, NULL))
1266     g_error ("OOM");
1267   f->activated_name = dbus_bus_get_unique_name (f->activated);
1268
1269   expect_new_connection (f);
1270   take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
1271   expect_take_well_known_name (f, f->activated,
1272       "com.example.SystemdActivatable1");
1273
1274   /* The message is delivered to the activatable service. */
1275   while (f->activated_message == NULL)
1276     test_main_context_iterate (f->ctx, TRUE);
1277
1278   m = f->activated_message;
1279   f->activated_message = NULL;
1280   assert_signal (m, f->sender_name, "/foo",
1281       "com.example.bar", "UnicastSignal1", "",
1282       "com.example.SystemdActivatable1");
1283   dbus_message_unref (m);
1284
1285   /* The sender sends a message to a different activatable service. */
1286   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1287   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
1288     g_error ("OOM");
1289   dbus_connection_send (f->sender, m, NULL);
1290   dbus_message_unref (m);
1291
1292   /* This time systemd is already ready for it. */
1293   while (g_queue_get_length (&f->monitored) < 2 ||
1294       f->systemd_message == NULL)
1295     test_main_context_iterate (f->ctx, TRUE);
1296
1297   m = f->systemd_message;
1298   f->systemd_message = NULL;
1299   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1300       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1301       "org.freedesktop.systemd1");
1302   dbus_message_unref (m);
1303
1304   /* The monitor sees the activation request and the signal that
1305    * prompted it.*/
1306   m = g_queue_pop_head (&f->monitored);
1307   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1308       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1309       "org.freedesktop.systemd1");
1310   dbus_message_unref (m);
1311   m = g_queue_pop_head (&f->monitored);
1312   assert_signal (m, f->sender_name, "/foo",
1313       "com.example.bar", "UnicastSignal2", "",
1314       "com.example.SystemdActivatable2");
1315   dbus_message_unref (m);
1316
1317   /* The activatable service takes its name. Here I'm faking it by using
1318    * an existing connection. */
1319   take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
1320
1321   /* The message is delivered to the activatable service.
1322    * Implementation detail: the monitor sees this happen before it even
1323    * sees that the name request happened, which is pretty odd. */
1324   while (f->activated_message == NULL)
1325     test_main_context_iterate (f->ctx, TRUE);
1326
1327   m = f->activated_message;
1328   f->activated_message = NULL;
1329   assert_signal (m, f->sender_name, "/foo",
1330       "com.example.bar", "UnicastSignal2", "",
1331       "com.example.SystemdActivatable2");
1332   dbus_message_unref (m);
1333
1334   expect_take_well_known_name (f, f->activated,
1335       "com.example.SystemdActivatable2");
1336
1337   /* A third activation. */
1338   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1339   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
1340     g_error ("OOM");
1341   dbus_connection_send (f->sender, m, NULL);
1342   dbus_message_unref (m);
1343
1344   /* Once again, we see the activation request and the reason. */
1345   while (g_queue_get_length (&f->monitored) < 2)
1346     test_main_context_iterate (f->ctx, TRUE);
1347
1348   m = g_queue_pop_head (&f->monitored);
1349   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1350       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1351       "org.freedesktop.systemd1");
1352   dbus_message_unref (m);
1353   m = g_queue_pop_head (&f->monitored);
1354   assert_signal (m, f->sender_name, "/foo",
1355       "com.example.bar", "UnicastSignal3", "",
1356       "com.example.SystemdActivatable3");
1357   dbus_message_unref (m);
1358
1359   /* systemd gets the request too. */
1360   while (f->systemd_message == NULL)
1361     test_main_context_iterate (f->ctx, TRUE);
1362
1363   m = f->systemd_message;
1364   f->systemd_message = NULL;
1365   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1366       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1367       "org.freedesktop.systemd1");
1368   dbus_message_unref (m);
1369
1370   /* This time activation fails */
1371   m = dbus_message_new_signal ("/org/freedesktop/systemd1",
1372       "org.freedesktop.systemd1.Activator", "ActivationFailure");
1373
1374   do
1375     {
1376       const char *unit = "dbus-com.example.SystemdActivatable3.service";
1377       const char *error_name = "com.example.Nope";
1378       const char *error_message = "Computer says no";
1379
1380       if (!dbus_message_append_args (m,
1381             DBUS_TYPE_STRING, &unit,
1382             DBUS_TYPE_STRING, &error_name,
1383             DBUS_TYPE_STRING, &error_message,
1384             DBUS_TYPE_INVALID))
1385         g_error ("OOM");
1386     }
1387   while (0);
1388
1389   if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
1390     g_error ("OOM");
1391   dbus_connection_send (f->systemd, m, NULL);
1392   dbus_message_unref (m);
1393
1394   /* The monitor sees activation fail */
1395
1396   /* Once again, we see the activation request and the reason. */
1397   while (g_queue_get_length (&f->monitored) < 1)
1398     test_main_context_iterate (f->ctx, TRUE);
1399
1400   m = g_queue_pop_head (&f->monitored);
1401   assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1402       "com.example.Nope");
1403   dbus_message_unref (m);
1404 }
1405
1406 static void
1407 teardown (Fixture *f,
1408     gconstpointer context G_GNUC_UNUSED)
1409 {
1410   dbus_error_free (&f->e);
1411   g_clear_error (&f->ge);
1412
1413   if (f->monitor != NULL)
1414     {
1415       dbus_connection_remove_filter (f->monitor, monitor_filter, f);
1416       dbus_connection_close (f->monitor);
1417       dbus_connection_unref (f->monitor);
1418       f->monitor = NULL;
1419     }
1420
1421   if (f->sender != NULL)
1422     {
1423       dbus_connection_close (f->sender);
1424       dbus_connection_unref (f->sender);
1425       f->sender = NULL;
1426     }
1427
1428   if (f->recipient != NULL)
1429     {
1430       dbus_connection_remove_filter (f->recipient, recipient_filter, f);
1431       dbus_connection_close (f->recipient);
1432       dbus_connection_unref (f->recipient);
1433       f->recipient = NULL;
1434     }
1435
1436   if (f->systemd != NULL)
1437     {
1438       dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1439       dbus_connection_close (f->systemd);
1440       dbus_connection_unref (f->systemd);
1441       f->systemd = NULL;
1442     }
1443
1444   if (f->activated != NULL)
1445     {
1446       dbus_connection_remove_filter (f->activated, activated_filter, f);
1447       dbus_connection_close (f->activated);
1448       dbus_connection_unref (f->activated);
1449       f->activated = NULL;
1450     }
1451
1452   test_kill_pid (f->daemon_pid);
1453   g_spawn_close_pid (f->daemon_pid);
1454
1455   test_main_context_unref (f->ctx);
1456
1457   g_queue_foreach (&f->monitored, (GFunc) dbus_message_unref, NULL);
1458   g_queue_clear (&f->monitored);
1459
1460   g_free (f->address);
1461 }
1462
1463 int
1464 main (int argc,
1465     char **argv)
1466 {
1467   test_init (&argc, &argv);
1468
1469   g_test_add ("/monitor/become", Fixture, &side_effects_config,
1470       setup, test_become_monitor, teardown);
1471   g_test_add ("/monitor/broadcast", Fixture, NULL,
1472       setup, test_broadcast, teardown);
1473   g_test_add ("/monitor/forbidden-broadcast", Fixture, &forbidding_config,
1474       setup, test_forbidden_broadcast, teardown);
1475   g_test_add ("/monitor/unicast-signal", Fixture, NULL,
1476       setup, test_unicast_signal, teardown);
1477   g_test_add ("/monitor/forbidden", Fixture, &forbidding_config,
1478       setup, test_forbidden, teardown);
1479   g_test_add ("/monitor/method-call", Fixture, NULL,
1480       setup, test_method_call, teardown);
1481   g_test_add ("/monitor/forbidden-method", Fixture, &forbidding_config,
1482       setup, test_forbidden_method_call, teardown);
1483   g_test_add ("/monitor/dbus-daemon", Fixture, NULL,
1484       setup, test_dbus_daemon, teardown);
1485   g_test_add ("/monitor/selective", Fixture, &selective_config,
1486       setup, test_selective, teardown);
1487   g_test_add ("/monitor/wildcard", Fixture, &wildcard_config,
1488       setup, test_unicast_signal, teardown);
1489   g_test_add ("/monitor/no-rule", Fixture, &no_rules_config,
1490       setup, test_unicast_signal, teardown);
1491   g_test_add ("/monitor/eavesdrop", Fixture, &eavesdrop_config,
1492       setup, test_unicast_signal, teardown);
1493   g_test_add ("/monitor/no-eavesdrop", Fixture, &no_eavesdrop_config,
1494       setup, test_unicast_signal, teardown);
1495   g_test_add ("/monitor/activation", Fixture, &fake_systemd_config,
1496       setup, test_activation, teardown);
1497
1498   return g_test_run ();
1499 }