From: Timo Sirainen Date: Fri, 17 Nov 2017 20:37:00 +0000 (+0200) Subject: lib: path-util - Add more code paths to test in path_normalize() X-Git-Tag: 2.3.0.rc1~433 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d7a2f56cd0e550a92cb160b346f33a84b0daa75e;p=thirdparty%2Fdovecot%2Fcore.git lib: path-util - Add more code paths to test in path_normalize() --- diff --git a/src/lib/test-path-util.c b/src/lib/test-path-util.c index f9ccf9d93c..71fe6a9fca 100644 --- a/src/lib/test-path-util.c +++ b/src/lib/test-path-util.c @@ -6,6 +6,7 @@ #include "str.h" #include +#include #include #include @@ -16,6 +17,7 @@ static const char *cwd; static const char *link1; static const char *link2; static const char *link3; +static const char *link4; static void test_local_path(void) { @@ -96,9 +98,33 @@ static void test_relative_dotdot(void) static void test_link1(void) { - const char *npath = NULL, *error = NULL; + const char *old_dir, *npath = NULL, *error = NULL; test_assert(t_realpath_to(link1, "/", &npath, &error) == 0); test_assert_strcmp(npath, tmpdir); + + /* .../link1/link1/child */ + test_assert(t_realpath_to(t_strconcat(link1, "/link1/child", NULL), + "/", &npath, &error) == 0); + test_assert_strcmp(npath, t_strconcat(tmpdir, "/child", NULL)); + + /* relative link1/link1/child */ + if (t_get_working_dir(&old_dir, &error) < 0) + i_fatal("t_get_working_dir() failed: %s", error); + if (chdir(tmpdir) < 0) + i_fatal("chdir(%s) failed: %m", tmpdir); + test_assert(t_realpath(t_strconcat("link1", "/link1/child", NULL), + &npath, &error) == 0); + if (chdir(old_dir) < 0) + i_fatal("chdir(%s) failed: %m", old_dir); +} + +static void test_link4(void) +{ + const char *npath = NULL, *error = NULL; + + test_assert(t_realpath_to(t_strconcat(link1, "/link4/child", NULL), + "/", &npath, &error) == 0); + test_assert_strcmp(npath, t_strconcat(tmpdir, "/child", NULL)); } static void test_link_loop(void) @@ -120,6 +146,35 @@ static void test_abspath_vs_normpath(void) test_assert_strcmp(norm, "/bin"); } +static void create_links(const char *tmpdir) +{ + link1 = t_strconcat(tmpdir, "/link1", NULL); + if (symlink(tmpdir, link1) < 0) { + i_fatal("symlink(%s, %s) failed: %m", tmpdir, link1); + } + const char *link1_child = t_strconcat(link1, "/child", NULL); + int fd = creat(link1_child, 0600); + if (fd == -1) + i_fatal("creat(%s) failed: %m", link1_child); + i_close_fd(&fd); + + /* link2 and link3 point to each other to create a loop */ + link2 = t_strconcat(tmpdir, "/link2", NULL); + link3 = t_strconcat(tmpdir, "/link3", NULL); + if (symlink(link3, link2) < 0) { + i_fatal("symlink(%s, %s) failed: %m", link3, link2); + } + if (symlink(link2, link3) < 0) { + i_fatal("symlink(%s, %s) failed: %m", link2, link3); + } + + /* link4 points to link1 */ + link4 = t_strconcat(tmpdir, "/link4", NULL); + if (symlink("link1", link4) < 0) { + i_fatal("symlink(link1, %s) failed: %m", link4); + } +} + static void test_link_alloc(void) { #define COMPONENT_COMPONENT "/component-component" @@ -143,20 +198,7 @@ static void test_link_alloc(void) o_tmpdir = tmpdir; tmpdir = str_c(basedir); - link1 = t_strconcat(tmpdir, "/link1", NULL); - if (symlink(tmpdir, link1) < 0) { - i_fatal("symlink(%s, %s) failed: %m", tmpdir, link1); - } - - /* link2 and link3 point to each other to create a loop */ - link2 = t_strconcat(tmpdir, "/link2", NULL); - link3 = t_strconcat(tmpdir, "/link3", NULL); - if (symlink(link3, link2) < 0) { - i_fatal("symlink(%s, %s) failed: %m", link3, link2); - } - if (symlink(link2, link3) < 0) { - i_fatal("symlink(%s, %s) failed: %m", link2, link3); - } + create_links(tmpdir); test_link1(); test_link_loop(); @@ -183,20 +225,7 @@ static void test_init(void) i_fatal("mkdir: %m"); } - link1 = t_strconcat(tmpdir, "/link1", NULL); - if (symlink(tmpdir, link1) < 0) { - i_fatal("symlink(%s, %s) failed: %m", tmpdir, link1); - } - - /* link2 and link3 point to each other to create a loop */ - link2 = t_strconcat(tmpdir, "/link2", NULL); - link3 = t_strconcat(tmpdir, "/link3", NULL); - if (symlink(link3, link2) < 0) { - i_fatal("symlink(%s, %s) failed: %m", link3, link2); - } - if (symlink(link2, link3) < 0) { - i_fatal("symlink(%s, %s) failed: %m", link2, link3); - } + create_links(tmpdir); } void test_path_util(void) @@ -211,6 +240,7 @@ void test_path_util(void) test_nonexistent_path(); test_relative_dotdot(); test_link1(); + test_link4(); test_link_loop(); test_abspath_vs_normpath(); test_link_alloc();