Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
5 : This program is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as published by
7 : the Free Software Foundation; either version 2.1 of the License, or
8 : (at your option) any later version.
9 :
10 : This program is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General Public License along
16 : with this program; if not, write to the Free Software Foundation, Inc.,
17 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #include "test.h"
21 :
22 : #include <sstream>
23 :
24 : #include "log.h"
25 : #include "serialization.h"
26 : #include "nodedef.h"
27 : #include "noise.h"
28 :
29 1 : class TestFilePath : public TestBase {
30 : public:
31 1 : TestFilePath() { TestManager::registerTestModule(this); }
32 0 : const char *getName() { return "TestFilePath"; }
33 :
34 : void runTests(IGameDef *gamedef);
35 :
36 : void testIsDirDelimiter();
37 : void testPathStartsWith();
38 : void testRemoveLastPathComponent();
39 : void testRemoveLastPathComponentWithTrailingDelimiter();
40 : void testRemoveRelativePathComponent();
41 : };
42 :
43 1 : static TestFilePath g_test_instance;
44 :
45 0 : void TestFilePath::runTests(IGameDef *gamedef)
46 : {
47 0 : TEST(testIsDirDelimiter);
48 0 : TEST(testPathStartsWith);
49 0 : TEST(testRemoveLastPathComponent);
50 0 : TEST(testRemoveLastPathComponentWithTrailingDelimiter);
51 0 : TEST(testRemoveRelativePathComponent);
52 0 : }
53 :
54 : ////////////////////////////////////////////////////////////////////////////////
55 :
56 : // adjusts a POSIX path to system-specific conventions
57 : // -> changes '/' to DIR_DELIM
58 : // -> absolute paths start with "C:\\" on windows
59 0 : std::string p(std::string path)
60 : {
61 0 : for (size_t i = 0; i < path.size(); ++i) {
62 0 : if (path[i] == '/') {
63 0 : path.replace(i, 1, DIR_DELIM);
64 0 : i += std::string(DIR_DELIM).size() - 1; // generally a no-op
65 : }
66 : }
67 :
68 : #ifdef _WIN32
69 : if (path[0] == '\\')
70 : path = "C:" + path;
71 : #endif
72 :
73 0 : return path;
74 : }
75 :
76 :
77 0 : void TestFilePath::testIsDirDelimiter()
78 : {
79 0 : UASSERT(fs::IsDirDelimiter('/') == true);
80 0 : UASSERT(fs::IsDirDelimiter('A') == false);
81 0 : UASSERT(fs::IsDirDelimiter(0) == false);
82 : #ifdef _WIN32
83 : UASSERT(fs::IsDirDelimiter('\\') == true);
84 : #else
85 0 : UASSERT(fs::IsDirDelimiter('\\') == false);
86 : #endif
87 0 : }
88 :
89 :
90 0 : void TestFilePath::testPathStartsWith()
91 : {
92 0 : const int numpaths = 12;
93 : std::string paths[numpaths] = {
94 : "",
95 : p("/"),
96 : p("/home/user/minetest"),
97 : p("/home/user/minetest/bin"),
98 : p("/home/user/.minetest"),
99 : p("/tmp/dir/file"),
100 : p("/tmp/file/"),
101 : p("/tmP/file"),
102 : p("/tmp"),
103 : p("/tmp/dir"),
104 : p("/home/user2/minetest/worlds"),
105 : p("/home/user2/minetest/world"),
106 0 : };
107 : /*
108 : expected fs::PathStartsWith results
109 : 0 = returns false
110 : 1 = returns true
111 : 2 = returns false on windows, true elsewhere
112 : 3 = returns true on windows, false elsewhere
113 : 4 = returns true if and only if
114 : FILESYS_CASE_INSENSITIVE is true
115 : */
116 : int expected_results[numpaths][numpaths] = {
117 : {1,2,0,0,0,0,0,0,0,0,0,0},
118 : {1,1,0,0,0,0,0,0,0,0,0,0},
119 : {1,1,1,0,0,0,0,0,0,0,0,0},
120 : {1,1,1,1,0,0,0,0,0,0,0,0},
121 : {1,1,0,0,1,0,0,0,0,0,0,0},
122 : {1,1,0,0,0,1,0,0,1,1,0,0},
123 : {1,1,0,0,0,0,1,4,1,0,0,0},
124 : {1,1,0,0,0,0,4,1,4,0,0,0},
125 : {1,1,0,0,0,0,0,0,1,0,0,0},
126 : {1,1,0,0,0,0,0,0,1,1,0,0},
127 : {1,1,0,0,0,0,0,0,0,0,1,0},
128 : {1,1,0,0,0,0,0,0,0,0,0,1},
129 0 : };
130 :
131 0 : for (int i = 0; i < numpaths; i++)
132 0 : for (int j = 0; j < numpaths; j++){
133 : /*verbosestream<<"testing fs::PathStartsWith(\""
134 : <<paths[i]<<"\", \""
135 : <<paths[j]<<"\")"<<std::endl;*/
136 0 : bool starts = fs::PathStartsWith(paths[i], paths[j]);
137 0 : int expected = expected_results[i][j];
138 0 : if(expected == 0){
139 0 : UASSERT(starts == false);
140 : }
141 0 : else if(expected == 1){
142 0 : UASSERT(starts == true);
143 : }
144 : #ifdef _WIN32
145 : else if(expected == 2){
146 : UASSERT(starts == false);
147 : }
148 : else if(expected == 3){
149 : UASSERT(starts == true);
150 : }
151 : #else
152 0 : else if(expected == 2){
153 0 : UASSERT(starts == true);
154 : }
155 0 : else if(expected == 3){
156 0 : UASSERT(starts == false);
157 : }
158 : #endif
159 0 : else if(expected == 4){
160 0 : UASSERT(starts == (bool)FILESYS_CASE_INSENSITIVE);
161 : }
162 : }
163 0 : }
164 :
165 :
166 0 : void TestFilePath::testRemoveLastPathComponent()
167 : {
168 0 : std::string path, result, removed;
169 :
170 0 : UASSERT(fs::RemoveLastPathComponent("") == "");
171 0 : path = p("/home/user/minetest/bin/..//worlds/world1");
172 0 : result = fs::RemoveLastPathComponent(path, &removed, 0);
173 0 : UASSERT(result == path);
174 0 : UASSERT(removed == "");
175 0 : result = fs::RemoveLastPathComponent(path, &removed, 1);
176 0 : UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
177 0 : UASSERT(removed == p("world1"));
178 0 : result = fs::RemoveLastPathComponent(path, &removed, 2);
179 0 : UASSERT(result == p("/home/user/minetest/bin/.."));
180 0 : UASSERT(removed == p("worlds/world1"));
181 0 : result = fs::RemoveLastPathComponent(path, &removed, 3);
182 0 : UASSERT(result == p("/home/user/minetest/bin"));
183 0 : UASSERT(removed == p("../worlds/world1"));
184 0 : result = fs::RemoveLastPathComponent(path, &removed, 4);
185 0 : UASSERT(result == p("/home/user/minetest"));
186 0 : UASSERT(removed == p("bin/../worlds/world1"));
187 0 : result = fs::RemoveLastPathComponent(path, &removed, 5);
188 0 : UASSERT(result == p("/home/user"));
189 0 : UASSERT(removed == p("minetest/bin/../worlds/world1"));
190 0 : result = fs::RemoveLastPathComponent(path, &removed, 6);
191 0 : UASSERT(result == p("/home"));
192 0 : UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
193 0 : result = fs::RemoveLastPathComponent(path, &removed, 7);
194 : #ifdef _WIN32
195 : UASSERT(result == "C:");
196 : #else
197 0 : UASSERT(result == "");
198 : #endif
199 0 : UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
200 0 : }
201 :
202 :
203 0 : void TestFilePath::testRemoveLastPathComponentWithTrailingDelimiter()
204 : {
205 0 : std::string path, result, removed;
206 :
207 0 : path = p("/home/user/minetest/bin/..//worlds/world1/");
208 0 : result = fs::RemoveLastPathComponent(path, &removed, 0);
209 0 : UASSERT(result == path);
210 0 : UASSERT(removed == "");
211 0 : result = fs::RemoveLastPathComponent(path, &removed, 1);
212 0 : UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
213 0 : UASSERT(removed == p("world1"));
214 0 : result = fs::RemoveLastPathComponent(path, &removed, 2);
215 0 : UASSERT(result == p("/home/user/minetest/bin/.."));
216 0 : UASSERT(removed == p("worlds/world1"));
217 0 : result = fs::RemoveLastPathComponent(path, &removed, 3);
218 0 : UASSERT(result == p("/home/user/minetest/bin"));
219 0 : UASSERT(removed == p("../worlds/world1"));
220 0 : result = fs::RemoveLastPathComponent(path, &removed, 4);
221 0 : UASSERT(result == p("/home/user/minetest"));
222 0 : UASSERT(removed == p("bin/../worlds/world1"));
223 0 : result = fs::RemoveLastPathComponent(path, &removed, 5);
224 0 : UASSERT(result == p("/home/user"));
225 0 : UASSERT(removed == p("minetest/bin/../worlds/world1"));
226 0 : result = fs::RemoveLastPathComponent(path, &removed, 6);
227 0 : UASSERT(result == p("/home"));
228 0 : UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
229 0 : result = fs::RemoveLastPathComponent(path, &removed, 7);
230 : #ifdef _WIN32
231 : UASSERT(result == "C:");
232 : #else
233 0 : UASSERT(result == "");
234 : #endif
235 0 : UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
236 0 : }
237 :
238 :
239 0 : void TestFilePath::testRemoveRelativePathComponent()
240 : {
241 0 : std::string path, result, removed;
242 :
243 0 : path = p("/home/user/minetest/bin");
244 0 : result = fs::RemoveRelativePathComponents(path);
245 0 : UASSERT(result == path);
246 0 : path = p("/home/user/minetest/bin/../worlds/world1");
247 0 : result = fs::RemoveRelativePathComponents(path);
248 0 : UASSERT(result == p("/home/user/minetest/worlds/world1"));
249 0 : path = p("/home/user/minetest/bin/../worlds/world1/");
250 0 : result = fs::RemoveRelativePathComponents(path);
251 0 : UASSERT(result == p("/home/user/minetest/worlds/world1"));
252 0 : path = p(".");
253 0 : result = fs::RemoveRelativePathComponents(path);
254 0 : UASSERT(result == "");
255 0 : path = p("./subdir/../..");
256 0 : result = fs::RemoveRelativePathComponents(path);
257 0 : UASSERT(result == "");
258 0 : path = p("/a/b/c/.././../d/../e/f/g/../h/i/j/../../../..");
259 0 : result = fs::RemoveRelativePathComponents(path);
260 0 : UASSERT(result == p("/a/e"));
261 3 : }
|