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
18 #include <cstdio> // for printf
20 #include <system_error>
23 #include "make_string.h"
24 #include "test_macros.h"
25 #include "rapid-cxx-test.h"
26 #include "format_string.h"
28 // For creating socket files
29 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
30 # include <sys/socket.h>
36 inline int mkdir(const char* path
, int mode
) { (void)mode
; return ::_mkdir(path
); }
37 inline int ftruncate(int fd
, off_t length
) { return ::_chsize(fd
, length
); }
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
))
43 if (GetLastError() != ERROR_INVALID_PARAMETER
)
45 return !CreateSymbolicLinkA(newname
, oldname
, flags
);
47 inline int link(const char *oldname
, const char* newname
) {
48 return !CreateHardLinkA(newname
, oldname
, NULL
);
50 inline int setenv(const char *var
, const char *val
, int overwrite
) {
52 return ::_putenv((std::string(var
) + "=" + std::string(val
)).c_str());
54 inline int unsetenv(const char *var
) {
55 return ::_putenv((std::string(var
) + "=").c_str());
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
))
64 capacity
= TotalNumberOfBytes
.QuadPart
;
65 free
= TotalNumberOfFreeBytes
.QuadPart
;
66 avail
= FreeBytesAvailableToCaller
.QuadPart
;
75 inline int symlink(const char* oldname
, const char* newname
, bool is_dir
) { (void)is_dir
; return ::symlink(oldname
, newname
); }
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)
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
95 capacity
= do_mult(expect
.f_blocks
);
96 free
= do_mult(expect
.f_bfree
);
97 avail
= do_mult(expect
.f_bavail
);
102 inline std::string
getcwd() {
103 // Assume that path lengths are not greater than this.
104 // This should be fine for testing purposes.
106 char* ret
= ::getcwd(buf
, sizeof(buf
));
107 assert(ret
&& "getcwd failed");
108 return std::string(ret
);
111 inline bool exists(std::string
const& path
) {
113 return ::stat(path
.c_str(), &tmp
) == 0;
115 } // end namespace utils
117 struct scoped_test_env
119 scoped_test_env() : test_root(available_cwd_path()) {
121 // Windows mkdir can create multiple recursive directories
123 std::string cmd
= "mkdir " + test_root
.string();
125 std::string cmd
= "mkdir -p " + test_root
.string();
127 int ret
= std::system(cmd
.c_str());
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
134 test_root
= fs::canonical(test_root
);
139 std::string cmd
= "rmdir /s /q " + test_root
.string();
140 int ret
= std::system(cmd
.c_str());
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 {} \\;";
148 std::string cmd
= "chmod -R 777 " + test_root
.string();
149 #endif // defined(__MVS__)
150 int ret
= std::system(cmd
.c_str());
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
159 cmd
= "rm -rf " + test_root
.string();
160 ret
= std::system(cmd
.c_str());
165 scoped_test_env(scoped_test_env
const &) = delete;
166 scoped_test_env
& operator=(scoped_test_env
const &) = delete;
168 fs::path
make_env_path(std::string p
) { return sanitize_path(p
); }
170 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
);
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
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
;
194 auto large_file_fopen
= fopen64
;
195 auto large_file_ftruncate
= ftruncate64
;
196 using large_file_offset_t
= off64_t
;
199 filename
= sanitize_path(std::move(filename
));
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
);
209 #if defined(_WIN32) || defined(__MVS__)
210 # define FOPEN_CLOEXEC_FLAG ""
212 # define FOPEN_CLOEXEC_FLAG "e"
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(),
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
));
233 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
241 std::string
create_file_dir_symlink(fs::path source_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();
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
);
255 std::string
create_symlink(fs::path source_path
,
257 bool sanitize_source
= true) {
258 return create_file_dir_symlink(source_path
, to_path
, sanitize_source
,
262 std::string
create_directory_symlink(fs::path source_path
,
264 bool sanitize_source
= true) {
265 return create_file_dir_symlink(source_path
, to_path
, sanitize_source
,
269 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());
280 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
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)
291 std::string
create_socket(std::string file
) {
292 file
= sanitize_path(std::move(file
));
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
));
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.
315 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
));
328 /// This class generates the following tree:
331 /// ├── bad_symlink -> dne
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
346 class static_test_env
{
347 scoped_test_env 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);
366 const fs::path Root
= env_
.test_root
;
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
;
374 const std::vector
<fs::path
> TestFileList
= {
375 makePath("empty_file"),
376 makePath("non_empty_file"),
377 makePath("dir1/file1"),
378 makePath("dir1/file2")
381 const std::vector
<fs::path
> TestDirList
= {
383 makePath("dir1/dir2"),
384 makePath("dir1/dir2/dir3")
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
399 const std::vector
<fs::path
> DirIterationList
= {
400 makePath("dir1/dir2"),
401 makePath("dir1/file1"),
402 makePath("dir1/file2")
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"),
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")
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"),
438 CWDGuard() : oldCwd_(utils::getcwd()) { }
440 int ret
= ::chdir(oldCwd_
.c_str());
441 assert(ret
== 0 && "chdir failed");
444 CWDGuard(CWDGuard
const&) = delete;
445 CWDGuard
& operator=(CWDGuard
const&) = delete;
450 const MultiStringType PathList
[] = {
466 MKSTR("///foo///bar"),
490 MKSTR("c:/foo\\bar"),
492 MKSTR("/finally/we/need/one/really/really/really/really/really/really/really/long/string")
494 const unsigned PathListSize
= sizeof(PathList
) / sizeof(MultiStringType
);
496 template <class Iter
>
497 Iter
IterEnd(Iter B
) {
498 using VT
= typename
std::iterator_traits
<Iter
>::value_type
;
499 for (; *B
!= VT
{}; ++B
)
504 template <class CharT
>
505 const CharT
* StrEnd(CharT
const* P
) {
509 template <class CharT
>
510 std::size_t StrLen(CharT
const* P
) {
511 return StrEnd(P
) - P
;
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.
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
);
522 template <class Iter1
, class Iter2
>
523 bool checkCollectionsEqual(
524 Iter1 start1
, Iter1
const end1
525 , Iter2 start2
, Iter2
const end2
528 while (start1
!= end1
&& start2
!= end2
) {
529 if (*start1
!= *start2
) {
534 return (start1
== end1
&& start2
== end2
);
538 template <class Iter1
, class Iter2
>
539 bool checkCollectionsEqualBackwards(
540 Iter1
const start1
, Iter1 end1
541 , Iter2
const start2
, Iter2 end2
544 while (start1
!= end1
&& start2
!= end2
) {
546 if (*end1
!= *end2
) {
550 return (start1
== end1
&& start2
== end2
);
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) {
558 auto GetErrc
= [&]() {
561 return errc::address_family_not_supported
;
563 return errc::address_not_available
;
565 return errc::address_in_use
;
567 return errc::argument_list_too_long
;
569 assert(false && "Idx out of range");
573 return std::make_error_code(GetErrc());
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
))
586 template <class... ErrcT
>
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
);
592 // Provide our own Sleep routine since std::this_thread::sleep_for is not
593 // available in single-threaded mode.
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
;
599 using Clock
= steady_clock
;
601 const auto wake_time
= Clock::now() + dur
;
602 while (Clock::now() < wake_time
)
606 inline bool PathEq(fs::path
const& LHS
, fs::path
const& RHS
) {
607 return LHS
.native() == RHS
.native();
610 inline bool PathEqIgnoreSep(fs::path LHS
, fs::path RHS
) {
611 LHS
.make_preferred();
612 RHS
.make_preferred();
613 return LHS
.native() == RHS
.native();
616 inline fs::perms
NormalizeExpectedPerms(fs::perms P
) {
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
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
;
626 fs::perms::owner_write
| fs::perms::group_write
| fs::perms::others_write
;
627 if ((P
& Write
) != fs::perms::none
)
633 struct ExceptionChecker
{
634 std::errc expected_err
;
635 fs::path expected_path1
;
636 fs::path expected_path2
;
638 const char* func_name
;
639 std::string opt_message
;
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
= {})
647 : expected_err(first_err
), expected_path1(p
), num_paths(1),
648 func_name(fun_name
), opt_message(opt_msg
) {}
650 explicit ExceptionChecker(fs::path p1
, fs::path p2
, std::errc first_err
,
651 const char* fun_name
, std::string opt_msg
= {})
652 : expected_err(first_err
), expected_path1(p1
), expected_path2(p2
),
653 num_paths(2), func_name(fun_name
), opt_message(opt_msg
) {}
655 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
));
662 void check_libcxx_string(fs::filesystem_error
const& Err
) {
663 std::string message
= std::make_error_code(expected_err
).message();
665 std::string additional_msg
= "";
666 if (!opt_message
.empty()) {
667 additional_msg
= opt_message
+ ": ";
669 auto transform_path
= [](const fs::path
& p
) {
670 return "\"" + p
.string() + "\"";
672 std::string format
= [&]() -> std::string
{
675 return format_string("filesystem error: in %s: %s%s", func_name
,
676 additional_msg
, message
);
678 return format_string("filesystem error: in %s: %s%s [%s]", func_name
,
679 additional_msg
, message
,
680 transform_path(expected_path1
).c_str());
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());
687 TEST_CHECK(false && "unexpected case");
691 TEST_CHECK(format
== Err
.what());
692 if (format
!= Err
.what()) {
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());
700 ExceptionChecker(ExceptionChecker
const&) = delete;
701 ExceptionChecker
& operator=(ExceptionChecker
const&) = delete;
705 inline fs::path
GetWindowsInaccessibleDir() {
706 // Only makes sense on windows, but the code can be compiled for
708 const fs::path
dir("C:\\System Volume Information");
710 const fs::path
root("C:\\");
711 for (const auto &ent
: fs::directory_iterator(root
, ec
)) {
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());
721 // Check that it indeed is inaccessible as expected
722 (void)fs::exists(ent
, 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());
731 fprintf(stderr
, "No inaccessible directory \"%s\" found, skipping tests "
732 "regarding it\n", dir
.string().c_str());
736 #endif /* FILESYSTEM_TEST_HELPER_H */