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/raw_ostream.h"
14 #include "gtest/gtest.h"
17 using namespace llvm::sys;
19 #define ASSERT_NO_ERROR(x) \
20 if (error_code ASSERT_NO_ERROR_ec = x) { \
21 SmallString<128> MessageStorage; \
22 raw_svector_ostream Message(MessageStorage); \
23 Message << #x ": did not return errc::success.\n" \
24 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
25 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
26 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
31 TEST(is_separator, Works) {
32 EXPECT_TRUE(path::is_separator('/'));
33 EXPECT_FALSE(path::is_separator('\0'));
34 EXPECT_FALSE(path::is_separator('-'));
35 EXPECT_FALSE(path::is_separator(' '));
38 EXPECT_TRUE(path::is_separator('\\'));
40 EXPECT_FALSE(path::is_separator('\\'));
45 SmallVector<StringRef, 40> paths;
48 paths.push_back("..");
49 paths.push_back("foo");
51 paths.push_back("/foo");
52 paths.push_back("foo/");
53 paths.push_back("/foo/");
54 paths.push_back("foo/bar");
55 paths.push_back("/foo/bar");
56 paths.push_back("//net");
57 paths.push_back("//net/foo");
58 paths.push_back("///foo///");
59 paths.push_back("///foo///bar");
60 paths.push_back("/.");
61 paths.push_back("./");
62 paths.push_back("/..");
63 paths.push_back("../");
64 paths.push_back("foo/.");
65 paths.push_back("foo/..");
66 paths.push_back("foo/./");
67 paths.push_back("foo/./bar");
68 paths.push_back("foo/..");
69 paths.push_back("foo/../");
70 paths.push_back("foo/../bar");
71 paths.push_back("c:");
72 paths.push_back("c:/");
73 paths.push_back("c:foo");
74 paths.push_back("c:/foo");
75 paths.push_back("c:foo/");
76 paths.push_back("c:/foo/");
77 paths.push_back("c:/foo/bar");
78 paths.push_back("prn:");
79 paths.push_back("c:\\");
80 paths.push_back("c:foo");
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\\bar");
87 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
91 for (sys::path::const_iterator ci = sys::path::begin(*i),
92 ce = sys::path::end(*i);
95 ASSERT_FALSE(ci->empty());
98 #if 0 // Valgrind is whining about this.
99 outs() << " Reverse Iteration: [";
100 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
101 ce = sys::path::rend(*i);
104 outs() << *ci << ',';
109 path::has_root_path(*i);
111 path::has_root_name(*i);
113 path::has_root_directory(*i);
114 path::root_directory(*i);
115 path::has_parent_path(*i);
116 path::parent_path(*i);
117 path::has_filename(*i);
121 path::has_extension(*i);
123 path::is_absolute(*i);
124 path::is_relative(*i);
126 SmallString<128> temp_store;
128 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
130 path::remove_filename(temp_store);
133 path::replace_extension(temp_store, "ext");
134 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
135 stem = path::stem(filename);
136 ext = path::extension(filename);
137 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
139 path::native(*i, temp_store);
143 class FileSystemTest : public testing::Test {
145 /// Unique temporary directory in which all created filesystem entities must
146 /// be placed. It is recursively removed at the end of each test.
147 SmallString<128> TestDirectory;
149 virtual void SetUp() {
152 fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
154 // We don't care about this specific file.
156 TestDirectory = path::parent_path(TestDirectory);
157 errs() << "Test Directory: " << TestDirectory << '\n';
161 virtual void TearDown() {
163 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
167 TEST_F(FileSystemTest, Unique) {
168 // Create a temp file.
170 SmallString<64> TempPath;
172 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
174 // The same file should return an identical unique id.
176 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
177 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
180 // Different files should return different unique ids.
182 SmallString<64> TempPath2;
184 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor2, TempPath2));
187 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
189 ::close(FileDescriptor2);
191 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
193 // Two paths representing the same file on disk should still provide the
194 // same unique id. We can test this by making a hard link.
195 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
197 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
200 ::close(FileDescriptor);
203 TEST_F(FileSystemTest, TempFiles) {
204 // Create a temp file.
206 SmallString<64> TempPath;
208 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
210 // Make sure it exists.
212 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
213 EXPECT_TRUE(TempFileExists);
215 // Create another temp tile.
217 SmallString<64> TempPath2;
218 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
219 ASSERT_NE(TempPath.str(), TempPath2.str());
221 fs::file_status A, B;
222 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
223 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
224 EXPECT_FALSE(fs::equivalent(A, B));
226 // Try to copy the first to the second.
228 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
231 // Try again with the proper options.
232 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
233 fs::copy_option::overwrite_if_exists));
235 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
236 EXPECT_TRUE(TempFileExists);
238 // Make sure Temp2 doesn't exist.
239 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
240 EXPECT_FALSE(TempFileExists);
242 // Create a hard link to Temp1.
243 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
245 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
247 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
248 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
249 EXPECT_TRUE(fs::equivalent(A, B));
252 ::close(FileDescriptor);
253 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
254 EXPECT_TRUE(TempFileExists);
256 // Remove the hard link.
257 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
258 EXPECT_TRUE(TempFileExists);
260 // Make sure Temp1 doesn't exist.
261 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
262 EXPECT_FALSE(TempFileExists);
265 // Path name > 260 chars should get an error.
266 const char *Path270 =
267 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
268 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
269 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
270 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
271 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
272 EXPECT_EQ(fs::unique_file(Twine(Path270), FileDescriptor, TempPath),
273 windows_error::path_not_found);
277 TEST_F(FileSystemTest, DirectoryIteration) {
279 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
282 // Create a known hierarchy to recurse over.
284 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
285 + "/recursive/a0/aa1", existed));
286 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
287 + "/recursive/a0/ab1", existed));
288 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
289 + "/recursive/dontlookhere/da1", existed));
290 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
291 + "/recursive/z0/za1", existed));
292 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
293 + "/recursive/pop/p1", existed));
294 typedef std::vector<std::string> v_t;
296 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
297 + "/recursive", ec), e; i != e; i.increment(ec)){
299 if (path::filename(i->path()) == "p1") {
301 // FIXME: recursive_directory_iterator should be more robust.
304 if (path::filename(i->path()) == "dontlookhere")
306 visited.push_back(path::filename(i->path()));
308 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
309 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
310 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
311 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
313 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
314 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
315 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
316 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
317 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
319 // Make sure that each path was visited correctly.
320 ASSERT_NE(a0, visited.end());
321 ASSERT_NE(aa1, visited.end());
322 ASSERT_NE(ab1, visited.end());
323 ASSERT_NE(dontlookhere, visited.end());
324 ASSERT_EQ(da1, visited.end()); // Not visited.
325 ASSERT_NE(z0, visited.end());
326 ASSERT_NE(za1, visited.end());
327 ASSERT_NE(pop, visited.end());
328 ASSERT_EQ(p1, visited.end()); // Not visited.
330 // Make sure that parents were visited before children. No other ordering
331 // guarantees can be made across siblings.
337 const char elf[] = {0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
339 TEST_F(FileSystemTest, Magic) {
341 const char *filename;
342 const char *magic_str;
343 size_t magic_str_len;
344 fs::file_magic magic;
346 {"magic.archive", "!<arch>\x0A", 8, fs::file_magic::archive},
347 {"magic.elf", elf, sizeof(elf),
348 fs::file_magic::elf_relocatable}
351 // Create some files filled with magic.
352 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
354 SmallString<128> file_pathname(TestDirectory);
355 path::append(file_pathname, i->filename);
357 raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
358 raw_fd_ostream::F_Binary);
359 ASSERT_FALSE(file.has_error());
360 StringRef magic(i->magic_str, i->magic_str_len);
364 ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
366 EXPECT_EQ(i->magic, fs::identify_magic(magic));
370 #if !defined(_WIN32) // FIXME: Win32 has different permission schema.
371 TEST_F(FileSystemTest, Permissions) {
372 // Create a temp file.
374 SmallString<64> TempPath;
376 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
378 // Mark file as read-only
379 const fs::perms AllWrite = fs::owner_write|fs::group_write|fs::others_write;
380 ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::remove_perms|AllWrite));
382 // Verify file is read-only
383 fs::file_status Status;
384 ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
385 bool AnyWriteBits = (Status.permissions() & AllWrite);
386 EXPECT_FALSE(AnyWriteBits);
388 // Mark file as read-write
389 ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::add_perms|AllWrite));
391 // Verify file is read-write
392 ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
393 AnyWriteBits = (Status.permissions() & AllWrite);
394 EXPECT_TRUE(AnyWriteBits);
398 TEST_F(FileSystemTest, FileMapping) {
399 // Create a temp file.
401 SmallString<64> TempPath;
403 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
404 // Map in temp file and add some content
406 StringRef Val("hello there");
408 fs::mapped_file_region mfr(FileDescriptor,
410 fs::mapped_file_region::readwrite,
415 std::copy(Val.begin(), Val.end(), mfr.data());
416 // Explicitly add a 0.
417 mfr.data()[Val.size()] = 0;
421 // Map it back in read-only
422 fs::mapped_file_region mfr(Twine(TempPath),
423 fs::mapped_file_region::readonly,
430 EXPECT_EQ(StringRef(mfr.const_data()), Val);
434 #if LLVM_HAS_RVALUE_REFERENCES
435 fs::mapped_file_region m(Twine(TempPath),
436 fs::mapped_file_region::readonly,
441 const char *Data = m.const_data();
442 fs::mapped_file_region mfrrv(llvm_move(m));
443 EXPECT_EQ(mfrrv.const_data(), Data);
446 } // anonymous namespace