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 "chromeos/dbus/cros_disks_client.h"
11 #include "storage/browser/fileapi/external_mount_points.h"
12 #include "storage/browser/fileapi/file_system_url.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "url/url_util.h"
16 #define FPL(x) FILE_PATH_LITERAL(x)
18 using storage::ExternalMountPoints
;
19 using storage::FileSystemURL
;
23 FileSystemURL
CreateFileSystemURL(const std::string
& extension
,
25 ExternalMountPoints
* mount_points
) {
26 return mount_points
->CreateCrackedFileSystemURL(
27 GURL("chrome-extension://" + extension
+ "/"),
28 storage::kFileSystemTypeExternal
,
29 base::FilePath::FromUTF8Unsafe(path
));
32 TEST(ChromeOSFileSystemBackendTest
, DefaultMountPoints
) {
33 // Make sure no system-level mount points are registered before testing
34 // to avoid flakiness.
35 storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems();
37 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
38 storage::ExternalMountPoints::CreateRefCounted());
39 chromeos::FileSystemBackend
backend(
40 NULL
, // drive_delegate
41 NULL
, // file_system_provider_delegate
44 storage::ExternalMountPoints::GetSystemInstance());
45 backend
.AddSystemMountPoints();
46 std::vector
<base::FilePath
> root_dirs
= backend
.GetRootDirectories();
47 std::set
<base::FilePath
> root_dirs_set(root_dirs
.begin(), root_dirs
.end());
49 // By default there should be 3 mount points (in system mount points):
50 EXPECT_EQ(3u, root_dirs
.size());
52 EXPECT_TRUE(root_dirs_set
.count(
53 chromeos::CrosDisksClient::GetRemovableDiskMountPoint()));
54 EXPECT_TRUE(root_dirs_set
.count(
55 chromeos::CrosDisksClient::GetArchiveMountPoint()));
56 EXPECT_TRUE(root_dirs_set
.count(base::FilePath(FPL("/usr/share/oem"))));
59 TEST(ChromeOSFileSystemBackendTest
, GetRootDirectories
) {
60 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
61 storage::ExternalMountPoints::CreateRefCounted());
63 scoped_refptr
<storage::ExternalMountPoints
> system_mount_points(
64 storage::ExternalMountPoints::CreateRefCounted());
66 chromeos::FileSystemBackend
backend(NULL
, // drive_delegate
67 NULL
, // file_system_provider_delegate
70 system_mount_points
.get());
72 const size_t initial_root_dirs_size
= backend
.GetRootDirectories().size();
74 // Register 'local' test mount points.
75 mount_points
->RegisterFileSystem("c",
76 storage::kFileSystemTypeNativeLocal
,
77 storage::FileSystemMountOption(),
78 base::FilePath(FPL("/a/b/c")));
79 mount_points
->RegisterFileSystem("d",
80 storage::kFileSystemTypeNativeLocal
,
81 storage::FileSystemMountOption(),
82 base::FilePath(FPL("/b/c/d")));
84 // Register system test mount points.
85 system_mount_points
->RegisterFileSystem("d",
86 storage::kFileSystemTypeNativeLocal
,
87 storage::FileSystemMountOption(),
88 base::FilePath(FPL("/g/c/d")));
89 system_mount_points
->RegisterFileSystem("e",
90 storage::kFileSystemTypeNativeLocal
,
91 storage::FileSystemMountOption(),
92 base::FilePath(FPL("/g/d/e")));
94 std::vector
<base::FilePath
> root_dirs
= backend
.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"))));
103 TEST(ChromeOSFileSystemBackendTest
, AccessPermissions
) {
104 url::AddStandardScheme("chrome-extension", url::SCHEME_WITHOUT_PORT
);
106 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
107 storage::ExternalMountPoints::CreateRefCounted());
108 scoped_refptr
<storage::ExternalMountPoints
> system_mount_points(
109 storage::ExternalMountPoints::CreateRefCounted());
110 chromeos::FileSystemBackend
backend(NULL
, // drive_delegate
111 NULL
, // file_system_provider_delegate
112 NULL
, // mtp_delegate
114 system_mount_points
.get());
116 std::string
extension("ddammdhioacbehjngdmkjcjbnfginlla");
118 // Initialize mount points.
119 ASSERT_TRUE(system_mount_points
->RegisterFileSystem(
121 storage::kFileSystemTypeNativeLocal
,
122 storage::FileSystemMountOption(),
123 base::FilePath(FPL("/g/system"))));
124 ASSERT_TRUE(mount_points
->RegisterFileSystem(
126 storage::kFileSystemTypeNativeLocal
,
127 storage::FileSystemMountOption(),
128 base::FilePath(FPL("/media/removable"))));
129 ASSERT_TRUE(mount_points
->RegisterFileSystem(
131 storage::kFileSystemTypeRestrictedNativeLocal
,
132 storage::FileSystemMountOption(),
133 base::FilePath(FPL("/usr/share/oem"))));
135 // Backend specific mount point access.
136 EXPECT_FALSE(backend
.IsAccessAllowed(
137 CreateFileSystemURL(extension
, "removable/foo", mount_points
.get())));
139 backend
.GrantFileAccessToExtension(extension
,
140 base::FilePath(FPL("removable/foo")));
141 EXPECT_TRUE(backend
.IsAccessAllowed(
142 CreateFileSystemURL(extension
, "removable/foo", mount_points
.get())));
143 EXPECT_FALSE(backend
.IsAccessAllowed(
144 CreateFileSystemURL(extension
, "removable/foo1", mount_points
.get())));
146 // System mount point access.
147 EXPECT_FALSE(backend
.IsAccessAllowed(
148 CreateFileSystemURL(extension
, "system/foo", system_mount_points
.get())));
150 backend
.GrantFileAccessToExtension(extension
,
151 base::FilePath(FPL("system/foo")));
152 EXPECT_TRUE(backend
.IsAccessAllowed(
153 CreateFileSystemURL(extension
, "system/foo", system_mount_points
.get())));
154 EXPECT_FALSE(backend
.IsAccessAllowed(
155 CreateFileSystemURL(extension
, "system/foo1",
156 system_mount_points
.get())));
158 // The extension cannot access new mount points.
159 // TODO(tbarzic): This should probably be changed.
161 mount_points
->RegisterFileSystem("test",
162 storage::kFileSystemTypeNativeLocal
,
163 storage::FileSystemMountOption(),
164 base::FilePath(FPL("/foo/test"))));
165 EXPECT_FALSE(backend
.IsAccessAllowed(
166 CreateFileSystemURL(extension
, "test_/foo", mount_points
.get())));
168 backend
.RevokeAccessForExtension(extension
);
169 EXPECT_FALSE(backend
.IsAccessAllowed(
170 CreateFileSystemURL(extension
, "removable/foo", mount_points
.get())));
173 TEST(ChromeOSFileSystemBackendTest
, GetVirtualPathConflictWithSystemPoints
) {
174 scoped_refptr
<storage::ExternalMountPoints
> mount_points(
175 storage::ExternalMountPoints::CreateRefCounted());
176 scoped_refptr
<storage::ExternalMountPoints
> system_mount_points(
177 storage::ExternalMountPoints::CreateRefCounted());
178 chromeos::FileSystemBackend
backend(NULL
, // drive_delegate
179 NULL
, // file_system_provider_delegate
180 NULL
, // mtp_delegate
182 system_mount_points
.get());
184 const storage::FileSystemType type
= storage::kFileSystemTypeNativeLocal
;
185 const storage::FileSystemMountOption option
=
186 storage::FileSystemMountOption();
188 // Backend specific mount points.
189 ASSERT_TRUE(mount_points
->RegisterFileSystem(
190 "b", type
, option
, base::FilePath(FPL("/a/b"))));
191 ASSERT_TRUE(mount_points
->RegisterFileSystem(
192 "y", type
, option
, base::FilePath(FPL("/z/y"))));
193 ASSERT_TRUE(mount_points
->RegisterFileSystem(
194 "n", type
, option
, base::FilePath(FPL("/m/n"))));
196 // System mount points
197 ASSERT_TRUE(system_mount_points
->RegisterFileSystem(
198 "gb", type
, option
, base::FilePath(FPL("/a/b"))));
200 system_mount_points
->RegisterFileSystem(
201 "gz", type
, option
, base::FilePath(FPL("/z"))));
202 ASSERT_TRUE(system_mount_points
->RegisterFileSystem(
203 "gp", type
, option
, base::FilePath(FPL("/m/n/o/p"))));
206 const base::FilePath::CharType
* const local_path
;
208 const base::FilePath::CharType
* const virtual_path
;
211 const TestCase kTestCases
[] = {
212 // Same paths in both mount points.
213 { FPL("/a/b/c/d"), true, FPL("b/c/d") },
214 // System mount points path more specific.
215 { FPL("/m/n/o/p/r/s"), true, FPL("n/o/p/r/s") },
216 // System mount points path less specific.
217 { FPL("/z/y/x"), true, FPL("y/x") },
218 // Only system mount points path matches.
219 { FPL("/z/q/r/s"), true, FPL("gz/q/r/s") },
221 { FPL("/foo/xxx"), false, FPL("") },
224 for (size_t i
= 0; i
< arraysize(kTestCases
); ++i
) {
225 // Initialize virtual path with a value.
226 base::FilePath
virtual_path(FPL("/mount"));
227 base::FilePath
local_path(kTestCases
[i
].local_path
);
228 EXPECT_EQ(kTestCases
[i
].success
,
229 backend
.GetVirtualPath(local_path
, &virtual_path
))
230 << "Resolving " << kTestCases
[i
].local_path
;
232 // There are no guarantees for |virtual_path| value if |GetVirtualPath|
234 if (!kTestCases
[i
].success
)
237 base::FilePath
expected_virtual_path(kTestCases
[i
].virtual_path
);
238 EXPECT_EQ(expected_virtual_path
, virtual_path
)
239 << "Resolving " << kTestCases
[i
].local_path
;