Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: webkit/chromeos/fileapi/cros_mount_point_provider_unittest.cc

Issue 11648027: Extract external file systems handling from isolated context. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: few nits Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
kinuko 2013/01/10 05:47:31 nit: 2012 -> 2013
tonibarzic 2013/01/11 01:50:04 Done.
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 "base/file_path.h"
6 #include "googleurl/src/url_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h"
9 #include "webkit/fileapi/external_mount_points.h"
10 #include "webkit/fileapi/file_system_url.h"
11 #include "webkit/fileapi/isolated_context.h"
12 #include "webkit/quota/mock_special_storage_policy.h"
13
14 #define FPL(x) FILE_PATH_LITERAL(x)
15
16 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
17 #define DRIVE FPL("C:")
18 #else
19 #define DRIVE
20 #endif
21
22 namespace {
23
24 FilePath GetFilePath(const std::string& path_str) {
25 return FilePath(DRIVE FPL(path_str));
26 }
27
28 fileapi::FileSystemURL CreateFileSystemURL(const std::string& extension,
29 const std::string& path) {
30 return fileapi::FileSystemURL(GURL("chrome-extension://" + extension + "/"),
31 fileapi::kFileSystemTypeNativeLocal,
32 GetFilePath(path));
33 }
34
35 bool VectorHasPath(const std::vector<FilePath>& path_vector,
36 const FilePath& path) {
37 for (size_t i = 0; i < path_vector.size(); ++i) {
38 if (path_vector[i] == path) {
39 return true;
40 }
kinuko 2013/01/10 05:47:31 style-nit: no { } for one line block
tonibarzic 2013/01/11 01:50:04 Done.
41 }
42 return false;
43 }
44
45 TEST(CrosMountPointProviderTest, DefaultMountPoints) {
46 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
47 new quota::MockSpecialStoragePolicy();
48 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
49 fileapi::ExternalMountPoints::CreateRefCounted());
50 chromeos::CrosMountPointProvider provider(
51 storage_policy,
52 mount_points.get(),
53 fileapi::ExternalMountPoints::GetSystemInstance());
54 // By default there should be 4 mount points.
55 std::vector<FilePath> root_dirs = provider.GetRootDirectories();
56 EXPECT_EQ(4u, root_dirs.size());
57 EXPECT_TRUE(VectorHasPath(root_dirs, GetFilePath("/media/removable")));
58 EXPECT_TRUE(VectorHasPath(root_dirs, GetFilePath("/media/archive")));
59 EXPECT_TRUE(VectorHasPath(root_dirs, GetFilePath("/usr/share/oem")));
60 // Fourth mount point is Downloads, but it's local path is device specific.
kinuko 2013/01/10 05:47:31 nit: it's local path -> its local path ?
tonibarzic 2013/01/11 01:50:04 Done.
61 }
62
63 TEST(CrosMountPointProviderTest, GetRootDirectories) {
64 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
65 new quota::MockSpecialStoragePolicy();
66 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
67 fileapi::ExternalMountPoints::CreateRefCounted());
68
69 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
70 fileapi::ExternalMountPoints::CreateRefCounted());
71
72 chromeos::CrosMountPointProvider provider(
73 storage_policy,
74 mount_points.get(),
75 system_mount_points.get());
76
77 // Register 'local' test mount points.
78 mount_points->RegisterFileSystem("c",
79 fileapi::kFileSystemTypeNativeLocal,
80 GetFilePath("/a/b/c"));
81 mount_points->RegisterFileSystem("d",
82 fileapi::kFileSystemTypeNativeLocal,
83 GetFilePath("/b/c/d"));
84
85 // Register system test mount points.
86 system_mount_points->RegisterFileSystem("d",
87 fileapi::kFileSystemTypeNativeLocal,
88 GetFilePath("/g/c/d"));
89 system_mount_points->RegisterFileSystem("e",
90 fileapi::kFileSystemTypeNativeLocal,
91 GetFilePath("/g/d/e"));
92
93 std::vector<FilePath> root_dirs = provider.GetRootDirectories();
94 EXPECT_EQ(4u, root_dirs.size());
95 EXPECT_TRUE(VectorHasPath(root_dirs, GetFilePath("/a/b/c")));
96 EXPECT_TRUE(VectorHasPath(root_dirs, GetFilePath("/b/c/d")));
97 EXPECT_TRUE(VectorHasPath(root_dirs, GetFilePath("/g/c/d")));
98 EXPECT_TRUE(VectorHasPath(root_dirs, GetFilePath("/g/d/e")));
99 }
100
101 TEST(CrosMountPointProviderTest, MountPointsVisibility) {
102 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
103 new quota::MockSpecialStoragePolicy();
104 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
105 fileapi::ExternalMountPoints::CreateRefCounted());
106 scoped_refptr<fileapi::ExternalMountPoints> sibling_mount_points(
107 fileapi::ExternalMountPoints::CreateRefCounted());
108
109 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
110 fileapi::ExternalMountPoints::CreateRefCounted());
111
112 chromeos::CrosMountPointProvider provider(
113 storage_policy,
114 mount_points.get(),
115 system_mount_points.get());
116
117 // A provider that shares system_mount_points with |provider|.
118 chromeos::CrosMountPointProvider sibling_provider(
119 storage_policy,
120 sibling_mount_points.get(),
121 system_mount_points.get());
122
123 FilePath ignored;
124
125 // Add mount point to the provider.
126 EXPECT_TRUE(provider.AddLocalMountPoint(GetFilePath("/a/b/c")));
127
128 EXPECT_TRUE(provider.HasMountPoint(GetFilePath("/a/b/c")));
129 EXPECT_FALSE(sibling_provider.HasMountPoint(GetFilePath("/a/b/c")));
130 EXPECT_TRUE(mount_points->GetRegisteredPath("c", &ignored));
131 EXPECT_FALSE(system_mount_points->GetRegisteredPath("c", &ignored));
132
133 // Add mount point directly to |mount_points|. It should be seen by
134 // |provider|.
135 EXPECT_TRUE(mount_points->RegisterFileSystem(
136 "d", fileapi::kFileSystemTypeNativeLocal, GetFilePath("/b/c/d")));
137
138 EXPECT_TRUE(provider.HasMountPoint(GetFilePath("/b/c/d")));
139 EXPECT_FALSE(sibling_provider.HasMountPoint(GetFilePath("/b/c/d")));
140
141 // Add mount point to system mount points.
142 EXPECT_TRUE(system_mount_points->RegisterFileSystem(
143 "e", fileapi::kFileSystemTypeNativeLocal, GetFilePath("/g/c/d/e")));
144
145 EXPECT_FALSE(provider.HasMountPoint(GetFilePath("/g/c/d/e")));
146 EXPECT_FALSE(sibling_provider.HasMountPoint(GetFilePath("/g/c/d/e")));
147
148 // Can't remove system nount point.
kinuko 2013/01/10 05:47:31 nount -> mount
tonibarzic 2013/01/11 01:50:04 Done.
149 provider.RemoveMountPoint(GetFilePath("/g/c/d/e"));
150 EXPECT_TRUE(system_mount_points->GetRegisteredPath("e", &ignored));
151
152 // Add a mount points whose paths overlap with the system one's.
153 // The same path
kinuko 2013/01/10 05:47:31 nit: ends comments with period (or ':' in this cas
tonibarzic 2013/01/11 01:50:04 Done.
154 EXPECT_TRUE(provider.AddLocalMountPoint(GetFilePath("/g/c/d/e")));
155 EXPECT_TRUE(provider.HasMountPoint(GetFilePath("/g/c/d/e")));
156 provider.RemoveMountPoint(GetFilePath("/g/c/d/e"));
157 // Parent path.
158 EXPECT_TRUE(provider.AddLocalMountPoint(GetFilePath("/g")));
159 EXPECT_TRUE(provider.HasMountPoint(GetFilePath("/g")));
160 provider.RemoveMountPoint(GetFilePath("/g"));
161 // Child path.
162 EXPECT_TRUE(provider.AddLocalMountPoint(GetFilePath("/g/c/d/e/f/g")));
163 EXPECT_TRUE(provider.HasMountPoint(GetFilePath("/g/c/d/e/f/g")));
164 provider.RemoveMountPoint(GetFilePath("/g/c/d/e/f/g"));
165
166 // Add mount point with the same name as a global one. Should succeed.
167 EXPECT_TRUE(provider.AddLocalMountPoint(GetFilePath("/d/e")));
168
169 EXPECT_TRUE(provider.HasMountPoint(GetFilePath("/d/e")));
170
171 // Remove system mount point with the same name as the added one.
172 // Should fail.
173 provider.RemoveMountPoint(GetFilePath("/g/c/d/e"));
174
175 EXPECT_TRUE(provider.HasMountPoint(GetFilePath("/d/e")));
176 EXPECT_TRUE(system_mount_points->GetRegisteredPath("e", &ignored));
177
178 // Remove mount point.
179 provider.RemoveMountPoint(GetFilePath("/d/e"));
180
181 EXPECT_FALSE(provider.HasMountPoint(GetFilePath("/d/e")));
182 }
183
184 TEST(CrosMountPointProviderTest, AddMountPoint) {
kinuko 2013/01/10 05:47:31 Shouldn't this test be rather added as ExternalMou
tonibarzic 2013/01/11 01:50:04 yes, it should (fwiw that's what I planned to do y
185 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
186 new quota::MockSpecialStoragePolicy();
187 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
188 fileapi::ExternalMountPoints::CreateRefCounted());
189 scoped_refptr<fileapi::ExternalMountPoints> ignored_mount_points(
190 fileapi::ExternalMountPoints::CreateRefCounted());
191 chromeos::CrosMountPointProvider provider(storage_policy,
192 mount_points.get(),
193 ignored_mount_points);
194
195 struct TestCase {
196 const char* const mount_point;
197 bool success;
198 bool should_exist_after_adding;
199 };
200
201 const TestCase kTestCases[] = {
202 // Valid mount point.
203 { "/foo/test", true, true },
204 // Valid mount point with only one path component.
205 { "/bbb/", true, true },
206 // Existing mount point path is substring of the mount points path.
207 { "/foo/test11", true, true },
208 // Path substring of an existing path.
209 { "/foo/test1", true, true },
210 // Empty mount point path.
211 { "", false, false },
212 // Empty mount point name.
213 { "/", false, false },
214 // References parent.
215 { "../foo/invalid", false, false },
216 // Relative path.
217 { "foo/relative", false, false },
218 // Existing mount point.
219 { "/foo/test", false, true },
220 // Existing mount point with trailing /.
221 { "/foo/test/", false, false },
222 // Mount point with the same name exists.
223 { "/foo/a/test", false, false },
224 // Child of an existing mount point.
225 { "/foo/test/a", false, false },
226 // Parent of an existing mount point.
227 { "/foo", false, false },
228 // Bit bigger depth.
229 { "/foo/a/b/c/d/e/f/g", true, true },
230 // Sibling mount point (with similar name) exists.
231 { "/foo/a/b/c/d/e/ff", true, true },
232 // Lexicographically last among existing mount points.
233 { "/zzz/yyy", true, true },
234 // Parent of the lexicographically last mount point.
235 { "/zzz/", false, false },
236 // Child of the lexicographically last mount point.
237 { "/zzz/yyy/xxx", false, false },
238 // Lexicographically first among existing mount points.
239 { "/a/b", true, true },
240 // Parent of lexicographically first mount point.
241 { "/a", false, false },
242 // Child of lexicographically last mount point.
243 { "/a/b/c", false, false },
244 };
245
246 // Test adding mount points.
247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
248 FilePath mount_point = GetFilePath(kTestCases[i].mount_point);
249 EXPECT_EQ(kTestCases[i].success,
250 provider.AddLocalMountPoint(mount_point))
251 << "Adding mount point: " << kTestCases[i].mount_point;
252 }
253
254 // Test that final mount point presence state is as expected.
255 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
256 FilePath mount_point = GetFilePath(kTestCases[i].mount_point);
257 EXPECT_EQ(kTestCases[i].should_exist_after_adding,
258 provider.HasMountPoint(mount_point))
259 << "Has mount point: " << kTestCases[i].mount_point;
260 }
261 }
262
263 TEST(CrosMountPointProviderTest, AccessPermissions) {
264 url_util::AddStandardScheme("chrome-extension");
265
266 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy =
267 new quota::MockSpecialStoragePolicy();
268 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
269 fileapi::ExternalMountPoints::CreateRefCounted());
270 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
271 fileapi::ExternalMountPoints::CreateRefCounted());
272 chromeos::CrosMountPointProvider provider(
273 storage_policy,
274 mount_points.get(),
275 system_mount_points.get());
276
277 std::string extension("ddammdhioacbehjngdmkjcjbnfginlla");
278
279 storage_policy->AddFileHandler(extension);
280
281 // Initialize mount points.
282 system_mount_points->RegisterFileSystem("system",
283 fileapi::kFileSystemTypeNativeLocal,
284 GetFilePath("/g/system"));
285 ASSERT_TRUE(provider.AddLocalMountPoint(GetFilePath("/media/removable")));
286 ASSERT_TRUE(provider.AddRestrictedLocalMountPoint(
287 GetFilePath("/usr/share/oem")));
288
289 // Provider specific mount point access.
290 EXPECT_FALSE(provider.IsAccessAllowed(CreateFileSystemURL(extension,
291 "removable/foo")));
292
293 provider.GrantFileAccessToExtension(extension, GetFilePath("removable/foo"));
294 EXPECT_TRUE(provider.IsAccessAllowed(CreateFileSystemURL(extension,
295 "removable/foo")));
296 EXPECT_FALSE(provider.IsAccessAllowed(CreateFileSystemURL(extension,
297 "removable/foo1")));
298
299 // System mount point access.
300 EXPECT_FALSE(
301 provider.IsAccessAllowed(CreateFileSystemURL(extension, "system/foo")));
302
303 provider.GrantFileAccessToExtension(extension, GetFilePath("system/foo"));
304 EXPECT_TRUE(
305 provider.IsAccessAllowed(CreateFileSystemURL(extension, "system/foo")));
306 EXPECT_FALSE(
307 provider.IsAccessAllowed(CreateFileSystemURL(extension, "system/foo1")));
308
309 // oem is restricted file system.
310 provider.GrantFileAccessToExtension(extension, GetFilePath("oem/foo"));
311 // The extension should not be able to access the file even if
312 // GrantFileAccessToExtension was called.
313 EXPECT_FALSE(
314 provider.IsAccessAllowed(CreateFileSystemURL(extension, "oem/foo")));
315
316 provider.GrantFullAccessToExtension(extension);
317 // The extension should be able to access restricted file system after it was
318 // granted full access.
319 EXPECT_TRUE(
320 provider.IsAccessAllowed(CreateFileSystemURL(extension, "oem/foo")));
321 // The extension which was granted fulle access should be able to access any
322 // path on curent file systems.
323 EXPECT_TRUE(provider.IsAccessAllowed(
324 CreateFileSystemURL(extension, "removable/foo1")));
325 EXPECT_TRUE(provider.IsAccessAllowed(
326 CreateFileSystemURL(extension, "system/foo1")));
327
328 // The extension still cannot access new mount points.
329 ASSERT_TRUE(provider.AddLocalMountPoint(GetFilePath("/foo/test")));
330 EXPECT_FALSE(
331 provider.IsAccessAllowed(CreateFileSystemURL(extension, "test_/foo")));
332
333 provider.RevokeAccessForExtension(extension);
334 EXPECT_FALSE(provider.IsAccessAllowed(
335 CreateFileSystemURL(extension,"removable/foo")));
336
337 fileapi::FileSystemURL internal_url(GURL("chrome://foo"),
338 fileapi::kFileSystemTypeExternal,
339 GetFilePath("removable/"));
kinuko 2013/01/10 05:47:31 nit: indent
tonibarzic 2013/01/11 01:50:04 Done.
340 // Internal WebUI should have full access.
341 EXPECT_TRUE(provider.IsAccessAllowed(internal_url));
342 }
343
344 // TODO(tbarzic): This could be moved to external_mount_points tests.
kinuko 2013/01/10 05:47:31 Yep, this would fit better in external_mount_point
tonibarzic 2013/01/11 01:50:04 Done.
345 TEST(CrosMountPointProvider, GetVirtualPath) {
346 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy =
347 new quota::MockSpecialStoragePolicy();
348 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
349 fileapi::ExternalMountPoints::CreateRefCounted());
350 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
351 fileapi::ExternalMountPoints::CreateRefCounted());
352 chromeos::CrosMountPointProvider provider(storage_policy,
353 system_mount_points.get(),
354 mount_points.get());
355
356 ASSERT_TRUE(provider.AddLocalMountPoint(GetFilePath("/a/b/c")));
357 ASSERT_TRUE(provider.AddLocalMountPoint(GetFilePath("/z/y/x")));
358 ASSERT_TRUE(provider.AddLocalMountPoint(GetFilePath("/m/n/o")));
359
360 // Register mount point whose name does not match its path base name.
361 ASSERT_TRUE(
362 mount_points->RegisterFileSystem("mount",
363 fileapi::kFileSystemTypeNativeLocal,
364 FilePath(DRIVE FPL("/root/foo"))));
365
366 struct TestCase {
367 const char* const local_path;
368 bool success;
369 const char* const virtual_path;
370 };
371
372 const TestCase kTestCases[] = {
373 // Empty path.
374 { "", false, "" },
375 // No registered mount point (but is parent to a mount point).
376 { "/a/b", false, ""},
377 // No registered mount point (but is parent to a mount point).
378 { "/z/y", false, ""},
379 // No registered mount point (but is parent to a mount point).
380 { "/m/n", false, ""},
381 // No registered mount point.
382 { "/foo/mount", false, ""},
383 // An existing mount point path is substring.
384 { "/a/b/c1", false, "" },
385 // No leading /.
386 { "a/b/c", false, "" },
387 // Sibling to a root path.
388 { "/a/b/d/e", false, ""},
389 // Sibling to a root path.
390 { "/z/y/v/u", false, ""},
391 // Sibling to a root path.
392 { "/m/n/p/q", false, ""},
393 // Mount point root path.
394 { "/a/b/c", true, "c"},
395 // Mount point root path.
396 { "/z/y/x", true, "x"},
397 // Mount point root path.
398 { "/m/n/o", true, "o"},
399 // Mount point child path.
400 { "/a/b/c/d/e", true, "c/d/e"},
401 // Mount point child path.
402 { "/z/y/x/v/u", true, "x/v/u"},
403 // Mount point child path.
404 { "/m/n/o/p/q", true, "o/p/q"},
405 // Name doesn't match mount point path base name.
406 { "/root/foo/a/b/c", true, "mount/a/b/c" },
407 { "/root/foo", true, "mount" },
408 };
409
410 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
411 // Init virtual path with a value.
412 FilePath virtual_path(DRIVE FPL("/mount"));
413 FilePath local_path(DRIVE FPL(kTestCases[i].local_path));
414 EXPECT_EQ(kTestCases[i].success,
415 provider.GetVirtualPath(local_path, &virtual_path))
416 << "Resolving " << kTestCases[i].local_path;
417
418 // There are no guaranties for |virtual_path| value if |GetVirtualPath|
419 // fails.
420 if (!kTestCases[i].success)
421 continue;
422
423 FilePath expected_virtual_path(DRIVE FPL(kTestCases[i].virtual_path));
424 EXPECT_EQ(expected_virtual_path, virtual_path)
425 << "Resolving " << kTestCases[i].local_path;
426 }
427 }
428
429 TEST(CrosMountPointProvider, GetVirtualPathConflictWithSystemPoints) {
430 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy =
431 new quota::MockSpecialStoragePolicy();
432 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
433 fileapi::ExternalMountPoints::CreateRefCounted());
434 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
435 fileapi::ExternalMountPoints::CreateRefCounted());
436 chromeos::CrosMountPointProvider provider(storage_policy,
437 mount_points.get(),
438 system_mount_points.get());
439
440 const fileapi::FileSystemType type = fileapi::kFileSystemTypeNativeLocal;
441
442 // Provider specific mount points.
443 ASSERT_TRUE(
444 mount_points->RegisterFileSystem("b", type, GetFilePath("/a/b")));
445 ASSERT_TRUE(
446 mount_points->RegisterFileSystem("y", type, GetFilePath("/z/y")));
447 ASSERT_TRUE(
448 mount_points->RegisterFileSystem("n", type, GetFilePath("/m/n")));
449
450 // System mount points
451 ASSERT_TRUE(
452 system_mount_points->RegisterFileSystem("gb", type, GetFilePath("/a/b")));
453 ASSERT_TRUE(
454 system_mount_points->RegisterFileSystem("gz", type, GetFilePath("/z")));
455 ASSERT_TRUE(system_mount_points->RegisterFileSystem("gp", type,
456 GetFilePath("/m/n/o/p")));
457 struct TestCase {
458 const char* const local_path;
459 bool success;
460 const char* const virtual_path;
461 };
462
463 const TestCase kTestCases[] = {
464 // Same paths in both mount points.
465 { "/a/b/c/d", true, "b/c/d" },
466 // System mount points path more specific.
467 { "/m/n/o/p/r/s", true, "n/o/p/r/s" },
468 // System mount points path less specific.
469 { "/z/y/x", true, "y/x"},
470 // Only system mount points path matches.
471 { "/z/q/r/s", true, "gz/q/r/s"},
472 // No match.
473 { "/foo/xxx", false, "" },
474 };
475
476 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
477 // Init virtual path with a value.
478 FilePath virtual_path(DRIVE FPL("/mount"));
479 FilePath local_path(DRIVE FPL(kTestCases[i].local_path));
480 EXPECT_EQ(kTestCases[i].success,
481 provider.GetVirtualPath(local_path, &virtual_path))
482 << "Resolving " << kTestCases[i].local_path;
483
484 // There are no guaranties for |virtual_path| value if |GetVirtualPath|
485 // fails.
486 if (!kTestCases[i].success)
487 continue;
488
489 FilePath expected_virtual_path(DRIVE FPL(kTestCases[i].virtual_path));
490 EXPECT_EQ(expected_virtual_path, virtual_path)
491 << "Resolving " << kTestCases[i].local_path;
492 }
493 }
494
495 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698