1 #ifndef FILESYSTEM_TEST_HELPER_H
2 #define FILESYSTEM_TEST_HELPER_H
4 #include "filesystem_include.h"
6 #include <sys/stat.h> // for stat, mkdir, mkfifo
8 #include <unistd.h> // for ftruncate, link, symlink, getcwd, chdir
9 #include <sys/statvfs.h>
13 #include <windows.h> // for CreateSymbolicLink, CreateHardLink
20 #include <cstdio> // for printf
22 #include <system_error>
23 #include <type_traits>
26 #include "assert_macros.h"
27 #include "make_string.h"
28 #include "test_macros.h"
29 #include "format_string.h"
31 // For creating socket files
32 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
33 # include <sys/socket.h>
39 inline int mkdir(const char* path
, int mode
) { (void)mode
; return ::_mkdir(path
); }
40 inline int symlink(const char* oldname
, const char* newname
, bool is_dir
) {
41 DWORD flags
= is_dir
? SYMBOLIC_LINK_FLAG_DIRECTORY
: 0;
42 if (CreateSymbolicLinkA(newname
, oldname
,
43 flags
| SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
))
45 if (GetLastError() != ERROR_INVALID_PARAMETER
)
47 return !CreateSymbolicLinkA(newname
, oldname
, flags
);
49 inline int link(const char *oldname
, const char* newname
) {
50 return !CreateHardLinkA(newname
, oldname
, NULL
);
52 inline int setenv(const char *var
, const char *val
, int overwrite
) {
54 return ::_putenv((std::string(var
) + "=" + std::string(val
)).c_str());
56 inline int unsetenv(const char *var
) {
57 return ::_putenv((std::string(var
) + "=").c_str());
59 inline bool space(std::string path
, std::uintmax_t &capacity
,
60 std::uintmax_t &free
, std::uintmax_t &avail
) {
61 ULARGE_INTEGER FreeBytesAvailableToCaller
, TotalNumberOfBytes
,
62 TotalNumberOfFreeBytes
;
63 if (!GetDiskFreeSpaceExA(path
.c_str(), &FreeBytesAvailableToCaller
,
64 &TotalNumberOfBytes
, &TotalNumberOfFreeBytes
))
66 capacity
= TotalNumberOfBytes
.QuadPart
;
67 free
= TotalNumberOfFreeBytes
.QuadPart
;
68 avail
= FreeBytesAvailableToCaller
.QuadPart
;
76 inline int symlink(const char* oldname
, const char* newname
, bool is_dir
) { (void)is_dir
; return ::symlink(oldname
, newname
); }
80 inline bool space(std::string path
, std::uintmax_t &capacity
,
81 std::uintmax_t &free
, std::uintmax_t &avail
) {
82 struct statvfs expect
;
83 if (::statvfs(path
.c_str(), &expect
) == -1)
85 assert(expect
.f_bavail
> 0);
86 assert(expect
.f_bfree
> 0);
87 assert(expect
.f_bsize
> 0);
88 assert(expect
.f_blocks
> 0);
89 assert(expect
.f_frsize
> 0);
90 auto do_mult
= [&](std::uintmax_t val
) {
91 std::uintmax_t fsize
= expect
.f_frsize
;
92 std::uintmax_t new_val
= val
* fsize
;
93 assert(new_val
/ fsize
== val
); // Test for overflow
96 capacity
= do_mult(expect
.f_blocks
);
97 free
= do_mult(expect
.f_bfree
);
98 avail
= do_mult(expect
.f_bavail
);
103 // N.B. libc might define some of the foo[64] identifiers using macros from
104 // foo64 -> foo or vice versa.
106 using off64_t
= std::int64_t;
107 #elif defined(__MVS__) || defined(__LP64__)
108 using off64_t
= ::off_t
;
113 inline FILE* fopen64(const char* pathname
, const char* mode
) {
114 // Bionic does not distinguish between fopen and fopen64, but fopen64
115 // wasn't added until API 24.
116 #if defined(_WIN32) || defined(__MVS__) || defined(__LP64__) || defined(__BIONIC__)
117 return ::fopen(pathname
, mode
);
119 return ::fopen64(pathname
, mode
);
123 inline int ftruncate64(int fd
, off64_t length
) {
125 // _chsize_s sets errno on failure and also returns the error number.
126 return ::_chsize_s(fd
, length
) ? -1 : 0;
127 #elif defined(__MVS__) || defined(__LP64__)
128 return ::ftruncate(fd
, length
);
130 return ::ftruncate64(fd
, length
);
134 inline std::string
getcwd() {
135 // Assume that path lengths are not greater than this.
136 // This should be fine for testing purposes.
138 char* ret
= ::getcwd(buf
, sizeof(buf
));
139 assert(ret
&& "getcwd failed");
140 return std::string(ret
);
143 inline bool exists(std::string
const& path
) {
145 return ::stat(path
.c_str(), &tmp
) == 0;
147 } // end namespace utils
149 struct scoped_test_env
151 scoped_test_env() : test_root(available_cwd_path()) {
153 // Windows mkdir can create multiple recursive directories
155 std::string cmd
= "mkdir " + test_root
.string();
157 std::string cmd
= "mkdir -p " + test_root
.string();
159 int ret
= std::system(cmd
.c_str());
162 // Ensure that the root_path is fully resolved, i.e. it contains no
163 // symlinks. The filesystem tests depend on that. We do this after
164 // creating the root_path, because `fs::canonical` requires the
166 test_root
= fs::canonical(test_root
);
171 std::string cmd
= "rmdir /s /q " + test_root
.string();
172 int ret
= std::system(cmd
.c_str());
176 // The behaviour of chmod -R on z/OS prevents recursive
177 // permission change for directories that do not have read permission.
178 std::string cmd
= "find " + test_root
.string() + " -exec chmod 777 {} \\;";
180 std::string cmd
= "chmod -R 777 " + test_root
.string();
181 #endif // defined(__MVS__)
182 int ret
= std::system(cmd
.c_str());
183 # if !defined(_AIX) && !defined(__ANDROID__)
184 // On AIX the chmod command will return non-zero when trying to set
185 // the permissions on a directory that contains a bad symlink. This triggers
186 // the assert, despite being able to delete everything with the following
189 // Android's chmod was buggy in old OSs, but skipping this assert is
190 // sufficient to ensure that the `rm -rf` succeeds for almost all tests:
191 // - Android L: chmod aborts after one error
192 // - Android L and M: chmod -R tries to set permissions of a symlink
194 // LIBCXX-ANDROID-FIXME: Other fixes to consider: place a toybox chmod
195 // onto old devices, re-enable this assert for devices running Android N
196 // and up, rewrite this chmod+rm in C or C++.
200 cmd
= "rm -rf " + test_root
.string();
201 ret
= std::system(cmd
.c_str());
206 scoped_test_env(scoped_test_env
const &) = delete;
207 scoped_test_env
& operator=(scoped_test_env
const &) = delete;
209 fs::path
make_env_path(std::string p
) { return sanitize_path(p
); }
211 std::string
sanitize_path(std::string raw
) {
212 assert(raw
.find("..") == std::string::npos
);
213 std::string root
= test_root
.string();
214 if (root
.compare(0, root
.size(), raw
, 0, root
.size()) != 0) {
215 assert(raw
.front() != '\\');
216 fs::path
tmp(test_root
);
223 // Purposefully using a size potentially larger than off_t here so we can
224 // test the behavior of libc++fs when it is built with _FILE_OFFSET_BITS=64
225 // but the caller is not (std::filesystem also uses uintmax_t rather than
226 // off_t). On a 32-bit system this allows us to create a file larger than
228 std::string
create_file(fs::path filename_path
, std::uintmax_t size
= 0) {
229 std::string filename
= sanitize_path(filename_path
.string());
232 static_cast<typename
std::make_unsigned
<utils::off64_t
>::type
>(
233 std::numeric_limits
<utils::off64_t
>::max())) {
234 fprintf(stderr
, "create_file(%s, %ju) too large\n",
235 filename
.c_str(), size
);
239 #if defined(_WIN32) || defined(__MVS__)
240 # define FOPEN_CLOEXEC_FLAG ""
242 # define FOPEN_CLOEXEC_FLAG "e"
244 FILE* file
= utils::fopen64(filename
.c_str(), "w" FOPEN_CLOEXEC_FLAG
);
245 if (file
== nullptr) {
246 fprintf(stderr
, "fopen %s failed: %s\n", filename
.c_str(),
251 if (utils::ftruncate64(
252 fileno(file
), static_cast<utils::off64_t
>(size
)) == -1) {
253 fprintf(stderr
, "ftruncate %s %ju failed: %s\n", filename
.c_str(),
254 size
, strerror(errno
));
263 std::string
create_dir(fs::path filename_path
) {
264 std::string filename
= filename_path
.string();
265 filename
= sanitize_path(std::move(filename
));
266 int ret
= utils::mkdir(filename
.c_str(), 0777); // rwxrwxrwx mode
271 std::string
create_file_dir_symlink(fs::path source_path
,
273 bool sanitize_source
= true,
274 bool is_dir
= false) {
275 std::string source
= source_path
.string();
276 std::string to
= to_path
.string();
278 source
= sanitize_path(std::move(source
));
279 to
= sanitize_path(std::move(to
));
280 int ret
= utils::symlink(source
.c_str(), to
.c_str(), is_dir
);
285 std::string
create_symlink(fs::path source_path
,
287 bool sanitize_source
= true) {
288 return create_file_dir_symlink(source_path
, to_path
, sanitize_source
,
292 std::string
create_directory_symlink(fs::path source_path
,
294 bool sanitize_source
= true) {
295 return create_file_dir_symlink(source_path
, to_path
, sanitize_source
,
299 std::string
create_hardlink(fs::path source_path
, fs::path to_path
) {
300 std::string source
= source_path
.string();
301 std::string to
= to_path
.string();
302 source
= sanitize_path(std::move(source
));
303 to
= sanitize_path(std::move(to
));
304 int ret
= utils::link(source
.c_str(), to
.c_str());
310 std::string
create_fifo(std::string file
) {
311 file
= sanitize_path(std::move(file
));
312 int ret
= ::mkfifo(file
.c_str(), 0666); // rw-rw-rw- mode
318 // Some platforms doesn't support socket files so we shouldn't even
319 // allow tests to call this unguarded.
320 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
321 std::string
create_socket(std::string file
) {
322 file
= sanitize_path(std::move(file
));
324 ::sockaddr_un address
;
325 address
.sun_family
= AF_UNIX
;
326 assert(file
.size() <= sizeof(address
.sun_path
));
327 ::strncpy(address
.sun_path
, file
.c_str(), sizeof(address
.sun_path
));
328 int fd
= ::socket(AF_UNIX
, SOCK_STREAM
, 0);
329 ::bind(fd
, reinterpret_cast<::sockaddr
*>(&address
), sizeof(address
));
337 // This could potentially introduce a filesystem race if multiple
338 // scoped_test_envs were created concurrently in the same test (hence
339 // sharing the same cwd). However, it is fairly unlikely to happen as
340 // we generally don't use scoped_test_env from multiple threads, so
341 // this is deemed acceptable.
342 // The cwd.filename() itself isn't unique across all tests in the suite,
343 // so start the numbering from a hash of the full cwd, to avoid
344 // different tests interfering with each other.
345 static inline fs::path
available_cwd_path() {
346 fs::path
const cwd
= utils::getcwd();
347 fs::path
const tmp
= fs::temp_directory_path();
348 std::string base
= cwd
.filename().string();
349 std::size_t i
= std::hash
<std::string
>()(cwd
.string());
350 fs::path p
= tmp
/ (base
+ "-static_env." + std::to_string(i
));
351 while (utils::exists(p
.string())) {
352 p
= tmp
/ (base
+ "-static_env." + std::to_string(++i
));
358 /// This class generates the following tree:
361 /// |-- bad_symlink -> dne
368 /// | | `-- symlink_to_dir3 -> dir3
372 /// |-- non_empty_file
373 /// |-- symlink_to_dir -> dir1
374 /// `-- symlink_to_empty_file -> empty_file
376 class static_test_env
{
377 scoped_test_env env_
;
380 env_
.create_symlink("dne", "bad_symlink", false);
381 env_
.create_dir("dir1");
382 env_
.create_dir("dir1/dir2");
383 env_
.create_file("dir1/dir2/afile3");
384 env_
.create_dir("dir1/dir2/dir3");
385 env_
.create_file("dir1/dir2/dir3/file5");
386 env_
.create_file("dir1/dir2/file4");
387 env_
.create_directory_symlink("dir3", "dir1/dir2/symlink_to_dir3", false);
388 env_
.create_file("dir1/file1");
389 env_
.create_file("dir1/file2", 42);
390 env_
.create_file("empty_file");
391 env_
.create_file("non_empty_file", 42);
392 env_
.create_directory_symlink("dir1", "symlink_to_dir", false);
393 env_
.create_symlink("empty_file", "symlink_to_empty_file", false);
396 const fs::path Root
= env_
.test_root
;
398 fs::path
makePath(fs::path
const& p
) const {
399 // env_path is expected not to contain symlinks.
400 fs::path
const& env_path
= Root
;
404 const std::vector
<fs::path
> TestFileList
= {
405 makePath("empty_file"),
406 makePath("non_empty_file"),
407 makePath("dir1/file1"),
408 makePath("dir1/file2")
411 const std::vector
<fs::path
> TestDirList
= {
413 makePath("dir1/dir2"),
414 makePath("dir1/dir2/dir3")
417 const fs::path File
= TestFileList
[0];
418 const fs::path Dir
= TestDirList
[0];
419 const fs::path Dir2
= TestDirList
[1];
420 const fs::path Dir3
= TestDirList
[2];
421 const fs::path SymlinkToFile
= makePath("symlink_to_empty_file");
422 const fs::path SymlinkToDir
= makePath("symlink_to_dir");
423 const fs::path BadSymlink
= makePath("bad_symlink");
424 const fs::path DNE
= makePath("DNE");
425 const fs::path EmptyFile
= TestFileList
[0];
426 const fs::path NonEmptyFile
= TestFileList
[1];
427 const fs::path CharFile
= "/dev/null"; // Hopefully this exists
429 const std::vector
<fs::path
> DirIterationList
= {
430 makePath("dir1/dir2"),
431 makePath("dir1/file1"),
432 makePath("dir1/file2")
435 const std::vector
<fs::path
> DirIterationListDepth1
= {
436 makePath("dir1/dir2/afile3"),
437 makePath("dir1/dir2/dir3"),
438 makePath("dir1/dir2/symlink_to_dir3"),
439 makePath("dir1/dir2/file4"),
442 const std::vector
<fs::path
> RecDirIterationList
= {
443 makePath("dir1/dir2"),
444 makePath("dir1/file1"),
445 makePath("dir1/file2"),
446 makePath("dir1/dir2/afile3"),
447 makePath("dir1/dir2/dir3"),
448 makePath("dir1/dir2/symlink_to_dir3"),
449 makePath("dir1/dir2/file4"),
450 makePath("dir1/dir2/dir3/file5")
453 const std::vector
<fs::path
> RecDirFollowSymlinksIterationList
= {
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/file4"),
460 makePath("dir1/dir2/dir3/file5"),
461 makePath("dir1/dir2/symlink_to_dir3"),
462 makePath("dir1/dir2/symlink_to_dir3/file5"),
468 CWDGuard() : oldCwd_(utils::getcwd()) { }
470 int ret
= ::chdir(oldCwd_
.c_str());
471 assert(ret
== 0 && "chdir failed");
474 CWDGuard(CWDGuard
const&) = delete;
475 CWDGuard
& operator=(CWDGuard
const&) = delete;
478 // We often need to test that the error_code was cleared if no error occurs
479 // this function returns an error_code which is set to an error that will
480 // never be returned by the filesystem functions.
481 inline std::error_code
GetTestEC(unsigned Idx
= 0) {
483 auto GetErrc
= [&]() {
486 return errc::address_family_not_supported
;
488 return errc::address_not_available
;
490 return errc::address_in_use
;
492 return errc::argument_list_too_long
;
494 assert(false && "Idx out of range");
498 return std::make_error_code(GetErrc());
501 inline bool ErrorIsImp(const std::error_code
& ec
,
502 std::vector
<std::errc
> const& errors
) {
503 std::error_condition cond
= ec
.default_error_condition();
504 for (auto errc
: errors
) {
505 if (cond
.value() == static_cast<int>(errc
))
511 template <class... ErrcT
>
512 inline bool ErrorIs(const std::error_code
& ec
, std::errc First
, ErrcT
... Rest
) {
513 std::vector
<std::errc
> errors
= {First
, Rest
...};
514 return ErrorIsImp(ec
, errors
);
517 // Provide our own Sleep routine since std::this_thread::sleep_for is not
518 // available in single-threaded mode.
519 template <class Dur
> void SleepFor(Dur dur
) {
520 using namespace std::chrono
;
521 #if defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
522 using Clock
= system_clock
;
524 using Clock
= steady_clock
;
526 const auto wake_time
= Clock::now() + dur
;
527 while (Clock::now() < wake_time
)
531 inline fs::perms
NormalizeExpectedPerms(fs::perms P
) {
533 // On Windows, fs::perms only maps down to one bit stored in the filesystem,
534 // a boolean readonly flag.
535 // Normalize permissions to the format it gets returned; all fs entries are
536 // read+exec for all users; writable ones also have the write bit set for
538 P
|= fs::perms::owner_read
| fs::perms::group_read
| fs::perms::others_read
;
539 P
|= fs::perms::owner_exec
| fs::perms::group_exec
| fs::perms::others_exec
;
541 fs::perms::owner_write
| fs::perms::group_write
| fs::perms::others_write
;
542 if ((P
& Write
) != fs::perms::none
)
548 struct ExceptionChecker
{
549 std::errc expected_err
;
550 fs::path expected_path1
;
551 fs::path expected_path2
;
553 const char* func_name
;
554 std::string opt_message
;
556 explicit ExceptionChecker(std::errc first_err
, const char* fun_name
,
557 std::string opt_msg
= {})
558 : expected_err
{first_err
}, num_paths(0), func_name(fun_name
),
559 opt_message(opt_msg
) {}
560 explicit ExceptionChecker(fs::path p
, std::errc first_err
,
561 const char* fun_name
, std::string opt_msg
= {})
562 : expected_err(first_err
), expected_path1(p
), num_paths(1),
563 func_name(fun_name
), opt_message(opt_msg
) {}
565 explicit ExceptionChecker(fs::path p1
, fs::path p2
, std::errc first_err
,
566 const char* fun_name
, std::string opt_msg
= {})
567 : expected_err(first_err
), expected_path1(p1
), expected_path2(p2
),
568 num_paths(2), func_name(fun_name
), opt_message(opt_msg
) {}
570 void operator()(fs::filesystem_error
const& Err
) {
571 assert(ErrorIsImp(Err
.code(), {expected_err
}));
572 assert(Err
.path1() == expected_path1
);
573 assert(Err
.path2() == expected_path2
);
574 LIBCPP_ONLY(check_libcxx_string(Err
));
577 void check_libcxx_string(fs::filesystem_error
const& Err
) {
578 std::string message
= std::make_error_code(expected_err
).message();
580 std::string additional_msg
= "";
581 if (!opt_message
.empty()) {
582 additional_msg
= opt_message
+ ": ";
584 auto transform_path
= [](const fs::path
& p
) {
585 return "\"" + p
.string() + "\"";
587 std::string format
= [&]() -> std::string
{
590 return format_string("filesystem error: in %s: %s%s", func_name
,
591 additional_msg
, message
);
593 return format_string("filesystem error: in %s: %s%s [%s]", func_name
,
594 additional_msg
, message
,
595 transform_path(expected_path1
).c_str());
597 return format_string("filesystem error: in %s: %s%s [%s] [%s]",
598 func_name
, additional_msg
, message
,
599 transform_path(expected_path1
).c_str(),
600 transform_path(expected_path2
).c_str());
602 TEST_FAIL("unexpected case");
606 assert(format
== Err
.what());
607 if (format
!= Err
.what()) {
609 "filesystem_error::what() does not match expected output:\n");
610 fprintf(stderr
, " expected: \"%s\"\n", format
.c_str());
611 fprintf(stderr
, " actual: \"%s\"\n\n", Err
.what());
615 ExceptionChecker(ExceptionChecker
const&) = delete;
616 ExceptionChecker
& operator=(ExceptionChecker
const&) = delete;
620 inline fs::path
GetWindowsInaccessibleDir() {
621 // Only makes sense on windows, but the code can be compiled for
623 const fs::path
dir("C:\\System Volume Information");
625 const fs::path
root("C:\\");
626 for (const auto &ent
: fs::directory_iterator(root
, ec
)) {
629 // Basic sanity checks on the directory_entry
630 if (!ent
.exists() || !ent
.is_directory()) {
631 fprintf(stderr
, "The expected inaccessible directory \"%s\" was found "
632 "but doesn't behave as expected, skipping tests "
633 "regarding it\n", dir
.string().c_str());
636 // Check that it indeed is inaccessible as expected
637 (void)fs::exists(ent
, ec
);
639 fprintf(stderr
, "The expected inaccessible directory \"%s\" was found "
640 "but seems to be accessible, skipping tests "
641 "regarding it\n", dir
.string().c_str());
646 fprintf(stderr
, "No inaccessible directory \"%s\" found, skipping tests "
647 "regarding it\n", dir
.string().c_str());
651 #endif /* FILESYSTEM_TEST_HELPER_H */