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 | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 10 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 11 | #include "llvm/Support/PathV2.h" |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 861ef4b | 2010-11-24 19:20:28 +0000 | [diff] [blame] | 14 | |
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 | |
Michael J. Spencer | 013d15a | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 32 | TEST(Support, Path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 33 | SmallVector<StringRef, 40> paths; |
| 34 | paths.push_back(""); |
| 35 | paths.push_back("."); |
| 36 | paths.push_back(".."); |
| 37 | paths.push_back("foo"); |
| 38 | paths.push_back("/"); |
| 39 | paths.push_back("/foo"); |
| 40 | paths.push_back("foo/"); |
| 41 | paths.push_back("/foo/"); |
| 42 | paths.push_back("foo/bar"); |
| 43 | paths.push_back("/foo/bar"); |
| 44 | paths.push_back("//net"); |
| 45 | paths.push_back("//net/foo"); |
| 46 | paths.push_back("///foo///"); |
| 47 | paths.push_back("///foo///bar"); |
| 48 | paths.push_back("/."); |
| 49 | paths.push_back("./"); |
| 50 | paths.push_back("/.."); |
| 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/.."); |
| 57 | paths.push_back("foo/../"); |
| 58 | paths.push_back("foo/../bar"); |
| 59 | paths.push_back("c:"); |
| 60 | paths.push_back("c:/"); |
| 61 | paths.push_back("c:foo"); |
| 62 | paths.push_back("c:/foo"); |
| 63 | paths.push_back("c:foo/"); |
| 64 | paths.push_back("c:/foo/"); |
| 65 | paths.push_back("c:/foo/bar"); |
| 66 | paths.push_back("prn:"); |
| 67 | paths.push_back("c:\\"); |
| 68 | paths.push_back("c:foo"); |
| 69 | paths.push_back("c:\\foo"); |
| 70 | paths.push_back("c:foo\\"); |
| 71 | paths.push_back("c:\\foo\\"); |
| 72 | paths.push_back("c:\\foo/"); |
| 73 | paths.push_back("c:/foo\\bar"); |
| 74 | |
| 75 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 76 | e = paths.end(); |
| 77 | i != e; |
| 78 | ++i) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 79 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 80 | ce = sys::path::end(*i); |
| 81 | ci != ce; |
| 82 | ++ci) { |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 83 | ASSERT_FALSE(ci->empty()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 84 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 85 | |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 86 | #if 0 // Valgrind is whining about this. |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 87 | outs() << " Reverse Iteration: ["; |
| 88 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 89 | ce = sys::path::rend(*i); |
| 90 | ci != ce; |
| 91 | ++ci) { |
| 92 | outs() << *ci << ','; |
| 93 | } |
| 94 | outs() << "]\n"; |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 95 | #endif |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 96 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 97 | path::has_root_path(*i); |
| 98 | path::root_path(*i); |
| 99 | path::has_root_name(*i); |
| 100 | path::root_name(*i); |
| 101 | path::has_root_directory(*i); |
| 102 | path::root_directory(*i); |
| 103 | path::has_parent_path(*i); |
| 104 | path::parent_path(*i); |
| 105 | path::has_filename(*i); |
| 106 | path::filename(*i); |
| 107 | path::has_stem(*i); |
| 108 | path::stem(*i); |
| 109 | path::has_extension(*i); |
| 110 | path::extension(*i); |
| 111 | path::is_absolute(*i); |
| 112 | path::is_relative(*i); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 113 | |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 114 | SmallString<128> temp_store; |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 115 | temp_store = *i; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 116 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 117 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 118 | path::remove_filename(temp_store); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 119 | |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 120 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 121 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 122 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 123 | stem = path::stem(filename); |
| 124 | ext = path::extension(filename); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 125 | EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); |
| 126 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 127 | path::native(*i, temp_store); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 128 | } |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 129 | } |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 130 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 131 | class FileSystemTest : public testing::Test { |
| 132 | protected: |
| 133 | /// Unique temporary directory in which all created filesystem entities must |
| 134 | /// be placed. It is recursively removed at the end of each test. |
| 135 | SmallString<128> TestDirectory; |
| 136 | |
| 137 | virtual void SetUp() { |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 138 | int fd; |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 139 | ASSERT_NO_ERROR( |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 140 | fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd, |
| 141 | TestDirectory)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 142 | // We don't care about this specific file. |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 143 | ::close(fd); |
| 144 | TestDirectory = path::parent_path(TestDirectory); |
| 145 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 146 | errs().flush(); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | virtual void TearDown() { |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 150 | uint32_t removed; |
| 151 | ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 152 | } |
| 153 | }; |
| 154 | |
| 155 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 156 | // Create a temp file. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 157 | int FileDescriptor; |
| 158 | SmallString<64> TempPath; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 159 | ASSERT_NO_ERROR( |
| 160 | fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 161 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 162 | // Make sure it exists. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 163 | bool TempFileExists; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 164 | ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 165 | EXPECT_TRUE(TempFileExists); |
| 166 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 167 | // Create another temp tile. |
| 168 | int FD2; |
| 169 | SmallString<64> TempPath2; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 170 | ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 171 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 172 | |
| 173 | // Try to copy the first to the second. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 174 | EXPECT_EQ( |
| 175 | fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 176 | |
| 177 | ::close(FD2); |
| 178 | // Try again with the proper options. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 179 | ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2), |
| 180 | fs::copy_option::overwrite_if_exists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 181 | // Remove Temp2. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 182 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 183 | EXPECT_TRUE(TempFileExists); |
| 184 | |
| 185 | // Make sure Temp2 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 186 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 187 | EXPECT_FALSE(TempFileExists); |
| 188 | |
| 189 | // Create a hard link to Temp1. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 190 | ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 191 | bool equal; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 192 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 193 | EXPECT_TRUE(equal); |
| 194 | |
| 195 | // Remove Temp1. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 196 | ::close(FileDescriptor); |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 197 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 106aa73 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 198 | EXPECT_TRUE(TempFileExists); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 199 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 200 | // Remove the hard link. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 201 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 202 | EXPECT_TRUE(TempFileExists); |
| 203 | |
| 204 | // Make sure Temp1 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 205 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); |
Benjamin Kramer | 6d6d16a | 2010-12-03 12:33:32 +0000 | [diff] [blame] | 206 | EXPECT_FALSE(TempFileExists); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 207 | } |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 208 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 209 | TEST_F(FileSystemTest, DirectoryIteration) { |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 210 | error_code ec; |
Michael J. Spencer | f9fd078 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 211 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 212 | ASSERT_NO_ERROR(ec); |
| 213 | } |
Michael J. Spencer | 238589e | 2011-01-06 05:58:02 +0000 | [diff] [blame^] | 214 | |
| 215 | TEST_F(FileSystemTest, Magic) { |
| 216 | struct type { |
| 217 | const char *filename; |
| 218 | const char *magic_str; |
| 219 | size_t magic_str_len; |
| 220 | } types [] = {{"magic.archive", "!<arch>\x0A", 8}}; |
| 221 | |
| 222 | // Create some files filled with magic. |
| 223 | for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; |
| 224 | ++i) { |
| 225 | SmallString<128> file_pathname(TestDirectory); |
| 226 | path::append(file_pathname, i->filename); |
| 227 | std::string ErrMsg; |
| 228 | raw_fd_ostream file(file_pathname.c_str(), ErrMsg, |
| 229 | raw_fd_ostream::F_Binary); |
| 230 | ASSERT_FALSE(file.has_error()); |
| 231 | StringRef magic(i->magic_str, i->magic_str_len); |
| 232 | file << magic; |
| 233 | file.flush(); |
| 234 | bool res = false; |
| 235 | ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res)); |
| 236 | EXPECT_TRUE(res); |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 237 | } |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | } // anonymous namespace |