1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
6 #include "string-util.h"
9 #include "tmpfile-util.h"
10 #include "xdg-autostart-service.h"
12 static void test_translate_name(void) {
13 _cleanup_free_
char *t
;
15 assert_se(t
= xdg_autostart_service_translate_name("a-b.blub.desktop"));
16 assert_se(streq(t
, "app-a\\x2db.blub-autostart.service"));
19 static void test_xdg_format_exec_start_one(const char *exec
, const char *expected
) {
20 _cleanup_free_
char* out
= NULL
;
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
));
27 static void test_xdg_format_exec_start(void) {
28 test_xdg_format_exec_start_one("/bin/sleep 100", "/bin/sleep \"100\"");
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");
33 /* Unknown % identifier currently remain, but are escaped. */
34 test_xdg_format_exec_start_one("/bin/sleep %X \"%Y\"", "/bin/sleep \"%%X\" \"%%Y\"");
36 test_xdg_format_exec_start_one("/bin/sleep \";\\\"\"", "/bin/sleep \";\\\"\"");
39 static const char* const xdg_desktop_file
[] = {
41 "Exec\t =\t /bin/sleep 100\n" /* Whitespace Before/After = must be ignored */
43 "NotShowIn=C;;D\\\\\\;;E\n", /* "C", "", "D\;", "E" */
53 static void test_xdg_desktop_parse(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
;
58 log_info("== %s[%i] ==", __func__
, i
);
60 assert_se(fmkostemp_safe(name
, "r+", &f
) == 0);
61 assert_se(fwrite(s
, strlen(s
), 1, f
) == 1);
64 assert_se(service
= xdg_autostart_service_parse_desktop(name
));
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
);
74 /* The second entry is not permissible and will be ignored (and error logged). */
75 assert_se(streq(service
->exec_string
, "a"));
78 assert_se(service
->hidden
);
83 int main(int argc
, char *argv
[]) {
84 test_setup_logging(LOG_DEBUG
);
86 test_translate_name();
87 test_xdg_format_exec_start();
89 for (size_t i
= 0; i
< ELEMENTSOF(xdg_desktop_file
); i
++)
90 test_xdg_desktop_parse(i
, xdg_desktop_file
[i
]);