1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
//===-- TestFS.cpp ----------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "TestFS.h"
#include "llvm/Support/Errc.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
/// An implementation of vfs::FileSystem that only allows access to
/// files and folders inside a set of whitelisted directories.
///
/// FIXME(ibiryukov): should it also emulate access to parents of whitelisted
/// directories with only whitelisted contents?
class FilteredFileSystem : public vfs::FileSystem {
public:
/// The paths inside \p WhitelistedDirs should be absolute
FilteredFileSystem(std::vector<std::string> WhitelistedDirs,
IntrusiveRefCntPtr<vfs::FileSystem> InnerFS)
: WhitelistedDirs(std::move(WhitelistedDirs)), InnerFS(InnerFS) {
assert(std::all_of(WhitelistedDirs.begin(), WhitelistedDirs.end(),
[](const std::string &Path) -> bool {
return llvm::sys::path::is_absolute(Path);
}) &&
"Not all WhitelistedDirs are absolute");
}
virtual llvm::ErrorOr<vfs::Status> status(const Twine &Path) {
if (!isInsideWhitelistedDir(Path))
return llvm::errc::no_such_file_or_directory;
return InnerFS->status(Path);
}
virtual llvm::ErrorOr<std::unique_ptr<vfs::File>>
openFileForRead(const Twine &Path) {
if (!isInsideWhitelistedDir(Path))
return llvm::errc::no_such_file_or_directory;
return InnerFS->openFileForRead(Path);
}
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFile(const Twine &Name, int64_t FileSize = -1,
bool RequiresNullTerminator = true,
bool IsVolatile = false) {
if (!isInsideWhitelistedDir(Name))
return llvm::errc::no_such_file_or_directory;
return InnerFS->getBufferForFile(Name, FileSize, RequiresNullTerminator,
IsVolatile);
}
virtual vfs::directory_iterator dir_begin(const Twine &Dir,
std::error_code &EC) {
if (!isInsideWhitelistedDir(Dir)) {
EC = llvm::errc::no_such_file_or_directory;
return vfs::directory_iterator();
}
return InnerFS->dir_begin(Dir, EC);
}
virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) {
return InnerFS->setCurrentWorkingDirectory(Path);
}
virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const {
return InnerFS->getCurrentWorkingDirectory();
}
bool exists(const Twine &Path) {
if (!isInsideWhitelistedDir(Path))
return false;
return InnerFS->exists(Path);
}
std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const {
return InnerFS->makeAbsolute(Path);
}
private:
bool isInsideWhitelistedDir(const Twine &InputPath) const {
SmallString<128> Path;
InputPath.toVector(Path);
if (makeAbsolute(Path))
return false;
for (const auto &Dir : WhitelistedDirs) {
if (Path.startswith(Dir))
return true;
}
return false;
}
std::vector<std::string> WhitelistedDirs;
IntrusiveRefCntPtr<vfs::FileSystem> InnerFS;
};
/// Create a vfs::FileSystem that has access only to temporary directories
/// (obtained by calling system_temp_directory).
IntrusiveRefCntPtr<vfs::FileSystem> getTempOnlyFS() {
llvm::SmallString<128> TmpDir1;
llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, TmpDir1);
llvm::SmallString<128> TmpDir2;
llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/true, TmpDir2);
std::vector<std::string> TmpDirs;
TmpDirs.push_back(TmpDir1.str());
if (TmpDir1 != TmpDir2)
TmpDirs.push_back(TmpDir2.str());
return new FilteredFileSystem(std::move(TmpDirs), vfs::getRealFileSystem());
}
} // namespace
IntrusiveRefCntPtr<vfs::FileSystem>
buildTestFS(llvm::StringMap<std::string> const &Files) {
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> MemFS(
new vfs::InMemoryFileSystem);
for (auto &FileAndContents : Files)
MemFS->addFile(FileAndContents.first(), time_t(),
llvm::MemoryBuffer::getMemBuffer(FileAndContents.second,
FileAndContents.first()));
auto OverlayFS = IntrusiveRefCntPtr<vfs::OverlayFileSystem>(
new vfs::OverlayFileSystem(getTempOnlyFS()));
OverlayFS->pushOverlay(std::move(MemFS));
return OverlayFS;
}
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
MockFSProvider::getTaggedFileSystem(PathRef File) {
if (ExpectedFile) {
EXPECT_EQ(*ExpectedFile, File);
}
auto FS = buildTestFS(Files);
return make_tagged(FS, Tag);
}
MockCompilationDatabase::MockCompilationDatabase()
: ExtraClangFlags({"-ffreestanding"}) {} // Avoid implicit stdc-predef.h.
llvm::Optional<tooling::CompileCommand>
MockCompilationDatabase::getCompileCommand(PathRef File) const {
if (ExtraClangFlags.empty())
return llvm::None;
auto CommandLine = ExtraClangFlags;
CommandLine.insert(CommandLine.begin(), "clang");
CommandLine.insert(CommandLine.end(), File.str());
return {tooling::CompileCommand(llvm::sys::path::parent_path(File),
llvm::sys::path::filename(File),
std::move(CommandLine), "")};
}
static const char *getVirtualTestRoot() {
#ifdef LLVM_ON_WIN32
return "C:\\clangd-test";
#else
return "/clangd-test";
#endif
}
llvm::SmallString<32> getVirtualTestFilePath(PathRef File) {
assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
llvm::SmallString<32> Path;
llvm::sys::path::append(Path, getVirtualTestRoot(), File);
return Path;
}
} // namespace clangd
} // namespace clang
|