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

Side by Side Diff: chrome/browser/chromeos/fileapi/cros_mount_point_provider_unittest.cc

Issue 18344013: fileapi: Rename FileSystemMountProvider to FileSystemBackend (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 7 years, 5 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 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 "chrome/browser/chromeos/fileapi/cros_mount_point_provider.h"
6
7 #include <set>
8
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "chromeos/dbus/cros_disks_client.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/url_util.h"
14 #include "webkit/browser/fileapi/external_mount_points.h"
15 #include "webkit/browser/fileapi/file_permission_policy.h"
16 #include "webkit/browser/fileapi/file_system_url.h"
17 #include "webkit/browser/fileapi/isolated_context.h"
18 #include "webkit/browser/quota/mock_special_storage_policy.h"
19
20 #define FPL(x) FILE_PATH_LITERAL(x)
21
22 using fileapi::ExternalMountPoints;
23 using fileapi::FileSystemURL;
24
25 namespace {
26
27 FileSystemURL CreateFileSystemURL(const std::string& extension,
28 const char* path,
29 ExternalMountPoints* mount_points) {
30 return mount_points->CreateCrackedFileSystemURL(
31 GURL("chrome-extension://" + extension + "/"),
32 fileapi::kFileSystemTypeExternal,
33 base::FilePath::FromUTF8Unsafe(path));
34 }
35
36 TEST(CrosMountPointProviderTest, DefaultMountPoints) {
37 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
38 new quota::MockSpecialStoragePolicy();
39 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
40 fileapi::ExternalMountPoints::CreateRefCounted());
41 chromeos::CrosMountPointProvider provider(
42 NULL, // drive_delegate
43 storage_policy,
44 mount_points.get(),
45 fileapi::ExternalMountPoints::GetSystemInstance());
46 provider.AddSystemMountPoints();
47 std::vector<base::FilePath> root_dirs = provider.GetRootDirectories();
48 std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end());
49
50 // By default there should be 4 mount points (in system mount points):
51 EXPECT_EQ(4u, root_dirs.size());
52 base::FilePath home_path;
53 ASSERT_TRUE(PathService::Get(base::DIR_HOME, &home_path));
54
55 EXPECT_TRUE(root_dirs_set.count(home_path.AppendASCII("Downloads")));
56 EXPECT_TRUE(root_dirs_set.count(
57 chromeos::CrosDisksClient::GetRemovableDiskMountPoint()));
58 EXPECT_TRUE(root_dirs_set.count(
59 chromeos::CrosDisksClient::GetArchiveMountPoint()));
60 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/usr/share/oem"))));
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 NULL, // drive_delegate
74 storage_policy,
75 mount_points.get(),
76 system_mount_points.get());
77
78 const size_t initial_root_dirs_size = provider.GetRootDirectories().size();
79
80 // Register 'local' test mount points.
81 mount_points->RegisterFileSystem("c",
82 fileapi::kFileSystemTypeNativeLocal,
83 base::FilePath(FPL("/a/b/c")));
84 mount_points->RegisterFileSystem("d",
85 fileapi::kFileSystemTypeNativeLocal,
86 base::FilePath(FPL("/b/c/d")));
87
88 // Register system test mount points.
89 system_mount_points->RegisterFileSystem("d",
90 fileapi::kFileSystemTypeNativeLocal,
91 base::FilePath(FPL("/g/c/d")));
92 system_mount_points->RegisterFileSystem("e",
93 fileapi::kFileSystemTypeNativeLocal,
94 base::FilePath(FPL("/g/d/e")));
95
96 std::vector<base::FilePath> root_dirs = provider.GetRootDirectories();
97 std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end());
98 EXPECT_EQ(initial_root_dirs_size + 4, root_dirs.size());
99 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/a/b/c"))));
100 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/b/c/d"))));
101 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/c/d"))));
102 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/d/e"))));
103 }
104
105 TEST(CrosMountPointProviderTest, AccessPermissions) {
106 url_util::AddStandardScheme("chrome-extension");
107
108 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy =
109 new quota::MockSpecialStoragePolicy();
110 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
111 fileapi::ExternalMountPoints::CreateRefCounted());
112 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
113 fileapi::ExternalMountPoints::CreateRefCounted());
114 chromeos::CrosMountPointProvider provider(
115 NULL, // drive_delegate
116 storage_policy,
117 mount_points.get(),
118 system_mount_points.get());
119
120 std::string extension("ddammdhioacbehjngdmkjcjbnfginlla");
121
122 storage_policy->AddFileHandler(extension);
123
124 // Initialize mount points.
125 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
126 "system",
127 fileapi::kFileSystemTypeNativeLocal,
128 base::FilePath(FPL("/g/system"))));
129 ASSERT_TRUE(mount_points->RegisterFileSystem(
130 "removable",
131 fileapi::kFileSystemTypeNativeLocal,
132 base::FilePath(FPL("/media/removable"))));
133 ASSERT_TRUE(mount_points->RegisterFileSystem(
134 "oem",
135 fileapi::kFileSystemTypeRestrictedNativeLocal,
136 base::FilePath(FPL("/usr/share/oem"))));
137
138 // Provider specific mount point access.
139 EXPECT_FALSE(provider.IsAccessAllowed(
140 CreateFileSystemURL(extension, "removable/foo", mount_points.get())));
141
142 provider.GrantFileAccessToExtension(extension,
143 base::FilePath(FPL("removable/foo")));
144 EXPECT_TRUE(provider.IsAccessAllowed(
145 CreateFileSystemURL(extension, "removable/foo", mount_points.get())));
146 EXPECT_FALSE(provider.IsAccessAllowed(
147 CreateFileSystemURL(extension, "removable/foo1", mount_points.get())));
148
149 // System mount point access.
150 EXPECT_FALSE(provider.IsAccessAllowed(
151 CreateFileSystemURL(extension, "system/foo", system_mount_points.get())));
152
153 provider.GrantFileAccessToExtension(extension,
154 base::FilePath(FPL("system/foo")));
155 EXPECT_TRUE(provider.IsAccessAllowed(
156 CreateFileSystemURL(extension, "system/foo", system_mount_points.get())));
157 EXPECT_FALSE(provider.IsAccessAllowed(
158 CreateFileSystemURL(extension, "system/foo1",
159 system_mount_points.get())));
160
161 // oem is restricted file system.
162 provider.GrantFileAccessToExtension(
163 extension, base::FilePath(FPL("oem/foo")));
164 // The extension should not be able to access the file even if
165 // GrantFileAccessToExtension was called.
166 EXPECT_FALSE(provider.IsAccessAllowed(
167 CreateFileSystemURL(extension, "oem/foo", mount_points.get())));
168
169 provider.GrantFullAccessToExtension(extension);
170 // The extension should be able to access restricted file system after it was
171 // granted full access.
172 EXPECT_TRUE(provider.IsAccessAllowed(
173 CreateFileSystemURL(extension, "oem/foo", mount_points.get())));
174 // The extension which was granted full access should be able to access any
175 // path on current file systems.
176 EXPECT_TRUE(provider.IsAccessAllowed(
177 CreateFileSystemURL(extension, "removable/foo1", mount_points.get())));
178 EXPECT_TRUE(provider.IsAccessAllowed(
179 CreateFileSystemURL(extension, "system/foo1",
180 system_mount_points.get())));
181
182 // The extension cannot access new mount points.
183 // TODO(tbarzic): This should probably be changed.
184 ASSERT_TRUE(mount_points->RegisterFileSystem(
185 "test",
186 fileapi::kFileSystemTypeNativeLocal,
187 base::FilePath(FPL("/foo/test"))));
188 EXPECT_FALSE(provider.IsAccessAllowed(
189 CreateFileSystemURL(extension, "test_/foo", mount_points.get())));
190
191 provider.RevokeAccessForExtension(extension);
192 EXPECT_FALSE(provider.IsAccessAllowed(
193 CreateFileSystemURL(extension, "removable/foo", mount_points.get())));
194
195 fileapi::FileSystemURL internal_url = FileSystemURL::CreateForTest(
196 GURL("chrome://foo"),
197 fileapi::kFileSystemTypeExternal,
198 base::FilePath(FPL("removable/")));
199 // Internal WebUI should have full access.
200 EXPECT_TRUE(provider.IsAccessAllowed(internal_url));
201 }
202
203 TEST(CrosMountPointProvider, GetVirtualPathConflictWithSystemPoints) {
204 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy =
205 new quota::MockSpecialStoragePolicy();
206 scoped_refptr<fileapi::ExternalMountPoints> mount_points(
207 fileapi::ExternalMountPoints::CreateRefCounted());
208 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points(
209 fileapi::ExternalMountPoints::CreateRefCounted());
210 chromeos::CrosMountPointProvider provider(
211 NULL, // drive_delegate
212 storage_policy,
213 mount_points.get(),
214 system_mount_points.get());
215
216 const fileapi::FileSystemType type = fileapi::kFileSystemTypeNativeLocal;
217
218 // Provider specific mount points.
219 ASSERT_TRUE(
220 mount_points->RegisterFileSystem("b", type, base::FilePath(FPL("/a/b"))));
221 ASSERT_TRUE(
222 mount_points->RegisterFileSystem("y", type, base::FilePath(FPL("/z/y"))));
223 ASSERT_TRUE(
224 mount_points->RegisterFileSystem("n", type, base::FilePath(FPL("/m/n"))));
225
226 // System mount points
227 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
228 "gb", type, base::FilePath(FPL("/a/b"))));
229 ASSERT_TRUE(
230 system_mount_points->RegisterFileSystem(
231 "gz", type, base::FilePath(FPL("/z"))));
232 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
233 "gp", type, base::FilePath(FPL("/m/n/o/p"))));
234
235 struct TestCase {
236 const base::FilePath::CharType* const local_path;
237 bool success;
238 const base::FilePath::CharType* const virtual_path;
239 };
240
241 const TestCase kTestCases[] = {
242 // Same paths in both mount points.
243 { FPL("/a/b/c/d"), true, FPL("b/c/d") },
244 // System mount points path more specific.
245 { FPL("/m/n/o/p/r/s"), true, FPL("n/o/p/r/s") },
246 // System mount points path less specific.
247 { FPL("/z/y/x"), true, FPL("y/x") },
248 // Only system mount points path matches.
249 { FPL("/z/q/r/s"), true, FPL("gz/q/r/s") },
250 // No match.
251 { FPL("/foo/xxx"), false, FPL("") },
252 };
253
254 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
255 // Initialize virtual path with a value.
256 base::FilePath virtual_path(FPL("/mount"));
257 base::FilePath local_path(kTestCases[i].local_path);
258 EXPECT_EQ(kTestCases[i].success,
259 provider.GetVirtualPath(local_path, &virtual_path))
260 << "Resolving " << kTestCases[i].local_path;
261
262 // There are no guarantees for |virtual_path| value if |GetVirtualPath|
263 // fails.
264 if (!kTestCases[i].success)
265 continue;
266
267 base::FilePath expected_virtual_path(kTestCases[i].virtual_path);
268 EXPECT_EQ(expected_virtual_path, virtual_path)
269 << "Resolving " << kTestCases[i].local_path;
270 }
271 }
272
273 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698