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