filesystem_test_helper.hpp 12 KB

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