test-path-util.c revision 796beea86bcdc92e4ba2f4865414a951b1717e5c
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2016 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *tmpdir;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainenstatic const char *cwd;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *link1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *link2;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *link3;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *expected = t_strconcat(cwd, "/README.md", NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_assert(t_normpath_to("README.md", cwd, &npath, &error) == 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic void test_absolute_path_no_change(void) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_assert(t_normpath_to("/", "/", &npath, &error) == 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_assert(t_normpath_to(cwd, cwd, &npath, &error) == 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int path_height(const char* p) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (n = 0; *p != '\0'; ++p)
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen n += *p == '/';
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainenstatic void test_travel_to_root(void) {
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen test_assert_idx(t_normpath_to("../", npath, &npath, &error) == 0, l);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainenstatic void test_extra_slashes(void) {
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen test_assert(t_normpath_to(".", cwd, &npath, &error) == 0);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen test_assert(t_normpath_to("./", cwd, &npath, &error) == 0);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen test_assert(t_normpath_to(".////", cwd, &npath, &error) == 0);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainenstatic void test_nonexistent_path(void) {
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen const char *expected = t_strconcat(cwd, "/nonexistent", NULL);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_assert(t_normpath_to("nonexistent", cwd, &npath, &error) == 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_assert(t_realpath_to("nonexistent", cwd, &npath, &error) == -1);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen test_assert(t_normpath_to(rel_path, tmpdir, &npath, &error) == 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_assert(t_normpath_to("..", tmpdir, &npath, &error) == 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_assert(t_normpath_to("../", tmpdir, &npath, &error) == 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_assert(t_normpath_to("../.", tmpdir, &npath, &error) == 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic void test_link1() {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen test_assert(t_realpath_to(link1, "/", &npath, &error) == 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen test_assert(t_realpath_to(link2, "/", &npath, &error) == -1);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *abs = t_abspath_to("../../bin", "/usr/lib/");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_assert_strcmp(abs, "/usr/lib//../../bin");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_assert(t_normpath_to("../../bin", "/usr///lib/", &norm, &error) == 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void test_cleanup(void)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (unlink_directory(tmpdir, UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("unlink_directory() failed: %s", error);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void test_init(void) {
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen test_assert(t_get_working_dir(&cwd, &error) == 0);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tmpdir = t_strconcat(cwd, "/"TEMP_DIRNAME, NULL);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen i_fatal("symlink(%s, %s) failed: %m", tmpdir, link1);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen /* link2 and link3 point to each other to create a loop */
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen i_fatal("symlink(%s, %s) failed: %m", link3, link2);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen i_fatal("symlink(%s, %s) failed: %m", link2, link3);