1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Path.h"
11 #include "llvm/Support/Errc.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "gtest/gtest.h"
28 using namespace llvm::sys;
30 #define ASSERT_NO_ERROR(x) \
31 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
32 SmallString<128> MessageStorage; \
33 raw_svector_ostream Message(MessageStorage); \
34 Message << #x ": did not return errc::success.\n" \
35 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
36 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
37 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
43 TEST(is_separator, Works) {
44 EXPECT_TRUE(path::is_separator('/'));
45 EXPECT_FALSE(path::is_separator('\0'));
46 EXPECT_FALSE(path::is_separator('-'));
47 EXPECT_FALSE(path::is_separator(' '));
50 EXPECT_TRUE(path::is_separator('\\'));
52 EXPECT_FALSE(path::is_separator('\\'));
57 SmallVector<StringRef, 40> paths;
60 paths.push_back("..");
61 paths.push_back("foo");
63 paths.push_back("/foo");
64 paths.push_back("foo/");
65 paths.push_back("/foo/");
66 paths.push_back("foo/bar");
67 paths.push_back("/foo/bar");
68 paths.push_back("//net");
69 paths.push_back("//net/foo");
70 paths.push_back("///foo///");
71 paths.push_back("///foo///bar");
72 paths.push_back("/.");
73 paths.push_back("./");
74 paths.push_back("/..");
75 paths.push_back("../");
76 paths.push_back("foo/.");
77 paths.push_back("foo/..");
78 paths.push_back("foo/./");
79 paths.push_back("foo/./bar");
80 paths.push_back("foo/..");
81 paths.push_back("foo/../");
82 paths.push_back("foo/../bar");
83 paths.push_back("c:");
84 paths.push_back("c:/");
85 paths.push_back("c:foo");
86 paths.push_back("c:/foo");
87 paths.push_back("c:foo/");
88 paths.push_back("c:/foo/");
89 paths.push_back("c:/foo/bar");
90 paths.push_back("prn:");
91 paths.push_back("c:\\");
92 paths.push_back("c:foo");
93 paths.push_back("c:\\foo");
94 paths.push_back("c:foo\\");
95 paths.push_back("c:\\foo\\");
96 paths.push_back("c:\\foo/");
97 paths.push_back("c:/foo\\bar");
99 SmallVector<StringRef, 5> ComponentStack;
100 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
104 for (sys::path::const_iterator ci = sys::path::begin(*i),
105 ce = sys::path::end(*i);
108 ASSERT_FALSE(ci->empty());
109 ComponentStack.push_back(*ci);
112 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
113 ce = sys::path::rend(*i);
116 ASSERT_TRUE(*ci == ComponentStack.back());
117 ComponentStack.pop_back();
119 ASSERT_TRUE(ComponentStack.empty());
121 path::has_root_path(*i);
123 path::has_root_name(*i);
125 path::has_root_directory(*i);
126 path::root_directory(*i);
127 path::has_parent_path(*i);
128 path::parent_path(*i);
129 path::has_filename(*i);
133 path::has_extension(*i);
135 path::is_absolute(*i);
136 path::is_relative(*i);
138 SmallString<128> temp_store;
140 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
142 path::remove_filename(temp_store);
145 path::replace_extension(temp_store, "ext");
146 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
147 stem = path::stem(filename);
148 ext = path::extension(filename);
149 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
151 path::native(*i, temp_store);
154 SmallString<32> Relative("foo.cpp");
155 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
156 Relative[5] = '/'; // Fix up windows paths.
157 ASSERT_EQ("/root/foo.cpp", Relative);
160 TEST(Support, RelativePathIterator) {
161 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
162 typedef SmallVector<StringRef, 4> PathComponents;
163 PathComponents ExpectedPathComponents;
164 PathComponents ActualPathComponents;
166 StringRef(Path).split(ExpectedPathComponents, '/');
168 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
170 ActualPathComponents.push_back(*I);
173 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
175 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
176 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
180 TEST(Support, RelativePathDotIterator) {
181 SmallString<64> Path(StringRef(".c/.d/../."));
182 typedef SmallVector<StringRef, 4> PathComponents;
183 PathComponents ExpectedPathComponents;
184 PathComponents ActualPathComponents;
186 StringRef(Path).split(ExpectedPathComponents, '/');
188 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
190 ActualPathComponents.push_back(*I);
193 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
195 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
196 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
200 TEST(Support, AbsolutePathIterator) {
201 SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
202 typedef SmallVector<StringRef, 4> PathComponents;
203 PathComponents ExpectedPathComponents;
204 PathComponents ActualPathComponents;
206 StringRef(Path).split(ExpectedPathComponents, '/');
208 // The root path will also be a component when iterating
209 ExpectedPathComponents[0] = "/";
211 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
213 ActualPathComponents.push_back(*I);
216 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
218 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
219 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
223 TEST(Support, AbsolutePathDotIterator) {
224 SmallString<64> Path(StringRef("/.c/.d/../."));
225 typedef SmallVector<StringRef, 4> PathComponents;
226 PathComponents ExpectedPathComponents;
227 PathComponents ActualPathComponents;
229 StringRef(Path).split(ExpectedPathComponents, '/');
231 // The root path will also be a component when iterating
232 ExpectedPathComponents[0] = "/";
234 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
236 ActualPathComponents.push_back(*I);
239 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
241 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
242 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
247 TEST(Support, AbsolutePathIteratorWin32) {
248 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
249 typedef SmallVector<StringRef, 4> PathComponents;
250 PathComponents ExpectedPathComponents;
251 PathComponents ActualPathComponents;
253 StringRef(Path).split(ExpectedPathComponents, "\\");
255 // The root path (which comes after the drive name) will also be a component
257 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
259 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
261 ActualPathComponents.push_back(*I);
264 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
266 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
267 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
270 #endif // LLVM_ON_WIN32
272 TEST(Support, AbsolutePathIteratorEnd) {
273 // Trailing slashes are converted to '.' unless they are part of the root path.
274 SmallVector<StringRef, 4> Paths;
275 Paths.push_back("/foo/");
276 Paths.push_back("/foo//");
277 Paths.push_back("//net//");
279 Paths.push_back("c:\\\\");
282 for (StringRef Path : Paths) {
283 StringRef LastComponent = *path::rbegin(Path);
284 EXPECT_EQ(".", LastComponent);
287 SmallVector<StringRef, 3> RootPaths;
288 RootPaths.push_back("/");
289 RootPaths.push_back("//net/");
291 RootPaths.push_back("c:\\");
294 for (StringRef Path : RootPaths) {
295 StringRef LastComponent = *path::rbegin(Path);
296 EXPECT_EQ(1u, LastComponent.size());
297 EXPECT_TRUE(path::is_separator(LastComponent[0]));
301 TEST(Support, HomeDirectory) {
303 // This test only makes sense on Unix if $HOME is set.
304 if (::getenv("HOME")) {
306 SmallString<128> HomeDir;
307 EXPECT_TRUE(path::home_directory(HomeDir));
308 EXPECT_FALSE(HomeDir.empty());
314 class FileSystemTest : public testing::Test {
316 /// Unique temporary directory in which all created filesystem entities must
317 /// be placed. It is removed at the end of each test (must be empty).
318 SmallString<128> TestDirectory;
320 void SetUp() override {
322 fs::createUniqueDirectory("file-system-test", TestDirectory));
323 // We don't care about this specific file.
324 errs() << "Test Directory: " << TestDirectory << '\n';
328 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
331 TEST_F(FileSystemTest, Unique) {
332 // Create a temp file.
334 SmallString<64> TempPath;
336 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
338 // The same file should return an identical unique id.
340 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
341 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
344 // Different files should return different unique ids.
346 SmallString<64> TempPath2;
348 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
351 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
353 ::close(FileDescriptor2);
355 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
357 // Two paths representing the same file on disk should still provide the
358 // same unique id. We can test this by making a hard link.
359 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
361 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
364 ::close(FileDescriptor);
366 SmallString<128> Dir1;
368 fs::createUniqueDirectory("dir1", Dir1));
369 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
370 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
373 SmallString<128> Dir2;
375 fs::createUniqueDirectory("dir2", Dir2));
376 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
380 TEST_F(FileSystemTest, TempFiles) {
381 // Create a temp file.
383 SmallString<64> TempPath;
385 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
387 // Make sure it exists.
388 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
390 // Create another temp tile.
392 SmallString<64> TempPath2;
393 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
394 ASSERT_TRUE(TempPath2.endswith(".temp"));
395 ASSERT_NE(TempPath.str(), TempPath2.str());
397 fs::file_status A, B;
398 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
399 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
400 EXPECT_FALSE(fs::equivalent(A, B));
405 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
406 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
407 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
408 errc::no_such_file_or_directory);
410 std::error_code EC = fs::status(TempPath2.c_str(), B);
411 EXPECT_EQ(EC, errc::no_such_file_or_directory);
412 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
414 // Make sure Temp2 doesn't exist.
415 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
416 errc::no_such_file_or_directory);
418 SmallString<64> TempPath3;
419 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
420 ASSERT_FALSE(TempPath3.endswith("."));
422 // Create a hard link to Temp1.
423 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
425 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
427 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
428 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
429 EXPECT_TRUE(fs::equivalent(A, B));
432 ::close(FileDescriptor);
433 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
435 // Remove the hard link.
436 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
438 // Make sure Temp1 doesn't exist.
439 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
440 errc::no_such_file_or_directory);
443 // Path name > 260 chars should get an error.
444 const char *Path270 =
445 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
446 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
447 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
448 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
449 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
450 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
451 errc::invalid_argument);
452 // Relative path < 247 chars, no problem.
453 const char *Path216 =
454 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
455 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
456 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
457 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
458 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
459 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
463 TEST_F(FileSystemTest, CreateDir) {
464 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
465 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
466 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
468 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
471 // Set a 0000 umask so that we can test our directory permissions.
472 mode_t OldUmask = ::umask(0000);
474 fs::file_status Status;
476 fs::create_directory(Twine(TestDirectory) + "baz500", false,
477 fs::perms::owner_read | fs::perms::owner_exe));
478 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
479 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
480 fs::perms::owner_read | fs::perms::owner_exe);
481 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
482 fs::perms::all_all));
483 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
484 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
486 // Restore umask to be safe.
491 // Prove that create_directories() can handle a pathname > 248 characters,
492 // which is the documented limit for CreateDirectory().
493 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
494 // Generate a directory path guaranteed to fall into that range.
495 size_t TmpLen = TestDirectory.size();
496 const char *OneDir = "\\123456789";
497 size_t OneDirLen = strlen(OneDir);
498 ASSERT_LT(OneDirLen, 12U);
499 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
500 SmallString<260> LongDir(TestDirectory);
501 for (size_t I = 0; I < NLevels; ++I)
502 LongDir.append(OneDir);
503 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
504 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
505 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
507 // Tidy up, "recursively" removing the directories.
508 StringRef ThisDir(LongDir);
509 for (size_t J = 0; J < NLevels; ++J) {
510 ASSERT_NO_ERROR(fs::remove(ThisDir));
511 ThisDir = path::parent_path(ThisDir);
514 // Similarly for a relative pathname. Need to set the current directory to
515 // TestDirectory so that the one we create ends up in the right place.
516 char PreviousDir[260];
517 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
518 ASSERT_GT(PreviousDirLen, 0U);
519 ASSERT_LT(PreviousDirLen, 260U);
520 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
522 // Generate a relative directory name with absolute length > 248.
523 size_t LongDirLen = 249 - TestDirectory.size();
524 LongDir.assign(LongDirLen, 'a');
525 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
526 // While we're here, prove that .. and . handling works in these long paths.
527 const char *DotDotDirs = "\\..\\.\\b";
528 LongDir.append(DotDotDirs);
529 ASSERT_NO_ERROR(fs::create_directory("b"));
530 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
532 ASSERT_NO_ERROR(fs::remove("b"));
533 ASSERT_NO_ERROR(fs::remove(
534 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
535 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
539 TEST_F(FileSystemTest, DirectoryIteration) {
541 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
544 // Create a known hierarchy to recurse over.
546 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
548 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
549 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
550 "/recursive/dontlookhere/da1"));
552 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
554 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
555 typedef std::vector<std::string> v_t;
557 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
558 + "/recursive", ec), e; i != e; i.increment(ec)){
560 if (path::filename(i->path()) == "p1") {
562 // FIXME: recursive_directory_iterator should be more robust.
565 if (path::filename(i->path()) == "dontlookhere")
567 visited.push_back(path::filename(i->path()));
569 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
570 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
571 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
572 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
574 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
575 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
576 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
577 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
578 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
580 // Make sure that each path was visited correctly.
581 ASSERT_NE(a0, visited.end());
582 ASSERT_NE(aa1, visited.end());
583 ASSERT_NE(ab1, visited.end());
584 ASSERT_NE(dontlookhere, visited.end());
585 ASSERT_EQ(da1, visited.end()); // Not visited.
586 ASSERT_NE(z0, visited.end());
587 ASSERT_NE(za1, visited.end());
588 ASSERT_NE(pop, visited.end());
589 ASSERT_EQ(p1, visited.end()); // Not visited.
591 // Make sure that parents were visited before children. No other ordering
592 // guarantees can be made across siblings.
597 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
598 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
599 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
601 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
602 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
603 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
604 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
605 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
606 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
607 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
610 const char archive[] = "!<arch>\x0A";
611 const char bitcode[] = "\xde\xc0\x17\x0b";
612 const char coff_object[] = "\x00\x00......";
613 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
614 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
615 const char coff_import_library[] = "\x00\x00\xff\xff....";
616 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
617 0, 0, 0, 0, 0, 0, 0, 0, 1 };
618 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00";
619 const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01";
620 const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02";
621 const char macho_fixed_virtual_memory_shared_lib[] =
622 "\xfe\xed\xfa\xce..........\x00\x03";
623 const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04";
624 const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05";
625 const char macho_dynamically_linked_shared_lib[] =
626 "\xfe\xed\xfa\xce..........\x00\x06";
627 const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07";
628 const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08";
629 const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a";
630 const char macho_kext_bundle[] = "\xfe\xed\xfa\xce..........\x00\x0b";
631 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
632 const char macho_dynamically_linked_shared_lib_stub[] =
633 "\xfe\xed\xfa\xce..........\x00\x09";
635 TEST_F(FileSystemTest, Magic) {
637 const char *filename;
638 const char *magic_str;
639 size_t magic_str_len;
640 fs::file_magic magic;
642 #define DEFINE(magic) \
643 { #magic, magic, sizeof(magic), fs::file_magic::magic }
647 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
648 DEFINE(coff_import_library),
649 DEFINE(elf_relocatable),
650 DEFINE(macho_universal_binary),
651 DEFINE(macho_object),
652 DEFINE(macho_executable),
653 DEFINE(macho_fixed_virtual_memory_shared_lib),
655 DEFINE(macho_preload_executable),
656 DEFINE(macho_dynamically_linked_shared_lib),
657 DEFINE(macho_dynamic_linker),
658 DEFINE(macho_bundle),
659 DEFINE(macho_dynamically_linked_shared_lib_stub),
660 DEFINE(macho_dsym_companion),
661 DEFINE(macho_kext_bundle),
662 DEFINE(windows_resource)
666 // Create some files filled with magic.
667 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
669 SmallString<128> file_pathname(TestDirectory);
670 path::append(file_pathname, i->filename);
672 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
673 ASSERT_FALSE(file.has_error());
674 StringRef magic(i->magic_str, i->magic_str_len);
677 EXPECT_EQ(i->magic, fs::identify_magic(magic));
678 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
683 TEST_F(FileSystemTest, CarriageReturn) {
684 SmallString<128> FilePathname(TestDirectory);
686 path::append(FilePathname, "test");
689 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
694 auto Buf = MemoryBuffer::getFile(FilePathname.str());
695 EXPECT_TRUE((bool)Buf);
696 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
700 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
705 auto Buf = MemoryBuffer::getFile(FilePathname.str());
706 EXPECT_TRUE((bool)Buf);
707 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
709 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
713 TEST_F(FileSystemTest, Resize) {
715 SmallString<64> TempPath;
716 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
717 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
718 fs::file_status Status;
719 ASSERT_NO_ERROR(fs::status(FD, Status));
720 ASSERT_EQ(Status.getSize(), 123U);
723 TEST_F(FileSystemTest, FileMapping) {
724 // Create a temp file.
726 SmallString<64> TempPath;
728 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
729 unsigned Size = 4096;
730 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
732 // Map in temp file and add some content
734 StringRef Val("hello there");
736 fs::mapped_file_region mfr(FileDescriptor,
737 fs::mapped_file_region::readwrite, Size, 0, EC);
739 std::copy(Val.begin(), Val.end(), mfr.data());
740 // Explicitly add a 0.
741 mfr.data()[Val.size()] = 0;
745 // Map it back in read-only
747 EC = fs::openFileForRead(Twine(TempPath), FD);
749 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
753 EXPECT_EQ(StringRef(mfr.const_data()), Val);
756 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
758 ASSERT_EQ(close(FD), 0);
761 TEST(Support, NormalizePath) {
762 #if defined(LLVM_ON_WIN32)
763 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
764 EXPECT_EQ(path__, windows__);
766 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
767 EXPECT_EQ(path__, not_windows__);
770 SmallString<64> Path1("a");
771 SmallString<64> Path2("a/b");
772 SmallString<64> Path3("a\\b");
773 SmallString<64> Path4("a\\\\b");
774 SmallString<64> Path5("\\a");
775 SmallString<64> Path6("a\\");
778 EXPECT_PATH_IS(Path1, "a", "a");
781 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
784 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
787 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
790 EXPECT_PATH_IS(Path5, "\\a", "/a");
793 EXPECT_PATH_IS(Path6, "a\\", "a/");
795 #undef EXPECT_PATH_IS
798 TEST(Support, RemoveLeadingDotSlash) {
799 StringRef Path1("././/foolz/wat");
800 StringRef Path2("./////");
802 Path1 = path::remove_leading_dotslash(Path1);
803 EXPECT_EQ(Path1, "foolz/wat");
804 Path2 = path::remove_leading_dotslash(Path2);
805 EXPECT_EQ(Path2, "");
807 } // anonymous namespace