blob: a1e13650cf1714dfa639fceae2537917dcc19bc1 [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"
David Majnemer0d955d02016-08-11 22:21:41 +000011#include "llvm/ADT/STLExtras.h"
Pawel Bylica7187e4b2015-10-16 09:08:59 +000012#include "llvm/Support/ConvertUTF.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000013#include "llvm/Support/Errc.h"
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000014#include "llvm/Support/ErrorHandling.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000015#include "llvm/Support/FileSystem.h"
Rafael Espindola84ab9b32013-07-19 14:41:25 +000016#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f063602011-01-06 05:57:54 +000017#include "llvm/Support/raw_ostream.h"
Michael J. Spencer98847782010-11-24 19:20:05 +000018#include "gtest/gtest.h"
19
Rafael Espindolaa813d602014-06-11 03:58:34 +000020#ifdef LLVM_ON_WIN32
Nico Weberdd2ca832016-04-18 13:54:50 +000021#include "llvm/ADT/ArrayRef.h"
NAKAMURA Takumib94f0522015-06-16 06:46:16 +000022#include <windows.h>
Rafael Espindolaa813d602014-06-11 03:58:34 +000023#include <winerror.h>
24#endif
25
Frederic Riss6b9396c2015-08-06 21:04:55 +000026#ifdef LLVM_ON_UNIX
27#include <sys/stat.h>
28#endif
29
Michael J. Spencerebad2f92010-11-29 22:28:51 +000030using namespace llvm;
Michael J. Spencer45710402010-12-03 01:21:28 +000031using namespace llvm::sys;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000032
Rafael Espindolac049c652014-06-13 03:20:08 +000033#define ASSERT_NO_ERROR(x) \
34 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
35 SmallString<128> MessageStorage; \
36 raw_svector_ostream Message(MessageStorage); \
37 Message << #x ": did not return errc::success.\n" \
38 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
39 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
40 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
41 } else { \
42 }
Michael J. Spencer3b264ba2011-01-04 17:00:18 +000043
Michael J. Spencer98847782010-11-24 19:20:05 +000044namespace {
45
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000046TEST(is_separator, Works) {
47 EXPECT_TRUE(path::is_separator('/'));
48 EXPECT_FALSE(path::is_separator('\0'));
49 EXPECT_FALSE(path::is_separator('-'));
50 EXPECT_FALSE(path::is_separator(' '));
51
52#ifdef LLVM_ON_WIN32
53 EXPECT_TRUE(path::is_separator('\\'));
54#else
55 EXPECT_FALSE(path::is_separator('\\'));
56#endif
57}
58
Michael J. Spencer3ef91c52010-11-29 22:29:04 +000059TEST(Support, Path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000060 SmallVector<StringRef, 40> paths;
61 paths.push_back("");
62 paths.push_back(".");
63 paths.push_back("..");
64 paths.push_back("foo");
65 paths.push_back("/");
66 paths.push_back("/foo");
67 paths.push_back("foo/");
68 paths.push_back("/foo/");
69 paths.push_back("foo/bar");
70 paths.push_back("/foo/bar");
71 paths.push_back("//net");
72 paths.push_back("//net/foo");
73 paths.push_back("///foo///");
74 paths.push_back("///foo///bar");
75 paths.push_back("/.");
76 paths.push_back("./");
77 paths.push_back("/..");
78 paths.push_back("../");
79 paths.push_back("foo/.");
80 paths.push_back("foo/..");
81 paths.push_back("foo/./");
82 paths.push_back("foo/./bar");
83 paths.push_back("foo/..");
84 paths.push_back("foo/../");
85 paths.push_back("foo/../bar");
86 paths.push_back("c:");
87 paths.push_back("c:/");
88 paths.push_back("c:foo");
89 paths.push_back("c:/foo");
90 paths.push_back("c:foo/");
91 paths.push_back("c:/foo/");
92 paths.push_back("c:/foo/bar");
93 paths.push_back("prn:");
94 paths.push_back("c:\\");
95 paths.push_back("c:foo");
96 paths.push_back("c:\\foo");
97 paths.push_back("c:foo\\");
98 paths.push_back("c:\\foo\\");
99 paths.push_back("c:\\foo/");
100 paths.push_back("c:/foo\\bar");
101
Justin Bogner0c274ae2014-07-16 08:18:58 +0000102 SmallVector<StringRef, 5> ComponentStack;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000103 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
104 e = paths.end();
105 i != e;
106 ++i) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000107 for (sys::path::const_iterator ci = sys::path::begin(*i),
108 ce = sys::path::end(*i);
109 ci != ce;
110 ++ci) {
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000111 ASSERT_FALSE(ci->empty());
Justin Bogner0c274ae2014-07-16 08:18:58 +0000112 ComponentStack.push_back(*ci);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000113 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000114
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000115 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
116 ce = sys::path::rend(*i);
117 ci != ce;
118 ++ci) {
Justin Bogner0c274ae2014-07-16 08:18:58 +0000119 ASSERT_TRUE(*ci == ComponentStack.back());
120 ComponentStack.pop_back();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000121 }
Justin Bogner0c274ae2014-07-16 08:18:58 +0000122 ASSERT_TRUE(ComponentStack.empty());
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000123
Michael J. Spencerf616b212010-12-07 17:04:04 +0000124 path::has_root_path(*i);
125 path::root_path(*i);
126 path::has_root_name(*i);
127 path::root_name(*i);
128 path::has_root_directory(*i);
129 path::root_directory(*i);
130 path::has_parent_path(*i);
131 path::parent_path(*i);
132 path::has_filename(*i);
133 path::filename(*i);
134 path::has_stem(*i);
135 path::stem(*i);
136 path::has_extension(*i);
137 path::extension(*i);
138 path::is_absolute(*i);
139 path::is_relative(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000140
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000141 SmallString<128> temp_store;
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000142 temp_store = *i;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000143 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000144 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000145 path::remove_filename(temp_store);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000146
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000147 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000148 path::replace_extension(temp_store, "ext");
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000149 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000150 stem = path::stem(filename);
151 ext = path::extension(filename);
Justin Bogner487e7642014-08-04 17:36:41 +0000152 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000153
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000154 path::native(*i, temp_store);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000155 }
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000156
157 SmallString<32> Relative("foo.cpp");
158 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
Benjamin Kramer2b4e14e2015-10-05 14:15:13 +0000159 Relative[5] = '/'; // Fix up windows paths.
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000160 ASSERT_EQ("/root/foo.cpp", Relative);
Michael J. Spencer346a1332011-01-05 16:39:05 +0000161}
Michael J. Spencer45710402010-12-03 01:21:28 +0000162
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000163TEST(Support, RelativePathIterator) {
164 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
165 typedef SmallVector<StringRef, 4> PathComponents;
166 PathComponents ExpectedPathComponents;
167 PathComponents ActualPathComponents;
168
Chandler Carruthe4405e92015-09-10 06:12:31 +0000169 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000170
171 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
172 ++I) {
173 ActualPathComponents.push_back(*I);
174 }
175
176 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
177
178 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
179 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
180 }
181}
182
Ben Langmuir2547f932015-03-10 00:04:29 +0000183TEST(Support, RelativePathDotIterator) {
184 SmallString<64> Path(StringRef(".c/.d/../."));
185 typedef SmallVector<StringRef, 4> PathComponents;
186 PathComponents ExpectedPathComponents;
187 PathComponents ActualPathComponents;
188
Chandler Carruthe4405e92015-09-10 06:12:31 +0000189 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000190
191 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
192 ++I) {
193 ActualPathComponents.push_back(*I);
194 }
195
196 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
197
198 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
199 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
200 }
201}
202
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000203TEST(Support, AbsolutePathIterator) {
204 SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
205 typedef SmallVector<StringRef, 4> PathComponents;
206 PathComponents ExpectedPathComponents;
207 PathComponents ActualPathComponents;
208
Chandler Carruthe4405e92015-09-10 06:12:31 +0000209 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000210
211 // The root path will also be a component when iterating
212 ExpectedPathComponents[0] = "/";
213
214 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
215 ++I) {
216 ActualPathComponents.push_back(*I);
217 }
218
219 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
220
221 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
222 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
223 }
224}
225
Ben Langmuir2547f932015-03-10 00:04:29 +0000226TEST(Support, AbsolutePathDotIterator) {
227 SmallString<64> Path(StringRef("/.c/.d/../."));
228 typedef SmallVector<StringRef, 4> PathComponents;
229 PathComponents ExpectedPathComponents;
230 PathComponents ActualPathComponents;
231
Chandler Carruthe4405e92015-09-10 06:12:31 +0000232 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000233
234 // The root path will also be a component when iterating
235 ExpectedPathComponents[0] = "/";
236
237 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
238 ++I) {
239 ActualPathComponents.push_back(*I);
240 }
241
242 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
243
244 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
245 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
246 }
247}
248
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000249#ifdef LLVM_ON_WIN32
250TEST(Support, AbsolutePathIteratorWin32) {
251 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
252 typedef SmallVector<StringRef, 4> PathComponents;
253 PathComponents ExpectedPathComponents;
254 PathComponents ActualPathComponents;
255
256 StringRef(Path).split(ExpectedPathComponents, "\\");
257
258 // The root path (which comes after the drive name) will also be a component
259 // when iterating.
260 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
261
262 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
263 ++I) {
264 ActualPathComponents.push_back(*I);
265 }
266
267 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
268
269 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
270 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
271 }
272}
273#endif // LLVM_ON_WIN32
274
Ben Langmuir8d116392014-03-05 19:56:30 +0000275TEST(Support, AbsolutePathIteratorEnd) {
276 // Trailing slashes are converted to '.' unless they are part of the root path.
277 SmallVector<StringRef, 4> Paths;
278 Paths.push_back("/foo/");
279 Paths.push_back("/foo//");
280 Paths.push_back("//net//");
281#ifdef LLVM_ON_WIN32
282 Paths.push_back("c:\\\\");
283#endif
284
285 for (StringRef Path : Paths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000286 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000287 EXPECT_EQ(".", LastComponent);
288 }
289
290 SmallVector<StringRef, 3> RootPaths;
291 RootPaths.push_back("/");
292 RootPaths.push_back("//net/");
293#ifdef LLVM_ON_WIN32
294 RootPaths.push_back("c:\\");
295#endif
296
297 for (StringRef Path : RootPaths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000298 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000299 EXPECT_EQ(1u, LastComponent.size());
300 EXPECT_TRUE(path::is_separator(LastComponent[0]));
301 }
302}
303
Peter Collingbournef7d41012014-01-31 23:46:06 +0000304TEST(Support, HomeDirectory) {
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000305 std::string expected;
Pawel Bylica7187e4b2015-10-16 09:08:59 +0000306#ifdef LLVM_ON_WIN32
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000307 if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) {
308 auto pathLen = ::wcslen(path);
309 ArrayRef<char> ref{reinterpret_cast<char const *>(path),
310 pathLen * sizeof(wchar_t)};
311 convertUTF16ToUTF8String(ref, expected);
312 }
Pawel Bylica7187e4b2015-10-16 09:08:59 +0000313#else
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000314 if (char const *path = ::getenv("HOME"))
315 expected = path;
Peter Collingbournef7d41012014-01-31 23:46:06 +0000316#endif
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000317 // Do not try to test it if we don't know what to expect.
318 // On Windows we use something better than env vars.
319 if (!expected.empty()) {
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000320 SmallString<128> HomeDir;
321 auto status = path::home_directory(HomeDir);
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000322 EXPECT_TRUE(status);
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000323 EXPECT_EQ(expected, HomeDir);
324 }
Peter Collingbournef7d41012014-01-31 23:46:06 +0000325}
326
Pawel Bylica0e97e5c2015-11-02 09:49:17 +0000327TEST(Support, UserCacheDirectory) {
328 SmallString<13> CacheDir;
329 SmallString<20> CacheDir2;
330 auto Status = path::user_cache_directory(CacheDir, "");
331 EXPECT_TRUE(Status ^ CacheDir.empty());
332
333 if (Status) {
334 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed
335 EXPECT_EQ(CacheDir, CacheDir2); // and return same paths
336
337 EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c"));
338 auto It = path::rbegin(CacheDir);
339 EXPECT_EQ("file.c", *It);
340 EXPECT_EQ("B", *++It);
341 EXPECT_EQ("A", *++It);
342 auto ParentDir = *++It;
343
344 // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0"
345 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2",
346 "\xE2\x84\xB5.0"));
347 auto It2 = path::rbegin(CacheDir2);
348 EXPECT_EQ("\xE2\x84\xB5.0", *It2);
349 EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2);
350 auto ParentDir2 = *++It2;
351
352 EXPECT_EQ(ParentDir, ParentDir2);
353 }
354}
355
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000356TEST(Support, TempDirectory) {
357 SmallString<32> TempDir;
358 path::system_temp_directory(false, TempDir);
359 EXPECT_TRUE(!TempDir.empty());
360 TempDir.clear();
361 path::system_temp_directory(true, TempDir);
362 EXPECT_TRUE(!TempDir.empty());
363}
364
David Blaikie06d56182015-11-17 20:38:54 +0000365#ifdef LLVM_ON_WIN32
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000366static std::string path2regex(std::string Path) {
367 size_t Pos = 0;
368 while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
369 Path.replace(Pos, 1, "\\\\");
370 Pos += 2;
371 }
372 return Path;
373}
374
375/// Helper for running temp dir test in separated process. See below.
376#define EXPECT_TEMP_DIR(prepare, expected) \
377 EXPECT_EXIT( \
378 { \
379 prepare; \
380 SmallString<300> TempDir; \
381 path::system_temp_directory(true, TempDir); \
382 raw_os_ostream(std::cerr) << TempDir; \
383 std::exit(0); \
384 }, \
385 ::testing::ExitedWithCode(0), path2regex(expected))
386
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000387TEST(SupportDeathTest, TempDirectoryOnWindows) {
388 // In this test we want to check how system_temp_directory responds to
389 // different values of specific env vars. To prevent corrupting env vars of
390 // the current process all checks are done in separated processes.
391 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder");
392 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"),
393 "C:\\Unix\\Path\\Seperators");
394 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$");
395 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep");
396 EXPECT_TEMP_DIR(
397 _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
398 "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
399
400 // Test $TMP empty, $TEMP set.
401 EXPECT_TEMP_DIR(
402 {
403 _wputenv_s(L"TMP", L"");
404 _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
405 },
406 "C:\\Valid\\Path");
407
408 // All related env vars empty
409 EXPECT_TEMP_DIR(
410 {
411 _wputenv_s(L"TMP", L"");
412 _wputenv_s(L"TEMP", L"");
413 _wputenv_s(L"USERPROFILE", L"");
414 },
415 "C:\\Temp");
416
417 // Test evn var / path with 260 chars.
418 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
419 while (Expected.size() < 260)
420 Expected.append("\\DirNameWith19Charss");
Reid Kleckner88e9ff62016-02-10 19:29:01 +0000421 ASSERT_EQ(260U, Expected.size());
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000422 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
423}
424#endif
425
Michael J. Spencer346a1332011-01-05 16:39:05 +0000426class FileSystemTest : public testing::Test {
427protected:
428 /// Unique temporary directory in which all created filesystem entities must
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000429 /// be placed. It is removed at the end of each test (must be empty).
Michael J. Spencer346a1332011-01-05 16:39:05 +0000430 SmallString<128> TestDirectory;
431
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000432 void SetUp() override {
Michael J. Spencer346a1332011-01-05 16:39:05 +0000433 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000434 fs::createUniqueDirectory("file-system-test", TestDirectory));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000435 // We don't care about this specific file.
Michael J. Spencer42368cc2011-01-05 16:39:46 +0000436 errs() << "Test Directory: " << TestDirectory << '\n';
437 errs().flush();
Michael J. Spencer346a1332011-01-05 16:39:05 +0000438 }
439
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000440 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000441};
442
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000443TEST_F(FileSystemTest, Unique) {
444 // Create a temp file.
445 int FileDescriptor;
446 SmallString<64> TempPath;
447 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000448 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000449
450 // The same file should return an identical unique id.
Rafael Espindola7f822a92013-07-29 21:26:49 +0000451 fs::UniqueID F1, F2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000452 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
453 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000454 ASSERT_EQ(F1, F2);
455
456 // Different files should return different unique ids.
457 int FileDescriptor2;
458 SmallString<64> TempPath2;
459 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000460 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
461
Rafael Espindola7f822a92013-07-29 21:26:49 +0000462 fs::UniqueID D;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000463 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000464 ASSERT_NE(D, F1);
465 ::close(FileDescriptor2);
466
467 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
468
469 // Two paths representing the same file on disk should still provide the
470 // same unique id. We can test this by making a hard link.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000471 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Rafael Espindola7f822a92013-07-29 21:26:49 +0000472 fs::UniqueID D2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000473 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000474 ASSERT_EQ(D2, F1);
475
476 ::close(FileDescriptor);
Rafael Espindolaa5932af2013-07-30 20:25:53 +0000477
478 SmallString<128> Dir1;
479 ASSERT_NO_ERROR(
480 fs::createUniqueDirectory("dir1", Dir1));
481 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
482 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
483 ASSERT_EQ(F1, F2);
484
485 SmallString<128> Dir2;
486 ASSERT_NO_ERROR(
487 fs::createUniqueDirectory("dir2", Dir2));
488 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
489 ASSERT_NE(F1, F2);
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000490}
491
Michael J. Spencer346a1332011-01-05 16:39:05 +0000492TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000493 // Create a temp file.
Michael J. Spencer45710402010-12-03 01:21:28 +0000494 int FileDescriptor;
495 SmallString<64> TempPath;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000496 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000497 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Michael J. Spencer45710402010-12-03 01:21:28 +0000498
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000499 // Make sure it exists.
Rafael Espindolac1599152014-09-11 19:11:02 +0000500 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000501
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000502 // Create another temp tile.
503 int FD2;
504 SmallString<64> TempPath2;
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000505 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
Rafael Espindolad3c89042013-07-25 15:00:17 +0000506 ASSERT_TRUE(TempPath2.endswith(".temp"));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000507 ASSERT_NE(TempPath.str(), TempPath2.str());
508
Michael J. Spencer203d7802011-12-12 06:04:28 +0000509 fs::file_status A, B;
510 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
511 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
512 EXPECT_FALSE(fs::equivalent(A, B));
513
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000514 ::close(FD2);
Rafael Espindola213c4cb2013-07-18 03:29:51 +0000515
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000516 // Remove Temp2.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000517 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
518 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
519 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000520 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000521
Rafael Espindolac049c652014-06-13 03:20:08 +0000522 std::error_code EC = fs::status(TempPath2.c_str(), B);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000523 EXPECT_EQ(EC, errc::no_such_file_or_directory);
Rafael Espindola107b74c2013-07-31 00:10:25 +0000524 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
525
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000526 // Make sure Temp2 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000527 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
528 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000529
Rafael Espindolad3c89042013-07-25 15:00:17 +0000530 SmallString<64> TempPath3;
531 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
532 ASSERT_FALSE(TempPath3.endswith("."));
533
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000534 // Create a hard link to Temp1.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000535 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000536 bool equal;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000537 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000538 EXPECT_TRUE(equal);
Michael J. Spencer203d7802011-12-12 06:04:28 +0000539 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
540 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
541 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000542
543 // Remove Temp1.
Michael J. Spencer45710402010-12-03 01:21:28 +0000544 ::close(FileDescriptor);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000545 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000546
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000547 // Remove the hard link.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000548 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000549
550 // Make sure Temp1 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000551 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
552 errc::no_such_file_or_directory);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000553
554#ifdef LLVM_ON_WIN32
555 // Path name > 260 chars should get an error.
556 const char *Path270 =
557 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
558 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
559 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
560 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
561 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000562 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000563 errc::invalid_argument);
564 // Relative path < 247 chars, no problem.
565 const char *Path216 =
566 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
567 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
568 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
569 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000570 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000571 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000572#endif
Michael J. Spencer346a1332011-01-05 16:39:05 +0000573}
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000574
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000575TEST_F(FileSystemTest, CreateDir) {
576 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
577 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
578 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000579 errc::file_exists);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000580 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000581
Frederic Riss6b9396c2015-08-06 21:04:55 +0000582#ifdef LLVM_ON_UNIX
583 // Set a 0000 umask so that we can test our directory permissions.
584 mode_t OldUmask = ::umask(0000);
585
586 fs::file_status Status;
587 ASSERT_NO_ERROR(
588 fs::create_directory(Twine(TestDirectory) + "baz500", false,
589 fs::perms::owner_read | fs::perms::owner_exe));
590 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
591 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
592 fs::perms::owner_read | fs::perms::owner_exe);
593 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
594 fs::perms::all_all));
595 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
596 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
597
598 // Restore umask to be safe.
599 ::umask(OldUmask);
600#endif
601
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000602#ifdef LLVM_ON_WIN32
603 // Prove that create_directories() can handle a pathname > 248 characters,
604 // which is the documented limit for CreateDirectory().
605 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
606 // Generate a directory path guaranteed to fall into that range.
607 size_t TmpLen = TestDirectory.size();
608 const char *OneDir = "\\123456789";
609 size_t OneDirLen = strlen(OneDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000610 ASSERT_LT(OneDirLen, 12U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000611 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
612 SmallString<260> LongDir(TestDirectory);
613 for (size_t I = 0; I < NLevels; ++I)
614 LongDir.append(OneDir);
615 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
616 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
617 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
618 errc::file_exists);
619 // Tidy up, "recursively" removing the directories.
620 StringRef ThisDir(LongDir);
621 for (size_t J = 0; J < NLevels; ++J) {
622 ASSERT_NO_ERROR(fs::remove(ThisDir));
623 ThisDir = path::parent_path(ThisDir);
624 }
625
626 // Similarly for a relative pathname. Need to set the current directory to
627 // TestDirectory so that the one we create ends up in the right place.
628 char PreviousDir[260];
629 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000630 ASSERT_GT(PreviousDirLen, 0U);
631 ASSERT_LT(PreviousDirLen, 260U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000632 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
633 LongDir.clear();
634 // Generate a relative directory name with absolute length > 248.
635 size_t LongDirLen = 249 - TestDirectory.size();
636 LongDir.assign(LongDirLen, 'a');
637 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
638 // While we're here, prove that .. and . handling works in these long paths.
639 const char *DotDotDirs = "\\..\\.\\b";
640 LongDir.append(DotDotDirs);
Paul Robinson1b6c7342014-11-13 00:36:34 +0000641 ASSERT_NO_ERROR(fs::create_directory("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000642 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
643 // And clean up.
Paul Robinson1b6c7342014-11-13 00:36:34 +0000644 ASSERT_NO_ERROR(fs::remove("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000645 ASSERT_NO_ERROR(fs::remove(
646 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
647 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
648#endif
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000649}
650
Michael J. Spencer346a1332011-01-05 16:39:05 +0000651TEST_F(FileSystemTest, DirectoryIteration) {
Rafael Espindolac049c652014-06-13 03:20:08 +0000652 std::error_code ec;
Michael J. Spencer1f063602011-01-06 05:57:54 +0000653 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
654 ASSERT_NO_ERROR(ec);
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000655
656 // Create a known hierarchy to recurse over.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000657 ASSERT_NO_ERROR(
658 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
659 ASSERT_NO_ERROR(
660 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
661 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
662 "/recursive/dontlookhere/da1"));
663 ASSERT_NO_ERROR(
664 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
665 ASSERT_NO_ERROR(
666 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000667 typedef std::vector<std::string> v_t;
668 v_t visited;
669 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
670 + "/recursive", ec), e; i != e; i.increment(ec)){
671 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000672 if (path::filename(i->path()) == "p1") {
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000673 i.pop();
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000674 // FIXME: recursive_directory_iterator should be more robust.
675 if (i == e) break;
676 }
Michael J. Spencer9b54a3e2011-12-09 01:14:41 +0000677 if (path::filename(i->path()) == "dontlookhere")
678 i.no_push();
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000679 visited.push_back(path::filename(i->path()));
680 }
David Majnemer0d955d02016-08-11 22:21:41 +0000681 v_t::const_iterator a0 = find(visited, "a0");
682 v_t::const_iterator aa1 = find(visited, "aa1");
683 v_t::const_iterator ab1 = find(visited, "ab1");
684 v_t::const_iterator dontlookhere = find(visited, "dontlookhere");
685 v_t::const_iterator da1 = find(visited, "da1");
686 v_t::const_iterator z0 = find(visited, "z0");
687 v_t::const_iterator za1 = find(visited, "za1");
688 v_t::const_iterator pop = find(visited, "pop");
689 v_t::const_iterator p1 = find(visited, "p1");
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000690
691 // Make sure that each path was visited correctly.
692 ASSERT_NE(a0, visited.end());
693 ASSERT_NE(aa1, visited.end());
694 ASSERT_NE(ab1, visited.end());
695 ASSERT_NE(dontlookhere, visited.end());
696 ASSERT_EQ(da1, visited.end()); // Not visited.
697 ASSERT_NE(z0, visited.end());
698 ASSERT_NE(za1, visited.end());
699 ASSERT_NE(pop, visited.end());
700 ASSERT_EQ(p1, visited.end()); // Not visited.
701
702 // Make sure that parents were visited before children. No other ordering
703 // guarantees can be made across siblings.
704 ASSERT_LT(a0, aa1);
705 ASSERT_LT(a0, ab1);
706 ASSERT_LT(z0, za1);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000707
708 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
709 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
710 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
711 ASSERT_NO_ERROR(
712 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
713 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
714 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
715 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
716 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
717 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
718 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
Bruno Cardoso Lopesead771c2016-05-13 21:31:32 +0000719
720 // Test recursive_directory_iterator level()
721 ASSERT_NO_ERROR(
722 fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c"));
723 fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E;
724 for (int l = 0; I != E; I.increment(ec), ++l) {
725 ASSERT_NO_ERROR(ec);
726 EXPECT_EQ(I.level(), l);
727 }
728 EXPECT_EQ(I, E);
729 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c"));
730 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b"));
731 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a"));
732 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel"));
Michael J. Spencer1f063602011-01-06 05:57:54 +0000733}
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000734
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000735const char archive[] = "!<arch>\x0A";
736const char bitcode[] = "\xde\xc0\x17\x0b";
Rui Ueyama829c4392013-11-14 22:09:08 +0000737const char coff_object[] = "\x00\x00......";
Rui Ueyama5c69ff52014-09-11 22:34:32 +0000738const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
Rui Ueyama2acb0582014-09-11 21:09:57 +0000739 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000740const char coff_import_library[] = "\x00\x00\xff\xff....";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000741const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
742 0, 0, 0, 0, 0, 0, 0, 0, 1 };
Etienne Bergeron1562f692016-04-05 01:46:26 +0000743const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
Kevin Enderby87c85b72016-01-26 23:43:37 +0000744const char macho_object[] =
745 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
746const char macho_executable[] =
747 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000748const char macho_fixed_virtual_memory_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000749 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
750const char macho_core[] =
751 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
752const char macho_preload_executable[] =
753 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000754const char macho_dynamically_linked_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000755 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
756const char macho_dynamic_linker[] =
757 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
758const char macho_bundle[] =
759 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
760const char macho_dsym_companion[] =
761 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
762const char macho_kext_bundle[] =
763 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000764const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000765const char macho_dynamically_linked_shared_lib_stub[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000766 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000767
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000768TEST_F(FileSystemTest, Magic) {
769 struct type {
770 const char *filename;
771 const char *magic_str;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000772 size_t magic_str_len;
773 fs::file_magic magic;
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000774 } types[] = {
775#define DEFINE(magic) \
776 { #magic, magic, sizeof(magic), fs::file_magic::magic }
777 DEFINE(archive),
778 DEFINE(bitcode),
Rui Ueyama829c4392013-11-14 22:09:08 +0000779 DEFINE(coff_object),
Rui Ueyama2acb0582014-09-11 21:09:57 +0000780 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000781 DEFINE(coff_import_library),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000782 DEFINE(elf_relocatable),
783 DEFINE(macho_universal_binary),
784 DEFINE(macho_object),
785 DEFINE(macho_executable),
786 DEFINE(macho_fixed_virtual_memory_shared_lib),
787 DEFINE(macho_core),
788 DEFINE(macho_preload_executable),
789 DEFINE(macho_dynamically_linked_shared_lib),
790 DEFINE(macho_dynamic_linker),
791 DEFINE(macho_bundle),
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000792 DEFINE(macho_dynamically_linked_shared_lib_stub),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000793 DEFINE(macho_dsym_companion),
Justin Bognera7ad4b32015-02-25 22:59:20 +0000794 DEFINE(macho_kext_bundle),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000795 DEFINE(windows_resource)
796#undef DEFINE
797 };
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000798
799 // Create some files filled with magic.
800 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
801 ++i) {
802 SmallString<128> file_pathname(TestDirectory);
803 path::append(file_pathname, i->filename);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000804 std::error_code EC;
805 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000806 ASSERT_FALSE(file.has_error());
807 StringRef magic(i->magic_str, i->magic_str_len);
808 file << magic;
Michael J. Spencerb5871802011-01-15 18:52:49 +0000809 file.close();
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000810 EXPECT_EQ(i->magic, fs::identify_magic(magic));
Rafael Espindola78dcc032014-01-10 20:36:42 +0000811 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000812 }
Michael J. Spencer98847782010-11-24 19:20:05 +0000813}
814
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000815#ifdef LLVM_ON_WIN32
816TEST_F(FileSystemTest, CarriageReturn) {
817 SmallString<128> FilePathname(TestDirectory);
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000818 std::error_code EC;
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000819 path::append(FilePathname, "test");
820
821 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000822 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
823 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000824 File << '\n';
825 }
826 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000827 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000828 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000829 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000830 }
831
832 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000833 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
834 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000835 File << '\n';
836 }
837 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000838 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000839 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000840 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000841 }
Rafael Espindola78dcc032014-01-10 20:36:42 +0000842 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000843}
844#endif
845
Rafael Espindola59aaa6c2014-12-12 17:55:12 +0000846TEST_F(FileSystemTest, Resize) {
847 int FD;
848 SmallString<64> TempPath;
849 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
850 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
851 fs::file_status Status;
852 ASSERT_NO_ERROR(fs::status(FD, Status));
853 ASSERT_EQ(Status.getSize(), 123U);
854}
855
Nick Kledzik18497e92012-06-20 00:28:54 +0000856TEST_F(FileSystemTest, FileMapping) {
857 // Create a temp file.
858 int FileDescriptor;
859 SmallString<64> TempPath;
860 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000861 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000862 unsigned Size = 4096;
863 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
864
Nick Kledzik18497e92012-06-20 00:28:54 +0000865 // Map in temp file and add some content
Rafael Espindolac049c652014-06-13 03:20:08 +0000866 std::error_code EC;
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000867 StringRef Val("hello there");
868 {
869 fs::mapped_file_region mfr(FileDescriptor,
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000870 fs::mapped_file_region::readwrite, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000871 ASSERT_NO_ERROR(EC);
872 std::copy(Val.begin(), Val.end(), mfr.data());
873 // Explicitly add a 0.
874 mfr.data()[Val.size()] = 0;
875 // Unmap temp file
876 }
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000877
Nick Kledzik18497e92012-06-20 00:28:54 +0000878 // Map it back in read-only
Rafael Espindola71bc5072014-12-11 17:17:26 +0000879 int FD;
880 EC = fs::openFileForRead(Twine(TempPath), FD);
881 ASSERT_NO_ERROR(EC);
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000882 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000883 ASSERT_NO_ERROR(EC);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000884
Nick Kledzik18497e92012-06-20 00:28:54 +0000885 // Verify content
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000886 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000887
Nick Kledzik18497e92012-06-20 00:28:54 +0000888 // Unmap temp file
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000889 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000890 ASSERT_NO_ERROR(EC);
Rafael Espindola71bc5072014-12-11 17:17:26 +0000891 ASSERT_EQ(close(FD), 0);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000892}
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000893
894TEST(Support, NormalizePath) {
895#if defined(LLVM_ON_WIN32)
896#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
897 EXPECT_EQ(path__, windows__);
898#else
899#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
900 EXPECT_EQ(path__, not_windows__);
901#endif
902
903 SmallString<64> Path1("a");
904 SmallString<64> Path2("a/b");
905 SmallString<64> Path3("a\\b");
906 SmallString<64> Path4("a\\\\b");
907 SmallString<64> Path5("\\a");
908 SmallString<64> Path6("a\\");
909
Rafael Espindola7e774c22014-08-08 21:35:52 +0000910 path::native(Path1);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000911 EXPECT_PATH_IS(Path1, "a", "a");
912
Rafael Espindola7e774c22014-08-08 21:35:52 +0000913 path::native(Path2);
Rafael Espindolaffbabb72014-08-09 00:37:05 +0000914 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000915
Rafael Espindola7e774c22014-08-08 21:35:52 +0000916 path::native(Path3);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000917 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
918
Rafael Espindola7e774c22014-08-08 21:35:52 +0000919 path::native(Path4);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000920 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
921
Rafael Espindola7e774c22014-08-08 21:35:52 +0000922 path::native(Path5);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000923 EXPECT_PATH_IS(Path5, "\\a", "/a");
924
Rafael Espindola7e774c22014-08-08 21:35:52 +0000925 path::native(Path6);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000926 EXPECT_PATH_IS(Path6, "a\\", "a/");
927
928#undef EXPECT_PATH_IS
929}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000930
931TEST(Support, RemoveLeadingDotSlash) {
932 StringRef Path1("././/foolz/wat");
933 StringRef Path2("./////");
934
935 Path1 = path::remove_leading_dotslash(Path1);
936 EXPECT_EQ(Path1, "foolz/wat");
937 Path2 = path::remove_leading_dotslash(Path2);
938 EXPECT_EQ(Path2, "");
939}
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000940
941static std::string remove_dots(StringRef path,
942 bool remove_dot_dot) {
943 SmallString<256> buffer(path);
944 path::remove_dots(buffer, remove_dot_dot);
945 return buffer.str();
946}
947
948TEST(Support, RemoveDots) {
Mike Aizatskyf8cf7132015-11-09 19:36:53 +0000949#if defined(LLVM_ON_WIN32)
950 EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false));
951 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false));
952
953 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false));
954 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true));
955 EXPECT_EQ("c", remove_dots(".\\.\\c", true));
956
957 SmallString<64> Path1(".\\.\\c");
958 EXPECT_TRUE(path::remove_dots(Path1, true));
959 EXPECT_EQ("c", Path1);
960#else
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000961 EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false));
962 EXPECT_EQ("", remove_dots("./////", false));
963
964 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false));
965 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true));
966 EXPECT_EQ("c", remove_dots("././c", true));
967
968 SmallString<64> Path1("././c");
969 EXPECT_TRUE(path::remove_dots(Path1, true));
970 EXPECT_EQ("c", Path1);
Mike Aizatskyf8cf7132015-11-09 19:36:53 +0000971#endif
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000972}
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000973
974TEST(Support, ReplacePathPrefix) {
975 SmallString<64> Path1("/foo");
976 SmallString<64> Path2("/old/foo");
977 SmallString<64> OldPrefix("/old");
978 SmallString<64> NewPrefix("/new");
979 SmallString<64> NewPrefix2("/longernew");
980 SmallString<64> EmptyPrefix("");
981
982 SmallString<64> Path = Path1;
983 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
984 EXPECT_EQ(Path, "/foo");
985 Path = Path2;
986 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
987 EXPECT_EQ(Path, "/new/foo");
988 Path = Path2;
989 path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
990 EXPECT_EQ(Path, "/longernew/foo");
991 Path = Path1;
992 path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
993 EXPECT_EQ(Path, "/new/foo");
994 Path = Path2;
995 path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
996 EXPECT_EQ(Path, "/foo");
997}
Taewook Ohd9153272016-06-13 15:54:56 +0000998
999TEST_F(FileSystemTest, PathFromFD) {
1000 // Create a temp file.
1001 int FileDescriptor;
1002 SmallString<64> TempPath;
1003 ASSERT_NO_ERROR(
1004 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1005
1006 // Make sure it exists.
1007 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1008
1009 // Try to get the path from the file descriptor
1010 SmallString<64> ResultPath;
1011 std::error_code ErrorCode =
1012 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1013
1014 // If we succeeded, check that the paths are the same (modulo case):
1015 if (!ErrorCode) {
1016 // The paths returned by createTemporaryFile and getPathFromOpenFD
1017 // should reference the same file on disk.
1018 fs::UniqueID D1, D2;
1019 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1020 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1021 ASSERT_EQ(D1, D2);
1022 }
1023
1024 ::close(FileDescriptor);
1025}
1026
Aaron Ballman3dd74b82016-06-20 20:28:49 +00001027TEST_F(FileSystemTest, PathFromFDWin32) {
1028 // Create a temp file.
1029 int FileDescriptor;
1030 SmallString<64> TempPath;
1031 ASSERT_NO_ERROR(
1032 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1033
1034 // Make sure it exists.
1035 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1036
1037 SmallVector<char, 8> ResultPath;
1038 std::error_code ErrorCode =
1039 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1040
1041 if (!ErrorCode) {
1042 // Now that we know how much space is required for the path, create a path
1043 // buffer with exactly enough space (sans null terminator, which should not
1044 // be present), and call getPathFromOpenFD again to ensure that the API
1045 // properly handles exactly-sized buffers.
1046 SmallVector<char, 8> ExactSizedPath(ResultPath.size());
1047 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath);
1048 ResultPath = ExactSizedPath;
1049 }
1050
1051 if (!ErrorCode) {
1052 fs::UniqueID D1, D2;
1053 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1054 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1055 ASSERT_EQ(D1, D2);
1056 }
1057 ::close(FileDescriptor);
1058}
1059
Aaron Ballman0ad00462016-06-21 14:24:48 +00001060TEST_F(FileSystemTest, PathFromFDUnicode) {
1061 // Create a temp file.
1062 int FileDescriptor;
1063 SmallString<64> TempPath;
1064
1065 // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0"
1066 ASSERT_NO_ERROR(
1067 fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
1068 "\xE2\x84\xB5.0", FileDescriptor, TempPath));
1069
1070 // Make sure it exists.
1071 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1072
1073 SmallVector<char, 8> ResultPath;
1074 std::error_code ErrorCode =
1075 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1076
1077 if (!ErrorCode) {
1078 fs::UniqueID D1, D2;
1079 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1080 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1081 ASSERT_EQ(D1, D2);
1082 }
1083 ::close(FileDescriptor);
1084}
1085
Taewook Ohd9153272016-06-13 15:54:56 +00001086TEST_F(FileSystemTest, OpenFileForRead) {
1087 // Create a temp file.
1088 int FileDescriptor;
1089 SmallString<64> TempPath;
1090 ASSERT_NO_ERROR(
1091 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1092
1093 // Make sure it exists.
1094 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1095
1096 // Open the file for read
1097 int FileDescriptor2;
1098 SmallString<64> ResultPath;
1099 ASSERT_NO_ERROR(
1100 fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath))
1101
1102 // If we succeeded, check that the paths are the same (modulo case):
1103 if (!ResultPath.empty()) {
1104 // The paths returned by createTemporaryFile and getPathFromOpenFD
1105 // should reference the same file on disk.
1106 fs::UniqueID D1, D2;
1107 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1108 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1109 ASSERT_EQ(D1, D2);
1110 }
1111
1112 ::close(FileDescriptor);
1113}
Michael J. Spencer98847782010-11-24 19:20:05 +00001114} // anonymous namespace