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" |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ConvertUTF.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Errc.h" |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 14 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FileSystem.h" |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FileUtilities.h" |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| 20 | |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 21 | #ifdef LLVM_ON_WIN32 |
Nico Weber | dd2ca83 | 2016-04-18 13:54:50 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" |
NAKAMURA Takumi | b94f052 | 2015-06-16 06:46:16 +0000 | [diff] [blame] | 23 | #include <windows.h> |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 24 | #include <winerror.h> |
| 25 | #endif |
| 26 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 27 | #ifdef LLVM_ON_UNIX |
| 28 | #include <sys/stat.h> |
| 29 | #endif |
| 30 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 32 | using namespace llvm::sys; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 33 | |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 34 | #define ASSERT_NO_ERROR(x) \ |
| 35 | if (std::error_code ASSERT_NO_ERROR_ec = x) { \ |
| 36 | SmallString<128> MessageStorage; \ |
| 37 | raw_svector_ostream Message(MessageStorage); \ |
| 38 | Message << #x ": did not return errc::success.\n" \ |
| 39 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 40 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 41 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
| 42 | } else { \ |
| 43 | } |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 44 | |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 47 | TEST(is_separator, Works) { |
| 48 | EXPECT_TRUE(path::is_separator('/')); |
| 49 | EXPECT_FALSE(path::is_separator('\0')); |
| 50 | EXPECT_FALSE(path::is_separator('-')); |
| 51 | EXPECT_FALSE(path::is_separator(' ')); |
| 52 | |
| 53 | #ifdef LLVM_ON_WIN32 |
| 54 | EXPECT_TRUE(path::is_separator('\\')); |
| 55 | #else |
| 56 | EXPECT_FALSE(path::is_separator('\\')); |
| 57 | #endif |
| 58 | } |
| 59 | |
Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 60 | TEST(Support, Path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 61 | SmallVector<StringRef, 40> paths; |
| 62 | paths.push_back(""); |
| 63 | paths.push_back("."); |
| 64 | paths.push_back(".."); |
| 65 | paths.push_back("foo"); |
| 66 | paths.push_back("/"); |
| 67 | paths.push_back("/foo"); |
| 68 | paths.push_back("foo/"); |
| 69 | paths.push_back("/foo/"); |
| 70 | paths.push_back("foo/bar"); |
| 71 | paths.push_back("/foo/bar"); |
| 72 | paths.push_back("//net"); |
| 73 | paths.push_back("//net/foo"); |
| 74 | paths.push_back("///foo///"); |
| 75 | paths.push_back("///foo///bar"); |
| 76 | paths.push_back("/."); |
| 77 | paths.push_back("./"); |
| 78 | paths.push_back("/.."); |
| 79 | paths.push_back("../"); |
| 80 | paths.push_back("foo/."); |
| 81 | paths.push_back("foo/.."); |
| 82 | paths.push_back("foo/./"); |
| 83 | paths.push_back("foo/./bar"); |
| 84 | paths.push_back("foo/.."); |
| 85 | paths.push_back("foo/../"); |
| 86 | paths.push_back("foo/../bar"); |
| 87 | paths.push_back("c:"); |
| 88 | paths.push_back("c:/"); |
| 89 | paths.push_back("c:foo"); |
| 90 | paths.push_back("c:/foo"); |
| 91 | paths.push_back("c:foo/"); |
| 92 | paths.push_back("c:/foo/"); |
| 93 | paths.push_back("c:/foo/bar"); |
| 94 | paths.push_back("prn:"); |
| 95 | paths.push_back("c:\\"); |
| 96 | paths.push_back("c:foo"); |
| 97 | paths.push_back("c:\\foo"); |
| 98 | paths.push_back("c:foo\\"); |
| 99 | paths.push_back("c:\\foo\\"); |
| 100 | paths.push_back("c:\\foo/"); |
| 101 | paths.push_back("c:/foo\\bar"); |
| 102 | |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 103 | SmallVector<StringRef, 5> ComponentStack; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 104 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 105 | e = paths.end(); |
| 106 | i != e; |
| 107 | ++i) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 108 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 109 | ce = sys::path::end(*i); |
| 110 | ci != ce; |
| 111 | ++ci) { |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 112 | ASSERT_FALSE(ci->empty()); |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 113 | ComponentStack.push_back(*ci); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 114 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 115 | |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 116 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 117 | ce = sys::path::rend(*i); |
| 118 | ci != ce; |
| 119 | ++ci) { |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 120 | ASSERT_TRUE(*ci == ComponentStack.back()); |
| 121 | ComponentStack.pop_back(); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 122 | } |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 123 | ASSERT_TRUE(ComponentStack.empty()); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 124 | |
Justin Bogner | 0db71d9 | 2016-10-16 22:09:24 +0000 | [diff] [blame] | 125 | // Crash test most of the API - since we're iterating over all of our paths |
| 126 | // here there isn't really anything reasonable to assert on in the results. |
| 127 | (void)path::has_root_path(*i); |
| 128 | (void)path::root_path(*i); |
| 129 | (void)path::has_root_name(*i); |
| 130 | (void)path::root_name(*i); |
| 131 | (void)path::has_root_directory(*i); |
| 132 | (void)path::root_directory(*i); |
| 133 | (void)path::has_parent_path(*i); |
| 134 | (void)path::parent_path(*i); |
| 135 | (void)path::has_filename(*i); |
| 136 | (void)path::filename(*i); |
| 137 | (void)path::has_stem(*i); |
| 138 | (void)path::stem(*i); |
| 139 | (void)path::has_extension(*i); |
| 140 | (void)path::extension(*i); |
| 141 | (void)path::is_absolute(*i); |
| 142 | (void)path::is_relative(*i); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 143 | |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 144 | SmallString<128> temp_store; |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 145 | temp_store = *i; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 146 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 147 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 148 | path::remove_filename(temp_store); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 149 | |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 150 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 151 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 152 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 153 | stem = path::stem(filename); |
| 154 | ext = path::extension(filename); |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame] | 155 | EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str()); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 156 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 157 | path::native(*i, temp_store); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 158 | } |
Benjamin Kramer | ae1d599 | 2015-10-05 13:02:43 +0000 | [diff] [blame] | 159 | |
| 160 | SmallString<32> Relative("foo.cpp"); |
| 161 | ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative)); |
Benjamin Kramer | 2b4e14e | 2015-10-05 14:15:13 +0000 | [diff] [blame] | 162 | Relative[5] = '/'; // Fix up windows paths. |
Benjamin Kramer | ae1d599 | 2015-10-05 13:02:43 +0000 | [diff] [blame] | 163 | ASSERT_EQ("/root/foo.cpp", Relative); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 164 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 165 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 166 | TEST(Support, RelativePathIterator) { |
| 167 | SmallString<64> Path(StringRef("c/d/e/foo.txt")); |
| 168 | typedef SmallVector<StringRef, 4> PathComponents; |
| 169 | PathComponents ExpectedPathComponents; |
| 170 | PathComponents ActualPathComponents; |
| 171 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 172 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 173 | |
| 174 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 175 | ++I) { |
| 176 | ActualPathComponents.push_back(*I); |
| 177 | } |
| 178 | |
| 179 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 180 | |
| 181 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 182 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 183 | } |
| 184 | } |
| 185 | |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 186 | TEST(Support, RelativePathDotIterator) { |
| 187 | SmallString<64> Path(StringRef(".c/.d/../.")); |
| 188 | typedef SmallVector<StringRef, 4> PathComponents; |
| 189 | PathComponents ExpectedPathComponents; |
| 190 | PathComponents ActualPathComponents; |
| 191 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 192 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 193 | |
| 194 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 195 | ++I) { |
| 196 | ActualPathComponents.push_back(*I); |
| 197 | } |
| 198 | |
| 199 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 200 | |
| 201 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 202 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 203 | } |
| 204 | } |
| 205 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 206 | TEST(Support, AbsolutePathIterator) { |
| 207 | SmallString<64> Path(StringRef("/c/d/e/foo.txt")); |
| 208 | typedef SmallVector<StringRef, 4> PathComponents; |
| 209 | PathComponents ExpectedPathComponents; |
| 210 | PathComponents ActualPathComponents; |
| 211 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 212 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 213 | |
| 214 | // The root path will also be a component when iterating |
| 215 | ExpectedPathComponents[0] = "/"; |
| 216 | |
| 217 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 218 | ++I) { |
| 219 | ActualPathComponents.push_back(*I); |
| 220 | } |
| 221 | |
| 222 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 223 | |
| 224 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 225 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 226 | } |
| 227 | } |
| 228 | |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 229 | TEST(Support, AbsolutePathDotIterator) { |
| 230 | SmallString<64> Path(StringRef("/.c/.d/../.")); |
| 231 | typedef SmallVector<StringRef, 4> PathComponents; |
| 232 | PathComponents ExpectedPathComponents; |
| 233 | PathComponents ActualPathComponents; |
| 234 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 235 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 236 | |
| 237 | // The root path will also be a component when iterating |
| 238 | ExpectedPathComponents[0] = "/"; |
| 239 | |
| 240 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 241 | ++I) { |
| 242 | ActualPathComponents.push_back(*I); |
| 243 | } |
| 244 | |
| 245 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 246 | |
| 247 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 248 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 249 | } |
| 250 | } |
| 251 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 252 | #ifdef LLVM_ON_WIN32 |
| 253 | TEST(Support, AbsolutePathIteratorWin32) { |
| 254 | SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); |
| 255 | typedef SmallVector<StringRef, 4> PathComponents; |
| 256 | PathComponents ExpectedPathComponents; |
| 257 | PathComponents ActualPathComponents; |
| 258 | |
| 259 | StringRef(Path).split(ExpectedPathComponents, "\\"); |
| 260 | |
| 261 | // The root path (which comes after the drive name) will also be a component |
| 262 | // when iterating. |
| 263 | ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); |
| 264 | |
| 265 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 266 | ++I) { |
| 267 | ActualPathComponents.push_back(*I); |
| 268 | } |
| 269 | |
| 270 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 271 | |
| 272 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 273 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 274 | } |
| 275 | } |
| 276 | #endif // LLVM_ON_WIN32 |
| 277 | |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 278 | TEST(Support, AbsolutePathIteratorEnd) { |
| 279 | // Trailing slashes are converted to '.' unless they are part of the root path. |
| 280 | SmallVector<StringRef, 4> Paths; |
| 281 | Paths.push_back("/foo/"); |
| 282 | Paths.push_back("/foo//"); |
| 283 | Paths.push_back("//net//"); |
| 284 | #ifdef LLVM_ON_WIN32 |
| 285 | Paths.push_back("c:\\\\"); |
| 286 | #endif |
| 287 | |
| 288 | for (StringRef Path : Paths) { |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame] | 289 | StringRef LastComponent = *path::rbegin(Path); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 290 | EXPECT_EQ(".", LastComponent); |
| 291 | } |
| 292 | |
| 293 | SmallVector<StringRef, 3> RootPaths; |
| 294 | RootPaths.push_back("/"); |
| 295 | RootPaths.push_back("//net/"); |
| 296 | #ifdef LLVM_ON_WIN32 |
| 297 | RootPaths.push_back("c:\\"); |
| 298 | #endif |
| 299 | |
| 300 | for (StringRef Path : RootPaths) { |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame] | 301 | StringRef LastComponent = *path::rbegin(Path); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 302 | EXPECT_EQ(1u, LastComponent.size()); |
| 303 | EXPECT_TRUE(path::is_separator(LastComponent[0])); |
| 304 | } |
| 305 | } |
| 306 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 307 | TEST(Support, HomeDirectory) { |
NAKAMURA Takumi | cc275e4 | 2015-10-16 09:40:01 +0000 | [diff] [blame] | 308 | std::string expected; |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 309 | #ifdef LLVM_ON_WIN32 |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 310 | if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) { |
| 311 | auto pathLen = ::wcslen(path); |
| 312 | ArrayRef<char> ref{reinterpret_cast<char const *>(path), |
| 313 | pathLen * sizeof(wchar_t)}; |
| 314 | convertUTF16ToUTF8String(ref, expected); |
| 315 | } |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 316 | #else |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 317 | if (char const *path = ::getenv("HOME")) |
| 318 | expected = path; |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 319 | #endif |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 320 | // Do not try to test it if we don't know what to expect. |
| 321 | // On Windows we use something better than env vars. |
| 322 | if (!expected.empty()) { |
NAKAMURA Takumi | cc275e4 | 2015-10-16 09:40:01 +0000 | [diff] [blame] | 323 | SmallString<128> HomeDir; |
| 324 | auto status = path::home_directory(HomeDir); |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 325 | EXPECT_TRUE(status); |
NAKAMURA Takumi | cc275e4 | 2015-10-16 09:40:01 +0000 | [diff] [blame] | 326 | EXPECT_EQ(expected, HomeDir); |
| 327 | } |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Pawel Bylica | 0e97e5c | 2015-11-02 09:49:17 +0000 | [diff] [blame] | 330 | TEST(Support, UserCacheDirectory) { |
| 331 | SmallString<13> CacheDir; |
| 332 | SmallString<20> CacheDir2; |
| 333 | auto Status = path::user_cache_directory(CacheDir, ""); |
| 334 | EXPECT_TRUE(Status ^ CacheDir.empty()); |
| 335 | |
| 336 | if (Status) { |
| 337 | EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed |
| 338 | EXPECT_EQ(CacheDir, CacheDir2); // and return same paths |
| 339 | |
| 340 | EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c")); |
| 341 | auto It = path::rbegin(CacheDir); |
| 342 | EXPECT_EQ("file.c", *It); |
| 343 | EXPECT_EQ("B", *++It); |
| 344 | EXPECT_EQ("A", *++It); |
| 345 | auto ParentDir = *++It; |
| 346 | |
| 347 | // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0" |
| 348 | EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2", |
| 349 | "\xE2\x84\xB5.0")); |
| 350 | auto It2 = path::rbegin(CacheDir2); |
| 351 | EXPECT_EQ("\xE2\x84\xB5.0", *It2); |
| 352 | EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2); |
| 353 | auto ParentDir2 = *++It2; |
| 354 | |
| 355 | EXPECT_EQ(ParentDir, ParentDir2); |
| 356 | } |
| 357 | } |
| 358 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 359 | TEST(Support, TempDirectory) { |
| 360 | SmallString<32> TempDir; |
| 361 | path::system_temp_directory(false, TempDir); |
| 362 | EXPECT_TRUE(!TempDir.empty()); |
| 363 | TempDir.clear(); |
| 364 | path::system_temp_directory(true, TempDir); |
| 365 | EXPECT_TRUE(!TempDir.empty()); |
| 366 | } |
| 367 | |
David Blaikie | 06d5618 | 2015-11-17 20:38:54 +0000 | [diff] [blame] | 368 | #ifdef LLVM_ON_WIN32 |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 369 | static std::string path2regex(std::string Path) { |
| 370 | size_t Pos = 0; |
| 371 | while ((Pos = Path.find('\\', Pos)) != std::string::npos) { |
| 372 | Path.replace(Pos, 1, "\\\\"); |
| 373 | Pos += 2; |
| 374 | } |
| 375 | return Path; |
| 376 | } |
| 377 | |
| 378 | /// Helper for running temp dir test in separated process. See below. |
| 379 | #define EXPECT_TEMP_DIR(prepare, expected) \ |
| 380 | EXPECT_EXIT( \ |
| 381 | { \ |
| 382 | prepare; \ |
| 383 | SmallString<300> TempDir; \ |
| 384 | path::system_temp_directory(true, TempDir); \ |
| 385 | raw_os_ostream(std::cerr) << TempDir; \ |
| 386 | std::exit(0); \ |
| 387 | }, \ |
| 388 | ::testing::ExitedWithCode(0), path2regex(expected)) |
| 389 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 390 | TEST(SupportDeathTest, TempDirectoryOnWindows) { |
| 391 | // In this test we want to check how system_temp_directory responds to |
| 392 | // different values of specific env vars. To prevent corrupting env vars of |
| 393 | // the current process all checks are done in separated processes. |
| 394 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder"); |
| 395 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"), |
| 396 | "C:\\Unix\\Path\\Seperators"); |
| 397 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$"); |
| 398 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep"); |
| 399 | EXPECT_TEMP_DIR( |
| 400 | _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"), |
| 401 | "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80"); |
| 402 | |
| 403 | // Test $TMP empty, $TEMP set. |
| 404 | EXPECT_TEMP_DIR( |
| 405 | { |
| 406 | _wputenv_s(L"TMP", L""); |
| 407 | _wputenv_s(L"TEMP", L"C:\\Valid\\Path"); |
| 408 | }, |
| 409 | "C:\\Valid\\Path"); |
| 410 | |
| 411 | // All related env vars empty |
| 412 | EXPECT_TEMP_DIR( |
| 413 | { |
| 414 | _wputenv_s(L"TMP", L""); |
| 415 | _wputenv_s(L"TEMP", L""); |
| 416 | _wputenv_s(L"USERPROFILE", L""); |
| 417 | }, |
| 418 | "C:\\Temp"); |
| 419 | |
| 420 | // Test evn var / path with 260 chars. |
| 421 | SmallString<270> Expected{"C:\\Temp\\AB\\123456789"}; |
| 422 | while (Expected.size() < 260) |
| 423 | Expected.append("\\DirNameWith19Charss"); |
Reid Kleckner | 88e9ff6 | 2016-02-10 19:29:01 +0000 | [diff] [blame] | 424 | ASSERT_EQ(260U, Expected.size()); |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 425 | EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str()); |
| 426 | } |
| 427 | #endif |
| 428 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 429 | class FileSystemTest : public testing::Test { |
| 430 | protected: |
| 431 | /// Unique temporary directory in which all created filesystem entities must |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 432 | /// be placed. It is removed at the end of each test (must be empty). |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 433 | SmallString<128> TestDirectory; |
| 434 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 435 | void SetUp() override { |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 436 | ASSERT_NO_ERROR( |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 437 | fs::createUniqueDirectory("file-system-test", TestDirectory)); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 438 | // We don't care about this specific file. |
Michael J. Spencer | 42368cc | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 439 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 440 | errs().flush(); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 443 | void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); } |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 446 | TEST_F(FileSystemTest, Unique) { |
| 447 | // Create a temp file. |
| 448 | int FileDescriptor; |
| 449 | SmallString<64> TempPath; |
| 450 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 451 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 452 | |
| 453 | // The same file should return an identical unique id. |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 454 | fs::UniqueID F1, F2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 455 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); |
| 456 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 457 | ASSERT_EQ(F1, F2); |
| 458 | |
| 459 | // Different files should return different unique ids. |
| 460 | int FileDescriptor2; |
| 461 | SmallString<64> TempPath2; |
| 462 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 463 | fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); |
| 464 | |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 465 | fs::UniqueID D; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 466 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 467 | ASSERT_NE(D, F1); |
| 468 | ::close(FileDescriptor2); |
| 469 | |
| 470 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 471 | |
| 472 | // Two paths representing the same file on disk should still provide the |
| 473 | // same unique id. We can test this by making a hard link. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 474 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 475 | fs::UniqueID D2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 476 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 477 | ASSERT_EQ(D2, F1); |
| 478 | |
| 479 | ::close(FileDescriptor); |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 480 | |
| 481 | SmallString<128> Dir1; |
| 482 | ASSERT_NO_ERROR( |
| 483 | fs::createUniqueDirectory("dir1", Dir1)); |
| 484 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); |
| 485 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); |
| 486 | ASSERT_EQ(F1, F2); |
| 487 | |
| 488 | SmallString<128> Dir2; |
| 489 | ASSERT_NO_ERROR( |
| 490 | fs::createUniqueDirectory("dir2", Dir2)); |
| 491 | ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); |
| 492 | ASSERT_NE(F1, F2); |
Reid Kleckner | 1a4398a | 2016-09-02 01:10:53 +0000 | [diff] [blame] | 493 | ASSERT_NO_ERROR(fs::remove(Dir1)); |
| 494 | ASSERT_NO_ERROR(fs::remove(Dir2)); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 495 | ASSERT_NO_ERROR(fs::remove(TempPath2)); |
| 496 | ASSERT_NO_ERROR(fs::remove(TempPath)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 499 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 500 | // Create a temp file. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 501 | int FileDescriptor; |
| 502 | SmallString<64> TempPath; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 503 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 504 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 505 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 506 | // Make sure it exists. |
Rafael Espindola | c159915 | 2014-09-11 19:11:02 +0000 | [diff] [blame] | 507 | ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 508 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 509 | // Create another temp tile. |
| 510 | int FD2; |
| 511 | SmallString<64> TempPath2; |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 512 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 513 | ASSERT_TRUE(TempPath2.endswith(".temp")); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 514 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 515 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 516 | fs::file_status A, B; |
| 517 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 518 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 519 | EXPECT_FALSE(fs::equivalent(A, B)); |
| 520 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 521 | ::close(FD2); |
Rafael Espindola | 213c4cb | 2013-07-18 03:29:51 +0000 | [diff] [blame] | 522 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 523 | // Remove Temp2. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 524 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 525 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 526 | ASSERT_EQ(fs::remove(Twine(TempPath2), false), |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 527 | errc::no_such_file_or_directory); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 528 | |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 529 | std::error_code EC = fs::status(TempPath2.c_str(), B); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 530 | EXPECT_EQ(EC, errc::no_such_file_or_directory); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 531 | EXPECT_EQ(B.type(), fs::file_type::file_not_found); |
| 532 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 533 | // Make sure Temp2 doesn't exist. |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 534 | ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist), |
| 535 | errc::no_such_file_or_directory); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 536 | |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 537 | SmallString<64> TempPath3; |
| 538 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); |
| 539 | ASSERT_FALSE(TempPath3.endswith(".")); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 540 | FileRemover Cleanup3(TempPath3); |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 541 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 542 | // Create a hard link to Temp1. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 543 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 544 | bool equal; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 545 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 546 | EXPECT_TRUE(equal); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 547 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 548 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 549 | EXPECT_TRUE(fs::equivalent(A, B)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 550 | |
| 551 | // Remove Temp1. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 552 | ::close(FileDescriptor); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 553 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 554 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 555 | // Remove the hard link. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 556 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 557 | |
| 558 | // Make sure Temp1 doesn't exist. |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 559 | ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist), |
| 560 | errc::no_such_file_or_directory); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 561 | |
| 562 | #ifdef LLVM_ON_WIN32 |
| 563 | // Path name > 260 chars should get an error. |
| 564 | const char *Path270 = |
| 565 | "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" |
| 566 | "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" |
| 567 | "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" |
| 568 | "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" |
| 569 | "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 570 | EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath), |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 571 | errc::invalid_argument); |
| 572 | // Relative path < 247 chars, no problem. |
| 573 | const char *Path216 = |
| 574 | "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" |
| 575 | "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" |
| 576 | "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" |
| 577 | "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 578 | ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath)); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 579 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 580 | #endif |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 581 | } |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 582 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 583 | TEST_F(FileSystemTest, CreateDir) { |
| 584 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 585 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 586 | ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 587 | errc::file_exists); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 588 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 589 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 590 | #ifdef LLVM_ON_UNIX |
| 591 | // Set a 0000 umask so that we can test our directory permissions. |
| 592 | mode_t OldUmask = ::umask(0000); |
| 593 | |
| 594 | fs::file_status Status; |
| 595 | ASSERT_NO_ERROR( |
| 596 | fs::create_directory(Twine(TestDirectory) + "baz500", false, |
| 597 | fs::perms::owner_read | fs::perms::owner_exe)); |
| 598 | ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status)); |
| 599 | ASSERT_EQ(Status.permissions() & fs::perms::all_all, |
| 600 | fs::perms::owner_read | fs::perms::owner_exe); |
| 601 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false, |
| 602 | fs::perms::all_all)); |
| 603 | ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status)); |
| 604 | ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all); |
| 605 | |
| 606 | // Restore umask to be safe. |
| 607 | ::umask(OldUmask); |
| 608 | #endif |
| 609 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 610 | #ifdef LLVM_ON_WIN32 |
| 611 | // Prove that create_directories() can handle a pathname > 248 characters, |
| 612 | // which is the documented limit for CreateDirectory(). |
| 613 | // (248 is MAX_PATH subtracting room for an 8.3 filename.) |
| 614 | // Generate a directory path guaranteed to fall into that range. |
| 615 | size_t TmpLen = TestDirectory.size(); |
| 616 | const char *OneDir = "\\123456789"; |
| 617 | size_t OneDirLen = strlen(OneDir); |
Aaron Ballman | 493456e | 2014-11-13 13:39:49 +0000 | [diff] [blame] | 618 | ASSERT_LT(OneDirLen, 12U); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 619 | size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1; |
| 620 | SmallString<260> LongDir(TestDirectory); |
| 621 | for (size_t I = 0; I < NLevels; ++I) |
| 622 | LongDir.append(OneDir); |
| 623 | ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); |
| 624 | ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); |
| 625 | ASSERT_EQ(fs::create_directories(Twine(LongDir), false), |
| 626 | errc::file_exists); |
| 627 | // Tidy up, "recursively" removing the directories. |
| 628 | StringRef ThisDir(LongDir); |
| 629 | for (size_t J = 0; J < NLevels; ++J) { |
| 630 | ASSERT_NO_ERROR(fs::remove(ThisDir)); |
| 631 | ThisDir = path::parent_path(ThisDir); |
| 632 | } |
| 633 | |
| 634 | // Similarly for a relative pathname. Need to set the current directory to |
| 635 | // TestDirectory so that the one we create ends up in the right place. |
| 636 | char PreviousDir[260]; |
| 637 | size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir); |
Aaron Ballman | 493456e | 2014-11-13 13:39:49 +0000 | [diff] [blame] | 638 | ASSERT_GT(PreviousDirLen, 0U); |
| 639 | ASSERT_LT(PreviousDirLen, 260U); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 640 | ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0); |
| 641 | LongDir.clear(); |
| 642 | // Generate a relative directory name with absolute length > 248. |
| 643 | size_t LongDirLen = 249 - TestDirectory.size(); |
| 644 | LongDir.assign(LongDirLen, 'a'); |
| 645 | ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir))); |
| 646 | // While we're here, prove that .. and . handling works in these long paths. |
| 647 | const char *DotDotDirs = "\\..\\.\\b"; |
| 648 | LongDir.append(DotDotDirs); |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 649 | ASSERT_NO_ERROR(fs::create_directory("b")); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 650 | ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists); |
| 651 | // And clean up. |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 652 | ASSERT_NO_ERROR(fs::remove("b")); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 653 | ASSERT_NO_ERROR(fs::remove( |
| 654 | Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs))))); |
| 655 | ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0); |
| 656 | #endif |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 659 | TEST_F(FileSystemTest, DirectoryIteration) { |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 660 | std::error_code ec; |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 661 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 662 | ASSERT_NO_ERROR(ec); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 663 | |
| 664 | // Create a known hierarchy to recurse over. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 665 | ASSERT_NO_ERROR( |
| 666 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 667 | ASSERT_NO_ERROR( |
| 668 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 669 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + |
| 670 | "/recursive/dontlookhere/da1")); |
| 671 | ASSERT_NO_ERROR( |
| 672 | fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 673 | ASSERT_NO_ERROR( |
| 674 | fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 675 | typedef std::vector<std::string> v_t; |
| 676 | v_t visited; |
| 677 | for (fs::recursive_directory_iterator i(Twine(TestDirectory) |
| 678 | + "/recursive", ec), e; i != e; i.increment(ec)){ |
| 679 | ASSERT_NO_ERROR(ec); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 680 | if (path::filename(i->path()) == "p1") { |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 681 | i.pop(); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 682 | // FIXME: recursive_directory_iterator should be more robust. |
| 683 | if (i == e) break; |
| 684 | } |
Michael J. Spencer | 9b54a3e | 2011-12-09 01:14:41 +0000 | [diff] [blame] | 685 | if (path::filename(i->path()) == "dontlookhere") |
| 686 | i.no_push(); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 687 | visited.push_back(path::filename(i->path())); |
| 688 | } |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 689 | v_t::const_iterator a0 = find(visited, "a0"); |
| 690 | v_t::const_iterator aa1 = find(visited, "aa1"); |
| 691 | v_t::const_iterator ab1 = find(visited, "ab1"); |
| 692 | v_t::const_iterator dontlookhere = find(visited, "dontlookhere"); |
| 693 | v_t::const_iterator da1 = find(visited, "da1"); |
| 694 | v_t::const_iterator z0 = find(visited, "z0"); |
| 695 | v_t::const_iterator za1 = find(visited, "za1"); |
| 696 | v_t::const_iterator pop = find(visited, "pop"); |
| 697 | v_t::const_iterator p1 = find(visited, "p1"); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 698 | |
| 699 | // Make sure that each path was visited correctly. |
| 700 | ASSERT_NE(a0, visited.end()); |
| 701 | ASSERT_NE(aa1, visited.end()); |
| 702 | ASSERT_NE(ab1, visited.end()); |
| 703 | ASSERT_NE(dontlookhere, visited.end()); |
| 704 | ASSERT_EQ(da1, visited.end()); // Not visited. |
| 705 | ASSERT_NE(z0, visited.end()); |
| 706 | ASSERT_NE(za1, visited.end()); |
| 707 | ASSERT_NE(pop, visited.end()); |
| 708 | ASSERT_EQ(p1, visited.end()); // Not visited. |
| 709 | |
| 710 | // Make sure that parents were visited before children. No other ordering |
| 711 | // guarantees can be made across siblings. |
| 712 | ASSERT_LT(a0, aa1); |
| 713 | ASSERT_LT(a0, ab1); |
| 714 | ASSERT_LT(z0, za1); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 715 | |
| 716 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 717 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 718 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); |
| 719 | ASSERT_NO_ERROR( |
| 720 | fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); |
| 721 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); |
| 722 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); |
| 723 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); |
| 724 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 725 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); |
| 726 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); |
Bruno Cardoso Lopes | ead771c | 2016-05-13 21:31:32 +0000 | [diff] [blame] | 727 | |
| 728 | // Test recursive_directory_iterator level() |
| 729 | ASSERT_NO_ERROR( |
| 730 | fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c")); |
| 731 | fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E; |
| 732 | for (int l = 0; I != E; I.increment(ec), ++l) { |
| 733 | ASSERT_NO_ERROR(ec); |
| 734 | EXPECT_EQ(I.level(), l); |
| 735 | } |
| 736 | EXPECT_EQ(I, E); |
| 737 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c")); |
| 738 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b")); |
| 739 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a")); |
| 740 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel")); |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 741 | } |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 742 | |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 743 | const char archive[] = "!<arch>\x0A"; |
| 744 | const char bitcode[] = "\xde\xc0\x17\x0b"; |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 745 | const char coff_object[] = "\x00\x00......"; |
Rui Ueyama | 5c69ff5 | 2014-09-11 22:34:32 +0000 | [diff] [blame] | 746 | const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......" |
Rui Ueyama | 2acb058 | 2014-09-11 21:09:57 +0000 | [diff] [blame] | 747 | "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8"; |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 748 | const char coff_import_library[] = "\x00\x00\xff\xff...."; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 749 | const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, |
| 750 | 0, 0, 0, 0, 0, 0, 0, 0, 1 }; |
Etienne Bergeron | 1562f69 | 2016-04-05 01:46:26 +0000 | [diff] [blame] | 751 | const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00"; |
Kevin Enderby | 87c85b7 | 2016-01-26 23:43:37 +0000 | [diff] [blame] | 752 | const char macho_object[] = |
| 753 | "\xfe\xed\xfa\xce........\x00\x00\x00\x01............"; |
| 754 | const char macho_executable[] = |
| 755 | "\xfe\xed\xfa\xce........\x00\x00\x00\x02............"; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 756 | const char macho_fixed_virtual_memory_shared_lib[] = |
Kevin Enderby | 87c85b7 | 2016-01-26 23:43:37 +0000 | [diff] [blame] | 757 | "\xfe\xed\xfa\xce........\x00\x00\x00\x03............"; |
| 758 | const char macho_core[] = |
| 759 | "\xfe\xed\xfa\xce........\x00\x00\x00\x04............"; |
| 760 | const char macho_preload_executable[] = |
| 761 | "\xfe\xed\xfa\xce........\x00\x00\x00\x05............"; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 762 | const char macho_dynamically_linked_shared_lib[] = |
Kevin Enderby | 87c85b7 | 2016-01-26 23:43:37 +0000 | [diff] [blame] | 763 | "\xfe\xed\xfa\xce........\x00\x00\x00\x06............"; |
| 764 | const char macho_dynamic_linker[] = |
| 765 | "\xfe\xed\xfa\xce........\x00\x00\x00\x07............"; |
| 766 | const char macho_bundle[] = |
| 767 | "\xfe\xed\xfa\xce........\x00\x00\x00\x08............"; |
| 768 | const char macho_dsym_companion[] = |
| 769 | "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............"; |
| 770 | const char macho_kext_bundle[] = |
| 771 | "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............"; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 772 | const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; |
Nick Kledzik | 2d2b254 | 2014-09-17 00:53:44 +0000 | [diff] [blame] | 773 | const char macho_dynamically_linked_shared_lib_stub[] = |
Kevin Enderby | 87c85b7 | 2016-01-26 23:43:37 +0000 | [diff] [blame] | 774 | "\xfe\xed\xfa\xce........\x00\x00\x00\x09............"; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 775 | |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 776 | TEST_F(FileSystemTest, Magic) { |
| 777 | struct type { |
| 778 | const char *filename; |
| 779 | const char *magic_str; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 780 | size_t magic_str_len; |
| 781 | fs::file_magic magic; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 782 | } types[] = { |
| 783 | #define DEFINE(magic) \ |
| 784 | { #magic, magic, sizeof(magic), fs::file_magic::magic } |
| 785 | DEFINE(archive), |
| 786 | DEFINE(bitcode), |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 787 | DEFINE(coff_object), |
Rui Ueyama | 2acb058 | 2014-09-11 21:09:57 +0000 | [diff] [blame] | 788 | { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object }, |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 789 | DEFINE(coff_import_library), |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 790 | DEFINE(elf_relocatable), |
| 791 | DEFINE(macho_universal_binary), |
| 792 | DEFINE(macho_object), |
| 793 | DEFINE(macho_executable), |
| 794 | DEFINE(macho_fixed_virtual_memory_shared_lib), |
| 795 | DEFINE(macho_core), |
| 796 | DEFINE(macho_preload_executable), |
| 797 | DEFINE(macho_dynamically_linked_shared_lib), |
| 798 | DEFINE(macho_dynamic_linker), |
| 799 | DEFINE(macho_bundle), |
Nick Kledzik | 2d2b254 | 2014-09-17 00:53:44 +0000 | [diff] [blame] | 800 | DEFINE(macho_dynamically_linked_shared_lib_stub), |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 801 | DEFINE(macho_dsym_companion), |
Justin Bogner | a7ad4b3 | 2015-02-25 22:59:20 +0000 | [diff] [blame] | 802 | DEFINE(macho_kext_bundle), |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 803 | DEFINE(windows_resource) |
| 804 | #undef DEFINE |
| 805 | }; |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 806 | |
| 807 | // Create some files filled with magic. |
| 808 | for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; |
| 809 | ++i) { |
| 810 | SmallString<128> file_pathname(TestDirectory); |
| 811 | path::append(file_pathname, i->filename); |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 812 | std::error_code EC; |
| 813 | raw_fd_ostream file(file_pathname, EC, sys::fs::F_None); |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 814 | ASSERT_FALSE(file.has_error()); |
| 815 | StringRef magic(i->magic_str, i->magic_str_len); |
| 816 | file << magic; |
Michael J. Spencer | b587180 | 2011-01-15 18:52:49 +0000 | [diff] [blame] | 817 | file.close(); |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 818 | EXPECT_EQ(i->magic, fs::identify_magic(magic)); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 819 | ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 820 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 823 | #ifdef LLVM_ON_WIN32 |
| 824 | TEST_F(FileSystemTest, CarriageReturn) { |
| 825 | SmallString<128> FilePathname(TestDirectory); |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 826 | std::error_code EC; |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 827 | path::append(FilePathname, "test"); |
| 828 | |
| 829 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 830 | raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text); |
| 831 | ASSERT_NO_ERROR(EC); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 832 | File << '\n'; |
| 833 | } |
| 834 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 835 | auto Buf = MemoryBuffer::getFile(FilePathname.str()); |
Aaron Ballman | 586ee60 | 2014-07-06 20:20:02 +0000 | [diff] [blame] | 836 | EXPECT_TRUE((bool)Buf); |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 837 | EXPECT_EQ(Buf.get()->getBuffer(), "\r\n"); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 841 | raw_fd_ostream File(FilePathname, EC, sys::fs::F_None); |
| 842 | ASSERT_NO_ERROR(EC); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 843 | File << '\n'; |
| 844 | } |
| 845 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 846 | auto Buf = MemoryBuffer::getFile(FilePathname.str()); |
Aaron Ballman | 586ee60 | 2014-07-06 20:20:02 +0000 | [diff] [blame] | 847 | EXPECT_TRUE((bool)Buf); |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 848 | EXPECT_EQ(Buf.get()->getBuffer(), "\n"); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 849 | } |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 850 | ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 851 | } |
| 852 | #endif |
| 853 | |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 854 | TEST_F(FileSystemTest, Resize) { |
| 855 | int FD; |
| 856 | SmallString<64> TempPath; |
| 857 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); |
| 858 | ASSERT_NO_ERROR(fs::resize_file(FD, 123)); |
| 859 | fs::file_status Status; |
| 860 | ASSERT_NO_ERROR(fs::status(FD, Status)); |
| 861 | ASSERT_EQ(Status.getSize(), 123U); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 862 | ::close(FD); |
| 863 | ASSERT_NO_ERROR(fs::remove(TempPath)); |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 866 | TEST_F(FileSystemTest, FileMapping) { |
| 867 | // Create a temp file. |
| 868 | int FileDescriptor; |
| 869 | SmallString<64> TempPath; |
| 870 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 871 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 872 | unsigned Size = 4096; |
| 873 | ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size)); |
| 874 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 875 | // Map in temp file and add some content |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 876 | std::error_code EC; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 877 | StringRef Val("hello there"); |
| 878 | { |
| 879 | fs::mapped_file_region mfr(FileDescriptor, |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 880 | fs::mapped_file_region::readwrite, Size, 0, EC); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 881 | ASSERT_NO_ERROR(EC); |
| 882 | std::copy(Val.begin(), Val.end(), mfr.data()); |
| 883 | // Explicitly add a 0. |
| 884 | mfr.data()[Val.size()] = 0; |
| 885 | // Unmap temp file |
| 886 | } |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 887 | ASSERT_EQ(close(FileDescriptor), 0); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 888 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 889 | // Map it back in read-only |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 890 | { |
| 891 | int FD; |
| 892 | EC = fs::openFileForRead(Twine(TempPath), FD); |
| 893 | ASSERT_NO_ERROR(EC); |
| 894 | fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC); |
| 895 | ASSERT_NO_ERROR(EC); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 896 | |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 897 | // Verify content |
| 898 | EXPECT_EQ(StringRef(mfr.const_data()), Val); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 899 | |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 900 | // Unmap temp file |
| 901 | fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC); |
| 902 | ASSERT_NO_ERROR(EC); |
| 903 | ASSERT_EQ(close(FD), 0); |
| 904 | } |
| 905 | ASSERT_NO_ERROR(fs::remove(TempPath)); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 906 | } |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 907 | |
| 908 | TEST(Support, NormalizePath) { |
| 909 | #if defined(LLVM_ON_WIN32) |
| 910 | #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ |
| 911 | EXPECT_EQ(path__, windows__); |
| 912 | #else |
| 913 | #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ |
| 914 | EXPECT_EQ(path__, not_windows__); |
| 915 | #endif |
| 916 | |
| 917 | SmallString<64> Path1("a"); |
| 918 | SmallString<64> Path2("a/b"); |
| 919 | SmallString<64> Path3("a\\b"); |
| 920 | SmallString<64> Path4("a\\\\b"); |
| 921 | SmallString<64> Path5("\\a"); |
| 922 | SmallString<64> Path6("a\\"); |
| 923 | |
Rafael Espindola | 7e774c2 | 2014-08-08 21:35:52 +0000 | [diff] [blame] | 924 | path::native(Path1); |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 925 | EXPECT_PATH_IS(Path1, "a", "a"); |
| 926 | |
Rafael Espindola | 7e774c2 | 2014-08-08 21:35:52 +0000 | [diff] [blame] | 927 | path::native(Path2); |
Rafael Espindola | ffbabb7 | 2014-08-09 00:37:05 +0000 | [diff] [blame] | 928 | EXPECT_PATH_IS(Path2, "a\\b", "a/b"); |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 929 | |
Rafael Espindola | 7e774c2 | 2014-08-08 21:35:52 +0000 | [diff] [blame] | 930 | path::native(Path3); |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 931 | EXPECT_PATH_IS(Path3, "a\\b", "a/b"); |
| 932 | |
Rafael Espindola | 7e774c2 | 2014-08-08 21:35:52 +0000 | [diff] [blame] | 933 | path::native(Path4); |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 934 | EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); |
| 935 | |
Rafael Espindola | 7e774c2 | 2014-08-08 21:35:52 +0000 | [diff] [blame] | 936 | path::native(Path5); |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 937 | EXPECT_PATH_IS(Path5, "\\a", "/a"); |
| 938 | |
Rafael Espindola | 7e774c2 | 2014-08-08 21:35:52 +0000 | [diff] [blame] | 939 | path::native(Path6); |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 940 | EXPECT_PATH_IS(Path6, "a\\", "a/"); |
| 941 | |
| 942 | #undef EXPECT_PATH_IS |
| 943 | } |
Douglas Katzman | a26be4a | 2015-09-02 21:02:10 +0000 | [diff] [blame] | 944 | |
| 945 | TEST(Support, RemoveLeadingDotSlash) { |
| 946 | StringRef Path1("././/foolz/wat"); |
| 947 | StringRef Path2("./////"); |
| 948 | |
| 949 | Path1 = path::remove_leading_dotslash(Path1); |
| 950 | EXPECT_EQ(Path1, "foolz/wat"); |
| 951 | Path2 = path::remove_leading_dotslash(Path2); |
| 952 | EXPECT_EQ(Path2, ""); |
| 953 | } |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 954 | |
| 955 | static std::string remove_dots(StringRef path, |
| 956 | bool remove_dot_dot) { |
| 957 | SmallString<256> buffer(path); |
| 958 | path::remove_dots(buffer, remove_dot_dot); |
| 959 | return buffer.str(); |
| 960 | } |
| 961 | |
| 962 | TEST(Support, RemoveDots) { |
Mike Aizatsky | f8cf713 | 2015-11-09 19:36:53 +0000 | [diff] [blame] | 963 | #if defined(LLVM_ON_WIN32) |
| 964 | EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false)); |
| 965 | EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false)); |
| 966 | |
| 967 | EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false)); |
| 968 | EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true)); |
| 969 | EXPECT_EQ("c", remove_dots(".\\.\\c", true)); |
Eric Liu | af5a28f | 2016-10-13 15:07:14 +0000 | [diff] [blame] | 970 | EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true)); |
| 971 | EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true)); |
Benjamin Kramer | 937dd7a | 2016-10-17 13:28:21 +0000 | [diff] [blame^] | 972 | EXPECT_EQ("\\a\\c", remove_dots("\\..\\..\\a\\c", true)); |
| 973 | EXPECT_EQ("\\a\\c", remove_dots("\\..\\a\\b\\\\..\\.\\.\\\\c", true)); |
Mike Aizatsky | f8cf713 | 2015-11-09 19:36:53 +0000 | [diff] [blame] | 974 | |
| 975 | SmallString<64> Path1(".\\.\\c"); |
| 976 | EXPECT_TRUE(path::remove_dots(Path1, true)); |
| 977 | EXPECT_EQ("c", Path1); |
| 978 | #else |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 979 | EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false)); |
| 980 | EXPECT_EQ("", remove_dots("./////", false)); |
| 981 | |
| 982 | EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false)); |
| 983 | EXPECT_EQ("b/c", remove_dots("./a/../b/c", true)); |
| 984 | EXPECT_EQ("c", remove_dots("././c", true)); |
Eric Liu | af5a28f | 2016-10-13 15:07:14 +0000 | [diff] [blame] | 985 | EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true)); |
| 986 | EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true)); |
Benjamin Kramer | 937dd7a | 2016-10-17 13:28:21 +0000 | [diff] [blame^] | 987 | EXPECT_EQ("/a/c", remove_dots("/../../a/c", true)); |
| 988 | EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true)); |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 989 | |
| 990 | SmallString<64> Path1("././c"); |
| 991 | EXPECT_TRUE(path::remove_dots(Path1, true)); |
| 992 | EXPECT_EQ("c", Path1); |
Mike Aizatsky | f8cf713 | 2015-11-09 19:36:53 +0000 | [diff] [blame] | 993 | #endif |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 994 | } |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 995 | |
| 996 | TEST(Support, ReplacePathPrefix) { |
| 997 | SmallString<64> Path1("/foo"); |
| 998 | SmallString<64> Path2("/old/foo"); |
| 999 | SmallString<64> OldPrefix("/old"); |
| 1000 | SmallString<64> NewPrefix("/new"); |
| 1001 | SmallString<64> NewPrefix2("/longernew"); |
| 1002 | SmallString<64> EmptyPrefix(""); |
| 1003 | |
| 1004 | SmallString<64> Path = Path1; |
| 1005 | path::replace_path_prefix(Path, OldPrefix, NewPrefix); |
| 1006 | EXPECT_EQ(Path, "/foo"); |
| 1007 | Path = Path2; |
| 1008 | path::replace_path_prefix(Path, OldPrefix, NewPrefix); |
| 1009 | EXPECT_EQ(Path, "/new/foo"); |
| 1010 | Path = Path2; |
| 1011 | path::replace_path_prefix(Path, OldPrefix, NewPrefix2); |
| 1012 | EXPECT_EQ(Path, "/longernew/foo"); |
| 1013 | Path = Path1; |
| 1014 | path::replace_path_prefix(Path, EmptyPrefix, NewPrefix); |
| 1015 | EXPECT_EQ(Path, "/new/foo"); |
| 1016 | Path = Path2; |
| 1017 | path::replace_path_prefix(Path, OldPrefix, EmptyPrefix); |
| 1018 | EXPECT_EQ(Path, "/foo"); |
| 1019 | } |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1020 | |
| 1021 | TEST_F(FileSystemTest, PathFromFD) { |
| 1022 | // Create a temp file. |
| 1023 | int FileDescriptor; |
| 1024 | SmallString<64> TempPath; |
| 1025 | ASSERT_NO_ERROR( |
| 1026 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1027 | FileRemover Cleanup(TempPath); |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1028 | |
| 1029 | // Make sure it exists. |
| 1030 | ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); |
| 1031 | |
| 1032 | // Try to get the path from the file descriptor |
| 1033 | SmallString<64> ResultPath; |
| 1034 | std::error_code ErrorCode = |
| 1035 | fs::getPathFromOpenFD(FileDescriptor, ResultPath); |
| 1036 | |
| 1037 | // If we succeeded, check that the paths are the same (modulo case): |
| 1038 | if (!ErrorCode) { |
| 1039 | // The paths returned by createTemporaryFile and getPathFromOpenFD |
| 1040 | // should reference the same file on disk. |
| 1041 | fs::UniqueID D1, D2; |
| 1042 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); |
| 1043 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); |
| 1044 | ASSERT_EQ(D1, D2); |
| 1045 | } |
| 1046 | |
| 1047 | ::close(FileDescriptor); |
| 1048 | } |
| 1049 | |
Aaron Ballman | 3dd74b8 | 2016-06-20 20:28:49 +0000 | [diff] [blame] | 1050 | TEST_F(FileSystemTest, PathFromFDWin32) { |
| 1051 | // Create a temp file. |
| 1052 | int FileDescriptor; |
| 1053 | SmallString<64> TempPath; |
| 1054 | ASSERT_NO_ERROR( |
| 1055 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1056 | FileRemover Cleanup(TempPath); |
Aaron Ballman | 3dd74b8 | 2016-06-20 20:28:49 +0000 | [diff] [blame] | 1057 | |
| 1058 | // Make sure it exists. |
| 1059 | ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); |
| 1060 | |
| 1061 | SmallVector<char, 8> ResultPath; |
| 1062 | std::error_code ErrorCode = |
| 1063 | fs::getPathFromOpenFD(FileDescriptor, ResultPath); |
| 1064 | |
| 1065 | if (!ErrorCode) { |
| 1066 | // Now that we know how much space is required for the path, create a path |
| 1067 | // buffer with exactly enough space (sans null terminator, which should not |
| 1068 | // be present), and call getPathFromOpenFD again to ensure that the API |
| 1069 | // properly handles exactly-sized buffers. |
| 1070 | SmallVector<char, 8> ExactSizedPath(ResultPath.size()); |
| 1071 | ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath); |
| 1072 | ResultPath = ExactSizedPath; |
| 1073 | } |
| 1074 | |
| 1075 | if (!ErrorCode) { |
| 1076 | fs::UniqueID D1, D2; |
| 1077 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); |
| 1078 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); |
| 1079 | ASSERT_EQ(D1, D2); |
| 1080 | } |
| 1081 | ::close(FileDescriptor); |
| 1082 | } |
| 1083 | |
Aaron Ballman | 0ad0046 | 2016-06-21 14:24:48 +0000 | [diff] [blame] | 1084 | TEST_F(FileSystemTest, PathFromFDUnicode) { |
| 1085 | // Create a temp file. |
| 1086 | int FileDescriptor; |
| 1087 | SmallString<64> TempPath; |
| 1088 | |
| 1089 | // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0" |
| 1090 | ASSERT_NO_ERROR( |
| 1091 | fs::createTemporaryFile("\xCF\x80r\xC2\xB2", |
| 1092 | "\xE2\x84\xB5.0", FileDescriptor, TempPath)); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1093 | FileRemover Cleanup(TempPath); |
Aaron Ballman | 0ad0046 | 2016-06-21 14:24:48 +0000 | [diff] [blame] | 1094 | |
| 1095 | // Make sure it exists. |
| 1096 | ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); |
| 1097 | |
| 1098 | SmallVector<char, 8> ResultPath; |
| 1099 | std::error_code ErrorCode = |
| 1100 | fs::getPathFromOpenFD(FileDescriptor, ResultPath); |
| 1101 | |
| 1102 | if (!ErrorCode) { |
| 1103 | fs::UniqueID D1, D2; |
| 1104 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); |
| 1105 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); |
| 1106 | ASSERT_EQ(D1, D2); |
| 1107 | } |
| 1108 | ::close(FileDescriptor); |
| 1109 | } |
| 1110 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1111 | TEST_F(FileSystemTest, OpenFileForRead) { |
| 1112 | // Create a temp file. |
| 1113 | int FileDescriptor; |
| 1114 | SmallString<64> TempPath; |
| 1115 | ASSERT_NO_ERROR( |
| 1116 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1117 | FileRemover Cleanup(TempPath); |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1118 | |
| 1119 | // Make sure it exists. |
| 1120 | ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); |
| 1121 | |
| 1122 | // Open the file for read |
| 1123 | int FileDescriptor2; |
| 1124 | SmallString<64> ResultPath; |
| 1125 | ASSERT_NO_ERROR( |
| 1126 | fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath)) |
| 1127 | |
| 1128 | // If we succeeded, check that the paths are the same (modulo case): |
| 1129 | if (!ResultPath.empty()) { |
| 1130 | // The paths returned by createTemporaryFile and getPathFromOpenFD |
| 1131 | // should reference the same file on disk. |
| 1132 | fs::UniqueID D1, D2; |
| 1133 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); |
| 1134 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); |
| 1135 | ASSERT_EQ(D1, D2); |
| 1136 | } |
| 1137 | |
| 1138 | ::close(FileDescriptor); |
| 1139 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 1140 | } // anonymous namespace |