Michael J. Spencer | 013d15a | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | a11c3e2 | 2013-06-11 22:21:28 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Path.h" |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 11 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | c9c9825 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 13 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 18 | using namespace llvm::sys; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 19 | |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 20 | #define ASSERT_NO_ERROR(x) \ |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 21 | if (error_code ASSERT_NO_ERROR_ec = x) { \ |
| 22 | SmallString<128> MessageStorage; \ |
| 23 | raw_svector_ostream Message(MessageStorage); \ |
| 24 | Message << #x ": did not return errc::success.\n" \ |
| 25 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 26 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 27 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 28 | } else {} |
| 29 | |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
Zhanyong Wan | 63cc3a8 | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 32 | TEST(is_separator, Works) { |
| 33 | EXPECT_TRUE(path::is_separator('/')); |
| 34 | EXPECT_FALSE(path::is_separator('\0')); |
| 35 | EXPECT_FALSE(path::is_separator('-')); |
| 36 | EXPECT_FALSE(path::is_separator(' ')); |
| 37 | |
| 38 | #ifdef LLVM_ON_WIN32 |
| 39 | EXPECT_TRUE(path::is_separator('\\')); |
| 40 | #else |
| 41 | EXPECT_FALSE(path::is_separator('\\')); |
| 42 | #endif |
| 43 | } |
| 44 | |
Michael J. Spencer | 013d15a | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 45 | TEST(Support, Path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 46 | SmallVector<StringRef, 40> paths; |
| 47 | paths.push_back(""); |
| 48 | paths.push_back("."); |
| 49 | paths.push_back(".."); |
| 50 | paths.push_back("foo"); |
| 51 | paths.push_back("/"); |
| 52 | paths.push_back("/foo"); |
| 53 | paths.push_back("foo/"); |
| 54 | paths.push_back("/foo/"); |
| 55 | paths.push_back("foo/bar"); |
| 56 | paths.push_back("/foo/bar"); |
| 57 | paths.push_back("//net"); |
| 58 | paths.push_back("//net/foo"); |
| 59 | paths.push_back("///foo///"); |
| 60 | paths.push_back("///foo///bar"); |
| 61 | paths.push_back("/."); |
| 62 | paths.push_back("./"); |
| 63 | paths.push_back("/.."); |
| 64 | paths.push_back("../"); |
| 65 | paths.push_back("foo/."); |
| 66 | paths.push_back("foo/.."); |
| 67 | paths.push_back("foo/./"); |
| 68 | paths.push_back("foo/./bar"); |
| 69 | paths.push_back("foo/.."); |
| 70 | paths.push_back("foo/../"); |
| 71 | paths.push_back("foo/../bar"); |
| 72 | paths.push_back("c:"); |
| 73 | paths.push_back("c:/"); |
| 74 | paths.push_back("c:foo"); |
| 75 | paths.push_back("c:/foo"); |
| 76 | paths.push_back("c:foo/"); |
| 77 | paths.push_back("c:/foo/"); |
| 78 | paths.push_back("c:/foo/bar"); |
| 79 | paths.push_back("prn:"); |
| 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/"); |
| 86 | paths.push_back("c:/foo\\bar"); |
| 87 | |
| 88 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 89 | e = paths.end(); |
| 90 | i != e; |
| 91 | ++i) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 92 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 93 | ce = sys::path::end(*i); |
| 94 | ci != ce; |
| 95 | ++ci) { |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 96 | ASSERT_FALSE(ci->empty()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 97 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 98 | |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 99 | #if 0 // Valgrind is whining about this. |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 100 | outs() << " Reverse Iteration: ["; |
| 101 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 102 | ce = sys::path::rend(*i); |
| 103 | ci != ce; |
| 104 | ++ci) { |
| 105 | outs() << *ci << ','; |
| 106 | } |
| 107 | outs() << "]\n"; |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 108 | #endif |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 109 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 110 | path::has_root_path(*i); |
| 111 | path::root_path(*i); |
| 112 | path::has_root_name(*i); |
| 113 | path::root_name(*i); |
| 114 | path::has_root_directory(*i); |
| 115 | path::root_directory(*i); |
| 116 | path::has_parent_path(*i); |
| 117 | path::parent_path(*i); |
| 118 | path::has_filename(*i); |
| 119 | path::filename(*i); |
| 120 | path::has_stem(*i); |
| 121 | path::stem(*i); |
| 122 | path::has_extension(*i); |
| 123 | path::extension(*i); |
| 124 | path::is_absolute(*i); |
| 125 | path::is_relative(*i); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 126 | |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 127 | SmallString<128> temp_store; |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 128 | temp_store = *i; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 129 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 130 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 131 | path::remove_filename(temp_store); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 132 | |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 133 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 134 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 135 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 136 | stem = path::stem(filename); |
| 137 | ext = path::extension(filename); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 138 | EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); |
| 139 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 140 | path::native(*i, temp_store); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 141 | } |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 142 | } |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 143 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 144 | class FileSystemTest : public testing::Test { |
| 145 | protected: |
| 146 | /// Unique temporary directory in which all created filesystem entities must |
| 147 | /// be placed. It is recursively removed at the end of each test. |
| 148 | SmallString<128> TestDirectory; |
| 149 | |
| 150 | virtual void SetUp() { |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 151 | ASSERT_NO_ERROR( |
Rafael Espindola | 08ddd12 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 152 | fs::createUniqueDirectory("file-system-test", TestDirectory)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 153 | // We don't care about this specific file. |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 154 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 155 | errs().flush(); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | virtual void TearDown() { |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 159 | uint32_t removed; |
| 160 | ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
Aaron Ballman | 4655485 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 164 | TEST_F(FileSystemTest, Unique) { |
| 165 | // Create a temp file. |
| 166 | int FileDescriptor; |
| 167 | SmallString<64> TempPath; |
| 168 | ASSERT_NO_ERROR( |
Rafael Espindola | 1276b39 | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 169 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Aaron Ballman | 4655485 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 170 | |
| 171 | // The same file should return an identical unique id. |
Rafael Espindola | 3ed45fe | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 172 | fs::UniqueID F1, F2; |
Rafael Espindola | 9aa3365 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 173 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); |
| 174 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); |
Aaron Ballman | 4655485 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 175 | ASSERT_EQ(F1, F2); |
| 176 | |
| 177 | // Different files should return different unique ids. |
| 178 | int FileDescriptor2; |
| 179 | SmallString<64> TempPath2; |
| 180 | ASSERT_NO_ERROR( |
Rafael Espindola | 1276b39 | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 181 | fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); |
| 182 | |
Rafael Espindola | 3ed45fe | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 183 | fs::UniqueID D; |
Rafael Espindola | 9aa3365 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 184 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); |
Aaron Ballman | 4655485 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 185 | ASSERT_NE(D, F1); |
| 186 | ::close(FileDescriptor2); |
| 187 | |
| 188 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 189 | |
| 190 | // Two paths representing the same file on disk should still provide the |
| 191 | // same unique id. We can test this by making a hard link. |
| 192 | ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); |
Rafael Espindola | 3ed45fe | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 193 | fs::UniqueID D2; |
Rafael Espindola | 9aa3365 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 194 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); |
Aaron Ballman | 4655485 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 195 | ASSERT_EQ(D2, F1); |
| 196 | |
| 197 | ::close(FileDescriptor); |
Rafael Espindola | f4ab63f | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 198 | |
| 199 | SmallString<128> Dir1; |
| 200 | ASSERT_NO_ERROR( |
| 201 | fs::createUniqueDirectory("dir1", Dir1)); |
| 202 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); |
| 203 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); |
| 204 | ASSERT_EQ(F1, F2); |
| 205 | |
| 206 | SmallString<128> Dir2; |
| 207 | ASSERT_NO_ERROR( |
| 208 | fs::createUniqueDirectory("dir2", Dir2)); |
| 209 | ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); |
| 210 | ASSERT_NE(F1, F2); |
Aaron Ballman | 4655485 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 213 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 214 | // Create a temp file. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 215 | int FileDescriptor; |
| 216 | SmallString<64> TempPath; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 217 | ASSERT_NO_ERROR( |
Rafael Espindola | 1276b39 | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 218 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 219 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 220 | // Make sure it exists. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 221 | bool TempFileExists; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 222 | ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 223 | EXPECT_TRUE(TempFileExists); |
| 224 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 225 | // Create another temp tile. |
| 226 | int FD2; |
| 227 | SmallString<64> TempPath2; |
Rafael Espindola | 1276b39 | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 228 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); |
Rafael Espindola | 268f400 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 229 | ASSERT_TRUE(TempPath2.endswith(".temp")); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 230 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 231 | |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 232 | fs::file_status A, B; |
| 233 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 234 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 235 | EXPECT_FALSE(fs::equivalent(A, B)); |
| 236 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 237 | ::close(FD2); |
Rafael Espindola | 1c881d9 | 2013-07-18 03:29:51 +0000 | [diff] [blame] | 238 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 239 | // Remove Temp2. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 240 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 241 | EXPECT_TRUE(TempFileExists); |
| 242 | |
Rafael Espindola | 9ce8b28 | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 243 | error_code EC = fs::status(TempPath2.c_str(), B); |
| 244 | EXPECT_EQ(EC, errc::no_such_file_or_directory); |
| 245 | EXPECT_EQ(B.type(), fs::file_type::file_not_found); |
| 246 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 247 | // Make sure Temp2 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 248 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 249 | EXPECT_FALSE(TempFileExists); |
| 250 | |
Rafael Espindola | 268f400 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 251 | SmallString<64> TempPath3; |
| 252 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); |
| 253 | ASSERT_FALSE(TempPath3.endswith(".")); |
| 254 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 255 | // Create a hard link to Temp1. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 256 | ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 257 | bool equal; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 258 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 259 | EXPECT_TRUE(equal); |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 260 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 261 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 262 | EXPECT_TRUE(fs::equivalent(A, B)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 263 | |
| 264 | // Remove Temp1. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 265 | ::close(FileDescriptor); |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 266 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 106aa73 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 267 | EXPECT_TRUE(TempFileExists); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 268 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 269 | // Remove the hard link. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 270 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 271 | EXPECT_TRUE(TempFileExists); |
| 272 | |
| 273 | // Make sure Temp1 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 274 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); |
Benjamin Kramer | 6d6d16a | 2010-12-03 12:33:32 +0000 | [diff] [blame] | 275 | EXPECT_FALSE(TempFileExists); |
Aaron Ballman | 551152f | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 276 | |
| 277 | #ifdef LLVM_ON_WIN32 |
| 278 | // Path name > 260 chars should get an error. |
| 279 | const char *Path270 = |
| 280 | "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" |
| 281 | "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" |
| 282 | "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" |
| 283 | "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" |
| 284 | "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; |
Rafael Espindola | 2c87a17 | 2013-07-05 22:32:33 +0000 | [diff] [blame] | 285 | EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath), |
Aaron Ballman | 551152f | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 286 | windows_error::path_not_found); |
| 287 | #endif |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 288 | } |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 289 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 290 | TEST_F(FileSystemTest, DirectoryIteration) { |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 291 | error_code ec; |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 292 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 293 | ASSERT_NO_ERROR(ec); |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 294 | |
| 295 | // Create a known hierarchy to recurse over. |
| 296 | bool existed; |
| 297 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 298 | + "/recursive/a0/aa1", existed)); |
| 299 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 300 | + "/recursive/a0/ab1", existed)); |
| 301 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 302 | + "/recursive/dontlookhere/da1", existed)); |
| 303 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 304 | + "/recursive/z0/za1", existed)); |
| 305 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 306 | + "/recursive/pop/p1", existed)); |
| 307 | typedef std::vector<std::string> v_t; |
| 308 | v_t visited; |
| 309 | for (fs::recursive_directory_iterator i(Twine(TestDirectory) |
| 310 | + "/recursive", ec), e; i != e; i.increment(ec)){ |
| 311 | ASSERT_NO_ERROR(ec); |
NAKAMURA Takumi | 52ee230 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 312 | if (path::filename(i->path()) == "p1") { |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 313 | i.pop(); |
NAKAMURA Takumi | 52ee230 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 314 | // FIXME: recursive_directory_iterator should be more robust. |
| 315 | if (i == e) break; |
| 316 | } |
Michael J. Spencer | bd3825e | 2011-12-09 01:14:41 +0000 | [diff] [blame] | 317 | if (path::filename(i->path()) == "dontlookhere") |
| 318 | i.no_push(); |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 319 | visited.push_back(path::filename(i->path())); |
| 320 | } |
| 321 | v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0"); |
| 322 | v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1"); |
| 323 | v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1"); |
| 324 | v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(), |
| 325 | "dontlookhere"); |
| 326 | v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1"); |
| 327 | v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0"); |
| 328 | v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1"); |
| 329 | v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop"); |
| 330 | v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1"); |
| 331 | |
| 332 | // Make sure that each path was visited correctly. |
| 333 | ASSERT_NE(a0, visited.end()); |
| 334 | ASSERT_NE(aa1, visited.end()); |
| 335 | ASSERT_NE(ab1, visited.end()); |
| 336 | ASSERT_NE(dontlookhere, visited.end()); |
| 337 | ASSERT_EQ(da1, visited.end()); // Not visited. |
| 338 | ASSERT_NE(z0, visited.end()); |
| 339 | ASSERT_NE(za1, visited.end()); |
| 340 | ASSERT_NE(pop, visited.end()); |
| 341 | ASSERT_EQ(p1, visited.end()); // Not visited. |
| 342 | |
| 343 | // Make sure that parents were visited before children. No other ordering |
| 344 | // guarantees can be made across siblings. |
| 345 | ASSERT_LT(a0, aa1); |
| 346 | ASSERT_LT(a0, ab1); |
| 347 | ASSERT_LT(z0, za1); |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 348 | } |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 349 | |
Michael J. Spencer | c0a74e2 | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 350 | const char elf[] = {0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
| 351 | |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 352 | TEST_F(FileSystemTest, Magic) { |
| 353 | struct type { |
| 354 | const char *filename; |
| 355 | const char *magic_str; |
Michael J. Spencer | c0a74e2 | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 356 | size_t magic_str_len; |
| 357 | fs::file_magic magic; |
| 358 | } types [] = { |
| 359 | {"magic.archive", "!<arch>\x0A", 8, fs::file_magic::archive}, |
| 360 | {"magic.elf", elf, sizeof(elf), |
| 361 | fs::file_magic::elf_relocatable} |
| 362 | }; |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 363 | |
| 364 | // Create some files filled with magic. |
| 365 | for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; |
| 366 | ++i) { |
| 367 | SmallString<128> file_pathname(TestDirectory); |
| 368 | path::append(file_pathname, i->filename); |
| 369 | std::string ErrMsg; |
Rafael Espindola | c1b49b5 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 370 | raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_Binary); |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 371 | ASSERT_FALSE(file.has_error()); |
| 372 | StringRef magic(i->magic_str, i->magic_str_len); |
| 373 | file << magic; |
Michael J. Spencer | 248f9f2 | 2011-01-15 18:52:49 +0000 | [diff] [blame] | 374 | file.close(); |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 375 | bool res = false; |
| 376 | ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res)); |
| 377 | EXPECT_TRUE(res); |
Michael J. Spencer | c0a74e2 | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 378 | EXPECT_EQ(i->magic, fs::identify_magic(magic)); |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 379 | } |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Rafael Espindola | c9c9825 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 382 | #ifdef LLVM_ON_WIN32 |
| 383 | TEST_F(FileSystemTest, CarriageReturn) { |
| 384 | SmallString<128> FilePathname(TestDirectory); |
| 385 | std::string ErrMsg; |
| 386 | path::append(FilePathname, "test"); |
| 387 | |
| 388 | { |
| 389 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg); |
| 390 | EXPECT_EQ(ErrMsg, ""); |
| 391 | File << '\n'; |
| 392 | } |
| 393 | { |
| 394 | OwningPtr<MemoryBuffer> Buf; |
| 395 | MemoryBuffer::getFile(FilePathname, Buf); |
| 396 | EXPECT_EQ(Buf->getBuffer(), "\r\n"); |
| 397 | } |
| 398 | |
| 399 | { |
| 400 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Binary); |
| 401 | EXPECT_EQ(ErrMsg, ""); |
| 402 | File << '\n'; |
| 403 | } |
| 404 | { |
| 405 | OwningPtr<MemoryBuffer> Buf; |
| 406 | MemoryBuffer::getFile(FilePathname, Buf); |
| 407 | EXPECT_EQ(Buf->getBuffer(), "\n"); |
| 408 | } |
| 409 | } |
| 410 | #endif |
| 411 | |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 412 | TEST_F(FileSystemTest, FileMapping) { |
| 413 | // Create a temp file. |
| 414 | int FileDescriptor; |
| 415 | SmallString<64> TempPath; |
| 416 | ASSERT_NO_ERROR( |
Rafael Espindola | 1276b39 | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 417 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 418 | // Map in temp file and add some content |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 419 | error_code EC; |
| 420 | StringRef Val("hello there"); |
| 421 | { |
| 422 | fs::mapped_file_region mfr(FileDescriptor, |
Michael J. Spencer | d483d28 | 2013-03-14 00:33:37 +0000 | [diff] [blame] | 423 | true, |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 424 | fs::mapped_file_region::readwrite, |
| 425 | 4096, |
| 426 | 0, |
| 427 | EC); |
| 428 | ASSERT_NO_ERROR(EC); |
| 429 | std::copy(Val.begin(), Val.end(), mfr.data()); |
| 430 | // Explicitly add a 0. |
| 431 | mfr.data()[Val.size()] = 0; |
| 432 | // Unmap temp file |
| 433 | } |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 434 | |
| 435 | // Map it back in read-only |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 436 | fs::mapped_file_region mfr(Twine(TempPath), |
| 437 | fs::mapped_file_region::readonly, |
| 438 | 0, |
| 439 | 0, |
| 440 | EC); |
| 441 | ASSERT_NO_ERROR(EC); |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 442 | |
| 443 | // Verify content |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 444 | EXPECT_EQ(StringRef(mfr.const_data()), Val); |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 445 | |
| 446 | // Unmap temp file |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 447 | |
Chandler Carruth | 4334dd9 | 2012-11-30 11:45:22 +0000 | [diff] [blame] | 448 | #if LLVM_HAS_RVALUE_REFERENCES |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 449 | fs::mapped_file_region m(Twine(TempPath), |
| 450 | fs::mapped_file_region::readonly, |
| 451 | 0, |
| 452 | 0, |
| 453 | EC); |
| 454 | ASSERT_NO_ERROR(EC); |
| 455 | const char *Data = m.const_data(); |
| 456 | fs::mapped_file_region mfrrv(llvm_move(m)); |
| 457 | EXPECT_EQ(mfrrv.const_data(), Data); |
NAKAMURA Takumi | ae241ea | 2012-06-24 03:48:40 +0000 | [diff] [blame] | 458 | #endif |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 459 | } |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 460 | } // anonymous namespace |