filesystem_test_helper.hpp 12 KB

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