blob: cfd448b5fc902dd19e54ffeb451dc0b695cf5130 [file] [log] [blame]
Michael J. Spencer3ef91c52010-11-29 22:29:04 +00001//===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
Michael J. Spencer98847782010-11-24 19:20:05 +00002//
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 Espindola3bc8e712013-06-11 22:21:28 +000010#include "llvm/Support/Path.h"
Simon Dardise8bb3922017-02-22 14:34:45 +000011#include "llvm/ADT/SmallVector.h"
David Majnemer0d955d02016-08-11 22:21:41 +000012#include "llvm/ADT/STLExtras.h"
Simon Dardise8bb3922017-02-22 14:34:45 +000013#include "llvm/ADT/Triple.h"
Pawel Bylica7187e4b2015-10-16 09:08:59 +000014#include "llvm/Support/ConvertUTF.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000015#include "llvm/Support/Errc.h"
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000016#include "llvm/Support/ErrorHandling.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000017#include "llvm/Support/FileSystem.h"
Reid Kleckner75e557f2016-09-02 00:51:34 +000018#include "llvm/Support/FileUtilities.h"
Simon Dardise8bb3922017-02-22 14:34:45 +000019#include "llvm/Support/Host.h"
Rafael Espindola84ab9b32013-07-19 14:41:25 +000020#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f063602011-01-06 05:57:54 +000021#include "llvm/Support/raw_ostream.h"
Michael J. Spencer98847782010-11-24 19:20:05 +000022#include "gtest/gtest.h"
23
Rafael Espindolaa813d602014-06-11 03:58:34 +000024#ifdef LLVM_ON_WIN32
Nico Weberdd2ca832016-04-18 13:54:50 +000025#include "llvm/ADT/ArrayRef.h"
NAKAMURA Takumib94f0522015-06-16 06:46:16 +000026#include <windows.h>
Rafael Espindolaa813d602014-06-11 03:58:34 +000027#include <winerror.h>
28#endif
29
Frederic Riss6b9396c2015-08-06 21:04:55 +000030#ifdef LLVM_ON_UNIX
31#include <sys/stat.h>
32#endif
33
Michael J. Spencerebad2f92010-11-29 22:28:51 +000034using namespace llvm;
Michael J. Spencer45710402010-12-03 01:21:28 +000035using namespace llvm::sys;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000036
Rafael Espindolac049c652014-06-13 03:20:08 +000037#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. Spencer3b264ba2011-01-04 17:00:18 +000047
Michael J. Spencer98847782010-11-24 19:20:05 +000048namespace {
49
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000050TEST(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 Turner5c5091f2017-03-16 22:28:04 +000056 EXPECT_TRUE(path::is_separator('\\', path::Style::windows));
57 EXPECT_FALSE(path::is_separator('\\', path::Style::posix));
58
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000059#ifdef LLVM_ON_WIN32
60 EXPECT_TRUE(path::is_separator('\\'));
61#else
Zachary Turner78d15af2017-03-16 23:19:40 +000062 EXPECT_FALSE(path::is_separator('\\'));
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000063#endif
64}
65
Michael J. Spencer3ef91c52010-11-29 22:29:04 +000066TEST(Support, Path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000067 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 Bogner0c274ae2014-07-16 08:18:58 +0000109 SmallVector<StringRef, 5> ComponentStack;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000110 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
111 e = paths.end();
112 i != e;
113 ++i) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000114 for (sys::path::const_iterator ci = sys::path::begin(*i),
115 ce = sys::path::end(*i);
116 ci != ce;
117 ++ci) {
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000118 ASSERT_FALSE(ci->empty());
Justin Bogner0c274ae2014-07-16 08:18:58 +0000119 ComponentStack.push_back(*ci);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000120 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000121
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000122 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
123 ce = sys::path::rend(*i);
124 ci != ce;
125 ++ci) {
Justin Bogner0c274ae2014-07-16 08:18:58 +0000126 ASSERT_TRUE(*ci == ComponentStack.back());
127 ComponentStack.pop_back();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000128 }
Justin Bogner0c274ae2014-07-16 08:18:58 +0000129 ASSERT_TRUE(ComponentStack.empty());
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000130
Justin Bogner0db71d92016-10-16 22:09:24 +0000131 // 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. Spencerebad2f92010-11-29 22:28:51 +0000149
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000150 SmallString<128> temp_store;
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000151 temp_store = *i;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000152 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000153 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000154 path::remove_filename(temp_store);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000155
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000156 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000157 path::replace_extension(temp_store, "ext");
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000158 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000159 stem = path::stem(filename);
160 ext = path::extension(filename);
Justin Bogner487e7642014-08-04 17:36:41 +0000161 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000162
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000163 path::native(*i, temp_store);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000164 }
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000165
166 SmallString<32> Relative("foo.cpp");
167 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
Benjamin Kramer2b4e14e2015-10-05 14:15:13 +0000168 Relative[5] = '/'; // Fix up windows paths.
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000169 ASSERT_EQ("/root/foo.cpp", Relative);
Michael J. Spencer346a1332011-01-05 16:39:05 +0000170}
Michael J. Spencer45710402010-12-03 01:21:28 +0000171
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000172TEST(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 Carruthe4405e92015-09-10 06:12:31 +0000178 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000179
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 Langmuir2547f932015-03-10 00:04:29 +0000192TEST(Support, RelativePathDotIterator) {
193 SmallString<64> Path(StringRef(".c/.d/../."));
194 typedef SmallVector<StringRef, 4> PathComponents;
195 PathComponents ExpectedPathComponents;
196 PathComponents ActualPathComponents;
197
Chandler Carruthe4405e92015-09-10 06:12:31 +0000198 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000199
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. Siraj73537ea2013-08-12 17:10:49 +0000212TEST(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 Carruthe4405e92015-09-10 06:12:31 +0000218 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000219
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 Langmuir2547f932015-03-10 00:04:29 +0000235TEST(Support, AbsolutePathDotIterator) {
236 SmallString<64> Path(StringRef("/.c/.d/../."));
237 typedef SmallVector<StringRef, 4> PathComponents;
238 PathComponents ExpectedPathComponents;
239 PathComponents ActualPathComponents;
240
Chandler Carruthe4405e92015-09-10 06:12:31 +0000241 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000242
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. Siraj73537ea2013-08-12 17:10:49 +0000258TEST(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 Turner5c5091f2017-03-16 22:28:04 +0000270 for (path::const_iterator I = path::begin(Path, path::Style::windows),
271 E = path::end(Path);
272 I != E; ++I) {
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000273 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. Siraj73537ea2013-08-12 17:10:49 +0000282
Ben Langmuir8d116392014-03-05 19:56:30 +0000283TEST(Support, AbsolutePathIteratorEnd) {
284 // Trailing slashes are converted to '.' unless they are part of the root path.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000285 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 Langmuir8d116392014-03-05 19:56:30 +0000290
Zachary Turner5c5091f2017-03-16 22:28:04 +0000291 for (auto &Path : Paths) {
292 StringRef LastComponent = *path::rbegin(Path.first, Path.second);
Ben Langmuir8d116392014-03-05 19:56:30 +0000293 EXPECT_EQ(".", LastComponent);
294 }
295
Zachary Turner5c5091f2017-03-16 22:28:04 +0000296 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 Langmuir8d116392014-03-05 19:56:30 +0000300
Zachary Turner5c5091f2017-03-16 22:28:04 +0000301 for (auto &Path : RootPaths) {
302 StringRef LastComponent = *path::rbegin(Path.first, Path.second);
Ben Langmuir8d116392014-03-05 19:56:30 +0000303 EXPECT_EQ(1u, LastComponent.size());
Zachary Turner5c5091f2017-03-16 22:28:04 +0000304 EXPECT_TRUE(path::is_separator(LastComponent[0], Path.second));
Ben Langmuir8d116392014-03-05 19:56:30 +0000305 }
306}
307
Peter Collingbournef7d41012014-01-31 23:46:06 +0000308TEST(Support, HomeDirectory) {
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000309 std::string expected;
Pawel Bylica7187e4b2015-10-16 09:08:59 +0000310#ifdef LLVM_ON_WIN32
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000311 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 Bylica7187e4b2015-10-16 09:08:59 +0000317#else
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000318 if (char const *path = ::getenv("HOME"))
319 expected = path;
Peter Collingbournef7d41012014-01-31 23:46:06 +0000320#endif
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000321 // 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 Takumicc275e42015-10-16 09:40:01 +0000324 SmallString<128> HomeDir;
325 auto status = path::home_directory(HomeDir);
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000326 EXPECT_TRUE(status);
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000327 EXPECT_EQ(expected, HomeDir);
328 }
Peter Collingbournef7d41012014-01-31 23:46:06 +0000329}
330
Pawel Bylica0e97e5c2015-11-02 09:49:17 +0000331TEST(Support, UserCacheDirectory) {
332 SmallString<13> CacheDir;
333 SmallString<20> CacheDir2;
334 auto Status = path::user_cache_directory(CacheDir, "");
335 EXPECT_TRUE(Status ^ CacheDir.empty());
336
337 if (Status) {
338 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed
339 EXPECT_EQ(CacheDir, CacheDir2); // and return same paths
340
341 EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c"));
342 auto It = path::rbegin(CacheDir);
343 EXPECT_EQ("file.c", *It);
344 EXPECT_EQ("B", *++It);
345 EXPECT_EQ("A", *++It);
346 auto ParentDir = *++It;
347
348 // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0"
349 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2",
350 "\xE2\x84\xB5.0"));
351 auto It2 = path::rbegin(CacheDir2);
352 EXPECT_EQ("\xE2\x84\xB5.0", *It2);
353 EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2);
354 auto ParentDir2 = *++It2;
355
356 EXPECT_EQ(ParentDir, ParentDir2);
357 }
358}
359
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000360TEST(Support, TempDirectory) {
361 SmallString<32> TempDir;
362 path::system_temp_directory(false, TempDir);
363 EXPECT_TRUE(!TempDir.empty());
364 TempDir.clear();
365 path::system_temp_directory(true, TempDir);
366 EXPECT_TRUE(!TempDir.empty());
367}
368
David Blaikie06d56182015-11-17 20:38:54 +0000369#ifdef LLVM_ON_WIN32
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000370static std::string path2regex(std::string Path) {
371 size_t Pos = 0;
372 while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
373 Path.replace(Pos, 1, "\\\\");
374 Pos += 2;
375 }
376 return Path;
377}
378
379/// Helper for running temp dir test in separated process. See below.
380#define EXPECT_TEMP_DIR(prepare, expected) \
381 EXPECT_EXIT( \
382 { \
383 prepare; \
384 SmallString<300> TempDir; \
385 path::system_temp_directory(true, TempDir); \
386 raw_os_ostream(std::cerr) << TempDir; \
387 std::exit(0); \
388 }, \
389 ::testing::ExitedWithCode(0), path2regex(expected))
390
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000391TEST(SupportDeathTest, TempDirectoryOnWindows) {
392 // In this test we want to check how system_temp_directory responds to
393 // different values of specific env vars. To prevent corrupting env vars of
394 // the current process all checks are done in separated processes.
395 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder");
396 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"),
397 "C:\\Unix\\Path\\Seperators");
398 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$");
399 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep");
400 EXPECT_TEMP_DIR(
401 _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
402 "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
403
404 // Test $TMP empty, $TEMP set.
405 EXPECT_TEMP_DIR(
406 {
407 _wputenv_s(L"TMP", L"");
408 _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
409 },
410 "C:\\Valid\\Path");
411
412 // All related env vars empty
413 EXPECT_TEMP_DIR(
414 {
415 _wputenv_s(L"TMP", L"");
416 _wputenv_s(L"TEMP", L"");
417 _wputenv_s(L"USERPROFILE", L"");
418 },
419 "C:\\Temp");
420
421 // Test evn var / path with 260 chars.
422 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
423 while (Expected.size() < 260)
424 Expected.append("\\DirNameWith19Charss");
Reid Kleckner88e9ff62016-02-10 19:29:01 +0000425 ASSERT_EQ(260U, Expected.size());
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000426 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
427}
428#endif
429
Michael J. Spencer346a1332011-01-05 16:39:05 +0000430class FileSystemTest : public testing::Test {
431protected:
432 /// Unique temporary directory in which all created filesystem entities must
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000433 /// be placed. It is removed at the end of each test (must be empty).
Michael J. Spencer346a1332011-01-05 16:39:05 +0000434 SmallString<128> TestDirectory;
435
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000436 void SetUp() override {
Michael J. Spencer346a1332011-01-05 16:39:05 +0000437 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000438 fs::createUniqueDirectory("file-system-test", TestDirectory));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000439 // We don't care about this specific file.
Michael J. Spencer42368cc2011-01-05 16:39:46 +0000440 errs() << "Test Directory: " << TestDirectory << '\n';
441 errs().flush();
Michael J. Spencer346a1332011-01-05 16:39:05 +0000442 }
443
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000444 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000445};
446
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000447TEST_F(FileSystemTest, Unique) {
448 // Create a temp file.
449 int FileDescriptor;
450 SmallString<64> TempPath;
451 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000452 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000453
454 // The same file should return an identical unique id.
Rafael Espindola7f822a92013-07-29 21:26:49 +0000455 fs::UniqueID F1, F2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000456 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
457 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000458 ASSERT_EQ(F1, F2);
459
460 // Different files should return different unique ids.
461 int FileDescriptor2;
462 SmallString<64> TempPath2;
463 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000464 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
465
Rafael Espindola7f822a92013-07-29 21:26:49 +0000466 fs::UniqueID D;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000467 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000468 ASSERT_NE(D, F1);
469 ::close(FileDescriptor2);
470
471 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
472
473 // Two paths representing the same file on disk should still provide the
474 // same unique id. We can test this by making a hard link.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000475 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Rafael Espindola7f822a92013-07-29 21:26:49 +0000476 fs::UniqueID D2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000477 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000478 ASSERT_EQ(D2, F1);
479
480 ::close(FileDescriptor);
Rafael Espindolaa5932af2013-07-30 20:25:53 +0000481
482 SmallString<128> Dir1;
483 ASSERT_NO_ERROR(
484 fs::createUniqueDirectory("dir1", Dir1));
485 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
486 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
487 ASSERT_EQ(F1, F2);
488
489 SmallString<128> Dir2;
490 ASSERT_NO_ERROR(
491 fs::createUniqueDirectory("dir2", Dir2));
492 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
493 ASSERT_NE(F1, F2);
Reid Kleckner1a4398a2016-09-02 01:10:53 +0000494 ASSERT_NO_ERROR(fs::remove(Dir1));
495 ASSERT_NO_ERROR(fs::remove(Dir2));
Reid Kleckner75e557f2016-09-02 00:51:34 +0000496 ASSERT_NO_ERROR(fs::remove(TempPath2));
497 ASSERT_NO_ERROR(fs::remove(TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000498}
499
Zachary Turnere48ace62017-03-10 17:39:21 +0000500TEST_F(FileSystemTest, RealPath) {
501 ASSERT_NO_ERROR(
502 fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3"));
503 ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3"));
504
505 SmallString<64> RealBase;
506 SmallString<64> Expected;
507 SmallString<64> Actual;
508
509 // TestDirectory itself might be under a symlink or have been specified with
510 // a different case than the existing temp directory. In such cases real_path
511 // on the concatenated path will differ in the TestDirectory portion from
512 // how we specified it. Make sure to compare against the real_path of the
513 // TestDirectory, and not just the value of TestDirectory.
514 ASSERT_NO_ERROR(fs::real_path(TestDirectory, RealBase));
515 path::native(Twine(RealBase) + "/test1/test2", Expected);
516
517 ASSERT_NO_ERROR(fs::real_path(
518 Twine(TestDirectory) + "/././test1/../test1/test2/./test3/..", Actual));
519
520 EXPECT_EQ(Expected, Actual);
521
522 SmallString<64> HomeDir;
Zachary Turneraeab48a2017-03-10 17:47:13 +0000523 bool Result = llvm::sys::path::home_directory(HomeDir);
524 if (Result) {
525 ASSERT_NO_ERROR(fs::real_path(HomeDir, Expected));
526 ASSERT_NO_ERROR(fs::real_path("~", Actual, true));
527 EXPECT_EQ(Expected, Actual);
528 ASSERT_NO_ERROR(fs::real_path("~/", Actual, true));
529 EXPECT_EQ(Expected, Actual);
530 }
Zachary Turnere48ace62017-03-10 17:39:21 +0000531
532 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
533}
534
Michael J. Spencer346a1332011-01-05 16:39:05 +0000535TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000536 // Create a temp file.
Michael J. Spencer45710402010-12-03 01:21:28 +0000537 int FileDescriptor;
538 SmallString<64> TempPath;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000539 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000540 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Michael J. Spencer45710402010-12-03 01:21:28 +0000541
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000542 // Make sure it exists.
Rafael Espindolac1599152014-09-11 19:11:02 +0000543 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000544
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000545 // Create another temp tile.
546 int FD2;
547 SmallString<64> TempPath2;
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000548 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
Rafael Espindolad3c89042013-07-25 15:00:17 +0000549 ASSERT_TRUE(TempPath2.endswith(".temp"));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000550 ASSERT_NE(TempPath.str(), TempPath2.str());
551
Michael J. Spencer203d7802011-12-12 06:04:28 +0000552 fs::file_status A, B;
553 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
554 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
555 EXPECT_FALSE(fs::equivalent(A, B));
556
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000557 ::close(FD2);
Rafael Espindola213c4cb2013-07-18 03:29:51 +0000558
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000559 // Remove Temp2.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000560 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
561 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
562 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000563 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000564
Rafael Espindolac049c652014-06-13 03:20:08 +0000565 std::error_code EC = fs::status(TempPath2.c_str(), B);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000566 EXPECT_EQ(EC, errc::no_such_file_or_directory);
Rafael Espindola107b74c2013-07-31 00:10:25 +0000567 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
568
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000569 // Make sure Temp2 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000570 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
571 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000572
Rafael Espindolad3c89042013-07-25 15:00:17 +0000573 SmallString<64> TempPath3;
574 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
575 ASSERT_FALSE(TempPath3.endswith("."));
Reid Kleckner75e557f2016-09-02 00:51:34 +0000576 FileRemover Cleanup3(TempPath3);
Rafael Espindolad3c89042013-07-25 15:00:17 +0000577
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000578 // Create a hard link to Temp1.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000579 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000580 bool equal;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000581 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000582 EXPECT_TRUE(equal);
Michael J. Spencer203d7802011-12-12 06:04:28 +0000583 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
584 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
585 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000586
587 // Remove Temp1.
Michael J. Spencer45710402010-12-03 01:21:28 +0000588 ::close(FileDescriptor);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000589 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000590
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000591 // Remove the hard link.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000592 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000593
594 // Make sure Temp1 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000595 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
596 errc::no_such_file_or_directory);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000597
598#ifdef LLVM_ON_WIN32
599 // Path name > 260 chars should get an error.
600 const char *Path270 =
601 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
602 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
603 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
604 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
605 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000606 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000607 errc::invalid_argument);
608 // Relative path < 247 chars, no problem.
609 const char *Path216 =
610 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
611 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
612 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
613 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000614 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000615 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000616#endif
Michael J. Spencer346a1332011-01-05 16:39:05 +0000617}
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000618
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000619TEST_F(FileSystemTest, CreateDir) {
620 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
621 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
622 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000623 errc::file_exists);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000624 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000625
Frederic Riss6b9396c2015-08-06 21:04:55 +0000626#ifdef LLVM_ON_UNIX
627 // Set a 0000 umask so that we can test our directory permissions.
628 mode_t OldUmask = ::umask(0000);
629
630 fs::file_status Status;
631 ASSERT_NO_ERROR(
632 fs::create_directory(Twine(TestDirectory) + "baz500", false,
633 fs::perms::owner_read | fs::perms::owner_exe));
634 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
635 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
636 fs::perms::owner_read | fs::perms::owner_exe);
637 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
638 fs::perms::all_all));
639 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
640 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
641
642 // Restore umask to be safe.
643 ::umask(OldUmask);
644#endif
645
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000646#ifdef LLVM_ON_WIN32
647 // Prove that create_directories() can handle a pathname > 248 characters,
648 // which is the documented limit for CreateDirectory().
649 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
650 // Generate a directory path guaranteed to fall into that range.
651 size_t TmpLen = TestDirectory.size();
652 const char *OneDir = "\\123456789";
653 size_t OneDirLen = strlen(OneDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000654 ASSERT_LT(OneDirLen, 12U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000655 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
656 SmallString<260> LongDir(TestDirectory);
657 for (size_t I = 0; I < NLevels; ++I)
658 LongDir.append(OneDir);
659 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
660 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
661 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
662 errc::file_exists);
663 // Tidy up, "recursively" removing the directories.
664 StringRef ThisDir(LongDir);
665 for (size_t J = 0; J < NLevels; ++J) {
666 ASSERT_NO_ERROR(fs::remove(ThisDir));
667 ThisDir = path::parent_path(ThisDir);
668 }
669
670 // Similarly for a relative pathname. Need to set the current directory to
671 // TestDirectory so that the one we create ends up in the right place.
672 char PreviousDir[260];
673 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000674 ASSERT_GT(PreviousDirLen, 0U);
675 ASSERT_LT(PreviousDirLen, 260U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000676 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
677 LongDir.clear();
678 // Generate a relative directory name with absolute length > 248.
679 size_t LongDirLen = 249 - TestDirectory.size();
680 LongDir.assign(LongDirLen, 'a');
681 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
682 // While we're here, prove that .. and . handling works in these long paths.
683 const char *DotDotDirs = "\\..\\.\\b";
684 LongDir.append(DotDotDirs);
Paul Robinson1b6c7342014-11-13 00:36:34 +0000685 ASSERT_NO_ERROR(fs::create_directory("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000686 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
687 // And clean up.
Paul Robinson1b6c7342014-11-13 00:36:34 +0000688 ASSERT_NO_ERROR(fs::remove("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000689 ASSERT_NO_ERROR(fs::remove(
690 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
691 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
692#endif
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000693}
694
Michael J. Spencer346a1332011-01-05 16:39:05 +0000695TEST_F(FileSystemTest, DirectoryIteration) {
Rafael Espindolac049c652014-06-13 03:20:08 +0000696 std::error_code ec;
Michael J. Spencer1f063602011-01-06 05:57:54 +0000697 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
698 ASSERT_NO_ERROR(ec);
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000699
700 // Create a known hierarchy to recurse over.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000701 ASSERT_NO_ERROR(
702 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
703 ASSERT_NO_ERROR(
704 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
705 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
706 "/recursive/dontlookhere/da1"));
707 ASSERT_NO_ERROR(
708 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
709 ASSERT_NO_ERROR(
710 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000711 typedef std::vector<std::string> v_t;
712 v_t visited;
713 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
714 + "/recursive", ec), e; i != e; i.increment(ec)){
715 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000716 if (path::filename(i->path()) == "p1") {
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000717 i.pop();
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000718 // FIXME: recursive_directory_iterator should be more robust.
719 if (i == e) break;
720 }
Michael J. Spencer9b54a3e2011-12-09 01:14:41 +0000721 if (path::filename(i->path()) == "dontlookhere")
722 i.no_push();
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000723 visited.push_back(path::filename(i->path()));
724 }
David Majnemer0d955d02016-08-11 22:21:41 +0000725 v_t::const_iterator a0 = find(visited, "a0");
726 v_t::const_iterator aa1 = find(visited, "aa1");
727 v_t::const_iterator ab1 = find(visited, "ab1");
728 v_t::const_iterator dontlookhere = find(visited, "dontlookhere");
729 v_t::const_iterator da1 = find(visited, "da1");
730 v_t::const_iterator z0 = find(visited, "z0");
731 v_t::const_iterator za1 = find(visited, "za1");
732 v_t::const_iterator pop = find(visited, "pop");
733 v_t::const_iterator p1 = find(visited, "p1");
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000734
735 // Make sure that each path was visited correctly.
736 ASSERT_NE(a0, visited.end());
737 ASSERT_NE(aa1, visited.end());
738 ASSERT_NE(ab1, visited.end());
739 ASSERT_NE(dontlookhere, visited.end());
740 ASSERT_EQ(da1, visited.end()); // Not visited.
741 ASSERT_NE(z0, visited.end());
742 ASSERT_NE(za1, visited.end());
743 ASSERT_NE(pop, visited.end());
744 ASSERT_EQ(p1, visited.end()); // Not visited.
745
746 // Make sure that parents were visited before children. No other ordering
747 // guarantees can be made across siblings.
748 ASSERT_LT(a0, aa1);
749 ASSERT_LT(a0, ab1);
750 ASSERT_LT(z0, za1);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000751
752 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
753 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
754 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
755 ASSERT_NO_ERROR(
756 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
757 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
758 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
759 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
760 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
761 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
762 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
Bruno Cardoso Lopesead771c2016-05-13 21:31:32 +0000763
764 // Test recursive_directory_iterator level()
765 ASSERT_NO_ERROR(
766 fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c"));
767 fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E;
768 for (int l = 0; I != E; I.increment(ec), ++l) {
769 ASSERT_NO_ERROR(ec);
770 EXPECT_EQ(I.level(), l);
771 }
772 EXPECT_EQ(I, E);
773 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c"));
774 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b"));
775 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a"));
776 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel"));
Michael J. Spencer1f063602011-01-06 05:57:54 +0000777}
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000778
Juergen Ributzkae7d0fa682017-03-13 21:34:07 +0000779#ifdef LLVM_ON_UNIX
780TEST_F(FileSystemTest, BrokenSymlinkDirectoryIteration) {
781 // Create a known hierarchy to recurse over.
782 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + "/symlink"));
783 ASSERT_NO_ERROR(
784 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/a"));
785 ASSERT_NO_ERROR(
786 fs::create_directories(Twine(TestDirectory) + "/symlink/b/bb"));
787 ASSERT_NO_ERROR(
788 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/ba"));
789 ASSERT_NO_ERROR(
790 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/bc"));
791 ASSERT_NO_ERROR(
792 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/c"));
793 ASSERT_NO_ERROR(
794 fs::create_directories(Twine(TestDirectory) + "/symlink/d/dd/ddd"));
795 ASSERT_NO_ERROR(fs::create_link(Twine(TestDirectory) + "/symlink/d/dd",
796 Twine(TestDirectory) + "/symlink/d/da"));
797 ASSERT_NO_ERROR(
798 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/e"));
799
800 typedef std::vector<std::string> v_t;
801 v_t visited;
802
803 // The directory iterator doesn't stat the file, so we should be able to
804 // iterate over the whole directory.
805 std::error_code ec;
806 for (fs::directory_iterator i(Twine(TestDirectory) + "/symlink", ec), e;
807 i != e; i.increment(ec)) {
808 ASSERT_NO_ERROR(ec);
809 visited.push_back(path::filename(i->path()));
810 }
811 std::sort(visited.begin(), visited.end());
812 v_t expected = {"a", "b", "c", "d", "e"};
813 ASSERT_TRUE(visited.size() == expected.size());
814 ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin()));
815 visited.clear();
816
817 // The recursive directory iterator has to stat the file, so we need to skip
818 // the broken symlinks.
819 for (fs::recursive_directory_iterator
820 i(Twine(TestDirectory) + "/symlink", ec),
821 e;
822 i != e; i.increment(ec)) {
823 ASSERT_NO_ERROR(ec);
824
825 fs::file_status status;
Juergen Ributzka118bb452017-03-14 18:37:44 +0000826 if (i->status(status) ==
827 std::make_error_code(std::errc::no_such_file_or_directory)) {
Juergen Ributzkae7d0fa682017-03-13 21:34:07 +0000828 i.no_push();
829 continue;
830 }
831
832 visited.push_back(path::filename(i->path()));
833 }
834 std::sort(visited.begin(), visited.end());
835 expected = {"b", "bb", "d", "da", "dd", "ddd", "ddd"};
836 ASSERT_TRUE(visited.size() == expected.size());
837 ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin()));
838 visited.clear();
839
840 // This recursive directory iterator doesn't follow symlinks, so we don't need
841 // to skip them.
842 for (fs::recursive_directory_iterator
843 i(Twine(TestDirectory) + "/symlink", ec, /*follow_symlinks=*/false),
844 e;
845 i != e; i.increment(ec)) {
846 ASSERT_NO_ERROR(ec);
847 visited.push_back(path::filename(i->path()));
848 }
Juergen Ributzkac0d61602017-03-13 21:40:20 +0000849 std::sort(visited.begin(), visited.end());
Juergen Ributzkae7d0fa682017-03-13 21:34:07 +0000850 expected = {"a", "b", "ba", "bb", "bc", "c", "d", "da", "dd", "ddd", "e"};
851 ASSERT_TRUE(visited.size() == expected.size());
852 ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin()));
853
854 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/symlink"));
855}
856#endif
857
Zachary Turner260bda32017-03-08 22:49:32 +0000858TEST_F(FileSystemTest, Remove) {
859 SmallString<64> BaseDir;
860 SmallString<64> Paths[4];
861 int fds[4];
862 ASSERT_NO_ERROR(fs::createUniqueDirectory("fs_remove", BaseDir));
863
864 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/baz"));
865 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/buzz"));
866 ASSERT_NO_ERROR(fs::createUniqueFile(
867 Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[0], Paths[0]));
868 ASSERT_NO_ERROR(fs::createUniqueFile(
869 Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[1], Paths[1]));
870 ASSERT_NO_ERROR(fs::createUniqueFile(
871 Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[2], Paths[2]));
872 ASSERT_NO_ERROR(fs::createUniqueFile(
873 Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[3], Paths[3]));
874
875 for (int fd : fds)
876 ::close(fd);
877
878 EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/baz"));
879 EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/buzz"));
880 EXPECT_TRUE(fs::exists(Paths[0]));
881 EXPECT_TRUE(fs::exists(Paths[1]));
882 EXPECT_TRUE(fs::exists(Paths[2]));
883 EXPECT_TRUE(fs::exists(Paths[3]));
884
885 ASSERT_NO_ERROR(fs::remove_directories("D:/footest"));
886
887 ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
888 ASSERT_FALSE(fs::exists(BaseDir));
889}
890
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000891const char archive[] = "!<arch>\x0A";
892const char bitcode[] = "\xde\xc0\x17\x0b";
Rui Ueyama829c4392013-11-14 22:09:08 +0000893const char coff_object[] = "\x00\x00......";
Rui Ueyama5c69ff52014-09-11 22:34:32 +0000894const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
Rui Ueyama2acb0582014-09-11 21:09:57 +0000895 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000896const char coff_import_library[] = "\x00\x00\xff\xff....";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000897const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
898 0, 0, 0, 0, 0, 0, 0, 0, 1 };
Etienne Bergeron1562f692016-04-05 01:46:26 +0000899const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
Kevin Enderby87c85b72016-01-26 23:43:37 +0000900const char macho_object[] =
901 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
902const char macho_executable[] =
903 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000904const char macho_fixed_virtual_memory_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000905 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
906const char macho_core[] =
907 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
908const char macho_preload_executable[] =
909 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000910const char macho_dynamically_linked_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000911 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
912const char macho_dynamic_linker[] =
913 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
914const char macho_bundle[] =
915 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
916const char macho_dsym_companion[] =
917 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
918const char macho_kext_bundle[] =
919 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000920const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000921const char macho_dynamically_linked_shared_lib_stub[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000922 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000923
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000924TEST_F(FileSystemTest, Magic) {
925 struct type {
926 const char *filename;
927 const char *magic_str;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000928 size_t magic_str_len;
929 fs::file_magic magic;
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000930 } types[] = {
931#define DEFINE(magic) \
932 { #magic, magic, sizeof(magic), fs::file_magic::magic }
933 DEFINE(archive),
934 DEFINE(bitcode),
Rui Ueyama829c4392013-11-14 22:09:08 +0000935 DEFINE(coff_object),
Rui Ueyama2acb0582014-09-11 21:09:57 +0000936 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000937 DEFINE(coff_import_library),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000938 DEFINE(elf_relocatable),
939 DEFINE(macho_universal_binary),
940 DEFINE(macho_object),
941 DEFINE(macho_executable),
942 DEFINE(macho_fixed_virtual_memory_shared_lib),
943 DEFINE(macho_core),
944 DEFINE(macho_preload_executable),
945 DEFINE(macho_dynamically_linked_shared_lib),
946 DEFINE(macho_dynamic_linker),
947 DEFINE(macho_bundle),
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000948 DEFINE(macho_dynamically_linked_shared_lib_stub),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000949 DEFINE(macho_dsym_companion),
Justin Bognera7ad4b32015-02-25 22:59:20 +0000950 DEFINE(macho_kext_bundle),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000951 DEFINE(windows_resource)
952#undef DEFINE
953 };
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000954
955 // Create some files filled with magic.
956 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
957 ++i) {
958 SmallString<128> file_pathname(TestDirectory);
959 path::append(file_pathname, i->filename);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000960 std::error_code EC;
961 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000962 ASSERT_FALSE(file.has_error());
963 StringRef magic(i->magic_str, i->magic_str_len);
964 file << magic;
Michael J. Spencerb5871802011-01-15 18:52:49 +0000965 file.close();
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000966 EXPECT_EQ(i->magic, fs::identify_magic(magic));
Rafael Espindola78dcc032014-01-10 20:36:42 +0000967 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000968 }
Michael J. Spencer98847782010-11-24 19:20:05 +0000969}
970
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000971#ifdef LLVM_ON_WIN32
972TEST_F(FileSystemTest, CarriageReturn) {
973 SmallString<128> FilePathname(TestDirectory);
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000974 std::error_code EC;
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000975 path::append(FilePathname, "test");
976
977 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000978 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
979 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000980 File << '\n';
981 }
982 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000983 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000984 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000985 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000986 }
987
988 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000989 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
990 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000991 File << '\n';
992 }
993 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000994 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000995 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000996 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000997 }
Rafael Espindola78dcc032014-01-10 20:36:42 +0000998 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000999}
1000#endif
1001
Rafael Espindola59aaa6c2014-12-12 17:55:12 +00001002TEST_F(FileSystemTest, Resize) {
1003 int FD;
1004 SmallString<64> TempPath;
1005 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
1006 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
1007 fs::file_status Status;
1008 ASSERT_NO_ERROR(fs::status(FD, Status));
1009 ASSERT_EQ(Status.getSize(), 123U);
Reid Kleckner75e557f2016-09-02 00:51:34 +00001010 ::close(FD);
1011 ASSERT_NO_ERROR(fs::remove(TempPath));
Rafael Espindola59aaa6c2014-12-12 17:55:12 +00001012}
1013
Nick Kledzik18497e92012-06-20 00:28:54 +00001014TEST_F(FileSystemTest, FileMapping) {
1015 // Create a temp file.
1016 int FileDescriptor;
1017 SmallString<64> TempPath;
1018 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +00001019 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Rafael Espindolac69f13b2014-12-12 18:13:23 +00001020 unsigned Size = 4096;
1021 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
1022
Nick Kledzik18497e92012-06-20 00:28:54 +00001023 // Map in temp file and add some content
Rafael Espindolac049c652014-06-13 03:20:08 +00001024 std::error_code EC;
Michael J. Spenceref2284f2012-08-15 19:05:47 +00001025 StringRef Val("hello there");
1026 {
1027 fs::mapped_file_region mfr(FileDescriptor,
Rafael Espindolac69f13b2014-12-12 18:13:23 +00001028 fs::mapped_file_region::readwrite, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +00001029 ASSERT_NO_ERROR(EC);
1030 std::copy(Val.begin(), Val.end(), mfr.data());
1031 // Explicitly add a 0.
1032 mfr.data()[Val.size()] = 0;
1033 // Unmap temp file
1034 }
Reid Kleckner75e557f2016-09-02 00:51:34 +00001035 ASSERT_EQ(close(FileDescriptor), 0);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +00001036
Nick Kledzik18497e92012-06-20 00:28:54 +00001037 // Map it back in read-only
Reid Kleckner75e557f2016-09-02 00:51:34 +00001038 {
1039 int FD;
1040 EC = fs::openFileForRead(Twine(TempPath), FD);
1041 ASSERT_NO_ERROR(EC);
1042 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
1043 ASSERT_NO_ERROR(EC);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +00001044
Reid Kleckner75e557f2016-09-02 00:51:34 +00001045 // Verify content
1046 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +00001047
Reid Kleckner75e557f2016-09-02 00:51:34 +00001048 // Unmap temp file
1049 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
1050 ASSERT_NO_ERROR(EC);
1051 ASSERT_EQ(close(FD), 0);
1052 }
1053 ASSERT_NO_ERROR(fs::remove(TempPath));
Michael J. Spenceref2284f2012-08-15 19:05:47 +00001054}
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001055
1056TEST(Support, NormalizePath) {
Zachary Turner5c5091f2017-03-16 22:28:04 +00001057 using TestTuple = std::tuple<StringRef, StringRef, StringRef>;
1058 TestTuple Tests[] = {{"a", "a", "a"}, {"a/b", "a\\b", "a/b"},
1059 {"a\\b", "a\\b", "a/b"}, {"a\\\\b", "a\\\\b", "a\\\\b"},
1060 {"\\a", "\\a", "/a"}, {"a\\", "a\\", "a/"}};
1061 for (auto &T : Tests) {
1062 SmallString<64> Win = std::get<0>(T);
1063 SmallString<64> Posix = Win;
1064 path::native(Win, path::Style::windows);
1065 path::native(Posix, path::Style::posix);
1066 EXPECT_EQ(std::get<1>(T), Win);
1067 EXPECT_EQ(std::get<2>(T), Posix);
1068 }
Serge Pavlov9c761a32017-03-01 09:38:15 +00001069
1070#if defined(LLVM_ON_WIN32)
1071 SmallString<64> PathHome;
1072 path::home_directory(PathHome);
1073
1074 const char *Path7a = "~/aaa";
1075 SmallString<64> Path7(Path7a);
1076 path::native(Path7);
1077 EXPECT_TRUE(Path7.endswith("\\aaa"));
1078 EXPECT_TRUE(Path7.startswith(PathHome));
1079 EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1));
1080
1081 const char *Path8a = "~";
1082 SmallString<64> Path8(Path8a);
1083 path::native(Path8);
1084 EXPECT_EQ(Path8, PathHome);
1085
1086 const char *Path9a = "~aaa";
1087 SmallString<64> Path9(Path9a);
1088 path::native(Path9);
1089 EXPECT_EQ(Path9, "~aaa");
1090
1091 const char *Path10a = "aaa/~/b";
1092 SmallString<64> Path10(Path10a);
1093 path::native(Path10);
1094 EXPECT_EQ(Path10, "aaa\\~\\b");
1095#endif
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001096}
Douglas Katzmana26be4a2015-09-02 21:02:10 +00001097
1098TEST(Support, RemoveLeadingDotSlash) {
1099 StringRef Path1("././/foolz/wat");
1100 StringRef Path2("./////");
1101
1102 Path1 = path::remove_leading_dotslash(Path1);
1103 EXPECT_EQ(Path1, "foolz/wat");
1104 Path2 = path::remove_leading_dotslash(Path2);
1105 EXPECT_EQ(Path2, "");
1106}
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001107
Zachary Turner5c5091f2017-03-16 22:28:04 +00001108static std::string remove_dots(StringRef path, bool remove_dot_dot,
1109 path::Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001110 SmallString<256> buffer(path);
Zachary Turner5c5091f2017-03-16 22:28:04 +00001111 path::remove_dots(buffer, remove_dot_dot, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001112 return buffer.str();
1113}
1114
1115TEST(Support, RemoveDots) {
Zachary Turner5c5091f2017-03-16 22:28:04 +00001116 EXPECT_EQ("foolz\\wat",
1117 remove_dots(".\\.\\\\foolz\\wat", false, path::Style::windows));
1118 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false, path::Style::windows));
Mike Aizatskyf8cf7132015-11-09 19:36:53 +00001119
Zachary Turner5c5091f2017-03-16 22:28:04 +00001120 EXPECT_EQ("a\\..\\b\\c",
1121 remove_dots(".\\a\\..\\b\\c", false, path::Style::windows));
1122 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true, path::Style::windows));
1123 EXPECT_EQ("c", remove_dots(".\\.\\c", true, path::Style::windows));
1124 EXPECT_EQ("..\\a\\c",
1125 remove_dots("..\\a\\b\\..\\c", true, path::Style::windows));
1126 EXPECT_EQ("..\\..\\a\\c",
1127 remove_dots("..\\..\\a\\b\\..\\c", true, path::Style::windows));
Mike Aizatskyf8cf7132015-11-09 19:36:53 +00001128
1129 SmallString<64> Path1(".\\.\\c");
Zachary Turner5c5091f2017-03-16 22:28:04 +00001130 EXPECT_TRUE(path::remove_dots(Path1, true, path::Style::windows));
Mike Aizatskyf8cf7132015-11-09 19:36:53 +00001131 EXPECT_EQ("c", Path1);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001132
Zachary Turner5c5091f2017-03-16 22:28:04 +00001133 EXPECT_EQ("foolz/wat",
1134 remove_dots("././/foolz/wat", false, path::Style::posix));
1135 EXPECT_EQ("", remove_dots("./////", false, path::Style::posix));
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001136
Zachary Turner5c5091f2017-03-16 22:28:04 +00001137 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false, path::Style::posix));
1138 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true, path::Style::posix));
1139 EXPECT_EQ("c", remove_dots("././c", true, path::Style::posix));
1140 EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true, path::Style::posix));
1141 EXPECT_EQ("../../a/c",
1142 remove_dots("../../a/b/../c", true, path::Style::posix));
1143 EXPECT_EQ("/a/c", remove_dots("/../../a/c", true, path::Style::posix));
1144 EXPECT_EQ("/a/c",
1145 remove_dots("/../a/b//../././/c", true, path::Style::posix));
1146
1147 SmallString<64> Path2("././c");
1148 EXPECT_TRUE(path::remove_dots(Path2, true, path::Style::posix));
1149 EXPECT_EQ("c", Path2);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001150}
Teresa Johnsonbbd10b42016-05-17 14:45:30 +00001151
1152TEST(Support, ReplacePathPrefix) {
1153 SmallString<64> Path1("/foo");
1154 SmallString<64> Path2("/old/foo");
1155 SmallString<64> OldPrefix("/old");
1156 SmallString<64> NewPrefix("/new");
1157 SmallString<64> NewPrefix2("/longernew");
1158 SmallString<64> EmptyPrefix("");
1159
1160 SmallString<64> Path = Path1;
1161 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1162 EXPECT_EQ(Path, "/foo");
1163 Path = Path2;
1164 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1165 EXPECT_EQ(Path, "/new/foo");
1166 Path = Path2;
1167 path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
1168 EXPECT_EQ(Path, "/longernew/foo");
1169 Path = Path1;
1170 path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
1171 EXPECT_EQ(Path, "/new/foo");
1172 Path = Path2;
1173 path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
1174 EXPECT_EQ(Path, "/foo");
1175}
Taewook Ohd9153272016-06-13 15:54:56 +00001176
1177TEST_F(FileSystemTest, PathFromFD) {
1178 // Create a temp file.
1179 int FileDescriptor;
1180 SmallString<64> TempPath;
1181 ASSERT_NO_ERROR(
1182 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001183 FileRemover Cleanup(TempPath);
Taewook Ohd9153272016-06-13 15:54:56 +00001184
1185 // Make sure it exists.
1186 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1187
1188 // Try to get the path from the file descriptor
1189 SmallString<64> ResultPath;
1190 std::error_code ErrorCode =
1191 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1192
1193 // If we succeeded, check that the paths are the same (modulo case):
1194 if (!ErrorCode) {
1195 // The paths returned by createTemporaryFile and getPathFromOpenFD
1196 // should reference the same file on disk.
1197 fs::UniqueID D1, D2;
1198 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1199 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1200 ASSERT_EQ(D1, D2);
1201 }
1202
1203 ::close(FileDescriptor);
1204}
1205
Aaron Ballman3dd74b82016-06-20 20:28:49 +00001206TEST_F(FileSystemTest, PathFromFDWin32) {
1207 // Create a temp file.
1208 int FileDescriptor;
1209 SmallString<64> TempPath;
1210 ASSERT_NO_ERROR(
1211 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001212 FileRemover Cleanup(TempPath);
Aaron Ballman3dd74b82016-06-20 20:28:49 +00001213
1214 // Make sure it exists.
1215 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1216
1217 SmallVector<char, 8> ResultPath;
1218 std::error_code ErrorCode =
1219 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1220
1221 if (!ErrorCode) {
1222 // Now that we know how much space is required for the path, create a path
1223 // buffer with exactly enough space (sans null terminator, which should not
1224 // be present), and call getPathFromOpenFD again to ensure that the API
1225 // properly handles exactly-sized buffers.
1226 SmallVector<char, 8> ExactSizedPath(ResultPath.size());
1227 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath);
1228 ResultPath = ExactSizedPath;
1229 }
1230
1231 if (!ErrorCode) {
1232 fs::UniqueID D1, D2;
1233 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1234 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1235 ASSERT_EQ(D1, D2);
1236 }
1237 ::close(FileDescriptor);
1238}
1239
Aaron Ballman0ad00462016-06-21 14:24:48 +00001240TEST_F(FileSystemTest, PathFromFDUnicode) {
1241 // Create a temp file.
1242 int FileDescriptor;
1243 SmallString<64> TempPath;
1244
1245 // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0"
1246 ASSERT_NO_ERROR(
1247 fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
1248 "\xE2\x84\xB5.0", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001249 FileRemover Cleanup(TempPath);
Aaron Ballman0ad00462016-06-21 14:24:48 +00001250
1251 // Make sure it exists.
1252 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1253
1254 SmallVector<char, 8> ResultPath;
1255 std::error_code ErrorCode =
1256 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1257
1258 if (!ErrorCode) {
1259 fs::UniqueID D1, D2;
1260 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1261 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1262 ASSERT_EQ(D1, D2);
1263 }
1264 ::close(FileDescriptor);
1265}
1266
Taewook Ohd9153272016-06-13 15:54:56 +00001267TEST_F(FileSystemTest, OpenFileForRead) {
1268 // Create a temp file.
1269 int FileDescriptor;
1270 SmallString<64> TempPath;
1271 ASSERT_NO_ERROR(
1272 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001273 FileRemover Cleanup(TempPath);
Taewook Ohd9153272016-06-13 15:54:56 +00001274
1275 // Make sure it exists.
1276 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1277
1278 // Open the file for read
1279 int FileDescriptor2;
1280 SmallString<64> ResultPath;
1281 ASSERT_NO_ERROR(
1282 fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath))
1283
1284 // If we succeeded, check that the paths are the same (modulo case):
1285 if (!ResultPath.empty()) {
1286 // The paths returned by createTemporaryFile and getPathFromOpenFD
1287 // should reference the same file on disk.
1288 fs::UniqueID D1, D2;
1289 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1290 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1291 ASSERT_EQ(D1, D2);
1292 }
1293
1294 ::close(FileDescriptor);
1295}
Pavel Labath2f096092017-01-24 10:32:03 +00001296
1297TEST_F(FileSystemTest, set_current_path) {
1298 SmallString<128> path;
1299
1300 ASSERT_NO_ERROR(fs::current_path(path));
1301 ASSERT_NE(TestDirectory, path);
1302
1303 struct RestorePath {
1304 SmallString<128> path;
1305 RestorePath(const SmallString<128> &path) : path(path) {}
1306 ~RestorePath() { fs::set_current_path(path); }
1307 } restore_path(path);
1308
1309 ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
1310
1311 ASSERT_NO_ERROR(fs::current_path(path));
Pavel Labath50440092017-01-24 11:35:26 +00001312
1313 fs::UniqueID D1, D2;
1314 ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1));
1315 ASSERT_NO_ERROR(fs::getUniqueID(path, D2));
Aaron Ballman345012d2017-03-13 12:24:51 +00001316 ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path;
1317}
1318
James Henderson566fdf42017-03-16 11:22:09 +00001319TEST_F(FileSystemTest, permissions) {
1320 int FD;
1321 SmallString<64> TempPath;
1322 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
1323 FileRemover Cleanup(TempPath);
1324
1325 // Make sure it exists.
1326 ASSERT_TRUE(fs::exists(Twine(TempPath)));
1327
1328 auto CheckPermissions = [&](fs::perms Expected) {
1329 ErrorOr<fs::perms> Actual = fs::getPermissions(TempPath);
1330 return Actual && *Actual == Expected;
1331 };
1332
1333 std::error_code NoError;
1334 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all), NoError);
1335 EXPECT_TRUE(CheckPermissions(fs::all_all));
1336
1337 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::all_exe), NoError);
1338 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::all_exe));
1339
1340#if defined(LLVM_ON_WIN32)
1341 fs::perms ReadOnly = fs::all_read | fs::all_exe;
1342 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
1343 EXPECT_TRUE(CheckPermissions(ReadOnly));
1344
1345 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
1346 EXPECT_TRUE(CheckPermissions(ReadOnly));
1347
1348 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
1349 EXPECT_TRUE(CheckPermissions(fs::all_all));
1350
1351 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
1352 EXPECT_TRUE(CheckPermissions(ReadOnly));
1353
1354 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
1355 EXPECT_TRUE(CheckPermissions(fs::all_all));
1356
1357 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
1358 EXPECT_TRUE(CheckPermissions(ReadOnly));
1359
1360 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
1361 EXPECT_TRUE(CheckPermissions(fs::all_all));
1362
1363 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
1364 EXPECT_TRUE(CheckPermissions(ReadOnly));
1365
1366 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
1367 EXPECT_TRUE(CheckPermissions(fs::all_all));
1368
1369 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
1370 EXPECT_TRUE(CheckPermissions(ReadOnly));
1371
1372 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
1373 EXPECT_TRUE(CheckPermissions(fs::all_all));
1374
1375 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
1376 EXPECT_TRUE(CheckPermissions(ReadOnly));
1377
1378 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
1379 EXPECT_TRUE(CheckPermissions(fs::all_all));
1380
1381 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
1382 EXPECT_TRUE(CheckPermissions(ReadOnly));
1383
1384 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
1385 EXPECT_TRUE(CheckPermissions(fs::all_all));
1386
1387 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
1388 EXPECT_TRUE(CheckPermissions(ReadOnly));
1389
1390 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
1391 EXPECT_TRUE(CheckPermissions(ReadOnly));
1392
1393 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
1394 EXPECT_TRUE(CheckPermissions(ReadOnly));
1395
1396 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
1397 EXPECT_TRUE(CheckPermissions(ReadOnly));
1398
1399 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
1400 fs::set_gid_on_exe |
1401 fs::sticky_bit),
1402 NoError);
1403 EXPECT_TRUE(CheckPermissions(ReadOnly));
1404
1405 EXPECT_EQ(fs::setPermissions(TempPath, ReadOnly | fs::set_uid_on_exe |
1406 fs::set_gid_on_exe |
1407 fs::sticky_bit),
1408 NoError);
1409 EXPECT_TRUE(CheckPermissions(ReadOnly));
1410
1411 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError);
1412 EXPECT_TRUE(CheckPermissions(fs::all_all));
1413#else
1414 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
1415 EXPECT_TRUE(CheckPermissions(fs::no_perms));
1416
1417 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
1418 EXPECT_TRUE(CheckPermissions(fs::owner_read));
1419
1420 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
1421 EXPECT_TRUE(CheckPermissions(fs::owner_write));
1422
1423 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
1424 EXPECT_TRUE(CheckPermissions(fs::owner_exe));
1425
1426 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
1427 EXPECT_TRUE(CheckPermissions(fs::owner_all));
1428
1429 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
1430 EXPECT_TRUE(CheckPermissions(fs::group_read));
1431
1432 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
1433 EXPECT_TRUE(CheckPermissions(fs::group_write));
1434
1435 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
1436 EXPECT_TRUE(CheckPermissions(fs::group_exe));
1437
1438 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
1439 EXPECT_TRUE(CheckPermissions(fs::group_all));
1440
1441 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
1442 EXPECT_TRUE(CheckPermissions(fs::others_read));
1443
1444 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
1445 EXPECT_TRUE(CheckPermissions(fs::others_write));
1446
1447 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
1448 EXPECT_TRUE(CheckPermissions(fs::others_exe));
1449
1450 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
1451 EXPECT_TRUE(CheckPermissions(fs::others_all));
1452
1453 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
1454 EXPECT_TRUE(CheckPermissions(fs::all_read));
1455
1456 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
1457 EXPECT_TRUE(CheckPermissions(fs::all_write));
1458
1459 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
1460 EXPECT_TRUE(CheckPermissions(fs::all_exe));
1461
1462 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
1463 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe));
1464
1465 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
1466 EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe));
1467
1468 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
1469 EXPECT_TRUE(CheckPermissions(fs::sticky_bit));
1470
1471 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
1472 fs::set_gid_on_exe |
1473 fs::sticky_bit),
1474 NoError);
1475 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe | fs::set_gid_on_exe |
1476 fs::sticky_bit));
1477
1478 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::set_uid_on_exe |
1479 fs::set_gid_on_exe |
1480 fs::sticky_bit),
1481 NoError);
1482 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::set_uid_on_exe |
1483 fs::set_gid_on_exe | fs::sticky_bit));
1484
1485 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError);
1486 EXPECT_TRUE(CheckPermissions(fs::all_perms));
1487#endif
1488}
1489
Aaron Ballman345012d2017-03-13 12:24:51 +00001490} // anonymous namespace