Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// |
Michael J. Spencer | 9884778 | 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 | 3bc8e71 | 2013-06-11 22:21:28 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Path.h" |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 11 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 13 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame^] | 17 | #ifdef LLVM_ON_WIN32 |
| 18 | #include <winerror.h> |
| 19 | #endif |
| 20 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 22 | using namespace llvm::sys; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 23 | |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 24 | #define ASSERT_NO_ERROR(x) \ |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 25 | if (error_code ASSERT_NO_ERROR_ec = x) { \ |
| 26 | SmallString<128> MessageStorage; \ |
| 27 | raw_svector_ostream Message(MessageStorage); \ |
| 28 | Message << #x ": did not return errc::success.\n" \ |
| 29 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 30 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 31 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 32 | } else {} |
| 33 | |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 36 | TEST(is_separator, Works) { |
| 37 | EXPECT_TRUE(path::is_separator('/')); |
| 38 | EXPECT_FALSE(path::is_separator('\0')); |
| 39 | EXPECT_FALSE(path::is_separator('-')); |
| 40 | EXPECT_FALSE(path::is_separator(' ')); |
| 41 | |
| 42 | #ifdef LLVM_ON_WIN32 |
| 43 | EXPECT_TRUE(path::is_separator('\\')); |
| 44 | #else |
| 45 | EXPECT_FALSE(path::is_separator('\\')); |
| 46 | #endif |
| 47 | } |
| 48 | |
Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 49 | TEST(Support, Path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 50 | SmallVector<StringRef, 40> paths; |
| 51 | paths.push_back(""); |
| 52 | paths.push_back("."); |
| 53 | paths.push_back(".."); |
| 54 | paths.push_back("foo"); |
| 55 | paths.push_back("/"); |
| 56 | paths.push_back("/foo"); |
| 57 | paths.push_back("foo/"); |
| 58 | paths.push_back("/foo/"); |
| 59 | paths.push_back("foo/bar"); |
| 60 | paths.push_back("/foo/bar"); |
| 61 | paths.push_back("//net"); |
| 62 | paths.push_back("//net/foo"); |
| 63 | paths.push_back("///foo///"); |
| 64 | paths.push_back("///foo///bar"); |
| 65 | paths.push_back("/."); |
| 66 | paths.push_back("./"); |
| 67 | paths.push_back("/.."); |
| 68 | paths.push_back("../"); |
| 69 | paths.push_back("foo/."); |
| 70 | paths.push_back("foo/.."); |
| 71 | paths.push_back("foo/./"); |
| 72 | paths.push_back("foo/./bar"); |
| 73 | paths.push_back("foo/.."); |
| 74 | paths.push_back("foo/../"); |
| 75 | paths.push_back("foo/../bar"); |
| 76 | paths.push_back("c:"); |
| 77 | paths.push_back("c:/"); |
| 78 | paths.push_back("c:foo"); |
| 79 | paths.push_back("c:/foo"); |
| 80 | paths.push_back("c:foo/"); |
| 81 | paths.push_back("c:/foo/"); |
| 82 | paths.push_back("c:/foo/bar"); |
| 83 | paths.push_back("prn:"); |
| 84 | paths.push_back("c:\\"); |
| 85 | paths.push_back("c:foo"); |
| 86 | paths.push_back("c:\\foo"); |
| 87 | paths.push_back("c:foo\\"); |
| 88 | paths.push_back("c:\\foo\\"); |
| 89 | paths.push_back("c:\\foo/"); |
| 90 | paths.push_back("c:/foo\\bar"); |
| 91 | |
| 92 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 93 | e = paths.end(); |
| 94 | i != e; |
| 95 | ++i) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 96 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 97 | ce = sys::path::end(*i); |
| 98 | ci != ce; |
| 99 | ++ci) { |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 100 | ASSERT_FALSE(ci->empty()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 101 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 102 | |
Michael J. Spencer | 75942f9 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 103 | #if 0 // Valgrind is whining about this. |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 104 | outs() << " Reverse Iteration: ["; |
| 105 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 106 | ce = sys::path::rend(*i); |
| 107 | ci != ce; |
| 108 | ++ci) { |
| 109 | outs() << *ci << ','; |
| 110 | } |
| 111 | outs() << "]\n"; |
Michael J. Spencer | 75942f9 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 112 | #endif |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 113 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 114 | path::has_root_path(*i); |
| 115 | path::root_path(*i); |
| 116 | path::has_root_name(*i); |
| 117 | path::root_name(*i); |
| 118 | path::has_root_directory(*i); |
| 119 | path::root_directory(*i); |
| 120 | path::has_parent_path(*i); |
| 121 | path::parent_path(*i); |
| 122 | path::has_filename(*i); |
| 123 | path::filename(*i); |
| 124 | path::has_stem(*i); |
| 125 | path::stem(*i); |
| 126 | path::has_extension(*i); |
| 127 | path::extension(*i); |
| 128 | path::is_absolute(*i); |
| 129 | path::is_relative(*i); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 130 | |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 131 | SmallString<128> temp_store; |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 132 | temp_store = *i; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 133 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 134 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 135 | path::remove_filename(temp_store); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 136 | |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 137 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 138 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 139 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 140 | stem = path::stem(filename); |
| 141 | ext = path::extension(filename); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 142 | EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); |
| 143 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 144 | path::native(*i, temp_store); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 145 | } |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 146 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 147 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 148 | TEST(Support, RelativePathIterator) { |
| 149 | SmallString<64> Path(StringRef("c/d/e/foo.txt")); |
| 150 | typedef SmallVector<StringRef, 4> PathComponents; |
| 151 | PathComponents ExpectedPathComponents; |
| 152 | PathComponents ActualPathComponents; |
| 153 | |
| 154 | StringRef(Path).split(ExpectedPathComponents, "/"); |
| 155 | |
| 156 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 157 | ++I) { |
| 158 | ActualPathComponents.push_back(*I); |
| 159 | } |
| 160 | |
| 161 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 162 | |
| 163 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 164 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | TEST(Support, AbsolutePathIterator) { |
| 169 | SmallString<64> Path(StringRef("/c/d/e/foo.txt")); |
| 170 | typedef SmallVector<StringRef, 4> PathComponents; |
| 171 | PathComponents ExpectedPathComponents; |
| 172 | PathComponents ActualPathComponents; |
| 173 | |
| 174 | StringRef(Path).split(ExpectedPathComponents, "/"); |
| 175 | |
| 176 | // The root path will also be a component when iterating |
| 177 | ExpectedPathComponents[0] = "/"; |
| 178 | |
| 179 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 180 | ++I) { |
| 181 | ActualPathComponents.push_back(*I); |
| 182 | } |
| 183 | |
| 184 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 185 | |
| 186 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 187 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | #ifdef LLVM_ON_WIN32 |
| 192 | TEST(Support, AbsolutePathIteratorWin32) { |
| 193 | SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); |
| 194 | typedef SmallVector<StringRef, 4> PathComponents; |
| 195 | PathComponents ExpectedPathComponents; |
| 196 | PathComponents ActualPathComponents; |
| 197 | |
| 198 | StringRef(Path).split(ExpectedPathComponents, "\\"); |
| 199 | |
| 200 | // The root path (which comes after the drive name) will also be a component |
| 201 | // when iterating. |
| 202 | ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); |
| 203 | |
| 204 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 205 | ++I) { |
| 206 | ActualPathComponents.push_back(*I); |
| 207 | } |
| 208 | |
| 209 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 210 | |
| 211 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 212 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 213 | } |
| 214 | } |
| 215 | #endif // LLVM_ON_WIN32 |
| 216 | |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 217 | TEST(Support, AbsolutePathIteratorEnd) { |
| 218 | // Trailing slashes are converted to '.' unless they are part of the root path. |
| 219 | SmallVector<StringRef, 4> Paths; |
| 220 | Paths.push_back("/foo/"); |
| 221 | Paths.push_back("/foo//"); |
| 222 | Paths.push_back("//net//"); |
| 223 | #ifdef LLVM_ON_WIN32 |
| 224 | Paths.push_back("c:\\\\"); |
| 225 | #endif |
| 226 | |
| 227 | for (StringRef Path : Paths) { |
| 228 | StringRef LastComponent = *--path::end(Path); |
| 229 | EXPECT_EQ(".", LastComponent); |
| 230 | } |
| 231 | |
| 232 | SmallVector<StringRef, 3> RootPaths; |
| 233 | RootPaths.push_back("/"); |
| 234 | RootPaths.push_back("//net/"); |
| 235 | #ifdef LLVM_ON_WIN32 |
| 236 | RootPaths.push_back("c:\\"); |
| 237 | #endif |
| 238 | |
| 239 | for (StringRef Path : RootPaths) { |
| 240 | StringRef LastComponent = *--path::end(Path); |
| 241 | EXPECT_EQ(1u, LastComponent.size()); |
| 242 | EXPECT_TRUE(path::is_separator(LastComponent[0])); |
| 243 | } |
| 244 | } |
| 245 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 246 | TEST(Support, HomeDirectory) { |
| 247 | #ifdef LLVM_ON_UNIX |
| 248 | // This test only makes sense on Unix if $HOME is set. |
| 249 | if (::getenv("HOME")) { |
| 250 | #endif |
| 251 | SmallString<128> HomeDir; |
| 252 | EXPECT_TRUE(path::home_directory(HomeDir)); |
| 253 | EXPECT_FALSE(HomeDir.empty()); |
| 254 | #ifdef LLVM_ON_UNIX |
| 255 | } |
| 256 | #endif |
| 257 | } |
| 258 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 259 | class FileSystemTest : public testing::Test { |
| 260 | protected: |
| 261 | /// Unique temporary directory in which all created filesystem entities must |
| 262 | /// be placed. It is recursively removed at the end of each test. |
| 263 | SmallString<128> TestDirectory; |
| 264 | |
| 265 | virtual void SetUp() { |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 266 | ASSERT_NO_ERROR( |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 267 | fs::createUniqueDirectory("file-system-test", TestDirectory)); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 268 | // We don't care about this specific file. |
Michael J. Spencer | 42368cc | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 269 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 270 | errs().flush(); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | virtual void TearDown() { |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 274 | ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 275 | } |
| 276 | }; |
| 277 | |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 278 | TEST_F(FileSystemTest, Unique) { |
| 279 | // Create a temp file. |
| 280 | int FileDescriptor; |
| 281 | SmallString<64> TempPath; |
| 282 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 283 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 284 | |
| 285 | // The same file should return an identical unique id. |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 286 | fs::UniqueID F1, F2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 287 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); |
| 288 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 289 | ASSERT_EQ(F1, F2); |
| 290 | |
| 291 | // Different files should return different unique ids. |
| 292 | int FileDescriptor2; |
| 293 | SmallString<64> TempPath2; |
| 294 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 295 | fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); |
| 296 | |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 297 | fs::UniqueID D; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 298 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 299 | ASSERT_NE(D, F1); |
| 300 | ::close(FileDescriptor2); |
| 301 | |
| 302 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 303 | |
| 304 | // Two paths representing the same file on disk should still provide the |
| 305 | // same unique id. We can test this by making a hard link. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 306 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 307 | fs::UniqueID D2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 308 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 309 | ASSERT_EQ(D2, F1); |
| 310 | |
| 311 | ::close(FileDescriptor); |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 312 | |
| 313 | SmallString<128> Dir1; |
| 314 | ASSERT_NO_ERROR( |
| 315 | fs::createUniqueDirectory("dir1", Dir1)); |
| 316 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); |
| 317 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); |
| 318 | ASSERT_EQ(F1, F2); |
| 319 | |
| 320 | SmallString<128> Dir2; |
| 321 | ASSERT_NO_ERROR( |
| 322 | fs::createUniqueDirectory("dir2", Dir2)); |
| 323 | ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); |
| 324 | ASSERT_NE(F1, F2); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 327 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 328 | // Create a temp file. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 329 | int FileDescriptor; |
| 330 | SmallString<64> TempPath; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 331 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 332 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 333 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 334 | // Make sure it exists. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 335 | bool TempFileExists; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 336 | ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 337 | EXPECT_TRUE(TempFileExists); |
| 338 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 339 | // Create another temp tile. |
| 340 | int FD2; |
| 341 | SmallString<64> TempPath2; |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 342 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 343 | ASSERT_TRUE(TempPath2.endswith(".temp")); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 344 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 345 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 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 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 351 | ::close(FD2); |
Rafael Espindola | 213c4cb | 2013-07-18 03:29:51 +0000 | [diff] [blame] | 352 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 353 | // Remove Temp2. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 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); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 358 | |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 359 | 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 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 363 | // Make sure Temp2 doesn't exist. |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 364 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 365 | EXPECT_FALSE(TempFileExists); |
| 366 | |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 367 | SmallString<64> TempPath3; |
| 368 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); |
| 369 | ASSERT_FALSE(TempPath3.endswith(".")); |
| 370 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 371 | // Create a hard link to Temp1. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 372 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 373 | bool equal; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 374 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 375 | EXPECT_TRUE(equal); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 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)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 379 | |
| 380 | // Remove Temp1. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 381 | ::close(FileDescriptor); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 382 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 383 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 384 | // Remove the hard link. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 385 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 386 | |
| 387 | // Make sure Temp1 doesn't exist. |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 388 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); |
Benjamin Kramer | 23e6bdf | 2010-12-03 12:33:32 +0000 | [diff] [blame] | 389 | EXPECT_FALSE(TempFileExists); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 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"; |
Rafael Espindola | 2eb1ae4 | 2013-07-05 22:32:33 +0000 | [diff] [blame] | 399 | EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath), |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame^] | 400 | error_code(ERROR_PATH_NOT_FOUND, system_category())); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 401 | #endif |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 402 | } |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 403 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 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 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 412 | TEST_F(FileSystemTest, DirectoryIteration) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 413 | error_code ec; |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 414 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 415 | ASSERT_NO_ERROR(ec); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 416 | |
| 417 | // Create a known hierarchy to recurse over. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 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")); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 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); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 433 | if (path::filename(i->path()) == "p1") { |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 434 | i.pop(); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 435 | // FIXME: recursive_directory_iterator should be more robust. |
| 436 | if (i == e) break; |
| 437 | } |
Michael J. Spencer | 9b54a3e | 2011-12-09 01:14:41 +0000 | [diff] [blame] | 438 | if (path::filename(i->path()) == "dontlookhere") |
| 439 | i.no_push(); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 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); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 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")); |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 481 | } |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 482 | |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 483 | const char archive[] = "!<arch>\x0A"; |
| 484 | const char bitcode[] = "\xde\xc0\x17\x0b"; |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 485 | const char coff_object[] = "\x00\x00......"; |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 486 | const char coff_import_library[] = "\x00\x00\xff\xff...."; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 487 | const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, |
| 488 | 0, 0, 0, 0, 0, 0, 0, 0, 1 }; |
| 489 | const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00"; |
| 490 | const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01"; |
| 491 | const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02"; |
| 492 | const char macho_fixed_virtual_memory_shared_lib[] = |
| 493 | "\xfe\xed\xfa\xce..........\x00\x03"; |
| 494 | const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04"; |
| 495 | const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05"; |
| 496 | const char macho_dynamically_linked_shared_lib[] = |
| 497 | "\xfe\xed\xfa\xce..........\x00\x06"; |
| 498 | const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07"; |
| 499 | const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08"; |
| 500 | const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a"; |
| 501 | const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 502 | |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 503 | TEST_F(FileSystemTest, Magic) { |
| 504 | struct type { |
| 505 | const char *filename; |
| 506 | const char *magic_str; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 507 | size_t magic_str_len; |
| 508 | fs::file_magic magic; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 509 | } types[] = { |
| 510 | #define DEFINE(magic) \ |
| 511 | { #magic, magic, sizeof(magic), fs::file_magic::magic } |
| 512 | DEFINE(archive), |
| 513 | DEFINE(bitcode), |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 514 | DEFINE(coff_object), |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 515 | DEFINE(coff_import_library), |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 516 | DEFINE(elf_relocatable), |
| 517 | DEFINE(macho_universal_binary), |
| 518 | DEFINE(macho_object), |
| 519 | DEFINE(macho_executable), |
| 520 | DEFINE(macho_fixed_virtual_memory_shared_lib), |
| 521 | DEFINE(macho_core), |
| 522 | DEFINE(macho_preload_executable), |
| 523 | DEFINE(macho_dynamically_linked_shared_lib), |
| 524 | DEFINE(macho_dynamic_linker), |
| 525 | DEFINE(macho_bundle), |
| 526 | DEFINE(macho_dsym_companion), |
| 527 | DEFINE(windows_resource) |
| 528 | #undef DEFINE |
| 529 | }; |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 530 | |
| 531 | // Create some files filled with magic. |
| 532 | for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; |
| 533 | ++i) { |
| 534 | SmallString<128> file_pathname(TestDirectory); |
| 535 | path::append(file_pathname, i->filename); |
| 536 | std::string ErrMsg; |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 537 | raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_None); |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 538 | ASSERT_FALSE(file.has_error()); |
| 539 | StringRef magic(i->magic_str, i->magic_str_len); |
| 540 | file << magic; |
Michael J. Spencer | b587180 | 2011-01-15 18:52:49 +0000 | [diff] [blame] | 541 | file.close(); |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 542 | bool res = false; |
| 543 | ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res)); |
| 544 | EXPECT_TRUE(res); |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 545 | EXPECT_EQ(i->magic, fs::identify_magic(magic)); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 546 | ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 547 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 550 | #ifdef LLVM_ON_WIN32 |
| 551 | TEST_F(FileSystemTest, CarriageReturn) { |
| 552 | SmallString<128> FilePathname(TestDirectory); |
| 553 | std::string ErrMsg; |
| 554 | path::append(FilePathname, "test"); |
| 555 | |
| 556 | { |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 557 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Text); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 558 | EXPECT_EQ(ErrMsg, ""); |
| 559 | File << '\n'; |
| 560 | } |
| 561 | { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 562 | std::unique_ptr<MemoryBuffer> Buf; |
Rafael Espindola | e5bf246 | 2013-10-25 19:47:55 +0000 | [diff] [blame] | 563 | MemoryBuffer::getFile(FilePathname.c_str(), Buf); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 564 | EXPECT_EQ(Buf->getBuffer(), "\r\n"); |
| 565 | } |
| 566 | |
| 567 | { |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 568 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_None); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 569 | EXPECT_EQ(ErrMsg, ""); |
| 570 | File << '\n'; |
| 571 | } |
| 572 | { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 573 | std::unique_ptr<MemoryBuffer> Buf; |
Rafael Espindola | e5bf246 | 2013-10-25 19:47:55 +0000 | [diff] [blame] | 574 | MemoryBuffer::getFile(FilePathname.c_str(), Buf); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 575 | EXPECT_EQ(Buf->getBuffer(), "\n"); |
| 576 | } |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 577 | ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 578 | } |
| 579 | #endif |
| 580 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 581 | TEST_F(FileSystemTest, FileMapping) { |
| 582 | // Create a temp file. |
| 583 | int FileDescriptor; |
| 584 | SmallString<64> TempPath; |
| 585 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 586 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 587 | // Map in temp file and add some content |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 588 | error_code EC; |
| 589 | StringRef Val("hello there"); |
| 590 | { |
| 591 | fs::mapped_file_region mfr(FileDescriptor, |
Michael J. Spencer | 01ac34e | 2013-03-14 00:33:37 +0000 | [diff] [blame] | 592 | true, |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 593 | fs::mapped_file_region::readwrite, |
| 594 | 4096, |
| 595 | 0, |
| 596 | EC); |
| 597 | ASSERT_NO_ERROR(EC); |
| 598 | std::copy(Val.begin(), Val.end(), mfr.data()); |
| 599 | // Explicitly add a 0. |
| 600 | mfr.data()[Val.size()] = 0; |
| 601 | // Unmap temp file |
| 602 | } |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 603 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 604 | // Map it back in read-only |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 605 | fs::mapped_file_region mfr(Twine(TempPath), |
| 606 | fs::mapped_file_region::readonly, |
| 607 | 0, |
| 608 | 0, |
| 609 | EC); |
| 610 | ASSERT_NO_ERROR(EC); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 611 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 612 | // Verify content |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 613 | EXPECT_EQ(StringRef(mfr.const_data()), Val); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 614 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 615 | // Unmap temp file |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 616 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 617 | fs::mapped_file_region m(Twine(TempPath), |
| 618 | fs::mapped_file_region::readonly, |
| 619 | 0, |
| 620 | 0, |
| 621 | EC); |
| 622 | ASSERT_NO_ERROR(EC); |
| 623 | const char *Data = m.const_data(); |
Chandler Carruth | 002da5d | 2014-03-02 04:08:41 +0000 | [diff] [blame] | 624 | fs::mapped_file_region mfrrv(std::move(m)); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 625 | EXPECT_EQ(mfrrv.const_data(), Data); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 626 | } |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 627 | |
| 628 | TEST(Support, NormalizePath) { |
| 629 | #if defined(LLVM_ON_WIN32) |
| 630 | #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ |
| 631 | EXPECT_EQ(path__, windows__); |
| 632 | #else |
| 633 | #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ |
| 634 | EXPECT_EQ(path__, not_windows__); |
| 635 | #endif |
| 636 | |
| 637 | SmallString<64> Path1("a"); |
| 638 | SmallString<64> Path2("a/b"); |
| 639 | SmallString<64> Path3("a\\b"); |
| 640 | SmallString<64> Path4("a\\\\b"); |
| 641 | SmallString<64> Path5("\\a"); |
| 642 | SmallString<64> Path6("a\\"); |
| 643 | |
| 644 | ASSERT_NO_ERROR(fs::normalize_separators(Path1)); |
| 645 | EXPECT_PATH_IS(Path1, "a", "a"); |
| 646 | |
| 647 | ASSERT_NO_ERROR(fs::normalize_separators(Path2)); |
| 648 | EXPECT_PATH_IS(Path2, "a/b", "a/b"); |
| 649 | |
| 650 | ASSERT_NO_ERROR(fs::normalize_separators(Path3)); |
| 651 | EXPECT_PATH_IS(Path3, "a\\b", "a/b"); |
| 652 | |
| 653 | ASSERT_NO_ERROR(fs::normalize_separators(Path4)); |
| 654 | EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); |
| 655 | |
| 656 | ASSERT_NO_ERROR(fs::normalize_separators(Path5)); |
| 657 | EXPECT_PATH_IS(Path5, "\\a", "/a"); |
| 658 | |
| 659 | ASSERT_NO_ERROR(fs::normalize_separators(Path6)); |
| 660 | EXPECT_PATH_IS(Path6, "a\\", "a/"); |
| 661 | |
| 662 | #undef EXPECT_PATH_IS |
| 663 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 664 | } // anonymous namespace |