path_recode.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. lib/vfs - vfs_path_t charset recode functions
  3. Copyright (C) 2011-2024
  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. 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. /* *INDENT-OFF* */
  67. static const struct test_path_recode_ds
  68. {
  69. const char *input_codepage;
  70. const char *input_path;
  71. const char *expected_element_path;
  72. const char *expected_recoded_path;
  73. } test_path_recode_ds[] =
  74. {
  75. { /* 0. */
  76. "UTF-8",
  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. "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
  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. { /* 2. */
  88. "KOI8-R",
  89. "/тестовый/путь",
  90. "/тестовый/путь",
  91. "/тестовый/путь"
  92. },
  93. { /* 3. */
  94. "KOI8-R",
  95. "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
  96. "/тестовый/путь",
  97. "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
  98. },
  99. { /* 4. Test encode info at start */
  100. "UTF-8",
  101. "#enc:KOI8-R/bla-bla/some/path",
  102. "/bla-bla/some/path",
  103. "/#enc:KOI8-R/bla-bla/some/path"
  104. },
  105. };
  106. /* *INDENT-ON* */
  107. /* @Test(dataSource = "test_path_recode_ds") */
  108. /* *INDENT-OFF* */
  109. START_PARAMETRIZED_TEST (test_path_recode, test_path_recode_ds)
  110. /* *INDENT-ON* */
  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. /* *INDENT-OFF* */
  129. END_PARAMETRIZED_TEST
  130. /* *INDENT-ON* */
  131. /* --------------------------------------------------------------------------------------------- */
  132. static struct vfs_class vfs_test_ops1;
  133. /* @DataSource("test_path_to_str_flags_ds") */
  134. /* *INDENT-OFF* */
  135. static const struct test_path_to_str_flags_ds
  136. {
  137. const char *input_path;
  138. const vfs_path_flag_t input_from_str_flags;
  139. const vfs_path_flag_t input_to_str_flags;
  140. const char *expected_path;
  141. } test_path_to_str_flags_ds[] =
  142. {
  143. { /* 0. */
  144. "test1://user:passwd@127.0.0.1",
  145. VPF_NO_CANON,
  146. VPF_STRIP_PASSWORD,
  147. "test1://user@127.0.0.1/"
  148. },
  149. { /* 1. */
  150. "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  151. VPF_NONE,
  152. VPF_STRIP_PASSWORD,
  153. "/test1://user@host.name/#enc:KOI8-R/тестовый/путь"
  154. },
  155. { /* 2. */
  156. "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  157. VPF_NONE,
  158. VPF_RECODE,
  159. "/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
  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. { /* 4. */
  168. "/mock/home/test/dir",
  169. VPF_NONE,
  170. VPF_STRIP_HOME,
  171. "~/test/dir"
  172. },
  173. { /* 5. */
  174. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  175. VPF_NONE,
  176. VPF_STRIP_HOME | VPF_STRIP_PASSWORD,
  177. "~/test1://user@host.name/#enc:KOI8-R/тестовый/путь"
  178. },
  179. { /* 6. */
  180. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  181. VPF_NONE,
  182. VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET,
  183. "~/test1://user@host.name/тестовый/путь"
  184. },
  185. { /* 7. */
  186. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  187. VPF_NONE,
  188. VPF_STRIP_HOME | VPF_RECODE,
  189. "~/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
  190. },
  191. { /* 8. */
  192. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
  193. VPF_NONE,
  194. VPF_STRIP_HOME | VPF_RECODE | VPF_STRIP_PASSWORD,
  195. "~/test1://user@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
  196. },
  197. };
  198. /* *INDENT-ON* */
  199. /* @Test(dataSource = "test_path_to_str_flags_ds") */
  200. /* *INDENT-OFF* */
  201. START_PARAMETRIZED_TEST (test_path_to_str_flags, test_path_to_str_flags_ds)
  202. /* *INDENT-ON* */
  203. {
  204. /* given */
  205. vfs_path_t *vpath;
  206. char *str_path;
  207. test_init_vfs ("UTF-8");
  208. vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  209. vfs_register_class (&vfs_test_ops1);
  210. /* when */
  211. vpath = vfs_path_from_str_flags (data->input_path, data->input_from_str_flags);
  212. str_path = vfs_path_to_str_flags (vpath, 0, data->input_to_str_flags);
  213. /* then */
  214. mctest_assert_str_eq (str_path, data->expected_path);
  215. g_free (str_path);
  216. vfs_path_free (vpath, TRUE);
  217. test_deinit_vfs ();
  218. }
  219. /* *INDENT-OFF* */
  220. END_PARAMETRIZED_TEST
  221. /* *INDENT-ON* */
  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. /* --------------------------------------------------------------------------------------------- */