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