1 #ifndef FILESYSTEM_TEST_HELPER_H
2 #define FILESYSTEM_TEST_HELPER_H
3 
4 #include "filesystem_include.h"
5 
6 #include <sys/stat.h> // for stat, mkdir, mkfifo
7 #ifndef _WIN32
8 #include <unistd.h> // for ftruncate, link, symlink, getcwd, chdir
9 #include <sys/statvfs.h>
10 #else
11 #include <io.h>
12 #include <direct.h>
13 #include <windows.h> // for CreateSymbolicLink, CreateHardLink
14 #endif
15 
16 #include <cassert>
17 #include <chrono>
18 #include <cstdio> // for printf
19 #include <string>
20 #include <system_error>
21 #include <vector>
22 
23 #include "make_string.h"
24 #include "test_macros.h"
25 #include "rapid-cxx-test.h"
26 #include "format_string.h"
27 
28 // For creating socket files
29 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
30 # include <sys/socket.h>
31 # include <sys/un.h>
32 #endif
33 
34 namespace utils {
35 #ifdef _WIN32
mkdir(const char * path,int mode)36     inline int mkdir(const char* path, int mode) { (void)mode; return ::_mkdir(path); }
ftruncate(int fd,off_t length)37     inline int ftruncate(int fd, off_t length) { return ::_chsize(fd, length); }
symlink(const char * oldname,const char * newname,bool is_dir)38     inline int symlink(const char* oldname, const char* newname, bool is_dir) {
39         DWORD flags = is_dir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
40         if (CreateSymbolicLinkA(newname, oldname,
41                                 flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE))
42           return 0;
43         if (GetLastError() != ERROR_INVALID_PARAMETER)
44           return 1;
45         return !CreateSymbolicLinkA(newname, oldname, flags);
46     }
link(const char * oldname,const char * newname)47     inline int link(const char *oldname, const char* newname) {
48         return !CreateHardLinkA(newname, oldname, NULL);
49     }
setenv(const char * var,const char * val,int overwrite)50     inline int setenv(const char *var, const char *val, int overwrite) {
51         (void)overwrite;
52         return ::_putenv((std::string(var) + "=" + std::string(val)).c_str());
53     }
unsetenv(const char * var)54     inline int unsetenv(const char *var) {
55         return ::_putenv((std::string(var) + "=").c_str());
56     }
space(std::string path,std::uintmax_t & capacity,std::uintmax_t & free,std::uintmax_t & avail)57     inline bool space(std::string path, std::uintmax_t &capacity,
58                       std::uintmax_t &free, std::uintmax_t &avail) {
59         ULARGE_INTEGER FreeBytesAvailableToCaller, TotalNumberOfBytes,
60                        TotalNumberOfFreeBytes;
61         if (!GetDiskFreeSpaceExA(path.c_str(), &FreeBytesAvailableToCaller,
62                                  &TotalNumberOfBytes, &TotalNumberOfFreeBytes))
63           return false;
64         capacity = TotalNumberOfBytes.QuadPart;
65         free = TotalNumberOfFreeBytes.QuadPart;
66         avail = FreeBytesAvailableToCaller.QuadPart;
67         assert(capacity > 0);
68         assert(free > 0);
69         assert(avail > 0);
70         return true;
71     }
72 #else
73     using ::mkdir;
74     using ::ftruncate;
75     inline int symlink(const char* oldname, const char* newname, bool is_dir) { (void)is_dir; return ::symlink(oldname, newname); }
76     using ::link;
77     using ::setenv;
78     using ::unsetenv;
79     inline bool space(std::string path, std::uintmax_t &capacity,
80                       std::uintmax_t &free, std::uintmax_t &avail) {
81         struct statvfs expect;
82         if (::statvfs(path.c_str(), &expect) == -1)
83           return false;
84         assert(expect.f_bavail > 0);
85         assert(expect.f_bfree > 0);
86         assert(expect.f_bsize > 0);
87         assert(expect.f_blocks > 0);
88         assert(expect.f_frsize > 0);
89         auto do_mult = [&](std::uintmax_t val) {
90             std::uintmax_t fsize = expect.f_frsize;
91             std::uintmax_t new_val = val * fsize;
92             assert(new_val / fsize == val); // Test for overflow
93             return new_val;
94         };
95         capacity = do_mult(expect.f_blocks);
96         free = do_mult(expect.f_bfree);
97         avail = do_mult(expect.f_bavail);
98         return true;
99     }
100 #endif
101 
getcwd()102     inline std::string getcwd() {
103         // Assume that path lengths are not greater than this.
104         // This should be fine for testing purposes.
105         char buf[4096];
106         char* ret = ::getcwd(buf, sizeof(buf));
107         assert(ret && "getcwd failed");
108         return std::string(ret);
109     }
110 
exists(std::string const & path)111     inline bool exists(std::string const& path) {
112         struct ::stat tmp;
113         return ::stat(path.c_str(), &tmp) == 0;
114     }
115 } // end namespace utils
116 
117 struct scoped_test_env
118 {
scoped_test_envscoped_test_env119     scoped_test_env() : test_root(available_cwd_path()) {
120 #ifdef _WIN32
121         // Windows mkdir can create multiple recursive directories
122         // if needed.
123         std::string cmd = "mkdir " + test_root.string();
124 #else
125         std::string cmd = "mkdir -p " + test_root.string();
126 #endif
127         int ret = std::system(cmd.c_str());
128         assert(ret == 0);
129 
130         // Ensure that the root_path is fully resolved, i.e. it contains no
131         // symlinks. The filesystem tests depend on that. We do this after
132         // creating the root_path, because `fs::canonical` requires the
133         // path to exist.
134         test_root = fs::canonical(test_root);
135     }
136 
~scoped_test_envscoped_test_env137     ~scoped_test_env() {
138 #ifdef _WIN32
139         std::string cmd = "rmdir /s /q " + test_root.string();
140         int ret = std::system(cmd.c_str());
141         assert(ret == 0);
142 #else
143 #if defined(__MVS__)
144         // The behaviour of chmod -R on z/OS prevents recursive
145         // permission change for directories that do not have read permission.
146         std::string cmd = "find  " + test_root.string() + " -exec chmod 777 {} \\;";
147 #else
148         std::string cmd = "chmod -R 777 " + test_root.string();
149 #endif // defined(__MVS__)
150         int ret = std::system(cmd.c_str());
151 #if !defined(_AIX)
152         // On AIX the chmod command will return non-zero when trying to set
153         // the permissions on a directory that contains a bad symlink. This triggers
154         // the assert, despite being able to delete everything with the following
155         // `rm -r` command.
156         assert(ret == 0);
157 #endif
158 
159         cmd = "rm -rf " + test_root.string();
160         ret = std::system(cmd.c_str());
161         assert(ret == 0);
162 #endif
163     }
164 
165     scoped_test_env(scoped_test_env const &) = delete;
166     scoped_test_env & operator=(scoped_test_env const &) = delete;
167 
make_env_pathscoped_test_env168     fs::path make_env_path(std::string p) { return sanitize_path(p); }
169 
sanitize_pathscoped_test_env170     std::string sanitize_path(std::string raw) {
171         assert(raw.find("..") == std::string::npos);
172         std::string root = test_root.string();
173         if (root.compare(0, root.size(), raw, 0, root.size()) != 0) {
174             assert(raw.front() != '\\');
175             fs::path tmp(test_root);
176             tmp /= raw;
177             return tmp.string();
178         }
179         return raw;
180     }
181 
182     // Purposefully using a size potentially larger than off_t here so we can
183     // test the behavior of libc++fs when it is built with _FILE_OFFSET_BITS=64
184     // but the caller is not (std::filesystem also uses uintmax_t rather than
185     // off_t). On a 32-bit system this allows us to create a file larger than
186     // 2GB.
187     std::string create_file(fs::path filename_path, uintmax_t size = 0) {
188         std::string filename = filename_path.string();
189 #if defined(__LP64__) || defined(_WIN32) || defined(__MVS__)
190         auto large_file_fopen = fopen;
191         auto large_file_ftruncate = utils::ftruncate;
192         using large_file_offset_t = off_t;
193 #else
194         auto large_file_fopen = fopen64;
195         auto large_file_ftruncate = ftruncate64;
196         using large_file_offset_t = off64_t;
197 #endif
198 
199         filename = sanitize_path(std::move(filename));
200 
201         if (size >
202             static_cast<typename std::make_unsigned<large_file_offset_t>::type>(
203                 std::numeric_limits<large_file_offset_t>::max())) {
204             fprintf(stderr, "create_file(%s, %ju) too large\n",
205                     filename.c_str(), size);
206             abort();
207         }
208 
209 #if defined(_WIN32) || defined(__MVS__)
210 #  define FOPEN_CLOEXEC_FLAG ""
211 #else
212 #  define FOPEN_CLOEXEC_FLAG "e"
213 #endif
214         FILE* file = large_file_fopen(filename.c_str(), "w" FOPEN_CLOEXEC_FLAG);
215         if (file == nullptr) {
216             fprintf(stderr, "fopen %s failed: %s\n", filename.c_str(),
217                     strerror(errno));
218             abort();
219         }
220 
221         if (large_file_ftruncate(
222                 fileno(file), static_cast<large_file_offset_t>(size)) == -1) {
223             fprintf(stderr, "ftruncate %s %ju failed: %s\n", filename.c_str(),
224                     size, strerror(errno));
225             fclose(file);
226             abort();
227         }
228 
229         fclose(file);
230         return filename;
231     }
232 
create_dirscoped_test_env233     std::string create_dir(fs::path filename_path) {
234         std::string filename = filename_path.string();
235         filename = sanitize_path(std::move(filename));
236         int ret = utils::mkdir(filename.c_str(), 0777); // rwxrwxrwx mode
237         assert(ret == 0);
238         return filename;
239     }
240 
241     std::string create_file_dir_symlink(fs::path source_path,
242                                         fs::path to_path,
243                                         bool sanitize_source = true,
244                                         bool is_dir = false) {
245         std::string source = source_path.string();
246         std::string to = to_path.string();
247         if (sanitize_source)
248             source = sanitize_path(std::move(source));
249         to = sanitize_path(std::move(to));
250         int ret = utils::symlink(source.c_str(), to.c_str(), is_dir);
251         assert(ret == 0);
252         return to;
253     }
254 
255     std::string create_symlink(fs::path source_path,
256                                fs::path to_path,
257                                bool sanitize_source = true) {
258         return create_file_dir_symlink(source_path, to_path, sanitize_source,
259                                        false);
260     }
261 
262     std::string create_directory_symlink(fs::path source_path,
263                                          fs::path to_path,
264                                          bool sanitize_source = true) {
265         return create_file_dir_symlink(source_path, to_path, sanitize_source,
266                                        true);
267     }
268 
create_hardlinkscoped_test_env269     std::string create_hardlink(fs::path source_path, fs::path to_path) {
270         std::string source = source_path.string();
271         std::string to = to_path.string();
272         source = sanitize_path(std::move(source));
273         to = sanitize_path(std::move(to));
274         int ret = utils::link(source.c_str(), to.c_str());
275         assert(ret == 0);
276         return to;
277     }
278 
279 #ifndef _WIN32
create_fifoscoped_test_env280     std::string create_fifo(std::string file) {
281         file = sanitize_path(std::move(file));
282         int ret = ::mkfifo(file.c_str(), 0666); // rw-rw-rw- mode
283         assert(ret == 0);
284         return file;
285     }
286 #endif
287 
288   // Some platforms doesn't support socket files so we shouldn't even
289   // allow tests to call this unguarded.
290 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
create_socketscoped_test_env291     std::string create_socket(std::string file) {
292         file = sanitize_path(std::move(file));
293 
294         ::sockaddr_un address;
295         address.sun_family = AF_UNIX;
296         assert(file.size() <= sizeof(address.sun_path));
297         ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
298         int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
299         ::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address));
300         return file;
301     }
302 #endif
303 
304     fs::path test_root;
305 
306 private:
307     // This could potentially introduce a filesystem race if multiple
308     // scoped_test_envs were created concurrently in the same test (hence
309     // sharing the same cwd). However, it is fairly unlikely to happen as
310     // we generally don't use scoped_test_env from multiple threads, so
311     // this is deemed acceptable.
312     // The cwd.filename() itself isn't unique across all tests in the suite,
313     // so start the numbering from a hash of the full cwd, to avoid
314     // different tests interfering with each other.
available_cwd_pathscoped_test_env315     static inline fs::path available_cwd_path() {
316         fs::path const cwd = utils::getcwd();
317         fs::path const tmp = fs::temp_directory_path();
318         std::string base = cwd.filename().string();
319         size_t i = std::hash<std::string>()(cwd.string());
320         fs::path p = tmp / (base + "-static_env." + std::to_string(i));
321         while (utils::exists(p.string())) {
322             p = tmp / (base + "-static_env." + std::to_string(++i));
323         }
324         return p;
325     }
326 };
327 
328 /// This class generates the following tree:
329 ///
330 ///     static_test_env
331 ///     ├── bad_symlink -> dne
332 ///     ├── dir1
333 ///     │   ├── dir2
334 ///     │   │   ├── afile3
335 ///     │   │   ├── dir3
336 ///     │   │   │   └── file5
337 ///     │   │   ├── file4
338 ///     │   │   └── symlink_to_dir3 -> dir3
339 ///     │   ├── file1
340 ///     │   └── file2
341 ///     ├── empty_file
342 ///     ├── non_empty_file
343 ///     ├── symlink_to_dir -> dir1
344 ///     └── symlink_to_empty_file -> empty_file
345 ///
346 class static_test_env {
347     scoped_test_env env_;
348 public:
static_test_env()349     static_test_env() {
350         env_.create_symlink("dne", "bad_symlink", false);
351         env_.create_dir("dir1");
352         env_.create_dir("dir1/dir2");
353         env_.create_file("dir1/dir2/afile3");
354         env_.create_dir("dir1/dir2/dir3");
355         env_.create_file("dir1/dir2/dir3/file5");
356         env_.create_file("dir1/dir2/file4");
357         env_.create_directory_symlink("dir3", "dir1/dir2/symlink_to_dir3", false);
358         env_.create_file("dir1/file1");
359         env_.create_file("dir1/file2", 42);
360         env_.create_file("empty_file");
361         env_.create_file("non_empty_file", 42);
362         env_.create_directory_symlink("dir1", "symlink_to_dir", false);
363         env_.create_symlink("empty_file", "symlink_to_empty_file", false);
364     }
365 
366     const fs::path Root = env_.test_root;
367 
makePath(fs::path const & p)368     fs::path makePath(fs::path const& p) const {
369         // env_path is expected not to contain symlinks.
370         fs::path const& env_path = Root;
371         return env_path / p;
372     }
373 
374     const std::vector<fs::path> TestFileList = {
375         makePath("empty_file"),
376         makePath("non_empty_file"),
377         makePath("dir1/file1"),
378         makePath("dir1/file2")
379     };
380 
381     const std::vector<fs::path> TestDirList = {
382         makePath("dir1"),
383         makePath("dir1/dir2"),
384         makePath("dir1/dir2/dir3")
385     };
386 
387     const fs::path File          = TestFileList[0];
388     const fs::path Dir           = TestDirList[0];
389     const fs::path Dir2          = TestDirList[1];
390     const fs::path Dir3          = TestDirList[2];
391     const fs::path SymlinkToFile = makePath("symlink_to_empty_file");
392     const fs::path SymlinkToDir  = makePath("symlink_to_dir");
393     const fs::path BadSymlink    = makePath("bad_symlink");
394     const fs::path DNE           = makePath("DNE");
395     const fs::path EmptyFile     = TestFileList[0];
396     const fs::path NonEmptyFile  = TestFileList[1];
397     const fs::path CharFile      = "/dev/null"; // Hopefully this exists
398 
399     const std::vector<fs::path> DirIterationList = {
400         makePath("dir1/dir2"),
401         makePath("dir1/file1"),
402         makePath("dir1/file2")
403     };
404 
405     const std::vector<fs::path> DirIterationListDepth1 = {
406         makePath("dir1/dir2/afile3"),
407         makePath("dir1/dir2/dir3"),
408         makePath("dir1/dir2/symlink_to_dir3"),
409         makePath("dir1/dir2/file4"),
410     };
411 
412     const std::vector<fs::path> RecDirIterationList = {
413         makePath("dir1/dir2"),
414         makePath("dir1/file1"),
415         makePath("dir1/file2"),
416         makePath("dir1/dir2/afile3"),
417         makePath("dir1/dir2/dir3"),
418         makePath("dir1/dir2/symlink_to_dir3"),
419         makePath("dir1/dir2/file4"),
420         makePath("dir1/dir2/dir3/file5")
421     };
422 
423     const std::vector<fs::path> RecDirFollowSymlinksIterationList = {
424         makePath("dir1/dir2"),
425         makePath("dir1/file1"),
426         makePath("dir1/file2"),
427         makePath("dir1/dir2/afile3"),
428         makePath("dir1/dir2/dir3"),
429         makePath("dir1/dir2/file4"),
430         makePath("dir1/dir2/dir3/file5"),
431         makePath("dir1/dir2/symlink_to_dir3"),
432         makePath("dir1/dir2/symlink_to_dir3/file5"),
433     };
434 };
435 
436 struct CWDGuard {
437   std::string oldCwd_;
CWDGuardCWDGuard438   CWDGuard() : oldCwd_(utils::getcwd()) { }
~CWDGuardCWDGuard439   ~CWDGuard() {
440     int ret = ::chdir(oldCwd_.c_str());
441     assert(ret == 0 && "chdir failed");
442   }
443 
444   CWDGuard(CWDGuard const&) = delete;
445   CWDGuard& operator=(CWDGuard const&) = delete;
446 };
447 
448 // Misc test types
449 
450 const MultiStringType PathList[] = {
451         MKSTR(""),
452         MKSTR(" "),
453         MKSTR("//"),
454         MKSTR("."),
455         MKSTR(".."),
456         MKSTR("foo"),
457         MKSTR("/"),
458         MKSTR("/foo"),
459         MKSTR("foo/"),
460         MKSTR("/foo/"),
461         MKSTR("foo/bar"),
462         MKSTR("/foo/bar"),
463         MKSTR("//net"),
464         MKSTR("//net/foo"),
465         MKSTR("///foo///"),
466         MKSTR("///foo///bar"),
467         MKSTR("/."),
468         MKSTR("./"),
469         MKSTR("/.."),
470         MKSTR("../"),
471         MKSTR("foo/."),
472         MKSTR("foo/.."),
473         MKSTR("foo/./"),
474         MKSTR("foo/./bar"),
475         MKSTR("foo/../"),
476         MKSTR("foo/../bar"),
477         MKSTR("c:"),
478         MKSTR("c:/"),
479         MKSTR("c:foo"),
480         MKSTR("c:/foo"),
481         MKSTR("c:foo/"),
482         MKSTR("c:/foo/"),
483         MKSTR("c:/foo/bar"),
484         MKSTR("prn:"),
485         MKSTR("c:\\"),
486         MKSTR("c:\\foo"),
487         MKSTR("c:foo\\"),
488         MKSTR("c:\\foo\\"),
489         MKSTR("c:\\foo/"),
490         MKSTR("c:/foo\\bar"),
491         MKSTR("//"),
492         MKSTR("/finally/we/need/one/really/really/really/really/really/really/really/long/string")
493 };
494 const unsigned PathListSize = sizeof(PathList) / sizeof(MultiStringType);
495 
496 template <class Iter>
IterEnd(Iter B)497 Iter IterEnd(Iter B) {
498   using VT = typename std::iterator_traits<Iter>::value_type;
499   for (; *B != VT{}; ++B)
500     ;
501   return B;
502 }
503 
504 template <class CharT>
StrEnd(CharT const * P)505 const CharT* StrEnd(CharT const* P) {
506     return IterEnd(P);
507 }
508 
509 template <class CharT>
StrLen(CharT const * P)510 std::size_t StrLen(CharT const* P) {
511     return StrEnd(P) - P;
512 }
513 
514 // Testing the allocation behavior of the code_cvt functions requires
515 // *knowing* that the allocation was not done by "path::__str_".
516 // This hack forces path to allocate enough memory.
PathReserve(fs::path & p,std::size_t N)517 inline void PathReserve(fs::path& p, std::size_t N) {
518   auto const& native_ref = p.native();
519   const_cast<fs::path::string_type&>(native_ref).reserve(N);
520 }
521 
522 template <class Iter1, class Iter2>
checkCollectionsEqual(Iter1 start1,Iter1 const end1,Iter2 start2,Iter2 const end2)523 bool checkCollectionsEqual(
524     Iter1 start1, Iter1 const end1
525   , Iter2 start2, Iter2 const end2
526   )
527 {
528     while (start1 != end1 && start2 != end2) {
529         if (*start1 != *start2) {
530             return false;
531         }
532         ++start1; ++start2;
533     }
534     return (start1 == end1 && start2 == end2);
535 }
536 
537 
538 template <class Iter1, class Iter2>
checkCollectionsEqualBackwards(Iter1 const start1,Iter1 end1,Iter2 const start2,Iter2 end2)539 bool checkCollectionsEqualBackwards(
540     Iter1 const start1, Iter1 end1
541   , Iter2 const start2, Iter2 end2
542   )
543 {
544     while (start1 != end1 && start2 != end2) {
545         --end1; --end2;
546         if (*end1 != *end2) {
547             return false;
548         }
549     }
550     return (start1 == end1 && start2 == end2);
551 }
552 
553 // We often need to test that the error_code was cleared if no error occurs
554 // this function returns an error_code which is set to an error that will
555 // never be returned by the filesystem functions.
556 inline std::error_code GetTestEC(unsigned Idx = 0) {
557   using std::errc;
558   auto GetErrc = [&]() {
559     switch (Idx) {
560     case 0:
561       return errc::address_family_not_supported;
562     case 1:
563       return errc::address_not_available;
564     case 2:
565       return errc::address_in_use;
566     case 3:
567       return errc::argument_list_too_long;
568     default:
569       assert(false && "Idx out of range");
570       std::abort();
571     }
572   };
573   return std::make_error_code(GetErrc());
574 }
575 
ErrorIsImp(const std::error_code & ec,std::vector<std::errc> const & errors)576 inline bool ErrorIsImp(const std::error_code& ec,
577                        std::vector<std::errc> const& errors) {
578   std::error_condition cond = ec.default_error_condition();
579   for (auto errc : errors) {
580     if (cond.value() == static_cast<int>(errc))
581       return true;
582   }
583   return false;
584 }
585 
586 template <class... ErrcT>
ErrorIs(const std::error_code & ec,std::errc First,ErrcT...Rest)587 inline bool ErrorIs(const std::error_code& ec, std::errc First, ErrcT... Rest) {
588   std::vector<std::errc> errors = {First, Rest...};
589   return ErrorIsImp(ec, errors);
590 }
591 
592 // Provide our own Sleep routine since std::this_thread::sleep_for is not
593 // available in single-threaded mode.
SleepFor(Dur dur)594 template <class Dur> void SleepFor(Dur dur) {
595     using namespace std::chrono;
596 #if defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
597     using Clock = system_clock;
598 #else
599     using Clock = steady_clock;
600 #endif
601     const auto wake_time = Clock::now() + dur;
602     while (Clock::now() < wake_time)
603         ;
604 }
605 
PathEq(fs::path const & LHS,fs::path const & RHS)606 inline bool PathEq(fs::path const& LHS, fs::path const& RHS) {
607   return LHS.native() == RHS.native();
608 }
609 
PathEqIgnoreSep(fs::path LHS,fs::path RHS)610 inline bool PathEqIgnoreSep(fs::path LHS, fs::path RHS) {
611   LHS.make_preferred();
612   RHS.make_preferred();
613   return LHS.native() == RHS.native();
614 }
615 
NormalizeExpectedPerms(fs::perms P)616 inline fs::perms NormalizeExpectedPerms(fs::perms P) {
617 #ifdef _WIN32
618   // On Windows, fs::perms only maps down to one bit stored in the filesystem,
619   // a boolean readonly flag.
620   // Normalize permissions to the format it gets returned; all fs entries are
621   // read+exec for all users; writable ones also have the write bit set for
622   // all users.
623   P |= fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read;
624   P |= fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec;
625   fs::perms Write =
626       fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
627   if ((P & Write) != fs::perms::none)
628     P |= Write;
629 #endif
630   return P;
631 }
632 
633 struct ExceptionChecker {
634   std::errc expected_err;
635   fs::path expected_path1;
636   fs::path expected_path2;
637   unsigned num_paths;
638   const char* func_name;
639   std::string opt_message;
640 
641   explicit ExceptionChecker(std::errc first_err, const char* fun_name,
642                             std::string opt_msg = {})
643       : expected_err{first_err}, num_paths(0), func_name(fun_name),
644         opt_message(opt_msg) {}
645   explicit ExceptionChecker(fs::path p, std::errc first_err,
646                             const char* fun_name, std::string opt_msg = {})
expected_errExceptionChecker647       : expected_err(first_err), expected_path1(p), num_paths(1),
648         func_name(fun_name), opt_message(opt_msg) {}
649 
650   explicit ExceptionChecker(fs::path p1, fs::path p2, std::errc first_err,
651                             const char* fun_name, std::string opt_msg = {})
expected_errExceptionChecker652       : expected_err(first_err), expected_path1(p1), expected_path2(p2),
653         num_paths(2), func_name(fun_name), opt_message(opt_msg) {}
654 
operatorExceptionChecker655   void operator()(fs::filesystem_error const& Err) {
656     TEST_CHECK(ErrorIsImp(Err.code(), {expected_err}));
657     TEST_CHECK(Err.path1() == expected_path1);
658     TEST_CHECK(Err.path2() == expected_path2);
659     LIBCPP_ONLY(check_libcxx_string(Err));
660   }
661 
check_libcxx_stringExceptionChecker662   void check_libcxx_string(fs::filesystem_error const& Err) {
663     std::string message = std::make_error_code(expected_err).message();
664 
665     std::string additional_msg = "";
666     if (!opt_message.empty()) {
667       additional_msg = opt_message + ": ";
668     }
669     auto transform_path = [](const fs::path& p) {
670       return "\"" + p.string() + "\"";
671     };
672     std::string format = [&]() -> std::string {
673       switch (num_paths) {
674       case 0:
675         return format_string("filesystem error: in %s: %s%s", func_name,
676                              additional_msg, message);
677       case 1:
678         return format_string("filesystem error: in %s: %s%s [%s]", func_name,
679                              additional_msg, message,
680                              transform_path(expected_path1).c_str());
681       case 2:
682         return format_string("filesystem error: in %s: %s%s [%s] [%s]",
683                              func_name, additional_msg, message,
684                              transform_path(expected_path1).c_str(),
685                              transform_path(expected_path2).c_str());
686       default:
687         TEST_CHECK(false && "unexpected case");
688         return "";
689       }
690     }();
691     TEST_CHECK(format == Err.what());
692     if (format != Err.what()) {
693       fprintf(stderr,
694               "filesystem_error::what() does not match expected output:\n");
695       fprintf(stderr, "  expected: \"%s\"\n", format.c_str());
696       fprintf(stderr, "  actual:   \"%s\"\n\n", Err.what());
697     }
698   }
699 
700   ExceptionChecker(ExceptionChecker const&) = delete;
701   ExceptionChecker& operator=(ExceptionChecker const&) = delete;
702 
703 };
704 
GetWindowsInaccessibleDir()705 inline fs::path GetWindowsInaccessibleDir() {
706   // Only makes sense on windows, but the code can be compiled for
707   // any platform.
708   const fs::path dir("C:\\System Volume Information");
709   std::error_code ec;
710   const fs::path root("C:\\");
711   for (const auto &ent : fs::directory_iterator(root, ec)) {
712     if (ent != dir)
713       continue;
714     // Basic sanity checks on the directory_entry
715     if (!ent.exists() || !ent.is_directory()) {
716       fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
717                       "but doesn't behave as expected, skipping tests "
718                       "regarding it\n", dir.string().c_str());
719       return fs::path();
720     }
721     // Check that it indeed is inaccessible as expected
722     (void)fs::exists(ent, ec);
723     if (!ec) {
724       fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
725                       "but seems to be accessible, skipping tests "
726                       "regarding it\n", dir.string().c_str());
727       return fs::path();
728     }
729     return ent;
730   }
731   fprintf(stderr, "No inaccessible directory \"%s\" found, skipping tests "
732                   "regarding it\n", dir.string().c_str());
733   return fs::path();
734 }
735 
736 #endif /* FILESYSTEM_TEST_HELPER_H */
737