Crossfire Server, Trunk
check_path.cpp
Go to the documentation of this file.
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 crossfire-devel@real-time.com
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 #include "toolkit_common.h"
38 
39 #include "path.h"
40 
41 static void setup(void) {
42  /* put any initialisation steps here, they will be run before each testcase */
43 }
44 
45 static void teardown(void) {
46  /* put any cleanup steps here, they will be run after each testcase */
47 }
48 
49 static void check_combine(const char *src, const char *dst, const char *exp) {
50  char res[HUGE_BUF];
51 
52  path_combine(src, dst, res, HUGE_BUF);
53  FAIL_UNLESS(strcmp(res, exp) == 0, "path_combine(%s, %s) = %s but should be %s", src, dst, res, exp);
54 }
55 
56 static void check_normalize(const char *path, const char *exp) {
57  char tmp[HUGE_BUF];
58 
59  /* This is needed as path_normalize modifies in place. */
60  strncpy(tmp, path, sizeof(tmp));
61  tmp[HUGE_BUF-1] = '\0';
63  FAIL_UNLESS(strcmp(tmp, exp) == 0, "path_normalize(%s) = %s but should be %s", path, tmp, exp);
64 }
65 
66 static void check_combine_and_normalize(const char *src, const char *dst, const char *exp) {
67  char res[HUGE_BUF];
68 
69  path_combine_and_normalize(src, dst, res, sizeof(res));
70  FAIL_UNLESS(strcmp(res, exp) == 0, "path_combine_and_normalize(%s, %s) = %s but should be %s", src, dst, res, exp);
71 }
72 
73 START_TEST(test_path_combine) {
74  check_combine("/path1/file1", "/path2/file2", "/path2/file2");
75  check_combine("path1/file1", "/path2/file2", "/path2/file2");
76  check_combine("/path1/file1", "path2/file2", "/path1/path2/file2");
77  check_combine("path1/file1", "path2/file2", "path1/path2/file2");
78  check_combine("/path1", "/path2", "/path2");
79  check_combine("path1", "/path2", "/path2");
80  check_combine("/path1", "path2", "/path2");
81  check_combine("path1", "path2", "path2");
82 }
83 END_TEST
84 
85 START_TEST(test_path_normalize) {
86  check_normalize("", "");
87  check_normalize("/", "/");
88  check_normalize("path1/file1", "path1/file1");
89  check_normalize("/path1/file1", "/path1/file1");
90  check_normalize("/path1//file1", "/path1/file1");
91  check_normalize("//path1/file1", "/path1/file1");
92  check_normalize("///////x////////y///////z////////", "/x/y/z");
93  check_normalize("/a/b/../c/d/../e/../../f/g/../h", "/a/f/h");
94  check_normalize("//a//b//..//c//d//..//e//..//..//f//g//..//h", "/a/f/h");
95  check_normalize("../a", "../a");
96  check_normalize("a/../../b", "../b");
97  check_normalize("/../a", "/a");
98  check_normalize("/a/../../b", "/b");
99  check_normalize("./b/./c/.d/..e/./f", "b/c/.d/..e/f");
100  check_normalize("/b/././././e", "/b/e");
101  check_normalize(".", ""); /* maybe the result should be "."? */
102  check_normalize("/.", "/");
103  check_normalize("./", ""); /* maybe the result should be "."? */
104  check_normalize("/a/b/..", "/a");
105  check_normalize("/a/b/../..", "/");
106  check_normalize("/a/b/../../..", "/");
107  check_normalize("a/b/..", "a");
108  check_normalize("a/b/../..", "");
109  check_normalize("a/b/../../..", "..");
110 }
111 END_TEST
112 
113 START_TEST(test_path_combine_and_normalize) {
114  check_combine_and_normalize("/path1/file1", "/path2/file2", "/path2/file2");
115  check_combine_and_normalize("path1/file1", "/path2/file2", "/path2/file2");
116  check_combine_and_normalize("/path1/file1", "path2/file2", "/path1/path2/file2");
117  check_combine_and_normalize("/path1", "/path2", "/path2");
118  check_combine_and_normalize("path1", "/path2", "/path2");
119  check_combine_and_normalize("/path1", "path2", "/path2");
120  check_combine_and_normalize("/path1/file1/../u", "path2/x/../y/z/../a/b/..", "/path1/path2/y/a");
121  check_combine_and_normalize("/path1/file1", "/path2//file2", "/path2/file2");
122  check_combine_and_normalize("/path1/file1", "/..", "/");
123  check_combine_and_normalize("/path1/file1", "../x", "/x");
124  check_combine_and_normalize("/path1/file1", "../../../x", "/x");
125  check_combine_and_normalize("/path1/file1", "/.x/..x/...x/x", "/.x/..x/...x/x");
126 }
127 END_TEST
128 
129 static Suite *path_suite(void) {
130  Suite *s = suite_create("path");
131  TCase *tc_core = tcase_create("Core");
132 
133  /*setup and teardown will be called before each test in testcase 'tc_core' */
134  tcase_add_checked_fixture(tc_core, setup, teardown);
135 
136  suite_add_tcase(s, tc_core);
137  tcase_add_test(tc_core, test_path_combine);
138  tcase_add_test(tc_core, test_path_normalize);
139  tcase_add_test(tc_core, test_path_combine_and_normalize);
140 
141  return s;
142 }
143 
144 int main(void) {
145  int nf;
146  Suite *s = path_suite();
147  SRunner *sr = srunner_create(s);
148 
149  srunner_set_xml(sr, LOGDIR "/unit/common/path.xml");
150  srunner_set_log(sr, LOGDIR "/unit/common/path.out");
151  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
152  nf = srunner_ntests_failed(sr);
153  srunner_free(sr);
154  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
155 }
path.h
global.h
START_TEST
START_TEST(test_path_combine)
Definition: check_path.cpp:73
Ice.tmp
int tmp
Definition: Ice.py:207
HUGE_BUF
#define HUGE_BUF
Definition: define.h:37
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
toolkit_common.h
check_normalize
static void check_normalize(const char *path, const char *exp)
Definition: check_path.cpp:56
path_combine_and_normalize
char * path_combine_and_normalize(const char *src, const char *dst, char *path, size_t size)
Definition: path.cpp:172
python_init.path
path
Definition: python_init.py:8
path_normalize
void path_normalize(char *path)
Definition: path.cpp:85
path_suite
static END_TEST Suite * path_suite(void)
Definition: check_path.cpp:129
check_combine_and_normalize
static void check_combine_and_normalize(const char *src, const char *dst, const char *exp)
Definition: check_path.cpp:66
check_combine
static void check_combine(const char *src, const char *dst, const char *exp)
Definition: check_path.cpp:49
teardown
static void teardown(void)
Definition: check_path.cpp:45
path_combine
char * path_combine(const char *src, const char *dst, char *path, size_t size)
Definition: path.cpp:51
setup
static void setup(void)
Definition: check_path.cpp:41
main
int main(void)
Definition: check_path.cpp:144
altar_valkyrie.res
int res
Definition: altar_valkyrie.py:74