xdg: move tests for xdg-autostart-generator
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 31 Dec 2020 20:02:17 +0000 (05:02 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 18 Jan 2021 22:04:19 +0000 (07:04 +0900)
Then, we can drop src/xdg-autostart-generator from include directories
later.

src/fuzz/fuzz-xdg-desktop.c [deleted file]
src/fuzz/meson.build
src/test/meson.build
src/test/test-xdg-autostart.c [deleted file]
src/xdg-autostart-generator/fuzz-xdg-desktop.c [new file with mode: 0644]
src/xdg-autostart-generator/test-xdg-autostart.c [new file with mode: 0644]

diff --git a/src/fuzz/fuzz-xdg-desktop.c b/src/fuzz/fuzz-xdg-desktop.c
deleted file mode 100644 (file)
index 23077e4..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "alloc-util.h"
-#include "fd-util.h"
-#include "fs-util.h"
-#include "rm-rf.h"
-#include "string-util.h"
-#include "strv.h"
-#include "tests.h"
-#include "tmpfile-util.h"
-#include "fuzz.h"
-#include "xdg-autostart-service.h"
-
-int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
-        _cleanup_(unlink_tempfilep) char name[] = "/tmp/fuzz-xdg-desktop.XXXXXX";
-        _cleanup_close_ int fd = -1;
-        _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
-        _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
-
-        /* We don't want to fill the logs with messages about parse errors.
-         * Disable most logging if not running standalone */
-        if (!getenv("SYSTEMD_LOG_LEVEL"))
-                log_set_max_level(LOG_CRIT);
-
-        assert_se(mkdtemp_malloc("/tmp/fuzz-xdg-desktop-XXXXXX", &tmpdir) >= 0);
-
-        fd = mkostemp_safe(name);
-        assert_se(fd >= 0);
-        assert_se(write(fd, data, size) == (ssize_t) size);
-
-        assert_se(service = xdg_autostart_service_parse_desktop(name));
-        assert_se(service->name = strdup("fuzz-xdg-desktop.service"));
-        (void) xdg_autostart_service_generate_unit(service, tmpdir);
-
-        return 0;
-}
index 83527a68fbb6ccfb017875c0708a980ba06ccbfb..de6fe482e75fe047e9fe5f6c5f6db6e3495457ba 100644 (file)
@@ -145,7 +145,7 @@ fuzzers += [
          [libshared],
          []],
 
