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