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