diff options
author | Louis Dionne <ldionne@apple.com> | 2019-03-19 19:27:29 +0000 |
---|---|---|
committer | Louis Dionne <ldionne@apple.com> | 2019-03-19 19:27:29 +0000 |
commit | f7b43230b844373b421571467864a5fbf644e38d (patch) | |
tree | b5cd58d906a56409c2db98997f60f4d2020cbd47 /libcxx/test/std/input.output/filesystems/fs.op.funcs | |
parent | d81df259b3505ba892f2dec1818eada3fe4b56a6 (diff) | |
download | bcm5719-llvm-f7b43230b844373b421571467864a5fbf644e38d.tar.gz bcm5719-llvm-f7b43230b844373b421571467864a5fbf644e38d.zip |
Revert "[libc++] Build <filesystem> support as part of the dylib"
When I applied r356500 (https://reviews.llvm.org/D59152), I somehow
deleted all of filesystem's tests. I will revert r356500 and re-apply
it properly.
llvm-svn: 356505
Diffstat (limited to 'libcxx/test/std/input.output/filesystems/fs.op.funcs')
41 files changed, 5061 insertions, 0 deletions
diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.absolute/absolute.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.absolute/absolute.pass.cpp new file mode 100644 index 00000000000..2f06fd16557 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.absolute/absolute.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path absolute(const path& p, const path& base=current_path()); + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_absolute_path_test_suite) + +TEST_CASE(absolute_signature_test) +{ + const path p; ((void)p); + std::error_code ec; + ASSERT_NOT_NOEXCEPT(absolute(p)); + ASSERT_NOT_NOEXCEPT(absolute(p, ec)); +} + + +TEST_CASE(basic_test) +{ + const fs::path cwd = fs::current_path(); + const struct { + std::string input; + std::string expect; + } TestCases [] = { + {"", cwd / ""}, + {"foo", cwd / "foo"}, + {"foo/", cwd / "foo/"}, + {"/already_absolute", "/already_absolute"} + }; + for (auto& TC : TestCases) { + std::error_code ec = GetTestEC(); + const path ret = absolute(TC.input, ec); + TEST_CHECK(!ec); + TEST_CHECK(ret.is_absolute()); + TEST_CHECK(PathEq(ret, TC.expect)); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.canonical/canonical.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.canonical/canonical.pass.cpp new file mode 100644 index 00000000000..7717c261eb8 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.canonical/canonical.pass.cpp @@ -0,0 +1,123 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path canonical(const path& p); +// path canonical(const path& p, error_code& ec); + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +struct CWDGuard { + path OldCWD; + CWDGuard() : OldCWD(fs::current_path()) { } + ~CWDGuard() { fs::current_path(OldCWD); } + + CWDGuard(CWDGuard const&) = delete; + CWDGuard& operator=(CWDGuard const&) = delete; +}; + +TEST_SUITE(filesystem_canonical_path_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(canonical(p)); + ASSERT_NOT_NOEXCEPT(canonical(p, ec)); +} + +// There are 4 cases is the proposal for absolute path. +// Each scope tests one of the cases. +TEST_CASE(test_canonical) +{ + CWDGuard guard; + // has_root_name() && has_root_directory() + const path Root = StaticEnv::Root; + const path RootName = Root.filename(); + const path DirName = StaticEnv::Dir.filename(); + const path SymlinkName = StaticEnv::SymlinkToFile.filename(); + struct TestCase { + path p; + path expect; + path base; + TestCase(path p1, path e, path b = StaticEnv::Root) + : p(p1), expect(e), base(b) {} + }; + const TestCase testCases[] = { + { ".", Root, Root}, + { DirName / ".." / "." / DirName, StaticEnv::Dir, Root}, + { StaticEnv::Dir2 / "..", StaticEnv::Dir }, + { StaticEnv::Dir3 / "../..", StaticEnv::Dir }, + { StaticEnv::Dir / ".", StaticEnv::Dir }, + { Root / "." / DirName / ".." / DirName, StaticEnv::Dir}, + { path("..") / "." / RootName / DirName / ".." / DirName, StaticEnv::Dir, Root}, + { StaticEnv::SymlinkToFile, StaticEnv::File }, + { SymlinkName, StaticEnv::File, StaticEnv::Root} + }; + for (auto& TC : testCases) { + std::error_code ec = GetTestEC(); + fs::current_path(TC.base); + const path ret = canonical(TC.p, ec); + TEST_REQUIRE(!ec); + const path ret2 = canonical(TC.p); + TEST_CHECK(PathEq(ret, TC.expect)); + TEST_CHECK(PathEq(ret, ret2)); + TEST_CHECK(ret.is_absolute()); + } +} + +TEST_CASE(test_dne_path) +{ + std::error_code ec = GetTestEC(); + { + const path ret = canonical(StaticEnv::DNE, ec); + TEST_CHECK(ec != GetTestEC()); + TEST_REQUIRE(ec); + TEST_CHECK(ret == path{}); + } + { + TEST_CHECK_THROW(filesystem_error, canonical(StaticEnv::DNE)); + } +} + +TEST_CASE(test_exception_contains_paths) +{ +#ifndef TEST_HAS_NO_EXCEPTIONS + CWDGuard guard; + const path p = "blabla/dne"; + try { + canonical(p); + TEST_REQUIRE(false); + } catch (filesystem_error const& err) { + TEST_CHECK(err.path1() == p); + // libc++ provides the current path as the second path in the exception + LIBCPP_ONLY(TEST_CHECK(err.path2() == current_path())); + } + fs::current_path(StaticEnv::Dir); + try { + canonical(p); + TEST_REQUIRE(false); + } catch (filesystem_error const& err) { + TEST_CHECK(err.path1() == p); + LIBCPP_ONLY(TEST_CHECK(err.path2() == StaticEnv::Dir)); + } +#endif +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp new file mode 100644 index 00000000000..c791a74361d --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp @@ -0,0 +1,314 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void copy(const path& from, const path& to); +// void copy(const path& from, const path& to, error_code& ec); +// void copy(const path& from, const path& to, copy_options options); +// void copy(const path& from, const path& to, copy_options options, +// error_code& ec); + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cstddef> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +using CO = fs::copy_options; + +TEST_SUITE(filesystem_copy_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + const copy_options opts{}; ((void)opts); + ASSERT_NOT_NOEXCEPT(fs::copy(p, p)); + ASSERT_NOT_NOEXCEPT(fs::copy(p, p, ec)); + ASSERT_NOT_NOEXCEPT(copy(p, p, opts)); + ASSERT_NOT_NOEXCEPT(copy(p, p, opts, ec)); +} + +// There are 4 cases is the proposal for absolute path. +// Each scope tests one of the cases. +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, path const& t, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::copy(f, t); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == t + && err.code() == ec; + } +#else + ((void)f); ((void)t); ((void)ec); + return true; +#endif + }; + + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path dir = env.create_dir("dir"); + const path fifo = env.create_fifo("fifo"); + TEST_REQUIRE(is_other(fifo)); + + const auto test_ec = GetTestEC(); + + // !exists(f) + { + std::error_code ec = test_ec; + const path f = StaticEnv::DNE; + const path t = env.test_root; + fs::copy(f, t, ec); + TEST_REQUIRE(ec); + TEST_REQUIRE(ec != test_ec); + TEST_CHECK(checkThrow(f, t, ec)); + } + { // equivalent(f, t) == true + std::error_code ec = test_ec; + fs::copy(file, file, ec); + TEST_REQUIRE(ec); + TEST_REQUIRE(ec != test_ec); + TEST_CHECK(checkThrow(file, file, ec)); + } + { // is_directory(from) && is_file(to) + std::error_code ec = test_ec; + fs::copy(dir, file, ec); + TEST_REQUIRE(ec); + TEST_REQUIRE(ec != test_ec); + TEST_CHECK(checkThrow(dir, file, ec)); + } + { // is_other(from) + std::error_code ec = test_ec; + fs::copy(fifo, dir, ec); + TEST_REQUIRE(ec); + TEST_REQUIRE(ec != test_ec); + TEST_CHECK(checkThrow(fifo, dir, ec)); + } + { // is_other(to) + std::error_code ec = test_ec; + fs::copy(file, fifo, ec); + TEST_REQUIRE(ec); + TEST_REQUIRE(ec != test_ec); + TEST_CHECK(checkThrow(file, fifo, ec)); + } +} + +TEST_CASE(from_is_symlink) +{ + scoped_test_env env; + const path file = env.create_file("file", 42); + const path symlink = env.create_symlink(file, "sym"); + const path dne = env.make_env_path("dne"); + + { // skip symlinks + std::error_code ec = GetTestEC(); + fs::copy(symlink, dne, copy_options::skip_symlinks, ec); + TEST_CHECK(!ec); + TEST_CHECK(!exists(dne)); + } + { + const path dest = env.make_env_path("dest"); + std::error_code ec = GetTestEC(); + fs::copy(symlink, dest, copy_options::copy_symlinks, ec); + TEST_CHECK(!ec); + TEST_CHECK(exists(dest)); + TEST_CHECK(is_symlink(dest)); + } + { // copy symlink but target exists + std::error_code ec = GetTestEC(); + fs::copy(symlink, file, copy_options::copy_symlinks, ec); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + } + { // create symlinks but target exists + std::error_code ec = GetTestEC(); + fs::copy(symlink, file, copy_options::create_symlinks, ec); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + } +} + +TEST_CASE(from_is_regular_file) +{ + scoped_test_env env; + const path file = env.create_file("file", 42); + const path dir = env.create_dir("dir"); + { // skip copy because of directory + const path dest = env.make_env_path("dest1"); + std::error_code ec = GetTestEC(); + fs::copy(file, dest, CO::directories_only, ec); + TEST_CHECK(!ec); + TEST_CHECK(!exists(dest)); + } + { // create symlink to file + const path dest = env.make_env_path("sym"); + std::error_code ec = GetTestEC(); + fs::copy(file, dest, CO::create_symlinks, ec); + TEST_CHECK(!ec); + TEST_CHECK(is_symlink(dest)); + TEST_CHECK(equivalent(file, canonical(dest))); + } + { // create hard link to file + const path dest = env.make_env_path("hardlink"); + TEST_CHECK(hard_link_count(file) == 1); + std::error_code ec = GetTestEC(); + fs::copy(file, dest, CO::create_hard_links, ec); + TEST_CHECK(!ec); + TEST_CHECK(exists(dest)); + TEST_CHECK(hard_link_count(file) == 2); + } + { // is_directory(t) + const path dest_dir = env.create_dir("dest_dir"); + const path expect_dest = dest_dir / file.filename(); + std::error_code ec = GetTestEC(); + fs::copy(file, dest_dir, ec); + TEST_CHECK(!ec); + TEST_CHECK(is_regular_file(expect_dest)); + } + { // otherwise copy_file(from, to, ...) + const path dest = env.make_env_path("file_copy"); + std::error_code ec = GetTestEC(); + fs::copy(file, dest, ec); + TEST_CHECK(!ec); + TEST_CHECK(is_regular_file(dest)); + } +} + +TEST_CASE(from_is_directory) +{ + struct FileInfo { + path filename; + std::size_t size; + }; + const FileInfo files[] = { + {"file1", 0}, + {"file2", 42}, + {"file3", 300} + }; + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path nested_dir_name = "dir2"; + const path nested_dir = env.create_dir("dir/dir2"); + + for (auto& FI : files) { + env.create_file(dir / FI.filename, FI.size); + env.create_file(nested_dir / FI.filename, FI.size); + } + { // test for non-existent directory + const path dest = env.make_env_path("dest_dir1"); + std::error_code ec = GetTestEC(); + fs::copy(dir, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_directory(dest)); + for (auto& FI : files) { + path created = dest / FI.filename; + TEST_CHECK(is_regular_file(created)); + TEST_CHECK(file_size(created) == FI.size); + } + TEST_CHECK(!is_directory(dest / nested_dir_name)); + } + { // test for existing directory + const path dest = env.create_dir("dest_dir2"); + std::error_code ec = GetTestEC(); + fs::copy(dir, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_directory(dest)); + for (auto& FI : files) { + path created = dest / FI.filename; + TEST_CHECK(is_regular_file(created)); + TEST_CHECK(file_size(created) == FI.size); + } + TEST_CHECK(!is_directory(dest / nested_dir_name)); + } + { // test recursive copy + const path dest = env.make_env_path("dest_dir3"); + std::error_code ec = GetTestEC(); + fs::copy(dir, dest, CO::recursive, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_directory(dest)); + const path nested_dest = dest / nested_dir_name; + TEST_REQUIRE(is_directory(nested_dest)); + for (auto& FI : files) { + path created = dest / FI.filename; + path nested_created = nested_dest / FI.filename; + TEST_CHECK(is_regular_file(created)); + TEST_CHECK(file_size(created) == FI.size); + TEST_CHECK(is_regular_file(nested_created)); + TEST_CHECK(file_size(nested_created) == FI.size); + } + } +} + +TEST_CASE(test_copy_symlinks_to_symlink_dir) +{ + scoped_test_env env; + const path file1 = env.create_file("file1", 42); + const path file2 = env.create_file("file2", 101); + const path file2_sym = env.create_symlink(file2, "file2_sym"); + const path dir = env.create_dir("dir"); + const path dir_sym = env.create_symlink(dir, "dir_sym"); + { + std::error_code ec = GetTestEC(); + fs::copy(file1, dir_sym, copy_options::copy_symlinks, ec); + TEST_CHECK(!ec); + const path dest = env.make_env_path("dir/file1"); + TEST_CHECK(exists(dest)); + TEST_CHECK(!is_symlink(dest)); + TEST_CHECK(file_size(dest) == 42); + } +} + + +TEST_CASE(test_dir_create_symlink) +{ + scoped_test_env env; + const path dir = env.create_dir("dir1"); + const path dest = env.make_env_path("dne"); + { + std::error_code ec = GetTestEC(); + fs::copy(dir, dest, copy_options::create_symlinks, ec); + TEST_CHECK(ec == std::make_error_code(std::errc::is_a_directory)); + TEST_CHECK(!exists(dest)); + TEST_CHECK(!is_symlink(dest)); + } + { + std::error_code ec = GetTestEC(); + fs::copy(dir, dest, copy_options::create_symlinks|copy_options::recursive, ec); + TEST_CHECK(ec == std::make_error_code(std::errc::is_a_directory)); + TEST_CHECK(!exists(dest)); + TEST_CHECK(!is_symlink(dest)); + } +} + +TEST_CASE(test_otherwise_no_effects_clause) +{ + scoped_test_env env; + const path dir = env.create_dir("dir1"); + { // skip copy because of directory + const path dest = env.make_env_path("dest1"); + std::error_code ec; + fs::copy(dir, dest, CO::directories_only, ec); + TEST_CHECK(!ec); + TEST_CHECK(!exists(dest)); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file.pass.cpp new file mode 100644 index 00000000000..90da3b535ad --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file.pass.cpp @@ -0,0 +1,188 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool copy_file(const path& from, const path& to); +// bool copy_file(const path& from, const path& to, error_code& ec) noexcept; +// bool copy_file(const path& from, const path& to, copy_options options); +// bool copy_file(const path& from, const path& to, copy_options options, +// error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <chrono> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +#include <iostream> + +using namespace fs; + +using CO = fs::copy_options; + +TEST_SUITE(filesystem_copy_file_test_suite) + +TEST_CASE(test_signatures) { + const path p; + ((void)p); + const copy_options opts{}; + ((void)opts); + std::error_code ec; + ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p)), bool); + ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts)), bool); + ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, ec)), bool); + ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts, ec)), bool); + ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p)); + ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, opts)); + ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, ec)); + ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, opts, ec)); +} + +TEST_CASE(test_error_reporting) { + + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path file2 = env.create_file("file2", 55); + const path non_regular_file = env.create_fifo("non_reg"); + const path dne = env.make_env_path("dne"); + + { // exists(to) && equivalent(to, from) + std::error_code ec; + TEST_CHECK(fs::copy_file(file, file, copy_options::overwrite_existing, + ec) == false); + TEST_CHECK(ErrorIs(ec, std::errc::file_exists)); + ExceptionChecker Checker(file, file, std::errc::file_exists, "copy_file"); + TEST_CHECK_THROW_RESULT(filesystem_error, Checker, copy_file(file, file, copy_options::overwrite_existing)); + + } + { // exists(to) && !(skip_existing | overwrite_existing | update_existing) + std::error_code ec; + TEST_CHECK(fs::copy_file(file, file2, ec) == false); + TEST_CHECK(ErrorIs(ec, std::errc::file_exists)); + ExceptionChecker Checker(file, file, std::errc::file_exists, "copy_file"); + TEST_CHECK_THROW_RESULT(filesystem_error, Checker, copy_file(file, file, copy_options::overwrite_existing)); + + } +} + +TEST_CASE(non_regular_file_test) { + scoped_test_env env; + const path fifo = env.create_fifo("fifo"); + const path dest = env.make_env_path("dest"); + const path file = env.create_file("file", 42); + + { + std::error_code ec = GetTestEC(); + TEST_REQUIRE(fs::copy_file(fifo, dest, ec) == false); + TEST_CHECK(ErrorIs(ec, std::errc::not_supported)); + TEST_CHECK(!exists(dest)); + } + { + std::error_code ec = GetTestEC(); + TEST_REQUIRE(fs::copy_file(file, fifo, copy_options::overwrite_existing, + ec) == false); + TEST_CHECK(ErrorIs(ec, std::errc::not_supported)); + TEST_CHECK(is_fifo(fifo)); + } + +} + +TEST_CASE(test_attributes_get_copied) { + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path dest = env.make_env_path("file2"); + auto st = status(file); + perms new_perms = perms::owner_read; + permissions(file, new_perms); + std::error_code ec = GetTestEC(); + TEST_REQUIRE(fs::copy_file(file, dest, ec) == true); + TEST_CHECK(!ec); + auto new_st = status(dest); + TEST_CHECK(new_st.permissions() == new_perms); +} + +TEST_CASE(copy_dir_test) { + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path dest = env.create_dir("dir1"); + std::error_code ec = GetTestEC(); + TEST_CHECK(fs::copy_file(file, dest, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + ec = GetTestEC(); + TEST_CHECK(fs::copy_file(dest, file, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); +} + +TEST_CASE(copy_file) { + scoped_test_env env; + const path file = env.create_file("file1", 42); + + { // !exists(to) + const path dest = env.make_env_path("dest1"); + std::error_code ec = GetTestEC(); + + TEST_REQUIRE(fs::copy_file(file, dest, ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(file_size(dest) == 42); + } + { // exists(to) && overwrite_existing + const path dest = env.create_file("dest2", 55); + permissions(dest, perms::all); + permissions(file, + perms::group_write | perms::owner_write | perms::others_write, + perm_options::remove); + + std::error_code ec = GetTestEC(); + TEST_REQUIRE(fs::copy_file(file, dest, copy_options::overwrite_existing, + ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(file_size(dest) == 42); + TEST_CHECK(status(dest).permissions() == status(file).permissions()); + } + { // exists(to) && update_existing + using Sec = std::chrono::seconds; + const path older = env.create_file("older_file", 1); + + SleepFor(Sec(2)); + const path from = env.create_file("update_from", 55); + + SleepFor(Sec(2)); + const path newer = env.create_file("newer_file", 2); + + std::error_code ec = GetTestEC(); + TEST_REQUIRE( + fs::copy_file(from, older, copy_options::update_existing, ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(file_size(older) == 55); + + TEST_REQUIRE( + fs::copy_file(from, newer, copy_options::update_existing, ec) == false); + TEST_CHECK(!ec); + TEST_CHECK(file_size(newer) == 2); + } + { // skip_existing + const path file2 = env.create_file("file2", 55); + std::error_code ec = GetTestEC(); + TEST_REQUIRE(fs::copy_file(file, file2, copy_options::skip_existing, ec) == + false); + TEST_CHECK(!ec); + TEST_CHECK(file_size(file2) == 55); + } +} + + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file_large.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file_large.pass.cpp new file mode 100644 index 00000000000..f419039fa89 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file_large.pass.cpp @@ -0,0 +1,98 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 +// REQUIRES: long_tests + +// <filesystem> + +// bool copy_file(const path& from, const path& to); +// bool copy_file(const path& from, const path& to, error_code& ec) noexcept; +// bool copy_file(const path& from, const path& to, copy_options options); +// bool copy_file(const path& from, const path& to, copy_options options, +// error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <chrono> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_copy_file_test_suite) + +static std::string random_hex_chars(uintmax_t size) { + std::string data; + data.reserve(size); + for (uintmax_t I = 0; I < size; ++I) + data.push_back(random_utils::random_hex_char()); + return data; +} + +// This test is intended to test 'sendfile's 2gb limit for a single call, and +// to ensure that libc++ correctly copies files larger than that limit. +// However it requires allocating ~5GB of filesystem space. This might not +// be acceptable on all systems. +TEST_CASE(large_file) { + using namespace fs; + constexpr uintmax_t sendfile_size_limit = 2147479552ull; + constexpr uintmax_t additional_size = 1024; + constexpr uintmax_t test_file_size = sendfile_size_limit + additional_size; + static_assert(test_file_size > sendfile_size_limit, ""); + + scoped_test_env env; + + // Check that we have more than sufficient room to create the files needed + // to perform the test. + if (space(env.test_root).available < 3 * test_file_size) { + TEST_UNSUPPORTED(); + } + + // Use python to create a file right at the size limit. + const path file = env.create_file("source", sendfile_size_limit); + // Create some random data that looks different than the data before the + // size limit. + const std::string additional_data = random_hex_chars(additional_size); + // Append this known data to the end of the source file. + { + std::ofstream outf(file.native(), std::ios_base::app); + TEST_REQUIRE(outf.good()); + outf << additional_data; + TEST_REQUIRE(outf); + } + TEST_REQUIRE(file_size(file) == test_file_size); + const path dest = env.make_env_path("dest"); + + std::error_code ec = GetTestEC(); + TEST_CHECK(copy_file(file, dest, ec)); + TEST_CHECK(!ec); + + TEST_REQUIRE(is_regular_file(dest)); + TEST_CHECK(file_size(dest) == test_file_size); + + // Read the data from the end of the destination file, and ensure it matches + // the data at the end of the source file. + std::string out_data; + out_data.reserve(additional_size); + { + std::ifstream dest_file(dest.native()); + TEST_REQUIRE(dest_file); + dest_file.seekg(sendfile_size_limit); + TEST_REQUIRE(dest_file); + dest_file >> out_data; + TEST_CHECK(dest_file.eof()); + } + TEST_CHECK(out_data.size() == additional_data.size()); + TEST_CHECK(out_data == additional_data); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_symlink/copy_symlink.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_symlink/copy_symlink.pass.cpp new file mode 100644 index 00000000000..e687ee5ded9 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_symlink/copy_symlink.pass.cpp @@ -0,0 +1,107 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void copy_symlink(const path& existing_symlink, const path& new_symlink); +// void copy_symlink(const path& existing_symlink, const path& new_symlink, +// error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_copy_symlink_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(fs::copy_symlink(p, p)); + ASSERT_NOEXCEPT(fs::copy_symlink(p, p, ec)); +} + + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, path const& t, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::copy_symlink(f, t); + return true; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.code() == ec; + } +#else + ((void)f); ((void)t); ((void)ec); + return true; +#endif + }; + + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path file2 = env.create_file("file2", 55); + const path sym = env.create_symlink(file, "sym"); + const path dir = env.create_dir("dir"); + const path dne = env.make_env_path("dne"); + { // from is a file, not a symlink + std::error_code ec; + fs::copy_symlink(file, dne, ec); + TEST_REQUIRE(ec); + TEST_CHECK(checkThrow(file, dne, ec)); + } + { // from is a file, not a symlink + std::error_code ec; + fs::copy_symlink(dir, dne, ec); + TEST_REQUIRE(ec); + TEST_CHECK(checkThrow(dir, dne, ec)); + } + { // destination exists + std::error_code ec; + fs::copy_symlink(sym, file2, ec); + TEST_REQUIRE(ec); + } +} + +TEST_CASE(copy_symlink_basic) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path dir_sym = env.create_symlink(dir, "dir_sym"); + const path file = env.create_file("file", 42); + const path file_sym = env.create_symlink(file, "file_sym"); + { // test for directory symlinks + const path dest = env.make_env_path("dest1"); + std::error_code ec; + fs::copy_symlink(dir_sym, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_symlink(dest)); + TEST_CHECK(equivalent(dest, dir)); + } + { // test for file symlinks + const path dest = env.make_env_path("dest2"); + std::error_code ec; + fs::copy_symlink(file_sym, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_symlink(dest)); + TEST_CHECK(equivalent(dest, file)); + } +} + + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directories/create_directories.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directories/create_directories.pass.cpp new file mode 100644 index 00000000000..c7a72038a76 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directories/create_directories.pass.cpp @@ -0,0 +1,100 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool create_directories(const path& p); +// bool create_directories(const path& p, error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_create_directories_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::create_directories(p)), bool); + ASSERT_SAME_TYPE(decltype(fs::create_directories(p, ec)), bool); + ASSERT_NOT_NOEXCEPT(fs::create_directories(p)); + ASSERT_NOT_NOEXCEPT(fs::create_directories(p, ec)); +} + +TEST_CASE(create_existing_directory) +{ + scoped_test_env env; + const path dir = env.create_dir("dir1"); + std::error_code ec; + TEST_CHECK(fs::create_directories(dir, ec) == false); + TEST_CHECK(!ec); + TEST_CHECK(is_directory(dir)); +} + +TEST_CASE(create_directory_one_level) +{ + scoped_test_env env; + const path dir = env.make_env_path("dir1"); + std::error_code ec; + TEST_CHECK(fs::create_directories(dir, ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(is_directory(dir)); +} + +TEST_CASE(create_directories_multi_level) +{ + scoped_test_env env; + const path dir = env.make_env_path("dir1/dir2/dir3"); + std::error_code ec; + TEST_CHECK(fs::create_directories(dir, ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(is_directory(dir)); +} + +TEST_CASE(create_directory_symlinks) { + scoped_test_env env; + const path root = env.create_dir("dir"); + const path sym_dest_dead = env.make_env_path("dead"); + const path dead_sym = env.create_symlink(sym_dest_dead, "dir/sym_dir"); + const path target = env.make_env_path("dir/sym_dir/foo"); + { + std::error_code ec = GetTestEC(); + TEST_CHECK(create_directories(target, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(!exists(sym_dest_dead)); + TEST_CHECK(!exists(dead_sym)); + } +} + + +TEST_CASE(create_directory_through_symlinks) { + scoped_test_env env; + const path root = env.create_dir("dir"); + const path sym_dir = env.create_symlink(root, "sym_dir"); + const path target = env.make_env_path("sym_dir/foo"); + const path resolved_target = env.make_env_path("dir/foo"); + TEST_REQUIRE(is_directory(sym_dir)); + { + std::error_code ec = GetTestEC(); + TEST_CHECK(create_directories(target, ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(is_directory(target)); + TEST_CHECK(is_directory(resolved_target)); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory/create_directory.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory/create_directory.pass.cpp new file mode 100644 index 00000000000..f512a30efce --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory/create_directory.pass.cpp @@ -0,0 +1,102 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool create_directory(const path& p); +// bool create_directory(const path& p, error_code& ec) noexcept; +// bool create_directory(const path& p, const path& attr); +// bool create_directory(const path& p, const path& attr, error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +#include <sys/types.h> +#include <sys/stat.h> + +using namespace fs; + +fs::perms read_umask() { + mode_t old_mask = umask(0); + umask(old_mask); // reset the mask to the old value. + return static_cast<fs::perms>(old_mask); +} + +TEST_SUITE(filesystem_create_directory_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::create_directory(p)), bool); + ASSERT_SAME_TYPE(decltype(fs::create_directory(p, ec)), bool); + ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p)), bool); + ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p, ec)), bool); + ASSERT_NOT_NOEXCEPT(fs::create_directory(p)); + ASSERT_NOEXCEPT(fs::create_directory(p, ec)); + ASSERT_NOT_NOEXCEPT(fs::create_directory(p, p)); + ASSERT_NOEXCEPT(fs::create_directory(p, p, ec)); +} + + +TEST_CASE(create_existing_directory) +{ + scoped_test_env env; + const path dir = env.create_dir("dir1"); + std::error_code ec; + TEST_CHECK(fs::create_directory(dir, ec) == false); + TEST_CHECK(!ec); + TEST_CHECK(is_directory(dir)); + // Test throwing version + TEST_CHECK(fs::create_directory(dir) == false); +} + +TEST_CASE(create_directory_one_level) +{ + scoped_test_env env; + const path dir = env.make_env_path("dir1"); + std::error_code ec; + TEST_CHECK(fs::create_directory(dir, ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(is_directory(dir)); + + auto st = status(dir); + const perms expect_perms = perms::all & ~(read_umask()); + TEST_CHECK((st.permissions() & perms::all) == expect_perms); +} + +TEST_CASE(create_directory_multi_level) +{ + scoped_test_env env; + const path dir = env.make_env_path("dir1/dir2"); + const path dir1 = env.make_env_path("dir1"); + std::error_code ec; + TEST_CHECK(fs::create_directory(dir, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(!is_directory(dir)); + TEST_CHECK(!is_directory(dir1)); +} + +TEST_CASE(dest_is_file) +{ + scoped_test_env env; + const path file = env.create_file("file", 42); + std::error_code ec = GetTestEC(); + TEST_CHECK(fs::create_directory(file, ec) == false); + TEST_CHECK(!ec); + TEST_CHECK(is_regular_file(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory/create_directory_with_attributes.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory/create_directory_with_attributes.pass.cpp new file mode 100644 index 00000000000..796b7a72af3 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory/create_directory_with_attributes.pass.cpp @@ -0,0 +1,131 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool create_directory(const path& p, const path& attr); +// bool create_directory(const path& p, const path& attr, error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_create_directory_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p)), bool); + ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p, ec)), bool); + ASSERT_NOT_NOEXCEPT(fs::create_directory(p, p)); + ASSERT_NOEXCEPT(fs::create_directory(p, p, ec)); +} + +TEST_CASE(create_existing_directory) +{ + scoped_test_env env; + const path dir = env.create_dir("dir1"); + const path dir2 = env.create_dir("dir2"); + + const perms orig_p = status(dir).permissions(); + permissions(dir2, perms::none); + + std::error_code ec; + TEST_CHECK(fs::create_directory(dir, dir2, ec) == false); + TEST_CHECK(!ec); + + // Check that the permissions were unchanged + TEST_CHECK(orig_p == status(dir).permissions()); + + // Test throwing version + TEST_CHECK(fs::create_directory(dir, dir2) == false); +} + +TEST_CASE(create_directory_one_level) +{ + scoped_test_env env; + // Remove setgid which mkdir would inherit + permissions(env.test_root, perms::set_gid, perm_options::remove); + + const path dir = env.make_env_path("dir1"); + const path attr_dir = env.create_dir("dir2"); + permissions(attr_dir, perms::none); + + std::error_code ec; + TEST_CHECK(fs::create_directory(dir, attr_dir, ec) == true); + TEST_CHECK(!ec); + TEST_CHECK(is_directory(dir)); + + // Check that the new directory has the same permissions as attr_dir + auto st = status(dir); + TEST_CHECK(st.permissions() == perms::none); +} + +TEST_CASE(create_directory_multi_level) +{ + scoped_test_env env; + const path dir = env.make_env_path("dir1/dir2"); + const path dir1 = env.make_env_path("dir1"); + const path attr_dir = env.create_dir("attr_dir"); + std::error_code ec = GetTestEC(); + TEST_CHECK(fs::create_directory(dir, attr_dir, ec) == false); + TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory)); + TEST_CHECK(!is_directory(dir)); + TEST_CHECK(!is_directory(dir1)); +} + +TEST_CASE(dest_is_file) +{ + scoped_test_env env; + const path file = env.create_file("file", 42); + const path attr_dir = env.create_dir("attr_dir"); + std::error_code ec = GetTestEC(); + TEST_CHECK(fs::create_directory(file, attr_dir, ec) == false); + TEST_CHECK(!ec); + TEST_CHECK(is_regular_file(file)); +} + +TEST_CASE(attr_dir_is_invalid) { + scoped_test_env env; + const path file = env.create_file("file", 42); + const path dest = env.make_env_path("dir"); + const path dne = env.make_env_path("dne"); + { + std::error_code ec = GetTestEC(); + TEST_CHECK(create_directory(dest, file, ec) == false); + TEST_CHECK(ErrorIs(ec, std::errc::not_a_directory)); + } + TEST_REQUIRE(!exists(dest)); + { + std::error_code ec = GetTestEC(); + TEST_CHECK(create_directory(dest, dne, ec) == false); + TEST_CHECK(ErrorIs(ec, std::errc::not_a_directory)); + } +} + +TEST_CASE(dest_is_symlink) { + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path sym = env.create_symlink("dne_sym", "dne_sym_name"); + { + std::error_code ec = GetTestEC(); + TEST_CHECK(create_directory(sym, dir, ec) == false); + TEST_CHECK(!ec); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory_symlink/create_directory_symlink.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory_symlink/create_directory_symlink.pass.cpp new file mode 100644 index 00000000000..e4bf0907436 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directory_symlink/create_directory_symlink.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void create_directory_symlink(const path& existing_symlink, const path& new_symlink); +// void create_directory_symlink(const path& existing_symlink, const path& new_symlink, +// error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_create_directory_symlink_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(fs::create_directory_symlink(p, p)); + ASSERT_NOEXCEPT(fs::create_directory_symlink(p, p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path file2 = env.create_file("file2", 55); + const path sym = env.create_symlink(file, "sym"); + { // destination exists + std::error_code ec; + fs::create_directory_symlink(sym, file2, ec); + TEST_REQUIRE(ec); + } +} + +TEST_CASE(create_directory_symlink_basic) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path dir_sym = env.create_symlink(dir, "dir_sym"); + + const path dest = env.make_env_path("dest1"); + std::error_code ec; + fs::create_directory_symlink(dir_sym, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_symlink(dest)); + TEST_CHECK(equivalent(dest, dir)); +} + + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_hard_link/create_hard_link.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_hard_link/create_hard_link.pass.cpp new file mode 100644 index 00000000000..b645ccbc253 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_hard_link/create_hard_link.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void create_hard_link(const path& existing_symlink, const path& new_symlink); +// void create_hard_link(const path& existing_symlink, const path& new_symlink, +// error_code& ec) noexcept; + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_create_hard_link_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(fs::create_hard_link(p, p)); + ASSERT_NOEXCEPT(fs::create_hard_link(p, p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path file2 = env.create_file("file2", 55); + const path sym = env.create_symlink(file, "sym"); + { // destination exists + std::error_code ec; + fs::create_hard_link(sym, file2, ec); + TEST_REQUIRE(ec); + } +} + +TEST_CASE(create_file_hard_link) +{ + scoped_test_env env; + const path file = env.create_file("file"); + const path dest = env.make_env_path("dest1"); + std::error_code ec; + TEST_CHECK(hard_link_count(file) == 1); + fs::create_hard_link(file, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(exists(dest)); + TEST_CHECK(equivalent(dest, file)); + TEST_CHECK(hard_link_count(file) == 2); +} + +TEST_CASE(create_directory_hard_link_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path dest = env.make_env_path("dest2"); + std::error_code ec; + + fs::create_hard_link(dir, dest, ec); + TEST_REQUIRE(ec); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp new file mode 100644 index 00000000000..a2658a51ead --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp @@ -0,0 +1,75 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void create_symlink(const path& existing_symlink, const path& new_symlink); +// void create_symlink(const path& existing_symlink, const path& new_symlink, +// error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_create_symlink_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(fs::create_symlink(p, p)); + ASSERT_NOEXCEPT(fs::create_symlink(p, p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path file2 = env.create_file("file2", 55); + const path sym = env.create_symlink(file, "sym"); + { // destination exists + std::error_code ec; + fs::create_symlink(sym, file2, ec); + TEST_REQUIRE(ec); + } +} + +TEST_CASE(create_symlink_basic) +{ + scoped_test_env env; + const path file = env.create_file("file", 42); + const path file_sym = env.create_symlink(file, "file_sym"); + const path dir = env.create_dir("dir"); + const path dir_sym = env.create_symlink(dir, "dir_sym"); + { + const path dest = env.make_env_path("dest1"); + std::error_code ec; + fs::create_symlink(file_sym, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_symlink(dest)); + TEST_CHECK(equivalent(dest, file)); + } + { + const path dest = env.make_env_path("dest2"); + std::error_code ec; + fs::create_symlink(dir_sym, dest, ec); + TEST_REQUIRE(!ec); + TEST_CHECK(is_symlink(dest)); + TEST_CHECK(equivalent(dest, dir)); + } +} + + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.current_path/current_path.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.current_path/current_path.pass.cpp new file mode 100644 index 00000000000..41136a7a956 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.current_path/current_path.pass.cpp @@ -0,0 +1,92 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path current_path(); +// path current_path(error_code& ec); +// void current_path(path const&); +// void current_path(path const&, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_current_path_path_test_suite) + +TEST_CASE(current_path_signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(current_path()); + ASSERT_NOT_NOEXCEPT(current_path(ec)); + ASSERT_NOT_NOEXCEPT(current_path(p)); + ASSERT_NOEXCEPT(current_path(p, ec)); +} + +TEST_CASE(current_path_test) +{ + std::error_code ec; + const path p = current_path(ec); + TEST_REQUIRE(!ec); + TEST_CHECK(p.is_absolute()); + TEST_CHECK(is_directory(p)); + + const path p2 = current_path(); + TEST_CHECK(p2 == p); +} + +TEST_CASE(current_path_after_change_test) +{ + const path new_path = StaticEnv::Dir; + current_path(new_path); + TEST_CHECK(current_path() == new_path); +} + +TEST_CASE(current_path_is_file_test) +{ + const path p = StaticEnv::File; + std::error_code ec; + const path old_p = current_path(); + current_path(p, ec); + TEST_CHECK(ec); + TEST_CHECK(old_p == current_path()); +} + +TEST_CASE(set_to_non_absolute_path) +{ + const path base = StaticEnv::Dir; + current_path(base); + const path p = StaticEnv::Dir2.filename(); + std::error_code ec; + current_path(p, ec); + TEST_CHECK(!ec); + const path new_cwd = current_path(); + TEST_CHECK(new_cwd == StaticEnv::Dir2); + TEST_CHECK(new_cwd.is_absolute()); +} + +TEST_CASE(set_to_empty) +{ + const path p = ""; + std::error_code ec; + const path old_p = current_path(); + current_path(p, ec); + TEST_CHECK(ec); + TEST_CHECK(old_p == current_path()); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.equivalent/equivalent.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.equivalent/equivalent.pass.cpp new file mode 100644 index 00000000000..ebe4fc67bb9 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.equivalent/equivalent.pass.cpp @@ -0,0 +1,111 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool equivalent(path const& lhs, path const& rhs); +// bool equivalent(path const& lhs, path const& rhs, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(equivalent_test_suite) + +TEST_CASE(signature_test) { + const path p; + ((void)p); + std::error_code ec; + ((void)ec); + ASSERT_NOEXCEPT(equivalent(p, p, ec)); + ASSERT_NOT_NOEXCEPT(equivalent(p, p)); +} + +TEST_CASE(equivalent_test) { + struct TestCase { + path lhs; + path rhs; + bool expect; + }; + const TestCase testCases[] = { + {StaticEnv::Dir, StaticEnv::Dir, true}, + {StaticEnv::File, StaticEnv::Dir, false}, + {StaticEnv::Dir, StaticEnv::SymlinkToDir, true}, + {StaticEnv::Dir, StaticEnv::SymlinkToFile, false}, + {StaticEnv::File, StaticEnv::File, true}, + {StaticEnv::File, StaticEnv::SymlinkToFile, true}, + }; + for (auto& TC : testCases) { + std::error_code ec; + TEST_CHECK(equivalent(TC.lhs, TC.rhs, ec) == TC.expect); + TEST_CHECK(!ec); + } +} + +TEST_CASE(equivalent_reports_error_if_input_dne) { + const path E = StaticEnv::File; + const path DNE = StaticEnv::DNE; + { // Test that an error is reported when either of the paths don't exist + std::error_code ec = GetTestEC(); + TEST_CHECK(equivalent(E, DNE, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + } + { + std::error_code ec = GetTestEC(); + TEST_CHECK(equivalent(DNE, E, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + } + { + TEST_CHECK_THROW(filesystem_error, equivalent(DNE, E)); + TEST_CHECK_THROW(filesystem_error, equivalent(E, DNE)); + } + { // Test that an exception is thrown if both paths do not exist. + TEST_CHECK_THROW(filesystem_error, equivalent(DNE, DNE)); + } + { + std::error_code ec = GetTestEC(); + TEST_CHECK(equivalent(DNE, DNE, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + } +} + +TEST_CASE(equivalent_hardlink_succeeds) { + scoped_test_env env; + path const file = env.create_file("file", 42); + const path hl1 = env.create_hardlink(file, "hl1"); + const path hl2 = env.create_hardlink(file, "hl2"); + TEST_CHECK(equivalent(file, hl1)); + TEST_CHECK(equivalent(file, hl2)); + TEST_CHECK(equivalent(hl1, hl2)); +} + +TEST_CASE(equivalent_is_other_succeeds) { + scoped_test_env env; + path const file = env.create_file("file", 42); + const path fifo1 = env.create_fifo("fifo1"); + const path fifo2 = env.create_fifo("fifo2"); + // Required to test behavior for inputs where is_other(p) is true. + TEST_REQUIRE(is_other(fifo1)); + TEST_CHECK(!equivalent(file, fifo1)); + TEST_CHECK(!equivalent(fifo2, file)); + TEST_CHECK(!equivalent(fifo1, fifo2)); + TEST_CHECK(equivalent(fifo1, fifo1)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.exists/exists.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.exists/exists.pass.cpp new file mode 100644 index 00000000000..2ae1c41e64c --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.exists/exists.pass.cpp @@ -0,0 +1,96 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool exists(file_status s) noexcept +// bool exists(path const& p); +// bool exists(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(exists_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(exists(s)); + ASSERT_NOEXCEPT(exists(p, ec)); + ASSERT_NOT_NOEXCEPT(exists(p)); +} + +TEST_CASE(exists_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, true}, + {file_type::directory, true}, + {file_type::symlink, true}, + {file_type::block, true}, + {file_type::character, true}, + {file_type::fifo, true}, + {file_type::socket, true}, + {file_type::unknown, true} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(exists(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(exists(p) == false); + + std::error_code ec = GetTestEC(); + TEST_CHECK(exists(p, ec) == false); + TEST_CHECK(!ec); +} + +TEST_CASE(test_exists_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(exists(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, exists(file)); +} + +TEST_CASE(test_name_too_long) { + std::string long_name(2500, 'a'); + const path file(long_name); + + std::error_code ec; + TEST_CHECK(exists(file, ec) == false); + TEST_CHECK(ec); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.file_size/file_size.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.file_size/file_size.pass.cpp new file mode 100644 index 00000000000..3d597b04665 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.file_size/file_size.pass.cpp @@ -0,0 +1,84 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// uintmax_t file_size(const path& p); +// uintmax_t file_size(const path& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(file_size_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(file_size(p)), uintmax_t); + ASSERT_SAME_TYPE(decltype(file_size(p, ec)), uintmax_t); + ASSERT_NOT_NOEXCEPT(file_size(p)); + ASSERT_NOEXCEPT(file_size(p, ec)); +} + +TEST_CASE(file_size_empty_test) +{ + const path p = StaticEnv::EmptyFile; + TEST_CHECK(file_size(p) == 0); + std::error_code ec; + TEST_CHECK(file_size(p, ec) == 0); +} + +TEST_CASE(file_size_non_empty) +{ + scoped_test_env env; + const path p = env.create_file("file", 42); + TEST_CHECK(file_size(p) == 42); + std::error_code ec; + TEST_CHECK(file_size(p, ec) == 42); +} + +TEST_CASE(symlink_test_case) +{ + const path p = StaticEnv::File; + const path p2 = StaticEnv::SymlinkToFile; + TEST_CHECK(file_size(p) == file_size(p2)); +} + +TEST_CASE(file_size_error_cases) +{ + struct { + path p; + std::errc expected_err; + } TestCases[] = { + {StaticEnv::Dir, std::errc::is_a_directory}, + {StaticEnv::SymlinkToDir, std::errc::is_a_directory}, + {StaticEnv::BadSymlink, std::errc::no_such_file_or_directory}, + {StaticEnv::DNE, std::errc::no_such_file_or_directory}, + {"", std::errc::no_such_file_or_directory}}; + const uintmax_t expect = static_cast<uintmax_t>(-1); + for (auto& TC : TestCases) { + std::error_code ec = GetTestEC(); + TEST_CHECK(file_size(TC.p, ec) == expect); + TEST_CHECK(ErrorIs(ec, TC.expected_err)); + + ExceptionChecker Checker(TC.p, TC.expected_err, "file_size"); + TEST_CHECK_THROW_RESULT(filesystem_error, Checker, file_size(TC.p)); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.hard_lk_ct/hard_link_count.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.hard_lk_ct/hard_link_count.pass.cpp new file mode 100644 index 00000000000..55527c90826 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.hard_lk_ct/hard_link_count.pass.cpp @@ -0,0 +1,98 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// uintmax_t hard_link_count(const path& p); +// uintmax_t hard_link_count(const path& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(hard_link_count_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(hard_link_count(p)), uintmax_t); + ASSERT_SAME_TYPE(decltype(hard_link_count(p, ec)), uintmax_t); + ASSERT_NOT_NOEXCEPT(hard_link_count(p)); + ASSERT_NOEXCEPT(hard_link_count(p, ec)); +} + +TEST_CASE(hard_link_count_for_file) +{ + TEST_CHECK(hard_link_count(StaticEnv::File) == 1); + std::error_code ec; + TEST_CHECK(hard_link_count(StaticEnv::File, ec) == 1); +} + +TEST_CASE(hard_link_count_for_directory) +{ + uintmax_t DirExpect = 3; // hard link from . .. and Dir2 + uintmax_t Dir3Expect = 2; // hard link from . .. + uintmax_t DirExpectAlt = DirExpect; + uintmax_t Dir3ExpectAlt = Dir3Expect; +#if defined(__APPLE__) + // Filesystems formatted with case sensitive hfs+ behave unixish as + // expected. Normal hfs+ filesystems report the number of directory + // entries instead. + DirExpectAlt = 5; // . .. Dir2 file1 file2 + Dir3Expect = 3; // . .. file5 +#endif + TEST_CHECK(hard_link_count(StaticEnv::Dir) == DirExpect || + hard_link_count(StaticEnv::Dir) == DirExpectAlt || + hard_link_count(StaticEnv::Dir) == 1); + TEST_CHECK(hard_link_count(StaticEnv::Dir3) == Dir3Expect || + hard_link_count(StaticEnv::Dir3) == Dir3ExpectAlt || + hard_link_count(StaticEnv::Dir3) == 1); + + std::error_code ec; + TEST_CHECK(hard_link_count(StaticEnv::Dir, ec) == DirExpect || + hard_link_count(StaticEnv::Dir, ec) == DirExpectAlt || + hard_link_count(StaticEnv::Dir) == 1); + TEST_CHECK(hard_link_count(StaticEnv::Dir3, ec) == Dir3Expect || + hard_link_count(StaticEnv::Dir3, ec) == Dir3ExpectAlt || + hard_link_count(StaticEnv::Dir3) == 1); +} +TEST_CASE(hard_link_count_increments_test) +{ + scoped_test_env env; + const path file = env.create_file("file", 42); + TEST_CHECK(hard_link_count(file) == 1); + + env.create_hardlink(file, "file_hl"); + TEST_CHECK(hard_link_count(file) == 2); +} + + +TEST_CASE(hard_link_count_error_cases) +{ + const path testCases[] = { + StaticEnv::BadSymlink, + StaticEnv::DNE + }; + const uintmax_t expect = static_cast<uintmax_t>(-1); + for (auto& TC : testCases) { + std::error_code ec; + TEST_CHECK(hard_link_count(TC, ec) == expect); + TEST_CHECK(ec); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_block_file/is_block_file.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_block_file/is_block_file.pass.cpp new file mode 100644 index 00000000000..fbfb62c0e5f --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_block_file/is_block_file.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_block_file(file_status s) noexcept +// bool is_block_file(path const& p); +// bool is_block_file(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_block_file_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_block_file(s)); + ASSERT_NOEXCEPT(is_block_file(p, ec)); + ASSERT_NOT_NOEXCEPT(is_block_file(p)); +} + +TEST_CASE(is_block_file_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, false}, + {file_type::directory, false}, + {file_type::symlink, false}, + {file_type::block, true}, + {file_type::character, false}, + {file_type::fifo, false}, + {file_type::socket, false}, + {file_type::unknown, false} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_block_file(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_block_file(p) == false); +} + +TEST_CASE(test_is_block_file_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_block_file(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_block_file(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_char_file/is_character_file.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_char_file/is_character_file.pass.cpp new file mode 100644 index 00000000000..e011dce0739 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_char_file/is_character_file.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_character_file(file_status s) noexcept +// bool is_character_file(path const& p); +// bool is_character_file(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_character_file_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_character_file(s)); + ASSERT_NOEXCEPT(is_character_file(p, ec)); + ASSERT_NOT_NOEXCEPT(is_character_file(p)); +} + +TEST_CASE(is_character_file_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, false}, + {file_type::directory, false}, + {file_type::symlink, false}, + {file_type::block, false}, + {file_type::character, true}, + {file_type::fifo, false}, + {file_type::socket, false}, + {file_type::unknown, false} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_character_file(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_character_file(p) == false); +} + +TEST_CASE(test_is_character_file_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_character_file(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_character_file(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_directory/is_directory.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_directory/is_directory.pass.cpp new file mode 100644 index 00000000000..049129834a3 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_directory/is_directory.pass.cpp @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_directory(file_status s) noexcept +// bool is_directory(path const& p); +// bool is_directory(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_directory_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_directory(s)); + ASSERT_NOEXCEPT(is_directory(p, ec)); + ASSERT_NOT_NOEXCEPT(is_directory(p)); +} + +TEST_CASE(is_directory_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, false}, + {file_type::directory, true}, + {file_type::symlink, false}, + {file_type::block, false}, + {file_type::character, false}, + {file_type::fifo, false}, + {file_type::socket, false}, + {file_type::unknown, false} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_directory(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_directory(p) == false); +} + +TEST_CASE(static_env_test) +{ + TEST_CHECK(is_directory(StaticEnv::Dir)); + TEST_CHECK(is_directory(StaticEnv::SymlinkToDir)); + TEST_CHECK(!is_directory(StaticEnv::File)); +} + +TEST_CASE(test_is_directory_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path dir2 = env.create_dir("dir/dir2"); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_directory(dir2, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_directory(dir2)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_empty/is_empty.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_empty/is_empty.pass.cpp new file mode 100644 index 00000000000..8a8fc50f676 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_empty/is_empty.pass.cpp @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_empty(path const& p); +// bool is_empty(path const& p, std::error_code& ec); + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_empty_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(is_empty(p, ec)); + ASSERT_NOT_NOEXCEPT(is_empty(p)); +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + std::error_code ec; + TEST_CHECK(is_empty(p, ec) == false); + TEST_CHECK(ec); + TEST_CHECK_THROW(filesystem_error, is_empty(p)); +} + +TEST_CASE(test_is_empty_directory) +{ + TEST_CHECK(!is_empty(StaticEnv::Dir)); + TEST_CHECK(!is_empty(StaticEnv::SymlinkToDir)); +} + +TEST_CASE(test_is_empty_directory_dynamic) +{ + scoped_test_env env; + TEST_CHECK(is_empty(env.test_root)); + env.create_file("foo", 42); + TEST_CHECK(!is_empty(env.test_root)); +} + +TEST_CASE(test_is_empty_file) +{ + TEST_CHECK(is_empty(StaticEnv::EmptyFile)); + TEST_CHECK(!is_empty(StaticEnv::NonEmptyFile)); +} + +TEST_CASE(test_is_empty_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path dir2 = env.create_dir("dir/dir2"); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_empty(dir2, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_empty(dir2)); +} + +TEST_CASE(test_directory_access_denied) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file1 = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec = GetTestEC(); + TEST_CHECK(is_empty(dir, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + + TEST_CHECK_THROW(filesystem_error, is_empty(dir)); +} + + +TEST_CASE(test_fifo_fails) +{ + scoped_test_env env; + const path fifo = env.create_fifo("fifo"); + + std::error_code ec = GetTestEC(); + TEST_CHECK(is_empty(fifo, ec) == false); + TEST_CHECK(ec); + TEST_CHECK(ec != GetTestEC()); + + TEST_CHECK_THROW(filesystem_error, is_empty(fifo)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_fifo/is_fifo.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_fifo/is_fifo.pass.cpp new file mode 100644 index 00000000000..8cf036dfd89 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_fifo/is_fifo.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_fifo(file_status s) noexcept +// bool is_fifo(path const& p); +// bool is_fifo(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_fifo_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_fifo(s)); + ASSERT_NOEXCEPT(is_fifo(p, ec)); + ASSERT_NOT_NOEXCEPT(is_fifo(p)); +} + +TEST_CASE(is_fifo_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, false}, + {file_type::directory, false}, + {file_type::symlink, false}, + {file_type::block, false}, + {file_type::character, false}, + {file_type::fifo, true}, + {file_type::socket, false}, + {file_type::unknown, false} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_fifo(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_fifo(p) == false); +} + +TEST_CASE(test_is_fifo_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_fifo(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_fifo(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_other/is_other.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_other/is_other.pass.cpp new file mode 100644 index 00000000000..fb7107060c1 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_other/is_other.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_other(file_status s) noexcept +// bool is_other(path const& p); +// bool is_other(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_other_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_other(s)); + ASSERT_NOEXCEPT(is_other(p, ec)); + ASSERT_NOT_NOEXCEPT(is_other(p)); +} + +TEST_CASE(is_other_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, false}, + {file_type::directory, false}, + {file_type::symlink, false}, + {file_type::block, true}, + {file_type::character, true}, + {file_type::fifo, true}, + {file_type::socket, true}, + {file_type::unknown, true} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_other(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_other(p) == false); +} + +TEST_CASE(test_is_other_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_other(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_other(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_regular_file/is_regular_file.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_regular_file/is_regular_file.pass.cpp new file mode 100644 index 00000000000..55d97f2e2e4 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_regular_file/is_regular_file.pass.cpp @@ -0,0 +1,86 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_regular_file(file_status s) noexcept +// bool is_regular_file(path const& p); +// bool is_regular_file(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_regular_file_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_regular_file(s)); + ASSERT_NOEXCEPT(is_regular_file(p, ec)); + ASSERT_NOT_NOEXCEPT(is_regular_file(p)); +} + +TEST_CASE(is_regular_file_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, true}, + {file_type::directory, false}, + {file_type::symlink, false}, + {file_type::block, false}, + {file_type::character, false}, + {file_type::fifo, false}, + {file_type::socket, false}, + {file_type::unknown, false} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_regular_file(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_regular_file(p) == false); + std::error_code ec; + TEST_CHECK(is_regular_file(p, ec) == false); + TEST_CHECK(ec); +} + +TEST_CASE(test_is_regular_file_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_regular_file(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_regular_file(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_socket/is_socket.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_socket/is_socket.pass.cpp new file mode 100644 index 00000000000..f0d894c997c --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_socket/is_socket.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_socket(file_status s) noexcept +// bool is_socket(path const& p); +// bool is_socket(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_socket_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_socket(s)); + ASSERT_NOEXCEPT(is_socket(p, ec)); + ASSERT_NOT_NOEXCEPT(is_socket(p)); +} + +TEST_CASE(is_socket_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, false}, + {file_type::directory, false}, + {file_type::symlink, false}, + {file_type::block, false}, + {file_type::character, false}, + {file_type::fifo, false}, + {file_type::socket, true}, + {file_type::unknown, false} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_socket(s) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_socket(p) == false); +} + +TEST_CASE(test_is_socket_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_socket(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_socket(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_symlink/is_symlink.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_symlink/is_symlink.pass.cpp new file mode 100644 index 00000000000..8d17235714b --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_symlink/is_symlink.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool is_symlink(file_status s) noexcept +// bool is_symlink(path const& p); +// bool is_symlink(path const& p, std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(is_symlink_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOEXCEPT(is_symlink(s)); + ASSERT_NOEXCEPT(is_symlink(p, ec)); + ASSERT_NOT_NOEXCEPT(is_symlink(p)); +} + +TEST_CASE(is_symlink_status_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, false}, + {file_type::regular, false}, + {file_type::directory, false}, + {file_type::symlink, true}, + {file_type::block, false}, + {file_type::character, false}, + {file_type::fifo, false}, + {file_type::socket, false}, + {file_type::unknown, false} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(is_symlink(s) == TC.expect); + } +} + +TEST_CASE(static_env_test) +{ + struct TestCase { + path p; + bool expect; + }; + const TestCase testCases[] = { + {StaticEnv::File, false}, + {StaticEnv::Dir, false}, + {StaticEnv::SymlinkToFile, true}, + {StaticEnv::SymlinkToDir, true}, + {StaticEnv::BadSymlink, true} + }; + for (auto& TC : testCases) { + TEST_CHECK(is_symlink(TC.p) == TC.expect); + } +} + +TEST_CASE(test_exist_not_found) +{ + const path p = StaticEnv::DNE; + TEST_CHECK(is_symlink(p) == false); + std::error_code ec; + TEST_CHECK(is_symlink(p, ec) == false); + TEST_CHECK(ec); +} + +TEST_CASE(test_is_symlink_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec; + TEST_CHECK(is_symlink(file, ec) == false); + TEST_CHECK(ec); + + TEST_CHECK_THROW(filesystem_error, is_symlink(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.last_write_time/last_write_time.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.last_write_time/last_write_time.pass.cpp new file mode 100644 index 00000000000..5e26e8e10a3 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.last_write_time/last_write_time.pass.cpp @@ -0,0 +1,588 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// file_time_type last_write_time(const path& p); +// file_time_type last_write_time(const path& p, std::error_code& ec) noexcept; +// void last_write_time(const path& p, file_time_type new_time); +// void last_write_time(const path& p, file_time_type new_type, +// std::error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <chrono> +#include <fstream> +#include <cstdlib> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +#include <sys/stat.h> +#include <iostream> + +#include <fcntl.h> +#include <sys/time.h> + +using namespace fs; + +using TimeSpec = struct ::timespec; +using StatT = struct ::stat; + +using Sec = std::chrono::duration<file_time_type::rep>; +using Hours = std::chrono::hours; +using Minutes = std::chrono::minutes; +using MicroSec = std::chrono::duration<file_time_type::rep, std::micro>; +using NanoSec = std::chrono::duration<file_time_type::rep, std::nano>; +using std::chrono::duration_cast; + +#if defined(__APPLE__) +TimeSpec extract_mtime(StatT const& st) { return st.st_mtimespec; } +TimeSpec extract_atime(StatT const& st) { return st.st_atimespec; } +#else +TimeSpec extract_mtime(StatT const& st) { return st.st_mtim; } +TimeSpec extract_atime(StatT const& st) { return st.st_atim; } +#endif + +bool ConvertToTimeSpec(TimeSpec& ts, file_time_type ft) { + using SecFieldT = decltype(TimeSpec::tv_sec); + using NSecFieldT = decltype(TimeSpec::tv_nsec); + using SecLim = std::numeric_limits<SecFieldT>; + using NSecLim = std::numeric_limits<NSecFieldT>; + + auto secs = duration_cast<Sec>(ft.time_since_epoch()); + auto nsecs = duration_cast<NanoSec>(ft.time_since_epoch() - secs); + if (nsecs.count() < 0) { + if (Sec::min().count() > SecLim::min()) { + secs += Sec(1); + nsecs -= Sec(1); + } else { + nsecs = NanoSec(0); + } + } + if (SecLim::max() < secs.count() || SecLim::min() > secs.count()) + return false; + if (NSecLim::max() < nsecs.count() || NSecLim::min() > nsecs.count()) + return false; + ts.tv_sec = secs.count(); + ts.tv_nsec = nsecs.count(); + return true; +} + +bool ConvertFromTimeSpec(file_time_type& ft, TimeSpec ts) { + auto secs_part = duration_cast<file_time_type::duration>(Sec(ts.tv_sec)); + if (duration_cast<Sec>(secs_part).count() != ts.tv_sec) + return false; + auto subsecs = duration_cast<file_time_type::duration>(NanoSec(ts.tv_nsec)); + auto dur = secs_part + subsecs; + if (dur < secs_part && subsecs.count() >= 0) + return false; + ft = file_time_type(dur); + return true; +} + +bool CompareTimeExact(TimeSpec ts, TimeSpec ts2) { + return ts2.tv_sec == ts.tv_sec && ts2.tv_nsec == ts.tv_nsec; +} +bool CompareTimeExact(file_time_type ft, TimeSpec ts) { + TimeSpec ts2 = {}; + if (!ConvertToTimeSpec(ts2, ft)) + return false; + return CompareTimeExact(ts, ts2); +} +bool CompareTimeExact(TimeSpec ts, file_time_type ft) { + return CompareTimeExact(ft, ts); +} + +struct Times { + TimeSpec access, write; +}; + +Times GetTimes(path const& p) { + StatT st; + if (::stat(p.c_str(), &st) == -1) { + std::error_code ec(errno, std::generic_category()); +#ifndef TEST_HAS_NO_EXCEPTIONS + throw ec; +#else + std::cerr << ec.message() << std::endl; + std::exit(EXIT_FAILURE); +#endif + } + return {extract_atime(st), extract_mtime(st)}; +} + +TimeSpec LastAccessTime(path const& p) { return GetTimes(p).access; } + +TimeSpec LastWriteTime(path const& p) { return GetTimes(p).write; } + +Times GetSymlinkTimes(path const& p) { + StatT st; + if (::lstat(p.c_str(), &st) == -1) { + std::error_code ec(errno, std::generic_category()); +#ifndef TEST_HAS_NO_EXCEPTIONS + throw ec; +#else + std::cerr << ec.message() << std::endl; + std::exit(EXIT_FAILURE); +#endif + } + Times res; + res.access = extract_atime(st); + res.write = extract_mtime(st); + return res; +} + +namespace { + +// In some configurations, the comparison is tautological and the test is valid. +// We disable the warning so that we can actually test it regardless. Also, that +// diagnostic is pretty new, so also don't fail if old clang does not support it +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-warning-option" +#pragma clang diagnostic ignored "-Wunknown-pragmas" +#pragma clang diagnostic ignored "-Wtautological-constant-compare" +#endif + +static const bool SupportsNegativeTimes = [] { + using namespace std::chrono; + std::error_code ec; + TimeSpec old_write_time, new_write_time; + { // WARNING: Do not assert in this scope. + scoped_test_env env; + const path file = env.create_file("file", 42); + old_write_time = LastWriteTime(file); + file_time_type tp(seconds(-5)); + fs::last_write_time(file, tp, ec); + new_write_time = LastWriteTime(file); + } + + return !ec && new_write_time.tv_sec < 0; +}(); + +static const bool SupportsMaxTime = [] { + using namespace std::chrono; + TimeSpec max_ts = {}; + if (!ConvertToTimeSpec(max_ts, file_time_type::max())) + return false; + + std::error_code ec; + TimeSpec old_write_time, new_write_time; + { // WARNING: Do not assert in this scope. + scoped_test_env env; + const path file = env.create_file("file", 42); + old_write_time = LastWriteTime(file); + file_time_type tp = file_time_type::max(); + fs::last_write_time(file, tp, ec); + new_write_time = LastWriteTime(file); + } + return !ec && new_write_time.tv_sec > max_ts.tv_sec - 1; +}(); + +static const bool SupportsMinTime = [] { + using namespace std::chrono; + TimeSpec min_ts = {}; + if (!ConvertToTimeSpec(min_ts, file_time_type::min())) + return false; + std::error_code ec; + TimeSpec old_write_time, new_write_time; + { // WARNING: Do not assert in this scope. + scoped_test_env env; + const path file = env.create_file("file", 42); + old_write_time = LastWriteTime(file); + file_time_type tp = file_time_type::min(); + fs::last_write_time(file, tp, ec); + new_write_time = LastWriteTime(file); + } + return !ec && new_write_time.tv_sec < min_ts.tv_sec + 1; +}(); + +static const bool SupportsNanosecondRoundTrip = [] { + NanoSec ns(3); + static_assert(std::is_same<file_time_type::period, std::nano>::value, ""); + + // Test that the system call we use to set the times also supports nanosecond + // resolution. (utimes does not) + file_time_type ft(ns); + { + scoped_test_env env; + const path p = env.create_file("file", 42); + last_write_time(p, ft); + return last_write_time(p) == ft; + } +}(); + +// The HFS+ filesystem (used by default before macOS 10.13) stores timestamps at +// a 1-second granularity, and APFS (now the default) at a 1 nanosecond granularity. +// 1-second granularity is also the norm on many of the supported filesystems +// on Linux as well. +static const bool WorkaroundStatTruncatesToSeconds = [] { + MicroSec micros(3); + static_assert(std::is_same<file_time_type::period, std::nano>::value, ""); + + file_time_type ft(micros); + { + scoped_test_env env; + const path p = env.create_file("file", 42); + if (LastWriteTime(p).tv_nsec != 0) + return false; + last_write_time(p, ft); + return last_write_time(p) != ft && LastWriteTime(p).tv_nsec == 0; + } +}(); + +static const bool SupportsMinRoundTrip = [] { + TimeSpec ts = {}; + if (!ConvertToTimeSpec(ts, file_time_type::min())) + return false; + file_time_type min_val = {}; + if (!ConvertFromTimeSpec(min_val, ts)) + return false; + return min_val == file_time_type::min(); +}(); + +} // end namespace + +static bool CompareTime(TimeSpec t1, TimeSpec t2) { + if (SupportsNanosecondRoundTrip) + return CompareTimeExact(t1, t2); + if (t1.tv_sec != t2.tv_sec) + return false; + + auto diff = std::abs(t1.tv_nsec - t2.tv_nsec); + if (WorkaroundStatTruncatesToSeconds) + return diff < duration_cast<NanoSec>(Sec(1)).count(); + return diff < duration_cast<NanoSec>(MicroSec(1)).count(); +} + +static bool CompareTime(file_time_type t1, TimeSpec t2) { + TimeSpec ts1 = {}; + if (!ConvertToTimeSpec(ts1, t1)) + return false; + return CompareTime(ts1, t2); +} + +static bool CompareTime(TimeSpec t1, file_time_type t2) { + return CompareTime(t2, t1); +} + +static bool CompareTime(file_time_type t1, file_time_type t2) { + auto min_secs = duration_cast<Sec>(file_time_type::min().time_since_epoch()); + bool IsMin = + t1.time_since_epoch() < min_secs || t2.time_since_epoch() < min_secs; + + if (SupportsNanosecondRoundTrip && (!IsMin || SupportsMinRoundTrip)) + return t1 == t2; + if (IsMin) { + return duration_cast<Sec>(t1.time_since_epoch()) == + duration_cast<Sec>(t2.time_since_epoch()); + } + file_time_type::duration dur; + if (t1 > t2) + dur = t1 - t2; + else + dur = t2 - t1; + if (WorkaroundStatTruncatesToSeconds) + return duration_cast<Sec>(dur).count() == 0; + return duration_cast<MicroSec>(dur).count() == 0; +} + +// Check if a time point is representable on a given filesystem. Check that: +// (A) 'tp' is representable as a time_t +// (B) 'tp' is non-negative or the filesystem supports negative times. +// (C) 'tp' is not 'file_time_type::max()' or the filesystem supports the max +// value. +// (D) 'tp' is not 'file_time_type::min()' or the filesystem supports the min +// value. +inline bool TimeIsRepresentableByFilesystem(file_time_type tp) { + TimeSpec ts = {}; + if (!ConvertToTimeSpec(ts, tp)) + return false; + else if (tp.time_since_epoch().count() < 0 && !SupportsNegativeTimes) + return false; + else if (tp == file_time_type::max() && !SupportsMaxTime) + return false; + else if (tp == file_time_type::min() && !SupportsMinTime) + return false; + return true; +} + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + +// Create a sub-second duration using the smallest period the filesystem supports. +file_time_type::duration SubSec(long long val) { + using SubSecT = file_time_type::duration; + if (SupportsNanosecondRoundTrip) { + return duration_cast<SubSecT>(NanoSec(val)); + } else { + return duration_cast<SubSecT>(MicroSec(val)); + } +} + +TEST_SUITE(last_write_time_test_suite) + +TEST_CASE(signature_test) +{ + const file_time_type t; + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(last_write_time(p)), file_time_type); + ASSERT_SAME_TYPE(decltype(last_write_time(p, ec)), file_time_type); + ASSERT_SAME_TYPE(decltype(last_write_time(p, t)), void); + ASSERT_SAME_TYPE(decltype(last_write_time(p, t, ec)), void); + ASSERT_NOT_NOEXCEPT(last_write_time(p)); + ASSERT_NOT_NOEXCEPT(last_write_time(p, t)); + ASSERT_NOEXCEPT(last_write_time(p, ec)); + ASSERT_NOEXCEPT(last_write_time(p, t, ec)); +} + +TEST_CASE(read_last_write_time_static_env_test) +{ + using C = file_time_type::clock; + file_time_type min = file_time_type::min(); + { + file_time_type ret = last_write_time(StaticEnv::File); + TEST_CHECK(ret != min); + TEST_CHECK(ret < C::now()); + TEST_CHECK(CompareTime(ret, LastWriteTime(StaticEnv::File))); + + file_time_type ret2 = last_write_time(StaticEnv::SymlinkToFile); + TEST_CHECK(CompareTime(ret, ret2)); + TEST_CHECK(CompareTime(ret2, LastWriteTime(StaticEnv::SymlinkToFile))); + } + { + file_time_type ret = last_write_time(StaticEnv::Dir); + TEST_CHECK(ret != min); + TEST_CHECK(ret < C::now()); + TEST_CHECK(CompareTime(ret, LastWriteTime(StaticEnv::Dir))); + + file_time_type ret2 = last_write_time(StaticEnv::SymlinkToDir); + TEST_CHECK(CompareTime(ret, ret2)); + TEST_CHECK(CompareTime(ret2, LastWriteTime(StaticEnv::SymlinkToDir))); + } +} + +TEST_CASE(get_last_write_time_dynamic_env_test) +{ + using Clock = file_time_type::clock; + using Sec = std::chrono::seconds; + scoped_test_env env; + + const path file = env.create_file("file", 42); + const path dir = env.create_dir("dir"); + + const auto file_times = GetTimes(file); + const TimeSpec file_write_time = file_times.write; + const auto dir_times = GetTimes(dir); + const TimeSpec dir_write_time = dir_times.write; + + file_time_type ftime = last_write_time(file); + TEST_CHECK(Clock::to_time_t(ftime) == file_write_time.tv_sec); + TEST_CHECK(CompareTime(ftime, file_write_time)); + + file_time_type dtime = last_write_time(dir); + TEST_CHECK(Clock::to_time_t(dtime) == dir_write_time.tv_sec); + TEST_CHECK(CompareTime(dtime, dir_write_time)); + + SleepFor(Sec(2)); + + // update file and add a file to the directory. Make sure the times increase. + std::ofstream of(file, std::ofstream::app); + of << "hello"; + of.close(); + env.create_file("dir/file1", 1); + + file_time_type ftime2 = last_write_time(file); + file_time_type dtime2 = last_write_time(dir); + + TEST_CHECK(ftime2 > ftime); + TEST_CHECK(dtime2 > dtime); + TEST_CHECK(CompareTime(LastWriteTime(file), ftime2)); + TEST_CHECK(CompareTime(LastWriteTime(dir), dtime2)); +} + + +TEST_CASE(set_last_write_time_dynamic_env_test) +{ + using Clock = file_time_type::clock; + scoped_test_env env; + + const path file = env.create_file("file", 42); + const path dir = env.create_dir("dir"); + const auto now = Clock::now(); + const file_time_type epoch_time = now - now.time_since_epoch(); + + const file_time_type future_time = now + Hours(3) + Sec(42) + SubSec(17); + const file_time_type past_time = now - Minutes(3) - Sec(42) - SubSec(17); + const file_time_type before_epoch_time = + epoch_time - Minutes(3) - Sec(42) - SubSec(17); + // FreeBSD has a bug in their utimes implementation where the time is not update + // when the number of seconds is '-1'. +#if defined(__FreeBSD__) || defined(__NetBSD__) + const file_time_type just_before_epoch_time = + epoch_time - Sec(2) - SubSec(17); +#else + const file_time_type just_before_epoch_time = epoch_time - SubSec(17); +#endif + + struct TestCase { + const char * case_name; + path p; + file_time_type new_time; + } cases[] = { + {"file, epoch_time", file, epoch_time}, + {"dir, epoch_time", dir, epoch_time}, + {"file, future_time", file, future_time}, + {"dir, future_time", dir, future_time}, + {"file, past_time", file, past_time}, + {"dir, past_time", dir, past_time}, + {"file, before_epoch_time", file, before_epoch_time}, + {"dir, before_epoch_time", dir, before_epoch_time}, + {"file, just_before_epoch_time", file, just_before_epoch_time}, + {"dir, just_before_epoch_time", dir, just_before_epoch_time} + }; + for (const auto& TC : cases) { + std::cerr << "Test Case = " << TC.case_name << "\n"; + const auto old_times = GetTimes(TC.p); + file_time_type old_time; + TEST_REQUIRE(ConvertFromTimeSpec(old_time, old_times.write)); + + std::error_code ec = GetTestEC(); + last_write_time(TC.p, TC.new_time, ec); + TEST_CHECK(!ec); + + ec = GetTestEC(); + file_time_type got_time = last_write_time(TC.p, ec); + TEST_REQUIRE(!ec); + + if (TimeIsRepresentableByFilesystem(TC.new_time)) { + TEST_CHECK(got_time != old_time); + TEST_CHECK(CompareTime(got_time, TC.new_time)); + TEST_CHECK(CompareTime(LastAccessTime(TC.p), old_times.access)); + } + } +} + +TEST_CASE(last_write_time_symlink_test) +{ + using Clock = file_time_type::clock; + + scoped_test_env env; + + const path file = env.create_file("file", 42); + const path sym = env.create_symlink("file", "sym"); + + const file_time_type new_time = Clock::now() + Hours(3); + + const auto old_times = GetTimes(sym); + const auto old_sym_times = GetSymlinkTimes(sym); + + std::error_code ec = GetTestEC(); + last_write_time(sym, new_time, ec); + TEST_CHECK(!ec); + + file_time_type got_time = last_write_time(sym); + TEST_CHECK(!CompareTime(got_time, old_times.write)); + if (!WorkaroundStatTruncatesToSeconds) { + TEST_CHECK(got_time == new_time); + } else { + TEST_CHECK(CompareTime(got_time, new_time)); + } + + TEST_CHECK(CompareTime(LastWriteTime(file), new_time)); + TEST_CHECK(CompareTime(LastAccessTime(sym), old_times.access)); + Times sym_times = GetSymlinkTimes(sym); + TEST_CHECK(CompareTime(sym_times.write, old_sym_times.write)); +} + + +TEST_CASE(test_write_min_time) +{ + scoped_test_env env; + const path p = env.create_file("file", 42); + const file_time_type old_time = last_write_time(p); + file_time_type new_time = file_time_type::min(); + + std::error_code ec = GetTestEC(); + last_write_time(p, new_time, ec); + file_time_type tt = last_write_time(p); + + if (TimeIsRepresentableByFilesystem(new_time)) { + TEST_CHECK(!ec); + TEST_CHECK(CompareTime(tt, new_time)); + + last_write_time(p, old_time); + new_time = file_time_type::min() + SubSec(1); + + ec = GetTestEC(); + last_write_time(p, new_time, ec); + tt = last_write_time(p); + + if (TimeIsRepresentableByFilesystem(new_time)) { + TEST_CHECK(!ec); + TEST_CHECK(CompareTime(tt, new_time)); + } else { + TEST_CHECK(ErrorIs(ec, std::errc::value_too_large)); + TEST_CHECK(tt == old_time); + } + } else { + TEST_CHECK(ErrorIs(ec, std::errc::value_too_large)); + TEST_CHECK(tt == old_time); + } +} + +TEST_CASE(test_write_max_time) { + scoped_test_env env; + const path p = env.create_file("file", 42); + const file_time_type old_time = last_write_time(p); + file_time_type new_time = file_time_type::max(); + + std::error_code ec = GetTestEC(); + last_write_time(p, new_time, ec); + file_time_type tt = last_write_time(p); + + if (TimeIsRepresentableByFilesystem(new_time)) { + TEST_CHECK(!ec); + TEST_CHECK(CompareTime(tt, new_time)); + } else { + TEST_CHECK(ErrorIs(ec, std::errc::value_too_large)); + TEST_CHECK(tt == old_time); + } +} + +TEST_CASE(test_value_on_failure) +{ + const path p = StaticEnv::DNE; + std::error_code ec = GetTestEC(); + TEST_CHECK(last_write_time(p, ec) == file_time_type::min()); + TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory)); +} + +TEST_CASE(test_exists_fails) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + permissions(dir, perms::none); + + std::error_code ec = GetTestEC(); + TEST_CHECK(last_write_time(file, ec) == file_time_type::min()); + TEST_CHECK(ErrorIs(ec, std::errc::permission_denied)); + + ExceptionChecker Checker(file, std::errc::permission_denied, + "last_write_time"); + TEST_CHECK_THROW_RESULT(filesystem_error, Checker, last_write_time(file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp new file mode 100644 index 00000000000..434fcea1dd9 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp @@ -0,0 +1,181 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void permissions(const path& p, perms prms, +// perm_options opts = perm_options::replace); +// void permissions(const path& p, perms prms, std::error_code& ec) noexcept; +// void permissions(const path& p, perms prms, perm_options opts, std::error_code); + + + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +using PR = fs::perms; + +TEST_SUITE(filesystem_permissions_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + const perms pr{}; ((void)pr); + const perm_options opts{}; ((void)opts); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr)); + ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr, opts)); + ASSERT_NOEXCEPT(fs::permissions(p, pr, ec)); + ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr, opts, ec)); +} + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, fs::perms opts, + const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::permissions(f, opts); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == "" + && err.code() == ec; + } +#else + ((void)f); ((void)opts); ((void)ec); + return true; +#endif + }; + + scoped_test_env env; + const path dne = env.make_env_path("dne"); + const path dne_sym = env.create_symlink(dne, "dne_sym"); + { // !exists + std::error_code ec = GetTestEC(); + fs::permissions(dne, fs::perms{}, ec); + TEST_REQUIRE(ec); + TEST_CHECK(ec != GetTestEC()); + TEST_CHECK(checkThrow(dne, fs::perms{}, ec)); + } + { + std::error_code ec = GetTestEC(); + fs::permissions(dne_sym, fs::perms{}, ec); + TEST_REQUIRE(ec); + TEST_CHECK(ec != GetTestEC()); + TEST_CHECK(checkThrow(dne_sym, fs::perms{}, ec)); + } +} + +TEST_CASE(basic_permissions_test) +{ + scoped_test_env env; + const path file = env.create_file("file1", 42); + const path dir = env.create_dir("dir1"); + const path file_for_sym = env.create_file("file2", 42); + const path sym = env.create_symlink(file_for_sym, "sym"); + const perm_options AP = perm_options::add; + const perm_options RP = perm_options::remove; + const perm_options NF = perm_options::nofollow; + struct TestCase { + path p; + perms set_perms; + perms expected; + perm_options opts; + TestCase(path xp, perms xperms, perms xexpect, + perm_options xopts = perm_options::replace) + : p(xp), set_perms(xperms), expected(xexpect), opts(xopts) {} + } cases[] = { + // test file + {file, perms::none, perms::none}, + {file, perms::owner_all, perms::owner_all}, + {file, perms::group_all, perms::owner_all | perms::group_all, AP}, + {file, perms::group_all, perms::owner_all, RP}, + // test directory + {dir, perms::none, perms::none}, + {dir, perms::owner_all, perms::owner_all}, + {dir, perms::group_all, perms::owner_all | perms::group_all, AP}, + {dir, perms::group_all, perms::owner_all, RP}, + // test symlink without symlink_nofollow + {sym, perms::none, perms::none}, + {sym, perms::owner_all, perms::owner_all}, + {sym, perms::group_all, perms::owner_all | perms::group_all, AP}, + {sym, perms::group_all, perms::owner_all, RP}, + // test non-symlink with symlink_nofollow. The last test on file/dir + // will have set their permissions to perms::owner_all + {file, perms::group_all, perms::owner_all | perms::group_all, AP | NF}, + {dir, perms::group_all, perms::owner_all | perms::group_all, AP | NF} + }; + for (auto const& TC : cases) { + TEST_CHECK(status(TC.p).permissions() != TC.expected); + { + std::error_code ec = GetTestEC(); + permissions(TC.p, TC.set_perms, TC.opts, ec); + TEST_CHECK(!ec); + auto pp = status(TC.p).permissions(); + TEST_CHECK(pp == TC.expected); + } + if (TC.opts == perm_options::replace) { + std::error_code ec = GetTestEC(); + permissions(TC.p, TC.set_perms, ec); + TEST_CHECK(!ec); + auto pp = status(TC.p).permissions(); + TEST_CHECK(pp == TC.expected); + } + } +} + +TEST_CASE(test_no_resolve_symlink_on_symlink) +{ + scoped_test_env env; + const path file = env.create_file("file", 42); + const path sym = env.create_symlink(file, "sym"); + const auto file_perms = status(file).permissions(); + + struct TestCase { + perms set_perms; + perms expected; // only expected on platform that support symlink perms. + perm_options opts = perm_options::replace; + TestCase(perms xperms, perms xexpect, + perm_options xopts = perm_options::replace) + : set_perms(xperms), expected(xexpect), opts(xopts) {} + } cases[] = { + {perms::owner_all, perms::owner_all}, + {perms::group_all, perms::owner_all | perms::group_all, perm_options::add}, + {perms::owner_all, perms::group_all, perm_options::remove}, + }; + for (auto const& TC : cases) { +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) + // On OS X symlink permissions are supported. We should get an empty + // error code and the expected permissions. + const auto expected_link_perms = TC.expected; + std::error_code expected_ec; +#else + // On linux symlink permissions are not supported. The error code should + // be 'operation_not_supported' and the symlink permissions should be + // unchanged. + const auto expected_link_perms = symlink_status(sym).permissions(); + std::error_code expected_ec = std::make_error_code(std::errc::operation_not_supported); +#endif + std::error_code ec = GetTestEC(); + permissions(sym, TC.set_perms, TC.opts | perm_options::nofollow, ec); + TEST_CHECK(ec == expected_ec); + TEST_CHECK(status(file).permissions() == file_perms); + TEST_CHECK(symlink_status(sym).permissions() == expected_link_perms); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.proximate/proximate.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.proximate/proximate.pass.cpp new file mode 100644 index 00000000000..a7c498434df --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.proximate/proximate.pass.cpp @@ -0,0 +1,132 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path proximate(const path& p, error_code &ec) +// path proximate(const path& p, const path& base = current_path()) +// path proximate(const path& p, const path& base, error_code& ec); + +#include "filesystem_include.hpp" +#include <type_traits> +#include <vector> +#include <iostream> +#include <cassert> + +#include "test_macros.h" +#include "test_iterators.h" +#include "count_new.hpp" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + + +static int count_path_elems(const fs::path& p) { + int count = 0; + for (auto& elem : p) { + if (elem != "/" && elem != "") + ++count; + } + return count; +} + +TEST_SUITE(filesystem_proximate_path_test_suite) + + +TEST_CASE(signature_test) +{ + using fs::path; + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(proximate(p)); + ASSERT_NOT_NOEXCEPT(proximate(p, p)); + ASSERT_NOT_NOEXCEPT(proximate(p, ec)); + ASSERT_NOT_NOEXCEPT(proximate(p, p, ec)); +} + +TEST_CASE(basic_test) { + using fs::path; + const path cwd = fs::current_path(); + const path parent_cwd = cwd.parent_path(); + const path curdir = cwd.filename(); + TEST_REQUIRE(!cwd.native().empty()); + int cwd_depth = count_path_elems(cwd); + path dot_dot_to_root; + for (int i=0; i < cwd_depth; ++i) + dot_dot_to_root /= ".."; + path relative_cwd = cwd.native().substr(1); + // clang-format off + struct { + std::string input; + std::string base; + std::string expect; + } TestCases[] = { + {"", "", "."}, + {cwd, "a", ".."}, + {parent_cwd, "a", "../.."}, + {"a", cwd, "a"}, + {"a", parent_cwd, "fs.op.proximate/a"}, + {"/", "a", dot_dot_to_root / ".."}, + {"/", "a/b", dot_dot_to_root / "../.."}, + {"/", "a/b/", dot_dot_to_root / "../.."}, + {"a", "/", relative_cwd / "a"}, + {"a/b", "/", relative_cwd / "a/b"}, + {"a", "/net", ".." / relative_cwd / "a"}, + {"//foo/", "//foo", "."}, + {"//foo", "//foo/", "."}, + {"//foo", "//foo", "."}, + {"//foo/", "//foo/", "."}, + {"//base", "a", dot_dot_to_root / "../base"}, + {"a", "a", "."}, + {"a/b", "a/b", "."}, + {"a/b/c/", "a/b/c/", "."}, + {"//foo/a/b", "//foo/a/b", "."}, + {"/a/d", "/a/b/c", "../../d"}, + {"/a/b/c", "/a/d", "../b/c"}, + {"a/b/c", "a", "b/c"}, + {"a/b/c", "a/b/c/x/y", "../.."}, + {"a/b/c", "a/b/c", "."}, + {"a/b", "c/d", "../../a/b"} + }; + // clang-format on + int ID = 0; + for (auto& TC : TestCases) { + ++ID; + std::error_code ec = GetTestEC(); + fs::path p(TC.input); + const fs::path output = fs::proximate(p, TC.base, ec); + if (ec) { + TEST_CHECK(!ec); + std::cerr << "TEST CASE #" << ID << " FAILED: \n"; + std::cerr << " Input: '" << TC.input << "'\n"; + std::cerr << " Base: '" << TC.base << "'\n"; + std::cerr << " Expected: '" << TC.expect << "'\n"; + + std::cerr << std::endl; + } else if (!PathEq(output, TC.expect)) { + TEST_CHECK(PathEq(output, TC.expect)); + + const path canon_input = fs::weakly_canonical(TC.input); + const path canon_base = fs::weakly_canonical(TC.base); + const path lexically_p = canon_input.lexically_proximate(canon_base); + std::cerr << "TEST CASE #" << ID << " FAILED: \n"; + std::cerr << " Input: '" << TC.input << "'\n"; + std::cerr << " Base: '" << TC.base << "'\n"; + std::cerr << " Expected: '" << TC.expect << "'\n"; + std::cerr << " Output: '" << output.native() << "'\n"; + std::cerr << " Lex Prox: '" << lexically_p.native() << "'\n"; + std::cerr << " Canon Input: " << canon_input << "\n"; + std::cerr << " Canon Base: " << canon_base << "\n"; + + std::cerr << std::endl; + } + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.read_symlink/read_symlink.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.read_symlink/read_symlink.pass.cpp new file mode 100644 index 00000000000..488738eb526 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.read_symlink/read_symlink.pass.cpp @@ -0,0 +1,99 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path read_symlink(const path& p); +// path read_symlink(const path& p, error_code& ec); + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_read_symlink_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::read_symlink(p)), fs::path); + ASSERT_SAME_TYPE(decltype(fs::read_symlink(p, ec)), fs::path); + + ASSERT_NOT_NOEXCEPT(fs::read_symlink(p)); + // Not noexcept because of narrow contract + ASSERT_NOT_NOEXCEPT(fs::read_symlink(p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::read_symlink(f); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == "" + && err.code() == ec; + } +#else + ((void)f); ((void)ec); + return true; +#endif + }; + + scoped_test_env env; + const path cases[] = { + env.make_env_path("dne"), + env.create_file("file", 42), + env.create_dir("dir") + }; + for (path const& p : cases) { + std::error_code ec; + const path ret = fs::read_symlink(p, ec); + TEST_REQUIRE(ec); + TEST_CHECK(ret == path{}); + TEST_CHECK(checkThrow(p, ec)); + } + +} + +TEST_CASE(basic_symlink_test) +{ + scoped_test_env env; + const path dne = env.make_env_path("dne"); + const path file = env.create_file("file", 42); + const path dir = env.create_dir("dir"); + const path link = env.create_symlink(dne, "link"); + const path nested_link = env.make_env_path("nested_link"); + create_symlink(link, nested_link); + struct TestCase { + path symlink; + path expected; + } testCases[] = { + {env.create_symlink(dne, "dne_link"), dne}, + {env.create_symlink(file, "file_link"), file}, + {env.create_symlink(dir, "dir_link"), dir}, + {nested_link, link} + }; + for (auto& TC : testCases) { + std::error_code ec = std::make_error_code(std::errc::address_in_use); + const path ret = read_symlink(TC.symlink, ec); + TEST_CHECK(!ec); + TEST_CHECK(ret == TC.expected); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.relative/relative.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.relative/relative.pass.cpp new file mode 100644 index 00000000000..fd9bac796df --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.relative/relative.pass.cpp @@ -0,0 +1,117 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path proximate(const path& p, error_code &ec) +// path proximate(const path& p, const path& base = current_path()) +// path proximate(const path& p, const path& base, error_code& ec); + +#include "filesystem_include.hpp" +#include <string> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "test_iterators.h" +#include "count_new.hpp" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + + +TEST_SUITE(filesystem_proximate_path_test_suite) + +TEST_CASE(test_signature_0) { + fs::path p(""); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(fs::current_path())); +} + +TEST_CASE(test_signature_1) { + fs::path p("."); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(fs::current_path())); +} + +TEST_CASE(test_signature_2) { + fs::path p(StaticEnv::File); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::File)); +} + +TEST_CASE(test_signature_3) { + fs::path p(StaticEnv::Dir); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir)); +} + +TEST_CASE(test_signature_4) { + fs::path p(StaticEnv::SymlinkToDir); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir)); +} + +TEST_CASE(test_signature_5) { + fs::path p(StaticEnv::SymlinkToDir / "dir2/."); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir / "dir2")); +} + +TEST_CASE(test_signature_6) { + // FIXME? If the trailing separator occurs in a part of the path that exists, + // it is ommitted. Otherwise it is added to the end of the result. + fs::path p(StaticEnv::SymlinkToDir / "dir2/./"); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir / "dir2")); +} + +TEST_CASE(test_signature_7) { + fs::path p(StaticEnv::SymlinkToDir / "dir2/DNE/./"); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir / "dir2/DNE/")); +} + +TEST_CASE(test_signature_8) { + fs::path p(StaticEnv::SymlinkToDir / "dir2"); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir2)); +} + +TEST_CASE(test_signature_9) { + fs::path p(StaticEnv::SymlinkToDir / "dir2/../dir2/DNE/.."); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir2 / "")); +} + +TEST_CASE(test_signature_10) { + fs::path p(StaticEnv::SymlinkToDir / "dir2/dir3/../DNE/DNE2"); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir2 / "DNE/DNE2")); +} + +TEST_CASE(test_signature_11) { + fs::path p(StaticEnv::Dir / "../dir1"); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir)); +} + +TEST_CASE(test_signature_12) { + fs::path p(StaticEnv::Dir / "./."); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir)); +} + +TEST_CASE(test_signature_13) { + fs::path p(StaticEnv::Dir / "DNE/../foo"); + const fs::path output = fs::weakly_canonical(p); + TEST_CHECK(output == std::string(StaticEnv::Dir / "foo")); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove/remove.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove/remove.pass.cpp new file mode 100644 index 00000000000..3fb20524a25 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove/remove.pass.cpp @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool remove(const path& p); +// bool remove(const path& p, error_code& ec) noexcept; + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_remove_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::remove(p)), bool); + ASSERT_SAME_TYPE(decltype(fs::remove(p, ec)), bool); + + ASSERT_NOT_NOEXCEPT(fs::remove(p)); + ASSERT_NOEXCEPT(fs::remove(p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::remove(f); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == "" + && err.code() == ec; + } +#else + ((void)f); ((void)ec); + return true; +#endif + }; + scoped_test_env env; + const path non_empty_dir = env.create_dir("dir"); + env.create_file(non_empty_dir / "file1", 42); + const path bad_perms_dir = env.create_dir("bad_dir"); + const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42); + permissions(bad_perms_dir, perms::none); + const path testCases[] = { + non_empty_dir, + file_in_bad_dir, + }; + for (auto& p : testCases) { + std::error_code ec; + + TEST_CHECK(!fs::remove(p, ec)); + TEST_CHECK(ec); + TEST_CHECK(checkThrow(p, ec)); + } + + // PR#35780 + const path testCasesNonexistant[] = { + "", + env.make_env_path("dne") + }; + + for (auto& p : testCasesNonexistant) { + std::error_code ec; + + TEST_CHECK(!fs::remove(p, ec)); + TEST_CHECK(!ec); + } +} + +TEST_CASE(basic_remove_test) +{ + scoped_test_env env; + const path dne = env.make_env_path("dne"); + const path link = env.create_symlink(dne, "link"); + const path nested_link = env.make_env_path("nested_link"); + create_symlink(link, nested_link); + const path testCases[] = { + env.create_file("file", 42), + env.create_dir("empty_dir"), + nested_link, + link + }; + for (auto& p : testCases) { + std::error_code ec = std::make_error_code(std::errc::address_in_use); + TEST_CHECK(remove(p, ec)); + TEST_CHECK(!ec); + TEST_CHECK(!exists(symlink_status(p))); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove_all/remove_all.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove_all/remove_all.pass.cpp new file mode 100644 index 00000000000..523ce5f064f --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove_all/remove_all.pass.cpp @@ -0,0 +1,152 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// uintmax_t remove_all(const path& p); +// uintmax_t remove_all(const path& p, error_code& ec) noexcept; + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_remove_all_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::remove_all(p)), std::uintmax_t); + ASSERT_SAME_TYPE(decltype(fs::remove_all(p, ec)), std::uintmax_t); + + ASSERT_NOT_NOEXCEPT(fs::remove_all(p)); + ASSERT_NOT_NOEXCEPT(fs::remove_all(p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::remove_all(f); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == "" + && err.code() == ec; + } +#else + ((void)f); ((void)ec); + return true; +#endif + }; + scoped_test_env env; + const path non_empty_dir = env.create_dir("dir"); + env.create_file(non_empty_dir / "file1", 42); + const path bad_perms_dir = env.create_dir("bad_dir"); + const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42); + permissions(bad_perms_dir, perms::none); + const path bad_perms_file = env.create_file("file2", 42); + permissions(bad_perms_file, perms::none); + + const path testCases[] = { + file_in_bad_dir + }; + const auto BadRet = static_cast<std::uintmax_t>(-1); + for (auto& p : testCases) { + std::error_code ec; + + TEST_CHECK(fs::remove_all(p, ec) == BadRet); + TEST_CHECK(ec); + TEST_CHECK(checkThrow(p, ec)); + } + + // PR#35780 + const path testCasesNonexistant[] = { + "", + env.make_env_path("dne") + }; + for (auto &p : testCasesNonexistant) { + std::error_code ec; + + TEST_CHECK(fs::remove_all(p, ec) == 0); + TEST_CHECK(!ec); + } +} + +TEST_CASE(basic_remove_all_test) +{ + scoped_test_env env; + const path dne = env.make_env_path("dne"); + const path link = env.create_symlink(dne, "link"); + const path nested_link = env.make_env_path("nested_link"); + create_symlink(link, nested_link); + const path testCases[] = { + env.create_file("file", 42), + env.create_dir("empty_dir"), + nested_link, + link + }; + for (auto& p : testCases) { + std::error_code ec = std::make_error_code(std::errc::address_in_use); + TEST_CHECK(remove(p, ec)); + TEST_CHECK(!ec); + TEST_CHECK(!exists(symlink_status(p))); + } +} + +TEST_CASE(symlink_to_dir) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file(dir / "file", 42); + const path link = env.create_symlink(dir, "sym"); + + { + std::error_code ec = std::make_error_code(std::errc::address_in_use); + TEST_CHECK(remove_all(link, ec) == 1); + TEST_CHECK(!ec); + TEST_CHECK(!exists(symlink_status(link))); + TEST_CHECK(exists(dir)); + TEST_CHECK(exists(file)); + } +} + + +TEST_CASE(nested_dir) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path dir1 = env.create_dir(dir / "dir1"); + const path out_of_dir_file = env.create_file("file1", 42); + const path all_files[] = { + dir, dir1, + env.create_file(dir / "file1", 42), + env.create_symlink(out_of_dir_file, dir / "sym1"), + env.create_file(dir1 / "file2", 42), + env.create_symlink(dir, dir1 / "sym2") + }; + const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]); + + std::error_code ec = std::make_error_code(std::errc::address_in_use); + TEST_CHECK(remove_all(dir, ec) == expected_count); + TEST_CHECK(!ec); + for (auto const& p : all_files) { + TEST_CHECK(!exists(symlink_status(p))); + } + TEST_CHECK(exists(out_of_dir_file)); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.rename/rename.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.rename/rename.pass.cpp new file mode 100644 index 00000000000..8373d67b890 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.rename/rename.pass.cpp @@ -0,0 +1,124 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void rename(const path& old_p, const path& new_p); +// void rename(const path& old_p, const path& new_p, error_code& ec) noexcept; + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_rename_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(fs::rename(p, p)), void); + ASSERT_SAME_TYPE(decltype(fs::rename(p, p, ec)), void); + + ASSERT_NOT_NOEXCEPT(fs::rename(p, p)); + ASSERT_NOEXCEPT(fs::rename(p, p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, path const& t, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::rename(f, t); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == t + && err.code() == ec; + } +#else + ((void)f); ((void)t); ((void)ec); + return true; +#endif + }; + scoped_test_env env; + const path dne = env.make_env_path("dne"); + const path file = env.create_file("file1", 42); + const path dir = env.create_dir("dir1"); + struct TestCase { + path from; + path to; + } cases[] = { + {dne, dne}, + {file, dir}, + {dir, file} + }; + for (auto& TC : cases) { + auto from_before = status(TC.from); + auto to_before = status(TC.to); + std::error_code ec; + rename(TC.from, TC.to, ec); + TEST_REQUIRE(ec); + TEST_CHECK(from_before.type() == status(TC.from).type()); + TEST_CHECK(to_before.type() == status(TC.to).type()); + TEST_CHECK(checkThrow(TC.from, TC.to, ec)); + } +} + +TEST_CASE(basic_rename_test) +{ + scoped_test_env env; + + const std::error_code set_ec = std::make_error_code(std::errc::address_in_use); + const path file = env.create_file("file1", 42); + { // same file + std::error_code ec = set_ec; + rename(file, file, ec); + TEST_CHECK(!ec); + TEST_CHECK(is_regular_file(file)); + TEST_CHECK(file_size(file) == 42); + } + const path sym = env.create_symlink(file, "sym"); + { // file -> symlink + std::error_code ec = set_ec; + rename(file, sym, ec); + TEST_CHECK(!ec); + TEST_CHECK(!exists(file)); + TEST_CHECK(is_regular_file(symlink_status(sym))); + TEST_CHECK(file_size(sym) == 42); + } + const path file2 = env.create_file("file2", 42); + const path file3 = env.create_file("file3", 100); + { // file -> file + std::error_code ec = set_ec; + rename(file2, file3, ec); + TEST_CHECK(!ec); + TEST_CHECK(!exists(file2)); + TEST_CHECK(is_regular_file(file3)); + TEST_CHECK(file_size(file3) == 42); + } + const path dne = env.make_env_path("dne"); + const path bad_sym = env.create_symlink(dne, "bad_sym"); + const path bad_sym_dest = env.make_env_path("bad_sym2"); + { // bad-symlink + std::error_code ec = set_ec; + rename(bad_sym, bad_sym_dest, ec); + TEST_CHECK(!ec); + TEST_CHECK(!exists(symlink_status(bad_sym))); + TEST_CHECK(is_symlink(bad_sym_dest)); + TEST_CHECK(read_symlink(bad_sym_dest) == dne); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.resize_file/resize_file.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.resize_file/resize_file.pass.cpp new file mode 100644 index 00000000000..43ce5b060df --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.resize_file/resize_file.pass.cpp @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// void resize_file(const path& p, uintmax_t new_size); +// void resize_file(const path& p, uintmax_t new_size, error_code& ec) noexcept; + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_resize_file_test_suite) + +TEST_CASE(test_signatures) +{ + const path p; ((void)p); + std::uintmax_t i; ((void)i); + std::error_code ec; ((void)ec); + + ASSERT_SAME_TYPE(decltype(fs::resize_file(p, i)), void); + ASSERT_SAME_TYPE(decltype(fs::resize_file(p, i, ec)), void); + + ASSERT_NOT_NOEXCEPT(fs::resize_file(p, i)); + ASSERT_NOEXCEPT(fs::resize_file(p, i, ec)); +} + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, std::uintmax_t s, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + fs::resize_file(f, s); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == "" + && err.code() == ec; + } +#else + ((void)f); ((void)s); ((void)ec); + return true; +#endif + }; + scoped_test_env env; + const path dne = env.make_env_path("dne"); + const path bad_sym = env.create_symlink(dne, "sym"); + const path dir = env.create_dir("dir1"); + const path cases[] = { + dne, bad_sym, dir + }; + for (auto& p : cases) { + std::error_code ec; + resize_file(p, 42, ec); + TEST_REQUIRE(ec); + TEST_CHECK(checkThrow(p, 42, ec)); + } +} + +TEST_CASE(basic_resize_file_test) +{ + scoped_test_env env; + const path file1 = env.create_file("file1", 42); + const auto set_ec = std::make_error_code(std::errc::address_in_use); + { // grow file + const std::uintmax_t new_s = 100; + std::error_code ec = set_ec; + resize_file(file1, new_s, ec); + TEST_CHECK(!ec); + TEST_CHECK(file_size(file1) == new_s); + } + { // shrink file + const std::uintmax_t new_s = 1; + std::error_code ec = set_ec; + resize_file(file1, new_s, ec); + TEST_CHECK(!ec); + TEST_CHECK(file_size(file1) == new_s); + } + { // shrink file to zero + const std::uintmax_t new_s = 0; + std::error_code ec = set_ec; + resize_file(file1, new_s, ec); + TEST_CHECK(!ec); + TEST_CHECK(file_size(file1) == new_s); + } + const path sym = env.create_symlink(file1, "sym"); + { // grow file via symlink + const std::uintmax_t new_s = 1024; + std::error_code ec = set_ec; + resize_file(sym, new_s, ec); + TEST_CHECK(!ec); + TEST_CHECK(file_size(file1) == new_s); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp new file mode 100644 index 00000000000..7bb6c55fae3 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp @@ -0,0 +1,127 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// space_info space(const path& p); +// space_info space(const path& p, error_code& ec) noexcept; + +#include "filesystem_include.hpp" +#include <sys/statvfs.h> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +bool EqualDelta(std::uintmax_t x, std::uintmax_t y, std::uintmax_t delta) { + if (x >= y) { + return (x - y) <= delta; + } else { + return (y - x) <= delta; + } +} + +TEST_SUITE(filesystem_space_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_SAME_TYPE(decltype(space(p)), space_info); + ASSERT_SAME_TYPE(decltype(space(p, ec)), space_info); + ASSERT_NOT_NOEXCEPT(space(p)); + ASSERT_NOEXCEPT(space(p, ec)); +} + +TEST_CASE(test_error_reporting) +{ + auto checkThrow = [](path const& f, const std::error_code& ec) + { +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + space(f); + return false; + } catch (filesystem_error const& err) { + return err.path1() == f + && err.path2() == "" + && err.code() == ec; + } +#else + ((void)f); ((void)ec); + return true; +#endif + }; + const path cases[] = { + "", + StaticEnv::DNE, + StaticEnv::BadSymlink + }; + for (auto& p : cases) { + const auto expect = static_cast<std::uintmax_t>(-1); + std::error_code ec; + space_info info = space(p, ec); + TEST_CHECK(ec); + TEST_CHECK(info.capacity == expect); + TEST_CHECK(info.free == expect); + TEST_CHECK(info.available == expect); + TEST_CHECK(checkThrow(p, ec)); + } +} + +TEST_CASE(basic_space_test) +{ + // All the test cases should reside on the same filesystem and therefore + // should have the same expected result. Compute this expected result + // one and check that it looks semi-sane. + struct statvfs expect; + TEST_REQUIRE(::statvfs(StaticEnv::Dir.c_str(), &expect) != -1); + TEST_CHECK(expect.f_bavail > 0); + TEST_CHECK(expect.f_bfree > 0); + TEST_CHECK(expect.f_bsize > 0); + TEST_CHECK(expect.f_blocks > 0); + TEST_REQUIRE(expect.f_frsize > 0); + auto do_mult = [&](std::uintmax_t val) { + std::uintmax_t fsize = expect.f_frsize; + std::uintmax_t new_val = val * fsize; + TEST_CHECK(new_val / fsize == val); // Test for overflow + return new_val; + }; + const std::uintmax_t bad_value = static_cast<std::uintmax_t>(-1); + const std::uintmax_t expect_capacity = do_mult(expect.f_blocks); + const std::uintmax_t expect_free = do_mult(expect.f_bfree); + const std::uintmax_t expect_avail = do_mult(expect.f_bavail); + + // Other processes running on the operating system may have changed + // the amount of space available. Check that these are within tolerances. + // Currently 5% of capacity + const std::uintmax_t delta = expect_capacity / 20; + const path cases[] = { + StaticEnv::File, + StaticEnv::Dir, + StaticEnv::Dir2, + StaticEnv::SymlinkToFile, + StaticEnv::SymlinkToDir + }; + for (auto& p : cases) { + std::error_code ec = GetTestEC(); + space_info info = space(p, ec); + TEST_CHECK(!ec); + TEST_CHECK(info.capacity != bad_value); + TEST_CHECK(expect_capacity == info.capacity); + TEST_CHECK(info.free != bad_value); + TEST_CHECK(EqualDelta(expect_free, info.free, delta)); + TEST_CHECK(info.available != bad_value); + TEST_CHECK(EqualDelta(expect_avail, info.available, delta)); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp new file mode 100644 index 00000000000..93444bb0007 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp @@ -0,0 +1,165 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// file_status status(const path& p); +// file_status status(const path& p, error_code& ec) noexcept; + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_status_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(status(p)); + ASSERT_NOEXCEPT(status(p, ec)); +} + +TEST_CASE(test_status_not_found) +{ + const std::error_code expect_ec = + std::make_error_code(std::errc::no_such_file_or_directory); + const path cases[] { + StaticEnv::DNE, + StaticEnv::BadSymlink + }; + for (auto& p : cases) { + std::error_code ec = std::make_error_code(std::errc::address_in_use); + // test non-throwing overload. + file_status st = status(p, ec); + TEST_CHECK(ec == expect_ec); + TEST_CHECK(st.type() == file_type::not_found); + TEST_CHECK(st.permissions() == perms::unknown); + // test throwing overload. It should not throw even though it reports + // that the file was not found. + TEST_CHECK_NO_THROW(st = status(p)); + TEST_CHECK(st.type() == file_type::not_found); + TEST_CHECK(st.permissions() == perms::unknown); + } +} + +TEST_CASE(test_status_cannot_resolve) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file = env.create_file("dir/file", 42); + const path sym = env.create_symlink("dir/file", "sym"); + permissions(dir, perms::none); + + const std::error_code set_ec = + std::make_error_code(std::errc::address_in_use); + const std::error_code perm_ec = + std::make_error_code(std::errc::permission_denied); + const std::error_code name_too_long_ec = + std::make_error_code(std::errc::filename_too_long); + + struct TestCase { + path p; + std::error_code expect_ec; + } const TestCases[] = { + {file, perm_ec}, + {sym, perm_ec}, + {path(std::string(2500, 'a')), name_too_long_ec} + }; + for (auto& TC : TestCases) + { + { // test non-throwing case + std::error_code ec = set_ec; + file_status st = status(TC.p, ec); + TEST_CHECK(ec == TC.expect_ec); + TEST_CHECK(st.type() == file_type::none); + TEST_CHECK(st.permissions() == perms::unknown); + } +#ifndef TEST_HAS_NO_EXCEPTIONS + { // test throwing case + try { + status(TC.p); + } catch (filesystem_error const& err) { + TEST_CHECK(err.path1() == TC.p); + TEST_CHECK(err.path2() == ""); + TEST_CHECK(err.code() == TC.expect_ec); + } + } +#endif + } +} + +TEST_CASE(status_file_types_test) +{ + scoped_test_env env; + struct TestCase { + path p; + file_type expect_type; + } cases[] = { + {StaticEnv::File, file_type::regular}, + {StaticEnv::SymlinkToFile, file_type::regular}, + {StaticEnv::Dir, file_type::directory}, + {StaticEnv::SymlinkToDir, file_type::directory}, + // Block files tested elsewhere + {StaticEnv::CharFile, file_type::character}, +#if !defined(__APPLE__) && !defined(__FreeBSD__) // No support for domain sockets + {env.create_socket("socket"), file_type::socket}, +#endif + {env.create_fifo("fifo"), file_type::fifo} + }; + for (const auto& TC : cases) { + // test non-throwing case + std::error_code ec = std::make_error_code(std::errc::address_in_use); + file_status st = status(TC.p, ec); + TEST_CHECK(!ec); + TEST_CHECK(st.type() == TC.expect_type); + TEST_CHECK(st.permissions() != perms::unknown); + // test throwing case + TEST_REQUIRE_NO_THROW(st = status(TC.p)); + TEST_CHECK(st.type() == TC.expect_type); + TEST_CHECK(st.permissions() != perms::unknown); + } +} + +TEST_CASE(test_block_file) +{ + const path possible_paths[] = { + "/dev/drive0", // Apple + "/dev/sda", + "/dev/loop0" + }; + path p; + for (const path& possible_p : possible_paths) { + std::error_code ec; + if (exists(possible_p, ec)) { + p = possible_p; + break; + } + } + if (p == path{}) { + TEST_UNSUPPORTED(); + } + // test non-throwing case + std::error_code ec = std::make_error_code(std::errc::address_in_use); + file_status st = status(p, ec); + TEST_CHECK(!ec); + TEST_CHECK(st.type() == file_type::block); + TEST_CHECK(st.permissions() != perms::unknown); + // test throwing case + TEST_REQUIRE_NO_THROW(st = status(p)); + TEST_CHECK(st.type() == file_type::block); + TEST_CHECK(st.permissions() != perms::unknown); +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status_known/status_known.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status_known/status_known.pass.cpp new file mode 100644 index 00000000000..93a251c8452 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status_known/status_known.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// bool status_known(file_status s) noexcept; + +#include "filesystem_include.hpp" +#include <type_traits> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(status_known_test_suite) + +TEST_CASE(signature_test) +{ + file_status s; ((void)s); + ASSERT_SAME_TYPE(decltype(status_known(s)), bool); + ASSERT_NOEXCEPT(status_known(s)); +} + +TEST_CASE(status_known_test) +{ + struct TestCase { + file_type type; + bool expect; + }; + const TestCase testCases[] = { + {file_type::none, false}, + {file_type::not_found, true}, + {file_type::regular, true}, + {file_type::directory, true}, + {file_type::symlink, true}, + {file_type::block, true}, + {file_type::character, true}, + {file_type::fifo, true}, + {file_type::socket, true}, + {file_type::unknown, true} + }; + for (auto& TC : testCases) { + file_status s(TC.type); + TEST_CHECK(status_known(s) == TC.expect); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp new file mode 100644 index 00000000000..a4d883dbaa2 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp @@ -0,0 +1,191 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// file_status symlink_status(const path& p); +// file_status symlink_status(const path& p, error_code& ec) noexcept; + +#include "filesystem_include.hpp" + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +TEST_SUITE(filesystem_symlink_status_test_suite) + +TEST_CASE(signature_test) +{ + const path p; ((void)p); + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(symlink_status(p)); + ASSERT_NOEXCEPT(symlink_status(p, ec)); +} + +TEST_CASE(test_symlink_status_not_found) +{ + const std::error_code expect_ec = + std::make_error_code(std::errc::no_such_file_or_directory); + const path cases[] { + StaticEnv::DNE + }; + for (auto& p : cases) { + std::error_code ec = std::make_error_code(std::errc::address_in_use); + // test non-throwing overload. + file_status st = symlink_status(p, ec); + TEST_CHECK(ec == expect_ec); + TEST_CHECK(st.type() == file_type::not_found); + TEST_CHECK(st.permissions() == perms::unknown); + // test throwing overload. It should not throw even though it reports + // that the file was not found. + TEST_CHECK_NO_THROW(st = status(p)); + TEST_CHECK(st.type() == file_type::not_found); + TEST_CHECK(st.permissions() == perms::unknown); + } +} + +TEST_CASE(test_symlink_status_cannot_resolve) +{ + scoped_test_env env; + const path dir = env.create_dir("dir"); + const path file_in_dir = env.create_file("dir/file", 42); + const path sym_in_dir = env.create_symlink("dir/file", "dir/bad_sym"); + const path sym_points_in_dir = env.create_symlink("dir/file", "sym"); + permissions(dir, perms::none); + + const std::error_code set_ec = + std::make_error_code(std::errc::address_in_use); + const std::error_code expect_ec = + std::make_error_code(std::errc::permission_denied); + + const path fail_cases[] = { + file_in_dir, sym_in_dir + }; + for (auto& p : fail_cases) + { + { // test non-throwing case + std::error_code ec = set_ec; + file_status st = symlink_status(p, ec); + TEST_CHECK(ec == expect_ec); + TEST_CHECK(st.type() == file_type::none); + TEST_CHECK(st.permissions() == perms::unknown); + } +#ifndef TEST_HAS_NO_EXCEPTIONS + { // test throwing case + try { + symlink_status(p); + } catch (filesystem_error const& err) { + TEST_CHECK(err.path1() == p); + TEST_CHECK(err.path2() == ""); + TEST_CHECK(err.code() == expect_ec); + } + } +#endif + } + // Test that a symlink that points into a directory without read perms + // can be stat-ed using symlink_status + { + std::error_code ec = set_ec; + file_status st = symlink_status(sym_points_in_dir, ec); + TEST_CHECK(!ec); + TEST_CHECK(st.type() == file_type::symlink); + TEST_CHECK(st.permissions() != perms::unknown); + // test non-throwing version + TEST_REQUIRE_NO_THROW(st = symlink_status(sym_points_in_dir)); + TEST_CHECK(st.type() == file_type::symlink); + TEST_CHECK(st.permissions() != perms::unknown); + } +} + + +TEST_CASE(symlink_status_file_types_test) +{ + scoped_test_env env; + struct TestCase { + path p; + file_type expect_type; + } cases[] = { + {StaticEnv::BadSymlink, file_type::symlink}, + {StaticEnv::File, file_type::regular}, + {StaticEnv::SymlinkToFile, file_type::symlink}, + {StaticEnv::Dir, file_type::directory}, + {StaticEnv::SymlinkToDir, file_type::symlink}, + // Block files tested elsewhere + {StaticEnv::CharFile, file_type::character}, +#if !defined(__APPLE__) && !defined(__FreeBSD__) // No support for domain sockets + {env.create_socket("socket"), file_type::socket}, +#endif + {env.create_fifo("fifo"), file_type::fifo} + }; + for (const auto& TC : cases) { + // test non-throwing case + std::error_code ec = std::make_error_code(std::errc::address_in_use); + file_status st = symlink_status(TC.p, ec); + TEST_CHECK(!ec); + TEST_CHECK(st.type() == TC.expect_type); + TEST_CHECK(st.permissions() != perms::unknown); + // test throwing case + TEST_REQUIRE_NO_THROW(st = symlink_status(TC.p)); + TEST_CHECK(st.type() == TC.expect_type); + TEST_CHECK(st.permissions() != perms::unknown); + } +} + +TEST_CASE(test_block_file) +{ + const path possible_paths[] = { + "/dev/drive0", // Apple + "/dev/sda", // Linux + "/dev/loop0" // Linux + // No FreeBSD files known + }; + path p; + for (const path& possible_p : possible_paths) { + std::error_code ec; + if (exists(possible_p, ec)) { + p = possible_p; + break; + } + } + if (p == path{}) { + TEST_UNSUPPORTED(); + } + scoped_test_env env; + { // test block file + // test non-throwing case + std::error_code ec = std::make_error_code(std::errc::address_in_use); + file_status st = symlink_status(p, ec); + TEST_CHECK(!ec); + TEST_CHECK(st.type() == file_type::block); + TEST_CHECK(st.permissions() != perms::unknown); + // test throwing case + TEST_REQUIRE_NO_THROW(st = symlink_status(p)); + TEST_CHECK(st.type() == file_type::block); + TEST_CHECK(st.permissions() != perms::unknown); + } + const path sym = env.make_env_path("sym"); + create_symlink(p, sym); + { // test symlink to block file + // test non-throwing case + std::error_code ec = std::make_error_code(std::errc::address_in_use); + file_status st = symlink_status(sym, ec); + TEST_CHECK(!ec); + TEST_CHECK(st.type() == file_type::symlink); + TEST_CHECK(st.permissions() != perms::unknown); + // test throwing case + TEST_REQUIRE_NO_THROW(st = symlink_status(sym)); + TEST_CHECK(st.type() == file_type::symlink); + TEST_CHECK(st.permissions() != perms::unknown); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp new file mode 100644 index 00000000000..fbce591683c --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp @@ -0,0 +1,120 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path temp_directory_path(); +// path temp_directory_path(error_code& ec); + +#include "filesystem_include.hpp" +#include <memory> +#include <cstdlib> +#include <cstring> +#include <cassert> + +#include "test_macros.h" +#include "rapid-cxx-test.hpp" +#include "filesystem_test_helper.hpp" + +using namespace fs; + +void PutEnv(std::string var, std::string value) { + assert(::setenv(var.c_str(), value.c_str(), /* overwrite */ 1) == 0); +} + +void UnsetEnv(std::string var) { + assert(::unsetenv(var.c_str()) == 0); +} + +TEST_SUITE(filesystem_temp_directory_path_test_suite) + +TEST_CASE(signature_test) +{ + std::error_code ec; ((void)ec); + ASSERT_NOT_NOEXCEPT(temp_directory_path()); + ASSERT_NOT_NOEXCEPT(temp_directory_path(ec)); +} + +TEST_CASE(basic_tests) +{ + scoped_test_env env; + const path dne = env.make_env_path("dne"); + const path file = env.create_file("file", 42); + const path dir_perms = env.create_dir("bad_perms_dir"); + const path nested_dir = env.create_dir("bad_perms_dir/nested"); + permissions(dir_perms, perms::none); + const std::error_code expect_ec = std::make_error_code(std::errc::not_a_directory); + struct TestCase { + std::string name; + path p; + } cases[] = { + {"TMPDIR", env.create_dir("dir1")}, + {"TMP", env.create_dir("dir2")}, + {"TEMP", env.create_dir("dir3")}, + {"TEMPDIR", env.create_dir("dir4")} + }; + for (auto& TC : cases) { + PutEnv(TC.name, TC.p); + } + for (auto& TC : cases) { + std::error_code ec = GetTestEC(); + path ret = temp_directory_path(ec); + TEST_CHECK(!ec); + TEST_CHECK(ret == TC.p); + TEST_CHECK(is_directory(ret)); + + // Set the env variable to a path that does not exist and check + // that it fails. + PutEnv(TC.name, dne); + ec = GetTestEC(); + ret = temp_directory_path(ec); + LIBCPP_ONLY(TEST_CHECK(ec == expect_ec)); + TEST_CHECK(ec != GetTestEC()); + TEST_CHECK(ec); + TEST_CHECK(ret == ""); + + // Set the env variable to point to a file and check that it fails. + PutEnv(TC.name, file); + ec = GetTestEC(); + ret = temp_directory_path(ec); + LIBCPP_ONLY(TEST_CHECK(ec == expect_ec)); + TEST_CHECK(ec != GetTestEC()); + TEST_CHECK(ec); + TEST_CHECK(ret == ""); + + // Set the env variable to point to a dir we can't access + PutEnv(TC.name, nested_dir); + ec = GetTestEC(); + ret = temp_directory_path(ec); + TEST_CHECK(ec == std::make_error_code(std::errc::permission_denied)); + TEST_CHECK(ret == ""); + + // Set the env variable to point to a non-existent dir + PutEnv(TC.name, TC.p / "does_not_exist"); + ec = GetTestEC(); + ret = temp_directory_path(ec); + TEST_CHECK(ec != GetTestEC()); + TEST_CHECK(ec); + TEST_CHECK(ret == ""); + + // Finally erase this env variable + UnsetEnv(TC.name); + } + // No env variables are defined + { + std::error_code ec = GetTestEC(); + path ret = temp_directory_path(ec); + TEST_CHECK(!ec); + TEST_CHECK(ret == "/tmp"); + TEST_CHECK(is_directory(ret)); + } +} + +TEST_SUITE_END() diff --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.weakly_canonical/weakly_canonical.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.weakly_canonical/weakly_canonical.pass.cpp new file mode 100644 index 00000000000..94a8e13bc08 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.weakly_canonical/weakly_canonical.pass.cpp @@ -0,0 +1,75 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +// path weakly_canonical(const path& p); +// path weakly_canonical(const path& p, error_code& ec); + +#include "filesystem_include.hpp" +#include <type_traits> +#include <vector> +#include <iostream> +#include <cassert> + +#include "test_macros.h" +#include "test_iterators.h" +#include "count_new.hpp" +#include "filesystem_test_helper.hpp" + + +int main(int, char**) { + // clang-format off + struct { + std::string input; + std::string expect; + } TestCases[] = { + {"", fs::current_path()}, + {".", fs::current_path()}, + {"/", "/"}, + {"/foo", "/foo"}, + {"/.", "/"}, + {"/./", "/"}, + {"a/b", fs::current_path() / "a/b"}, + {"a", fs::current_path() / "a"}, + {"a/b/", fs::current_path() / "a/b/"}, + {StaticEnv::File, StaticEnv::File}, + {StaticEnv::Dir, StaticEnv::Dir}, + {StaticEnv::SymlinkToDir, StaticEnv::Dir}, + {StaticEnv::SymlinkToDir / "dir2/.", StaticEnv::Dir / "dir2"}, + // FIXME? If the trailing separator occurs in a part of the path that exists, + // it is ommitted. Otherwise it is added to the end of the result. + {StaticEnv::SymlinkToDir / "dir2/./", StaticEnv::Dir / "dir2"}, + {StaticEnv::SymlinkToDir / "dir2/DNE/./", StaticEnv::Dir / "dir2/DNE/"}, + {StaticEnv::SymlinkToDir / "dir2", StaticEnv::Dir2}, + {StaticEnv::SymlinkToDir / "dir2/../dir2/DNE/..", StaticEnv::Dir2 / ""}, + {StaticEnv::SymlinkToDir / "dir2/dir3/../DNE/DNE2", StaticEnv::Dir2 / "DNE/DNE2"}, + {StaticEnv::Dir / "../dir1", StaticEnv::Dir}, + {StaticEnv::Dir / "./.", StaticEnv::Dir}, + {StaticEnv::Dir / "DNE/../foo", StaticEnv::Dir / "foo"} + }; + // clang-format on + int ID = 0; + bool Failed = false; + for (auto& TC : TestCases) { + ++ID; + fs::path p(TC.input); + const fs::path output = fs::weakly_canonical(p); + if (!PathEq(output, TC.expect)) { + Failed = true; + std::cerr << "TEST CASE #" << ID << " FAILED: \n"; + std::cerr << " Input: '" << TC.input << "'\n"; + std::cerr << " Expected: '" << TC.expect << "'\n"; + std::cerr << " Output: '" << output.native() << "'"; + std::cerr << std::endl; + } + } + return Failed; +} |