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