2 lib/vfs - vfs_path_t charset recode functions
5 The Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2011
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "/lib/vfs"
32 #include "lib/global.c"
35 #define HAVE_CHARSET 1
38 #include "lib/charsets.h"
40 #include "lib/strutil.h"
41 #include "lib/vfs/xdirentry.h"
42 #include "lib/vfs/path.h"
44 #include "src/vfs/local/local.c"
46 const char *mc_config_get_home_dir (void);
49 mc_config_get_home_dir (void)
65 /* --------------------------------------------------------------------------------------------- */
67 #define path_recode_one_check(input, etalon1, etalon2) {\
68 vpath = vfs_path_from_str (input);\
69 element = vfs_path_get_by_index(vpath, -1);\
70 fail_unless ( strcmp (element->path, etalon1) == 0, "expected: %s\nactual: %s\n", etalon1, element->path);\
71 result = vfs_path_to_str(vpath);\
72 fail_unless ( strcmp (result, etalon2) == 0, "\nexpected: %s\nactual: %s\n", etalon2, result);\
74 vfs_path_free (vpath);\
77 START_TEST (test_path_recode_base_utf8
)
81 const vfs_path_element_t
*element
;
83 str_init_strings ("UTF-8");
87 vfs_setup_work_dir ();
89 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
91 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
92 load_codepages_list ();
94 path_recode_one_check("/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ");
96 path_recode_one_check("/#enc:KOI8-R/теÑ�товый/путь", "/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/#enc:KOI8-R/теÑ�товый/путь");
98 free_codepages_list ();
99 str_uninit_strings ();
105 /* --------------------------------------------------------------------------------------------- */
107 START_TEST (test_path_recode_base_koi8r
)
111 const vfs_path_element_t
*element
;
113 str_init_strings ("KOI8-R");
117 vfs_setup_work_dir ();
119 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
121 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
122 load_codepages_list ();
124 path_recode_one_check("/тестовый/путь", "/тестовый/путь", "/тестовый/путь");
126 path_recode_one_check("/#enc:UTF-8/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", "/теÑ�товый/путь", "/#enc:UTF-8/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ");
128 free_codepages_list ();
129 str_uninit_strings ();
135 /* --------------------------------------------------------------------------------------------- */
136 struct vfs_s_subclass test_subclass1
;
137 struct vfs_class vfs_test_ops1
;
139 START_TEST(test_path_to_str_flags
)
145 str_init_strings ("UTF-8");
150 vfs_setup_work_dir ();
151 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
152 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
153 load_codepages_list ();
155 test_subclass1
.flags
= VFS_S_REMOTE
;
156 vfs_s_init_class (&vfs_test_ops1
, &test_subclass1
);
157 vfs_test_ops1
.name
= "testfs1";
158 vfs_test_ops1
.flags
= VFSF_NOLINKS
;
159 vfs_test_ops1
.prefix
= "test1";
160 vfs_register_class (&vfs_test_ops1
);
162 vpath
= vfs_path_from_str_flags ("test1://user:passwd@127.0.0.1", VPF_NO_CANON
);
163 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_PASSWORD
);
164 fail_unless (strcmp ("test1://user@127.0.0.1/", str_path
) == 0, "\nstr=%s\n", str_path
);
166 vfs_path_free (vpath
);
168 vpath
= vfs_path_from_str ("/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь");
169 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_PASSWORD
);
170 fail_unless (strcmp ("/test1://user@host.name/#enc:KOI8-R/тестовый/путь", str_path
) == 0, "\nstr=%s\n", str_path
);
173 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_RECODE
);
174 fail_unless (strcmp ("/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path
) == 0, "\nstr=%s\n", str_path
);
177 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_RECODE
| VPF_STRIP_PASSWORD
);
178 fail_unless (strcmp ("/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path
) == 0, "\nstr=%s\n", str_path
);
181 vfs_path_free (vpath
);
183 vpath
= vfs_path_build_filename (mc_config_get_home_dir (), "test", "dir", NULL
);
184 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
);
185 fail_unless (strcmp ("~/test/dir", str_path
) == 0, "\nstr=%s\n", str_path
);
187 vfs_path_free (vpath
);
189 vpath
= vfs_path_build_filename (mc_config_get_home_dir (), "test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь", NULL
);
190 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
191 fail_unless (strcmp ("~/test1://user@host.name/#enc:KOI8-R/тестовый/путь", str_path
) == 0, "\nstr=%s\n", str_path
);
193 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
| VPF_HIDE_CHARSET
);
194 fail_unless (strcmp ("~/test1://user@host.name/тестовый/путь", str_path
) == 0, "\nstr=%s\n", str_path
);
196 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_RECODE
);
197 fail_unless (strcmp ("~/test1://user:passwd@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path
) == 0, "\nstr=%s\n", str_path
);
199 str_path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_RECODE
| VPF_STRIP_PASSWORD
);
200 fail_unless (strcmp ("~/test1://user@host.name/ÔÅÓÔÏ×ÙÊ/ÐÕÔØ", str_path
) == 0, "\nstr=%s\n", str_path
);
202 vfs_path_free (vpath
);
204 free_codepages_list ();
205 str_uninit_strings ();
211 /* --------------------------------------------------------------------------------------------- */
219 Suite
*s
= suite_create (TEST_SUITE_NAME
);
220 TCase
*tc_core
= tcase_create ("Core");
223 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
225 /* Add new tests here: *************** */
226 tcase_add_test (tc_core
, test_path_recode_base_utf8
);
227 tcase_add_test (tc_core
, test_path_recode_base_koi8r
);
228 tcase_add_test (tc_core
, test_path_to_str_flags
);
229 /* *********************************** */
231 suite_add_tcase (s
, tc_core
);
232 sr
= srunner_create (s
);
233 srunner_print (sr
, CK_VERBOSE
);
234 srunner_set_log (sr
, "path_recode.log");
235 srunner_run_all (sr
, CK_NORMAL
);
236 number_failed
= srunner_ntests_failed (sr
);
238 return (number_failed
== 0) ? 0 : 1;
241 /* --------------------------------------------------------------------------------------------- */