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.
4
5 #include <stddef.h>
6
7 #include <sstream>
8
9 #include "base/files/file_path.h"
10 #include "base/macros.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15
16 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
17 #include "base/test/scoped_locale.h"
18 #endif
19
20 // This macro helps avoid wrapped lines in the test structs.
21 #define FPL(x) FILE_PATH_LITERAL(x)
22
23 // This macro constructs strings which can contain NULs.
24 #define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
25
26 namespace base {
27
28 struct UnaryTestData {
29 const FilePath::CharType* input;
30 const FilePath::CharType* expected;
31 };
32
33 struct UnaryBooleanTestData {
34 const FilePath::CharType* input;
35 bool expected;
36 };
37
38 struct BinaryTestData {
39 const FilePath::CharType* inputs[2];
40 const FilePath::CharType* expected;
41 };
42
43 struct BinaryBooleanTestData {
44 const FilePath::CharType* inputs[2];
45 bool expected;
46 };
47
48 struct BinaryIntTestData {
49 const FilePath::CharType* inputs[2];
50 int expected;
51 };
52
53 struct UTF8TestData {
54 const FilePath::CharType* native;
55 const char* utf8;
56 };
57
58 // file_util winds up using autoreleased objects on the Mac, so this needs
59 // to be a PlatformTest
60 typedef PlatformTest FilePathTest;
61
TEST_F(FilePathTest,DirName)62 TEST_F(FilePathTest, DirName) {
63 const struct UnaryTestData cases[] = {
64 { FPL(""), FPL(".") },
65 { FPL("aa"), FPL(".") },
66 { FPL("/aa/bb"), FPL("/aa") },
67 { FPL("/aa/bb/"), FPL("/aa") },
68 { FPL("/aa/bb//"), FPL("/aa") },
69 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
70 { FPL("/aa"), FPL("/") },
71 { FPL("/aa/"), FPL("/") },
72 { FPL("/"), FPL("/") },
73 { FPL("//"), FPL("//") },
74 { FPL("///"), FPL("/") },
75 { FPL("aa/"), FPL(".") },
76 { FPL("aa/bb"), FPL("aa") },
77 { FPL("aa/bb/"), FPL("aa") },
78 { FPL("aa/bb//"), FPL("aa") },
79 { FPL("aa//bb//"), FPL("aa") },
80 { FPL("aa//bb/"), FPL("aa") },
81 { FPL("aa//bb"), FPL("aa") },
82 { FPL("//aa/bb"), FPL("//aa") },
83 { FPL("//aa/"), FPL("//") },
84 { FPL("//aa"), FPL("//") },
85 { FPL("0:"), FPL(".") },
86 { FPL("@:"), FPL(".") },
87 { FPL("[:"), FPL(".") },
88 { FPL("`:"), FPL(".") },
89 { FPL("{:"), FPL(".") },
90 { FPL("\xB3:"), FPL(".") },
91 { FPL("\xC5:"), FPL(".") },
92 { FPL("/aa/../bb/cc"), FPL("/aa/../bb")},
93 #if defined(OS_WIN)
94 { FPL("\x0143:"), FPL(".") },
95 #endif // OS_WIN
96 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
97 { FPL("c:"), FPL("c:") },
98 { FPL("C:"), FPL("C:") },
99 { FPL("A:"), FPL("A:") },
100 { FPL("Z:"), FPL("Z:") },
101 { FPL("a:"), FPL("a:") },
102 { FPL("z:"), FPL("z:") },
103 { FPL("c:aa"), FPL("c:") },
104 { FPL("c:/"), FPL("c:/") },
105 { FPL("c://"), FPL("c://") },
106 { FPL("c:///"), FPL("c:/") },
107 { FPL("c:/aa"), FPL("c:/") },
108 { FPL("c:/aa/"), FPL("c:/") },
109 { FPL("c:/aa/bb"), FPL("c:/aa") },
110 { FPL("c:aa/bb"), FPL("c:aa") },
111 #endif // FILE_PATH_USES_DRIVE_LETTERS
112 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
113 { FPL("\\aa\\bb"), FPL("\\aa") },
114 { FPL("\\aa\\bb\\"), FPL("\\aa") },
115 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
116 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
117 { FPL("\\aa"), FPL("\\") },
118 { FPL("\\aa\\"), FPL("\\") },
119 { FPL("\\"), FPL("\\") },
120 { FPL("\\\\"), FPL("\\\\") },
121 { FPL("\\\\\\"), FPL("\\") },
122 { FPL("aa\\"), FPL(".") },
123 { FPL("aa\\bb"), FPL("aa") },
124 { FPL("aa\\bb\\"), FPL("aa") },
125 { FPL("aa\\bb\\\\"), FPL("aa") },
126 { FPL("aa\\\\bb\\\\"), FPL("aa") },
127 { FPL("aa\\\\bb\\"), FPL("aa") },
128 { FPL("aa\\\\bb"), FPL("aa") },
129 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
130 { FPL("\\\\aa\\"), FPL("\\\\") },
131 { FPL("\\\\aa"), FPL("\\\\") },
132 { FPL("aa\\..\\bb\\c"), FPL("aa\\..\\bb")},
133 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
134 { FPL("c:\\"), FPL("c:\\") },
135 { FPL("c:\\\\"), FPL("c:\\\\") },
136 { FPL("c:\\\\\\"), FPL("c:\\") },
137 { FPL("c:\\aa"), FPL("c:\\") },
138 { FPL("c:\\aa\\"), FPL("c:\\") },
139 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
140 { FPL("c:aa\\bb"), FPL("c:aa") },
141 #endif // FILE_PATH_USES_DRIVE_LETTERS
142 #endif // FILE_PATH_USES_WIN_SEPARATORS
143 };
144
145 for (size_t i = 0; i < arraysize(cases); ++i) {
146 FilePath input(cases[i].input);
147 FilePath observed = input.DirName();
148 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
149 "i: " << i << ", input: " << input.value();
150 }
151 }
152
TEST_F(FilePathTest,BaseName)153 TEST_F(FilePathTest, BaseName) {
154 const struct UnaryTestData cases[] = {
155 { FPL(""), FPL("") },
156 { FPL("aa"), FPL("aa") },
157 { FPL("/aa/bb"), FPL("bb") },
158 { FPL("/aa/bb/"), FPL("bb") },
159 { FPL("/aa/bb//"), FPL("bb") },
160 { FPL("/aa/bb/ccc"), FPL("ccc") },
161 { FPL("/aa"), FPL("aa") },
162 { FPL("/"), FPL("/") },
163 { FPL("//"), FPL("//") },
164 { FPL("///"), FPL("/") },
165 { FPL("aa/"), FPL("aa") },
166 { FPL("aa/bb"), FPL("bb") },
167 { FPL("aa/bb/"), FPL("bb") },
168 { FPL("aa/bb//"), FPL("bb") },
169 { FPL("aa//bb//"), FPL("bb") },
170 { FPL("aa//bb/"), FPL("bb") },
171 { FPL("aa//bb"), FPL("bb") },
172 { FPL("//aa/bb"), FPL("bb") },
173 { FPL("//aa/"), FPL("aa") },
174 { FPL("//aa"), FPL("aa") },
175 { FPL("0:"), FPL("0:") },
176 { FPL("@:"), FPL("@:") },
177 { FPL("[:"), FPL("[:") },
178 { FPL("`:"), FPL("`:") },
179 { FPL("{:"), FPL("{:") },
180 { FPL("\xB3:"), FPL("\xB3:") },
181 { FPL("\xC5:"), FPL("\xC5:") },
182 #if defined(OS_WIN)
183 { FPL("\x0143:"), FPL("\x0143:") },
184 #endif // OS_WIN
185 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
186 { FPL("c:"), FPL("") },
187 { FPL("C:"), FPL("") },
188 { FPL("A:"), FPL("") },
189 { FPL("Z:"), FPL("") },
190 { FPL("a:"), FPL("") },
191 { FPL("z:"), FPL("") },
192 { FPL("c:aa"), FPL("aa") },
193 { FPL("c:/"), FPL("/") },
194 { FPL("c://"), FPL("//") },
195 { FPL("c:///"), FPL("/") },
196 { FPL("c:/aa"), FPL("aa") },
197 { FPL("c:/aa/"), FPL("aa") },
198 { FPL("c:/aa/bb"), FPL("bb") },
199 { FPL("c:aa/bb"), FPL("bb") },
200 #endif // FILE_PATH_USES_DRIVE_LETTERS
201 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
202 { FPL("\\aa\\bb"), FPL("bb") },
203 { FPL("\\aa\\bb\\"), FPL("bb") },
204 { FPL("\\aa\\bb\\\\"), FPL("bb") },
205 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
206 { FPL("\\aa"), FPL("aa") },
207 { FPL("\\"), FPL("\\") },
208 { FPL("\\\\"), FPL("\\\\") },
209 { FPL("\\\\\\"), FPL("\\") },
210 { FPL("aa\\"), FPL("aa") },
211 { FPL("aa\\bb"), FPL("bb") },
212 { FPL("aa\\bb\\"), FPL("bb") },
213 { FPL("aa\\bb\\\\"), FPL("bb") },
214 { FPL("aa\\\\bb\\\\"), FPL("bb") },
215 { FPL("aa\\\\bb\\"), FPL("bb") },
216 { FPL("aa\\\\bb"), FPL("bb") },
217 { FPL("\\\\aa\\bb"), FPL("bb") },
218 { FPL("\\\\aa\\"), FPL("aa") },
219 { FPL("\\\\aa"), FPL("aa") },
220 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
221 { FPL("c:\\"), FPL("\\") },
222 { FPL("c:\\\\"), FPL("\\\\") },
223 { FPL("c:\\\\\\"), FPL("\\") },
224 { FPL("c:\\aa"), FPL("aa") },
225 { FPL("c:\\aa\\"), FPL("aa") },
226 { FPL("c:\\aa\\bb"), FPL("bb") },
227 { FPL("c:aa\\bb"), FPL("bb") },
228 #endif // FILE_PATH_USES_DRIVE_LETTERS
229 #endif // FILE_PATH_USES_WIN_SEPARATORS
230 };
231
232 for (size_t i = 0; i < arraysize(cases); ++i) {
233 FilePath input(cases[i].input);
234 FilePath observed = input.BaseName();
235 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
236 "i: " << i << ", input: " << input.value();
237 }
238 }
239
TEST_F(FilePathTest,Append)240 TEST_F(FilePathTest, Append) {
241 const struct BinaryTestData cases[] = {
242 { { FPL(""), FPL("cc") }, FPL("cc") },
243 { { FPL("."), FPL("ff") }, FPL("ff") },
244 { { FPL("."), FPL("") }, FPL(".") },
245 { { FPL("/"), FPL("cc") }, FPL("/cc") },
246 { { FPL("/aa"), FPL("") }, FPL("/aa") },
247 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
248 { { FPL("//aa"), FPL("") }, FPL("//aa") },
249 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
250 { { FPL("//"), FPL("aa") }, FPL("//aa") },
251 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
252 { { FPL("c:"), FPL("a") }, FPL("c:a") },
253 { { FPL("c:"), FPL("") }, FPL("c:") },
254 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
255 { { FPL("c://"), FPL("a") }, FPL("c://a") },
256 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
257 #endif // FILE_PATH_USES_DRIVE_LETTERS
258 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
259 // Append introduces the default separator character, so these test cases
260 // need to be defined with different expected results on platforms that use
261 // different default separator characters.
262 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
263 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
264 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
265 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
266 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
267 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
268 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
269 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
270 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
271 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
272 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
273 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
274 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
275 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
276 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
277 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
278 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
279 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
280 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
281 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
282 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
283 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
284 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
285 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
286 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
287 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
288 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
289 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
290 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
291 #endif // FILE_PATH_USES_DRIVE_LETTERS
292 #else // FILE_PATH_USES_WIN_SEPARATORS
293 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
294 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
295 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
296 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
297 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
298 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
299 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
300 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
301 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
302 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
303 { { FPL("c:/"), FPL("") }, FPL("c:/") },
304 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
305 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
306 #endif // FILE_PATH_USES_DRIVE_LETTERS
307 #endif // FILE_PATH_USES_WIN_SEPARATORS
308 };
309
310 for (size_t i = 0; i < arraysize(cases); ++i) {
311 FilePath root(cases[i].inputs[0]);
312 FilePath::StringType leaf(cases[i].inputs[1]);
313 FilePath observed_str = root.Append(leaf);
314 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
315 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
316 FilePath observed_path = root.Append(FilePath(leaf));
317 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
318 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
319
320 // TODO(erikkay): It would be nice to have a unicode test append value to
321 // handle the case when AppendASCII is passed UTF8
322 #if defined(OS_WIN)
323 std::string ascii = WideToUTF8(leaf);
324 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
325 std::string ascii = leaf;
326 #endif
327 observed_str = root.AppendASCII(ascii);
328 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
329 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
330 }
331 }
332
TEST_F(FilePathTest,StripTrailingSeparators)333 TEST_F(FilePathTest, StripTrailingSeparators) {
334 const struct UnaryTestData cases[] = {
335 { FPL(""), FPL("") },
336 { FPL("/"), FPL("/") },
337 { FPL("//"), FPL("//") },
338 { FPL("///"), FPL("/") },
339 { FPL("////"), FPL("/") },
340 { FPL("a/"), FPL("a") },
341 { FPL("a//"), FPL("a") },
342 { FPL("a///"), FPL("a") },
343 { FPL("a////"), FPL("a") },
344 { FPL("/a"), FPL("/a") },
345 { FPL("/a/"), FPL("/a") },
346 { FPL("/a//"), FPL("/a") },
347 { FPL("/a///"), FPL("/a") },
348 { FPL("/a////"), FPL("/a") },
349 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
350 { FPL("c:"), FPL("c:") },
351 { FPL("c:/"), FPL("c:/") },
352 { FPL("c://"), FPL("c://") },
353 { FPL("c:///"), FPL("c:/") },
354 { FPL("c:////"), FPL("c:/") },
355 { FPL("c:/a"), FPL("c:/a") },
356 { FPL("c:/a/"), FPL("c:/a") },
357 { FPL("c:/a//"), FPL("c:/a") },
358 { FPL("c:/a///"), FPL("c:/a") },
359 { FPL("c:/a////"), FPL("c:/a") },
360 #endif // FILE_PATH_USES_DRIVE_LETTERS
361 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
362 { FPL("\\"), FPL("\\") },
363 { FPL("\\\\"), FPL("\\\\") },
364 { FPL("\\\\\\"), FPL("\\") },
365 { FPL("\\\\\\\\"), FPL("\\") },
366 { FPL("a\\"), FPL("a") },
367 { FPL("a\\\\"), FPL("a") },
368 { FPL("a\\\\\\"), FPL("a") },
369 { FPL("a\\\\\\\\"), FPL("a") },
370 { FPL("\\a"), FPL("\\a") },
371 { FPL("\\a\\"), FPL("\\a") },
372 { FPL("\\a\\\\"), FPL("\\a") },
373 { FPL("\\a\\\\\\"), FPL("\\a") },
374 { FPL("\\a\\\\\\\\"), FPL("\\a") },
375 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
376 { FPL("c:\\"), FPL("c:\\") },
377 { FPL("c:\\\\"), FPL("c:\\\\") },
378 { FPL("c:\\\\\\"), FPL("c:\\") },
379 { FPL("c:\\\\\\\\"), FPL("c:\\") },
380 { FPL("c:\\a"), FPL("c:\\a") },
381 { FPL("c:\\a\\"), FPL("c:\\a") },
382 { FPL("c:\\a\\\\"), FPL("c:\\a") },
383 { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
384 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
385 #endif // FILE_PATH_USES_DRIVE_LETTERS
386 #endif // FILE_PATH_USES_WIN_SEPARATORS
387 };
388
389 for (size_t i = 0; i < arraysize(cases); ++i) {
390 FilePath input(cases[i].input);
391 FilePath observed = input.StripTrailingSeparators();
392 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
393 "i: " << i << ", input: " << input.value();
394 }
395 }
396
TEST_F(FilePathTest,IsAbsolute)397 TEST_F(FilePathTest, IsAbsolute) {
398 const struct UnaryBooleanTestData cases[] = {
399 { FPL(""), false },
400 { FPL("a"), false },
401 { FPL("c:"), false },
402 { FPL("c:a"), false },
403 { FPL("a/b"), false },
404 { FPL("//"), true },
405 { FPL("//a"), true },
406 { FPL("c:a/b"), false },
407 { FPL("?:/a"), false },
408 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
409 { FPL("/"), false },
410 { FPL("/a"), false },
411 { FPL("/."), false },
412 { FPL("/.."), false },
413 { FPL("c:/"), true },
414 { FPL("c:/a"), true },
415 { FPL("c:/."), true },
416 { FPL("c:/.."), true },
417 { FPL("C:/a"), true },
418 { FPL("d:/a"), true },
419 #else // FILE_PATH_USES_DRIVE_LETTERS
420 { FPL("/"), true },
421 { FPL("/a"), true },
422 { FPL("/."), true },
423 { FPL("/.."), true },
424 { FPL("c:/"), false },
425 #endif // FILE_PATH_USES_DRIVE_LETTERS
426 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
427 { FPL("a\\b"), false },
428 { FPL("\\\\"), true },
429 { FPL("\\\\a"), true },
430 { FPL("a\\b"), false },
431 { FPL("\\\\"), true },
432 { FPL("//a"), true },
433 { FPL("c:a\\b"), false },
434 { FPL("?:\\a"), false },
435 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
436 { FPL("\\"), false },
437 { FPL("\\a"), false },
438 { FPL("\\."), false },
439 { FPL("\\.."), false },
440 { FPL("c:\\"), true },
441 { FPL("c:\\"), true },
442 { FPL("c:\\a"), true },
443 { FPL("c:\\."), true },
444 { FPL("c:\\.."), true },
445 { FPL("C:\\a"), true },
446 { FPL("d:\\a"), true },
447 #else // FILE_PATH_USES_DRIVE_LETTERS
448 { FPL("\\"), true },
449 { FPL("\\a"), true },
450 { FPL("\\."), true },
451 { FPL("\\.."), true },
452 { FPL("c:\\"), false },
453 #endif // FILE_PATH_USES_DRIVE_LETTERS
454 #endif // FILE_PATH_USES_WIN_SEPARATORS
455 };
456
457 for (size_t i = 0; i < arraysize(cases); ++i) {
458 FilePath input(cases[i].input);
459 bool observed = input.IsAbsolute();
460 EXPECT_EQ(cases[i].expected, observed) <<
461 "i: " << i << ", input: " << input.value();
462 }
463 }
464
TEST_F(FilePathTest,PathComponentsTest)465 TEST_F(FilePathTest, PathComponentsTest) {
466 const struct UnaryTestData cases[] = {
467 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
468 { FPL("///"), FPL("|/")},
469 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
470 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
471 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
472 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
473 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
474 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
475 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
476 { FPL("/"), FPL("|/")},
477 { FPL("foo"), FPL("|foo")},
478 { FPL(""), FPL("")},
479 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
480 { FPL("e:/foo"), FPL("|e:|/|foo")},
481 { FPL("e:/"), FPL("|e:|/")},
482 { FPL("e:"), FPL("|e:")},
483 #endif // FILE_PATH_USES_DRIVE_LETTERS
484 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
485 { FPL("../foo"), FPL("|..|foo")},
486 { FPL("./foo"), FPL("|foo")},
487 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
488 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
489 { FPL("\\\\\\"), FPL("|\\")},
490 { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
491 { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
492 { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
493 { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
494 { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
495 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
496 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
497 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
498 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
499 { FPL("\\"), FPL("|\\")},
500 #endif // FILE_PATH_USES_WIN_SEPARATORS
501 };
502
503 for (size_t i = 0; i < arraysize(cases); ++i) {
504 FilePath input(cases[i].input);
505 std::vector<FilePath::StringType> comps;
506 input.GetComponents(&comps);
507
508 FilePath::StringType observed;
509 for (size_t j = 0; j < comps.size(); ++j) {
510 observed.append(FILE_PATH_LITERAL("|"), 1);
511 observed.append(comps[j]);
512 }
513 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
514 "i: " << i << ", input: " << input.value();
515 }
516 }
517
TEST_F(FilePathTest,IsParentTest)518 TEST_F(FilePathTest, IsParentTest) {
519 const struct BinaryBooleanTestData cases[] = {
520 { { FPL("/"), FPL("/foo/bar/baz") }, true},
521 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
522 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
523 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
524 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
525 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
526 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
527 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
528 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
529 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
530 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
531 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
532 { { FPL(""), FPL("foo") }, false},
533 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
534 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
535 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
536 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
537 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
538 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
539 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
540 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
541 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
542 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
543 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
544 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
545 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
546 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
547 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
548 #endif // FILE_PATH_USES_DRIVE_LETTERS
549 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
550 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
551 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
552 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
553 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
554 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
555 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
556 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
557 #endif // FILE_PATH_USES_WIN_SEPARATORS
558 };
559
560 for (size_t i = 0; i < arraysize(cases); ++i) {
561 FilePath parent(cases[i].inputs[0]);
562 FilePath child(cases[i].inputs[1]);
563
564 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
565 "i: " << i << ", parent: " << parent.value() << ", child: " <<
566 child.value();
567 }
568 }
569
TEST_F(FilePathTest,AppendRelativePathTest)570 TEST_F(FilePathTest, AppendRelativePathTest) {
571 const struct BinaryTestData cases[] = {
572 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
573 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
574 #else // FILE_PATH_USES_WIN_SEPARATORS
575 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
576 #endif // FILE_PATH_USES_WIN_SEPARATORS
577 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
578 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
579 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
580 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
581 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
582 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
583 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
584 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
585 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
586 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
587 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
588 { { FPL(""), FPL("foo") }, FPL("")},
589 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
590 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
591 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
592 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
593 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
594 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
595 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
596 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
597 // TODO(akalin): Figure out how to handle the corner case in the
598 // commented-out test case below. Appending to an empty path gives
599 // /foo\bar\baz but appending to a nonempty path "blah" gives
600 // blah\foo\bar\baz.
601 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
602 #endif // FILE_PATH_USES_WIN_SEPARATORS
603 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
604 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
605 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
606 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
607 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
608 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
609 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
610 #endif // FILE_PATH_USES_DRIVE_LETTERS
611 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
612 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
613 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
614 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
615 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
616 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
617 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
618 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
619 #endif // FILE_PATH_USES_WIN_SEPARATORS
620 };
621
622 const FilePath base(FPL("blah"));
623
624 for (size_t i = 0; i < arraysize(cases); ++i) {
625 FilePath parent(cases[i].inputs[0]);
626 FilePath child(cases[i].inputs[1]);
627 {
628 FilePath result;
629 bool success = parent.AppendRelativePath(child, &result);
630 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
631 "i: " << i << ", parent: " << parent.value() << ", child: " <<
632 child.value();
633 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
634 "i: " << i << ", parent: " << parent.value() << ", child: " <<
635 child.value();
636 }
637 {
638 FilePath result(base);
639 bool success = parent.AppendRelativePath(child, &result);
640 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
641 "i: " << i << ", parent: " << parent.value() << ", child: " <<
642 child.value();
643 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
644 "i: " << i << ", parent: " << parent.value() << ", child: " <<
645 child.value();
646 }
647 }
648 }
649
TEST_F(FilePathTest,EqualityTest)650 TEST_F(FilePathTest, EqualityTest) {
651 const struct BinaryBooleanTestData cases[] = {
652 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
653 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
654 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
655 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
656 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
657 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
658 { { FPL("foo/bar"), FPL("foo/bar") }, true},
659 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
660 { { FPL(""), FPL("foo") }, false},
661 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
662 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
663 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
664 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
665 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
666 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
667 { { FPL("c:/"), FPL("c:/") }, true},
668 { { FPL("c:"), FPL("c:") }, true},
669 { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
670 { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
671 { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
672 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
673 #endif // FILE_PATH_USES_DRIVE_LETTERS
674 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
675 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
676 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
677 { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
678 { { FPL("\\"), FPL("\\") }, true},
679 { { FPL("\\"), FPL("/") }, false},
680 { { FPL(""), FPL("\\") }, false},
681 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
682 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
683 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
684 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
685 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
686 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
687 #endif // FILE_PATH_USES_DRIVE_LETTERS
688 #endif // FILE_PATH_USES_WIN_SEPARATORS
689 };
690
691 for (size_t i = 0; i < arraysize(cases); ++i) {
692 FilePath a(cases[i].inputs[0]);
693 FilePath b(cases[i].inputs[1]);
694
695 EXPECT_EQ(a == b, cases[i].expected) <<
696 "equality i: " << i << ", a: " << a.value() << ", b: " <<
697 b.value();
698 }
699
700 for (size_t i = 0; i < arraysize(cases); ++i) {
701 FilePath a(cases[i].inputs[0]);
702 FilePath b(cases[i].inputs[1]);
703
704 EXPECT_EQ(a != b, !cases[i].expected) <<
705 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
706 b.value();
707 }
708 }
709
TEST_F(FilePathTest,Extension)710 TEST_F(FilePathTest, Extension) {
711 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
712
713 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
714 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
715 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.FinalExtension());
716
717 FilePath base = jpg.BaseName().RemoveExtension();
718 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
719
720 FilePath path_no_ext = base_dir.Append(base);
721 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
722
723 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
724 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
725 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.FinalExtension());
726 }
727
TEST_F(FilePathTest,Extension2)728 TEST_F(FilePathTest, Extension2) {
729 const struct UnaryTestData cases[] = {
730 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
731 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
732 { FPL("C:\\a\\b\\c."), FPL(".") },
733 { FPL("C:\\a\\b\\c"), FPL("") },
734 { FPL("C:\\a\\b\\"), FPL("") },
735 { FPL("C:\\a\\b.\\"), FPL(".") },
736 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
737 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
738 { FPL("C:\\foo.bar\\.."), FPL("") },
739 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
740 #endif
741 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
742 { FPL("/foo/bar/baz."), FPL(".") },
743 { FPL("/foo/bar/baz.."), FPL(".") },
744 { FPL("/foo/bar/baz"), FPL("") },
745 { FPL("/foo/bar/"), FPL("") },
746 { FPL("/foo/bar./"), FPL(".") },
747 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
748 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
749 { FPL("/foo.12345.gz"), FPL(".gz") },
750 { FPL("/foo..gz"), FPL(".gz") },
751 { FPL("."), FPL("") },
752 { FPL(".."), FPL("") },
753 { FPL("./foo"), FPL("") },
754 { FPL("./foo.ext"), FPL(".ext") },
755 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
756 { FPL("/foo.bar////"), FPL(".bar") },
757 { FPL("/foo.bar/.."), FPL("") },
758 { FPL("/foo.bar/..////"), FPL("") },
759 { FPL("/foo.1234.luser.js"), FPL(".js") },
760 { FPL("/user.js"), FPL(".js") },
761 };
762 const struct UnaryTestData double_extension_cases[] = {
763 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
764 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
765 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
766 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
767 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
768 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
769 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
770 { FPL("/foo.1234.user.js"), FPL(".user.js") },
771 { FPL("foo.user.js"), FPL(".user.js") },
772 { FPL("/foo.tar.bz"), FPL(".tar.bz") },
773 };
774 for (unsigned int i = 0; i < arraysize(cases); ++i) {
775 FilePath path(cases[i].input);
776 FilePath::StringType extension = path.Extension();
777 FilePath::StringType final_extension = path.FinalExtension();
778 EXPECT_STREQ(cases[i].expected, extension.c_str())
779 << "i: " << i << ", path: " << path.value();
780 EXPECT_STREQ(cases[i].expected, final_extension.c_str())
781 << "i: " << i << ", path: " << path.value();
782 }
783 for (unsigned int i = 0; i < arraysize(double_extension_cases); ++i) {
784 FilePath path(double_extension_cases[i].input);
785 FilePath::StringType extension = path.Extension();
786 EXPECT_STREQ(double_extension_cases[i].expected, extension.c_str())
787 << "i: " << i << ", path: " << path.value();
788 }
789 }
790
TEST_F(FilePathTest,InsertBeforeExtension)791 TEST_F(FilePathTest, InsertBeforeExtension) {
792 const struct BinaryTestData cases[] = {
793 { { FPL(""), FPL("") }, FPL("") },
794 { { FPL(""), FPL("txt") }, FPL("") },
795 { { FPL("."), FPL("txt") }, FPL("") },
796 { { FPL(".."), FPL("txt") }, FPL("") },
797 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
798 { { FPL("."), FPL("") }, FPL(".") },
799 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
800 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
801 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
802 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
803 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
804 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
805 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
806 { { FPL("foo"), FPL("") }, FPL("foo") },
807 { { FPL("foo"), FPL(".") }, FPL("foo.") },
808 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
809 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
810 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
811 { { FPL("\\"), FPL("") }, FPL("\\") },
812 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
813 { { FPL("\\."), FPL("txt") }, FPL("") },
814 { { FPL("\\.."), FPL("txt") }, FPL("") },
815 { { FPL("\\."), FPL("") }, FPL("\\.") },
816 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
817 FPL("C:\\bar\\footxt.dll") },
818 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
819 FPL("C:\\bar.baz\\foodlltxt") },
820 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
821 FPL("C:\\bar.baz\\footxt.dll") },
822 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
823 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
824 { { FPL("C:\\bar.baz\\foo"), FPL("") },
825 FPL("C:\\bar.baz\\foo") },
826 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
827 FPL("C:\\bar.baz\\foo.exe") },
828 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
829 FPL("C:\\bar.baz\\foo.dll.exe") },
830 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
831 FPL("C:\\bar\\baz\\foo (1).exe") },
832 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
833 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
834 #endif
835 { { FPL("/"), FPL("") }, FPL("/") },
836 { { FPL("/"), FPL("txt") }, FPL("/txt") },
837 { { FPL("/."), FPL("txt") }, FPL("") },
838 { { FPL("/.."), FPL("txt") }, FPL("") },
839 { { FPL("/."), FPL("") }, FPL("/.") },
840 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
841 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
842 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
843 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
844 FPL("/bar.baz/foo.dlltxt.exe") },
845 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
846 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
847 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
848 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
849 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
850 };
851 for (unsigned int i = 0; i < arraysize(cases); ++i) {
852 FilePath path(cases[i].inputs[0]);
853 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
854 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
855 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
856 }
857 }
858
TEST_F(FilePathTest,RemoveExtension)859 TEST_F(FilePathTest, RemoveExtension) {
860 const struct UnaryTestData cases[] = {
861 { FPL(""), FPL("") },
862 { FPL("."), FPL(".") },
863 { FPL(".."), FPL("..") },
864 { FPL("foo.dll"), FPL("foo") },
865 { FPL("./foo.dll"), FPL("./foo") },
866 { FPL("foo..dll"), FPL("foo.") },
867 { FPL("foo"), FPL("foo") },
868 { FPL("foo."), FPL("foo") },
869 { FPL("foo.."), FPL("foo.") },
870 { FPL("foo.baz.dll"), FPL("foo.baz") },
871 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
872 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
873 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
874 #endif
875 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
876 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
877 };
878 for (unsigned int i = 0; i < arraysize(cases); ++i) {
879 FilePath path(cases[i].input);
880 FilePath removed = path.RemoveExtension();
881 FilePath removed_final = path.RemoveFinalExtension();
882 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
883 ", path: " << path.value();
884 EXPECT_EQ(cases[i].expected, removed_final.value()) << "i: " << i <<
885 ", path: " << path.value();
886 }
887 {
888 FilePath path(FPL("foo.tar.gz"));
889 FilePath removed = path.RemoveExtension();
890 FilePath removed_final = path.RemoveFinalExtension();
891 EXPECT_EQ(FPL("foo"), removed.value()) << ", path: " << path.value();
892 EXPECT_EQ(FPL("foo.tar"), removed_final.value()) << ", path: "
893 << path.value();
894 }
895 }
896
TEST_F(FilePathTest,ReplaceExtension)897 TEST_F(FilePathTest, ReplaceExtension) {
898 const struct BinaryTestData cases[] = {
899 { { FPL(""), FPL("") }, FPL("") },
900 { { FPL(""), FPL("txt") }, FPL("") },
901 { { FPL("."), FPL("txt") }, FPL("") },
902 { { FPL(".."), FPL("txt") }, FPL("") },
903 { { FPL("."), FPL("") }, FPL("") },
904 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
905 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
906 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
907 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
908 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
909 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
910 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
911 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
912 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
913 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
914 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
915 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
916 { { FPL("foo"), FPL("") }, FPL("foo") },
917 { { FPL("foo"), FPL(".") }, FPL("foo") },
918 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
919 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
920 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
921 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
922 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
923 #endif
924 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
925 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
926 };
927 for (unsigned int i = 0; i < arraysize(cases); ++i) {
928 FilePath path(cases[i].inputs[0]);
929 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
930 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
931 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
932 }
933 }
934
TEST_F(FilePathTest,AddExtension)935 TEST_F(FilePathTest, AddExtension) {
936 const struct BinaryTestData cases[] = {
937 { { FPL(""), FPL("") }, FPL("") },
938 { { FPL(""), FPL("txt") }, FPL("") },
939 { { FPL("."), FPL("txt") }, FPL("") },
940 { { FPL(".."), FPL("txt") }, FPL("") },
941 { { FPL("."), FPL("") }, FPL("") },
942 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
943 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
944 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
945 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
946 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
947 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
948 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
949 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
950 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
951 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
952 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
953 { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
954 { { FPL("foo"), FPL("") }, FPL("foo") },
955 { { FPL("foo"), FPL(".") }, FPL("foo") },
956 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
957 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
958 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
959 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
960 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
961 #endif
962 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
963 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
964 };
965 for (unsigned int i = 0; i < arraysize(cases); ++i) {
966 FilePath path(cases[i].inputs[0]);
967 FilePath added = path.AddExtension(cases[i].inputs[1]);
968 EXPECT_EQ(cases[i].expected, added.value()) << "i: " << i <<
969 ", path: " << path.value() << ", add: " << cases[i].inputs[1];
970 }
971 }
972
TEST_F(FilePathTest,MatchesExtension)973 TEST_F(FilePathTest, MatchesExtension) {
974 const struct BinaryBooleanTestData cases[] = {
975 { { FPL("foo"), FPL("") }, true},
976 { { FPL("foo"), FPL(".") }, false},
977 { { FPL("foo."), FPL("") }, false},
978 { { FPL("foo."), FPL(".") }, true},
979 { { FPL("foo.txt"), FPL(".dll") }, false},
980 { { FPL("foo.txt"), FPL(".txt") }, true},
981 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
982 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
983 { { FPL("foo.TXT"), FPL(".txt") }, true},
984 { { FPL("foo.txt"), FPL(".TXT") }, true},
985 { { FPL("foo.tXt"), FPL(".txt") }, true},
986 { { FPL("foo.txt"), FPL(".tXt") }, true},
987 { { FPL("foo.tXt"), FPL(".TXT") }, true},
988 { { FPL("foo.tXt"), FPL(".tXt") }, true},
989 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
990 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
991 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
992 #endif // FILE_PATH_USES_DRIVE_LETTERS
993 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
994 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
995 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
996 #endif // FILE_PATH_USES_DRIVE_LETTERS
997 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
998 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
999 #if defined(OS_WIN) || defined(OS_MACOSX)
1000 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1001 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
1002 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
1003 // C with circumflex: direct comparison, and upper case vs. lower case
1004 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
1005 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
1006 #endif
1007 };
1008
1009 for (size_t i = 0; i < arraysize(cases); ++i) {
1010 FilePath path(cases[i].inputs[0]);
1011 FilePath::StringType ext(cases[i].inputs[1]);
1012
1013 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
1014 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
1015 }
1016 }
1017
TEST_F(FilePathTest,CompareIgnoreCase)1018 TEST_F(FilePathTest, CompareIgnoreCase) {
1019 const struct BinaryIntTestData cases[] = {
1020 { { FPL("foo"), FPL("foo") }, 0},
1021 { { FPL("FOO"), FPL("foo") }, 0},
1022 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
1023 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
1024 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
1025 { { FPL("foO"), FPL("foo") }, 0},
1026 { { FPL("foo"), FPL("foO") }, 0},
1027 { { FPL("fOo"), FPL("foo") }, 0},
1028 { { FPL("foo"), FPL("fOo") }, 0},
1029 { { FPL("bar"), FPL("foo") }, -1},
1030 { { FPL("foo"), FPL("bar") }, 1},
1031 { { FPL("BAR"), FPL("foo") }, -1},
1032 { { FPL("FOO"), FPL("bar") }, 1},
1033 { { FPL("bar"), FPL("FOO") }, -1},
1034 { { FPL("foo"), FPL("BAR") }, 1},
1035 { { FPL("BAR"), FPL("FOO") }, -1},
1036 { { FPL("FOO"), FPL("BAR") }, 1},
1037 // German "Eszett" (lower case and the new-fangled upper case)
1038 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
1039 // However, neither Windows nor Mac OSX converts these.
1040 // (or even have glyphs for <uppercase eszett>)
1041 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
1042 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
1043 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
1044 { { FPL("SS"), FPL("\u00DF") }, -1},
1045 { { FPL("SS"), FPL("\u1E9E") }, -1},
1046 #if defined(OS_WIN) || defined(OS_MACOSX)
1047 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1048 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1049 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1050 // C with circumflex: direct comparison, and upper case vs. lower case
1051 { { FPL("\u0109"), FPL("\u0109") }, 0},
1052 { { FPL("\u0108"), FPL("\u0109") }, 0},
1053 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
1054 { { FPL("\u0428"), FPL("\u0428") }, 0},
1055 { { FPL("\u0428"), FPL("\u0448") }, 0},
1056 // Greek letter DELTA: direct comparison, and upper case vs. lower case
1057 { { FPL("\u0394"), FPL("\u0394") }, 0},
1058 { { FPL("\u0394"), FPL("\u03B4") }, 0},
1059 // Japanese full-width A: direct comparison, and upper case vs. lower case
1060 // Note that full-width and standard characters are considered different.
1061 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
1062 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
1063 { { FPL("A"), FPL("\uFF21") }, -1},
1064 { { FPL("A"), FPL("\uFF41") }, -1},
1065 { { FPL("a"), FPL("\uFF21") }, -1},
1066 { { FPL("a"), FPL("\uFF41") }, -1},
1067 #endif
1068 #if defined(OS_MACOSX)
1069 // Codepoints > 0x1000
1070 // Georgian letter DON: direct comparison, and upper case vs. lower case
1071 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
1072 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
1073 // Combining characters vs. pre-composed characters, upper and lower case
1074 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1075 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1076 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
1077 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
1078 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
1079 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
1080 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1081 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1082 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1083 #endif
1084 };
1085
1086 for (size_t i = 0; i < arraysize(cases); ++i) {
1087 FilePath::StringType s1(cases[i].inputs[0]);
1088 FilePath::StringType s2(cases[i].inputs[1]);
1089 int result = FilePath::CompareIgnoreCase(s1, s2);
1090 EXPECT_EQ(cases[i].expected, result) <<
1091 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1092 }
1093 }
1094
TEST_F(FilePathTest,ReferencesParent)1095 TEST_F(FilePathTest, ReferencesParent) {
1096 const struct UnaryBooleanTestData cases[] = {
1097 { FPL("."), false },
1098 { FPL(".."), true },
1099 { FPL(".. "), true },
1100 { FPL(" .."), true },
1101 { FPL("..."), true },
1102 { FPL("a.."), false },
1103 { FPL("..a"), false },
1104 { FPL("../"), true },
1105 { FPL("/.."), true },
1106 { FPL("/../"), true },
1107 { FPL("/a../"), false },
1108 { FPL("/..a/"), false },
1109 { FPL("//.."), true },
1110 { FPL("..//"), true },
1111 { FPL("//..//"), true },
1112 { FPL("a//..//c"), true },
1113 { FPL("../b/c"), true },
1114 { FPL("/../b/c"), true },
1115 { FPL("a/b/.."), true },
1116 { FPL("a/b/../"), true },
1117 { FPL("a/../c"), true },
1118 { FPL("a/b/c"), false },
1119 };
1120
1121 for (size_t i = 0; i < arraysize(cases); ++i) {
1122 FilePath input(cases[i].input);
1123 bool observed = input.ReferencesParent();
1124 EXPECT_EQ(cases[i].expected, observed) <<
1125 "i: " << i << ", input: " << input.value();
1126 }
1127 }
1128
TEST_F(FilePathTest,FromUTF8Unsafe_And_AsUTF8Unsafe)1129 TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
1130 const struct UTF8TestData cases[] = {
1131 { FPL("foo.txt"), "foo.txt" },
1132 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1133 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1134 // Full-width "ABC".
1135 { FPL("\uFF21\uFF22\uFF23.txt"),
1136 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1137 };
1138
1139 #if !defined(SYSTEM_NATIVE_UTF8) && defined(OS_LINUX)
1140 ScopedLocale locale("en_US.UTF-8");
1141 #endif
1142
1143 for (size_t i = 0; i < arraysize(cases); ++i) {
1144 // Test FromUTF8Unsafe() works.
1145 FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
1146 EXPECT_EQ(cases[i].native, from_utf8.value())
1147 << "i: " << i << ", input: " << cases[i].native;
1148 // Test AsUTF8Unsafe() works.
1149 FilePath from_native = FilePath(cases[i].native);
1150 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1151 << "i: " << i << ", input: " << cases[i].native;
1152 // Test the two file paths are identical.
1153 EXPECT_EQ(from_utf8.value(), from_native.value());
1154 }
1155 }
1156
TEST_F(FilePathTest,ConstructWithNUL)1157 TEST_F(FilePathTest, ConstructWithNUL) {
1158 // Assert FPS() works.
1159 ASSERT_EQ(3U, FPS("a\0b").length());
1160
1161 // Test constructor strips '\0'
1162 FilePath path(FPS("a\0b"));
1163 EXPECT_EQ(1U, path.value().length());
1164 EXPECT_EQ(FPL("a"), path.value());
1165 }
1166
TEST_F(FilePathTest,AppendWithNUL)1167 TEST_F(FilePathTest, AppendWithNUL) {
1168 // Assert FPS() works.
1169 ASSERT_EQ(3U, FPS("b\0b").length());
1170
1171 // Test Append() strips '\0'
1172 FilePath path(FPL("a"));
1173 path = path.Append(FPS("b\0b"));
1174 EXPECT_EQ(3U, path.value().length());
1175 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1176 EXPECT_EQ(FPL("a\\b"), path.value());
1177 #else
1178 EXPECT_EQ(FPL("a/b"), path.value());
1179 #endif
1180 }
1181
TEST_F(FilePathTest,ReferencesParentWithNUL)1182 TEST_F(FilePathTest, ReferencesParentWithNUL) {
1183 // Assert FPS() works.
1184 ASSERT_EQ(3U, FPS("..\0").length());
1185
1186 // Test ReferencesParent() doesn't break with "..\0"
1187 FilePath path(FPS("..\0"));
1188 EXPECT_TRUE(path.ReferencesParent());
1189 }
1190
1191 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
TEST_F(FilePathTest,NormalizePathSeparators)1192 TEST_F(FilePathTest, NormalizePathSeparators) {
1193 const struct UnaryTestData cases[] = {
1194 { FPL("foo/bar"), FPL("foo\\bar") },
1195 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1196 { FPL("foo\\bar"), FPL("foo\\bar") },
1197 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1198 { FPL("foo"), FPL("foo") },
1199 // Trailing slashes don't automatically get stripped. That's what
1200 // StripTrailingSeparators() is for.
1201 { FPL("foo\\"), FPL("foo\\") },
1202 { FPL("foo/"), FPL("foo\\") },
1203 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1204 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1205 { FPL("foo/bar/"), FPL("foo\\bar\\") },
1206 { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1207 { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1208 { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1209 { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1210 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1211 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1212 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1213 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1214 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1215 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1216 // This method does not normalize the number of path separators.
1217 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1218 { FPL("foo//bar"), FPL("foo\\\\bar") },
1219 { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1220 { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1221 { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1222 { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1223 { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1224 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1225 };
1226 for (size_t i = 0; i < arraysize(cases); ++i) {
1227 FilePath input(cases[i].input);
1228 FilePath observed = input.NormalizePathSeparators();
1229 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1230 "i: " << i << ", input: " << input.value();
1231 }
1232 }
1233 #endif
1234
TEST_F(FilePathTest,EndsWithSeparator)1235 TEST_F(FilePathTest, EndsWithSeparator) {
1236 const UnaryBooleanTestData cases[] = {
1237 { FPL(""), false },
1238 { FPL("/"), true },
1239 { FPL("foo/"), true },
1240 { FPL("bar"), false },
1241 { FPL("/foo/bar"), false },
1242 };
1243 for (size_t i = 0; i < arraysize(cases); ++i) {
1244 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1245 EXPECT_EQ(cases[i].expected, input.EndsWithSeparator());
1246 }
1247 }
1248
TEST_F(FilePathTest,AsEndingWithSeparator)1249 TEST_F(FilePathTest, AsEndingWithSeparator) {
1250 const UnaryTestData cases[] = {
1251 { FPL(""), FPL("") },
1252 { FPL("/"), FPL("/") },
1253 { FPL("foo"), FPL("foo/") },
1254 { FPL("foo/"), FPL("foo/") }
1255 };
1256 for (size_t i = 0; i < arraysize(cases); ++i) {
1257 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1258 FilePath expected = FilePath(cases[i].expected).NormalizePathSeparators();
1259 EXPECT_EQ(expected.value(), input.AsEndingWithSeparator().value());
1260 }
1261 }
1262
1263 #if defined(OS_ANDROID)
TEST_F(FilePathTest,ContentUriTest)1264 TEST_F(FilePathTest, ContentUriTest) {
1265 const struct UnaryBooleanTestData cases[] = {
1266 { FPL("content://foo.bar"), true },
1267 { FPL("content://foo.bar/"), true },
1268 { FPL("content://foo/bar"), true },
1269 { FPL("CoNTenT://foo.bar"), true },
1270 { FPL("content://"), true },
1271 { FPL("content:///foo.bar"), true },
1272 { FPL("content://3foo/bar"), true },
1273 { FPL("content://_foo/bar"), true },
1274 { FPL(".. "), false },
1275 { FPL("foo.bar"), false },
1276 { FPL("content:foo.bar"), false },
1277 { FPL("content:/foo.ba"), false },
1278 { FPL("content:/dir/foo.bar"), false },
1279 { FPL("content: //foo.bar"), false },
1280 { FPL("content%2a%2f%2f"), false },
1281 };
1282
1283 for (size_t i = 0; i < arraysize(cases); ++i) {
1284 FilePath input(cases[i].input);
1285 bool observed = input.IsContentUri();
1286 EXPECT_EQ(cases[i].expected, observed) <<
1287 "i: " << i << ", input: " << input.value();
1288 }
1289 }
1290 #endif
1291
1292 // Test the operator<<(ostream, FilePath).
TEST_F(FilePathTest,PrintToOstream)1293 TEST_F(FilePathTest, PrintToOstream) {
1294 std::stringstream ss;
1295 FilePath fp(FPL("foo"));
1296 ss << fp;
1297 EXPECT_EQ("foo", ss.str());
1298 }
1299
1300 // Test GetHFSDecomposedForm should return empty result for invalid UTF-8
1301 // strings.
1302 #if defined(OS_MACOSX)
TEST_F(FilePathTest,GetHFSDecomposedFormWithInvalidInput)1303 TEST_F(FilePathTest, GetHFSDecomposedFormWithInvalidInput) {
1304 const FilePath::CharType* cases[] = {
1305 FPL("\xc3\x28"),
1306 FPL("\xe2\x82\x28"),
1307 FPL("\xe2\x28\xa1"),
1308 FPL("\xf0\x28\x8c\xbc"),
1309 FPL("\xf0\x28\x8c\x28"),
1310 };
1311 for (auto* invalid_input : cases) {
1312 FilePath::StringType observed = FilePath::GetHFSDecomposedForm(
1313 invalid_input);
1314 EXPECT_TRUE(observed.empty());
1315 }
1316 }
1317 #endif
1318
1319 } // namespace base
1320