vfs_path_to_str() now return URL string instead of old representation
[midnight-commander.git] / lib / tests / vfs / vfs_path_string_convert.c
blobfd96c9390d388031267c571888056879e0d45240
1 /* lib/vfs - get vfs_path_t from string
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.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;
44 static void
45 setup (void)
48 str_init_strings (NULL);
50 vfs_init ();
51 init_localfs ();
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);
75 static void
76 teardown (void)
78 vfs_shut ();
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)
87 vfs_path_t *vpath;
88 size_t vpath_len;
89 char *result;
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);
98 g_free(result);
100 vfs_path_free(vpath);
102 END_TEST
104 /* --------------------------------------------------------------------------------------------- */
106 START_TEST (test_vfs_path_from_to_string2)
108 vfs_path_t *vpath;
109 size_t vpath_len;
110 char *result;
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);
121 g_free(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);
129 END_TEST
131 /* --------------------------------------------------------------------------------------------- */
132 START_TEST (test_vfs_path_from_to_partial_string_by_class)
134 vfs_path_t *vpath;
135 char *result;
136 vpath = vfs_path_from_str (ETALON_PATH_STR);
139 result = vfs_path_to_str_elements_count(vpath, -1);
140 fail_unless(
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);
143 g_free(result);
145 result = vfs_path_to_str_elements_count(vpath, -2);
146 fail_unless(
147 strcmp("/test1://bla-bla/some/path/", result) == 0,
148 "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result);
149 g_free(result);
151 result = vfs_path_to_str_elements_count(vpath, -3);
152 fail_unless(
153 strcmp("/", result) == 0,
154 "expected(%s) doesn't equal to actual(%s)", "/", result);
155 g_free(result);
157 /* index out of bound*/
158 result = vfs_path_to_str_elements_count(vpath, -4);
159 fail_unless(
160 strcmp("", result) == 0,
161 "expected(%s) doesn't equal to actual(%s)", "", result);
162 g_free(result);
165 result = vfs_path_to_str_elements_count(vpath, 1);
166 fail_unless(
167 strcmp("/", result) == 0,
168 "expected(%s) doesn't equal to actual(%s)", "/", result);
169 g_free(result);
171 result = vfs_path_to_str_elements_count(vpath, 2);
172 fail_unless(
173 strcmp("/test1://bla-bla/some/path/", result) == 0,
174 "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result);
175 g_free(result);
177 result = vfs_path_to_str_elements_count(vpath, 3);
178 fail_unless(
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);
181 g_free(result);
183 result = vfs_path_to_str_elements_count(vpath, 4);
184 fail_unless(
185 strcmp(ETALON_PATH_URL_STR, result) == 0,
186 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
187 g_free(result);
189 /* index out of bound*/
190 result = vfs_path_to_str_elements_count(vpath, 5);
191 fail_unless(
192 strcmp(ETALON_PATH_URL_STR, result) == 0,
193 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
194 g_free(result);
196 vfs_path_free(vpath);
198 END_TEST
199 /* --------------------------------------------------------------------------------------------- */
201 #define encoding_check( input , etalon ) \
203 vfs_path_t *vpath; \
204 char *result; \
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 ); \
211 g_free(result); \
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 ();
220 encoding_check (
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"
225 encoding_check (
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"
230 encoding_check (
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"
235 encoding_check (
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"
240 encoding_check (
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"
245 encoding_check (
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 ();
253 END_TEST
254 /* --------------------------------------------------------------------------------------------- */
255 #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
256 START_TEST (test_vfs_path_encoding_at_end)
258 vfs_path_t *vpath;
259 char *result;
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 );
276 g_free(result);
277 vfs_path_free(vpath);
279 free_codepages_list ();
282 END_TEST
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)
289 vfs_path_t *vpath;
290 size_t vpath_len;
291 char *result;
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);
299 g_free(result);
301 vfs_path_free(vpath);
303 END_TEST
305 /* --------------------------------------------------------------------------------------------- */
308 main (void)
310 int number_failed;
312 Suite *s = suite_create (TEST_SUITE_NAME);
313 TCase *tc_core = tcase_create ("Core");
314 SRunner *sr;
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);
332 srunner_free (sr);
333 return (number_failed == 0) ? 0 : 1;
336 /* --------------------------------------------------------------------------------------------- */