// Copyright 2012 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// Checks if a directory entry exists and matches a specific type.
///
/// \param dir The directory in which to look for the entry.
/// \param name The name of the entry to look up.
/// \param expected_type The expected type of the file as given by dir(5).
///
/// \return True if the entry exists and matches the given type; false
/// otherwise.
static bool
lookup(const char* dir, const char* name, const int expected_type)
{
DIR* dirp = opendir(dir);
ATF_REQUIRE(dirp != NULL);
bool found = false;
struct dirent* dp;
while (!found && (dp = readdir(dirp)) != NULL) {
if (strcmp(dp->d_name, name) == 0 &&
dp->d_type == expected_type) {
found = true;
}
}
closedir(dirp);
return found;
}
/// Executes 'mount -t tmpfs' (or a similar variant).
///
/// This function must be called from a subprocess, as it never returns.
///
/// \param mount_point Location on which to mount a tmpfs.
static void
run_mount_tmpfs(const char* mount_point)
{
const char* mount_args[16];
const int ret = execvp(mount_args[0], KYUA_DEFS_UNCONST(mount_args));
assert(ret == -1);
err(EXIT_FAILURE, "Failed to exec %s", mount_args[0]);
};
/// Mounts a temporary file system.
///
/// This is only provided for testing purposes. The mounted file system
/// contains no valuable data.
///
/// Note that the calling test case is skipped if the current operating system
/// is not supported.
///
/// \param mount_point The path on which the file system will be mounted.
static void
mount_tmpfs(const char* mount_point)
{
// SunOS's mount(8) requires paths to be absolute. To err on the side of
// caution, let's make it absolute in all cases.
//const fspath abs_mount_point = mount_point.is_absolute() ?
// mount_point : mount_point.to_absolute();
pid_t pid = fork();
ATF_REQUIRE(pid != -1);
if (pid == 0)
run_mount_tmpfs(mount_point);
int status;
ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
ATF_REQUIRE(WIFEXITED(status));
if (WEXITSTATUS(status) == 123)
atf_tc_skip("Don't know how to mount a file system for testing "
"purposes");
else
ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
}
ATF_TC(cleanup__subdir__unprotect_symlink);
ATF_TC_HEAD(cleanup__subdir__unprotect_symlink, tc)
{
atf_tc_set_md_var(tc, "require.progs", "/bin/ls");
// We are ensuring that chmod is not run on the target of a symlink, so
// we cannot be root (nor we don't want to, to prevent unprotecting a
// system file!).
atf_tc_set_md_var(tc, "require.user", "unprivileged");
}
ATF_TC_BODY(cleanup__subdir__unprotect_symlink, tc)
{
ATF_REQUIRE(mkdir("root", 0755) != -1);
ATF_REQUIRE(mkdir("root/dir1", 0755) != -1);
ATF_REQUIRE(symlink("/bin/ls", "root/dir1/ls") != -1);
ATF_REQUIRE(chmod("root/dir1", 0555) != -1);
ATF_REQUIRE(!kyua_error_is_set(kyua_fs_cleanup("root")));
ATF_REQUIRE(!lookup(".", "root", DT_DIR));
}
ATF_TC_WITHOUT_HEAD(cleanup__subdir__links);
ATF_TC_BODY(cleanup__subdir__links, tc)
{
ATF_REQUIRE(mkdir("root", 0755) != -1);
ATF_REQUIRE(mkdir("root/dir1", 0755) != -1);
ATF_REQUIRE(symlink("../../root", "root/dir1/loop") != -1);
ATF_REQUIRE(symlink("non-existent", "root/missing") != -1);
ATF_REQUIRE(lookup(".", "root", DT_DIR));
kyua_error_t error = kyua_fs_cleanup("root");
if (kyua_error_is_set(error)) {
if (lchmod_fails())
atf_tc_expect_fail("lchmod(2) is not implemented in your system");
kyua_error_free(error);
atf_tc_fail("kyua_fs_cleanup returned an error");
}
ATF_REQUIRE(!lookup(".", "root", DT_DIR));
}