path_recode.c 7.4 KB

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