path_recode.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. lib/vfs - vfs_path_t charset recode functions
  3. Copyright (C) 2011-2017
  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. #include "lib/charsets.h"
  22. #include "lib/strutil.h"
  23. #include "lib/vfs/xdirentry.h"
  24. #include "lib/vfs/path.h"
  25. #include "src/vfs/local/local.c"
  26. /* --------------------------------------------------------------------------------------------- */
  27. /* @Mock */
  28. const char *
  29. mc_config_get_home_dir (void)
  30. {
  31. return "/mock/home";
  32. }
  33. /* --------------------------------------------------------------------------------------------- */
  34. /* @Before */
  35. static void
  36. setup (void)
  37. {
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* @After */
  41. static void
  42. teardown (void)
  43. {
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. static void
  47. test_init_vfs (const char *encoding)
  48. {
  49. str_init_strings (encoding);
  50. vfs_init ();
  51. init_localfs ();
  52. vfs_setup_work_dir ();
  53. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  54. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  55. load_codepages_list ();
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. static void
  59. test_deinit_vfs (void)
  60. {
  61. free_codepages_list ();
  62. str_uninit_strings ();
  63. vfs_shut ();
  64. }
  65. /* --------------------------------------------------------------------------------------------- */
  66. /* @DataSource("test_path_recode_ds") */
  67. /* *INDENT-OFF* */
  68. static const struct test_path_recode_ds
  69. {
  70. const char *input_codepage;
  71. const char *input_path;
  72. const char *expected_element_path;
  73. const char *expected_recoded_path;
  74. } test_path_recode_ds[] =
  75. {
  76. { /* 0. */
  77. "UTF-8",
  78. "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ",
  79. "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ",
  80. "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  81. },
  82. { /* 1. */
  83. "UTF-8",
  84. "/#enc:KOI8-R/те�товый/путь",
  85. "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ",
  86. "/#enc:KOI8-R/те�товый/путь"
  87. },
  88. { /* 2. */
  89. "KOI8-R",
  90. "/те�товый/путь",
  91. "/те�товый/путь",
  92. "/те�товый/путь"
  93. },
  94. { /* 3. */
  95. "KOI8-R",
  96. "/#enc:UTF-8/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ",
  97. "/те�товый/путь",
  98. "/#enc:UTF-8/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  99. },
  100. { /* 4. Test encode info at start */
  101. "UTF-8",
  102. "#enc:KOI8-R/bla-bla/some/path",
  103. "/bla-bla/some/path",
  104. "/#enc:KOI8-R/bla-bla/some/path"
  105. },
  106. };
  107. /* *INDENT-ON* */
  108. /* @Test(dataSource = "test_path_recode_ds") */
  109. /* *INDENT-OFF* */
  110. START_PARAMETRIZED_TEST (test_path_recode, test_path_recode_ds)
  111. /* *INDENT-ON* */
  112. {
  113. /* given */
  114. vfs_path_t *vpath;
  115. const vfs_path_element_t *element;
  116. const char *vpath_str;
  117. test_init_vfs (data->input_codepage);
  118. /* when */
  119. vpath = vfs_path_from_str (data->input_path);
  120. element = vfs_path_get_by_index (vpath, -1);
  121. /* then */
  122. vpath_str = vfs_path_as_str (vpath);
  123. mctest_assert_ptr_ne (vpath, NULL);
  124. mctest_assert_str_eq (element->path, data->expected_element_path);
  125. mctest_assert_str_eq (vpath_str, data->expected_recoded_path);
  126. vfs_path_free (vpath);
  127. test_deinit_vfs ();
  128. }
  129. /* *INDENT-OFF* */
  130. END_PARAMETRIZED_TEST
  131. /* *INDENT-ON* */
  132. /* --------------------------------------------------------------------------------------------- */
  133. static struct vfs_s_subclass test_subclass1;
  134. static struct vfs_class vfs_test_ops1;
  135. /* @DataSource("test_path_to_str_flags_ds") */
  136. /* *INDENT-OFF* */
  137. static const struct test_path_to_str_flags_ds
  138. {
  139. const char *input_path;
  140. const vfs_path_flag_t input_from_str_flags;
  141. const vfs_path_flag_t input_to_str_flags;
  142. const char *expected_path;
  143. } test_path_to_str_flags_ds[] =
  144. {
  145. { /* 0. */
  146. "test1://user:passwd@127.0.0.1",
  147. VPF_NO_CANON,
  148. VPF_STRIP_PASSWORD,
  149. "test1://user@127.0.0.1/"
  150. },
  151. { /* 1. */
  152. "/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  153. VPF_NONE,
  154. VPF_STRIP_PASSWORD,
  155. "/test1://user@host.name/#enc:KOI8-R/те�товый/путь"
  156. },
  157. { /* 2. */
  158. "/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  159. VPF_NONE,
  160. VPF_RECODE,
  161. "/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  162. },
  163. { /* 3. */
  164. "/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  165. VPF_NONE,
  166. VPF_RECODE | VPF_STRIP_PASSWORD,
  167. "/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  168. },
  169. { /* 4. */
  170. "/mock/home/test/dir",
  171. VPF_NONE,
  172. VPF_STRIP_HOME,
  173. "~/test/dir"
  174. },
  175. { /* 5. */
  176. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  177. VPF_NONE,
  178. VPF_STRIP_HOME | VPF_STRIP_PASSWORD,
  179. "~/test1://user@host.name/#enc:KOI8-R/те�товый/путь"
  180. },
  181. { /* 6. */
  182. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  183. VPF_NONE,
  184. VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET,
  185. "~/test1://user@host.name/те�товый/путь"
  186. },
  187. { /* 7. */
  188. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  189. VPF_NONE,
  190. VPF_STRIP_HOME | VPF_RECODE,
  191. "~/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  192. },
  193. { /* 8. */
  194. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  195. VPF_NONE,
  196. VPF_STRIP_HOME | VPF_RECODE | VPF_STRIP_PASSWORD,
  197. "~/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  198. },
  199. };
  200. /* *INDENT-ON* */
  201. /* @Test(dataSource = "test_path_to_str_flags_ds") */
  202. /* *INDENT-OFF* */
  203. START_PARAMETRIZED_TEST (test_path_to_str_flags, test_path_to_str_flags_ds)
  204. /* *INDENT-ON* */
  205. {
  206. /* given */
  207. vfs_path_t *vpath;
  208. char *str_path;
  209. test_init_vfs ("UTF-8");
  210. test_subclass1.flags = VFS_S_REMOTE;
  211. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  212. vfs_test_ops1.name = "testfs1";
  213. vfs_test_ops1.flags = VFSF_NOLINKS;
  214. vfs_test_ops1.prefix = "test1";
  215. vfs_register_class (&vfs_test_ops1);
  216. /* when */
  217. vpath = vfs_path_from_str_flags (data->input_path, data->input_from_str_flags);
  218. str_path = vfs_path_to_str_flags (vpath, 0, data->input_to_str_flags);
  219. /* then */
  220. mctest_assert_str_eq (str_path, data->expected_path);
  221. g_free (str_path);
  222. vfs_path_free (vpath);
  223. test_deinit_vfs ();
  224. }
  225. /* *INDENT-OFF* */
  226. END_PARAMETRIZED_TEST
  227. /* *INDENT-ON* */
  228. /* --------------------------------------------------------------------------------------------- */
  229. int
  230. main (void)
  231. {
  232. int number_failed;
  233. Suite *s = suite_create (TEST_SUITE_NAME);
  234. TCase *tc_core = tcase_create ("Core");
  235. SRunner *sr;
  236. tcase_add_checked_fixture (tc_core, setup, teardown);
  237. /* Add new tests here: *************** */
  238. mctest_add_parameterized_test (tc_core, test_path_recode, test_path_recode_ds);
  239. mctest_add_parameterized_test (tc_core, test_path_to_str_flags, test_path_to_str_flags_ds);
  240. /* *********************************** */
  241. suite_add_tcase (s, tc_core);
  242. sr = srunner_create (s);
  243. srunner_set_log (sr, "path_recode.log");
  244. srunner_run_all (sr, CK_ENV);
  245. number_failed = srunner_ntests_failed (sr);
  246. srunner_free (sr);
  247. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  248. }
  249. /* --------------------------------------------------------------------------------------------- */