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