vfs_path_string_convert.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. lib/vfs - get vfs_path_t from string
  3. Copyright (C) 2011, 2013
  4. The 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. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  29. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  30. #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
  31. #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  32. /* --------------------------------------------------------------------------------------------- */
  33. /* @Before */
  34. static void
  35. setup (void)
  36. {
  37. str_init_strings (NULL);
  38. vfs_init ();
  39. init_localfs ();
  40. vfs_setup_work_dir ();
  41. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  42. vfs_test_ops1.name = "testfs1";
  43. vfs_test_ops1.flags = VFSF_NOLINKS;
  44. vfs_test_ops1.prefix = "test1";
  45. vfs_register_class (&vfs_test_ops1);
  46. test_subclass2.flags = VFS_S_REMOTE;
  47. vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
  48. vfs_test_ops2.name = "testfs2";
  49. vfs_test_ops2.prefix = "test2";
  50. vfs_register_class (&vfs_test_ops2);
  51. vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
  52. vfs_test_ops3.name = "testfs3";
  53. vfs_test_ops3.prefix = "test3";
  54. vfs_register_class (&vfs_test_ops3);
  55. #ifdef HAVE_CHARSET
  56. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  57. load_codepages_list ();
  58. #endif /* HAVE_CHARSET */
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. /* @After */
  62. static void
  63. teardown (void)
  64. {
  65. #ifdef HAVE_CHARSET
  66. free_codepages_list ();
  67. #endif /* HAVE_CHARSET */
  68. vfs_shut ();
  69. str_uninit_strings ();
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. /* @DataSource("test_from_to_string_ds") */
  73. /* *INDENT-OFF* */
  74. static const struct test_from_to_string_ds
  75. {
  76. const char *input_string;
  77. const char *expected_result;
  78. const char *expected_element_path;
  79. const size_t expected_elements_count;
  80. struct vfs_class *expected_vfs_class;
  81. } test_from_to_string_ds[] =
  82. {
  83. { /* 0. */
  84. ETALON_PATH_STR,
  85. ETALON_PATH_URL_STR,
  86. "111/22/33",
  87. 4,
  88. &vfs_test_ops3
  89. },
  90. { /* 1. */
  91. "/",
  92. "/",
  93. "/",
  94. 1,
  95. &vfs_local_ops
  96. },
  97. { /* 2. */
  98. "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
  99. "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
  100. "111/22/33",
  101. 4,
  102. &vfs_test_ops3
  103. },
  104. #ifdef HAVE_CHARSET
  105. { /* 3. */
  106. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  107. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  108. "111/22/33",
  109. 4,
  110. &vfs_test_ops3
  111. },
  112. { /* 4. */
  113. "/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  114. "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  115. "111/22/33",
  116. 4,
  117. &vfs_test_ops3
  118. },
  119. { /* 5. */
  120. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
  121. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  122. "111/22/33",
  123. 4,
  124. &vfs_test_ops3
  125. },
  126. { /* 6. */
  127. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
  128. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  129. "111/22/33",
  130. 4,
  131. &vfs_test_ops3
  132. },
  133. { /* 7. */
  134. "/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  135. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  136. "111/22/33",
  137. 4,
  138. &vfs_test_ops3
  139. },
  140. { /* 8. */
  141. "/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  142. "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
  143. "111/22/33",
  144. 4,
  145. &vfs_test_ops3
  146. },
  147. #endif /* HAVE_CHARSET */
  148. };
  149. /* *INDENT-ON* */
  150. /* @Test */
  151. /* *INDENT-OFF* */
  152. START_PARAMETRIZED_TEST (test_from_to_string, test_from_to_string_ds)
  153. /* *INDENT-ON* */
  154. {
  155. /* given */
  156. vfs_path_t *vpath;
  157. size_t vpath_len;
  158. const vfs_path_element_t *path_element;
  159. const char *actual_result;
  160. vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
  161. /* when */
  162. vpath_len = vfs_path_elements_count (vpath);
  163. actual_result = vfs_path_as_str (vpath);
  164. path_element = vfs_path_get_by_index (vpath, -1);
  165. /* then */
  166. mctest_assert_int_eq (vpath_len, data->expected_elements_count);
  167. mctest_assert_str_eq (actual_result, data->expected_result);
  168. mctest_assert_ptr_eq (path_element->class, data->expected_vfs_class);
  169. mctest_assert_str_eq (path_element->path, data->expected_element_path);
  170. vfs_path_free (vpath);
  171. }
  172. /* *INDENT-OFF* */
  173. END_PARAMETRIZED_TEST
  174. /* *INDENT-ON* */
  175. /* --------------------------------------------------------------------------------------------- */
  176. /* @DataSource("test_partial_string_by_index_ds") */
  177. /* *INDENT-OFF* */
  178. static const struct test_partial_string_by_index_ds
  179. {
  180. const char *input_string;
  181. const off_t element_index;
  182. const char *expected_result;
  183. } test_partial_string_by_index_ds[] =
  184. {
  185. { /* 0. */
  186. ETALON_PATH_STR,
  187. -1,
  188. "/test1://bla-bla/some/path/test2://bla-bla/some/path"
  189. },
  190. { /* 1. */
  191. ETALON_PATH_STR,
  192. -2,
  193. "/test1://bla-bla/some/path/"
  194. },
  195. { /* 2. */
  196. ETALON_PATH_STR,
  197. -3,
  198. "/"
  199. },
  200. { /* 3. Index out of bound */
  201. ETALON_PATH_STR,
  202. -4,
  203. ""
  204. },
  205. { /* 4. */
  206. ETALON_PATH_STR,
  207. 1,
  208. "/"
  209. },
  210. { /* 5. */
  211. ETALON_PATH_STR,
  212. 2,
  213. "/test1://bla-bla/some/path/"
  214. },
  215. { /* 6. */
  216. ETALON_PATH_STR,
  217. 3,
  218. "/test1://bla-bla/some/path/test2://bla-bla/some/path"
  219. },
  220. { /* 6. */
  221. ETALON_PATH_STR,
  222. 4,
  223. ETALON_PATH_URL_STR
  224. },
  225. { /* 7. Index out of bound */
  226. ETALON_PATH_STR,
  227. 5,
  228. ETALON_PATH_URL_STR
  229. },
  230. };
  231. /* *INDENT-ON* */
  232. /* @Test(dataSource = "test_partial_string_by_index_ds") */
  233. /* *INDENT-OFF* */
  234. START_PARAMETRIZED_TEST (test_partial_string_by_index, test_partial_string_by_index_ds)
  235. /* *INDENT-ON* */
  236. {
  237. /* given */
  238. vfs_path_t *vpath;
  239. char *actual_result;
  240. vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
  241. /* when */
  242. actual_result = vfs_path_to_str_elements_count (vpath, data->element_index);
  243. /* then */
  244. mctest_assert_str_eq (actual_result, data->expected_result);
  245. g_free (actual_result);
  246. vfs_path_free (vpath);
  247. }
  248. /* *INDENT-OFF* */
  249. END_PARAMETRIZED_TEST
  250. /* *INDENT-ON* */
  251. /* --------------------------------------------------------------------------------------------- */
  252. #ifdef HAVE_CHARSET
  253. /* --------------------------------------------------------------------------------------------- */
  254. #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
  255. /* @Test */
  256. /* *INDENT-OFF* */
  257. START_TEST (test_vfs_path_encoding_at_end)
  258. /* *INDENT-ON* */
  259. {
  260. /* given */
  261. vfs_path_t *vpath;
  262. const char *result;
  263. const vfs_path_element_t *element;
  264. vpath =
  265. vfs_path_from_str_flags ("/path/to/file.ext#test1:/#enc:KOI8-R", VPF_USE_DEPRECATED_PARSER);
  266. /* when */
  267. result = vfs_path_as_str (vpath);
  268. element = vfs_path_get_by_index (vpath, -1);
  269. /* then */
  270. mctest_assert_str_eq (element->path, "");
  271. mctest_assert_not_null (element->encoding);
  272. mctest_assert_str_eq (result, ETALON_STR);
  273. vfs_path_free (vpath);
  274. }
  275. /* *INDENT-OFF* */
  276. END_TEST
  277. /* *INDENT-ON* */
  278. #endif /* HAVE_CHARSET */
  279. /* --------------------------------------------------------------------------------------------- */
  280. int
  281. main (void)
  282. {
  283. int number_failed;
  284. Suite *s = suite_create (TEST_SUITE_NAME);
  285. TCase *tc_core = tcase_create ("Core");
  286. SRunner *sr;
  287. tcase_add_checked_fixture (tc_core, setup, teardown);
  288. /* Add new tests here: *************** */
  289. mctest_add_parameterized_test (tc_core, test_from_to_string, test_from_to_string_ds);
  290. mctest_add_parameterized_test (tc_core, test_partial_string_by_index,
  291. test_partial_string_by_index_ds);
  292. #ifdef HAVE_CHARSET
  293. tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
  294. #endif
  295. /* *********************************** */
  296. suite_add_tcase (s, tc_core);
  297. sr = srunner_create (s);
  298. srunner_set_log (sr, "vfs_path_string_convert.log");
  299. srunner_run_all (sr, CK_NORMAL);
  300. number_failed = srunner_ntests_failed (sr);
  301. srunner_free (sr);
  302. return (number_failed == 0) ? 0 : 1;
  303. }
  304. /* --------------------------------------------------------------------------------------------- */