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

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 // 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.
4
5 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h"
6
7 #include "base/file_path.h"
8 #include "googleurl/src/url_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/fileapi/external_mount_points.h"
11 #include "webkit/fileapi/file_system_url.h"
12 #include "webkit/fileapi/isolated_context.h"
13 #include "webkit/quota/mock_special_storage_policy.h"
14
15 namespace {
16
17 #define FPL(x) FILE_PATH_LITERAL(x)
kinuko 2013/01/11 08:20:52 tiny nit: this is totally fine but having #define
tbarzic 2013/01/11 09:13:16 Done.
18
19 fileapi::FileSystemURL CreateFileSystemURL(const std::string& extension,
20 const FilePath::CharType* path) {
21 return fileapi::FileSystemURL(GURL("chrome-extension://" + extension + "/"),
22 fileapi::kFileSystemTypeNativeLocal,
23 FilePath(path));
kinuko 2013/01/11 08:20:52 Alternatively you can just take const char* (so yo
tbarzic 2013/01/11 09:13:16 Done.
24 }
25
26 bool VectorHasPath(const std::vector<FilePath>& path_vector,
27 const FilePath& path) {
28 for (size_t i = 0; i < path_vector.size(); ++i) {
29 if (path_vector[i] == path)
30 return true;
31 }
32 return false;
33 }
34
35 TEST(CrosMountPointProviderTest, DefaultMountPoints) {
36 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
37 new quota::MockSpecialStoragePolicy();
38 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
39 fileapi::ExternalMountPoints::CreateRefCounted());
40 chromeos::CrosMountPointProvider provider(
41 storage_policy,
42 mount_points.get(),
43 fileapi::ExternalMountPoints::GetSystemInstance());
44 // By default there should be 4 mount points.
45 std::vector<FilePath> root_dirs = provider.GetRootDirectories();
46 EXPECT_EQ(4u, root_dirs.size());
47 EXPECT_TRUE(VectorHasPath(root_dirs, FilePath(FPL("/media/removable"))));
48 EXPECT_TRUE(VectorHasPath(root_dirs, FilePath(FPL("/media/archive"))));
49 EXPECT_TRUE(VectorHasPath(root_dirs, FilePath(FPL("/usr/share/oem"))));
kinuko 2013/01/11 08:20:52 Well, maybe... using std::set like following might
tbarzic 2013/01/11 09:13:16 Done.
50 // Fourth mount point is Downloads, but its local path is device specific.
51 }
52
53 TEST(CrosMountPointProviderTest, GetRootDirectories) {
54 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
55 new quota::MockSpecialStoragePolicy();
56 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
57 fileapi::ExternalMountPoints::CreateRefCounted());
58
59 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
60 fileapi::ExternalMountPoints::CreateRefCounted());
61
62 chromeos::CrosMountPointProvider provider(
63 storage_policy,
64 mount_points.get(),
65 system_mount_points.get());
66
67 // Register 'local' test mount points.
68 mount_points->RegisterFileSystem("c",
69 fileapi::kFileSystemTypeNativeLocal,
70 FilePath(FPL("/a/b/c")));
71 mount_points->RegisterFileSystem("d",
72 fileapi::kFileSystemTypeNativeLocal,
73 FilePath(FPL("/b/c/d")));
74
75 // Register system test mount points.
76 system_mount_points->RegisterFileSystem("d",
77 fileapi::kFileSystemTypeNativeLocal,
78 FilePath(FPL("/g/c/d")));
79 system_mount_points->RegisterFileSystem("e",
80 fileapi::kFileSystemTypeNativeLocal,
81 FilePath(FPL("/g/d/e")));
82
83 std::vector<FilePath> root_dirs = provider.GetRootDirectories();
84 EXPECT_EQ(4u, root_dirs.size());
85 EXPECT_TRUE(VectorHasPath(root_dirs, FilePath(FPL("/a/b/c"))));
86 EXPECT_TRUE(VectorHasPath(root_dirs, FilePath(FPL("/b/c/d"))));
87 EXPECT_TRUE(VectorHasPath(root_dirs, FilePath(FPL("/g/c/d"))));
88 EXPECT_TRUE(VectorHasPath(root_dirs, FilePath(FPL("/g/d/e"))));
89 }
90
91 TEST(CrosMountPointProviderTest, MountPointsVisibility) {
92 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
93 new quota::MockSpecialStoragePolicy();
94 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
95 fileapi::ExternalMountPoints::CreateRefCounted());
96 scoped_refptr<fileapi::ExternalMountPoints> sibling_mount_points(
97 fileapi::ExternalMountPoints::CreateRefCounted());
98
99 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
100 fileapi::ExternalMountPoints::CreateRefCounted());
101
102 chromeos::CrosMountPointProvider provider(
103 storage_policy,
104 mount_points.get(),
105 system_mount_points.get());
106
107 // A provider that shares system_mount_points with |provider|.
108 chromeos::CrosMountPointProvider sibling_provider(
109 storage_policy,
110 sibling_mount_points.get(),
111 system_mount_points.get());
112
113 FilePath ignored;
114
115 // Adding empty mount point should fail.
116 EXPECT_FALSE(provider.AddLocalMountPoint(FilePath()));
117
118 // Add mount point to the provider.
119 EXPECT_TRUE(provider.AddLocalMountPoint(FilePath(FPL("/a/b/c"))));
120
121 EXPECT_TRUE(provider.HasMountPoint(FilePath(FPL("/a/b/c"))));
122 // The mount point with the same name exists, but path is different.
123 EXPECT_FALSE(provider.HasMountPoint(FilePath(FPL("/x/a/b/c"))));
124 EXPECT_FALSE(sibling_provider.HasMountPoint(FilePath(FPL("/a/b/c"))));
125 EXPECT_TRUE(mount_points->GetRegisteredPath("c", &ignored));
126 EXPECT_FALSE(system_mount_points->GetRegisteredPath("c", &ignored));
127
128 // Add mount point directly to |mount_points|. It should be seen by
129 // |provider|.
130 EXPECT_TRUE(mount_points->RegisterFileSystem(
131 "d", fileapi::kFileSystemTypeNativeLocal, FilePath(FPL("/b/c/d"))));
132
133 EXPECT_TRUE(provider.HasMountPoint(FilePath(FPL("/b/c/d"))));
134 EXPECT_FALSE(sibling_provider.HasMountPoint(FilePath(FPL("/b/c/d"))));
135
136 // Add mount point to system mount points.
137 EXPECT_TRUE(system_mount_points->RegisterFileSystem(
138 "e", fileapi::kFileSystemTypeNativeLocal, FilePath(FPL("/g/c/d/e"))));
139
140 EXPECT_FALSE(provider.HasMountPoint(FilePath(FPL("/g/c/d/e"))));
141 EXPECT_FALSE(sibling_provider.HasMountPoint(FilePath(FPL("/g/c/d/e"))));
142
143 // Can't remove system mount point.
144 provider.RemoveMountPoint(FilePath(FPL("/g/c/d/e")));
145 EXPECT_TRUE(system_mount_points->GetRegisteredPath("e", &ignored));
146
147 // Add a mount points whose paths overlap with the system one's.
148 // The same path:
149 EXPECT_TRUE(provider.AddLocalMountPoint(FilePath(FPL("/g/c/d/e"))));
150 EXPECT_TRUE(provider.HasMountPoint(FilePath(FPL("/g/c/d/e"))));
151 provider.RemoveMountPoint(FilePath(FPL("/g/c/d/e")));
152 // Parent path:
153 EXPECT_TRUE(provider.AddLocalMountPoint(FilePath(FPL("/g"))));
154 EXPECT_TRUE(provider.HasMountPoint(FilePath(FPL("/g"))));
155 provider.RemoveMountPoint(FilePath(FPL("/g")));
156 // Child path:
157 EXPECT_TRUE(provider.AddLocalMountPoint(FilePath(FPL("/g/c/d/e/f/g"))));
158 EXPECT_TRUE(provider.HasMountPoint(FilePath(FPL("/g/c/d/e/f/g"))));
159 provider.RemoveMountPoint(FilePath(FPL("/g/c/d/e/f/g")));
160
161 // Add mount point with the same name as a global one. Should succeed.
162 EXPECT_TRUE(provider.AddLocalMountPoint(FilePath(FPL("/d/e"))));
163
164 EXPECT_TRUE(provider.HasMountPoint(FilePath(FPL("/d/e"))));
165
166 // Remove system mount point with the same name as the added one.
167 // Should fail.
168 provider.RemoveMountPoint(FilePath(FPL("/g/c/d/e")));
169
170 EXPECT_TRUE(provider.HasMountPoint(FilePath(FPL("/d/e"))));
171 EXPECT_TRUE(system_mount_points->GetRegisteredPath("e", &ignored));
172
173 // Remove mount point.
174 provider.RemoveMountPoint(FilePath(FPL("/d/e")));
175
176 EXPECT_FALSE(provider.HasMountPoint(FilePath(FPL("/d/e"))));
177 }
178
179 TEST(CrosMountPointProviderTest, AccessPermissions) {
180 url_util::AddStandardScheme("chrome-extension");
181
182 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy =
183 new quota::MockSpecialStoragePolicy();
184 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
185 fileapi::ExternalMountPoints::CreateRefCounted());
186 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
187 fileapi::ExternalMountPoints::CreateRefCounted());
188 chromeos::CrosMountPointProvider provider(
189 storage_policy,
190 mount_points.get(),
191 system_mount_points.get());
192
193 std::string extension("ddammdhioacbehjngdmkjcjbnfginlla");
194
195 storage_policy->AddFileHandler(extension);
196
197 // Initialize mount points.
198 system_mount_points->RegisterFileSystem("system",
199 fileapi::kFileSystemTypeNativeLocal,
200 FilePath(FPL("/g/system")));
201 ASSERT_TRUE(provider.AddLocalMountPoint(FilePath(FPL("/media/removable"))));
202 ASSERT_TRUE(provider.AddRestrictedLocalMountPoint(
203 FilePath(FPL("/usr/share/oem"))));
204
205 // Provider specific mount point access.
206 EXPECT_FALSE(provider.IsAccessAllowed(
207 CreateFileSystemURL(extension, FPL("removable/foo"))));
208
209 provider.GrantFileAccessToExtension(extension,
210 FilePath(FPL("removable/foo")));
211 EXPECT_TRUE(provider.IsAccessAllowed(
212 CreateFileSystemURL(extension, FPL("removable/foo"))));
213 EXPECT_FALSE(provider.IsAccessAllowed(
214 CreateFileSystemURL(extension, FPL("removable/foo1"))));
215
216 // System mount point access.
217 EXPECT_FALSE(provider.IsAccessAllowed(
218 CreateFileSystemURL(extension, FPL("system/foo"))));
219
220 provider.GrantFileAccessToExtension(extension, FilePath(FPL("system/foo")));
221 EXPECT_TRUE(provider.IsAccessAllowed(
222 CreateFileSystemURL(extension, FPL("system/foo"))));
223 EXPECT_FALSE(provider.IsAccessAllowed(
224 CreateFileSystemURL(extension, FPL("system/foo1"))));
225
226 // oem is restricted file system.
227 provider.GrantFileAccessToExtension(extension, FilePath(FPL("oem/foo")));
228 // The extension should not be able to access the file even if
229 // GrantFileAccessToExtension was called.
230 EXPECT_FALSE(provider.IsAccessAllowed(
231 CreateFileSystemURL(extension, FPL("oem/foo"))));
232
233 provider.GrantFullAccessToExtension(extension);
234 // The extension should be able to access restricted file system after it was
235 // granted full access.
236 EXPECT_TRUE(provider.IsAccessAllowed(
237 CreateFileSystemURL(extension, FPL("oem/foo"))));
238 // The extension which was granted full access should be able to access any
239 // path on current file systems.
240 EXPECT_TRUE(provider.IsAccessAllowed(
241 CreateFileSystemURL(extension, FPL("removable/foo1"))));
242 EXPECT_TRUE(provider.IsAccessAllowed(
243 CreateFileSystemURL(extension, FPL("system/foo1"))));
244
245 // The extension still cannot access new mount points.
246 ASSERT_TRUE(provider.AddLocalMountPoint(FilePath(FPL("/foo/test"))));
247 EXPECT_FALSE(provider.IsAccessAllowed(
248 CreateFileSystemURL(extension, FPL("test_/foo"))));
249
250 provider.RevokeAccessForExtension(extension);
251 EXPECT_FALSE(provider.IsAccessAllowed(
252 CreateFileSystemURL(extension, FPL("removable/foo"))));
253
254 fileapi::FileSystemURL internal_url(GURL("chrome://foo"),
255 fileapi::kFileSystemTypeExternal,
256 FilePath(FPL("removable/")));
257 // Internal WebUI should have full access.
258 EXPECT_TRUE(provider.IsAccessAllowed(internal_url));
259 }
260
261 TEST(CrosMountPointProvider, GetVirtualPathConflictWithSystemPoints) {
262 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy =
263 new quota::MockSpecialStoragePolicy();
264 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
265 fileapi::ExternalMountPoints::CreateRefCounted());
266 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
267 fileapi::ExternalMountPoints::CreateRefCounted());
268 chromeos::CrosMountPointProvider provider(storage_policy,
269 mount_points.get(),
270 system_mount_points.get());
271
272 const fileapi::FileSystemType type = fileapi::kFileSystemTypeNativeLocal;
273
274 // Provider specific mount points.
275 ASSERT_TRUE(
276 mount_points->RegisterFileSystem("b", type, FilePath(FPL("/a/b"))));
277 ASSERT_TRUE(
278 mount_points->RegisterFileSystem("y", type, FilePath(FPL("/z/y"))));
279 ASSERT_TRUE(
280 mount_points->RegisterFileSystem("n", type, FilePath(FPL("/m/n"))));
281
282 // System mount points
283 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
284 "gb", type, FilePath(FPL("/a/b"))));
285 ASSERT_TRUE(
286 system_mount_points->RegisterFileSystem("gz", type, FilePath(FPL("/z"))));
287 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
288 "gp", type, FilePath(FPL("/m/n/o/p"))));
289
290 struct TestCase {
291 const FilePath::CharType* const local_path;
292 bool success;
293 const FilePath::CharType* const virtual_path;
294 };
295
296 const TestCase kTestCases[] = {
297 // Same paths in both mount points.
298 { FPL("/a/b/c/d"), true, FPL("b/c/d") },
299 // System mount points path more specific.
300 { FPL("/m/n/o/p/r/s"), true, FPL("n/o/p/r/s") },
301 // System mount points path less specific.
302 { FPL("/z/y/x"), true, FPL("y/x") },
303 // Only system mount points path matches.
304 { FPL("/z/q/r/s"), true, FPL("gz/q/r/s") },
305 // No match.
306 { FPL("/foo/xxx"), false, FPL("") },
307 };
308
309 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
310 // Initialize virtual path with a value.
311 FilePath virtual_path(FPL("/mount"));
312 FilePath local_path(kTestCases[i].local_path);
313 EXPECT_EQ(kTestCases[i].success,
314 provider.GetVirtualPath(local_path, &virtual_path))
315 << "Resolving " << kTestCases[i].local_path;
316
317 // There are no guarantees for |virtual_path| value if |GetVirtualPath|
318 // fails.
319 if (!kTestCases[i].success)
320 continue;
321
322 FilePath expected_virtual_path(kTestCases[i].virtual_path);
323 EXPECT_EQ(expected_virtual_path, virtual_path)
324 << "Resolving " << kTestCases[i].local_path;
325 }
326 }
327
328 } // namespace
329
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698