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 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 test_subclass2
.flags
= VFS_S_REMOTE
;
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 #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
85 START_TEST (test_vfs_path_from_to_string
)
90 vpath
= vfs_path_from_str (ETALON_PATH_STR
);
93 vpath_len
= vfs_path_elements_count(vpath
);
94 fail_unless(vpath_len
== 4, "vpath length should be 4 (actial: %d)",vpath_len
);
96 result
= vfs_path_to_str(vpath
);
97 fail_unless(strcmp(ETALON_PATH_URL_STR
, result
) == 0, "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR
, result
);
100 vfs_path_free(vpath
);
104 /* --------------------------------------------------------------------------------------------- */
106 START_TEST (test_vfs_path_from_to_string2
)
111 vfs_path_element_t
*path_element
;
113 vpath
= vfs_path_from_str ("/");
116 vpath_len
= vfs_path_elements_count(vpath
);
117 fail_unless(vpath_len
== 1, "vpath length should be 1 (actial: %d)",vpath_len
);
119 result
= vfs_path_to_str(vpath
);
120 fail_unless(strcmp("/", result
) == 0, "expected(%s) doesn't equal to actual(%s)", "/", result
);
122 path_element
= vfs_path_get_by_index (vpath
, -1);
123 fail_unless(strcmp("/", path_element
->path
) == 0, "expected(%s) doesn't equal to actual(%s)", "/", path_element
->path
);
125 fail_unless(path_element
->class == &vfs_local_ops
, "actual vfs-class doesn't equal to localfs");
127 vfs_path_free(vpath
);
131 /* --------------------------------------------------------------------------------------------- */
132 START_TEST (test_vfs_path_from_to_partial_string_by_class
)
136 vpath
= vfs_path_from_str (ETALON_PATH_STR
);
139 result
= vfs_path_to_str_elements_count(vpath
, -1);
141 strcmp("/test1://bla-bla/some/path/test2://bla-bla/some/path", result
) == 0,
142 "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/test2://bla-bla/some/path", result
);
145 result
= vfs_path_to_str_elements_count(vpath
, -2);
147 strcmp("/test1://bla-bla/some/path/", result
) == 0,
148 "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result
);
151 result
= vfs_path_to_str_elements_count(vpath
, -3);
153 strcmp("/", result
) == 0,
154 "expected(%s) doesn't equal to actual(%s)", "/", result
);
157 /* index out of bound*/
158 result
= vfs_path_to_str_elements_count(vpath
, -4);
160 strcmp("", result
) == 0,
161 "expected(%s) doesn't equal to actual(%s)", "", result
);
165 result
= vfs_path_to_str_elements_count(vpath
, 1);
167 strcmp("/", result
) == 0,
168 "expected(%s) doesn't equal to actual(%s)", "/", result
);
171 result
= vfs_path_to_str_elements_count(vpath
, 2);
173 strcmp("/test1://bla-bla/some/path/", result
) == 0,
174 "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result
);
177 result
= vfs_path_to_str_elements_count(vpath
, 3);
179 strcmp("/test1://bla-bla/some/path/test2://bla-bla/some/path", result
) == 0,
180 "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/test2://bla-bla/some/path", result
);
183 result
= vfs_path_to_str_elements_count(vpath
, 4);
185 strcmp(ETALON_PATH_URL_STR
, result
) == 0,
186 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR
, result
);
189 /* index out of bound*/
190 result
= vfs_path_to_str_elements_count(vpath
, 5);
192 strcmp(ETALON_PATH_URL_STR
, result
) == 0,
193 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR
, result
);
196 vfs_path_free(vpath
);
199 /* --------------------------------------------------------------------------------------------- */
201 #define encoding_check( input , etalon ) \
206 vpath = vfs_path_from_str (input); \
207 result = vfs_path_to_str(vpath); \
208 fail_unless( result != NULL && strcmp(result, etalon) ==0, \
209 "\ninput : %s\nactual: %s\netalon: %s", input, result , etalon ); \
212 vfs_path_free(vpath); \
215 START_TEST (test_vfs_path_from_to_string_encoding
)
217 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
218 load_codepages_list ();
221 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
222 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
226 "/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
227 "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
231 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
232 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
236 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
237 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
241 "/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
242 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
246 "/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
247 "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
250 free_codepages_list ();
254 /* --------------------------------------------------------------------------------------------- */
255 #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
256 START_TEST (test_vfs_path_encoding_at_end
)
260 vfs_path_element_t
*element
;
262 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
263 load_codepages_list ();
265 vpath
= vfs_path_from_str ("/path/to/file.ext#test1:/#enc:KOI8-R");
266 result
= vfs_path_to_str(vpath
);
268 element
= vfs_path_get_by_index(vpath
, -1);
269 fail_unless(*element
->path
== '\0', "element->path should be empty, but actual value is '%s'",element
->path
);
270 fail_unless(element
->encoding
!= NULL
&& strcmp(element
->encoding
, "KOI8-R") == 0,
271 "element->encoding should be 'KOI8-R', but actual value is '%s'",element
->encoding
);
273 fail_unless( result
!= NULL
&& strcmp(result
, ETALON_STR
) ==0,
274 "\nactual: %s\netalon: %s", result
, ETALON_STR
);
277 vfs_path_free(vpath
);
279 free_codepages_list ();
283 /* --------------------------------------------------------------------------------------------- */
285 #undef ETALON_PATH_STR
286 #define ETALON_PATH_STR "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33"
287 START_TEST (test_vfs_path_from_to_string_uri
)
292 vpath
= vfs_path_from_str (ETALON_PATH_STR
);
294 vpath_len
= vfs_path_elements_count(vpath
);
295 fail_unless(vpath_len
== 4, "vpath length should be 4 (actial: %d)",vpath_len
);
297 result
= vfs_path_to_str(vpath
);
298 fail_unless(strcmp(ETALON_PATH_STR
, result
) == 0, "\nexpected(%s)\ndoesn't equal to actual(%s)", ETALON_PATH_STR
, result
);
301 vfs_path_free(vpath
);
305 /* --------------------------------------------------------------------------------------------- */
312 Suite
*s
= suite_create (TEST_SUITE_NAME
);
313 TCase
*tc_core
= tcase_create ("Core");
316 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
318 /* Add new tests here: *************** */
319 tcase_add_test (tc_core
, test_vfs_path_from_to_string
);
320 tcase_add_test (tc_core
, test_vfs_path_from_to_string2
);
321 tcase_add_test (tc_core
, test_vfs_path_from_to_partial_string_by_class
);
322 tcase_add_test (tc_core
, test_vfs_path_from_to_string_encoding
);
323 tcase_add_test (tc_core
, test_vfs_path_encoding_at_end
);
324 tcase_add_test (tc_core
, test_vfs_path_from_to_string_uri
);
325 /* *********************************** */
327 suite_add_tcase (s
, tc_core
);
328 sr
= srunner_create (s
);
329 srunner_set_log (sr
, "vfs_path_string_convert.log");
330 srunner_run_all (sr
, CK_NORMAL
);
331 number_failed
= srunner_ntests_failed (sr
);
333 return (number_failed
== 0) ? 0 : 1;
336 /* --------------------------------------------------------------------------------------------- */