comparison unittests/Support/Path.cpp @ 120:1172e4bd9c6f

update 4.0.0
author mir3636
date Fri, 25 Nov 2016 19:14:25 +0900
parents 7d135dc70f03
children 803732b1fca8
comparison
equal deleted inserted replaced
101:34baf5011add 120:1172e4bd9c6f
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "llvm/Support/Path.h" 10 #include "llvm/Support/Path.h"
11 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Support/ConvertUTF.h" 12 #include "llvm/Support/ConvertUTF.h"
12 #include "llvm/Support/Errc.h" 13 #include "llvm/Support/Errc.h"
13 #include "llvm/Support/ErrorHandling.h" 14 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/FileSystem.h" 15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/FileUtilities.h"
15 #include "llvm/Support/MemoryBuffer.h" 17 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/raw_ostream.h" 18 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h" 19 #include "gtest/gtest.h"
18 20
19 #ifdef LLVM_ON_WIN32 21 #ifdef LLVM_ON_WIN32
22 #include "llvm/ADT/ArrayRef.h"
20 #include <windows.h> 23 #include <windows.h>
21 #include <winerror.h> 24 #include <winerror.h>
22 #endif 25 #endif
23 26
24 #ifdef LLVM_ON_UNIX 27 #ifdef LLVM_ON_UNIX
117 ASSERT_TRUE(*ci == ComponentStack.back()); 120 ASSERT_TRUE(*ci == ComponentStack.back());
118 ComponentStack.pop_back(); 121 ComponentStack.pop_back();
119 } 122 }
120 ASSERT_TRUE(ComponentStack.empty()); 123 ASSERT_TRUE(ComponentStack.empty());
121 124
122 path::has_root_path(*i); 125 // Crash test most of the API - since we're iterating over all of our paths
123 path::root_path(*i); 126 // here there isn't really anything reasonable to assert on in the results.
124 path::has_root_name(*i); 127 (void)path::has_root_path(*i);
125 path::root_name(*i); 128 (void)path::root_path(*i);
126 path::has_root_directory(*i); 129 (void)path::has_root_name(*i);
127 path::root_directory(*i); 130 (void)path::root_name(*i);
128 path::has_parent_path(*i); 131 (void)path::has_root_directory(*i);
129 path::parent_path(*i); 132 (void)path::root_directory(*i);
130 path::has_filename(*i); 133 (void)path::has_parent_path(*i);
131 path::filename(*i); 134 (void)path::parent_path(*i);
132 path::has_stem(*i); 135 (void)path::has_filename(*i);
133 path::stem(*i); 136 (void)path::filename(*i);
134 path::has_extension(*i); 137 (void)path::has_stem(*i);
135 path::extension(*i); 138 (void)path::stem(*i);
136 path::is_absolute(*i); 139 (void)path::has_extension(*i);
137 path::is_relative(*i); 140 (void)path::extension(*i);
141 (void)path::is_absolute(*i);
142 (void)path::is_relative(*i);
138 143
139 SmallString<128> temp_store; 144 SmallString<128> temp_store;
140 temp_store = *i; 145 temp_store = *i;
141 ASSERT_NO_ERROR(fs::make_absolute(temp_store)); 146 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
142 temp_store = *i; 147 temp_store = *i;
414 419
415 // Test evn var / path with 260 chars. 420 // Test evn var / path with 260 chars.
416 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"}; 421 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
417 while (Expected.size() < 260) 422 while (Expected.size() < 260)
418 Expected.append("\\DirNameWith19Charss"); 423 Expected.append("\\DirNameWith19Charss");
419 ASSERT_EQ(260, Expected.size()); 424 ASSERT_EQ(260U, Expected.size());
420 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str()); 425 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
421 } 426 }
422 #endif 427 #endif
423 428
424 class FileSystemTest : public testing::Test { 429 class FileSystemTest : public testing::Test {
483 SmallString<128> Dir2; 488 SmallString<128> Dir2;
484 ASSERT_NO_ERROR( 489 ASSERT_NO_ERROR(
485 fs::createUniqueDirectory("dir2", Dir2)); 490 fs::createUniqueDirectory("dir2", Dir2));
486 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); 491 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
487 ASSERT_NE(F1, F2); 492 ASSERT_NE(F1, F2);
493 ASSERT_NO_ERROR(fs::remove(Dir1));
494 ASSERT_NO_ERROR(fs::remove(Dir2));
495 ASSERT_NO_ERROR(fs::remove(TempPath2));
496 ASSERT_NO_ERROR(fs::remove(TempPath));
488 } 497 }
489 498
490 TEST_F(FileSystemTest, TempFiles) { 499 TEST_F(FileSystemTest, TempFiles) {
491 // Create a temp file. 500 // Create a temp file.
492 int FileDescriptor; 501 int FileDescriptor;
526 errc::no_such_file_or_directory); 535 errc::no_such_file_or_directory);
527 536
528 SmallString<64> TempPath3; 537 SmallString<64> TempPath3;
529 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); 538 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
530 ASSERT_FALSE(TempPath3.endswith(".")); 539 ASSERT_FALSE(TempPath3.endswith("."));
540 FileRemover Cleanup3(TempPath3);
531 541
532 // Create a hard link to Temp1. 542 // Create a hard link to Temp1.
533 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); 543 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
534 bool equal; 544 bool equal;
535 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); 545 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
674 } 684 }
675 if (path::filename(i->path()) == "dontlookhere") 685 if (path::filename(i->path()) == "dontlookhere")
676 i.no_push(); 686 i.no_push();
677 visited.push_back(path::filename(i->path())); 687 visited.push_back(path::filename(i->path()));
678 } 688 }
679 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0"); 689 v_t::const_iterator a0 = find(visited, "a0");
680 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1"); 690 v_t::const_iterator aa1 = find(visited, "aa1");
681 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1"); 691 v_t::const_iterator ab1 = find(visited, "ab1");
682 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(), 692 v_t::const_iterator dontlookhere = find(visited, "dontlookhere");
683 "dontlookhere"); 693 v_t::const_iterator da1 = find(visited, "da1");
684 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1"); 694 v_t::const_iterator z0 = find(visited, "z0");
685 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0"); 695 v_t::const_iterator za1 = find(visited, "za1");
686 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1"); 696 v_t::const_iterator pop = find(visited, "pop");
687 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop"); 697 v_t::const_iterator p1 = find(visited, "p1");
688 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
689 698
690 // Make sure that each path was visited correctly. 699 // Make sure that each path was visited correctly.
691 ASSERT_NE(a0, visited.end()); 700 ASSERT_NE(a0, visited.end());
692 ASSERT_NE(aa1, visited.end()); 701 ASSERT_NE(aa1, visited.end());
693 ASSERT_NE(ab1, visited.end()); 702 ASSERT_NE(ab1, visited.end());
713 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); 722 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
714 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); 723 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
715 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); 724 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
716 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); 725 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
717 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); 726 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
727
728 // Test recursive_directory_iterator level()
729 ASSERT_NO_ERROR(
730 fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c"));
731 fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E;
732 for (int l = 0; I != E; I.increment(ec), ++l) {
733 ASSERT_NO_ERROR(ec);
734 EXPECT_EQ(I.level(), l);
735 }
736 EXPECT_EQ(I, E);
737 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c"));
738 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b"));
739 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a"));
740 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel"));
718 } 741 }
719 742
720 const char archive[] = "!<arch>\x0A"; 743 const char archive[] = "!<arch>\x0A";
721 const char bitcode[] = "\xde\xc0\x17\x0b"; 744 const char bitcode[] = "\xde\xc0\x17\x0b";
722 const char coff_object[] = "\x00\x00......"; 745 const char coff_object[] = "\x00\x00......";
723 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......" 746 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
724 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8"; 747 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
725 const char coff_import_library[] = "\x00\x00\xff\xff...."; 748 const char coff_import_library[] = "\x00\x00\xff\xff....";
726 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 749 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
727 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 750 0, 0, 0, 0, 0, 0, 0, 0, 1 };
728 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00"; 751 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
729 const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01"; 752 const char macho_object[] =
730 const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02"; 753 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
754 const char macho_executable[] =
755 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
731 const char macho_fixed_virtual_memory_shared_lib[] = 756 const char macho_fixed_virtual_memory_shared_lib[] =
732 "\xfe\xed\xfa\xce..........\x00\x03"; 757 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
733 const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04"; 758 const char macho_core[] =
734 const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05"; 759 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
760 const char macho_preload_executable[] =
761 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
735 const char macho_dynamically_linked_shared_lib[] = 762 const char macho_dynamically_linked_shared_lib[] =
736 "\xfe\xed\xfa\xce..........\x00\x06"; 763 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
737 const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07"; 764 const char macho_dynamic_linker[] =
738 const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08"; 765 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
739 const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a"; 766 const char macho_bundle[] =
740 const char macho_kext_bundle[] = "\xfe\xed\xfa\xce..........\x00\x0b"; 767 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
768 const char macho_dsym_companion[] =
769 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
770 const char macho_kext_bundle[] =
771 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
741 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; 772 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
742 const char macho_dynamically_linked_shared_lib_stub[] = 773 const char macho_dynamically_linked_shared_lib_stub[] =
743 "\xfe\xed\xfa\xce..........\x00\x09"; 774 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
744 775
745 TEST_F(FileSystemTest, Magic) { 776 TEST_F(FileSystemTest, Magic) {
746 struct type { 777 struct type {
747 const char *filename; 778 const char *filename;
748 const char *magic_str; 779 const char *magic_str;
826 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); 857 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
827 ASSERT_NO_ERROR(fs::resize_file(FD, 123)); 858 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
828 fs::file_status Status; 859 fs::file_status Status;
829 ASSERT_NO_ERROR(fs::status(FD, Status)); 860 ASSERT_NO_ERROR(fs::status(FD, Status));
830 ASSERT_EQ(Status.getSize(), 123U); 861 ASSERT_EQ(Status.getSize(), 123U);
862 ::close(FD);
863 ASSERT_NO_ERROR(fs::remove(TempPath));
831 } 864 }
832 865
833 TEST_F(FileSystemTest, FileMapping) { 866 TEST_F(FileSystemTest, FileMapping) {
834 // Create a temp file. 867 // Create a temp file.
835 int FileDescriptor; 868 int FileDescriptor;
849 std::copy(Val.begin(), Val.end(), mfr.data()); 882 std::copy(Val.begin(), Val.end(), mfr.data());
850 // Explicitly add a 0. 883 // Explicitly add a 0.
851 mfr.data()[Val.size()] = 0; 884 mfr.data()[Val.size()] = 0;
852 // Unmap temp file 885 // Unmap temp file
853 } 886 }
887 ASSERT_EQ(close(FileDescriptor), 0);
854 888
855 // Map it back in read-only 889 // Map it back in read-only
856 int FD; 890 {
857 EC = fs::openFileForRead(Twine(TempPath), FD); 891 int FD;
858 ASSERT_NO_ERROR(EC); 892 EC = fs::openFileForRead(Twine(TempPath), FD);
859 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC); 893 ASSERT_NO_ERROR(EC);
860 ASSERT_NO_ERROR(EC); 894 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
861 895 ASSERT_NO_ERROR(EC);
862 // Verify content 896
863 EXPECT_EQ(StringRef(mfr.const_data()), Val); 897 // Verify content
864 898 EXPECT_EQ(StringRef(mfr.const_data()), Val);
865 // Unmap temp file 899
866 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC); 900 // Unmap temp file
867 ASSERT_NO_ERROR(EC); 901 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
868 ASSERT_EQ(close(FD), 0); 902 ASSERT_NO_ERROR(EC);
903 ASSERT_EQ(close(FD), 0);
904 }
905 ASSERT_NO_ERROR(fs::remove(TempPath));
869 } 906 }
870 907
871 TEST(Support, NormalizePath) { 908 TEST(Support, NormalizePath) {
872 #if defined(LLVM_ON_WIN32) 909 #if defined(LLVM_ON_WIN32)
873 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ 910 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
928 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false)); 965 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false));
929 966
930 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false)); 967 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false));
931 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true)); 968 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true));
932 EXPECT_EQ("c", remove_dots(".\\.\\c", true)); 969 EXPECT_EQ("c", remove_dots(".\\.\\c", true));
970 EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true));
971 EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true));
933 972
934 SmallString<64> Path1(".\\.\\c"); 973 SmallString<64> Path1(".\\.\\c");
935 EXPECT_TRUE(path::remove_dots(Path1, true)); 974 EXPECT_TRUE(path::remove_dots(Path1, true));
936 EXPECT_EQ("c", Path1); 975 EXPECT_EQ("c", Path1);
937 #else 976 #else
939 EXPECT_EQ("", remove_dots("./////", false)); 978 EXPECT_EQ("", remove_dots("./////", false));
940 979
941 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false)); 980 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false));
942 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true)); 981 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true));
943 EXPECT_EQ("c", remove_dots("././c", true)); 982 EXPECT_EQ("c", remove_dots("././c", true));
983 EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true));
984 EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true));
985 EXPECT_EQ("/a/c", remove_dots("/../../a/c", true));
986 EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true));
944 987
945 SmallString<64> Path1("././c"); 988 SmallString<64> Path1("././c");
946 EXPECT_TRUE(path::remove_dots(Path1, true)); 989 EXPECT_TRUE(path::remove_dots(Path1, true));
947 EXPECT_EQ("c", Path1); 990 EXPECT_EQ("c", Path1);
948 #endif 991 #endif
949 } 992 }
993
994 TEST(Support, ReplacePathPrefix) {
995 SmallString<64> Path1("/foo");
996 SmallString<64> Path2("/old/foo");
997 SmallString<64> OldPrefix("/old");
998 SmallString<64> NewPrefix("/new");
999 SmallString<64> NewPrefix2("/longernew");
1000 SmallString<64> EmptyPrefix("");
1001
1002 SmallString<64> Path = Path1;
1003 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1004 EXPECT_EQ(Path, "/foo");
1005 Path = Path2;
1006 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1007 EXPECT_EQ(Path, "/new/foo");
1008 Path = Path2;
1009 path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
1010 EXPECT_EQ(Path, "/longernew/foo");
1011 Path = Path1;
1012 path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
1013 EXPECT_EQ(Path, "/new/foo");
1014 Path = Path2;
1015 path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
1016 EXPECT_EQ(Path, "/foo");
1017 }
1018
1019 TEST_F(FileSystemTest, PathFromFD) {
1020 // Create a temp file.
1021 int FileDescriptor;
1022 SmallString<64> TempPath;
1023 ASSERT_NO_ERROR(
1024 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1025 FileRemover Cleanup(TempPath);
1026
1027 // Make sure it exists.
1028 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1029
1030 // Try to get the path from the file descriptor
1031 SmallString<64> ResultPath;
1032 std::error_code ErrorCode =
1033 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1034
1035 // If we succeeded, check that the paths are the same (modulo case):
1036 if (!ErrorCode) {
1037 // The paths returned by createTemporaryFile and getPathFromOpenFD
1038 // should reference the same file on disk.
1039 fs::UniqueID D1, D2;
1040 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1041 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1042 ASSERT_EQ(D1, D2);
1043 }
1044
1045 ::close(FileDescriptor);
1046 }
1047
1048 TEST_F(FileSystemTest, PathFromFDWin32) {
1049 // Create a temp file.
1050 int FileDescriptor;
1051 SmallString<64> TempPath;
1052 ASSERT_NO_ERROR(
1053 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1054 FileRemover Cleanup(TempPath);
1055
1056 // Make sure it exists.
1057 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1058
1059 SmallVector<char, 8> ResultPath;
1060 std::error_code ErrorCode =
1061 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1062
1063 if (!ErrorCode) {
1064 // Now that we know how much space is required for the path, create a path
1065 // buffer with exactly enough space (sans null terminator, which should not
1066 // be present), and call getPathFromOpenFD again to ensure that the API
1067 // properly handles exactly-sized buffers.
1068 SmallVector<char, 8> ExactSizedPath(ResultPath.size());
1069 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath);
1070 ResultPath = ExactSizedPath;
1071 }
1072
1073 if (!ErrorCode) {
1074 fs::UniqueID D1, D2;
1075 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1076 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1077 ASSERT_EQ(D1, D2);
1078 }
1079 ::close(FileDescriptor);
1080 }
1081
1082 TEST_F(FileSystemTest, PathFromFDUnicode) {
1083 // Create a temp file.
1084 int FileDescriptor;
1085 SmallString<64> TempPath;
1086
1087 // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0"
1088 ASSERT_NO_ERROR(
1089 fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
1090 "\xE2\x84\xB5.0", FileDescriptor, TempPath));
1091 FileRemover Cleanup(TempPath);
1092
1093 // Make sure it exists.
1094 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1095
1096 SmallVector<char, 8> ResultPath;
1097 std::error_code ErrorCode =
1098 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1099
1100 if (!ErrorCode) {
1101 fs::UniqueID D1, D2;
1102 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1103 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1104 ASSERT_EQ(D1, D2);
1105 }
1106 ::close(FileDescriptor);
1107 }
1108
1109 TEST_F(FileSystemTest, OpenFileForRead) {
1110 // Create a temp file.
1111 int FileDescriptor;
1112 SmallString<64> TempPath;
1113 ASSERT_NO_ERROR(
1114 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1115 FileRemover Cleanup(TempPath);
1116
1117 // Make sure it exists.
1118 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1119
1120 // Open the file for read
1121 int FileDescriptor2;
1122 SmallString<64> ResultPath;
1123 ASSERT_NO_ERROR(
1124 fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath))
1125
1126 // If we succeeded, check that the paths are the same (modulo case):
1127 if (!ResultPath.empty()) {
1128 // The paths returned by createTemporaryFile and getPathFromOpenFD
1129 // should reference the same file on disk.
1130 fs::UniqueID D1, D2;
1131 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1132 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1133 ASSERT_EQ(D1, D2);
1134 }
1135
1136 ::close(FileDescriptor);
1137 }
950 } // anonymous namespace 1138 } // anonymous namespace