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 --- bin/gensquashfs/Makemodule.am | 2 +- bin/gensquashfs/mkfs.h | 1 + bin/sqfs2tar/sqfs2tar.h | 3 +- bin/tar2sqfs/Makemodule.am | 2 +- bin/tar2sqfs/tar2sqfs.h | 1 + include/fstree.h | 19 -------- include/util/util.h | 19 ++++++++ lib/common/hardlink.c | 1 + lib/fstree/Makemodule.am | 2 - lib/fstree/canonicalize_name.c | 60 ----------------------- lib/fstree/filename_sane.c | 78 ------------------------------ lib/fstree/fstree_from_file.c | 1 + lib/fstree/hardlink.c | 1 + lib/fstree/sort_by_file.c | 1 + lib/util/Makemodule.am | 2 + lib/util/canonicalize_name.c | 60 +++++++++++++++++++++++ lib/util/filename_sane.c | 78 ++++++++++++++++++++++++++++++ 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 +++++++++++++++++++++++++++++++++++++ 24 files changed, 364 insertions(+), 355 deletions(-) delete mode 100644 lib/fstree/canonicalize_name.c delete mode 100644 lib/fstree/filename_sane.c create mode 100644 lib/util/canonicalize_name.c create mode 100644 lib/util/filename_sane.c 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 diff --git a/bin/gensquashfs/Makemodule.am b/bin/gensquashfs/Makemodule.am index 03f4f4c..b0b8a79 100644 --- a/bin/gensquashfs/Makemodule.am +++ b/bin/gensquashfs/Makemodule.am @@ -2,7 +2,7 @@ gensquashfs_SOURCES = bin/gensquashfs/mkfs.c bin/gensquashfs/mkfs.h gensquashfs_SOURCES += bin/gensquashfs/options.c bin/gensquashfs/selinux.c gensquashfs_SOURCES += bin/gensquashfs/dirscan_xattr.c gensquashfs_LDADD = libcommon.a libsquashfs.la libfstree.a libio.a -gensquashfs_LDADD += libcompat.a $(LZO_LIBS) $(PTHREAD_LIBS) +gensquashfs_LDADD += libutil.a libcompat.a $(LZO_LIBS) $(PTHREAD_LIBS) gensquashfs_CPPFLAGS = $(AM_CPPFLAGS) gensquashfs_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) diff --git a/bin/gensquashfs/mkfs.h b/bin/gensquashfs/mkfs.h index 4f455bd..2697d3e 100644 --- a/bin/gensquashfs/mkfs.h +++ b/bin/gensquashfs/mkfs.h @@ -11,6 +11,7 @@ #include "common.h" #include "fstree.h" +#include "util/util.h" #ifdef HAVE_SYS_XATTR_H #include diff --git a/bin/sqfs2tar/sqfs2tar.h b/bin/sqfs2tar/sqfs2tar.h index 4575793..71b491d 100644 --- a/bin/sqfs2tar/sqfs2tar.h +++ b/bin/sqfs2tar/sqfs2tar.h @@ -9,8 +9,9 @@ #include "config.h" #include "common.h" -#include "tar/tar.h" +#include "util/util.h" +#include "tar/tar.h" #include "io/xfrm.h" #include diff --git a/bin/tar2sqfs/Makemodule.am b/bin/tar2sqfs/Makemodule.am index 818f6e2..a9c503f 100644 --- a/bin/tar2sqfs/Makemodule.am +++ b/bin/tar2sqfs/Makemodule.am @@ -2,7 +2,7 @@ tar2sqfs_SOURCES = bin/tar2sqfs/tar2sqfs.c bin/tar2sqfs/tar2sqfs.h tar2sqfs_SOURCES += bin/tar2sqfs/options.c bin/tar2sqfs/process_tarball.c tar2sqfs_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) tar2sqfs_LDADD = libcommon.a libsquashfs.la libtar.a libio.a -tar2sqfs_LDADD += libfstree.a libcompat.a libfstree.a $(LZO_LIBS) +tar2sqfs_LDADD += libfstree.a libcompat.a libfstree.a libutil.a $(LZO_LIBS) tar2sqfs_LDADD += $(ZLIB_LIBS) $(XZ_LIBS) $(ZSTD_LIBS) $(BZIP2_LIBS) tar2sqfs_LDADD += $(PTHREAD_LIBS) diff --git a/bin/tar2sqfs/tar2sqfs.h b/bin/tar2sqfs/tar2sqfs.h index 40c3b75..6e4d123 100644 --- a/bin/tar2sqfs/tar2sqfs.h +++ b/bin/tar2sqfs/tar2sqfs.h @@ -11,6 +11,7 @@ #include "common.h" #include "compat.h" +#include "util/util.h" #include "tar/tar.h" #include "tar/format.h" #include "io/xfrm.h" diff --git a/include/fstree.h b/include/fstree.h index 3fb4f47..7bdbddf 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -236,25 +236,6 @@ tree_node_t *fstree_get_node_by_path(fstree_t *fs, tree_node_t *root, const char *path, bool create_implicitly, bool stop_at_parent); -/* - Convert back to forward slashed, remove all preceeding and trailing slashes, - collapse all sequences of slashes, remove all path components that are '.' - and returns failure state if one of the path components is '..'. - - Returns 0 on success. -*/ -int canonicalize_name(char *filename); - -/* - Returns true if a given filename is sane, false if it is not (e.g. contains - slashes or it is equal to '.' or '..'). - - If check_os_specific is true, this also checks if the filename contains - a character, or is equal to a name, that is black listed on the current OS. - E.g. on Windows, a file named "COM0" or "AUX" is a no-no. - */ -bool is_filename_sane(const char *name, bool check_os_specific); - /* Add a hard link node. Returns NULL on failure and sets errno. */ diff --git a/include/util/util.h b/include/util/util.h index af7d196..c18a558 100644 --- a/include/util/util.h +++ b/include/util/util.h @@ -43,4 +43,23 @@ SQFS_INTERNAL bool is_memory_zero(const void *blob, size_t size); */ SQFS_INTERNAL int mkdir_p(const char *path); +/* + Remove all preceeding and trailing slashes, collapse all sequences of + slashes, remove all path components that are '.' and returns failure + state if one of the path components is '..'. + + Returns 0 on success. +*/ +SQFS_INTERNAL int canonicalize_name(char *filename); + +/* + Returns true if a given filename is sane, false if it is not (e.g. contains + slashes or it is equal to '.' or '..'). + + If check_os_specific is true, this also checks if the filename contains + a character, or is equal to a name, that is black listed on the current OS. + E.g. on Windows, a file named "COM0" or "AUX" is a no-no. + */ +SQFS_INTERNAL bool is_filename_sane(const char *name, bool check_os_specific); + #endif /* SQFS_UTIL_H */ diff --git a/lib/common/hardlink.c b/lib/common/hardlink.c index f8d4d4a..f28620f 100644 --- a/lib/common/hardlink.c +++ b/lib/common/hardlink.c @@ -6,6 +6,7 @@ */ #include "common.h" #include "util/rbtree.h" +#include "util/util.h" #include #include diff --git a/lib/fstree/Makemodule.am b/lib/fstree/Makemodule.am index c2981ce..fec63b4 100644 --- a/lib/fstree/Makemodule.am +++ b/lib/fstree/Makemodule.am @@ -5,8 +5,6 @@ libfstree_a_SOURCES += lib/fstree/mknode.c lib/fstree/fstree_from_dir.c libfstree_a_SOURCES += lib/fstree/add_by_path.c lib/fstree/get_by_path.c libfstree_a_SOURCES += include/fstree.h lib/fstree/internal.h libfstree_a_SOURCES += lib/fstree/source_date_epoch.c -libfstree_a_SOURCES += lib/fstree/canonicalize_name.c -libfstree_a_SOURCES += lib/fstree/filename_sane.c libfstree_a_SOURCES += lib/fstree/sort_by_file.c libfstree_a_CFLAGS = $(AM_CFLAGS) libfstree_a_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/lib/fstree/canonicalize_name.c b/lib/fstree/canonicalize_name.c deleted file mode 100644 index 7fbd5a7..0000000 --- a/lib/fstree/canonicalize_name.c +++ /dev/null @@ -1,60 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * canonicalize_name.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" -#include "fstree.h" - -static void normalize_slashes(char *filename) -{ - char *dst = filename, *src = filename; - - while (*src == '/') - ++src; - - while (*src != '\0') { - if (*src == '/') { - while (*src == '/') - ++src; - if (*src == '\0') - break; - *(dst++) = '/'; - } else { - *(dst++) = *(src++); - } - } - - *dst = '\0'; -} - -int canonicalize_name(char *filename) -{ - char *dst = filename, *src = filename; - - normalize_slashes(filename); - - while (*src != '\0') { - if (src[0] == '.') { - if (src[1] == '\0') - break; - if (src[1] == '/') { - src += 2; - continue; - } - if (src[1] == '.' && (src[2] == '/' || src[2] == '\0')) - return -1; - } - - while (*src != '\0' && *src != '/') - *(dst++) = *(src++); - - if (*src == '/') - *(dst++) = *(src++); - } - - *dst = '\0'; - normalize_slashes(filename); - return 0; -} diff --git a/lib/fstree/filename_sane.c b/lib/fstree/filename_sane.c deleted file mode 100644 index 91c15da..0000000 --- a/lib/fstree/filename_sane.c +++ /dev/null @@ -1,78 +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 - -#if defined(_WIN32) || defined(__WINDOWS__) || defined(TEST_WIN32) -#ifdef _MSC_VER -#define strncasecmp _strnicmp -#define strcasecmp _stricmp -#endif - -static const char *bad_names[] = { - "CON", "PRN", "AUX", "NUL", - "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", - "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", -}; - -static bool is_allowed_by_os(const char *name) -{ - size_t len, i; - - for (i = 0; i < sizeof(bad_names) / sizeof(bad_names[0]); ++i) { - len = strlen(bad_names[i]); - - if (strncasecmp(name, bad_names[i], len) != 0) - continue; - - if (name[len] == '\0') - return false; - - if (name[len] == '.' && strchr(name + len + 1, '.') == NULL) - return false; - } - - return true; -} -#else -static bool is_allowed_by_os(const char *name) -{ - (void)name; - return true; -} -#endif - -bool is_filename_sane(const char *name, bool check_os_specific) -{ - if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) - return false; - - if (check_os_specific && !is_allowed_by_os(name)) - return false; - - while (*name != '\0') { - if (*name == '/') - return false; - -#if defined(_WIN32) || defined(__WINDOWS__) || defined(TEST_WIN32) - if (check_os_specific) { - if (*name == '<' || *name == '>' || *name == ':') - return false; - if (*name == '"' || *name == '|' || *name == '?') - return false; - if (*name == '*' || *name == '\\' || *name <= 31) - return false; - } -#endif - - ++name; - } - - return true; -} diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c index dd289bc..411c64f 100644 --- a/lib/fstree/fstree_from_file.c +++ b/lib/fstree/fstree_from_file.c @@ -6,6 +6,7 @@ */ #include "config.h" +#include "util/util.h" #include "io/file.h" #include "fstree.h" #include "compat.h" diff --git a/lib/fstree/hardlink.c b/lib/fstree/hardlink.c index f45acf7..2165b5f 100644 --- a/lib/fstree/hardlink.c +++ b/lib/fstree/hardlink.c @@ -6,6 +6,7 @@ */ #include "config.h" +#include "util/util.h" #include "fstree.h" #include diff --git a/lib/fstree/sort_by_file.c b/lib/fstree/sort_by_file.c index 7f1f19f..ed4a58c 100644 --- a/lib/fstree/sort_by_file.c +++ b/lib/fstree/sort_by_file.c @@ -6,6 +6,7 @@ */ #include "config.h" +#include "util/util.h" #include "fstree.h" #include "compat.h" diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index bc3d618..d37f2b1 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -10,6 +10,8 @@ libutil_a_SOURCES += include/util/w32threadwrap.h libutil_a_SOURCES += lib/util/threadpool_serial.c libutil_a_SOURCES += lib/util/is_memory_zero.c libutil_a_SOURCES += lib/util/mkdir_p.c +libutil_a_SOURCES += lib/util/canonicalize_name.c +libutil_a_SOURCES += lib/util/filename_sane.c libutil_a_CFLAGS = $(AM_CFLAGS) libutil_a_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/lib/util/canonicalize_name.c b/lib/util/canonicalize_name.c new file mode 100644 index 0000000..534e89e --- /dev/null +++ b/lib/util/canonicalize_name.c @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * canonicalize_name.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" +#include "util/util.h" + +static void normalize_slashes(char *filename) +{ + char *dst = filename, *src = filename; + + while (*src == '/') + ++src; + + while (*src != '\0') { + if (*src == '/') { + while (*src == '/') + ++src; + if (*src == '\0') + break; + *(dst++) = '/'; + } else { + *(dst++) = *(src++); + } + } + + *dst = '\0'; +} + +int canonicalize_name(char *filename) +{ + char *dst = filename, *src = filename; + + normalize_slashes(filename); + + while (*src != '\0') { + if (src[0] == '.') { + if (src[1] == '\0') + break; + if (src[1] == '/') { + src += 2; + continue; + } + if (src[1] == '.' && (src[2] == '/' || src[2] == '\0')) + return -1; + } + + while (*src != '\0' && *src != '/') + *(dst++) = *(src++); + + if (*src == '/') + *(dst++) = *(src++); + } + + *dst = '\0'; + normalize_slashes(filename); + return 0; +} diff --git a/lib/util/filename_sane.c b/lib/util/filename_sane.c new file mode 100644 index 0000000..b52ce4d --- /dev/null +++ b/lib/util/filename_sane.c @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * filename_sane.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" +#include "util/util.h" + +#include + +#if defined(_WIN32) || defined(__WINDOWS__) || defined(TEST_WIN32) +#ifdef _MSC_VER +#define strncasecmp _strnicmp +#define strcasecmp _stricmp +#endif + +static const char *bad_names[] = { + "CON", "PRN", "AUX", "NUL", + "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", + "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", +}; + +static bool is_allowed_by_os(const char *name) +{ + size_t len, i; + + for (i = 0; i < sizeof(bad_names) / sizeof(bad_names[0]); ++i) { + len = strlen(bad_names[i]); + + if (strncasecmp(name, bad_names[i], len) != 0) + continue; + + if (name[len] == '\0') + return false; + + if (name[len] == '.' && strchr(name + len + 1, '.') == NULL) + return false; + } + + return true; +} +#else +static bool is_allowed_by_os(const char *name) +{ + (void)name; + return true; +} +#endif + +bool is_filename_sane(const char *name, bool check_os_specific) +{ + if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) + return false; + + if (check_os_specific && !is_allowed_by_os(name)) + return false; + + while (*name != '\0') { + if (*name == '/') + return false; + +#if defined(_WIN32) || defined(__WINDOWS__) || defined(TEST_WIN32) + if (check_os_specific) { + if (*name == '<' || *name == '>' || *name == ':') + return false; + if (*name == '"' || *name == '|' || *name == '?') + return false; + if (*name == '*' || *name == '\\' || *name <= 31) + return false; + } +#endif + + ++name; + } + + return true; +} 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