Added new fnction for manipulate vpath objects:
[midnight-commander.git] / tests / lib / vfs / path_manipulations.c
blobb4823196ac6bfa9281e6036fa96754aa196d612f
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 #ifndef HAVE_CHARSET
30 #define HAVE_CHARSET 1
31 #endif
33 #include "lib/charsets.h"
35 #include "lib/strutil.h"
36 #include "lib/vfs/xdirentry.h"
37 #include "lib/vfs/path.h"
39 #include "src/vfs/local/local.c"
42 struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
43 struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
45 static void
46 setup (void)
49 str_init_strings (NULL);
51 vfs_init ();
52 init_localfs ();
53 vfs_setup_work_dir ();
56 test_subclass1.flags = VFS_S_REMOTE;
57 vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
59 vfs_test_ops1.name = "testfs1";
60 vfs_test_ops1.flags = VFSF_NOLINKS;
61 vfs_test_ops1.prefix = "test1";
62 vfs_register_class (&vfs_test_ops1);
64 vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
65 vfs_test_ops2.name = "testfs2";
66 vfs_test_ops2.prefix = "test2";
67 vfs_register_class (&vfs_test_ops2);
69 vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
70 vfs_test_ops3.name = "testfs3";
71 vfs_test_ops3.prefix = "test3";
72 vfs_test_ops3.flags = VFSF_LOCAL;
73 vfs_register_class (&vfs_test_ops3);
75 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
76 load_codepages_list ();
79 static void
80 teardown (void)
82 free_codepages_list ();
84 vfs_shut ();
85 str_uninit_strings ();
88 /* --------------------------------------------------------------------------------------------- */
90 START_TEST (test_vfs_path_tokens_count)
92 size_t tokens_count;
93 vfs_path_t *vpath;
95 vpath = vfs_path_from_str ("/");
96 tokens_count = vfs_path_tokens_count(vpath);
97 fail_unless (tokens_count == 0, "actual: %zu; expected: 0\n", tokens_count);
98 vfs_path_free (vpath);
100 vpath = vfs_path_from_str ("/path");
101 tokens_count = vfs_path_tokens_count(vpath);
102 fail_unless (tokens_count == 1, "actual: %zu; expected: 1\n", tokens_count);
103 vfs_path_free (vpath);
105 vpath = vfs_path_from_str ("/path1/path2/path3");
106 tokens_count = vfs_path_tokens_count(vpath);
107 fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
108 vfs_path_free (vpath);
110 vpath = vfs_path_from_str_flags ("test3://path1/path2/path3/path4", VPF_NO_CANON);
111 tokens_count = vfs_path_tokens_count(vpath);
112 fail_unless (tokens_count == 4, "actual: %zu; expected: 4\n", tokens_count);
113 vfs_path_free (vpath);
115 vpath = vfs_path_from_str_flags ("path1/path2/path3", VPF_NO_CANON);
116 tokens_count = vfs_path_tokens_count(vpath);
117 fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
118 vfs_path_free (vpath);
120 vpath = vfs_path_from_str ("/path1/path2/path3/");
121 tokens_count = vfs_path_tokens_count(vpath);
122 fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
123 vfs_path_free (vpath);
125 vpath = vfs_path_from_str ("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
126 tokens_count = vfs_path_tokens_count(vpath);
127 fail_unless (tokens_count == 5, "actual: %zu; expected: 5\n", tokens_count);
128 vfs_path_free (vpath);
130 vpath = vfs_path_from_str (
131 "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
133 tokens_count = vfs_path_tokens_count(vpath);
134 fail_unless (tokens_count == 11, "actual: %zu; expected: 11\n", tokens_count);
135 vfs_path_free (vpath);
137 END_TEST
139 /* --------------------------------------------------------------------------------------------- */
141 #define check_invalid_token_str(input, start, length) { \
142 vpath = vfs_path_from_str (input); \
143 path_tokens = vfs_path_tokens_get(vpath, start, length); \
144 fail_unless (path_tokens == NULL, "path_tokens should be NULL!\n"); \
145 g_free (path_tokens); \
146 vfs_path_free (vpath); \
149 #define check_token_str(input, start, length, etalon) { \
150 vpath = vfs_path_from_str_flags (input, VPF_NO_CANON); \
151 path_tokens = vfs_path_tokens_get(vpath, start, length); \
152 fail_unless (path_tokens != NULL, "path_tokens shouldn't equal to NULL!\n"); \
153 if (path_tokens != NULL) \
154 fail_unless (strcmp(path_tokens, etalon) == 0, "\nactual: '%s'\netalon: '%s'", path_tokens, etalon); \
155 g_free (path_tokens); \
156 vfs_path_free (vpath); \
159 START_TEST (test_vfs_path_tokens_get)
161 vfs_path_t *vpath;
162 char *path_tokens;
164 /* Invalid start position */
165 check_invalid_token_str ("/" , 2, 1);
167 /* Invalid negative position */
168 check_invalid_token_str ("/path" , -3, 1);
170 /* Count of tokens is zero. Count should be autocorrected */
171 check_token_str ("/path", 0, 0, "path");
173 /* get 'path2/path3' by 1,2 */
174 check_token_str ("/path1/path2/path3/path4", 1, 2, "path2/path3");
176 /* get 'path2/path3' by 1,2 from LOCAL VFS */
177 check_token_str ("test3://path1/path2/path3/path4", 1, 2, "path2/path3");
179 /* get 'path2/path3' by 1,2 from LOCAL VFS with encoding */
180 check_token_str ("test3://path1/path2/test3://#enc:KOI8-R/path3/path4", 1, 2, "path2/test3://#enc:KOI8-R/path3");
182 /* get 'path2/path3' by 1,2 with encoding */
183 check_token_str ("#enc:KOI8-R/path1/path2/path3/path4", 1, 2, "#enc:KOI8-R/path2/path3");
185 /* get 'path2/path3' by 1,2 from non-LOCAL VFS */
186 check_token_str ("test2://path1/path2/path3/path4", 1, 2, "test2://path2/path3");
188 /* get 'path2/path3' by 1,2 throught non-LOCAL VFS */
189 check_token_str ("/path1/path2/test1://user:pass@some.host:12345/path3/path4", 1, 2, "path2/test1://user:pass@some.host:12345/path3");
191 /* get 'path2/path3' by 1,2 from LOCAL VFS */
192 /* TODO: currently this test don't passed. Probably broken string URI parser */
193 /* check_token_str ("test3://path1/path2/test2://test3://path3/path4", 1, 2, "path2/path3"); */
195 /* get 'path2/path3' by 1,2 where path2 it's LOCAL VFS */
196 check_token_str ("test3://path1/path2/test2://path3/path4", 1, 2, "path2/test2://path3");
198 /* get 'path2/path3' by 1,2 where path3 it's LOCAL VFS */
199 check_token_str ("test2://path1/path2/test3://path3/path4", 1, 2, "test2://path2/test3://path3");
201 /* get 'path4' by -1,1 */
202 check_token_str ("/path1/path2/path3/path4", -1, 1, "path4");
204 /* get 'path2/path3/path4' by -3,0 */
205 check_token_str ("/path1/path2/path3/path4", -3, 0, "path2/path3/path4");
208 END_TEST
210 /* --------------------------------------------------------------------------------------------- */
212 START_TEST (test_vfs_path_append_vpath)
214 vfs_path_t *vpath1, *vpath2, *vpath3;
216 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");
217 vpath2 = vfs_path_from_str("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
219 vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
221 fail_unless (vfs_path_elements_count(vpath3) == 6,
222 "\nvpath elements count should be %d, actial is %d\n",
224 vfs_path_elements_count(vpath3)
226 vfs_path_free (vpath1);
227 vfs_path_free (vpath2);
228 vfs_path_free (vpath3);
231 END_TEST
233 /* --------------------------------------------------------------------------------------------- */
236 main (void)
238 int number_failed;
240 Suite *s = suite_create (TEST_SUITE_NAME);
241 TCase *tc_core = tcase_create ("Core");
242 SRunner *sr;
244 tcase_add_checked_fixture (tc_core, setup, teardown);
246 /* Add new tests here: *************** */
247 tcase_add_test (tc_core, test_vfs_path_tokens_count);
248 tcase_add_test (tc_core, test_vfs_path_tokens_get);
249 tcase_add_test (tc_core, test_vfs_path_append_vpath);
250 /* *********************************** */
252 suite_add_tcase (s, tc_core);
253 sr = srunner_create (s);
254 srunner_set_log (sr, "path_manipulations.log");
255 srunner_run_all (sr, CK_NORMAL);
256 number_failed = srunner_ntests_failed (sr);
257 srunner_free (sr);
258 return (number_failed == 0) ? 0 : 1;
261 /* --------------------------------------------------------------------------------------------- */