path_recode.c 8.2 KB

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