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" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
Simon Dardis | e8bb392 | 2017-02-22 14:34:45 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 14 | #include "llvm/BinaryFormat/Magic.h" |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ConvertUTF.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errc.h" |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileUtilities.h" |
Simon Dardis | e8bb392 | 2017-02-22 14:34:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Host.h" |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 23 | #include "gtest/gtest.h" |
| 24 | |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 25 | #ifdef LLVM_ON_WIN32 |
Nico Weber | dd2ca83 | 2016-04-18 13:54:50 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ArrayRef.h" |
NAKAMURA Takumi | b94f052 | 2015-06-16 06:46:16 +0000 | [diff] [blame] | 27 | #include <windows.h> |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 28 | #include <winerror.h> |
| 29 | #endif |
| 30 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 31 | #ifdef LLVM_ON_UNIX |
Zachary Turner | d8350cf | 2017-03-22 16:30:06 +0000 | [diff] [blame] | 32 | #include <pwd.h> |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #endif |
| 35 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 37 | using namespace llvm::sys; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 38 | |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 39 | #define ASSERT_NO_ERROR(x) \ |
| 40 | if (std::error_code ASSERT_NO_ERROR_ec = x) { \ |
| 41 | SmallString<128> MessageStorage; \ |
| 42 | raw_svector_ostream Message(MessageStorage); \ |
| 43 | Message << #x ": did not return errc::success.\n" \ |
| 44 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 45 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 46 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
| 47 | } else { \ |
| 48 | } |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 49 | |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 52 | TEST(is_separator, Works) { |
| 53 | EXPECT_TRUE(path::is_separator('/')); |
| 54 | EXPECT_FALSE(path::is_separator('\0')); |
| 55 | EXPECT_FALSE(path::is_separator('-')); |
| 56 | EXPECT_FALSE(path::is_separator(' ')); |
| 57 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 58 | EXPECT_TRUE(path::is_separator('\\', path::Style::windows)); |
| 59 | EXPECT_FALSE(path::is_separator('\\', path::Style::posix)); |
| 60 | |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 61 | #ifdef LLVM_ON_WIN32 |
| 62 | EXPECT_TRUE(path::is_separator('\\')); |
| 63 | #else |
Zachary Turner | 78d15af | 2017-03-16 23:19:40 +0000 | [diff] [blame] | 64 | EXPECT_FALSE(path::is_separator('\\')); |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 65 | #endif |
| 66 | } |
| 67 | |
Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 68 | TEST(Support, Path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 69 | SmallVector<StringRef, 40> paths; |
| 70 | paths.push_back(""); |
| 71 | paths.push_back("."); |
| 72 | paths.push_back(".."); |
| 73 | paths.push_back("foo"); |
| 74 | paths.push_back("/"); |
| 75 | paths.push_back("/foo"); |
| 76 | paths.push_back("foo/"); |
| 77 | paths.push_back("/foo/"); |
| 78 | paths.push_back("foo/bar"); |
| 79 | paths.push_back("/foo/bar"); |
| 80 | paths.push_back("//net"); |
| 81 | paths.push_back("//net/foo"); |
| 82 | paths.push_back("///foo///"); |
| 83 | paths.push_back("///foo///bar"); |
| 84 | paths.push_back("/."); |
| 85 | paths.push_back("./"); |
| 86 | paths.push_back("/.."); |
| 87 | paths.push_back("../"); |
| 88 | paths.push_back("foo/."); |
| 89 | paths.push_back("foo/.."); |
| 90 | paths.push_back("foo/./"); |
| 91 | paths.push_back("foo/./bar"); |
| 92 | paths.push_back("foo/.."); |
| 93 | paths.push_back("foo/../"); |
| 94 | paths.push_back("foo/../bar"); |
| 95 | paths.push_back("c:"); |
| 96 | paths.push_back("c:/"); |
| 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 | paths.push_back("prn:"); |
| 103 | paths.push_back("c:\\"); |
| 104 | paths.push_back("c:foo"); |
| 105 | paths.push_back("c:\\foo"); |
| 106 | paths.push_back("c:foo\\"); |
| 107 | paths.push_back("c:\\foo\\"); |
| 108 | paths.push_back("c:\\foo/"); |
| 109 | paths.push_back("c:/foo\\bar"); |
| 110 | |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 111 | SmallVector<StringRef, 5> ComponentStack; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 112 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 113 | e = paths.end(); |
| 114 | i != e; |
| 115 | ++i) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 116 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 117 | ce = sys::path::end(*i); |
| 118 | ci != ce; |
| 119 | ++ci) { |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 120 | ASSERT_FALSE(ci->empty()); |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 121 | ComponentStack.push_back(*ci); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 122 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 123 | |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 124 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 125 | ce = sys::path::rend(*i); |
| 126 | ci != ce; |
| 127 | ++ci) { |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 128 | ASSERT_TRUE(*ci == ComponentStack.back()); |
| 129 | ComponentStack.pop_back(); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 130 | } |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 131 | ASSERT_TRUE(ComponentStack.empty()); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 132 | |
Justin Bogner | 0db71d9 | 2016-10-16 22:09:24 +0000 | [diff] [blame] | 133 | // Crash test most of the API - since we're iterating over all of our paths |
| 134 | // here there isn't really anything reasonable to assert on in the results. |
| 135 | (void)path::has_root_path(*i); |
| 136 | (void)path::root_path(*i); |
| 137 | (void)path::has_root_name(*i); |
| 138 | (void)path::root_name(*i); |
| 139 | (void)path::has_root_directory(*i); |
| 140 | (void)path::root_directory(*i); |
| 141 | (void)path::has_parent_path(*i); |
| 142 | (void)path::parent_path(*i); |
| 143 | (void)path::has_filename(*i); |
| 144 | (void)path::filename(*i); |
| 145 | (void)path::has_stem(*i); |
| 146 | (void)path::stem(*i); |
| 147 | (void)path::has_extension(*i); |
| 148 | (void)path::extension(*i); |
| 149 | (void)path::is_absolute(*i); |
| 150 | (void)path::is_relative(*i); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 151 | |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 152 | SmallString<128> temp_store; |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 153 | temp_store = *i; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 154 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 155 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 156 | path::remove_filename(temp_store); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 157 | |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 158 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 159 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 160 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 161 | stem = path::stem(filename); |
| 162 | ext = path::extension(filename); |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame] | 163 | EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str()); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 164 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 165 | path::native(*i, temp_store); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 166 | } |
Benjamin Kramer | ae1d599 | 2015-10-05 13:02:43 +0000 | [diff] [blame] | 167 | |
| 168 | SmallString<32> Relative("foo.cpp"); |
| 169 | ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative)); |
Benjamin Kramer | 2b4e14e | 2015-10-05 14:15:13 +0000 | [diff] [blame] | 170 | Relative[5] = '/'; // Fix up windows paths. |
Benjamin Kramer | ae1d599 | 2015-10-05 13:02:43 +0000 | [diff] [blame] | 171 | ASSERT_EQ("/root/foo.cpp", Relative); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 172 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 173 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 174 | TEST(Support, RelativePathIterator) { |
| 175 | SmallString<64> Path(StringRef("c/d/e/foo.txt")); |
| 176 | typedef SmallVector<StringRef, 4> PathComponents; |
| 177 | PathComponents ExpectedPathComponents; |
| 178 | PathComponents ActualPathComponents; |
| 179 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 180 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 181 | |
| 182 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 183 | ++I) { |
| 184 | ActualPathComponents.push_back(*I); |
| 185 | } |
| 186 | |
| 187 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 188 | |
| 189 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 190 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 191 | } |
| 192 | } |
| 193 | |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 194 | TEST(Support, RelativePathDotIterator) { |
| 195 | SmallString<64> Path(StringRef(".c/.d/../.")); |
| 196 | typedef SmallVector<StringRef, 4> PathComponents; |
| 197 | PathComponents ExpectedPathComponents; |
| 198 | PathComponents ActualPathComponents; |
| 199 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 200 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 201 | |
| 202 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 203 | ++I) { |
| 204 | ActualPathComponents.push_back(*I); |
| 205 | } |
| 206 | |
| 207 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 208 | |
| 209 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 210 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 211 | } |
| 212 | } |
| 213 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 214 | TEST(Support, AbsolutePathIterator) { |
| 215 | SmallString<64> Path(StringRef("/c/d/e/foo.txt")); |
| 216 | typedef SmallVector<StringRef, 4> PathComponents; |
| 217 | PathComponents ExpectedPathComponents; |
| 218 | PathComponents ActualPathComponents; |
| 219 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 220 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 221 | |
| 222 | // The root path will also be a component when iterating |
| 223 | ExpectedPathComponents[0] = "/"; |
| 224 | |
| 225 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 226 | ++I) { |
| 227 | ActualPathComponents.push_back(*I); |
| 228 | } |
| 229 | |
| 230 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 231 | |
| 232 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 233 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 234 | } |
| 235 | } |
| 236 | |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 237 | TEST(Support, AbsolutePathDotIterator) { |
| 238 | SmallString<64> Path(StringRef("/.c/.d/../.")); |
| 239 | typedef SmallVector<StringRef, 4> PathComponents; |
| 240 | PathComponents ExpectedPathComponents; |
| 241 | PathComponents ActualPathComponents; |
| 242 | |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 243 | StringRef(Path).split(ExpectedPathComponents, '/'); |
Ben Langmuir | 2547f93 | 2015-03-10 00:04:29 +0000 | [diff] [blame] | 244 | |
| 245 | // The root path will also be a component when iterating |
| 246 | ExpectedPathComponents[0] = "/"; |
| 247 | |
| 248 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 249 | ++I) { |
| 250 | ActualPathComponents.push_back(*I); |
| 251 | } |
| 252 | |
| 253 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 254 | |
| 255 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 256 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 257 | } |
| 258 | } |
| 259 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 260 | TEST(Support, AbsolutePathIteratorWin32) { |
| 261 | SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); |
| 262 | typedef SmallVector<StringRef, 4> PathComponents; |
| 263 | PathComponents ExpectedPathComponents; |
| 264 | PathComponents ActualPathComponents; |
| 265 | |
| 266 | StringRef(Path).split(ExpectedPathComponents, "\\"); |
| 267 | |
| 268 | // The root path (which comes after the drive name) will also be a component |
| 269 | // when iterating. |
| 270 | ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); |
| 271 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 272 | for (path::const_iterator I = path::begin(Path, path::Style::windows), |
| 273 | E = path::end(Path); |
| 274 | I != E; ++I) { |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 275 | ActualPathComponents.push_back(*I); |
| 276 | } |
| 277 | |
| 278 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 279 | |
| 280 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 281 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 282 | } |
| 283 | } |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 284 | |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 285 | TEST(Support, AbsolutePathIteratorEnd) { |
| 286 | // Trailing slashes are converted to '.' unless they are part of the root path. |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 287 | SmallVector<std::pair<StringRef, path::Style>, 4> Paths; |
| 288 | Paths.emplace_back("/foo/", path::Style::native); |
| 289 | Paths.emplace_back("/foo//", path::Style::native); |
| 290 | Paths.emplace_back("//net//", path::Style::native); |
| 291 | Paths.emplace_back("c:\\\\", path::Style::windows); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 292 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 293 | for (auto &Path : Paths) { |
| 294 | StringRef LastComponent = *path::rbegin(Path.first, Path.second); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 295 | EXPECT_EQ(".", LastComponent); |
| 296 | } |
| 297 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 298 | SmallVector<std::pair<StringRef, path::Style>, 3> RootPaths; |
| 299 | RootPaths.emplace_back("/", path::Style::native); |
| 300 | RootPaths.emplace_back("//net/", path::Style::native); |
| 301 | RootPaths.emplace_back("c:\\", path::Style::windows); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 302 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 303 | for (auto &Path : RootPaths) { |
| 304 | StringRef LastComponent = *path::rbegin(Path.first, Path.second); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 305 | EXPECT_EQ(1u, LastComponent.size()); |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 306 | EXPECT_TRUE(path::is_separator(LastComponent[0], Path.second)); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 310 | TEST(Support, HomeDirectory) { |
NAKAMURA Takumi | cc275e4 | 2015-10-16 09:40:01 +0000 | [diff] [blame] | 311 | std::string expected; |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 312 | #ifdef LLVM_ON_WIN32 |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 313 | if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) { |
| 314 | auto pathLen = ::wcslen(path); |
| 315 | ArrayRef<char> ref{reinterpret_cast<char const *>(path), |
| 316 | pathLen * sizeof(wchar_t)}; |
| 317 | convertUTF16ToUTF8String(ref, expected); |
| 318 | } |
Pawel Bylica | 7187e4b | 2015-10-16 09:08:59 +0000 | [diff] [blame] | 319 | #else |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 320 | if (char const *path = ::getenv("HOME")) |
| 321 | expected = path; |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 322 | #endif |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 323 | // Do not try to test it if we don't know what to expect. |
| 324 | // On Windows we use something better than env vars. |
| 325 | if (!expected.empty()) { |
NAKAMURA Takumi | cc275e4 | 2015-10-16 09:40:01 +0000 | [diff] [blame] | 326 | SmallString<128> HomeDir; |
| 327 | auto status = path::home_directory(HomeDir); |
Pawel Bylica | 2fa025c | 2015-10-16 10:11:07 +0000 | [diff] [blame] | 328 | EXPECT_TRUE(status); |
NAKAMURA Takumi | cc275e4 | 2015-10-16 09:40:01 +0000 | [diff] [blame] | 329 | EXPECT_EQ(expected, HomeDir); |
| 330 | } |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Zachary Turner | d8350cf | 2017-03-22 16:30:06 +0000 | [diff] [blame] | 333 | #ifdef LLVM_ON_UNIX |
Zachary Turner | a3cf70b | 2017-03-22 15:24:59 +0000 | [diff] [blame] | 334 | TEST(Support, HomeDirectoryWithNoEnv) { |
Zachary Turner | d8350cf | 2017-03-22 16:30:06 +0000 | [diff] [blame] | 335 | std::string OriginalStorage; |
| 336 | char const *OriginalEnv = ::getenv("HOME"); |
| 337 | if (OriginalEnv) { |
| 338 | // We're going to unset it, so make a copy and save a pointer to the copy |
| 339 | // so that we can reset it at the end of the test. |
| 340 | OriginalStorage = OriginalEnv; |
| 341 | OriginalEnv = OriginalStorage.c_str(); |
| 342 | } |
| 343 | |
| 344 | // Don't run the test if we have nothing to compare against. |
| 345 | struct passwd *pw = getpwuid(getuid()); |
| 346 | if (!pw || !pw->pw_dir) return; |
| 347 | |
Zachary Turner | a3cf70b | 2017-03-22 15:24:59 +0000 | [diff] [blame] | 348 | ::unsetenv("HOME"); |
Zachary Turner | d8350cf | 2017-03-22 16:30:06 +0000 | [diff] [blame] | 349 | EXPECT_EQ(nullptr, ::getenv("HOME")); |
| 350 | std::string PwDir = pw->pw_dir; |
Zachary Turner | a3cf70b | 2017-03-22 15:24:59 +0000 | [diff] [blame] | 351 | |
| 352 | SmallString<128> HomeDir; |
| 353 | auto status = path::home_directory(HomeDir); |
| 354 | EXPECT_TRUE(status); |
Zachary Turner | d8350cf | 2017-03-22 16:30:06 +0000 | [diff] [blame] | 355 | EXPECT_EQ(PwDir, HomeDir); |
Zachary Turner | a3cf70b | 2017-03-22 15:24:59 +0000 | [diff] [blame] | 356 | |
Zachary Turner | d8350cf | 2017-03-22 16:30:06 +0000 | [diff] [blame] | 357 | // Now put the environment back to its original state (meaning that if it was |
| 358 | // unset before, we don't reset it). |
| 359 | if (OriginalEnv) ::setenv("HOME", OriginalEnv, 1); |
Zachary Turner | a3cf70b | 2017-03-22 15:24:59 +0000 | [diff] [blame] | 360 | } |
| 361 | #endif |
| 362 | |
Pawel Bylica | 0e97e5c | 2015-11-02 09:49:17 +0000 | [diff] [blame] | 363 | TEST(Support, UserCacheDirectory) { |
| 364 | SmallString<13> CacheDir; |
| 365 | SmallString<20> CacheDir2; |
| 366 | auto Status = path::user_cache_directory(CacheDir, ""); |
| 367 | EXPECT_TRUE(Status ^ CacheDir.empty()); |
| 368 | |
| 369 | if (Status) { |
| 370 | EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed |
| 371 | EXPECT_EQ(CacheDir, CacheDir2); // and return same paths |
| 372 | |
| 373 | EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c")); |
| 374 | auto It = path::rbegin(CacheDir); |
| 375 | EXPECT_EQ("file.c", *It); |
| 376 | EXPECT_EQ("B", *++It); |
| 377 | EXPECT_EQ("A", *++It); |
| 378 | auto ParentDir = *++It; |
| 379 | |
| 380 | // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0" |
| 381 | EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2", |
| 382 | "\xE2\x84\xB5.0")); |
| 383 | auto It2 = path::rbegin(CacheDir2); |
| 384 | EXPECT_EQ("\xE2\x84\xB5.0", *It2); |
| 385 | EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2); |
| 386 | auto ParentDir2 = *++It2; |
| 387 | |
| 388 | EXPECT_EQ(ParentDir, ParentDir2); |
| 389 | } |
| 390 | } |
| 391 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 392 | TEST(Support, TempDirectory) { |
| 393 | SmallString<32> TempDir; |
| 394 | path::system_temp_directory(false, TempDir); |
| 395 | EXPECT_TRUE(!TempDir.empty()); |
| 396 | TempDir.clear(); |
| 397 | path::system_temp_directory(true, TempDir); |
| 398 | EXPECT_TRUE(!TempDir.empty()); |
| 399 | } |
| 400 | |
David Blaikie | 06d5618 | 2015-11-17 20:38:54 +0000 | [diff] [blame] | 401 | #ifdef LLVM_ON_WIN32 |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 402 | static std::string path2regex(std::string Path) { |
| 403 | size_t Pos = 0; |
| 404 | while ((Pos = Path.find('\\', Pos)) != std::string::npos) { |
| 405 | Path.replace(Pos, 1, "\\\\"); |
| 406 | Pos += 2; |
| 407 | } |
| 408 | return Path; |
| 409 | } |
| 410 | |
| 411 | /// Helper for running temp dir test in separated process. See below. |
| 412 | #define EXPECT_TEMP_DIR(prepare, expected) \ |
| 413 | EXPECT_EXIT( \ |
| 414 | { \ |
| 415 | prepare; \ |
| 416 | SmallString<300> TempDir; \ |
| 417 | path::system_temp_directory(true, TempDir); \ |
| 418 | raw_os_ostream(std::cerr) << TempDir; \ |
| 419 | std::exit(0); \ |
| 420 | }, \ |
| 421 | ::testing::ExitedWithCode(0), path2regex(expected)) |
| 422 | |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 423 | TEST(SupportDeathTest, TempDirectoryOnWindows) { |
| 424 | // In this test we want to check how system_temp_directory responds to |
| 425 | // different values of specific env vars. To prevent corrupting env vars of |
| 426 | // the current process all checks are done in separated processes. |
| 427 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder"); |
| 428 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"), |
| 429 | "C:\\Unix\\Path\\Seperators"); |
| 430 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$"); |
| 431 | EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep"); |
| 432 | EXPECT_TEMP_DIR( |
| 433 | _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"), |
| 434 | "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80"); |
| 435 | |
| 436 | // Test $TMP empty, $TEMP set. |
| 437 | EXPECT_TEMP_DIR( |
| 438 | { |
| 439 | _wputenv_s(L"TMP", L""); |
| 440 | _wputenv_s(L"TEMP", L"C:\\Valid\\Path"); |
| 441 | }, |
| 442 | "C:\\Valid\\Path"); |
| 443 | |
| 444 | // All related env vars empty |
| 445 | EXPECT_TEMP_DIR( |
| 446 | { |
| 447 | _wputenv_s(L"TMP", L""); |
| 448 | _wputenv_s(L"TEMP", L""); |
| 449 | _wputenv_s(L"USERPROFILE", L""); |
| 450 | }, |
| 451 | "C:\\Temp"); |
| 452 | |
| 453 | // Test evn var / path with 260 chars. |
| 454 | SmallString<270> Expected{"C:\\Temp\\AB\\123456789"}; |
| 455 | while (Expected.size() < 260) |
| 456 | Expected.append("\\DirNameWith19Charss"); |
Reid Kleckner | 88e9ff6 | 2016-02-10 19:29:01 +0000 | [diff] [blame] | 457 | ASSERT_EQ(260U, Expected.size()); |
Pawel Bylica | a90e745 | 2015-11-17 16:54:32 +0000 | [diff] [blame] | 458 | EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str()); |
| 459 | } |
| 460 | #endif |
| 461 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 462 | class FileSystemTest : public testing::Test { |
| 463 | protected: |
| 464 | /// Unique temporary directory in which all created filesystem entities must |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 465 | /// 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] | 466 | SmallString<128> TestDirectory; |
| 467 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 468 | void SetUp() override { |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 469 | ASSERT_NO_ERROR( |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 470 | fs::createUniqueDirectory("file-system-test", TestDirectory)); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 471 | // We don't care about this specific file. |
Michael J. Spencer | 42368cc | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 472 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 473 | errs().flush(); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 476 | void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); } |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 477 | }; |
| 478 | |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 479 | TEST_F(FileSystemTest, Unique) { |
| 480 | // Create a temp file. |
| 481 | int FileDescriptor; |
| 482 | SmallString<64> TempPath; |
| 483 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 484 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 485 | |
| 486 | // The same file should return an identical unique id. |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 487 | fs::UniqueID F1, F2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 488 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); |
| 489 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 490 | ASSERT_EQ(F1, F2); |
| 491 | |
| 492 | // Different files should return different unique ids. |
| 493 | int FileDescriptor2; |
| 494 | SmallString<64> TempPath2; |
| 495 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 496 | fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); |
| 497 | |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 498 | fs::UniqueID D; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 499 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 500 | ASSERT_NE(D, F1); |
| 501 | ::close(FileDescriptor2); |
| 502 | |
| 503 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 504 | |
| 505 | // Two paths representing the same file on disk should still provide the |
| 506 | // same unique id. We can test this by making a hard link. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 507 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 508 | fs::UniqueID D2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 509 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 510 | ASSERT_EQ(D2, F1); |
| 511 | |
| 512 | ::close(FileDescriptor); |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 513 | |
| 514 | SmallString<128> Dir1; |
| 515 | ASSERT_NO_ERROR( |
| 516 | fs::createUniqueDirectory("dir1", Dir1)); |
| 517 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); |
| 518 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); |
| 519 | ASSERT_EQ(F1, F2); |
| 520 | |
| 521 | SmallString<128> Dir2; |
| 522 | ASSERT_NO_ERROR( |
| 523 | fs::createUniqueDirectory("dir2", Dir2)); |
| 524 | ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); |
| 525 | ASSERT_NE(F1, F2); |
Reid Kleckner | 1a4398a | 2016-09-02 01:10:53 +0000 | [diff] [blame] | 526 | ASSERT_NO_ERROR(fs::remove(Dir1)); |
| 527 | ASSERT_NO_ERROR(fs::remove(Dir2)); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 528 | ASSERT_NO_ERROR(fs::remove(TempPath2)); |
| 529 | ASSERT_NO_ERROR(fs::remove(TempPath)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 532 | TEST_F(FileSystemTest, RealPath) { |
| 533 | ASSERT_NO_ERROR( |
| 534 | fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3")); |
| 535 | ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3")); |
| 536 | |
| 537 | SmallString<64> RealBase; |
| 538 | SmallString<64> Expected; |
| 539 | SmallString<64> Actual; |
| 540 | |
| 541 | // TestDirectory itself might be under a symlink or have been specified with |
| 542 | // a different case than the existing temp directory. In such cases real_path |
| 543 | // on the concatenated path will differ in the TestDirectory portion from |
| 544 | // how we specified it. Make sure to compare against the real_path of the |
| 545 | // TestDirectory, and not just the value of TestDirectory. |
| 546 | ASSERT_NO_ERROR(fs::real_path(TestDirectory, RealBase)); |
| 547 | path::native(Twine(RealBase) + "/test1/test2", Expected); |
| 548 | |
| 549 | ASSERT_NO_ERROR(fs::real_path( |
| 550 | Twine(TestDirectory) + "/././test1/../test1/test2/./test3/..", Actual)); |
| 551 | |
| 552 | EXPECT_EQ(Expected, Actual); |
| 553 | |
| 554 | SmallString<64> HomeDir; |
Zachary Turner | aeab48a | 2017-03-10 17:47:13 +0000 | [diff] [blame] | 555 | bool Result = llvm::sys::path::home_directory(HomeDir); |
| 556 | if (Result) { |
| 557 | ASSERT_NO_ERROR(fs::real_path(HomeDir, Expected)); |
| 558 | ASSERT_NO_ERROR(fs::real_path("~", Actual, true)); |
| 559 | EXPECT_EQ(Expected, Actual); |
| 560 | ASSERT_NO_ERROR(fs::real_path("~/", Actual, true)); |
| 561 | EXPECT_EQ(Expected, Actual); |
| 562 | } |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 563 | |
| 564 | ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1")); |
| 565 | } |
| 566 | |
Rafael Espindola | fe161b9 | 2017-11-22 19:59:05 +0000 | [diff] [blame^] | 567 | TEST_F(FileSystemTest, TempFileKeepDiscard) { |
| 568 | // We can keep then discard. |
| 569 | auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%"); |
| 570 | ASSERT_TRUE((bool)TempFileOrError); |
| 571 | fs::TempFile File = std::move(*TempFileOrError); |
| 572 | ASSERT_FALSE((bool)File.keep(TestDirectory + "/keep")); |
| 573 | ASSERT_FALSE((bool)File.discard()); |
| 574 | ASSERT_TRUE(fs::exists(TestDirectory + "/keep")); |
| 575 | ASSERT_NO_ERROR(fs::remove(TestDirectory + "/keep")); |
| 576 | } |
| 577 | |
| 578 | TEST_F(FileSystemTest, TempFileDiscardDiscard) { |
| 579 | // We can discard twice. |
| 580 | auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%"); |
| 581 | ASSERT_TRUE((bool)TempFileOrError); |
| 582 | fs::TempFile File = std::move(*TempFileOrError); |
| 583 | ASSERT_FALSE((bool)File.discard()); |
| 584 | ASSERT_FALSE((bool)File.discard()); |
| 585 | ASSERT_FALSE(fs::exists(TestDirectory + "/keep")); |
| 586 | } |
| 587 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 588 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 589 | // Create a temp file. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 590 | int FileDescriptor; |
| 591 | SmallString<64> TempPath; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 592 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 593 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 594 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 595 | // Make sure it exists. |
Rafael Espindola | c159915 | 2014-09-11 19:11:02 +0000 | [diff] [blame] | 596 | ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 597 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 598 | // Create another temp tile. |
| 599 | int FD2; |
| 600 | SmallString<64> TempPath2; |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 601 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 602 | ASSERT_TRUE(TempPath2.endswith(".temp")); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 603 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 604 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 605 | fs::file_status A, B; |
| 606 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 607 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 608 | EXPECT_FALSE(fs::equivalent(A, B)); |
| 609 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 610 | ::close(FD2); |
Rafael Espindola | 213c4cb | 2013-07-18 03:29:51 +0000 | [diff] [blame] | 611 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 612 | // Remove Temp2. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 613 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 614 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 615 | ASSERT_EQ(fs::remove(Twine(TempPath2), false), |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 616 | errc::no_such_file_or_directory); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 617 | |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 618 | std::error_code EC = fs::status(TempPath2.c_str(), B); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 619 | EXPECT_EQ(EC, errc::no_such_file_or_directory); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 620 | EXPECT_EQ(B.type(), fs::file_type::file_not_found); |
| 621 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 622 | // Make sure Temp2 doesn't exist. |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 623 | ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist), |
| 624 | errc::no_such_file_or_directory); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 625 | |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 626 | SmallString<64> TempPath3; |
| 627 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); |
| 628 | ASSERT_FALSE(TempPath3.endswith(".")); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 629 | FileRemover Cleanup3(TempPath3); |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 630 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 631 | // Create a hard link to Temp1. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 632 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 633 | bool equal; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 634 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 635 | EXPECT_TRUE(equal); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 636 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 637 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 638 | EXPECT_TRUE(fs::equivalent(A, B)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 639 | |
| 640 | // Remove Temp1. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 641 | ::close(FileDescriptor); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 642 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 643 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 644 | // Remove the hard link. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 645 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 646 | |
| 647 | // Make sure Temp1 doesn't exist. |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 648 | ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist), |
| 649 | errc::no_such_file_or_directory); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 650 | |
| 651 | #ifdef LLVM_ON_WIN32 |
| 652 | // Path name > 260 chars should get an error. |
| 653 | const char *Path270 = |
| 654 | "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" |
| 655 | "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" |
| 656 | "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" |
| 657 | "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" |
| 658 | "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 659 | EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath), |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 660 | errc::invalid_argument); |
| 661 | // Relative path < 247 chars, no problem. |
| 662 | const char *Path216 = |
| 663 | "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" |
| 664 | "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" |
| 665 | "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" |
| 666 | "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 667 | ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath)); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 668 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 669 | #endif |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 670 | } |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 671 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 672 | TEST_F(FileSystemTest, CreateDir) { |
| 673 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 674 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 675 | ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 676 | errc::file_exists); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 677 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 678 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 679 | #ifdef LLVM_ON_UNIX |
| 680 | // Set a 0000 umask so that we can test our directory permissions. |
| 681 | mode_t OldUmask = ::umask(0000); |
| 682 | |
| 683 | fs::file_status Status; |
| 684 | ASSERT_NO_ERROR( |
| 685 | fs::create_directory(Twine(TestDirectory) + "baz500", false, |
| 686 | fs::perms::owner_read | fs::perms::owner_exe)); |
| 687 | ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status)); |
| 688 | ASSERT_EQ(Status.permissions() & fs::perms::all_all, |
| 689 | fs::perms::owner_read | fs::perms::owner_exe); |
| 690 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false, |
| 691 | fs::perms::all_all)); |
| 692 | ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status)); |
| 693 | ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all); |
| 694 | |
| 695 | // Restore umask to be safe. |
| 696 | ::umask(OldUmask); |
| 697 | #endif |
| 698 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 699 | #ifdef LLVM_ON_WIN32 |
| 700 | // Prove that create_directories() can handle a pathname > 248 characters, |
| 701 | // which is the documented limit for CreateDirectory(). |
| 702 | // (248 is MAX_PATH subtracting room for an 8.3 filename.) |
| 703 | // Generate a directory path guaranteed to fall into that range. |
| 704 | size_t TmpLen = TestDirectory.size(); |
| 705 | const char *OneDir = "\\123456789"; |
| 706 | size_t OneDirLen = strlen(OneDir); |
Aaron Ballman | 493456e | 2014-11-13 13:39:49 +0000 | [diff] [blame] | 707 | ASSERT_LT(OneDirLen, 12U); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 708 | size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1; |
| 709 | SmallString<260> LongDir(TestDirectory); |
| 710 | for (size_t I = 0; I < NLevels; ++I) |
| 711 | LongDir.append(OneDir); |
| 712 | ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); |
| 713 | ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); |
| 714 | ASSERT_EQ(fs::create_directories(Twine(LongDir), false), |
| 715 | errc::file_exists); |
| 716 | // Tidy up, "recursively" removing the directories. |
| 717 | StringRef ThisDir(LongDir); |
| 718 | for (size_t J = 0; J < NLevels; ++J) { |
| 719 | ASSERT_NO_ERROR(fs::remove(ThisDir)); |
| 720 | ThisDir = path::parent_path(ThisDir); |
| 721 | } |
| 722 | |
Pirama Arumuga Nainar | 3d48bb5 | 2017-08-21 20:49:44 +0000 | [diff] [blame] | 723 | // Also verify that paths with Unix separators are handled correctly. |
| 724 | std::string LongPathWithUnixSeparators(TestDirectory.str()); |
| 725 | // Add at least one subdirectory to TestDirectory, and replace slashes with |
| 726 | // backslashes |
| 727 | do { |
| 728 | LongPathWithUnixSeparators.append("/DirNameWith19Charss"); |
| 729 | } while (LongPathWithUnixSeparators.size() < 260); |
| 730 | std::replace(LongPathWithUnixSeparators.begin(), |
| 731 | LongPathWithUnixSeparators.end(), |
| 732 | '\\', '/'); |
| 733 | ASSERT_NO_ERROR(fs::create_directories(Twine(LongPathWithUnixSeparators))); |
| 734 | // cleanup |
| 735 | ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + |
| 736 | "/DirNameWith19Charss")); |
| 737 | |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 738 | // Similarly for a relative pathname. Need to set the current directory to |
| 739 | // TestDirectory so that the one we create ends up in the right place. |
| 740 | char PreviousDir[260]; |
| 741 | size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir); |
Aaron Ballman | 493456e | 2014-11-13 13:39:49 +0000 | [diff] [blame] | 742 | ASSERT_GT(PreviousDirLen, 0U); |
| 743 | ASSERT_LT(PreviousDirLen, 260U); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 744 | ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0); |
| 745 | LongDir.clear(); |
| 746 | // Generate a relative directory name with absolute length > 248. |
| 747 | size_t LongDirLen = 249 - TestDirectory.size(); |
| 748 | LongDir.assign(LongDirLen, 'a'); |
| 749 | ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir))); |
| 750 | // While we're here, prove that .. and . handling works in these long paths. |
| 751 | const char *DotDotDirs = "\\..\\.\\b"; |
| 752 | LongDir.append(DotDotDirs); |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 753 | ASSERT_NO_ERROR(fs::create_directory("b")); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 754 | ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists); |
| 755 | // And clean up. |
Paul Robinson | 1b6c734 | 2014-11-13 00:36:34 +0000 | [diff] [blame] | 756 | ASSERT_NO_ERROR(fs::remove("b")); |
Paul Robinson | d9c4a9a | 2014-11-13 00:12:14 +0000 | [diff] [blame] | 757 | ASSERT_NO_ERROR(fs::remove( |
| 758 | Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs))))); |
| 759 | ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0); |
| 760 | #endif |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 763 | TEST_F(FileSystemTest, DirectoryIteration) { |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 764 | std::error_code ec; |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 765 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 766 | ASSERT_NO_ERROR(ec); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 767 | |
| 768 | // Create a known hierarchy to recurse over. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 769 | ASSERT_NO_ERROR( |
| 770 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 771 | ASSERT_NO_ERROR( |
| 772 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 773 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + |
| 774 | "/recursive/dontlookhere/da1")); |
| 775 | ASSERT_NO_ERROR( |
| 776 | fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 777 | ASSERT_NO_ERROR( |
| 778 | fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 779 | typedef std::vector<std::string> v_t; |
| 780 | v_t visited; |
| 781 | for (fs::recursive_directory_iterator i(Twine(TestDirectory) |
| 782 | + "/recursive", ec), e; i != e; i.increment(ec)){ |
| 783 | ASSERT_NO_ERROR(ec); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 784 | if (path::filename(i->path()) == "p1") { |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 785 | i.pop(); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 786 | // FIXME: recursive_directory_iterator should be more robust. |
| 787 | if (i == e) break; |
| 788 | } |
Michael J. Spencer | 9b54a3e | 2011-12-09 01:14:41 +0000 | [diff] [blame] | 789 | if (path::filename(i->path()) == "dontlookhere") |
| 790 | i.no_push(); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 791 | visited.push_back(path::filename(i->path())); |
| 792 | } |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 793 | v_t::const_iterator a0 = find(visited, "a0"); |
| 794 | v_t::const_iterator aa1 = find(visited, "aa1"); |
| 795 | v_t::const_iterator ab1 = find(visited, "ab1"); |
| 796 | v_t::const_iterator dontlookhere = find(visited, "dontlookhere"); |
| 797 | v_t::const_iterator da1 = find(visited, "da1"); |
| 798 | v_t::const_iterator z0 = find(visited, "z0"); |
| 799 | v_t::const_iterator za1 = find(visited, "za1"); |
| 800 | v_t::const_iterator pop = find(visited, "pop"); |
| 801 | v_t::const_iterator p1 = find(visited, "p1"); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 802 | |
| 803 | // Make sure that each path was visited correctly. |
| 804 | ASSERT_NE(a0, visited.end()); |
| 805 | ASSERT_NE(aa1, visited.end()); |
| 806 | ASSERT_NE(ab1, visited.end()); |
| 807 | ASSERT_NE(dontlookhere, visited.end()); |
| 808 | ASSERT_EQ(da1, visited.end()); // Not visited. |
| 809 | ASSERT_NE(z0, visited.end()); |
| 810 | ASSERT_NE(za1, visited.end()); |
| 811 | ASSERT_NE(pop, visited.end()); |
| 812 | ASSERT_EQ(p1, visited.end()); // Not visited. |
| 813 | |
| 814 | // Make sure that parents were visited before children. No other ordering |
| 815 | // guarantees can be made across siblings. |
| 816 | ASSERT_LT(a0, aa1); |
| 817 | ASSERT_LT(a0, ab1); |
| 818 | ASSERT_LT(z0, za1); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 819 | |
| 820 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 821 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 822 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); |
| 823 | ASSERT_NO_ERROR( |
| 824 | fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); |
| 825 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); |
| 826 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); |
| 827 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); |
| 828 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 829 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); |
| 830 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); |
Bruno Cardoso Lopes | ead771c | 2016-05-13 21:31:32 +0000 | [diff] [blame] | 831 | |
| 832 | // Test recursive_directory_iterator level() |
| 833 | ASSERT_NO_ERROR( |
| 834 | fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c")); |
| 835 | fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E; |
| 836 | for (int l = 0; I != E; I.increment(ec), ++l) { |
| 837 | ASSERT_NO_ERROR(ec); |
| 838 | EXPECT_EQ(I.level(), l); |
| 839 | } |
| 840 | EXPECT_EQ(I, E); |
| 841 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c")); |
| 842 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b")); |
| 843 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a")); |
| 844 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel")); |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 845 | } |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 846 | |
Juergen Ributzka | e7d0fa68 | 2017-03-13 21:34:07 +0000 | [diff] [blame] | 847 | #ifdef LLVM_ON_UNIX |
| 848 | TEST_F(FileSystemTest, BrokenSymlinkDirectoryIteration) { |
| 849 | // Create a known hierarchy to recurse over. |
| 850 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + "/symlink")); |
| 851 | ASSERT_NO_ERROR( |
| 852 | fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/a")); |
| 853 | ASSERT_NO_ERROR( |
| 854 | fs::create_directories(Twine(TestDirectory) + "/symlink/b/bb")); |
| 855 | ASSERT_NO_ERROR( |
| 856 | fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/ba")); |
| 857 | ASSERT_NO_ERROR( |
| 858 | fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/bc")); |
| 859 | ASSERT_NO_ERROR( |
| 860 | fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/c")); |
| 861 | ASSERT_NO_ERROR( |
| 862 | fs::create_directories(Twine(TestDirectory) + "/symlink/d/dd/ddd")); |
| 863 | ASSERT_NO_ERROR(fs::create_link(Twine(TestDirectory) + "/symlink/d/dd", |
| 864 | Twine(TestDirectory) + "/symlink/d/da")); |
| 865 | ASSERT_NO_ERROR( |
| 866 | fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/e")); |
| 867 | |
| 868 | typedef std::vector<std::string> v_t; |
| 869 | v_t visited; |
| 870 | |
| 871 | // The directory iterator doesn't stat the file, so we should be able to |
| 872 | // iterate over the whole directory. |
| 873 | std::error_code ec; |
| 874 | for (fs::directory_iterator i(Twine(TestDirectory) + "/symlink", ec), e; |
| 875 | i != e; i.increment(ec)) { |
| 876 | ASSERT_NO_ERROR(ec); |
| 877 | visited.push_back(path::filename(i->path())); |
| 878 | } |
| 879 | std::sort(visited.begin(), visited.end()); |
| 880 | v_t expected = {"a", "b", "c", "d", "e"}; |
| 881 | ASSERT_TRUE(visited.size() == expected.size()); |
| 882 | ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); |
| 883 | visited.clear(); |
| 884 | |
| 885 | // The recursive directory iterator has to stat the file, so we need to skip |
| 886 | // the broken symlinks. |
| 887 | for (fs::recursive_directory_iterator |
| 888 | i(Twine(TestDirectory) + "/symlink", ec), |
| 889 | e; |
| 890 | i != e; i.increment(ec)) { |
| 891 | ASSERT_NO_ERROR(ec); |
| 892 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 893 | ErrorOr<fs::basic_file_status> status = i->status(); |
| 894 | if (status.getError() == |
Juergen Ributzka | 118bb45 | 2017-03-14 18:37:44 +0000 | [diff] [blame] | 895 | std::make_error_code(std::errc::no_such_file_or_directory)) { |
Juergen Ributzka | e7d0fa68 | 2017-03-13 21:34:07 +0000 | [diff] [blame] | 896 | i.no_push(); |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | visited.push_back(path::filename(i->path())); |
| 901 | } |
| 902 | std::sort(visited.begin(), visited.end()); |
| 903 | expected = {"b", "bb", "d", "da", "dd", "ddd", "ddd"}; |
| 904 | ASSERT_TRUE(visited.size() == expected.size()); |
| 905 | ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); |
| 906 | visited.clear(); |
| 907 | |
| 908 | // This recursive directory iterator doesn't follow symlinks, so we don't need |
| 909 | // to skip them. |
| 910 | for (fs::recursive_directory_iterator |
| 911 | i(Twine(TestDirectory) + "/symlink", ec, /*follow_symlinks=*/false), |
| 912 | e; |
| 913 | i != e; i.increment(ec)) { |
| 914 | ASSERT_NO_ERROR(ec); |
| 915 | visited.push_back(path::filename(i->path())); |
| 916 | } |
Juergen Ributzka | c0d6160 | 2017-03-13 21:40:20 +0000 | [diff] [blame] | 917 | std::sort(visited.begin(), visited.end()); |
Juergen Ributzka | e7d0fa68 | 2017-03-13 21:34:07 +0000 | [diff] [blame] | 918 | expected = {"a", "b", "ba", "bb", "bc", "c", "d", "da", "dd", "ddd", "e"}; |
| 919 | ASSERT_TRUE(visited.size() == expected.size()); |
| 920 | ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); |
| 921 | |
| 922 | ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/symlink")); |
| 923 | } |
| 924 | #endif |
| 925 | |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 926 | TEST_F(FileSystemTest, Remove) { |
| 927 | SmallString<64> BaseDir; |
| 928 | SmallString<64> Paths[4]; |
| 929 | int fds[4]; |
| 930 | ASSERT_NO_ERROR(fs::createUniqueDirectory("fs_remove", BaseDir)); |
| 931 | |
| 932 | ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/baz")); |
| 933 | ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/buzz")); |
| 934 | ASSERT_NO_ERROR(fs::createUniqueFile( |
| 935 | Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[0], Paths[0])); |
| 936 | ASSERT_NO_ERROR(fs::createUniqueFile( |
| 937 | Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[1], Paths[1])); |
| 938 | ASSERT_NO_ERROR(fs::createUniqueFile( |
| 939 | Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[2], Paths[2])); |
| 940 | ASSERT_NO_ERROR(fs::createUniqueFile( |
| 941 | Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[3], Paths[3])); |
| 942 | |
| 943 | for (int fd : fds) |
| 944 | ::close(fd); |
| 945 | |
| 946 | EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/baz")); |
| 947 | EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/buzz")); |
| 948 | EXPECT_TRUE(fs::exists(Paths[0])); |
| 949 | EXPECT_TRUE(fs::exists(Paths[1])); |
| 950 | EXPECT_TRUE(fs::exists(Paths[2])); |
| 951 | EXPECT_TRUE(fs::exists(Paths[3])); |
| 952 | |
| 953 | ASSERT_NO_ERROR(fs::remove_directories("D:/footest")); |
| 954 | |
| 955 | ASSERT_NO_ERROR(fs::remove_directories(BaseDir)); |
| 956 | ASSERT_FALSE(fs::exists(BaseDir)); |
| 957 | } |
| 958 | |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 959 | #ifdef LLVM_ON_WIN32 |
| 960 | TEST_F(FileSystemTest, CarriageReturn) { |
| 961 | SmallString<128> FilePathname(TestDirectory); |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 962 | std::error_code EC; |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 963 | path::append(FilePathname, "test"); |
| 964 | |
| 965 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 966 | raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text); |
| 967 | ASSERT_NO_ERROR(EC); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 968 | File << '\n'; |
| 969 | } |
| 970 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 971 | auto Buf = MemoryBuffer::getFile(FilePathname.str()); |
Aaron Ballman | 586ee60 | 2014-07-06 20:20:02 +0000 | [diff] [blame] | 972 | EXPECT_TRUE((bool)Buf); |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 973 | EXPECT_EQ(Buf.get()->getBuffer(), "\r\n"); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 977 | raw_fd_ostream File(FilePathname, EC, sys::fs::F_None); |
| 978 | ASSERT_NO_ERROR(EC); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 979 | File << '\n'; |
| 980 | } |
| 981 | { |
Reid Kleckner | d83c63b | 2014-08-26 00:24:23 +0000 | [diff] [blame] | 982 | auto Buf = MemoryBuffer::getFile(FilePathname.str()); |
Aaron Ballman | 586ee60 | 2014-07-06 20:20:02 +0000 | [diff] [blame] | 983 | EXPECT_TRUE((bool)Buf); |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 984 | EXPECT_EQ(Buf.get()->getBuffer(), "\n"); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 985 | } |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 986 | ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 987 | } |
| 988 | #endif |
| 989 | |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 990 | TEST_F(FileSystemTest, Resize) { |
| 991 | int FD; |
| 992 | SmallString<64> TempPath; |
| 993 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); |
| 994 | ASSERT_NO_ERROR(fs::resize_file(FD, 123)); |
| 995 | fs::file_status Status; |
| 996 | ASSERT_NO_ERROR(fs::status(FD, Status)); |
| 997 | ASSERT_EQ(Status.getSize(), 123U); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 998 | ::close(FD); |
| 999 | ASSERT_NO_ERROR(fs::remove(TempPath)); |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 1002 | TEST_F(FileSystemTest, MD5) { |
| 1003 | int FD; |
| 1004 | SmallString<64> TempPath; |
| 1005 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); |
| 1006 | StringRef Data("abcdefghijklmnopqrstuvwxyz"); |
Galina Kistanova | 4f5771d | 2017-05-12 17:00:13 +0000 | [diff] [blame] | 1007 | ASSERT_EQ(write(FD, Data.data(), Data.size()), static_cast<ssize_t>(Data.size())); |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 1008 | lseek(FD, 0, SEEK_SET); |
| 1009 | auto Hash = fs::md5_contents(FD); |
| 1010 | ::close(FD); |
| 1011 | ASSERT_NO_ERROR(Hash.getError()); |
| 1012 | |
| 1013 | EXPECT_STREQ("c3fcd3d76192e4007dfb496cca67e13b", Hash->digest().c_str()); |
| 1014 | } |
| 1015 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 1016 | TEST_F(FileSystemTest, FileMapping) { |
| 1017 | // Create a temp file. |
| 1018 | int FileDescriptor; |
| 1019 | SmallString<64> TempPath; |
| 1020 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 1021 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 1022 | unsigned Size = 4096; |
| 1023 | ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size)); |
| 1024 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 1025 | // Map in temp file and add some content |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 1026 | std::error_code EC; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 1027 | StringRef Val("hello there"); |
| 1028 | { |
| 1029 | fs::mapped_file_region mfr(FileDescriptor, |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 1030 | fs::mapped_file_region::readwrite, Size, 0, EC); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 1031 | ASSERT_NO_ERROR(EC); |
| 1032 | std::copy(Val.begin(), Val.end(), mfr.data()); |
| 1033 | // Explicitly add a 0. |
| 1034 | mfr.data()[Val.size()] = 0; |
| 1035 | // Unmap temp file |
| 1036 | } |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1037 | ASSERT_EQ(close(FileDescriptor), 0); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 1038 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 1039 | // Map it back in read-only |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1040 | { |
| 1041 | int FD; |
| 1042 | EC = fs::openFileForRead(Twine(TempPath), FD); |
| 1043 | ASSERT_NO_ERROR(EC); |
| 1044 | fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC); |
| 1045 | ASSERT_NO_ERROR(EC); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 1046 | |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1047 | // Verify content |
| 1048 | EXPECT_EQ(StringRef(mfr.const_data()), Val); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 1049 | |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1050 | // Unmap temp file |
| 1051 | fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC); |
| 1052 | ASSERT_NO_ERROR(EC); |
| 1053 | ASSERT_EQ(close(FD), 0); |
| 1054 | } |
| 1055 | ASSERT_NO_ERROR(fs::remove(TempPath)); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 1056 | } |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 1057 | |
| 1058 | TEST(Support, NormalizePath) { |
Zachary Turner | 8fb09e3 | 2017-03-17 00:16:21 +0000 | [diff] [blame] | 1059 | using TestTuple = std::tuple<const char *, const char *, const char *>; |
| 1060 | std::vector<TestTuple> Tests; |
| 1061 | Tests.emplace_back("a", "a", "a"); |
| 1062 | Tests.emplace_back("a/b", "a\\b", "a/b"); |
| 1063 | Tests.emplace_back("a\\b", "a\\b", "a/b"); |
| 1064 | Tests.emplace_back("a\\\\b", "a\\\\b", "a\\\\b"); |
| 1065 | Tests.emplace_back("\\a", "\\a", "/a"); |
| 1066 | Tests.emplace_back("a\\", "a\\", "a/"); |
| 1067 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1068 | for (auto &T : Tests) { |
Zachary Turner | 3735006 | 2017-03-17 00:28:23 +0000 | [diff] [blame] | 1069 | SmallString<64> Win(std::get<0>(T)); |
| 1070 | SmallString<64> Posix(Win); |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1071 | path::native(Win, path::Style::windows); |
| 1072 | path::native(Posix, path::Style::posix); |
| 1073 | EXPECT_EQ(std::get<1>(T), Win); |
| 1074 | EXPECT_EQ(std::get<2>(T), Posix); |
| 1075 | } |
Serge Pavlov | 9c761a3 | 2017-03-01 09:38:15 +0000 | [diff] [blame] | 1076 | |
| 1077 | #if defined(LLVM_ON_WIN32) |
| 1078 | SmallString<64> PathHome; |
| 1079 | path::home_directory(PathHome); |
| 1080 | |
| 1081 | const char *Path7a = "~/aaa"; |
| 1082 | SmallString<64> Path7(Path7a); |
| 1083 | path::native(Path7); |
| 1084 | EXPECT_TRUE(Path7.endswith("\\aaa")); |
| 1085 | EXPECT_TRUE(Path7.startswith(PathHome)); |
| 1086 | EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1)); |
| 1087 | |
| 1088 | const char *Path8a = "~"; |
| 1089 | SmallString<64> Path8(Path8a); |
| 1090 | path::native(Path8); |
| 1091 | EXPECT_EQ(Path8, PathHome); |
| 1092 | |
| 1093 | const char *Path9a = "~aaa"; |
| 1094 | SmallString<64> Path9(Path9a); |
| 1095 | path::native(Path9); |
| 1096 | EXPECT_EQ(Path9, "~aaa"); |
| 1097 | |
| 1098 | const char *Path10a = "aaa/~/b"; |
| 1099 | SmallString<64> Path10(Path10a); |
| 1100 | path::native(Path10); |
| 1101 | EXPECT_EQ(Path10, "aaa\\~\\b"); |
| 1102 | #endif |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 1103 | } |
Douglas Katzman | a26be4a | 2015-09-02 21:02:10 +0000 | [diff] [blame] | 1104 | |
| 1105 | TEST(Support, RemoveLeadingDotSlash) { |
| 1106 | StringRef Path1("././/foolz/wat"); |
| 1107 | StringRef Path2("./////"); |
| 1108 | |
| 1109 | Path1 = path::remove_leading_dotslash(Path1); |
| 1110 | EXPECT_EQ(Path1, "foolz/wat"); |
| 1111 | Path2 = path::remove_leading_dotslash(Path2); |
| 1112 | EXPECT_EQ(Path2, ""); |
| 1113 | } |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 1114 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1115 | static std::string remove_dots(StringRef path, bool remove_dot_dot, |
| 1116 | path::Style style) { |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 1117 | SmallString<256> buffer(path); |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1118 | path::remove_dots(buffer, remove_dot_dot, style); |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 1119 | return buffer.str(); |
| 1120 | } |
| 1121 | |
| 1122 | TEST(Support, RemoveDots) { |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1123 | EXPECT_EQ("foolz\\wat", |
| 1124 | remove_dots(".\\.\\\\foolz\\wat", false, path::Style::windows)); |
| 1125 | EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false, path::Style::windows)); |
Mike Aizatsky | f8cf713 | 2015-11-09 19:36:53 +0000 | [diff] [blame] | 1126 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1127 | EXPECT_EQ("a\\..\\b\\c", |
| 1128 | remove_dots(".\\a\\..\\b\\c", false, path::Style::windows)); |
| 1129 | EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true, path::Style::windows)); |
| 1130 | EXPECT_EQ("c", remove_dots(".\\.\\c", true, path::Style::windows)); |
| 1131 | EXPECT_EQ("..\\a\\c", |
| 1132 | remove_dots("..\\a\\b\\..\\c", true, path::Style::windows)); |
| 1133 | EXPECT_EQ("..\\..\\a\\c", |
| 1134 | remove_dots("..\\..\\a\\b\\..\\c", true, path::Style::windows)); |
Mike Aizatsky | f8cf713 | 2015-11-09 19:36:53 +0000 | [diff] [blame] | 1135 | |
| 1136 | SmallString<64> Path1(".\\.\\c"); |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1137 | EXPECT_TRUE(path::remove_dots(Path1, true, path::Style::windows)); |
Mike Aizatsky | f8cf713 | 2015-11-09 19:36:53 +0000 | [diff] [blame] | 1138 | EXPECT_EQ("c", Path1); |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 1139 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1140 | EXPECT_EQ("foolz/wat", |
| 1141 | remove_dots("././/foolz/wat", false, path::Style::posix)); |
| 1142 | EXPECT_EQ("", remove_dots("./////", false, path::Style::posix)); |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 1143 | |
Zachary Turner | 5c5091f | 2017-03-16 22:28:04 +0000 | [diff] [blame] | 1144 | EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false, path::Style::posix)); |
| 1145 | EXPECT_EQ("b/c", remove_dots("./a/../b/c", true, path::Style::posix)); |
| 1146 | EXPECT_EQ("c", remove_dots("././c", true, path::Style::posix)); |
| 1147 | EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true, path::Style::posix)); |
| 1148 | EXPECT_EQ("../../a/c", |
| 1149 | remove_dots("../../a/b/../c", true, path::Style::posix)); |
| 1150 | EXPECT_EQ("/a/c", remove_dots("/../../a/c", true, path::Style::posix)); |
| 1151 | EXPECT_EQ("/a/c", |
| 1152 | remove_dots("/../a/b//../././/c", true, path::Style::posix)); |
| 1153 | |
| 1154 | SmallString<64> Path2("././c"); |
| 1155 | EXPECT_TRUE(path::remove_dots(Path2, true, path::Style::posix)); |
| 1156 | EXPECT_EQ("c", Path2); |
Mike Aizatsky | 662b4fd | 2015-11-09 18:56:31 +0000 | [diff] [blame] | 1157 | } |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 1158 | |
| 1159 | TEST(Support, ReplacePathPrefix) { |
| 1160 | SmallString<64> Path1("/foo"); |
| 1161 | SmallString<64> Path2("/old/foo"); |
| 1162 | SmallString<64> OldPrefix("/old"); |
| 1163 | SmallString<64> NewPrefix("/new"); |
| 1164 | SmallString<64> NewPrefix2("/longernew"); |
| 1165 | SmallString<64> EmptyPrefix(""); |
| 1166 | |
| 1167 | SmallString<64> Path = Path1; |
| 1168 | path::replace_path_prefix(Path, OldPrefix, NewPrefix); |
| 1169 | EXPECT_EQ(Path, "/foo"); |
| 1170 | Path = Path2; |
| 1171 | path::replace_path_prefix(Path, OldPrefix, NewPrefix); |
| 1172 | EXPECT_EQ(Path, "/new/foo"); |
| 1173 | Path = Path2; |
| 1174 | path::replace_path_prefix(Path, OldPrefix, NewPrefix2); |
| 1175 | EXPECT_EQ(Path, "/longernew/foo"); |
| 1176 | Path = Path1; |
| 1177 | path::replace_path_prefix(Path, EmptyPrefix, NewPrefix); |
| 1178 | EXPECT_EQ(Path, "/new/foo"); |
| 1179 | Path = Path2; |
| 1180 | path::replace_path_prefix(Path, OldPrefix, EmptyPrefix); |
| 1181 | EXPECT_EQ(Path, "/foo"); |
| 1182 | } |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1183 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1184 | TEST_F(FileSystemTest, OpenFileForRead) { |
| 1185 | // Create a temp file. |
| 1186 | int FileDescriptor; |
| 1187 | SmallString<64> TempPath; |
| 1188 | ASSERT_NO_ERROR( |
| 1189 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 1190 | FileRemover Cleanup(TempPath); |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Make sure it exists. |
| 1193 | ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); |
| 1194 | |
| 1195 | // Open the file for read |
| 1196 | int FileDescriptor2; |
| 1197 | SmallString<64> ResultPath; |
| 1198 | ASSERT_NO_ERROR( |
| 1199 | fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath)) |
| 1200 | |
| 1201 | // If we succeeded, check that the paths are the same (modulo case): |
| 1202 | if (!ResultPath.empty()) { |
| 1203 | // The paths returned by createTemporaryFile and getPathFromOpenFD |
| 1204 | // should reference the same file on disk. |
| 1205 | fs::UniqueID D1, D2; |
| 1206 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); |
| 1207 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); |
| 1208 | ASSERT_EQ(D1, D2); |
| 1209 | } |
| 1210 | |
| 1211 | ::close(FileDescriptor); |
| 1212 | } |
Pavel Labath | 2f09609 | 2017-01-24 10:32:03 +0000 | [diff] [blame] | 1213 | |
| 1214 | TEST_F(FileSystemTest, set_current_path) { |
| 1215 | SmallString<128> path; |
| 1216 | |
| 1217 | ASSERT_NO_ERROR(fs::current_path(path)); |
| 1218 | ASSERT_NE(TestDirectory, path); |
| 1219 | |
| 1220 | struct RestorePath { |
| 1221 | SmallString<128> path; |
| 1222 | RestorePath(const SmallString<128> &path) : path(path) {} |
| 1223 | ~RestorePath() { fs::set_current_path(path); } |
| 1224 | } restore_path(path); |
| 1225 | |
| 1226 | ASSERT_NO_ERROR(fs::set_current_path(TestDirectory)); |
| 1227 | |
| 1228 | ASSERT_NO_ERROR(fs::current_path(path)); |
Pavel Labath | 5044009 | 2017-01-24 11:35:26 +0000 | [diff] [blame] | 1229 | |
| 1230 | fs::UniqueID D1, D2; |
| 1231 | ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1)); |
| 1232 | ASSERT_NO_ERROR(fs::getUniqueID(path, D2)); |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 1233 | ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path; |
| 1234 | } |
| 1235 | |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 1236 | TEST_F(FileSystemTest, permissions) { |
| 1237 | int FD; |
| 1238 | SmallString<64> TempPath; |
| 1239 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); |
| 1240 | FileRemover Cleanup(TempPath); |
| 1241 | |
| 1242 | // Make sure it exists. |
| 1243 | ASSERT_TRUE(fs::exists(Twine(TempPath))); |
| 1244 | |
| 1245 | auto CheckPermissions = [&](fs::perms Expected) { |
| 1246 | ErrorOr<fs::perms> Actual = fs::getPermissions(TempPath); |
| 1247 | return Actual && *Actual == Expected; |
| 1248 | }; |
| 1249 | |
| 1250 | std::error_code NoError; |
| 1251 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all), NoError); |
| 1252 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1253 | |
| 1254 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::all_exe), NoError); |
| 1255 | EXPECT_TRUE(CheckPermissions(fs::all_read | fs::all_exe)); |
| 1256 | |
| 1257 | #if defined(LLVM_ON_WIN32) |
| 1258 | fs::perms ReadOnly = fs::all_read | fs::all_exe; |
| 1259 | EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError); |
| 1260 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1261 | |
| 1262 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError); |
| 1263 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1264 | |
| 1265 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError); |
| 1266 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1267 | |
| 1268 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError); |
| 1269 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1270 | |
| 1271 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError); |
| 1272 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1273 | |
| 1274 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError); |
| 1275 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1276 | |
| 1277 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError); |
| 1278 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1279 | |
| 1280 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError); |
| 1281 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1282 | |
| 1283 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError); |
| 1284 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1285 | |
| 1286 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError); |
| 1287 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1288 | |
| 1289 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError); |
| 1290 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1291 | |
| 1292 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError); |
| 1293 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1294 | |
| 1295 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError); |
| 1296 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1297 | |
| 1298 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError); |
| 1299 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1300 | |
| 1301 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError); |
| 1302 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1303 | |
| 1304 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError); |
| 1305 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1306 | |
| 1307 | EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError); |
| 1308 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1309 | |
| 1310 | EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError); |
| 1311 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1312 | |
| 1313 | EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError); |
| 1314 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1315 | |
| 1316 | EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe | |
| 1317 | fs::set_gid_on_exe | |
| 1318 | fs::sticky_bit), |
| 1319 | NoError); |
| 1320 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1321 | |
| 1322 | EXPECT_EQ(fs::setPermissions(TempPath, ReadOnly | fs::set_uid_on_exe | |
| 1323 | fs::set_gid_on_exe | |
| 1324 | fs::sticky_bit), |
| 1325 | NoError); |
| 1326 | EXPECT_TRUE(CheckPermissions(ReadOnly)); |
| 1327 | |
| 1328 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError); |
| 1329 | EXPECT_TRUE(CheckPermissions(fs::all_all)); |
| 1330 | #else |
| 1331 | EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError); |
| 1332 | EXPECT_TRUE(CheckPermissions(fs::no_perms)); |
| 1333 | |
| 1334 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError); |
| 1335 | EXPECT_TRUE(CheckPermissions(fs::owner_read)); |
| 1336 | |
| 1337 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError); |
| 1338 | EXPECT_TRUE(CheckPermissions(fs::owner_write)); |
| 1339 | |
| 1340 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError); |
| 1341 | EXPECT_TRUE(CheckPermissions(fs::owner_exe)); |
| 1342 | |
| 1343 | EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError); |
| 1344 | EXPECT_TRUE(CheckPermissions(fs::owner_all)); |
| 1345 | |
| 1346 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError); |
| 1347 | EXPECT_TRUE(CheckPermissions(fs::group_read)); |
| 1348 | |
| 1349 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError); |
| 1350 | EXPECT_TRUE(CheckPermissions(fs::group_write)); |
| 1351 | |
| 1352 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError); |
| 1353 | EXPECT_TRUE(CheckPermissions(fs::group_exe)); |
| 1354 | |
| 1355 | EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError); |
| 1356 | EXPECT_TRUE(CheckPermissions(fs::group_all)); |
| 1357 | |
| 1358 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError); |
| 1359 | EXPECT_TRUE(CheckPermissions(fs::others_read)); |
| 1360 | |
| 1361 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError); |
| 1362 | EXPECT_TRUE(CheckPermissions(fs::others_write)); |
| 1363 | |
| 1364 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError); |
| 1365 | EXPECT_TRUE(CheckPermissions(fs::others_exe)); |
| 1366 | |
| 1367 | EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError); |
| 1368 | EXPECT_TRUE(CheckPermissions(fs::others_all)); |
| 1369 | |
| 1370 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError); |
| 1371 | EXPECT_TRUE(CheckPermissions(fs::all_read)); |
| 1372 | |
| 1373 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError); |
| 1374 | EXPECT_TRUE(CheckPermissions(fs::all_write)); |
| 1375 | |
| 1376 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError); |
| 1377 | EXPECT_TRUE(CheckPermissions(fs::all_exe)); |
| 1378 | |
| 1379 | EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError); |
| 1380 | EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe)); |
| 1381 | |
| 1382 | EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError); |
| 1383 | EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe)); |
| 1384 | |
Dimitry Andric | 49e033f | 2017-04-24 18:54:48 +0000 | [diff] [blame] | 1385 | // Modern BSDs require root to set the sticky bit on files. |
| 1386 | #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 1387 | EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError); |
| 1388 | EXPECT_TRUE(CheckPermissions(fs::sticky_bit)); |
| 1389 | |
| 1390 | EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe | |
| 1391 | fs::set_gid_on_exe | |
| 1392 | fs::sticky_bit), |
| 1393 | NoError); |
| 1394 | EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe | fs::set_gid_on_exe | |
| 1395 | fs::sticky_bit)); |
| 1396 | |
| 1397 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::set_uid_on_exe | |
| 1398 | fs::set_gid_on_exe | |
| 1399 | fs::sticky_bit), |
| 1400 | NoError); |
| 1401 | EXPECT_TRUE(CheckPermissions(fs::all_read | fs::set_uid_on_exe | |
| 1402 | fs::set_gid_on_exe | fs::sticky_bit)); |
| 1403 | |
| 1404 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError); |
| 1405 | EXPECT_TRUE(CheckPermissions(fs::all_perms)); |
Dimitry Andric | 49e033f | 2017-04-24 18:54:48 +0000 | [diff] [blame] | 1406 | #endif // !FreeBSD && !NetBSD && !OpenBSD |
| 1407 | |
| 1408 | EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms & ~fs::sticky_bit), |
| 1409 | NoError); |
| 1410 | EXPECT_TRUE(CheckPermissions(fs::all_perms & ~fs::sticky_bit)); |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 1411 | #endif |
| 1412 | } |
| 1413 | |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 1414 | } // anonymous namespace |