blob: de06868c0147cae10fccf0afb012ddad220b9e2a [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
Eric Fiselier7c0ed442018-07-22 02:00:53 +0000108namespace random_utils {
109inline char to_hex(int ch) {
110 return ch < 10 ? static_cast<char>('0' + ch)
111 : static_cast<char>('a' + (ch - 10));
112}
113
114inline char random_hex_char() {
115 static std::mt19937 rd{std::random_device{}()};
116 static std::uniform_int_distribution<int> mrand{0, 15};
117 return to_hex(mrand(rd));
118}
119
120} // namespace random_utils
121
Eric Fiselierc7979582016-06-17 19:46:40 +0000122struct scoped_test_env
123{
124 scoped_test_env() : test_root(random_env_path())
125 { fs_helper_run(fs_make_cmd("init_test_directory", test_root)); }
126
127 ~scoped_test_env()
128 { fs_helper_run(fs_make_cmd("destroy_test_directory", test_root)); }
129
130 scoped_test_env(scoped_test_env const &) = delete;
131 scoped_test_env & operator=(scoped_test_env const &) = delete;
132
133 fs::path make_env_path(std::string p) { return sanitize_path(p); }
134
135 std::string sanitize_path(std::string raw) {
136 assert(raw.find("..") == std::string::npos);
137 std::string const& root = test_root.native();
138 if (root.compare(0, root.size(), raw, 0, root.size()) != 0) {
139 assert(raw.front() != '\\');
140 fs::path tmp(test_root);
141 tmp /= raw;
142 return std::move(const_cast<std::string&>(tmp.native()));
143 }
144 return raw;
145 }
146
147 std::string create_file(std::string filename, std::size_t size = 0) {
148 filename = sanitize_path(std::move(filename));
149 std::string out_str(size, 'a');
150 {
151 std::ofstream out(filename.c_str());
152 out << out_str;
153 }
154 return filename;
155 }
156
157 std::string create_dir(std::string filename) {
158 filename = sanitize_path(std::move(filename));
159 fs_helper_run(fs_make_cmd("create_dir", filename));
160 return filename;
161 }
162
163 std::string create_symlink(std::string source, std::string to) {
164 source = sanitize_path(std::move(source));
165 to = sanitize_path(std::move(to));
166 fs_helper_run(fs_make_cmd("create_symlink", source, to));
167 return to;
168 }
169
170 std::string create_hardlink(std::string source, std::string to) {
171 source = sanitize_path(std::move(source));
172 to = sanitize_path(std::move(to));
173 fs_helper_run(fs_make_cmd("create_hardlink", source, to));
174 return to;
175 }
176
177 std::string create_fifo(std::string file) {
178 file = sanitize_path(std::move(file));
179 fs_helper_run(fs_make_cmd("create_fifo", file));
180 return file;
181 }
182
183 // OS X and FreeBSD doesn't support socket files so we shouldn't even
184 // allow tests to call this unguarded.
185#if !defined(__FreeBSD__) && !defined(__APPLE__)
186 std::string create_socket(std::string file) {
187 file = sanitize_path(std::move(file));
188 fs_helper_run(fs_make_cmd("create_socket", file));
189 return file;
190 }
191#endif
192
193 fs::path const test_root;
194
195private:
Eric Fiselierc7979582016-06-17 19:46:40 +0000196 static std::string unique_path_suffix() {
197 std::string model = "test.%%%%%%";
198 for (auto & ch : model) {
Eric Fiselier7c0ed442018-07-22 02:00:53 +0000199 if (ch == '%')
200 ch = random_utils::random_hex_char();
Eric Fiselierc7979582016-06-17 19:46:40 +0000201 }
202 return model;
203 }
204
205 // This could potentially introduce a filesystem race with other tests
206 // running at the same time, but oh well, it's just test code.
207 static inline fs::path random_env_path() {
208 static const char* env_path = LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT;
209 fs::path p = fs::path(env_path) / unique_path_suffix();
210 assert(p.parent_path() == env_path);
211 return p;
212 }
213
214 static inline std::string make_arg(std::string const& arg) {
215 return "'" + arg + "'";
216 }
217
218 static inline std::string make_arg(std::size_t arg) {
219 return std::to_string(arg);
220 }
221
222 template <class T>
223 static inline std::string
224 fs_make_cmd(std::string const& cmd_name, T const& arg) {
225 return cmd_name + "(" + make_arg(arg) + ")";
226 }
227
228 template <class T, class U>
229 static inline std::string
230 fs_make_cmd(std::string const& cmd_name, T const& arg1, U const& arg2) {
231 return cmd_name + "(" + make_arg(arg1) + ", " + make_arg(arg2) + ")";
232 }
233
234 static inline void fs_helper_run(std::string const& raw_cmd) {
Eric Fiselier78046e42017-05-09 23:47:20 +0000235 // check that the fs test root in the environment matches what we were
Eric Fiselierc7979582016-06-17 19:46:40 +0000236 // compiled with.
237 static bool checked = checkDynamicTestRoot();
Eric Fiselierfd838222016-12-23 23:37:52 +0000238 ((void)checked);
Eric Fiselierc7979582016-06-17 19:46:40 +0000239 std::string cmd = LIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER;
240 cmd += " \"" + raw_cmd + "\"";
241 int ret = std::system(cmd.c_str());
242 assert(ret == 0);
243 }
244
245 static bool checkDynamicTestRoot() {
Eric Fiselier48bc8502016-06-21 21:54:23 +0000246 // LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT is expected not to contain symlinks.
Eric Fiselierc7979582016-06-17 19:46:40 +0000247 char* fs_root = std::getenv("LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT");
248 if (!fs_root) {
249 std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT must be a defined "
250 "environment variable when running the test.\n");
251 std::abort();
252 }
253 if (std::string(fs_root) != LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT) {
Eric Fiselier78046e42017-05-09 23:47:20 +0000254 std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT environment variable"
Eric Fiselierc7979582016-06-17 19:46:40 +0000255 " must have the same value as when the test was compiled.\n");
256 std::printf(" Current Value: '%s'\n", fs_root);
257 std::printf(" Expected Value: '%s'\n", LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT);
258 std::abort();
259 }
260 return true;
261 }
262
263};
264
265#endif // LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT
266
267// Misc test types
268
269#define CONCAT2(LHS, RHS) LHS##RHS
270#define CONCAT(LHS, RHS) CONCAT2(LHS, RHS)
271#define MKSTR(Str) {Str, CONCAT(L, Str), CONCAT(u, Str), CONCAT(U, Str)}
272
273struct MultiStringType {
274 const char* s;
275 const wchar_t* w;
276 const char16_t* u16;
277 const char32_t* u32;
278
279 operator const char* () const { return s; }
280 operator const wchar_t* () const { return w; }
281 operator const char16_t* () const { return u16; }
282 operator const char32_t* () const { return u32; }
283};
284
285const MultiStringType PathList[] = {
286 MKSTR(""),
287 MKSTR(" "),
288 MKSTR("//"),
289 MKSTR("."),
290 MKSTR(".."),
291 MKSTR("foo"),
292 MKSTR("/"),
293 MKSTR("/foo"),
294 MKSTR("foo/"),
295 MKSTR("/foo/"),
296 MKSTR("foo/bar"),
297 MKSTR("/foo/bar"),
298 MKSTR("//net"),
299 MKSTR("//net/foo"),
300 MKSTR("///foo///"),
301 MKSTR("///foo///bar"),
302 MKSTR("/."),
303 MKSTR("./"),
304 MKSTR("/.."),
305 MKSTR("../"),
306 MKSTR("foo/."),
307 MKSTR("foo/.."),
308 MKSTR("foo/./"),
309 MKSTR("foo/./bar"),
310 MKSTR("foo/../"),
311 MKSTR("foo/../bar"),
312 MKSTR("c:"),
313 MKSTR("c:/"),
314 MKSTR("c:foo"),
315 MKSTR("c:/foo"),
316 MKSTR("c:foo/"),
317 MKSTR("c:/foo/"),
318 MKSTR("c:/foo/bar"),
319 MKSTR("prn:"),
320 MKSTR("c:\\"),
321 MKSTR("c:\\foo"),
322 MKSTR("c:foo\\"),
323 MKSTR("c:\\foo\\"),
324 MKSTR("c:\\foo/"),
325 MKSTR("c:/foo\\bar"),
326 MKSTR("//"),
327 MKSTR("/finally/we/need/one/really/really/really/really/really/really/really/long/string")
328};
329const unsigned PathListSize = sizeof(PathList) / sizeof(MultiStringType);
330
331template <class Iter>
332Iter IterEnd(Iter B) {
333 using VT = typename std::iterator_traits<Iter>::value_type;
334 for (; *B != VT{}; ++B)
335 ;
336 return B;
337}
338
339template <class CharT>
340const CharT* StrEnd(CharT const* P) {
341 return IterEnd(P);
342}
343
344template <class CharT>
345std::size_t StrLen(CharT const* P) {
346 return StrEnd(P) - P;
347}
348
349// Testing the allocation behavior of the code_cvt functions requires
350// *knowing* that the allocation was not done by "path::__str_".
351// This hack forces path to allocate enough memory.
352inline void PathReserve(fs::path& p, std::size_t N) {
353 auto const& native_ref = p.native();
354 const_cast<std::string&>(native_ref).reserve(N);
355}
356
357template <class Iter1, class Iter2>
358bool checkCollectionsEqual(
359 Iter1 start1, Iter1 const end1
360 , Iter2 start2, Iter2 const end2
361 )
362{
363 while (start1 != end1 && start2 != end2) {
364 if (*start1 != *start2) {
365 return false;
366 }
367 ++start1; ++start2;
368 }
369 return (start1 == end1 && start2 == end2);
370}
371
Eric Fiselierdc62be192016-06-18 04:10:23 +0000372
373template <class Iter1, class Iter2>
374bool checkCollectionsEqualBackwards(
375 Iter1 const start1, Iter1 end1
376 , Iter2 const start2, Iter2 end2
377 )
378{
379 while (start1 != end1 && start2 != end2) {
380 --end1; --end2;
381 if (*end1 != *end2) {
382 return false;
383 }
384 }
385 return (start1 == end1 && start2 == end2);
386}
387
Eric Fiselierc7979582016-06-17 19:46:40 +0000388// We often need to test that the error_code was cleared if no error occurs
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +0000389// this function returns an error_code which is set to an error that will
Eric Fiselierc7979582016-06-17 19:46:40 +0000390// never be returned by the filesystem functions.
Eric Fiselierc1699862018-07-20 01:22:32 +0000391inline std::error_code GetTestEC(unsigned Idx = 0) {
392 using std::errc;
393 auto GetErrc = [&]() {
394 switch (Idx) {
395 case 0:
396 return errc::address_family_not_supported;
397 case 1:
398 return errc::address_not_available;
399 case 2:
400 return errc::address_in_use;
401 case 3:
402 return errc::argument_list_too_long;
403 default:
404 assert(false && "Idx out of range");
405 std::abort();
406 }
407 };
408 return std::make_error_code(GetErrc());
409}
410
411inline bool ErrorIsImp(const std::error_code& ec,
412 std::vector<std::errc> const& errors) {
413 for (auto errc : errors) {
414 if (ec == std::make_error_code(errc))
415 return true;
416 }
417 return false;
418}
419
420template <class... ErrcT>
421inline bool ErrorIs(const std::error_code& ec, std::errc First, ErrcT... Rest) {
422 std::vector<std::errc> errors = {First, Rest...};
423 return ErrorIsImp(ec, errors);
Eric Fiselierc7979582016-06-17 19:46:40 +0000424}
425
Eric Fiselier93e32122016-06-17 21:44:26 +0000426// Provide our own Sleep routine since std::this_thread::sleep_for is not
427// available in single-threaded mode.
428void SleepFor(std::chrono::seconds dur) {
429 using namespace std::chrono;
Eric Fiselier824ed8c2016-06-18 18:32:26 +0000430#if defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
431 using Clock = system_clock;
432#else
433 using Clock = steady_clock;
434#endif
435 const auto wake_time = Clock::now() + dur;
436 while (Clock::now() < wake_time)
Eric Fiselier93e32122016-06-17 21:44:26 +0000437 ;
438}
439
Eric Fiselierd7fae182018-04-02 23:03:41 +0000440inline bool PathEq(fs::path const& LHS, fs::path const& RHS) {
441 return LHS.native() == RHS.native();
442}
443
Eric Fiselierc1699862018-07-20 01:22:32 +0000444struct ExceptionChecker {
445 std::vector<std::errc> expected_err_list;
446 fs::path expected_path1;
447 fs::path expected_path2;
448
449 template <class... ErrcT>
450 explicit ExceptionChecker(fs::path p, std::errc first_err, ErrcT... rest_err)
451 : expected_err_list({first_err, rest_err...}), expected_path1(p) {}
452
453 template <class... ErrcT>
454 explicit ExceptionChecker(fs::path p1, fs::path p2, std::errc first_err,
455 ErrcT... rest_err)
456 : expected_err_list({first_err, rest_err...}), expected_path1(p1),
457 expected_path2(p2) {}
458
459 void operator()(fs::filesystem_error const& Err) const {
460 TEST_CHECK(ErrorIsImp(Err.code(), expected_err_list));
461 TEST_CHECK(Err.path1() == expected_path1);
462 TEST_CHECK(Err.path2() == expected_path2);
463 }
464};
465
Eric Fiselierc7979582016-06-17 19:46:40 +0000466#endif /* FILESYSTEM_TEST_HELPER_HPP */