VirtualFileSystemTest.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Content);
  475. return getVFSFromYAML(Buffer, CountingDiagHandler, this, ExternalFS);
  476. }
  477. IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLString(
  478. StringRef Content,
  479. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS = new DummyFileSystem()) {
  480. std::string VersionPlusContent("{\n 'version':0,\n");
  481. VersionPlusContent += Content.slice(Content.find('{') + 1, StringRef::npos);
  482. return getFromYAMLRawString(VersionPlusContent, ExternalFS);
  483. }
  484. };
  485. TEST_F(VFSFromYAMLTest, BasicVFSFromYAML) {
  486. IntrusiveRefCntPtr<vfs::FileSystem> FS;
  487. FS = getFromYAMLString("");
  488. EXPECT_EQ(nullptr, FS.getPtr());
  489. FS = getFromYAMLString("[]");
  490. EXPECT_EQ(nullptr, FS.getPtr());
  491. FS = getFromYAMLString("'string'");
  492. EXPECT_EQ(nullptr, FS.getPtr());
  493. EXPECT_EQ(3, NumDiagnostics);
  494. }
  495. TEST_F(VFSFromYAMLTest, MappedFiles) {
  496. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  497. Lower->addRegularFile("//root/foo/bar/a");
  498. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  499. getFromYAMLString("{ 'roots': [\n"
  500. "{\n"
  501. " 'type': 'directory',\n"
  502. " 'name': '//root/',\n"
  503. " 'contents': [ {\n"
  504. " 'type': 'file',\n"
  505. " 'name': 'file1',\n"
  506. " 'external-contents': '//root/foo/bar/a'\n"
  507. " },\n"
  508. " {\n"
  509. " 'type': 'file',\n"
  510. " 'name': 'file2',\n"
  511. " 'external-contents': '//root/foo/b'\n"
  512. " }\n"
  513. " ]\n"
  514. "}\n"
  515. "]\n"
  516. "}",
  517. Lower);
  518. ASSERT_TRUE(FS.getPtr() != nullptr);
  519. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  520. new vfs::OverlayFileSystem(Lower));
  521. O->pushOverlay(FS);
  522. // file
  523. ErrorOr<vfs::Status> S = O->status("//root/file1");
  524. ASSERT_FALSE(S.getError());
  525. EXPECT_EQ("//root/foo/bar/a", S->getName());
  526. ErrorOr<vfs::Status> SLower = O->status("//root/foo/bar/a");
  527. EXPECT_EQ("//root/foo/bar/a", SLower->getName());
  528. EXPECT_TRUE(S->equivalent(*SLower));
  529. // directory
  530. S = O->status("//root/");
  531. ASSERT_FALSE(S.getError());
  532. EXPECT_TRUE(S->isDirectory());
  533. EXPECT_TRUE(S->equivalent(*O->status("//root/"))); // non-volatile UniqueID
  534. // broken mapping
  535. EXPECT_EQ(O->status("//root/file2").getError(),
  536. llvm::errc::no_such_file_or_directory);
  537. EXPECT_EQ(0, NumDiagnostics);
  538. }
  539. TEST_F(VFSFromYAMLTest, CaseInsensitive) {
  540. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  541. Lower->addRegularFile("//root/foo/bar/a");
  542. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  543. getFromYAMLString("{ 'case-sensitive': 'false',\n"
  544. " 'roots': [\n"
  545. "{\n"
  546. " 'type': 'directory',\n"
  547. " 'name': '//root/',\n"
  548. " 'contents': [ {\n"
  549. " 'type': 'file',\n"
  550. " 'name': 'XX',\n"
  551. " 'external-contents': '//root/foo/bar/a'\n"
  552. " }\n"
  553. " ]\n"
  554. "}]}",
  555. Lower);
  556. ASSERT_TRUE(FS.getPtr() != nullptr);
  557. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  558. new vfs::OverlayFileSystem(Lower));
  559. O->pushOverlay(FS);
  560. ErrorOr<vfs::Status> S = O->status("//root/XX");
  561. ASSERT_FALSE(S.getError());
  562. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  563. ASSERT_FALSE(SS.getError());
  564. EXPECT_TRUE(S->equivalent(*SS));
  565. SS = O->status("//root/xX");
  566. EXPECT_TRUE(S->equivalent(*SS));
  567. SS = O->status("//root/Xx");
  568. EXPECT_TRUE(S->equivalent(*SS));
  569. EXPECT_EQ(0, NumDiagnostics);
  570. }
  571. TEST_F(VFSFromYAMLTest, CaseSensitive) {
  572. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  573. Lower->addRegularFile("//root/foo/bar/a");
  574. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  575. getFromYAMLString("{ 'case-sensitive': 'true',\n"
  576. " 'roots': [\n"
  577. "{\n"
  578. " 'type': 'directory',\n"
  579. " 'name': '//root/',\n"
  580. " 'contents': [ {\n"
  581. " 'type': 'file',\n"
  582. " 'name': 'XX',\n"
  583. " 'external-contents': '//root/foo/bar/a'\n"
  584. " }\n"
  585. " ]\n"
  586. "}]}",
  587. Lower);
  588. ASSERT_TRUE(FS.getPtr() != nullptr);
  589. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  590. new vfs::OverlayFileSystem(Lower));
  591. O->pushOverlay(FS);
  592. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  593. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  594. SS = O->status("//root/xX");
  595. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  596. SS = O->status("//root/Xx");
  597. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  598. EXPECT_EQ(0, NumDiagnostics);
  599. }
  600. TEST_F(VFSFromYAMLTest, IllegalVFSFile) {
  601. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  602. // invalid YAML at top-level
  603. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString("{]", Lower);
  604. EXPECT_EQ(nullptr, FS.getPtr());
  605. // invalid YAML in roots
  606. FS = getFromYAMLString("{ 'roots':[}", Lower);
  607. // invalid YAML in directory
  608. FS = getFromYAMLString(
  609. "{ 'roots':[ { 'name': 'foo', 'type': 'directory', 'contents': [}",
  610. Lower);
  611. EXPECT_EQ(nullptr, FS.getPtr());
  612. // invalid configuration
  613. FS = getFromYAMLString("{ 'knobular': 'true', 'roots':[] }", Lower);
  614. EXPECT_EQ(nullptr, FS.getPtr());
  615. FS = getFromYAMLString("{ 'case-sensitive': 'maybe', 'roots':[] }", Lower);
  616. EXPECT_EQ(nullptr, FS.getPtr());
  617. // invalid roots
  618. FS = getFromYAMLString("{ 'roots':'' }", Lower);
  619. EXPECT_EQ(nullptr, FS.getPtr());
  620. FS = getFromYAMLString("{ 'roots':{} }", Lower);
  621. EXPECT_EQ(nullptr, FS.getPtr());
  622. // invalid entries
  623. FS = getFromYAMLString(
  624. "{ 'roots':[ { 'type': 'other', 'name': 'me', 'contents': '' }", Lower);
  625. EXPECT_EQ(nullptr, FS.getPtr());
  626. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': [], "
  627. "'external-contents': 'other' }",
  628. Lower);
  629. EXPECT_EQ(nullptr, FS.getPtr());
  630. FS = getFromYAMLString(
  631. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': [] }",
  632. Lower);
  633. EXPECT_EQ(nullptr, FS.getPtr());
  634. FS = getFromYAMLString(
  635. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': {} }",
  636. Lower);
  637. EXPECT_EQ(nullptr, FS.getPtr());
  638. FS = getFromYAMLString(
  639. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': {} }",
  640. Lower);
  641. EXPECT_EQ(nullptr, FS.getPtr());
  642. FS = getFromYAMLString(
  643. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': '' }",
  644. Lower);
  645. EXPECT_EQ(nullptr, FS.getPtr());
  646. FS = getFromYAMLString(
  647. "{ 'roots':[ { 'thingy': 'directory', 'name': 'me', 'contents': [] }",
  648. Lower);
  649. EXPECT_EQ(nullptr, FS.getPtr());
  650. // missing mandatory fields
  651. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': 'me' }", Lower);
  652. EXPECT_EQ(nullptr, FS.getPtr());
  653. FS = getFromYAMLString(
  654. "{ 'roots':[ { 'type': 'file', 'external-contents': 'other' }", Lower);
  655. EXPECT_EQ(nullptr, FS.getPtr());
  656. FS = getFromYAMLString("{ 'roots':[ { 'name': 'me', 'contents': [] }", Lower);
  657. EXPECT_EQ(nullptr, FS.getPtr());
  658. // duplicate keys
  659. FS = getFromYAMLString("{ 'roots':[], 'roots':[] }", Lower);
  660. EXPECT_EQ(nullptr, FS.getPtr());
  661. FS = getFromYAMLString(
  662. "{ 'case-sensitive':'true', 'case-sensitive':'true', 'roots':[] }",
  663. Lower);
  664. EXPECT_EQ(nullptr, FS.getPtr());
  665. FS =
  666. getFromYAMLString("{ 'roots':[{'name':'me', 'name':'you', 'type':'file', "
  667. "'external-contents':'blah' } ] }",
  668. Lower);
  669. EXPECT_EQ(nullptr, FS.getPtr());
  670. // missing version
  671. FS = getFromYAMLRawString("{ 'roots':[] }", Lower);
  672. EXPECT_EQ(nullptr, FS.getPtr());
  673. // bad version number
  674. FS = getFromYAMLRawString("{ 'version':'foo', 'roots':[] }", Lower);
  675. EXPECT_EQ(nullptr, FS.getPtr());
  676. FS = getFromYAMLRawString("{ 'version':-1, 'roots':[] }", Lower);
  677. EXPECT_EQ(nullptr, FS.getPtr());
  678. FS = getFromYAMLRawString("{ 'version':100000, 'roots':[] }", Lower);
  679. EXPECT_EQ(nullptr, FS.getPtr());
  680. EXPECT_EQ(24, NumDiagnostics);
  681. }
  682. TEST_F(VFSFromYAMLTest, UseExternalName) {
  683. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  684. Lower->addRegularFile("//root/external/file");
  685. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  686. "{ 'roots': [\n"
  687. " { 'type': 'file', 'name': '//root/A',\n"
  688. " 'external-contents': '//root/external/file'\n"
  689. " },\n"
  690. " { 'type': 'file', 'name': '//root/B',\n"
  691. " 'use-external-name': true,\n"
  692. " 'external-contents': '//root/external/file'\n"
  693. " },\n"
  694. " { 'type': 'file', 'name': '//root/C',\n"
  695. " 'use-external-name': false,\n"
  696. " 'external-contents': '//root/external/file'\n"
  697. " }\n"
  698. "] }", Lower);
  699. ASSERT_TRUE(nullptr != FS.getPtr());
  700. // default true
  701. EXPECT_EQ("//root/external/file", FS->status("//root/A")->getName());
  702. // explicit
  703. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  704. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  705. // global configuration
  706. FS = getFromYAMLString(
  707. "{ 'use-external-names': false,\n"
  708. " 'roots': [\n"
  709. " { 'type': 'file', 'name': '//root/A',\n"
  710. " 'external-contents': '//root/external/file'\n"
  711. " },\n"
  712. " { 'type': 'file', 'name': '//root/B',\n"
  713. " 'use-external-name': true,\n"
  714. " 'external-contents': '//root/external/file'\n"
  715. " },\n"
  716. " { 'type': 'file', 'name': '//root/C',\n"
  717. " 'use-external-name': false,\n"
  718. " 'external-contents': '//root/external/file'\n"
  719. " }\n"
  720. "] }", Lower);
  721. ASSERT_TRUE(nullptr != FS.getPtr());
  722. // default
  723. EXPECT_EQ("//root/A", FS->status("//root/A")->getName());
  724. // explicit
  725. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  726. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  727. }
  728. TEST_F(VFSFromYAMLTest, MultiComponentPath) {
  729. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  730. Lower->addRegularFile("//root/other");
  731. // file in roots
  732. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  733. "{ 'roots': [\n"
  734. " { 'type': 'file', 'name': '//root/path/to/file',\n"
  735. " 'external-contents': '//root/other' }]\n"
  736. "}", Lower);
  737. ASSERT_TRUE(nullptr != FS.getPtr());
  738. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  739. EXPECT_FALSE(FS->status("//root/path/to").getError());
  740. EXPECT_FALSE(FS->status("//root/path").getError());
  741. EXPECT_FALSE(FS->status("//root/").getError());
  742. // at the start
  743. FS = getFromYAMLString(
  744. "{ 'roots': [\n"
  745. " { 'type': 'directory', 'name': '//root/path/to',\n"
  746. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  747. " 'external-contents': '//root/other' }]}]\n"
  748. "}", Lower);
  749. ASSERT_TRUE(nullptr != FS.getPtr());
  750. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  751. EXPECT_FALSE(FS->status("//root/path/to").getError());
  752. EXPECT_FALSE(FS->status("//root/path").getError());
  753. EXPECT_FALSE(FS->status("//root/").getError());
  754. // at the end
  755. FS = getFromYAMLString(
  756. "{ 'roots': [\n"
  757. " { 'type': 'directory', 'name': '//root/',\n"
  758. " 'contents': [ { 'type': 'file', 'name': 'path/to/file',\n"
  759. " 'external-contents': '//root/other' }]}]\n"
  760. "}", Lower);
  761. ASSERT_TRUE(nullptr != FS.getPtr());
  762. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  763. EXPECT_FALSE(FS->status("//root/path/to").getError());
  764. EXPECT_FALSE(FS->status("//root/path").getError());
  765. EXPECT_FALSE(FS->status("//root/").getError());
  766. }
  767. TEST_F(VFSFromYAMLTest, TrailingSlashes) {
  768. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  769. Lower->addRegularFile("//root/other");
  770. // file in roots
  771. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  772. "{ 'roots': [\n"
  773. " { 'type': 'directory', 'name': '//root/path/to////',\n"
  774. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  775. " 'external-contents': '//root/other' }]}]\n"
  776. "}", Lower);
  777. ASSERT_TRUE(nullptr != FS.getPtr());
  778. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  779. EXPECT_FALSE(FS->status("//root/path/to").getError());
  780. EXPECT_FALSE(FS->status("//root/path").getError());
  781. EXPECT_FALSE(FS->status("//root/").getError());
  782. }
  783. TEST_F(VFSFromYAMLTest, DirectoryIteration) {
  784. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  785. Lower->addDirectory("//root/");
  786. Lower->addDirectory("//root/foo");
  787. Lower->addDirectory("//root/foo/bar");
  788. Lower->addRegularFile("//root/foo/bar/a");
  789. Lower->addRegularFile("//root/foo/bar/b");
  790. Lower->addRegularFile("//root/file3");
  791. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  792. getFromYAMLString("{ 'use-external-names': false,\n"
  793. " 'roots': [\n"
  794. "{\n"
  795. " 'type': 'directory',\n"
  796. " 'name': '//root/',\n"
  797. " 'contents': [ {\n"
  798. " 'type': 'file',\n"
  799. " 'name': 'file1',\n"
  800. " 'external-contents': '//root/foo/bar/a'\n"
  801. " },\n"
  802. " {\n"
  803. " 'type': 'file',\n"
  804. " 'name': 'file2',\n"
  805. " 'external-contents': '//root/foo/bar/b'\n"
  806. " }\n"
  807. " ]\n"
  808. "}\n"
  809. "]\n"
  810. "}",
  811. Lower);
  812. ASSERT_TRUE(FS.getPtr() != NULL);
  813. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  814. new vfs::OverlayFileSystem(Lower));
  815. O->pushOverlay(FS);
  816. std::error_code EC;
  817. {
  818. const char *Contents[] = {"//root/file1", "//root/file2", "//root/file3",
  819. "//root/foo"};
  820. checkContents(O->dir_begin("//root/", EC), makeStringRefVector(Contents));
  821. }
  822. {
  823. const char *Contents[] = {"//root/foo/bar/a", "//root/foo/bar/b"};
  824. checkContents(O->dir_begin("//root/foo/bar", EC),
  825. makeStringRefVector(Contents));
  826. }
  827. }