blob: 20e087213751267f91e7c1925c5fb4ff32a3f871 [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"
Pawel Bylica7187e4b2015-10-16 09:08:59 +000011#include "llvm/Support/ConvertUTF.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000012#include "llvm/Support/Errc.h"
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000013#include "llvm/Support/ErrorHandling.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000014#include "llvm/Support/FileSystem.h"
Rafael Espindola84ab9b32013-07-19 14:41:25 +000015#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f063602011-01-06 05:57:54 +000016#include "llvm/Support/raw_ostream.h"
Michael J. Spencer98847782010-11-24 19:20:05 +000017#include "gtest/gtest.h"
18
Rafael Espindolaa813d602014-06-11 03:58:34 +000019#ifdef LLVM_ON_WIN32
NAKAMURA Takumib94f0522015-06-16 06:46:16 +000020#include <windows.h>
Rafael Espindolaa813d602014-06-11 03:58:34 +000021#include <winerror.h>
22#endif
23
Frederic Riss6b9396c2015-08-06 21:04:55 +000024#ifdef LLVM_ON_UNIX
25#include <sys/stat.h>
26#endif
27
Michael J. Spencerebad2f92010-11-29 22:28:51 +000028using namespace llvm;
Michael J. Spencer45710402010-12-03 01:21:28 +000029using namespace llvm::sys;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000030
Rafael Espindolac049c652014-06-13 03:20:08 +000031#define ASSERT_NO_ERROR(x) \
32 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
33 SmallString<128> MessageStorage; \
34 raw_svector_ostream Message(MessageStorage); \
35 Message << #x ": did not return errc::success.\n" \
36 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
37 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
38 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
39 } else { \
40 }
Michael J. Spencer3b264ba2011-01-04 17:00:18 +000041
Michael J. Spencer98847782010-11-24 19:20:05 +000042namespace {
43
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000044TEST(is_separator, Works) {
45 EXPECT_TRUE(path::is_separator('/'));
46 EXPECT_FALSE(path::is_separator('\0'));
47 EXPECT_FALSE(path::is_separator('-'));
48 EXPECT_FALSE(path::is_separator(' '));
49
50#ifdef LLVM_ON_WIN32
51 EXPECT_TRUE(path::is_separator('\\'));
52#else
53 EXPECT_FALSE(path::is_separator('\\'));
54#endif
55}
56
Michael J. Spencer3ef91c52010-11-29 22:29:04 +000057TEST(Support, Path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000058 SmallVector<StringRef, 40> paths;
59 paths.push_back("");
60 paths.push_back(".");
61 paths.push_back("..");
62 paths.push_back("foo");
63 paths.push_back("/");
64 paths.push_back("/foo");
65 paths.push_back("foo/");
66 paths.push_back("/foo/");
67 paths.push_back("foo/bar");
68 paths.push_back("/foo/bar");
69 paths.push_back("//net");
70 paths.push_back("//net/foo");
71 paths.push_back("///foo///");
72 paths.push_back("///foo///bar");
73 paths.push_back("/.");
74 paths.push_back("./");
75 paths.push_back("/..");
76 paths.push_back("../");
77 paths.push_back("foo/.");
78 paths.push_back("foo/..");
79 paths.push_back("foo/./");
80 paths.push_back("foo/./bar");
81 paths.push_back("foo/..");
82 paths.push_back("foo/../");
83 paths.push_back("foo/../bar");
84 paths.push_back("c:");
85 paths.push_back("c:/");
86 paths.push_back("c:foo");
87 paths.push_back("c:/foo");
88 paths.push_back("c:foo/");
89 paths.push_back("c:/foo/");
90 paths.push_back("c:/foo/bar");
91 paths.push_back("prn:");
92 paths.push_back("c:\\");
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/");
98 paths.push_back("c:/foo\\bar");
99
Justin Bogner0c274ae2014-07-16 08:18:58 +0000100 SmallVector<StringRef, 5> ComponentStack;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000101 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
102 e = paths.end();
103 i != e;
104 ++i) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000105 for (sys::path::const_iterator ci = sys::path::begin(*i),
106 ce = sys::path::end(*i);
107 ci != ce;
108 ++ci) {
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000109 ASSERT_FALSE(ci->empty());
Justin Bogner0c274ae2014-07-16 08:18:58 +0000110 ComponentStack.push_back(*ci);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000111 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000112
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000113 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
114 ce = sys::path::rend(*i);
115 ci != ce;
116 ++ci) {
Justin Bogner0c274ae2014-07-16 08:18:58 +0000117 ASSERT_TRUE(*ci == ComponentStack.back());
118 ComponentStack.pop_back();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000119 }
Justin Bogner0c274ae2014-07-16 08:18:58 +0000120 ASSERT_TRUE(ComponentStack.empty());
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000121
Michael J. Spencerf616b212010-12-07 17:04:04 +0000122 path::has_root_path(*i);
123 path::root_path(*i);
124 path::has_root_name(*i);
125 path::root_name(*i);
126 path::has_root_directory(*i);
127 path::root_directory(*i);
128 path::has_parent_path(*i);
129 path::parent_path(*i);
130 path::has_filename(*i);
131 path::filename(*i);
132 path::has_stem(*i);
133 path::stem(*i);
134 path::has_extension(*i);
135 path::extension(*i);
136 path::is_absolute(*i);
137 path::is_relative(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000138
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000139 SmallString<128> temp_store;
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000140 temp_store = *i;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000141 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000142 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000143 path::remove_filename(temp_store);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000144
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000145 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000146 path::replace_extension(temp_store, "ext");
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000147 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000148 stem = path::stem(filename);
149 ext = path::extension(filename);
Justin Bogner487e7642014-08-04 17:36:41 +0000150 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000151
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000152 path::native(*i, temp_store);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000153 }
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000154
155 SmallString<32> Relative("foo.cpp");
156 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
Benjamin Kramer2b4e14e2015-10-05 14:15:13 +0000157 Relative[5] = '/'; // Fix up windows paths.
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000158 ASSERT_EQ("/root/foo.cpp", Relative);
Michael J. Spencer346a1332011-01-05 16:39:05 +0000159}
Michael J. Spencer45710402010-12-03 01:21:28 +0000160
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000161TEST(Support, RelativePathIterator) {
162 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
163 typedef SmallVector<StringRef, 4> PathComponents;
164 PathComponents ExpectedPathComponents;
165 PathComponents ActualPathComponents;
166
Chandler Carruthe4405e92015-09-10 06:12:31 +0000167 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000168
169 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
170 ++I) {
171 ActualPathComponents.push_back(*I);
172 }
173
174 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
175
176 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
177 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
178 }
179}
180
Ben Langmuir2547f932015-03-10 00:04:29 +0000181TEST(Support, RelativePathDotIterator) {
182 SmallString<64> Path(StringRef(".c/.d/../."));
183 typedef SmallVector<StringRef, 4> PathComponents;
184 PathComponents ExpectedPathComponents;
185 PathComponents ActualPathComponents;
186
Chandler Carruthe4405e92015-09-10 06:12:31 +0000187 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000188
189 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
190 ++I) {
191 ActualPathComponents.push_back(*I);
192 }
193
194 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
195
196 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
197 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
198 }
199}
200
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000201TEST(Support, AbsolutePathIterator) {
202 SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
203 typedef SmallVector<StringRef, 4> PathComponents;
204 PathComponents ExpectedPathComponents;
205 PathComponents ActualPathComponents;
206
Chandler Carruthe4405e92015-09-10 06:12:31 +0000207 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000208
209 // The root path will also be a component when iterating
210 ExpectedPathComponents[0] = "/";
211
212 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
213 ++I) {
214 ActualPathComponents.push_back(*I);
215 }
216
217 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
218
219 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
220 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
221 }
222}
223
Ben Langmuir2547f932015-03-10 00:04:29 +0000224TEST(Support, AbsolutePathDotIterator) {
225 SmallString<64> Path(StringRef("/.c/.d/../."));
226 typedef SmallVector<StringRef, 4> PathComponents;
227 PathComponents ExpectedPathComponents;
228 PathComponents ActualPathComponents;
229
Chandler Carruthe4405e92015-09-10 06:12:31 +0000230 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000231
232 // The root path will also be a component when iterating
233 ExpectedPathComponents[0] = "/";
234
235 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
236 ++I) {
237 ActualPathComponents.push_back(*I);
238 }
239
240 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
241
242 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
243 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
244 }
245}
246
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000247#ifdef LLVM_ON_WIN32
248TEST(Support, AbsolutePathIteratorWin32) {
249 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
250 typedef SmallVector<StringRef, 4> PathComponents;
251 PathComponents ExpectedPathComponents;
252 PathComponents ActualPathComponents;
253
254 StringRef(Path).split(ExpectedPathComponents, "\\");
255
256 // The root path (which comes after the drive name) will also be a component
257 // when iterating.
258 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
259
260 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
261 ++I) {
262 ActualPathComponents.push_back(*I);
263 }
264
265 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
266
267 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
268 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
269 }
270}
271#endif // LLVM_ON_WIN32
272
Ben Langmuir8d116392014-03-05 19:56:30 +0000273TEST(Support, AbsolutePathIteratorEnd) {
274 // Trailing slashes are converted to '.' unless they are part of the root path.
275 SmallVector<StringRef, 4> Paths;
276 Paths.push_back("/foo/");
277 Paths.push_back("/foo//");
278 Paths.push_back("//net//");
279#ifdef LLVM_ON_WIN32
280 Paths.push_back("c:\\\\");
281#endif
282
283 for (StringRef Path : Paths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000284 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000285 EXPECT_EQ(".", LastComponent);
286 }
287
288 SmallVector<StringRef, 3> RootPaths;
289 RootPaths.push_back("/");
290 RootPaths.push_back("//net/");
291#ifdef LLVM_ON_WIN32
292 RootPaths.push_back("c:\\");
293#endif
294
295 for (StringRef Path : RootPaths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000296 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000297 EXPECT_EQ(1u, LastComponent.size());
298 EXPECT_TRUE(path::is_separator(LastComponent[0]));
299 }
300}
301
Peter Collingbournef7d41012014-01-31 23:46:06 +0000302TEST(Support, HomeDirectory) {
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000303 std::string expected;
Pawel Bylica7187e4b2015-10-16 09:08:59 +0000304#ifdef LLVM_ON_WIN32
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000305 if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) {
306 auto pathLen = ::wcslen(path);
307 ArrayRef<char> ref{reinterpret_cast<char const *>(path),
308 pathLen * sizeof(wchar_t)};
309 convertUTF16ToUTF8String(ref, expected);
310 }
Pawel Bylica7187e4b2015-10-16 09:08:59 +0000311#else
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000312 if (char const *path = ::getenv("HOME"))
313 expected = path;
Peter Collingbournef7d41012014-01-31 23:46:06 +0000314#endif
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000315 // Do not try to test it if we don't know what to expect.
316 // On Windows we use something better than env vars.
317 if (!expected.empty()) {
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000318 SmallString<128> HomeDir;
319 auto status = path::home_directory(HomeDir);
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000320 EXPECT_TRUE(status);
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000321 EXPECT_EQ(expected, HomeDir);
322 }
Peter Collingbournef7d41012014-01-31 23:46:06 +0000323}
324
Pawel Bylica0e97e5c2015-11-02 09:49:17 +0000325TEST(Support, UserCacheDirectory) {
326 SmallString<13> CacheDir;
327 SmallString<20> CacheDir2;
328 auto Status = path::user_cache_directory(CacheDir, "");
329 EXPECT_TRUE(Status ^ CacheDir.empty());
330
331 if (Status) {
332 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed
333 EXPECT_EQ(CacheDir, CacheDir2); // and return same paths
334
335 EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c"));
336 auto It = path::rbegin(CacheDir);
337 EXPECT_EQ("file.c", *It);
338 EXPECT_EQ("B", *++It);
339 EXPECT_EQ("A", *++It);
340 auto ParentDir = *++It;
341
342 // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0"
343 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2",
344 "\xE2\x84\xB5.0"));
345 auto It2 = path::rbegin(CacheDir2);
346 EXPECT_EQ("\xE2\x84\xB5.0", *It2);
347 EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2);
348 auto ParentDir2 = *++It2;
349
350 EXPECT_EQ(ParentDir, ParentDir2);
351 }
352}
353
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000354TEST(Support, TempDirectory) {
355 SmallString<32> TempDir;
356 path::system_temp_directory(false, TempDir);
357 EXPECT_TRUE(!TempDir.empty());
358 TempDir.clear();
359 path::system_temp_directory(true, TempDir);
360 EXPECT_TRUE(!TempDir.empty());
361}
362
David Blaikie06d56182015-11-17 20:38:54 +0000363#ifdef LLVM_ON_WIN32
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000364static std::string path2regex(std::string Path) {
365 size_t Pos = 0;
366 while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
367 Path.replace(Pos, 1, "\\\\");
368 Pos += 2;
369 }
370 return Path;
371}
372
373/// Helper for running temp dir test in separated process. See below.
374#define EXPECT_TEMP_DIR(prepare, expected) \
375 EXPECT_EXIT( \
376 { \
377 prepare; \
378 SmallString<300> TempDir; \
379 path::system_temp_directory(true, TempDir); \
380 raw_os_ostream(std::cerr) << TempDir; \
381 std::exit(0); \
382 }, \
383 ::testing::ExitedWithCode(0), path2regex(expected))
384
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000385TEST(SupportDeathTest, TempDirectoryOnWindows) {
386 // In this test we want to check how system_temp_directory responds to
387 // different values of specific env vars. To prevent corrupting env vars of
388 // the current process all checks are done in separated processes.
389 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder");
390 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"),
391 "C:\\Unix\\Path\\Seperators");
392 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$");
393 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep");
394 EXPECT_TEMP_DIR(
395 _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
396 "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
397
398 // Test $TMP empty, $TEMP set.
399 EXPECT_TEMP_DIR(
400 {
401 _wputenv_s(L"TMP", L"");
402 _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
403 },
404 "C:\\Valid\\Path");
405
406 // All related env vars empty
407 EXPECT_TEMP_DIR(
408 {
409 _wputenv_s(L"TMP", L"");
410 _wputenv_s(L"TEMP", L"");
411 _wputenv_s(L"USERPROFILE", L"");
412 },
413 "C:\\Temp");
414
415 // Test evn var / path with 260 chars.
416 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
417 while (Expected.size() < 260)
418 Expected.append("\\DirNameWith19Charss");
Reid Kleckner88e9ff62016-02-10 19:29:01 +0000419 ASSERT_EQ(260U, Expected.size());
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000420 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
421}
422#endif
423
Michael J. Spencer346a1332011-01-05 16:39:05 +0000424class FileSystemTest : public testing::Test {
425protected:
426 /// Unique temporary directory in which all created filesystem entities must
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000427 /// be placed. It is removed at the end of each test (must be empty).
Michael J. Spencer346a1332011-01-05 16:39:05 +0000428 SmallString<128> TestDirectory;
429
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000430 void SetUp() override {
Michael J. Spencer346a1332011-01-05 16:39:05 +0000431 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000432 fs::createUniqueDirectory("file-system-test", TestDirectory));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000433 // We don't care about this specific file.
Michael J. Spencer42368cc2011-01-05 16:39:46 +0000434 errs() << "Test Directory: " << TestDirectory << '\n';
435 errs().flush();
Michael J. Spencer346a1332011-01-05 16:39:05 +0000436 }
437
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000438 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000439};
440
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000441TEST_F(FileSystemTest, Unique) {
442 // Create a temp file.
443 int FileDescriptor;
444 SmallString<64> TempPath;
445 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000446 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000447
448 // The same file should return an identical unique id.
Rafael Espindola7f822a92013-07-29 21:26:49 +0000449 fs::UniqueID F1, F2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000450 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
451 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000452 ASSERT_EQ(F1, F2);
453
454 // Different files should return different unique ids.
455 int FileDescriptor2;
456 SmallString<64> TempPath2;
457 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000458 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
459
Rafael Espindola7f822a92013-07-29 21:26:49 +0000460 fs::UniqueID D;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000461 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000462 ASSERT_NE(D, F1);
463 ::close(FileDescriptor2);
464
465 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
466
467 // Two paths representing the same file on disk should still provide the
468 // same unique id. We can test this by making a hard link.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000469 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Rafael Espindola7f822a92013-07-29 21:26:49 +0000470 fs::UniqueID D2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000471 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000472 ASSERT_EQ(D2, F1);
473
474 ::close(FileDescriptor);
Rafael Espindolaa5932af2013-07-30 20:25:53 +0000475
476 SmallString<128> Dir1;
477 ASSERT_NO_ERROR(
478 fs::createUniqueDirectory("dir1", Dir1));
479 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
480 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
481 ASSERT_EQ(F1, F2);
482
483 SmallString<128> Dir2;
484 ASSERT_NO_ERROR(
485 fs::createUniqueDirectory("dir2", Dir2));
486 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
487 ASSERT_NE(F1, F2);
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000488}
489
Michael J. Spencer346a1332011-01-05 16:39:05 +0000490TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000491 // Create a temp file.
Michael J. Spencer45710402010-12-03 01:21:28 +0000492 int FileDescriptor;
493 SmallString<64> TempPath;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000494 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000495 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Michael J. Spencer45710402010-12-03 01:21:28 +0000496
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000497 // Make sure it exists.
Rafael Espindolac1599152014-09-11 19:11:02 +0000498 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000499
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000500 // Create another temp tile.
501 int FD2;
502 SmallString<64> TempPath2;
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000503 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
Rafael Espindolad3c89042013-07-25 15:00:17 +0000504 ASSERT_TRUE(TempPath2.endswith(".temp"));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000505 ASSERT_NE(TempPath.str(), TempPath2.str());
506
Michael J. Spencer203d7802011-12-12 06:04:28 +0000507 fs::file_status A, B;
508 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
509 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
510 EXPECT_FALSE(fs::equivalent(A, B));
511
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000512 ::close(FD2);
Rafael Espindola213c4cb2013-07-18 03:29:51 +0000513
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000514 // Remove Temp2.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000515 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
516 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
517 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000518 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000519
Rafael Espindolac049c652014-06-13 03:20:08 +0000520 std::error_code EC = fs::status(TempPath2.c_str(), B);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000521 EXPECT_EQ(EC, errc::no_such_file_or_directory);
Rafael Espindola107b74c2013-07-31 00:10:25 +0000522 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
523
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000524 // Make sure Temp2 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000525 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
526 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000527
Rafael Espindolad3c89042013-07-25 15:00:17 +0000528 SmallString<64> TempPath3;
529 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
530 ASSERT_FALSE(TempPath3.endswith("."));
531
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000532 // Create a hard link to Temp1.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000533 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000534 bool equal;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000535 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000536 EXPECT_TRUE(equal);
Michael J. Spencer203d7802011-12-12 06:04:28 +0000537 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
538 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
539 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000540
541 // Remove Temp1.
Michael J. Spencer45710402010-12-03 01:21:28 +0000542 ::close(FileDescriptor);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000543 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000544
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000545 // Remove the hard link.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000546 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000547
548 // Make sure Temp1 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000549 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
550 errc::no_such_file_or_directory);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000551
552#ifdef LLVM_ON_WIN32
553 // Path name > 260 chars should get an error.
554 const char *Path270 =
555 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
556 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
557 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
558 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
559 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000560 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000561 errc::invalid_argument);
562 // Relative path < 247 chars, no problem.
563 const char *Path216 =
564 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
565 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
566 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
567 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000568 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000569 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000570#endif
Michael J. Spencer346a1332011-01-05 16:39:05 +0000571}
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000572
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000573TEST_F(FileSystemTest, CreateDir) {
574 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
575 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
576 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000577 errc::file_exists);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000578 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000579
Frederic Riss6b9396c2015-08-06 21:04:55 +0000580#ifdef LLVM_ON_UNIX
581 // Set a 0000 umask so that we can test our directory permissions.
582 mode_t OldUmask = ::umask(0000);
583
584 fs::file_status Status;
585 ASSERT_NO_ERROR(
586 fs::create_directory(Twine(TestDirectory) + "baz500", false,
587 fs::perms::owner_read | fs::perms::owner_exe));
588 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
589 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
590 fs::perms::owner_read | fs::perms::owner_exe);
591 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
592 fs::perms::all_all));
593 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
594 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
595
596 // Restore umask to be safe.
597 ::umask(OldUmask);
598#endif
599
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000600#ifdef LLVM_ON_WIN32
601 // Prove that create_directories() can handle a pathname > 248 characters,
602 // which is the documented limit for CreateDirectory().
603 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
604 // Generate a directory path guaranteed to fall into that range.
605 size_t TmpLen = TestDirectory.size();
606 const char *OneDir = "\\123456789";
607 size_t OneDirLen = strlen(OneDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000608 ASSERT_LT(OneDirLen, 12U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000609 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
610 SmallString<260> LongDir(TestDirectory);
611 for (size_t I = 0; I < NLevels; ++I)
612 LongDir.append(OneDir);
613 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
614 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
615 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
616 errc::file_exists);
617 // Tidy up, "recursively" removing the directories.
618 StringRef ThisDir(LongDir);
619 for (size_t J = 0; J < NLevels; ++J) {
620 ASSERT_NO_ERROR(fs::remove(ThisDir));
621 ThisDir = path::parent_path(ThisDir);
622 }
623
624 // Similarly for a relative pathname. Need to set the current directory to
625 // TestDirectory so that the one we create ends up in the right place.
626 char PreviousDir[260];
627 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000628 ASSERT_GT(PreviousDirLen, 0U);
629 ASSERT_LT(PreviousDirLen, 260U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000630 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
631 LongDir.clear();
632 // Generate a relative directory name with absolute length > 248.
633 size_t LongDirLen = 249 - TestDirectory.size();
634 LongDir.assign(LongDirLen, 'a');
635 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
636 // While we're here, prove that .. and . handling works in these long paths.
637 const char *DotDotDirs = "\\..\\.\\b";
638 LongDir.append(DotDotDirs);
Paul Robinson1b6c7342014-11-13 00:36:34 +0000639 ASSERT_NO_ERROR(fs::create_directory("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000640 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
641 // And clean up.
Paul Robinson1b6c7342014-11-13 00:36:34 +0000642 ASSERT_NO_ERROR(fs::remove("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000643 ASSERT_NO_ERROR(fs::remove(
644 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
645 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
646#endif
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000647}
648
Michael J. Spencer346a1332011-01-05 16:39:05 +0000649TEST_F(FileSystemTest, DirectoryIteration) {
Rafael Espindolac049c652014-06-13 03:20:08 +0000650 std::error_code ec;
Michael J. Spencer1f063602011-01-06 05:57:54 +0000651 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
652 ASSERT_NO_ERROR(ec);
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000653
654 // Create a known hierarchy to recurse over.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000655 ASSERT_NO_ERROR(
656 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
657 ASSERT_NO_ERROR(
658 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
659 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
660 "/recursive/dontlookhere/da1"));
661 ASSERT_NO_ERROR(
662 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
663 ASSERT_NO_ERROR(
664 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000665 typedef std::vector<std::string> v_t;
666 v_t visited;
667 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
668 + "/recursive", ec), e; i != e; i.increment(ec)){
669 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000670 if (path::filename(i->path()) == "p1") {
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000671 i.pop();
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000672 // FIXME: recursive_directory_iterator should be more robust.
673 if (i == e) break;
674 }
Michael J. Spencer9b54a3e2011-12-09 01:14:41 +0000675 if (path::filename(i->path()) == "dontlookhere")
676 i.no_push();
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000677 visited.push_back(path::filename(i->path()));
678 }
679 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
680 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
681 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
682 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
683 "dontlookhere");
684 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
685 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
686 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
687 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
688 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
689
690 // Make sure that each path was visited correctly.
691 ASSERT_NE(a0, visited.end());
692 ASSERT_NE(aa1, visited.end());
693 ASSERT_NE(ab1, visited.end());
694 ASSERT_NE(dontlookhere, visited.end());
695 ASSERT_EQ(da1, visited.end()); // Not visited.
696 ASSERT_NE(z0, visited.end());
697 ASSERT_NE(za1, visited.end());
698 ASSERT_NE(pop, visited.end());
699 ASSERT_EQ(p1, visited.end()); // Not visited.
700
701 // Make sure that parents were visited before children. No other ordering
702 // guarantees can be made across siblings.
703 ASSERT_LT(a0, aa1);
704 ASSERT_LT(a0, ab1);
705 ASSERT_LT(z0, za1);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000706
707 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
708 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
709 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
710 ASSERT_NO_ERROR(
711 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
712 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
713 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
714 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
715 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
716 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
717 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
Michael J. Spencer1f063602011-01-06 05:57:54 +0000718}
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000719
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000720const char archive[] = "!<arch>\x0A";
721const char bitcode[] = "\xde\xc0\x17\x0b";
Rui Ueyama829c4392013-11-14 22:09:08 +0000722const char coff_object[] = "\x00\x00......";
Rui Ueyama5c69ff52014-09-11 22:34:32 +0000723const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
Rui Ueyama2acb0582014-09-11 21:09:57 +0000724 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000725const char coff_import_library[] = "\x00\x00\xff\xff....";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000726const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
727 0, 0, 0, 0, 0, 0, 0, 0, 1 };
Etienne Bergeron1562f692016-04-05 01:46:26 +0000728const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
Kevin Enderby87c85b72016-01-26 23:43:37 +0000729const char macho_object[] =
730 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
731const char macho_executable[] =
732 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000733const char macho_fixed_virtual_memory_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000734 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
735const char macho_core[] =
736 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
737const char macho_preload_executable[] =
738 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000739const char macho_dynamically_linked_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000740 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
741const char macho_dynamic_linker[] =
742 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
743const char macho_bundle[] =
744 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
745const char macho_dsym_companion[] =
746 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
747const char macho_kext_bundle[] =
748 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000749const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000750const char macho_dynamically_linked_shared_lib_stub[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000751 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000752
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000753TEST_F(FileSystemTest, Magic) {
754 struct type {
755 const char *filename;
756 const char *magic_str;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000757 size_t magic_str_len;
758 fs::file_magic magic;
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000759 } types[] = {
760#define DEFINE(magic) \
761 { #magic, magic, sizeof(magic), fs::file_magic::magic }
762 DEFINE(archive),
763 DEFINE(bitcode),
Rui Ueyama829c4392013-11-14 22:09:08 +0000764 DEFINE(coff_object),
Rui Ueyama2acb0582014-09-11 21:09:57 +0000765 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000766 DEFINE(coff_import_library),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000767 DEFINE(elf_relocatable),
768 DEFINE(macho_universal_binary),
769 DEFINE(macho_object),
770 DEFINE(macho_executable),
771 DEFINE(macho_fixed_virtual_memory_shared_lib),
772 DEFINE(macho_core),
773 DEFINE(macho_preload_executable),
774 DEFINE(macho_dynamically_linked_shared_lib),
775 DEFINE(macho_dynamic_linker),
776 DEFINE(macho_bundle),
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000777 DEFINE(macho_dynamically_linked_shared_lib_stub),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000778 DEFINE(macho_dsym_companion),
Justin Bognera7ad4b32015-02-25 22:59:20 +0000779 DEFINE(macho_kext_bundle),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000780 DEFINE(windows_resource)
781#undef DEFINE
782 };
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000783
784 // Create some files filled with magic.
785 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
786 ++i) {
787 SmallString<128> file_pathname(TestDirectory);
788 path::append(file_pathname, i->filename);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000789 std::error_code EC;
790 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000791 ASSERT_FALSE(file.has_error());
792 StringRef magic(i->magic_str, i->magic_str_len);
793 file << magic;
Michael J. Spencerb5871802011-01-15 18:52:49 +0000794 file.close();
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000795 EXPECT_EQ(i->magic, fs::identify_magic(magic));
Rafael Espindola78dcc032014-01-10 20:36:42 +0000796 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000797 }
Michael J. Spencer98847782010-11-24 19:20:05 +0000798}
799
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000800#ifdef LLVM_ON_WIN32
801TEST_F(FileSystemTest, CarriageReturn) {
802 SmallString<128> FilePathname(TestDirectory);
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000803 std::error_code EC;
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000804 path::append(FilePathname, "test");
805
806 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000807 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
808 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000809 File << '\n';
810 }
811 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000812 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000813 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000814 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000815 }
816
817 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000818 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
819 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000820 File << '\n';
821 }
822 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000823 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000824 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000825 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000826 }
Rafael Espindola78dcc032014-01-10 20:36:42 +0000827 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000828}
829#endif
830
Rafael Espindola59aaa6c2014-12-12 17:55:12 +0000831TEST_F(FileSystemTest, Resize) {
832 int FD;
833 SmallString<64> TempPath;
834 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
835 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
836 fs::file_status Status;
837 ASSERT_NO_ERROR(fs::status(FD, Status));
838 ASSERT_EQ(Status.getSize(), 123U);
839}
840
Nick Kledzik18497e92012-06-20 00:28:54 +0000841TEST_F(FileSystemTest, FileMapping) {
842 // Create a temp file.
843 int FileDescriptor;
844 SmallString<64> TempPath;
845 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000846 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000847 unsigned Size = 4096;
848 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
849
Nick Kledzik18497e92012-06-20 00:28:54 +0000850 // Map in temp file and add some content
Rafael Espindolac049c652014-06-13 03:20:08 +0000851 std::error_code EC;
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000852 StringRef Val("hello there");
853 {
854 fs::mapped_file_region mfr(FileDescriptor,
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000855 fs::mapped_file_region::readwrite, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000856 ASSERT_NO_ERROR(EC);
857 std::copy(Val.begin(), Val.end(), mfr.data());
858 // Explicitly add a 0.
859 mfr.data()[Val.size()] = 0;
860 // Unmap temp file
861 }
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000862
Nick Kledzik18497e92012-06-20 00:28:54 +0000863 // Map it back in read-only
Rafael Espindola71bc5072014-12-11 17:17:26 +0000864 int FD;
865 EC = fs::openFileForRead(Twine(TempPath), FD);
866 ASSERT_NO_ERROR(EC);
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000867 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000868 ASSERT_NO_ERROR(EC);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000869
Nick Kledzik18497e92012-06-20 00:28:54 +0000870 // Verify content
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000871 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000872
Nick Kledzik18497e92012-06-20 00:28:54 +0000873 // Unmap temp file
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000874 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000875 ASSERT_NO_ERROR(EC);
Rafael Espindola71bc5072014-12-11 17:17:26 +0000876 ASSERT_EQ(close(FD), 0);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000877}
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000878
879TEST(Support, NormalizePath) {
880#if defined(LLVM_ON_WIN32)
881#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
882 EXPECT_EQ(path__, windows__);
883#else
884#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
885 EXPECT_EQ(path__, not_windows__);
886#endif
887
888 SmallString<64> Path1("a");
889 SmallString<64> Path2("a/b");
890 SmallString<64> Path3("a\\b");
891 SmallString<64> Path4("a\\\\b");
892 SmallString<64> Path5("\\a");
893 SmallString<64> Path6("a\\");
894
Rafael Espindola7e774c22014-08-08 21:35:52 +0000895 path::native(Path1);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000896 EXPECT_PATH_IS(Path1, "a", "a");
897
Rafael Espindola7e774c22014-08-08 21:35:52 +0000898 path::native(Path2);
Rafael Espindolaffbabb72014-08-09 00:37:05 +0000899 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000900
Rafael Espindola7e774c22014-08-08 21:35:52 +0000901 path::native(Path3);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000902 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
903
Rafael Espindola7e774c22014-08-08 21:35:52 +0000904 path::native(Path4);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000905 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
906
Rafael Espindola7e774c22014-08-08 21:35:52 +0000907 path::native(Path5);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000908 EXPECT_PATH_IS(Path5, "\\a", "/a");
909
Rafael Espindola7e774c22014-08-08 21:35:52 +0000910 path::native(Path6);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000911 EXPECT_PATH_IS(Path6, "a\\", "a/");
912
913#undef EXPECT_PATH_IS
914}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000915
916TEST(Support, RemoveLeadingDotSlash) {
917 StringRef Path1("././/foolz/wat");
918 StringRef Path2("./////");
919
920 Path1 = path::remove_leading_dotslash(Path1);
921 EXPECT_EQ(Path1, "foolz/wat");
922 Path2 = path::remove_leading_dotslash(Path2);
923 EXPECT_EQ(Path2, "");
924}
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000925
926static std::string remove_dots(StringRef path,
927 bool remove_dot_dot) {
928 SmallString<256> buffer(path);
929 path::remove_dots(buffer, remove_dot_dot);
930 return buffer.str();
931}
932
933TEST(Support, RemoveDots) {
Mike Aizatskyf8cf7132015-11-09 19:36:53 +0000934#if defined(LLVM_ON_WIN32)
935 EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false));
936 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false));
937
938 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false));
939 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true));
940 EXPECT_EQ("c", remove_dots(".\\.\\c", true));
941
942 SmallString<64> Path1(".\\.\\c");
943 EXPECT_TRUE(path::remove_dots(Path1, true));
944 EXPECT_EQ("c", Path1);
945#else
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000946 EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false));
947 EXPECT_EQ("", remove_dots("./////", false));
948
949 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false));
950 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true));
951 EXPECT_EQ("c", remove_dots("././c", true));
952
953 SmallString<64> Path1("././c");
954 EXPECT_TRUE(path::remove_dots(Path1, true));
955 EXPECT_EQ("c", Path1);
Mike Aizatskyf8cf7132015-11-09 19:36:53 +0000956#endif
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000957}
Michael J. Spencer98847782010-11-24 19:20:05 +0000958} // anonymous namespace