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

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

Powered by Google App Engine
This is Rietveld 408576698