Code refactoring in tests.
[midnight-commander.git] / tests / lib / vfs / canonicalize_pathname.c
blob434b491a95f8f026cd8aa24a35bb53aba355a7d2
1 /*
2 lib - canonicalize path
4 Copyright (C) 2011, 2013
5 The 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 #include "lib/strutil.h"
31 #include "lib/util.h"
32 #include "lib/vfs/xdirentry.h"
34 #include "src/vfs/local/local.c"
36 static void
37 setup (void)
39 str_init_strings (NULL);
41 vfs_init ();
42 init_localfs ();
43 vfs_setup_work_dir ();
46 static void
47 teardown (void)
49 vfs_shut ();
50 str_uninit_strings ();
53 /* --------------------------------------------------------------------------------------------- */
55 #define check_canonicalize( input, etalon ) { \
56 path = g_strdup(input); \
57 canonicalize_pathname (path); \
58 fail_unless ( \
59 strcmp(path, etalon) == 0, \
60 "\nactual value (%s)\nnot equal to etalon (%s)", path, etalon \
61 ); \
62 g_free(path); \
65 /* *INDENT-OFF* */
66 START_TEST (test_canonicalize_path)
67 /* *INDENT-ON* */
69 char *path;
70 static struct vfs_s_subclass test_subclass;
71 static struct vfs_class vfs_test_ops;
73 vfs_s_init_class (&vfs_test_ops, &test_subclass);
75 vfs_test_ops.name = "testfs";
76 vfs_test_ops.flags = VFSF_NOLINKS;
77 vfs_test_ops.prefix = "ftp";
78 test_subclass.flags = VFS_S_REMOTE;
79 vfs_register_class (&vfs_test_ops);
81 /* UNC path */
82 check_canonicalize ("//some_server/ww", "//some_server/ww");
84 /* join slashes */
85 check_canonicalize ("///some_server/////////ww", "/some_server/ww");
87 /* Collapse "/./" -> "/" */
88 check_canonicalize ("//some_server//.///////ww/./././.", "//some_server/ww");
90 /* Remove leading "./" */
91 check_canonicalize ("./some_server/ww", "some_server/ww");
93 /* some/.. -> . */
94 check_canonicalize ("some_server/..", ".");
96 /* Collapse "/.." with the previous part of path */
97 check_canonicalize ("/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
98 "/some_server/ww/ww/some_server/ww");
100 /* URI style */
101 check_canonicalize ("/some_server/ww/ftp://user:pass@host.net/path/",
102 "/some_server/ww/ftp://user:pass@host.net/path");
104 check_canonicalize ("/some_server/ww/ftp://user:pass@host.net/path/../../", "/some_server/ww");
106 check_canonicalize ("ftp://user:pass@host.net/path/../../", ".");
108 check_canonicalize ("ftp://user/../../", "..");
110 /* *INDENT-OFF* */
111 END_TEST
112 /* *INDENT-ON* */
114 /* --------------------------------------------------------------------------------------------- */
117 main (void)
119 int number_failed;
121 Suite *s = suite_create (TEST_SUITE_NAME);
122 TCase *tc_core = tcase_create ("Core");
123 SRunner *sr;
125 tcase_add_checked_fixture (tc_core, setup, teardown);
127 /* Add new tests here: *************** */
128 tcase_add_test (tc_core, test_canonicalize_path);
129 /* *********************************** */
131 suite_add_tcase (s, tc_core);
132 sr = srunner_create (s);
133 srunner_set_log (sr, "canonicalize_pathname.log");
134 srunner_run_all (sr, CK_NORMAL);
135 number_failed = srunner_ntests_failed (sr);
136 srunner_free (sr);
137 return (number_failed == 0) ? 0 : 1;
140 /* --------------------------------------------------------------------------------------------- */