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.
5 #include "chrome/browser/chromeos/fileapi/file_system_backend.h"
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_system_url.h"
16 #include "webkit/browser/quota/mock_special_storage_policy.h"
18 #define FPL(x) FILE_PATH_LITERAL(x)
20 using fileapi::ExternalMountPoints
;
21 using fileapi::FileSystemURL
;
25 FileSystemURL
CreateFileSystemURL(const std::string
& extension
,
27 ExternalMountPoints
* mount_points
) {
28 return mount_points
->CreateCrackedFileSystemURL(
29 GURL("chrome-extension://" + extension
+ "/"),
30 fileapi::kFileSystemTypeExternal
,
31 base::FilePath::FromUTF8Unsafe(path
));
34 TEST(ChromeOSFileSystemBackendTest
, DefaultMountPoints
) {
35 // Make sure no system-level mount points are registered before testing
36 // to avoid flakiness.
37 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems();
39 scoped_refptr
<quota::SpecialStoragePolicy
> storage_policy
=
40 new quota::MockSpecialStoragePolicy();
41 scoped_refptr
<fileapi::ExternalMountPoints
> mount_points(
42 fileapi::ExternalMountPoints::CreateRefCounted());
43 chromeos::FileSystemBackend
backend(
44 NULL
, // drive_delegate
47 fileapi::ExternalMountPoints::GetSystemInstance());
48 backend
.AddSystemMountPoints();
49 std::vector
<base::FilePath
> root_dirs
= backend
.GetRootDirectories();
50 std::set
<base::FilePath
> root_dirs_set(root_dirs
.begin(), root_dirs
.end());
52 // By default there should be 3 mount points (in system mount points):
53 EXPECT_EQ(3u, root_dirs
.size());
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"))));
62 TEST(ChromeOSFileSystemBackendTest
, GetRootDirectories
) {
63 scoped_refptr
<quota::SpecialStoragePolicy
> storage_policy
=
64 new quota::MockSpecialStoragePolicy();
65 scoped_refptr
<fileapi::ExternalMountPoints
> mount_points(
66 fileapi::ExternalMountPoints::CreateRefCounted());
68 scoped_refptr
<fileapi::ExternalMountPoints
> system_mount_points(
69 fileapi::ExternalMountPoints::CreateRefCounted());
71 chromeos::FileSystemBackend
backend(
72 NULL
, // drive_delegate
75 system_mount_points
.get());
77 const size_t initial_root_dirs_size
= backend
.GetRootDirectories().size();
79 // Register 'local' test mount points.
80 mount_points
->RegisterFileSystem("c",
81 fileapi::kFileSystemTypeNativeLocal
,
82 fileapi::FileSystemMountOption(),
83 base::FilePath(FPL("/a/b/c")));
84 mount_points
->RegisterFileSystem("d",
85 fileapi::kFileSystemTypeNativeLocal
,
86 fileapi::FileSystemMountOption(),
87 base::FilePath(FPL("/b/c/d")));
89 // Register system test mount points.
90 system_mount_points
->RegisterFileSystem("d",
91 fileapi::kFileSystemTypeNativeLocal
,
92 fileapi::FileSystemMountOption(),
93 base::FilePath(FPL("/g/c/d")));
94 system_mount_points
->RegisterFileSystem("e",
95 fileapi::kFileSystemTypeNativeLocal
,
96 fileapi::FileSystemMountOption(),
97 base::FilePath(FPL("/g/d/e")));
99 std::vector
<base::FilePath
> root_dirs
= backend
.GetRootDirectories();
100 std::set
<base::FilePath
> root_dirs_set(root_dirs
.begin(), root_dirs
.end());
101 EXPECT_EQ(initial_root_dirs_size
+ 4, root_dirs
.size());
102 EXPECT_TRUE(root_dirs_set
.count(base::FilePath(FPL("/a/b/c"))));
103 EXPECT_TRUE(root_dirs_set
.count(base::FilePath(FPL("/b/c/d"))));
104 EXPECT_TRUE(root_dirs_set
.count(base::FilePath(FPL("/g/c/d"))));
105 EXPECT_TRUE(root_dirs_set
.count(base::FilePath(FPL("/g/d/e"))));
108 TEST(ChromeOSFileSystemBackendTest
, AccessPermissions
) {
109 url_util::AddStandardScheme("chrome-extension");
111 scoped_refptr
<quota::MockSpecialStoragePolicy
> storage_policy
=
112 new quota::MockSpecialStoragePolicy();
113 scoped_refptr
<fileapi::ExternalMountPoints
> mount_points(
114 fileapi::ExternalMountPoints::CreateRefCounted());
115 scoped_refptr
<fileapi::ExternalMountPoints
> system_mount_points(
116 fileapi::ExternalMountPoints::CreateRefCounted());
117 chromeos::FileSystemBackend
backend(
118 NULL
, // drive_delegate
121 system_mount_points
.get());
123 std::string
extension("ddammdhioacbehjngdmkjcjbnfginlla");
125 storage_policy
->AddFileHandler(extension
);
127 // Initialize mount points.
128 ASSERT_TRUE(system_mount_points
->RegisterFileSystem(
130 fileapi::kFileSystemTypeNativeLocal
,
131 fileapi::FileSystemMountOption(),
132 base::FilePath(FPL("/g/system"))));
133 ASSERT_TRUE(mount_points
->RegisterFileSystem(
135 fileapi::kFileSystemTypeNativeLocal
,
136 fileapi::FileSystemMountOption(),
137 base::FilePath(FPL("/media/removable"))));
138 ASSERT_TRUE(mount_points
->RegisterFileSystem(
140 fileapi::kFileSystemTypeRestrictedNativeLocal
,
141 fileapi::FileSystemMountOption(),
142 base::FilePath(FPL("/usr/share/oem"))));
144 // Backend specific mount point access.
145 EXPECT_FALSE(backend
.IsAccessAllowed(
146 CreateFileSystemURL(extension
, "removable/foo", mount_points
.get())));
148 backend
.GrantFileAccessToExtension(extension
,
149 base::FilePath(FPL("removable/foo")));
150 EXPECT_TRUE(backend
.IsAccessAllowed(
151 CreateFileSystemURL(extension
, "removable/foo", mount_points
.get())));
152 EXPECT_FALSE(backend
.IsAccessAllowed(
153 CreateFileSystemURL(extension
, "removable/foo1", mount_points
.get())));
155 // System mount point access.
156 EXPECT_FALSE(backend
.IsAccessAllowed(
157 CreateFileSystemURL(extension
, "system/foo", system_mount_points
.get())));
159 backend
.GrantFileAccessToExtension(extension
,
160 base::FilePath(FPL("system/foo")));
161 EXPECT_TRUE(backend
.IsAccessAllowed(
162 CreateFileSystemURL(extension
, "system/foo", system_mount_points
.get())));
163 EXPECT_FALSE(backend
.IsAccessAllowed(
164 CreateFileSystemURL(extension
, "system/foo1",
165 system_mount_points
.get())));
167 // oem is restricted file system.
168 backend
.GrantFileAccessToExtension(
169 extension
, base::FilePath(FPL("oem/foo")));
170 // The extension should not be able to access the file even if
171 // GrantFileAccessToExtension was called.
172 EXPECT_FALSE(backend
.IsAccessAllowed(
173 CreateFileSystemURL(extension
, "oem/foo", mount_points
.get())));
175 backend
.GrantFullAccessToExtension(extension
);
176 // The extension should be able to access restricted file system after it was
177 // granted full access.
178 EXPECT_TRUE(backend
.IsAccessAllowed(
179 CreateFileSystemURL(extension
, "oem/foo", mount_points
.get())));
180 // The extension which was granted full access should be able to access any
181 // path on current file systems.
182 EXPECT_TRUE(backend
.IsAccessAllowed(
183 CreateFileSystemURL(extension
, "removable/foo1", mount_points
.get())));
184 EXPECT_TRUE(backend
.IsAccessAllowed(
185 CreateFileSystemURL(extension
, "system/foo1",
186 system_mount_points
.get())));
188 // The extension cannot access new mount points.
189 // TODO(tbarzic): This should probably be changed.
190 ASSERT_TRUE(mount_points
->RegisterFileSystem(
192 fileapi::kFileSystemTypeNativeLocal
,
193 fileapi::FileSystemMountOption(),
194 base::FilePath(FPL("/foo/test"))));
195 EXPECT_FALSE(backend
.IsAccessAllowed(
196 CreateFileSystemURL(extension
, "test_/foo", mount_points
.get())));
198 backend
.RevokeAccessForExtension(extension
);
199 EXPECT_FALSE(backend
.IsAccessAllowed(
200 CreateFileSystemURL(extension
, "removable/foo", mount_points
.get())));
202 fileapi::FileSystemURL internal_url
= FileSystemURL::CreateForTest(
203 GURL("chrome://foo"),
204 fileapi::kFileSystemTypeExternal
,
205 base::FilePath(FPL("removable/")));
206 // Internal WebUI should have full access.
207 EXPECT_TRUE(backend
.IsAccessAllowed(internal_url
));
210 TEST(ChromeOSFileSystemBackendTest
, GetVirtualPathConflictWithSystemPoints
) {
211 scoped_refptr
<quota::MockSpecialStoragePolicy
> storage_policy
=
212 new quota::MockSpecialStoragePolicy();
213 scoped_refptr
<fileapi::ExternalMountPoints
> mount_points(
214 fileapi::ExternalMountPoints::CreateRefCounted());
215 scoped_refptr
<fileapi::ExternalMountPoints
> system_mount_points(
216 fileapi::ExternalMountPoints::CreateRefCounted());
217 chromeos::FileSystemBackend
backend(
218 NULL
, // drive_delegate
221 system_mount_points
.get());
223 const fileapi::FileSystemType type
= fileapi::kFileSystemTypeNativeLocal
;
224 const fileapi::FileSystemMountOption option
=
225 fileapi::FileSystemMountOption();
227 // Backend specific mount points.
228 ASSERT_TRUE(mount_points
->RegisterFileSystem(
229 "b", type
, option
, base::FilePath(FPL("/a/b"))));
230 ASSERT_TRUE(mount_points
->RegisterFileSystem(
231 "y", type
, option
, base::FilePath(FPL("/z/y"))));
232 ASSERT_TRUE(mount_points
->RegisterFileSystem(
233 "n", type
, option
, base::FilePath(FPL("/m/n"))));
235 // System mount points
236 ASSERT_TRUE(system_mount_points
->RegisterFileSystem(
237 "gb", type
, option
, base::FilePath(FPL("/a/b"))));
239 system_mount_points
->RegisterFileSystem(
240 "gz", type
, option
, base::FilePath(FPL("/z"))));
241 ASSERT_TRUE(system_mount_points
->RegisterFileSystem(
242 "gp", type
, option
, base::FilePath(FPL("/m/n/o/p"))));
245 const base::FilePath::CharType
* const local_path
;
247 const base::FilePath::CharType
* const virtual_path
;
250 const TestCase kTestCases
[] = {
251 // Same paths in both mount points.
252 { FPL("/a/b/c/d"), true, FPL("b/c/d") },
253 // System mount points path more specific.
254 { FPL("/m/n/o/p/r/s"), true, FPL("n/o/p/r/s") },
255 // System mount points path less specific.
256 { FPL("/z/y/x"), true, FPL("y/x") },
257 // Only system mount points path matches.
258 { FPL("/z/q/r/s"), true, FPL("gz/q/r/s") },
260 { FPL("/foo/xxx"), false, FPL("") },
263 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
264 // Initialize virtual path with a value.
265 base::FilePath
virtual_path(FPL("/mount"));
266 base::FilePath
local_path(kTestCases
[i
].local_path
);
267 EXPECT_EQ(kTestCases
[i
].success
,
268 backend
.GetVirtualPath(local_path
, &virtual_path
))
269 << "Resolving " << kTestCases
[i
].local_path
;
271 // There are no guarantees for |virtual_path| value if |GetVirtualPath|
273 if (!kTestCases
[i
].success
)
276 base::FilePath
expected_virtual_path(kTestCases
[i
].virtual_path
);
277 EXPECT_EQ(expected_virtual_path
, virtual_path
)
278 << "Resolving " << kTestCases
[i
].local_path
;