xref: /minix3/external/bsd/llvm/dist/llvm/unittests/Support/Path.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
11*0a6a1f1dSLionel Sambuc #include "llvm/Support/Errc.h"
12f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
13f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
14f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
15f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
16f4a2713aSLionel Sambuc #include "gtest/gtest.h"
17f4a2713aSLionel Sambuc 
18*0a6a1f1dSLionel Sambuc #ifdef LLVM_ON_WIN32
19*0a6a1f1dSLionel Sambuc #include <Windows.h>
20*0a6a1f1dSLionel Sambuc #include <winerror.h>
21*0a6a1f1dSLionel Sambuc #endif
22*0a6a1f1dSLionel Sambuc 
23f4a2713aSLionel Sambuc using namespace llvm;
24f4a2713aSLionel Sambuc using namespace llvm::sys;
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc #define ASSERT_NO_ERROR(x)                                                     \
27*0a6a1f1dSLionel Sambuc   if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
28f4a2713aSLionel Sambuc     SmallString<128> MessageStorage;                                           \
29f4a2713aSLionel Sambuc     raw_svector_ostream Message(MessageStorage);                               \
30f4a2713aSLionel Sambuc     Message << #x ": did not return errc::success.\n"                          \
31f4a2713aSLionel Sambuc             << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"          \
32f4a2713aSLionel Sambuc             << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";      \
33f4a2713aSLionel Sambuc     GTEST_FATAL_FAILURE_(MessageStorage.c_str());                              \
34*0a6a1f1dSLionel Sambuc   } else {                                                                     \
35*0a6a1f1dSLionel Sambuc   }
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc namespace {
38f4a2713aSLionel Sambuc 
TEST(is_separator,Works)39f4a2713aSLionel Sambuc TEST(is_separator, Works) {
40f4a2713aSLionel Sambuc   EXPECT_TRUE(path::is_separator('/'));
41f4a2713aSLionel Sambuc   EXPECT_FALSE(path::is_separator('\0'));
42f4a2713aSLionel Sambuc   EXPECT_FALSE(path::is_separator('-'));
43f4a2713aSLionel Sambuc   EXPECT_FALSE(path::is_separator(' '));
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
46f4a2713aSLionel Sambuc   EXPECT_TRUE(path::is_separator('\\'));
47f4a2713aSLionel Sambuc #else
48f4a2713aSLionel Sambuc   EXPECT_FALSE(path::is_separator('\\'));
49f4a2713aSLionel Sambuc #endif
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc 
TEST(Support,Path)52f4a2713aSLionel Sambuc TEST(Support, Path) {
53f4a2713aSLionel Sambuc   SmallVector<StringRef, 40> paths;
54f4a2713aSLionel Sambuc   paths.push_back("");
55f4a2713aSLionel Sambuc   paths.push_back(".");
56f4a2713aSLionel Sambuc   paths.push_back("..");
57f4a2713aSLionel Sambuc   paths.push_back("foo");
58f4a2713aSLionel Sambuc   paths.push_back("/");
59f4a2713aSLionel Sambuc   paths.push_back("/foo");
60f4a2713aSLionel Sambuc   paths.push_back("foo/");
61f4a2713aSLionel Sambuc   paths.push_back("/foo/");
62f4a2713aSLionel Sambuc   paths.push_back("foo/bar");
63f4a2713aSLionel Sambuc   paths.push_back("/foo/bar");
64f4a2713aSLionel Sambuc   paths.push_back("//net");
65f4a2713aSLionel Sambuc   paths.push_back("//net/foo");
66f4a2713aSLionel Sambuc   paths.push_back("///foo///");
67f4a2713aSLionel Sambuc   paths.push_back("///foo///bar");
68f4a2713aSLionel Sambuc   paths.push_back("/.");
69f4a2713aSLionel Sambuc   paths.push_back("./");
70f4a2713aSLionel Sambuc   paths.push_back("/..");
71f4a2713aSLionel Sambuc   paths.push_back("../");
72f4a2713aSLionel Sambuc   paths.push_back("foo/.");
73f4a2713aSLionel Sambuc   paths.push_back("foo/..");
74f4a2713aSLionel Sambuc   paths.push_back("foo/./");
75f4a2713aSLionel Sambuc   paths.push_back("foo/./bar");
76f4a2713aSLionel Sambuc   paths.push_back("foo/..");
77f4a2713aSLionel Sambuc   paths.push_back("foo/../");
78f4a2713aSLionel Sambuc   paths.push_back("foo/../bar");
79f4a2713aSLionel Sambuc   paths.push_back("c:");
80f4a2713aSLionel Sambuc   paths.push_back("c:/");
81f4a2713aSLionel Sambuc   paths.push_back("c:foo");
82f4a2713aSLionel Sambuc   paths.push_back("c:/foo");
83f4a2713aSLionel Sambuc   paths.push_back("c:foo/");
84f4a2713aSLionel Sambuc   paths.push_back("c:/foo/");
85f4a2713aSLionel Sambuc   paths.push_back("c:/foo/bar");
86f4a2713aSLionel Sambuc   paths.push_back("prn:");
87f4a2713aSLionel Sambuc   paths.push_back("c:\\");
88f4a2713aSLionel Sambuc   paths.push_back("c:foo");
89f4a2713aSLionel Sambuc   paths.push_back("c:\\foo");
90f4a2713aSLionel Sambuc   paths.push_back("c:foo\\");
91f4a2713aSLionel Sambuc   paths.push_back("c:\\foo\\");
92f4a2713aSLionel Sambuc   paths.push_back("c:\\foo/");
93f4a2713aSLionel Sambuc   paths.push_back("c:/foo\\bar");
94f4a2713aSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc   SmallVector<StringRef, 5> ComponentStack;
96f4a2713aSLionel Sambuc   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
97f4a2713aSLionel Sambuc                                                   e = paths.end();
98f4a2713aSLionel Sambuc                                                   i != e;
99f4a2713aSLionel Sambuc                                                   ++i) {
100f4a2713aSLionel Sambuc     for (sys::path::const_iterator ci = sys::path::begin(*i),
101f4a2713aSLionel Sambuc                                    ce = sys::path::end(*i);
102f4a2713aSLionel Sambuc                                    ci != ce;
103f4a2713aSLionel Sambuc                                    ++ci) {
104f4a2713aSLionel Sambuc       ASSERT_FALSE(ci->empty());
105*0a6a1f1dSLionel Sambuc       ComponentStack.push_back(*ci);
106f4a2713aSLionel Sambuc     }
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc     for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
109f4a2713aSLionel Sambuc                                      ce = sys::path::rend(*i);
110f4a2713aSLionel Sambuc                                      ci != ce;
111f4a2713aSLionel Sambuc                                      ++ci) {
112*0a6a1f1dSLionel Sambuc       ASSERT_TRUE(*ci == ComponentStack.back());
113*0a6a1f1dSLionel Sambuc       ComponentStack.pop_back();
114f4a2713aSLionel Sambuc     }
115*0a6a1f1dSLionel Sambuc     ASSERT_TRUE(ComponentStack.empty());
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc     path::has_root_path(*i);
118f4a2713aSLionel Sambuc     path::root_path(*i);
119f4a2713aSLionel Sambuc     path::has_root_name(*i);
120f4a2713aSLionel Sambuc     path::root_name(*i);
121f4a2713aSLionel Sambuc     path::has_root_directory(*i);
122f4a2713aSLionel Sambuc     path::root_directory(*i);
123f4a2713aSLionel Sambuc     path::has_parent_path(*i);
124f4a2713aSLionel Sambuc     path::parent_path(*i);
125f4a2713aSLionel Sambuc     path::has_filename(*i);
126f4a2713aSLionel Sambuc     path::filename(*i);
127f4a2713aSLionel Sambuc     path::has_stem(*i);
128f4a2713aSLionel Sambuc     path::stem(*i);
129f4a2713aSLionel Sambuc     path::has_extension(*i);
130f4a2713aSLionel Sambuc     path::extension(*i);
131f4a2713aSLionel Sambuc     path::is_absolute(*i);
132f4a2713aSLionel Sambuc     path::is_relative(*i);
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc     SmallString<128> temp_store;
135f4a2713aSLionel Sambuc     temp_store = *i;
136f4a2713aSLionel Sambuc     ASSERT_NO_ERROR(fs::make_absolute(temp_store));
137f4a2713aSLionel Sambuc     temp_store = *i;
138f4a2713aSLionel Sambuc     path::remove_filename(temp_store);
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc     temp_store = *i;
141f4a2713aSLionel Sambuc     path::replace_extension(temp_store, "ext");
142f4a2713aSLionel Sambuc     StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
143f4a2713aSLionel Sambuc     stem = path::stem(filename);
144f4a2713aSLionel Sambuc     ext  = path::extension(filename);
145*0a6a1f1dSLionel Sambuc     EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc     path::native(*i, temp_store);
148f4a2713aSLionel Sambuc   }
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc 
TEST(Support,RelativePathIterator)151f4a2713aSLionel Sambuc TEST(Support, RelativePathIterator) {
152f4a2713aSLionel Sambuc   SmallString<64> Path(StringRef("c/d/e/foo.txt"));
153f4a2713aSLionel Sambuc   typedef SmallVector<StringRef, 4> PathComponents;
154f4a2713aSLionel Sambuc   PathComponents ExpectedPathComponents;
155f4a2713aSLionel Sambuc   PathComponents ActualPathComponents;
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   StringRef(Path).split(ExpectedPathComponents, "/");
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
160f4a2713aSLionel Sambuc        ++I) {
161f4a2713aSLionel Sambuc     ActualPathComponents.push_back(*I);
162f4a2713aSLionel Sambuc   }
163f4a2713aSLionel Sambuc 
164f4a2713aSLionel Sambuc   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
167f4a2713aSLionel Sambuc     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
168f4a2713aSLionel Sambuc   }
169f4a2713aSLionel Sambuc }
170f4a2713aSLionel Sambuc 
TEST(Support,AbsolutePathIterator)171f4a2713aSLionel Sambuc TEST(Support, AbsolutePathIterator) {
172f4a2713aSLionel Sambuc   SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
173f4a2713aSLionel Sambuc   typedef SmallVector<StringRef, 4> PathComponents;
174f4a2713aSLionel Sambuc   PathComponents ExpectedPathComponents;
175f4a2713aSLionel Sambuc   PathComponents ActualPathComponents;
176f4a2713aSLionel Sambuc 
177f4a2713aSLionel Sambuc   StringRef(Path).split(ExpectedPathComponents, "/");
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc   // The root path will also be a component when iterating
180f4a2713aSLionel Sambuc   ExpectedPathComponents[0] = "/";
181f4a2713aSLionel Sambuc 
182f4a2713aSLionel Sambuc   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
183f4a2713aSLionel Sambuc        ++I) {
184f4a2713aSLionel Sambuc     ActualPathComponents.push_back(*I);
185f4a2713aSLionel Sambuc   }
186f4a2713aSLionel Sambuc 
187f4a2713aSLionel Sambuc   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
188f4a2713aSLionel Sambuc 
189f4a2713aSLionel Sambuc   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
190f4a2713aSLionel Sambuc     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
191f4a2713aSLionel Sambuc   }
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc 
194f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
TEST(Support,AbsolutePathIteratorWin32)195f4a2713aSLionel Sambuc TEST(Support, AbsolutePathIteratorWin32) {
196f4a2713aSLionel Sambuc   SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
197f4a2713aSLionel Sambuc   typedef SmallVector<StringRef, 4> PathComponents;
198f4a2713aSLionel Sambuc   PathComponents ExpectedPathComponents;
199f4a2713aSLionel Sambuc   PathComponents ActualPathComponents;
200f4a2713aSLionel Sambuc 
201f4a2713aSLionel Sambuc   StringRef(Path).split(ExpectedPathComponents, "\\");
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc   // The root path (which comes after the drive name) will also be a component
204f4a2713aSLionel Sambuc   // when iterating.
205f4a2713aSLionel Sambuc   ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
206f4a2713aSLionel Sambuc 
207f4a2713aSLionel Sambuc   for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
208f4a2713aSLionel Sambuc        ++I) {
209f4a2713aSLionel Sambuc     ActualPathComponents.push_back(*I);
210f4a2713aSLionel Sambuc   }
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc   ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc   for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
215f4a2713aSLionel Sambuc     EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
216f4a2713aSLionel Sambuc   }
217f4a2713aSLionel Sambuc }
218f4a2713aSLionel Sambuc #endif // LLVM_ON_WIN32
219f4a2713aSLionel Sambuc 
TEST(Support,AbsolutePathIteratorEnd)220*0a6a1f1dSLionel Sambuc TEST(Support, AbsolutePathIteratorEnd) {
221*0a6a1f1dSLionel Sambuc   // Trailing slashes are converted to '.' unless they are part of the root path.
222*0a6a1f1dSLionel Sambuc   SmallVector<StringRef, 4> Paths;
223*0a6a1f1dSLionel Sambuc   Paths.push_back("/foo/");
224*0a6a1f1dSLionel Sambuc   Paths.push_back("/foo//");
225*0a6a1f1dSLionel Sambuc   Paths.push_back("//net//");
226*0a6a1f1dSLionel Sambuc #ifdef LLVM_ON_WIN32
227*0a6a1f1dSLionel Sambuc   Paths.push_back("c:\\\\");
228*0a6a1f1dSLionel Sambuc #endif
229*0a6a1f1dSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc   for (StringRef Path : Paths) {
231*0a6a1f1dSLionel Sambuc     StringRef LastComponent = *path::rbegin(Path);
232*0a6a1f1dSLionel Sambuc     EXPECT_EQ(".", LastComponent);
233*0a6a1f1dSLionel Sambuc   }
234*0a6a1f1dSLionel Sambuc 
235*0a6a1f1dSLionel Sambuc   SmallVector<StringRef, 3> RootPaths;
236*0a6a1f1dSLionel Sambuc   RootPaths.push_back("/");
237*0a6a1f1dSLionel Sambuc   RootPaths.push_back("//net/");
238*0a6a1f1dSLionel Sambuc #ifdef LLVM_ON_WIN32
239*0a6a1f1dSLionel Sambuc   RootPaths.push_back("c:\\");
240*0a6a1f1dSLionel Sambuc #endif
241*0a6a1f1dSLionel Sambuc 
242*0a6a1f1dSLionel Sambuc   for (StringRef Path : RootPaths) {
243*0a6a1f1dSLionel Sambuc     StringRef LastComponent = *path::rbegin(Path);
244*0a6a1f1dSLionel Sambuc     EXPECT_EQ(1u, LastComponent.size());
245*0a6a1f1dSLionel Sambuc     EXPECT_TRUE(path::is_separator(LastComponent[0]));
246*0a6a1f1dSLionel Sambuc   }
247*0a6a1f1dSLionel Sambuc }
248*0a6a1f1dSLionel Sambuc 
TEST(Support,HomeDirectory)249*0a6a1f1dSLionel Sambuc TEST(Support, HomeDirectory) {
250*0a6a1f1dSLionel Sambuc #ifdef LLVM_ON_UNIX
251*0a6a1f1dSLionel Sambuc   // This test only makes sense on Unix if $HOME is set.
252*0a6a1f1dSLionel Sambuc   if (::getenv("HOME")) {
253*0a6a1f1dSLionel Sambuc #endif
254*0a6a1f1dSLionel Sambuc     SmallString<128> HomeDir;
255*0a6a1f1dSLionel Sambuc     EXPECT_TRUE(path::home_directory(HomeDir));
256*0a6a1f1dSLionel Sambuc     EXPECT_FALSE(HomeDir.empty());
257*0a6a1f1dSLionel Sambuc #ifdef LLVM_ON_UNIX
258*0a6a1f1dSLionel Sambuc   }
259*0a6a1f1dSLionel Sambuc #endif
260*0a6a1f1dSLionel Sambuc }
261*0a6a1f1dSLionel Sambuc 
262f4a2713aSLionel Sambuc class FileSystemTest : public testing::Test {
263f4a2713aSLionel Sambuc protected:
264f4a2713aSLionel Sambuc   /// Unique temporary directory in which all created filesystem entities must
265*0a6a1f1dSLionel Sambuc   /// be placed. It is removed at the end of each test (must be empty).
266f4a2713aSLionel Sambuc   SmallString<128> TestDirectory;
267f4a2713aSLionel Sambuc 
SetUp()268f4a2713aSLionel Sambuc   virtual void SetUp() {
269f4a2713aSLionel Sambuc     ASSERT_NO_ERROR(
270f4a2713aSLionel Sambuc         fs::createUniqueDirectory("file-system-test", TestDirectory));
271f4a2713aSLionel Sambuc     // We don't care about this specific file.
272f4a2713aSLionel Sambuc     errs() << "Test Directory: " << TestDirectory << '\n';
273f4a2713aSLionel Sambuc     errs().flush();
274f4a2713aSLionel Sambuc   }
275f4a2713aSLionel Sambuc 
TearDown()276f4a2713aSLionel Sambuc   virtual void TearDown() {
277*0a6a1f1dSLionel Sambuc     ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
278f4a2713aSLionel Sambuc   }
279f4a2713aSLionel Sambuc };
280f4a2713aSLionel Sambuc 
TEST_F(FileSystemTest,Unique)281f4a2713aSLionel Sambuc TEST_F(FileSystemTest, Unique) {
282f4a2713aSLionel Sambuc   // Create a temp file.
283f4a2713aSLionel Sambuc   int FileDescriptor;
284f4a2713aSLionel Sambuc   SmallString<64> TempPath;
285f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(
286f4a2713aSLionel Sambuc       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
287f4a2713aSLionel Sambuc 
288f4a2713aSLionel Sambuc   // The same file should return an identical unique id.
289f4a2713aSLionel Sambuc   fs::UniqueID F1, F2;
290f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
291f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
292f4a2713aSLionel Sambuc   ASSERT_EQ(F1, F2);
293f4a2713aSLionel Sambuc 
294f4a2713aSLionel Sambuc   // Different files should return different unique ids.
295f4a2713aSLionel Sambuc   int FileDescriptor2;
296f4a2713aSLionel Sambuc   SmallString<64> TempPath2;
297f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(
298f4a2713aSLionel Sambuc       fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc   fs::UniqueID D;
301f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
302f4a2713aSLionel Sambuc   ASSERT_NE(D, F1);
303f4a2713aSLionel Sambuc   ::close(FileDescriptor2);
304f4a2713aSLionel Sambuc 
305f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
306f4a2713aSLionel Sambuc 
307f4a2713aSLionel Sambuc   // Two paths representing the same file on disk should still provide the
308f4a2713aSLionel Sambuc   // same unique id.  We can test this by making a hard link.
309*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
310f4a2713aSLionel Sambuc   fs::UniqueID D2;
311f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
312f4a2713aSLionel Sambuc   ASSERT_EQ(D2, F1);
313f4a2713aSLionel Sambuc 
314f4a2713aSLionel Sambuc   ::close(FileDescriptor);
315f4a2713aSLionel Sambuc 
316f4a2713aSLionel Sambuc   SmallString<128> Dir1;
317f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(
318f4a2713aSLionel Sambuc      fs::createUniqueDirectory("dir1", Dir1));
319f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
320f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
321f4a2713aSLionel Sambuc   ASSERT_EQ(F1, F2);
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   SmallString<128> Dir2;
324f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(
325f4a2713aSLionel Sambuc      fs::createUniqueDirectory("dir2", Dir2));
326f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
327f4a2713aSLionel Sambuc   ASSERT_NE(F1, F2);
328f4a2713aSLionel Sambuc }
329f4a2713aSLionel Sambuc 
TEST_F(FileSystemTest,TempFiles)330f4a2713aSLionel Sambuc TEST_F(FileSystemTest, TempFiles) {
331f4a2713aSLionel Sambuc   // Create a temp file.
332f4a2713aSLionel Sambuc   int FileDescriptor;
333f4a2713aSLionel Sambuc   SmallString<64> TempPath;
334f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(
335f4a2713aSLionel Sambuc       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc   // Make sure it exists.
338*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
339f4a2713aSLionel Sambuc 
340f4a2713aSLionel Sambuc   // Create another temp tile.
341f4a2713aSLionel Sambuc   int FD2;
342f4a2713aSLionel Sambuc   SmallString<64> TempPath2;
343f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
344f4a2713aSLionel Sambuc   ASSERT_TRUE(TempPath2.endswith(".temp"));
345f4a2713aSLionel Sambuc   ASSERT_NE(TempPath.str(), TempPath2.str());
346f4a2713aSLionel Sambuc 
347f4a2713aSLionel Sambuc   fs::file_status A, B;
348f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
349f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
350f4a2713aSLionel Sambuc   EXPECT_FALSE(fs::equivalent(A, B));
351f4a2713aSLionel Sambuc 
352f4a2713aSLionel Sambuc   ::close(FD2);
353f4a2713aSLionel Sambuc 
354f4a2713aSLionel Sambuc   // Remove Temp2.
355*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
356*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
357*0a6a1f1dSLionel Sambuc   ASSERT_EQ(fs::remove(Twine(TempPath2), false),
358*0a6a1f1dSLionel Sambuc             errc::no_such_file_or_directory);
359f4a2713aSLionel Sambuc 
360*0a6a1f1dSLionel Sambuc   std::error_code EC = fs::status(TempPath2.c_str(), B);
361f4a2713aSLionel Sambuc   EXPECT_EQ(EC, errc::no_such_file_or_directory);
362f4a2713aSLionel Sambuc   EXPECT_EQ(B.type(), fs::file_type::file_not_found);
363f4a2713aSLionel Sambuc 
364f4a2713aSLionel Sambuc   // Make sure Temp2 doesn't exist.
365*0a6a1f1dSLionel Sambuc   ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
366*0a6a1f1dSLionel Sambuc             errc::no_such_file_or_directory);
367f4a2713aSLionel Sambuc 
368f4a2713aSLionel Sambuc   SmallString<64> TempPath3;
369f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
370f4a2713aSLionel Sambuc   ASSERT_FALSE(TempPath3.endswith("."));
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc   // Create a hard link to Temp1.
373*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
374f4a2713aSLionel Sambuc   bool equal;
375f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
376f4a2713aSLionel Sambuc   EXPECT_TRUE(equal);
377f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
378f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
379f4a2713aSLionel Sambuc   EXPECT_TRUE(fs::equivalent(A, B));
380f4a2713aSLionel Sambuc 
381f4a2713aSLionel Sambuc   // Remove Temp1.
382f4a2713aSLionel Sambuc   ::close(FileDescriptor);
383*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
384f4a2713aSLionel Sambuc 
385f4a2713aSLionel Sambuc   // Remove the hard link.
386*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
387f4a2713aSLionel Sambuc 
388f4a2713aSLionel Sambuc   // Make sure Temp1 doesn't exist.
389*0a6a1f1dSLionel Sambuc   ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
390*0a6a1f1dSLionel Sambuc             errc::no_such_file_or_directory);
391f4a2713aSLionel Sambuc 
392f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
393f4a2713aSLionel Sambuc   // Path name > 260 chars should get an error.
394f4a2713aSLionel Sambuc   const char *Path270 =
395f4a2713aSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
396f4a2713aSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
397f4a2713aSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
398f4a2713aSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
399f4a2713aSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
400*0a6a1f1dSLionel Sambuc   EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
401*0a6a1f1dSLionel Sambuc             errc::invalid_argument);
402*0a6a1f1dSLionel Sambuc   // Relative path < 247 chars, no problem.
403*0a6a1f1dSLionel Sambuc   const char *Path216 =
404*0a6a1f1dSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
405*0a6a1f1dSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
406*0a6a1f1dSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
407*0a6a1f1dSLionel Sambuc     "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
408*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
409*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
410*0a6a1f1dSLionel Sambuc #endif
411*0a6a1f1dSLionel Sambuc }
412*0a6a1f1dSLionel Sambuc 
TEST_F(FileSystemTest,CreateDir)413*0a6a1f1dSLionel Sambuc TEST_F(FileSystemTest, CreateDir) {
414*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
415*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
416*0a6a1f1dSLionel Sambuc   ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
417*0a6a1f1dSLionel Sambuc             errc::file_exists);
418*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
419*0a6a1f1dSLionel Sambuc 
420*0a6a1f1dSLionel Sambuc #ifdef LLVM_ON_WIN32
421*0a6a1f1dSLionel Sambuc   // Prove that create_directories() can handle a pathname > 248 characters,
422*0a6a1f1dSLionel Sambuc   // which is the documented limit for CreateDirectory().
423*0a6a1f1dSLionel Sambuc   // (248 is MAX_PATH subtracting room for an 8.3 filename.)
424*0a6a1f1dSLionel Sambuc   // Generate a directory path guaranteed to fall into that range.
425*0a6a1f1dSLionel Sambuc   size_t TmpLen = TestDirectory.size();
426*0a6a1f1dSLionel Sambuc   const char *OneDir = "\\123456789";
427*0a6a1f1dSLionel Sambuc   size_t OneDirLen = strlen(OneDir);
428*0a6a1f1dSLionel Sambuc   ASSERT_LT(OneDirLen, 12U);
429*0a6a1f1dSLionel Sambuc   size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
430*0a6a1f1dSLionel Sambuc   SmallString<260> LongDir(TestDirectory);
431*0a6a1f1dSLionel Sambuc   for (size_t I = 0; I < NLevels; ++I)
432*0a6a1f1dSLionel Sambuc     LongDir.append(OneDir);
433*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
434*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
435*0a6a1f1dSLionel Sambuc   ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
436*0a6a1f1dSLionel Sambuc             errc::file_exists);
437*0a6a1f1dSLionel Sambuc   // Tidy up, "recursively" removing the directories.
438*0a6a1f1dSLionel Sambuc   StringRef ThisDir(LongDir);
439*0a6a1f1dSLionel Sambuc   for (size_t J = 0; J < NLevels; ++J) {
440*0a6a1f1dSLionel Sambuc     ASSERT_NO_ERROR(fs::remove(ThisDir));
441*0a6a1f1dSLionel Sambuc     ThisDir = path::parent_path(ThisDir);
442*0a6a1f1dSLionel Sambuc   }
443*0a6a1f1dSLionel Sambuc 
444*0a6a1f1dSLionel Sambuc   // Similarly for a relative pathname.  Need to set the current directory to
445*0a6a1f1dSLionel Sambuc   // TestDirectory so that the one we create ends up in the right place.
446*0a6a1f1dSLionel Sambuc   char PreviousDir[260];
447*0a6a1f1dSLionel Sambuc   size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
448*0a6a1f1dSLionel Sambuc   ASSERT_GT(PreviousDirLen, 0U);
449*0a6a1f1dSLionel Sambuc   ASSERT_LT(PreviousDirLen, 260U);
450*0a6a1f1dSLionel Sambuc   ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
451*0a6a1f1dSLionel Sambuc   LongDir.clear();
452*0a6a1f1dSLionel Sambuc   // Generate a relative directory name with absolute length > 248.
453*0a6a1f1dSLionel Sambuc   size_t LongDirLen = 249 - TestDirectory.size();
454*0a6a1f1dSLionel Sambuc   LongDir.assign(LongDirLen, 'a');
455*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
456*0a6a1f1dSLionel Sambuc   // While we're here, prove that .. and . handling works in these long paths.
457*0a6a1f1dSLionel Sambuc   const char *DotDotDirs = "\\..\\.\\b";
458*0a6a1f1dSLionel Sambuc   LongDir.append(DotDotDirs);
459*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_directory("b"));
460*0a6a1f1dSLionel Sambuc   ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
461*0a6a1f1dSLionel Sambuc   // And clean up.
462*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove("b"));
463*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(
464*0a6a1f1dSLionel Sambuc     Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
465*0a6a1f1dSLionel Sambuc   ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
466f4a2713aSLionel Sambuc #endif
467f4a2713aSLionel Sambuc }
468f4a2713aSLionel Sambuc 
TEST_F(FileSystemTest,DirectoryIteration)469f4a2713aSLionel Sambuc TEST_F(FileSystemTest, DirectoryIteration) {
470*0a6a1f1dSLionel Sambuc   std::error_code ec;
471f4a2713aSLionel Sambuc   for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
472f4a2713aSLionel Sambuc     ASSERT_NO_ERROR(ec);
473f4a2713aSLionel Sambuc 
474f4a2713aSLionel Sambuc   // Create a known hierarchy to recurse over.
475*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(
476*0a6a1f1dSLionel Sambuc       fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
477*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(
478*0a6a1f1dSLionel Sambuc       fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
479*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
480*0a6a1f1dSLionel Sambuc                                          "/recursive/dontlookhere/da1"));
481*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(
482*0a6a1f1dSLionel Sambuc       fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
483*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(
484*0a6a1f1dSLionel Sambuc       fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
485f4a2713aSLionel Sambuc   typedef std::vector<std::string> v_t;
486f4a2713aSLionel Sambuc   v_t visited;
487f4a2713aSLionel Sambuc   for (fs::recursive_directory_iterator i(Twine(TestDirectory)
488f4a2713aSLionel Sambuc          + "/recursive", ec), e; i != e; i.increment(ec)){
489f4a2713aSLionel Sambuc     ASSERT_NO_ERROR(ec);
490f4a2713aSLionel Sambuc     if (path::filename(i->path()) == "p1") {
491f4a2713aSLionel Sambuc       i.pop();
492f4a2713aSLionel Sambuc       // FIXME: recursive_directory_iterator should be more robust.
493f4a2713aSLionel Sambuc       if (i == e) break;
494f4a2713aSLionel Sambuc     }
495f4a2713aSLionel Sambuc     if (path::filename(i->path()) == "dontlookhere")
496f4a2713aSLionel Sambuc       i.no_push();
497f4a2713aSLionel Sambuc     visited.push_back(path::filename(i->path()));
498f4a2713aSLionel Sambuc   }
499f4a2713aSLionel Sambuc   v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
500f4a2713aSLionel Sambuc   v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
501f4a2713aSLionel Sambuc   v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
502f4a2713aSLionel Sambuc   v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
503f4a2713aSLionel Sambuc                                                "dontlookhere");
504f4a2713aSLionel Sambuc   v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
505f4a2713aSLionel Sambuc   v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
506f4a2713aSLionel Sambuc   v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
507f4a2713aSLionel Sambuc   v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
508f4a2713aSLionel Sambuc   v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
509f4a2713aSLionel Sambuc 
510f4a2713aSLionel Sambuc   // Make sure that each path was visited correctly.
511f4a2713aSLionel Sambuc   ASSERT_NE(a0, visited.end());
512f4a2713aSLionel Sambuc   ASSERT_NE(aa1, visited.end());
513f4a2713aSLionel Sambuc   ASSERT_NE(ab1, visited.end());
514f4a2713aSLionel Sambuc   ASSERT_NE(dontlookhere, visited.end());
515f4a2713aSLionel Sambuc   ASSERT_EQ(da1, visited.end()); // Not visited.
516f4a2713aSLionel Sambuc   ASSERT_NE(z0, visited.end());
517f4a2713aSLionel Sambuc   ASSERT_NE(za1, visited.end());
518f4a2713aSLionel Sambuc   ASSERT_NE(pop, visited.end());
519f4a2713aSLionel Sambuc   ASSERT_EQ(p1, visited.end()); // Not visited.
520f4a2713aSLionel Sambuc 
521f4a2713aSLionel Sambuc   // Make sure that parents were visited before children. No other ordering
522f4a2713aSLionel Sambuc   // guarantees can be made across siblings.
523f4a2713aSLionel Sambuc   ASSERT_LT(a0, aa1);
524f4a2713aSLionel Sambuc   ASSERT_LT(a0, ab1);
525f4a2713aSLionel Sambuc   ASSERT_LT(z0, za1);
526*0a6a1f1dSLionel Sambuc 
527*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
528*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
529*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
530*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(
531*0a6a1f1dSLionel Sambuc       fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
532*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
533*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
534*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
535*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
536*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
537*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
538f4a2713aSLionel Sambuc }
539f4a2713aSLionel Sambuc 
540f4a2713aSLionel Sambuc const char archive[] = "!<arch>\x0A";
541f4a2713aSLionel Sambuc const char bitcode[] = "\xde\xc0\x17\x0b";
542f4a2713aSLionel Sambuc const char coff_object[] = "\x00\x00......";
543*0a6a1f1dSLionel Sambuc const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
544*0a6a1f1dSLionel Sambuc     "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
545f4a2713aSLionel Sambuc const char coff_import_library[] = "\x00\x00\xff\xff....";
546f4a2713aSLionel Sambuc const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
547f4a2713aSLionel Sambuc                                  0,    0,   0,   0,   0, 0, 0, 0, 1 };
548f4a2713aSLionel Sambuc const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00";
549f4a2713aSLionel Sambuc const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01";
550f4a2713aSLionel Sambuc const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02";
551f4a2713aSLionel Sambuc const char macho_fixed_virtual_memory_shared_lib[] =
552f4a2713aSLionel Sambuc     "\xfe\xed\xfa\xce..........\x00\x03";
553f4a2713aSLionel Sambuc const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04";
554f4a2713aSLionel Sambuc const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05";
555f4a2713aSLionel Sambuc const char macho_dynamically_linked_shared_lib[] =
556f4a2713aSLionel Sambuc     "\xfe\xed\xfa\xce..........\x00\x06";
557f4a2713aSLionel Sambuc const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07";
558f4a2713aSLionel Sambuc const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08";
559f4a2713aSLionel Sambuc const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a";
560f4a2713aSLionel Sambuc const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
561*0a6a1f1dSLionel Sambuc const char macho_dynamically_linked_shared_lib_stub[] =
562*0a6a1f1dSLionel Sambuc     "\xfe\xed\xfa\xce..........\x00\x09";
563f4a2713aSLionel Sambuc 
TEST_F(FileSystemTest,Magic)564f4a2713aSLionel Sambuc TEST_F(FileSystemTest, Magic) {
565f4a2713aSLionel Sambuc   struct type {
566f4a2713aSLionel Sambuc     const char *filename;
567f4a2713aSLionel Sambuc     const char *magic_str;
568f4a2713aSLionel Sambuc     size_t magic_str_len;
569f4a2713aSLionel Sambuc     fs::file_magic magic;
570f4a2713aSLionel Sambuc   } types[] = {
571f4a2713aSLionel Sambuc #define DEFINE(magic)                                           \
572f4a2713aSLionel Sambuc     { #magic, magic, sizeof(magic), fs::file_magic::magic }
573f4a2713aSLionel Sambuc     DEFINE(archive),
574f4a2713aSLionel Sambuc     DEFINE(bitcode),
575f4a2713aSLionel Sambuc     DEFINE(coff_object),
576*0a6a1f1dSLionel Sambuc     { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
577f4a2713aSLionel Sambuc     DEFINE(coff_import_library),
578f4a2713aSLionel Sambuc     DEFINE(elf_relocatable),
579f4a2713aSLionel Sambuc     DEFINE(macho_universal_binary),
580f4a2713aSLionel Sambuc     DEFINE(macho_object),
581f4a2713aSLionel Sambuc     DEFINE(macho_executable),
582f4a2713aSLionel Sambuc     DEFINE(macho_fixed_virtual_memory_shared_lib),
583f4a2713aSLionel Sambuc     DEFINE(macho_core),
584f4a2713aSLionel Sambuc     DEFINE(macho_preload_executable),
585f4a2713aSLionel Sambuc     DEFINE(macho_dynamically_linked_shared_lib),
586f4a2713aSLionel Sambuc     DEFINE(macho_dynamic_linker),
587f4a2713aSLionel Sambuc     DEFINE(macho_bundle),
588*0a6a1f1dSLionel Sambuc     DEFINE(macho_dynamically_linked_shared_lib_stub),
589f4a2713aSLionel Sambuc     DEFINE(macho_dsym_companion),
590f4a2713aSLionel Sambuc     DEFINE(windows_resource)
591f4a2713aSLionel Sambuc #undef DEFINE
592f4a2713aSLionel Sambuc     };
593f4a2713aSLionel Sambuc 
594f4a2713aSLionel Sambuc   // Create some files filled with magic.
595f4a2713aSLionel Sambuc   for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
596f4a2713aSLionel Sambuc                                                                      ++i) {
597f4a2713aSLionel Sambuc     SmallString<128> file_pathname(TestDirectory);
598f4a2713aSLionel Sambuc     path::append(file_pathname, i->filename);
599*0a6a1f1dSLionel Sambuc     std::error_code EC;
600*0a6a1f1dSLionel Sambuc     raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
601f4a2713aSLionel Sambuc     ASSERT_FALSE(file.has_error());
602f4a2713aSLionel Sambuc     StringRef magic(i->magic_str, i->magic_str_len);
603f4a2713aSLionel Sambuc     file << magic;
604f4a2713aSLionel Sambuc     file.close();
605f4a2713aSLionel Sambuc     EXPECT_EQ(i->magic, fs::identify_magic(magic));
606*0a6a1f1dSLionel Sambuc     ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
607f4a2713aSLionel Sambuc   }
608f4a2713aSLionel Sambuc }
609f4a2713aSLionel Sambuc 
610f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
TEST_F(FileSystemTest,CarriageReturn)611f4a2713aSLionel Sambuc TEST_F(FileSystemTest, CarriageReturn) {
612f4a2713aSLionel Sambuc   SmallString<128> FilePathname(TestDirectory);
613*0a6a1f1dSLionel Sambuc   std::error_code EC;
614f4a2713aSLionel Sambuc   path::append(FilePathname, "test");
615f4a2713aSLionel Sambuc 
616f4a2713aSLionel Sambuc   {
617*0a6a1f1dSLionel Sambuc     raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
618*0a6a1f1dSLionel Sambuc     ASSERT_NO_ERROR(EC);
619f4a2713aSLionel Sambuc     File << '\n';
620f4a2713aSLionel Sambuc   }
621f4a2713aSLionel Sambuc   {
622*0a6a1f1dSLionel Sambuc     auto Buf = MemoryBuffer::getFile(FilePathname.str());
623*0a6a1f1dSLionel Sambuc     EXPECT_TRUE((bool)Buf);
624*0a6a1f1dSLionel Sambuc     EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
625f4a2713aSLionel Sambuc   }
626f4a2713aSLionel Sambuc 
627f4a2713aSLionel Sambuc   {
628*0a6a1f1dSLionel Sambuc     raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
629*0a6a1f1dSLionel Sambuc     ASSERT_NO_ERROR(EC);
630f4a2713aSLionel Sambuc     File << '\n';
631f4a2713aSLionel Sambuc   }
632f4a2713aSLionel Sambuc   {
633*0a6a1f1dSLionel Sambuc     auto Buf = MemoryBuffer::getFile(FilePathname.str());
634*0a6a1f1dSLionel Sambuc     EXPECT_TRUE((bool)Buf);
635*0a6a1f1dSLionel Sambuc     EXPECT_EQ(Buf.get()->getBuffer(), "\n");
636f4a2713aSLionel Sambuc   }
637*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
638f4a2713aSLionel Sambuc }
639f4a2713aSLionel Sambuc #endif
640f4a2713aSLionel Sambuc 
TEST_F(FileSystemTest,Resize)641*0a6a1f1dSLionel Sambuc TEST_F(FileSystemTest, Resize) {
642*0a6a1f1dSLionel Sambuc   int FD;
643*0a6a1f1dSLionel Sambuc   SmallString<64> TempPath;
644*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
645*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::resize_file(FD, 123));
646*0a6a1f1dSLionel Sambuc   fs::file_status Status;
647*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::status(FD, Status));
648*0a6a1f1dSLionel Sambuc   ASSERT_EQ(Status.getSize(), 123U);
649*0a6a1f1dSLionel Sambuc }
650*0a6a1f1dSLionel Sambuc 
TEST_F(FileSystemTest,FileMapping)651f4a2713aSLionel Sambuc TEST_F(FileSystemTest, FileMapping) {
652f4a2713aSLionel Sambuc   // Create a temp file.
653f4a2713aSLionel Sambuc   int FileDescriptor;
654f4a2713aSLionel Sambuc   SmallString<64> TempPath;
655f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(
656f4a2713aSLionel Sambuc       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
657*0a6a1f1dSLionel Sambuc   unsigned Size = 4096;
658*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
659*0a6a1f1dSLionel Sambuc 
660f4a2713aSLionel Sambuc   // Map in temp file and add some content
661*0a6a1f1dSLionel Sambuc   std::error_code EC;
662f4a2713aSLionel Sambuc   StringRef Val("hello there");
663f4a2713aSLionel Sambuc   {
664f4a2713aSLionel Sambuc     fs::mapped_file_region mfr(FileDescriptor,
665*0a6a1f1dSLionel Sambuc                                fs::mapped_file_region::readwrite, Size, 0, EC);
666f4a2713aSLionel Sambuc     ASSERT_NO_ERROR(EC);
667f4a2713aSLionel Sambuc     std::copy(Val.begin(), Val.end(), mfr.data());
668f4a2713aSLionel Sambuc     // Explicitly add a 0.
669f4a2713aSLionel Sambuc     mfr.data()[Val.size()] = 0;
670f4a2713aSLionel Sambuc     // Unmap temp file
671f4a2713aSLionel Sambuc   }
672f4a2713aSLionel Sambuc 
673f4a2713aSLionel Sambuc   // Map it back in read-only
674*0a6a1f1dSLionel Sambuc   int FD;
675*0a6a1f1dSLionel Sambuc   EC = fs::openFileForRead(Twine(TempPath), FD);
676*0a6a1f1dSLionel Sambuc   ASSERT_NO_ERROR(EC);
677*0a6a1f1dSLionel Sambuc   fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
678f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(EC);
679f4a2713aSLionel Sambuc 
680f4a2713aSLionel Sambuc   // Verify content
681f4a2713aSLionel Sambuc   EXPECT_EQ(StringRef(mfr.const_data()), Val);
682f4a2713aSLionel Sambuc 
683f4a2713aSLionel Sambuc   // Unmap temp file
684*0a6a1f1dSLionel Sambuc   fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
685f4a2713aSLionel Sambuc   ASSERT_NO_ERROR(EC);
686*0a6a1f1dSLionel Sambuc   ASSERT_EQ(close(FD), 0);
687*0a6a1f1dSLionel Sambuc }
688*0a6a1f1dSLionel Sambuc 
TEST(Support,NormalizePath)689*0a6a1f1dSLionel Sambuc TEST(Support, NormalizePath) {
690*0a6a1f1dSLionel Sambuc #if defined(LLVM_ON_WIN32)
691*0a6a1f1dSLionel Sambuc #define EXPECT_PATH_IS(path__, windows__, not_windows__)                        \
692*0a6a1f1dSLionel Sambuc   EXPECT_EQ(path__, windows__);
693*0a6a1f1dSLionel Sambuc #else
694*0a6a1f1dSLionel Sambuc #define EXPECT_PATH_IS(path__, windows__, not_windows__)                        \
695*0a6a1f1dSLionel Sambuc   EXPECT_EQ(path__, not_windows__);
696f4a2713aSLionel Sambuc #endif
697*0a6a1f1dSLionel Sambuc 
698*0a6a1f1dSLionel Sambuc   SmallString<64> Path1("a");
699*0a6a1f1dSLionel Sambuc   SmallString<64> Path2("a/b");
700*0a6a1f1dSLionel Sambuc   SmallString<64> Path3("a\\b");
701*0a6a1f1dSLionel Sambuc   SmallString<64> Path4("a\\\\b");
702*0a6a1f1dSLionel Sambuc   SmallString<64> Path5("\\a");
703*0a6a1f1dSLionel Sambuc   SmallString<64> Path6("a\\");
704*0a6a1f1dSLionel Sambuc 
705*0a6a1f1dSLionel Sambuc   path::native(Path1);
706*0a6a1f1dSLionel Sambuc   EXPECT_PATH_IS(Path1, "a", "a");
707*0a6a1f1dSLionel Sambuc 
708*0a6a1f1dSLionel Sambuc   path::native(Path2);
709*0a6a1f1dSLionel Sambuc   EXPECT_PATH_IS(Path2, "a\\b", "a/b");
710*0a6a1f1dSLionel Sambuc 
711*0a6a1f1dSLionel Sambuc   path::native(Path3);
712*0a6a1f1dSLionel Sambuc   EXPECT_PATH_IS(Path3, "a\\b", "a/b");
713*0a6a1f1dSLionel Sambuc 
714*0a6a1f1dSLionel Sambuc   path::native(Path4);
715*0a6a1f1dSLionel Sambuc   EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
716*0a6a1f1dSLionel Sambuc 
717*0a6a1f1dSLionel Sambuc   path::native(Path5);
718*0a6a1f1dSLionel Sambuc   EXPECT_PATH_IS(Path5, "\\a", "/a");
719*0a6a1f1dSLionel Sambuc 
720*0a6a1f1dSLionel Sambuc   path::native(Path6);
721*0a6a1f1dSLionel Sambuc   EXPECT_PATH_IS(Path6, "a\\", "a/");
722*0a6a1f1dSLionel Sambuc 
723*0a6a1f1dSLionel Sambuc #undef EXPECT_PATH_IS
724f4a2713aSLionel Sambuc }
725f4a2713aSLionel Sambuc } // anonymous namespace
726