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"
24 using namespace llvm::sys;
26 #define ASSERT_NO_ERROR(x) \
27 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
28 SmallString<128> MessageStorage; \
29 raw_svector_ostream Message(MessageStorage); \
30 Message << #x ": did not return errc::success.\n" \
31 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
32 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
33 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
39 TEST(is_separator, Works) {
40 EXPECT_TRUE(path::is_separator('/'));
41 EXPECT_FALSE(path::is_separator('\0'));
42 EXPECT_FALSE(path::is_separator('-'));
43 EXPECT_FALSE(path::is_separator(' '));
46 EXPECT_TRUE(path::is_separator('\\'));
48 EXPECT_FALSE(path::is_separator('\\'));
53 SmallVector<StringRef, 40> paths;
56 paths.push_back("..");
57 paths.push_back("foo");
59 paths.push_back("/foo");
60 paths.push_back("foo/");
61 paths.push_back("/foo/");
62 paths.push_back("foo/bar");
63 paths.push_back("/foo/bar");
64 paths.push_back("//net");
65 paths.push_back("//net/foo");
66 paths.push_back("///foo///");
67 paths.push_back("///foo///bar");
68 paths.push_back("/.");
69 paths.push_back("./");
70 paths.push_back("/..");
71 paths.push_back("../");
72 paths.push_back("foo/.");
73 paths.push_back("foo/..");
74 paths.push_back("foo/./");
75 paths.push_back("foo/./bar");
76 paths.push_back("foo/..");
77 paths.push_back("foo/../");
78 paths.push_back("foo/../bar");
79 paths.push_back("c:");
80 paths.push_back("c:/");
81 paths.push_back("c:foo");
82 paths.push_back("c:/foo");
83 paths.push_back("c:foo/");
84 paths.push_back("c:/foo/");
85 paths.push_back("c:/foo/bar");
86 paths.push_back("prn:");
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/");
93 paths.push_back("c:/foo\\bar");
95 SmallVector<StringRef, 5> ComponentStack;
96 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
100 for (sys::path::const_iterator ci = sys::path::begin(*i),
101 ce = sys::path::end(*i);
104 ASSERT_FALSE(ci->empty());
105 ComponentStack.push_back(*ci);
108 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
109 ce = sys::path::rend(*i);
112 ASSERT_TRUE(*ci == ComponentStack.back());
113 ComponentStack.pop_back();
115 ASSERT_TRUE(ComponentStack.empty());
117 path::has_root_path(*i);
119 path::has_root_name(*i);
121 path::has_root_directory(*i);
122 path::root_directory(*i);
123 path::has_parent_path(*i);
124 path::parent_path(*i);
125 path::has_filename(*i);
129 path::has_extension(*i);
131 path::is_absolute(*i);
132 path::is_relative(*i);
134 SmallString<128> temp_store;
136 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
138 path::remove_filename(temp_store);
141 path::replace_extension(temp_store, "ext");
142 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
143 stem = path::stem(filename);
144 ext = path::extension(filename);
145 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
147 path::native(*i, temp_store);
151 TEST(Support, RelativePathIterator) {
152 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
153 typedef SmallVector<StringRef, 4> PathComponents;
154 PathComponents ExpectedPathComponents;
155 PathComponents ActualPathComponents;
157 StringRef(Path).split(ExpectedPathComponents, "/");
159 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
161 ActualPathComponents.push_back(*I);
164 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
166 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
167 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
171 TEST(Support, RelativePathDotIterator) {
172 SmallString<64> Path(StringRef(".c/.d/../."));
173 typedef SmallVector<StringRef, 4> PathComponents;
174 PathComponents ExpectedPathComponents;
175 PathComponents ActualPathComponents;
177 StringRef(Path).split(ExpectedPathComponents, "/");
179 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
181 ActualPathComponents.push_back(*I);
184 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
186 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
187 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
191 TEST(Support, AbsolutePathIterator) {
192 SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
193 typedef SmallVector<StringRef, 4> PathComponents;
194 PathComponents ExpectedPathComponents;
195 PathComponents ActualPathComponents;
197 StringRef(Path).split(ExpectedPathComponents, "/");
199 // The root path will also be a component when iterating
200 ExpectedPathComponents[0] = "/";
202 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
204 ActualPathComponents.push_back(*I);
207 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
209 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
210 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
214 TEST(Support, AbsolutePathDotIterator) {
215 SmallString<64> Path(StringRef("/.c/.d/../."));
216 typedef SmallVector<StringRef, 4> PathComponents;
217 PathComponents ExpectedPathComponents;
218 PathComponents ActualPathComponents;
220 StringRef(Path).split(ExpectedPathComponents, "/");
222 // The root path will also be a component when iterating
223 ExpectedPathComponents[0] = "/";
225 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
227 ActualPathComponents.push_back(*I);
230 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
232 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
233 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
238 TEST(Support, AbsolutePathIteratorWin32) {
239 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
240 typedef SmallVector<StringRef, 4> PathComponents;
241 PathComponents ExpectedPathComponents;
242 PathComponents ActualPathComponents;
244 StringRef(Path).split(ExpectedPathComponents, "\\");
246 // The root path (which comes after the drive name) will also be a component
248 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
250 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
252 ActualPathComponents.push_back(*I);
255 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
257 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
258 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
261 #endif // LLVM_ON_WIN32
263 TEST(Support, AbsolutePathIteratorEnd) {
264 // Trailing slashes are converted to '.' unless they are part of the root path.
265 SmallVector<StringRef, 4> Paths;
266 Paths.push_back("/foo/");
267 Paths.push_back("/foo//");
268 Paths.push_back("//net//");
270 Paths.push_back("c:\\\\");
273 for (StringRef Path : Paths) {
274 StringRef LastComponent = *path::rbegin(Path);
275 EXPECT_EQ(".", LastComponent);
278 SmallVector<StringRef, 3> RootPaths;
279 RootPaths.push_back("/");
280 RootPaths.push_back("//net/");
282 RootPaths.push_back("c:\\");
285 for (StringRef Path : RootPaths) {
286 StringRef LastComponent = *path::rbegin(Path);
287 EXPECT_EQ(1u, LastComponent.size());
288 EXPECT_TRUE(path::is_separator(LastComponent[0]));
292 TEST(Support, HomeDirectory) {
294 // This test only makes sense on Unix if $HOME is set.
295 if (::getenv("HOME")) {
297 SmallString<128> HomeDir;
298 EXPECT_TRUE(path::home_directory(HomeDir));
299 EXPECT_FALSE(HomeDir.empty());
305 class FileSystemTest : public testing::Test {
307 /// Unique temporary directory in which all created filesystem entities must
308 /// be placed. It is removed at the end of each test (must be empty).
309 SmallString<128> TestDirectory;
311 virtual void SetUp() {
313 fs::createUniqueDirectory("file-system-test", TestDirectory));
314 // We don't care about this specific file.
315 errs() << "Test Directory: " << TestDirectory << '\n';
319 virtual void TearDown() {
320 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
324 TEST_F(FileSystemTest, Unique) {
325 // Create a temp file.
327 SmallString<64> TempPath;
329 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
331 // The same file should return an identical unique id.
333 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
334 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
337 // Different files should return different unique ids.
339 SmallString<64> TempPath2;
341 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
344 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
346 ::close(FileDescriptor2);
348 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
350 // Two paths representing the same file on disk should still provide the
351 // same unique id. We can test this by making a hard link.
352 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
354 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
357 ::close(FileDescriptor);
359 SmallString<128> Dir1;
361 fs::createUniqueDirectory("dir1", Dir1));
362 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
363 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
366 SmallString<128> Dir2;
368 fs::createUniqueDirectory("dir2", Dir2));
369 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
373 TEST_F(FileSystemTest, TempFiles) {
374 // Create a temp file.
376 SmallString<64> TempPath;
378 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
380 // Make sure it exists.
381 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
383 // Create another temp tile.
385 SmallString<64> TempPath2;
386 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
387 ASSERT_TRUE(TempPath2.endswith(".temp"));
388 ASSERT_NE(TempPath.str(), TempPath2.str());
390 fs::file_status A, B;
391 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
392 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
393 EXPECT_FALSE(fs::equivalent(A, B));
398 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
399 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
400 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
401 errc::no_such_file_or_directory);
403 std::error_code EC = fs::status(TempPath2.c_str(), B);
404 EXPECT_EQ(EC, errc::no_such_file_or_directory);
405 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
407 // Make sure Temp2 doesn't exist.
408 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
409 errc::no_such_file_or_directory);
411 SmallString<64> TempPath3;
412 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
413 ASSERT_FALSE(TempPath3.endswith("."));
415 // Create a hard link to Temp1.
416 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
418 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
420 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
421 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
422 EXPECT_TRUE(fs::equivalent(A, B));
425 ::close(FileDescriptor);
426 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
428 // Remove the hard link.
429 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
431 // Make sure Temp1 doesn't exist.
432 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
433 errc::no_such_file_or_directory);
436 // Path name > 260 chars should get an error.
437 const char *Path270 =
438 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
439 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
440 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
441 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
442 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
443 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
444 errc::invalid_argument);
445 // Relative path < 247 chars, no problem.
446 const char *Path216 =
447 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
448 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
449 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
450 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
451 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
452 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
456 TEST_F(FileSystemTest, CreateDir) {
457 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
458 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
459 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
461 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
464 // Prove that create_directories() can handle a pathname > 248 characters,
465 // which is the documented limit for CreateDirectory().
466 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
467 // Generate a directory path guaranteed to fall into that range.
468 size_t TmpLen = TestDirectory.size();
469 const char *OneDir = "\\123456789";
470 size_t OneDirLen = strlen(OneDir);
471 ASSERT_LT(OneDirLen, 12U);
472 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
473 SmallString<260> LongDir(TestDirectory);
474 for (size_t I = 0; I < NLevels; ++I)
475 LongDir.append(OneDir);
476 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
477 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
478 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
480 // Tidy up, "recursively" removing the directories.
481 StringRef ThisDir(LongDir);
482 for (size_t J = 0; J < NLevels; ++J) {
483 ASSERT_NO_ERROR(fs::remove(ThisDir));
484 ThisDir = path::parent_path(ThisDir);
487 // Similarly for a relative pathname. Need to set the current directory to
488 // TestDirectory so that the one we create ends up in the right place.
489 char PreviousDir[260];
490 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
491 ASSERT_GT(PreviousDirLen, 0U);
492 ASSERT_LT(PreviousDirLen, 260U);
493 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
495 // Generate a relative directory name with absolute length > 248.
496 size_t LongDirLen = 249 - TestDirectory.size();
497 LongDir.assign(LongDirLen, 'a');
498 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
499 // While we're here, prove that .. and . handling works in these long paths.
500 const char *DotDotDirs = "\\..\\.\\b";
501 LongDir.append(DotDotDirs);
502 ASSERT_NO_ERROR(fs::create_directory("b"));
503 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
505 ASSERT_NO_ERROR(fs::remove("b"));
506 ASSERT_NO_ERROR(fs::remove(
507 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
508 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
512 TEST_F(FileSystemTest, DirectoryIteration) {
514 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
517 // Create a known hierarchy to recurse over.
519 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
521 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
522 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
523 "/recursive/dontlookhere/da1"));
525 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
527 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
528 typedef std::vector<std::string> v_t;
530 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
531 + "/recursive", ec), e; i != e; i.increment(ec)){
533 if (path::filename(i->path()) == "p1") {
535 // FIXME: recursive_directory_iterator should be more robust.
538 if (path::filename(i->path()) == "dontlookhere")
540 visited.push_back(path::filename(i->path()));
542 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
543 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
544 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
545 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
547 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
548 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
549 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
550 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
551 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
553 // Make sure that each path was visited correctly.
554 ASSERT_NE(a0, visited.end());
555 ASSERT_NE(aa1, visited.end());
556 ASSERT_NE(ab1, visited.end());
557 ASSERT_NE(dontlookhere, visited.end());
558 ASSERT_EQ(da1, visited.end()); // Not visited.
559 ASSERT_NE(z0, visited.end());
560 ASSERT_NE(za1, visited.end());
561 ASSERT_NE(pop, visited.end());
562 ASSERT_EQ(p1, visited.end()); // Not visited.
564 // Make sure that parents were visited before children. No other ordering
565 // guarantees can be made across siblings.
570 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
571 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
572 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
574 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
575 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
576 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
577 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
578 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
579 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
580 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
583 const char archive[] = "!<arch>\x0A";
584 const char bitcode[] = "\xde\xc0\x17\x0b";
585 const char coff_object[] = "\x00\x00......";
586 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
587 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
588 const char coff_import_library[] = "\x00\x00\xff\xff....";
589 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
590 0, 0, 0, 0, 0, 0, 0, 0, 1 };
591 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00";
592 const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01";
593 const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02";
594 const char macho_fixed_virtual_memory_shared_lib[] =
595 "\xfe\xed\xfa\xce..........\x00\x03";
596 const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04";
597 const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05";
598 const char macho_dynamically_linked_shared_lib[] =
599 "\xfe\xed\xfa\xce..........\x00\x06";
600 const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07";
601 const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08";
602 const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a";
603 const char macho_kext_bundle[] = "\xfe\xed\xfa\xce..........\x00\x0b";
604 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
605 const char macho_dynamically_linked_shared_lib_stub[] =
606 "\xfe\xed\xfa\xce..........\x00\x09";
608 TEST_F(FileSystemTest, Magic) {
610 const char *filename;
611 const char *magic_str;
612 size_t magic_str_len;
613 fs::file_magic magic;
615 #define DEFINE(magic) \
616 { #magic, magic, sizeof(magic), fs::file_magic::magic }
620 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
621 DEFINE(coff_import_library),
622 DEFINE(elf_relocatable),
623 DEFINE(macho_universal_binary),
624 DEFINE(macho_object),
625 DEFINE(macho_executable),
626 DEFINE(macho_fixed_virtual_memory_shared_lib),
628 DEFINE(macho_preload_executable),
629 DEFINE(macho_dynamically_linked_shared_lib),
630 DEFINE(macho_dynamic_linker),
631 DEFINE(macho_bundle),
632 DEFINE(macho_dynamically_linked_shared_lib_stub),
633 DEFINE(macho_dsym_companion),
634 DEFINE(macho_kext_bundle),
635 DEFINE(windows_resource)
639 // Create some files filled with magic.
640 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
642 SmallString<128> file_pathname(TestDirectory);
643 path::append(file_pathname, i->filename);
645 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
646 ASSERT_FALSE(file.has_error());
647 StringRef magic(i->magic_str, i->magic_str_len);
650 EXPECT_EQ(i->magic, fs::identify_magic(magic));
651 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
656 TEST_F(FileSystemTest, CarriageReturn) {
657 SmallString<128> FilePathname(TestDirectory);
659 path::append(FilePathname, "test");
662 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
667 auto Buf = MemoryBuffer::getFile(FilePathname.str());
668 EXPECT_TRUE((bool)Buf);
669 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
673 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
678 auto Buf = MemoryBuffer::getFile(FilePathname.str());
679 EXPECT_TRUE((bool)Buf);
680 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
682 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
686 TEST_F(FileSystemTest, Resize) {
688 SmallString<64> TempPath;
689 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
690 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
691 fs::file_status Status;
692 ASSERT_NO_ERROR(fs::status(FD, Status));
693 ASSERT_EQ(Status.getSize(), 123U);
696 TEST_F(FileSystemTest, FileMapping) {
697 // Create a temp file.
699 SmallString<64> TempPath;
701 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
702 unsigned Size = 4096;
703 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
705 // Map in temp file and add some content
707 StringRef Val("hello there");
709 fs::mapped_file_region mfr(FileDescriptor,
710 fs::mapped_file_region::readwrite, Size, 0, EC);
712 std::copy(Val.begin(), Val.end(), mfr.data());
713 // Explicitly add a 0.
714 mfr.data()[Val.size()] = 0;
718 // Map it back in read-only
720 EC = fs::openFileForRead(Twine(TempPath), FD);
722 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
726 EXPECT_EQ(StringRef(mfr.const_data()), Val);
729 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
731 ASSERT_EQ(close(FD), 0);
734 TEST(Support, NormalizePath) {
735 #if defined(LLVM_ON_WIN32)
736 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
737 EXPECT_EQ(path__, windows__);
739 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
740 EXPECT_EQ(path__, not_windows__);
743 SmallString<64> Path1("a");
744 SmallString<64> Path2("a/b");
745 SmallString<64> Path3("a\\b");
746 SmallString<64> Path4("a\\\\b");
747 SmallString<64> Path5("\\a");
748 SmallString<64> Path6("a\\");
751 EXPECT_PATH_IS(Path1, "a", "a");
754 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
757 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
760 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
763 EXPECT_PATH_IS(Path5, "\\a", "/a");
766 EXPECT_PATH_IS(Path6, "a\\", "a/");
768 #undef EXPECT_PATH_IS
770 } // anonymous namespace