diff options
| author | Nicole Mazzuca <t-nimaz@microsoft.com> | 2019-07-15 16:43:55 -0700 |
|---|---|---|
| committer | Nicole Mazzuca <t-nimaz@microsoft.com> | 2019-07-15 16:43:55 -0700 |
| commit | 02c977186e890e4090d91c171b42d20729e668af (patch) | |
| tree | 23ade9a4f25cad61bb659ccf1853fd5d0197e8ba /toolsrc/src/tests.files.cpp | |
| parent | 771e23c665f1960ef4e7fd9816e0d21c943a7903 (diff) | |
| download | vcpkg-02c977186e890e4090d91c171b42d20729e668af.tar.gz vcpkg-02c977186e890e4090d91c171b42d20729e668af.zip | |
modify files test to include symlinks
Diffstat (limited to 'toolsrc/src/tests.files.cpp')
| -rw-r--r-- | toolsrc/src/tests.files.cpp | 121 |
1 files changed, 87 insertions, 34 deletions
diff --git a/toolsrc/src/tests.files.cpp b/toolsrc/src/tests.files.cpp index 73c7eb5bc..c99dbb286 100644 --- a/toolsrc/src/tests.files.cpp +++ b/toolsrc/src/tests.files.cpp @@ -10,65 +10,60 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; -namespace UnitTest1 { - class FilesTest : public TestClass<FilesTest> { +namespace UnitTest1 +{ + class FilesTest : public TestClass<FilesTest> + { using uid = std::uniform_int_distribution<std::uint64_t>; - - std::string get_random_filename() - { - std::random_device rd; - return vcpkg::Strings::b64url_encode(uid{}(rd)); - } - - void create_directory_tree( - vcpkg::Files::Filesystem& fs, - std::uint64_t depth, - const fs::path& base) - { - std::random_device rd; - constexpr auto max_depth = std::uint64_t(3); - const auto width = depth ? uid{0, (max_depth - depth) * 3 / 2}(rd) : 5; - std::error_code ec; - if (width == 0) { - fs.write_contents(base, "", ec); - Assert::IsFalse(bool(ec)); + public: + FilesTest() + { + HKEY key; + const auto status = RegOpenKeyExW( + HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock)", 0, KEY_READ, &key); - return; + if (!status) + { + ALLOW_SYMLINKS = false; + std::clog << "Symlinks are not allowed on this system\n"; } - - fs.create_directory(base, ec); - Assert::IsFalse(bool(ec)); - - for (int i = 0; i < width; ++i) { - create_directory_tree(fs, depth + 1, base / get_random_filename()); + else + { + ALLOW_SYMLINKS = true; + RegCloseKey(key); } } - - TEST_METHOD(remove_all) { + + private: + TEST_METHOD(remove_all) + { + auto urbg = get_urbg(0); + fs::path temp_dir; { wchar_t* tmp = static_cast<wchar_t*>(calloc(32'767, 2)); - if (!GetEnvironmentVariableW(L"TEMP", tmp, 32'767)) { + if (!GetEnvironmentVariableW(L"TEMP", tmp, 32'767)) + { Assert::Fail(L"GetEnvironmentVariable(\"TEMP\") failed"); } temp_dir = tmp; std::string dir_name = "vcpkg-tmp-dir-"; - dir_name += get_random_filename(); + dir_name += get_random_filename(urbg); temp_dir /= dir_name; } auto& fs = vcpkg::Files::get_real_filesystem(); - std::cout << "temp dir is: " << temp_dir << '\n'; + std::clog << "temp dir is: " << temp_dir << '\n'; std::error_code ec; - create_directory_tree(fs, 0, temp_dir); + create_directory_tree(urbg, fs, 0, temp_dir); fs::path fp; fs.remove_all(temp_dir, ec, fp); @@ -76,5 +71,63 @@ namespace UnitTest1 { Assert::IsFalse(fs.exists(temp_dir)); } + + bool ALLOW_SYMLINKS; + + std::mt19937_64 get_urbg(std::uint64_t index) + { + // smallest prime > 2**63 - 1 + return std::mt19937_64{index + 9223372036854775837}; + } + + std::string get_random_filename(std::mt19937_64& urbg) { return vcpkg::Strings::b64url_encode(uid{}(urbg)); } + + void create_directory_tree(std::mt19937_64& urbg, + vcpkg::Files::Filesystem& fs, + std::uint64_t depth, + const fs::path& base) + { + std::random_device rd; + constexpr auto max_depth = std::uint64_t(3); + const auto width = depth ? uid{0, (max_depth - depth) * 3 / 2}(urbg) : 5; + + std::error_code ec; + if (width == 0) + { + // I don't want to move urbg forward conditionally + const auto type = uid{0, 3}(urbg); + if (type == 0 || !ALLOW_SYMLINKS) + { + // 0 is a regular file + fs.write_contents(base, "", ec); + } + else if (type == 1) + { + // 1 is a regular symlink + fs.write_contents(base, "", ec); + Assert::IsFalse(bool(ec)); + fs::path base_link = base; + base_link.append("-link"); + fs::stdfs::create_symlink(base, base_link, ec); + } + else + { + // 2 is a directory symlink + fs::stdfs::create_directory_symlink(".", base, ec); + } + + Assert::IsFalse(bool(ec)); + + return; + } + + fs.create_directory(base, ec); + Assert::IsFalse(bool(ec)); + + for (int i = 0; i < width; ++i) + { + create_directory_tree(urbg, fs, depth + 1, base / get_random_filename(urbg)); + } + } }; } |
