1 //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Basic/FileManager.h"
10 #include "clang/Basic/FileSystemOptions.h"
11 #include "clang/Basic/FileSystemStatCache.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/VirtualFileSystem.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 using namespace clang;
19 
20 namespace {
21 
22 // Used to create a fake file system for running the tests with such
23 // that the tests are not affected by the structure/contents of the
24 // file system on the machine running the tests.
25 class FakeStatCache : public FileSystemStatCache {
26 private:
27   // Maps a file/directory path to its desired stat result.  Anything
28   // not in this map is considered to not exist in the file system.
29   llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls;
30 
31   void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile,
32                              const char *StatPath) {
33 #ifndef _WIN32
34     SmallString<128> NormalizedPath(Path);
35     llvm::sys::path::native(NormalizedPath);
36     Path = NormalizedPath.c_str();
37 
38     SmallString<128> NormalizedStatPath;
39     if (StatPath) {
40       NormalizedStatPath = StatPath;
41       llvm::sys::path::native(NormalizedStatPath);
42       StatPath = NormalizedStatPath.c_str();
43     }
44 #endif
45 
46     if (!StatPath)
47       StatPath = Path;
48 
49     auto fileType = IsFile ?
50       llvm::sys::fs::file_type::regular_file :
51       llvm::sys::fs::file_type::directory_file;
52     llvm::vfs::Status Status(StatPath, llvm::sys::fs::UniqueID(1, INode),
53                              /*MTime*/{}, /*User*/0, /*Group*/0,
54                              /*Size*/0, fileType,
55                              llvm::sys::fs::perms::all_all);
56     StatCalls[Path] = Status;
57   }
58 
59 public:
60   // Inject a file with the given inode value to the fake file system.
61   void InjectFile(const char *Path, ino_t INode,
62                   const char *StatPath = nullptr) {
63     InjectFileOrDirectory(Path, INode, /*IsFile=*/true, StatPath);
64   }
65 
66   // Inject a directory with the given inode value to the fake file system.
67   void InjectDirectory(const char *Path, ino_t INode) {
68     InjectFileOrDirectory(Path, INode, /*IsFile=*/false, nullptr);
69   }
70 
71   // Implement FileSystemStatCache::getStat().
72   std::error_code getStat(StringRef Path, llvm::vfs::Status &Status,
73                           bool isFile,
74                           std::unique_ptr<llvm::vfs::File> *F,
75                           llvm::vfs::FileSystem &FS) override {
76 #ifndef _WIN32
77     SmallString<128> NormalizedPath(Path);
78     llvm::sys::path::native(NormalizedPath);
79     Path = NormalizedPath.c_str();
80 #endif
81 
82     if (StatCalls.count(Path) != 0) {
83       Status = StatCalls[Path];
84       return std::error_code();
85     }
86 
87     return std::make_error_code(std::errc::no_such_file_or_directory);
88   }
89 };
90 
91 // The test fixture.
92 class FileManagerTest : public ::testing::Test {
93  protected:
94   FileManagerTest() : manager(options) {
95   }
96 
97   FileSystemOptions options;
98   FileManager manager;
99 };
100 
101 // When a virtual file is added, its getDir() field is set correctly
102 // (not NULL, correct name).
103 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
104   const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
105   ASSERT_TRUE(file != nullptr);
106 
107   const DirectoryEntry *dir = file->getDir();
108   ASSERT_TRUE(dir != nullptr);
109   EXPECT_EQ(".", dir->getName());
110 
111   file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
112   ASSERT_TRUE(file != nullptr);
113 
114   dir = file->getDir();
115   ASSERT_TRUE(dir != nullptr);
116   EXPECT_EQ("x/y", dir->getName());
117 }
118 
119 // Before any virtual file is added, no virtual directory exists.
120 TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
121   // An empty FakeStatCache causes all stat calls made by the
122   // FileManager to report "file/directory doesn't exist".  This
123   // avoids the possibility of the result of this test being affected
124   // by what's in the real file system.
125   manager.setStatCache(std::make_unique<FakeStatCache>());
126 
127   ASSERT_FALSE(manager.getDirectory("virtual/dir/foo"));
128   ASSERT_FALSE(manager.getDirectory("virtual/dir"));
129   ASSERT_FALSE(manager.getDirectory("virtual"));
130 }
131 
132 // When a virtual file is added, all of its ancestors should be created.
133 TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
134   // Fake an empty real file system.
135   manager.setStatCache(std::make_unique<FakeStatCache>());
136 
137   manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
138   ASSERT_FALSE(manager.getDirectory("virtual/dir/foo"));
139 
140   auto dir = manager.getDirectory("virtual/dir");
141   ASSERT_TRUE(dir);
142   EXPECT_EQ("virtual/dir", (*dir)->getName());
143 
144   dir = manager.getDirectory("virtual");
145   ASSERT_TRUE(dir);
146   EXPECT_EQ("virtual", (*dir)->getName());
147 }
148 
149 // getFile() returns non-NULL if a real file exists at the given path.
150 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
151   // Inject fake files into the file system.
152   auto statCache = std::make_unique<FakeStatCache>();
153   statCache->InjectDirectory("/tmp", 42);
154   statCache->InjectFile("/tmp/test", 43);
155 
156 #ifdef _WIN32
157   const char *DirName = "C:.";
158   const char *FileName = "C:test";
159   statCache->InjectDirectory(DirName, 44);
160   statCache->InjectFile(FileName, 45);
161 #endif
162 
163   manager.setStatCache(std::move(statCache));
164 
165   auto file = manager.getFile("/tmp/test");
166   ASSERT_TRUE(file);
167   ASSERT_TRUE((*file)->isValid());
168   EXPECT_EQ("/tmp/test", (*file)->getName());
169 
170   const DirectoryEntry *dir = (*file)->getDir();
171   ASSERT_TRUE(dir != nullptr);
172   EXPECT_EQ("/tmp", dir->getName());
173 
174 #ifdef _WIN32
175   file = manager.getFile(FileName);
176   ASSERT_TRUE(file);
177 
178   dir = (*file)->getDir();
179   ASSERT_TRUE(dir != NULL);
180   EXPECT_EQ(DirName, dir->getName());
181 #endif
182 }
183 
184 // getFile() returns non-NULL if a virtual file exists at the given path.
185 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
186   // Fake an empty real file system.
187   manager.setStatCache(std::make_unique<FakeStatCache>());
188 
189   manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
190   auto file = manager.getFile("virtual/dir/bar.h");
191   ASSERT_TRUE(file);
192   ASSERT_TRUE((*file)->isValid());
193   EXPECT_EQ("virtual/dir/bar.h", (*file)->getName());
194 
195   const DirectoryEntry *dir = (*file)->getDir();
196   ASSERT_TRUE(dir != nullptr);
197   EXPECT_EQ("virtual/dir", dir->getName());
198 }
199 
200 // getFile() returns different FileEntries for different paths when
201 // there's no aliasing.
202 TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
203   // Inject two fake files into the file system.  Different inodes
204   // mean the files are not symlinked together.
205   auto statCache = std::make_unique<FakeStatCache>();
206   statCache->InjectDirectory(".", 41);
207   statCache->InjectFile("foo.cpp", 42);
208   statCache->InjectFile("bar.cpp", 43);
209   manager.setStatCache(std::move(statCache));
210 
211   auto fileFoo = manager.getFile("foo.cpp");
212   auto fileBar = manager.getFile("bar.cpp");
213   ASSERT_TRUE(fileFoo);
214   ASSERT_TRUE((*fileFoo)->isValid());
215   ASSERT_TRUE(fileBar);
216   ASSERT_TRUE((*fileBar)->isValid());
217   EXPECT_NE(*fileFoo, *fileBar);
218 }
219 
220 // getFile() returns an error if neither a real file nor a virtual file
221 // exists at the given path.
222 TEST_F(FileManagerTest, getFileReturnsErrorForNonexistentFile) {
223   // Inject a fake foo.cpp into the file system.
224   auto statCache = std::make_unique<FakeStatCache>();
225   statCache->InjectDirectory(".", 41);
226   statCache->InjectFile("foo.cpp", 42);
227   statCache->InjectDirectory("MyDirectory", 49);
228   manager.setStatCache(std::move(statCache));
229 
230   // Create a virtual bar.cpp file.
231   manager.getVirtualFile("bar.cpp", 200, 0);
232 
233   auto file = manager.getFile("xyz.txt");
234   ASSERT_FALSE(file);
235   ASSERT_EQ(file.getError(), std::errc::no_such_file_or_directory);
236 
237   auto readingDirAsFile = manager.getFile("MyDirectory");
238   ASSERT_FALSE(readingDirAsFile);
239   ASSERT_EQ(readingDirAsFile.getError(), std::errc::is_a_directory);
240 
241   auto readingFileAsDir = manager.getDirectory("foo.cpp");
242   ASSERT_FALSE(readingFileAsDir);
243   ASSERT_EQ(readingFileAsDir.getError(), std::errc::not_a_directory);
244 }
245 
246 // The following tests apply to Unix-like system only.
247 
248 #ifndef _WIN32
249 
250 // getFile() returns the same FileEntry for real files that are aliases.
251 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
252   // Inject two real files with the same inode.
253   auto statCache = std::make_unique<FakeStatCache>();
254   statCache->InjectDirectory("abc", 41);
255   statCache->InjectFile("abc/foo.cpp", 42);
256   statCache->InjectFile("abc/bar.cpp", 42);
257   manager.setStatCache(std::move(statCache));
258 
259   auto f1 = manager.getFile("abc/foo.cpp");
260   auto f2 = manager.getFile("abc/bar.cpp");
261 
262   EXPECT_EQ(f1 ? *f1 : nullptr,
263             f2 ? *f2 : nullptr);
264 
265   // Check that getFileRef also does the right thing.
266   auto r1 = manager.getFileRef("abc/foo.cpp");
267   auto r2 = manager.getFileRef("abc/bar.cpp");
268   ASSERT_FALSE(!r1);
269   ASSERT_FALSE(!r2);
270 
271   EXPECT_EQ("abc/foo.cpp", r1->getName());
272   EXPECT_EQ("abc/bar.cpp", r2->getName());
273   EXPECT_EQ((f1 ? *f1 : nullptr), &r1->getFileEntry());
274   EXPECT_EQ((f2 ? *f2 : nullptr), &r2->getFileEntry());
275 }
276 
277 TEST_F(FileManagerTest, getFileRefReturnsCorrectNameForDifferentStatPath) {
278   // Inject files with the same inode, but where some files have a stat that
279   // gives a different name. This is adding coverage for weird stat behaviour
280   // triggered by the RedirectingFileSystem that FileManager::getFileRef has
281   // special logic for.
282   auto StatCache = std::make_unique<FakeStatCache>();
283   StatCache->InjectDirectory("dir", 40);
284   StatCache->InjectFile("dir/f1.cpp", 41);
285   StatCache->InjectFile("dir/f1-alias.cpp", 41, "dir/f1.cpp");
286   StatCache->InjectFile("dir/f2.cpp", 42);
287   StatCache->InjectFile("dir/f2-alias.cpp", 42, "dir/f2.cpp");
288 
289   // This unintuitive rename-the-file-on-stat behaviour supports how the
290   // RedirectingFileSystem VFS layer responds to stats. However, even if you
291   // have two layers, you should only get a single filename back. As such the
292   // following stat cache behaviour is not supported (the correct stat entry
293   // for a double-redirection would be "dir/f1.cpp") and the getFileRef below
294   // should assert.
295   StatCache->InjectFile("dir/f1-alias-alias.cpp", 41, "dir/f1-alias.cpp");
296 
297   manager.setStatCache(std::move(StatCache));
298 
299   // With F1, test accessing the non-redirected name first.
300   auto F1 = manager.getFileRef("dir/f1.cpp");
301   auto F1Alias = manager.getFileRef("dir/f1-alias.cpp");
302   auto F1Alias2 = manager.getFileRef("dir/f1-alias.cpp");
303   ASSERT_FALSE(!F1);
304   ASSERT_FALSE(!F1Alias);
305   ASSERT_FALSE(!F1Alias2);
306   EXPECT_EQ("dir/f1.cpp", F1->getName());
307   EXPECT_EQ("dir/f1.cpp", F1->getFileEntry().getName());
308   EXPECT_EQ("dir/f1.cpp", F1Alias->getName());
309   EXPECT_EQ("dir/f1.cpp", F1Alias2->getName());
310   EXPECT_EQ(&F1->getFileEntry(), &F1Alias->getFileEntry());
311   EXPECT_EQ(&F1->getFileEntry(), &F1Alias2->getFileEntry());
312 
313 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
314   EXPECT_DEATH((void)manager.getFileRef("dir/f1-alias-alias.cpp"),
315                "filename redirected to a non-canonical filename?");
316 #endif
317 
318   // With F2, test accessing the redirected name first.
319   auto F2Alias = manager.getFileRef("dir/f2-alias.cpp");
320   auto F2 = manager.getFileRef("dir/f2.cpp");
321   auto F2Alias2 = manager.getFileRef("dir/f2-alias.cpp");
322   ASSERT_FALSE(!F2);
323   ASSERT_FALSE(!F2Alias);
324   ASSERT_FALSE(!F2Alias2);
325   EXPECT_EQ("dir/f2.cpp", F2->getName());
326   EXPECT_EQ("dir/f2.cpp", F2->getFileEntry().getName());
327   EXPECT_EQ("dir/f2.cpp", F2Alias->getName());
328   EXPECT_EQ("dir/f2.cpp", F2Alias2->getName());
329   EXPECT_EQ(&F2->getFileEntry(), &F2Alias->getFileEntry());
330   EXPECT_EQ(&F2->getFileEntry(), &F2Alias2->getFileEntry());
331 }
332 
333 // getFile() returns the same FileEntry for virtual files that have
334 // corresponding real files that are aliases.
335 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
336   // Inject two real files with the same inode.
337   auto statCache = std::make_unique<FakeStatCache>();
338   statCache->InjectDirectory("abc", 41);
339   statCache->InjectFile("abc/foo.cpp", 42);
340   statCache->InjectFile("abc/bar.cpp", 42);
341   manager.setStatCache(std::move(statCache));
342 
343   ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid());
344   ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid());
345 
346   auto f1 = manager.getFile("abc/foo.cpp");
347   auto f2 = manager.getFile("abc/bar.cpp");
348 
349   EXPECT_EQ(f1 ? *f1 : nullptr,
350             f2 ? *f2 : nullptr);
351 }
352 
353 // getFile() Should return the same entry as getVirtualFile if the file actually
354 // is a virtual file, even if the name is not exactly the same (but is after
355 // normalisation done by the file system, like on Windows). This can be checked
356 // here by checking the size.
357 TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
358   // Inject fake files into the file system.
359   auto statCache = std::make_unique<FakeStatCache>();
360   statCache->InjectDirectory("c:\\tmp", 42);
361   statCache->InjectFile("c:\\tmp\\test", 43);
362 
363   manager.setStatCache(std::move(statCache));
364 
365   // Inject the virtual file:
366   const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1);
367   ASSERT_TRUE(file1 != nullptr);
368   ASSERT_TRUE(file1->isValid());
369   EXPECT_EQ(43U, file1->getUniqueID().getFile());
370   EXPECT_EQ(123, file1->getSize());
371 
372   // Lookup the virtual file with a different name:
373   auto file2 = manager.getFile("c:/tmp/test", 100, 1);
374   ASSERT_TRUE(file2);
375   ASSERT_TRUE((*file2)->isValid());
376   // Check that it's the same UFE:
377   EXPECT_EQ(file1, *file2);
378   EXPECT_EQ(43U, (*file2)->getUniqueID().getFile());
379   // Check that the contents of the UFE are not overwritten by the entry in the
380   // filesystem:
381   EXPECT_EQ(123, (*file2)->getSize());
382 }
383 
384 #endif  // !_WIN32
385 
386 TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
387   SmallString<64> CustomWorkingDir;
388 #ifdef _WIN32
389   CustomWorkingDir = "C:";
390 #else
391   CustomWorkingDir = "/";
392 #endif
393   llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path");
394 
395   auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
396       new llvm::vfs::InMemoryFileSystem);
397   // setCurrentworkingdirectory must finish without error.
398   ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
399 
400   FileSystemOptions Opts;
401   FileManager Manager(Opts, FS);
402 
403   SmallString<64> Path("a/foo.cpp");
404 
405   SmallString<64> ExpectedResult(CustomWorkingDir);
406   llvm::sys::path::append(ExpectedResult, Path);
407 
408   ASSERT_TRUE(Manager.makeAbsolutePath(Path));
409   EXPECT_EQ(Path, ExpectedResult);
410 }
411 
412 // getVirtualFile should always fill the real path.
413 TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
414   SmallString<64> CustomWorkingDir;
415 #ifdef _WIN32
416   CustomWorkingDir = "C:/";
417 #else
418   CustomWorkingDir = "/";
419 #endif
420 
421   auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
422       new llvm::vfs::InMemoryFileSystem);
423   // setCurrentworkingdirectory must finish without error.
424   ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
425 
426   FileSystemOptions Opts;
427   FileManager Manager(Opts, FS);
428 
429   // Inject fake files into the file system.
430   auto statCache = std::make_unique<FakeStatCache>();
431   statCache->InjectDirectory("/tmp", 42);
432   statCache->InjectFile("/tmp/test", 43);
433 
434   Manager.setStatCache(std::move(statCache));
435 
436   // Check for real path.
437   const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1);
438   ASSERT_TRUE(file != nullptr);
439   ASSERT_TRUE(file->isValid());
440   SmallString<64> ExpectedResult = CustomWorkingDir;
441 
442   llvm::sys::path::append(ExpectedResult, "tmp", "test");
443   EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult);
444 }
445 
446 TEST_F(FileManagerTest, getFileDontOpenRealPath) {
447   SmallString<64> CustomWorkingDir;
448 #ifdef _WIN32
449   CustomWorkingDir = "C:/";
450 #else
451   CustomWorkingDir = "/";
452 #endif
453 
454   auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
455       new llvm::vfs::InMemoryFileSystem);
456   // setCurrentworkingdirectory must finish without error.
457   ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
458 
459   FileSystemOptions Opts;
460   FileManager Manager(Opts, FS);
461 
462   // Inject fake files into the file system.
463   auto statCache = std::make_unique<FakeStatCache>();
464   statCache->InjectDirectory("/tmp", 42);
465   statCache->InjectFile("/tmp/test", 43);
466 
467   Manager.setStatCache(std::move(statCache));
468 
469   // Check for real path.
470   auto file = Manager.getFile("/tmp/test", /*OpenFile=*/false);
471   ASSERT_TRUE(file);
472   ASSERT_TRUE((*file)->isValid());
473   SmallString<64> ExpectedResult = CustomWorkingDir;
474 
475   llvm::sys::path::append(ExpectedResult, "tmp", "test");
476   EXPECT_EQ((*file)->tryGetRealPathName(), ExpectedResult);
477 }
478 
479 TEST_F(FileManagerTest, getBypassFile) {
480   SmallString<64> CustomWorkingDir;
481 #ifdef _WIN32
482   CustomWorkingDir = "C:/";
483 #else
484   CustomWorkingDir = "/";
485 #endif
486 
487   auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
488       new llvm::vfs::InMemoryFileSystem);
489   // setCurrentworkingdirectory must finish without error.
490   ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
491 
492   FileSystemOptions Opts;
493   FileManager Manager(Opts, FS);
494 
495   // Inject fake files into the file system.
496   auto Cache = std::make_unique<FakeStatCache>();
497   Cache->InjectDirectory("/tmp", 42);
498   Cache->InjectFile("/tmp/test", 43);
499   Manager.setStatCache(std::move(Cache));
500 
501   // Set up a virtual file with a different size than FakeStatCache uses.
502   const FileEntry *File = Manager.getVirtualFile("/tmp/test", /*Size=*/10, 0);
503   ASSERT_TRUE(File);
504   const FileEntry &FE = *File;
505   EXPECT_TRUE(FE.isValid());
506   EXPECT_EQ(FE.getSize(), 10);
507 
508   // Calling a second time should not affect the UID or size.
509   unsigned VirtualUID = FE.getUID();
510   EXPECT_EQ(
511       &FE,
512       &expectedToOptional(Manager.getFileRef("/tmp/test"))->getFileEntry());
513   EXPECT_EQ(FE.getUID(), VirtualUID);
514   EXPECT_EQ(FE.getSize(), 10);
515 
516   // Bypass the file.
517   llvm::Optional<FileEntryRef> BypassRef =
518       Manager.getBypassFile(File->getLastRef());
519   ASSERT_TRUE(BypassRef);
520   EXPECT_TRUE(BypassRef->isValid());
521   EXPECT_EQ("/tmp/test", BypassRef->getName());
522 
523   // Check that it's different in the right ways.
524   EXPECT_NE(&BypassRef->getFileEntry(), File);
525   EXPECT_NE(BypassRef->getUID(), VirtualUID);
526   EXPECT_NE(BypassRef->getSize(), FE.getSize());
527 
528   // The virtual file should still be returned when searching.
529   EXPECT_EQ(
530       &FE,
531       &expectedToOptional(Manager.getFileRef("/tmp/test"))->getFileEntry());
532 }
533 
534 } // anonymous namespace
535