]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/Support/Path.cpp
Vendor import of llvm trunk r178860:
[FreeBSD/FreeBSD.git] / unittests / Support / Path.cpp
1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
2 //
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
10 #include "llvm/Support/PathV2.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"
15
16 using namespace llvm;
17 using namespace llvm::sys;
18
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()); \
27   } else {}
28
29 namespace {
30
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(' '));
36
37 #ifdef LLVM_ON_WIN32
38   EXPECT_TRUE(path::is_separator('\\'));
39 #else
40   EXPECT_FALSE(path::is_separator('\\'));
41 #endif
42 }
43
44 TEST(Support, Path) {
45   SmallVector<StringRef, 40> paths;
46   paths.push_back("");
47   paths.push_back(".");
48   paths.push_back("..");
49   paths.push_back("foo");
50   paths.push_back("/");
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");
86
87   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
88                                                   e = paths.end();
89                                                   i != e;
90                                                   ++i) {
91     for (sys::path::const_iterator ci = sys::path::begin(*i),
92                                    ce = sys::path::end(*i);
93                                    ci != ce;
94                                    ++ci) {
95       ASSERT_FALSE(ci->empty());
96     }
97
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);
102                                      ci != ce;
103                                      ++ci) {
104       outs() << *ci << ',';
105     }
106     outs() << "]\n";
107 #endif
108
109     path::has_root_path(*i);
110     path::root_path(*i);
111     path::has_root_name(*i);
112     path::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);
118     path::filename(*i);
119     path::has_stem(*i);
120     path::stem(*i);
121     path::has_extension(*i);
122     path::extension(*i);
123     path::is_absolute(*i);
124     path::is_relative(*i);
125
126     SmallString<128> temp_store;
127     temp_store = *i;
128     ASSERT_NO_ERROR(fs::make_absolute(temp_store));
129     temp_store = *i;
130     path::remove_filename(temp_store);
131
132     temp_store = *i;
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());
138
139     path::native(*i, temp_store);
140   }
141 }
142
143 class FileSystemTest : public testing::Test {
144 protected:
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;
148
149   virtual void SetUp() {
150     int fd;
151     ASSERT_NO_ERROR(
152       fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
153                       TestDirectory));
154     // We don't care about this specific file.
155     ::close(fd);
156     TestDirectory = path::parent_path(TestDirectory);
157     errs() << "Test Directory: " << TestDirectory << '\n';
158     errs().flush();
159   }
160
161   virtual void TearDown() {
162     uint32_t removed;
163     ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
164   }
165 };
166
167 TEST_F(FileSystemTest, TempFiles) {
168   // Create a temp file.
169   int FileDescriptor;
170   SmallString<64> TempPath;
171   ASSERT_NO_ERROR(
172     fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
173
174   // Make sure it exists.
175   bool TempFileExists;
176   ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
177   EXPECT_TRUE(TempFileExists);
178
179   // Create another temp tile.
180   int FD2;
181   SmallString<64> TempPath2;
182   ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
183   ASSERT_NE(TempPath.str(), TempPath2.str());
184
185   fs::file_status A, B;
186   ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
187   ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
188   EXPECT_FALSE(fs::equivalent(A, B));
189
190   // Try to copy the first to the second.
191   EXPECT_EQ(
192     fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
193
194   ::close(FD2);
195   // Try again with the proper options.
196   ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
197                                 fs::copy_option::overwrite_if_exists));
198   // Remove Temp2.
199   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
200   EXPECT_TRUE(TempFileExists);
201
202   // Make sure Temp2 doesn't exist.
203   ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
204   EXPECT_FALSE(TempFileExists);
205
206   // Create a hard link to Temp1.
207   ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
208   bool equal;
209   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
210   EXPECT_TRUE(equal);
211   ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
212   ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
213   EXPECT_TRUE(fs::equivalent(A, B));
214
215   // Remove Temp1.
216   ::close(FileDescriptor);
217   ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
218   EXPECT_TRUE(TempFileExists);
219
220   // Remove the hard link.
221   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
222   EXPECT_TRUE(TempFileExists);
223
224   // Make sure Temp1 doesn't exist.
225   ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
226   EXPECT_FALSE(TempFileExists);
227
228 #ifdef LLVM_ON_WIN32
229   // Path name > 260 chars should get an error.
230   const char *Path270 =
231     "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
232     "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
233     "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
234     "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
235     "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
236   EXPECT_EQ(fs::unique_file(Twine(Path270), FileDescriptor, TempPath),
237             windows_error::path_not_found);
238 #endif
239 }
240
241 TEST_F(FileSystemTest, DirectoryIteration) {
242   error_code ec;
243   for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
244     ASSERT_NO_ERROR(ec);
245
246   // Create a known hierarchy to recurse over.
247   bool existed;
248   ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
249                   + "/recursive/a0/aa1", existed));
250   ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
251                   + "/recursive/a0/ab1", existed));
252   ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
253                   + "/recursive/dontlookhere/da1", existed));
254   ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
255                   + "/recursive/z0/za1", existed));
256   ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
257                   + "/recursive/pop/p1", existed));
258   typedef std::vector<std::string> v_t;
259   v_t visited;
260   for (fs::recursive_directory_iterator i(Twine(TestDirectory)
261          + "/recursive", ec), e; i != e; i.increment(ec)){
262     ASSERT_NO_ERROR(ec);
263     if (path::filename(i->path()) == "p1") {
264       i.pop();
265       // FIXME: recursive_directory_iterator should be more robust.
266       if (i == e) break;
267     }
268     if (path::filename(i->path()) == "dontlookhere")
269       i.no_push();
270     visited.push_back(path::filename(i->path()));
271   }
272   v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
273   v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
274   v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
275   v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
276                                                "dontlookhere");
277   v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
278   v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
279   v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
280   v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
281   v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
282
283   // Make sure that each path was visited correctly.
284   ASSERT_NE(a0, visited.end());
285   ASSERT_NE(aa1, visited.end());
286   ASSERT_NE(ab1, visited.end());
287   ASSERT_NE(dontlookhere, visited.end());
288   ASSERT_EQ(da1, visited.end()); // Not visited.
289   ASSERT_NE(z0, visited.end());
290   ASSERT_NE(za1, visited.end());
291   ASSERT_NE(pop, visited.end());
292   ASSERT_EQ(p1, visited.end()); // Not visited.
293
294   // Make sure that parents were visited before children. No other ordering
295   // guarantees can be made across siblings.
296   ASSERT_LT(a0, aa1);
297   ASSERT_LT(a0, ab1);
298   ASSERT_LT(z0, za1);
299 }
300
301 TEST_F(FileSystemTest, Magic) {
302   struct type {
303     const char *filename;
304     const char *magic_str;
305     size_t      magic_str_len;
306   } types [] = {{"magic.archive", "!<arch>\x0A", 8}};
307
308   // Create some files filled with magic.
309   for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
310                                                                      ++i) {
311     SmallString<128> file_pathname(TestDirectory);
312     path::append(file_pathname, i->filename);
313     std::string ErrMsg;
314     raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
315                         raw_fd_ostream::F_Binary);
316     ASSERT_FALSE(file.has_error());
317     StringRef magic(i->magic_str, i->magic_str_len);
318     file << magic;
319     file.close();
320     bool res = false;
321     ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
322     EXPECT_TRUE(res);
323   }
324 }
325
326 #if !defined(_WIN32) // FIXME: Win32 has different permission schema.
327 TEST_F(FileSystemTest, Permissions) {
328   // Create a temp file.
329   int FileDescriptor;
330   SmallString<64> TempPath;
331   ASSERT_NO_ERROR(
332     fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
333
334   // Mark file as read-only
335   const fs::perms AllWrite = fs::owner_write|fs::group_write|fs::others_write;
336   ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::remove_perms|AllWrite));
337  
338   // Verify file is read-only
339   fs::file_status Status;
340   ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
341   bool AnyWriteBits = (Status.permissions() & AllWrite);
342   EXPECT_FALSE(AnyWriteBits);
343   
344   // Mark file as read-write
345   ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::add_perms|AllWrite));
346   
347   // Verify file is read-write
348   ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
349   AnyWriteBits = (Status.permissions() & AllWrite);
350   EXPECT_TRUE(AnyWriteBits);
351 }
352 #endif
353
354 TEST_F(FileSystemTest, FileMapping) {
355   // Create a temp file.
356   int FileDescriptor;
357   SmallString<64> TempPath;
358   ASSERT_NO_ERROR(
359     fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
360   // Map in temp file and add some content
361   error_code EC;
362   StringRef Val("hello there");
363   {
364     fs::mapped_file_region mfr(FileDescriptor,
365                                true,
366                                fs::mapped_file_region::readwrite,
367                                4096,
368                                0,
369                                EC);
370     ASSERT_NO_ERROR(EC);
371     std::copy(Val.begin(), Val.end(), mfr.data());
372     // Explicitly add a 0.
373     mfr.data()[Val.size()] = 0;
374     // Unmap temp file
375   }
376   
377   // Map it back in read-only
378   fs::mapped_file_region mfr(Twine(TempPath),
379                              fs::mapped_file_region::readonly,
380                              0,
381                              0,
382                              EC);
383   ASSERT_NO_ERROR(EC);
384   
385   // Verify content
386   EXPECT_EQ(StringRef(mfr.const_data()), Val);
387   
388   // Unmap temp file
389
390 #if LLVM_HAS_RVALUE_REFERENCES
391   fs::mapped_file_region m(Twine(TempPath),
392                              fs::mapped_file_region::readonly,
393                              0,
394                              0,
395                              EC);
396   ASSERT_NO_ERROR(EC);
397   const char *Data = m.const_data();
398   fs::mapped_file_region mfrrv(llvm_move(m));
399   EXPECT_EQ(mfrrv.const_data(), Data);
400 #endif
401 }
402 } // anonymous namespace