blob: e3f46a16af3383103e3750a8e518bec7fc1b24ad [file] [log] [blame]
Eric Fiselierc7979582016-06-17 19:46:40 +00001#ifndef FILESYSTEM_TEST_HELPER_HPP
2#define FILESYSTEM_TEST_HELPER_HPP
3
Eric Fiselier19aae8f2018-03-26 05:46:57 +00004#include "filesystem_include.hpp"
Eric Fiselierc7979582016-06-17 19:46:40 +00005#include <cassert>
6#include <cstdio> // for printf
7#include <string>
8#include <fstream>
9#include <random>
Eric Fiselier93e32122016-06-17 21:44:26 +000010#include <chrono>
Eric Fiselierc1699862018-07-20 01:22:32 +000011#include <vector>
12
13#include "rapid-cxx-test.hpp"
Eric Fiselierc7979582016-06-17 19:46:40 +000014
Eric Fiselierc7979582016-06-17 19:46:40 +000015// static test helpers
16
17#ifndef LIBCXX_FILESYSTEM_STATIC_TEST_ROOT
18#warning "STATIC TESTS DISABLED"
19#else // LIBCXX_FILESYSTEM_STATIC_TEST_ROOT
20
21namespace StaticEnv {
22
23inline fs::path makePath(fs::path const& p) {
Eric Fiselier48bc8502016-06-21 21:54:23 +000024 // env_path is expected not to contain symlinks.
Eric Fiselierc7979582016-06-17 19:46:40 +000025 static const fs::path env_path = LIBCXX_FILESYSTEM_STATIC_TEST_ROOT;
26 return env_path / p;
27}
28
29static const fs::path Root = LIBCXX_FILESYSTEM_STATIC_TEST_ROOT;
30
31static const fs::path TestFileList[] = {
32 makePath("empty_file"),
33 makePath("non_empty_file"),
34 makePath("dir1/file1"),
35 makePath("dir1/file2")
36};
37const std::size_t TestFileListSize = sizeof(TestFileList) / sizeof(fs::path);
38
39static const fs::path TestDirList[] = {
40 makePath("dir1"),
41 makePath("dir1/dir2"),
42 makePath("dir1/dir2/dir3")
43};
44const std::size_t TestDirListSize = sizeof(TestDirList) / sizeof(fs::path);
45
46static const fs::path File = TestFileList[0];
47static const fs::path Dir = TestDirList[0];
48static const fs::path Dir2 = TestDirList[1];
49static const fs::path Dir3 = TestDirList[2];
50static const fs::path SymlinkToFile = makePath("symlink_to_empty_file");
51static const fs::path SymlinkToDir = makePath("symlink_to_dir");
52static const fs::path BadSymlink = makePath("bad_symlink");
53static const fs::path DNE = makePath("DNE");
54static const fs::path EmptyFile = TestFileList[0];
55static const fs::path NonEmptyFile = TestFileList[1];
56static const fs::path CharFile = "/dev/null"; // Hopefully this exists
57
58static const fs::path DirIterationList[] = {
59 makePath("dir1/dir2"),
60 makePath("dir1/file1"),
61 makePath("dir1/file2")
62};
63const std::size_t DirIterationListSize = sizeof(DirIterationList)
64 / sizeof(fs::path);
65
66static const fs::path DirIterationListDepth1[] = {
67 makePath("dir1/dir2/afile3"),
68 makePath("dir1/dir2/dir3"),
69 makePath("dir1/dir2/symlink_to_dir3"),
70 makePath("dir1/dir2/file4"),
71};
72
73static const fs::path RecDirIterationList[] = {
74 makePath("dir1/dir2"),
75 makePath("dir1/file1"),
76 makePath("dir1/file2"),
77 makePath("dir1/dir2/afile3"),
78 makePath("dir1/dir2/dir3"),
79 makePath("dir1/dir2/symlink_to_dir3"),
80 makePath("dir1/dir2/file4"),
81 makePath("dir1/dir2/dir3/file5")
82};
83
84static const fs::path RecDirFollowSymlinksIterationList[] = {
85 makePath("dir1/dir2"),
86 makePath("dir1/file1"),
87 makePath("dir1/file2"),
88 makePath("dir1/dir2/afile3"),
89 makePath("dir1/dir2/dir3"),
90 makePath("dir1/dir2/file4"),
91 makePath("dir1/dir2/dir3/file5"),
92 makePath("dir1/dir2/symlink_to_dir3"),
93 makePath("dir1/dir2/symlink_to_dir3/file5"),
94};
95
96} // namespace StaticEnv
97
98#endif // LIBCXX_FILESYSTEM_STATIC_TEST_ROOT
99
100#ifndef LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT
101#warning LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT must be defined
102#else // LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT
103
104#ifndef LIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER
105#error LIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER must be defined
106#endif
107
108struct scoped_test_env
109{
110 scoped_test_env() : test_root(random_env_path())
111 { fs_helper_run(fs_make_cmd("init_test_directory", test_root)); }
112
113 ~scoped_test_env()
114 { fs_helper_run(fs_make_cmd("destroy_test_directory", test_root)); }
115
116 scoped_test_env(scoped_test_env const &) = delete;
117 scoped_test_env & operator=(scoped_test_env const &) = delete;
118
119 fs::path make_env_path(std::string p) { return sanitize_path(p); }
120
121 std::string sanitize_path(std::string raw) {
122 assert(raw.find("..") == std::string::npos);
123 std::string const& root = test_root.native();
124 if (root.compare(0, root.size(), raw, 0, root.size()) != 0) {
125 assert(raw.front() != '\\');
126 fs::path tmp(test_root);
127 tmp /= raw;
128 return std::move(const_cast<std::string&>(tmp.native()));
129 }
130 return raw;
131 }
132
133 std::string create_file(std::string filename, std::size_t size = 0) {
134 filename = sanitize_path(std::move(filename));
135 std::string out_str(size, 'a');
136 {
137 std::ofstream out(filename.c_str());
138 out << out_str;
139 }
140 return filename;
141 }
142
143 std::string create_dir(std::string filename) {
144 filename = sanitize_path(std::move(filename));
145 fs_helper_run(fs_make_cmd("create_dir", filename));
146 return filename;
147 }
148
149 std::string create_symlink(std::string source, std::string to) {
150 source = sanitize_path(std::move(source));
151 to = sanitize_path(std::move(to));
152 fs_helper_run(fs_make_cmd("create_symlink", source, to));
153 return to;
154 }
155
156 std::string create_hardlink(std::string source, std::string to) {
157 source = sanitize_path(std::move(source));
158 to = sanitize_path(std::move(to));
159 fs_helper_run(fs_make_cmd("create_hardlink", source, to));
160 return to;
161 }
162
163 std::string create_fifo(std::string file) {
164 file = sanitize_path(std::move(file));
165 fs_helper_run(fs_make_cmd("create_fifo", file));
166 return file;
167 }
168
169 // OS X and FreeBSD doesn't support socket files so we shouldn't even
170 // allow tests to call this unguarded.
171#if !defined(__FreeBSD__) && !defined(__APPLE__)
172 std::string create_socket(std::string file) {
173 file = sanitize_path(std::move(file));
174 fs_helper_run(fs_make_cmd("create_socket", file));
175 return file;
176 }
177#endif
178
179 fs::path const test_root;
180
181private:
182 static char to_hex(int ch) {
183 return ch < 10 ? static_cast<char>('0' + ch)
184 : static_cast<char>('a' + (ch - 10));
185 }
186
187 static char random_hex_char() {
188 static std::mt19937 rd { std::random_device{}() };
189 static std::uniform_int_distribution<int> mrand{0, 15};
190 return to_hex( mrand(rd) );
191 }
192
193 static std::string unique_path_suffix() {
194 std::string model = "test.%%%%%%";
195 for (auto & ch : model) {
196 if (ch == '%') ch = random_hex_char();
197 }
198 return model;
199 }
200
201 // This could potentially introduce a filesystem race with other tests
202 // running at the same time, but oh well, it's just test code.
203 static inline fs::path random_env_path() {
204 static const char* env_path = LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT;
205 fs::path p = fs::path(env_path) / unique_path_suffix();
206 assert(p.parent_path() == env_path);
207 return p;
208 }
209
210 static inline std::string make_arg(std::string const& arg) {
211 return "'" + arg + "'";
212 }
213
214 static inline std::string make_arg(std::size_t arg) {
215 return std::to_string(arg);
216 }
217
218 template <class T>
219 static inline std::string
220 fs_make_cmd(std::string const& cmd_name, T const& arg) {
221 return cmd_name + "(" + make_arg(arg) + ")";
222 }
223
224 template <class T, class U>
225 static inline std::string
226 fs_make_cmd(std::string const& cmd_name, T const& arg1, U const& arg2) {
227 return cmd_name + "(" + make_arg(arg1) + ", " + make_arg(arg2) + ")";
228 }
229
230 static inline void fs_helper_run(std::string const& raw_cmd) {
Eric Fiselier78046e42017-05-09 23:47:20 +0000231 // check that the fs test root in the environment matches what we were
Eric Fiselierc7979582016-06-17 19:46:40 +0000232 // compiled with.
233 static bool checked = checkDynamicTestRoot();
Eric Fiselierfd838222016-12-23 23:37:52 +0000234 ((void)checked);
Eric Fiselierc7979582016-06-17 19:46:40 +0000235 std::string cmd = LIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER;
236 cmd += " \"" + raw_cmd + "\"";
237 int ret = std::system(cmd.c_str());
238 assert(ret == 0);
239 }
240
241 static bool checkDynamicTestRoot() {
Eric Fiselier48bc8502016-06-21 21:54:23 +0000242 // LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT is expected not to contain symlinks.
Eric Fiselierc7979582016-06-17 19:46:40 +0000243 char* fs_root = std::getenv("LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT");
244 if (!fs_root) {
245 std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT must be a defined "
246 "environment variable when running the test.\n");
247 std::abort();
248 }
249 if (std::string(fs_root) != LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT) {
Eric Fiselier78046e42017-05-09 23:47:20 +0000250 std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT environment variable"
Eric Fiselierc7979582016-06-17 19:46:40 +0000251 " must have the same value as when the test was compiled.\n");
252 std::printf(" Current Value: '%s'\n", fs_root);
253 std::printf(" Expected Value: '%s'\n", LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT);
254 std::abort();
255 }
256 return true;
257 }
258
259};
260
261#endif // LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT
262
263// Misc test types
264
265#define CONCAT2(LHS, RHS) LHS##RHS
266#define CONCAT(LHS, RHS) CONCAT2(LHS, RHS)
267#define MKSTR(Str) {Str, CONCAT(L, Str), CONCAT(u, Str), CONCAT(U, Str)}
268
269struct MultiStringType {
270 const char* s;
271 const wchar_t* w;
272 const char16_t* u16;
273 const char32_t* u32;
274
275 operator const char* () const { return s; }
276 operator const wchar_t* () const { return w; }
277 operator const char16_t* () const { return u16; }
278 operator const char32_t* () const { return u32; }
279};
280
281const MultiStringType PathList[] = {
282 MKSTR(""),
283 MKSTR(" "),
284 MKSTR("//"),
285 MKSTR("."),
286 MKSTR(".."),
287 MKSTR("foo"),
288 MKSTR("/"),
289 MKSTR("/foo"),
290 MKSTR("foo/"),
291 MKSTR("/foo/"),
292 MKSTR("foo/bar"),
293 MKSTR("/foo/bar"),
294 MKSTR("//net"),
295 MKSTR("//net/foo"),
296 MKSTR("///foo///"),
297 MKSTR("///foo///bar"),
298 MKSTR("/."),
299 MKSTR("./"),
300 MKSTR("/.."),
301 MKSTR("../"),
302 MKSTR("foo/."),
303 MKSTR("foo/.."),
304 MKSTR("foo/./"),
305 MKSTR("foo/./bar"),
306 MKSTR("foo/../"),
307 MKSTR("foo/../bar"),
308 MKSTR("c:"),
309 MKSTR("c:/"),
310 MKSTR("c:foo"),
311 MKSTR("c:/foo"),
312 MKSTR("c:foo/"),
313 MKSTR("c:/foo/"),
314 MKSTR("c:/foo/bar"),
315 MKSTR("prn:"),
316 MKSTR("c:\\"),
317 MKSTR("c:\\foo"),
318 MKSTR("c:foo\\"),
319 MKSTR("c:\\foo\\"),
320 MKSTR("c:\\foo/"),
321 MKSTR("c:/foo\\bar"),
322 MKSTR("//"),
323 MKSTR("/finally/we/need/one/really/really/really/really/really/really/really/long/string")
324};
325const unsigned PathListSize = sizeof(PathList) / sizeof(MultiStringType);
326
327template <class Iter>
328Iter IterEnd(Iter B) {
329 using VT = typename std::iterator_traits<Iter>::value_type;
330 for (; *B != VT{}; ++B)
331 ;
332 return B;
333}
334
335template <class CharT>
336const CharT* StrEnd(CharT const* P) {
337 return IterEnd(P);
338}
339
340template <class CharT>
341std::size_t StrLen(CharT const* P) {
342 return StrEnd(P) - P;
343}
344
345// Testing the allocation behavior of the code_cvt functions requires
346// *knowing* that the allocation was not done by "path::__str_".
347// This hack forces path to allocate enough memory.
348inline void PathReserve(fs::path& p, std::size_t N) {
349 auto const& native_ref = p.native();
350 const_cast<std::string&>(native_ref).reserve(N);
351}
352
353template <class Iter1, class Iter2>
354bool checkCollectionsEqual(
355 Iter1 start1, Iter1 const end1
356 , Iter2 start2, Iter2 const end2
357 )
358{
359 while (start1 != end1 && start2 != end2) {
360 if (*start1 != *start2) {
361 return false;
362 }
363 ++start1; ++start2;
364 }
365 return (start1 == end1 && start2 == end2);
366}
367
Eric Fiselierdc62be192016-06-18 04:10:23 +0000368
369template <class Iter1, class Iter2>
370bool checkCollectionsEqualBackwards(
371 Iter1 const start1, Iter1 end1
372 , Iter2 const start2, Iter2 end2
373 )
374{
375 while (start1 != end1 && start2 != end2) {
376 --end1; --end2;
377 if (*end1 != *end2) {
378 return false;
379 }
380 }
381 return (start1 == end1 && start2 == end2);
382}
383
Eric Fiselierc7979582016-06-17 19:46:40 +0000384// We often need to test that the error_code was cleared if no error occurs
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +0000385// this function returns an error_code which is set to an error that will
Eric Fiselierc7979582016-06-17 19:46:40 +0000386// never be returned by the filesystem functions.
Eric Fiselierc1699862018-07-20 01:22:32 +0000387inline std::error_code GetTestEC(unsigned Idx = 0) {
388 using std::errc;
389 auto GetErrc = [&]() {
390 switch (Idx) {
391 case 0:
392 return errc::address_family_not_supported;
393 case 1:
394 return errc::address_not_available;
395 case 2:
396 return errc::address_in_use;
397 case 3:
398 return errc::argument_list_too_long;
399 default:
400 assert(false && "Idx out of range");
401 std::abort();
402 }
403 };
404 return std::make_error_code(GetErrc());
405}
406
407inline bool ErrorIsImp(const std::error_code& ec,
408 std::vector<std::errc> const& errors) {
409 for (auto errc : errors) {
410 if (ec == std::make_error_code(errc))
411 return true;
412 }
413 return false;
414}
415
416template <class... ErrcT>
417inline bool ErrorIs(const std::error_code& ec, std::errc First, ErrcT... Rest) {
418 std::vector<std::errc> errors = {First, Rest...};
419 return ErrorIsImp(ec, errors);
Eric Fiselierc7979582016-06-17 19:46:40 +0000420}
421
Eric Fiselier93e32122016-06-17 21:44:26 +0000422// Provide our own Sleep routine since std::this_thread::sleep_for is not
423// available in single-threaded mode.
424void SleepFor(std::chrono::seconds dur) {
425 using namespace std::chrono;
Eric Fiselier824ed8c2016-06-18 18:32:26 +0000426#if defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
427 using Clock = system_clock;
428#else
429 using Clock = steady_clock;
430#endif
431 const auto wake_time = Clock::now() + dur;
432 while (Clock::now() < wake_time)
Eric Fiselier93e32122016-06-17 21:44:26 +0000433 ;
434}
435
Eric Fiselierd7fae182018-04-02 23:03:41 +0000436inline bool PathEq(fs::path const& LHS, fs::path const& RHS) {
437 return LHS.native() == RHS.native();
438}
439
Eric Fiselierc1699862018-07-20 01:22:32 +0000440struct ExceptionChecker {
441 std::vector<std::errc> expected_err_list;
442 fs::path expected_path1;
443 fs::path expected_path2;
444
445 template <class... ErrcT>
446 explicit ExceptionChecker(fs::path p, std::errc first_err, ErrcT... rest_err)
447 : expected_err_list({first_err, rest_err...}), expected_path1(p) {}
448
449 template <class... ErrcT>
450 explicit ExceptionChecker(fs::path p1, fs::path p2, std::errc first_err,
451 ErrcT... rest_err)
452 : expected_err_list({first_err, rest_err...}), expected_path1(p1),
453 expected_path2(p2) {}
454
455 void operator()(fs::filesystem_error const& Err) const {
456 TEST_CHECK(ErrorIsImp(Err.code(), expected_err_list));
457 TEST_CHECK(Err.path1() == expected_path1);
458 TEST_CHECK(Err.path2() == expected_path2);
459 }
460};
461
Eric Fiselierc7979582016-06-17 19:46:40 +0000462#endif /* FILESYSTEM_TEST_HELPER_HPP */