1 // Copyright (c) 2013 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 fileapi::FileSystemURL;
25 TEST(ExternalMountPointsTest, AddMountPoint) {
26 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
27 fileapi::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) {
110 EXPECT_EQ(kTestCases[i].success,
111 mount_points->RegisterFileSystem(
113 fileapi::kFileSystemTypeNativeLocal,
114 fileapi::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<fileapi::ExternalMountPoints> mount_points(
136 fileapi::ExternalMountPoints::CreateRefCounted());
138 mount_points->RegisterFileSystem("c",
139 fileapi::kFileSystemTypeNativeLocal,
140 fileapi::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 fileapi::kFileSystemTypeNativeLocal,
145 fileapi::FileSystemMountOption(),
146 base::FilePath(DRIVE FPL("/a/b/c(1)")));
147 mount_points->RegisterFileSystem("x",
148 fileapi::kFileSystemTypeNativeLocal,
149 fileapi::FileSystemMountOption(),
150 base::FilePath(DRIVE FPL("/z/y/x")));
151 mount_points->RegisterFileSystem("o",
152 fileapi::kFileSystemTypeNativeLocal,
153 fileapi::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 fileapi::kFileSystemTypeNativeLocal,
158 fileapi::FileSystemMountOption(),
159 base::FilePath(DRIVE FPL("/root/foo")));
160 // A mount point with an empty path.
161 mount_points->RegisterFileSystem("empty_path",
162 fileapi::kFileSystemTypeNativeLocal,
163 fileapi::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<fileapi::ExternalMountPoints> mount_points(
238 fileapi::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 fileapi::kFileSystemTypeExternal));
247 // Shouldn't handle the rest.
248 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
249 fileapi::kFileSystemTypeIsolated));
250 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
251 fileapi::kFileSystemTypeTemporary));
252 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
253 fileapi::kFileSystemTypePersistent));
254 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
255 fileapi::kFileSystemTypeTest));
256 // Not even if it's external subtype.
257 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
258 fileapi::kFileSystemTypeNativeLocal));
259 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
260 fileapi::kFileSystemTypeRestrictedNativeLocal));
261 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
262 fileapi::kFileSystemTypeDrive));
263 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
264 fileapi::kFileSystemTypeSyncable));
267 TEST(ExternalMountPointsTest, CreateCrackedFileSystemURL) {
268 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
269 fileapi::ExternalMountPoints::CreateRefCounted());
271 const GURL kTestOrigin("http://chromium.org");
273 mount_points->RegisterFileSystem("c",
274 fileapi::kFileSystemTypeNativeLocal,
275 fileapi::FileSystemMountOption(),
276 base::FilePath(DRIVE FPL("/a/b/c")));
277 mount_points->RegisterFileSystem("c(1)",
278 fileapi::kFileSystemTypeDrive,
279 fileapi::FileSystemMountOption(),
280 base::FilePath(DRIVE FPL("/a/b/c(1)")));
281 mount_points->RegisterFileSystem("empty_path",
282 fileapi::kFileSystemTypeSyncable,
283 fileapi::FileSystemMountOption(),
285 mount_points->RegisterFileSystem("mount",
286 fileapi::kFileSystemTypeDrive,
287 fileapi::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, fileapi::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 fileapi::kFileSystemTypeNativeLocal,
303 base::FilePath(FPL("c")));
304 EXPECT_FALSE(native_local.is_valid());
307 const base::FilePath::CharType* const path;
309 fileapi::FileSystemType expect_type;
310 const base::FilePath::CharType* const expect_path;
311 const char* const expect_fs_id;
314 const TestCase kTestCases[] = {
316 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" },
318 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)/d/e"), "c(1)" },
320 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"), "c(1)" },
321 { FPL("empty_path/a"),
322 true, fileapi::kFileSystemTypeSyncable, FPL("a"), "empty_path" },
324 true, fileapi::kFileSystemTypeSyncable, FPL(""), "empty_path" },
326 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" },
328 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root"), "mount" },
330 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
332 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
334 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
337 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
339 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
340 { FPL("/empty_path"),
341 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
342 // PAth references parent.
344 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
345 { FPL("/empty_path/a/../b"),
346 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
347 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
349 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" },
350 { FPL("mount\\a\\b"),
351 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" },
355 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
356 FileSystemURL cracked = mount_points->CreateCrackedFileSystemURL(
358 fileapi::kFileSystemTypeExternal,
359 base::FilePath(kTestCases[i].path));
361 EXPECT_EQ(kTestCases[i].expect_valid, cracked.is_valid())
362 << "Test case index: " << i;
364 if (!kTestCases[i].expect_valid)
367 EXPECT_EQ(kTestOrigin, cracked.origin())
368 << "Test case index: " << i;
369 EXPECT_EQ(kTestCases[i].expect_type, cracked.type())
370 << "Test case index: " << i;
371 EXPECT_EQ(base::FilePath(
372 kTestCases[i].expect_path).NormalizePathSeparators(), cracked.path())
373 << "Test case index: " << i;
374 EXPECT_EQ(base::FilePath(kTestCases[i].path).NormalizePathSeparators(),
375 cracked.virtual_path())
376 << "Test case index: " << i;
377 EXPECT_EQ(kTestCases[i].expect_fs_id, cracked.filesystem_id())
378 << "Test case index: " << i;
379 EXPECT_EQ(fileapi::kFileSystemTypeExternal, cracked.mount_type())
380 << "Test case index: " << i;
384 TEST(ExternalMountPointsTest, CrackVirtualPath) {
385 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
386 fileapi::ExternalMountPoints::CreateRefCounted());
388 const GURL kTestOrigin("http://chromium.org");
390 mount_points->RegisterFileSystem("c",
391 fileapi::kFileSystemTypeNativeLocal,
392 fileapi::FileSystemMountOption(),
393 base::FilePath(DRIVE FPL("/a/b/c")));
394 mount_points->RegisterFileSystem("c(1)",
395 fileapi::kFileSystemTypeDrive,
396 fileapi::FileSystemMountOption(),
397 base::FilePath(DRIVE FPL("/a/b/c(1)")));
398 mount_points->RegisterFileSystem("empty_path",
399 fileapi::kFileSystemTypeSyncable,
400 fileapi::FileSystemMountOption(),
402 mount_points->RegisterFileSystem("mount",
403 fileapi::kFileSystemTypeDrive,
404 fileapi::FileSystemMountOption(),
405 base::FilePath(DRIVE FPL("/root")));
408 const base::FilePath::CharType* const path;
410 fileapi::FileSystemType expect_type;
411 const base::FilePath::CharType* const expect_path;
412 const char* const expect_name;
415 const TestCase kTestCases[] = {
417 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" },
419 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)/d/e"), "c(1)" },
421 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"), "c(1)" },
422 { FPL("empty_path/a"),
423 true, fileapi::kFileSystemTypeSyncable, FPL("a"), "empty_path" },
425 true, fileapi::kFileSystemTypeSyncable, FPL(""), "empty_path" },
427 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" },
429 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root"), "mount" },
431 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
433 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
435 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
438 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
440 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
441 { FPL("/empty_path"),
442 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
443 // PAth references parent.
445 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
446 { FPL("/empty_path/a/../b"),
447 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" },
448 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
450 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" },
451 { FPL("mount\\a\\b"),
452 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" },
456 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
457 std::string cracked_name;
458 fileapi::FileSystemType cracked_type;
459 base::FilePath cracked_path;
460 fileapi::FileSystemMountOption cracked_option;
461 EXPECT_EQ(kTestCases[i].expect_valid,
462 mount_points->CrackVirtualPath(base::FilePath(kTestCases[i].path),
463 &cracked_name, &cracked_type, &cracked_path, &cracked_option))
464 << "Test case index: " << i;
466 if (!kTestCases[i].expect_valid)
469 EXPECT_EQ(kTestCases[i].expect_type, cracked_type)
470 << "Test case index: " << i;
471 EXPECT_EQ(base::FilePath(
472 kTestCases[i].expect_path).NormalizePathSeparators(), cracked_path)
473 << "Test case index: " << i;
474 EXPECT_EQ(kTestCases[i].expect_name, cracked_name)
475 << "Test case index: " << i;
479 TEST(ExternalMountPointsTest, MountOption) {
480 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
481 fileapi::ExternalMountPoints::CreateRefCounted());
483 mount_points->RegisterFileSystem(
485 fileapi::kFileSystemTypeNativeLocal,
486 fileapi::FileSystemMountOption(fileapi::COPY_SYNC_OPTION_NO_SYNC),
487 base::FilePath(DRIVE FPL("/nosync")));
488 mount_points->RegisterFileSystem(
490 fileapi::kFileSystemTypeNativeLocal,
491 fileapi::FileSystemMountOption(fileapi::COPY_SYNC_OPTION_SYNC),
492 base::FilePath(DRIVE FPL("/sync")));
495 fileapi::FileSystemType type;
496 fileapi::FileSystemMountOption option;
498 EXPECT_TRUE(mount_points->CrackVirtualPath(
499 base::FilePath(FPL("nosync/file")), &name, &type, &path, &option));
500 EXPECT_EQ(fileapi::COPY_SYNC_OPTION_NO_SYNC, option.copy_sync_option());
501 EXPECT_TRUE(mount_points->CrackVirtualPath(
502 base::FilePath(FPL("sync/file")), &name, &type, &path, &option));
503 EXPECT_EQ(fileapi::COPY_SYNC_OPTION_SYNC, option.copy_sync_option());