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