Added URL-like path parser
[midnight-commander.git] / lib / tests / canonicalize_pathname.c
blobfd56f8d35d7868b21fb2b0ca0f756de527437e07
1 /* lib - canonicalize path
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"
25 #include <check.h>
28 #include "lib/global.h"
29 #include "lib/util.h"
31 static void
32 setup (void)
36 static void
37 teardown (void)
41 /* --------------------------------------------------------------------------------------------- */
43 #define check_canonicalize( input, etalon ) { \
44 path = g_strdup(input); \
45 canonicalize_pathname (path); \
46 fail_unless ( \
47 strcmp(path, etalon) == 0, \
48 "\nactual value (%s)\nnot equal to etalon (%s)", path, etalon \
49 ); \
50 g_free(path); \
53 START_TEST (test_canonicalize_path)
55 char *path;
57 /* UNC path */
58 check_canonicalize ("//some_server/ww", "//some_server/ww");
60 /* join slashes */
61 check_canonicalize ("///some_server/////////ww", "/some_server/ww");
63 /* Collapse "/./" -> "/" */
64 check_canonicalize ("//some_server//.///////ww/./././.", "//some_server/ww");
66 /* Remove leading "./" */
67 check_canonicalize ("./some_server/ww", "some_server/ww");
69 /* some/.. -> . */
70 check_canonicalize ("some_server/..", ".");
72 /* Collapse "/.." with the previous part of path */
73 check_canonicalize ("/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww", "/some_server/ww/ww/some_server/ww");
75 /* URI style */
76 check_canonicalize ("/some_server/ww/ftp://user:pass@host.net/path/", "/some_server/ww/ftp://user:pass@host.net/path");
78 check_canonicalize ("/some_server/ww/ftp://user:pass@host.net/path/../../", "/some_server/ww");
80 check_canonicalize ("ftp://user:pass@host.net/path/../../", ".");
82 check_canonicalize ("ftp://user/../../", "..");
85 END_TEST
87 /* --------------------------------------------------------------------------------------------- */
89 int
90 main (void)
92 int number_failed;
94 Suite *s = suite_create (TEST_SUITE_NAME);
95 TCase *tc_core = tcase_create ("Core");
96 SRunner *sr;
98 tcase_add_checked_fixture (tc_core, setup, teardown);
100 /* Add new tests here: *************** */
101 tcase_add_test (tc_core, test_canonicalize_path);
102 /* *********************************** */
104 suite_add_tcase (s, tc_core);
105 sr = srunner_create (s);
106 srunner_set_log (sr, "canonicalize_pathname.log");
107 srunner_run_all (sr, CK_NORMAL);
108 number_failed = srunner_ntests_failed (sr);
109 srunner_free (sr);
110 return (number_failed == 0) ? 0 : 1;
113 /* --------------------------------------------------------------------------------------------- */