OSDN Git Service

Add support for getting file system permissions and implement sys::fs::permissions...
[android-x86/external-llvm.git] / unittests / Support / Path.cpp
1 //===- llvm/unittest/Support/Path.cpp - Path 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
10 #include "llvm/Support/Path.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/Support/ConvertUTF.h"
15 #include "llvm/Support/Errc.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/FileUtilities.h"
19 #include "llvm/Support/Host.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "gtest/gtest.h"
23
24 #ifdef LLVM_ON_WIN32
25 #include "llvm/ADT/ArrayRef.h"
26 #include <windows.h>
27 #include <winerror.h>
28 #endif
29
30 #ifdef LLVM_ON_UNIX
31 #include <sys/stat.h>
32 #endif
33
34 using namespace llvm;
35 using namespace llvm::sys;
36
37 #define ASSERT_NO_ERROR(x)                                                     \
38   if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
39     SmallString<128> MessageStorage;                                           \
40     raw_svector_ostream Message(MessageStorage);                               \
41     Message << #x ": did not return errc::success.\n"                          \
42             << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"          \
43             << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";      \
44     GTEST_FATAL_FAILURE_(MessageStorage.c_str());                              \
45   } else {                                                                     \
46   }
47
48 namespace {
49
50 TEST(is_separator, Works) {
51   EXPECT_TRUE(path::is_separator('/'));
52   EXPECT_FALSE(path::is_separator('\0'));
53   EXPECT_FALSE(path::is_separator('-'));
54   EXPECT_FALSE(path::is_separator(' '));
55
56 #ifdef LLVM_ON_WIN32
57   EXPECT_TRUE(path::is_separator('\\'));
58 #else
59   EXPECT_FALSE(path::is_separator('\\'));
60 #endif
61 }
62
63 TEST(Support, Path) {
64   SmallVector<StringRef, 40> paths;
65   paths.push_back("");
66   paths.push_back(".");
67   paths.push_back("..");
68   paths.push_back("foo");
69   paths.push_back("/");
70   paths.push_back("/foo");
71   paths.push_back("foo/");
72   paths.push_back("/foo/");
73   paths.push_back("foo/bar");
74   paths.push_back("/foo/bar");
75   paths.push_back("//net");
76   paths.push_back("//net/foo");
77   paths.push_back("///foo///");
78   paths.push_back("///foo///bar");
79   paths.push_back("/.");
80   paths.push_back("./");
81   paths.push_back("/..");
82   paths.push_back("../");
83   paths.push_back("foo/.");
84   paths.push_back("foo/..");
85   paths.push_back("foo/./");
86   paths.push_back("foo/./bar");
87   paths.push_back("foo/..");
88   paths.push_back("foo/../");
89   paths.push_back("foo/../bar");
90   paths.push_back("c:");
91   paths.push_back("c:/");
92   paths.push_back("c:foo");
93   paths.push_back("c:/foo");
94   paths.push_back("c:foo/");
95   paths.push_back("c:/foo/");
96   paths.push_back("c:/foo/bar");
97   paths.push_back("prn:");
98   paths.push_back("c:\\");
99   paths.push_back("c:foo");
100   paths.push_back("c:\\foo");
101   paths.push_back("c:foo\\");
102   paths.push_back("c:\\foo\\");
103   paths.push_back("c:\\foo/");
104   paths.push_back("c:/foo\\bar");
105
106   SmallVector<StringRef, 5> ComponentStack;
107   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
108                                                   e = paths.end();
109                                                   i != e;
110                                                   ++i) {
111     for (sys::path::const_iterator ci = sys::path::begin(*i),
112                                    ce = sys::path::end(*i);
113                                    ci != ce;
114                                    ++ci) {
115       ASSERT_FALSE(ci->empty());
116       ComponentStack.push_back(*ci);
117     }
118
119     for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
120                                      ce = sys::path::rend(*i);
121                                      ci != ce;
122                                      ++ci) {
123       ASSERT_TRUE(*ci == ComponentStack.back());
124       ComponentStack.pop_back();
125     }
126     ASSERT_TRUE(ComponentStack.empty());
127
128     // Crash test most of the API - since we're iterating over all of our paths
129     // here there isn't really anything reasonable to assert on in the results.
130     (void)path::has_root_path(*i);
131     (void)path::root_path(*i);
132     (void)path::has_root_name(*i);
133     (void)path::root_name(*i);
134     (void)path::has_root_directory(*i);
135     (void)path::root_directory(*i);
136     (void)path::has_parent_path(*i);
137     (void)path::parent_path(*i);
138     (void)path::has_filename(*i);
139     (void)path::filename(*i);
140     (void)path::has_stem(*i);
141     (void)path::stem(*i);
142     (void)path::has_extension(*i);
143     (void)path::extension(*i);
144     (void)path::is_absolute(*i);
145     (void)path::is_relative(*i);
146
147     SmallString<128> temp_store;
148     temp_store = *i;
149     ASSERT_NO_ERROR(fs::make_absolute(temp_store));
150     temp_store = *i;
151     path::remove_filename(temp_store);
152
153     temp_store = *i;
154     path::replace_extension(temp_store, "ext");
155     StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
156     stem = path::stem(filename);
157     ext  = path::extension(filename);
158     EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
159
160     path::native(*i, temp_store);
161   }
162
163   SmallString<32> Relative("foo.cpp");
164   ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
165   Relative[5] = '/'; // Fix up windows paths.
166   ASSERT_EQ("/root/foo.cpp", Relative);
167 }
168
169 TEST(Support, RelativePathIterator) {
170   SmallString<64> Path(StringRef("c/d/e/foo.txt"));
171   typedef SmallVector<StringRef, 4> PathComponents;
172   PathComponents ExpectedPathComponents;
173   PathComponents ActualPathComponents;
174
175   StringRef(Path).split(ExpectedPathComponents, '/');
176
177   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
178        ++I) {
179     ActualPathComponents.push_back(*I);
180   }
181
182   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
183
184   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
185     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
186   }
187 }
188
189 TEST(Support, RelativePathDotIterator) {
190   SmallString<64> Path(StringRef(".c/.d/../."));
191   typedef SmallVector<StringRef, 4> PathComponents;
192   PathComponents ExpectedPathComponents;
193   PathComponents ActualPathComponents;
194
195   StringRef(Path).split(ExpectedPathComponents, '/');
196
197   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
198        ++I) {
199     ActualPathComponents.push_back(*I);
200   }
201
202   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
203
204   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
205     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
206   }
207 }
208
209 TEST(Support, AbsolutePathIterator) {
210   SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
211   typedef SmallVector<StringRef, 4> PathComponents;
212   PathComponents ExpectedPathComponents;
213   PathComponents ActualPathComponents;
214
215   StringRef(Path).split(ExpectedPathComponents, '/');
216
217   // The root path will also be a component when iterating
218   ExpectedPathComponents[0] = "/";
219
220   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
221        ++I) {
222     ActualPathComponents.push_back(*I);
223   }
224
225   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
226
227   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
228     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
229   }
230 }
231
232 TEST(Support, AbsolutePathDotIterator) {
233   SmallString<64> Path(StringRef("/.c/.d/../."));
234   typedef SmallVector<StringRef, 4> PathComponents;
235   PathComponents ExpectedPathComponents;
236   PathComponents ActualPathComponents;
237
238   StringRef(Path).split(ExpectedPathComponents, '/');
239
240   // The root path will also be a component when iterating
241   ExpectedPathComponents[0] = "/";
242
243   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
244        ++I) {
245     ActualPathComponents.push_back(*I);
246   }
247
248   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
249
250   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
251     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
252   }
253 }
254
255 #ifdef LLVM_ON_WIN32
256 TEST(Support, AbsolutePathIteratorWin32) {
257   SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
258   typedef SmallVector<StringRef, 4> PathComponents;
259   PathComponents ExpectedPathComponents;
260   PathComponents ActualPathComponents;
261
262   StringRef(Path).split(ExpectedPathComponents, "\\");
263
264   // The root path (which comes after the drive name) will also be a component
265   // when iterating.
266   ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
267
268   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
269        ++I) {
270     ActualPathComponents.push_back(*I);
271   }
272
273   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
274
275   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
276     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
277   }
278 }
279 #endif // LLVM_ON_WIN32
280
281 TEST(Support, AbsolutePathIteratorEnd) {
282   // Trailing slashes are converted to '.' unless they are part of the root path.
283   SmallVector<StringRef, 4> Paths;
284   Paths.push_back("/foo/");
285   Paths.push_back("/foo//");
286   Paths.push_back("//net//");
287 #ifdef LLVM_ON_WIN32
288   Paths.push_back("c:\\\\");
289 #endif
290
291   for (StringRef Path : Paths) {
292     StringRef LastComponent = *path::rbegin(Path);
293     EXPECT_EQ(".", LastComponent);
294   }
295
296   SmallVector<StringRef, 3> RootPaths;
297   RootPaths.push_back("/");
298   RootPaths.push_back("//net/");
299 #ifdef LLVM_ON_WIN32
300   RootPaths.push_back("c:\\");
301 #endif
302
303   for (StringRef Path : RootPaths) {
304     StringRef LastComponent = *path::rbegin(Path);
305     EXPECT_EQ(1u, LastComponent.size());
306     EXPECT_TRUE(path::is_separator(LastComponent[0]));
307   }
308 }
309
310 TEST(Support, HomeDirectory) {
311   std::string expected;
312 #ifdef LLVM_ON_WIN32
313   if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) {
314     auto pathLen = ::wcslen(path);
315     ArrayRef<char> ref{reinterpret_cast<char const *>(path),
316                        pathLen * sizeof(wchar_t)};
317     convertUTF16ToUTF8String(ref, expected);
318   }
319 #else
320   if (char const *path = ::getenv("HOME"))
321     expected = path;
322 #endif
323   // Do not try to test it if we don't know what to expect.
324   // On Windows we use something better than env vars.
325   if (!expected.empty()) {
326     SmallString<128> HomeDir;
327     auto status = path::home_directory(HomeDir);
328     EXPECT_TRUE(status);
329     EXPECT_EQ(expected, HomeDir);
330   }
331 }
332
333 TEST(Support, UserCacheDirectory) {
334   SmallString<13> CacheDir;
335   SmallString<20> CacheDir2;
336   auto Status = path::user_cache_directory(CacheDir, "");
337   EXPECT_TRUE(Status ^ CacheDir.empty());
338
339   if (Status) {
340     EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed
341     EXPECT_EQ(CacheDir, CacheDir2); // and return same paths
342
343     EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c"));
344     auto It = path::rbegin(CacheDir);
345     EXPECT_EQ("file.c", *It);
346     EXPECT_EQ("B", *++It);
347     EXPECT_EQ("A", *++It);
348     auto ParentDir = *++It;
349
350     // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0"
351     EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2",
352                                            "\xE2\x84\xB5.0"));
353     auto It2 = path::rbegin(CacheDir2);
354     EXPECT_EQ("\xE2\x84\xB5.0", *It2);
355     EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2);
356     auto ParentDir2 = *++It2;
357
358     EXPECT_EQ(ParentDir, ParentDir2);
359   }
360 }
361
362 TEST(Support, TempDirectory) {
363   SmallString<32> TempDir;
364   path::system_temp_directory(false, TempDir);
365   EXPECT_TRUE(!TempDir.empty());
366   TempDir.clear();
367   path::system_temp_directory(true, TempDir);
368   EXPECT_TRUE(!TempDir.empty());
369 }
370
371 #ifdef LLVM_ON_WIN32
372 static std::string path2regex(std::string Path) {
373   size_t Pos = 0;
374   while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
375     Path.replace(Pos, 1, "\\\\");
376     Pos += 2;
377   }
378   return Path;
379 }
380
381 /// Helper for running temp dir test in separated process. See below.
382 #define EXPECT_TEMP_DIR(prepare, expected)                                     \
383   EXPECT_EXIT(                                                                 \
384       {                                                                        \
385         prepare;                                                               \
386         SmallString<300> TempDir;                                              \
387         path::system_temp_directory(true, TempDir);                            \
388         raw_os_ostream(std::cerr) << TempDir;                                  \
389         std::exit(0);                                                          \
390       },                                                                       \
391       ::testing::ExitedWithCode(0), path2regex(expected))
392
393 TEST(SupportDeathTest, TempDirectoryOnWindows) {
394   // In this test we want to check how system_temp_directory responds to
395   // different values of specific env vars. To prevent corrupting env vars of
396   // the current process all checks are done in separated processes.
397   EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder");
398   EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"),
399                   "C:\\Unix\\Path\\Seperators");
400   EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$");
401   EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep");
402   EXPECT_TEMP_DIR(
403       _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
404       "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
405
406   // Test $TMP empty, $TEMP set.
407   EXPECT_TEMP_DIR(
408       {
409         _wputenv_s(L"TMP", L"");
410         _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
411       },
412       "C:\\Valid\\Path");
413
414   // All related env vars empty
415   EXPECT_TEMP_DIR(
416   {
417     _wputenv_s(L"TMP", L"");
418     _wputenv_s(L"TEMP", L"");
419     _wputenv_s(L"USERPROFILE", L"");
420   },
421     "C:\\Temp");
422
423   // Test evn var / path with 260 chars.
424   SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
425   while (Expected.size() < 260)
426     Expected.append("\\DirNameWith19Charss");
427   ASSERT_EQ(260U, Expected.size());
428   EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
429 }
430 #endif
431
432 class FileSystemTest : public testing::Test {
433 protected:
434   /// Unique temporary directory in which all created filesystem entities must
435   /// be placed. It is removed at the end of each test (must be empty).
436   SmallString<128> TestDirectory;
437
438   void SetUp() override {
439     ASSERT_NO_ERROR(
440         fs::createUniqueDirectory("file-system-test", TestDirectory));
441     // We don't care about this specific file.
442     errs() << "Test Directory: " << TestDirectory << '\n';
443     errs().flush();
444   }
445
446   void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
447 };
448
449 TEST_F(FileSystemTest, Unique) {
450   // Create a temp file.
451   int FileDescriptor;
452   SmallString<64> TempPath;
453   ASSERT_NO_ERROR(
454       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
455
456   // The same file should return an identical unique id.
457   fs::UniqueID F1, F2;
458   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
459   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
460   ASSERT_EQ(F1, F2);
461
462   // Different files should return different unique ids.
463   int FileDescriptor2;
464   SmallString<64> TempPath2;
465   ASSERT_NO_ERROR(
466       fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
467
468   fs::UniqueID D;
469   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
470   ASSERT_NE(D, F1);
471   ::close(FileDescriptor2);
472
473   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
474
475   // Two paths representing the same file on disk should still provide the
476   // same unique id.  We can test this by making a hard link.
477   ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
478   fs::UniqueID D2;
479   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
480   ASSERT_EQ(D2, F1);
481
482   ::close(FileDescriptor);
483
484   SmallString<128> Dir1;
485   ASSERT_NO_ERROR(
486      fs::createUniqueDirectory("dir1", Dir1));
487   ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
488   ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
489   ASSERT_EQ(F1, F2);
490
491   SmallString<128> Dir2;
492   ASSERT_NO_ERROR(
493      fs::createUniqueDirectory("dir2", Dir2));
494   ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
495   ASSERT_NE(F1, F2);
496   ASSERT_NO_ERROR(fs::remove(Dir1));
497   ASSERT_NO_ERROR(fs::remove(Dir2));
498   ASSERT_NO_ERROR(fs::remove(TempPath2));
499   ASSERT_NO_ERROR(fs::remove(TempPath));
500 }
501
502 TEST_F(FileSystemTest, RealPath) {
503   ASSERT_NO_ERROR(
504       fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3"));
505   ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3"));
506
507   SmallString<64> RealBase;
508   SmallString<64> Expected;
509   SmallString<64> Actual;
510
511   // TestDirectory itself might be under a symlink or have been specified with
512   // a different case than the existing temp directory.  In such cases real_path
513   // on the concatenated path will differ in the TestDirectory portion from
514   // how we specified it.  Make sure to compare against the real_path of the
515   // TestDirectory, and not just the value of TestDirectory.
516   ASSERT_NO_ERROR(fs::real_path(TestDirectory, RealBase));
517   path::native(Twine(RealBase) + "/test1/test2", Expected);
518
519   ASSERT_NO_ERROR(fs::real_path(
520       Twine(TestDirectory) + "/././test1/../test1/test2/./test3/..", Actual));
521
522   EXPECT_EQ(Expected, Actual);
523
524   SmallString<64> HomeDir;
525   bool Result = llvm::sys::path::home_directory(HomeDir);
526   if (Result) {
527     ASSERT_NO_ERROR(fs::real_path(HomeDir, Expected));
528     ASSERT_NO_ERROR(fs::real_path("~", Actual, true));
529     EXPECT_EQ(Expected, Actual);
530     ASSERT_NO_ERROR(fs::real_path("~/", Actual, true));
531     EXPECT_EQ(Expected, Actual);
532   }
533
534   ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
535 }
536
537 TEST_F(FileSystemTest, TempFiles) {
538   // Create a temp file.
539   int FileDescriptor;
540   SmallString<64> TempPath;
541   ASSERT_NO_ERROR(
542       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
543
544   // Make sure it exists.
545   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
546
547   // Create another temp tile.
548   int FD2;
549   SmallString<64> TempPath2;
550   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
551   ASSERT_TRUE(TempPath2.endswith(".temp"));
552   ASSERT_NE(TempPath.str(), TempPath2.str());
553
554   fs::file_status A, B;
555   ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
556   ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
557   EXPECT_FALSE(fs::equivalent(A, B));
558
559   ::close(FD2);
560
561   // Remove Temp2.
562   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
563   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
564   ASSERT_EQ(fs::remove(Twine(TempPath2), false),
565             errc::no_such_file_or_directory);
566
567   std::error_code EC = fs::status(TempPath2.c_str(), B);
568   EXPECT_EQ(EC, errc::no_such_file_or_directory);
569   EXPECT_EQ(B.type(), fs::file_type::file_not_found);
570
571   // Make sure Temp2 doesn't exist.
572   ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
573             errc::no_such_file_or_directory);
574
575   SmallString<64> TempPath3;
576   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
577   ASSERT_FALSE(TempPath3.endswith("."));
578   FileRemover Cleanup3(TempPath3);
579
580   // Create a hard link to Temp1.
581   ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
582   bool equal;
583   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
584   EXPECT_TRUE(equal);
585   ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
586   ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
587   EXPECT_TRUE(fs::equivalent(A, B));
588
589   // Remove Temp1.
590   ::close(FileDescriptor);
591   ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
592
593   // Remove the hard link.
594   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
595
596   // Make sure Temp1 doesn't exist.
597   ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
598             errc::no_such_file_or_directory);
599
600 #ifdef LLVM_ON_WIN32
601   // Path name > 260 chars should get an error.
602   const char *Path270 =
603     "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
604     "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
605     "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
606     "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
607     "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
608   EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
609             errc::invalid_argument);
610   // Relative path < 247 chars, no problem.
611   const char *Path216 =
612     "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
613     "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
614     "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
615     "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
616   ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
617   ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
618 #endif
619 }
620
621 TEST_F(FileSystemTest, CreateDir) {
622   ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
623   ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
624   ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
625             errc::file_exists);
626   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
627
628 #ifdef LLVM_ON_UNIX
629   // Set a 0000 umask so that we can test our directory permissions.
630   mode_t OldUmask = ::umask(0000);
631
632   fs::file_status Status;
633   ASSERT_NO_ERROR(
634       fs::create_directory(Twine(TestDirectory) + "baz500", false,
635                            fs::perms::owner_read | fs::perms::owner_exe));
636   ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
637   ASSERT_EQ(Status.permissions() & fs::perms::all_all,
638             fs::perms::owner_read | fs::perms::owner_exe);
639   ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
640                                        fs::perms::all_all));
641   ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
642   ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
643
644   // Restore umask to be safe.
645   ::umask(OldUmask);
646 #endif
647
648 #ifdef LLVM_ON_WIN32
649   // Prove that create_directories() can handle a pathname > 248 characters,
650   // which is the documented limit for CreateDirectory().
651   // (248 is MAX_PATH subtracting room for an 8.3 filename.)
652   // Generate a directory path guaranteed to fall into that range.
653   size_t TmpLen = TestDirectory.size();
654   const char *OneDir = "\\123456789";
655   size_t OneDirLen = strlen(OneDir);
656   ASSERT_LT(OneDirLen, 12U);
657   size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
658   SmallString<260> LongDir(TestDirectory);
659   for (size_t I = 0; I < NLevels; ++I)
660     LongDir.append(OneDir);
661   ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
662   ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
663   ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
664             errc::file_exists);
665   // Tidy up, "recursively" removing the directories.
666   StringRef ThisDir(LongDir);
667   for (size_t J = 0; J < NLevels; ++J) {
668     ASSERT_NO_ERROR(fs::remove(ThisDir));
669     ThisDir = path::parent_path(ThisDir);
670   }
671
672   // Similarly for a relative pathname.  Need to set the current directory to
673   // TestDirectory so that the one we create ends up in the right place.
674   char PreviousDir[260];
675   size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
676   ASSERT_GT(PreviousDirLen, 0U);
677   ASSERT_LT(PreviousDirLen, 260U);
678   ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
679   LongDir.clear();
680   // Generate a relative directory name with absolute length > 248.
681   size_t LongDirLen = 249 - TestDirectory.size();
682   LongDir.assign(LongDirLen, 'a');
683   ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
684   // While we're here, prove that .. and . handling works in these long paths.
685   const char *DotDotDirs = "\\..\\.\\b";
686   LongDir.append(DotDotDirs);
687   ASSERT_NO_ERROR(fs::create_directory("b"));
688   ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
689   // And clean up.
690   ASSERT_NO_ERROR(fs::remove("b"));
691   ASSERT_NO_ERROR(fs::remove(
692     Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
693   ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
694 #endif
695 }
696
697 TEST_F(FileSystemTest, DirectoryIteration) {
698   std::error_code ec;
699   for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
700     ASSERT_NO_ERROR(ec);
701
702   // Create a known hierarchy to recurse over.
703   ASSERT_NO_ERROR(
704       fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
705   ASSERT_NO_ERROR(
706       fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
707   ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
708                                          "/recursive/dontlookhere/da1"));
709   ASSERT_NO_ERROR(
710       fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
711   ASSERT_NO_ERROR(
712       fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
713   typedef std::vector<std::string> v_t;
714   v_t visited;
715   for (fs::recursive_directory_iterator i(Twine(TestDirectory)
716          + "/recursive", ec), e; i != e; i.increment(ec)){
717     ASSERT_NO_ERROR(ec);
718     if (path::filename(i->path()) == "p1") {
719       i.pop();
720       // FIXME: recursive_directory_iterator should be more robust.
721       if (i == e) break;
722     }
723     if (path::filename(i->path()) == "dontlookhere")
724       i.no_push();
725     visited.push_back(path::filename(i->path()));
726   }
727   v_t::const_iterator a0 = find(visited, "a0");
728   v_t::const_iterator aa1 = find(visited, "aa1");
729   v_t::const_iterator ab1 = find(visited, "ab1");
730   v_t::const_iterator dontlookhere = find(visited, "dontlookhere");
731   v_t::const_iterator da1 = find(visited, "da1");
732   v_t::const_iterator z0 = find(visited, "z0");
733   v_t::const_iterator za1 = find(visited, "za1");
734   v_t::const_iterator pop = find(visited, "pop");
735   v_t::const_iterator p1 = find(visited, "p1");
736
737   // Make sure that each path was visited correctly.
738   ASSERT_NE(a0, visited.end());
739   ASSERT_NE(aa1, visited.end());
740   ASSERT_NE(ab1, visited.end());
741   ASSERT_NE(dontlookhere, visited.end());
742   ASSERT_EQ(da1, visited.end()); // Not visited.
743   ASSERT_NE(z0, visited.end());
744   ASSERT_NE(za1, visited.end());
745   ASSERT_NE(pop, visited.end());
746   ASSERT_EQ(p1, visited.end()); // Not visited.
747
748   // Make sure that parents were visited before children. No other ordering
749   // guarantees can be made across siblings.
750   ASSERT_LT(a0, aa1);
751   ASSERT_LT(a0, ab1);
752   ASSERT_LT(z0, za1);
753
754   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
755   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
756   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
757   ASSERT_NO_ERROR(
758       fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
759   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
760   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
761   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
762   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
763   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
764   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
765
766   // Test recursive_directory_iterator level()
767   ASSERT_NO_ERROR(
768       fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c"));
769   fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E;
770   for (int l = 0; I != E; I.increment(ec), ++l) {
771     ASSERT_NO_ERROR(ec);
772     EXPECT_EQ(I.level(), l);
773   }
774   EXPECT_EQ(I, E);
775   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c"));
776   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b"));
777   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a"));
778   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel"));
779 }
780
781 TEST_F(FileSystemTest, Remove) {
782   SmallString<64> BaseDir;
783   SmallString<64> Paths[4];
784   int fds[4];
785   ASSERT_NO_ERROR(fs::createUniqueDirectory("fs_remove", BaseDir));
786
787   ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/baz"));
788   ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/buzz"));
789   ASSERT_NO_ERROR(fs::createUniqueFile(
790       Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[0], Paths[0]));
791   ASSERT_NO_ERROR(fs::createUniqueFile(
792       Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[1], Paths[1]));
793   ASSERT_NO_ERROR(fs::createUniqueFile(
794       Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[2], Paths[2]));
795   ASSERT_NO_ERROR(fs::createUniqueFile(
796       Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[3], Paths[3]));
797
798   for (int fd : fds)
799     ::close(fd);
800
801   EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/baz"));
802   EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/buzz"));
803   EXPECT_TRUE(fs::exists(Paths[0]));
804   EXPECT_TRUE(fs::exists(Paths[1]));
805   EXPECT_TRUE(fs::exists(Paths[2]));
806   EXPECT_TRUE(fs::exists(Paths[3]));
807
808   ASSERT_NO_ERROR(fs::remove_directories("D:/footest"));
809
810   ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
811   ASSERT_FALSE(fs::exists(BaseDir));
812 }
813
814 const char archive[] = "!<arch>\x0A";
815 const char bitcode[] = "\xde\xc0\x17\x0b";
816 const char coff_object[] = "\x00\x00......";
817 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
818     "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
819 const char coff_import_library[] = "\x00\x00\xff\xff....";
820 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
821                                  0,    0,   0,   0,   0, 0, 0, 0, 1 };
822 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
823 const char macho_object[] =
824     "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
825 const char macho_executable[] =
826     "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
827 const char macho_fixed_virtual_memory_shared_lib[] =
828     "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
829 const char macho_core[] =
830     "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
831 const char macho_preload_executable[] =
832     "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
833 const char macho_dynamically_linked_shared_lib[] =
834     "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
835 const char macho_dynamic_linker[] =
836     "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
837 const char macho_bundle[] =
838     "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
839 const char macho_dsym_companion[] =
840     "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
841 const char macho_kext_bundle[] =
842     "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
843 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
844 const char macho_dynamically_linked_shared_lib_stub[] =
845     "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
846
847 TEST_F(FileSystemTest, Magic) {
848   struct type {
849     const char *filename;
850     const char *magic_str;
851     size_t magic_str_len;
852     fs::file_magic magic;
853   } types[] = {
854 #define DEFINE(magic)                                           \
855     { #magic, magic, sizeof(magic), fs::file_magic::magic }
856     DEFINE(archive),
857     DEFINE(bitcode),
858     DEFINE(coff_object),
859     { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
860     DEFINE(coff_import_library),
861     DEFINE(elf_relocatable),
862     DEFINE(macho_universal_binary),
863     DEFINE(macho_object),
864     DEFINE(macho_executable),
865     DEFINE(macho_fixed_virtual_memory_shared_lib),
866     DEFINE(macho_core),
867     DEFINE(macho_preload_executable),
868     DEFINE(macho_dynamically_linked_shared_lib),
869     DEFINE(macho_dynamic_linker),
870     DEFINE(macho_bundle),
871     DEFINE(macho_dynamically_linked_shared_lib_stub),
872     DEFINE(macho_dsym_companion),
873     DEFINE(macho_kext_bundle),
874     DEFINE(windows_resource)
875 #undef DEFINE
876     };
877
878   // Create some files filled with magic.
879   for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
880                                                                      ++i) {
881     SmallString<128> file_pathname(TestDirectory);
882     path::append(file_pathname, i->filename);
883     std::error_code EC;
884     raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
885     ASSERT_FALSE(file.has_error());
886     StringRef magic(i->magic_str, i->magic_str_len);
887     file << magic;
888     file.close();
889     EXPECT_EQ(i->magic, fs::identify_magic(magic));
890     ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
891   }
892 }
893
894 #ifdef LLVM_ON_WIN32
895 TEST_F(FileSystemTest, CarriageReturn) {
896   SmallString<128> FilePathname(TestDirectory);
897   std::error_code EC;
898   path::append(FilePathname, "test");
899
900   {
901     raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
902     ASSERT_NO_ERROR(EC);
903     File << '\n';
904   }
905   {
906     auto Buf = MemoryBuffer::getFile(FilePathname.str());
907     EXPECT_TRUE((bool)Buf);
908     EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
909   }
910
911   {
912     raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
913     ASSERT_NO_ERROR(EC);
914     File << '\n';
915   }
916   {
917     auto Buf = MemoryBuffer::getFile(FilePathname.str());
918     EXPECT_TRUE((bool)Buf);
919     EXPECT_EQ(Buf.get()->getBuffer(), "\n");
920   }
921   ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
922 }
923 #endif
924
925 TEST_F(FileSystemTest, Resize) {
926   int FD;
927   SmallString<64> TempPath;
928   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
929   ASSERT_NO_ERROR(fs::resize_file(FD, 123));
930   fs::file_status Status;
931   ASSERT_NO_ERROR(fs::status(FD, Status));
932   ASSERT_EQ(Status.getSize(), 123U);
933   ::close(FD);
934   ASSERT_NO_ERROR(fs::remove(TempPath));
935 }
936
937 TEST_F(FileSystemTest, FileMapping) {
938   // Create a temp file.
939   int FileDescriptor;
940   SmallString<64> TempPath;
941   ASSERT_NO_ERROR(
942       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
943   unsigned Size = 4096;
944   ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
945
946   // Map in temp file and add some content
947   std::error_code EC;
948   StringRef Val("hello there");
949   {
950     fs::mapped_file_region mfr(FileDescriptor,
951                                fs::mapped_file_region::readwrite, Size, 0, EC);
952     ASSERT_NO_ERROR(EC);
953     std::copy(Val.begin(), Val.end(), mfr.data());
954     // Explicitly add a 0.
955     mfr.data()[Val.size()] = 0;
956     // Unmap temp file
957   }
958   ASSERT_EQ(close(FileDescriptor), 0);
959
960   // Map it back in read-only
961   {
962     int FD;
963     EC = fs::openFileForRead(Twine(TempPath), FD);
964     ASSERT_NO_ERROR(EC);
965     fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
966     ASSERT_NO_ERROR(EC);
967
968     // Verify content
969     EXPECT_EQ(StringRef(mfr.const_data()), Val);
970
971     // Unmap temp file
972     fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
973     ASSERT_NO_ERROR(EC);
974     ASSERT_EQ(close(FD), 0);
975   }
976   ASSERT_NO_ERROR(fs::remove(TempPath));
977 }
978
979 TEST(Support, NormalizePath) {
980 #if defined(LLVM_ON_WIN32)
981 #define EXPECT_PATH_IS(path__, windows__, not_windows__)                        \
982   EXPECT_EQ(path__, windows__);
983 #else
984 #define EXPECT_PATH_IS(path__, windows__, not_windows__)                        \
985   EXPECT_EQ(path__, not_windows__);
986 #endif
987
988   SmallString<64> Path1("a");
989   SmallString<64> Path2("a/b");
990   SmallString<64> Path3("a\\b");
991   SmallString<64> Path4("a\\\\b");
992   SmallString<64> Path5("\\a");
993   SmallString<64> Path6("a\\");
994
995   path::native(Path1);
996   EXPECT_PATH_IS(Path1, "a", "a");
997
998   path::native(Path2);
999   EXPECT_PATH_IS(Path2, "a\\b", "a/b");
1000
1001   path::native(Path3);
1002   EXPECT_PATH_IS(Path3, "a\\b", "a/b");
1003
1004   path::native(Path4);
1005   EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
1006
1007   path::native(Path5);
1008   EXPECT_PATH_IS(Path5, "\\a", "/a");
1009
1010   path::native(Path6);
1011   EXPECT_PATH_IS(Path6, "a\\", "a/");
1012
1013 #undef EXPECT_PATH_IS
1014
1015 #if defined(LLVM_ON_WIN32)
1016   SmallString<64> PathHome;
1017   path::home_directory(PathHome);
1018
1019   const char *Path7a = "~/aaa";
1020   SmallString<64> Path7(Path7a);
1021   path::native(Path7);
1022   EXPECT_TRUE(Path7.endswith("\\aaa"));
1023   EXPECT_TRUE(Path7.startswith(PathHome));
1024   EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1));
1025
1026   const char *Path8a = "~";
1027   SmallString<64> Path8(Path8a);
1028   path::native(Path8);
1029   EXPECT_EQ(Path8, PathHome);
1030
1031   const char *Path9a = "~aaa";
1032   SmallString<64> Path9(Path9a);
1033   path::native(Path9);
1034   EXPECT_EQ(Path9, "~aaa");
1035
1036   const char *Path10a = "aaa/~/b";
1037   SmallString<64> Path10(Path10a);
1038   path::native(Path10);
1039   EXPECT_EQ(Path10, "aaa\\~\\b");
1040 #endif
1041 }
1042
1043 TEST(Support, RemoveLeadingDotSlash) {
1044   StringRef Path1("././/foolz/wat");
1045   StringRef Path2("./////");
1046
1047   Path1 = path::remove_leading_dotslash(Path1);
1048   EXPECT_EQ(Path1, "foolz/wat");
1049   Path2 = path::remove_leading_dotslash(Path2);
1050   EXPECT_EQ(Path2, "");
1051 }
1052
1053 static std::string remove_dots(StringRef path,
1054     bool remove_dot_dot) {
1055   SmallString<256> buffer(path);
1056   path::remove_dots(buffer, remove_dot_dot);
1057   return buffer.str();
1058 }
1059
1060 TEST(Support, RemoveDots) {
1061 #if defined(LLVM_ON_WIN32)
1062   EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false));
1063   EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false));
1064
1065   EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false));
1066   EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true));
1067   EXPECT_EQ("c", remove_dots(".\\.\\c", true));
1068   EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true));
1069   EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true));
1070
1071   SmallString<64> Path1(".\\.\\c");
1072   EXPECT_TRUE(path::remove_dots(Path1, true));
1073   EXPECT_EQ("c", Path1);
1074 #else
1075   EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false));
1076   EXPECT_EQ("", remove_dots("./////", false));
1077
1078   EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false));
1079   EXPECT_EQ("b/c", remove_dots("./a/../b/c", true));
1080   EXPECT_EQ("c", remove_dots("././c", true));
1081   EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true));
1082   EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true));
1083   EXPECT_EQ("/a/c", remove_dots("/../../a/c", true));
1084   EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true));
1085
1086   SmallString<64> Path1("././c");
1087   EXPECT_TRUE(path::remove_dots(Path1, true));
1088   EXPECT_EQ("c", Path1);
1089 #endif
1090 }
1091
1092 TEST(Support, ReplacePathPrefix) {
1093   SmallString<64> Path1("/foo");
1094   SmallString<64> Path2("/old/foo");
1095   SmallString<64> OldPrefix("/old");
1096   SmallString<64> NewPrefix("/new");
1097   SmallString<64> NewPrefix2("/longernew");
1098   SmallString<64> EmptyPrefix("");
1099
1100   SmallString<64> Path = Path1;
1101   path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1102   EXPECT_EQ(Path, "/foo");
1103   Path = Path2;
1104   path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1105   EXPECT_EQ(Path, "/new/foo");
1106   Path = Path2;
1107   path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
1108   EXPECT_EQ(Path, "/longernew/foo");
1109   Path = Path1;
1110   path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
1111   EXPECT_EQ(Path, "/new/foo");
1112   Path = Path2;
1113   path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
1114   EXPECT_EQ(Path, "/foo");
1115 }
1116
1117 TEST_F(FileSystemTest, PathFromFD) {
1118   // Create a temp file.
1119   int FileDescriptor;
1120   SmallString<64> TempPath;
1121   ASSERT_NO_ERROR(
1122       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1123   FileRemover Cleanup(TempPath);
1124
1125   // Make sure it exists.
1126   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1127
1128   // Try to get the path from the file descriptor
1129   SmallString<64> ResultPath;
1130   std::error_code ErrorCode =
1131       fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1132
1133   // If we succeeded, check that the paths are the same (modulo case):
1134   if (!ErrorCode) {
1135     // The paths returned by createTemporaryFile and getPathFromOpenFD
1136     // should reference the same file on disk.
1137     fs::UniqueID D1, D2;
1138     ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1139     ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1140     ASSERT_EQ(D1, D2);
1141   }
1142
1143   ::close(FileDescriptor);
1144 }
1145
1146 TEST_F(FileSystemTest, PathFromFDWin32) {
1147   // Create a temp file.
1148   int FileDescriptor;
1149   SmallString<64> TempPath;
1150   ASSERT_NO_ERROR(
1151     fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1152   FileRemover Cleanup(TempPath);
1153
1154   // Make sure it exists.
1155   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1156   
1157   SmallVector<char, 8> ResultPath;
1158   std::error_code ErrorCode =
1159     fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1160
1161   if (!ErrorCode) {
1162     // Now that we know how much space is required for the path, create a path
1163     // buffer with exactly enough space (sans null terminator, which should not
1164     // be present), and call getPathFromOpenFD again to ensure that the API
1165     // properly handles exactly-sized buffers.
1166     SmallVector<char, 8> ExactSizedPath(ResultPath.size());
1167     ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath);
1168     ResultPath = ExactSizedPath;
1169   }
1170
1171   if (!ErrorCode) {
1172     fs::UniqueID D1, D2;
1173     ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1174     ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1175     ASSERT_EQ(D1, D2);
1176   }
1177   ::close(FileDescriptor);
1178 }
1179
1180 TEST_F(FileSystemTest, PathFromFDUnicode) {
1181   // Create a temp file.
1182   int FileDescriptor;
1183   SmallString<64> TempPath;
1184
1185   // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0"
1186   ASSERT_NO_ERROR(
1187     fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
1188                             "\xE2\x84\xB5.0", FileDescriptor, TempPath));
1189   FileRemover Cleanup(TempPath);
1190
1191   // Make sure it exists.
1192   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1193
1194   SmallVector<char, 8> ResultPath;
1195   std::error_code ErrorCode =
1196     fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1197
1198   if (!ErrorCode) {
1199     fs::UniqueID D1, D2;
1200     ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1201     ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1202     ASSERT_EQ(D1, D2);
1203   }
1204   ::close(FileDescriptor);
1205 }
1206
1207 TEST_F(FileSystemTest, OpenFileForRead) {
1208   // Create a temp file.
1209   int FileDescriptor;
1210   SmallString<64> TempPath;
1211   ASSERT_NO_ERROR(
1212       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1213   FileRemover Cleanup(TempPath);
1214
1215   // Make sure it exists.
1216   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1217
1218   // Open the file for read
1219   int FileDescriptor2;
1220   SmallString<64> ResultPath;
1221   ASSERT_NO_ERROR(
1222       fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath))
1223
1224   // If we succeeded, check that the paths are the same (modulo case):
1225   if (!ResultPath.empty()) {
1226     // The paths returned by createTemporaryFile and getPathFromOpenFD
1227     // should reference the same file on disk.
1228     fs::UniqueID D1, D2;
1229     ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1230     ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1231     ASSERT_EQ(D1, D2);
1232   }
1233
1234   ::close(FileDescriptor);
1235 }
1236
1237 TEST_F(FileSystemTest, set_current_path) {
1238   SmallString<128> path;
1239
1240   ASSERT_NO_ERROR(fs::current_path(path));
1241   ASSERT_NE(TestDirectory, path);
1242
1243   struct RestorePath {
1244     SmallString<128> path;
1245     RestorePath(const SmallString<128> &path) : path(path) {}
1246     ~RestorePath() { fs::set_current_path(path); }
1247   } restore_path(path);
1248
1249   ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
1250
1251   ASSERT_NO_ERROR(fs::current_path(path));
1252
1253   fs::UniqueID D1, D2;
1254   ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1));
1255   ASSERT_NO_ERROR(fs::getUniqueID(path, D2));
1256   ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path;\r
1257 }\r
1258 \r
1259 TEST_F(FileSystemTest, permissions) {\r
1260   int FD;\r
1261   SmallString<64> TempPath;\r
1262   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));\r
1263   FileRemover Cleanup(TempPath);\r
1264 \r
1265   // Make sure it exists.\r
1266   ASSERT_TRUE(fs::exists(Twine(TempPath)));\r
1267 \r
1268   auto CheckPermissions = [&](fs::perms Expected) {\r
1269     ErrorOr<fs::perms> Actual = fs::getPermissions(TempPath);\r
1270     return Actual && *Actual == Expected;\r
1271   };\r
1272 \r
1273   std::error_code NoError;\r
1274   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all), NoError);\r
1275   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1276 \r
1277   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::all_exe), NoError);\r
1278   EXPECT_TRUE(CheckPermissions(fs::all_read | fs::all_exe));\r
1279 \r
1280 #if defined(LLVM_ON_WIN32)\r
1281   fs::perms ReadOnly = fs::all_read | fs::all_exe;\r
1282   EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);\r
1283   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1284 \r
1285   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);\r
1286   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1287 \r
1288   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);\r
1289   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1290 \r
1291   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);\r
1292   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1293 \r
1294   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);\r
1295   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1296 \r
1297   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);\r
1298   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1299 \r
1300   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);\r
1301   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1302 \r
1303   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);\r
1304   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1305 \r
1306   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);\r
1307   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1308 \r
1309   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);\r
1310   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1311 \r
1312   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);\r
1313   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1314 \r
1315   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);\r
1316   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1317 \r
1318   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);\r
1319   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1320 \r
1321   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);\r
1322   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1323 \r
1324   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);\r
1325   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1326 \r
1327   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);\r
1328   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1329 \r
1330   EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);\r
1331   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1332 \r
1333   EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);\r
1334   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1335 \r
1336   EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);\r
1337   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1338 \r
1339   EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |\r
1340                                              fs::set_gid_on_exe |\r
1341                                              fs::sticky_bit),\r
1342             NoError);\r
1343   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1344 \r
1345   EXPECT_EQ(fs::setPermissions(TempPath, ReadOnly | fs::set_uid_on_exe |\r
1346                                              fs::set_gid_on_exe |\r
1347                                              fs::sticky_bit),\r
1348             NoError);\r
1349   EXPECT_TRUE(CheckPermissions(ReadOnly));\r
1350 \r
1351   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all | fs::set_uid_on_exe |\r
1352                                              fs::set_gid_on_exe |\r
1353                                              fs::sticky_bit),\r
1354             NoError);\r
1355   EXPECT_TRUE(CheckPermissions(fs::all_all));\r
1356 #else\r
1357   EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);\r
1358   EXPECT_TRUE(CheckPermissions(fs::no_perms));\r
1359 \r
1360   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);\r
1361   EXPECT_TRUE(CheckPermissions(fs::owner_read));\r
1362 \r
1363   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);\r
1364   EXPECT_TRUE(CheckPermissions(fs::owner_write));\r
1365 \r
1366   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);\r
1367   EXPECT_TRUE(CheckPermissions(fs::owner_exe));\r
1368 \r
1369   EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);\r
1370   EXPECT_TRUE(CheckPermissions(fs::owner_all));\r
1371 \r
1372   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);\r
1373   EXPECT_TRUE(CheckPermissions(fs::group_read));\r
1374 \r
1375   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);\r
1376   EXPECT_TRUE(CheckPermissions(fs::group_write));\r
1377 \r
1378   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);\r
1379   EXPECT_TRUE(CheckPermissions(fs::group_exe));\r
1380 \r
1381   EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);\r
1382   EXPECT_TRUE(CheckPermissions(fs::group_all));\r
1383 \r
1384   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);\r
1385   EXPECT_TRUE(CheckPermissions(fs::others_read));\r
1386 \r
1387   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);\r
1388   EXPECT_TRUE(CheckPermissions(fs::others_write));\r
1389 \r
1390   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);\r
1391   EXPECT_TRUE(CheckPermissions(fs::others_exe));\r
1392 \r
1393   EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);\r
1394   EXPECT_TRUE(CheckPermissions(fs::others_all));\r
1395 \r
1396   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);\r
1397   EXPECT_TRUE(CheckPermissions(fs::all_read));\r
1398 \r
1399   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);\r
1400   EXPECT_TRUE(CheckPermissions(fs::all_write));\r
1401 \r
1402   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);\r
1403   EXPECT_TRUE(CheckPermissions(fs::all_exe));\r
1404 \r
1405   EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);\r
1406   EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe));\r
1407 \r
1408   EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);\r
1409   EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe));\r
1410 \r
1411   EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);\r
1412   EXPECT_TRUE(CheckPermissions(fs::sticky_bit));\r
1413 \r
1414   EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |\r
1415                                              fs::set_gid_on_exe |\r
1416                                              fs::sticky_bit),\r
1417             NoError);\r
1418   EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe | fs::set_gid_on_exe |\r
1419                                fs::sticky_bit));\r
1420 \r
1421   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::set_uid_on_exe |\r
1422                                              fs::set_gid_on_exe |\r
1423                                              fs::sticky_bit),\r
1424             NoError);\r
1425   EXPECT_TRUE(CheckPermissions(fs::all_read | fs::set_uid_on_exe |\r
1426                                fs::set_gid_on_exe | fs::sticky_bit));\r
1427 \r
1428   EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all | fs::set_uid_on_exe |\r
1429                                              fs::set_gid_on_exe |\r
1430                                              fs::sticky_bit),\r
1431             NoError);\r
1432   EXPECT_TRUE(CheckPermissions(fs::all_all | fs::set_uid_on_exe |\r
1433                                fs::set_gid_on_exe | fs::sticky_bit));\r
1434 #endif\r
1435 }\r
1436 \r
1437 } // anonymous namespace\r