path_manipulations.c 13 KB

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