bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi const char *expected = t_strconcat(cwd, "/README.md", NULL);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("README.md", cwd, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("/", "/", &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to(cwd, cwd, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi for (n = 0; *p != '\0'; ++p)
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi n += *p == '/';
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert_idx(t_normpath_to("../", npath, &npath, &error) == 0, l);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to(".", cwd, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("./", cwd, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to(".////", cwd, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi const char *expected = t_strconcat(cwd, "/nonexistent", NULL);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("nonexistent", cwd, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_realpath_to("nonexistent", cwd, &npath, &error) == -1);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to(rel_path, tmpdir, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("..", tmpdir, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("../", tmpdir, &npath, &error) == 0);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("../.", tmpdir, &npath, &error) == 0);
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen const char *old_dir, *npath = NULL, *error = NULL;
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_realpath_to(link1, "/", &npath, &error) == 0);
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen test_assert(t_realpath_to(t_strconcat(link1, "/link1/child", NULL),
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen test_assert_strcmp(npath, t_strconcat(tmpdir, "/child", NULL));
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen /* relative link1/link1/child */
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen i_fatal("t_get_working_dir() failed: %s", error);
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen test_assert(t_realpath(t_strconcat("link1", "/link1/child", NULL),
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen test_assert(t_realpath_to(t_strconcat(link1, "/link4/child", NULL),
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen test_assert_strcmp(npath, t_strconcat(tmpdir, "/child", NULL));
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_realpath_to(link2, "/", &npath, &error) == -1);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi const char *abs = t_abspath_to("../../bin", "/usr/lib/");
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert_strcmp(abs, "/usr/lib//../../bin");
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_normpath_to("../../bin", "/usr///lib/", &norm, &error) == 0);
4ee65ee399f701f94adfe40489a87bdf5edca316Josef 'Jeff' Sipek i_fatal("symlink(%s, %s) failed: %m", tmpdir, link1);
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen const char *link1_child = t_strconcat(link1, "/child", NULL);
4ee65ee399f701f94adfe40489a87bdf5edca316Josef 'Jeff' Sipek /* link2 and link3 point to each other to create a loop */
4ee65ee399f701f94adfe40489a87bdf5edca316Josef 'Jeff' Sipek link2 = t_strconcat(tmpdir, "/link2", NULL);
4ee65ee399f701f94adfe40489a87bdf5edca316Josef 'Jeff' Sipek link3 = t_strconcat(tmpdir, "/link3", NULL);
4ee65ee399f701f94adfe40489a87bdf5edca316Josef 'Jeff' Sipek i_fatal("symlink(%s, %s) failed: %m", link3, link2);
4ee65ee399f701f94adfe40489a87bdf5edca316Josef 'Jeff' Sipek i_fatal("symlink(%s, %s) failed: %m", link2, link3);
d7a2f56cd0e550a92cb160b346f33a84b0daa75eTimo Sirainen /* link4 points to link1 */
4ee65ee399f701f94adfe40489a87bdf5edca316Josef 'Jeff' Sipek i_fatal("symlink(link1, %s) failed: %m", link4);
dfe2b5d36666dfc941821dadf59267d28ff58ff5Aki Tuomi /* idea here is to make sure component-component
dfe2b5d36666dfc941821dadf59267d28ff58ff5Aki Tuomi would optimally hit to the nearest_power value.
dfe2b5d36666dfc941821dadf59267d28ff58ff5Aki Tuomi it has to be big enough to cause requirement for
dfe2b5d36666dfc941821dadf59267d28ff58ff5Aki Tuomi allocation in t_realpath. */
dead275d577c87b8f8ba476a7709db758dfffb9bTimo Sirainen size_t len = nearest_power(I_MAX(127, str_len(basedir) + strlen(COMPONENT_COMPONENT) + 1)) -
71acd1d082704ebabd034d3e73110b39fa5b7ef2Timo Sirainen /* try enough different sized base directory lengths so the code
71acd1d082704ebabd034d3e73110b39fa5b7ef2Timo Sirainen hits the different reallocations and tests for off-by-one errors */
71acd1d082704ebabd034d3e73110b39fa5b7ef2Timo Sirainen /* path_normalize() initially allocates 128 bytes, so we'll test paths
71acd1d082704ebabd034d3e73110b39fa5b7ef2Timo Sirainen up to that length+1. */
796beea86bcdc92e4ba2f4865414a951b1717e5cTimo Sirainen if (unlink_directory(tmpdir, UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0)
796beea86bcdc92e4ba2f4865414a951b1717e5cTimo Sirainen i_error("unlink_directory() failed: %s", error);
b28403dc4da6ee942ff18315596b43d4c4e9b7bdMartti Rannanjärvi test_assert(t_get_working_dir(&cwd, &error) == 0);