1 /* lib/vfs - get vfs_path_t from string
3 Copyright (C) 2011 Free Software Foundation, Inc.
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"
27 #include "lib/global.c"
30 #define HAVE_CHARSET 1
33 #include "lib/charsets.h"
35 #include "lib/strutil.h"
36 #include "lib/vfs/xdirentry.h"
37 #include "lib/vfs/path.c" /* for testing static methods */
39 #include "src/vfs/local/local.c"
41 struct vfs_s_subclass test_subclass1
, test_subclass2
, test_subclass3
;
42 struct vfs_class vfs_test_ops1
, vfs_test_ops2
, vfs_test_ops3
;
48 str_init_strings (NULL
);
52 vfs_setup_work_dir ();
55 test_subclass1
.flags
= VFS_S_REMOTE
;
56 vfs_s_init_class (&vfs_test_ops1
, &test_subclass1
);
58 vfs_test_ops1
.name
= "testfs1";
59 vfs_test_ops1
.flags
= VFSF_NOLINKS
;
60 vfs_test_ops1
.prefix
= "test1";
61 vfs_register_class (&vfs_test_ops1
);
63 vfs_s_init_class (&vfs_test_ops2
, &test_subclass2
);
64 vfs_test_ops2
.name
= "testfs2";
65 vfs_test_ops2
.prefix
= "test2";
66 vfs_register_class (&vfs_test_ops2
);
68 vfs_s_init_class (&vfs_test_ops3
, &test_subclass3
);
69 vfs_test_ops3
.name
= "testfs3";
70 vfs_test_ops3
.prefix
= "test3";
71 vfs_register_class (&vfs_test_ops3
);
79 str_uninit_strings ();
82 /* --------------------------------------------------------------------------------------------- */
83 #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
84 START_TEST (test_vfs_path_from_to_string
)
89 vpath
= vfs_path_from_str (ETALON_PATH_STR
);
92 vpath_len
= vfs_path_elements_count(vpath
);
93 fail_unless(vpath_len
== 4, "vpath length should be 4 (actial: %d)",vpath_len
);
95 result
= vfs_path_to_str(vpath
);
96 fail_unless(strcmp(ETALON_PATH_STR
, result
) == 0, "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_STR
, result
);
103 /* --------------------------------------------------------------------------------------------- */
105 START_TEST (test_vfs_path_from_to_string2
)
110 vfs_path_element_t
*path_element
;
112 vpath
= vfs_path_from_str ("/");
115 vpath_len
= vfs_path_elements_count(vpath
);
116 fail_unless(vpath_len
== 1, "vpath length should be 1 (actial: %d)",vpath_len
);
118 result
= vfs_path_to_str(vpath
);
119 fail_unless(strcmp("/", result
) == 0, "expected(%s) doesn't equal to actual(%s)", "/", result
);
121 path_element
= vfs_path_get_by_index (vpath
, -1);
122 fail_unless(strcmp("/", path_element
->path
) == 0, "expected(%s) doesn't equal to actual(%s)", "/", path_element
->path
);
124 fail_unless(path_element
->class == &vfs_local_ops
, "actual vfs-class doesn't equal to localfs");
126 vfs_path_free(vpath
);
130 /* --------------------------------------------------------------------------------------------- */
131 START_TEST (test_vfs_path_from_to_partial_string_by_class
)
135 vpath
= vfs_path_from_str (ETALON_PATH_STR
);
138 result
= vfs_path_to_str_elements_count(vpath
, -1);
140 strcmp("/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result
) == 0,
141 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result
);
144 result
= vfs_path_to_str_elements_count(vpath
, -2);
146 strcmp("/#test1/bla-bla/some/path/", result
) == 0,
147 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/", result
);
150 result
= vfs_path_to_str_elements_count(vpath
, -3);
152 strcmp("/", result
) == 0,
153 "expected(%s) doesn't equal to actual(%s)", "/", result
);
156 /* index out of bound*/
157 result
= vfs_path_to_str_elements_count(vpath
, -4);
159 strcmp("", result
) == 0,
160 "expected(%s) doesn't equal to actual(%s)", "", result
);
164 result
= vfs_path_to_str_elements_count(vpath
, 1);
166 strcmp("/", result
) == 0,
167 "expected(%s) doesn't equal to actual(%s)", "/", result
);
170 result
= vfs_path_to_str_elements_count(vpath
, 2);
172 strcmp("/#test1/bla-bla/some/path/", result
) == 0,
173 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/", result
);
176 result
= vfs_path_to_str_elements_count(vpath
, 3);
178 strcmp("/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result
) == 0,
179 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result
);
182 result
= vfs_path_to_str_elements_count(vpath
, 4);
184 strcmp(ETALON_PATH_STR
, result
) == 0,
185 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_STR
, result
);
188 /* index out of bound*/
189 result
= vfs_path_to_str_elements_count(vpath
, 5);
191 strcmp(ETALON_PATH_STR
, result
) == 0,
192 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_STR
, result
);
195 vfs_path_free(vpath
);
198 /* --------------------------------------------------------------------------------------------- */
200 #define encoding_check( input , etalon ) \
205 vpath = vfs_path_from_str (input); \
206 result = vfs_path_to_str(vpath); \
207 fail_unless( result != NULL && strcmp(result, etalon) ==0, \
208 "\ninput : %s\nactual: %s\netalon: %s", input, result , etalon ); \
211 vfs_path_free(vpath); \
214 START_TEST (test_vfs_path_from_to_string_encoding
)
216 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
217 load_codepages_list ();
220 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
221 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
225 "/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
226 "/#test1/#enc:IBM866/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
230 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
231 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
235 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
236 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
240 "/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
241 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
245 "/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
246 "/#test1/#enc:IBM866/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
249 free_codepages_list ();
253 /* --------------------------------------------------------------------------------------------- */
254 #define ETALON_STR "/path/to/file.ext#test1/#enc:KOI8-R"
255 START_TEST (test_vfs_path_encoding_at_end
)
259 vfs_path_element_t
*element
;
261 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
262 load_codepages_list ();
264 vpath
= vfs_path_from_str ("/path/to/file.ext#test1:/#enc:KOI8-R");
265 result
= vfs_path_to_str(vpath
);
267 element
= vfs_path_get_by_index(vpath
, -1);
268 fail_unless(*element
->path
== '\0', "element->path should be empty, but actual value is '%s'",element
->path
);
269 fail_unless(element
->encoding
!= NULL
&& strcmp(element
->encoding
, "KOI8-R") == 0,
270 "element->encoding should be 'KOI8-R', but actual value is '%s'",element
->encoding
);
272 fail_unless( result
!= NULL
&& strcmp(result
, ETALON_STR
) ==0,
273 "\nactual: %s\netalon: %s", result
, ETALON_STR
);
276 vfs_path_free(vpath
);
278 free_codepages_list ();
282 /* --------------------------------------------------------------------------------------------- */
289 Suite
*s
= suite_create (TEST_SUITE_NAME
);
290 TCase
*tc_core
= tcase_create ("Core");
293 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
295 /* Add new tests here: *************** */
296 tcase_add_test (tc_core
, test_vfs_path_from_to_string
);
297 tcase_add_test (tc_core
, test_vfs_path_from_to_string2
);
298 tcase_add_test (tc_core
, test_vfs_path_from_to_partial_string_by_class
);
299 tcase_add_test (tc_core
, test_vfs_path_from_to_string_encoding
);
300 tcase_add_test (tc_core
, test_vfs_path_encoding_at_end
);
301 /* *********************************** */
303 suite_add_tcase (s
, tc_core
);
304 sr
= srunner_create (s
);
305 srunner_set_log (sr
, "vfs_path_string_convert.log");
306 srunner_run_all (sr
, CK_NORMAL
);
307 number_failed
= srunner_ntests_failed (sr
);
309 return (number_failed
== 0) ? 0 : 1;
312 /* --------------------------------------------------------------------------------------------- */