From da787dd39f5a563b462d716d3bcfbb039dee9cd1 Mon Sep 17 00:00:00 2001 From: Alberto Sartori Date: Sun, 10 Apr 2022 20:21:18 +0200 Subject: add u+w permission when installing a file ... to allow for overwriting --- .../file_system/file_system_manager.test.cpp | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) (limited to 'test/buildtool/file_system/file_system_manager.test.cpp') diff --git a/test/buildtool/file_system/file_system_manager.test.cpp b/test/buildtool/file_system/file_system_manager.test.cpp index 607e3dd2..dfbbd1ce 100644 --- a/test/buildtool/file_system/file_system_manager.test.cpp +++ b/test/buildtool/file_system/file_system_manager.test.cpp @@ -53,6 +53,8 @@ namespace fs = std::filesystem; constexpr auto kFilePerms = fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read; +constexpr auto kInstalledPerms = fs::perms::owner_write; + constexpr auto kExecPerms = fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec; @@ -64,6 +66,14 @@ auto HasFilePermissions(fs::path const& path) noexcept -> bool { } } +auto HasInstalledFilePermissions(fs::path const& path) noexcept -> bool { + try { + return fs::status(path).permissions() == (kFilePerms | kInstalledPerms); + } catch (...) { + return false; + } +} + auto HasExecutablePermissions(fs::path const& path) noexcept -> bool { try { return fs::status(path).permissions() == (kFilePerms | kExecPerms); @@ -72,6 +82,15 @@ auto HasExecutablePermissions(fs::path const& path) noexcept -> bool { } } +auto HasInstalledExecutablePermissions(fs::path const& path) noexcept -> bool { + try { + return fs::status(path).permissions() == + (kFilePerms | kExecPerms | kInstalledPerms); + } catch (...) { + return false; + } +} + auto HasEpochTime(fs::path const& path) noexcept -> bool { try { return fs::last_write_time(path) == @@ -232,6 +251,41 @@ TEST_CASE_METHOD(CopyFileFixture, "CopyFileAs", "[file_system]") { run_test.template operator()(true); } } + SECTION("as installed file") { + auto run_test = [&](bool fd_less) { + // Copy as file was successful + CHECK(FileSystemManager::CopyFileAs( + from_, to_, ObjectType::File, fd_less)); + + // file exists + CHECK(std::filesystem::exists(to_)); + CHECK(std::filesystem::is_regular_file(to_)); + CHECK(not FileSystemManager::IsExecutable(to_)); + + // Contents are equal + auto const content_from = FileSystemManager::ReadFile(from_); + CHECK(content_from.has_value()); + auto const content_to = FileSystemManager::ReadFile(to_); + CHECK(content_to.has_value()); + CHECK(content_from == content_to); + + // permissions should be 0644 + CHECK(HasInstalledFilePermissions(to_)); + if constexpr (kSetEpochTime) { + CHECK(HasEpochTime(to_)); + } + }; + + SECTION("direct") { run_test(false); } + SECTION("fd_less") { run_test(true); } + SECTION("direct with epoch") { + run_test.template operator()(false); + } + SECTION("fd_less with epoch") { + run_test.template operator()(true); + } + } SECTION("as executable") { auto run_test = [&](bool fd_less) { // Copy as file was successful @@ -257,6 +311,41 @@ TEST_CASE_METHOD(CopyFileFixture, "CopyFileAs", "[file_system]") { } }; + SECTION("direct") { run_test(false); } + SECTION("fd_less") { run_test(true); } + SECTION("direct with epoch") { + run_test.template operator()(false); + } + SECTION("fd_less with epoch") { + run_test.template operator()(true); + } + } + SECTION("as installed executable") { + auto run_test = [&](bool fd_less) { + // Copy as file was successful + CHECK(FileSystemManager::CopyFileAs( + from_, to_, ObjectType::Executable, fd_less)); + + // file exists + CHECK(std::filesystem::exists(to_)); + CHECK(std::filesystem::is_regular_file(to_)); + CHECK(FileSystemManager::IsExecutable(to_)); + + // Contents are equal + auto const content_from = FileSystemManager::ReadFile(from_); + CHECK(content_from.has_value()); + auto const content_to = FileSystemManager::ReadFile(to_); + CHECK(content_to.has_value()); + CHECK(content_from == content_to); + + // permissions should be 0755 + CHECK(HasInstalledExecutablePermissions(to_)); + if constexpr (kSetEpochTime) { + CHECK(HasEpochTime(to_)); + } + }; + SECTION("direct") { run_test(false); } SECTION("fd_less") { run_test(true); } SECTION("direct with epoch") { -- cgit v1.2.3