Merge branch '3723_skin_selector_location'
[midnight-commander.git] / tests / lib / vfs / canonicalize_pathname.c
blob36559b53f6752d3ad63b78e4fad7f213b0491d43
1 /*
2 lib - canonicalize path
4 Copyright (C) 2011-2016
5 Free Software Foundation, Inc.
7 Written by:
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"
30 #ifdef HAVE_CHARSET
31 #include "lib/charsets.h"
32 #endif
34 #include "lib/strutil.h"
35 #include "lib/util.h"
36 #include "lib/vfs/xdirentry.h"
38 #include "src/vfs/local/local.c"
40 static struct vfs_s_subclass test_subclass;
41 static struct vfs_class vfs_test_ops;
43 /* --------------------------------------------------------------------------------------------- */
45 /* @Before */
46 static void
47 setup (void)
49 str_init_strings (NULL);
51 vfs_init ();
52 init_localfs ();
53 vfs_setup_work_dir ();
55 #ifdef HAVE_CHARSET
56 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
57 load_codepages_list ();
58 #endif
60 vfs_s_init_class (&vfs_test_ops, &test_subclass);
62 vfs_test_ops.name = "testfs";
63 vfs_test_ops.flags = VFSF_NOLINKS;
64 vfs_test_ops.prefix = "ftp";
65 test_subclass.flags = VFS_S_REMOTE;
66 vfs_register_class (&vfs_test_ops);
69 /* --------------------------------------------------------------------------------------------- */
71 /* @After */
72 static void
73 teardown (void)
75 #ifdef HAVE_CHARSET
76 free_codepages_list ();
77 #endif
79 vfs_shut ();
80 str_uninit_strings ();
83 /* --------------------------------------------------------------------------------------------- */
85 /* @DataSource("test_canonicalize_path_ds") */
86 /* *INDENT-OFF* */
87 static const struct test_canonicalize_path_ds
89 const char *input_path;
90 const char *expected_path;
91 } test_canonicalize_path_ds[] =
93 { /* 0. UNC path */
94 "//some_server/ww",
95 "//some_server/ww"
97 { /* 1. join slashes */
98 "///some_server/////////ww",
99 "/some_server/ww"
101 { /* 2. Collapse "/./" -> "/" */
102 "//some_server//.///////ww/./././.",
103 "//some_server/ww"
105 {/* 3. Remove leading "./" */
106 "./some_server/ww",
107 "some_server/ww"
109 { /* 4. some/.. -> . */
110 "some_server/..",
113 { /* 5. Collapse "/.." with the previous part of path */
114 "/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
115 "/some_server/ww/ww/some_server/ww"
117 { /* 6. URI style */
118 "/some_server/ww/ftp://user:pass@host.net/path/",
119 "/some_server/ww/ftp://user:pass@host.net/path"
121 { /* 7. */
122 "/some_server/ww/ftp://user:pass@host.net/path/../../",
123 "/some_server/ww"
125 { /* 8. */
126 "ftp://user:pass@host.net/path/../../",
129 { /* 9. */
130 "ftp://user/../../",
131 ".."
133 #ifdef HAVE_CHARSET
134 { /* 10. Supported encoding */
135 "/b/#enc:utf-8/../c",
136 "/c"
138 { /* 11. Unsupported encoding */
139 "/b/#enc:aaaa/../c",
140 "/b/c"
142 { /* 12. Supported encoding */
143 "/b/../#enc:utf-8/c",
144 "/#enc:utf-8/c"
146 { /* 13. Unsupported encoding */
147 "/b/../#enc:aaaa/c",
148 "/#enc:aaaa/c"
150 { /* 14. Supported encoding */
151 "/b/c/#enc:utf-8/..",
152 "/b"
154 { /* 15. Unsupported encoding */
155 "/b/c/#enc:aaaa/..",
156 "/b/c"
158 { /* 16. Supported encoding */
159 "/b/c/../#enc:utf-8",
160 "/b/#enc:utf-8"
162 { /* 17. Unsupported encoding */
163 "/b/c/../#enc:aaaa",
164 "/b/#enc:aaaa"
166 #endif /* HAVE_CHARSET */
168 /* *INDENT-ON* */
170 /* @Test(dataSource = "test_canonicalize_path_ds") */
171 /* *INDENT-OFF* */
172 START_PARAMETRIZED_TEST (test_canonicalize_path, test_canonicalize_path_ds)
173 /* *INDENT-ON* */
175 /* given */
176 char *actual_path;
178 actual_path = g_strdup (data->input_path);
180 /* when */
181 canonicalize_pathname (actual_path);
183 /* then */
184 mctest_assert_str_eq (actual_path, data->expected_path) g_free (actual_path);
186 /* *INDENT-OFF* */
187 END_PARAMETRIZED_TEST
188 /* *INDENT-ON* */
190 /* --------------------------------------------------------------------------------------------- */
193 main (void)
195 int number_failed;
197 Suite *s = suite_create (TEST_SUITE_NAME);
198 TCase *tc_core = tcase_create ("Core");
199 SRunner *sr;
201 tcase_add_checked_fixture (tc_core, setup, teardown);
203 /* Add new tests here: *************** */
204 mctest_add_parameterized_test (tc_core, test_canonicalize_path, test_canonicalize_path_ds);
205 /* *********************************** */
207 suite_add_tcase (s, tc_core);
208 sr = srunner_create (s);
209 srunner_set_log (sr, "canonicalize_pathname.log");
210 srunner_run_all (sr, CK_ENV);
211 number_failed = srunner_ntests_failed (sr);
212 srunner_free (sr);
213 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
216 /* --------------------------------------------------------------------------------------------- */