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

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

Powered by Google App Engine
This is Rietveld 408576698