blob: 8068b3203063f9c6ecb52660f75343456be75a8b [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
56#ifdef LLVM_ON_WIN32
57 EXPECT_TRUE(path::is_separator('\\'));
58#else
59 EXPECT_FALSE(path::is_separator('\\'));
60#endif
61}
62
Michael J. Spencer3ef91c52010-11-29 22:29:04 +000063TEST(Support, Path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000064 SmallVector<StringRef, 40> paths;
65 paths.push_back("");
66 paths.push_back(".");
67 paths.push_back("..");
68 paths.push_back("foo");
69 paths.push_back("/");
70 paths.push_back("/foo");
71 paths.push_back("foo/");
72 paths.push_back("/foo/");
73 paths.push_back("foo/bar");
74 paths.push_back("/foo/bar");
75 paths.push_back("//net");
76 paths.push_back("//net/foo");
77 paths.push_back("///foo///");
78 paths.push_back("///foo///bar");
79 paths.push_back("/.");
80 paths.push_back("./");
81 paths.push_back("/..");
82 paths.push_back("../");
83 paths.push_back("foo/.");
84 paths.push_back("foo/..");
85 paths.push_back("foo/./");
86 paths.push_back("foo/./bar");
87 paths.push_back("foo/..");
88 paths.push_back("foo/../");
89 paths.push_back("foo/../bar");
90 paths.push_back("c:");
91 paths.push_back("c:/");
92 paths.push_back("c:foo");
93 paths.push_back("c:/foo");
94 paths.push_back("c:foo/");
95 paths.push_back("c:/foo/");
96 paths.push_back("c:/foo/bar");
97 paths.push_back("prn:");
98 paths.push_back("c:\\");
99 paths.push_back("c:foo");
100 paths.push_back("c:\\foo");
101 paths.push_back("c:foo\\");
102 paths.push_back("c:\\foo\\");
103 paths.push_back("c:\\foo/");
104 paths.push_back("c:/foo\\bar");
105
Justin Bogner0c274ae2014-07-16 08:18:58 +0000106 SmallVector<StringRef, 5> ComponentStack;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000107 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
108 e = paths.end();
109 i != e;
110 ++i) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000111 for (sys::path::const_iterator ci = sys::path::begin(*i),
112 ce = sys::path::end(*i);
113 ci != ce;
114 ++ci) {
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000115 ASSERT_FALSE(ci->empty());
Justin Bogner0c274ae2014-07-16 08:18:58 +0000116 ComponentStack.push_back(*ci);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000117 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000118
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000119 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
120 ce = sys::path::rend(*i);
121 ci != ce;
122 ++ci) {
Justin Bogner0c274ae2014-07-16 08:18:58 +0000123 ASSERT_TRUE(*ci == ComponentStack.back());
124 ComponentStack.pop_back();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000125 }
Justin Bogner0c274ae2014-07-16 08:18:58 +0000126 ASSERT_TRUE(ComponentStack.empty());
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000127
Justin Bogner0db71d92016-10-16 22:09:24 +0000128 // Crash test most of the API - since we're iterating over all of our paths
129 // here there isn't really anything reasonable to assert on in the results.
130 (void)path::has_root_path(*i);
131 (void)path::root_path(*i);
132 (void)path::has_root_name(*i);
133 (void)path::root_name(*i);
134 (void)path::has_root_directory(*i);
135 (void)path::root_directory(*i);
136 (void)path::has_parent_path(*i);
137 (void)path::parent_path(*i);
138 (void)path::has_filename(*i);
139 (void)path::filename(*i);
140 (void)path::has_stem(*i);
141 (void)path::stem(*i);
142 (void)path::has_extension(*i);
143 (void)path::extension(*i);
144 (void)path::is_absolute(*i);
145 (void)path::is_relative(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000146
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000147 SmallString<128> temp_store;
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000148 temp_store = *i;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000149 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000150 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000151 path::remove_filename(temp_store);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000152
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000153 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000154 path::replace_extension(temp_store, "ext");
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000155 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000156 stem = path::stem(filename);
157 ext = path::extension(filename);
Justin Bogner487e7642014-08-04 17:36:41 +0000158 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000159
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000160 path::native(*i, temp_store);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000161 }
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000162
163 SmallString<32> Relative("foo.cpp");
164 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
Benjamin Kramer2b4e14e2015-10-05 14:15:13 +0000165 Relative[5] = '/'; // Fix up windows paths.
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000166 ASSERT_EQ("/root/foo.cpp", Relative);
Michael J. Spencer346a1332011-01-05 16:39:05 +0000167}
Michael J. Spencer45710402010-12-03 01:21:28 +0000168
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000169TEST(Support, RelativePathIterator) {
170 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
171 typedef SmallVector<StringRef, 4> PathComponents;
172 PathComponents ExpectedPathComponents;
173 PathComponents ActualPathComponents;
174
Chandler Carruthe4405e92015-09-10 06:12:31 +0000175 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000176
177 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
178 ++I) {
179 ActualPathComponents.push_back(*I);
180 }
181
182 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
183
184 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
185 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
186 }
187}
188
Ben Langmuir2547f932015-03-10 00:04:29 +0000189TEST(Support, RelativePathDotIterator) {
190 SmallString<64> Path(StringRef(".c/.d/../."));
191 typedef SmallVector<StringRef, 4> PathComponents;
192 PathComponents ExpectedPathComponents;
193 PathComponents ActualPathComponents;
194
Chandler Carruthe4405e92015-09-10 06:12:31 +0000195 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000196
197 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
198 ++I) {
199 ActualPathComponents.push_back(*I);
200 }
201
202 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
203
204 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
205 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
206 }
207}
208
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000209TEST(Support, AbsolutePathIterator) {
210 SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
211 typedef SmallVector<StringRef, 4> PathComponents;
212 PathComponents ExpectedPathComponents;
213 PathComponents ActualPathComponents;
214
Chandler Carruthe4405e92015-09-10 06:12:31 +0000215 StringRef(Path).split(ExpectedPathComponents, '/');
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000216
217 // The root path will also be a component when iterating
218 ExpectedPathComponents[0] = "/";
219
220 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
221 ++I) {
222 ActualPathComponents.push_back(*I);
223 }
224
225 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
226
227 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
228 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
229 }
230}
231
Ben Langmuir2547f932015-03-10 00:04:29 +0000232TEST(Support, AbsolutePathDotIterator) {
233 SmallString<64> Path(StringRef("/.c/.d/../."));
234 typedef SmallVector<StringRef, 4> PathComponents;
235 PathComponents ExpectedPathComponents;
236 PathComponents ActualPathComponents;
237
Chandler Carruthe4405e92015-09-10 06:12:31 +0000238 StringRef(Path).split(ExpectedPathComponents, '/');
Ben Langmuir2547f932015-03-10 00:04:29 +0000239
240 // The root path will also be a component when iterating
241 ExpectedPathComponents[0] = "/";
242
243 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
244 ++I) {
245 ActualPathComponents.push_back(*I);
246 }
247
248 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
249
250 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
251 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
252 }
253}
254
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000255#ifdef LLVM_ON_WIN32
256TEST(Support, AbsolutePathIteratorWin32) {
257 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
258 typedef SmallVector<StringRef, 4> PathComponents;
259 PathComponents ExpectedPathComponents;
260 PathComponents ActualPathComponents;
261
262 StringRef(Path).split(ExpectedPathComponents, "\\");
263
264 // The root path (which comes after the drive name) will also be a component
265 // when iterating.
266 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
267
268 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
269 ++I) {
270 ActualPathComponents.push_back(*I);
271 }
272
273 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
274
275 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
276 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
277 }
278}
279#endif // LLVM_ON_WIN32
280
Ben Langmuir8d116392014-03-05 19:56:30 +0000281TEST(Support, AbsolutePathIteratorEnd) {
282 // Trailing slashes are converted to '.' unless they are part of the root path.
283 SmallVector<StringRef, 4> Paths;
284 Paths.push_back("/foo/");
285 Paths.push_back("/foo//");
286 Paths.push_back("//net//");
287#ifdef LLVM_ON_WIN32
288 Paths.push_back("c:\\\\");
289#endif
290
291 for (StringRef Path : Paths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000292 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000293 EXPECT_EQ(".", LastComponent);
294 }
295
296 SmallVector<StringRef, 3> RootPaths;
297 RootPaths.push_back("/");
298 RootPaths.push_back("//net/");
299#ifdef LLVM_ON_WIN32
300 RootPaths.push_back("c:\\");
301#endif
302
303 for (StringRef Path : RootPaths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000304 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000305 EXPECT_EQ(1u, LastComponent.size());
306 EXPECT_TRUE(path::is_separator(LastComponent[0]));
307 }
308}
309
Peter Collingbournef7d41012014-01-31 23:46:06 +0000310TEST(Support, HomeDirectory) {
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000311 std::string expected;
Pawel Bylica7187e4b2015-10-16 09:08:59 +0000312#ifdef LLVM_ON_WIN32
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000313 if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) {
314 auto pathLen = ::wcslen(path);
315 ArrayRef<char> ref{reinterpret_cast<char const *>(path),
316 pathLen * sizeof(wchar_t)};
317 convertUTF16ToUTF8String(ref, expected);
318 }
Pawel Bylica7187e4b2015-10-16 09:08:59 +0000319#else
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000320 if (char const *path = ::getenv("HOME"))
321 expected = path;
Peter Collingbournef7d41012014-01-31 23:46:06 +0000322#endif
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000323 // Do not try to test it if we don't know what to expect.
324 // On Windows we use something better than env vars.
325 if (!expected.empty()) {
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000326 SmallString<128> HomeDir;
327 auto status = path::home_directory(HomeDir);
Pawel Bylica2fa025c2015-10-16 10:11:07 +0000328 EXPECT_TRUE(status);
NAKAMURA Takumicc275e42015-10-16 09:40:01 +0000329 EXPECT_EQ(expected, HomeDir);
330 }
Peter Collingbournef7d41012014-01-31 23:46:06 +0000331}
332
Pawel Bylica0e97e5c2015-11-02 09:49:17 +0000333TEST(Support, UserCacheDirectory) {
334 SmallString<13> CacheDir;
335 SmallString<20> CacheDir2;
336 auto Status = path::user_cache_directory(CacheDir, "");
337 EXPECT_TRUE(Status ^ CacheDir.empty());
338
339 if (Status) {
340 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed
341 EXPECT_EQ(CacheDir, CacheDir2); // and return same paths
342
343 EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c"));
344 auto It = path::rbegin(CacheDir);
345 EXPECT_EQ("file.c", *It);
346 EXPECT_EQ("B", *++It);
347 EXPECT_EQ("A", *++It);
348 auto ParentDir = *++It;
349
350 // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0"
351 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2",
352 "\xE2\x84\xB5.0"));
353 auto It2 = path::rbegin(CacheDir2);
354 EXPECT_EQ("\xE2\x84\xB5.0", *It2);
355 EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2);
356 auto ParentDir2 = *++It2;
357
358 EXPECT_EQ(ParentDir, ParentDir2);
359 }
360}
361
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000362TEST(Support, TempDirectory) {
363 SmallString<32> TempDir;
364 path::system_temp_directory(false, TempDir);
365 EXPECT_TRUE(!TempDir.empty());
366 TempDir.clear();
367 path::system_temp_directory(true, TempDir);
368 EXPECT_TRUE(!TempDir.empty());
369}
370
David Blaikie06d56182015-11-17 20:38:54 +0000371#ifdef LLVM_ON_WIN32
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000372static std::string path2regex(std::string Path) {
373 size_t Pos = 0;
374 while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
375 Path.replace(Pos, 1, "\\\\");
376 Pos += 2;
377 }
378 return Path;
379}
380
381/// Helper for running temp dir test in separated process. See below.
382#define EXPECT_TEMP_DIR(prepare, expected) \
383 EXPECT_EXIT( \
384 { \
385 prepare; \
386 SmallString<300> TempDir; \
387 path::system_temp_directory(true, TempDir); \
388 raw_os_ostream(std::cerr) << TempDir; \
389 std::exit(0); \
390 }, \
391 ::testing::ExitedWithCode(0), path2regex(expected))
392
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000393TEST(SupportDeathTest, TempDirectoryOnWindows) {
394 // In this test we want to check how system_temp_directory responds to
395 // different values of specific env vars. To prevent corrupting env vars of
396 // the current process all checks are done in separated processes.
397 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder");
398 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"),
399 "C:\\Unix\\Path\\Seperators");
400 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$");
401 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep");
402 EXPECT_TEMP_DIR(
403 _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
404 "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
405
406 // Test $TMP empty, $TEMP set.
407 EXPECT_TEMP_DIR(
408 {
409 _wputenv_s(L"TMP", L"");
410 _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
411 },
412 "C:\\Valid\\Path");
413
414 // All related env vars empty
415 EXPECT_TEMP_DIR(
416 {
417 _wputenv_s(L"TMP", L"");
418 _wputenv_s(L"TEMP", L"");
419 _wputenv_s(L"USERPROFILE", L"");
420 },
421 "C:\\Temp");
422
423 // Test evn var / path with 260 chars.
424 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
425 while (Expected.size() < 260)
426 Expected.append("\\DirNameWith19Charss");
Reid Kleckner88e9ff62016-02-10 19:29:01 +0000427 ASSERT_EQ(260U, Expected.size());
Pawel Bylicaa90e7452015-11-17 16:54:32 +0000428 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
429}
430#endif
431
Michael J. Spencer346a1332011-01-05 16:39:05 +0000432class FileSystemTest : public testing::Test {
433protected:
434 /// Unique temporary directory in which all created filesystem entities must
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000435 /// be placed. It is removed at the end of each test (must be empty).
Michael J. Spencer346a1332011-01-05 16:39:05 +0000436 SmallString<128> TestDirectory;
437
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000438 void SetUp() override {
Michael J. Spencer346a1332011-01-05 16:39:05 +0000439 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000440 fs::createUniqueDirectory("file-system-test", TestDirectory));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000441 // We don't care about this specific file.
Michael J. Spencer42368cc2011-01-05 16:39:46 +0000442 errs() << "Test Directory: " << TestDirectory << '\n';
443 errs().flush();
Michael J. Spencer346a1332011-01-05 16:39:05 +0000444 }
445
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000446 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000447};
448
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000449TEST_F(FileSystemTest, Unique) {
450 // Create a temp file.
451 int FileDescriptor;
452 SmallString<64> TempPath;
453 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000454 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000455
456 // The same file should return an identical unique id.
Rafael Espindola7f822a92013-07-29 21:26:49 +0000457 fs::UniqueID F1, F2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000458 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
459 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000460 ASSERT_EQ(F1, F2);
461
462 // Different files should return different unique ids.
463 int FileDescriptor2;
464 SmallString<64> TempPath2;
465 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000466 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
467
Rafael Espindola7f822a92013-07-29 21:26:49 +0000468 fs::UniqueID D;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000469 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000470 ASSERT_NE(D, F1);
471 ::close(FileDescriptor2);
472
473 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
474
475 // Two paths representing the same file on disk should still provide the
476 // same unique id. We can test this by making a hard link.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000477 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Rafael Espindola7f822a92013-07-29 21:26:49 +0000478 fs::UniqueID D2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000479 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000480 ASSERT_EQ(D2, F1);
481
482 ::close(FileDescriptor);
Rafael Espindolaa5932af2013-07-30 20:25:53 +0000483
484 SmallString<128> Dir1;
485 ASSERT_NO_ERROR(
486 fs::createUniqueDirectory("dir1", Dir1));
487 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
488 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
489 ASSERT_EQ(F1, F2);
490
491 SmallString<128> Dir2;
492 ASSERT_NO_ERROR(
493 fs::createUniqueDirectory("dir2", Dir2));
494 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
495 ASSERT_NE(F1, F2);
Reid Kleckner1a4398a2016-09-02 01:10:53 +0000496 ASSERT_NO_ERROR(fs::remove(Dir1));
497 ASSERT_NO_ERROR(fs::remove(Dir2));
Reid Kleckner75e557f2016-09-02 00:51:34 +0000498 ASSERT_NO_ERROR(fs::remove(TempPath2));
499 ASSERT_NO_ERROR(fs::remove(TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000500}
501
Zachary Turnere48ace62017-03-10 17:39:21 +0000502TEST_F(FileSystemTest, RealPath) {
503 ASSERT_NO_ERROR(
504 fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3"));
505 ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3"));
506
507 SmallString<64> RealBase;
508 SmallString<64> Expected;
509 SmallString<64> Actual;
510
511 // TestDirectory itself might be under a symlink or have been specified with
512 // a different case than the existing temp directory. In such cases real_path
513 // on the concatenated path will differ in the TestDirectory portion from
514 // how we specified it. Make sure to compare against the real_path of the
515 // TestDirectory, and not just the value of TestDirectory.
516 ASSERT_NO_ERROR(fs::real_path(TestDirectory, RealBase));
517 path::native(Twine(RealBase) + "/test1/test2", Expected);
518
519 ASSERT_NO_ERROR(fs::real_path(
520 Twine(TestDirectory) + "/././test1/../test1/test2/./test3/..", Actual));
521
522 EXPECT_EQ(Expected, Actual);
523
524 SmallString<64> HomeDir;
Zachary Turneraeab48a2017-03-10 17:47:13 +0000525 bool Result = llvm::sys::path::home_directory(HomeDir);
526 if (Result) {
527 ASSERT_NO_ERROR(fs::real_path(HomeDir, Expected));
528 ASSERT_NO_ERROR(fs::real_path("~", Actual, true));
529 EXPECT_EQ(Expected, Actual);
530 ASSERT_NO_ERROR(fs::real_path("~/", Actual, true));
531 EXPECT_EQ(Expected, Actual);
532 }
Zachary Turnere48ace62017-03-10 17:39:21 +0000533
534 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
535}
536
Michael J. Spencer346a1332011-01-05 16:39:05 +0000537TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000538 // Create a temp file.
Michael J. Spencer45710402010-12-03 01:21:28 +0000539 int FileDescriptor;
540 SmallString<64> TempPath;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000541 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000542 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Michael J. Spencer45710402010-12-03 01:21:28 +0000543
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000544 // Make sure it exists.
Rafael Espindolac1599152014-09-11 19:11:02 +0000545 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000546
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000547 // Create another temp tile.
548 int FD2;
549 SmallString<64> TempPath2;
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000550 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
Rafael Espindolad3c89042013-07-25 15:00:17 +0000551 ASSERT_TRUE(TempPath2.endswith(".temp"));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000552 ASSERT_NE(TempPath.str(), TempPath2.str());
553
Michael J. Spencer203d7802011-12-12 06:04:28 +0000554 fs::file_status A, B;
555 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
556 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
557 EXPECT_FALSE(fs::equivalent(A, B));
558
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000559 ::close(FD2);
Rafael Espindola213c4cb2013-07-18 03:29:51 +0000560
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000561 // Remove Temp2.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000562 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
563 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
564 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000565 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000566
Rafael Espindolac049c652014-06-13 03:20:08 +0000567 std::error_code EC = fs::status(TempPath2.c_str(), B);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000568 EXPECT_EQ(EC, errc::no_such_file_or_directory);
Rafael Espindola107b74c2013-07-31 00:10:25 +0000569 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
570
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000571 // Make sure Temp2 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000572 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
573 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000574
Rafael Espindolad3c89042013-07-25 15:00:17 +0000575 SmallString<64> TempPath3;
576 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
577 ASSERT_FALSE(TempPath3.endswith("."));
Reid Kleckner75e557f2016-09-02 00:51:34 +0000578 FileRemover Cleanup3(TempPath3);
Rafael Espindolad3c89042013-07-25 15:00:17 +0000579
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000580 // Create a hard link to Temp1.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000581 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000582 bool equal;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000583 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000584 EXPECT_TRUE(equal);
Michael J. Spencer203d7802011-12-12 06:04:28 +0000585 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
586 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
587 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000588
589 // Remove Temp1.
Michael J. Spencer45710402010-12-03 01:21:28 +0000590 ::close(FileDescriptor);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000591 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000592
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000593 // Remove the hard link.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000594 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000595
596 // Make sure Temp1 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000597 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
598 errc::no_such_file_or_directory);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000599
600#ifdef LLVM_ON_WIN32
601 // Path name > 260 chars should get an error.
602 const char *Path270 =
603 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
604 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
605 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
606 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
607 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000608 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000609 errc::invalid_argument);
610 // Relative path < 247 chars, no problem.
611 const char *Path216 =
612 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
613 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
614 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
615 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000616 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000617 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000618#endif
Michael J. Spencer346a1332011-01-05 16:39:05 +0000619}
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000620
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000621TEST_F(FileSystemTest, CreateDir) {
622 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
623 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
624 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000625 errc::file_exists);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000626 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000627
Frederic Riss6b9396c2015-08-06 21:04:55 +0000628#ifdef LLVM_ON_UNIX
629 // Set a 0000 umask so that we can test our directory permissions.
630 mode_t OldUmask = ::umask(0000);
631
632 fs::file_status Status;
633 ASSERT_NO_ERROR(
634 fs::create_directory(Twine(TestDirectory) + "baz500", false,
635 fs::perms::owner_read | fs::perms::owner_exe));
636 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
637 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
638 fs::perms::owner_read | fs::perms::owner_exe);
639 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
640 fs::perms::all_all));
641 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
642 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
643
644 // Restore umask to be safe.
645 ::umask(OldUmask);
646#endif
647
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000648#ifdef LLVM_ON_WIN32
649 // Prove that create_directories() can handle a pathname > 248 characters,
650 // which is the documented limit for CreateDirectory().
651 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
652 // Generate a directory path guaranteed to fall into that range.
653 size_t TmpLen = TestDirectory.size();
654 const char *OneDir = "\\123456789";
655 size_t OneDirLen = strlen(OneDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000656 ASSERT_LT(OneDirLen, 12U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000657 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
658 SmallString<260> LongDir(TestDirectory);
659 for (size_t I = 0; I < NLevels; ++I)
660 LongDir.append(OneDir);
661 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
662 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
663 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
664 errc::file_exists);
665 // Tidy up, "recursively" removing the directories.
666 StringRef ThisDir(LongDir);
667 for (size_t J = 0; J < NLevels; ++J) {
668 ASSERT_NO_ERROR(fs::remove(ThisDir));
669 ThisDir = path::parent_path(ThisDir);
670 }
671
672 // Similarly for a relative pathname. Need to set the current directory to
673 // TestDirectory so that the one we create ends up in the right place.
674 char PreviousDir[260];
675 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000676 ASSERT_GT(PreviousDirLen, 0U);
677 ASSERT_LT(PreviousDirLen, 260U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000678 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
679 LongDir.clear();
680 // Generate a relative directory name with absolute length > 248.
681 size_t LongDirLen = 249 - TestDirectory.size();
682 LongDir.assign(LongDirLen, 'a');
683 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
684 // While we're here, prove that .. and . handling works in these long paths.
685 const char *DotDotDirs = "\\..\\.\\b";
686 LongDir.append(DotDotDirs);
Paul Robinson1b6c7342014-11-13 00:36:34 +0000687 ASSERT_NO_ERROR(fs::create_directory("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000688 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
689 // And clean up.
Paul Robinson1b6c7342014-11-13 00:36:34 +0000690 ASSERT_NO_ERROR(fs::remove("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000691 ASSERT_NO_ERROR(fs::remove(
692 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
693 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
694#endif
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000695}
696
Michael J. Spencer346a1332011-01-05 16:39:05 +0000697TEST_F(FileSystemTest, DirectoryIteration) {
Rafael Espindolac049c652014-06-13 03:20:08 +0000698 std::error_code ec;
Michael J. Spencer1f063602011-01-06 05:57:54 +0000699 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
700 ASSERT_NO_ERROR(ec);
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000701
702 // Create a known hierarchy to recurse over.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000703 ASSERT_NO_ERROR(
704 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
705 ASSERT_NO_ERROR(
706 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
707 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
708 "/recursive/dontlookhere/da1"));
709 ASSERT_NO_ERROR(
710 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
711 ASSERT_NO_ERROR(
712 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000713 typedef std::vector<std::string> v_t;
714 v_t visited;
715 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
716 + "/recursive", ec), e; i != e; i.increment(ec)){
717 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000718 if (path::filename(i->path()) == "p1") {
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000719 i.pop();
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000720 // FIXME: recursive_directory_iterator should be more robust.
721 if (i == e) break;
722 }
Michael J. Spencer9b54a3e2011-12-09 01:14:41 +0000723 if (path::filename(i->path()) == "dontlookhere")
724 i.no_push();
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000725 visited.push_back(path::filename(i->path()));
726 }
David Majnemer0d955d02016-08-11 22:21:41 +0000727 v_t::const_iterator a0 = find(visited, "a0");
728 v_t::const_iterator aa1 = find(visited, "aa1");
729 v_t::const_iterator ab1 = find(visited, "ab1");
730 v_t::const_iterator dontlookhere = find(visited, "dontlookhere");
731 v_t::const_iterator da1 = find(visited, "da1");
732 v_t::const_iterator z0 = find(visited, "z0");
733 v_t::const_iterator za1 = find(visited, "za1");
734 v_t::const_iterator pop = find(visited, "pop");
735 v_t::const_iterator p1 = find(visited, "p1");
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000736
737 // Make sure that each path was visited correctly.
738 ASSERT_NE(a0, visited.end());
739 ASSERT_NE(aa1, visited.end());
740 ASSERT_NE(ab1, visited.end());
741 ASSERT_NE(dontlookhere, visited.end());
742 ASSERT_EQ(da1, visited.end()); // Not visited.
743 ASSERT_NE(z0, visited.end());
744 ASSERT_NE(za1, visited.end());
745 ASSERT_NE(pop, visited.end());
746 ASSERT_EQ(p1, visited.end()); // Not visited.
747
748 // Make sure that parents were visited before children. No other ordering
749 // guarantees can be made across siblings.
750 ASSERT_LT(a0, aa1);
751 ASSERT_LT(a0, ab1);
752 ASSERT_LT(z0, za1);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000753
754 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
755 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
756 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
757 ASSERT_NO_ERROR(
758 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
759 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
760 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
761 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
762 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
763 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
764 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
Bruno Cardoso Lopesead771c2016-05-13 21:31:32 +0000765
766 // Test recursive_directory_iterator level()
767 ASSERT_NO_ERROR(
768 fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c"));
769 fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E;
770 for (int l = 0; I != E; I.increment(ec), ++l) {
771 ASSERT_NO_ERROR(ec);
772 EXPECT_EQ(I.level(), l);
773 }
774 EXPECT_EQ(I, E);
775 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c"));
776 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b"));
777 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a"));
778 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel"));
Michael J. Spencer1f063602011-01-06 05:57:54 +0000779}
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000780
Juergen Ributzkae7d0fa682017-03-13 21:34:07 +0000781#ifdef LLVM_ON_UNIX
782TEST_F(FileSystemTest, BrokenSymlinkDirectoryIteration) {
783 // Create a known hierarchy to recurse over.
784 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + "/symlink"));
785 ASSERT_NO_ERROR(
786 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/a"));
787 ASSERT_NO_ERROR(
788 fs::create_directories(Twine(TestDirectory) + "/symlink/b/bb"));
789 ASSERT_NO_ERROR(
790 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/ba"));
791 ASSERT_NO_ERROR(
792 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/bc"));
793 ASSERT_NO_ERROR(
794 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/c"));
795 ASSERT_NO_ERROR(
796 fs::create_directories(Twine(TestDirectory) + "/symlink/d/dd/ddd"));
797 ASSERT_NO_ERROR(fs::create_link(Twine(TestDirectory) + "/symlink/d/dd",
798 Twine(TestDirectory) + "/symlink/d/da"));
799 ASSERT_NO_ERROR(
800 fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/e"));
801
802 typedef std::vector<std::string> v_t;
803 v_t visited;
804
805 // The directory iterator doesn't stat the file, so we should be able to
806 // iterate over the whole directory.
807 std::error_code ec;
808 for (fs::directory_iterator i(Twine(TestDirectory) + "/symlink", ec), e;
809 i != e; i.increment(ec)) {
810 ASSERT_NO_ERROR(ec);
811 visited.push_back(path::filename(i->path()));
812 }
813 std::sort(visited.begin(), visited.end());
814 v_t expected = {"a", "b", "c", "d", "e"};
815 ASSERT_TRUE(visited.size() == expected.size());
816 ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin()));
817 visited.clear();
818
819 // The recursive directory iterator has to stat the file, so we need to skip
820 // the broken symlinks.
821 for (fs::recursive_directory_iterator
822 i(Twine(TestDirectory) + "/symlink", ec),
823 e;
824 i != e; i.increment(ec)) {
825 ASSERT_NO_ERROR(ec);
826
827 fs::file_status status;
Juergen Ributzka118bb452017-03-14 18:37:44 +0000828 if (i->status(status) ==
829 std::make_error_code(std::errc::no_such_file_or_directory)) {
Juergen Ributzkae7d0fa682017-03-13 21:34:07 +0000830 i.no_push();
831 continue;
832 }
833
834 visited.push_back(path::filename(i->path()));
835 }
836 std::sort(visited.begin(), visited.end());
837 expected = {"b", "bb", "d", "da", "dd", "ddd", "ddd"};
838 ASSERT_TRUE(visited.size() == expected.size());
839 ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin()));
840 visited.clear();
841
842 // This recursive directory iterator doesn't follow symlinks, so we don't need
843 // to skip them.
844 for (fs::recursive_directory_iterator
845 i(Twine(TestDirectory) + "/symlink", ec, /*follow_symlinks=*/false),
846 e;
847 i != e; i.increment(ec)) {
848 ASSERT_NO_ERROR(ec);
849 visited.push_back(path::filename(i->path()));
850 }
Juergen Ributzkac0d61602017-03-13 21:40:20 +0000851 std::sort(visited.begin(), visited.end());
Juergen Ributzkae7d0fa682017-03-13 21:34:07 +0000852 expected = {"a", "b", "ba", "bb", "bc", "c", "d", "da", "dd", "ddd", "e"};
853 ASSERT_TRUE(visited.size() == expected.size());
854 ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin()));
855
856 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/symlink"));
857}
858#endif
859
Zachary Turner260bda32017-03-08 22:49:32 +0000860TEST_F(FileSystemTest, Remove) {
861 SmallString<64> BaseDir;
862 SmallString<64> Paths[4];
863 int fds[4];
864 ASSERT_NO_ERROR(fs::createUniqueDirectory("fs_remove", BaseDir));
865
866 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/baz"));
867 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/buzz"));
868 ASSERT_NO_ERROR(fs::createUniqueFile(
869 Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[0], Paths[0]));
870 ASSERT_NO_ERROR(fs::createUniqueFile(
871 Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[1], Paths[1]));
872 ASSERT_NO_ERROR(fs::createUniqueFile(
873 Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[2], Paths[2]));
874 ASSERT_NO_ERROR(fs::createUniqueFile(
875 Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[3], Paths[3]));
876
877 for (int fd : fds)
878 ::close(fd);
879
880 EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/baz"));
881 EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/buzz"));
882 EXPECT_TRUE(fs::exists(Paths[0]));
883 EXPECT_TRUE(fs::exists(Paths[1]));
884 EXPECT_TRUE(fs::exists(Paths[2]));
885 EXPECT_TRUE(fs::exists(Paths[3]));
886
887 ASSERT_NO_ERROR(fs::remove_directories("D:/footest"));
888
889 ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
890 ASSERT_FALSE(fs::exists(BaseDir));
891}
892
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000893const char archive[] = "!<arch>\x0A";
894const char bitcode[] = "\xde\xc0\x17\x0b";
Rui Ueyama829c4392013-11-14 22:09:08 +0000895const char coff_object[] = "\x00\x00......";
Rui Ueyama5c69ff52014-09-11 22:34:32 +0000896const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
Rui Ueyama2acb0582014-09-11 21:09:57 +0000897 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000898const char coff_import_library[] = "\x00\x00\xff\xff....";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000899const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
900 0, 0, 0, 0, 0, 0, 0, 0, 1 };
Etienne Bergeron1562f692016-04-05 01:46:26 +0000901const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
Kevin Enderby87c85b72016-01-26 23:43:37 +0000902const char macho_object[] =
903 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
904const char macho_executable[] =
905 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000906const char macho_fixed_virtual_memory_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000907 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
908const char macho_core[] =
909 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
910const char macho_preload_executable[] =
911 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000912const char macho_dynamically_linked_shared_lib[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000913 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
914const char macho_dynamic_linker[] =
915 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
916const char macho_bundle[] =
917 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
918const char macho_dsym_companion[] =
919 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
920const char macho_kext_bundle[] =
921 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000922const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000923const char macho_dynamically_linked_shared_lib_stub[] =
Kevin Enderby87c85b72016-01-26 23:43:37 +0000924 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000925
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000926TEST_F(FileSystemTest, Magic) {
927 struct type {
928 const char *filename;
929 const char *magic_str;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000930 size_t magic_str_len;
931 fs::file_magic magic;
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000932 } types[] = {
933#define DEFINE(magic) \
934 { #magic, magic, sizeof(magic), fs::file_magic::magic }
935 DEFINE(archive),
936 DEFINE(bitcode),
Rui Ueyama829c4392013-11-14 22:09:08 +0000937 DEFINE(coff_object),
Rui Ueyama2acb0582014-09-11 21:09:57 +0000938 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000939 DEFINE(coff_import_library),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000940 DEFINE(elf_relocatable),
941 DEFINE(macho_universal_binary),
942 DEFINE(macho_object),
943 DEFINE(macho_executable),
944 DEFINE(macho_fixed_virtual_memory_shared_lib),
945 DEFINE(macho_core),
946 DEFINE(macho_preload_executable),
947 DEFINE(macho_dynamically_linked_shared_lib),
948 DEFINE(macho_dynamic_linker),
949 DEFINE(macho_bundle),
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000950 DEFINE(macho_dynamically_linked_shared_lib_stub),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000951 DEFINE(macho_dsym_companion),
Justin Bognera7ad4b32015-02-25 22:59:20 +0000952 DEFINE(macho_kext_bundle),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000953 DEFINE(windows_resource)
954#undef DEFINE
955 };
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000956
957 // Create some files filled with magic.
958 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
959 ++i) {
960 SmallString<128> file_pathname(TestDirectory);
961 path::append(file_pathname, i->filename);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000962 std::error_code EC;
963 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000964 ASSERT_FALSE(file.has_error());
965 StringRef magic(i->magic_str, i->magic_str_len);
966 file << magic;
Michael J. Spencerb5871802011-01-15 18:52:49 +0000967 file.close();
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000968 EXPECT_EQ(i->magic, fs::identify_magic(magic));
Rafael Espindola78dcc032014-01-10 20:36:42 +0000969 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000970 }
Michael J. Spencer98847782010-11-24 19:20:05 +0000971}
972
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000973#ifdef LLVM_ON_WIN32
974TEST_F(FileSystemTest, CarriageReturn) {
975 SmallString<128> FilePathname(TestDirectory);
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000976 std::error_code EC;
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000977 path::append(FilePathname, "test");
978
979 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000980 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
981 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000982 File << '\n';
983 }
984 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000985 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000986 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000987 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000988 }
989
990 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000991 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
992 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000993 File << '\n';
994 }
995 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000996 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000997 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000998 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000999 }
Rafael Espindola78dcc032014-01-10 20:36:42 +00001000 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
Rafael Espindola84ab9b32013-07-19 14:41:25 +00001001}
1002#endif
1003
Rafael Espindola59aaa6c2014-12-12 17:55:12 +00001004TEST_F(FileSystemTest, Resize) {
1005 int FD;
1006 SmallString<64> TempPath;
1007 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
1008 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
1009 fs::file_status Status;
1010 ASSERT_NO_ERROR(fs::status(FD, Status));
1011 ASSERT_EQ(Status.getSize(), 123U);
Reid Kleckner75e557f2016-09-02 00:51:34 +00001012 ::close(FD);
1013 ASSERT_NO_ERROR(fs::remove(TempPath));
Rafael Espindola59aaa6c2014-12-12 17:55:12 +00001014}
1015
Nick Kledzik18497e92012-06-20 00:28:54 +00001016TEST_F(FileSystemTest, FileMapping) {
1017 // Create a temp file.
1018 int FileDescriptor;
1019 SmallString<64> TempPath;
1020 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +00001021 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Rafael Espindolac69f13b2014-12-12 18:13:23 +00001022 unsigned Size = 4096;
1023 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
1024
Nick Kledzik18497e92012-06-20 00:28:54 +00001025 // Map in temp file and add some content
Rafael Espindolac049c652014-06-13 03:20:08 +00001026 std::error_code EC;
Michael J. Spenceref2284f2012-08-15 19:05:47 +00001027 StringRef Val("hello there");
1028 {
1029 fs::mapped_file_region mfr(FileDescriptor,
Rafael Espindolac69f13b2014-12-12 18:13:23 +00001030 fs::mapped_file_region::readwrite, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +00001031 ASSERT_NO_ERROR(EC);
1032 std::copy(Val.begin(), Val.end(), mfr.data());
1033 // Explicitly add a 0.
1034 mfr.data()[Val.size()] = 0;
1035 // Unmap temp file
1036 }
Reid Kleckner75e557f2016-09-02 00:51:34 +00001037 ASSERT_EQ(close(FileDescriptor), 0);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +00001038
Nick Kledzik18497e92012-06-20 00:28:54 +00001039 // Map it back in read-only
Reid Kleckner75e557f2016-09-02 00:51:34 +00001040 {
1041 int FD;
1042 EC = fs::openFileForRead(Twine(TempPath), FD);
1043 ASSERT_NO_ERROR(EC);
1044 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
1045 ASSERT_NO_ERROR(EC);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +00001046
Reid Kleckner75e557f2016-09-02 00:51:34 +00001047 // Verify content
1048 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +00001049
Reid Kleckner75e557f2016-09-02 00:51:34 +00001050 // Unmap temp file
1051 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
1052 ASSERT_NO_ERROR(EC);
1053 ASSERT_EQ(close(FD), 0);
1054 }
1055 ASSERT_NO_ERROR(fs::remove(TempPath));
Michael J. Spenceref2284f2012-08-15 19:05:47 +00001056}
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001057
1058TEST(Support, NormalizePath) {
1059#if defined(LLVM_ON_WIN32)
1060#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
1061 EXPECT_EQ(path__, windows__);
1062#else
1063#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
1064 EXPECT_EQ(path__, not_windows__);
1065#endif
1066
1067 SmallString<64> Path1("a");
1068 SmallString<64> Path2("a/b");
1069 SmallString<64> Path3("a\\b");
1070 SmallString<64> Path4("a\\\\b");
1071 SmallString<64> Path5("\\a");
1072 SmallString<64> Path6("a\\");
1073
Rafael Espindola7e774c22014-08-08 21:35:52 +00001074 path::native(Path1);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001075 EXPECT_PATH_IS(Path1, "a", "a");
1076
Rafael Espindola7e774c22014-08-08 21:35:52 +00001077 path::native(Path2);
Rafael Espindolaffbabb72014-08-09 00:37:05 +00001078 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001079
Rafael Espindola7e774c22014-08-08 21:35:52 +00001080 path::native(Path3);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001081 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
1082
Rafael Espindola7e774c22014-08-08 21:35:52 +00001083 path::native(Path4);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001084 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
1085
Rafael Espindola7e774c22014-08-08 21:35:52 +00001086 path::native(Path5);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001087 EXPECT_PATH_IS(Path5, "\\a", "/a");
1088
Rafael Espindola7e774c22014-08-08 21:35:52 +00001089 path::native(Path6);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001090 EXPECT_PATH_IS(Path6, "a\\", "a/");
1091
1092#undef EXPECT_PATH_IS
Serge Pavlov9c761a32017-03-01 09:38:15 +00001093
1094#if defined(LLVM_ON_WIN32)
1095 SmallString<64> PathHome;
1096 path::home_directory(PathHome);
1097
1098 const char *Path7a = "~/aaa";
1099 SmallString<64> Path7(Path7a);
1100 path::native(Path7);
1101 EXPECT_TRUE(Path7.endswith("\\aaa"));
1102 EXPECT_TRUE(Path7.startswith(PathHome));
1103 EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1));
1104
1105 const char *Path8a = "~";
1106 SmallString<64> Path8(Path8a);
1107 path::native(Path8);
1108 EXPECT_EQ(Path8, PathHome);
1109
1110 const char *Path9a = "~aaa";
1111 SmallString<64> Path9(Path9a);
1112 path::native(Path9);
1113 EXPECT_EQ(Path9, "~aaa");
1114
1115 const char *Path10a = "aaa/~/b";
1116 SmallString<64> Path10(Path10a);
1117 path::native(Path10);
1118 EXPECT_EQ(Path10, "aaa\\~\\b");
1119#endif
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +00001120}
Douglas Katzmana26be4a2015-09-02 21:02:10 +00001121
1122TEST(Support, RemoveLeadingDotSlash) {
1123 StringRef Path1("././/foolz/wat");
1124 StringRef Path2("./////");
1125
1126 Path1 = path::remove_leading_dotslash(Path1);
1127 EXPECT_EQ(Path1, "foolz/wat");
1128 Path2 = path::remove_leading_dotslash(Path2);
1129 EXPECT_EQ(Path2, "");
1130}
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001131
1132static std::string remove_dots(StringRef path,
1133 bool remove_dot_dot) {
1134 SmallString<256> buffer(path);
1135 path::remove_dots(buffer, remove_dot_dot);
1136 return buffer.str();
1137}
1138
1139TEST(Support, RemoveDots) {
Mike Aizatskyf8cf7132015-11-09 19:36:53 +00001140#if defined(LLVM_ON_WIN32)
1141 EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false));
1142 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false));
1143
1144 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false));
1145 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true));
1146 EXPECT_EQ("c", remove_dots(".\\.\\c", true));
Eric Liuaf5a28f2016-10-13 15:07:14 +00001147 EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true));
1148 EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true));
Mike Aizatskyf8cf7132015-11-09 19:36:53 +00001149
1150 SmallString<64> Path1(".\\.\\c");
1151 EXPECT_TRUE(path::remove_dots(Path1, true));
1152 EXPECT_EQ("c", Path1);
1153#else
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001154 EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false));
1155 EXPECT_EQ("", remove_dots("./////", false));
1156
1157 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false));
1158 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true));
1159 EXPECT_EQ("c", remove_dots("././c", true));
Eric Liuaf5a28f2016-10-13 15:07:14 +00001160 EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true));
1161 EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true));
Benjamin Kramer937dd7a2016-10-17 13:28:21 +00001162 EXPECT_EQ("/a/c", remove_dots("/../../a/c", true));
1163 EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true));
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001164
1165 SmallString<64> Path1("././c");
1166 EXPECT_TRUE(path::remove_dots(Path1, true));
1167 EXPECT_EQ("c", Path1);
Mike Aizatskyf8cf7132015-11-09 19:36:53 +00001168#endif
Mike Aizatsky662b4fd2015-11-09 18:56:31 +00001169}
Teresa Johnsonbbd10b42016-05-17 14:45:30 +00001170
1171TEST(Support, ReplacePathPrefix) {
1172 SmallString<64> Path1("/foo");
1173 SmallString<64> Path2("/old/foo");
1174 SmallString<64> OldPrefix("/old");
1175 SmallString<64> NewPrefix("/new");
1176 SmallString<64> NewPrefix2("/longernew");
1177 SmallString<64> EmptyPrefix("");
1178
1179 SmallString<64> Path = Path1;
1180 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1181 EXPECT_EQ(Path, "/foo");
1182 Path = Path2;
1183 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1184 EXPECT_EQ(Path, "/new/foo");
1185 Path = Path2;
1186 path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
1187 EXPECT_EQ(Path, "/longernew/foo");
1188 Path = Path1;
1189 path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
1190 EXPECT_EQ(Path, "/new/foo");
1191 Path = Path2;
1192 path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
1193 EXPECT_EQ(Path, "/foo");
1194}
Taewook Ohd9153272016-06-13 15:54:56 +00001195
1196TEST_F(FileSystemTest, PathFromFD) {
1197 // Create a temp file.
1198 int FileDescriptor;
1199 SmallString<64> TempPath;
1200 ASSERT_NO_ERROR(
1201 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001202 FileRemover Cleanup(TempPath);
Taewook Ohd9153272016-06-13 15:54:56 +00001203
1204 // Make sure it exists.
1205 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1206
1207 // Try to get the path from the file descriptor
1208 SmallString<64> ResultPath;
1209 std::error_code ErrorCode =
1210 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1211
1212 // If we succeeded, check that the paths are the same (modulo case):
1213 if (!ErrorCode) {
1214 // The paths returned by createTemporaryFile and getPathFromOpenFD
1215 // should reference the same file on disk.
1216 fs::UniqueID D1, D2;
1217 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1218 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1219 ASSERT_EQ(D1, D2);
1220 }
1221
1222 ::close(FileDescriptor);
1223}
1224
Aaron Ballman3dd74b82016-06-20 20:28:49 +00001225TEST_F(FileSystemTest, PathFromFDWin32) {
1226 // Create a temp file.
1227 int FileDescriptor;
1228 SmallString<64> TempPath;
1229 ASSERT_NO_ERROR(
1230 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001231 FileRemover Cleanup(TempPath);
Aaron Ballman3dd74b82016-06-20 20:28:49 +00001232
1233 // Make sure it exists.
1234 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1235
1236 SmallVector<char, 8> ResultPath;
1237 std::error_code ErrorCode =
1238 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1239
1240 if (!ErrorCode) {
1241 // Now that we know how much space is required for the path, create a path
1242 // buffer with exactly enough space (sans null terminator, which should not
1243 // be present), and call getPathFromOpenFD again to ensure that the API
1244 // properly handles exactly-sized buffers.
1245 SmallVector<char, 8> ExactSizedPath(ResultPath.size());
1246 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath);
1247 ResultPath = ExactSizedPath;
1248 }
1249
1250 if (!ErrorCode) {
1251 fs::UniqueID D1, D2;
1252 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1253 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1254 ASSERT_EQ(D1, D2);
1255 }
1256 ::close(FileDescriptor);
1257}
1258
Aaron Ballman0ad00462016-06-21 14:24:48 +00001259TEST_F(FileSystemTest, PathFromFDUnicode) {
1260 // Create a temp file.
1261 int FileDescriptor;
1262 SmallString<64> TempPath;
1263
1264 // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0"
1265 ASSERT_NO_ERROR(
1266 fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
1267 "\xE2\x84\xB5.0", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001268 FileRemover Cleanup(TempPath);
Aaron Ballman0ad00462016-06-21 14:24:48 +00001269
1270 // Make sure it exists.
1271 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1272
1273 SmallVector<char, 8> ResultPath;
1274 std::error_code ErrorCode =
1275 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1276
1277 if (!ErrorCode) {
1278 fs::UniqueID D1, D2;
1279 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1280 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1281 ASSERT_EQ(D1, D2);
1282 }
1283 ::close(FileDescriptor);
1284}
1285
Taewook Ohd9153272016-06-13 15:54:56 +00001286TEST_F(FileSystemTest, OpenFileForRead) {
1287 // Create a temp file.
1288 int FileDescriptor;
1289 SmallString<64> TempPath;
1290 ASSERT_NO_ERROR(
1291 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Reid Kleckner75e557f2016-09-02 00:51:34 +00001292 FileRemover Cleanup(TempPath);
Taewook Ohd9153272016-06-13 15:54:56 +00001293
1294 // Make sure it exists.
1295 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1296
1297 // Open the file for read
1298 int FileDescriptor2;
1299 SmallString<64> ResultPath;
1300 ASSERT_NO_ERROR(
1301 fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath))
1302
1303 // If we succeeded, check that the paths are the same (modulo case):
1304 if (!ResultPath.empty()) {
1305 // The paths returned by createTemporaryFile and getPathFromOpenFD
1306 // should reference the same file on disk.
1307 fs::UniqueID D1, D2;
1308 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1309 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1310 ASSERT_EQ(D1, D2);
1311 }
1312
1313 ::close(FileDescriptor);
1314}
Pavel Labath2f096092017-01-24 10:32:03 +00001315
1316TEST_F(FileSystemTest, set_current_path) {
1317 SmallString<128> path;
1318
1319 ASSERT_NO_ERROR(fs::current_path(path));
1320 ASSERT_NE(TestDirectory, path);
1321
1322 struct RestorePath {
1323 SmallString<128> path;
1324 RestorePath(const SmallString<128> &path) : path(path) {}
1325 ~RestorePath() { fs::set_current_path(path); }
1326 } restore_path(path);
1327
1328 ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
1329
1330 ASSERT_NO_ERROR(fs::current_path(path));
Pavel Labath50440092017-01-24 11:35:26 +00001331
1332 fs::UniqueID D1, D2;
1333 ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1));
1334 ASSERT_NO_ERROR(fs::getUniqueID(path, D2));
Aaron Ballman345012d2017-03-13 12:24:51 +00001335 ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path;
1336}
1337
James Henderson566fdf42017-03-16 11:22:09 +00001338TEST_F(FileSystemTest, permissions) {
1339 int FD;
1340 SmallString<64> TempPath;
1341 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
1342 FileRemover Cleanup(TempPath);
1343
1344 // Make sure it exists.
1345 ASSERT_TRUE(fs::exists(Twine(TempPath)));
1346
1347 auto CheckPermissions = [&](fs::perms Expected) {
1348 ErrorOr<fs::perms> Actual = fs::getPermissions(TempPath);
1349 return Actual && *Actual == Expected;
1350 };
1351
1352 std::error_code NoError;
1353 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all), NoError);
1354 EXPECT_TRUE(CheckPermissions(fs::all_all));
1355
1356 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::all_exe), NoError);
1357 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::all_exe));
1358
1359#if defined(LLVM_ON_WIN32)
1360 fs::perms ReadOnly = fs::all_read | fs::all_exe;
1361 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
1362 EXPECT_TRUE(CheckPermissions(ReadOnly));
1363
1364 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
1365 EXPECT_TRUE(CheckPermissions(ReadOnly));
1366
1367 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
1368 EXPECT_TRUE(CheckPermissions(fs::all_all));
1369
1370 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
1371 EXPECT_TRUE(CheckPermissions(ReadOnly));
1372
1373 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
1374 EXPECT_TRUE(CheckPermissions(fs::all_all));
1375
1376 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
1377 EXPECT_TRUE(CheckPermissions(ReadOnly));
1378
1379 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
1380 EXPECT_TRUE(CheckPermissions(fs::all_all));
1381
1382 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
1383 EXPECT_TRUE(CheckPermissions(ReadOnly));
1384
1385 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
1386 EXPECT_TRUE(CheckPermissions(fs::all_all));
1387
1388 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
1389 EXPECT_TRUE(CheckPermissions(ReadOnly));
1390
1391 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
1392 EXPECT_TRUE(CheckPermissions(fs::all_all));
1393
1394 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
1395 EXPECT_TRUE(CheckPermissions(ReadOnly));
1396
1397 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
1398 EXPECT_TRUE(CheckPermissions(fs::all_all));
1399
1400 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
1401 EXPECT_TRUE(CheckPermissions(ReadOnly));
1402
1403 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
1404 EXPECT_TRUE(CheckPermissions(fs::all_all));
1405
1406 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
1407 EXPECT_TRUE(CheckPermissions(ReadOnly));
1408
1409 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
1410 EXPECT_TRUE(CheckPermissions(ReadOnly));
1411
1412 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
1413 EXPECT_TRUE(CheckPermissions(ReadOnly));
1414
1415 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
1416 EXPECT_TRUE(CheckPermissions(ReadOnly));
1417
1418 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
1419 fs::set_gid_on_exe |
1420 fs::sticky_bit),
1421 NoError);
1422 EXPECT_TRUE(CheckPermissions(ReadOnly));
1423
1424 EXPECT_EQ(fs::setPermissions(TempPath, ReadOnly | fs::set_uid_on_exe |
1425 fs::set_gid_on_exe |
1426 fs::sticky_bit),
1427 NoError);
1428 EXPECT_TRUE(CheckPermissions(ReadOnly));
1429
1430 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError);
1431 EXPECT_TRUE(CheckPermissions(fs::all_all));
1432#else
1433 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
1434 EXPECT_TRUE(CheckPermissions(fs::no_perms));
1435
1436 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
1437 EXPECT_TRUE(CheckPermissions(fs::owner_read));
1438
1439 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
1440 EXPECT_TRUE(CheckPermissions(fs::owner_write));
1441
1442 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
1443 EXPECT_TRUE(CheckPermissions(fs::owner_exe));
1444
1445 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
1446 EXPECT_TRUE(CheckPermissions(fs::owner_all));
1447
1448 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
1449 EXPECT_TRUE(CheckPermissions(fs::group_read));
1450
1451 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
1452 EXPECT_TRUE(CheckPermissions(fs::group_write));
1453
1454 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
1455 EXPECT_TRUE(CheckPermissions(fs::group_exe));
1456
1457 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
1458 EXPECT_TRUE(CheckPermissions(fs::group_all));
1459
1460 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
1461 EXPECT_TRUE(CheckPermissions(fs::others_read));
1462
1463 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
1464 EXPECT_TRUE(CheckPermissions(fs::others_write));
1465
1466 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
1467 EXPECT_TRUE(CheckPermissions(fs::others_exe));
1468
1469 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
1470 EXPECT_TRUE(CheckPermissions(fs::others_all));
1471
1472 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
1473 EXPECT_TRUE(CheckPermissions(fs::all_read));
1474
1475 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
1476 EXPECT_TRUE(CheckPermissions(fs::all_write));
1477
1478 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
1479 EXPECT_TRUE(CheckPermissions(fs::all_exe));
1480
1481 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
1482 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe));
1483
1484 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
1485 EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe));
1486
1487 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
1488 EXPECT_TRUE(CheckPermissions(fs::sticky_bit));
1489
1490 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
1491 fs::set_gid_on_exe |
1492 fs::sticky_bit),
1493 NoError);
1494 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe | fs::set_gid_on_exe |
1495 fs::sticky_bit));
1496
1497 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::set_uid_on_exe |
1498 fs::set_gid_on_exe |
1499 fs::sticky_bit),
1500 NoError);
1501 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::set_uid_on_exe |
1502 fs::set_gid_on_exe | fs::sticky_bit));
1503
1504 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError);
1505 EXPECT_TRUE(CheckPermissions(fs::all_perms));
1506#endif
1507}
1508
Aaron Ballman345012d2017-03-13 12:24:51 +00001509} // anonymous namespace