1 // Copyright 2014 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.
5 #include "webkit/browser/fileapi/external_mount_points.h"
9 #include "base/files/file_path.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "webkit/browser/fileapi/file_system_url.h"
13 #define FPL FILE_PATH_LITERAL
15 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
16 #define DRIVE FPL("C:")
21 using storage::FileSystemURL
;
25 TEST(ExternalMountPointsTest
, AddMountPoint
) {
26 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
27 storage::ExternalMountPoints::CreateRefCounted());
30 // The mount point's name.
31 const char* const name
;
32 // The mount point's path.
33 const base::FilePath::CharType
* const path
;
34 // Whether the mount point registration should succeed.
36 // Path returned by GetRegisteredPath. NULL if the method is expected to
38 const base::FilePath::CharType
* const registered_path
;
41 const TestCase kTestCases
[] = {
43 { "test", DRIVE
FPL("/foo/test"), true, DRIVE
FPL("/foo/test") },
44 // Valid mount point with only one path component.
45 { "bbb", DRIVE
FPL("/bbb"), true, DRIVE
FPL("/bbb") },
46 // Existing mount point path is substring of the mount points path.
47 { "test11", DRIVE
FPL("/foo/test11"), true, DRIVE
FPL("/foo/test11") },
48 // Path substring of an existing path.
49 { "test1", DRIVE
FPL("/foo/test1"), true, DRIVE
FPL("/foo/test1") },
50 // Empty mount point name and path.
51 { "", DRIVE
FPL(""), false, NULL
},
52 // Empty mount point name.
53 { "", DRIVE
FPL("/ddd"), false, NULL
},
54 // Empty mount point path.
55 { "empty_path", FPL(""), true, FPL("") },
56 // Name different from path's base name.
57 { "not_base_name", DRIVE
FPL("/x/y/z"), true, DRIVE
FPL("/x/y/z") },
59 { "invalid", DRIVE
FPL("../foo/invalid"), false, NULL
},
61 { "relative", DRIVE
FPL("foo/relative"), false, NULL
},
62 // Existing mount point path.
63 { "path_exists", DRIVE
FPL("/foo/test"), false, NULL
},
64 // Mount point with the same name exists.
65 { "test", DRIVE
FPL("/foo/a/test_name_exists"), false,
66 DRIVE
FPL("/foo/test") },
67 // Child of an existing mount point.
68 { "a1", DRIVE
FPL("/foo/test/a"), false, NULL
},
69 // Parent of an existing mount point.
70 { "foo1", DRIVE
FPL("/foo"), false, NULL
},
72 { "g", DRIVE
FPL("/foo/a/b/c/d/e/f/g"), true,
73 DRIVE
FPL("/foo/a/b/c/d/e/f/g") },
74 // Sibling mount point (with similar name) exists.
75 { "ff", DRIVE
FPL("/foo/a/b/c/d/e/ff"), true,
76 DRIVE
FPL("/foo/a/b/c/d/e/ff") },
77 // Lexicographically last among existing mount points.
78 { "yyy", DRIVE
FPL("/zzz/yyy"), true, DRIVE
FPL("/zzz/yyy") },
79 // Parent of the lexicographically last mount point.
80 { "zzz1", DRIVE
FPL("/zzz"), false, NULL
},
81 // Child of the lexicographically last mount point.
82 { "xxx1", DRIVE
FPL("/zzz/yyy/xxx"), false, NULL
},
83 // Lexicographically first among existing mount points.
84 { "b", DRIVE
FPL("/a/b"), true, DRIVE
FPL("/a/b") },
85 // Parent of lexicographically first mount point.
86 { "a2", DRIVE
FPL("/a"), false, NULL
},
87 // Child of lexicographically last mount point.
88 { "c1", DRIVE
FPL("/a/b/c"), false, NULL
},
89 // Parent to all of the mount points.
90 { "root", DRIVE
FPL("/"), false, NULL
},
91 // Path contains .. component.
92 { "funky", DRIVE
FPL("/tt/fun/../funky"), false, NULL
},
93 // Windows separators.
94 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
95 { "win", DRIVE
FPL("\\try\\separators\\win"), true,
96 DRIVE
FPL("\\try\\separators\\win") },
97 { "win1", DRIVE
FPL("\\try/separators\\win1"), true,
98 DRIVE
FPL("\\try/separators\\win1") },
99 { "win2", DRIVE
FPL("\\try/separators\\win"), false, NULL
},
101 { "win", DRIVE
FPL("\\separators\\win"), false, NULL
},
102 { "win1", DRIVE
FPL("\\try/separators\\win1"), false, NULL
},
104 // Win separators, but relative path.
105 { "win2", DRIVE
FPL("try\\separators\\win2"), false, NULL
},
108 // Test adding mount points.
109 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
111 kTestCases
[i
].success
,
112 mount_points
->RegisterFileSystem(kTestCases
[i
].name
,
113 storage::kFileSystemTypeNativeLocal
,
114 storage::FileSystemMountOption(),
115 base::FilePath(kTestCases
[i
].path
)))
116 << "Adding mount point: " << kTestCases
[i
].name
<< " with path "
117 << kTestCases
[i
].path
;
120 // Test that final mount point presence state is as expected.
121 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
122 base::FilePath found_path
;
123 EXPECT_EQ(kTestCases
[i
].registered_path
!= NULL
,
124 mount_points
->GetRegisteredPath(kTestCases
[i
].name
, &found_path
))
125 << "Test case: " << i
;
127 if (kTestCases
[i
].registered_path
) {
128 base::FilePath
expected_path(kTestCases
[i
].registered_path
);
129 EXPECT_EQ(expected_path
.NormalizePathSeparators(), found_path
);
134 TEST(ExternalMountPointsTest
, GetVirtualPath
) {
135 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
136 storage::ExternalMountPoints::CreateRefCounted());
138 mount_points
->RegisterFileSystem("c",
139 storage::kFileSystemTypeNativeLocal
,
140 storage::FileSystemMountOption(),
141 base::FilePath(DRIVE
FPL("/a/b/c")));
142 // Note that "/a/b/c" < "/a/b/c(1)" < "/a/b/c/".
143 mount_points
->RegisterFileSystem("c(1)",
144 storage::kFileSystemTypeNativeLocal
,
145 storage::FileSystemMountOption(),
146 base::FilePath(DRIVE
FPL("/a/b/c(1)")));
147 mount_points
->RegisterFileSystem("x",
148 storage::kFileSystemTypeNativeLocal
,
149 storage::FileSystemMountOption(),
150 base::FilePath(DRIVE
FPL("/z/y/x")));
151 mount_points
->RegisterFileSystem("o",
152 storage::kFileSystemTypeNativeLocal
,
153 storage::FileSystemMountOption(),
154 base::FilePath(DRIVE
FPL("/m/n/o")));
155 // A mount point whose name does not match its path base name.
156 mount_points
->RegisterFileSystem("mount",
157 storage::kFileSystemTypeNativeLocal
,
158 storage::FileSystemMountOption(),
159 base::FilePath(DRIVE
FPL("/root/foo")));
160 // A mount point with an empty path.
161 mount_points
->RegisterFileSystem("empty_path",
162 storage::kFileSystemTypeNativeLocal
,
163 storage::FileSystemMountOption(),
167 const base::FilePath::CharType
* const local_path
;
169 const base::FilePath::CharType
* const virtual_path
;
172 const TestCase kTestCases
[] = {
174 { FPL(""), false, FPL("") },
175 // No registered mount point (but is parent to a mount point).
176 { DRIVE
FPL("/a/b"), false, FPL("") },
177 // No registered mount point (but is parent to a mount point).
178 { DRIVE
FPL("/z/y"), false, FPL("") },
179 // No registered mount point (but is parent to a mount point).
180 { DRIVE
FPL("/m/n"), false, FPL("") },
181 // No registered mount point.
182 { DRIVE
FPL("/foo/mount"), false, FPL("") },
183 // An existing mount point path is substring.
184 { DRIVE
FPL("/a/b/c1"), false, FPL("") },
186 { DRIVE
FPL("a/b/c"), false, FPL("") },
187 // Sibling to a root path.
188 { DRIVE
FPL("/a/b/d/e"), false, FPL("") },
189 // Sibling to a root path.
190 { DRIVE
FPL("/z/y/v/u"), false, FPL("") },
191 // Sibling to a root path.
192 { DRIVE
FPL("/m/n/p/q"), false, FPL("") },
193 // Mount point root path.
194 { DRIVE
FPL("/a/b/c"), true, FPL("c") },
195 // Mount point root path.
196 { DRIVE
FPL("/z/y/x"), true, FPL("x") },
197 // Mount point root path.
198 { DRIVE
FPL("/m/n/o"), true, FPL("o") },
199 // Mount point child path.
200 { DRIVE
FPL("/a/b/c/d/e"), true, FPL("c/d/e") },
201 // Mount point child path.
202 { DRIVE
FPL("/z/y/x/v/u"), true, FPL("x/v/u") },
203 // Mount point child path.
204 { DRIVE
FPL("/m/n/o/p/q"), true, FPL("o/p/q") },
205 // Name doesn't match mount point path base name.
206 { DRIVE
FPL("/root/foo/a/b/c"), true, FPL("mount/a/b/c") },
207 { DRIVE
FPL("/root/foo"), true, FPL("mount") },
208 // Mount point contains character whose ASCII code is smaller than file path
210 { DRIVE
FPL("/a/b/c(1)/d/e"), true, FPL("c(1)/d/e") },
211 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
212 // Path with win separators mixed in.
213 { DRIVE
FPL("/a\\b\\c/d"), true, FPL("c/d") },
217 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
218 // Initialize virtual path with a value.
219 base::FilePath
virtual_path(DRIVE
FPL("/mount"));
220 base::FilePath
local_path(kTestCases
[i
].local_path
);
221 EXPECT_EQ(kTestCases
[i
].success
,
222 mount_points
->GetVirtualPath(local_path
, &virtual_path
))
223 << "Resolving " << kTestCases
[i
].local_path
;
225 // There are no guarantees for |virtual_path| value if |GetVirtualPath|
227 if (!kTestCases
[i
].success
)
230 base::FilePath
expected_virtual_path(kTestCases
[i
].virtual_path
);
231 EXPECT_EQ(expected_virtual_path
.NormalizePathSeparators(), virtual_path
)
232 << "Resolving " << kTestCases
[i
].local_path
;
236 TEST(ExternalMountPointsTest
, HandlesFileSystemMountType
) {
237 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
238 storage::ExternalMountPoints::CreateRefCounted());
240 const GURL
test_origin("http://chromium.org");
241 const base::FilePath
test_path(FPL("/mount"));
243 // Should handle External File System.
244 EXPECT_TRUE(mount_points
->HandlesFileSystemMountType(
245 storage::kFileSystemTypeExternal
));
247 // Shouldn't handle the rest.
248 EXPECT_FALSE(mount_points
->HandlesFileSystemMountType(
249 storage::kFileSystemTypeIsolated
));
250 EXPECT_FALSE(mount_points
->HandlesFileSystemMountType(
251 storage::kFileSystemTypeTemporary
));
252 EXPECT_FALSE(mount_points
->HandlesFileSystemMountType(
253 storage::kFileSystemTypePersistent
));
255 mount_points
->HandlesFileSystemMountType(storage::kFileSystemTypeTest
));
256 // Not even if it's external subtype.
257 EXPECT_FALSE(mount_points
->HandlesFileSystemMountType(
258 storage::kFileSystemTypeNativeLocal
));
259 EXPECT_FALSE(mount_points
->HandlesFileSystemMountType(
260 storage::kFileSystemTypeRestrictedNativeLocal
));
262 mount_points
->HandlesFileSystemMountType(storage::kFileSystemTypeDrive
));
263 EXPECT_FALSE(mount_points
->HandlesFileSystemMountType(
264 storage::kFileSystemTypeSyncable
));
267 TEST(ExternalMountPointsTest
, CreateCrackedFileSystemURL
) {
268 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
269 storage::ExternalMountPoints::CreateRefCounted());
271 const GURL
kTestOrigin("http://chromium.org");
273 mount_points
->RegisterFileSystem("c",
274 storage::kFileSystemTypeNativeLocal
,
275 storage::FileSystemMountOption(),
276 base::FilePath(DRIVE
FPL("/a/b/c")));
277 mount_points
->RegisterFileSystem("c(1)",
278 storage::kFileSystemTypeDrive
,
279 storage::FileSystemMountOption(),
280 base::FilePath(DRIVE
FPL("/a/b/c(1)")));
281 mount_points
->RegisterFileSystem("empty_path",
282 storage::kFileSystemTypeSyncable
,
283 storage::FileSystemMountOption(),
285 mount_points
->RegisterFileSystem("mount",
286 storage::kFileSystemTypeDrive
,
287 storage::FileSystemMountOption(),
288 base::FilePath(DRIVE
FPL("/root")));
290 // Try cracking invalid GURL.
291 FileSystemURL invalid
= mount_points
->CrackURL(GURL("http://chromium.og"));
292 EXPECT_FALSE(invalid
.is_valid());
294 // Try cracking isolated path.
295 FileSystemURL isolated
= mount_points
->CreateCrackedFileSystemURL(
296 kTestOrigin
, storage::kFileSystemTypeIsolated
, base::FilePath(FPL("c")));
297 EXPECT_FALSE(isolated
.is_valid());
299 // Try native local which is not cracked.
300 FileSystemURL native_local
= mount_points
->CreateCrackedFileSystemURL(
302 storage::kFileSystemTypeNativeLocal
,
303 base::FilePath(FPL("c")));
304 EXPECT_FALSE(native_local
.is_valid());
307 const base::FilePath::CharType
* const path
;
309 storage::FileSystemType expect_type
;
310 const base::FilePath::CharType
* const expect_path
;
311 const char* const expect_fs_id
;
314 const TestCase kTestCases
[] = {
315 {FPL("c/d/e"), true, storage::kFileSystemTypeNativeLocal
,
316 DRIVE
FPL("/a/b/c/d/e"), "c"},
317 {FPL("c(1)/d/e"), true, storage::kFileSystemTypeDrive
,
318 DRIVE
FPL("/a/b/c(1)/d/e"), "c(1)"},
319 {FPL("c(1)"), true, storage::kFileSystemTypeDrive
, DRIVE
FPL("/a/b/c(1)"),
321 {FPL("empty_path/a"), true, storage::kFileSystemTypeSyncable
, FPL("a"),
323 {FPL("empty_path"), true, storage::kFileSystemTypeSyncable
, FPL(""),
325 {FPL("mount/a/b"), true, storage::kFileSystemTypeDrive
,
326 DRIVE
FPL("/root/a/b"), "mount"},
327 {FPL("mount"), true, storage::kFileSystemTypeDrive
, DRIVE
FPL("/root"),
329 {FPL("cc"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
330 {FPL(""), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
331 {FPL(".."), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
333 {FPL("/c/d/e"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
334 {FPL("/c(1)/d/e"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
335 {FPL("/empty_path"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
336 // PAth references parent.
337 {FPL("c/d/../e"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
338 {FPL("/empty_path/a/../b"), false, storage::kFileSystemTypeUnknown
, FPL(""),
340 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
341 {FPL("c/d\\e"), true, storage::kFileSystemTypeNativeLocal
,
342 DRIVE
FPL("/a/b/c/d/e"), "c"},
343 {FPL("mount\\a\\b"), true, storage::kFileSystemTypeDrive
,
344 DRIVE
FPL("/root/a/b"), "mount"},
348 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
349 FileSystemURL cracked
= mount_points
->CreateCrackedFileSystemURL(
351 storage::kFileSystemTypeExternal
,
352 base::FilePath(kTestCases
[i
].path
));
354 EXPECT_EQ(kTestCases
[i
].expect_valid
, cracked
.is_valid())
355 << "Test case index: " << i
;
357 if (!kTestCases
[i
].expect_valid
)
360 EXPECT_EQ(kTestOrigin
, cracked
.origin())
361 << "Test case index: " << i
;
362 EXPECT_EQ(kTestCases
[i
].expect_type
, cracked
.type())
363 << "Test case index: " << i
;
364 EXPECT_EQ(base::FilePath(
365 kTestCases
[i
].expect_path
).NormalizePathSeparators(), cracked
.path())
366 << "Test case index: " << i
;
367 EXPECT_EQ(base::FilePath(kTestCases
[i
].path
).NormalizePathSeparators(),
368 cracked
.virtual_path())
369 << "Test case index: " << i
;
370 EXPECT_EQ(kTestCases
[i
].expect_fs_id
, cracked
.filesystem_id())
371 << "Test case index: " << i
;
372 EXPECT_EQ(storage::kFileSystemTypeExternal
, cracked
.mount_type())
373 << "Test case index: " << i
;
377 TEST(ExternalMountPointsTest
, CrackVirtualPath
) {
378 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
379 storage::ExternalMountPoints::CreateRefCounted());
381 const GURL
kTestOrigin("http://chromium.org");
383 mount_points
->RegisterFileSystem("c",
384 storage::kFileSystemTypeNativeLocal
,
385 storage::FileSystemMountOption(),
386 base::FilePath(DRIVE
FPL("/a/b/c")));
387 mount_points
->RegisterFileSystem("c(1)",
388 storage::kFileSystemTypeDrive
,
389 storage::FileSystemMountOption(),
390 base::FilePath(DRIVE
FPL("/a/b/c(1)")));
391 mount_points
->RegisterFileSystem("empty_path",
392 storage::kFileSystemTypeSyncable
,
393 storage::FileSystemMountOption(),
395 mount_points
->RegisterFileSystem("mount",
396 storage::kFileSystemTypeDrive
,
397 storage::FileSystemMountOption(),
398 base::FilePath(DRIVE
FPL("/root")));
401 const base::FilePath::CharType
* const path
;
403 storage::FileSystemType expect_type
;
404 const base::FilePath::CharType
* const expect_path
;
405 const char* const expect_name
;
408 const TestCase kTestCases
[] = {
409 {FPL("c/d/e"), true, storage::kFileSystemTypeNativeLocal
,
410 DRIVE
FPL("/a/b/c/d/e"), "c"},
411 {FPL("c(1)/d/e"), true, storage::kFileSystemTypeDrive
,
412 DRIVE
FPL("/a/b/c(1)/d/e"), "c(1)"},
413 {FPL("c(1)"), true, storage::kFileSystemTypeDrive
, DRIVE
FPL("/a/b/c(1)"),
415 {FPL("empty_path/a"), true, storage::kFileSystemTypeSyncable
, FPL("a"),
417 {FPL("empty_path"), true, storage::kFileSystemTypeSyncable
, FPL(""),
419 {FPL("mount/a/b"), true, storage::kFileSystemTypeDrive
,
420 DRIVE
FPL("/root/a/b"), "mount"},
421 {FPL("mount"), true, storage::kFileSystemTypeDrive
, DRIVE
FPL("/root"),
423 {FPL("cc"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
424 {FPL(""), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
425 {FPL(".."), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
427 {FPL("/c/d/e"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
428 {FPL("/c(1)/d/e"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
429 {FPL("/empty_path"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
430 // PAth references parent.
431 {FPL("c/d/../e"), false, storage::kFileSystemTypeUnknown
, FPL(""), ""},
432 {FPL("/empty_path/a/../b"), false, storage::kFileSystemTypeUnknown
, FPL(""),
434 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
435 {FPL("c/d\\e"), true, storage::kFileSystemTypeNativeLocal
,
436 DRIVE
FPL("/a/b/c/d/e"), "c"},
437 {FPL("mount\\a\\b"), true, storage::kFileSystemTypeDrive
,
438 DRIVE
FPL("/root/a/b"), "mount"},
442 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
443 std::string cracked_name
;
444 storage::FileSystemType cracked_type
;
445 std::string cracked_id
;
446 base::FilePath cracked_path
;
447 storage::FileSystemMountOption cracked_option
;
448 EXPECT_EQ(kTestCases
[i
].expect_valid
,
449 mount_points
->CrackVirtualPath(base::FilePath(kTestCases
[i
].path
),
450 &cracked_name
, &cracked_type
, &cracked_id
, &cracked_path
,
452 << "Test case index: " << i
;
454 if (!kTestCases
[i
].expect_valid
)
457 EXPECT_EQ(kTestCases
[i
].expect_type
, cracked_type
)
458 << "Test case index: " << i
;
459 EXPECT_EQ(base::FilePath(
460 kTestCases
[i
].expect_path
).NormalizePathSeparators(), cracked_path
)
461 << "Test case index: " << i
;
462 EXPECT_EQ(kTestCases
[i
].expect_name
, cracked_name
)
463 << "Test case index: " << i
;
464 // As of now we don't mount other filesystems with non-empty filesystem_id
465 // onto external mount points.
466 EXPECT_TRUE(cracked_id
.empty()) << "Test case index: " << i
;
470 TEST(ExternalMountPointsTest
, MountOption
) {
471 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
472 storage::ExternalMountPoints::CreateRefCounted());
474 mount_points
->RegisterFileSystem(
476 storage::kFileSystemTypeNativeLocal
,
477 storage::FileSystemMountOption(storage::COPY_SYNC_OPTION_NO_SYNC
),
478 base::FilePath(DRIVE
FPL("/nosync")));
479 mount_points
->RegisterFileSystem(
481 storage::kFileSystemTypeNativeLocal
,
482 storage::FileSystemMountOption(storage::COPY_SYNC_OPTION_SYNC
),
483 base::FilePath(DRIVE
FPL("/sync")));
486 storage::FileSystemType type
;
487 std::string cracked_id
;
488 storage::FileSystemMountOption option
;
490 EXPECT_TRUE(mount_points
->CrackVirtualPath(
491 base::FilePath(FPL("nosync/file")), &name
, &type
, &cracked_id
, &path
,
493 EXPECT_EQ(storage::COPY_SYNC_OPTION_NO_SYNC
, option
.copy_sync_option());
494 EXPECT_TRUE(mount_points
->CrackVirtualPath(
495 base::FilePath(FPL("sync/file")), &name
, &type
, &cracked_id
, &path
,
497 EXPECT_EQ(storage::COPY_SYNC_OPTION_SYNC
, option
.copy_sync_option());
500 } // namespace content