Path: blob/main/contrib/kyua/utils/fs/operations_test.cpp
48081 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/fs/operations.hpp"2930extern "C" {31#include <sys/types.h>32#include <sys/stat.h>33#include <sys/wait.h>3435#include <dirent.h>36#include <signal.h>37#include <unistd.h>38}3940#include <cerrno>41#include <cstdlib>42#include <cstring>43#include <iostream>44#include <stdexcept>45#include <string>46#include <vector>4748#include <atf-c++.hpp>4950#include "utils/env.hpp"51#include "utils/format/containers.ipp"52#include "utils/format/macros.hpp"53#include "utils/fs/directory.hpp"54#include "utils/fs/exceptions.hpp"55#include "utils/fs/path.hpp"56#include "utils/optional.ipp"57#include "utils/passwd.hpp"58#include "utils/stream.hpp"59#include "utils/units.hpp"6061namespace fs = utils::fs;62namespace passwd = utils::passwd;63namespace units = utils::units;6465using utils::optional;666768namespace {697071/// Checks if a directory entry exists and matches a specific type.72///73/// \param dir The directory in which to look for the entry.74/// \param name The name of the entry to look up.75/// \param expected_type The expected type of the file as given by dir(5).76///77/// \return True if the entry exists and matches the given type; false78/// otherwise.79static bool80lookup(const char* dir, const char* name, const unsigned int expected_type)81{82DIR* dirp = ::opendir(dir);83ATF_REQUIRE(dirp != NULL);8485bool found = false;86struct dirent* dp;87while (!found && (dp = readdir(dirp)) != NULL) {88if (std::strcmp(dp->d_name, name) == 0) {89struct ::stat s;90const fs::path lookup_path = fs::path(dir) / name;91ATF_REQUIRE(::stat(lookup_path.c_str(), &s) != -1);92if ((s.st_mode & S_IFMT) == expected_type) {93found = true;94}95}96}97::closedir(dirp);98return found;99}100101102} // anonymous namespace103104105ATF_TEST_CASE_WITHOUT_HEAD(copy__ok);106ATF_TEST_CASE_BODY(copy__ok)107{108const fs::path source("f1.txt");109const fs::path target("f2.txt");110111atf::utils::create_file(source.str(), "This is the input");112fs::copy(source, target);113ATF_REQUIRE(atf::utils::compare_file(target.str(), "This is the input"));114}115116117ATF_TEST_CASE_WITHOUT_HEAD(copy__fail_open);118ATF_TEST_CASE_BODY(copy__fail_open)119{120const fs::path source("f1.txt");121const fs::path target("f2.txt");122123ATF_REQUIRE_THROW_RE(fs::error, "Cannot open copy source f1.txt",124fs::copy(source, target));125}126127128ATF_TEST_CASE(copy__fail_create);129ATF_TEST_CASE_HEAD(copy__fail_create)130{131set_md_var("require.user", "unprivileged");132}133ATF_TEST_CASE_BODY(copy__fail_create)134{135const fs::path source("f1.txt");136const fs::path target("f2.txt");137138atf::utils::create_file(target.str(), "Do not override");139ATF_REQUIRE(::chmod(target.c_str(), 0444) != -1);140141atf::utils::create_file(source.str(), "This is the input");142ATF_REQUIRE_THROW_RE(fs::error, "Cannot create copy target f2.txt",143fs::copy(source, target));144}145146147ATF_TEST_CASE_WITHOUT_HEAD(current_path__ok);148ATF_TEST_CASE_BODY(current_path__ok)149{150const fs::path previous = fs::current_path();151fs::mkdir(fs::path("root"), 0755);152ATF_REQUIRE(::chdir("root") != -1);153const fs::path cwd = fs::current_path();154ATF_REQUIRE_EQ(cwd.str().length() - 5, cwd.str().find("/root"));155ATF_REQUIRE_EQ(previous / "root", cwd);156}157158159ATF_TEST_CASE_WITHOUT_HEAD(current_path__enoent);160ATF_TEST_CASE_BODY(current_path__enoent)161{162const fs::path previous = fs::current_path();163fs::mkdir(fs::path("root"), 0755);164ATF_REQUIRE(::chdir("root") != -1);165ATF_REQUIRE(::rmdir("../root") != -1);166try {167(void)fs::current_path();168fail("system_errpr not raised");169} catch (const fs::system_error& e) {170ATF_REQUIRE_EQ(ENOENT, e.original_errno());171}172}173174175ATF_TEST_CASE_WITHOUT_HEAD(exists);176ATF_TEST_CASE_BODY(exists)177{178const fs::path dir("dir");179ATF_REQUIRE(!fs::exists(dir));180fs::mkdir(dir, 0755);181ATF_REQUIRE(fs::exists(dir));182}183184185ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__no_path);186ATF_TEST_CASE_BODY(find_in_path__no_path)187{188utils::unsetenv("PATH");189ATF_REQUIRE(!fs::find_in_path("ls"));190atf::utils::create_file("ls", "");191ATF_REQUIRE(!fs::find_in_path("ls"));192}193194195ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__empty_path);196ATF_TEST_CASE_BODY(find_in_path__empty_path)197{198utils::setenv("PATH", "");199ATF_REQUIRE(!fs::find_in_path("ls"));200atf::utils::create_file("ls", "");201ATF_REQUIRE(!fs::find_in_path("ls"));202}203204205ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__one_component);206ATF_TEST_CASE_BODY(find_in_path__one_component)207{208const fs::path dir = fs::current_path() / "bin";209fs::mkdir(dir, 0755);210utils::setenv("PATH", dir.str());211212ATF_REQUIRE(!fs::find_in_path("ls"));213atf::utils::create_file((dir / "ls").str(), "");214ATF_REQUIRE_EQ(dir / "ls", fs::find_in_path("ls").get());215}216217218ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__many_components);219ATF_TEST_CASE_BODY(find_in_path__many_components)220{221const fs::path dir1 = fs::current_path() / "dir1";222const fs::path dir2 = fs::current_path() / "dir2";223fs::mkdir(dir1, 0755);224fs::mkdir(dir2, 0755);225utils::setenv("PATH", dir1.str() + ":" + dir2.str());226227ATF_REQUIRE(!fs::find_in_path("ls"));228atf::utils::create_file((dir2 / "ls").str(), "");229ATF_REQUIRE_EQ(dir2 / "ls", fs::find_in_path("ls").get());230atf::utils::create_file((dir1 / "ls").str(), "");231ATF_REQUIRE_EQ(dir1 / "ls", fs::find_in_path("ls").get());232}233234235ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__current_directory);236ATF_TEST_CASE_BODY(find_in_path__current_directory)237{238utils::setenv("PATH", "bin:");239240ATF_REQUIRE(!fs::find_in_path("foo-bar"));241atf::utils::create_file("foo-bar", "");242ATF_REQUIRE_EQ(fs::path("foo-bar").to_absolute(),243fs::find_in_path("foo-bar").get());244}245246247ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__always_absolute);248ATF_TEST_CASE_BODY(find_in_path__always_absolute)249{250fs::mkdir(fs::path("my-bin"), 0755);251utils::setenv("PATH", "my-bin");252253ATF_REQUIRE(!fs::find_in_path("abcd"));254atf::utils::create_file("my-bin/abcd", "");255ATF_REQUIRE_EQ(fs::path("my-bin/abcd").to_absolute(),256fs::find_in_path("abcd").get());257}258259260ATF_TEST_CASE_WITHOUT_HEAD(free_disk_space__ok__smoke);261ATF_TEST_CASE_BODY(free_disk_space__ok__smoke)262{263const units::bytes space = fs::free_disk_space(fs::path("."));264ATF_REQUIRE(space > units::MB); // Simple test that should always pass.265}266267268/// Unmounts a directory without raising errors.269///270/// \param cookie Name of a file that exists while the mount point is still271/// mounted. Used to prevent a double-unmount, which would print a272/// misleading error message.273/// \param mount_point Path to the mount point to unmount.274static void275cleanup_mount_point(const fs::path& cookie, const fs::path& mount_point)276{277try {278if (fs::exists(cookie)) {279fs::unmount(mount_point);280}281} catch (const std::runtime_error& e) {282std::cerr << "Failed trying to unmount " + mount_point.str() +283" during cleanup: " << e.what() << '\n';284}285}286287288ATF_TEST_CASE_WITH_CLEANUP(free_disk_space__ok__real);289ATF_TEST_CASE_HEAD(free_disk_space__ok__real)290{291set_md_var("require.user", "root");292}293ATF_TEST_CASE_BODY(free_disk_space__ok__real)294{295try {296const fs::path mount_point("mount_point");297fs::mkdir(mount_point, 0755);298fs::mount_tmpfs(mount_point, units::bytes(32 * units::MB));299atf::utils::create_file("mounted", "");300const units::bytes space = fs::free_disk_space(fs::path(mount_point));301fs::unmount(mount_point);302fs::unlink(fs::path("mounted"));303ATF_REQUIRE(space < 35 * units::MB);304ATF_REQUIRE(space > 28 * units::MB);305} catch (const fs::unsupported_operation_error& e) {306ATF_SKIP(e.what());307}308}309ATF_TEST_CASE_CLEANUP(free_disk_space__ok__real)310{311cleanup_mount_point(fs::path("mounted"), fs::path("mount_point"));312}313314315ATF_TEST_CASE_WITHOUT_HEAD(free_disk_space__fail);316ATF_TEST_CASE_BODY(free_disk_space__fail)317{318ATF_REQUIRE_THROW_RE(fs::error, "Failed to stat file system for missing",319fs::free_disk_space(fs::path("missing")));320}321322323ATF_TEST_CASE_WITHOUT_HEAD(is_directory__ok);324ATF_TEST_CASE_BODY(is_directory__ok)325{326const fs::path file("file");327atf::utils::create_file(file.str(), "");328ATF_REQUIRE(!fs::is_directory(file));329330const fs::path dir("dir");331fs::mkdir(dir, 0755);332ATF_REQUIRE(fs::is_directory(dir));333}334335336ATF_TEST_CASE_WITH_CLEANUP(is_directory__fail);337ATF_TEST_CASE_HEAD(is_directory__fail)338{339set_md_var("require.user", "unprivileged");340}341ATF_TEST_CASE_BODY(is_directory__fail)342{343fs::mkdir(fs::path("dir"), 0000);344ATF_REQUIRE_THROW(fs::error, fs::is_directory(fs::path("dir/foo")));345}346ATF_TEST_CASE_CLEANUP(is_directory__fail)347{348if (::chmod("dir", 0755) == -1) {349// If we cannot restore the original permissions, we cannot do much350// more. However, leaving an unwritable directory behind will cause the351// runtime engine to report us as broken.352}353}354355356ATF_TEST_CASE_WITHOUT_HEAD(mkdir__ok);357ATF_TEST_CASE_BODY(mkdir__ok)358{359fs::mkdir(fs::path("dir"), 0755);360ATF_REQUIRE(lookup(".", "dir", S_IFDIR));361}362363364ATF_TEST_CASE_WITHOUT_HEAD(mkdir__enoent);365ATF_TEST_CASE_BODY(mkdir__enoent)366{367try {368fs::mkdir(fs::path("dir1/dir2"), 0755);369fail("system_error not raised");370} catch (const fs::system_error& e) {371ATF_REQUIRE_EQ(ENOENT, e.original_errno());372}373ATF_REQUIRE(!lookup(".", "dir1", S_IFDIR));374ATF_REQUIRE(!lookup(".", "dir2", S_IFDIR));375}376377378ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__one_component);379ATF_TEST_CASE_BODY(mkdir_p__one_component)380{381ATF_REQUIRE(!lookup(".", "new-dir", S_IFDIR));382fs::mkdir_p(fs::path("new-dir"), 0755);383ATF_REQUIRE(lookup(".", "new-dir", S_IFDIR));384}385386387ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__many_components);388ATF_TEST_CASE_BODY(mkdir_p__many_components)389{390ATF_REQUIRE(!lookup(".", "a", S_IFDIR));391fs::mkdir_p(fs::path("a/b/c"), 0755);392ATF_REQUIRE(lookup(".", "a", S_IFDIR));393ATF_REQUIRE(lookup("a", "b", S_IFDIR));394ATF_REQUIRE(lookup("a/b", "c", S_IFDIR));395}396397398ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__already_exists);399ATF_TEST_CASE_BODY(mkdir_p__already_exists)400{401fs::mkdir(fs::path("a"), 0755);402fs::mkdir(fs::path("a/b"), 0755);403fs::mkdir_p(fs::path("a/b"), 0755);404}405406407ATF_TEST_CASE(mkdir_p__eacces)408ATF_TEST_CASE_HEAD(mkdir_p__eacces)409{410set_md_var("require.user", "unprivileged");411}412ATF_TEST_CASE_BODY(mkdir_p__eacces)413{414fs::mkdir(fs::path("a"), 0755);415fs::mkdir(fs::path("a/b"), 0755);416ATF_REQUIRE(::chmod("a/b", 0555) != -1);417try {418fs::mkdir_p(fs::path("a/b/c/d"), 0755);419fail("system_error not raised");420} catch (const fs::system_error& e) {421ATF_REQUIRE_EQ(EACCES, e.original_errno());422}423ATF_REQUIRE(lookup(".", "a", S_IFDIR));424ATF_REQUIRE(lookup("a", "b", S_IFDIR));425ATF_REQUIRE(!lookup(".", "c", S_IFDIR));426ATF_REQUIRE(!lookup("a", "c", S_IFDIR));427ATF_REQUIRE(!lookup("a/b", "c", S_IFDIR));428}429430431ATF_TEST_CASE_WITHOUT_HEAD(mkdtemp_public)432ATF_TEST_CASE_BODY(mkdtemp_public)433{434const fs::path tmpdir = fs::current_path() / "tmp";435utils::setenv("TMPDIR", tmpdir.str());436fs::mkdir(tmpdir, 0755);437438const std::string dir_template("tempdir.XXXXXX");439const fs::path tempdir = fs::mkdtemp_public(dir_template);440ATF_REQUIRE(!lookup("tmp", dir_template.c_str(), S_IFDIR));441ATF_REQUIRE(lookup("tmp", tempdir.leaf_name().c_str(), S_IFDIR));442}443444445ATF_TEST_CASE(mkdtemp_public__getcwd_as_non_root)446ATF_TEST_CASE_HEAD(mkdtemp_public__getcwd_as_non_root)447{448set_md_var("require.config", "unprivileged-user");449set_md_var("require.user", "root");450}451ATF_TEST_CASE_BODY(mkdtemp_public__getcwd_as_non_root)452{453const std::string dir_template("dir.XXXXXX");454const fs::path dir = fs::mkdtemp_public(dir_template);455const fs::path subdir = dir / "subdir";456fs::mkdir(subdir, 0755);457458const uid_t old_euid = ::geteuid();459const gid_t old_egid = ::getegid();460461const passwd::user unprivileged_user = passwd::find_user_by_name(462get_config_var("unprivileged-user"));463ATF_REQUIRE(::setegid(unprivileged_user.gid) != -1);464ATF_REQUIRE(::seteuid(unprivileged_user.uid) != -1);465466// The next code block runs as non-root. We cannot use any ATF macros nor467// functions in it because a failure would cause the test to attempt to468// write to the ATF result file which may not be writable as non-root.469bool failed = false;470{471try {472if (::chdir(subdir.c_str()) == -1) {473std::cerr << "Cannot enter directory\n";474failed |= true;475} else {476fs::current_path();477}478} catch (const fs::error& e) {479failed |= true;480std::cerr << "Failed to query current path in: " << subdir << '\n';481}482483if (::seteuid(old_euid) == -1) {484std::cerr << "Failed to restore euid; cannot continue\n";485std::abort();486}487if (::setegid(old_egid) == -1) {488std::cerr << "Failed to restore egid; cannot continue\n";489std::abort();490}491}492493if (failed)494fail("Test failed; see stdout for details");495}496497498ATF_TEST_CASE(mkdtemp_public__search_permissions_as_non_root)499ATF_TEST_CASE_HEAD(mkdtemp_public__search_permissions_as_non_root)500{501set_md_var("require.config", "unprivileged-user");502set_md_var("require.user", "root");503}504ATF_TEST_CASE_BODY(mkdtemp_public__search_permissions_as_non_root)505{506const std::string dir_template("dir.XXXXXX");507const fs::path dir = fs::mkdtemp_public(dir_template);508const fs::path cookie = dir / "not-secret";509atf::utils::create_file(cookie.str(), "this is readable");510511// We are running as root so there is no reason to assume that our current512// work directory is accessible by non-root. Weaken the permissions so that513// our code below works.514ATF_REQUIRE(::chmod(".", 0755) != -1);515516const uid_t old_euid = ::geteuid();517const gid_t old_egid = ::getegid();518519const passwd::user unprivileged_user = passwd::find_user_by_name(520get_config_var("unprivileged-user"));521ATF_REQUIRE(::setegid(unprivileged_user.gid) != -1);522ATF_REQUIRE(::seteuid(unprivileged_user.uid) != -1);523524// The next code block runs as non-root. We cannot use any ATF macros nor525// functions in it because a failure would cause the test to attempt to526// write to the ATF result file which may not be writable as non-root.527bool failed = false;528{529try {530const std::string contents = utils::read_file(cookie);531std::cerr << "Read contents: " << contents << '\n';532failed |= (contents != "this is readable");533} catch (const std::runtime_error& e) {534failed |= true;535std::cerr << "Failed to read " << cookie << '\n';536}537538if (::seteuid(old_euid) == -1) {539std::cerr << "Failed to restore euid; cannot continue\n";540std::abort();541}542if (::setegid(old_egid) == -1) {543std::cerr << "Failed to restore egid; cannot continue\n";544std::abort();545}546}547548if (failed)549fail("Test failed; see stdout for details");550}551552553ATF_TEST_CASE_WITHOUT_HEAD(mkstemp)554ATF_TEST_CASE_BODY(mkstemp)555{556const fs::path tmpdir = fs::current_path() / "tmp";557utils::setenv("TMPDIR", tmpdir.str());558fs::mkdir(tmpdir, 0755);559560const std::string file_template("tempfile.XXXXXX");561const fs::path tempfile = fs::mkstemp(file_template);562ATF_REQUIRE(!lookup("tmp", file_template.c_str(), S_IFREG));563ATF_REQUIRE(lookup("tmp", tempfile.leaf_name().c_str(), S_IFREG));564}565566567static void568test_mount_tmpfs_ok(const units::bytes& size)569{570const fs::path mount_point("mount_point");571fs::mkdir(mount_point, 0755);572573try {574atf::utils::create_file("outside", "");575fs::mount_tmpfs(mount_point, size);576atf::utils::create_file("mounted", "");577atf::utils::create_file((mount_point / "inside").str(), "");578579struct ::stat outside, inside;580ATF_REQUIRE(::stat("outside", &outside) != -1);581ATF_REQUIRE(::stat((mount_point / "inside").c_str(), &inside) != -1);582ATF_REQUIRE(outside.st_dev != inside.st_dev);583fs::unmount(mount_point);584} catch (const fs::unsupported_operation_error& e) {585ATF_SKIP(e.what());586}587}588589590ATF_TEST_CASE_WITH_CLEANUP(mount_tmpfs__ok__default_size)591ATF_TEST_CASE_HEAD(mount_tmpfs__ok__default_size)592{593set_md_var("require.user", "root");594}595ATF_TEST_CASE_BODY(mount_tmpfs__ok__default_size)596{597test_mount_tmpfs_ok(units::bytes());598}599ATF_TEST_CASE_CLEANUP(mount_tmpfs__ok__default_size)600{601cleanup_mount_point(fs::path("mounted"), fs::path("mount_point"));602}603604605ATF_TEST_CASE_WITH_CLEANUP(mount_tmpfs__ok__explicit_size)606ATF_TEST_CASE_HEAD(mount_tmpfs__ok__explicit_size)607{608set_md_var("require.user", "root");609}610ATF_TEST_CASE_BODY(mount_tmpfs__ok__explicit_size)611{612test_mount_tmpfs_ok(units::bytes(10 * units::MB));613}614ATF_TEST_CASE_CLEANUP(mount_tmpfs__ok__explicit_size)615{616cleanup_mount_point(fs::path("mounted"), fs::path("mount_point"));617}618619620ATF_TEST_CASE(mount_tmpfs__fail)621ATF_TEST_CASE_HEAD(mount_tmpfs__fail)622{623set_md_var("require.user", "root");624}625ATF_TEST_CASE_BODY(mount_tmpfs__fail)626{627try {628fs::mount_tmpfs(fs::path("non-existent"));629} catch (const fs::unsupported_operation_error& e) {630ATF_SKIP(e.what());631} catch (const fs::error& e) {632// Expected.633}634}635636637ATF_TEST_CASE_WITHOUT_HEAD(rm_r__empty);638ATF_TEST_CASE_BODY(rm_r__empty)639{640fs::mkdir(fs::path("root"), 0755);641ATF_REQUIRE(lookup(".", "root", S_IFDIR));642fs::rm_r(fs::path("root"));643ATF_REQUIRE(!lookup(".", "root", S_IFDIR));644}645646647ATF_TEST_CASE_WITHOUT_HEAD(rm_r__files_and_directories);648ATF_TEST_CASE_BODY(rm_r__files_and_directories)649{650fs::mkdir(fs::path("root"), 0755);651atf::utils::create_file("root/.hidden_file", "");652fs::mkdir(fs::path("root/.hidden_dir"), 0755);653atf::utils::create_file("root/.hidden_dir/a", "");654atf::utils::create_file("root/file", "");655atf::utils::create_file("root/with spaces", "");656fs::mkdir(fs::path("root/dir1"), 0755);657fs::mkdir(fs::path("root/dir1/dir2"), 0755);658atf::utils::create_file("root/dir1/dir2/file", "");659fs::mkdir(fs::path("root/dir1/dir3"), 0755);660ATF_REQUIRE(lookup(".", "root", S_IFDIR));661fs::rm_r(fs::path("root"));662ATF_REQUIRE(!lookup(".", "root", S_IFDIR));663}664665666ATF_TEST_CASE_WITHOUT_HEAD(rm_r__bad_perms);667ATF_TEST_CASE_BODY(rm_r__bad_perms)668{669fs::mkdir(fs::path("root"), 0755);670fs::mkdir(fs::path("root/dir"), 0755);671atf::utils::create_file("root/dir/file", "");672::chmod(fs::path("root/dir").c_str(), 0000);673ATF_REQUIRE(lookup(".", "root", S_IFDIR));674fs::rm_r(fs::path("root"));675ATF_REQUIRE(!lookup(".", "root", S_IFDIR));676}677678679ATF_TEST_CASE_WITHOUT_HEAD(rmdir__ok)680ATF_TEST_CASE_BODY(rmdir__ok)681{682ATF_REQUIRE(::mkdir("foo", 0755) != -1);683ATF_REQUIRE(::access("foo", X_OK) == 0);684fs::rmdir(fs::path("foo"));685ATF_REQUIRE(::access("foo", X_OK) == -1);686}687688689ATF_TEST_CASE_WITHOUT_HEAD(rmdir__fail)690ATF_TEST_CASE_BODY(rmdir__fail)691{692ATF_REQUIRE_THROW_RE(fs::system_error, "Removal of foo failed",693fs::rmdir(fs::path("foo")));694}695696697ATF_TEST_CASE_WITHOUT_HEAD(scan_directory__ok)698ATF_TEST_CASE_BODY(scan_directory__ok)699{700fs::mkdir(fs::path("dir"), 0755);701atf::utils::create_file("dir/foo", "");702atf::utils::create_file("dir/.hidden", "");703704const std::set< fs::directory_entry > contents = fs::scan_directory(705fs::path("dir"));706707std::set< fs::directory_entry > exp_contents;708exp_contents.insert(fs::directory_entry("."));709exp_contents.insert(fs::directory_entry(".."));710exp_contents.insert(fs::directory_entry(".hidden"));711exp_contents.insert(fs::directory_entry("foo"));712713ATF_REQUIRE_EQ(exp_contents, contents);714}715716717ATF_TEST_CASE_WITHOUT_HEAD(scan_directory__fail)718ATF_TEST_CASE_BODY(scan_directory__fail)719{720ATF_REQUIRE_THROW_RE(fs::system_error, "opendir(.*missing.*) failed",721fs::scan_directory(fs::path("missing")));722}723724725ATF_TEST_CASE_WITHOUT_HEAD(unlink__ok)726ATF_TEST_CASE_BODY(unlink__ok)727{728atf::utils::create_file("foo", "");729ATF_REQUIRE(::access("foo", R_OK) == 0);730fs::unlink(fs::path("foo"));731ATF_REQUIRE(::access("foo", R_OK) == -1);732}733734735ATF_TEST_CASE_WITHOUT_HEAD(unlink__fail)736ATF_TEST_CASE_BODY(unlink__fail)737{738ATF_REQUIRE_THROW_RE(fs::system_error, "Removal of foo failed",739fs::unlink(fs::path("foo")));740}741742743ATF_TEST_CASE(unmount__ok)744ATF_TEST_CASE_HEAD(unmount__ok)745{746set_md_var("require.user", "root");747}748ATF_TEST_CASE_BODY(unmount__ok)749{750const fs::path mount_point("mount_point");751fs::mkdir(mount_point, 0755);752753atf::utils::create_file((mount_point / "test1").str(), "");754try {755fs::mount_tmpfs(mount_point);756} catch (const fs::unsupported_operation_error& e) {757ATF_SKIP(e.what());758}759760atf::utils::create_file((mount_point / "test2").str(), "");761762ATF_REQUIRE(!fs::exists(mount_point / "test1"));763ATF_REQUIRE( fs::exists(mount_point / "test2"));764fs::unmount(mount_point);765ATF_REQUIRE( fs::exists(mount_point / "test1"));766ATF_REQUIRE(!fs::exists(mount_point / "test2"));767}768769770ATF_TEST_CASE(unmount__fail)771ATF_TEST_CASE_HEAD(unmount__fail)772{773set_md_var("require.user", "root");774}775ATF_TEST_CASE_BODY(unmount__fail)776{777ATF_REQUIRE_THROW(fs::error, fs::unmount(fs::path("non-existent")));778}779780781ATF_INIT_TEST_CASES(tcs)782{783ATF_ADD_TEST_CASE(tcs, copy__ok);784ATF_ADD_TEST_CASE(tcs, copy__fail_open);785ATF_ADD_TEST_CASE(tcs, copy__fail_create);786787ATF_ADD_TEST_CASE(tcs, current_path__ok);788ATF_ADD_TEST_CASE(tcs, current_path__enoent);789790ATF_ADD_TEST_CASE(tcs, exists);791792ATF_ADD_TEST_CASE(tcs, find_in_path__no_path);793ATF_ADD_TEST_CASE(tcs, find_in_path__empty_path);794ATF_ADD_TEST_CASE(tcs, find_in_path__one_component);795ATF_ADD_TEST_CASE(tcs, find_in_path__many_components);796ATF_ADD_TEST_CASE(tcs, find_in_path__current_directory);797ATF_ADD_TEST_CASE(tcs, find_in_path__always_absolute);798799ATF_ADD_TEST_CASE(tcs, free_disk_space__ok__smoke);800ATF_ADD_TEST_CASE(tcs, free_disk_space__ok__real);801ATF_ADD_TEST_CASE(tcs, free_disk_space__fail);802803ATF_ADD_TEST_CASE(tcs, is_directory__ok);804ATF_ADD_TEST_CASE(tcs, is_directory__fail);805806ATF_ADD_TEST_CASE(tcs, mkdir__ok);807ATF_ADD_TEST_CASE(tcs, mkdir__enoent);808809ATF_ADD_TEST_CASE(tcs, mkdir_p__one_component);810ATF_ADD_TEST_CASE(tcs, mkdir_p__many_components);811ATF_ADD_TEST_CASE(tcs, mkdir_p__already_exists);812ATF_ADD_TEST_CASE(tcs, mkdir_p__eacces);813814ATF_ADD_TEST_CASE(tcs, mkdtemp_public);815ATF_ADD_TEST_CASE(tcs, mkdtemp_public__getcwd_as_non_root);816ATF_ADD_TEST_CASE(tcs, mkdtemp_public__search_permissions_as_non_root);817818ATF_ADD_TEST_CASE(tcs, mkstemp);819820ATF_ADD_TEST_CASE(tcs, mount_tmpfs__ok__default_size);821ATF_ADD_TEST_CASE(tcs, mount_tmpfs__ok__explicit_size);822ATF_ADD_TEST_CASE(tcs, mount_tmpfs__fail);823824ATF_ADD_TEST_CASE(tcs, rm_r__empty);825ATF_ADD_TEST_CASE(tcs, rm_r__files_and_directories);826ATF_ADD_TEST_CASE(tcs, rm_r__bad_perms);827828ATF_ADD_TEST_CASE(tcs, rmdir__ok);829ATF_ADD_TEST_CASE(tcs, rmdir__fail);830831ATF_ADD_TEST_CASE(tcs, scan_directory__ok);832ATF_ADD_TEST_CASE(tcs, scan_directory__fail);833834ATF_ADD_TEST_CASE(tcs, unlink__ok);835ATF_ADD_TEST_CASE(tcs, unlink__fail);836837ATF_ADD_TEST_CASE(tcs, unmount__ok);838ATF_ADD_TEST_CASE(tcs, unmount__fail);839}840841842