1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "fd-util.h"
5 #include "fs-util.h"
6 #include "string-util.h"
7 #include "strv.h"
8 #include "tests.h"
9 #include "tmpfile-util.h"
10 #include "xdg-autostart-service.h"
11
TEST(translate_name)12 TEST(translate_name) {
13 _cleanup_free_ char *t;
14
15 assert_se(t = xdg_autostart_service_translate_name("a-b.blub.desktop"));
16 assert_se(streq(t, "app-a\\x2db.blub@autostart.service"));
17 }
18
test_xdg_format_exec_start_one(const char * exec,const char * expected)19 static void test_xdg_format_exec_start_one(const char *exec, const char *expected) {
20 _cleanup_free_ char* out = NULL;
21
22 xdg_autostart_format_exec_start(exec, &out);
23 log_info("In: '%s', out: '%s', expected: '%s'", exec, out, expected);
24 assert_se(streq(out, expected));
25 }
26
TEST(xdg_format_exec_start)27 TEST(xdg_format_exec_start) {
28 test_xdg_format_exec_start_one("/bin/sleep 100", "/bin/sleep 100");
29
30 /* All standardised % identifiers are stripped. */
31 test_xdg_format_exec_start_one("/bin/sleep %f \"%F\" %u %U %d %D\t%n %N %i %c %k %v %m", "/bin/sleep");
32
33 /* Unknown % identifier currently remain, but are escaped. */
34 test_xdg_format_exec_start_one("/bin/sleep %X \"%Y\"", "/bin/sleep %%X %%Y");
35
36 test_xdg_format_exec_start_one("/bin/sleep \";\\\"\"", "/bin/sleep \";\\\"\"");
37 }
38
39 static const char* const xdg_desktop_file[] = {
40 ("[Desktop Entry]\n"
41 "Exec\t =\t /bin/sleep 100\n" /* Whitespace Before/After = must be ignored */
42 "OnlyShowIn = A;B;\n"
43 "NotShowIn=C;;D\\\\\\;;E\n"), /* "C", "", "D\;", "E" */
44
45 ("[Desktop Entry]\n"
46 "Exec=a\n"
47 "Exec=b\n"),
48
49 ("[Desktop Entry]\n"
50 "Hidden=\t true\n"),
51 };
52
test_xdg_desktop_parse_one(unsigned i,const char * s)53 static void test_xdg_desktop_parse_one(unsigned i, const char *s) {
54 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-xdg-autostart-parser.XXXXXX";
55 _cleanup_fclose_ FILE *f = NULL;
56 _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
57
58 log_info("== %s[%i] ==", __func__, i);
59
60 assert_se(fmkostemp_safe(name, "r+", &f) == 0);
61 assert_se(fwrite(s, strlen(s), 1, f) == 1);
62 rewind(f);
63
64 assert_se(service = xdg_autostart_service_parse_desktop(name));
65
66 switch (i) {
67 case 0:
68 assert_se(streq(service->exec_string, "/bin/sleep 100"));
69 assert_se(strv_equal(service->only_show_in, STRV_MAKE("A", "B")));
70 assert_se(strv_equal(service->not_show_in, STRV_MAKE("C", "D\\;", "E")));
71 assert_se(!service->hidden);
72 break;
73 case 1:
74 /* The second entry is not permissible and will be ignored (and error logged). */
75 assert_se(streq(service->exec_string, "a"));
76 break;
77 case 2:
78 assert_se(service->hidden);
79 break;
80 }
81 }
82
TEST(xdg_desktop_parse)83 TEST(xdg_desktop_parse) {
84 for (size_t i = 0; i < ELEMENTSOF(xdg_desktop_file); i++)
85 test_xdg_desktop_parse_one(i, xdg_desktop_file[i]);
86 }
87
88 DEFINE_TEST_MAIN(LOG_DEBUG);
89