filesystem_test_helper.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. 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 enviroment matches what we were
  191. // compiled with.
  192. static bool checked = checkDynamicTestRoot();
  193. std::string cmd = LIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER;
  194. cmd += " \"" + raw_cmd + "\"";
  195. int ret = std::system(cmd.c_str());
  196. assert(ret == 0);
  197. }
  198. static bool checkDynamicTestRoot() {
  199. char* fs_root = std::getenv("LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT");
  200. if (!fs_root) {
  201. std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT must be a defined "
  202. "environment variable when running the test.\n");
  203. std::abort();
  204. }
  205. if (std::string(fs_root) != LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT) {
  206. std::printf("ERROR: LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT enviroment variable"
  207. " must have the same value as when the test was compiled.\n");
  208. std::printf(" Current Value: '%s'\n", fs_root);
  209. std::printf(" Expected Value: '%s'\n", LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT);
  210. std::abort();
  211. }
  212. return true;
  213. }
  214. };
  215. #endif // LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT
  216. // Misc test types
  217. #define CONCAT2(LHS, RHS) LHS##RHS
  218. #define CONCAT(LHS, RHS) CONCAT2(LHS, RHS)
  219. #define MKSTR(Str) {Str, CONCAT(L, Str), CONCAT(u, Str), CONCAT(U, Str)}
  220. struct MultiStringType {
  221. const char* s;
  222. const wchar_t* w;
  223. const char16_t* u16;
  224. const char32_t* u32;
  225. operator const char* () const { return s; }
  226. operator const wchar_t* () const { return w; }
  227. operator const char16_t* () const { return u16; }
  228. operator const char32_t* () const { return u32; }
  229. };
  230. const MultiStringType PathList[] = {
  231. MKSTR(""),
  232. MKSTR(" "),
  233. MKSTR("//"),
  234. MKSTR("."),
  235. MKSTR(".."),
  236. MKSTR("foo"),
  237. MKSTR("/"),
  238. MKSTR("/foo"),
  239. MKSTR("foo/"),
  240. MKSTR("/foo/"),
  241. MKSTR("foo/bar"),
  242. MKSTR("/foo/bar"),
  243. MKSTR("//net"),
  244. MKSTR("//net/foo"),
  245. MKSTR("///foo///"),
  246. MKSTR("///foo///bar"),
  247. MKSTR("/."),
  248. MKSTR("./"),
  249. MKSTR("/.."),
  250. MKSTR("../"),
  251. MKSTR("foo/."),
  252. MKSTR("foo/.."),
  253. MKSTR("foo/./"),
  254. MKSTR("foo/./bar"),
  255. MKSTR("foo/../"),
  256. MKSTR("foo/../bar"),
  257. MKSTR("c:"),
  258. MKSTR("c:/"),
  259. MKSTR("c:foo"),
  260. MKSTR("c:/foo"),
  261. MKSTR("c:foo/"),
  262. MKSTR("c:/foo/"),
  263. MKSTR("c:/foo/bar"),
  264. MKSTR("prn:"),
  265. MKSTR("c:\\"),
  266. MKSTR("c:\\foo"),
  267. MKSTR("c:foo\\"),
  268. MKSTR("c:\\foo\\"),
  269. MKSTR("c:\\foo/"),
  270. MKSTR("c:/foo\\bar"),
  271. MKSTR("//"),
  272. MKSTR("/finally/we/need/one/really/really/really/really/really/really/really/long/string")
  273. };
  274. const unsigned PathListSize = sizeof(PathList) / sizeof(MultiStringType);
  275. template <class Iter>
  276. Iter IterEnd(Iter B) {
  277. using VT = typename std::iterator_traits<Iter>::value_type;
  278. for (; *B != VT{}; ++B)
  279. ;
  280. return B;
  281. }
  282. template <class CharT>
  283. const CharT* StrEnd(CharT const* P) {
  284. return IterEnd(P);
  285. }
  286. template <class CharT>
  287. std::size_t StrLen(CharT const* P) {
  288. return StrEnd(P) - P;
  289. }
  290. // Testing the allocation behavior of the code_cvt functions requires
  291. // *knowing* that the allocation was not done by "path::__str_".
  292. // This hack forces path to allocate enough memory.
  293. inline void PathReserve(fs::path& p, std::size_t N) {
  294. auto const& native_ref = p.native();
  295. const_cast<std::string&>(native_ref).reserve(N);
  296. }
  297. template <class Iter1, class Iter2>
  298. bool checkCollectionsEqual(
  299. Iter1 start1, Iter1 const end1
  300. , Iter2 start2, Iter2 const end2
  301. )
  302. {
  303. while (start1 != end1 && start2 != end2) {
  304. if (*start1 != *start2) {
  305. return false;
  306. }
  307. ++start1; ++start2;
  308. }
  309. return (start1 == end1 && start2 == end2);
  310. }
  311. template <class Iter1, class Iter2>
  312. bool checkCollectionsEqualBackwards(
  313. Iter1 const start1, Iter1 end1
  314. , Iter2 const start2, Iter2 end2
  315. )
  316. {
  317. while (start1 != end1 && start2 != end2) {
  318. --end1; --end2;
  319. if (*end1 != *end2) {
  320. return false;
  321. }
  322. }
  323. return (start1 == end1 && start2 == end2);
  324. }
  325. // We often need to test that the error_code was cleared if no error occurs
  326. // this function returns a error_code which is set to an error that will
  327. // never be returned by the filesystem functions.
  328. inline std::error_code GetTestEC() {
  329. return std::make_error_code(std::errc::address_family_not_supported);
  330. }
  331. // Provide our own Sleep routine since std::this_thread::sleep_for is not
  332. // available in single-threaded mode.
  333. void SleepFor(std::chrono::seconds dur) {
  334. using namespace std::chrono;
  335. const auto curr_time = system_clock::now();
  336. auto wake_time = curr_time + dur;
  337. while (system_clock::now() < wake_time)
  338. ;
  339. }
  340. #endif /* FILESYSTEM_TEST_HELPER_HPP */