Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / fileapi / external_mount_points_unittest.cc
blob02103da2a9435bd7b183935bf0b92dc786bc7817
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 "storage/browser/fileapi/external_mount_points.h"
7 #include <string>
9 #include "base/files/file_path.h"
10 #include "storage/browser/fileapi/file_system_url.h"
11 #include "storage/common/fileapi/file_system_mount_option.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #define FPL FILE_PATH_LITERAL
16 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
17 #define DRIVE FPL("C:")
18 #else
19 #define DRIVE
20 #endif
22 using storage::FileSystemURL;
24 namespace content {
26 TEST(ExternalMountPointsTest, AddMountPoint) {
27 scoped_refptr<storage::ExternalMountPoints> mount_points(
28 storage::ExternalMountPoints::CreateRefCounted());
30 struct TestCase {
31 // The mount point's name.
32 const char* const name;
33 // The mount point's path.
34 const base::FilePath::CharType* const path;
35 // Whether the mount point registration should succeed.
36 bool success;
37 // Path returned by GetRegisteredPath. NULL if the method is expected to
38 // fail.
39 const base::FilePath::CharType* const registered_path;
42 const TestCase kTestCases[] = {
43 // Valid mount point.
44 { "test", DRIVE FPL("/foo/test"), true, DRIVE FPL("/foo/test") },
45 // Valid mount point with only one path component.
46 { "bbb", DRIVE FPL("/bbb"), true, DRIVE FPL("/bbb") },
47 // Existing mount point path is substring of the mount points path.
48 { "test11", DRIVE FPL("/foo/test11"), true, DRIVE FPL("/foo/test11") },
49 // Path substring of an existing path.
50 { "test1", DRIVE FPL("/foo/test1"), true, DRIVE FPL("/foo/test1") },
51 // Empty mount point name and path.
52 { "", DRIVE FPL(""), false, NULL },
53 // Empty mount point name.
54 { "", DRIVE FPL("/ddd"), false, NULL },
55 // Empty mount point path.
56 { "empty_path", FPL(""), true, FPL("") },
57 // Name different from path's base name.
58 { "not_base_name", DRIVE FPL("/x/y/z"), true, DRIVE FPL("/x/y/z") },
59 // References parent.
60 { "invalid", DRIVE FPL("../foo/invalid"), false, NULL },
61 // Relative path.
62 { "relative", DRIVE FPL("foo/relative"), false, NULL },
63 // Existing mount point path.
64 { "path_exists", DRIVE FPL("/foo/test"), false, NULL },
65 // Mount point with the same name exists.
66 { "test", DRIVE FPL("/foo/a/test_name_exists"), false,
67 DRIVE FPL("/foo/test") },
68 // Child of an existing mount point.
69 { "a1", DRIVE FPL("/foo/test/a"), false, NULL },
70 // Parent of an existing mount point.
71 { "foo1", DRIVE FPL("/foo"), false, NULL },
72 // Bit bigger depth.
73 { "g", DRIVE FPL("/foo/a/b/c/d/e/f/g"), true,
74 DRIVE FPL("/foo/a/b/c/d/e/f/g") },
75 // Sibling mount point (with similar name) exists.
76 { "ff", DRIVE FPL("/foo/a/b/c/d/e/ff"), true,
77 DRIVE FPL("/foo/a/b/c/d/e/ff") },
78 // Lexicographically last among existing mount points.
79 { "yyy", DRIVE FPL("/zzz/yyy"), true, DRIVE FPL("/zzz/yyy") },
80 // Parent of the lexicographically last mount point.
81 { "zzz1", DRIVE FPL("/zzz"), false, NULL },
82 // Child of the lexicographically last mount point.
83 { "xxx1", DRIVE FPL("/zzz/yyy/xxx"), false, NULL },
84 // Lexicographically first among existing mount points.
85 { "b", DRIVE FPL("/a/b"), true, DRIVE FPL("/a/b") },
86 // Parent of lexicographically first mount point.
87 { "a2", DRIVE FPL("/a"), false, NULL },
88 // Child of lexicographically last mount point.
89 { "c1", DRIVE FPL("/a/b/c"), false, NULL },
90 // Parent to all of the mount points.
91 { "root", DRIVE FPL("/"), false, NULL },
92 // Path contains .. component.
93 { "funky", DRIVE FPL("/tt/fun/../funky"), false, NULL },
94 // Windows separators.
95 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
96 { "win", DRIVE FPL("\\try\\separators\\win"), true,
97 DRIVE FPL("\\try\\separators\\win") },
98 { "win1", DRIVE FPL("\\try/separators\\win1"), true,
99 DRIVE FPL("\\try/separators\\win1") },
100 { "win2", DRIVE FPL("\\try/separators\\win"), false, NULL },
101 #else
102 { "win", DRIVE FPL("\\separators\\win"), false, NULL },
103 { "win1", DRIVE FPL("\\try/separators\\win1"), false, NULL },
104 #endif
105 // Win separators, but relative path.
106 { "win2", DRIVE FPL("try\\separators\\win2"), false, NULL },
109 // Test adding mount points.
110 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
111 EXPECT_EQ(
112 kTestCases[i].success,
113 mount_points->RegisterFileSystem(kTestCases[i].name,
114 storage::kFileSystemTypeNativeLocal,
115 storage::FileSystemMountOption(),
116 base::FilePath(kTestCases[i].path)))
117 << "Adding mount point: " << kTestCases[i].name << " with path "
118 << kTestCases[i].path;
121 // Test that final mount point presence state is as expected.
122 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
123 base::FilePath found_path;
124 EXPECT_EQ(kTestCases[i].registered_path != NULL,
125 mount_points->GetRegisteredPath(kTestCases[i].name, &found_path))
126 << "Test case: " << i;
128 if (kTestCases[i].registered_path) {
129 base::FilePath expected_path(kTestCases[i].registered_path);
130 EXPECT_EQ(expected_path.NormalizePathSeparators(), found_path);
135 TEST(ExternalMountPointsTest, GetVirtualPath) {
136 scoped_refptr<storage::ExternalMountPoints> mount_points(
137 storage::ExternalMountPoints::CreateRefCounted());
139 mount_points->RegisterFileSystem("c",
140 storage::kFileSystemTypeNativeLocal,
141 storage::FileSystemMountOption(),
142 base::FilePath(DRIVE FPL("/a/b/c")));
143 // Note that "/a/b/c" < "/a/b/c(1)" < "/a/b/c/".
144 mount_points->RegisterFileSystem("c(1)",
145 storage::kFileSystemTypeNativeLocal,
146 storage::FileSystemMountOption(),
147 base::FilePath(DRIVE FPL("/a/b/c(1)")));
148 mount_points->RegisterFileSystem("x",
149 storage::kFileSystemTypeNativeLocal,
150 storage::FileSystemMountOption(),
151 base::FilePath(DRIVE FPL("/z/y/x")));
152 mount_points->RegisterFileSystem("o",
153 storage::kFileSystemTypeNativeLocal,
154 storage::FileSystemMountOption(),
155 base::FilePath(DRIVE FPL("/m/n/o")));
156 // A mount point whose name does not match its path base name.
157 mount_points->RegisterFileSystem("mount",
158 storage::kFileSystemTypeNativeLocal,
159 storage::FileSystemMountOption(),
160 base::FilePath(DRIVE FPL("/root/foo")));
161 // A mount point with an empty path.
162 mount_points->RegisterFileSystem("empty_path",
163 storage::kFileSystemTypeNativeLocal,
164 storage::FileSystemMountOption(),
165 base::FilePath());
167 struct TestCase {
168 const base::FilePath::CharType* const local_path;
169 bool success;
170 const base::FilePath::CharType* const virtual_path;
173 const TestCase kTestCases[] = {
174 // Empty path.
175 { FPL(""), false, FPL("") },
176 // No registered mount point (but is parent to a mount point).
177 { DRIVE FPL("/a/b"), false, FPL("") },
178 // No registered mount point (but is parent to a mount point).
179 { DRIVE FPL("/z/y"), false, FPL("") },
180 // No registered mount point (but is parent to a mount point).
181 { DRIVE FPL("/m/n"), false, FPL("") },
182 // No registered mount point.
183 { DRIVE FPL("/foo/mount"), false, FPL("") },
184 // An existing mount point path is substring.
185 { DRIVE FPL("/a/b/c1"), false, FPL("") },
186 // No leading /.
187 { DRIVE FPL("a/b/c"), false, FPL("") },
188 // Sibling to a root path.
189 { DRIVE FPL("/a/b/d/e"), false, FPL("") },
190 // Sibling to a root path.
191 { DRIVE FPL("/z/y/v/u"), false, FPL("") },
192 // Sibling to a root path.
193 { DRIVE FPL("/m/n/p/q"), false, FPL("") },
194 // Mount point root path.
195 { DRIVE FPL("/a/b/c"), true, FPL("c") },
196 // Mount point root path.
197 { DRIVE FPL("/z/y/x"), true, FPL("x") },
198 // Mount point root path.
199 { DRIVE FPL("/m/n/o"), true, FPL("o") },
200 // Mount point child path.
201 { DRIVE FPL("/a/b/c/d/e"), true, FPL("c/d/e") },
202 // Mount point child path.
203 { DRIVE FPL("/z/y/x/v/u"), true, FPL("x/v/u") },
204 // Mount point child path.
205 { DRIVE FPL("/m/n/o/p/q"), true, FPL("o/p/q") },
206 // Name doesn't match mount point path base name.
207 { DRIVE FPL("/root/foo/a/b/c"), true, FPL("mount/a/b/c") },
208 { DRIVE FPL("/root/foo"), true, FPL("mount") },
209 // Mount point contains character whose ASCII code is smaller than file path
210 // separator's.
211 { DRIVE FPL("/a/b/c(1)/d/e"), true, FPL("c(1)/d/e") },
212 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
213 // Path with win separators mixed in.
214 { DRIVE FPL("/a\\b\\c/d"), true, FPL("c/d") },
215 #endif
218 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
219 // Initialize virtual path with a value.
220 base::FilePath virtual_path(DRIVE FPL("/mount"));
221 base::FilePath local_path(kTestCases[i].local_path);
222 EXPECT_EQ(kTestCases[i].success,
223 mount_points->GetVirtualPath(local_path, &virtual_path))
224 << "Resolving " << kTestCases[i].local_path;
226 // There are no guarantees for |virtual_path| value if |GetVirtualPath|
227 // fails.
228 if (!kTestCases[i].success)
229 continue;
231 base::FilePath expected_virtual_path(kTestCases[i].virtual_path);
232 EXPECT_EQ(expected_virtual_path.NormalizePathSeparators(), virtual_path)
233 << "Resolving " << kTestCases[i].local_path;
237 TEST(ExternalMountPointsTest, HandlesFileSystemMountType) {
238 scoped_refptr<storage::ExternalMountPoints> mount_points(
239 storage::ExternalMountPoints::CreateRefCounted());
241 const GURL test_origin("http://chromium.org");
242 const base::FilePath test_path(FPL("/mount"));
244 // Should handle External File System.
245 EXPECT_TRUE(mount_points->HandlesFileSystemMountType(
246 storage::kFileSystemTypeExternal));
248 // Shouldn't handle the rest.
249 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
250 storage::kFileSystemTypeIsolated));
251 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
252 storage::kFileSystemTypeTemporary));
253 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
254 storage::kFileSystemTypePersistent));
255 EXPECT_FALSE(
256 mount_points->HandlesFileSystemMountType(storage::kFileSystemTypeTest));
257 // Not even if it's external subtype.
258 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
259 storage::kFileSystemTypeNativeLocal));
260 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
261 storage::kFileSystemTypeRestrictedNativeLocal));
262 EXPECT_FALSE(
263 mount_points->HandlesFileSystemMountType(storage::kFileSystemTypeDrive));
264 EXPECT_FALSE(mount_points->HandlesFileSystemMountType(
265 storage::kFileSystemTypeSyncable));
268 TEST(ExternalMountPointsTest, CreateCrackedFileSystemURL) {
269 scoped_refptr<storage::ExternalMountPoints> mount_points(
270 storage::ExternalMountPoints::CreateRefCounted());
272 const GURL kTestOrigin("http://chromium.org");
274 mount_points->RegisterFileSystem("c",
275 storage::kFileSystemTypeNativeLocal,
276 storage::FileSystemMountOption(),
277 base::FilePath(DRIVE FPL("/a/b/c")));
278 mount_points->RegisterFileSystem("c(1)",
279 storage::kFileSystemTypeDrive,
280 storage::FileSystemMountOption(),
281 base::FilePath(DRIVE FPL("/a/b/c(1)")));
282 mount_points->RegisterFileSystem("empty_path",
283 storage::kFileSystemTypeSyncable,
284 storage::FileSystemMountOption(),
285 base::FilePath());
286 mount_points->RegisterFileSystem("mount",
287 storage::kFileSystemTypeDrive,
288 storage::FileSystemMountOption(),
289 base::FilePath(DRIVE FPL("/root")));
291 // Try cracking invalid GURL.
292 FileSystemURL invalid = mount_points->CrackURL(GURL("http://chromium.og"));
293 EXPECT_FALSE(invalid.is_valid());
295 // Try cracking isolated path.
296 FileSystemURL isolated = mount_points->CreateCrackedFileSystemURL(
297 kTestOrigin, storage::kFileSystemTypeIsolated, base::FilePath(FPL("c")));
298 EXPECT_FALSE(isolated.is_valid());
300 // Try native local which is not cracked.
301 FileSystemURL native_local = mount_points->CreateCrackedFileSystemURL(
302 kTestOrigin,
303 storage::kFileSystemTypeNativeLocal,
304 base::FilePath(FPL("c")));
305 EXPECT_FALSE(native_local.is_valid());
307 struct TestCase {
308 const base::FilePath::CharType* const path;
309 bool expect_valid;
310 storage::FileSystemType expect_type;
311 const base::FilePath::CharType* const expect_path;
312 const char* const expect_fs_id;
315 const TestCase kTestCases[] = {
316 {FPL("c/d/e"), true, storage::kFileSystemTypeNativeLocal,
317 DRIVE FPL("/a/b/c/d/e"), "c"},
318 {FPL("c(1)/d/e"), true, storage::kFileSystemTypeDrive,
319 DRIVE FPL("/a/b/c(1)/d/e"), "c(1)"},
320 {FPL("c(1)"), true, storage::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"),
321 "c(1)"},
322 {FPL("empty_path/a"), true, storage::kFileSystemTypeSyncable, FPL("a"),
323 "empty_path"},
324 {FPL("empty_path"), true, storage::kFileSystemTypeSyncable, FPL(""),
325 "empty_path"},
326 {FPL("mount/a/b"), true, storage::kFileSystemTypeDrive,
327 DRIVE FPL("/root/a/b"), "mount"},
328 {FPL("mount"), true, storage::kFileSystemTypeDrive, DRIVE FPL("/root"),
329 "mount"},
330 {FPL("cc"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
331 {FPL(""), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
332 {FPL(".."), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
333 // Absolte paths.
334 {FPL("/c/d/e"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
335 {FPL("/c(1)/d/e"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
336 {FPL("/empty_path"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
337 // PAth references parent.
338 {FPL("c/d/../e"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
339 {FPL("/empty_path/a/../b"), false, storage::kFileSystemTypeUnknown, FPL(""),
340 ""},
341 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
342 {FPL("c/d\\e"), true, storage::kFileSystemTypeNativeLocal,
343 DRIVE FPL("/a/b/c/d/e"), "c"},
344 {FPL("mount\\a\\b"), true, storage::kFileSystemTypeDrive,
345 DRIVE FPL("/root/a/b"), "mount"},
346 #endif
349 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
350 FileSystemURL cracked = mount_points->CreateCrackedFileSystemURL(
351 kTestOrigin,
352 storage::kFileSystemTypeExternal,
353 base::FilePath(kTestCases[i].path));
355 EXPECT_EQ(kTestCases[i].expect_valid, cracked.is_valid())
356 << "Test case index: " << i;
358 if (!kTestCases[i].expect_valid)
359 continue;
361 EXPECT_EQ(kTestOrigin, cracked.origin())
362 << "Test case index: " << i;
363 EXPECT_EQ(kTestCases[i].expect_type, cracked.type())
364 << "Test case index: " << i;
365 EXPECT_EQ(base::FilePath(
366 kTestCases[i].expect_path).NormalizePathSeparators(), cracked.path())
367 << "Test case index: " << i;
368 EXPECT_EQ(base::FilePath(kTestCases[i].path).NormalizePathSeparators(),
369 cracked.virtual_path())
370 << "Test case index: " << i;
371 EXPECT_EQ(kTestCases[i].expect_fs_id, cracked.filesystem_id())
372 << "Test case index: " << i;
373 EXPECT_EQ(storage::kFileSystemTypeExternal, cracked.mount_type())
374 << "Test case index: " << i;
378 TEST(ExternalMountPointsTest, CrackVirtualPath) {
379 scoped_refptr<storage::ExternalMountPoints> mount_points(
380 storage::ExternalMountPoints::CreateRefCounted());
382 const GURL kTestOrigin("http://chromium.org");
384 mount_points->RegisterFileSystem("c",
385 storage::kFileSystemTypeNativeLocal,
386 storage::FileSystemMountOption(),
387 base::FilePath(DRIVE FPL("/a/b/c")));
388 mount_points->RegisterFileSystem("c(1)",
389 storage::kFileSystemTypeDrive,
390 storage::FileSystemMountOption(),
391 base::FilePath(DRIVE FPL("/a/b/c(1)")));
392 mount_points->RegisterFileSystem("empty_path",
393 storage::kFileSystemTypeSyncable,
394 storage::FileSystemMountOption(),
395 base::FilePath());
396 mount_points->RegisterFileSystem("mount",
397 storage::kFileSystemTypeDrive,
398 storage::FileSystemMountOption(),
399 base::FilePath(DRIVE FPL("/root")));
401 struct TestCase {
402 const base::FilePath::CharType* const path;
403 bool expect_valid;
404 storage::FileSystemType expect_type;
405 const base::FilePath::CharType* const expect_path;
406 const char* const expect_name;
409 const TestCase kTestCases[] = {
410 {FPL("c/d/e"), true, storage::kFileSystemTypeNativeLocal,
411 DRIVE FPL("/a/b/c/d/e"), "c"},
412 {FPL("c(1)/d/e"), true, storage::kFileSystemTypeDrive,
413 DRIVE FPL("/a/b/c(1)/d/e"), "c(1)"},
414 {FPL("c(1)"), true, storage::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"),
415 "c(1)"},
416 {FPL("empty_path/a"), true, storage::kFileSystemTypeSyncable, FPL("a"),
417 "empty_path"},
418 {FPL("empty_path"), true, storage::kFileSystemTypeSyncable, FPL(""),
419 "empty_path"},
420 {FPL("mount/a/b"), true, storage::kFileSystemTypeDrive,
421 DRIVE FPL("/root/a/b"), "mount"},
422 {FPL("mount"), true, storage::kFileSystemTypeDrive, DRIVE FPL("/root"),
423 "mount"},
424 {FPL("cc"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
425 {FPL(""), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
426 {FPL(".."), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
427 // Absolte paths.
428 {FPL("/c/d/e"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
429 {FPL("/c(1)/d/e"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
430 {FPL("/empty_path"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
431 // PAth references parent.
432 {FPL("c/d/../e"), false, storage::kFileSystemTypeUnknown, FPL(""), ""},
433 {FPL("/empty_path/a/../b"), false, storage::kFileSystemTypeUnknown, FPL(""),
434 ""},
435 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
436 {FPL("c/d\\e"), true, storage::kFileSystemTypeNativeLocal,
437 DRIVE FPL("/a/b/c/d/e"), "c"},
438 {FPL("mount\\a\\b"), true, storage::kFileSystemTypeDrive,
439 DRIVE FPL("/root/a/b"), "mount"},
440 #endif
443 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
444 std::string cracked_name;
445 storage::FileSystemType cracked_type;
446 std::string cracked_id;
447 base::FilePath cracked_path;
448 storage::FileSystemMountOption cracked_option;
449 EXPECT_EQ(kTestCases[i].expect_valid,
450 mount_points->CrackVirtualPath(base::FilePath(kTestCases[i].path),
451 &cracked_name, &cracked_type, &cracked_id, &cracked_path,
452 &cracked_option))
453 << "Test case index: " << i;
455 if (!kTestCases[i].expect_valid)
456 continue;
458 EXPECT_EQ(kTestCases[i].expect_type, cracked_type)
459 << "Test case index: " << i;
460 EXPECT_EQ(base::FilePath(
461 kTestCases[i].expect_path).NormalizePathSeparators(), cracked_path)
462 << "Test case index: " << i;
463 EXPECT_EQ(kTestCases[i].expect_name, cracked_name)
464 << "Test case index: " << i;
465 // As of now we don't mount other filesystems with non-empty filesystem_id
466 // onto external mount points.
467 EXPECT_TRUE(cracked_id.empty()) << "Test case index: " << i;
471 TEST(ExternalMountPointsTest, MountOption) {
472 scoped_refptr<storage::ExternalMountPoints> mount_points(
473 storage::ExternalMountPoints::CreateRefCounted());
475 mount_points->RegisterFileSystem(
476 "nosync", storage::kFileSystemTypeNativeLocal,
477 storage::FileSystemMountOption(
478 storage::FlushPolicy::NO_FLUSH_ON_COMPLETION),
479 base::FilePath(DRIVE FPL("/nosync")));
480 mount_points->RegisterFileSystem(
481 "sync", storage::kFileSystemTypeNativeLocal,
482 storage::FileSystemMountOption(storage::FlushPolicy::FLUSH_ON_COMPLETION),
483 base::FilePath(DRIVE FPL("/sync")));
485 std::string name;
486 storage::FileSystemType type;
487 std::string cracked_id;
488 storage::FileSystemMountOption option;
489 base::FilePath path;
490 EXPECT_TRUE(mount_points->CrackVirtualPath(
491 base::FilePath(FPL("nosync/file")), &name, &type, &cracked_id, &path,
492 &option));
493 EXPECT_EQ(storage::FlushPolicy::NO_FLUSH_ON_COMPLETION,
494 option.flush_policy());
495 EXPECT_TRUE(mount_points->CrackVirtualPath(
496 base::FilePath(FPL("sync/file")), &name, &type, &cracked_id, &path,
497 &option));
498 EXPECT_EQ(storage::FlushPolicy::FLUSH_ON_COMPLETION, option.flush_policy());
501 } // namespace content