path_recode.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. lib/vfs - vfs_path_t charset recode functions
  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. #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. 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_class vfs_test_ops1;
  134. /* @DataSource("test_path_to_str_flags_ds") */
  135. /* *INDENT-OFF* */
  136. static const struct test_path_to_str_flags_ds
  137. {
  138. const char *input_path;
  139. const vfs_path_flag_t input_from_str_flags;
  140. const vfs_path_flag_t input_to_str_flags;
  141. const char *expected_path;
  142. } test_path_to_str_flags_ds[] =
  143. {
  144. { /* 0. */
  145. "test1://user:passwd@127.0.0.1",
  146. VPF_NO_CANON,
  147. VPF_STRIP_PASSWORD,
  148. "test1://user@127.0.0.1/"
  149. },
  150. { /* 1. */
  151. "/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  152. VPF_NONE,
  153. VPF_STRIP_PASSWORD,
  154. "/test1://user@host.name/#enc:KOI8-R/те�товый/путь"
  155. },
  156. { /* 2. */
  157. "/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  158. VPF_NONE,
  159. VPF_RECODE,
  160. "/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  161. },
  162. { /* 3. */
  163. "/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  164. VPF_NONE,
  165. VPF_RECODE | VPF_STRIP_PASSWORD,
  166. "/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  167. },
  168. { /* 4. */
  169. "/mock/home/test/dir",
  170. VPF_NONE,
  171. VPF_STRIP_HOME,
  172. "~/test/dir"
  173. },
  174. { /* 5. */
  175. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  176. VPF_NONE,
  177. VPF_STRIP_HOME | VPF_STRIP_PASSWORD,
  178. "~/test1://user@host.name/#enc:KOI8-R/те�товый/путь"
  179. },
  180. { /* 6. */
  181. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  182. VPF_NONE,
  183. VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET,
  184. "~/test1://user@host.name/те�товый/путь"
  185. },
  186. { /* 7. */
  187. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  188. VPF_NONE,
  189. VPF_STRIP_HOME | VPF_RECODE,
  190. "~/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  191. },
  192. { /* 8. */
  193. "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь",
  194. VPF_NONE,
  195. VPF_STRIP_HOME | VPF_RECODE | VPF_STRIP_PASSWORD,
  196. "~/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ"
  197. },
  198. };
  199. /* *INDENT-ON* */
  200. /* @Test(dataSource = "test_path_to_str_flags_ds") */
  201. /* *INDENT-OFF* */
  202. START_PARAMETRIZED_TEST (test_path_to_str_flags, test_path_to_str_flags_ds)
  203. /* *INDENT-ON* */
  204. {
  205. /* given */
  206. vfs_path_t *vpath;
  207. char *str_path;
  208. test_init_vfs ("UTF-8");
  209. vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  210. vfs_register_class (&vfs_test_ops1);
  211. /* when */
  212. vpath = vfs_path_from_str_flags (data->input_path, data->input_from_str_flags);
  213. str_path = vfs_path_to_str_flags (vpath, 0, data->input_to_str_flags);
  214. /* then */
  215. mctest_assert_str_eq (str_path, data->expected_path);
  216. g_free (str_path);
  217. vfs_path_free (vpath);
  218. test_deinit_vfs ();
  219. }
  220. /* *INDENT-OFF* */
  221. END_PARAMETRIZED_TEST
  222. /* *INDENT-ON* */
  223. /* --------------------------------------------------------------------------------------------- */
  224. int
  225. main (void)
  226. {
  227. int number_failed;
  228. Suite *s = suite_create (TEST_SUITE_NAME);
  229. TCase *tc_core = tcase_create ("Core");
  230. SRunner *sr;
  231. tcase_add_checked_fixture (tc_core, setup, teardown);
  232. /* Add new tests here: *************** */
  233. mctest_add_parameterized_test (tc_core, test_path_recode, test_path_recode_ds);
  234. mctest_add_parameterized_test (tc_core, test_path_to_str_flags, test_path_to_str_flags_ds);
  235. /* *********************************** */
  236. suite_add_tcase (s, tc_core);
  237. sr = srunner_create (s);
  238. srunner_set_log (sr, "path_recode.log");
  239. srunner_run_all (sr, CK_ENV);
  240. number_failed = srunner_ntests_failed (sr);
  241. srunner_free (sr);
  242. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  243. }
  244. /* --------------------------------------------------------------------------------------------- */