VirtualFileSystemTest.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. //===- unittests/Basic/VirtualFileSystem.cpp ---------------- VFS tests ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Basic/VirtualFileSystem.h"
  10. #include "llvm/Support/Errc.h"
  11. #include "llvm/Support/MemoryBuffer.h"
  12. #include "llvm/Support/Path.h"
  13. #include "llvm/Support/SourceMgr.h"
  14. #include "gtest/gtest.h"
  15. #include <map>
  16. using namespace clang;
  17. using namespace llvm;
  18. using llvm::sys::fs::UniqueID;
  19. namespace {
  20. class DummyFileSystem : public vfs::FileSystem {
  21. int FSID; // used to produce UniqueIDs
  22. int FileID; // used to produce UniqueIDs
  23. std::map<std::string, vfs::Status> FilesAndDirs;
  24. static int getNextFSID() {
  25. static int Count = 0;
  26. return Count++;
  27. }
  28. public:
  29. DummyFileSystem() : FSID(getNextFSID()), FileID(0) {}
  30. ErrorOr<vfs::Status> status(const Twine &Path) {
  31. std::map<std::string, vfs::Status>::iterator I =
  32. FilesAndDirs.find(Path.str());
  33. if (I == FilesAndDirs.end())
  34. return make_error_code(llvm::errc::no_such_file_or_directory);
  35. return I->second;
  36. }
  37. std::error_code openFileForRead(const Twine &Path,
  38. std::unique_ptr<vfs::File> &Result) {
  39. llvm_unreachable("unimplemented");
  40. }
  41. std::error_code getBufferForFile(const Twine &Name,
  42. std::unique_ptr<MemoryBuffer> &Result,
  43. int64_t FileSize = -1,
  44. bool RequiresNullTerminator = true) {
  45. llvm_unreachable("unimplemented");
  46. }
  47. struct DirIterImpl : public clang::vfs::detail::DirIterImpl {
  48. std::map<std::string, vfs::Status> &FilesAndDirs;
  49. std::map<std::string, vfs::Status>::iterator I;
  50. std::string Path;
  51. bool isInPath(StringRef S) {
  52. if (Path.size() < S.size() && S.find(Path) == 0) {
  53. auto LastSep = S.find_last_of('/');
  54. if (LastSep == Path.size() || LastSep == Path.size()-1)
  55. return true;
  56. }
  57. return false;
  58. }
  59. DirIterImpl(std::map<std::string, vfs::Status> &FilesAndDirs,
  60. const Twine &_Path)
  61. : FilesAndDirs(FilesAndDirs), I(FilesAndDirs.begin()),
  62. Path(_Path.str()) {
  63. for ( ; I != FilesAndDirs.end(); ++I) {
  64. if (isInPath(I->first)) {
  65. CurrentEntry = I->second;
  66. break;
  67. }
  68. }
  69. }
  70. std::error_code increment() override {
  71. ++I;
  72. for ( ; I != FilesAndDirs.end(); ++I) {
  73. if (isInPath(I->first)) {
  74. CurrentEntry = I->second;
  75. break;
  76. }
  77. }
  78. if (I == FilesAndDirs.end())
  79. CurrentEntry = vfs::Status();
  80. return std::error_code();
  81. }
  82. };
  83. vfs::directory_iterator dir_begin(const Twine &Dir,
  84. std::error_code &EC) override {
  85. return vfs::directory_iterator(
  86. std::make_shared<DirIterImpl>(FilesAndDirs, Dir));
  87. }
  88. void addEntry(StringRef Path, const vfs::Status &Status) {
  89. FilesAndDirs[Path] = Status;
  90. }
  91. void addRegularFile(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
  92. vfs::Status S(Path, Path, UniqueID(FSID, FileID++), sys::TimeValue::now(),
  93. 0, 0, 1024, sys::fs::file_type::regular_file, Perms);
  94. addEntry(Path, S);
  95. }
  96. void addDirectory(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
  97. vfs::Status S(Path, Path, UniqueID(FSID, FileID++), sys::TimeValue::now(),
  98. 0, 0, 0, sys::fs::file_type::directory_file, Perms);
  99. addEntry(Path, S);
  100. }
  101. void addSymlink(StringRef Path) {
  102. vfs::Status S(Path, Path, UniqueID(FSID, FileID++), sys::TimeValue::now(),
  103. 0, 0, 0, sys::fs::file_type::symlink_file, sys::fs::all_all);
  104. addEntry(Path, S);
  105. }
  106. };
  107. } // end anonymous namespace
  108. TEST(VirtualFileSystemTest, StatusQueries) {
  109. IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
  110. ErrorOr<vfs::Status> Status((std::error_code()));
  111. D->addRegularFile("/foo");
  112. Status = D->status("/foo");
  113. ASSERT_FALSE(Status.getError());
  114. EXPECT_TRUE(Status->isStatusKnown());
  115. EXPECT_FALSE(Status->isDirectory());
  116. EXPECT_TRUE(Status->isRegularFile());
  117. EXPECT_FALSE(Status->isSymlink());
  118. EXPECT_FALSE(Status->isOther());
  119. EXPECT_TRUE(Status->exists());
  120. D->addDirectory("/bar");
  121. Status = D->status("/bar");
  122. ASSERT_FALSE(Status.getError());
  123. EXPECT_TRUE(Status->isStatusKnown());
  124. EXPECT_TRUE(Status->isDirectory());
  125. EXPECT_FALSE(Status->isRegularFile());
  126. EXPECT_FALSE(Status->isSymlink());
  127. EXPECT_FALSE(Status->isOther());
  128. EXPECT_TRUE(Status->exists());
  129. D->addSymlink("/baz");
  130. Status = D->status("/baz");
  131. ASSERT_FALSE(Status.getError());
  132. EXPECT_TRUE(Status->isStatusKnown());
  133. EXPECT_FALSE(Status->isDirectory());
  134. EXPECT_FALSE(Status->isRegularFile());
  135. EXPECT_TRUE(Status->isSymlink());
  136. EXPECT_FALSE(Status->isOther());
  137. EXPECT_TRUE(Status->exists());
  138. EXPECT_TRUE(Status->equivalent(*Status));
  139. ErrorOr<vfs::Status> Status2 = D->status("/foo");
  140. ASSERT_FALSE(Status2.getError());
  141. EXPECT_FALSE(Status->equivalent(*Status2));
  142. }
  143. TEST(VirtualFileSystemTest, BaseOnlyOverlay) {
  144. IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
  145. ErrorOr<vfs::Status> Status((std::error_code()));
  146. EXPECT_FALSE(Status = D->status("/foo"));
  147. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(new vfs::OverlayFileSystem(D));
  148. EXPECT_FALSE(Status = O->status("/foo"));
  149. D->addRegularFile("/foo");
  150. Status = D->status("/foo");
  151. EXPECT_FALSE(Status.getError());
  152. ErrorOr<vfs::Status> Status2((std::error_code()));
  153. Status2 = O->status("/foo");
  154. EXPECT_FALSE(Status2.getError());
  155. EXPECT_TRUE(Status->equivalent(*Status2));
  156. }
  157. TEST(VirtualFileSystemTest, OverlayFiles) {
  158. IntrusiveRefCntPtr<DummyFileSystem> Base(new DummyFileSystem());
  159. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  160. IntrusiveRefCntPtr<DummyFileSystem> Top(new DummyFileSystem());
  161. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  162. new vfs::OverlayFileSystem(Base));
  163. O->pushOverlay(Middle);
  164. O->pushOverlay(Top);
  165. ErrorOr<vfs::Status> Status1((std::error_code())),
  166. Status2((std::error_code())), Status3((std::error_code())),
  167. StatusB((std::error_code())), StatusM((std::error_code())),
  168. StatusT((std::error_code()));
  169. Base->addRegularFile("/foo");
  170. StatusB = Base->status("/foo");
  171. ASSERT_FALSE(StatusB.getError());
  172. Status1 = O->status("/foo");
  173. ASSERT_FALSE(Status1.getError());
  174. Middle->addRegularFile("/foo");
  175. StatusM = Middle->status("/foo");
  176. ASSERT_FALSE(StatusM.getError());
  177. Status2 = O->status("/foo");
  178. ASSERT_FALSE(Status2.getError());
  179. Top->addRegularFile("/foo");
  180. StatusT = Top->status("/foo");
  181. ASSERT_FALSE(StatusT.getError());
  182. Status3 = O->status("/foo");
  183. ASSERT_FALSE(Status3.getError());
  184. EXPECT_TRUE(Status1->equivalent(*StatusB));
  185. EXPECT_TRUE(Status2->equivalent(*StatusM));
  186. EXPECT_TRUE(Status3->equivalent(*StatusT));
  187. EXPECT_FALSE(Status1->equivalent(*Status2));
  188. EXPECT_FALSE(Status2->equivalent(*Status3));
  189. EXPECT_FALSE(Status1->equivalent(*Status3));
  190. }
  191. TEST(VirtualFileSystemTest, OverlayDirsNonMerged) {
  192. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  193. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  194. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  195. new vfs::OverlayFileSystem(Lower));
  196. O->pushOverlay(Upper);
  197. Lower->addDirectory("/lower-only");
  198. Upper->addDirectory("/upper-only");
  199. // non-merged paths should be the same
  200. ErrorOr<vfs::Status> Status1 = Lower->status("/lower-only");
  201. ASSERT_FALSE(Status1.getError());
  202. ErrorOr<vfs::Status> Status2 = O->status("/lower-only");
  203. ASSERT_FALSE(Status2.getError());
  204. EXPECT_TRUE(Status1->equivalent(*Status2));
  205. Status1 = Upper->status("/upper-only");
  206. ASSERT_FALSE(Status1.getError());
  207. Status2 = O->status("/upper-only");
  208. ASSERT_FALSE(Status2.getError());
  209. EXPECT_TRUE(Status1->equivalent(*Status2));
  210. }
  211. TEST(VirtualFileSystemTest, MergedDirPermissions) {
  212. // merged directories get the permissions of the upper dir
  213. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  214. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  215. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  216. new vfs::OverlayFileSystem(Lower));
  217. O->pushOverlay(Upper);
  218. ErrorOr<vfs::Status> Status((std::error_code()));
  219. Lower->addDirectory("/both", sys::fs::owner_read);
  220. Upper->addDirectory("/both", sys::fs::owner_all | sys::fs::group_read);
  221. Status = O->status("/both");
  222. ASSERT_FALSE(Status.getError());
  223. EXPECT_EQ(0740, Status->getPermissions());
  224. // permissions (as usual) are not recursively applied
  225. Lower->addRegularFile("/both/foo", sys::fs::owner_read);
  226. Upper->addRegularFile("/both/bar", sys::fs::owner_write);
  227. Status = O->status("/both/foo");
  228. ASSERT_FALSE( Status.getError());
  229. EXPECT_EQ(0400, Status->getPermissions());
  230. Status = O->status("/both/bar");
  231. ASSERT_FALSE(Status.getError());
  232. EXPECT_EQ(0200, Status->getPermissions());
  233. }
  234. namespace {
  235. struct ScopedDir {
  236. SmallString<128> Path;
  237. ScopedDir(const Twine &Name, bool Unique=false) {
  238. std::error_code EC;
  239. if (Unique) {
  240. EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
  241. } else {
  242. Path = Name.str();
  243. EC = llvm::sys::fs::create_directory(Twine(Path));
  244. }
  245. if (EC)
  246. Path = "";
  247. EXPECT_FALSE(EC);
  248. }
  249. ~ScopedDir() {
  250. if (Path != "")
  251. EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
  252. }
  253. operator StringRef() { return Path.str(); }
  254. };
  255. }
  256. TEST(VirtualFileSystemTest, BasicRealFSIteration) {
  257. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  258. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  259. std::error_code EC;
  260. vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC);
  261. ASSERT_FALSE(EC);
  262. EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty
  263. ScopedDir _a(TestDirectory+"/a");
  264. ScopedDir _ab(TestDirectory+"/a/b");
  265. ScopedDir _c(TestDirectory+"/c");
  266. ScopedDir _cd(TestDirectory+"/c/d");
  267. I = FS->dir_begin(Twine(TestDirectory), EC);
  268. ASSERT_FALSE(EC);
  269. ASSERT_NE(vfs::directory_iterator(), I);
  270. // Check either a or c, since we can't rely on the iteration order.
  271. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  272. I.increment(EC);
  273. ASSERT_FALSE(EC);
  274. ASSERT_NE(vfs::directory_iterator(), I);
  275. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  276. I.increment(EC);
  277. EXPECT_EQ(vfs::directory_iterator(), I);
  278. }
  279. TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
  280. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  281. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  282. std::error_code EC;
  283. auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  284. ASSERT_FALSE(EC);
  285. EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty
  286. ScopedDir _a(TestDirectory+"/a");
  287. ScopedDir _ab(TestDirectory+"/a/b");
  288. ScopedDir _c(TestDirectory+"/c");
  289. ScopedDir _cd(TestDirectory+"/c/d");
  290. I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  291. ASSERT_FALSE(EC);
  292. ASSERT_NE(vfs::recursive_directory_iterator(), I);
  293. std::vector<std::string> Contents;
  294. for (auto E = vfs::recursive_directory_iterator(); !EC && I != E;
  295. I.increment(EC)) {
  296. Contents.push_back(I->getName());
  297. }
  298. // Check contents, which may be in any order
  299. EXPECT_EQ(4U, Contents.size());
  300. int Counts[4] = { 0, 0, 0, 0 };
  301. for (const std::string &Name : Contents) {
  302. ASSERT_FALSE(Name.empty());
  303. int Index = Name[Name.size()-1] - 'a';
  304. ASSERT_TRUE(Index >= 0 && Index < 4);
  305. Counts[Index]++;
  306. }
  307. EXPECT_EQ(1, Counts[0]); // a
  308. EXPECT_EQ(1, Counts[1]); // b
  309. EXPECT_EQ(1, Counts[2]); // c
  310. EXPECT_EQ(1, Counts[3]); // d
  311. }
  312. template <typename T, size_t N>
  313. std::vector<StringRef> makeStringRefVector(const T (&Arr)[N]) {
  314. std::vector<StringRef> Vec;
  315. for (size_t i = 0; i != N; ++i)
  316. Vec.push_back(Arr[i]);
  317. return Vec;
  318. }
  319. template <typename DirIter>
  320. static void checkContents(DirIter I, ArrayRef<StringRef> Expected) {
  321. std::error_code EC;
  322. auto ExpectedIter = Expected.begin(), ExpectedEnd = Expected.end();
  323. for (DirIter E;
  324. !EC && I != E && ExpectedIter != ExpectedEnd;
  325. I.increment(EC), ++ExpectedIter)
  326. EXPECT_EQ(*ExpectedIter, I->getName());
  327. EXPECT_EQ(ExpectedEnd, ExpectedIter);
  328. EXPECT_EQ(DirIter(), I);
  329. }
  330. TEST(VirtualFileSystemTest, OverlayIteration) {
  331. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  332. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  333. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  334. new vfs::OverlayFileSystem(Lower));
  335. O->pushOverlay(Upper);
  336. std::error_code EC;
  337. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  338. Lower->addRegularFile("/file1");
  339. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file1"));
  340. Upper->addRegularFile("/file2");
  341. {
  342. const char *Contents[] = {"/file2", "/file1"};
  343. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  344. }
  345. Lower->addDirectory("/dir1");
  346. Lower->addRegularFile("/dir1/foo");
  347. Upper->addDirectory("/dir2");
  348. Upper->addRegularFile("/dir2/foo");
  349. checkContents(O->dir_begin("/dir2", EC), ArrayRef<StringRef>("/dir2/foo"));
  350. {
  351. const char *Contents[] = {"/dir2", "/file2", "/dir1", "/file1"};
  352. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  353. }
  354. }
  355. TEST(VirtualFileSystemTest, OverlayRecursiveIteration) {
  356. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  357. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  358. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  359. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  360. new vfs::OverlayFileSystem(Lower));
  361. O->pushOverlay(Middle);
  362. O->pushOverlay(Upper);
  363. std::error_code EC;
  364. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  365. ArrayRef<StringRef>());
  366. Lower->addRegularFile("/file1");
  367. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  368. ArrayRef<StringRef>("/file1"));
  369. Upper->addDirectory("/dir");
  370. Upper->addRegularFile("/dir/file2");
  371. {
  372. const char *Contents[] = {"/dir", "/dir/file2", "/file1"};
  373. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  374. makeStringRefVector(Contents));
  375. }
  376. Lower->addDirectory("/dir1");
  377. Lower->addRegularFile("/dir1/foo");
  378. Lower->addDirectory("/dir1/a");
  379. Lower->addRegularFile("/dir1/a/b");
  380. Middle->addDirectory("/a");
  381. Middle->addDirectory("/a/b");
  382. Middle->addDirectory("/a/b/c");
  383. Middle->addRegularFile("/a/b/c/d");
  384. Middle->addRegularFile("/hiddenByUp");
  385. Upper->addDirectory("/dir2");
  386. Upper->addRegularFile("/dir2/foo");
  387. Upper->addRegularFile("/hiddenByUp");
  388. checkContents(vfs::recursive_directory_iterator(*O, "/dir2", EC),
  389. ArrayRef<StringRef>("/dir2/foo"));
  390. {
  391. const char *Contents[] = { "/dir", "/dir/file2", "/dir2", "/dir2/foo",
  392. "/hiddenByUp", "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/dir1", "/dir1/a",
  393. "/dir1/a/b", "/dir1/foo", "/file1" };
  394. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  395. makeStringRefVector(Contents));
  396. }
  397. }
  398. TEST(VirtualFileSystemTest, ThreeLevelIteration) {
  399. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  400. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  401. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  402. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  403. new vfs::OverlayFileSystem(Lower));
  404. O->pushOverlay(Middle);
  405. O->pushOverlay(Upper);
  406. std::error_code EC;
  407. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  408. Middle->addRegularFile("/file2");
  409. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file2"));
  410. Lower->addRegularFile("/file1");
  411. Upper->addRegularFile("/file3");
  412. {
  413. const char *Contents[] = {"/file3", "/file2", "/file1"};
  414. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  415. }
  416. }
  417. TEST(VirtualFileSystemTest, HiddenInIteration) {
  418. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  419. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  420. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  421. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  422. new vfs::OverlayFileSystem(Lower));
  423. O->pushOverlay(Middle);
  424. O->pushOverlay(Upper);
  425. std::error_code EC;
  426. Lower->addRegularFile("/onlyInLow", sys::fs::owner_read);
  427. Lower->addRegularFile("/hiddenByMid", sys::fs::owner_read);
  428. Lower->addRegularFile("/hiddenByUp", sys::fs::owner_read);
  429. Middle->addRegularFile("/onlyInMid", sys::fs::owner_write);
  430. Middle->addRegularFile("/hiddenByMid", sys::fs::owner_write);
  431. Middle->addRegularFile("/hiddenByUp", sys::fs::owner_write);
  432. Upper->addRegularFile("/onlyInUp", sys::fs::owner_all);
  433. Upper->addRegularFile("/hiddenByUp", sys::fs::owner_all);
  434. {
  435. const char *Contents[] = {"/hiddenByUp", "/onlyInUp", "/hiddenByMid",
  436. "/onlyInMid", "/onlyInLow"};
  437. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  438. }
  439. // Make sure we get the top-most entry
  440. {
  441. std::error_code EC;
  442. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  443. for ( ; !EC && I != E; I.increment(EC))
  444. if (I->getName() == "/hiddenByUp")
  445. break;
  446. ASSERT_NE(E, I);
  447. EXPECT_EQ(sys::fs::owner_all, I->getPermissions());
  448. }
  449. {
  450. std::error_code EC;
  451. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  452. for ( ; !EC && I != E; I.increment(EC))
  453. if (I->getName() == "/hiddenByMid")
  454. break;
  455. ASSERT_NE(E, I);
  456. EXPECT_EQ(sys::fs::owner_write, I->getPermissions());
  457. }
  458. }
  459. // NOTE: in the tests below, we use '//root/' as our root directory, since it is
  460. // a legal *absolute* path on Windows as well as *nix.
  461. class VFSFromYAMLTest : public ::testing::Test {
  462. public:
  463. int NumDiagnostics;
  464. void SetUp() {
  465. NumDiagnostics = 0;
  466. }
  467. static void CountingDiagHandler(const SMDiagnostic &, void *Context) {
  468. VFSFromYAMLTest *Test = static_cast<VFSFromYAMLTest *>(Context);
  469. ++Test->NumDiagnostics;
  470. }
  471. IntrusiveRefCntPtr<vfs::FileSystem>
  472. getFromYAMLRawString(StringRef Content,
  473. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS) {
  474. std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Content);
  475. return getVFSFromYAML(std::move(Buffer), CountingDiagHandler, this,
  476. ExternalFS);
  477. }
  478. IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLString(
  479. StringRef Content,
  480. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS = new DummyFileSystem()) {
  481. std::string VersionPlusContent("{\n 'version':0,\n");
  482. VersionPlusContent += Content.slice(Content.find('{') + 1, StringRef::npos);
  483. return getFromYAMLRawString(VersionPlusContent, ExternalFS);
  484. }
  485. };
  486. TEST_F(VFSFromYAMLTest, BasicVFSFromYAML) {
  487. IntrusiveRefCntPtr<vfs::FileSystem> FS;
  488. FS = getFromYAMLString("");
  489. EXPECT_EQ(nullptr, FS.get());
  490. FS = getFromYAMLString("[]");
  491. EXPECT_EQ(nullptr, FS.get());
  492. FS = getFromYAMLString("'string'");
  493. EXPECT_EQ(nullptr, FS.get());
  494. EXPECT_EQ(3, NumDiagnostics);
  495. }
  496. TEST_F(VFSFromYAMLTest, MappedFiles) {
  497. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  498. Lower->addRegularFile("//root/foo/bar/a");
  499. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  500. getFromYAMLString("{ 'roots': [\n"
  501. "{\n"
  502. " 'type': 'directory',\n"
  503. " 'name': '//root/',\n"
  504. " 'contents': [ {\n"
  505. " 'type': 'file',\n"
  506. " 'name': 'file1',\n"
  507. " 'external-contents': '//root/foo/bar/a'\n"
  508. " },\n"
  509. " {\n"
  510. " 'type': 'file',\n"
  511. " 'name': 'file2',\n"
  512. " 'external-contents': '//root/foo/b'\n"
  513. " }\n"
  514. " ]\n"
  515. "}\n"
  516. "]\n"
  517. "}",
  518. Lower);
  519. ASSERT_TRUE(FS.get() != nullptr);
  520. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  521. new vfs::OverlayFileSystem(Lower));
  522. O->pushOverlay(FS);
  523. // file
  524. ErrorOr<vfs::Status> S = O->status("//root/file1");
  525. ASSERT_FALSE(S.getError());
  526. EXPECT_EQ("//root/foo/bar/a", S->getName());
  527. ErrorOr<vfs::Status> SLower = O->status("//root/foo/bar/a");
  528. EXPECT_EQ("//root/foo/bar/a", SLower->getName());
  529. EXPECT_TRUE(S->equivalent(*SLower));
  530. // directory
  531. S = O->status("//root/");
  532. ASSERT_FALSE(S.getError());
  533. EXPECT_TRUE(S->isDirectory());
  534. EXPECT_TRUE(S->equivalent(*O->status("//root/"))); // non-volatile UniqueID
  535. // broken mapping
  536. EXPECT_EQ(O->status("//root/file2").getError(),
  537. llvm::errc::no_such_file_or_directory);
  538. EXPECT_EQ(0, NumDiagnostics);
  539. }
  540. TEST_F(VFSFromYAMLTest, CaseInsensitive) {
  541. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  542. Lower->addRegularFile("//root/foo/bar/a");
  543. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  544. getFromYAMLString("{ 'case-sensitive': 'false',\n"
  545. " 'roots': [\n"
  546. "{\n"
  547. " 'type': 'directory',\n"
  548. " 'name': '//root/',\n"
  549. " 'contents': [ {\n"
  550. " 'type': 'file',\n"
  551. " 'name': 'XX',\n"
  552. " 'external-contents': '//root/foo/bar/a'\n"
  553. " }\n"
  554. " ]\n"
  555. "}]}",
  556. Lower);
  557. ASSERT_TRUE(FS.get() != nullptr);
  558. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  559. new vfs::OverlayFileSystem(Lower));
  560. O->pushOverlay(FS);
  561. ErrorOr<vfs::Status> S = O->status("//root/XX");
  562. ASSERT_FALSE(S.getError());
  563. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  564. ASSERT_FALSE(SS.getError());
  565. EXPECT_TRUE(S->equivalent(*SS));
  566. SS = O->status("//root/xX");
  567. EXPECT_TRUE(S->equivalent(*SS));
  568. SS = O->status("//root/Xx");
  569. EXPECT_TRUE(S->equivalent(*SS));
  570. EXPECT_EQ(0, NumDiagnostics);
  571. }
  572. TEST_F(VFSFromYAMLTest, CaseSensitive) {
  573. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  574. Lower->addRegularFile("//root/foo/bar/a");
  575. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  576. getFromYAMLString("{ 'case-sensitive': 'true',\n"
  577. " 'roots': [\n"
  578. "{\n"
  579. " 'type': 'directory',\n"
  580. " 'name': '//root/',\n"
  581. " 'contents': [ {\n"
  582. " 'type': 'file',\n"
  583. " 'name': 'XX',\n"
  584. " 'external-contents': '//root/foo/bar/a'\n"
  585. " }\n"
  586. " ]\n"
  587. "}]}",
  588. Lower);
  589. ASSERT_TRUE(FS.get() != nullptr);
  590. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  591. new vfs::OverlayFileSystem(Lower));
  592. O->pushOverlay(FS);
  593. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  594. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  595. SS = O->status("//root/xX");
  596. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  597. SS = O->status("//root/Xx");
  598. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  599. EXPECT_EQ(0, NumDiagnostics);
  600. }
  601. TEST_F(VFSFromYAMLTest, IllegalVFSFile) {
  602. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  603. // invalid YAML at top-level
  604. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString("{]", Lower);
  605. EXPECT_EQ(nullptr, FS.get());
  606. // invalid YAML in roots
  607. FS = getFromYAMLString("{ 'roots':[}", Lower);
  608. // invalid YAML in directory
  609. FS = getFromYAMLString(
  610. "{ 'roots':[ { 'name': 'foo', 'type': 'directory', 'contents': [}",
  611. Lower);
  612. EXPECT_EQ(nullptr, FS.get());
  613. // invalid configuration
  614. FS = getFromYAMLString("{ 'knobular': 'true', 'roots':[] }", Lower);
  615. EXPECT_EQ(nullptr, FS.get());
  616. FS = getFromYAMLString("{ 'case-sensitive': 'maybe', 'roots':[] }", Lower);
  617. EXPECT_EQ(nullptr, FS.get());
  618. // invalid roots
  619. FS = getFromYAMLString("{ 'roots':'' }", Lower);
  620. EXPECT_EQ(nullptr, FS.get());
  621. FS = getFromYAMLString("{ 'roots':{} }", Lower);
  622. EXPECT_EQ(nullptr, FS.get());
  623. // invalid entries
  624. FS = getFromYAMLString(
  625. "{ 'roots':[ { 'type': 'other', 'name': 'me', 'contents': '' }", Lower);
  626. EXPECT_EQ(nullptr, FS.get());
  627. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': [], "
  628. "'external-contents': 'other' }",
  629. Lower);
  630. EXPECT_EQ(nullptr, FS.get());
  631. FS = getFromYAMLString(
  632. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': [] }",
  633. Lower);
  634. EXPECT_EQ(nullptr, FS.get());
  635. FS = getFromYAMLString(
  636. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': {} }",
  637. Lower);
  638. EXPECT_EQ(nullptr, FS.get());
  639. FS = getFromYAMLString(
  640. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': {} }",
  641. Lower);
  642. EXPECT_EQ(nullptr, FS.get());
  643. FS = getFromYAMLString(
  644. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': '' }",
  645. Lower);
  646. EXPECT_EQ(nullptr, FS.get());
  647. FS = getFromYAMLString(
  648. "{ 'roots':[ { 'thingy': 'directory', 'name': 'me', 'contents': [] }",
  649. Lower);
  650. EXPECT_EQ(nullptr, FS.get());
  651. // missing mandatory fields
  652. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': 'me' }", Lower);
  653. EXPECT_EQ(nullptr, FS.get());
  654. FS = getFromYAMLString(
  655. "{ 'roots':[ { 'type': 'file', 'external-contents': 'other' }", Lower);
  656. EXPECT_EQ(nullptr, FS.get());
  657. FS = getFromYAMLString("{ 'roots':[ { 'name': 'me', 'contents': [] }", Lower);
  658. EXPECT_EQ(nullptr, FS.get());
  659. // duplicate keys
  660. FS = getFromYAMLString("{ 'roots':[], 'roots':[] }", Lower);
  661. EXPECT_EQ(nullptr, FS.get());
  662. FS = getFromYAMLString(
  663. "{ 'case-sensitive':'true', 'case-sensitive':'true', 'roots':[] }",
  664. Lower);
  665. EXPECT_EQ(nullptr, FS.get());
  666. FS =
  667. getFromYAMLString("{ 'roots':[{'name':'me', 'name':'you', 'type':'file', "
  668. "'external-contents':'blah' } ] }",
  669. Lower);
  670. EXPECT_EQ(nullptr, FS.get());
  671. // missing version
  672. FS = getFromYAMLRawString("{ 'roots':[] }", Lower);
  673. EXPECT_EQ(nullptr, FS.get());
  674. // bad version number
  675. FS = getFromYAMLRawString("{ 'version':'foo', 'roots':[] }", Lower);
  676. EXPECT_EQ(nullptr, FS.get());
  677. FS = getFromYAMLRawString("{ 'version':-1, 'roots':[] }", Lower);
  678. EXPECT_EQ(nullptr, FS.get());
  679. FS = getFromYAMLRawString("{ 'version':100000, 'roots':[] }", Lower);
  680. EXPECT_EQ(nullptr, FS.get());
  681. EXPECT_EQ(24, NumDiagnostics);
  682. }
  683. TEST_F(VFSFromYAMLTest, UseExternalName) {
  684. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  685. Lower->addRegularFile("//root/external/file");
  686. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  687. "{ 'roots': [\n"
  688. " { 'type': 'file', 'name': '//root/A',\n"
  689. " 'external-contents': '//root/external/file'\n"
  690. " },\n"
  691. " { 'type': 'file', 'name': '//root/B',\n"
  692. " 'use-external-name': true,\n"
  693. " 'external-contents': '//root/external/file'\n"
  694. " },\n"
  695. " { 'type': 'file', 'name': '//root/C',\n"
  696. " 'use-external-name': false,\n"
  697. " 'external-contents': '//root/external/file'\n"
  698. " }\n"
  699. "] }", Lower);
  700. ASSERT_TRUE(nullptr != FS.get());
  701. // default true
  702. EXPECT_EQ("//root/external/file", FS->status("//root/A")->getName());
  703. // explicit
  704. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  705. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  706. // global configuration
  707. FS = getFromYAMLString(
  708. "{ 'use-external-names': false,\n"
  709. " 'roots': [\n"
  710. " { 'type': 'file', 'name': '//root/A',\n"
  711. " 'external-contents': '//root/external/file'\n"
  712. " },\n"
  713. " { 'type': 'file', 'name': '//root/B',\n"
  714. " 'use-external-name': true,\n"
  715. " 'external-contents': '//root/external/file'\n"
  716. " },\n"
  717. " { 'type': 'file', 'name': '//root/C',\n"
  718. " 'use-external-name': false,\n"
  719. " 'external-contents': '//root/external/file'\n"
  720. " }\n"
  721. "] }", Lower);
  722. ASSERT_TRUE(nullptr != FS.get());
  723. // default
  724. EXPECT_EQ("//root/A", FS->status("//root/A")->getName());
  725. // explicit
  726. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  727. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  728. }
  729. TEST_F(VFSFromYAMLTest, MultiComponentPath) {
  730. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  731. Lower->addRegularFile("//root/other");
  732. // file in roots
  733. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  734. "{ 'roots': [\n"
  735. " { 'type': 'file', 'name': '//root/path/to/file',\n"
  736. " 'external-contents': '//root/other' }]\n"
  737. "}", Lower);
  738. ASSERT_TRUE(nullptr != FS.get());
  739. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  740. EXPECT_FALSE(FS->status("//root/path/to").getError());
  741. EXPECT_FALSE(FS->status("//root/path").getError());
  742. EXPECT_FALSE(FS->status("//root/").getError());
  743. // at the start
  744. FS = getFromYAMLString(
  745. "{ 'roots': [\n"
  746. " { 'type': 'directory', 'name': '//root/path/to',\n"
  747. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  748. " 'external-contents': '//root/other' }]}]\n"
  749. "}", Lower);
  750. ASSERT_TRUE(nullptr != FS.get());
  751. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  752. EXPECT_FALSE(FS->status("//root/path/to").getError());
  753. EXPECT_FALSE(FS->status("//root/path").getError());
  754. EXPECT_FALSE(FS->status("//root/").getError());
  755. // at the end
  756. FS = getFromYAMLString(
  757. "{ 'roots': [\n"
  758. " { 'type': 'directory', 'name': '//root/',\n"
  759. " 'contents': [ { 'type': 'file', 'name': 'path/to/file',\n"
  760. " 'external-contents': '//root/other' }]}]\n"
  761. "}", Lower);
  762. ASSERT_TRUE(nullptr != FS.get());
  763. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  764. EXPECT_FALSE(FS->status("//root/path/to").getError());
  765. EXPECT_FALSE(FS->status("//root/path").getError());
  766. EXPECT_FALSE(FS->status("//root/").getError());
  767. }
  768. TEST_F(VFSFromYAMLTest, TrailingSlashes) {
  769. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  770. Lower->addRegularFile("//root/other");
  771. // file in roots
  772. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  773. "{ 'roots': [\n"
  774. " { 'type': 'directory', 'name': '//root/path/to////',\n"
  775. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  776. " 'external-contents': '//root/other' }]}]\n"
  777. "}", Lower);
  778. ASSERT_TRUE(nullptr != FS.get());
  779. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  780. EXPECT_FALSE(FS->status("//root/path/to").getError());
  781. EXPECT_FALSE(FS->status("//root/path").getError());
  782. EXPECT_FALSE(FS->status("//root/").getError());
  783. }
  784. TEST_F(VFSFromYAMLTest, DirectoryIteration) {
  785. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  786. Lower->addDirectory("//root/");
  787. Lower->addDirectory("//root/foo");
  788. Lower->addDirectory("//root/foo/bar");
  789. Lower->addRegularFile("//root/foo/bar/a");
  790. Lower->addRegularFile("//root/foo/bar/b");
  791. Lower->addRegularFile("//root/file3");
  792. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  793. getFromYAMLString("{ 'use-external-names': false,\n"
  794. " 'roots': [\n"
  795. "{\n"
  796. " 'type': 'directory',\n"
  797. " 'name': '//root/',\n"
  798. " 'contents': [ {\n"
  799. " 'type': 'file',\n"
  800. " 'name': 'file1',\n"
  801. " 'external-contents': '//root/foo/bar/a'\n"
  802. " },\n"
  803. " {\n"
  804. " 'type': 'file',\n"
  805. " 'name': 'file2',\n"
  806. " 'external-contents': '//root/foo/bar/b'\n"
  807. " }\n"
  808. " ]\n"
  809. "}\n"
  810. "]\n"
  811. "}",
  812. Lower);
  813. ASSERT_TRUE(FS.get() != NULL);
  814. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  815. new vfs::OverlayFileSystem(Lower));
  816. O->pushOverlay(FS);
  817. std::error_code EC;
  818. {
  819. const char *Contents[] = {"//root/file1", "//root/file2", "//root/file3",
  820. "//root/foo"};
  821. checkContents(O->dir_begin("//root/", EC), makeStringRefVector(Contents));
  822. }
  823. {
  824. const char *Contents[] = {"//root/foo/bar/a", "//root/foo/bar/b"};
  825. checkContents(O->dir_begin("//root/foo/bar", EC),
  826. makeStringRefVector(Contents));
  827. }
  828. }