-        [['src/fuzz/fuzz-xdg-desktop.c',
+        [['src/xdg-autostart-generator/fuzz-xdg-desktop.c',
           'src/xdg-autostart-generator/xdg-autostart-service.h',
           'src/xdg-autostart-generator/xdg-autostart-service.c'],
          [],
index 14edc71399007be84381529d8963c701c19a9cac..b76cce27fd4d7edba0b79617d7c701696d6d1545 100644 (file)
@@ -1176,7 +1176,7 @@ tests += [
 ############################################################
 
 tests += [
-        [['src/test/test-xdg-autostart.c',
+        [['src/xdg-autostart-generator/test-xdg-autostart.c',
           'src/xdg-autostart-generator/xdg-autostart-service.c',
           'src/xdg-autostart-generator/xdg-autostart-service.h',],
          [],
diff --git a/src/test/test-xdg-autostart.c b/src/test/test-xdg-autostart.c
deleted file mode 100644 (file)
index a437e2c..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "alloc-util.h"
-#include "fd-util.h"
-#include "fs-util.h"
-#include "string-util.h"
-#include "strv.h"
-#include "tests.h"
-#include "tmpfile-util.h"
-#include "xdg-autostart-service.h"
-
-static void test_translate_name(void) {
-        _cleanup_free_ char *t;
-
-        assert_se(t = xdg_autostart_service_translate_name("a-b.blub.desktop"));
-        assert_se(streq(t, "app-a\\x2db.blub-autostart.service"));
-}
-
-static void test_xdg_format_exec_start_one(const char *exec, const char *expected) {
-        _cleanup_free_ char* out = NULL;
-
-        xdg_autostart_format_exec_start(exec, &out);
-        log_info("In: '%s', out: '%s', expected: '%s'", exec, out, expected);
-        assert_se(streq(out, expected));
-}
-
-static void test_xdg_format_exec_start(void) {
-        test_xdg_format_exec_start_one("/bin/sleep 100", "/bin/sleep \"100\"");
-
-        /* All standardised % identifiers are stripped. */
-        test_xdg_format_exec_start_one("/bin/sleep %f \"%F\" %u %U %d %D\t%n %N %i %c %k %v %m", "/bin/sleep");
-
-        /* Unknown % identifier currently remain, but are escaped. */
-        test_xdg_format_exec_start_one("/bin/sleep %X \"%Y\"", "/bin/sleep \"%%X\" \"%%Y\"");
-
-        test_xdg_format_exec_start_one("/bin/sleep \";\\\"\"", "/bin/sleep \";\\\"\"");
-}
-
-static const char* const xdg_desktop_file[] = {
-        "[Desktop Entry]\n"
-        "Exec\t =\t /bin/sleep 100\n" /* Whitespace Before/After = must be ignored */
-        "OnlyShowIn = A;B;\n"
-        "NotShowIn=C;;D\\\\\\;;E\n", /* "C", "", "D\;", "E" */
-
-        "[Desktop Entry]\n"
-        "Exec=a\n"
-        "Exec=b\n",
-
-        "[Desktop Entry]\n"
-        "Hidden=\t true\n",
-};
-
-static void test_xdg_desktop_parse(unsigned i, const char *s) {
-        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-xdg-autostart-parser.XXXXXX";
-        _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
-
-        log_info("== %s[%i] ==", __func__, i);
-
-        assert_se(fmkostemp_safe(name, "r+", &f) == 0);
-        assert_se(fwrite(s, strlen(s), 1, f) == 1);
-        rewind(f);
-
-        assert_se(service = xdg_autostart_service_parse_desktop(name));
-
-        switch (i) {
-        case 0:
-                assert_se(streq(service->exec_string, "/bin/sleep 100"));
-                assert_se(strv_equal(service->only_show_in, STRV_MAKE("A", "B")));
-                assert_se(strv_equal(service->not_show_in, STRV_MAKE("C", "D\\;", "E")));
-                assert_se(!service->hidden);
-                break;
-        case 1:
-                /* The second entry is not permissible and will be ignored (and error logged). */
-                assert_se(streq(service->exec_string, "a"));
-                break;
-        case 2:
-                assert_se(service->hidden);
-                break;
-        }
-}
-
-int main(int argc, char *argv[]) {
-        test_setup_logging(LOG_DEBUG);
-
-        test_translate_name();
-        test_xdg_format_exec_start();
-
-        for (size_t i = 0; i < ELEMENTSOF(xdg_desktop_file); i++)
-                test_xdg_desktop_parse(i, xdg_desktop_file[i]);
-
-        return 0;
-}
diff --git a/src/xdg-autostart-generator/fuzz-xdg-desktop.c b/src/xdg-autostart-generator/fuzz-xdg-desktop.c
new file mode 100644 (file)
index 0000000..23077e4
--- /dev/null
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "alloc-util.h"
+#include "fd-util.h"
+#include "fs-util.h"
+#include "rm-rf.h"
+#include "string-util.h"
+#include "strv.h"
+#include "tests.h"
+#include "tmpfile-util.h"
+#include "fuzz.h"
+#include "xdg-autostart-service.h"
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/fuzz-xdg-desktop.XXXXXX";
+        _cleanup_close_ int fd = -1;
+        _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
+        _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
+
+        /* We don't want to fill the logs with messages about parse errors.
+         * Disable most logging if not running standalone */
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
+                log_set_max_level(LOG_CRIT);
+
+        assert_se(mkdtemp_malloc("/tmp/fuzz-xdg-desktop-XXXXXX", &tmpdir) >= 0);
+
+        fd = mkostemp_safe(name);
+        assert_se(fd >= 0);
+        assert_se(write(fd, data, size) == (ssize_t) size);
+
+        assert_se(service = xdg_autostart_service_parse_desktop(name));
+        assert_se(service->name = strdup("fuzz-xdg-desktop.service"));
+        (void) xdg_autostart_service_generate_unit(service, tmpdir);
+
+        return 0;
+}
diff --git a/src/xdg-autostart-generator/test-xdg-autostart.c b/src/xdg-autostart-generator/test-xdg-autostart.c
new file mode 100644 (file)
index 0000000..a437e2c
--- /dev/null
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "alloc-util.h"
+#include "fd-util.h"
+#include "fs-util.h"
+#include "string-util.h"
+#include "strv.h"
+#include "tests.h"
+#include "tmpfile-util.h"
+#include "xdg-autostart-service.h"
+
+static void test_translate_name(void) {
+        _cleanup_free_ char *t;
+
+        assert_se(t = xdg_autostart_service_translate_name("a-b.blub.desktop"));
+        assert_se(streq(t, "app-a\\x2db.blub-autostart.service"));
+}
+
+static void test_xdg_format_exec_start_one(const char *exec, const char *expected) {
+        _cleanup_free_ char* out = NULL;
+
+        xdg_autostart_format_exec_start(exec, &out);
+        log_info("In: '%s', out: '%s', expected: '%s'", exec, out, expected);
+        assert_se(streq(out, expected));
+}
+
+static void test_xdg_format_exec_start(void) {
+        test_xdg_format_exec_start_one("/bin/sleep 100", "/bin/sleep \"100\"");
+
+        /* All standardised % identifiers are stripped. */
+        test_xdg_format_exec_start_one("/bin/sleep %f \"%F\" %u %U %d %D\t%n %N %i %c %k %v %m", "/bin/sleep");
+
+        /* Unknown % identifier currently remain, but are escaped. */
+        test_xdg_format_exec_start_one("/bin/sleep %X \"%Y\"", "/bin/sleep \"%%X\" \"%%Y\"");
+
+        test_xdg_format_exec_start_one("/bin/sleep \";\\\"\"", "/bin/sleep \";\\\"\"");
+}
+
+static const char* const xdg_desktop_file[] = {
+        "[Desktop Entry]\n"
+        "Exec\t =\t /bin/sleep 100\n" /* Whitespace Before/After = must be ignored */
+        "OnlyShowIn = A;B;\n"
+        "NotShowIn=C;;D\\\\\\;;E\n", /* "C", "", "D\;", "E" */
+
+        "[Desktop Entry]\n"
+        "Exec=a\n"
+        "Exec=b\n",
+
+        "[Desktop Entry]\n"
+        "Hidden=\t true\n",
+};
+
+static void test_xdg_desktop_parse(unsigned i, const char *s) {
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-xdg-autostart-parser.XXXXXX";
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
+
+        log_info("== %s[%i] ==", __func__, i);
+
+        assert_se(fmkostemp_safe(name, "r+", &f) == 0);
+        assert_se(fwrite(s, strlen(s), 1, f) == 1);
+        rewind(f);
+
+        assert_se(service = xdg_autostart_service_parse_desktop(name));
+
+        switch (i) {
+        case 0:
+                assert_se(streq(service->exec_string, "/bin/sleep 100"));
+                assert_se(strv_equal(service->only_show_in, STRV_MAKE("A", "B")));
+                assert_se(strv_equal(service->not_show_in, STRV_MAKE("C", "D\\;", "E")));
+                assert_se(!service->hidden);
+                break;
+        case 1:
+                /* The second entry is not permissible and will be ignored (and error logged). */
+                assert_se(streq(service->exec_string, "a"));
+                break;
+        case 2:
+                assert_se(service->hidden);
+                break;
+        }
+}
+
+int main(int argc, char *argv[]) {
+        test_setup_logging(LOG_DEBUG);
+
+        test_translate_name();
+        test_xdg_format_exec_start();
+
+        for (size_t i = 0; i < ELEMENTSOF(xdg_desktop_file); i++)
+                test_xdg_desktop_parse(i, xdg_desktop_file[i]);
+
+        return 0;
+}