1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// 2 // 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 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" 17 18 #ifdef LLVM_ON_WIN32 19 #include <winerror.h> 20 #endif 21 22 using namespace llvm; 23 using namespace llvm::sys; 24 25 #define ASSERT_NO_ERROR(x) \ 26 if (std::error_code ASSERT_NO_ERROR_ec = x) { \ 27 SmallString<128> MessageStorage; \ 28 raw_svector_ostream Message(MessageStorage); \ 29 Message << #x ": did not return errc::success.\n" \ 30 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 31 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 32 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 33 } else { \ 34 } 35 36 namespace { 37 38 TEST(is_separator, Works) { 39 EXPECT_TRUE(path::is_separator('/')); 40 EXPECT_FALSE(path::is_separator('\0')); 41 EXPECT_FALSE(path::is_separator('-')); 42 EXPECT_FALSE(path::is_separator(' ')); 43 44 #ifdef LLVM_ON_WIN32 45 EXPECT_TRUE(path::is_separator('\\')); 46 #else 47 EXPECT_FALSE(path::is_separator('\\')); 48 #endif 49 } 50 51 TEST(Support, Path) { 52 SmallVector<StringRef, 40> paths; 53 paths.push_back(""); 54 paths.push_back("."); 55 paths.push_back(".."); 56 paths.push_back("foo"); 57 paths.push_back("/"); 58 paths.push_back("/foo"); 59 paths.push_back("foo/"); 60 paths.push_back("/foo/"); 61 paths.push_back("foo/bar"); 62 paths.push_back("/foo/bar"); 63 paths.push_back("//net"); 64 paths.push_back("//net/foo"); 65 paths.push_back("///foo///"); 66 paths.push_back("///foo///bar"); 67 paths.push_back("/."); 68 paths.push_back("./"); 69 paths.push_back("/.."); 70 paths.push_back("../"); 71 paths.push_back("foo/."); 72 paths.push_back("foo/.."); 73 paths.push_back("foo/./"); 74 paths.push_back("foo/./bar"); 75 paths.push_back("foo/.."); 76 paths.push_back("foo/../"); 77 paths.push_back("foo/../bar"); 78 paths.push_back("c:"); 79 paths.push_back("c:/"); 80 paths.push_back("c:foo"); 81 paths.push_back("c:/foo"); 82 paths.push_back("c:foo/"); 83 paths.push_back("c:/foo/"); 84 paths.push_back("c:/foo/bar"); 85 paths.push_back("prn:"); 86 paths.push_back("c:\\"); 87 paths.push_back("c:foo"); 88 paths.push_back("c:\\foo"); 89 paths.push_back("c:foo\\"); 90 paths.push_back("c:\\foo\\"); 91 paths.push_back("c:\\foo/"); 92 paths.push_back("c:/foo\\bar"); 93 94 SmallVector<StringRef, 5> ComponentStack; 95 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), 96 e = paths.end(); 97 i != e; 98 ++i) { 99 for (sys::path::const_iterator ci = sys::path::begin(*i), 100 ce = sys::path::end(*i); 101 ci != ce; 102 ++ci) { 103 ASSERT_FALSE(ci->empty()); 104 ComponentStack.push_back(*ci); 105 } 106 107 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), 108 ce = sys::path::rend(*i); 109 ci != ce; 110 ++ci) { 111 ASSERT_TRUE(*ci == ComponentStack.back()); 112 ComponentStack.pop_back(); 113 } 114 ASSERT_TRUE(ComponentStack.empty()); 115 116 path::has_root_path(*i); 117 path::root_path(*i); 118 path::has_root_name(*i); 119 path::root_name(*i); 120 path::has_root_directory(*i); 121 path::root_directory(*i); 122 path::has_parent_path(*i); 123 path::parent_path(*i); 124 path::has_filename(*i); 125 path::filename(*i); 126 path::has_stem(*i); 127 path::stem(*i); 128 path::has_extension(*i); 129 path::extension(*i); 130 path::is_absolute(*i); 131 path::is_relative(*i); 132 133 SmallString<128> temp_store; 134 temp_store = *i; 135 ASSERT_NO_ERROR(fs::make_absolute(temp_store)); 136 temp_store = *i; 137 path::remove_filename(temp_store); 138 139 temp_store = *i; 140 path::replace_extension(temp_store, "ext"); 141 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; 142 stem = path::stem(filename); 143 ext = path::extension(filename); 144 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str()); 145 146 path::native(*i, temp_store); 147 } 148 } 149 150 TEST(Support, RelativePathIterator) { 151 SmallString<64> Path(StringRef("c/d/e/foo.txt")); 152 typedef SmallVector<StringRef, 4> PathComponents; 153 PathComponents ExpectedPathComponents; 154 PathComponents ActualPathComponents; 155 156 StringRef(Path).split(ExpectedPathComponents, "/"); 157 158 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 159 ++I) { 160 ActualPathComponents.push_back(*I); 161 } 162 163 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 164 165 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 166 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 167 } 168 } 169 170 TEST(Support, AbsolutePathIterator) { 171 SmallString<64> Path(StringRef("/c/d/e/foo.txt")); 172 typedef SmallVector<StringRef, 4> PathComponents; 173 PathComponents ExpectedPathComponents; 174 PathComponents ActualPathComponents; 175 176 StringRef(Path).split(ExpectedPathComponents, "/"); 177 178 // The root path will also be a component when iterating 179 ExpectedPathComponents[0] = "/"; 180 181 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 182 ++I) { 183 ActualPathComponents.push_back(*I); 184 } 185 186 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 187 188 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 189 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 190 } 191 } 192 193 #ifdef LLVM_ON_WIN32 194 TEST(Support, AbsolutePathIteratorWin32) { 195 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); 196 typedef SmallVector<StringRef, 4> PathComponents; 197 PathComponents ExpectedPathComponents; 198 PathComponents ActualPathComponents; 199 200 StringRef(Path).split(ExpectedPathComponents, "\\"); 201 202 // The root path (which comes after the drive name) will also be a component 203 // when iterating. 204 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); 205 206 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 207 ++I) { 208 ActualPathComponents.push_back(*I); 209 } 210 211 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 212 213 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 214 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 215 } 216 } 217 #endif // LLVM_ON_WIN32 218 219 TEST(Support, AbsolutePathIteratorEnd) { 220 // Trailing slashes are converted to '.' unless they are part of the root path. 221 SmallVector<StringRef, 4> Paths; 222 Paths.push_back("/foo/"); 223 Paths.push_back("/foo//"); 224 Paths.push_back("//net//"); 225 #ifdef LLVM_ON_WIN32 226 Paths.push_back("c:\\\\"); 227 #endif 228 229 for (StringRef Path : Paths) { 230 StringRef LastComponent = *path::rbegin(Path); 231 EXPECT_EQ(".", LastComponent); 232 } 233 234 SmallVector<StringRef, 3> RootPaths; 235 RootPaths.push_back("/"); 236 RootPaths.push_back("//net/"); 237 #ifdef LLVM_ON_WIN32 238 RootPaths.push_back("c:\\"); 239 #endif 240 241 for (StringRef Path : RootPaths) { 242 StringRef LastComponent = *path::rbegin(Path); 243 EXPECT_EQ(1u, LastComponent.size()); 244 EXPECT_TRUE(path::is_separator(LastComponent[0])); 245 } 246 } 247 248 TEST(Support, HomeDirectory) { 249 #ifdef LLVM_ON_UNIX 250 // This test only makes sense on Unix if $HOME is set. 251 if (::getenv("HOME")) { 252 #endif 253 SmallString<128> HomeDir; 254 EXPECT_TRUE(path::home_directory(HomeDir)); 255 EXPECT_FALSE(HomeDir.empty()); 256 #ifdef LLVM_ON_UNIX 257 } 258 #endif 259 } 260 261 class FileSystemTest : public testing::Test { 262 protected: 263 /// Unique temporary directory in which all created filesystem entities must 264 /// be placed. It is recursively removed at the end of each test. 265 SmallString<128> TestDirectory; 266 267 virtual void SetUp() { 268 ASSERT_NO_ERROR( 269 fs::createUniqueDirectory("file-system-test", TestDirectory)); 270 // We don't care about this specific file. 271 errs() << "Test Directory: " << TestDirectory << '\n'; 272 errs().flush(); 273 } 274 275 virtual void TearDown() { 276 ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); 277 } 278 }; 279 280 TEST_F(FileSystemTest, Unique) { 281 // Create a temp file. 282 int FileDescriptor; 283 SmallString<64> TempPath; 284 ASSERT_NO_ERROR( 285 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 286 287 // The same file should return an identical unique id. 288 fs::UniqueID F1, F2; 289 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); 290 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); 291 ASSERT_EQ(F1, F2); 292 293 // Different files should return different unique ids. 294 int FileDescriptor2; 295 SmallString<64> TempPath2; 296 ASSERT_NO_ERROR( 297 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); 298 299 fs::UniqueID D; 300 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); 301 ASSERT_NE(D, F1); 302 ::close(FileDescriptor2); 303 304 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 305 306 // Two paths representing the same file on disk should still provide the 307 // same unique id. We can test this by making a hard link. 308 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); 309 fs::UniqueID D2; 310 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); 311 ASSERT_EQ(D2, F1); 312 313 ::close(FileDescriptor); 314 315 SmallString<128> Dir1; 316 ASSERT_NO_ERROR( 317 fs::createUniqueDirectory("dir1", Dir1)); 318 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); 319 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); 320 ASSERT_EQ(F1, F2); 321 322 SmallString<128> Dir2; 323 ASSERT_NO_ERROR( 324 fs::createUniqueDirectory("dir2", Dir2)); 325 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); 326 ASSERT_NE(F1, F2); 327 } 328 329 TEST_F(FileSystemTest, TempFiles) { 330 // Create a temp file. 331 int FileDescriptor; 332 SmallString<64> TempPath; 333 ASSERT_NO_ERROR( 334 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 335 336 // Make sure it exists. 337 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 338 339 // Create another temp tile. 340 int FD2; 341 SmallString<64> TempPath2; 342 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); 343 ASSERT_TRUE(TempPath2.endswith(".temp")); 344 ASSERT_NE(TempPath.str(), TempPath2.str()); 345 346 fs::file_status A, B; 347 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 348 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 349 EXPECT_FALSE(fs::equivalent(A, B)); 350 351 ::close(FD2); 352 353 // Remove Temp2. 354 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 355 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 356 ASSERT_EQ(fs::remove(Twine(TempPath2), false), 357 errc::no_such_file_or_directory); 358 359 std::error_code EC = fs::status(TempPath2.c_str(), B); 360 EXPECT_EQ(EC, errc::no_such_file_or_directory); 361 EXPECT_EQ(B.type(), fs::file_type::file_not_found); 362 363 // Make sure Temp2 doesn't exist. 364 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist), 365 errc::no_such_file_or_directory); 366 367 SmallString<64> TempPath3; 368 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); 369 ASSERT_FALSE(TempPath3.endswith(".")); 370 371 // Create a hard link to Temp1. 372 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); 373 bool equal; 374 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); 375 EXPECT_TRUE(equal); 376 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 377 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 378 EXPECT_TRUE(fs::equivalent(A, B)); 379 380 // Remove Temp1. 381 ::close(FileDescriptor); 382 ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); 383 384 // Remove the hard link. 385 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 386 387 // Make sure Temp1 doesn't exist. 388 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist), 389 errc::no_such_file_or_directory); 390 391 #ifdef LLVM_ON_WIN32 392 // Path name > 260 chars should get an error. 393 const char *Path270 = 394 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" 395 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" 396 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" 397 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" 398 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; 399 EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath), 400 errc::no_such_file_or_directory); 401 #endif 402 } 403 404 TEST_F(FileSystemTest, CreateDir) { 405 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); 406 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); 407 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), 408 errc::file_exists); 409 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); 410 } 411 412 TEST_F(FileSystemTest, DirectoryIteration) { 413 std::error_code ec; 414 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) 415 ASSERT_NO_ERROR(ec); 416 417 // Create a known hierarchy to recurse over. 418 ASSERT_NO_ERROR( 419 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); 420 ASSERT_NO_ERROR( 421 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); 422 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + 423 "/recursive/dontlookhere/da1")); 424 ASSERT_NO_ERROR( 425 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); 426 ASSERT_NO_ERROR( 427 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); 428 typedef std::vector<std::string> v_t; 429 v_t visited; 430 for (fs::recursive_directory_iterator i(Twine(TestDirectory) 431 + "/recursive", ec), e; i != e; i.increment(ec)){ 432 ASSERT_NO_ERROR(ec); 433 if (path::filename(i->path()) == "p1") { 434 i.pop(); 435 // FIXME: recursive_directory_iterator should be more robust. 436 if (i == e) break; 437 } 438 if (path::filename(i->path()) == "dontlookhere") 439 i.no_push(); 440 visited.push_back(path::filename(i->path())); 441 } 442 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0"); 443 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1"); 444 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1"); 445 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(), 446 "dontlookhere"); 447 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1"); 448 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0"); 449 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1"); 450 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop"); 451 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1"); 452 453 // Make sure that each path was visited correctly. 454 ASSERT_NE(a0, visited.end()); 455 ASSERT_NE(aa1, visited.end()); 456 ASSERT_NE(ab1, visited.end()); 457 ASSERT_NE(dontlookhere, visited.end()); 458 ASSERT_EQ(da1, visited.end()); // Not visited. 459 ASSERT_NE(z0, visited.end()); 460 ASSERT_NE(za1, visited.end()); 461 ASSERT_NE(pop, visited.end()); 462 ASSERT_EQ(p1, visited.end()); // Not visited. 463 464 // Make sure that parents were visited before children. No other ordering 465 // guarantees can be made across siblings. 466 ASSERT_LT(a0, aa1); 467 ASSERT_LT(a0, ab1); 468 ASSERT_LT(z0, za1); 469 470 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); 471 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); 472 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); 473 ASSERT_NO_ERROR( 474 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); 475 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); 476 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); 477 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); 478 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); 479 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); 480 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); 481 } 482 483 const char archive[] = "!<arch>\x0A"; 484 const char bitcode[] = "\xde\xc0\x17\x0b"; 485 const char coff_object[] = "\x00\x00......"; 486 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......" 487 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8"; 488 const char coff_import_library[] = "\x00\x00\xff\xff...."; 489 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 490 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 491 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00"; 492 const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01"; 493 const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02"; 494 const char macho_fixed_virtual_memory_shared_lib[] = 495 "\xfe\xed\xfa\xce..........\x00\x03"; 496 const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04"; 497 const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05"; 498 const char macho_dynamically_linked_shared_lib[] = 499 "\xfe\xed\xfa\xce..........\x00\x06"; 500 const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07"; 501 const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08"; 502 const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a"; 503 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; 504 const char macho_dynamically_linked_shared_lib_stub[] = 505 "\xfe\xed\xfa\xce..........\x00\x09"; 506 507 TEST_F(FileSystemTest, Magic) { 508 struct type { 509 const char *filename; 510 const char *magic_str; 511 size_t magic_str_len; 512 fs::file_magic magic; 513 } types[] = { 514 #define DEFINE(magic) \ 515 { #magic, magic, sizeof(magic), fs::file_magic::magic } 516 DEFINE(archive), 517 DEFINE(bitcode), 518 DEFINE(coff_object), 519 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object }, 520 DEFINE(coff_import_library), 521 DEFINE(elf_relocatable), 522 DEFINE(macho_universal_binary), 523 DEFINE(macho_object), 524 DEFINE(macho_executable), 525 DEFINE(macho_fixed_virtual_memory_shared_lib), 526 DEFINE(macho_core), 527 DEFINE(macho_preload_executable), 528 DEFINE(macho_dynamically_linked_shared_lib), 529 DEFINE(macho_dynamic_linker), 530 DEFINE(macho_bundle), 531 DEFINE(macho_dynamically_linked_shared_lib_stub), 532 DEFINE(macho_dsym_companion), 533 DEFINE(windows_resource) 534 #undef DEFINE 535 }; 536 537 // Create some files filled with magic. 538 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; 539 ++i) { 540 SmallString<128> file_pathname(TestDirectory); 541 path::append(file_pathname, i->filename); 542 std::error_code EC; 543 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None); 544 ASSERT_FALSE(file.has_error()); 545 StringRef magic(i->magic_str, i->magic_str_len); 546 file << magic; 547 file.close(); 548 EXPECT_EQ(i->magic, fs::identify_magic(magic)); 549 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); 550 } 551 } 552 553 #ifdef LLVM_ON_WIN32 554 TEST_F(FileSystemTest, CarriageReturn) { 555 SmallString<128> FilePathname(TestDirectory); 556 std::error_code EC; 557 path::append(FilePathname, "test"); 558 559 { 560 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text); 561 ASSERT_NO_ERROR(EC); 562 File << '\n'; 563 } 564 { 565 auto Buf = MemoryBuffer::getFile(FilePathname.str()); 566 EXPECT_TRUE((bool)Buf); 567 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n"); 568 } 569 570 { 571 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None); 572 ASSERT_NO_ERROR(EC); 573 File << '\n'; 574 } 575 { 576 auto Buf = MemoryBuffer::getFile(FilePathname.str()); 577 EXPECT_TRUE((bool)Buf); 578 EXPECT_EQ(Buf.get()->getBuffer(), "\n"); 579 } 580 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); 581 } 582 #endif 583 584 TEST_F(FileSystemTest, FileMapping) { 585 // Create a temp file. 586 int FileDescriptor; 587 SmallString<64> TempPath; 588 ASSERT_NO_ERROR( 589 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 590 // Map in temp file and add some content 591 std::error_code EC; 592 StringRef Val("hello there"); 593 { 594 fs::mapped_file_region mfr(FileDescriptor, 595 true, 596 fs::mapped_file_region::readwrite, 597 4096, 598 0, 599 EC); 600 ASSERT_NO_ERROR(EC); 601 std::copy(Val.begin(), Val.end(), mfr.data()); 602 // Explicitly add a 0. 603 mfr.data()[Val.size()] = 0; 604 // Unmap temp file 605 } 606 607 // Map it back in read-only 608 fs::mapped_file_region mfr(Twine(TempPath), 609 fs::mapped_file_region::readonly, 610 0, 611 0, 612 EC); 613 ASSERT_NO_ERROR(EC); 614 615 // Verify content 616 EXPECT_EQ(StringRef(mfr.const_data()), Val); 617 618 // Unmap temp file 619 620 fs::mapped_file_region m(Twine(TempPath), 621 fs::mapped_file_region::readonly, 622 0, 623 0, 624 EC); 625 ASSERT_NO_ERROR(EC); 626 const char *Data = m.const_data(); 627 fs::mapped_file_region mfrrv(std::move(m)); 628 EXPECT_EQ(mfrrv.const_data(), Data); 629 } 630 631 TEST(Support, NormalizePath) { 632 #if defined(LLVM_ON_WIN32) 633 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ 634 EXPECT_EQ(path__, windows__); 635 #else 636 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ 637 EXPECT_EQ(path__, not_windows__); 638 #endif 639 640 SmallString<64> Path1("a"); 641 SmallString<64> Path2("a/b"); 642 SmallString<64> Path3("a\\b"); 643 SmallString<64> Path4("a\\\\b"); 644 SmallString<64> Path5("\\a"); 645 SmallString<64> Path6("a\\"); 646 647 path::native(Path1); 648 EXPECT_PATH_IS(Path1, "a", "a"); 649 650 path::native(Path2); 651 EXPECT_PATH_IS(Path2, "a\\b", "a/b"); 652 653 path::native(Path3); 654 EXPECT_PATH_IS(Path3, "a\\b", "a/b"); 655 656 path::native(Path4); 657 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); 658 659 path::native(Path5); 660 EXPECT_PATH_IS(Path5, "\\a", "/a"); 661 662 path::native(Path6); 663 EXPECT_PATH_IS(Path6, "a\\", "a/"); 664 665 #undef EXPECT_PATH_IS 666 } 667 } // anonymous namespace 668