vfs_path_string_convert.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. lib/vfs - get vfs_path_t from string
  3. Copyright (C) 2011-2019
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/vfs"
  20. #include "tests/mctest.h"
  21. #ifdef HAVE_CHARSET
  22. #include "lib/charsets.h"
  23. #endif
  24. #include "lib/strutil.h"
  25. #include "lib/vfs/xdirentry.h"
  26. #include "lib/vfs/path.c" /* for testing static methods */
  27. #include "src/vfs/local/local.c"
  28. static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  29. #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
  30. #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  31. /* --------------------------------------------------------------------------------------------- */
  32. /* @Before */
  33. static void
  34. setup (void)
  35. {
  36. str_init_strings (NULL);
  37. vfs_init ();
  38. vfs_init_localfs ();
  39. vfs_setup_work_dir ();
  40. vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS, "test1");
  41. vfs_register_class (&vfs_test_ops1);
  42. vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_REMOTE, "test2");
  43. vfs_register_class (&vfs_test_ops2);
  44. vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_UNKNOWN, "test3");
  45. vfs_register_class (&vfs_test_ops3);
  46. #ifdef HAVE_CHARSET
  47. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  48. load_codepages_list ();
  49. #endif /* HAVE_CHARSET */
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. /* @After */
  53. static void
  54. teardown (void)
  55. {
  56. #ifdef HAVE_CHARSET
  57. free_codepages_list ();
  58. #endif /* HAVE_CHARSET */
  59. vfs_shut ();
  60. str_uninit_strings ();
  61. }
  62. /* --------------------------------------------------------------------------------------------- */
  63. /* @DataSource("test_from_to_string_ds") */
  64. /* *INDENT-OFF* */
  65. static const struct test_from_to_string_ds
  66. {
  67. const char *input_string;
  68. const char *expected_result;
  69. const char *expected_element_path;
  70. const size_t expected_elements_count;
  71. struct vfs_class *expected_vfs_class;
  72. } test_from_to_string_ds[] =
  73. {
  74. { /* 0. */
  75. ETALON_PATH_STR,
  76. ETALON_PATH_URL_STR,
  77. "111/22/33",
  78. 4,
  79. &vfs_test_ops3
  80. },
  81. { /* 1. */
  82. "/",
  83. "/",
  84. "/",
  85. 1,
  86. VFS_CLASS (&local_subclass)
  87. },
  88. { /* 2. */
  89. "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
  90. "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
  91. "111/22/33",
  92. 4,
  93. &vfs_test_ops3
  94. },
  95. #ifdef HAVE_CHARSET
  96. { /* 3. */
  97. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  98. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  99. "111/22/33",
  100. 4,
  101. &vfs_test_ops3
  102. },
  103. { /* 4. */
  104. "/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  105. "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  106. "111/22/33",
  107. 4,
  108. &vfs_test_ops3
  109. },
  110. { /* 5. */
  111. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
  112. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  113. "111/22/33",
  114. 4,
  115. &vfs_test_ops3
  116. },
  117. { /* 6. */
  118. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
  119. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  120. "111/22/33",
  121. 4,
  122. &vfs_test_ops3
  123. },
  124. { /* 7. */
  125. "/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  126. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  127. "111/22/33",
  128. 4,
  129. &vfs_test_ops3
  130. },
  131. { /* 8. */
  132. "/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  133. "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  134. "111/22/33",
  135. 4,
  136. &vfs_test_ops3
  137. },
  138. #endif /* HAVE_CHARSET */
  139. };
  140. /* *INDENT-ON* */
  141. /* @Test */
  142. /* *INDENT-OFF* */
  143. START_PARAMETRIZED_TEST (test_from_to_string, test_from_to_string_ds)
  144. /* *INDENT-ON* */
  145. {
  146. /* given */
  147. vfs_path_t *vpath;
  148. size_t vpath_len;
  149. const vfs_path_element_t *path_element;
  150. const char *actual_result;
  151. vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
  152. /* when */
  153. vpath_len = vfs_path_elements_count (vpath);
  154. actual_result = vfs_path_as_str (vpath);
  155. path_element = vfs_path_get_by_index (vpath, -1);
  156. /* then */
  157. mctest_assert_int_eq (vpath_len, data->expected_elements_count);
  158. mctest_assert_str_eq (actual_result, data->expected_result);
  159. mctest_assert_ptr_eq (path_element->class, data->expected_vfs_class);
  160. mctest_assert_str_eq (path_element->path, data->expected_element_path);
  161. vfs_path_free (vpath);
  162. }
  163. /* *INDENT-OFF* */
  164. END_PARAMETRIZED_TEST
  165. /* *INDENT-ON* */
  166. /* --------------------------------------------------------------------------------------------- */
  167. /* @DataSource("test_partial_string_by_index_ds") */
  168. /* *INDENT-OFF* */
  169. static const struct test_partial_string_by_index_ds
  170. {
  171. const char *input_string;
  172. const off_t element_index;
  173. const char *expected_result;
  174. } test_partial_string_by_index_ds[] =
  175. {
  176. { /* 0. */
  177. ETALON_PATH_STR,
  178. -1,
  179. "/test1://bla-bla/some/path/test2://bla-bla/some/path"
  180. },
  181. { /* 1. */
  182. ETALON_PATH_STR,
  183. -2,
  184. "/test1://bla-bla/some/path/"
  185. },
  186. { /* 2. */
  187. ETALON_PATH_STR,
  188. -3,
  189. "/"
  190. },
  191. { /* 3. Index out of bound */
  192. ETALON_PATH_STR,
  193. -4,
  194. ""
  195. },
  196. { /* 4. */
  197. ETALON_PATH_STR,
  198. 1,
  199. "/"
  200. },
  201. { /* 5. */
  202. ETALON_PATH_STR,
  203. 2,
  204. "/test1://bla-bla/some/path/"
  205. },
  206. { /* 6. */
  207. ETALON_PATH_STR,
  208. 3,
  209. "/test1://bla-bla/some/path/test2://bla-bla/some/path"
  210. },
  211. { /* 6. */
  212. ETALON_PATH_STR,
  213. 4,
  214. ETALON_PATH_URL_STR
  215. },
  216. { /* 7. Index out of bound */
  217. ETALON_PATH_STR,
  218. 5,
  219. ETALON_PATH_URL_STR
  220. },
  221. };
  222. /* *INDENT-ON* */
  223. /* @Test(dataSource = "test_partial_string_by_index_ds") */
  224. /* *INDENT-OFF* */
  225. START_PARAMETRIZED_TEST (test_partial_string_by_index, test_partial_string_by_index_ds)
  226. /* *INDENT-ON* */
  227. {
  228. /* given */
  229. vfs_path_t *vpath;
  230. char *actual_result;
  231. vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
  232. /* when */
  233. actual_result = vfs_path_to_str_elements_count (vpath, data->element_index);
  234. /* then */
  235. mctest_assert_str_eq (actual_result, data->expected_result);
  236. g_free (actual_result);
  237. vfs_path_free (vpath);
  238. }
  239. /* *INDENT-OFF* */
  240. END_PARAMETRIZED_TEST
  241. /* *INDENT-ON* */
  242. /* --------------------------------------------------------------------------------------------- */
  243. #ifdef HAVE_CHARSET
  244. /* --------------------------------------------------------------------------------------------- */
  245. #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
  246. /* @Test */
  247. /* *INDENT-OFF* */
  248. START_TEST (test_vfs_path_encoding_at_end)
  249. /* *INDENT-ON* */
  250. {
  251. /* given */
  252. vfs_path_t *vpath;
  253. const char *result;
  254. const vfs_path_element_t *element;
  255. vpath =
  256. vfs_path_from_str_flags ("/path/to/file.ext#test1:/#enc:KOI8-R", VPF_USE_DEPRECATED_PARSER);
  257. /* when */
  258. result = vfs_path_as_str (vpath);
  259. element = vfs_path_get_by_index (vpath, -1);
  260. /* then */
  261. mctest_assert_str_eq (element->path, "");
  262. mctest_assert_not_null (element->encoding);
  263. mctest_assert_str_eq (result, ETALON_STR);
  264. vfs_path_free (vpath);
  265. }
  266. /* *INDENT-OFF* */
  267. END_TEST
  268. /* *INDENT-ON* */
  269. #endif /* HAVE_CHARSET */
  270. /* --------------------------------------------------------------------------------------------- */
  271. int
  272. main (void)
  273. {
  274. int number_failed;
  275. Suite *s = suite_create (TEST_SUITE_NAME);
  276. TCase *tc_core = tcase_create ("Core");
  277. SRunner *sr;
  278. tcase_add_checked_fixture (tc_core, setup, teardown);
  279. /* Add new tests here: *************** */
  280. mctest_add_parameterized_test (tc_core, test_from_to_string, test_from_to_string_ds);
  281. mctest_add_parameterized_test (tc_core, test_partial_string_by_index,
  282. test_partial_string_by_index_ds);
  283. #ifdef HAVE_CHARSET
  284. tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
  285. #endif
  286. /* *********************************** */
  287. suite_add_tcase (s, tc_core);
  288. sr = srunner_create (s);
  289. srunner_set_log (sr, "vfs_path_string_convert.log");
  290. srunner_run_all (sr, CK_ENV);
  291. number_failed = srunner_ntests_failed (sr);
  292. srunner_free (sr);
  293. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  294. }
  295. /* --------------------------------------------------------------------------------------------- */