Debian Codegraph @

test/unit/common/check_path.c

raw symbols SCIP
1 /*
2 * static char *rcsid_check_path_c =
3 * "$Id$";
4 */
5
6 /*
7 * CrossFire, A Multiplayer game for X-windows
8 *
9 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10 * Copyright (C) 1992 Frank Tore Johansen
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program 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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * The authors can be reached via e-mail at [email protected]
27 */
28
29 /*
30 * This is the unit tests file for common/path.c
31 */
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <check.h>
36 #include <global.h>
37
38 #include "path.h"
39
40 static void setup(void) {
41 /* put any initialisation steps here, they will be run before each testcase */
42 }
43
44 static void teardown(void) {
45 /* put any cleanup steps here, they will be run after each testcase */
46 }
47
48 static void check_combine(const char *src, const char *dst, const char *exp) {
49 char res[HUGE_BUF];
50
51 path_combine(src, dst, res, HUGE_BUF);
52 fail_unless(strcmp(res, exp) == 0, "path_combine(%s, %s) = %s but should be %s", src, dst, res, exp);
53 }
54
55 static void check_normalize(const char *path, const char *exp) {
56 char tmp[HUGE_BUF];
57
58 /* This is needed as path_normalize modifies in place. */
59 strncpy(tmp, path, sizeof(tmp));
60 tmp[HUGE_BUF-1] = '\0';
61 path_normalize(tmp);
62 fail_unless(strcmp(tmp, exp) == 0, "path_normalize(%s) = %s but should be %s", path, tmp, exp);
63 }
64
65 static void check_combine_and_normalize(const char *src, const char *dst, const char *exp) {
66 char res[HUGE_BUF];
67
68 path_combine_and_normalize(src, dst, res, sizeof(res));
69 fail_unless(strcmp(res, exp) == 0, "path_combine_and_normalize(%s, %s) = %s but should be %s", src, dst, res, exp);
70 }
71
72 START_TEST(test_path_combine) {
73 check_combine("/path1/file1", "/path2/file2", "/path2/file2");
74 check_combine("path1/file1", "/path2/file2", "/path2/file2");
75 check_combine("/path1/file1", "path2/file2", "/path1/path2/file2");
76 check_combine("path1/file1", "path2/file2", "path1/path2/file2");
77 check_combine("/path1", "/path2", "/path2");
78 check_combine("path1", "/path2", "/path2");
79 check_combine("/path1", "path2", "/path2");
80 check_combine("path1", "path2", "path2");
81 }
82 END_TEST
83
84 START_TEST(test_path_normalize) {
85 check_normalize("", "");
86 check_normalize("/", "/");
87 check_normalize("path1/file1", "path1/file1");
88 check_normalize("/path1/file1", "/path1/file1");
89 check_normalize("/path1//file1", "/path1/file1");
90 check_normalize("//path1/file1", "/path1/file1");
91 check_normalize("///////x////////y///////z////////", "/x/y/z");
92 check_normalize("/a/b/../c/d/../e/../../f/g/../h", "/a/f/h");
93 check_normalize("//a//b//..//c//d//..//e//..//..//f//g//..//h", "/a/f/h");
94 check_normalize("../a", "../a");
95 check_normalize("a/../../b", "../b");
96 check_normalize("/../a", "/a");
97 check_normalize("/a/../../b", "/b");
98 check_normalize("./b/./c/.d/..e/./f", "b/c/.d/..e/f");
99 check_normalize("/b/././././e", "/b/e");
100 check_normalize(".", ""); /* maybe the result should be "."? */
101 check_normalize("/.", "/");
102 check_normalize("./", ""); /* maybe the result should be "."? */
103 check_normalize("/a/b/..", "/a");
104 check_normalize("/a/b/../..", "/");
105 check_normalize("/a/b/../../..", "/");
106 check_normalize("a/b/..", "a");
107 check_normalize("a/b/../..", "");
108 check_normalize("a/b/../../..", "..");
109 }
110 END_TEST
111
112 START_TEST(test_path_combine_and_normalize) {
113 check_combine_and_normalize("/path1/file1", "/path2/file2", "/path2/file2");
114 check_combine_and_normalize("path1/file1", "/path2/file2", "/path2/file2");
115 check_combine_and_normalize("/path1/file1", "path2/file2", "/path1/path2/file2");
116 check_combine_and_normalize("/path1", "/path2", "/path2");
117 check_combine_and_normalize("path1", "/path2", "/path2");
118 check_combine_and_normalize("/path1", "path2", "/path2");
119 check_combine_and_normalize("/path1/file1/../u", "path2/x/../y/z/../a/b/..", "/path1/path2/y/a");
120 check_combine_and_normalize("/path1/file1", "/path2//file2", "/path2/file2");
121 check_combine_and_normalize("/path1/file1", "/..", "/");
122 check_combine_and_normalize("/path1/file1", "../x", "/x");
123 check_combine_and_normalize("/path1/file1", "../../../x", "/x");
124 check_combine_and_normalize("/path1/file1", "/.x/..x/...x/x", "/.x/..x/...x/x");
125 }
126 END_TEST
127
128 static Suite *path_suite(void) {
129 Suite *s = suite_create("path");
130 TCase *tc_core = tcase_create("Core");
131
132 /*setup and teardown will be called before each test in testcase 'tc_core' */
133 tcase_add_checked_fixture(tc_core, setup, teardown);
134
135 suite_add_tcase(s, tc_core);
136 tcase_add_test(tc_core, test_path_combine);
137 tcase_add_test(tc_core, test_path_normalize);
138 tcase_add_test(tc_core, test_path_combine_and_normalize);
139
140 return s;
141 }
142
143 int main(void) {
144 int nf;
145 Suite *s = path_suite();
146 SRunner *sr = srunner_create(s);
147
148 srunner_set_xml(sr, LOGDIR "/unit/common/path.xml");
149 srunner_set_log(sr, LOGDIR "/unit/common/path.out");
150 srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
151 nf = srunner_ntests_failed(sr);
152 srunner_free(sr);
153 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
154 }