blob: 09042113c01736919758958a2a66caca629b5a79 [file] [log] [blame]
Michael J. Spencer3ef91c52010-11-29 22:29:04 +00001//===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
Michael J. Spencer98847782010-11-24 19:20:05 +00002//
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
Rafael Espindola3bc8e712013-06-11 22:21:28 +000010#include "llvm/Support/Path.h"
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000011#include "llvm/Support/ErrorHandling.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000012#include "llvm/Support/FileSystem.h"
Rafael Espindola84ab9b32013-07-19 14:41:25 +000013#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f063602011-01-06 05:57:54 +000014#include "llvm/Support/raw_ostream.h"
Michael J. Spencer98847782010-11-24 19:20:05 +000015#include "gtest/gtest.h"
16
Rafael Espindolaa813d602014-06-11 03:58:34 +000017#ifdef LLVM_ON_WIN32
18#include <winerror.h>
19#endif
20
Michael J. Spencerebad2f92010-11-29 22:28:51 +000021using namespace llvm;
Michael J. Spencer45710402010-12-03 01:21:28 +000022using namespace llvm::sys;
Rafael Espindola3acea392014-06-12 21:46:39 +000023using std::error_code;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000024
Michael J. Spencer3b264ba2011-01-04 17:00:18 +000025#define ASSERT_NO_ERROR(x) \
Michael J. Spencer1f063602011-01-06 05:57:54 +000026 if (error_code ASSERT_NO_ERROR_ec = x) { \
27 SmallString<128> MessageStorage; \
28 raw_svector_ostream Message(MessageStorage); \
29 Message << #x ": did not return errc::success.\n" \
30 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
31 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
32 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
Michael J. Spencer3b264ba2011-01-04 17:00:18 +000033 } else {}
34
Michael J. Spencer98847782010-11-24 19:20:05 +000035namespace {
36
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000037TEST(is_separator, Works) {
38 EXPECT_TRUE(path::is_separator('/'));
39 EXPECT_FALSE(path::is_separator('\0'));
40 EXPECT_FALSE(path::is_separator('-'));
41 EXPECT_FALSE(path::is_separator(' '));
42
43#ifdef LLVM_ON_WIN32
44 EXPECT_TRUE(path::is_separator('\\'));
45#else
46 EXPECT_FALSE(path::is_separator('\\'));
47#endif
48}
49
Michael J. Spencer3ef91c52010-11-29 22:29:04 +000050TEST(Support, Path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000051 SmallVector<StringRef, 40> paths;
52 paths.push_back("");
53 paths.push_back(".");
54 paths.push_back("..");
55 paths.push_back("foo");
56 paths.push_back("/");
57 paths.push_back("/foo");
58 paths.push_back("foo/");
59 paths.push_back("/foo/");
60 paths.push_back("foo/bar");
61 paths.push_back("/foo/bar");
62 paths.push_back("//net");
63 paths.push_back("//net/foo");
64 paths.push_back("///foo///");
65 paths.push_back("///foo///bar");
66 paths.push_back("/.");
67 paths.push_back("./");
68 paths.push_back("/..");
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/..");
75 paths.push_back("foo/../");
76 paths.push_back("foo/../bar");
77 paths.push_back("c:");
78 paths.push_back("c:/");
79 paths.push_back("c:foo");
80 paths.push_back("c:/foo");
81 paths.push_back("c:foo/");
82 paths.push_back("c:/foo/");
83 paths.push_back("c:/foo/bar");
84 paths.push_back("prn:");
85 paths.push_back("c:\\");
86 paths.push_back("c:foo");
87 paths.push_back("c:\\foo");
88 paths.push_back("c:foo\\");
89 paths.push_back("c:\\foo\\");
90 paths.push_back("c:\\foo/");
91 paths.push_back("c:/foo\\bar");
92
93 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
94 e = paths.end();
95 i != e;
96 ++i) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000097 for (sys::path::const_iterator ci = sys::path::begin(*i),
98 ce = sys::path::end(*i);
99 ci != ce;
100 ++ci) {
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000101 ASSERT_FALSE(ci->empty());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000102 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000103
Michael J. Spencer75942f92010-12-01 22:28:42 +0000104#if 0 // Valgrind is whining about this.
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000105 outs() << " Reverse Iteration: [";
106 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
107 ce = sys::path::rend(*i);
108 ci != ce;
109 ++ci) {
110 outs() << *ci << ',';
111 }
112 outs() << "]\n";
Michael J. Spencer75942f92010-12-01 22:28:42 +0000113#endif
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000114
Michael J. Spencerf616b212010-12-07 17:04:04 +0000115 path::has_root_path(*i);
116 path::root_path(*i);
117 path::has_root_name(*i);
118 path::root_name(*i);
119 path::has_root_directory(*i);
120 path::root_directory(*i);
121 path::has_parent_path(*i);
122 path::parent_path(*i);
123 path::has_filename(*i);
124 path::filename(*i);
125 path::has_stem(*i);
126 path::stem(*i);
127 path::has_extension(*i);
128 path::extension(*i);
129 path::is_absolute(*i);
130 path::is_relative(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000131
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000132 SmallString<128> temp_store;
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000133 temp_store = *i;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000134 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000135 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000136 path::remove_filename(temp_store);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000137
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000138 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000139 path::replace_extension(temp_store, "ext");
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000140 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000141 stem = path::stem(filename);
142 ext = path::extension(filename);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000143 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
144
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000145 path::native(*i, temp_store);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000146 }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000147}
Michael J. Spencer45710402010-12-03 01:21:28 +0000148
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000149TEST(Support, RelativePathIterator) {
150 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
151 typedef SmallVector<StringRef, 4> PathComponents;
152 PathComponents ExpectedPathComponents;
153 PathComponents ActualPathComponents;
154
155 StringRef(Path).split(ExpectedPathComponents, "/");
156
157 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
158 ++I) {
159 ActualPathComponents.push_back(*I);
160 }
161
162 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
163
164 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
165 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
166 }
167}
168
169TEST(Support, AbsolutePathIterator) {
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 // The root path will also be a component when iterating
178 ExpectedPathComponents[0] = "/";
179
180 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
181 ++I) {
182 ActualPathComponents.push_back(*I);
183 }
184
185 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
186
187 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
188 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
189 }
190}
191
192#ifdef LLVM_ON_WIN32
193TEST(Support, AbsolutePathIteratorWin32) {
194 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
195 typedef SmallVector<StringRef, 4> PathComponents;
196 PathComponents ExpectedPathComponents;
197 PathComponents ActualPathComponents;
198
199 StringRef(Path).split(ExpectedPathComponents, "\\");
200
201 // The root path (which comes after the drive name) will also be a component
202 // when iterating.
203 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
204
205 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
206 ++I) {
207 ActualPathComponents.push_back(*I);
208 }
209
210 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
211
212 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
213 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
214 }
215}
216#endif // LLVM_ON_WIN32
217
Ben Langmuir8d116392014-03-05 19:56:30 +0000218TEST(Support, AbsolutePathIteratorEnd) {
219 // Trailing slashes are converted to '.' unless they are part of the root path.
220 SmallVector<StringRef, 4> Paths;
221 Paths.push_back("/foo/");
222 Paths.push_back("/foo//");
223 Paths.push_back("//net//");
224#ifdef LLVM_ON_WIN32
225 Paths.push_back("c:\\\\");
226#endif
227
228 for (StringRef Path : Paths) {
229 StringRef LastComponent = *--path::end(Path);
230 EXPECT_EQ(".", LastComponent);
231 }
232
233 SmallVector<StringRef, 3> RootPaths;
234 RootPaths.push_back("/");
235 RootPaths.push_back("//net/");
236#ifdef LLVM_ON_WIN32
237 RootPaths.push_back("c:\\");
238#endif
239
240 for (StringRef Path : RootPaths) {
241 StringRef LastComponent = *--path::end(Path);
242 EXPECT_EQ(1u, LastComponent.size());
243 EXPECT_TRUE(path::is_separator(LastComponent[0]));
244 }
245}
246
Peter Collingbournef7d41012014-01-31 23:46:06 +0000247TEST(Support, HomeDirectory) {
248#ifdef LLVM_ON_UNIX
249 // This test only makes sense on Unix if $HOME is set.
250 if (::getenv("HOME")) {
251#endif
252 SmallString<128> HomeDir;
253 EXPECT_TRUE(path::home_directory(HomeDir));
254 EXPECT_FALSE(HomeDir.empty());
255#ifdef LLVM_ON_UNIX
256 }
257#endif
258}
259
Michael J. Spencer346a1332011-01-05 16:39:05 +0000260class FileSystemTest : public testing::Test {
261protected:
262 /// Unique temporary directory in which all created filesystem entities must
263 /// be placed. It is recursively removed at the end of each test.
264 SmallString<128> TestDirectory;
265
266 virtual void SetUp() {
Michael J. Spencer346a1332011-01-05 16:39:05 +0000267 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000268 fs::createUniqueDirectory("file-system-test", TestDirectory));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000269 // We don't care about this specific file.
Michael J. Spencer42368cc2011-01-05 16:39:46 +0000270 errs() << "Test Directory: " << TestDirectory << '\n';
271 errs().flush();
Michael J. Spencer346a1332011-01-05 16:39:05 +0000272 }
273
274 virtual void TearDown() {
Rafael Espindola78dcc032014-01-10 20:36:42 +0000275 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000276 }
277};
278
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000279TEST_F(FileSystemTest, Unique) {
280 // Create a temp file.
281 int FileDescriptor;
282 SmallString<64> TempPath;
283 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000284 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000285
286 // The same file should return an identical unique id.
Rafael Espindola7f822a92013-07-29 21:26:49 +0000287 fs::UniqueID F1, F2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000288 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
289 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000290 ASSERT_EQ(F1, F2);
291
292 // Different files should return different unique ids.
293 int FileDescriptor2;
294 SmallString<64> TempPath2;
295 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000296 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
297
Rafael Espindola7f822a92013-07-29 21:26:49 +0000298 fs::UniqueID D;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000299 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000300 ASSERT_NE(D, F1);
301 ::close(FileDescriptor2);
302
303 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
304
305 // Two paths representing the same file on disk should still provide the
306 // same unique id. We can test this by making a hard link.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000307 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Rafael Espindola7f822a92013-07-29 21:26:49 +0000308 fs::UniqueID D2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000309 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000310 ASSERT_EQ(D2, F1);
311
312 ::close(FileDescriptor);
Rafael Espindolaa5932af2013-07-30 20:25:53 +0000313
314 SmallString<128> Dir1;
315 ASSERT_NO_ERROR(
316 fs::createUniqueDirectory("dir1", Dir1));
317 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
318 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
319 ASSERT_EQ(F1, F2);
320
321 SmallString<128> Dir2;
322 ASSERT_NO_ERROR(
323 fs::createUniqueDirectory("dir2", Dir2));
324 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
325 ASSERT_NE(F1, F2);
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000326}
327
Michael J. Spencer346a1332011-01-05 16:39:05 +0000328TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000329 // Create a temp file.
Michael J. Spencer45710402010-12-03 01:21:28 +0000330 int FileDescriptor;
331 SmallString<64> TempPath;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000332 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000333 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Michael J. Spencer45710402010-12-03 01:21:28 +0000334
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000335 // Make sure it exists.
Michael J. Spencer45710402010-12-03 01:21:28 +0000336 bool TempFileExists;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000337 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
Michael J. Spencer45710402010-12-03 01:21:28 +0000338 EXPECT_TRUE(TempFileExists);
339
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000340 // Create another temp tile.
341 int FD2;
342 SmallString<64> TempPath2;
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000343 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
Rafael Espindolad3c89042013-07-25 15:00:17 +0000344 ASSERT_TRUE(TempPath2.endswith(".temp"));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000345 ASSERT_NE(TempPath.str(), TempPath2.str());
346
Michael J. Spencer203d7802011-12-12 06:04:28 +0000347 fs::file_status A, B;
348 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
349 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
350 EXPECT_FALSE(fs::equivalent(A, B));
351
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000352 ::close(FD2);
Rafael Espindola213c4cb2013-07-18 03:29:51 +0000353
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000354 // Remove Temp2.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000355 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
356 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
357 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
Rafael Espindola5c4f8292014-06-11 19:05:50 +0000358 std::errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000359
Rafael Espindola107b74c2013-07-31 00:10:25 +0000360 error_code EC = fs::status(TempPath2.c_str(), B);
Rafael Espindola5c4f8292014-06-11 19:05:50 +0000361 EXPECT_EQ(EC, std::errc::no_such_file_or_directory);
Rafael Espindola107b74c2013-07-31 00:10:25 +0000362 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
363
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000364 // Make sure Temp2 doesn't exist.
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000365 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000366 EXPECT_FALSE(TempFileExists);
367
Rafael Espindolad3c89042013-07-25 15:00:17 +0000368 SmallString<64> TempPath3;
369 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
370 ASSERT_FALSE(TempPath3.endswith("."));
371
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000372 // Create a hard link to Temp1.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000373 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000374 bool equal;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000375 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000376 EXPECT_TRUE(equal);
Michael J. Spencer203d7802011-12-12 06:04:28 +0000377 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
378 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
379 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000380
381 // Remove Temp1.
Michael J. Spencer45710402010-12-03 01:21:28 +0000382 ::close(FileDescriptor);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000383 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000384
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000385 // Remove the hard link.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000386 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000387
388 // Make sure Temp1 doesn't exist.
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000389 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
Benjamin Kramer23e6bdf2010-12-03 12:33:32 +0000390 EXPECT_FALSE(TempFileExists);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000391
392#ifdef LLVM_ON_WIN32
393 // Path name > 260 chars should get an error.
394 const char *Path270 =
395 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
396 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
397 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
398 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
399 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Rafael Espindola2eb1ae42013-07-05 22:32:33 +0000400 EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath),
Rafael Espindola5c4f8292014-06-11 19:05:50 +0000401 std::errc::no_such_file_or_directory);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000402#endif
Michael J. Spencer346a1332011-01-05 16:39:05 +0000403}
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000404
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000405TEST_F(FileSystemTest, CreateDir) {
406 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
407 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
408 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
Rafael Espindola5c4f8292014-06-11 19:05:50 +0000409 std::errc::file_exists);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000410 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
411}
412
Michael J. Spencer346a1332011-01-05 16:39:05 +0000413TEST_F(FileSystemTest, DirectoryIteration) {
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000414 error_code ec;
Michael J. Spencer1f063602011-01-06 05:57:54 +0000415 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
416 ASSERT_NO_ERROR(ec);
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000417
418 // Create a known hierarchy to recurse over.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000419 ASSERT_NO_ERROR(
420 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
421 ASSERT_NO_ERROR(
422 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
423 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
424 "/recursive/dontlookhere/da1"));
425 ASSERT_NO_ERROR(
426 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
427 ASSERT_NO_ERROR(
428 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000429 typedef std::vector<std::string> v_t;
430 v_t visited;
431 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
432 + "/recursive", ec), e; i != e; i.increment(ec)){
433 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000434 if (path::filename(i->path()) == "p1") {
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000435 i.pop();
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000436 // FIXME: recursive_directory_iterator should be more robust.
437 if (i == e) break;
438 }
Michael J. Spencer9b54a3e2011-12-09 01:14:41 +0000439 if (path::filename(i->path()) == "dontlookhere")
440 i.no_push();
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000441 visited.push_back(path::filename(i->path()));
442 }
443 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
444 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
445 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
446 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
447 "dontlookhere");
448 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
449 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
450 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
451 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
452 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
453
454 // Make sure that each path was visited correctly.
455 ASSERT_NE(a0, visited.end());
456 ASSERT_NE(aa1, visited.end());
457 ASSERT_NE(ab1, visited.end());
458 ASSERT_NE(dontlookhere, visited.end());
459 ASSERT_EQ(da1, visited.end()); // Not visited.
460 ASSERT_NE(z0, visited.end());
461 ASSERT_NE(za1, visited.end());
462 ASSERT_NE(pop, visited.end());
463 ASSERT_EQ(p1, visited.end()); // Not visited.
464
465 // Make sure that parents were visited before children. No other ordering
466 // guarantees can be made across siblings.
467 ASSERT_LT(a0, aa1);
468 ASSERT_LT(a0, ab1);
469 ASSERT_LT(z0, za1);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000470
471 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
472 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
473 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
474 ASSERT_NO_ERROR(
475 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
476 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
477 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
478 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
479 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
480 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
481 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
Michael J. Spencer1f063602011-01-06 05:57:54 +0000482}
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000483
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000484const char archive[] = "!<arch>\x0A";
485const char bitcode[] = "\xde\xc0\x17\x0b";
Rui Ueyama829c4392013-11-14 22:09:08 +0000486const char coff_object[] = "\x00\x00......";
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000487const char coff_import_library[] = "\x00\x00\xff\xff....";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000488const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
489 0, 0, 0, 0, 0, 0, 0, 0, 1 };
490const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00";
491const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01";
492const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02";
493const char macho_fixed_virtual_memory_shared_lib[] =
494 "\xfe\xed\xfa\xce..........\x00\x03";
495const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04";
496const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05";
497const char macho_dynamically_linked_shared_lib[] =
498 "\xfe\xed\xfa\xce..........\x00\x06";
499const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07";
500const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08";
501const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a";
502const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000503
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000504TEST_F(FileSystemTest, Magic) {
505 struct type {
506 const char *filename;
507 const char *magic_str;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000508 size_t magic_str_len;
509 fs::file_magic magic;
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000510 } types[] = {
511#define DEFINE(magic) \
512 { #magic, magic, sizeof(magic), fs::file_magic::magic }
513 DEFINE(archive),
514 DEFINE(bitcode),
Rui Ueyama829c4392013-11-14 22:09:08 +0000515 DEFINE(coff_object),
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000516 DEFINE(coff_import_library),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000517 DEFINE(elf_relocatable),
518 DEFINE(macho_universal_binary),
519 DEFINE(macho_object),
520 DEFINE(macho_executable),
521 DEFINE(macho_fixed_virtual_memory_shared_lib),
522 DEFINE(macho_core),
523 DEFINE(macho_preload_executable),
524 DEFINE(macho_dynamically_linked_shared_lib),
525 DEFINE(macho_dynamic_linker),
526 DEFINE(macho_bundle),
527 DEFINE(macho_dsym_companion),
528 DEFINE(windows_resource)
529#undef DEFINE
530 };
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000531
532 // Create some files filled with magic.
533 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
534 ++i) {
535 SmallString<128> file_pathname(TestDirectory);
536 path::append(file_pathname, i->filename);
537 std::string ErrMsg;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000538 raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_None);
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000539 ASSERT_FALSE(file.has_error());
540 StringRef magic(i->magic_str, i->magic_str_len);
541 file << magic;
Michael J. Spencerb5871802011-01-15 18:52:49 +0000542 file.close();
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000543 EXPECT_EQ(i->magic, fs::identify_magic(magic));
Rafael Espindola78dcc032014-01-10 20:36:42 +0000544 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000545 }
Michael J. Spencer98847782010-11-24 19:20:05 +0000546}
547
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000548#ifdef LLVM_ON_WIN32
549TEST_F(FileSystemTest, CarriageReturn) {
550 SmallString<128> FilePathname(TestDirectory);
551 std::string ErrMsg;
552 path::append(FilePathname, "test");
553
554 {
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000555 raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Text);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000556 EXPECT_EQ(ErrMsg, "");
557 File << '\n';
558 }
559 {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000560 std::unique_ptr<MemoryBuffer> Buf;
Rafael Espindolae5bf2462013-10-25 19:47:55 +0000561 MemoryBuffer::getFile(FilePathname.c_str(), Buf);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000562 EXPECT_EQ(Buf->getBuffer(), "\r\n");
563 }
564
565 {
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000566 raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_None);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000567 EXPECT_EQ(ErrMsg, "");
568 File << '\n';
569 }
570 {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000571 std::unique_ptr<MemoryBuffer> Buf;
Rafael Espindolae5bf2462013-10-25 19:47:55 +0000572 MemoryBuffer::getFile(FilePathname.c_str(), Buf);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000573 EXPECT_EQ(Buf->getBuffer(), "\n");
574 }
Rafael Espindola78dcc032014-01-10 20:36:42 +0000575 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000576}
577#endif
578
Nick Kledzik18497e92012-06-20 00:28:54 +0000579TEST_F(FileSystemTest, FileMapping) {
580 // Create a temp file.
581 int FileDescriptor;
582 SmallString<64> TempPath;
583 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000584 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Nick Kledzik18497e92012-06-20 00:28:54 +0000585 // Map in temp file and add some content
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000586 error_code EC;
587 StringRef Val("hello there");
588 {
589 fs::mapped_file_region mfr(FileDescriptor,
Michael J. Spencer01ac34e2013-03-14 00:33:37 +0000590 true,
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000591 fs::mapped_file_region::readwrite,
592 4096,
593 0,
594 EC);
595 ASSERT_NO_ERROR(EC);
596 std::copy(Val.begin(), Val.end(), mfr.data());
597 // Explicitly add a 0.
598 mfr.data()[Val.size()] = 0;
599 // Unmap temp file
600 }
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000601
Nick Kledzik18497e92012-06-20 00:28:54 +0000602 // Map it back in read-only
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000603 fs::mapped_file_region mfr(Twine(TempPath),
604 fs::mapped_file_region::readonly,
605 0,
606 0,
607 EC);
608 ASSERT_NO_ERROR(EC);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000609
Nick Kledzik18497e92012-06-20 00:28:54 +0000610 // Verify content
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000611 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000612
Nick Kledzik18497e92012-06-20 00:28:54 +0000613 // Unmap temp file
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000614
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000615 fs::mapped_file_region m(Twine(TempPath),
616 fs::mapped_file_region::readonly,
617 0,
618 0,
619 EC);
620 ASSERT_NO_ERROR(EC);
621 const char *Data = m.const_data();
Chandler Carruth002da5d2014-03-02 04:08:41 +0000622 fs::mapped_file_region mfrrv(std::move(m));
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000623 EXPECT_EQ(mfrrv.const_data(), Data);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000624}
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000625
626TEST(Support, NormalizePath) {
627#if defined(LLVM_ON_WIN32)
628#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
629 EXPECT_EQ(path__, windows__);
630#else
631#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
632 EXPECT_EQ(path__, not_windows__);
633#endif
634
635 SmallString<64> Path1("a");
636 SmallString<64> Path2("a/b");
637 SmallString<64> Path3("a\\b");
638 SmallString<64> Path4("a\\\\b");
639 SmallString<64> Path5("\\a");
640 SmallString<64> Path6("a\\");
641
642 ASSERT_NO_ERROR(fs::normalize_separators(Path1));
643 EXPECT_PATH_IS(Path1, "a", "a");
644
645 ASSERT_NO_ERROR(fs::normalize_separators(Path2));
646 EXPECT_PATH_IS(Path2, "a/b", "a/b");
647
648 ASSERT_NO_ERROR(fs::normalize_separators(Path3));
649 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
650
651 ASSERT_NO_ERROR(fs::normalize_separators(Path4));
652 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
653
654 ASSERT_NO_ERROR(fs::normalize_separators(Path5));
655 EXPECT_PATH_IS(Path5, "\\a", "/a");
656
657 ASSERT_NO_ERROR(fs::normalize_separators(Path6));
658 EXPECT_PATH_IS(Path6, "a\\", "a/");
659
660#undef EXPECT_PATH_IS
661}
Michael J. Spencer98847782010-11-24 19:20:05 +0000662} // anonymous namespace