path_recode.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. lib/vfs - vfs_path_t charset recode functions
  3. Copyright (C) 2011-2025
  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 <https://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. vfs_init_localfs ();
  52. vfs_setup_work_dir ();
  53. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  54. load_codepages_list ();
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. static void
  58. test_deinit_vfs (void)
  59. {
  60. free_codepages_list ();
  61. str_uninit_strings ();
  62. vfs_shut ();
  63. }
  64. /* --------------------------------------------------------------------------------------------- */
  65. /* @DataSource("test_path_recode_ds") */
  66. static const struct test_path_recode_ds
  67. {
  68. const char *input_codepage;
  69. const char *input_path;
  70. const char *expected_element_path;
  71. const char *expected_recoded_path;
  72. } test_path_recode_ds[] = {
  73. {
  74. // 0.
  75. "UTF-8",
  76. "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  77. "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  78. "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  79. },
  80. {
  81. // 1.
  82. "UTF-8",
  83. "/#enc:KOI8-R/тестовый/путь",
  84. "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  85. "/#enc:KOI8-R/тестовый/путь",
  86. },
  87. {
  88. // 2.
  89. "KOI8-R",
  90. "/тестовый/путь",
  91. "/тестовый/путь",
  92. "/тестовый/путь",
  93. },
  94. {
  95. // 3.
  96. "KOI8-R",
  97. "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  98. "/тестовый/путь",
  99. "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  100. },
  101. {
  102. // 4. Test encode info at start
  103. "UTF-8",
  104. "#enc:KOI8-R/bla-bla/some/path",
  105. "/bla-bla/some/path",
  106. "/#enc:KOI8-R/bla-bla/some/path",
  107. },
  108. };
  109. /* @Test(dataSource = "test_path_recode_ds") */
  110. START_PARAMETRIZED_TEST (test_path_recode, test_path_recode_ds)
  111. {
  112. // given
  113. vfs_path_t *vpath;
  114. const char *element_path;
  115. const char *vpath_str;
  116. test_init_vfs (data->input_codepage);
  117. // when
  118. vpath = vfs_path_from_str (data->input_path);
  119. element_path = vfs_path_get_last_path_str (vpath);
  120. // then
  121. vpath_str = vfs_path_as_str (vpath);
  122. mctest_assert_ptr_ne (vpath, NULL);
  123. mctest_assert_str_eq (element_path, data->expected_element_path);
  124. mctest_assert_str_eq (vpath_str, data->expected_recoded_path);
  125. vfs_path_free (vpath, TRUE);
  126. test_deinit_vfs ();
  127. }
  128. END_PARAMETRIZED_TEST
  129. /* --------------------------------------------------------------------------------------------- */
  130. static struct vfs_class vfs_test_ops1;
  131. /* @DataSource("test_path_to_str_flags_ds") */
  132. static const struct test_path_to_str_flags_ds
  133. {
  134. const char *input_path;
  135. const vfs_path_flag_t input_from_str_flags;
  136. const vfs_path_flag_t input_to_str_flags;
  137. const char *expected_path;
  138. } test_path_to_str_flags_ds[] = {
  139. {
  140. // 0.
  141. "test1://user:passwd@127.0.0.1",
  142. VPF_NO_CANON,
  143. VPF_STRIP_PASSWORD,
  144. "test1://user@127.0.0.1/",
  145. },
  146. {
  147. // 1.
  148. "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  149. VPF_NONE,
  150. VPF_STRIP_PASSWORD,
  151. "/test1://user@host.name/#enc:KOI8-R/тестовый/путь",
  152. },
  153. {
  154. // 2.
  155. "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  156. VPF_NONE,
  157. VPF_RECODE,
  158. "/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  159. },
  160. {
  161. // 3.
  162. "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  163. VPF_NONE,
  164. VPF_RECODE | VPF_STRIP_PASSWORD,
  165. "/test1://user@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  166. },
  167. {
  168. // 4.
  169. "/mock/home/test/dir",
  170. VPF_NONE,
  171. VPF_STRIP_HOME,
  172. "~/test/dir",
  173. },
  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. {
  182. // 6.
  183. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  184. VPF_NONE,
  185. VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET,
  186. "~/test1://user@host.name/тестовый/путь",
  187. },
  188. {
  189. // 7.
  190. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  191. VPF_NONE,
  192. VPF_STRIP_HOME | VPF_RECODE,
  193. "~/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  194. },
  195. {
  196. // 8.
  197. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  198. VPF_NONE,
  199. VPF_STRIP_HOME | VPF_RECODE | VPF_STRIP_PASSWORD,
  200. "~/test1://user@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  201. },
  202. };
  203. /* @Test(dataSource = "test_path_to_str_flags_ds") */
  204. START_PARAMETRIZED_TEST (test_path_to_str_flags, test_path_to_str_flags_ds)
  205. {
  206. // given
  207. vfs_path_t *vpath;
  208. char *str_path;
  209. test_init_vfs ("UTF-8");
  210. vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  211. vfs_register_class (&vfs_test_ops1);
  212. // when
  213. vpath = vfs_path_from_str_flags (data->input_path, data->input_from_str_flags);
  214. str_path = vfs_path_to_str_flags (vpath, 0, data->input_to_str_flags);
  215. // then
  216. mctest_assert_str_eq (str_path, data->expected_path);
  217. g_free (str_path);
  218. vfs_path_free (vpath, TRUE);
  219. test_deinit_vfs ();
  220. }
  221. END_PARAMETRIZED_TEST
  222. /* --------------------------------------------------------------------------------------------- */
  223. int
  224. main (void)
  225. {
  226. TCase *tc_core;
  227. tc_core = tcase_create ("Core");
  228. tcase_add_checked_fixture (tc_core, setup, teardown);
  229. // Add new tests here: ***************
  230. mctest_add_parameterized_test (tc_core, test_path_recode, test_path_recode_ds);
  231. mctest_add_parameterized_test (tc_core, test_path_to_str_flags, test_path_to_str_flags_ds);
  232. // ***********************************
  233. return mctest_run_all (tc_core);
  234. }
  235. /* --------------------------------------------------------------------------------------------- */