From d6e2106e96b6969e045251d972e1adcceb9728df Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 5 Jul 2022 15:34:08 +0200 Subject: Cleanup: move filename_sane & canonicalize_path functions to libutil Signed-off-by: David Oberhollenzer --- tests/libfstree/Makemodule.am | 28 ++++------- tests/libfstree/canonicalize_name.c | 78 ------------------------------ tests/libfstree/filename_sane.c | 94 ------------------------------------- tests/libfstree/sort_file.c | 1 + tests/libutil/Makemodule.am | 15 +++++- tests/libutil/canonicalize_name.c | 78 ++++++++++++++++++++++++++++++ tests/libutil/filename_sane.c | 94 +++++++++++++++++++++++++++++++++++++ 7 files changed, 195 insertions(+), 193 deletions(-) delete mode 100644 tests/libfstree/canonicalize_name.c delete mode 100644 tests/libfstree/filename_sane.c create mode 100644 tests/libutil/canonicalize_name.c create mode 100644 tests/libutil/filename_sane.c (limited to 'tests') diff --git a/tests/libfstree/Makemodule.am b/tests/libfstree/Makemodule.am index a79d670..107a66c 100644 --- a/tests/libfstree/Makemodule.am +++ b/tests/libfstree/Makemodule.am @@ -1,8 +1,5 @@ FSTDATADIR=$(top_srcdir)/tests/libfstree -test_canonicalize_name_SOURCES = tests/libfstree/canonicalize_name.c -test_canonicalize_name_LDADD = libfstree.a libcompat.a - test_mknode_simple_SOURCES = tests/libfstree/mknode_simple.c test_mknode_simple_LDADD = libfstree.a libcompat.a @@ -16,7 +13,7 @@ test_mknode_dir_SOURCES = tests/libfstree/mknode_dir.c test_mknode_dir_LDADD = libfstree.a libcompat.a test_gen_inode_numbers_SOURCES = tests/libfstree/gen_inode_numbers.c -test_gen_inode_numbers_LDADD = libfstree.a libcompat.a +test_gen_inode_numbers_LDADD = libfstree.a libutil.a libcompat.a test_add_by_path_SOURCES = tests/libfstree/add_by_path.c test_add_by_path_LDADD = libfstree.a libcompat.a @@ -31,46 +28,37 @@ test_fstree_sort_LDADD = libfstree.a libio.a libcompat.a test_fstree_from_file_SOURCES = tests/libfstree/fstree_from_file.c test_fstree_from_file_CPPFLAGS = $(AM_CPPFLAGS) test_fstree_from_file_CPPFLAGS += -DTESTPATH=$(FSTDATADIR)/fstree1.txt -test_fstree_from_file_LDADD = libfstree.a libio.a libcompat.a +test_fstree_from_file_LDADD = libfstree.a libio.a libutil.a libcompat.a test_fstree_glob1_SOURCES = tests/libfstree/fstree_glob1.c test_fstree_glob1_CPPFLAGS = $(AM_CPPFLAGS) -DTESTPATH=$(FSTDATADIR) -test_fstree_glob1_LDADD = libfstree.a libio.a libcompat.a +test_fstree_glob1_LDADD = libfstree.a libio.a libutil.a libcompat.a test_fstree_from_dir_SOURCES = tests/libfstree/fstree_from_dir.c test_fstree_from_dir_CPPFLAGS = $(AM_CPPFLAGS) test_fstree_from_dir_CPPFLAGS += -DTESTPATH=$(top_srcdir)/tests/libtar/data -test_fstree_from_dir_LDADD = libfstree.a libcompat.a +test_fstree_from_dir_LDADD = libfstree.a libutil.a libcompat.a test_fstree_init_SOURCES = tests/libfstree/fstree_init.c test_fstree_init_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/lib/fstree test_fstree_init_LDADD = libfstree.a libio.a libcompat.a -test_filename_sane_SOURCES = tests/libfstree/filename_sane.c -test_filename_sane_SOURCES += lib/fstree/filename_sane.c -test_filename_sane_LDADD = libcompat.a - -test_filename_sane_w32_SOURCES = tests/libfstree/filename_sane.c -test_filename_sane_w32_SOURCES += lib/fstree/filename_sane.c -test_filename_sane_w32_CPPFLAGS = $(AM_CPPFLAGS) -DTEST_WIN32=1 -test_filename_sane_w32_LDADD = libcompat.a - test_fstree_epoch_SOURCES = tests/libfstree/epoch.c test_fstree_epoch_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/lib/fstree test_fstree_epoch_SOURCES += lib/fstree/source_date_epoch.c test_fstree_epoch_LDADD = libcompat.a test_sort_file_SOURCES = tests/libfstree/sort_file.c -test_sort_file_LDADD = libfstree.a libio.a libcompat.a +test_sort_file_LDADD = libfstree.a libio.a libutil.a libcompat.a fstree_fuzz_SOURCES = tests/libfstree/fstree_fuzz.c -fstree_fuzz_LDADD = libfstree.a libio.a libcompat.a +fstree_fuzz_LDADD = libfstree.a libio.a libutil.a libcompat.a FSTREE_TESTS = \ - test_canonicalize_name test_mknode_simple test_mknode_slink \ + test_mknode_simple test_mknode_slink \ test_mknode_reg test_mknode_dir test_gen_inode_numbers \ test_add_by_path test_get_path test_fstree_sort test_fstree_from_file \ - test_fstree_init test_filename_sane test_filename_sane_w32 \ + test_fstree_init \ test_fstree_from_dir test_fstree_glob1 test_fstree_epoch \ test_sort_file diff --git a/tests/libfstree/canonicalize_name.c b/tests/libfstree/canonicalize_name.c deleted file mode 100644 index f117a0d..0000000 --- a/tests/libfstree/canonicalize_name.c +++ /dev/null @@ -1,78 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * canonicalize_name.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" -#include "fstree.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/libfstree/filename_sane.c b/tests/libfstree/filename_sane.c deleted file mode 100644 index a1dc79c..0000000 --- a/tests/libfstree/filename_sane.c +++ /dev/null @@ -1,94 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * filename_sane.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" -#include "fstree.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", - "foo", "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; -} diff --git a/tests/libfstree/sort_file.c b/tests/libfstree/sort_file.c index 93d60e0..78d1606 100644 --- a/tests/libfstree/sort_file.c +++ b/tests/libfstree/sort_file.c @@ -9,6 +9,7 @@ #include "sqfs/block.h" #include "fstree.h" #include "util/test.h" +#include "util/util.h" static const char *listing = "dir /bin 0755 0 0\n" 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 + */ +#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 + */ +#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", + "foo", "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; +} -- cgit v1.2.3