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

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

Powered by Google App Engine
This is Rietveld 408576698