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