1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "utils/fs/path.hpp"
30
31 extern "C" {
32 #include <unistd.h>
33 }
34
35 #include <set>
36
37 #include <atf-c++.hpp>
38
39 #include "utils/fs/exceptions.hpp"
40
41 using utils::fs::invalid_path_error;
42 using utils::fs::join_error;
43 using utils::fs::path;
44
45
46 #define REQUIRE_JOIN_ERROR(path1, path2, expr) \
47 try { \
48 expr; \
49 ATF_FAIL("Expecting join_error but no error raised"); \
50 } catch (const join_error& e) { \
51 ATF_REQUIRE_EQ(path1, e.textual_path1()); \
52 ATF_REQUIRE_EQ(path2, e.textual_path2()); \
53 }
54
55
56 ATF_TEST_CASE_WITHOUT_HEAD(normalize__ok);
ATF_TEST_CASE_BODY(normalize__ok)57 ATF_TEST_CASE_BODY(normalize__ok)
58 {
59 ATF_REQUIRE_EQ(".", path(".").str());
60 ATF_REQUIRE_EQ("..", path("..").str());
61 ATF_REQUIRE_EQ("/", path("/").str());
62 ATF_REQUIRE_EQ("/", path("///").str());
63
64 ATF_REQUIRE_EQ("foo", path("foo").str());
65 ATF_REQUIRE_EQ("foo/bar", path("foo/bar").str());
66 ATF_REQUIRE_EQ("foo/bar", path("foo/bar/").str());
67
68 ATF_REQUIRE_EQ("/foo", path("/foo").str());
69 ATF_REQUIRE_EQ("/foo/bar", path("/foo/bar").str());
70 ATF_REQUIRE_EQ("/foo/bar", path("/foo/bar/").str());
71
72 ATF_REQUIRE_EQ("/foo", path("///foo").str());
73 ATF_REQUIRE_EQ("/foo/bar", path("///foo///bar").str());
74 ATF_REQUIRE_EQ("/foo/bar", path("///foo///bar///").str());
75
76 ATF_REQUIRE_EQ("./foo/bar", path("./foo/bar").str());
77 ATF_REQUIRE_EQ("./foo/bar", path("./foo/./bar").str());
78 ATF_REQUIRE_EQ("./foo/bar", path("././foo/./bar").str());
79 ATF_REQUIRE_EQ("foo/bar", path("foo/././bar").str());
80 }
81
82
83 ATF_TEST_CASE_WITHOUT_HEAD(normalize__invalid);
ATF_TEST_CASE_BODY(normalize__invalid)84 ATF_TEST_CASE_BODY(normalize__invalid)
85 {
86 try {
87 path("");
88 fail("invalid_path_error not raised");
89 } catch (const invalid_path_error& e) {
90 ATF_REQUIRE(e.invalid_path().empty());
91 }
92 }
93
94
95 ATF_TEST_CASE_WITHOUT_HEAD(is_absolute);
ATF_TEST_CASE_BODY(is_absolute)96 ATF_TEST_CASE_BODY(is_absolute)
97 {
98 ATF_REQUIRE( path("/").is_absolute());
99 ATF_REQUIRE( path("////").is_absolute());
100 ATF_REQUIRE( path("////a").is_absolute());
101 ATF_REQUIRE( path("//a//").is_absolute());
102 ATF_REQUIRE(!path("a////").is_absolute());
103 ATF_REQUIRE(!path("../foo").is_absolute());
104 }
105
106
107 ATF_TEST_CASE_WITHOUT_HEAD(is_parent_of);
ATF_TEST_CASE_BODY(is_parent_of)108 ATF_TEST_CASE_BODY(is_parent_of)
109 {
110 ATF_REQUIRE( path("/").is_parent_of(path("/")));
111 ATF_REQUIRE( path(".").is_parent_of(path(".")));
112 ATF_REQUIRE( path("/a").is_parent_of(path("/a")));
113 ATF_REQUIRE( path("/a/b/c").is_parent_of(path("/a/b/c")));
114 ATF_REQUIRE( path("a").is_parent_of(path("a")));
115 ATF_REQUIRE( path("a/b/c").is_parent_of(path("a/b/c")));
116
117 ATF_REQUIRE( path("/a/b/c").is_parent_of(path("/a/b/c/d")));
118 ATF_REQUIRE( path("/a/b/c").is_parent_of(path("/a/b/c/d/e")));
119 ATF_REQUIRE(!path("/a/b/c").is_parent_of(path("a/b/c")));
120 ATF_REQUIRE(!path("/a/b/c").is_parent_of(path("a/b/c/d/e")));
121
122 ATF_REQUIRE( path("a/b/c").is_parent_of(path("a/b/c/d")));
123 ATF_REQUIRE( path("a/b/c").is_parent_of(path("a/b/c/d/e")));
124 ATF_REQUIRE(!path("a/b/c").is_parent_of(path("/a/b/c")));
125 ATF_REQUIRE(!path("a/b/c").is_parent_of(path("/a/b/c/d/e")));
126
127 ATF_REQUIRE(!path("/a/b/c/d/e").is_parent_of(path("/a/b/c")));
128 ATF_REQUIRE(!path("/a/b/c/d/e").is_parent_of(path("a/b/c")));
129 ATF_REQUIRE(!path("a/b/c/d/e").is_parent_of(path("/a/b/c")));
130 ATF_REQUIRE(!path("a/b/c/d/e").is_parent_of(path("a/b/c")));
131 }
132
133
134 ATF_TEST_CASE_WITHOUT_HEAD(ncomponents);
ATF_TEST_CASE_BODY(ncomponents)135 ATF_TEST_CASE_BODY(ncomponents)
136 {
137 ATF_REQUIRE_EQ(1, path(".").ncomponents());
138 ATF_REQUIRE_EQ(1, path("/").ncomponents());
139
140 ATF_REQUIRE_EQ(1, path("abc").ncomponents());
141 ATF_REQUIRE_EQ(1, path("abc/").ncomponents());
142
143 ATF_REQUIRE_EQ(2, path("/abc").ncomponents());
144 ATF_REQUIRE_EQ(3, path("/abc/def").ncomponents());
145
146 ATF_REQUIRE_EQ(2, path("abc/def").ncomponents());
147 }
148
149
150 ATF_TEST_CASE_WITHOUT_HEAD(branch_path);
ATF_TEST_CASE_BODY(branch_path)151 ATF_TEST_CASE_BODY(branch_path)
152 {
153 ATF_REQUIRE_EQ(".", path(".").branch_path().str());
154 ATF_REQUIRE_EQ(".", path("foo").branch_path().str());
155 ATF_REQUIRE_EQ("foo", path("foo/bar").branch_path().str());
156 ATF_REQUIRE_EQ("/", path("/foo").branch_path().str());
157 ATF_REQUIRE_EQ("/foo", path("/foo/bar").branch_path().str());
158 }
159
160
161 ATF_TEST_CASE_WITHOUT_HEAD(leaf_name);
ATF_TEST_CASE_BODY(leaf_name)162 ATF_TEST_CASE_BODY(leaf_name)
163 {
164 ATF_REQUIRE_EQ(".", path(".").leaf_name());
165 ATF_REQUIRE_EQ("foo", path("foo").leaf_name());
166 ATF_REQUIRE_EQ("bar", path("foo/bar").leaf_name());
167 ATF_REQUIRE_EQ("foo", path("/foo").leaf_name());
168 ATF_REQUIRE_EQ("bar", path("/foo/bar").leaf_name());
169 }
170
171
172 ATF_TEST_CASE_WITHOUT_HEAD(to_absolute);
ATF_TEST_CASE_BODY(to_absolute)173 ATF_TEST_CASE_BODY(to_absolute)
174 {
175 ATF_REQUIRE(::chdir("/bin") != -1);
176 const std::string absolute = path("ls").to_absolute().str();
177 // In some systems (e.g. in Fedora 17), /bin is really a symlink to
178 // /usr/bin. Doing an explicit match of 'absolute' to /bin/ls fails in such
179 // case. Instead, attempt doing a search in the generated path just for a
180 // substring containing '/bin/ls'. Note that this can still fail if /bin is
181 // linked to something arbitrary like /a/b... but let's just assume this
182 // does not happen.
183 ATF_REQUIRE(absolute.find("/bin/ls") != std::string::npos);
184 }
185
186
187 ATF_TEST_CASE_WITHOUT_HEAD(compare_less_than);
ATF_TEST_CASE_BODY(compare_less_than)188 ATF_TEST_CASE_BODY(compare_less_than)
189 {
190 ATF_REQUIRE(!(path("/") < path("/")));
191 ATF_REQUIRE(!(path("/") < path("///")));
192
193 ATF_REQUIRE(!(path("/a/b/c") < path("/a/b/c")));
194
195 ATF_REQUIRE( path("/a") < path("/b"));
196 ATF_REQUIRE(!(path("/b") < path("/a")));
197
198 ATF_REQUIRE( path("/a") < path("/aa"));
199 ATF_REQUIRE(!(path("/aa") < path("/a")));
200 }
201
202
203 ATF_TEST_CASE_WITHOUT_HEAD(compare_equal);
ATF_TEST_CASE_BODY(compare_equal)204 ATF_TEST_CASE_BODY(compare_equal)
205 {
206 ATF_REQUIRE(path("/") == path("///"));
207 ATF_REQUIRE(path("/a") == path("///a"));
208 ATF_REQUIRE(path("/a") == path("///a///"));
209
210 ATF_REQUIRE(path("a/b/c") == path("a//b//c"));
211 ATF_REQUIRE(path("a/b/c") == path("a//b//c///"));
212 }
213
214
215 ATF_TEST_CASE_WITHOUT_HEAD(compare_different);
ATF_TEST_CASE_BODY(compare_different)216 ATF_TEST_CASE_BODY(compare_different)
217 {
218 ATF_REQUIRE(path("/") != path("//a/"));
219 ATF_REQUIRE(path("/a") != path("a///"));
220
221 ATF_REQUIRE(path("a/b/c") != path("a/b"));
222 ATF_REQUIRE(path("a/b/c") != path("a//b"));
223 ATF_REQUIRE(path("a/b/c") != path("/a/b/c"));
224 ATF_REQUIRE(path("a/b/c") != path("/a//b//c"));
225 }
226
227
228 ATF_TEST_CASE_WITHOUT_HEAD(concat__to_string);
ATF_TEST_CASE_BODY(concat__to_string)229 ATF_TEST_CASE_BODY(concat__to_string)
230 {
231 ATF_REQUIRE_EQ("foo/bar", (path("foo") / "bar").str());
232 ATF_REQUIRE_EQ("foo/bar", (path("foo/") / "bar").str());
233 ATF_REQUIRE_EQ("foo/bar/baz", (path("foo/") / "bar//baz///").str());
234
235 ATF_REQUIRE_THROW(invalid_path_error, path("foo") / "");
236 REQUIRE_JOIN_ERROR("foo", "/a/b", path("foo") / "/a/b");
237 }
238
239
240 ATF_TEST_CASE_WITHOUT_HEAD(concat__to_path);
ATF_TEST_CASE_BODY(concat__to_path)241 ATF_TEST_CASE_BODY(concat__to_path)
242 {
243 ATF_REQUIRE_EQ("foo/bar", (path("foo") / "bar").str());
244 ATF_REQUIRE_EQ("foo/bar", (path("foo/") / "bar").str());
245 ATF_REQUIRE_EQ("foo/bar/baz", (path("foo/") / "bar//baz///").str());
246
247 REQUIRE_JOIN_ERROR("foo", "/a/b", path("foo") / path("/a/b"));
248 }
249
250
251 ATF_TEST_CASE_WITHOUT_HEAD(use_as_key);
ATF_TEST_CASE_BODY(use_as_key)252 ATF_TEST_CASE_BODY(use_as_key)
253 {
254 std::set< path > paths;
255 paths.insert(path("/a"));
256 ATF_REQUIRE(paths.find(path("//a")) != paths.end());
257 ATF_REQUIRE(paths.find(path("a")) == paths.end());
258 }
259
260
ATF_INIT_TEST_CASES(tcs)261 ATF_INIT_TEST_CASES(tcs)
262 {
263 ATF_ADD_TEST_CASE(tcs, normalize__ok);
264 ATF_ADD_TEST_CASE(tcs, normalize__invalid);
265 ATF_ADD_TEST_CASE(tcs, is_absolute);
266 ATF_ADD_TEST_CASE(tcs, is_parent_of);
267 ATF_ADD_TEST_CASE(tcs, ncomponents);
268 ATF_ADD_TEST_CASE(tcs, branch_path);
269 ATF_ADD_TEST_CASE(tcs, leaf_name);
270 ATF_ADD_TEST_CASE(tcs, to_absolute);
271 ATF_ADD_TEST_CASE(tcs, compare_less_than);
272 ATF_ADD_TEST_CASE(tcs, compare_equal);
273 ATF_ADD_TEST_CASE(tcs, compare_different);
274 ATF_ADD_TEST_CASE(tcs, concat__to_string);
275 ATF_ADD_TEST_CASE(tcs, concat__to_path);
276 ATF_ADD_TEST_CASE(tcs, use_as_key);
277 }
278