tests: Fix use-after-free in the appinfo tests
[glib.git] / gio / tests / appinfo.c
blobd9d61b605fd93901b0c1872670785c5dd00b2b80
2 #include <locale.h>
3 #include <string.h>
5 #include <glib/gstdio.h>
6 #include <gio/gio.h>
7 #include <gio/gdesktopappinfo.h>
9 static void
10 test_launch_for_app_info (GAppInfo *appinfo)
12 GError *error;
13 GFile *file;
14 GList *l;
15 const gchar *path;
16 gchar *uri;
18 if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
20 g_printerr ("No DISPLAY. Skipping test. ");
21 return;
24 error = NULL;
25 g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
26 g_assert_no_error (error);
28 g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
29 g_assert_no_error (error);
31 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
32 file = g_file_new_for_path (path);
33 l = NULL;
34 l = g_list_append (l, file);
36 g_assert (g_app_info_launch (appinfo, l, NULL, &error));
37 g_assert_no_error (error);
38 g_list_free (l);
39 g_object_unref (file);
41 l = NULL;
42 uri = g_strconcat ("file://", g_test_get_dir (G_TEST_DIST), "/appinfo-test.desktop", NULL);
43 l = g_list_append (l, uri);
44 l = g_list_append (l, "file:///etc/group#adm");
46 g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
47 g_assert_no_error (error);
48 g_list_free (l);
49 g_free (uri);
52 static void
53 test_launch (void)
55 GAppInfo *appinfo;
56 const gchar *path;
58 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
59 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
60 g_assert (appinfo != NULL);
62 test_launch_for_app_info (appinfo);
63 g_object_unref (appinfo);
66 static void
67 test_launch_no_app_id (void)
69 const gchar desktop_file_base_contents[] =
70 "[Desktop Entry]\n"
71 "Type=Application\n"
72 "GenericName=generic-appinfo-test\n"
73 "Name=appinfo-test\n"
74 "Name[de]=appinfo-test-de\n"
75 "X-GNOME-FullName=example\n"
76 "X-GNOME-FullName[de]=Beispiel\n"
77 "Comment=GAppInfo example\n"
78 "Comment[de]=GAppInfo Beispiel\n"
79 "Icon=testicon.svg\n"
80 "Terminal=true\n"
81 "StartupNotify=true\n"
82 "StartupWMClass=appinfo-class\n"
83 "MimeType=image/png;image/jpeg;\n"
84 "Keywords=keyword1;test keyword;\n"
85 "Categories=GNOME;GTK;\n";
87 const char *exec_line_variants[] = {
88 "Exec=./appinfo-test --option %U %i --name %c --filename %k %m %%",
89 "Exec=./appinfo-test --option %u %i --name %c --filename %k %m %%"
92 gsize i;
94 g_test_bug ("791337");
96 for (i = 0; i < G_N_ELEMENTS (exec_line_variants); i++)
98 gchar *desktop_file_contents;
99 GKeyFile *fake_desktop_file;
100 GAppInfo *appinfo;
101 gboolean loaded;
103 g_test_message ("Exec line variant #%" G_GSIZE_FORMAT, i);
105 desktop_file_contents = g_strdup_printf ("%s\n%s",
106 desktop_file_base_contents,
107 exec_line_variants[i]);
109 /* We load a desktop file from memory to force the app not
110 * to have an app ID, which would check different codepaths.
112 fake_desktop_file = g_key_file_new ();
113 loaded = g_key_file_load_from_data (fake_desktop_file, desktop_file_contents, -1, G_KEY_FILE_NONE, NULL);
114 g_assert_true (loaded);
116 appinfo = (GAppInfo*)g_desktop_app_info_new_from_keyfile (fake_desktop_file);
117 g_assert (appinfo != NULL);
119 test_launch_for_app_info (appinfo);
121 g_free (desktop_file_contents);
122 g_object_unref (appinfo);
123 g_key_file_unref (fake_desktop_file);
127 static void
128 test_locale (const char *locale)
130 GAppInfo *appinfo;
131 gchar *orig = NULL;
132 const gchar *path;
134 orig = g_strdup (setlocale (LC_ALL, NULL));
135 g_setenv ("LANGUAGE", locale, TRUE);
136 setlocale (LC_ALL, "");
138 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
139 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
141 if (g_strcmp0 (locale, "C") == 0)
143 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
144 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
145 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
147 else if (g_str_has_prefix (locale, "en"))
149 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
150 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
151 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
153 else if (g_str_has_prefix (locale, "de"))
155 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
156 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
157 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
160 g_object_unref (appinfo);
162 g_setenv ("LANGUAGE", orig, TRUE);
163 setlocale (LC_ALL, "");
164 g_free (orig);
167 static void
168 test_text (void)
170 test_locale ("C");
171 test_locale ("en_US");
172 test_locale ("de");
173 test_locale ("de_DE.UTF-8");
176 static void
177 test_basic (void)
179 GAppInfo *appinfo;
180 GAppInfo *appinfo2;
181 GIcon *icon, *icon2;
182 const gchar *path;
184 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
185 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
187 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test.desktop");
188 g_assert (strstr (g_app_info_get_executable (appinfo), "appinfo-test") != NULL);
190 icon = g_app_info_get_icon (appinfo);
191 g_assert (G_IS_THEMED_ICON (icon));
192 icon2 = g_themed_icon_new ("testicon");
193 g_assert (g_icon_equal (icon, icon2));
194 g_object_unref (icon2);
196 appinfo2 = g_app_info_dup (appinfo);
197 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
198 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
200 g_object_unref (appinfo);
201 g_object_unref (appinfo2);
204 static void
205 test_show_in (void)
207 GAppInfo *appinfo;
208 const gchar *path;
210 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
211 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
212 g_assert (g_app_info_should_show (appinfo));
213 g_object_unref (appinfo);
215 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-gnome.desktop", NULL);
216 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
217 g_assert (g_app_info_should_show (appinfo));
218 g_object_unref (appinfo);
220 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-notgnome.desktop", NULL);
221 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
222 g_assert (!g_app_info_should_show (appinfo));
223 g_object_unref (appinfo);
226 static void
227 test_commandline (void)
229 GAppInfo *appinfo;
230 GError *error;
231 gchar *cmdline;
232 gchar *cmdline_out;
234 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
235 cmdline_out = g_strconcat (cmdline, " %u", NULL);
237 error = NULL;
238 appinfo = g_app_info_create_from_commandline (cmdline,
239 "cmdline-app-test",
240 G_APP_INFO_CREATE_SUPPORTS_URIS,
241 &error);
242 g_assert (appinfo != NULL);
243 g_assert_no_error (error);
244 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
245 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
246 g_assert (g_app_info_supports_uris (appinfo));
247 g_assert (!g_app_info_supports_files (appinfo));
249 g_object_unref (appinfo);
251 g_free (cmdline_out);
252 cmdline_out = g_strconcat (cmdline, " %f", NULL);
254 error = NULL;
255 appinfo = g_app_info_create_from_commandline (cmdline,
256 "cmdline-app-test",
257 G_APP_INFO_CREATE_NONE,
258 &error);
259 g_assert (appinfo != NULL);
260 g_assert_no_error (error);
261 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
262 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
263 g_assert (!g_app_info_supports_uris (appinfo));
264 g_assert (g_app_info_supports_files (appinfo));
266 g_object_unref (appinfo);
268 g_free (cmdline);
269 g_free (cmdline_out);
272 static void
273 test_launch_context (void)
275 GAppLaunchContext *context;
276 GAppInfo *appinfo;
277 gchar *str;
278 gchar *cmdline;
280 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
282 context = g_app_launch_context_new ();
283 appinfo = g_app_info_create_from_commandline (cmdline,
284 "cmdline-app-test",
285 G_APP_INFO_CREATE_SUPPORTS_URIS,
286 NULL);
288 str = g_app_launch_context_get_display (context, appinfo, NULL);
289 g_assert (str == NULL);
291 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
292 g_assert (str == NULL);
294 g_object_unref (appinfo);
295 g_object_unref (context);
297 g_free (cmdline);
300 static gboolean launched_reached;
302 static void
303 launched (GAppLaunchContext *context,
304 GAppInfo *info,
305 GVariant *platform_data,
306 gpointer user_data)
308 gint pid;
310 pid = 0;
311 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
312 g_assert (pid != 0);
314 launched_reached = TRUE;
317 static void
318 launch_failed (GAppLaunchContext *context,
319 const gchar *startup_notify_id)
321 g_assert_not_reached ();
324 static void
325 test_launch_context_signals (void)
327 GAppLaunchContext *context;
328 GAppInfo *appinfo;
329 GError *error = NULL;
330 gchar *cmdline;
332 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
334 context = g_app_launch_context_new ();
335 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
336 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
337 appinfo = g_app_info_create_from_commandline (cmdline,
338 "cmdline-app-test",
339 G_APP_INFO_CREATE_SUPPORTS_URIS,
340 NULL);
342 error = NULL;
343 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
344 g_assert_no_error (error);
346 g_assert (launched_reached);
348 g_object_unref (appinfo);
349 g_object_unref (context);
351 g_free (cmdline);
354 static void
355 test_tryexec (void)
357 GAppInfo *appinfo;
358 const gchar *path;
360 path = g_test_get_filename (G_TEST_DIST, "appinfo-test2.desktop", NULL);
361 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
363 g_assert (appinfo == NULL);
366 /* Test that we can set an appinfo as default for a mime type or
367 * file extension, and also add and remove handled mime types.
369 static void
370 test_associations (void)
372 GAppInfo *appinfo;
373 GAppInfo *appinfo2;
374 GError *error;
375 gboolean result;
376 GList *list;
377 gchar *cmdline;
379 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
380 appinfo = g_app_info_create_from_commandline (cmdline,
381 "cmdline-app-test",
382 G_APP_INFO_CREATE_SUPPORTS_URIS,
383 NULL);
385 error = NULL;
386 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
388 g_assert (result);
389 g_assert_no_error (error);
391 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
393 g_assert (appinfo2);
394 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
396 g_object_unref (appinfo2);
398 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
399 g_assert (result);
400 g_assert_no_error (error);
402 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
404 g_assert (appinfo2);
405 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
407 g_object_unref (appinfo2);
409 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
410 g_assert (result);
411 g_assert_no_error (error);
413 list = g_app_info_get_all_for_type ("application/x-gio-test");
414 g_assert_cmpint (g_list_length (list), ==, 1);
415 appinfo2 = list->data;
416 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
417 g_object_unref (appinfo2);
418 g_list_free (list);
420 g_assert (g_app_info_can_remove_supports_type (appinfo));
421 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
422 g_assert_no_error (error);
424 g_assert (g_app_info_can_delete (appinfo));
425 g_assert (g_app_info_delete (appinfo));
426 g_object_unref (appinfo);
429 static void
430 test_environment (void)
432 GAppLaunchContext *ctx;
433 gchar **env;
434 const gchar *path;
436 g_unsetenv ("FOO");
437 g_unsetenv ("BLA");
438 path = g_getenv ("PATH");
440 ctx = g_app_launch_context_new ();
442 env = g_app_launch_context_get_environment (ctx);
444 g_assert (g_environ_getenv (env, "FOO") == NULL);
445 g_assert (g_environ_getenv (env, "BLA") == NULL);
446 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
448 g_strfreev (env);
450 g_app_launch_context_setenv (ctx, "FOO", "bar");
451 g_app_launch_context_setenv (ctx, "BLA", "bla");
453 env = g_app_launch_context_get_environment (ctx);
455 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
456 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
457 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
459 g_strfreev (env);
461 g_app_launch_context_setenv (ctx, "FOO", "baz");
462 g_app_launch_context_unsetenv (ctx, "BLA");
464 env = g_app_launch_context_get_environment (ctx);
466 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
467 g_assert (g_environ_getenv (env, "BLA") == NULL);
469 g_strfreev (env);
471 g_object_unref (ctx);
474 static void
475 test_startup_wm_class (void)
477 GDesktopAppInfo *appinfo;
478 const char *wm_class;
479 const gchar *path;
481 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
482 appinfo = g_desktop_app_info_new_from_filename (path);
483 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
485 g_assert_cmpstr (wm_class, ==, "appinfo-class");
487 g_object_unref (appinfo);
490 static void
491 test_supported_types (void)
493 GAppInfo *appinfo;
494 const char * const *content_types;
495 const gchar *path;
497 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
498 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
499 content_types = g_app_info_get_supported_types (appinfo);
501 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
502 g_assert_cmpstr (content_types[0], ==, "image/png");
504 g_object_unref (appinfo);
507 static void
508 test_from_keyfile (void)
510 GDesktopAppInfo *info;
511 GKeyFile *kf;
512 GError *error = NULL;
513 const gchar *categories;
514 gchar **keywords;
515 const gchar *file;
516 const gchar *name;
517 const gchar *path;
519 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
520 kf = g_key_file_new ();
521 g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
522 g_assert_no_error (error);
523 info = g_desktop_app_info_new_from_keyfile (kf);
524 g_key_file_free (kf);
525 g_assert (info != NULL);
527 g_object_get (info, "filename", &file, NULL);
528 g_assert (file == NULL);
530 file = g_desktop_app_info_get_filename (info);
531 g_assert (file == NULL);
532 categories = g_desktop_app_info_get_categories (info);
533 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
534 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
535 g_assert_cmpint (g_strv_length (keywords), ==, 2);
536 g_assert_cmpstr (keywords[0], ==, "keyword1");
537 g_assert_cmpstr (keywords[1], ==, "test keyword");
538 name = g_desktop_app_info_get_generic_name (info);
539 g_assert_cmpstr (name, ==, "generic-appinfo-test");
540 g_assert (!g_desktop_app_info_get_nodisplay (info));
542 g_object_unref (info);
546 main (int argc, char *argv[])
548 const gchar *build_dir;
550 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE);
552 g_test_init (&argc, &argv, NULL);
553 g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
555 /* With Meson build we need to change into right directory, so that the
556 * appinfo-test binary can be found. */
557 build_dir = g_getenv ("G_TEST_BUILDDIR");
558 if (build_dir)
559 g_chdir (build_dir);
561 g_test_add_func ("/appinfo/basic", test_basic);
562 g_test_add_func ("/appinfo/text", test_text);
563 g_test_add_func ("/appinfo/launch", test_launch);
564 g_test_add_func ("/appinfo/launch/no-appid", test_launch_no_app_id);
565 g_test_add_func ("/appinfo/show-in", test_show_in);
566 g_test_add_func ("/appinfo/commandline", test_commandline);
567 g_test_add_func ("/appinfo/launch-context", test_launch_context);
568 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
569 g_test_add_func ("/appinfo/tryexec", test_tryexec);
570 g_test_add_func ("/appinfo/associations", test_associations);
571 g_test_add_func ("/appinfo/environment", test_environment);
572 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
573 g_test_add_func ("/appinfo/supported-types", test_supported_types);
574 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
576 return g_test_run ();