Tests tweak and cleanup in case of --disable-charset option usage.
[midnight-commander.git] / tests / lib / vfs / path_manipulations.c
blob1dce032d9d0b8d1e241abeb1d535dc050aad5ab0
1 /* lib/vfs - test vfs_path_t manipulation functions
3 Copyright (C) 2011 Free Software Foundation, Inc.
5 Written by:
6 Slava Zanko <slavazanko@gmail.com>, 2011
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define TEST_SUITE_NAME "/lib/vfs"
25 #include <check.h>
27 #include "lib/global.c"
29 #ifdef HAVE_CHARSET
30 #include "lib/charsets.h"
31 #endif
33 #include "lib/strutil.h"
34 #include "lib/vfs/xdirentry.h"
35 #include "lib/vfs/path.h"
37 #include "src/vfs/local/local.c"
40 struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
41 struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
43 static void
44 setup (void)
47 str_init_strings (NULL);
49 vfs_init ();
50 init_localfs ();
51 vfs_setup_work_dir ();
54 test_subclass1.flags = VFS_S_REMOTE;
55 vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
57 vfs_test_ops1.name = "testfs1";
58 vfs_test_ops1.flags = VFSF_NOLINKS;
59 vfs_test_ops1.prefix = "test1";
60 vfs_register_class (&vfs_test_ops1);
62 vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
63 vfs_test_ops2.name = "testfs2";
64 vfs_test_ops2.prefix = "test2";
65 vfs_register_class (&vfs_test_ops2);
67 vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
68 vfs_test_ops3.name = "testfs3";
69 vfs_test_ops3.prefix = "test3";
70 vfs_test_ops3.flags = VFSF_LOCAL;
71 vfs_register_class (&vfs_test_ops3);
73 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
74 #ifdef HAVE_CHARSET
75 load_codepages_list ();
76 #endif
79 static void
80 teardown (void)
82 #ifdef HAVE_CHARSET
83 free_codepages_list ();
84 #endif
86 vfs_shut ();
87 str_uninit_strings ();
90 /* --------------------------------------------------------------------------------------------- */
92 START_TEST (test_vfs_path_tokens_count)
94 size_t tokens_count;
95 vfs_path_t *vpath;
97 vpath = vfs_path_from_str ("/");
98 tokens_count = vfs_path_tokens_count(vpath);
99 fail_unless (tokens_count == 0, "actual: %zu; expected: 0\n", tokens_count);
100 vfs_path_free (vpath);
102 vpath = vfs_path_from_str ("/path");
103 tokens_count = vfs_path_tokens_count(vpath);
104 fail_unless (tokens_count == 1, "actual: %zu; expected: 1\n", tokens_count);
105 vfs_path_free (vpath);
107 vpath = vfs_path_from_str ("/path1/path2/path3");
108 tokens_count = vfs_path_tokens_count(vpath);
109 fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
110 vfs_path_free (vpath);
112 vpath = vfs_path_from_str_flags ("test3://path1/path2/path3/path4", VPF_NO_CANON);
113 tokens_count = vfs_path_tokens_count(vpath);
114 fail_unless (tokens_count == 4, "actual: %zu; expected: 4\n", tokens_count);
115 vfs_path_free (vpath);
117 vpath = vfs_path_from_str_flags ("path1/path2/path3", VPF_NO_CANON);
118 tokens_count = vfs_path_tokens_count(vpath);
119 fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
120 vfs_path_free (vpath);
122 vpath = vfs_path_from_str ("/path1/path2/path3/");
123 tokens_count = vfs_path_tokens_count(vpath);
124 fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
125 vfs_path_free (vpath);
127 vpath = vfs_path_from_str ("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
128 tokens_count = vfs_path_tokens_count(vpath);
129 fail_unless (tokens_count == 5, "actual: %zu; expected: 5\n", tokens_count);
130 vfs_path_free (vpath);
132 #ifdef HAVE_CHARSET
133 vpath = vfs_path_from_str (
134 "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
136 tokens_count = vfs_path_tokens_count(vpath);
137 fail_unless (tokens_count == 11, "actual: %zu; expected: 11\n", tokens_count);
138 vfs_path_free (vpath);
139 #endif
141 END_TEST
143 /* --------------------------------------------------------------------------------------------- */
145 #define check_invalid_token_str(input, start, length) { \
146 vpath = vfs_path_from_str (input); \
147 path_tokens = vfs_path_tokens_get(vpath, start, length); \
148 fail_unless (path_tokens == NULL, "path_tokens should be NULL!\n"); \
149 g_free (path_tokens); \
150 vfs_path_free (vpath); \
153 #define check_token_str(input, start, length, etalon) { \
154 vpath = vfs_path_from_str_flags (input, VPF_NO_CANON); \
155 path_tokens = vfs_path_tokens_get(vpath, start, length); \
156 fail_unless (path_tokens != NULL, "path_tokens shouldn't equal to NULL!\n"); \
157 if (path_tokens != NULL) \
158 fail_unless (strcmp(path_tokens, etalon) == 0, "\nactual: '%s'\netalon: '%s'", path_tokens, etalon); \
159 g_free (path_tokens); \
160 vfs_path_free (vpath); \
163 START_TEST (test_vfs_path_tokens_get)
165 vfs_path_t *vpath;
166 char *path_tokens;
168 /* Invalid start position */
169 check_invalid_token_str ("/" , 2, 1);
171 /* Invalid negative position */
172 check_invalid_token_str ("/path" , -3, 1);
174 /* Count of tokens is zero. Count should be autocorrected */
175 check_token_str ("/path", 0, 0, "path");
177 /* get 'path2/path3' by 1,2 */
178 check_token_str ("/path1/path2/path3/path4", 1, 2, "path2/path3");
180 /* get 'path2/path3' by 1,2 from LOCAL VFS */
181 check_token_str ("test3://path1/path2/path3/path4", 1, 2, "path2/path3");
183 #ifdef HAVE_CHARSET
184 /* get 'path2/path3' by 1,2 from LOCAL VFS with encoding */
185 check_token_str ("test3://path1/path2/test3://#enc:KOI8-R/path3/path4", 1, 2, "path2/test3://#enc:KOI8-R/path3");
187 /* get 'path2/path3' by 1,2 with encoding */
188 check_token_str ("#enc:KOI8-R/path1/path2/path3/path4", 1, 2, "#enc:KOI8-R/path2/path3");
189 #endif
191 /* get 'path2/path3' by 1,2 from non-LOCAL VFS */
192 check_token_str ("test2://path1/path2/path3/path4", 1, 2, "test2://path2/path3");
194 /* get 'path2/path3' by 1,2 throught non-LOCAL VFS */
195 check_token_str ("/path1/path2/test1://user:pass@some.host:12345/path3/path4", 1, 2, "path2/test1://user:pass@some.host:12345/path3");
197 /* get 'path2/path3' by 1,2 from LOCAL VFS */
198 /* TODO: currently this test don't passed. Probably broken string URI parser */
199 /* check_token_str ("test3://path1/path2/test2://test3://path3/path4", 1, 2, "path2/path3"); */
201 /* get 'path2/path3' by 1,2 where path2 it's LOCAL VFS */
202 check_token_str ("test3://path1/path2/test2://path3/path4", 1, 2, "path2/test2://path3");
204 /* get 'path2/path3' by 1,2 where path3 it's LOCAL VFS */
205 check_token_str ("test2://path1/path2/test3://path3/path4", 1, 2, "test2://path2/test3://path3");
207 /* get 'path4' by -1,1 */
208 check_token_str ("/path1/path2/path3/path4", -1, 1, "path4");
210 /* get 'path2/path3/path4' by -3,0 */
211 check_token_str ("/path1/path2/path3/path4", -3, 0, "path2/path3/path4");
214 END_TEST
216 /* --------------------------------------------------------------------------------------------- */
218 START_TEST (test_vfs_path_append_vpath)
220 vfs_path_t *vpath1, *vpath2, *vpath3;
222 #ifdef HAVE_CHARSET
223 vpath1 = vfs_path_from_str("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33");
224 #else
225 vpath1 = vfs_path_from_str("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33");
226 #endif
227 vpath2 = vfs_path_from_str("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
229 vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
231 fail_unless (vfs_path_elements_count(vpath3) == 6,
232 "\nvpath elements count should be %d, actial is %d\n",
234 vfs_path_elements_count(vpath3)
236 vfs_path_free (vpath1);
237 vfs_path_free (vpath2);
238 vfs_path_free (vpath3);
241 END_TEST
243 /* --------------------------------------------------------------------------------------------- */
245 START_TEST (test_vfs_path_relative)
247 vfs_path_t *vpath, *copy_vpath;
248 char *path_str;
250 vpath = vfs_path_from_str_flags("../bla-bla", VPF_NO_CANON);
251 fail_unless (vpath->relative, "relative flag fail!\n");
253 path_str = vfs_path_to_str (vpath);
255 fail_unless (strcmp(path_str, "../bla-bla") == 0, "relative fail!\nactual: [%s]\n", path_str);
256 g_free (path_str);
258 path_str = (char *) vfs_path_get_last_path_str (vpath);
259 fail_unless (strcmp(path_str, "../bla-bla") == 0, "relative fail!\nactual: element->path=[%s]\n", path_str);
261 copy_vpath = vfs_path_clone (vpath);
263 path_str = vfs_path_to_str (copy_vpath);
265 fail_unless (strcmp(path_str, "../bla-bla") == 0, "relative fail!\nactual: [%s]\n", path_str);
266 g_free (path_str);
268 vfs_path_free (copy_vpath);
269 vfs_path_free (vpath);
271 vpath = vfs_path_from_str_flags ("../path/test1://user:pass@some.host:12345/bla-bla/some/path/", VPF_NO_CANON);
272 path_str = vfs_path_to_str (vpath);
273 fail_unless (strcmp(path_str, "../path/test1://user:pass@some.host:12345/bla-bla/some/path/") == 0, "relative fail!\nactual: [%s]\n", path_str);
274 g_free (path_str);
275 vfs_path_free (vpath);
278 END_TEST
280 /* --------------------------------------------------------------------------------------------- */
283 main (void)
285 int number_failed;
287 Suite *s = suite_create (TEST_SUITE_NAME);
288 TCase *tc_core = tcase_create ("Core");
289 SRunner *sr;
291 tcase_add_checked_fixture (tc_core, setup, teardown);
293 /* Add new tests here: *************** */
294 tcase_add_test (tc_core, test_vfs_path_tokens_count);
295 tcase_add_test (tc_core, test_vfs_path_tokens_get);
296 tcase_add_test (tc_core, test_vfs_path_append_vpath);
297 tcase_add_test (tc_core, test_vfs_path_relative);
298 /* *********************************** */
300 suite_add_tcase (s, tc_core);
301 sr = srunner_create (s);
302 srunner_set_log (sr, "path_manipulations.log");
303 srunner_run_all (sr, CK_NORMAL);
304 number_failed = srunner_ntests_failed (sr);
305 srunner_free (sr);
306 return (number_failed == 0) ? 0 : 1;
309 /* --------------------------------------------------------------------------------------------- */