blob: 30ad1034200666f94a4a654cd686d7bc8b9d182b [file] [log] [blame]
Michael J. Spencer013d15a2010-11-29 22:29:04 +00001//===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000010#include "llvm/Support/FileSystem.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000011#include "llvm/Support/PathV2.h"
Michael J. Spencer753cbbb2010-12-06 04:28:42 +000012#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerf9fd0782011-01-06 05:57:54 +000013#include "llvm/Support/raw_ostream.h"
Michael J. Spencer861ef4b2010-11-24 19:20:28 +000014
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000015#include "gtest/gtest.h"
16
Michael J. Spencerdffde992010-11-29 22:28:51 +000017using namespace llvm;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000018using namespace llvm::sys;
Michael J. Spencerdffde992010-11-29 22:28:51 +000019
Michael J. Spencerba64b972011-01-04 17:00:18 +000020#define ASSERT_NO_ERROR(x) \
Michael J. Spencerf9fd0782011-01-06 05:57:54 +000021 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()); \
Michael J. Spencerba64b972011-01-04 17:00:18 +000028 } else {}
29
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000030namespace {
31
Zhanyong Wan63cc3a82011-02-11 21:24:40 +000032TEST(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(' '));
37
38#ifdef LLVM_ON_WIN32
39 EXPECT_TRUE(path::is_separator('\\'));
40#else
41 EXPECT_FALSE(path::is_separator('\\'));
42#endif
43}
44
Michael J. Spencer013d15a2010-11-29 22:29:04 +000045TEST(Support, Path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000046 SmallVector<StringRef, 40> paths;
47 paths.push_back("");
48 paths.push_back(".");
49 paths.push_back("..");
50 paths.push_back("foo");
51 paths.push_back("/");
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");
87
88 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
89 e = paths.end();
90 i != e;
91 ++i) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000092 for (sys::path::const_iterator ci = sys::path::begin(*i),
93 ce = sys::path::end(*i);
94 ci != ce;
95 ++ci) {
Michael J. Spencer371716c2010-12-03 02:22:34 +000096 ASSERT_FALSE(ci->empty());
Michael J. Spencerdffde992010-11-29 22:28:51 +000097 }
Michael J. Spencerdffde992010-11-29 22:28:51 +000098
Michael J. Spencer506e5792010-12-01 22:28:42 +000099#if 0 // Valgrind is whining about this.
Michael J. Spencera42cf732010-11-30 23:28:07 +0000100 outs() << " Reverse Iteration: [";
101 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
102 ce = sys::path::rend(*i);
103 ci != ce;
104 ++ci) {
105 outs() << *ci << ',';
106 }
107 outs() << "]\n";
Michael J. Spencer506e5792010-12-01 22:28:42 +0000108#endif
Michael J. Spencera42cf732010-11-30 23:28:07 +0000109
Michael J. Spencer50291592010-12-07 17:04:04 +0000110 path::has_root_path(*i);
111 path::root_path(*i);
112 path::has_root_name(*i);
113 path::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);
119 path::filename(*i);
120 path::has_stem(*i);
121 path::stem(*i);
122 path::has_extension(*i);
123 path::extension(*i);
124 path::is_absolute(*i);
125 path::is_relative(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000126
Michael J. Spencerba64b972011-01-04 17:00:18 +0000127 SmallString<128> temp_store;
Michael J. Spencer371716c2010-12-03 02:22:34 +0000128 temp_store = *i;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000129 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencer371716c2010-12-03 02:22:34 +0000130 temp_store = *i;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000131 path::remove_filename(temp_store);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000132
Michael J. Spencer371716c2010-12-03 02:22:34 +0000133 temp_store = *i;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000134 path::replace_extension(temp_store, "ext");
Michael J. Spencer1d389622010-12-01 06:03:33 +0000135 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencer50291592010-12-07 17:04:04 +0000136 stem = path::stem(filename);
137 ext = path::extension(filename);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000138 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
139
Michael J. Spencer936671b2010-12-07 03:57:37 +0000140 path::native(*i, temp_store);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000141 }
Michael J. Spencer25585162011-01-05 16:39:05 +0000142}
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000143
Michael J. Spencer25585162011-01-05 16:39:05 +0000144class FileSystemTest : public testing::Test {
145protected:
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;
149
150 virtual void SetUp() {
Michael J. Spencerf94f7322011-01-05 16:39:46 +0000151 int fd;
Michael J. Spencer25585162011-01-05 16:39:05 +0000152 ASSERT_NO_ERROR(
Michael J. Spencerf94f7322011-01-05 16:39:46 +0000153 fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
154 TestDirectory));
Michael J. Spencer25585162011-01-05 16:39:05 +0000155 // We don't care about this specific file.
Michael J. Spencerf94f7322011-01-05 16:39:46 +0000156 ::close(fd);
157 TestDirectory = path::parent_path(TestDirectory);
158 errs() << "Test Directory: " << TestDirectory << '\n';
159 errs().flush();
Michael J. Spencer25585162011-01-05 16:39:05 +0000160 }
161
162 virtual void TearDown() {
Michael J. Spencerf94f7322011-01-05 16:39:46 +0000163 uint32_t removed;
164 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
Michael J. Spencer25585162011-01-05 16:39:05 +0000165 }
166};
167
168TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000169 // Create a temp file.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000170 int FileDescriptor;
171 SmallString<64> TempPath;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000172 ASSERT_NO_ERROR(
173 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000174
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000175 // Make sure it exists.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000176 bool TempFileExists;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000177 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000178 EXPECT_TRUE(TempFileExists);
179
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000180 // Create another temp tile.
181 int FD2;
182 SmallString<64> TempPath2;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000183 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000184 ASSERT_NE(TempPath.str(), TempPath2.str());
185
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000186 fs::file_status A, B;
187 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
188 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
189 EXPECT_FALSE(fs::equivalent(A, B));
190
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000191 // Try to copy the first to the second.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000192 EXPECT_EQ(
193 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000194
195 ::close(FD2);
196 // Try again with the proper options.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000197 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
198 fs::copy_option::overwrite_if_exists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000199 // Remove Temp2.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000200 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000201 EXPECT_TRUE(TempFileExists);
202
203 // Make sure Temp2 doesn't exist.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000204 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000205 EXPECT_FALSE(TempFileExists);
206
207 // Create a hard link to Temp1.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000208 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000209 bool equal;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000210 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000211 EXPECT_TRUE(equal);
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000212 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
213 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
214 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000215
216 // Remove Temp1.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000217 ::close(FileDescriptor);
Michael J. Spencerba64b972011-01-04 17:00:18 +0000218 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
Michael J. Spencer106aa732010-12-03 17:53:43 +0000219 EXPECT_TRUE(TempFileExists);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000220
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000221 // Remove the hard link.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000222 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000223 EXPECT_TRUE(TempFileExists);
224
225 // Make sure Temp1 doesn't exist.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000226 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
Benjamin Kramer6d6d16a2010-12-03 12:33:32 +0000227 EXPECT_FALSE(TempFileExists);
Michael J. Spencer25585162011-01-05 16:39:05 +0000228}
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000229
Michael J. Spencer25585162011-01-05 16:39:05 +0000230TEST_F(FileSystemTest, DirectoryIteration) {
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000231 error_code ec;
Michael J. Spencerf9fd0782011-01-06 05:57:54 +0000232 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
233 ASSERT_NO_ERROR(ec);
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000234
235 // Create a known hierarchy to recurse over.
236 bool existed;
237 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
238 + "/recursive/a0/aa1", existed));
239 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
240 + "/recursive/a0/ab1", existed));
241 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
242 + "/recursive/dontlookhere/da1", existed));
243 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
244 + "/recursive/z0/za1", existed));
245 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
246 + "/recursive/pop/p1", existed));
247 typedef std::vector<std::string> v_t;
248 v_t visited;
249 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
250 + "/recursive", ec), e; i != e; i.increment(ec)){
251 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi52ee2302011-12-09 23:20:03 +0000252 if (path::filename(i->path()) == "p1") {
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000253 i.pop();
NAKAMURA Takumi52ee2302011-12-09 23:20:03 +0000254 // FIXME: recursive_directory_iterator should be more robust.
255 if (i == e) break;
256 }
Michael J. Spencerbd3825e2011-12-09 01:14:41 +0000257 if (path::filename(i->path()) == "dontlookhere")
258 i.no_push();
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000259 visited.push_back(path::filename(i->path()));
260 }
261 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
262 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
263 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
264 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
265 "dontlookhere");
266 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
267 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
268 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
269 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
270 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
271
272 // Make sure that each path was visited correctly.
273 ASSERT_NE(a0, visited.end());
274 ASSERT_NE(aa1, visited.end());
275 ASSERT_NE(ab1, visited.end());
276 ASSERT_NE(dontlookhere, visited.end());
277 ASSERT_EQ(da1, visited.end()); // Not visited.
278 ASSERT_NE(z0, visited.end());
279 ASSERT_NE(za1, visited.end());
280 ASSERT_NE(pop, visited.end());
281 ASSERT_EQ(p1, visited.end()); // Not visited.
282
283 // Make sure that parents were visited before children. No other ordering
284 // guarantees can be made across siblings.
285 ASSERT_LT(a0, aa1);
286 ASSERT_LT(a0, ab1);
287 ASSERT_LT(z0, za1);
Michael J. Spencerf9fd0782011-01-06 05:57:54 +0000288}
Michael J. Spencer238589e2011-01-06 05:58:02 +0000289
290TEST_F(FileSystemTest, Magic) {
291 struct type {
292 const char *filename;
293 const char *magic_str;
294 size_t magic_str_len;
295 } types [] = {{"magic.archive", "!<arch>\x0A", 8}};
296
297 // Create some files filled with magic.
298 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
299 ++i) {
300 SmallString<128> file_pathname(TestDirectory);
301 path::append(file_pathname, i->filename);
302 std::string ErrMsg;
303 raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
304 raw_fd_ostream::F_Binary);
305 ASSERT_FALSE(file.has_error());
306 StringRef magic(i->magic_str, i->magic_str_len);
307 file << magic;
Michael J. Spencer248f9f22011-01-15 18:52:49 +0000308 file.close();
Michael J. Spencer238589e2011-01-06 05:58:02 +0000309 bool res = false;
310 ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
311 EXPECT_TRUE(res);
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000312 }
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000313}
314
NAKAMURA Takumiebbd6fe2012-06-24 03:48:34 +0000315#if !defined(_WIN32) // FIXME: Win32 has different permission schema.
Nick Kledzikca077ec2012-06-20 00:28:54 +0000316TEST_F(FileSystemTest, Permissions) {
317 // Create a temp file.
318 int FileDescriptor;
319 SmallString<64> TempPath;
320 ASSERT_NO_ERROR(
321 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
322
323 // Mark file as read-only
324 const fs::perms AllWrite = fs::owner_write|fs::group_write|fs::others_write;
325 ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::remove_perms|AllWrite));
326
327 // Verify file is read-only
328 fs::file_status Status;
329 ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
330 bool AnyWriteBits = (Status.permissions() & AllWrite);
331 EXPECT_FALSE(AnyWriteBits);
332
333 // Mark file as read-write
334 ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::add_perms|AllWrite));
335
336 // Verify file is read-write
337 ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
338 AnyWriteBits = (Status.permissions() & AllWrite);
339 EXPECT_TRUE(AnyWriteBits);
340}
NAKAMURA Takumiebbd6fe2012-06-24 03:48:34 +0000341#endif
Nick Kledzikca077ec2012-06-20 00:28:54 +0000342
343TEST_F(FileSystemTest, FileMapping) {
344 // Create a temp file.
345 int FileDescriptor;
346 SmallString<64> TempPath;
347 ASSERT_NO_ERROR(
348 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
Nick Kledzikca077ec2012-06-20 00:28:54 +0000349 // Map in temp file and add some content
Michael J. Spencer1ebd25e2012-08-15 19:05:47 +0000350 error_code EC;
351 StringRef Val("hello there");
352 {
353 fs::mapped_file_region mfr(FileDescriptor,
354 fs::mapped_file_region::readwrite,
355 4096,
356 0,
357 EC);
358 ASSERT_NO_ERROR(EC);
359 std::copy(Val.begin(), Val.end(), mfr.data());
360 // Explicitly add a 0.
361 mfr.data()[Val.size()] = 0;
362 // Unmap temp file
363 }
Nick Kledzikca077ec2012-06-20 00:28:54 +0000364
365 // Map it back in read-only
Michael J. Spencer1ebd25e2012-08-15 19:05:47 +0000366 fs::mapped_file_region mfr(Twine(TempPath),
367 fs::mapped_file_region::readonly,
368 0,
369 0,
370 EC);
371 ASSERT_NO_ERROR(EC);
Nick Kledzikca077ec2012-06-20 00:28:54 +0000372
373 // Verify content
Michael J. Spencer1ebd25e2012-08-15 19:05:47 +0000374 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Nick Kledzikca077ec2012-06-20 00:28:54 +0000375
376 // Unmap temp file
Michael J. Spencer1ebd25e2012-08-15 19:05:47 +0000377
378#ifdef LLVM_USE_RVALUE_REFERENCES
379 fs::mapped_file_region m(Twine(TempPath),
380 fs::mapped_file_region::readonly,
381 0,
382 0,
383 EC);
384 ASSERT_NO_ERROR(EC);
385 const char *Data = m.const_data();
386 fs::mapped_file_region mfrrv(llvm_move(m));
387 EXPECT_EQ(mfrrv.const_data(), Data);
NAKAMURA Takumiae241ea2012-06-24 03:48:40 +0000388#endif
Michael J. Spencer1ebd25e2012-08-15 19:05:47 +0000389}
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000390} // anonymous namespace