1 //===-- FileCollectorTest.cpp -----------------------------------*- C++ -*-===// 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 "gmock/gmock.h" 10 #include "gtest/gtest.h" 11 12 #include "llvm/Support/FileCollector.h" 13 #include "llvm/Support/FileSystem.h" 14 15 using namespace llvm; 16 17 namespace llvm { 18 namespace vfs { 19 inline bool operator==(const llvm::vfs::YAMLVFSEntry &LHS, 20 const llvm::vfs::YAMLVFSEntry &RHS) { 21 return LHS.VPath == RHS.VPath && LHS.RPath == RHS.RPath; 22 } 23 } // namespace vfs 24 } // namespace llvm 25 26 namespace { 27 class TestingFileCollector : public FileCollector { 28 public: 29 using FileCollector::FileCollector; 30 using FileCollector::Root; 31 using FileCollector::Seen; 32 using FileCollector::SymlinkMap; 33 using FileCollector::VFSWriter; 34 35 bool hasSeen(StringRef fs) { 36 return Seen.find(fs) != Seen.end(); 37 } 38 }; 39 40 struct ScopedDir { 41 SmallString<128> Path; 42 ScopedDir(const Twine &Name, bool Unique = false) { 43 std::error_code EC; 44 if (Unique) { 45 EC = llvm::sys::fs::createUniqueDirectory(Name, Path); 46 } else { 47 Path = Name.str(); 48 EC = llvm::sys::fs::create_directory(Twine(Path)); 49 } 50 if (EC) 51 Path = ""; 52 EXPECT_FALSE(EC); 53 // Ensure the path is the real path so tests can use it to compare against 54 // realpath output. 55 SmallString<128> RealPath; 56 if (!llvm::sys::fs::real_path(Path, RealPath)) 57 Path.swap(RealPath); 58 } 59 ~ScopedDir() { 60 if (Path != "") { 61 EXPECT_FALSE(llvm::sys::fs::remove_directories(Path.str())); 62 } 63 } 64 operator StringRef() { return Path.str(); } 65 }; 66 67 struct ScopedLink { 68 SmallString<128> Path; 69 ScopedLink(const Twine &To, const Twine &From) { 70 Path = From.str(); 71 std::error_code EC = sys::fs::create_link(To, From); 72 if (EC) 73 Path = ""; 74 EXPECT_FALSE(EC); 75 } 76 ~ScopedLink() { 77 if (Path != "") { 78 EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); 79 } 80 } 81 operator StringRef() { return Path.str(); } 82 }; 83 84 struct ScopedFile { 85 SmallString<128> Path; 86 ScopedFile(const Twine &Name) { 87 std::error_code EC; 88 EC = llvm::sys::fs::createUniqueFile(Name, Path); 89 if (EC) 90 Path = ""; 91 EXPECT_FALSE(EC); 92 } 93 ~ScopedFile() { 94 if (Path != "") { 95 EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); 96 } 97 } 98 operator StringRef() { return Path.str(); } 99 }; 100 } // end anonymous namespace 101 102 TEST(FileCollectorTest, addFile) { 103 ScopedDir root("add_file_root", true); 104 std::string root_fs = std::string(root.Path.str()); 105 TestingFileCollector FileCollector(root_fs, root_fs); 106 107 FileCollector.addFile("/path/to/a"); 108 FileCollector.addFile("/path/to/b"); 109 FileCollector.addFile("/path/to/c"); 110 111 // Make sure the root is correct. 112 EXPECT_EQ(FileCollector.Root, root_fs); 113 114 // Make sure we've seen all the added files. 115 EXPECT_TRUE(FileCollector.hasSeen("/path/to/a")); 116 EXPECT_TRUE(FileCollector.hasSeen("/path/to/b")); 117 EXPECT_TRUE(FileCollector.hasSeen("/path/to/c")); 118 119 // Make sure we've only seen the added files. 120 EXPECT_FALSE(FileCollector.hasSeen("/path/to/d")); 121 } 122 123 TEST(FileCollectorTest, addDirectory) { 124 ScopedDir file_root("file_root", true); 125 126 llvm::SmallString<128> aaa = file_root.Path; 127 llvm::sys::path::append(aaa, "aaa"); 128 ScopedFile a(aaa.str()); 129 130 llvm::SmallString<128> bbb = file_root.Path; 131 llvm::sys::path::append(bbb, "bbb"); 132 ScopedFile b(bbb.str()); 133 134 llvm::SmallString<128> ccc = file_root.Path; 135 llvm::sys::path::append(ccc, "ccc"); 136 ScopedFile c(ccc.str()); 137 138 std::string root_fs = std::string(file_root.Path.str()); 139 TestingFileCollector FileCollector(root_fs, root_fs); 140 141 FileCollector.addDirectory(file_root.Path); 142 143 // Make sure the root is correct. 144 EXPECT_EQ(FileCollector.Root, root_fs); 145 146 // Make sure we've seen all the added files. 147 EXPECT_TRUE(FileCollector.hasSeen(a.Path)); 148 EXPECT_TRUE(FileCollector.hasSeen(b.Path)); 149 EXPECT_TRUE(FileCollector.hasSeen(c.Path)); 150 151 // Make sure we've only seen the added files. 152 llvm::SmallString<128> ddd = file_root.Path; 153 llvm::sys::path::append(ddd, "ddd"); 154 ScopedFile d(ddd.str()); 155 EXPECT_FALSE(FileCollector.hasSeen(d.Path)); 156 } 157 158 TEST(FileCollectorTest, copyFiles) { 159 ScopedDir file_root("file_root", true); 160 ScopedFile a(file_root + "/aaa"); 161 ScopedFile b(file_root + "/bbb"); 162 ScopedFile c(file_root + "/ccc"); 163 164 // Create file collector and add files. 165 ScopedDir root("copy_files_root", true); 166 std::string root_fs = std::string(root.Path.str()); 167 TestingFileCollector FileCollector(root_fs, root_fs); 168 FileCollector.addFile(a.Path); 169 FileCollector.addFile(b.Path); 170 FileCollector.addFile(c.Path); 171 172 // Make sure we can copy the files. 173 std::error_code ec = FileCollector.copyFiles(true); 174 EXPECT_FALSE(ec); 175 176 // Now add a bogus file and make sure we error out. 177 FileCollector.addFile("/some/bogus/file"); 178 ec = FileCollector.copyFiles(true); 179 EXPECT_TRUE(ec); 180 181 // However, if stop_on_error is true the copy should still succeed. 182 ec = FileCollector.copyFiles(false); 183 EXPECT_FALSE(ec); 184 } 185 186 TEST(FileCollectorTest, recordAndConstructDirectory) { 187 ScopedDir file_root("dir_root", true); 188 ScopedDir subdir(file_root + "/subdir"); 189 ScopedDir subdir2(file_root + "/subdir2"); 190 ScopedFile a(subdir2 + "/a"); 191 192 // Create file collector and add files. 193 ScopedDir root("copy_files_root", true); 194 std::string root_fs = std::string(root.Path.str()); 195 TestingFileCollector FileCollector(root_fs, root_fs); 196 FileCollector.addFile(a.Path); 197 198 // The empty directory isn't seen until we add it. 199 EXPECT_TRUE(FileCollector.hasSeen(a.Path)); 200 EXPECT_FALSE(FileCollector.hasSeen(subdir.Path)); 201 202 FileCollector.addFile(subdir.Path); 203 EXPECT_TRUE(FileCollector.hasSeen(subdir.Path)); 204 205 // Make sure we can construct the directory. 206 std::error_code ec = FileCollector.copyFiles(true); 207 EXPECT_FALSE(ec); 208 bool IsDirectory = false; 209 llvm::SmallString<128> SubdirInRoot = root.Path; 210 llvm::sys::path::append(SubdirInRoot, 211 llvm::sys::path::relative_path(subdir.Path)); 212 ec = sys::fs::is_directory(SubdirInRoot, IsDirectory); 213 EXPECT_FALSE(ec); 214 ASSERT_TRUE(IsDirectory); 215 } 216 217 TEST(FileCollectorTest, recordVFSAccesses) { 218 ScopedDir file_root("dir_root", true); 219 ScopedDir subdir(file_root + "/subdir"); 220 ScopedDir subdir2(file_root + "/subdir2"); 221 ScopedFile a(subdir2 + "/a"); 222 ScopedFile b(file_root + "/b"); 223 ScopedDir subdir3(file_root + "/subdir3"); 224 ScopedFile subdir3a(subdir3 + "/aa"); 225 ScopedDir subdir3b(subdir3 + "/subdirb"); 226 { 227 ScopedFile subdir3fileremoved(subdir3 + "/removed"); 228 } 229 230 // Create file collector and add files. 231 ScopedDir root("copy_files_root", true); 232 std::string root_fs = std::string(root.Path.str()); 233 auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs); 234 auto VFS = 235 FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector); 236 VFS->status(a.Path); 237 EXPECT_TRUE(Collector->hasSeen(a.Path)); 238 239 VFS->openFileForRead(b.Path); 240 EXPECT_TRUE(Collector->hasSeen(b.Path)); 241 242 VFS->status(subdir.Path); 243 EXPECT_TRUE(Collector->hasSeen(subdir.Path)); 244 245 #ifndef _WIN32 246 std::error_code EC; 247 auto It = VFS->dir_begin(subdir3.Path, EC); 248 EXPECT_FALSE(EC); 249 EXPECT_TRUE(Collector->hasSeen(subdir3.Path)); 250 EXPECT_TRUE(Collector->hasSeen(subdir3a.Path)); 251 EXPECT_TRUE(Collector->hasSeen(subdir3b.Path)); 252 std::string RemovedFileName = (Twine(subdir3.Path) + "/removed").str(); 253 EXPECT_FALSE(Collector->hasSeen(RemovedFileName)); 254 #endif 255 } 256 257 #ifndef _WIN32 258 TEST(FileCollectorTest, Symlinks) { 259 // Root where the original files live. 260 ScopedDir file_root("file_root", true); 261 262 // Create some files in the file root. 263 ScopedFile a(file_root + "/aaa"); 264 ScopedFile b(file_root + "/bbb"); 265 ScopedFile c(file_root + "/ccc"); 266 267 // Create a directory foo with file ddd. 268 ScopedDir foo(file_root + "/foo"); 269 ScopedFile d(foo + "/ddd"); 270 271 // Create a file eee in the foo's parent directory. 272 ScopedFile e(foo + "/../eee"); 273 274 // Create a symlink bar pointing to foo. 275 ScopedLink symlink(file_root + "/foo", file_root + "/bar"); 276 277 // Root where files are copied to. 278 ScopedDir reproducer_root("reproducer_root", true); 279 std::string root_fs = std::string(reproducer_root.Path.str()); 280 TestingFileCollector FileCollector(root_fs, root_fs); 281 282 // Add all the files to the collector. 283 FileCollector.addFile(a.Path); 284 FileCollector.addFile(b.Path); 285 FileCollector.addFile(c.Path); 286 FileCollector.addFile(d.Path); 287 FileCollector.addFile(e.Path); 288 FileCollector.addFile(file_root + "/bar/ddd"); 289 290 auto mapping = FileCollector.VFSWriter.getMappings(); 291 292 { 293 // Make sure the common case works. 294 std::string vpath = (file_root + "/aaa").str(); 295 std::string rpath = (reproducer_root.Path + file_root.Path + "/aaa").str(); 296 printf("%s -> %s\n", vpath.c_str(), rpath.c_str()); 297 EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); 298 } 299 300 { 301 // Make sure the virtual path points to the real source path. 302 std::string vpath = (file_root + "/bar/ddd").str(); 303 std::string rpath = 304 (reproducer_root.Path + file_root.Path + "/foo/ddd").str(); 305 printf("%s -> %s\n", vpath.c_str(), rpath.c_str()); 306 EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); 307 } 308 309 { 310 // Make sure that .. is removed from the source path. 311 std::string vpath = (file_root + "/eee").str(); 312 std::string rpath = (reproducer_root.Path + file_root.Path + "/eee").str(); 313 printf("%s -> %s\n", vpath.c_str(), rpath.c_str()); 314 EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath))); 315 } 316 } 317 318 TEST(FileCollectorTest, recordVFSSymlinkAccesses) { 319 ScopedDir file_root("dir_root", true); 320 ScopedFile a(file_root + "/a"); 321 ScopedLink symlink(file_root + "/a", file_root + "/b"); 322 323 // Create file collector and add files. 324 ScopedDir root("copy_files_root", true); 325 std::string root_fs = std::string(root.Path.str()); 326 auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs); 327 auto VFS = 328 FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector); 329 SmallString<256> Output; 330 VFS->getRealPath(symlink.Path, Output); 331 EXPECT_TRUE(Collector->hasSeen(a.Path)); 332 EXPECT_TRUE(Collector->hasSeen(symlink.Path)); 333 } 334 #endif 335