Fix race condition in gapplication/basic test
[platform/upstream/glib.git] / gio / tests / gapplication.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 #include "gdbus-tests.h"
7 #include "gdbus-sessionbus.h"
8
9 static gint outstanding_watches;
10 static GMainLoop *main_loop;
11
12 typedef struct
13 {
14   const gchar *expected_stdout;
15   gint stdout_pipe;
16 } ChildData;
17
18 static void
19 child_quit (GPid     pid,
20             gint     status,
21             gpointer data)
22 {
23   ChildData *child = data;
24   gssize expected, actual;
25   gchar *buffer;
26
27   g_assert_cmpint (status, ==, 0);
28
29   if (--outstanding_watches == 0)
30     g_main_loop_quit (main_loop);
31
32   expected = strlen (child->expected_stdout);
33   buffer = g_alloca (expected + 100);
34   actual = read (child->stdout_pipe, buffer, expected + 100);
35   close (child->stdout_pipe);
36
37   g_assert_cmpint (actual, >=, 0);
38
39   if (actual != expected ||
40       memcmp (buffer, child->expected_stdout, expected) != 0)
41     {
42       buffer[MIN(expected + 100, actual)] = '\0';
43
44       g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
45                child->expected_stdout,
46                (actual > expected) ? "truncated" : "full", buffer);
47     }
48
49   g_slice_free (ChildData, child);
50 }
51
52 static void
53 spawn (const gchar *expected_stdout,
54        const gchar *first_arg,
55        ...)
56 {
57   GError *error = NULL;
58   const gchar *arg;
59   GPtrArray *array;
60   ChildData *data;
61   gchar **args;
62   va_list ap;
63   GPid pid;
64   GPollFD fd;
65
66   va_start (ap, first_arg);
67   array = g_ptr_array_new ();
68   g_ptr_array_add (array, g_strdup ("./basic-application"));
69   for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
70     g_ptr_array_add (array, g_strdup (arg));
71   g_ptr_array_add (array, NULL);
72   args = (gchar **) g_ptr_array_free (array, FALSE);
73
74   va_end (ap);
75
76   data = g_slice_new (ChildData);
77   data->expected_stdout = expected_stdout;
78
79   g_spawn_async_with_pipes (NULL, args, NULL,
80                             G_SPAWN_DO_NOT_REAP_CHILD,
81                             NULL, NULL, &pid, NULL,
82                             &data->stdout_pipe, NULL, &error);
83   g_assert_no_error (error);
84
85   g_child_watch_add (pid, child_quit, data);
86   outstanding_watches++;
87
88   /* we block until the children write to stdout to make sure
89    * they have started, as they need to be executed in order;
90    * see https://bugzilla.gnome.org/show_bug.cgi?id=664627
91    */
92   fd.fd = data->stdout_pipe;
93   fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
94   g_poll (&fd, 1, -1);
95 }
96
97 static void
98 basic (void)
99 {
100   GDBusConnection *c;
101
102   session_bus_up ();
103   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
104
105   main_loop = g_main_loop_new (NULL, 0);
106
107   /* spawn the master */
108   spawn ("activated\n"
109          "open file:///a file:///b\n"
110          "cmdline '40 +' '2'\n"
111          "exit status: 0\n",
112          "./app", NULL);
113
114   /* send it some files */
115   spawn ("exit status: 0\n",
116          "./app", "/a", "/b", NULL);
117
118   spawn ("40 + 2 = 42\n"
119          "exit status: 42\n",
120          "./cmd", "40 +", "2", NULL);
121
122   g_main_loop_run (main_loop);
123
124   g_object_unref (c);
125   session_bus_down ();
126 }
127
128
129 #if 0
130 /* Now that we register non-unique apps on the bus we need to fix the
131  * following test not to assume that it's safe to create multiple instances
132  * of the same app in one process.
133  *
134  * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
135  * introduced this problem.
136  */
137
138 static GApplication *recently_activated;
139 static GMainLoop *loop;
140
141 static void
142 nonunique_activate (GApplication *application)
143 {
144   recently_activated = application;
145
146   if (loop != NULL)
147     g_main_loop_quit (loop);
148 }
149
150 static GApplication *
151 make_app (gboolean non_unique)
152 {
153   GApplication *app;
154   gboolean ok;
155
156   app = g_application_new ("org.gtk.Test-Application",
157                            non_unique ? G_APPLICATION_NON_UNIQUE : 0);
158   g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
159   ok = g_application_register (app, NULL, NULL);
160   if (!ok)
161     {
162       g_object_unref (app);
163       return NULL;
164     }
165
166   g_application_activate (app);
167
168   return app;
169 }
170
171 static void
172 test_nonunique (void)
173 {
174   GApplication *first, *second, *third, *fourth;
175
176   session_bus_up ();
177
178   first = make_app (TRUE);
179   /* non-remote because it is non-unique */
180   g_assert (!g_application_get_is_remote (first));
181   g_assert (recently_activated == first);
182   recently_activated = NULL;
183
184   second = make_app (FALSE);
185   /* non-remote because it is first */
186   g_assert (!g_application_get_is_remote (second));
187   g_assert (recently_activated == second);
188   recently_activated = NULL;
189
190   third = make_app (TRUE);
191   /* non-remote because it is non-unique */
192   g_assert (!g_application_get_is_remote (third));
193   g_assert (recently_activated == third);
194   recently_activated = NULL;
195
196   fourth = make_app (FALSE);
197   /* should have failed to register due to being
198    * unable to register the object paths
199    */
200   g_assert (fourth == NULL);
201   g_assert (recently_activated == NULL);
202
203   g_object_unref (first);
204   g_object_unref (second);
205   g_object_unref (third);
206
207   session_bus_down ();
208 }
209 #endif
210
211 static void
212 properties (void)
213 {
214   GDBusConnection *c;
215   GObject *app;
216   gchar *id;
217   GApplicationFlags flags;
218   gboolean registered;
219   guint timeout;
220   gboolean remote;
221   gboolean ret;
222   GError *error = NULL;
223
224   session_bus_up ();
225   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
226
227   app = g_object_new (G_TYPE_APPLICATION,
228                       "application-id", "org.gtk.TestApplication",
229                       NULL);
230
231   g_object_get (app,
232                 "application-id", &id,
233                 "flags", &flags,
234                 "is-registered", &registered,
235                 "inactivity-timeout", &timeout,
236                 NULL);
237
238   g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
239   g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
240   g_assert (!registered);
241   g_assert_cmpint (timeout, ==, 0);
242
243   ret = g_application_register (G_APPLICATION (app), NULL, &error);
244   g_assert (ret);
245   g_assert_no_error (error);
246
247   g_object_get (app,
248                 "is-registered", &registered,
249                 "is-remote", &remote,
250                 NULL);
251
252   g_assert (registered);
253   g_assert (!remote);
254
255   g_object_set (app,
256                 "inactivity-timeout", 1000,
257                 NULL);
258
259   g_application_quit (G_APPLICATION (app));
260
261   g_object_unref (c);
262   g_object_unref (app);
263   g_free (id);
264
265   session_bus_down ();
266 }
267
268 static void
269 appid (void)
270 {
271   gchar *id;
272
273   g_assert (!g_application_id_is_valid (""));
274   g_assert (!g_application_id_is_valid ("."));
275   g_assert (!g_application_id_is_valid ("a"));
276   g_assert (!g_application_id_is_valid ("abc"));
277   g_assert (!g_application_id_is_valid (".abc"));
278   g_assert (!g_application_id_is_valid ("abc."));
279   g_assert (!g_application_id_is_valid ("a..b"));
280   g_assert (!g_application_id_is_valid ("a/b"));
281   g_assert (!g_application_id_is_valid ("a\nb"));
282   g_assert (!g_application_id_is_valid ("a\nb"));
283   g_assert (!g_application_id_is_valid ("_a.b"));
284   g_assert (!g_application_id_is_valid ("-a.b"));
285   id = g_new0 (gchar, 261);
286   memset (id, 'a', 260);
287   id[1] = '.';
288   id[260] = 0;
289   g_assert (!g_application_id_is_valid (id));
290   g_free (id);
291
292   g_assert (g_application_id_is_valid ("a.b"));
293   g_assert (g_application_id_is_valid ("A.B"));
294   g_assert (g_application_id_is_valid ("A-.B"));
295   g_assert (g_application_id_is_valid ("a_b.c-d"));
296   g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
297 }
298
299 static gboolean nodbus_activated;
300
301 static gboolean
302 release_app (gpointer user_data)
303 {
304   g_application_release (user_data);
305   return G_SOURCE_REMOVE;
306 }
307
308 static void
309 nodbus_activate (GApplication *app)
310 {
311   nodbus_activated = TRUE;
312   g_application_hold (app);
313
314   g_assert (g_application_get_dbus_connection (app) == NULL);
315   g_assert (g_application_get_dbus_object_path (app) == NULL);
316
317   g_idle_add (release_app, app);
318 }
319
320 static void
321 test_nodbus (void)
322 {
323   gchar *argv[] = { "./unimportant", NULL };
324   GApplication *app;
325
326   app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
327   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
328   g_application_run (app, 1, argv);
329   g_object_unref (app);
330
331   g_assert (nodbus_activated);
332 }
333
334 static gboolean noappid_activated;
335
336 static void
337 noappid_activate (GApplication *app)
338 {
339   noappid_activated = TRUE;
340   g_application_hold (app);
341
342   g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
343
344   g_idle_add (release_app, app);
345 }
346
347 /* test that no appid -> non-unique */
348 static void
349 test_noappid (void)
350 {
351   gchar *argv[] = { "./unimportant", NULL };
352   GApplication *app;
353
354   app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
355   g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
356   g_application_run (app, 1, argv);
357   g_object_unref (app);
358
359   g_assert (noappid_activated);
360 }
361
362
363 static gboolean
364 quit_app (gpointer user_data)
365 {
366   g_application_quit (user_data);
367   return G_SOURCE_REMOVE;
368 }
369
370 static gboolean quit_activated;
371
372 static void
373 quit_activate (GApplication *app)
374 {
375   quit_activated = TRUE;
376   g_application_hold (app);
377
378   g_assert (g_application_get_dbus_connection (app) != NULL);
379   g_assert (g_application_get_dbus_object_path (app) != NULL);
380
381   g_idle_add (quit_app, app);
382 }
383
384 static void
385 test_quit (void)
386 {
387   GDBusConnection *c;
388   gchar *argv[] = { "./unimportant", NULL };
389   GApplication *app;
390
391   session_bus_up ();
392   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
393
394   app = g_application_new ("org.gtk.Unimportant",
395                            G_APPLICATION_FLAGS_NONE);
396   g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
397   g_application_run (app, 1, argv);
398   g_object_unref (app);
399   g_object_unref (c);
400
401   g_assert (quit_activated);
402
403   session_bus_down ();
404 }
405
406 static void
407 on_activate (GApplication *app)
408 {
409   gchar **actions;
410   GAction *action;
411   GVariant *state;
412
413   g_assert (!g_application_get_is_remote (app));
414
415   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
416   g_assert (g_strv_length (actions) == 0);
417   g_strfreev (actions);
418
419   action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
420   g_action_map_add_action (G_ACTION_MAP (app), action);
421
422   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
423   g_assert (g_strv_length (actions) == 1);
424   g_strfreev (actions);
425
426   g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
427   state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
428   g_assert (g_variant_get_boolean (state) == TRUE);
429
430   g_action_map_remove_action (G_ACTION_MAP (app), "test");
431
432   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
433   g_assert (g_strv_length (actions) == 0);
434   g_strfreev (actions);
435
436   g_idle_add (quit_app, app);
437 }
438
439 static void
440 test_actions (void)
441 {
442   gchar *argv[] = { "./unimportant", NULL };
443   GApplication *app;
444
445   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
446
447   app = g_application_new ("org.gtk.Unimportant",
448                            G_APPLICATION_FLAGS_NONE);
449   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
450   g_application_run (app, 1, argv);
451   g_object_unref (app);
452 }
453
454 typedef GApplication TestLocCmdApp;
455 typedef GApplicationClass TestLocCmdAppClass;
456
457 static GType test_loc_cmd_app_get_type (void);
458 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
459
460 static void
461 test_loc_cmd_app_init (TestLocCmdApp *app)
462 {
463 }
464
465 static void
466 test_loc_cmd_app_startup (GApplication *app)
467 {
468   g_assert_not_reached ();
469 }
470
471 static void
472 test_loc_cmd_app_shutdown (GApplication *app)
473 {
474   g_assert_not_reached ();
475 }
476
477 static gboolean
478 test_loc_cmd_app_local_command_line (GApplication   *application,
479                                      gchar        ***arguments,
480                                      gint           *exit_status)
481 {
482   return TRUE;
483 }
484
485 static void
486 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
487 {
488   G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
489   G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
490   G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
491 }
492
493 static void
494 test_local_command_line (void)
495 {
496   gchar *argv[] = { "./unimportant", "-invalid", NULL };
497   GApplication *app;
498
499   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
500
501   app = g_object_new (test_loc_cmd_app_get_type (),
502                       "application-id", "org.gtk.Unimportant",
503                       "flags", G_APPLICATION_FLAGS_NONE,
504                       NULL);
505   g_application_run (app, 1, argv);
506   g_object_unref (app);
507 }
508
509 int
510 main (int argc, char **argv)
511 {
512   g_test_init (&argc, &argv, NULL);
513
514   g_test_dbus_unset ();
515
516   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
517   g_test_add_func ("/gapplication/basic", basic);
518   g_test_add_func ("/gapplication/no-appid", test_noappid);
519 /*  g_test_add_func ("/gapplication/non-unique", test_nonunique); */
520   g_test_add_func ("/gapplication/properties", properties);
521   g_test_add_func ("/gapplication/app-id", appid);
522   g_test_add_func ("/gapplication/quit", test_quit);
523   g_test_add_func ("/gapplication/actions", test_actions);
524   g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
525
526   return g_test_run ();
527 }