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/ADT/STLExtras.h" 12 #include "llvm/Support/ConvertUTF.h" 13 #include "llvm/Support/Errc.h" 14 #include "llvm/Support/ErrorHandling.h" 15 #include "llvm/Support/FileSystem.h" 16 #include "llvm/Support/FileUtilities.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include "gtest/gtest.h" 20 21 #ifdef LLVM_ON_WIN32 22 #include "llvm/ADT/ArrayRef.h" 23 #include <windows.h> 24 #include <winerror.h> 25 #endif 26 27 #ifdef LLVM_ON_UNIX 28 #include <sys/stat.h> 29 #endif 30 31 using namespace llvm; 32 using namespace llvm::sys; 33 34 #define ASSERT_NO_ERROR(x) \ 35 if (std::error_code ASSERT_NO_ERROR_ec = x) { \ 36 SmallString<128> MessageStorage; \ 37 raw_svector_ostream Message(MessageStorage); \ 38 Message << #x ": did not return errc::success.\n" \ 39 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 40 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 41 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 42 } else { \ 43 } 44 45 namespace { 46 47 TEST(is_separator, Works) { 48 EXPECT_TRUE(path::is_separator('/')); 49 EXPECT_FALSE(path::is_separator('\0')); 50 EXPECT_FALSE(path::is_separator('-')); 51 EXPECT_FALSE(path::is_separator(' ')); 52 53 #ifdef LLVM_ON_WIN32 54 EXPECT_TRUE(path::is_separator('\\')); 55 #else 56 EXPECT_FALSE(path::is_separator('\\')); 57 #endif 58 } 59 60 TEST(Support, Path) { 61 SmallVector<StringRef, 40> paths; 62 paths.push_back(""); 63 paths.push_back("."); 64 paths.push_back(".."); 65 paths.push_back("foo"); 66 paths.push_back("/"); 67 paths.push_back("/foo"); 68 paths.push_back("foo/"); 69 paths.push_back("/foo/"); 70 paths.push_back("foo/bar"); 71 paths.push_back("/foo/bar"); 72 paths.push_back("//net"); 73 paths.push_back("//net/foo"); 74 paths.push_back("///foo///"); 75 paths.push_back("///foo///bar"); 76 paths.push_back("/."); 77 paths.push_back("./"); 78 paths.push_back("/.."); 79 paths.push_back("../"); 80 paths.push_back("foo/."); 81 paths.push_back("foo/.."); 82 paths.push_back("foo/./"); 83 paths.push_back("foo/./bar"); 84 paths.push_back("foo/.."); 85 paths.push_back("foo/../"); 86 paths.push_back("foo/../bar"); 87 paths.push_back("c:"); 88 paths.push_back("c:/"); 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"); 94 paths.push_back("prn:"); 95 paths.push_back("c:\\"); 96 paths.push_back("c:foo"); 97 paths.push_back("c:\\foo"); 98 paths.push_back("c:foo\\"); 99 paths.push_back("c:\\foo\\"); 100 paths.push_back("c:\\foo/"); 101 paths.push_back("c:/foo\\bar"); 102 103 SmallVector<StringRef, 5> ComponentStack; 104 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), 105 e = paths.end(); 106 i != e; 107 ++i) { 108 for (sys::path::const_iterator ci = sys::path::begin(*i), 109 ce = sys::path::end(*i); 110 ci != ce; 111 ++ci) { 112 ASSERT_FALSE(ci->empty()); 113 ComponentStack.push_back(*ci); 114 } 115 116 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), 117 ce = sys::path::rend(*i); 118 ci != ce; 119 ++ci) { 120 ASSERT_TRUE(*ci == ComponentStack.back()); 121 ComponentStack.pop_back(); 122 } 123 ASSERT_TRUE(ComponentStack.empty()); 124 125 path::has_root_path(*i); 126 path::root_path(*i); 127 path::has_root_name(*i); 128 path::root_name(*i); 129 path::has_root_directory(*i); 130 path::root_directory(*i); 131 path::has_parent_path(*i); 132 path::parent_path(*i); 133 path::has_filename(*i); 134 path::filename(*i); 135 path::has_stem(*i); 136 path::stem(*i); 137 path::has_extension(*i); 138 path::extension(*i); 139 path::is_absolute(*i); 140 path::is_relative(*i); 141 142 SmallString<128> temp_store; 143 temp_store = *i; 144 ASSERT_NO_ERROR(fs::make_absolute(temp_store)); 145 temp_store = *i; 146 path::remove_filename(temp_store); 147 148 temp_store = *i; 149 path::replace_extension(temp_store, "ext"); 150 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; 151 stem = path::stem(filename); 152 ext = path::extension(filename); 153 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str()); 154 155 path::native(*i, temp_store); 156 } 157 158 SmallString<32> Relative("foo.cpp"); 159 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative)); 160 Relative[5] = '/'; // Fix up windows paths. 161 ASSERT_EQ("/root/foo.cpp", Relative); 162 } 163 164 TEST(Support, RelativePathIterator) { 165 SmallString<64> Path(StringRef("c/d/e/foo.txt")); 166 typedef SmallVector<StringRef, 4> PathComponents; 167 PathComponents ExpectedPathComponents; 168 PathComponents ActualPathComponents; 169 170 StringRef(Path).split(ExpectedPathComponents, '/'); 171 172 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 173 ++I) { 174 ActualPathComponents.push_back(*I); 175 } 176 177 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 178 179 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 180 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 181 } 182 } 183 184 TEST(Support, RelativePathDotIterator) { 185 SmallString<64> Path(StringRef(".c/.d/../.")); 186 typedef SmallVector<StringRef, 4> PathComponents; 187 PathComponents ExpectedPathComponents; 188 PathComponents ActualPathComponents; 189 190 StringRef(Path).split(ExpectedPathComponents, '/'); 191 192 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 193 ++I) { 194 ActualPathComponents.push_back(*I); 195 } 196 197 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 198 199 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 200 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 201 } 202 } 203 204 TEST(Support, AbsolutePathIterator) { 205 SmallString<64> Path(StringRef("/c/d/e/foo.txt")); 206 typedef SmallVector<StringRef, 4> PathComponents; 207 PathComponents ExpectedPathComponents; 208 PathComponents ActualPathComponents; 209 210 StringRef(Path).split(ExpectedPathComponents, '/'); 211 212 // The root path will also be a component when iterating 213 ExpectedPathComponents[0] = "/"; 214 215 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 216 ++I) { 217 ActualPathComponents.push_back(*I); 218 } 219 220 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 221 222 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 223 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 224 } 225 } 226 227 TEST(Support, AbsolutePathDotIterator) { 228 SmallString<64> Path(StringRef("/.c/.d/../.")); 229 typedef SmallVector<StringRef, 4> PathComponents; 230 PathComponents ExpectedPathComponents; 231 PathComponents ActualPathComponents; 232 233 StringRef(Path).split(ExpectedPathComponents, '/'); 234 235 // The root path will also be a component when iterating 236 ExpectedPathComponents[0] = "/"; 237 238 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 239 ++I) { 240 ActualPathComponents.push_back(*I); 241 } 242 243 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 244 245 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 246 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 247 } 248 } 249 250 #ifdef LLVM_ON_WIN32 251 TEST(Support, AbsolutePathIteratorWin32) { 252 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); 253 typedef SmallVector<StringRef, 4> PathComponents; 254 PathComponents ExpectedPathComponents; 255 PathComponents ActualPathComponents; 256 257 StringRef(Path).split(ExpectedPathComponents, "\\"); 258 259 // The root path (which comes after the drive name) will also be a component 260 // when iterating. 261 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); 262 263 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 264 ++I) { 265 ActualPathComponents.push_back(*I); 266 } 267 268 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 269 270 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 271 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 272 } 273 } 274 #endif // LLVM_ON_WIN32 275 276 TEST(Support, AbsolutePathIteratorEnd) { 277 // Trailing slashes are converted to '.' unless they are part of the root path. 278 SmallVector<StringRef, 4> Paths; 279 Paths.push_back("/foo/"); 280 Paths.push_back("/foo//"); 281 Paths.push_back("//net//"); 282 #ifdef LLVM_ON_WIN32 283 Paths.push_back("c:\\\\"); 284 #endif 285 286 for (StringRef Path : Paths) { 287 StringRef LastComponent = *path::rbegin(Path); 288 EXPECT_EQ(".", LastComponent); 289 } 290 291 SmallVector<StringRef, 3> RootPaths; 292 RootPaths.push_back("/"); 293 RootPaths.push_back("//net/"); 294 #ifdef LLVM_ON_WIN32 295 RootPaths.push_back("c:\\"); 296 #endif 297 298 for (StringRef Path : RootPaths) { 299 StringRef LastComponent = *path::rbegin(Path); 300 EXPECT_EQ(1u, LastComponent.size()); 301 EXPECT_TRUE(path::is_separator(LastComponent[0])); 302 } 303 } 304 305 TEST(Support, HomeDirectory) { 306 std::string expected; 307 #ifdef LLVM_ON_WIN32 308 if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) { 309 auto pathLen = ::wcslen(path); 310 ArrayRef<char> ref{reinterpret_cast<char const *>(path), 311 pathLen * sizeof(wchar_t)}; 312 convertUTF16ToUTF8String(ref, expected); 313 } 314 #else 315 if (char const *path = ::getenv("HOME")) 316 expected = path; 317 #endif 318 // Do not try to test it if we don't know what to expect. 319 // On Windows we use something better than env vars. 320 if (!expected.empty()) { 321 SmallString<128> HomeDir; 322 auto status = path::home_directory(HomeDir); 323 EXPECT_TRUE(status); 324 EXPECT_EQ(expected, HomeDir); 325 } 326 } 327 328 TEST(Support, UserCacheDirectory) { 329 SmallString<13> CacheDir; 330 SmallString<20> CacheDir2; 331 auto Status = path::user_cache_directory(CacheDir, ""); 332 EXPECT_TRUE(Status ^ CacheDir.empty()); 333 334 if (Status) { 335 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed 336 EXPECT_EQ(CacheDir, CacheDir2); // and return same paths 337 338 EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c")); 339 auto It = path::rbegin(CacheDir); 340 EXPECT_EQ("file.c", *It); 341 EXPECT_EQ("B", *++It); 342 EXPECT_EQ("A", *++It); 343 auto ParentDir = *++It; 344 345 // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0" 346 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2", 347 "\xE2\x84\xB5.0")); 348 auto It2 = path::rbegin(CacheDir2); 349 EXPECT_EQ("\xE2\x84\xB5.0", *It2); 350 EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2); 351 auto ParentDir2 = *++It2; 352 353 EXPECT_EQ(ParentDir, ParentDir2); 354 } 355 } 356 357 TEST(Support, TempDirectory) { 358 SmallString<32> TempDir; 359 path::system_temp_directory(false, TempDir); 360 EXPECT_TRUE(!TempDir.empty()); 361 TempDir.clear(); 362 path::system_temp_directory(true, TempDir); 363 EXPECT_TRUE(!TempDir.empty()); 364 } 365 366 #ifdef LLVM_ON_WIN32 367 static std::string path2regex(std::string Path) { 368 size_t Pos = 0; 369 while ((Pos = Path.find('\\', Pos)) != std::string::npos) { 370 Path.replace(Pos, 1, "\\\\"); 371 Pos += 2; 372 } 373 return Path; 374 } 375 376 /// Helper for running temp dir test in separated process. See below. 377 #define EXPECT_TEMP_DIR(prepare, expected) \ 378 EXPECT_EXIT( \ 379 { \ 380 prepare; \ 381 SmallString<300> TempDir; \ 382 path::system_temp_directory(true, TempDir); \ 383 raw_os_ostream(std::cerr) << TempDir; \ 384 std::exit(0); \ 385 }, \ 386 ::testing::ExitedWithCode(0), path2regex(expected)) 387 388 TEST(SupportDeathTest, TempDirectoryOnWindows) { 389 // In this test we want to check how system_temp_directory responds to 390 // different values of specific env vars. To prevent corrupting env vars of 391 // the current process all checks are done in separated processes. 392 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder"); 393 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"), 394 "C:\\Unix\\Path\\Seperators"); 395 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$"); 396 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep"); 397 EXPECT_TEMP_DIR( 398 _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"), 399 "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80"); 400 401 // Test $TMP empty, $TEMP set. 402 EXPECT_TEMP_DIR( 403 { 404 _wputenv_s(L"TMP", L""); 405 _wputenv_s(L"TEMP", L"C:\\Valid\\Path"); 406 }, 407 "C:\\Valid\\Path"); 408 409 // All related env vars empty 410 EXPECT_TEMP_DIR( 411 { 412 _wputenv_s(L"TMP", L""); 413 _wputenv_s(L"TEMP", L""); 414 _wputenv_s(L"USERPROFILE", L""); 415 }, 416 "C:\\Temp"); 417 418 // Test evn var / path with 260 chars. 419 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"}; 420 while (Expected.size() < 260) 421 Expected.append("\\DirNameWith19Charss"); 422 ASSERT_EQ(260U, Expected.size()); 423 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str()); 424 } 425 #endif 426 427 class FileSystemTest : public testing::Test { 428 protected: 429 /// Unique temporary directory in which all created filesystem entities must 430 /// be placed. It is removed at the end of each test (must be empty). 431 SmallString<128> TestDirectory; 432 433 void SetUp() override { 434 ASSERT_NO_ERROR( 435 fs::createUniqueDirectory("file-system-test", TestDirectory)); 436 // We don't care about this specific file. 437 errs() << "Test Directory: " << TestDirectory << '\n'; 438 errs().flush(); 439 } 440 441 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); } 442 }; 443 444 TEST_F(FileSystemTest, Unique) { 445 // Create a temp file. 446 int FileDescriptor; 447 SmallString<64> TempPath; 448 ASSERT_NO_ERROR( 449 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 450 451 // The same file should return an identical unique id. 452 fs::UniqueID F1, F2; 453 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); 454 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); 455 ASSERT_EQ(F1, F2); 456 457 // Different files should return different unique ids. 458 int FileDescriptor2; 459 SmallString<64> TempPath2; 460 ASSERT_NO_ERROR( 461 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); 462 463 fs::UniqueID D; 464 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); 465 ASSERT_NE(D, F1); 466 ::close(FileDescriptor2); 467 468 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 469 470 // Two paths representing the same file on disk should still provide the 471 // same unique id. We can test this by making a hard link. 472 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); 473 fs::UniqueID D2; 474 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); 475 ASSERT_EQ(D2, F1); 476 477 ::close(FileDescriptor); 478 479 SmallString<128> Dir1; 480 ASSERT_NO_ERROR( 481 fs::createUniqueDirectory("dir1", Dir1)); 482 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); 483 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); 484 ASSERT_EQ(F1, F2); 485 486 SmallString<128> Dir2; 487 ASSERT_NO_ERROR( 488 fs::createUniqueDirectory("dir2", Dir2)); 489 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); 490 ASSERT_NE(F1, F2); 491 ASSERT_NO_ERROR(fs::remove(Dir1)); 492 ASSERT_NO_ERROR(fs::remove(Dir2)); 493 ASSERT_NO_ERROR(fs::remove(TempPath2)); 494 ASSERT_NO_ERROR(fs::remove(TempPath)); 495 } 496 497 TEST_F(FileSystemTest, TempFiles) { 498 // Create a temp file. 499 int FileDescriptor; 500 SmallString<64> TempPath; 501 ASSERT_NO_ERROR( 502 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 503 504 // Make sure it exists. 505 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 506 507 // Create another temp tile. 508 int FD2; 509 SmallString<64> TempPath2; 510 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); 511 ASSERT_TRUE(TempPath2.endswith(".temp")); 512 ASSERT_NE(TempPath.str(), TempPath2.str()); 513 514 fs::file_status A, B; 515 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 516 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 517 EXPECT_FALSE(fs::equivalent(A, B)); 518 519 ::close(FD2); 520 521 // Remove Temp2. 522 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 523 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 524 ASSERT_EQ(fs::remove(Twine(TempPath2), false), 525 errc::no_such_file_or_directory); 526 527 std::error_code EC = fs::status(TempPath2.c_str(), B); 528 EXPECT_EQ(EC, errc::no_such_file_or_directory); 529 EXPECT_EQ(B.type(), fs::file_type::file_not_found); 530 531 // Make sure Temp2 doesn't exist. 532 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist), 533 errc::no_such_file_or_directory); 534 535 SmallString<64> TempPath3; 536 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); 537 ASSERT_FALSE(TempPath3.endswith(".")); 538 FileRemover Cleanup3(TempPath3); 539 540 // Create a hard link to Temp1. 541 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); 542 bool equal; 543 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); 544 EXPECT_TRUE(equal); 545 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 546 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 547 EXPECT_TRUE(fs::equivalent(A, B)); 548 549 // Remove Temp1. 550 ::close(FileDescriptor); 551 ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); 552 553 // Remove the hard link. 554 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 555 556 // Make sure Temp1 doesn't exist. 557 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist), 558 errc::no_such_file_or_directory); 559 560 #ifdef LLVM_ON_WIN32 561 // Path name > 260 chars should get an error. 562 const char *Path270 = 563 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" 564 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" 565 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" 566 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" 567 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; 568 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath), 569 errc::invalid_argument); 570 // Relative path < 247 chars, no problem. 571 const char *Path216 = 572 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" 573 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" 574 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" 575 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; 576 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath)); 577 ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); 578 #endif 579 } 580 581 TEST_F(FileSystemTest, CreateDir) { 582 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); 583 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); 584 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), 585 errc::file_exists); 586 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); 587 588 #ifdef LLVM_ON_UNIX 589 // Set a 0000 umask so that we can test our directory permissions. 590 mode_t OldUmask = ::umask(0000); 591 592 fs::file_status Status; 593 ASSERT_NO_ERROR( 594 fs::create_directory(Twine(TestDirectory) + "baz500", false, 595 fs::perms::owner_read | fs::perms::owner_exe)); 596 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status)); 597 ASSERT_EQ(Status.permissions() & fs::perms::all_all, 598 fs::perms::owner_read | fs::perms::owner_exe); 599 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false, 600 fs::perms::all_all)); 601 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status)); 602 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all); 603 604 // Restore umask to be safe. 605 ::umask(OldUmask); 606 #endif 607 608 #ifdef LLVM_ON_WIN32 609 // Prove that create_directories() can handle a pathname > 248 characters, 610 // which is the documented limit for CreateDirectory(). 611 // (248 is MAX_PATH subtracting room for an 8.3 filename.) 612 // Generate a directory path guaranteed to fall into that range. 613 size_t TmpLen = TestDirectory.size(); 614 const char *OneDir = "\\123456789"; 615 size_t OneDirLen = strlen(OneDir); 616 ASSERT_LT(OneDirLen, 12U); 617 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1; 618 SmallString<260> LongDir(TestDirectory); 619 for (size_t I = 0; I < NLevels; ++I) 620 LongDir.append(OneDir); 621 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); 622 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); 623 ASSERT_EQ(fs::create_directories(Twine(LongDir), false), 624 errc::file_exists); 625 // Tidy up, "recursively" removing the directories. 626 StringRef ThisDir(LongDir); 627 for (size_t J = 0; J < NLevels; ++J) { 628 ASSERT_NO_ERROR(fs::remove(ThisDir)); 629 ThisDir = path::parent_path(ThisDir); 630 } 631 632 // Similarly for a relative pathname. Need to set the current directory to 633 // TestDirectory so that the one we create ends up in the right place. 634 char PreviousDir[260]; 635 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir); 636 ASSERT_GT(PreviousDirLen, 0U); 637 ASSERT_LT(PreviousDirLen, 260U); 638 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0); 639 LongDir.clear(); 640 // Generate a relative directory name with absolute length > 248. 641 size_t LongDirLen = 249 - TestDirectory.size(); 642 LongDir.assign(LongDirLen, 'a'); 643 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir))); 644 // While we're here, prove that .. and . handling works in these long paths. 645 const char *DotDotDirs = "\\..\\.\\b"; 646 LongDir.append(DotDotDirs); 647 ASSERT_NO_ERROR(fs::create_directory("b")); 648 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists); 649 // And clean up. 650 ASSERT_NO_ERROR(fs::remove("b")); 651 ASSERT_NO_ERROR(fs::remove( 652 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs))))); 653 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0); 654 #endif 655 } 656 657 TEST_F(FileSystemTest, DirectoryIteration) { 658 std::error_code ec; 659 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) 660 ASSERT_NO_ERROR(ec); 661 662 // Create a known hierarchy to recurse over. 663 ASSERT_NO_ERROR( 664 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); 665 ASSERT_NO_ERROR( 666 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); 667 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + 668 "/recursive/dontlookhere/da1")); 669 ASSERT_NO_ERROR( 670 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); 671 ASSERT_NO_ERROR( 672 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); 673 typedef std::vector<std::string> v_t; 674 v_t visited; 675 for (fs::recursive_directory_iterator i(Twine(TestDirectory) 676 + "/recursive", ec), e; i != e; i.increment(ec)){ 677 ASSERT_NO_ERROR(ec); 678 if (path::filename(i->path()) == "p1") { 679 i.pop(); 680 // FIXME: recursive_directory_iterator should be more robust. 681 if (i == e) break; 682 } 683 if (path::filename(i->path()) == "dontlookhere") 684 i.no_push(); 685 visited.push_back(path::filename(i->path())); 686 } 687 v_t::const_iterator a0 = find(visited, "a0"); 688 v_t::const_iterator aa1 = find(visited, "aa1"); 689 v_t::const_iterator ab1 = find(visited, "ab1"); 690 v_t::const_iterator dontlookhere = find(visited, "dontlookhere"); 691 v_t::const_iterator da1 = find(visited, "da1"); 692 v_t::const_iterator z0 = find(visited, "z0"); 693 v_t::const_iterator za1 = find(visited, "za1"); 694 v_t::const_iterator pop = find(visited, "pop"); 695 v_t::const_iterator p1 = find(visited, "p1"); 696 697 // Make sure that each path was visited correctly. 698 ASSERT_NE(a0, visited.end()); 699 ASSERT_NE(aa1, visited.end()); 700 ASSERT_NE(ab1, visited.end()); 701 ASSERT_NE(dontlookhere, visited.end()); 702 ASSERT_EQ(da1, visited.end()); // Not visited. 703 ASSERT_NE(z0, visited.end()); 704 ASSERT_NE(za1, visited.end()); 705 ASSERT_NE(pop, visited.end()); 706 ASSERT_EQ(p1, visited.end()); // Not visited. 707 708 // Make sure that parents were visited before children. No other ordering 709 // guarantees can be made across siblings. 710 ASSERT_LT(a0, aa1); 711 ASSERT_LT(a0, ab1); 712 ASSERT_LT(z0, za1); 713 714 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); 715 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); 716 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); 717 ASSERT_NO_ERROR( 718 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); 719 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); 720 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); 721 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); 722 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); 723 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); 724 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); 725 726 // Test recursive_directory_iterator level() 727 ASSERT_NO_ERROR( 728 fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c")); 729 fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E; 730 for (int l = 0; I != E; I.increment(ec), ++l) { 731 ASSERT_NO_ERROR(ec); 732 EXPECT_EQ(I.level(), l); 733 } 734 EXPECT_EQ(I, E); 735 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c")); 736 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b")); 737 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a")); 738 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel")); 739 } 740 741 const char archive[] = "!<arch>\x0A"; 742 const char bitcode[] = "\xde\xc0\x17\x0b"; 743 const char coff_object[] = "\x00\x00......"; 744 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......" 745 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8"; 746 const char coff_import_library[] = "\x00\x00\xff\xff...."; 747 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 748 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 749 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00"; 750 const char macho_object[] = 751 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............"; 752 const char macho_executable[] = 753 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............"; 754 const char macho_fixed_virtual_memory_shared_lib[] = 755 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............"; 756 const char macho_core[] = 757 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............"; 758 const char macho_preload_executable[] = 759 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............"; 760 const char macho_dynamically_linked_shared_lib[] = 761 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............"; 762 const char macho_dynamic_linker[] = 763 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............"; 764 const char macho_bundle[] = 765 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............"; 766 const char macho_dsym_companion[] = 767 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............"; 768 const char macho_kext_bundle[] = 769 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............"; 770 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; 771 const char macho_dynamically_linked_shared_lib_stub[] = 772 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............"; 773 774 TEST_F(FileSystemTest, Magic) { 775 struct type { 776 const char *filename; 777 const char *magic_str; 778 size_t magic_str_len; 779 fs::file_magic magic; 780 } types[] = { 781 #define DEFINE(magic) \ 782 { #magic, magic, sizeof(magic), fs::file_magic::magic } 783 DEFINE(archive), 784 DEFINE(bitcode), 785 DEFINE(coff_object), 786 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object }, 787 DEFINE(coff_import_library), 788 DEFINE(elf_relocatable), 789 DEFINE(macho_universal_binary), 790 DEFINE(macho_object), 791 DEFINE(macho_executable), 792 DEFINE(macho_fixed_virtual_memory_shared_lib), 793 DEFINE(macho_core), 794 DEFINE(macho_preload_executable), 795 DEFINE(macho_dynamically_linked_shared_lib), 796 DEFINE(macho_dynamic_linker), 797 DEFINE(macho_bundle), 798 DEFINE(macho_dynamically_linked_shared_lib_stub), 799 DEFINE(macho_dsym_companion), 800 DEFINE(macho_kext_bundle), 801 DEFINE(windows_resource) 802 #undef DEFINE 803 }; 804 805 // Create some files filled with magic. 806 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; 807 ++i) { 808 SmallString<128> file_pathname(TestDirectory); 809 path::append(file_pathname, i->filename); 810 std::error_code EC; 811 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None); 812 ASSERT_FALSE(file.has_error()); 813 StringRef magic(i->magic_str, i->magic_str_len); 814 file << magic; 815 file.close(); 816 EXPECT_EQ(i->magic, fs::identify_magic(magic)); 817 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); 818 } 819 } 820 821 #ifdef LLVM_ON_WIN32 822 TEST_F(FileSystemTest, CarriageReturn) { 823 SmallString<128> FilePathname(TestDirectory); 824 std::error_code EC; 825 path::append(FilePathname, "test"); 826 827 { 828 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text); 829 ASSERT_NO_ERROR(EC); 830 File << '\n'; 831 } 832 { 833 auto Buf = MemoryBuffer::getFile(FilePathname.str()); 834 EXPECT_TRUE((bool)Buf); 835 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n"); 836 } 837 838 { 839 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None); 840 ASSERT_NO_ERROR(EC); 841 File << '\n'; 842 } 843 { 844 auto Buf = MemoryBuffer::getFile(FilePathname.str()); 845 EXPECT_TRUE((bool)Buf); 846 EXPECT_EQ(Buf.get()->getBuffer(), "\n"); 847 } 848 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); 849 } 850 #endif 851 852 TEST_F(FileSystemTest, Resize) { 853 int FD; 854 SmallString<64> TempPath; 855 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); 856 ASSERT_NO_ERROR(fs::resize_file(FD, 123)); 857 fs::file_status Status; 858 ASSERT_NO_ERROR(fs::status(FD, Status)); 859 ASSERT_EQ(Status.getSize(), 123U); 860 ::close(FD); 861 ASSERT_NO_ERROR(fs::remove(TempPath)); 862 } 863 864 TEST_F(FileSystemTest, FileMapping) { 865 // Create a temp file. 866 int FileDescriptor; 867 SmallString<64> TempPath; 868 ASSERT_NO_ERROR( 869 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 870 unsigned Size = 4096; 871 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size)); 872 873 // Map in temp file and add some content 874 std::error_code EC; 875 StringRef Val("hello there"); 876 { 877 fs::mapped_file_region mfr(FileDescriptor, 878 fs::mapped_file_region::readwrite, Size, 0, EC); 879 ASSERT_NO_ERROR(EC); 880 std::copy(Val.begin(), Val.end(), mfr.data()); 881 // Explicitly add a 0. 882 mfr.data()[Val.size()] = 0; 883 // Unmap temp file 884 } 885 ASSERT_EQ(close(FileDescriptor), 0); 886 887 // Map it back in read-only 888 { 889 int FD; 890 EC = fs::openFileForRead(Twine(TempPath), FD); 891 ASSERT_NO_ERROR(EC); 892 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC); 893 ASSERT_NO_ERROR(EC); 894 895 // Verify content 896 EXPECT_EQ(StringRef(mfr.const_data()), Val); 897 898 // Unmap temp file 899 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC); 900 ASSERT_NO_ERROR(EC); 901 ASSERT_EQ(close(FD), 0); 902 } 903 ASSERT_NO_ERROR(fs::remove(TempPath)); 904 } 905 906 TEST(Support, NormalizePath) { 907 #if defined(LLVM_ON_WIN32) 908 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ 909 EXPECT_EQ(path__, windows__); 910 #else 911 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ 912 EXPECT_EQ(path__, not_windows__); 913 #endif 914 915 SmallString<64> Path1("a"); 916 SmallString<64> Path2("a/b"); 917 SmallString<64> Path3("a\\b"); 918 SmallString<64> Path4("a\\\\b"); 919 SmallString<64> Path5("\\a"); 920 SmallString<64> Path6("a\\"); 921 922 path::native(Path1); 923 EXPECT_PATH_IS(Path1, "a", "a"); 924 925 path::native(Path2); 926 EXPECT_PATH_IS(Path2, "a\\b", "a/b"); 927 928 path::native(Path3); 929 EXPECT_PATH_IS(Path3, "a\\b", "a/b"); 930 931 path::native(Path4); 932 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); 933 934 path::native(Path5); 935 EXPECT_PATH_IS(Path5, "\\a", "/a"); 936 937 path::native(Path6); 938 EXPECT_PATH_IS(Path6, "a\\", "a/"); 939 940 #undef EXPECT_PATH_IS 941 } 942 943 TEST(Support, RemoveLeadingDotSlash) { 944 StringRef Path1("././/foolz/wat"); 945 StringRef Path2("./////"); 946 947 Path1 = path::remove_leading_dotslash(Path1); 948 EXPECT_EQ(Path1, "foolz/wat"); 949 Path2 = path::remove_leading_dotslash(Path2); 950 EXPECT_EQ(Path2, ""); 951 } 952 953 static std::string remove_dots(StringRef path, 954 bool remove_dot_dot) { 955 SmallString<256> buffer(path); 956 path::remove_dots(buffer, remove_dot_dot); 957 return buffer.str(); 958 } 959 960 TEST(Support, RemoveDots) { 961 #if defined(LLVM_ON_WIN32) 962 EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false)); 963 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false)); 964 965 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false)); 966 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true)); 967 EXPECT_EQ("c", remove_dots(".\\.\\c", true)); 968 EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true)); 969 EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true)); 970 971 SmallString<64> Path1(".\\.\\c"); 972 EXPECT_TRUE(path::remove_dots(Path1, true)); 973 EXPECT_EQ("c", Path1); 974 #else 975 EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false)); 976 EXPECT_EQ("", remove_dots("./////", false)); 977 978 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false)); 979 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true)); 980 EXPECT_EQ("c", remove_dots("././c", true)); 981 EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true)); 982 EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true)); 983 984 SmallString<64> Path1("././c"); 985 EXPECT_TRUE(path::remove_dots(Path1, true)); 986 EXPECT_EQ("c", Path1); 987 #endif 988 } 989 990 TEST(Support, ReplacePathPrefix) { 991 SmallString<64> Path1("/foo"); 992 SmallString<64> Path2("/old/foo"); 993 SmallString<64> OldPrefix("/old"); 994 SmallString<64> NewPrefix("/new"); 995 SmallString<64> NewPrefix2("/longernew"); 996 SmallString<64> EmptyPrefix(""); 997 998 SmallString<64> Path = Path1; 999 path::replace_path_prefix(Path, OldPrefix, NewPrefix); 1000 EXPECT_EQ(Path, "/foo"); 1001 Path = Path2; 1002 path::replace_path_prefix(Path, OldPrefix, NewPrefix); 1003 EXPECT_EQ(Path, "/new/foo"); 1004 Path = Path2; 1005 path::replace_path_prefix(Path, OldPrefix, NewPrefix2); 1006 EXPECT_EQ(Path, "/longernew/foo"); 1007 Path = Path1; 1008 path::replace_path_prefix(Path, EmptyPrefix, NewPrefix); 1009 EXPECT_EQ(Path, "/new/foo"); 1010 Path = Path2; 1011 path::replace_path_prefix(Path, OldPrefix, EmptyPrefix); 1012 EXPECT_EQ(Path, "/foo"); 1013 } 1014 1015 TEST_F(FileSystemTest, PathFromFD) { 1016 // Create a temp file. 1017 int FileDescriptor; 1018 SmallString<64> TempPath; 1019 ASSERT_NO_ERROR( 1020 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 1021 FileRemover Cleanup(TempPath); 1022 1023 // Make sure it exists. 1024 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1025 1026 // Try to get the path from the file descriptor 1027 SmallString<64> ResultPath; 1028 std::error_code ErrorCode = 1029 fs::getPathFromOpenFD(FileDescriptor, ResultPath); 1030 1031 // If we succeeded, check that the paths are the same (modulo case): 1032 if (!ErrorCode) { 1033 // The paths returned by createTemporaryFile and getPathFromOpenFD 1034 // should reference the same file on disk. 1035 fs::UniqueID D1, D2; 1036 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1037 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1038 ASSERT_EQ(D1, D2); 1039 } 1040 1041 ::close(FileDescriptor); 1042 } 1043 1044 TEST_F(FileSystemTest, PathFromFDWin32) { 1045 // Create a temp file. 1046 int FileDescriptor; 1047 SmallString<64> TempPath; 1048 ASSERT_NO_ERROR( 1049 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 1050 FileRemover Cleanup(TempPath); 1051 1052 // Make sure it exists. 1053 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1054 1055 SmallVector<char, 8> ResultPath; 1056 std::error_code ErrorCode = 1057 fs::getPathFromOpenFD(FileDescriptor, ResultPath); 1058 1059 if (!ErrorCode) { 1060 // Now that we know how much space is required for the path, create a path 1061 // buffer with exactly enough space (sans null terminator, which should not 1062 // be present), and call getPathFromOpenFD again to ensure that the API 1063 // properly handles exactly-sized buffers. 1064 SmallVector<char, 8> ExactSizedPath(ResultPath.size()); 1065 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath); 1066 ResultPath = ExactSizedPath; 1067 } 1068 1069 if (!ErrorCode) { 1070 fs::UniqueID D1, D2; 1071 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1072 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1073 ASSERT_EQ(D1, D2); 1074 } 1075 ::close(FileDescriptor); 1076 } 1077 1078 TEST_F(FileSystemTest, PathFromFDUnicode) { 1079 // Create a temp file. 1080 int FileDescriptor; 1081 SmallString<64> TempPath; 1082 1083 // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0" 1084 ASSERT_NO_ERROR( 1085 fs::createTemporaryFile("\xCF\x80r\xC2\xB2", 1086 "\xE2\x84\xB5.0", FileDescriptor, TempPath)); 1087 FileRemover Cleanup(TempPath); 1088 1089 // Make sure it exists. 1090 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1091 1092 SmallVector<char, 8> ResultPath; 1093 std::error_code ErrorCode = 1094 fs::getPathFromOpenFD(FileDescriptor, ResultPath); 1095 1096 if (!ErrorCode) { 1097 fs::UniqueID D1, D2; 1098 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1099 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1100 ASSERT_EQ(D1, D2); 1101 } 1102 ::close(FileDescriptor); 1103 } 1104 1105 TEST_F(FileSystemTest, OpenFileForRead) { 1106 // Create a temp file. 1107 int FileDescriptor; 1108 SmallString<64> TempPath; 1109 ASSERT_NO_ERROR( 1110 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 1111 FileRemover Cleanup(TempPath); 1112 1113 // Make sure it exists. 1114 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1115 1116 // Open the file for read 1117 int FileDescriptor2; 1118 SmallString<64> ResultPath; 1119 ASSERT_NO_ERROR( 1120 fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath)) 1121 1122 // If we succeeded, check that the paths are the same (modulo case): 1123 if (!ResultPath.empty()) { 1124 // The paths returned by createTemporaryFile and getPathFromOpenFD 1125 // should reference the same file on disk. 1126 fs::UniqueID D1, D2; 1127 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1128 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1129 ASSERT_EQ(D1, D2); 1130 } 1131 1132 ::close(FileDescriptor); 1133 } 1134 } // anonymous namespace 1135