GVariant: fix dbus_message_copy()
[platform/upstream/dbus.git] / test / sd-activation.c
1 /* Unit tests for systemd activation, with or without AppArmor.
2  *
3  * We compile this source file twice: once with AppArmor support (if available)
4  * and once without.
5  *
6  * Copyright © 2010-2011 Nokia Corporation
7  * Copyright © 2015 Collabora Ltd.
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation files
11  * (the "Software"), to deal in the Software without restriction,
12  * including without limitation the rights to use, copy, modify, merge,
13  * publish, distribute, sublicense, and/or sell copies of the Software,
14  * and to permit persons to whom the Software is furnished to do so,
15  * subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  */
29
30 #include <config.h>
31
32 #include <errno.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <sys/types.h>
36
37 #include <glib/gstdio.h>
38
39 #if defined(HAVE_APPARMOR_2_10) && defined(DBUS_TEST_APPARMOR_ACTIVATION)
40 #include <sys/apparmor.h>
41 #endif
42
43 #include "test-utils-glib.h"
44
45 typedef struct {
46     TestMainContext *ctx;
47     DBusError e;
48     GError *ge;
49
50     gchar *address;
51     GPid daemon_pid;
52
53     DBusConnection *caller;
54     const char *caller_name;
55     DBusMessage *caller_message;
56     dbus_bool_t caller_filter_added;
57
58     DBusConnection *systemd;
59     const char *systemd_name;
60     DBusMessage *systemd_message;
61     dbus_bool_t systemd_filter_added;
62
63     DBusConnection *activated;
64     const char *activated_name;
65     DBusMessage *activated_message;
66     dbus_bool_t activated_filter_added;
67
68     gchar *transient_service_file;
69     gchar *tmp_runtime_dir;
70 } Fixture;
71
72 typedef enum
73 {
74   FLAG_EARLY_TRANSIENT_SERVICE = (1 << 0),
75   FLAG_NONE = 0
76 } Flags;
77
78 typedef struct
79 {
80   const gchar *bus_name;
81   Flags flags;
82 } Config;
83
84 /* this is a macro so it gets the right line number */
85 #define assert_signal(m, \
86     sender, path, iface, member, signature, \
87     destination) \
88 do { \
89   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
90       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
91   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
92   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
93   g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
94   g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
95   g_assert_cmpstr (dbus_message_get_member (m), ==, member); \
96   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
97   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
98   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
99 } while (0)
100
101 #define assert_method_call(m, sender, \
102     destination, path, iface, method, signature) \
103 do { \
104   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
105       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
106   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
107   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
108   g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
109   g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
110   g_assert_cmpstr (dbus_message_get_member (m), ==, method); \
111   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
112   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
113   g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
114 } while (0)
115
116 #define assert_method_reply(m, sender, destination, signature) \
117 do { \
118   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
119       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
120   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
121   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
122   g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
123   g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
124   g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
125   g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
126   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
127   g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
128 } while (0)
129
130 #define assert_error_reply(m, sender, destination, error_name) \
131 do { \
132   g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
133       ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_ERROR)); \
134   g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
135   g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
136   g_assert_cmpstr (dbus_message_get_error_name (m), ==, error_name); \
137   g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
138   g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
139   g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
140   g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
141   g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
142   g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
143 } while (0)
144
145 static DBusHandlerResult
146 systemd_filter (DBusConnection *connection,
147     DBusMessage *message,
148     void *user_data)
149 {
150   Fixture *f = user_data;
151
152   if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
153         "NameAcquired") ||
154       dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
155         "NameLost"))
156     {
157       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
158     }
159
160   g_test_message("sender %s iface %s member %s",
161                  dbus_message_get_sender (message),
162                  dbus_message_get_interface (message),
163                  dbus_message_get_member (message));
164
165
166   g_assert (f->systemd_message == NULL);
167   f->systemd_message = dbus_message_ref (message);
168
169   if (dbus_message_is_method_call (message, "org.freedesktop.systemd1.Manager",
170                                    "SetEnvironment"))
171     {
172       g_assert (dbus_message_get_no_reply (message));
173       g_test_message("got call");
174       return DBUS_HANDLER_RESULT_HANDLED;
175     }
176
177   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
178 }
179
180 static DBusHandlerResult
181 activated_filter (DBusConnection *connection,
182     DBusMessage *message,
183     void *user_data)
184 {
185   Fixture *f = user_data;
186
187   if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
188         "NameAcquired") ||
189       dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
190         "NameLost"))
191     {
192       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
193     }
194
195   g_assert (f->activated_message == NULL);
196   f->activated_message = dbus_message_ref (message);
197
198   /* Test code is expected to reply to method calls itself */
199   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
200     return DBUS_HANDLER_RESULT_HANDLED;
201
202   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
203 }
204
205 static DBusHandlerResult
206 caller_filter (DBusConnection *connection,
207     DBusMessage *message,
208     void *user_data)
209 {
210   Fixture *f = user_data;
211
212   if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
213         "NameAcquired") ||
214       dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
215         "NameLost"))
216     {
217       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
218     }
219
220   g_assert (f->caller_message == NULL);
221   f->caller_message = dbus_message_ref (message);
222
223   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
224 }
225
226 static void
227 fixture_create_transient_service (Fixture *f,
228                                   const gchar *name)
229 {
230   gchar *service;
231   gchar *content;
232   gboolean ok;
233
234   service = g_strdup_printf ("%s.service", name);
235   f->transient_service_file = g_build_filename (f->tmp_runtime_dir, "dbus-1",
236       "services", service, NULL);
237   g_free (service);
238
239   content = g_strdup_printf (
240       "[D-BUS Service]\n"
241       "Name=%s\n"
242       "Exec=/bin/false %s\n"
243       "SystemdService=dbus-%s.service\n", name, name, name);
244   ok = g_file_set_contents (f->transient_service_file, content, -1, &f->ge);
245   g_assert_no_error (f->ge);
246   g_assert (ok);
247   g_free (content);
248 }
249
250 static void
251 setup (Fixture *f,
252     gconstpointer context)
253 {
254   const Config *config = context;
255 #if defined(DBUS_TEST_APPARMOR_ACTIVATION) && defined(HAVE_APPARMOR_2_10)
256   aa_features *features;
257 #endif
258
259   f->ge = NULL;
260   dbus_error_init (&f->e);
261
262   f->tmp_runtime_dir = g_dir_make_tmp ("dbus-daemon-test.XXXXXX", &f->ge);
263   g_assert_no_error (f->ge);
264
265   if (config != NULL && (config->flags & FLAG_EARLY_TRANSIENT_SERVICE) != 0)
266     {
267       gchar *dbus1 = g_build_filename (f->tmp_runtime_dir, "dbus-1", NULL);
268       gchar *services = g_build_filename (dbus1, "services", NULL);
269
270       /* We just created it so the directories shouldn't exist yet */
271       test_mkdir (dbus1, 0700);
272       test_mkdir (services, 0700);
273       fixture_create_transient_service (f, config->bus_name);
274       g_free (dbus1);
275       g_free (services);
276     }
277
278 #if defined(DBUS_TEST_APPARMOR_ACTIVATION) && !defined(HAVE_APPARMOR_2_10)
279
280   g_test_skip ("AppArmor support not compiled or AppArmor 2.10 unavailable");
281   return;
282
283 #else
284
285 #if defined(DBUS_TEST_APPARMOR_ACTIVATION)
286   if (!aa_is_enabled ())
287     {
288       g_test_message ("aa_is_enabled() -> %s", g_strerror (errno));
289       g_test_skip ("AppArmor not enabled");
290       return;
291     }
292
293   if (aa_features_new_from_kernel (&features) != 0)
294     {
295       g_test_skip ("Unable to check AppArmor features");
296       return;
297     }
298
299   if (!aa_features_supports (features, "dbus/mask/send") ||
300       !aa_features_supports (features, "dbus/mask/receive"))
301     {
302       g_test_skip ("D-Bus send/receive mediation unavailable");
303       aa_features_unref (features);
304       return;
305     }
306
307   aa_features_unref (features);
308 #endif
309
310   f->ctx = test_main_context_get ();
311
312   f->address = test_get_dbus_daemon (
313       "valid-config-files/systemd-activation.conf",
314       TEST_USER_ME, f->tmp_runtime_dir, &f->daemon_pid);
315
316   if (f->address == NULL)
317     return;
318
319 #if defined(DBUS_TEST_APPARMOR_ACTIVATION)
320   /*
321    * Make use of the fact that the LSM security label (and other process
322    * properties) that are used for access-control are whatever was current
323    * at the time the connection was opened.
324    *
325    * 42 is arbitrary. In a real use of AppArmor it would be a securely-random
326    * value, to prevent less-privileged code (that does not know the magic
327    * value) from changing back.
328    */
329   if (aa_change_hat ("caller", 42) != 0)
330     g_error ("Unable to change profile to ...//^caller: %s",
331              g_strerror (errno));
332 #endif
333
334   f->caller = test_connect_to_bus (f->ctx, f->address);
335   f->caller_name = dbus_bus_get_unique_name (f->caller);
336
337 #if defined(DBUS_TEST_APPARMOR_ACTIVATION)
338   if (aa_change_hat (NULL, 42) != 0)
339     g_error ("Unable to change back to initial profile: %s",
340              g_strerror (errno));
341 #endif
342
343 #endif
344 }
345
346 static void
347 take_well_known_name (Fixture *f,
348     DBusConnection *connection,
349     const char *name)
350 {
351   int ret;
352
353   ret = dbus_bus_request_name (connection, name,
354       DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
355   test_assert_no_error (&f->e);
356   g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
357 }
358
359 static void
360 test_activation (Fixture *f,
361     gconstpointer context)
362 {
363   DBusMessage *m;
364
365   if (f->address == NULL)
366     return;
367
368   /* The sender sends a message to an activatable service. */
369   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
370   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
371     g_error ("OOM");
372   dbus_connection_send (f->caller, m, NULL);
373   dbus_message_unref (m);
374
375   /* The fake systemd connects to the bus. */
376   f->systemd = test_connect_to_bus (f->ctx, f->address);
377   if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
378     g_error ("OOM");
379   f->systemd_filter_added = TRUE;
380   f->systemd_name = dbus_bus_get_unique_name (f->systemd);
381   take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
382
383   /* It gets its activation request. */
384   while (f->systemd_message == NULL)
385     test_main_context_iterate (f->ctx, TRUE);
386
387   m = f->systemd_message;
388   f->systemd_message = NULL;
389   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
390       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
391       "org.freedesktop.systemd1");
392   dbus_message_unref (m);
393
394   /* systemd starts the activatable service. */
395   f->activated = test_connect_to_bus (f->ctx, f->address);
396   if (!dbus_connection_add_filter (f->activated, activated_filter,
397         f, NULL))
398     g_error ("OOM");
399   f->activated_filter_added = TRUE;
400   f->activated_name = dbus_bus_get_unique_name (f->activated);
401   take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
402
403   /* The message is delivered to the activatable service. */
404   while (f->activated_message == NULL)
405     test_main_context_iterate (f->ctx, TRUE);
406
407   m = f->activated_message;
408   f->activated_message = NULL;
409   assert_signal (m, f->caller_name, "/foo",
410       "com.example.bar", "UnicastSignal1", "",
411       "com.example.SystemdActivatable1");
412   dbus_message_unref (m);
413
414   /* The sender sends a message to a different activatable service. */
415   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
416   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
417     g_error ("OOM");
418   dbus_connection_send (f->caller, m, NULL);
419   dbus_message_unref (m);
420
421   /* This time systemd is already ready for it. */
422   while (f->systemd_message == NULL)
423     test_main_context_iterate (f->ctx, TRUE);
424
425   m = f->systemd_message;
426   f->systemd_message = NULL;
427   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
428       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
429       "org.freedesktop.systemd1");
430   dbus_message_unref (m);
431
432   /* A malicious process tries to disrupt the activation.
433    * In a more realistic scenario this would be another parallel
434    * connection. */
435   m = dbus_message_new_signal ("/org/freedesktop/systemd1",
436       "org.freedesktop.systemd1.Activator", "ActivationFailure");
437   if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
438     g_error ("OOM");
439
440   do
441     {
442       const char *unit = "dbus-com.example.SystemdActivatable2.service";
443       const char *error_name = "com.example.Malice";
444       const char *error_message = "I'm on yr bus, making yr activations fail";
445
446       if (!dbus_message_append_args (m,
447             DBUS_TYPE_STRING, &unit,
448             DBUS_TYPE_STRING, &error_name,
449             DBUS_TYPE_STRING, &error_message,
450             DBUS_TYPE_INVALID))
451         g_error ("OOM");
452     }
453   while (0);
454
455   dbus_connection_send (f->caller, m, NULL);
456   dbus_message_unref (m);
457
458   /* This is just to make sure that the malicious message has arrived and
459    * been processed by the dbus-daemon, i.e. @caller won the race
460    * with @activated. */
461   take_well_known_name (f, f->caller, "com.example.Sync");
462
463   /* The activatable service takes its name. Here I'm faking it by using
464    * an existing connection; in real life it would be yet another
465    * connection. */
466   take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
467
468   /* The message is delivered to the activatable service. */
469   while (f->activated_message == NULL)
470     test_main_context_iterate (f->ctx, TRUE);
471
472   m = f->activated_message;
473   f->activated_message = NULL;
474   assert_signal (m, f->caller_name, "/foo",
475       "com.example.bar", "UnicastSignal2", "",
476       "com.example.SystemdActivatable2");
477   dbus_message_unref (m);
478
479   /* A third activation. */
480   m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
481   if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
482     g_error ("OOM");
483   dbus_connection_send (f->caller, m, NULL);
484   dbus_message_unref (m);
485
486   while (f->systemd_message == NULL)
487     test_main_context_iterate (f->ctx, TRUE);
488
489   m = f->systemd_message;
490   f->systemd_message = NULL;
491   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
492       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
493       "org.freedesktop.systemd1");
494   dbus_message_unref (m);
495
496   /* This time activation fails */
497   m = dbus_message_new_signal ("/org/freedesktop/systemd1",
498       "org.freedesktop.systemd1.Activator", "ActivationFailure");
499
500   do
501     {
502       const char *unit = "dbus-com.example.SystemdActivatable3.service";
503       const char *error_name = "com.example.Nope";
504       const char *error_message = "Computer says no";
505
506       if (!dbus_message_append_args (m,
507             DBUS_TYPE_STRING, &unit,
508             DBUS_TYPE_STRING, &error_name,
509             DBUS_TYPE_STRING, &error_message,
510             DBUS_TYPE_INVALID))
511         g_error ("OOM");
512     }
513   while (0);
514
515   if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
516     g_error ("OOM");
517   dbus_connection_send (f->systemd, m, NULL);
518   dbus_message_unref (m);
519 }
520
521 static void
522 test_uae (Fixture *f,
523     gconstpointer context)
524 {
525   DBusMessage *m;
526   DBusPendingCall *pc;
527   DBusMessageIter args_iter, arr_iter, entry_iter;
528   const char *s;
529
530   if (f->address == NULL)
531     return;
532
533   m = dbus_message_new_method_call (DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
534       DBUS_INTERFACE_DBUS, "UpdateActivationEnvironment");
535
536   if (m == NULL)
537     g_error ("OOM");
538
539   dbus_message_iter_init_append (m, &args_iter);
540
541   /* Append an empty a{ss} (string => string dictionary). */
542   if (!dbus_message_iter_open_container (&args_iter, DBUS_TYPE_ARRAY,
543         "{ss}", &arr_iter) ||
544       !dbus_message_iter_close_container (&args_iter, &arr_iter))
545     g_error ("OOM");
546
547   if (!dbus_connection_send_with_reply (f->caller, m, &pc,
548         DBUS_TIMEOUT_USE_DEFAULT) || pc == NULL)
549     g_error ("OOM");
550
551   dbus_message_unref (m);
552   m = NULL;
553
554   if (dbus_pending_call_get_completed (pc))
555     test_pending_call_store_reply (pc, &m);
556   else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
557         &m, NULL))
558     g_error ("OOM");
559
560   while (m == NULL)
561     test_main_context_iterate (f->ctx, TRUE);
562
563   assert_method_reply (m, DBUS_SERVICE_DBUS, f->caller_name, "");
564   dbus_message_unref (m);
565
566   /* The fake systemd connects to the bus. */
567   f->systemd = test_connect_to_bus (f->ctx, f->address);
568   if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
569     g_error ("OOM");
570   f->systemd_name = dbus_bus_get_unique_name (f->systemd);
571   take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
572
573   /* It gets the SetEnvironment */
574   while (f->systemd_message == NULL)
575     test_main_context_iterate (f->ctx, TRUE);
576
577   m = f->systemd_message;
578   f->systemd_message = NULL;
579
580   /* With activation, the destination is the well-known name */
581   assert_method_call (m, DBUS_SERVICE_DBUS, "org.freedesktop.systemd1",
582       "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
583       "SetEnvironment", "as");
584
585   dbus_message_iter_init (m, &args_iter);
586   g_assert_cmpuint (dbus_message_iter_get_arg_type (&args_iter), ==,
587       DBUS_TYPE_ARRAY);
588   g_assert_cmpuint (dbus_message_iter_get_element_type (&args_iter), ==,
589       DBUS_TYPE_STRING);
590   dbus_message_iter_recurse (&args_iter, &arr_iter);
591   g_assert_cmpuint (dbus_message_iter_get_arg_type (&arr_iter), ==,
592       DBUS_TYPE_INVALID);
593   dbus_message_iter_next (&args_iter);
594   g_assert_cmpuint (dbus_message_iter_get_arg_type (&args_iter), ==,
595       DBUS_TYPE_INVALID);
596   dbus_message_unref (m);
597
598   m = dbus_message_new_method_call (DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
599       DBUS_INTERFACE_DBUS, "UpdateActivationEnvironment");
600
601   if (m == NULL)
602     g_error ("OOM");
603
604   dbus_message_iter_init_append (m, &args_iter);
605
606
607   {
608     const char *k1 = "Key1", *v1 = "Value1",
609                *k2 = "Key2", *v2 = "Value2";
610
611     /* Append a filled a{ss} (string => string dictionary). */
612     if (!dbus_message_iter_open_container (&args_iter, DBUS_TYPE_ARRAY,
613           "{ss}", &arr_iter) ||
614         !dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_DICT_ENTRY,
615           NULL, &entry_iter) ||
616         !dbus_message_iter_append_basic (&entry_iter, DBUS_TYPE_STRING,
617           &k1) ||
618         !dbus_message_iter_append_basic (&entry_iter, DBUS_TYPE_STRING,
619           &v1) ||
620         !dbus_message_iter_close_container (&arr_iter, &entry_iter) ||
621         !dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_DICT_ENTRY,
622           NULL, &entry_iter) ||
623         !dbus_message_iter_append_basic (&entry_iter, DBUS_TYPE_STRING,
624           &k2) ||
625         !dbus_message_iter_append_basic (&entry_iter, DBUS_TYPE_STRING,
626           &v2) ||
627         !dbus_message_iter_close_container (&arr_iter, &entry_iter) ||
628         !dbus_message_iter_close_container (&args_iter, &arr_iter))
629       g_error ("OOM");
630   }
631
632   if (!dbus_connection_send_with_reply (f->caller, m, &pc,
633         DBUS_TIMEOUT_USE_DEFAULT) || 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   assert_method_reply (m, DBUS_SERVICE_DBUS, f->caller_name, "");
649   dbus_message_unref (m);
650
651   while (f->systemd_message == NULL)
652     test_main_context_iterate (f->ctx, TRUE);
653
654   m = f->systemd_message;
655   f->systemd_message = NULL;
656
657   /* Without activation, the destination is the unique name */
658   assert_method_call (m, DBUS_SERVICE_DBUS, f->systemd_name,
659       "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
660       "SetEnvironment", "as");
661
662   dbus_message_iter_init (m, &args_iter);
663   g_assert_cmpuint (dbus_message_iter_get_arg_type (&args_iter), ==,
664       DBUS_TYPE_ARRAY);
665   g_assert_cmpuint (dbus_message_iter_get_element_type (&args_iter), ==,
666       DBUS_TYPE_STRING);
667   dbus_message_iter_recurse (&args_iter, &arr_iter);
668   g_assert_cmpuint (dbus_message_iter_get_arg_type (&arr_iter), ==,
669       DBUS_TYPE_STRING);
670   dbus_message_iter_get_basic (&arr_iter, &s);
671   g_assert_cmpstr (s, ==, "Key1=Value1");
672   dbus_message_iter_next (&arr_iter);
673   g_assert_cmpuint (dbus_message_iter_get_arg_type (&arr_iter), ==,
674       DBUS_TYPE_STRING);
675   dbus_message_iter_get_basic (&arr_iter, &s);
676   g_assert_cmpstr (s, ==, "Key2=Value2");
677   dbus_message_iter_next (&arr_iter);
678   g_assert_cmpuint (dbus_message_iter_get_arg_type (&arr_iter), ==,
679       DBUS_TYPE_INVALID);
680   dbus_message_iter_next (&args_iter);
681   g_assert_cmpuint (dbus_message_iter_get_arg_type (&args_iter), ==,
682       DBUS_TYPE_INVALID);
683   dbus_message_unref (m);
684 }
685
686 static void
687 test_deny_send (Fixture *f,
688     gconstpointer context)
689 {
690   DBusMessage *m;
691   const Config *config = context;
692
693   g_assert (config != NULL);
694   g_assert (config->bus_name != NULL);
695
696   if (f->address == NULL)
697     return;
698
699   if (!dbus_connection_add_filter (f->caller, caller_filter, f, NULL))
700     g_error ("OOM");
701
702   f->caller_filter_added = TRUE;
703
704   /* The sender sends a message to an activatable service. */
705   m = dbus_message_new_method_call (config->bus_name, "/foo",
706       "com.example.bar", "Call");
707   if (m == NULL)
708     g_error ("OOM");
709
710   dbus_connection_send (f->caller, m, NULL);
711   dbus_message_unref (m);
712
713   /*
714    * Even before the fake systemd connects to the bus, we get an error
715    * back: activation is not allowed.
716    *
717    * In the normal case, this is because the XML policy does not allow
718    * anyone to send messages to the bus name com.example.SendDenied.
719    *
720    * In the AppArmor case, this is because the AppArmor policy does not allow
721    * this process to send messages to the bus name
722    * com.example.SendDeniedByAppArmorName, or to the label
723    * @DBUS_TEST_EXEC@/com.example.SendDeniedByAppArmorLabel that we assume the
724    * service com.example.SendDeniedByAppArmorLabel will receive after systemd
725    * runs it.
726    */
727
728   while (f->caller_message == NULL)
729     test_main_context_iterate (f->ctx, TRUE);
730
731   m = f->caller_message;
732   f->caller_message = NULL;
733   assert_error_reply (m, DBUS_SERVICE_DBUS, f->caller_name,
734       DBUS_ERROR_ACCESS_DENIED);
735   dbus_message_unref (m);
736 }
737
738 static void
739 test_deny_receive (Fixture *f,
740     gconstpointer context)
741 {
742   DBusMessage *m;
743   const Config *config = context;
744
745   g_assert (config != NULL);
746   g_assert (config->bus_name != NULL);
747
748   if (f->address == NULL)
749     return;
750
751   if (!dbus_connection_add_filter (f->caller, caller_filter, f, NULL))
752     g_error ("OOM");
753
754   f->caller_filter_added = TRUE;
755
756   /* The sender sends a message to an activatable service.
757    * We set the interface name equal to the bus name to make it
758    * easier to write the necessary policy rules. */
759   m = dbus_message_new_method_call (config->bus_name, "/foo",
760                                     config->bus_name, "Call");
761   if (m == NULL)
762     g_error ("OOM");
763
764   dbus_connection_send (f->caller, m, NULL);
765   dbus_message_unref (m);
766
767   /* The fake systemd connects to the bus. */
768   f->systemd = test_connect_to_bus (f->ctx, f->address);
769   if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
770     g_error ("OOM");
771   f->systemd_filter_added = TRUE;
772   f->systemd_name = dbus_bus_get_unique_name (f->systemd);
773   take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
774
775   /* It gets its activation request. */
776   while (f->systemd_message == NULL)
777     test_main_context_iterate (f->ctx, TRUE);
778
779   m = f->systemd_message;
780   f->systemd_message = NULL;
781   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
782       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
783       "org.freedesktop.systemd1");
784   dbus_message_unref (m);
785
786   /* systemd starts the activatable service. */
787
788 #if defined(DBUS_TEST_APPARMOR_ACTIVATION) && defined(HAVE_APPARMOR_2_10)
789   /* The use of 42 here is arbitrary, see setup(). */
790   if (aa_change_hat (config->bus_name, 42) != 0)
791     g_error ("Unable to change profile to ...//^%s: %s",
792              config->bus_name, g_strerror (errno));
793 #endif
794
795   f->activated = test_connect_to_bus (f->ctx, f->address);
796   if (!dbus_connection_add_filter (f->activated, activated_filter,
797         f, NULL))
798     g_error ("OOM");
799   f->activated_filter_added = TRUE;
800   f->activated_name = dbus_bus_get_unique_name (f->activated);
801   take_well_known_name (f, f->activated, config->bus_name);
802
803 #if defined(DBUS_TEST_APPARMOR_ACTIVATION) && defined(HAVE_APPARMOR_2_10)
804   if (aa_change_hat (NULL, 42) != 0)
805     g_error ("Unable to change back to initial profile: %s",
806              g_strerror (errno));
807 #endif
808
809   /*
810    * We re-do the message matching, and now the message is
811    * forbidden by the receive policy.
812    *
813    * In the normal case, this is because the XML policy does not allow
814    * receiving any message with interface com.example.ReceiveDenied.
815    * We can't use the recipient's bus name here because the XML policy
816    * has no syntax for preventing the owner of a name from receiving
817    * messages - that would be pointless, because the sender could just
818    * open another connection and not own the same name on that connection.
819    *
820    * In the AppArmor case, this is because the AppArmor policy does not allow
821    * receiving messages with interface com.example.ReceiveDeniedByAppArmor
822    * from a peer with the same label we have. Again, we can't use the
823    * recipient's bus name because there is no syntax for this.
824    */
825   while (f->caller_message == NULL)
826     test_main_context_iterate (f->ctx, TRUE);
827
828   m = f->caller_message;
829   f->caller_message = NULL;
830   assert_error_reply (m, DBUS_SERVICE_DBUS, f->caller_name,
831       DBUS_ERROR_ACCESS_DENIED);
832   dbus_message_unref (m);
833
834   /* The activated service never even saw it. */
835   g_assert (f->activated_message == NULL);
836 }
837
838 /*
839  * Test that we can set up transient services.
840  *
841  * If (flags & FLAG_EARLY_TRANSIENT_SERVICE), we assert that a service that
842  * was deployed before starting systemd (in setup()) is available.
843  *
844  * Otherwise, we assert that a service that is deployed while dbus-daemon
845  * is already running becomes available after reloading the dbus-daemon
846  * configuration.
847  */
848 static void
849 test_transient_services (Fixture *f,
850     gconstpointer context)
851 {
852   const Config *config = context;
853   DBusMessage *m = NULL;
854   DBusMessage *send_reply = NULL;
855   DBusMessage *reply = NULL;
856   DBusPendingCall *pc;
857
858   g_assert (config != NULL);
859   g_assert (config->bus_name != NULL);
860
861   if (f->address == NULL)
862     return;
863
864   /* Connect the fake systemd to the bus. */
865   f->systemd = test_connect_to_bus (f->ctx, f->address);
866   if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
867     g_error ("OOM");
868   f->systemd_filter_added = TRUE;
869   f->systemd_name = dbus_bus_get_unique_name (f->systemd);
870   take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
871
872   if ((config->flags & FLAG_EARLY_TRANSIENT_SERVICE) == 0)
873     {
874       /* Try to activate a service that isn't there. */
875       m = dbus_message_new_method_call (config->bus_name,
876                                         "/foo", "com.example.bar", "Activate");
877
878       if (m == NULL ||
879           !dbus_connection_send_with_reply (f->caller, m, &pc,
880             DBUS_TIMEOUT_USE_DEFAULT) || pc == NULL)
881         g_error ("OOM");
882
883       dbus_message_unref (m);
884       m = NULL;
885
886       /* It fails. */
887
888       if (dbus_pending_call_get_completed (pc))
889         test_pending_call_store_reply (pc, &m);
890       else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
891             &m, NULL))
892         g_error ("OOM");
893
894       while (m == NULL)
895         test_main_context_iterate (f->ctx, TRUE);
896
897       assert_error_reply (m, DBUS_SERVICE_DBUS, f->caller_name,
898           DBUS_ERROR_SERVICE_UNKNOWN);
899
900       dbus_message_unref (m);
901       m = NULL;
902
903       /* Now generate a transient D-Bus service file for it. The directory
904        * should have been created during dbus-daemon startup, so we don't have to
905        * recreate it. */
906       fixture_create_transient_service (f, config->bus_name);
907
908       /* To guarantee that the transient service has been picked up, we have
909        * to reload. */
910       m = dbus_message_new_method_call (DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
911                                         DBUS_INTERFACE_DBUS, "ReloadConfig");
912
913       if (m == NULL ||
914           !dbus_connection_send_with_reply (f->caller, m, &pc,
915             DBUS_TIMEOUT_USE_DEFAULT) || pc == NULL)
916         g_error ("OOM");
917
918       dbus_message_unref (m);
919       m = NULL;
920
921       if (dbus_pending_call_get_completed (pc))
922         test_pending_call_store_reply (pc, &m);
923       else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
924             &m, NULL))
925         g_error ("OOM");
926
927       while (m == NULL)
928         test_main_context_iterate (f->ctx, TRUE);
929
930       assert_method_reply (m, DBUS_SERVICE_DBUS, f->caller_name, "");
931       dbus_message_unref (m);
932       m = NULL;
933     }
934
935   /* The service is present now. */
936   m = dbus_message_new_method_call (config->bus_name,
937                                     "/foo", "com.example.bar", "Activate");
938
939   if (m == NULL ||
940       !dbus_connection_send_with_reply (f->caller, m, &pc,
941         DBUS_TIMEOUT_USE_DEFAULT) || pc == NULL)
942     g_error ("OOM");
943
944   dbus_message_unref (m);
945   m = NULL;
946
947   if (dbus_pending_call_get_completed (pc))
948     test_pending_call_store_reply (pc, &reply);
949   else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
950         &reply, NULL))
951     g_error ("OOM");
952
953   /* The mock systemd is told to start the service. */
954   while (f->systemd_message == NULL)
955     test_main_context_iterate (f->ctx, TRUE);
956
957   m = f->systemd_message;
958   f->systemd_message = NULL;
959   assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
960       "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
961       "org.freedesktop.systemd1");
962   dbus_message_unref (m);
963   m = NULL;
964
965   /* The activatable service connects and gets its name. */
966   f->activated = test_connect_to_bus (f->ctx, f->address);
967   if (!dbus_connection_add_filter (f->activated, activated_filter,
968         f, NULL))
969     g_error ("OOM");
970   f->activated_filter_added = TRUE;
971   f->activated_name = dbus_bus_get_unique_name (f->activated);
972   take_well_known_name (f, f->activated, config->bus_name);
973
974   /* The message is delivered to the activatable service. */
975   while (f->activated_message == NULL)
976     test_main_context_iterate (f->ctx, TRUE);
977
978   m = f->activated_message;
979   f->activated_message = NULL;
980   assert_method_call (m, f->caller_name, config->bus_name, "/foo",
981       "com.example.bar", "Activate", "");
982
983   /* The activatable service sends back a reply. */
984   send_reply = dbus_message_new_method_return (m);
985
986   if (send_reply == NULL ||
987       !dbus_connection_send (f->activated, send_reply, NULL))
988     g_error ("OOM");
989
990   dbus_message_unref (send_reply);
991   send_reply = NULL;
992   dbus_message_unref (m);
993   m = NULL;
994
995   /* The caller receives the reply. */
996   while (reply == NULL)
997     test_main_context_iterate (f->ctx, TRUE);
998
999   assert_method_reply (reply, f->activated_name, f->caller_name, "");
1000   dbus_message_unref (reply);
1001   reply = NULL;
1002 }
1003
1004 static void
1005 teardown (Fixture *f,
1006     gconstpointer context G_GNUC_UNUSED)
1007 {
1008   dbus_error_free (&f->e);
1009   g_clear_error (&f->ge);
1010
1011   if (f->caller != NULL)
1012     {
1013       if (f->caller_filter_added)
1014         dbus_connection_remove_filter (f->caller, caller_filter, f);
1015
1016       dbus_connection_close (f->caller);
1017       dbus_connection_unref (f->caller);
1018       f->caller = NULL;
1019     }
1020
1021   if (f->systemd != NULL)
1022     {
1023       if (f->systemd_filter_added)
1024         dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1025
1026       dbus_connection_close (f->systemd);
1027       dbus_connection_unref (f->systemd);
1028       f->systemd = NULL;
1029     }
1030
1031   if (f->activated != NULL)
1032     {
1033       if (f->activated_filter_added)
1034         dbus_connection_remove_filter (f->activated, activated_filter, f);
1035
1036       dbus_connection_close (f->activated);
1037       dbus_connection_unref (f->activated);
1038       f->activated = NULL;
1039     }
1040
1041   if (f->daemon_pid != 0)
1042     {
1043       test_kill_pid (f->daemon_pid);
1044       g_spawn_close_pid (f->daemon_pid);
1045     }
1046
1047   if (f->ctx != NULL)
1048     test_main_context_unref (f->ctx);
1049
1050   g_free (f->address);
1051
1052   if (f->transient_service_file != NULL)
1053     {
1054       test_remove_if_exists (f->transient_service_file);
1055       g_free (f->transient_service_file);
1056     }
1057
1058   if (f->tmp_runtime_dir != NULL)
1059     {
1060       gchar *dbus1 = g_build_filename (f->tmp_runtime_dir, "dbus-1", NULL);
1061       gchar *services = g_build_filename (dbus1, "services", NULL);
1062
1063       test_rmdir_if_exists (services);
1064       test_rmdir_if_exists (dbus1);
1065       test_rmdir_if_exists (f->tmp_runtime_dir);
1066
1067       g_free (f->tmp_runtime_dir);
1068       g_free (dbus1);
1069       g_free (services);
1070     }
1071 }
1072
1073 static const Config deny_send_tests[] =
1074 {
1075 #if defined(DBUS_TEST_APPARMOR_ACTIVATION)
1076     { "com.example.SendDeniedByAppArmorLabel" },
1077     { "com.example.SendDeniedByNonexistentAppArmorLabel" },
1078     { "com.example.SendDeniedByAppArmorName" },
1079 #endif
1080     { "com.example.SendDenied" }
1081 };
1082
1083 static const Config deny_receive_tests[] =
1084 {
1085 #if defined(DBUS_TEST_APPARMOR_ACTIVATION)
1086     { "com.example.ReceiveDeniedByAppArmorLabel" },
1087 #endif
1088     { "com.example.ReceiveDenied" }
1089 };
1090
1091 static const Config transient_service_later =
1092 {
1093   "com.example.TransientActivatable1",
1094   FLAG_NONE
1095 };
1096
1097 static const Config transient_service_in_advance =
1098 {
1099   "com.example.TransientActivatable1",
1100   FLAG_EARLY_TRANSIENT_SERVICE
1101 };
1102
1103 int
1104 main (int argc,
1105     char **argv)
1106 {
1107   gsize i;
1108
1109   test_init (&argc, &argv);
1110
1111   g_test_add ("/sd-activation/activation", Fixture, NULL,
1112       setup, test_activation, teardown);
1113   g_test_add ("/sd-activation/uae", Fixture, NULL,
1114       setup, test_uae, teardown);
1115
1116   for (i = 0; i < G_N_ELEMENTS (deny_send_tests); i++)
1117     {
1118       gchar *name = g_strdup_printf ("/sd-activation/deny-send/%s",
1119                                      deny_send_tests[i].bus_name);
1120
1121       g_test_add (name, Fixture, &deny_send_tests[i],
1122                   setup, test_deny_send, teardown);
1123       g_free (name);
1124     }
1125
1126   for (i = 0; i < G_N_ELEMENTS (deny_receive_tests); i++)
1127     {
1128       gchar *name = g_strdup_printf ("/sd-activation/deny-receive/%s",
1129                                      deny_receive_tests[i].bus_name);
1130
1131       g_test_add (name, Fixture, &deny_receive_tests[i],
1132                   setup, test_deny_receive, teardown);
1133       g_free (name);
1134     }
1135
1136   g_test_add ("/sd-activation/transient-services/later", Fixture,
1137       &transient_service_later, setup, test_transient_services, teardown);
1138   g_test_add ("/sd-activation/transient-services/in-advance", Fixture,
1139       &transient_service_in_advance, setup, test_transient_services, teardown);
1140
1141   return g_test_run ();
1142 }