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 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 10 | #include "llvm/Support/PathV2.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" |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
| 15 | |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 16 | using namespace llvm; |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 17 | using namespace llvm::sys; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 18 | |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 19 | #define ASSERT_NO_ERROR(x) \ |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 20 | if (error_code ASSERT_NO_ERROR_ec = x) { \ |
| 21 | SmallString<128> MessageStorage; \ |
| 22 | raw_svector_ostream Message(MessageStorage); \ |
| 23 | Message << #x ": did not return errc::success.\n" \ |
| 24 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 25 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 26 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 27 | } else {} |
| 28 | |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | |
Zhanyong Wan | 63cc3a8 | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 31 | TEST(is_separator, Works) { |
| 32 | EXPECT_TRUE(path::is_separator('/')); |
| 33 | EXPECT_FALSE(path::is_separator('\0')); |
| 34 | EXPECT_FALSE(path::is_separator('-')); |
| 35 | EXPECT_FALSE(path::is_separator(' ')); |
| 36 | |
| 37 | #ifdef LLVM_ON_WIN32 |
| 38 | EXPECT_TRUE(path::is_separator('\\')); |
| 39 | #else |
| 40 | EXPECT_FALSE(path::is_separator('\\')); |
| 41 | #endif |
| 42 | } |
| 43 | |
Michael J. Spencer | 013d15a | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 44 | TEST(Support, Path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 45 | SmallVector<StringRef, 40> paths; |
| 46 | paths.push_back(""); |
| 47 | paths.push_back("."); |
| 48 | paths.push_back(".."); |
| 49 | paths.push_back("foo"); |
| 50 | paths.push_back("/"); |
| 51 | paths.push_back("/foo"); |
| 52 | paths.push_back("foo/"); |
| 53 | paths.push_back("/foo/"); |
| 54 | paths.push_back("foo/bar"); |
| 55 | paths.push_back("/foo/bar"); |
| 56 | paths.push_back("//net"); |
| 57 | paths.push_back("//net/foo"); |
| 58 | paths.push_back("///foo///"); |
| 59 | paths.push_back("///foo///bar"); |
| 60 | paths.push_back("/."); |
| 61 | paths.push_back("./"); |
| 62 | paths.push_back("/.."); |
| 63 | paths.push_back("../"); |
| 64 | paths.push_back("foo/."); |
| 65 | paths.push_back("foo/.."); |
| 66 | paths.push_back("foo/./"); |
| 67 | paths.push_back("foo/./bar"); |
| 68 | paths.push_back("foo/.."); |
| 69 | paths.push_back("foo/../"); |
| 70 | paths.push_back("foo/../bar"); |
| 71 | paths.push_back("c:"); |
| 72 | paths.push_back("c:/"); |
| 73 | paths.push_back("c:foo"); |
| 74 | paths.push_back("c:/foo"); |
| 75 | paths.push_back("c:foo/"); |
| 76 | paths.push_back("c:/foo/"); |
| 77 | paths.push_back("c:/foo/bar"); |
| 78 | paths.push_back("prn:"); |
| 79 | paths.push_back("c:\\"); |
| 80 | paths.push_back("c:foo"); |
| 81 | paths.push_back("c:\\foo"); |
| 82 | paths.push_back("c:foo\\"); |
| 83 | paths.push_back("c:\\foo\\"); |
| 84 | paths.push_back("c:\\foo/"); |
| 85 | paths.push_back("c:/foo\\bar"); |
| 86 | |
| 87 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 88 | e = paths.end(); |
| 89 | i != e; |
| 90 | ++i) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 91 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 92 | ce = sys::path::end(*i); |
| 93 | ci != ce; |
| 94 | ++ci) { |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 95 | ASSERT_FALSE(ci->empty()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 96 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 97 | |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 98 | #if 0 // Valgrind is whining about this. |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 99 | outs() << " Reverse Iteration: ["; |
| 100 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 101 | ce = sys::path::rend(*i); |
| 102 | ci != ce; |
| 103 | ++ci) { |
| 104 | outs() << *ci << ','; |
| 105 | } |
| 106 | outs() << "]\n"; |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 107 | #endif |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 108 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 109 | path::has_root_path(*i); |
| 110 | path::root_path(*i); |
| 111 | path::has_root_name(*i); |
| 112 | path::root_name(*i); |
| 113 | path::has_root_directory(*i); |
| 114 | path::root_directory(*i); |
| 115 | path::has_parent_path(*i); |
| 116 | path::parent_path(*i); |
| 117 | path::has_filename(*i); |
| 118 | path::filename(*i); |
| 119 | path::has_stem(*i); |
| 120 | path::stem(*i); |
| 121 | path::has_extension(*i); |
| 122 | path::extension(*i); |
| 123 | path::is_absolute(*i); |
| 124 | path::is_relative(*i); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 125 | |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 126 | SmallString<128> temp_store; |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 127 | temp_store = *i; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 128 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 129 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 130 | path::remove_filename(temp_store); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 131 | |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 132 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 133 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 134 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 135 | stem = path::stem(filename); |
| 136 | ext = path::extension(filename); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 137 | EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); |
| 138 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 139 | path::native(*i, temp_store); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 140 | } |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 141 | } |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 142 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 143 | class FileSystemTest : public testing::Test { |
| 144 | protected: |
| 145 | /// Unique temporary directory in which all created filesystem entities must |
| 146 | /// be placed. It is recursively removed at the end of each test. |
| 147 | SmallString<128> TestDirectory; |
| 148 | |
| 149 | virtual void SetUp() { |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 150 | int fd; |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 151 | ASSERT_NO_ERROR( |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 152 | fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd, |
| 153 | TestDirectory)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 154 | // We don't care about this specific file. |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 155 | ::close(fd); |
| 156 | TestDirectory = path::parent_path(TestDirectory); |
| 157 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 158 | errs().flush(); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | virtual void TearDown() { |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 162 | uint32_t removed; |
| 163 | ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 164 | } |
| 165 | }; |
| 166 | |
| 167 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 168 | // Create a temp file. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 169 | int FileDescriptor; |
| 170 | SmallString<64> TempPath; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 171 | ASSERT_NO_ERROR( |
| 172 | fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 173 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 174 | // Make sure it exists. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 175 | bool TempFileExists; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 176 | ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 177 | EXPECT_TRUE(TempFileExists); |
| 178 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 179 | // Create another temp tile. |
| 180 | int FD2; |
| 181 | SmallString<64> TempPath2; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 182 | ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 183 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 184 | |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 185 | fs::file_status A, B; |
| 186 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 187 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 188 | EXPECT_FALSE(fs::equivalent(A, B)); |
| 189 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 190 | // Try to copy the first to the second. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 191 | EXPECT_EQ( |
| 192 | fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 193 | |
| 194 | ::close(FD2); |
| 195 | // Try again with the proper options. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 196 | ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2), |
| 197 | fs::copy_option::overwrite_if_exists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 198 | // Remove Temp2. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 199 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 200 | EXPECT_TRUE(TempFileExists); |
| 201 | |
| 202 | // Make sure Temp2 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 203 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 204 | EXPECT_FALSE(TempFileExists); |
| 205 | |
| 206 | // Create a hard link to Temp1. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 207 | ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 208 | bool equal; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 209 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 210 | EXPECT_TRUE(equal); |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 211 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 212 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 213 | EXPECT_TRUE(fs::equivalent(A, B)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 214 | |
| 215 | // Remove Temp1. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 216 | ::close(FileDescriptor); |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 217 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 106aa73 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 218 | EXPECT_TRUE(TempFileExists); |
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 | // Remove the hard link. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 221 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 222 | EXPECT_TRUE(TempFileExists); |
| 223 | |
| 224 | // Make sure Temp1 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 225 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); |
Benjamin Kramer | 6d6d16a | 2010-12-03 12:33:32 +0000 | [diff] [blame] | 226 | EXPECT_FALSE(TempFileExists); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 227 | } |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 228 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 229 | TEST_F(FileSystemTest, DirectoryIteration) { |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 230 | error_code ec; |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 231 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 232 | ASSERT_NO_ERROR(ec); |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 233 | |
| 234 | // Create a known hierarchy to recurse over. |
| 235 | bool existed; |
| 236 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 237 | + "/recursive/a0/aa1", existed)); |
| 238 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 239 | + "/recursive/a0/ab1", existed)); |
| 240 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 241 | + "/recursive/dontlookhere/da1", existed)); |
| 242 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 243 | + "/recursive/z0/za1", existed)); |
| 244 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) |
| 245 | + "/recursive/pop/p1", existed)); |
| 246 | typedef std::vector<std::string> v_t; |
| 247 | v_t visited; |
| 248 | for (fs::recursive_directory_iterator i(Twine(TestDirectory) |
| 249 | + "/recursive", ec), e; i != e; i.increment(ec)){ |
| 250 | ASSERT_NO_ERROR(ec); |
NAKAMURA Takumi | 52ee230 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 251 | if (path::filename(i->path()) == "p1") { |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 252 | i.pop(); |
NAKAMURA Takumi | 52ee230 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 253 | // FIXME: recursive_directory_iterator should be more robust. |
| 254 | if (i == e) break; |
| 255 | } |
Michael J. Spencer | bd3825e | 2011-12-09 01:14:41 +0000 | [diff] [blame] | 256 | if (path::filename(i->path()) == "dontlookhere") |
| 257 | i.no_push(); |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 258 | visited.push_back(path::filename(i->path())); |
| 259 | } |
| 260 | v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0"); |
| 261 | v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1"); |
| 262 | v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1"); |
| 263 | v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(), |
| 264 | "dontlookhere"); |
| 265 | v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1"); |
| 266 | v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0"); |
| 267 | v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1"); |
| 268 | v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop"); |
| 269 | v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1"); |
| 270 | |
| 271 | // Make sure that each path was visited correctly. |
| 272 | ASSERT_NE(a0, visited.end()); |
| 273 | ASSERT_NE(aa1, visited.end()); |
| 274 | ASSERT_NE(ab1, visited.end()); |
| 275 | ASSERT_NE(dontlookhere, visited.end()); |
| 276 | ASSERT_EQ(da1, visited.end()); // Not visited. |
| 277 | ASSERT_NE(z0, visited.end()); |
| 278 | ASSERT_NE(za1, visited.end()); |
| 279 | ASSERT_NE(pop, visited.end()); |
| 280 | ASSERT_EQ(p1, visited.end()); // Not visited. |
| 281 | |
| 282 | // Make sure that parents were visited before children. No other ordering |
| 283 | // guarantees can be made across siblings. |
| 284 | ASSERT_LT(a0, aa1); |
| 285 | ASSERT_LT(a0, ab1); |
| 286 | ASSERT_LT(z0, za1); |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 287 | } |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 288 | |
| 289 | TEST_F(FileSystemTest, Magic) { |
| 290 | struct type { |
| 291 | const char *filename; |
| 292 | const char *magic_str; |
| 293 | size_t magic_str_len; |
| 294 | } types [] = {{"magic.archive", "!<arch>\x0A", 8}}; |
| 295 | |
| 296 | // Create some files filled with magic. |
| 297 | for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; |
| 298 | ++i) { |
| 299 | SmallString<128> file_pathname(TestDirectory); |
| 300 | path::append(file_pathname, i->filename); |
| 301 | std::string ErrMsg; |
| 302 | raw_fd_ostream file(file_pathname.c_str(), ErrMsg, |
| 303 | raw_fd_ostream::F_Binary); |
| 304 | ASSERT_FALSE(file.has_error()); |
| 305 | StringRef magic(i->magic_str, i->magic_str_len); |
| 306 | file << magic; |
Michael J. Spencer | 248f9f2 | 2011-01-15 18:52:49 +0000 | [diff] [blame] | 307 | file.close(); |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 308 | bool res = false; |
| 309 | ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res)); |
| 310 | EXPECT_TRUE(res); |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 311 | } |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 312 | } |
| 313 | |
NAKAMURA Takumi | ebbd6fe | 2012-06-24 03:48:34 +0000 | [diff] [blame] | 314 | #if !defined(_WIN32) // FIXME: Win32 has different permission schema. |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 315 | TEST_F(FileSystemTest, Permissions) { |
| 316 | // Create a temp file. |
| 317 | int FileDescriptor; |
| 318 | SmallString<64> TempPath; |
| 319 | ASSERT_NO_ERROR( |
| 320 | fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath)); |
| 321 | |
| 322 | // Mark file as read-only |
| 323 | const fs::perms AllWrite = fs::owner_write|fs::group_write|fs::others_write; |
| 324 | ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::remove_perms|AllWrite)); |
| 325 | |
| 326 | // Verify file is read-only |
| 327 | fs::file_status Status; |
| 328 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status)); |
| 329 | bool AnyWriteBits = (Status.permissions() & AllWrite); |
| 330 | EXPECT_FALSE(AnyWriteBits); |
| 331 | |
| 332 | // Mark file as read-write |
| 333 | ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::add_perms|AllWrite)); |
| 334 | |
| 335 | // Verify file is read-write |
| 336 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status)); |
| 337 | AnyWriteBits = (Status.permissions() & AllWrite); |
| 338 | EXPECT_TRUE(AnyWriteBits); |
| 339 | } |
NAKAMURA Takumi | ebbd6fe | 2012-06-24 03:48:34 +0000 | [diff] [blame] | 340 | #endif |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 341 | |
| 342 | TEST_F(FileSystemTest, FileMapping) { |
| 343 | // Create a temp file. |
| 344 | int FileDescriptor; |
| 345 | SmallString<64> TempPath; |
| 346 | ASSERT_NO_ERROR( |
| 347 | fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath)); |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 348 | // Map in temp file and add some content |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 349 | error_code EC; |
| 350 | StringRef Val("hello there"); |
| 351 | { |
| 352 | fs::mapped_file_region mfr(FileDescriptor, |
Michael J. Spencer | d483d28 | 2013-03-14 00:33:37 +0000 | [diff] [blame^] | 353 | true, |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 354 | fs::mapped_file_region::readwrite, |
| 355 | 4096, |
| 356 | 0, |
| 357 | EC); |
| 358 | ASSERT_NO_ERROR(EC); |
| 359 | std::copy(Val.begin(), Val.end(), mfr.data()); |
| 360 | // Explicitly add a 0. |
| 361 | mfr.data()[Val.size()] = 0; |
| 362 | // Unmap temp file |
| 363 | } |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 364 | |
| 365 | // Map it back in read-only |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 366 | fs::mapped_file_region mfr(Twine(TempPath), |
| 367 | fs::mapped_file_region::readonly, |
| 368 | 0, |
| 369 | 0, |
| 370 | EC); |
| 371 | ASSERT_NO_ERROR(EC); |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 372 | |
| 373 | // Verify content |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 374 | EXPECT_EQ(StringRef(mfr.const_data()), Val); |
Nick Kledzik | ca077ec | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 375 | |
| 376 | // Unmap temp file |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 377 | |
Chandler Carruth | 4334dd9 | 2012-11-30 11:45:22 +0000 | [diff] [blame] | 378 | #if LLVM_HAS_RVALUE_REFERENCES |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 379 | fs::mapped_file_region m(Twine(TempPath), |
| 380 | fs::mapped_file_region::readonly, |
| 381 | 0, |
| 382 | 0, |
| 383 | EC); |
| 384 | ASSERT_NO_ERROR(EC); |
| 385 | const char *Data = m.const_data(); |
| 386 | fs::mapped_file_region mfrrv(llvm_move(m)); |
| 387 | EXPECT_EQ(mfrrv.const_data(), Data); |
NAKAMURA Takumi | ae241ea | 2012-06-24 03:48:40 +0000 | [diff] [blame] | 388 | #endif |
Michael J. Spencer | 1ebd25e | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 389 | } |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 390 | } // anonymous namespace |