1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "gtest/gtest.h"
9 #include "nacl_io/kernel_proxy.h"
10 #include "nacl_io/path.h"
12 using namespace nacl_io
;
14 TEST(PathTest
, SanityChecks
) {
15 // can we construct and delete?
17 Path
*ph2
= new Path(".");
21 EXPECT_FALSE(p1
.IsAbsolute());
22 EXPECT_EQ(".", p1
.Join());
24 EXPECT_TRUE(p2
.IsAbsolute());
25 EXPECT_EQ("/", p2
.Join());
28 TEST(PathTest
, Assignment
) {
32 Path
abs_str("/abs/from/string");
33 Path
rel_str("rel/from/string");
34 Path
self_str("./rel/from/string");
36 EXPECT_EQ(0, empty
.Size());
37 EXPECT_FALSE(empty
.IsAbsolute());
38 EXPECT_EQ("", empty
.Join());
40 EXPECT_EQ(1, dot
.Size());
41 EXPECT_FALSE(dot
.IsAbsolute());
42 EXPECT_EQ(".", dot
.Join());
44 EXPECT_EQ(1, root
.Size());
45 EXPECT_TRUE(root
.IsAbsolute());
46 EXPECT_EQ("/", root
.Join());
48 EXPECT_EQ(4, abs_str
.Size());
49 EXPECT_TRUE(abs_str
.IsAbsolute());
50 EXPECT_EQ("/abs/from/string", abs_str
.Join());
52 EXPECT_EQ(3, rel_str
.Size());
53 EXPECT_FALSE(rel_str
.IsAbsolute());
54 EXPECT_EQ("rel/from/string", rel_str
.Join());
56 EXPECT_EQ(3, self_str
.Size());
57 EXPECT_FALSE(self_str
.IsAbsolute());
58 EXPECT_EQ("rel/from/string", self_str
.Join());
63 abs_str
= "/abs/from/assign";
64 rel_str
= "rel/from/assign";
65 self_str
= "./rel/from/assign";
67 EXPECT_EQ(1, empty
.Size());
68 EXPECT_FALSE(empty
.IsAbsolute());
69 EXPECT_EQ(".", empty
.Join());
71 EXPECT_EQ(1, dot
.Size());
72 EXPECT_FALSE(dot
.IsAbsolute());
73 EXPECT_EQ(".", dot
.Join());
75 EXPECT_EQ(1, root
.Size());
76 EXPECT_TRUE(root
.IsAbsolute());
77 EXPECT_EQ("/", root
.Join());
79 EXPECT_EQ(4, abs_str
.Size());
80 EXPECT_TRUE(abs_str
.IsAbsolute());
81 EXPECT_EQ("/abs/from/assign", abs_str
.Join());
83 EXPECT_EQ(3, rel_str
.Size());
84 EXPECT_FALSE(rel_str
.IsAbsolute());
85 EXPECT_EQ("rel/from/assign", rel_str
.Join());
87 EXPECT_EQ(3, self_str
.Size());
88 EXPECT_FALSE(self_str
.IsAbsolute());
89 EXPECT_EQ("rel/from/assign", self_str
.Join());
93 EXPECT_EQ(1, cpy_str
.Size());
94 EXPECT_FALSE(cpy_str
.IsAbsolute());
95 EXPECT_EQ(".", cpy_str
.Join());
98 EXPECT_EQ(1, cpy_str
.Size());
99 EXPECT_FALSE(cpy_str
.IsAbsolute());
100 EXPECT_EQ(".", cpy_str
.Join());
103 EXPECT_EQ(1, cpy_str
.Size());
104 EXPECT_TRUE(cpy_str
.IsAbsolute());
105 EXPECT_EQ("/", cpy_str
.Join());
108 EXPECT_EQ(4, cpy_str
.Size());
109 EXPECT_TRUE(cpy_str
.IsAbsolute());
110 EXPECT_EQ("/abs/from/assign", cpy_str
.Join());
113 EXPECT_EQ(3, cpy_str
.Size());
114 EXPECT_FALSE(cpy_str
.IsAbsolute());
115 EXPECT_EQ("rel/from/assign", cpy_str
.Join());
118 EXPECT_EQ(3, cpy_str
.Size());
119 EXPECT_FALSE(cpy_str
.IsAbsolute());
120 EXPECT_EQ("rel/from/assign", cpy_str
.Join());
124 TEST(PathTest
, Collapse
) {
125 StringArray_t path_components
;
127 Path
p1("/simple/splitter/test");
128 path_components
= p1
.Split();
129 EXPECT_EQ("/", path_components
[0]);
130 EXPECT_EQ("/", p1
.Part(0));
132 EXPECT_EQ("simple", path_components
[1]);
133 EXPECT_EQ("simple", p1
.Part(1));
135 EXPECT_EQ("splitter",path_components
[2]);
136 EXPECT_EQ("splitter",p1
.Part(2));
138 EXPECT_EQ("test", path_components
[3]);
139 EXPECT_EQ("test", p1
.Part(3));
141 Path
p2("///simple//splitter///test/");
142 path_components
= p2
.Split();
143 EXPECT_EQ(4, static_cast<int>(path_components
.size()));
144 EXPECT_EQ(4, static_cast<int>(p2
.Size()));
145 EXPECT_EQ("/", path_components
[0]);
146 EXPECT_EQ("simple", path_components
[1]);
147 EXPECT_EQ("splitter", path_components
[2]);
148 EXPECT_EQ("test", path_components
[3]);
150 Path
p3("sim/ple//spli/tter/te/st/");
151 path_components
= p3
.Split();
152 EXPECT_EQ(6, static_cast<int>(path_components
.size()));
153 EXPECT_FALSE(p3
.IsAbsolute());
154 EXPECT_EQ("sim", path_components
[0]);
155 EXPECT_EQ("ple", path_components
[1]);
156 EXPECT_EQ("spli", path_components
[2]);
157 EXPECT_EQ("tter", path_components
[3]);
158 EXPECT_EQ("te", path_components
[4]);
159 EXPECT_EQ("st", path_components
[5]);
162 path_components
= p4
.Split();
163 EXPECT_EQ(1, static_cast<int>(path_components
.size()));
166 path_components
= p5
.Split();
167 EXPECT_EQ(1, static_cast<int>(path_components
.size()));
170 TEST(PathTest
, AppendAndJoin
) {
171 Path
ph1("/usr/local/hi/there");
173 EXPECT_EQ("/usr/local/hi/there", ph1
.Join());
174 ph1
= ph1
.Append("..");
175 EXPECT_EQ("/usr/local/hi", ph1
.Join());
176 ph1
= ph1
.Append(".././././hi/there/../.././././");
177 EXPECT_EQ("/usr/local", ph1
.Join());
178 ph1
= ph1
.Append("../../../../../../../../././../");
179 EXPECT_EQ("/", ph1
.Join());
180 ph1
= ph1
.Append("usr/lib/../bin/.././etc/../local/../share");
181 EXPECT_EQ("/usr/share", ph1
.Join());
184 EXPECT_EQ(".", ph2
.Join());
187 EXPECT_EQ("/", ph3
.Join());
188 ph3
= ph3
.Append("");
189 EXPECT_EQ("/", ph3
.Join());
190 ph3
= ph3
.Append("USR/local/SHARE");
191 EXPECT_EQ("/USR/local/SHARE", ph3
.Join());
194 EXPECT_EQ("..", ph4
.Join());
195 ph4
= ph4
.Append("node1/node3/../../node1/./");
196 EXPECT_EQ("../node1", ph4
.Join());
197 ph4
= ph4
.Append("node4/../../node1/./node5");
198 EXPECT_EQ("../node1/node5", ph4
.Join());
201 TEST(PathTest
, AppendAbsolute
) {
202 Path
path("/usr/local");
204 EXPECT_EQ("/foo", path
.Join());
207 TEST(PathTest
, Invalid
) {
208 Path
absolute("/usr/local");
209 Path
current("./usr/local");
210 Path
relative("usr/local");
215 test
.Append("../..");
216 EXPECT_EQ("/", test
.Join());
219 test
.Append("../../..");
220 EXPECT_EQ("/", test
.Join());
223 test
.Append("../../../foo");
224 EXPECT_EQ("/foo", test
.Join());
227 test
.Append("../..");
228 EXPECT_EQ(".", test
.Join());
231 test
.Append("../../..");
232 EXPECT_EQ("..", test
.Join());
235 test
.Append("../../../foo");
236 EXPECT_EQ("../foo", test
.Join());
239 test
.Append("../..");
240 EXPECT_EQ(".", test
.Join());
243 test
.Append("../../..");
244 EXPECT_EQ("..", test
.Join());
247 test
.Append("../../../foo");
248 EXPECT_EQ("../foo", test
.Join());
251 TEST(PathTest
, Range
) {
252 Path
p("/an/absolute/path");
254 // p's parts should be ["/", "an", "absolute", "path"].
255 EXPECT_EQ("/an/absolute/path", p
.Range(0, 4));
256 EXPECT_EQ("an/absolute/path", p
.Range(1, 4));
257 EXPECT_EQ("absolute/path", p
.Range(2, 4));
258 EXPECT_EQ("path", p
.Range(3, 4));
260 EXPECT_EQ("/an/absolute", p
.Range(0, 3));
261 EXPECT_EQ("an/absolute", p
.Range(1, 3));
262 EXPECT_EQ("absolute", p
.Range(2, 3));