path_recode.c 7.4 KB

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