1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Path.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
18 using namespace llvm::sys;
20 #define ASSERT_NO_ERROR(x) \
21 if (error_code ASSERT_NO_ERROR_ec = x) { \
22 SmallString<128> MessageStorage; \
23 raw_svector_ostream Message(MessageStorage); \
24 Message << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
32 TEST(is_separator, Works) {
33 EXPECT_TRUE(path::is_separator('/'));
34 EXPECT_FALSE(path::is_separator('\0'));
35 EXPECT_FALSE(path::is_separator('-'));
36 EXPECT_FALSE(path::is_separator(' '));
39 EXPECT_TRUE(path::is_separator('\\'));
41 EXPECT_FALSE(path::is_separator('\\'));
46 SmallVector<StringRef, 40> paths;
49 paths.push_back("..");
50 paths.push_back("foo");
52 paths.push_back("/foo");
53 paths.push_back("foo/");
54 paths.push_back("/foo/");
55 paths.push_back("foo/bar");
56 paths.push_back("/foo/bar");
57 paths.push_back("//net");
58 paths.push_back("//net/foo");
59 paths.push_back("///foo///");
60 paths.push_back("///foo///bar");
61 paths.push_back("/.");
62 paths.push_back("./");
63 paths.push_back("/..");
64 paths.push_back("../");
65 paths.push_back("foo/.");
66 paths.push_back("foo/..");
67 paths.push_back("foo/./");
68 paths.push_back("foo/./bar");
69 paths.push_back("foo/..");
70 paths.push_back("foo/../");
71 paths.push_back("foo/../bar");
72 paths.push_back("c:");
73 paths.push_back("c:/");
74 paths.push_back("c:foo");
75 paths.push_back("c:/foo");
76 paths.push_back("c:foo/");
77 paths.push_back("c:/foo/");
78 paths.push_back("c:/foo/bar");
79 paths.push_back("prn:");
80 paths.push_back("c:\\");
81 paths.push_back("c:foo");
82 paths.push_back("c:\\foo");
83 paths.push_back("c:foo\\");
84 paths.push_back("c:\\foo\\");
85 paths.push_back("c:\\foo/");
86 paths.push_back("c:/foo\\bar");
88 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
92 for (sys::path::const_iterator ci = sys::path::begin(*i),
93 ce = sys::path::end(*i);
96 ASSERT_FALSE(ci->empty());
99 #if 0 // Valgrind is whining about this.
100 outs() << " Reverse Iteration: [";
101 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
102 ce = sys::path::rend(*i);
105 outs() << *ci << ',';
110 path::has_root_path(*i);
112 path::has_root_name(*i);
114 path::has_root_directory(*i);
115 path::root_directory(*i);
116 path::has_parent_path(*i);
117 path::parent_path(*i);
118 path::has_filename(*i);
122 path::has_extension(*i);
124 path::is_absolute(*i);
125 path::is_relative(*i);
127 SmallString<128> temp_store;
129 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
131 path::remove_filename(temp_store);
134 path::replace_extension(temp_store, "ext");
135 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
136 stem = path::stem(filename);
137 ext = path::extension(filename);
138 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
140 path::native(*i, temp_store);
144 class FileSystemTest : public testing::Test {
146 /// Unique temporary directory in which all created filesystem entities must
147 /// be placed. It is recursively removed at the end of each test.
148 SmallString<128> TestDirectory;
150 virtual void SetUp() {
152 fs::createUniqueDirectory("file-system-test", TestDirectory));
153 // We don't care about this specific file.
154 errs() << "Test Directory: " << TestDirectory << '\n';
158 virtual void TearDown() {
160 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
164 TEST_F(FileSystemTest, Unique) {
165 // Create a temp file.
167 SmallString<64> TempPath;
169 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
171 // The same file should return an identical unique id.
173 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
174 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
177 // Different files should return different unique ids.
179 SmallString<64> TempPath2;
181 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
184 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
186 ::close(FileDescriptor2);
188 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
190 // Two paths representing the same file on disk should still provide the
191 // same unique id. We can test this by making a hard link.
192 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
194 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
197 ::close(FileDescriptor);
199 SmallString<128> Dir1;
201 fs::createUniqueDirectory("dir1", Dir1));
202 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
203 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
206 SmallString<128> Dir2;
208 fs::createUniqueDirectory("dir2", Dir2));
209 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
213 TEST_F(FileSystemTest, TempFiles) {
214 // Create a temp file.
216 SmallString<64> TempPath;
218 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
220 // Make sure it exists.
222 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
223 EXPECT_TRUE(TempFileExists);
225 // Create another temp tile.
227 SmallString<64> TempPath2;
228 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
229 ASSERT_TRUE(TempPath2.endswith(".temp"));
230 ASSERT_NE(TempPath.str(), TempPath2.str());
232 fs::file_status A, B;
233 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
234 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
235 EXPECT_FALSE(fs::equivalent(A, B));
240 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
241 EXPECT_TRUE(TempFileExists);
243 error_code EC = fs::status(TempPath2.c_str(), B);
244 EXPECT_EQ(EC, errc::no_such_file_or_directory);
245 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
247 // Make sure Temp2 doesn't exist.
248 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
249 EXPECT_FALSE(TempFileExists);
251 SmallString<64> TempPath3;
252 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
253 ASSERT_FALSE(TempPath3.endswith("."));
255 // Create a hard link to Temp1.
256 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
258 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
260 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
261 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
262 EXPECT_TRUE(fs::equivalent(A, B));
265 ::close(FileDescriptor);
266 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
267 EXPECT_TRUE(TempFileExists);
269 // Remove the hard link.
270 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
271 EXPECT_TRUE(TempFileExists);
273 // Make sure Temp1 doesn't exist.
274 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
275 EXPECT_FALSE(TempFileExists);
278 // Path name > 260 chars should get an error.
279 const char *Path270 =
280 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
281 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
282 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
283 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
284 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
285 EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath),
286 windows_error::path_not_found);
290 TEST_F(FileSystemTest, DirectoryIteration) {
292 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
295 // Create a known hierarchy to recurse over.
297 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
298 + "/recursive/a0/aa1", existed));
299 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
300 + "/recursive/a0/ab1", existed));
301 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
302 + "/recursive/dontlookhere/da1", existed));
303 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
304 + "/recursive/z0/za1", existed));
305 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
306 + "/recursive/pop/p1", existed));
307 typedef std::vector<std::string> v_t;
309 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
310 + "/recursive", ec), e; i != e; i.increment(ec)){
312 if (path::filename(i->path()) == "p1") {
314 // FIXME: recursive_directory_iterator should be more robust.
317 if (path::filename(i->path()) == "dontlookhere")
319 visited.push_back(path::filename(i->path()));
321 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
322 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
323 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
324 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
326 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
327 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
328 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
329 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
330 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
332 // Make sure that each path was visited correctly.
333 ASSERT_NE(a0, visited.end());
334 ASSERT_NE(aa1, visited.end());
335 ASSERT_NE(ab1, visited.end());
336 ASSERT_NE(dontlookhere, visited.end());
337 ASSERT_EQ(da1, visited.end()); // Not visited.
338 ASSERT_NE(z0, visited.end());
339 ASSERT_NE(za1, visited.end());
340 ASSERT_NE(pop, visited.end());
341 ASSERT_EQ(p1, visited.end()); // Not visited.
343 // Make sure that parents were visited before children. No other ordering
344 // guarantees can be made across siblings.
350 const char elf[] = {0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
352 TEST_F(FileSystemTest, Magic) {
354 const char *filename;
355 const char *magic_str;
356 size_t magic_str_len;
357 fs::file_magic magic;
359 {"magic.archive", "!<arch>\x0A", 8, fs::file_magic::archive},
360 {"magic.elf", elf, sizeof(elf),
361 fs::file_magic::elf_relocatable}
364 // Create some files filled with magic.
365 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
367 SmallString<128> file_pathname(TestDirectory);
368 path::append(file_pathname, i->filename);
370 raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_Binary);
371 ASSERT_FALSE(file.has_error());
372 StringRef magic(i->magic_str, i->magic_str_len);
376 ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
378 EXPECT_EQ(i->magic, fs::identify_magic(magic));
383 TEST_F(FileSystemTest, CarriageReturn) {
384 SmallString<128> FilePathname(TestDirectory);
386 path::append(FilePathname, "test");
389 raw_fd_ostream File(FilePathname.c_str(), ErrMsg);
390 EXPECT_EQ(ErrMsg, "");
394 OwningPtr<MemoryBuffer> Buf;
395 MemoryBuffer::getFile(FilePathname, Buf);
396 EXPECT_EQ(Buf->getBuffer(), "\r\n");
400 raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Binary);
401 EXPECT_EQ(ErrMsg, "");
405 OwningPtr<MemoryBuffer> Buf;
406 MemoryBuffer::getFile(FilePathname, Buf);
407 EXPECT_EQ(Buf->getBuffer(), "\n");
412 TEST_F(FileSystemTest, FileMapping) {
413 // Create a temp file.
415 SmallString<64> TempPath;
417 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
418 // Map in temp file and add some content
420 StringRef Val("hello there");
422 fs::mapped_file_region mfr(FileDescriptor,
424 fs::mapped_file_region::readwrite,
429 std::copy(Val.begin(), Val.end(), mfr.data());
430 // Explicitly add a 0.
431 mfr.data()[Val.size()] = 0;
435 // Map it back in read-only
436 fs::mapped_file_region mfr(Twine(TempPath),
437 fs::mapped_file_region::readonly,
444 EXPECT_EQ(StringRef(mfr.const_data()), Val);
448 #if LLVM_HAS_RVALUE_REFERENCES
449 fs::mapped_file_region m(Twine(TempPath),
450 fs::mapped_file_region::readonly,
455 const char *Data = m.const_data();
456 fs::mapped_file_region mfrrv(llvm_move(m));
457 EXPECT_EQ(mfrrv.const_data(), Data);
460 } // anonymous namespace