path_recode.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. lib/vfs - vfs_path_t charset recode functions
  3. Copyright (C) 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011
  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 <config.h>
  21. #include <check.h>
  22. #include "lib/global.c"
  23. #include "lib/charsets.h"
  24. #include "lib/strutil.h"
  25. #include "lib/vfs/xdirentry.h"
  26. #include "lib/vfs/path.h"
  27. #include "src/vfs/local/local.c"
  28. const char *mc_config_get_home_dir (void);
  29. const char *
  30. mc_config_get_home_dir (void)
  31. {
  32. return "/mock/home";
  33. }
  34. static void
  35. setup (void)
  36. {
  37. }
  38. static void
  39. teardown (void)
  40. {
  41. }
  42. /* --------------------------------------------------------------------------------------------- */
  43. static void test_init_vfs(const char *encoding)
  44. {
  45. str_init_strings (encoding);
  46. vfs_init ();
  47. init_localfs ();
  48. vfs_setup_work_dir ();
  49. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  50. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  51. load_codepages_list ();
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. static void test_deinit_vfs()
  55. {
  56. free_codepages_list ();
  57. str_uninit_strings ();
  58. vfs_shut ();
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. #define path_recode_one_check(input, etalon1, etalon2) {\
  62. vpath = vfs_path_from_str (input);\
  63. element = vfs_path_get_by_index(vpath, -1);\
  64. fail_unless ( strcmp (element->path, etalon1) == 0, "expected: %s\nactual: %s\n", etalon1, element->path);\
  65. result = vfs_path_to_str(vpath);\
  66. fail_unless ( strcmp (result, etalon2) == 0, "\nexpected: %s\nactual: %s\n", etalon2, result);\
  67. g_free(result);\
  68. vfs_path_free (vpath);\
  69. }
  70. START_TEST (test_path_recode_base_utf8)
  71. {
  72. vfs_path_t *vpath;
  73. char *result;
  74. const vfs_path_element_t *element;
  75. test_init_vfs("UTF-8");
  76. path_recode_one_check("/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ");
  77. path_recode_one_check("/#enc:KOI8-R/теÑ�товый/путь", "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/#enc:KOI8-R/теÑ�товый/путь");
  78. test_deinit_vfs();
  79. }
  80. END_TEST
  81. /* --------------------------------------------------------------------------------------------- */
  82. START_TEST (test_path_recode_base_koi8r)
  83. {
  84. vfs_path_t *vpath;
  85. char *result;
  86. const vfs_path_element_t *element;
  87. test_init_vfs("KOI8-R");
  88. path_recode_one_check("/те�товый/путь", "/те�товый/путь", "/те�товый/путь");
  89. path_recode_one_check("/#enc:UTF-8/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/теÑ�товый/путь", "/#enc:UTF-8/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ");
  90. test_deinit_vfs();
  91. }
  92. END_TEST
  93. /* --------------------------------------------------------------------------------------------- */
  94. struct vfs_s_subclass test_subclass1;
  95. struct vfs_class vfs_test_ops1;
  96. START_TEST(test_path_to_str_flags)
  97. {
  98. vfs_path_t *vpath;
  99. char *str_path;
  100. test_init_vfs("UTF-8");
  101. test_subclass1.flags = VFS_S_REMOTE;
  102. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  103. vfs_test_ops1.name = "testfs1";
  104. vfs_test_ops1.flags = VFSF_NOLINKS;
  105. vfs_test_ops1.prefix = "test1";
  106. vfs_register_class (&vfs_test_ops1);
  107. vpath = vfs_path_from_str_flags ("test1://user:passwd@127.0.0.1", VPF_NO_CANON);
  108. str_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
  109. fail_unless (strcmp ("test1://user@127.0.0.1/", str_path) == 0, "\nstr=%s\n", str_path);
  110. g_free (str_path);
  111. vfs_path_free (vpath);
  112. vpath = vfs_path_from_str ("/test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь");
  113. str_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
  114. fail_unless (strcmp ("/test1://user@host.name/#enc:KOI8-R/те�товый/путь", str_path) == 0, "\nstr=%s\n", str_path);
  115. g_free (str_path);
  116. str_path = vfs_path_to_str_flags (vpath, 0, VPF_RECODE);
  117. fail_unless (strcmp ("/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path) == 0, "\nstr=%s\n", str_path);
  118. g_free (str_path);
  119. str_path = vfs_path_to_str_flags (vpath, 0, VPF_RECODE | VPF_STRIP_PASSWORD);
  120. fail_unless (strcmp ("/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path) == 0, "\nstr=%s\n", str_path);
  121. g_free (str_path);
  122. vfs_path_free (vpath);
  123. vpath = vfs_path_build_filename (mc_config_get_home_dir (), "test", "dir", NULL);
  124. str_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME);
  125. fail_unless (strcmp ("~/test/dir", str_path) == 0, "\nstr=%s\n", str_path);
  126. g_free (str_path);
  127. vfs_path_free (vpath);
  128. vpath = vfs_path_build_filename (mc_config_get_home_dir (), "test1://user:passwd@host.name/#enc:KOI8-R/те�товый/путь", NULL);
  129. str_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
  130. fail_unless (strcmp ("~/test1://user@host.name/#enc:KOI8-R/те�товый/путь", str_path) == 0, "\nstr=%s\n", str_path);
  131. g_free (str_path);
  132. str_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
  133. fail_unless (strcmp ("~/test1://user@host.name/те�товый/путь", str_path) == 0, "\nstr=%s\n", str_path);
  134. g_free (str_path);
  135. str_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_RECODE);
  136. fail_unless (strcmp ("~/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path) == 0, "\nstr=%s\n", str_path);
  137. g_free (str_path);
  138. str_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_RECODE | VPF_STRIP_PASSWORD);
  139. fail_unless (strcmp ("~/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path) == 0, "\nstr=%s\n", str_path);
  140. g_free (str_path);
  141. vfs_path_free (vpath);
  142. test_deinit_vfs();
  143. }
  144. END_TEST
  145. /* --------------------------------------------------------------------------------------------- */
  146. START_TEST(test_encode_info_at_start)
  147. {
  148. vfs_path_t *vpath;
  149. char *actual;
  150. const vfs_path_element_t *vpath_element;
  151. test_init_vfs("UTF-8");
  152. vpath = vfs_path_from_str ("#enc:KOI8-R/bla-bla/some/path");
  153. actual = vfs_path_to_str (vpath);
  154. fail_unless (strcmp ("/#enc:KOI8-R/bla-bla/some/path", actual) == 0, "\nactual=%s\n", actual);
  155. vpath_element = vfs_path_get_by_index (vpath, -1);
  156. fail_unless (strcmp ("/bla-bla/some/path", vpath_element->path) == 0, "\nvpath_element->path=%s\n", vpath_element->path);
  157. g_free (actual);
  158. vfs_path_free (vpath);
  159. test_deinit_vfs();
  160. }
  161. END_TEST
  162. /* --------------------------------------------------------------------------------------------- */
  163. int
  164. main (void)
  165. {
  166. int number_failed;
  167. Suite *s = suite_create (TEST_SUITE_NAME);
  168. TCase *tc_core = tcase_create ("Core");
  169. SRunner *sr;
  170. tcase_add_checked_fixture (tc_core, setup, teardown);
  171. /* Add new tests here: *************** */
  172. tcase_add_test (tc_core, test_path_recode_base_utf8);
  173. tcase_add_test (tc_core, test_path_recode_base_koi8r);
  174. tcase_add_test (tc_core, test_path_to_str_flags);
  175. tcase_add_test (tc_core, test_encode_info_at_start);
  176. /* *********************************** */
  177. suite_add_tcase (s, tc_core);
  178. sr = srunner_create (s);
  179. srunner_print (sr, CK_VERBOSE);
  180. srunner_set_log (sr, "path_recode.log");
  181. srunner_run_all (sr, CK_NORMAL);
  182. number_failed = srunner_ntests_failed (sr);
  183. srunner_free (sr);
  184. return (number_failed == 0) ? 0 : 1;
  185. }
  186. /* --------------------------------------------------------------------------------------------- */