blob: 7e3c7399a105c7719259787a970f311109fb6e18 [file] [log] [blame]
Eric Fiselierc7979582016-06-17 19:46:40 +00001#ifndef FILESYSTEM_TEST_HELPER_HPP
2#define FILESYSTEM_TEST_HELPER_HPP
3
4#include <experimental/filesystem>
5#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
12namespace fs = std::experimental::filesystem;
13
14// static test helpers
15
16#ifndef LIBCXX_FILESYSTEM_STATIC_TEST_ROOT
17#warning "STATIC TESTS DISABLED"
18#else // LIBCXX_FILESYSTEM_STATIC_TEST_ROOT
19
20namespace StaticEnv {
21
22inline fs::path makePath(fs::path const& p) {
23 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) {
229 // check that the fs test root in the enviroment matches what we were
230 // compiled with.
231 static bool checked = checkDynamicTestRoot();
232 std::string cmd = LIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER;
233 cmd += " \"" + raw_cmd + "\"";
234 int ret = std::system(cmd.c_str());
235 assert(ret == 0);
236 }
237
238 static bool checkDynamicTestRoot() {
239 char* fs_root = std::getenv("LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT");
240 if (!fs_root) {
241 std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT must be a defined "
242 "environment variable when running the test.\n");
243 std::abort();
244 }
245 if (std::string(fs_root) != LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT) {
246 std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT enviroment variable"
247 " must have the same value as when the test was compiled.\n");
248 std::printf(" Current Value: '%s'\n", fs_root);
249 std::printf(" Expected Value: '%s'\n", LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT);
250 std::abort();
251 }
252 return true;
253 }
254
255};
256
257#endif // LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT
258
259// Misc test types
260
261#define CONCAT2(LHS, RHS) LHS##RHS
262#define CONCAT(LHS, RHS) CONCAT2(LHS, RHS)
263#define MKSTR(Str) {Str, CONCAT(L, Str), CONCAT(u, Str), CONCAT(U, Str)}
264
265struct MultiStringType {
266 const char* s;
267 const wchar_t* w;
268 const char16_t* u16;
269 const char32_t* u32;
270
271 operator const char* () const { return s; }
272 operator const wchar_t* () const { return w; }
273 operator const char16_t* () const { return u16; }
274 operator const char32_t* () const { return u32; }
275};
276
277const MultiStringType PathList[] = {
278 MKSTR(""),
279 MKSTR(" "),
280 MKSTR("//"),
281 MKSTR("."),
282 MKSTR(".."),
283 MKSTR("foo"),
284 MKSTR("/"),
285 MKSTR("/foo"),
286 MKSTR("foo/"),
287 MKSTR("/foo/"),
288 MKSTR("foo/bar"),
289 MKSTR("/foo/bar"),
290 MKSTR("//net"),
291 MKSTR("//net/foo"),
292 MKSTR("///foo///"),
293 MKSTR("///foo///bar"),
294 MKSTR("/."),
295 MKSTR("./"),
296 MKSTR("/.."),
297 MKSTR("../"),
298 MKSTR("foo/."),
299 MKSTR("foo/.."),
300 MKSTR("foo/./"),
301 MKSTR("foo/./bar"),
302 MKSTR("foo/../"),
303 MKSTR("foo/../bar"),
304 MKSTR("c:"),
305 MKSTR("c:/"),
306 MKSTR("c:foo"),
307 MKSTR("c:/foo"),
308 MKSTR("c:foo/"),
309 MKSTR("c:/foo/"),
310 MKSTR("c:/foo/bar"),
311 MKSTR("prn:"),
312 MKSTR("c:\\"),
313 MKSTR("c:\\foo"),
314 MKSTR("c:foo\\"),
315 MKSTR("c:\\foo\\"),
316 MKSTR("c:\\foo/"),
317 MKSTR("c:/foo\\bar"),
318 MKSTR("//"),
319 MKSTR("/finally/we/need/one/really/really/really/really/really/really/really/long/string")
320};
321const unsigned PathListSize = sizeof(PathList) / sizeof(MultiStringType);
322
323template <class Iter>
324Iter IterEnd(Iter B) {
325 using VT = typename std::iterator_traits<Iter>::value_type;
326 for (; *B != VT{}; ++B)
327 ;
328 return B;
329}
330
331template <class CharT>
332const CharT* StrEnd(CharT const* P) {
333 return IterEnd(P);
334}
335
336template <class CharT>
337std::size_t StrLen(CharT const* P) {
338 return StrEnd(P) - P;
339}
340
341// Testing the allocation behavior of the code_cvt functions requires
342// *knowing* that the allocation was not done by "path::__str_".
343// This hack forces path to allocate enough memory.
344inline void PathReserve(fs::path& p, std::size_t N) {
345 auto const& native_ref = p.native();
346 const_cast<std::string&>(native_ref).reserve(N);
347}
348
349template <class Iter1, class Iter2>
350bool checkCollectionsEqual(
351 Iter1 start1, Iter1 const end1
352 , Iter2 start2, Iter2 const end2
353 )
354{
355 while (start1 != end1 && start2 != end2) {
356 if (*start1 != *start2) {
357 return false;
358 }
359 ++start1; ++start2;
360 }
361 return (start1 == end1 && start2 == end2);
362}
363
Eric Fiselierdc62be192016-06-18 04:10:23 +0000364
365template <class Iter1, class Iter2>
366bool checkCollectionsEqualBackwards(
367 Iter1 const start1, Iter1 end1
368 , Iter2 const start2, Iter2 end2
369 )
370{
371 while (start1 != end1 && start2 != end2) {
372 --end1; --end2;
373 if (*end1 != *end2) {
374 return false;
375 }
376 }
377 return (start1 == end1 && start2 == end2);
378}
379
Eric Fiselierc7979582016-06-17 19:46:40 +0000380// We often need to test that the error_code was cleared if no error occurs
381// this function returns a error_code which is set to an error that will
382// never be returned by the filesystem functions.
383inline std::error_code GetTestEC() {
384 return std::make_error_code(std::errc::address_family_not_supported);
385}
386
Eric Fiselier93e32122016-06-17 21:44:26 +0000387// Provide our own Sleep routine since std::this_thread::sleep_for is not
388// available in single-threaded mode.
389void SleepFor(std::chrono::seconds dur) {
390 using namespace std::chrono;
Eric Fiselier824ed8c2016-06-18 18:32:26 +0000391#if defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
392 using Clock = system_clock;
393#else
394 using Clock = steady_clock;
395#endif
396 const auto wake_time = Clock::now() + dur;
397 while (Clock::now() < wake_time)
Eric Fiselier93e32122016-06-17 21:44:26 +0000398 ;
399}
400
Eric Fiselierc7979582016-06-17 19:46:40 +0000401#endif /* FILESYSTEM_TEST_HELPER_HPP */