diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2022-02-28 13:14:23 +0100 |
---|---|---|
committer | Oliver Reiche <oliver.reiche@huawei.com> | 2022-02-28 13:44:06 +0100 |
commit | 0805693c1ef33bb83d8d21b10231d0ea20d9a74b (patch) | |
tree | 585b8073ad75e72ce4283d59a22b910ee298f836 | |
parent | 1bed68a665a323e929a3012ce28ac1a9b2cb0992 (diff) | |
download | justbuild-0805693c1ef33bb83d8d21b10231d0ea20d9a74b.tar.gz |
Test filesystem: Check exact permissions
... instead of relying on the filesystem preventing us from
writing to a read-only file, which wont happen if the user
is root.
-rw-r--r-- | test/buildtool/file_system/file_system_manager.test.cpp | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/test/buildtool/file_system/file_system_manager.test.cpp b/test/buildtool/file_system/file_system_manager.test.cpp index 5444a94b..8527fe17 100644 --- a/test/buildtool/file_system/file_system_manager.test.cpp +++ b/test/buildtool/file_system/file_system_manager.test.cpp @@ -45,6 +45,34 @@ class WriteFileFixture { "file"}; }; +namespace { + +namespace fs = std::filesystem; + +constexpr auto kFilePerms = + fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read; + +constexpr auto kExecPerms = + fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec; + +auto HasFilePermissions(fs::path const& path) noexcept -> bool { + try { + return fs::status(path).permissions() == kFilePerms; + } catch (...) { + return false; + } +} + +auto HasExecutablePermissions(fs::path const& path) noexcept -> bool { + try { + return fs::status(path).permissions() == (kFilePerms | kExecPerms); + } catch (...) { + return false; + } +} + +} // namespace + TEST_CASE("CreateDirectory", "[file_system]") { auto const dir = GENERATE(as<std::filesystem::path>{}, "level0", @@ -172,8 +200,8 @@ TEST_CASE_METHOD(CopyFileFixture, "CopyFileAs", "[file_system]") { CHECK(content_to.has_value()); CHECK(content_from == content_to); - // permissions should be 0444 (not writable, but removable) - CHECK(not FileSystemManager::WriteFile("replacement content", to_)); + // permissions should be 0444 + CHECK(HasFilePermissions(to_)); } SECTION("as executable") { // Copy as file was successful @@ -192,8 +220,8 @@ TEST_CASE_METHOD(CopyFileFixture, "CopyFileAs", "[file_system]") { CHECK(content_to.has_value()); CHECK(content_from == content_to); - // permissions should be 0555 (not writable, but removable) - CHECK(not FileSystemManager::WriteFile("replacement content", to_)); + // permissions should be 0555 + CHECK(HasExecutablePermissions(to_)); } } @@ -256,9 +284,8 @@ TEST_CASE_METHOD(WriteFileFixture, "WriteFileAs", "[file_system]") { CHECK(written_content.has_value()); CHECK(written_content == content); - // permissions should be 0444 (not writable, but removable) - CHECK(not FileSystemManager::WriteFile("replacement content", - file_path_)); + // permissions should be 0444 + CHECK(HasFilePermissions(file_path_)); } SECTION("as an executable") { std::string const content{"\n"}; @@ -274,9 +301,8 @@ TEST_CASE_METHOD(WriteFileFixture, "WriteFileAs", "[file_system]") { CHECK(written_content.has_value()); CHECK(written_content == content); - // permissions should be 0555 (not writable, but removable) - CHECK(not FileSystemManager::WriteFile("replacement content", - file_path_)); + // permissions should be 0555 + CHECK(HasExecutablePermissions(file_path_)); } } |