2 lib/vfs - get vfs_path_t from string
4 Copyright (C) 2011-2024
5 Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "/lib/vfs"
28 #include "tests/mctest.h"
31 #include "lib/charsets.h"
34 #include "lib/strutil.h"
35 #include "lib/vfs/xdirentry.h"
36 #include "lib/vfs/path.c" /* for testing static methods */
38 #include "src/vfs/local/local.c"
40 static struct vfs_class vfs_test_ops1
, vfs_test_ops2
, vfs_test_ops3
;
42 #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
43 #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
45 /* --------------------------------------------------------------------------------------------- */
51 str_init_strings ("UTF-8");
55 vfs_setup_work_dir ();
57 vfs_init_class (&vfs_test_ops1
, "testfs1", VFSF_NOLINKS
, "test1");
58 vfs_register_class (&vfs_test_ops1
);
60 vfs_init_class (&vfs_test_ops2
, "testfs2", VFSF_REMOTE
, "test2");
61 vfs_register_class (&vfs_test_ops2
);
63 vfs_init_class (&vfs_test_ops3
, "testfs3", VFSF_UNKNOWN
, "test3");
64 vfs_register_class (&vfs_test_ops3
);
67 mc_global
.sysconfig_dir
= (char *) TEST_SHARE_DIR
;
68 load_codepages_list ();
69 #endif /* HAVE_CHARSET */
72 /* --------------------------------------------------------------------------------------------- */
79 free_codepages_list ();
80 #endif /* HAVE_CHARSET */
83 str_uninit_strings ();
86 /* --------------------------------------------------------------------------------------------- */
87 /* @DataSource("test_from_to_string_ds") */
89 static const struct test_from_to_string_ds
91 const char *input_string
;
92 const char *expected_result
;
93 const char *expected_element_path
;
94 const size_t expected_elements_count
;
95 struct vfs_class
*expected_vfs_class
;
96 } test_from_to_string_ds
[] =
110 VFS_CLASS (&local_subclass
)
113 "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
114 "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
121 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
122 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
128 "/#test1/bla-bla1/#enc:CP866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
129 "/test1://#enc:CP866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
135 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:CP866/#enc:KOI8-R/some/path#test3/111/22/33",
136 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
142 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:CP866/some/#enc:KOI8-R/path#test3/111/22/33",
143 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
149 "/#test1/bla-bla1/some/path/#test2/#enc:CP866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
150 "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
156 "/#test1/bla-bla1/some/path/#enc:CP866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
157 "/test1://#enc:CP866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
162 #endif /* HAVE_CHARSET */
168 START_PARAMETRIZED_TEST (test_from_to_string
, test_from_to_string_ds
)
174 const vfs_path_element_t
*path_element
;
175 const char *actual_result
;
177 vpath
= vfs_path_from_str_flags (data
->input_string
, VPF_USE_DEPRECATED_PARSER
);
180 vpath_len
= vfs_path_elements_count (vpath
);
181 actual_result
= vfs_path_as_str (vpath
);
182 path_element
= vfs_path_get_by_index (vpath
, -1);
185 ck_assert_int_eq (vpath_len
, data
->expected_elements_count
);
186 mctest_assert_str_eq (actual_result
, data
->expected_result
);
187 mctest_assert_ptr_eq (path_element
->class, data
->expected_vfs_class
);
188 mctest_assert_str_eq (path_element
->path
, data
->expected_element_path
);
190 vfs_path_free (vpath
, TRUE
);
193 END_PARAMETRIZED_TEST
196 /* --------------------------------------------------------------------------------------------- */
198 /* @DataSource("test_partial_string_by_index_ds") */
200 static const struct test_partial_string_by_index_ds
202 const char *input_string
;
203 const off_t element_index
;
204 const char *expected_result
;
205 } test_partial_string_by_index_ds
[] =
210 "/test1://bla-bla/some/path/test2://bla-bla/some/path"
215 "/test1://bla-bla/some/path/"
222 { /* 3. Index out of bound */
235 "/test1://bla-bla/some/path/"
240 "/test1://bla-bla/some/path/test2://bla-bla/some/path"
247 { /* 7. Index out of bound */
255 /* @Test(dataSource = "test_partial_string_by_index_ds") */
257 START_PARAMETRIZED_TEST (test_partial_string_by_index
, test_partial_string_by_index_ds
)
263 vpath
= vfs_path_from_str_flags (data
->input_string
, VPF_USE_DEPRECATED_PARSER
);
266 actual_result
= vfs_path_to_str_elements_count (vpath
, data
->element_index
);
269 mctest_assert_str_eq (actual_result
, data
->expected_result
);
270 g_free (actual_result
);
272 vfs_path_free (vpath
, TRUE
);
275 END_PARAMETRIZED_TEST
278 /* --------------------------------------------------------------------------------------------- */
280 /* --------------------------------------------------------------------------------------------- */
282 #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
285 START_TEST (test_vfs_path_encoding_at_end
)
291 const vfs_path_element_t
*element
;
294 vfs_path_from_str_flags ("/path/to/file.ext#test1:/#enc:KOI8-R", VPF_USE_DEPRECATED_PARSER
);
297 result
= vfs_path_as_str (vpath
);
298 element
= vfs_path_get_by_index (vpath
, -1);
301 mctest_assert_str_eq (element
->path
, "");
302 mctest_assert_not_null (element
->encoding
);
303 mctest_assert_str_eq (result
, ETALON_STR
);
305 vfs_path_free (vpath
, TRUE
);
311 #endif /* HAVE_CHARSET */
312 /* --------------------------------------------------------------------------------------------- */
319 tc_core
= tcase_create ("Core");
321 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
323 /* Add new tests here: *************** */
324 mctest_add_parameterized_test (tc_core
, test_from_to_string
, test_from_to_string_ds
);
325 mctest_add_parameterized_test (tc_core
, test_partial_string_by_index
,
326 test_partial_string_by_index_ds
);
328 tcase_add_test (tc_core
, test_vfs_path_encoding_at_end
);
330 /* *********************************** */
332 return mctest_run_all (tc_core
);
335 /* --------------------------------------------------------------------------------------------- */