path_manipulations.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* lib/vfs - test vfs_path_t manipulation functions
  2. Copyright (C) 2011-2019
  3. Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software: you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation, either version 3 of the License,
  10. or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define TEST_SUITE_NAME "/lib/vfs"
  19. #include "tests/mctest.h"
  20. #ifdef HAVE_CHARSET
  21. #include "lib/charsets.h"
  22. #endif
  23. #include "lib/strutil.h"
  24. #include "lib/vfs/xdirentry.h"
  25. #include "lib/vfs/path.h"
  26. #include "src/vfs/local/local.c"
  27. static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  28. /* --------------------------------------------------------------------------------------------- */
  29. static void
  30. init_test_classes (void)
  31. {
  32. vfs_init_class (&vfs_test_ops1, "testfs1", VFS_NOLINKS | VFS_REMOTE, "test1");
  33. vfs_register_class (&vfs_test_ops1);
  34. vfs_init_class (&vfs_test_ops2, "testfs2", VFS_UNKNOWN, "test2");
  35. vfs_register_class (&vfs_test_ops2);
  36. vfs_init_class (&vfs_test_ops3, "testfs3", VFS_LOCAL, "test3");
  37. vfs_register_class (&vfs_test_ops3);
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* @Before */
  41. static void
  42. setup (void)
  43. {
  44. str_init_strings (NULL);
  45. vfs_init ();
  46. vfs_init_localfs ();
  47. vfs_setup_work_dir ();
  48. init_test_classes ();
  49. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  50. #ifdef HAVE_CHARSET
  51. load_codepages_list ();
  52. #endif
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. /* @After */
  56. static void
  57. teardown (void)
  58. {
  59. #ifdef HAVE_CHARSET
  60. free_codepages_list ();
  61. #endif
  62. vfs_shut ();
  63. str_uninit_strings ();
  64. }
  65. /* --------------------------------------------------------------------------------------------- */
  66. /* @DataSource("test_vfs_path_tokens_count_ds") */
  67. /* *INDENT-OFF* */
  68. static const struct test_vfs_path_tokens_count_ds
  69. {
  70. const char *input_path;
  71. const vfs_path_flag_t input_flags;
  72. const size_t expected_token_count;
  73. } test_vfs_path_tokens_count_ds[] =
  74. {
  75. { /* 0. */
  76. "/",
  77. VPF_NONE,
  78. 0
  79. },
  80. { /* 1. */
  81. "/path",
  82. VPF_NONE,
  83. 1
  84. },
  85. { /* 2. */
  86. "/path1/path2/path3",
  87. VPF_NONE,
  88. 3
  89. },
  90. { /* 3. */
  91. "test3://path1/path2/path3/path4",
  92. VPF_NO_CANON,
  93. 4
  94. },
  95. { /* 4. */
  96. "path1/path2/path3",
  97. VPF_NO_CANON,
  98. 3
  99. },
  100. { /* 5. */
  101. "/path1/path2/path3/",
  102. VPF_NONE,
  103. 3
  104. },
  105. { /* 6. */
  106. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
  107. VPF_NONE,
  108. 5
  109. },
  110. #ifdef HAVE_CHARSET
  111. { /* 7. */
  112. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/"
  113. "test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33",
  114. VPF_NONE,
  115. 11
  116. },
  117. #endif
  118. };
  119. /* *INDENT-ON* */
  120. /* @Test(dataSource = "test_vfs_path_tokens_count_ds") */
  121. /* *INDENT-OFF* */
  122. START_PARAMETRIZED_TEST (test_vfs_path_tokens_count, test_vfs_path_tokens_count_ds)
  123. /* *INDENT-ON* */
  124. {
  125. /* given */
  126. size_t tokens_count;
  127. vfs_path_t *vpath;
  128. vpath = vfs_path_from_str_flags (data->input_path, data->input_flags);
  129. /* when */
  130. tokens_count = vfs_path_tokens_count (vpath);
  131. /* then */
  132. mctest_assert_int_eq (tokens_count, data->expected_token_count);
  133. vfs_path_free (vpath);
  134. }
  135. /* *INDENT-OFF* */
  136. END_PARAMETRIZED_TEST
  137. /* *INDENT-ON* */
  138. /* --------------------------------------------------------------------------------------------- */
  139. /* @DataSource("test_vfs_path_tokens_get_ds") */
  140. /* *INDENT-OFF* */
  141. static const struct test_vfs_path_tokens_get_ds
  142. {
  143. const char *input_path;
  144. const ssize_t input_start_position;
  145. const ssize_t input_length;
  146. const char *expected_path;
  147. } test_vfs_path_tokens_get_ds[] =
  148. {
  149. { /* 0. Invalid start position */
  150. "/",
  151. 2,
  152. 1,
  153. NULL
  154. },
  155. { /* 1. Invalid negative position */
  156. "/path",
  157. -3,
  158. 1,
  159. NULL
  160. },
  161. { /* 2. Count of tokens is zero. Count should be autocorrected */
  162. "/path",
  163. 0,
  164. 0,
  165. "path"
  166. },
  167. { /* 3. get 'path2/path3' by 1,2 */
  168. "/path1/path2/path3/path4",
  169. 1,
  170. 2,
  171. "path2/path3"
  172. },
  173. { /* 4. get 'path2/path3' by 1,2 from LOCAL VFS */
  174. "test3://path1/path2/path3/path4",
  175. 1,
  176. 2,
  177. "path2/path3"
  178. },
  179. { /* 5. get 'path2/path3' by 1,2 from non-LOCAL VFS */
  180. "test2://path1/path2/path3/path4",
  181. 1,
  182. 2,
  183. "test2://path2/path3"
  184. },
  185. { /* 6. get 'path2/path3' by 1,2 throught non-LOCAL VFS */
  186. "/path1/path2/test1://user:pass@some.host:12345/path3/path4",
  187. 1,
  188. 2,
  189. "path2/test1://user:pass@some.host:12345/path3"
  190. },
  191. { /* 7. get 'path2/path3' by 1,2 where path2 it's LOCAL VFS */
  192. "test3://path1/path2/test2://path3/path4",
  193. 1,
  194. 2,
  195. "path2/test2://path3"
  196. },
  197. { /* 8. get 'path2/path3' by 1,2 where path3 it's LOCAL VFS */
  198. "test2://path1/path2/test3://path3/path4",
  199. 1,
  200. 2,
  201. "test2://path2/test3://path3"
  202. },
  203. { /* 9. get 'path4' by -1,1 */
  204. "/path1/path2/path3/path4",
  205. -1,
  206. 1,
  207. "path4"
  208. },
  209. { /* 10. get 'path2/path3/path4' by -3,0 */
  210. "/path1/path2/path3/path4",
  211. -3,
  212. 0,
  213. "path2/path3/path4"
  214. },
  215. #ifdef HAVE_CHARSET
  216. { /* 11. get 'path2/path3' by 1,2 from LOCAL VFS with encoding */
  217. "test3://path1/path2/test3://#enc:KOI8-R/path3/path4",
  218. 1,
  219. 2,
  220. "path2/test3://#enc:KOI8-R/path3"
  221. },
  222. { /* 12. get 'path2/path3' by 1,2 with encoding */
  223. "#enc:KOI8-R/path1/path2/path3/path4",
  224. 1,
  225. 2,
  226. "#enc:KOI8-R/path2/path3"
  227. },
  228. #endif
  229. /* TODO: currently this test don't passed. Probably broken string URI parser
  230. { *//* 13. get 'path2/path3' by 1,2 from LOCAL VFS *//*
  231. "test3://path1/path2/test2://test3://path3/path4",
  232. 1,
  233. 2,
  234. "path2/path3"
  235. },
  236. */
  237. };
  238. /* *INDENT-ON* */
  239. /* @Test(dataSource = "test_vfs_path_tokens_get_ds") */
  240. /* *INDENT-OFF* */
  241. START_PARAMETRIZED_TEST (test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds)
  242. /* *INDENT-ON* */
  243. {
  244. /* given */
  245. vfs_path_t *vpath;
  246. char *actual_path;
  247. vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
  248. /* when */
  249. actual_path = vfs_path_tokens_get (vpath, data->input_start_position, data->input_length);
  250. /* then */
  251. mctest_assert_str_eq (actual_path, data->expected_path);
  252. g_free (actual_path);
  253. vfs_path_free (vpath);
  254. }
  255. /* *INDENT-OFF* */
  256. END_PARAMETRIZED_TEST
  257. /* *INDENT-ON* */
  258. /* --------------------------------------------------------------------------------------------- */
  259. /* @DataSource("test_vfs_path_append_vpath_ds") */
  260. /* *INDENT-OFF* */
  261. static const struct test_vfs_path_append_vpath_ds
  262. {
  263. const char *input_path1;
  264. const char *input_path2;
  265. const int expected_element_count;
  266. const char *expected_path;
  267. } test_vfs_path_append_vpath_ds[] =
  268. {
  269. { /* 0. */
  270. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33",
  271. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
  272. 6,
  273. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  274. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
  275. },
  276. #ifdef HAVE_CHARSET
  277. { /* 1. */
  278. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33",
  279. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
  280. 6,
  281. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
  282. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
  283. },
  284. #endif /* HAVE_CHARSET */
  285. };
  286. /* *INDENT-ON* */
  287. /* @Test(dataSource = "test_vfs_path_append_vpath_ds") */
  288. /* *INDENT-OFF* */
  289. START_PARAMETRIZED_TEST (test_vfs_path_append_vpath, test_vfs_path_append_vpath_ds)
  290. /* *INDENT-ON* */
  291. {
  292. /* given */
  293. vfs_path_t *vpath1, *vpath2, *vpath3;
  294. vpath1 = vfs_path_from_str (data->input_path1);
  295. vpath2 = vfs_path_from_str (data->input_path2);
  296. /* when */
  297. vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
  298. /* then */
  299. mctest_assert_int_eq (vfs_path_elements_count (vpath3), data->expected_element_count);
  300. mctest_assert_str_eq (vfs_path_as_str (vpath3), data->expected_path);
  301. vfs_path_free (vpath1);
  302. vfs_path_free (vpath2);
  303. vfs_path_free (vpath3);
  304. }
  305. /* *INDENT-OFF* */
  306. END_PARAMETRIZED_TEST
  307. /* *INDENT-ON* */
  308. /* --------------------------------------------------------------------------------------------- */
  309. /* @DataSource("test_vfs_path_relative_ds") */
  310. /* *INDENT-OFF* */
  311. static const struct test_vfs_path_relative_ds
  312. {
  313. const char *input_path;
  314. const char *expected_path;
  315. const char *expected_last_path_in_element;
  316. } test_vfs_path_relative_ds[] =
  317. {
  318. { /* 0. */
  319. "../bla-bla",
  320. "../bla-bla",
  321. "../bla-bla"
  322. },
  323. { /* 1. */
  324. "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
  325. "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
  326. "bla-bla/some/path/"
  327. },
  328. };
  329. /* *INDENT-ON* */
  330. /* @Test(dataSource = "test_vfs_path_relative_ds") */
  331. /* *INDENT-OFF* */
  332. START_PARAMETRIZED_TEST (test_vfs_path_relative, test_vfs_path_relative_ds)
  333. /* *INDENT-ON* */
  334. {
  335. /* given */
  336. vfs_path_t *vpath;
  337. /* when */
  338. vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
  339. /* then */
  340. mctest_assert_int_eq (vpath->relative, TRUE);
  341. mctest_assert_str_eq (vfs_path_get_last_path_str (vpath), data->expected_last_path_in_element);
  342. mctest_assert_str_eq (vfs_path_as_str (vpath), data->expected_path);
  343. vfs_path_free (vpath);
  344. }
  345. /* *INDENT-OFF* */
  346. END_PARAMETRIZED_TEST
  347. /* *INDENT-ON* */
  348. /* --------------------------------------------------------------------------------------------- */
  349. /* @Test(dataSource = "test_vfs_path_relative_ds") */
  350. /* *INDENT-OFF* */
  351. START_PARAMETRIZED_TEST (test_vfs_path_relative_clone, test_vfs_path_relative_ds)
  352. /* *INDENT-ON* */
  353. {
  354. /* given */
  355. vfs_path_t *vpath, *cloned_vpath;
  356. vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
  357. /* when */
  358. cloned_vpath = vfs_path_clone (vpath);
  359. /* then */
  360. mctest_assert_int_eq (cloned_vpath->relative, TRUE);
  361. mctest_assert_str_eq (vfs_path_get_last_path_str (cloned_vpath),
  362. data->expected_last_path_in_element);
  363. mctest_assert_str_eq (vfs_path_as_str (cloned_vpath), data->expected_path);
  364. vfs_path_free (vpath);
  365. vfs_path_free (cloned_vpath);
  366. }
  367. /* *INDENT-OFF* */
  368. END_PARAMETRIZED_TEST
  369. /* *INDENT-ON* */
  370. /* --------------------------------------------------------------------------------------------- */
  371. int
  372. main (void)
  373. {
  374. int number_failed;
  375. Suite *s = suite_create (TEST_SUITE_NAME);
  376. TCase *tc_core = tcase_create ("Core");
  377. SRunner *sr;
  378. tcase_add_checked_fixture (tc_core, setup, teardown);
  379. /* Add new tests here: *************** */
  380. mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_count,
  381. test_vfs_path_tokens_count_ds);
  382. mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds);
  383. mctest_add_parameterized_test (tc_core, test_vfs_path_append_vpath,
  384. test_vfs_path_append_vpath_ds);
  385. mctest_add_parameterized_test (tc_core, test_vfs_path_relative, test_vfs_path_relative_ds);
  386. mctest_add_parameterized_test (tc_core, test_vfs_path_relative_clone,
  387. test_vfs_path_relative_ds);
  388. /* *********************************** */
  389. suite_add_tcase (s, tc_core);
  390. sr = srunner_create (s);
  391. srunner_set_log (sr, "path_manipulations.log");
  392. srunner_run_all (sr, CK_ENV);
  393. number_failed = srunner_ntests_failed (sr);
  394. srunner_free (sr);
  395. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  396. }
  397. /* --------------------------------------------------------------------------------------------- */