aboutsummaryrefslogtreecommitdiff
path: root/tests/libutil
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-07-05 15:34:08 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-07-08 19:17:35 +0200
commitd6e2106e96b6969e045251d972e1adcceb9728df (patch)
tree6435792bf334cdd1980c071348348f697cf027cb /tests/libutil
parent4a607edbdfc12f97da0810563fd2e699dcecaa71 (diff)
Cleanup: move filename_sane & canonicalize_path functions to libutil
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/libutil')
-rw-r--r--tests/libutil/Makemodule.am15
-rw-r--r--tests/libutil/canonicalize_name.c78
-rw-r--r--tests/libutil/filename_sane.c94
3 files changed, 186 insertions, 1 deletions
diff --git a/tests/libutil/Makemodule.am b/tests/libutil/Makemodule.am
index 95fb8ab..a3853d2 100644
--- a/tests/libutil/Makemodule.am
+++ b/tests/libutil/Makemodule.am
@@ -15,8 +15,21 @@ test_threadpool_LDADD = libutil.a libcompat.a $(PTHREAD_LIBS)
test_ismemzero_SOURCES = tests/libutil/is_memory_zero.c
test_ismemzero_LDADD = libutil.a libcompat.a
+test_canonicalize_name_SOURCES = tests/libutil/canonicalize_name.c
+test_canonicalize_name_LDADD = libutil.a libcompat.a
+
+test_filename_sane_SOURCES = tests/libutil/filename_sane.c
+test_filename_sane_SOURCES += lib/util/filename_sane.c
+test_filename_sane_LDADD = libcompat.a libutil.a
+
+test_filename_sane_w32_SOURCES = tests/libutil/filename_sane.c
+test_filename_sane_w32_SOURCES += lib/util/filename_sane.c
+test_filename_sane_w32_CPPFLAGS = $(AM_CPPFLAGS) -DTEST_WIN32=1
+test_filename_sane_w32_LDADD = libcompat.a
+
LIBUTIL_TESTS = \
- test_str_table test_rbtree test_xxhash test_threadpool test_ismemzero
+ test_str_table test_rbtree test_xxhash test_threadpool test_ismemzero \
+ test_canonicalize_name test_filename_sane test_filename_sane_w32
check_PROGRAMS += $(LIBUTIL_TESTS)
TESTS += $(LIBUTIL_TESTS)
diff --git a/tests/libutil/canonicalize_name.c b/tests/libutil/canonicalize_name.c
new file mode 100644
index 0000000..9f81b04
--- /dev/null
+++ b/tests/libutil/canonicalize_name.c
@@ -0,0 +1,78 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * canonicalize_name.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "util/util.h"
+#include "util/test.h"
+
+static const struct {
+ const char *in;
+ const char *out;
+} must_work[] = {
+ { "", "" },
+ { "/", "" },
+ { "\\", "\\" },
+ { "///", "" },
+ { "\\\\\\", "\\\\\\" },
+ { "/\\//\\\\/", "\\/\\\\" },
+ { "foo/bar/test", "foo/bar/test" },
+ { "foo\\bar\\test", "foo\\bar\\test" },
+ { "/foo/bar/test/", "foo/bar/test" },
+ { "\\foo\\bar\\test\\", "\\foo\\bar\\test\\" },
+ { "///foo//bar//test///", "foo/bar/test" },
+ { "./foo/././bar/test/./.", "foo/bar/test" },
+ { "./foo/././", "foo" },
+ { ".", "" },
+ { "./", "" },
+ { "./.", "" },
+ { "foo/.../bar", "foo/.../bar" },
+ { "foo/.test/bar", "foo/.test/bar" },
+};
+
+static const char *must_not_work[] = {
+ "..",
+ "foo/../bar",
+ "../foo/bar",
+ "foo/bar/..",
+ "foo/bar/../",
+};
+
+int main(int argc, char **argv)
+{
+ char buffer[512];
+ size_t i;
+ (void)argc; (void)argv;
+
+ for (i = 0; i < sizeof(must_work) / sizeof(must_work[0]); ++i) {
+ strcpy(buffer, must_work[i].in);
+
+ if (canonicalize_name(buffer)) {
+ fprintf(stderr, "Test case rejected: '%s'\n",
+ must_work[i].in);
+ return EXIT_FAILURE;
+ }
+
+ if (strcmp(buffer, must_work[i].out) != 0) {
+ fprintf(stderr, "Expected result: %s\n",
+ must_work[i].out);
+ fprintf(stderr, "Actual result: %s\n", buffer);
+ return EXIT_FAILURE;
+ }
+ }
+
+ for (i = 0; i < sizeof(must_not_work) / sizeof(must_not_work[0]); ++i) {
+ strcpy(buffer, must_not_work[i]);
+
+ if (canonicalize_name(buffer) == 0) {
+ fprintf(stderr, "Test case accepted: '%s'\n",
+ must_not_work[i]);
+ fprintf(stderr, "Transformed into: '%s'\n", buffer);
+ return EXIT_FAILURE;
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/libutil/filename_sane.c b/tests/libutil/filename_sane.c
new file mode 100644
index 0000000..9c9930d
--- /dev/null
+++ b/tests/libutil/filename_sane.c
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * filename_sane.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "util/util.h"
+#include "util/test.h"
+
+static const char *must_work[] = {
+ "foobar",
+ "test.txt",
+#if !defined(_WIN32) && !defined(__WINDOWS__) && !defined(TEST_WIN32)
+ "\\foo", "foo\\", "foo\\bar",
+#endif
+ NULL,
+};
+
+static const char *must_not_work[] = {
+ ".",
+ "..",
+ "/foo",
+ "foo/",
+ "foo/bar",
+ NULL,
+};
+
+static const char *must_not_work_here[] = {
+#if defined(_WIN32) || defined(__WINDOWS__) || defined(TEST_WIN32)
+ "\\foo", "foo\\", "foo\\bar",
+ "fo<o", "fo>o", "fo:o", "fo\"o",
+ "fo|o", "fo?o", "fo*o", "fo\ro",
+ "CON", "PRN", "AUX", "NUL",
+ "COM1", "COM2", "LPT1", "LPT2",
+ "con", "prn", "aux", "nul",
+ "com1", "com2", "lpt1", "lpt2",
+ "AUX.txt", "aux.txt", "NUL.txt", "nul.txt",
+#endif
+ NULL,
+};
+
+int main(int argc, char **argv)
+{
+ size_t i;
+ (void)argc; (void)argv;
+
+ for (i = 0; must_work[i] != NULL; ++i) {
+ if (!is_filename_sane(must_work[i], false)) {
+ fprintf(stderr, "%s was rejected!\n", must_work[i]);
+ return EXIT_FAILURE;
+ }
+
+ if (!is_filename_sane(must_work[i], true)) {
+ fprintf(stderr,
+ "%s was rejected when testing for "
+ "OS specific stuff!\n", must_work[i]);
+ return EXIT_FAILURE;
+ }
+ }
+
+ for (i = 0; must_not_work[i] != NULL; ++i) {
+ if (is_filename_sane(must_not_work[i], false)) {
+ fprintf(stderr, "%s was accepted!\n",
+ must_not_work[i]);
+ return EXIT_FAILURE;
+ }
+
+ if (is_filename_sane(must_not_work[i], true)) {
+ fprintf(stderr,
+ "%s was accepted when testing for "
+ "OS specific stuff!\n", must_not_work[i]);
+ return EXIT_FAILURE;
+ }
+ }
+
+ for (i = 0; must_not_work_here[i] != NULL; ++i) {
+ if (!is_filename_sane(must_not_work_here[i], false)) {
+ fprintf(stderr,
+ "%s was rejected in the generic test!\n",
+ must_not_work_here[i]);
+ return EXIT_FAILURE;
+ }
+
+ if (is_filename_sane(must_not_work_here[i], true)) {
+ fprintf(stderr,
+ "%s was accepted when testing for "
+ "OS specific stuff!\n", must_not_work_here[i]);
+ return EXIT_FAILURE;
+ }
+ }
+
+ return EXIT_SUCCESS;
+}