Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / support / filesystem_test_helper.h
blobf924f909fe150736192e1993192acaccc141a002
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
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
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>
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>
34 # include <sys/un.h>
35 #endif
37 namespace utils {
38 #ifdef _WIN32
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))
44 return 0;
45 if (GetLastError() != ERROR_INVALID_PARAMETER)
46 return 1;
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) {
53 (void)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))
65 return false;
66 capacity = TotalNumberOfBytes.QuadPart;
67 free = TotalNumberOfFreeBytes.QuadPart;
68 avail = FreeBytesAvailableToCaller.QuadPart;
69 assert(capacity > 0);
70 assert(free > 0);
71 assert(avail > 0);
72 return true;
74 #else
75 using ::mkdir;
76 inline int symlink(const char* oldname, const char* newname, bool is_dir) { (void)is_dir; return ::symlink(oldname, newname); }
77 using ::link;
78 using ::setenv;
79 using ::unsetenv;
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)
84 return false;
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
94 return new_val;
96 capacity = do_mult(expect.f_blocks);
97 free = do_mult(expect.f_bfree);
98 avail = do_mult(expect.f_bavail);
99 return true;
101 #endif
103 // N.B. libc might define some of the foo[64] identifiers using macros from
104 // foo64 -> foo or vice versa.
105 #if defined(_WIN32)
106 using off64_t = std::int64_t;
107 #elif defined(__MVS__) || defined(__LP64__)
108 using off64_t = ::off_t;
109 #else
110 using ::off64_t;
111 #endif
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);
118 #else
119 return ::fopen64(pathname, mode);
120 #endif
123 inline int ftruncate64(int fd, off64_t length) {
124 #if defined(_WIN32)
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);
129 #else
130 return ::ftruncate64(fd, length);
131 #endif
134 inline std::string getcwd() {
135 // Assume that path lengths are not greater than this.
136 // This should be fine for testing purposes.
137 char buf[4096];
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) {
144 struct ::stat tmp;
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()) {
152 #ifdef _WIN32
153 // Windows mkdir can create multiple recursive directories
154 // if needed.
155 std::string cmd = "mkdir " + test_root.string();
156 #else
157 std::string cmd = "mkdir -p " + test_root.string();
158 #endif
159 int ret = std::system(cmd.c_str());
160 assert(ret == 0);
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
165 // path to exist.
166 test_root = fs::canonical(test_root);
169 ~scoped_test_env() {
170 #ifdef _WIN32
171 std::string cmd = "rmdir /s /q " + test_root.string();
172 int ret = std::system(cmd.c_str());
173 assert(ret == 0);
174 #else
175 #if defined(__MVS__)
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 {} \\;";
179 #else
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
187 // `rm -r` command.
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
193 // target.
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++.
197 assert(ret == 0);
198 # endif
200 cmd = "rm -rf " + test_root.string();
201 ret = std::system(cmd.c_str());
202 assert(ret == 0);
203 #endif
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);
217 tmp /= raw;
218 return tmp.string();
220 return raw;
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
227 // 2GB.
228 std::string create_file(fs::path filename_path, std::uintmax_t size = 0) {
229 std::string filename = sanitize_path(filename_path.string());
231 if (size >
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);
236 abort();
239 #if defined(_WIN32) || defined(__MVS__)
240 # define FOPEN_CLOEXEC_FLAG ""
241 #else
242 # define FOPEN_CLOEXEC_FLAG "e"
243 #endif
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(),
247 strerror(errno));
248 abort();
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));
255 fclose(file);
256 abort();
259 fclose(file);
260 return filename;
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
267 assert(ret == 0);
268 return filename;
271 std::string create_file_dir_symlink(fs::path source_path,
272 fs::path to_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();
277 if (sanitize_source)
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);
281 assert(ret == 0);
282 return to;
285 std::string create_symlink(fs::path source_path,
286 fs::path to_path,
287 bool sanitize_source = true) {
288 return create_file_dir_symlink(source_path, to_path, sanitize_source,
289 false);
292 std::string create_directory_symlink(fs::path source_path,
293 fs::path to_path,
294 bool sanitize_source = true) {
295 return create_file_dir_symlink(source_path, to_path, sanitize_source,
296 true);
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());
305 assert(ret == 0);
306 return to;
309 #ifndef _WIN32
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
313 assert(ret == 0);
314 return file;
316 #endif
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));
330 return file;
332 #endif
334 fs::path test_root;
336 private:
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));
354 return p;
358 /// This class generates the following tree:
360 /// static_test_env
361 /// |-- bad_symlink -> dne
362 /// |-- dir1
363 /// | |-- dir2
364 /// | | |-- afile3
365 /// | | |-- dir3
366 /// | | | `-- file5
367 /// | | |-- file4
368 /// | | `-- symlink_to_dir3 -> dir3
369 /// | `-- file1
370 /// | `-- file2
371 /// |-- empty_file
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_;
378 public:
379 static_test_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;
401 return env_path / p;
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 = {
412 makePath("dir1"),
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"),
466 struct CWDGuard {
467 std::string oldCwd_;
468 CWDGuard() : oldCwd_(utils::getcwd()) { }
469 ~CWDGuard() {
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) {
482 using std::errc;
483 auto GetErrc = [&]() {
484 switch (Idx) {
485 case 0:
486 return errc::address_family_not_supported;
487 case 1:
488 return errc::address_not_available;
489 case 2:
490 return errc::address_in_use;
491 case 3:
492 return errc::argument_list_too_long;
493 default:
494 assert(false && "Idx out of range");
495 std::abort();
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))
506 return true;
508 return false;
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;
523 #else
524 using Clock = steady_clock;
525 #endif
526 const auto wake_time = Clock::now() + dur;
527 while (Clock::now() < wake_time)
531 inline fs::perms NormalizeExpectedPerms(fs::perms P) {
532 #ifdef _WIN32
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
537 // all users.
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;
540 fs::perms Write =
541 fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
542 if ((P & Write) != fs::perms::none)
543 P |= Write;
544 #endif
545 return P;
548 struct ExceptionChecker {
549 std::errc expected_err;
550 fs::path expected_path1;
551 fs::path expected_path2;
552 unsigned num_paths;
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 {
588 switch (num_paths) {
589 case 0:
590 return format_string("filesystem error: in %s: %s%s", func_name,
591 additional_msg, message);
592 case 1:
593 return format_string("filesystem error: in %s: %s%s [%s]", func_name,
594 additional_msg, message,
595 transform_path(expected_path1).c_str());
596 case 2:
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());
601 default:
602 TEST_FAIL("unexpected case");
603 return "";
605 }();
606 assert(format == Err.what());
607 if (format != Err.what()) {
608 fprintf(stderr,
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
622 // any platform.
623 const fs::path dir("C:\\System Volume Information");
624 std::error_code ec;
625 const fs::path root("C:\\");
626 for (const auto &ent : fs::directory_iterator(root, ec)) {
627 if (ent != dir)
628 continue;
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());
634 return fs::path();
636 // Check that it indeed is inaccessible as expected
637 (void)fs::exists(ent, ec);
638 if (!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());
642 return fs::path();
644 return ent;
646 fprintf(stderr, "No inaccessible directory \"%s\" found, skipping tests "
647 "regarding it\n", dir.string().c_str());
648 return fs::path();
651 #endif /* FILESYSTEM_TEST_HELPER_H */