modify files test to include symlinks

This commit is contained in:
Nicole Mazzuca 2019-07-15 16:43:55 -07:00
parent 771e23c665
commit 02c977186e
1 changed files with 100 additions and 47 deletions

View File

@ -10,28 +10,112 @@
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()
public:
FilesTest()
{
std::random_device rd;
return vcpkg::Strings::b64url_encode(uid{}(rd));
HKEY key;
const auto status = RegOpenKeyExW(
HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock)", 0, KEY_READ, &key);
if (!status)
{
ALLOW_SYMLINKS = false;
std::clog << "Symlinks are not allowed on this system\n";
}
else
{
ALLOW_SYMLINKS = true;
RegCloseKey(key);
}
}
void create_directory_tree(
vcpkg::Files::Filesystem& fs,
std::uint64_t depth,
const fs::path& base)
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))
{
Assert::Fail(L"GetEnvironmentVariable(\"TEMP\") failed");
}
temp_dir = tmp;
std::string dir_name = "vcpkg-tmp-dir-";
dir_name += get_random_filename(urbg);
temp_dir /= dir_name;
}
auto& fs = vcpkg::Files::get_real_filesystem();
std::clog << "temp dir is: " << temp_dir << '\n';
std::error_code ec;
create_directory_tree(urbg, fs, 0, temp_dir);
fs::path fp;
fs.remove_all(temp_dir, ec, fp);
Assert::IsFalse(bool(ec));
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}(rd) : 5;
const auto width = depth ? uid{0, (max_depth - depth) * 3 / 2}(urbg) : 5;
std::error_code ec;
if (width == 0) {
fs.write_contents(base, "", 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;
@ -40,41 +124,10 @@ namespace UnitTest1 {
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());
}
}
TEST_METHOD(remove_all) {
fs::path temp_dir;
for (int i = 0; i < width; ++i)
{
wchar_t* tmp = static_cast<wchar_t*>(calloc(32'767, 2));
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();
temp_dir /= dir_name;
create_directory_tree(urbg, fs, depth + 1, base / get_random_filename(urbg));
}
auto& fs = vcpkg::Files::get_real_filesystem();
std::cout << "temp dir is: " << temp_dir << '\n';
std::error_code ec;
create_directory_tree(fs, 0, temp_dir);
fs::path fp;
fs.remove_all(temp_dir, ec, fp);
Assert::IsFalse(bool(ec));
Assert::IsFalse(fs.exists(temp_dir));
}
};
}