diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2023-05-30 17:57:21 +0200 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2023-06-26 17:57:29 +0200 |
commit | 0b924c5c23a89760ddecf8d8f6baa19333f9b667 (patch) | |
tree | 53be66938dcfd1f38a7dea98566e6de39d6df10f /test/buildtool/file_system/file_system_manager.test.cpp | |
parent | b94f1b857c6bc1eab909c4fb9a0ba569d6d28a03 (diff) | |
download | justbuild-0b924c5c23a89760ddecf8d8f6baa19333f9b667.tar.gz |
filesystem: Add logic for handling (non-upwards) symlinks
Diffstat (limited to 'test/buildtool/file_system/file_system_manager.test.cpp')
-rw-r--r-- | test/buildtool/file_system/file_system_manager.test.cpp | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/test/buildtool/file_system/file_system_manager.test.cpp b/test/buildtool/file_system/file_system_manager.test.cpp index 4b48308e..b4a22035 100644 --- a/test/buildtool/file_system/file_system_manager.test.cpp +++ b/test/buildtool/file_system/file_system_manager.test.cpp @@ -18,6 +18,7 @@ #include <filesystem> #include <fstream> #include <iostream> +#include <unordered_map> #include "catch2/catch_test_macros.hpp" #include "catch2/generators/catch_generators_all.hpp" @@ -62,6 +63,104 @@ class WriteFileFixture { "file"}; }; +class SymlinkTestsFixture { + public: + SymlinkTestsFixture() noexcept { + REQUIRE(FileSystemManager::CreateDirectory(root_dir_)); + create_files(); + create_symlinks(); + } + SymlinkTestsFixture(SymlinkTestsFixture const&) = delete; + SymlinkTestsFixture(SymlinkTestsFixture&&) = delete; + ~SymlinkTestsFixture() noexcept { + CHECK(std::filesystem::remove_all(root_dir_)); + } + auto operator=(SymlinkTestsFixture const&) -> SymlinkTestsFixture& = delete; + auto operator=(SymlinkTestsFixture&&) -> SymlinkTestsFixture& = delete; + + std::filesystem::path const root_dir_{"./tmp-Symlinks"}; + + using filetree_t = std::unordered_map<std::string, ObjectType>; + filetree_t const kExpected = {{"foo", ObjectType::File}, + {"baz", ObjectType::Tree}, + {"baz/foo", ObjectType::File}}; + + struct LinkInfo { + std::string to; + std::string link; + bool resolvesToExisting; + bool isNonUpwards; + }; + std::vector<LinkInfo> const kSymExpected = { + {.to = "baz", + .link = "baz_l", + .resolvesToExisting = true, + .isNonUpwards = true}, + {.to = "../foo", + .link = "baz/foo_l", + .resolvesToExisting = true, + .isNonUpwards = false}, + {.to = "baz/foo_l", + .link = "bar_l", + .resolvesToExisting = true, + .isNonUpwards = true}, + {.to = "does_not_exist", + .link = "baz/non_existing_l", + .resolvesToExisting = false, + .isNonUpwards = true}, + {.to = "non_existing_l", + .link = "baz/non_existing_indirect_l", + .resolvesToExisting = false, + .isNonUpwards = true}, + {.to = "baz/../../does_not_exist", + .link = "non_existing_sneaky_l", + .resolvesToExisting = false, + .isNonUpwards = false}}; + + void create_files() { + for (auto const& [path, type] : kExpected) { + switch (type) { + case ObjectType::File: { + if (not FileSystemManager::WriteFile("", + root_dir_ / path)) { + Logger::Log(LogLevel::Error, + "Could not create test file at path {}", + (root_dir_ / path).string()); + std::exit(1); + }; + } break; + case ObjectType::Tree: { + if (not FileSystemManager::CreateDirectory(root_dir_ / + path)) { + Logger::Log(LogLevel::Error, + "Could not create test dir at path {}", + (root_dir_ / path).string()); + std::exit(1); + }; + } break; + default: { + Logger::Log(LogLevel::Error, + "File system failure in creating test dir"); + std::exit(1); + } + } + } + } + + void create_symlinks() { + for (auto const& link_info : kSymExpected) { + if (not FileSystemManager::CreateSymlink( + link_info.to, root_dir_ / link_info.link)) { + Logger::Log( + LogLevel::Error, + "File system failure in creating symlink at path {}", + (root_dir_ / link_info.link).string()); + std::exit(1); + } + } + } +}; + namespace { namespace fs = std::filesystem; @@ -677,3 +776,20 @@ TEST_CASE_METHOD(CopyFileFixture, "CreateFileHardlinkAs", "[file_system]") { } } } + +TEST_CASE_METHOD(SymlinkTestsFixture, "Symlinks", "[file_system]") { + auto i = GENERATE(range(0U, 6U /* kSymExpected.size() */)); + + SECTION(fmt::format("Non-upwards symlinks - entry {}", i)) { + CHECK(FileSystemManager::IsNonUpwardsSymlink(root_dir_ / + kSymExpected[i].link) == + kSymExpected[i].isNonUpwards); + } + + SECTION(fmt::format("Resolve symlinks - entry {}", i)) { + auto path = root_dir_ / kSymExpected[i].link; + REQUIRE(FileSystemManager::ResolveSymlinks(&path)); + CHECK(FileSystemManager::Exists(path) == + kSymExpected[i].resolvesToExisting); + } +} |