blob: 5d883c4041c0883b00e32f95a634c302db1bcbbb [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"
Rafael Espindola2a826e42014-06-13 17:20:48 +000011#include "llvm/Support/Errc.h"
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000012#include "llvm/Support/ErrorHandling.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000013#include "llvm/Support/FileSystem.h"
Rafael Espindola84ab9b32013-07-19 14:41:25 +000014#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f063602011-01-06 05:57:54 +000015#include "llvm/Support/raw_ostream.h"
Michael J. Spencer98847782010-11-24 19:20:05 +000016#include "gtest/gtest.h"
17
Rafael Espindolaa813d602014-06-11 03:58:34 +000018#ifdef LLVM_ON_WIN32
NAKAMURA Takumib94f0522015-06-16 06:46:16 +000019#include <windows.h>
Rafael Espindolaa813d602014-06-11 03:58:34 +000020#include <winerror.h>
21#endif
22
Frederic Riss6b9396c2015-08-06 21:04:55 +000023#ifdef LLVM_ON_UNIX
24#include <sys/stat.h>
25#endif
26
Michael J. Spencerebad2f92010-11-29 22:28:51 +000027using namespace llvm;
Michael J. Spencer45710402010-12-03 01:21:28 +000028using namespace llvm::sys;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000029
Rafael Espindolac049c652014-06-13 03:20:08 +000030#define ASSERT_NO_ERROR(x) \
31 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
32 SmallString<128> MessageStorage; \
33 raw_svector_ostream Message(MessageStorage); \
34 Message << #x ": did not return errc::success.\n" \
35 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
36 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
37 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
38 } else { \
39 }
Michael J. Spencer3b264ba2011-01-04 17:00:18 +000040
Michael J. Spencer98847782010-11-24 19:20:05 +000041namespace {
42
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000043TEST(is_separator, Works) {
44 EXPECT_TRUE(path::is_separator('/'));
45 EXPECT_FALSE(path::is_separator('\0'));
46 EXPECT_FALSE(path::is_separator('-'));
47 EXPECT_FALSE(path::is_separator(' '));
48
49#ifdef LLVM_ON_WIN32
50 EXPECT_TRUE(path::is_separator('\\'));
51#else
52 EXPECT_FALSE(path::is_separator('\\'));
53#endif
54}
55
Michael J. Spencer3ef91c52010-11-29 22:29:04 +000056TEST(Support, Path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000057 SmallVector<StringRef, 40> paths;
58 paths.push_back("");
59 paths.push_back(".");
60 paths.push_back("..");
61 paths.push_back("foo");
62 paths.push_back("/");
63 paths.push_back("/foo");
64 paths.push_back("foo/");
65 paths.push_back("/foo/");
66 paths.push_back("foo/bar");
67 paths.push_back("/foo/bar");
68 paths.push_back("//net");
69 paths.push_back("//net/foo");
70 paths.push_back("///foo///");
71 paths.push_back("///foo///bar");
72 paths.push_back("/.");
73 paths.push_back("./");
74 paths.push_back("/..");
75 paths.push_back("../");
76 paths.push_back("foo/.");
77 paths.push_back("foo/..");
78 paths.push_back("foo/./");
79 paths.push_back("foo/./bar");
80 paths.push_back("foo/..");
81 paths.push_back("foo/../");
82 paths.push_back("foo/../bar");
83 paths.push_back("c:");
84 paths.push_back("c:/");
85 paths.push_back("c:foo");
86 paths.push_back("c:/foo");
87 paths.push_back("c:foo/");
88 paths.push_back("c:/foo/");
89 paths.push_back("c:/foo/bar");
90 paths.push_back("prn:");
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/");
97 paths.push_back("c:/foo\\bar");
98
Justin Bogner0c274ae2014-07-16 08:18:58 +000099 SmallVector<StringRef, 5> ComponentStack;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000100 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
101 e = paths.end();
102 i != e;
103 ++i) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000104 for (sys::path::const_iterator ci = sys::path::begin(*i),
105 ce = sys::path::end(*i);
106 ci != ce;
107 ++ci) {
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000108 ASSERT_FALSE(ci->empty());
Justin Bogner0c274ae2014-07-16 08:18:58 +0000109 ComponentStack.push_back(*ci);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000110 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000111
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000112 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
113 ce = sys::path::rend(*i);
114 ci != ce;
115 ++ci) {
Justin Bogner0c274ae2014-07-16 08:18:58 +0000116 ASSERT_TRUE(*ci == ComponentStack.back());
117 ComponentStack.pop_back();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000118 }
Justin Bogner0c274ae2014-07-16 08:18:58 +0000119 ASSERT_TRUE(ComponentStack.empty());
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000120
Michael J. Spencerf616b212010-12-07 17:04:04 +0000121 path::has_root_path(*i);
122 path::root_path(*i);
123 path::has_root_name(*i);
124 path::root_name(*i);
125 path::has_root_directory(*i);
126 path::root_directory(*i);
127 path::has_parent_path(*i);
128 path::parent_path(*i);
129 path::has_filename(*i);
130 path::filename(*i);
131 path::has_stem(*i);
132 path::stem(*i);
133 path::has_extension(*i);
134 path::extension(*i);
135 path::is_absolute(*i);
136 path::is_relative(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000137
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000138 SmallString<128> temp_store;
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000139 temp_store = *i;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000140 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000141 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000142 path::remove_filename(temp_store);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000143
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000144 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000145 path::replace_extension(temp_store, "ext");
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000146 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000147 stem = path::stem(filename);
148 ext = path::extension(filename);
Justin Bogner487e7642014-08-04 17:36:41 +0000149 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000150
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000151 path::native(*i, temp_store);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000152 }
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000153
154 SmallString<32> Relative("foo.cpp");
155 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
Benjamin Kramer2b4e14e2015-10-05 14:15:13 +0000156 Relative[5] = '/'; // Fix up windows paths.
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000157 ASSERT_EQ("/root/foo.cpp", Relative);
Michael J. Spencer346a1332011-01-05 16:39:05 +0000158}
Michael J. Spencer45710402010-12-03 01:21:28 +0000159
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000160TEST(Support, RelativePathIterator) {
161 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
162 typedef SmallVector<StringRef, 4> PathComponents;
163 PathComponents ExpectedPathComponents;
164 PathComponents ActualPathComponents;
165
Chandler Carruthe4405e92015-09-10 06:12:31 +0000166 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000167
168 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
169 ++I) {
170 ActualPathComponents.push_back(*I);
171 }
172
173 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
174
175 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
176 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
177 }
178}
179
Ben Langmuir2547f932015-03-10 00:04:29 +0000180TEST(Support, RelativePathDotIterator) {
181 SmallString<64> Path(StringRef(".c/.d/../."));
182 typedef SmallVector<StringRef, 4> PathComponents;
183 PathComponents ExpectedPathComponents;
184 PathComponents ActualPathComponents;
185
Chandler Carruthe4405e92015-09-10 06:12:31 +0000186 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000187
188 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
189 ++I) {
190 ActualPathComponents.push_back(*I);
191 }
192
193 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
194
195 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
196 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
197 }
198}
199
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000200TEST(Support, AbsolutePathIterator) {
201 SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
202 typedef SmallVector<StringRef, 4> PathComponents;
203 PathComponents ExpectedPathComponents;
204 PathComponents ActualPathComponents;
205
Chandler Carruthe4405e92015-09-10 06:12:31 +0000206 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000207
208 // The root path will also be a component when iterating
209 ExpectedPathComponents[0] = "/";
210
211 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
212 ++I) {
213 ActualPathComponents.push_back(*I);
214 }
215
216 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
217
218 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
219 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
220 }
221}
222
Ben Langmuir2547f932015-03-10 00:04:29 +0000223TEST(Support, AbsolutePathDotIterator) {
224 SmallString<64> Path(StringRef("/.c/.d/../."));
225 typedef SmallVector<StringRef, 4> PathComponents;
226 PathComponents ExpectedPathComponents;
227 PathComponents ActualPathComponents;
228
Chandler Carruthe4405e92015-09-10 06:12:31 +0000229 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000230
231 // The root path will also be a component when iterating
232 ExpectedPathComponents[0] = "/";
233
234 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
235 ++I) {
236 ActualPathComponents.push_back(*I);
237 }
238
239 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
240
241 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
242 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
243 }
244}
245
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000246#ifdef LLVM_ON_WIN32
247TEST(Support, AbsolutePathIteratorWin32) {
248 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
249 typedef SmallVector<StringRef, 4> PathComponents;
250 PathComponents ExpectedPathComponents;
251 PathComponents ActualPathComponents;
252
253 StringRef(Path).split(ExpectedPathComponents, "\\");
254
255 // The root path (which comes after the drive name) will also be a component
256 // when iterating.
257 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
258
259 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
260 ++I) {
261 ActualPathComponents.push_back(*I);
262 }
263
264 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
265
266 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
267 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
268 }
269}
270#endif // LLVM_ON_WIN32
271
Ben Langmuir8d116392014-03-05 19:56:30 +0000272TEST(Support, AbsolutePathIteratorEnd) {
273 // Trailing slashes are converted to '.' unless they are part of the root path.
274 SmallVector<StringRef, 4> Paths;
275 Paths.push_back("/foo/");
276 Paths.push_back("/foo//");
277 Paths.push_back("//net//");
278#ifdef LLVM_ON_WIN32
279 Paths.push_back("c:\\\\");
280#endif
281
282 for (StringRef Path : Paths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000283 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000284 EXPECT_EQ(".", LastComponent);
285 }
286
287 SmallVector<StringRef, 3> RootPaths;
288 RootPaths.push_back("/");
289 RootPaths.push_back("//net/");
290#ifdef LLVM_ON_WIN32
291 RootPaths.push_back("c:\\");
292#endif
293
294 for (StringRef Path : RootPaths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000295 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000296 EXPECT_EQ(1u, LastComponent.size());
297 EXPECT_TRUE(path::is_separator(LastComponent[0]));
298 }
299}
300
Peter Collingbournef7d41012014-01-31 23:46:06 +0000301TEST(Support, HomeDirectory) {
302#ifdef LLVM_ON_UNIX
303 // This test only makes sense on Unix if $HOME is set.
304 if (::getenv("HOME")) {
305#endif
306 SmallString<128> HomeDir;
307 EXPECT_TRUE(path::home_directory(HomeDir));
308 EXPECT_FALSE(HomeDir.empty());
309#ifdef LLVM_ON_UNIX
310 }
311#endif
312}
313
Michael J. Spencer346a1332011-01-05 16:39:05 +0000314class FileSystemTest : public testing::Test {
315protected:
316 /// Unique temporary directory in which all created filesystem entities must
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000317 /// be placed. It is removed at the end of each test (must be empty).
Michael J. Spencer346a1332011-01-05 16:39:05 +0000318 SmallString<128> TestDirectory;
319
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000320 void SetUp() override {
Michael J. Spencer346a1332011-01-05 16:39:05 +0000321 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000322 fs::createUniqueDirectory("file-system-test", TestDirectory));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000323 // We don't care about this specific file.
Michael J. Spencer42368cc2011-01-05 16:39:46 +0000324 errs() << "Test Directory: " << TestDirectory << '\n';
325 errs().flush();
Michael J. Spencer346a1332011-01-05 16:39:05 +0000326 }
327
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000328 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000329};
330
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000331TEST_F(FileSystemTest, Unique) {
332 // Create a temp file.
333 int FileDescriptor;
334 SmallString<64> TempPath;
335 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000336 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000337
338 // The same file should return an identical unique id.
Rafael Espindola7f822a92013-07-29 21:26:49 +0000339 fs::UniqueID F1, F2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000340 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
341 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000342 ASSERT_EQ(F1, F2);
343
344 // Different files should return different unique ids.
345 int FileDescriptor2;
346 SmallString<64> TempPath2;
347 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000348 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
349
Rafael Espindola7f822a92013-07-29 21:26:49 +0000350 fs::UniqueID D;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000351 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000352 ASSERT_NE(D, F1);
353 ::close(FileDescriptor2);
354
355 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
356
357 // Two paths representing the same file on disk should still provide the
358 // same unique id. We can test this by making a hard link.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000359 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Rafael Espindola7f822a92013-07-29 21:26:49 +0000360 fs::UniqueID D2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000361 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000362 ASSERT_EQ(D2, F1);
363
364 ::close(FileDescriptor);
Rafael Espindolaa5932af2013-07-30 20:25:53 +0000365
366 SmallString<128> Dir1;
367 ASSERT_NO_ERROR(
368 fs::createUniqueDirectory("dir1", Dir1));
369 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
370 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
371 ASSERT_EQ(F1, F2);
372
373 SmallString<128> Dir2;
374 ASSERT_NO_ERROR(
375 fs::createUniqueDirectory("dir2", Dir2));
376 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
377 ASSERT_NE(F1, F2);
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000378}
379
Michael J. Spencer346a1332011-01-05 16:39:05 +0000380TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000381 // Create a temp file.
Michael J. Spencer45710402010-12-03 01:21:28 +0000382 int FileDescriptor;
383 SmallString<64> TempPath;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000384 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000385 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Michael J. Spencer45710402010-12-03 01:21:28 +0000386
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000387 // Make sure it exists.
Rafael Espindolac1599152014-09-11 19:11:02 +0000388 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000389
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000390 // Create another temp tile.
391 int FD2;
392 SmallString<64> TempPath2;
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000393 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
Rafael Espindolad3c89042013-07-25 15:00:17 +0000394 ASSERT_TRUE(TempPath2.endswith(".temp"));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000395 ASSERT_NE(TempPath.str(), TempPath2.str());
396
Michael J. Spencer203d7802011-12-12 06:04:28 +0000397 fs::file_status A, B;
398 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
399 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
400 EXPECT_FALSE(fs::equivalent(A, B));
401
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000402 ::close(FD2);
Rafael Espindola213c4cb2013-07-18 03:29:51 +0000403
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000404 // Remove Temp2.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000405 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
406 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
407 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000408 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000409
Rafael Espindolac049c652014-06-13 03:20:08 +0000410 std::error_code EC = fs::status(TempPath2.c_str(), B);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000411 EXPECT_EQ(EC, errc::no_such_file_or_directory);
Rafael Espindola107b74c2013-07-31 00:10:25 +0000412 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
413
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000414 // Make sure Temp2 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000415 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
416 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000417
Rafael Espindolad3c89042013-07-25 15:00:17 +0000418 SmallString<64> TempPath3;
419 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
420 ASSERT_FALSE(TempPath3.endswith("."));
421
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000422 // Create a hard link to Temp1.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000423 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000424 bool equal;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000425 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000426 EXPECT_TRUE(equal);
Michael J. Spencer203d7802011-12-12 06:04:28 +0000427 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
428 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
429 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000430
431 // Remove Temp1.
Michael J. Spencer45710402010-12-03 01:21:28 +0000432 ::close(FileDescriptor);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000433 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000434
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000435 // Remove the hard link.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000436 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000437
438 // Make sure Temp1 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000439 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
440 errc::no_such_file_or_directory);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000441
442#ifdef LLVM_ON_WIN32
443 // Path name > 260 chars should get an error.
444 const char *Path270 =
445 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
446 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
447 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
448 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
449 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000450 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000451 errc::invalid_argument);
452 // Relative path < 247 chars, no problem.
453 const char *Path216 =
454 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
455 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
456 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
457 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000458 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000459 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000460#endif
Michael J. Spencer346a1332011-01-05 16:39:05 +0000461}
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000462
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000463TEST_F(FileSystemTest, CreateDir) {
464 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
465 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
466 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000467 errc::file_exists);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000468 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000469
Frederic Riss6b9396c2015-08-06 21:04:55 +0000470#ifdef LLVM_ON_UNIX
471 // Set a 0000 umask so that we can test our directory permissions.
472 mode_t OldUmask = ::umask(0000);
473
474 fs::file_status Status;
475 ASSERT_NO_ERROR(
476 fs::create_directory(Twine(TestDirectory) + "baz500", false,
477 fs::perms::owner_read | fs::perms::owner_exe));
478 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
479 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
480 fs::perms::owner_read | fs::perms::owner_exe);
481 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
482 fs::perms::all_all));
483 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
484 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
485
486 // Restore umask to be safe.
487 ::umask(OldUmask);
488#endif
489
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000490#ifdef LLVM_ON_WIN32
491 // Prove that create_directories() can handle a pathname > 248 characters,
492 // which is the documented limit for CreateDirectory().
493 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
494 // Generate a directory path guaranteed to fall into that range.
495 size_t TmpLen = TestDirectory.size();
496 const char *OneDir = "\\123456789";
497 size_t OneDirLen = strlen(OneDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000498 ASSERT_LT(OneDirLen, 12U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000499 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
500 SmallString<260> LongDir(TestDirectory);
501 for (size_t I = 0; I < NLevels; ++I)
502 LongDir.append(OneDir);
503 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
504 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
505 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
506 errc::file_exists);
507 // Tidy up, "recursively" removing the directories.
508 StringRef ThisDir(LongDir);
509 for (size_t J = 0; J < NLevels; ++J) {
510 ASSERT_NO_ERROR(fs::remove(ThisDir));
511 ThisDir = path::parent_path(ThisDir);
512 }
513
514 // Similarly for a relative pathname. Need to set the current directory to
515 // TestDirectory so that the one we create ends up in the right place.
516 char PreviousDir[260];
517 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000518 ASSERT_GT(PreviousDirLen, 0U);
519 ASSERT_LT(PreviousDirLen, 260U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000520 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
521 LongDir.clear();
522 // Generate a relative directory name with absolute length > 248.
523 size_t LongDirLen = 249 - TestDirectory.size();
524 LongDir.assign(LongDirLen, 'a');
525 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
526 // While we're here, prove that .. and . handling works in these long paths.
527 const char *DotDotDirs = "\\..\\.\\b";
528 LongDir.append(DotDotDirs);
Paul Robinson1b6c7342014-11-13 00:36:34 +0000529 ASSERT_NO_ERROR(fs::create_directory("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000530 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
531 // And clean up.
Paul Robinson1b6c7342014-11-13 00:36:34 +0000532 ASSERT_NO_ERROR(fs::remove("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000533 ASSERT_NO_ERROR(fs::remove(
534 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
535 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
536#endif
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000537}
538
Michael J. Spencer346a1332011-01-05 16:39:05 +0000539TEST_F(FileSystemTest, DirectoryIteration) {
Rafael Espindolac049c652014-06-13 03:20:08 +0000540 std::error_code ec;
Michael J. Spencer1f063602011-01-06 05:57:54 +0000541 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
542 ASSERT_NO_ERROR(ec);
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000543
544 // Create a known hierarchy to recurse over.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000545 ASSERT_NO_ERROR(
546 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
547 ASSERT_NO_ERROR(
548 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
549 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
550 "/recursive/dontlookhere/da1"));
551 ASSERT_NO_ERROR(
552 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
553 ASSERT_NO_ERROR(
554 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000555 typedef std::vector<std::string> v_t;
556 v_t visited;
557 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
558 + "/recursive", ec), e; i != e; i.increment(ec)){
559 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000560 if (path::filename(i->path()) == "p1") {
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000561 i.pop();
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000562 // FIXME: recursive_directory_iterator should be more robust.
563 if (i == e) break;
564 }
Michael J. Spencer9b54a3e2011-12-09 01:14:41 +0000565 if (path::filename(i->path()) == "dontlookhere")
566 i.no_push();
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000567 visited.push_back(path::filename(i->path()));
568 }
569 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
570 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
571 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
572 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
573 "dontlookhere");
574 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
575 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
576 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
577 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
578 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
579
580 // Make sure that each path was visited correctly.
581 ASSERT_NE(a0, visited.end());
582 ASSERT_NE(aa1, visited.end());
583 ASSERT_NE(ab1, visited.end());
584 ASSERT_NE(dontlookhere, visited.end());
585 ASSERT_EQ(da1, visited.end()); // Not visited.
586 ASSERT_NE(z0, visited.end());
587 ASSERT_NE(za1, visited.end());
588 ASSERT_NE(pop, visited.end());
589 ASSERT_EQ(p1, visited.end()); // Not visited.
590
591 // Make sure that parents were visited before children. No other ordering
592 // guarantees can be made across siblings.
593 ASSERT_LT(a0, aa1);
594 ASSERT_LT(a0, ab1);
595 ASSERT_LT(z0, za1);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000596
597 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
598 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
599 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
600 ASSERT_NO_ERROR(
601 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
602 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
603 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
604 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
605 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
606 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
607 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
Michael J. Spencer1f063602011-01-06 05:57:54 +0000608}
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000609
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000610const char archive[] = "!<arch>\x0A";
611const char bitcode[] = "\xde\xc0\x17\x0b";
Rui Ueyama829c4392013-11-14 22:09:08 +0000612const char coff_object[] = "\x00\x00......";
Rui Ueyama5c69ff52014-09-11 22:34:32 +0000613const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
Rui Ueyama2acb0582014-09-11 21:09:57 +0000614 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000615const char coff_import_library[] = "\x00\x00\xff\xff....";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000616const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
617 0, 0, 0, 0, 0, 0, 0, 0, 1 };
618const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00";
619const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01";
620const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02";
621const char macho_fixed_virtual_memory_shared_lib[] =
622 "\xfe\xed\xfa\xce..........\x00\x03";
623const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04";
624const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05";
625const char macho_dynamically_linked_shared_lib[] =
626 "\xfe\xed\xfa\xce..........\x00\x06";
627const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07";
628const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08";
629const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a";
Justin Bognera7ad4b32015-02-25 22:59:20 +0000630const char macho_kext_bundle[] = "\xfe\xed\xfa\xce..........\x00\x0b";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000631const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000632const char macho_dynamically_linked_shared_lib_stub[] =
633 "\xfe\xed\xfa\xce..........\x00\x09";
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000634
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000635TEST_F(FileSystemTest, Magic) {
636 struct type {
637 const char *filename;
638 const char *magic_str;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000639 size_t magic_str_len;
640 fs::file_magic magic;
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000641 } types[] = {
642#define DEFINE(magic) \
643 { #magic, magic, sizeof(magic), fs::file_magic::magic }
644 DEFINE(archive),
645 DEFINE(bitcode),
Rui Ueyama829c4392013-11-14 22:09:08 +0000646 DEFINE(coff_object),
Rui Ueyama2acb0582014-09-11 21:09:57 +0000647 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000648 DEFINE(coff_import_library),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000649 DEFINE(elf_relocatable),
650 DEFINE(macho_universal_binary),
651 DEFINE(macho_object),
652 DEFINE(macho_executable),
653 DEFINE(macho_fixed_virtual_memory_shared_lib),
654 DEFINE(macho_core),
655 DEFINE(macho_preload_executable),
656 DEFINE(macho_dynamically_linked_shared_lib),
657 DEFINE(macho_dynamic_linker),
658 DEFINE(macho_bundle),
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000659 DEFINE(macho_dynamically_linked_shared_lib_stub),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000660 DEFINE(macho_dsym_companion),
Justin Bognera7ad4b32015-02-25 22:59:20 +0000661 DEFINE(macho_kext_bundle),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000662 DEFINE(windows_resource)
663#undef DEFINE
664 };
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000665
666 // Create some files filled with magic.
667 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
668 ++i) {
669 SmallString<128> file_pathname(TestDirectory);
670 path::append(file_pathname, i->filename);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000671 std::error_code EC;
672 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000673 ASSERT_FALSE(file.has_error());
674 StringRef magic(i->magic_str, i->magic_str_len);
675 file << magic;
Michael J. Spencerb5871802011-01-15 18:52:49 +0000676 file.close();
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000677 EXPECT_EQ(i->magic, fs::identify_magic(magic));
Rafael Espindola78dcc032014-01-10 20:36:42 +0000678 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000679 }
Michael J. Spencer98847782010-11-24 19:20:05 +0000680}
681
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000682#ifdef LLVM_ON_WIN32
683TEST_F(FileSystemTest, CarriageReturn) {
684 SmallString<128> FilePathname(TestDirectory);
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000685 std::error_code EC;
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000686 path::append(FilePathname, "test");
687
688 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000689 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
690 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000691 File << '\n';
692 }
693 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000694 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000695 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000696 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000697 }
698
699 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000700 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
701 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000702 File << '\n';
703 }
704 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000705 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000706 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000707 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000708 }
Rafael Espindola78dcc032014-01-10 20:36:42 +0000709 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000710}
711#endif
712
Rafael Espindola59aaa6c2014-12-12 17:55:12 +0000713TEST_F(FileSystemTest, Resize) {
714 int FD;
715 SmallString<64> TempPath;
716 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
717 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
718 fs::file_status Status;
719 ASSERT_NO_ERROR(fs::status(FD, Status));
720 ASSERT_EQ(Status.getSize(), 123U);
721}
722
Nick Kledzik18497e92012-06-20 00:28:54 +0000723TEST_F(FileSystemTest, FileMapping) {
724 // Create a temp file.
725 int FileDescriptor;
726 SmallString<64> TempPath;
727 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000728 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000729 unsigned Size = 4096;
730 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
731
Nick Kledzik18497e92012-06-20 00:28:54 +0000732 // Map in temp file and add some content
Rafael Espindolac049c652014-06-13 03:20:08 +0000733 std::error_code EC;
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000734 StringRef Val("hello there");
735 {
736 fs::mapped_file_region mfr(FileDescriptor,
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000737 fs::mapped_file_region::readwrite, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000738 ASSERT_NO_ERROR(EC);
739 std::copy(Val.begin(), Val.end(), mfr.data());
740 // Explicitly add a 0.
741 mfr.data()[Val.size()] = 0;
742 // Unmap temp file
743 }
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000744
Nick Kledzik18497e92012-06-20 00:28:54 +0000745 // Map it back in read-only
Rafael Espindola71bc5072014-12-11 17:17:26 +0000746 int FD;
747 EC = fs::openFileForRead(Twine(TempPath), FD);
748 ASSERT_NO_ERROR(EC);
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000749 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000750 ASSERT_NO_ERROR(EC);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000751
Nick Kledzik18497e92012-06-20 00:28:54 +0000752 // Verify content
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000753 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000754
Nick Kledzik18497e92012-06-20 00:28:54 +0000755 // Unmap temp file
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000756 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000757 ASSERT_NO_ERROR(EC);
Rafael Espindola71bc5072014-12-11 17:17:26 +0000758 ASSERT_EQ(close(FD), 0);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000759}
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000760
761TEST(Support, NormalizePath) {
762#if defined(LLVM_ON_WIN32)
763#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
764 EXPECT_EQ(path__, windows__);
765#else
766#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
767 EXPECT_EQ(path__, not_windows__);
768#endif
769
770 SmallString<64> Path1("a");
771 SmallString<64> Path2("a/b");
772 SmallString<64> Path3("a\\b");
773 SmallString<64> Path4("a\\\\b");
774 SmallString<64> Path5("\\a");
775 SmallString<64> Path6("a\\");
776
Rafael Espindola7e774c22014-08-08 21:35:52 +0000777 path::native(Path1);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000778 EXPECT_PATH_IS(Path1, "a", "a");
779
Rafael Espindola7e774c22014-08-08 21:35:52 +0000780 path::native(Path2);
Rafael Espindolaffbabb72014-08-09 00:37:05 +0000781 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000782
Rafael Espindola7e774c22014-08-08 21:35:52 +0000783 path::native(Path3);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000784 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
785
Rafael Espindola7e774c22014-08-08 21:35:52 +0000786 path::native(Path4);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000787 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
788
Rafael Espindola7e774c22014-08-08 21:35:52 +0000789 path::native(Path5);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000790 EXPECT_PATH_IS(Path5, "\\a", "/a");
791
Rafael Espindola7e774c22014-08-08 21:35:52 +0000792 path::native(Path6);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000793 EXPECT_PATH_IS(Path6, "a\\", "a/");
794
795#undef EXPECT_PATH_IS
796}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000797
798TEST(Support, RemoveLeadingDotSlash) {
799 StringRef Path1("././/foolz/wat");
800 StringRef Path2("./////");
801
802 Path1 = path::remove_leading_dotslash(Path1);
803 EXPECT_EQ(Path1, "foolz/wat");
804 Path2 = path::remove_leading_dotslash(Path2);
805 EXPECT_EQ(Path2, "");
806}
Michael J. Spencer98847782010-11-24 19:20:05 +0000807} // anonymous namespace