Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / chromeos / fileapi / file_system_backend_unittest.cc
blob8173fadd87abfd86435bbde3affceccc1cd8c173
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"
7 #include <set>
9 #include "base/files/file_path.h"
10 #include "chromeos/dbus/cros_disks_client.h"
11 #include "content/public/test/mock_special_storage_policy.h"
12 #include "storage/browser/fileapi/external_mount_points.h"
13 #include "storage/browser/fileapi/file_system_url.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/url_util.h"
17 #define FPL(x) FILE_PATH_LITERAL(x)
19 using storage::ExternalMountPoints;
20 using storage::FileSystemURL;
22 namespace {
24 FileSystemURL CreateFileSystemURL(const std::string& extension,
25 const char* path,
26 ExternalMountPoints* mount_points) {
27 return mount_points->CreateCrackedFileSystemURL(
28 GURL("chrome-extension://" + extension + "/"),
29 storage::kFileSystemTypeExternal,
30 base::FilePath::FromUTF8Unsafe(path));
33 TEST(ChromeOSFileSystemBackendTest, DefaultMountPoints) {
34 // Make sure no system-level mount points are registered before testing
35 // to avoid flakiness.
36 storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems();
38 scoped_refptr<storage::SpecialStoragePolicy> storage_policy =
39 new content::MockSpecialStoragePolicy();
40 scoped_refptr<storage::ExternalMountPoints> mount_points(
41 storage::ExternalMountPoints::CreateRefCounted());
42 chromeos::FileSystemBackend backend(
43 NULL, // drive_delegate
44 NULL, // file_system_provider_delegate
45 NULL, // mtp_delegate
46 storage_policy,
47 mount_points.get(),
48 storage::ExternalMountPoints::GetSystemInstance());
49 backend.AddSystemMountPoints();
50 std::vector<base::FilePath> root_dirs = backend.GetRootDirectories();
51 std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end());
53 // By default there should be 3 mount points (in system mount points):
54 EXPECT_EQ(3u, root_dirs.size());
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"))));
63 TEST(ChromeOSFileSystemBackendTest, GetRootDirectories) {
64 scoped_refptr<storage::SpecialStoragePolicy> storage_policy =
65 new content::MockSpecialStoragePolicy();
66 scoped_refptr<storage::ExternalMountPoints> mount_points(
67 storage::ExternalMountPoints::CreateRefCounted());
69 scoped_refptr<storage::ExternalMountPoints> system_mount_points(
70 storage::ExternalMountPoints::CreateRefCounted());
72 chromeos::FileSystemBackend backend(NULL, // drive_delegate
73 NULL, // file_system_provider_delegate
74 NULL, // mtp_delegate
75 storage_policy,
76 mount_points.get(),
77 system_mount_points.get());
79 const size_t initial_root_dirs_size = backend.GetRootDirectories().size();
81 // Register 'local' test mount points.
82 mount_points->RegisterFileSystem("c",
83 storage::kFileSystemTypeNativeLocal,
84 storage::FileSystemMountOption(),
85 base::FilePath(FPL("/a/b/c")));
86 mount_points->RegisterFileSystem("d",
87 storage::kFileSystemTypeNativeLocal,
88 storage::FileSystemMountOption(),
89 base::FilePath(FPL("/b/c/d")));
91 // Register system test mount points.
92 system_mount_points->RegisterFileSystem("d",
93 storage::kFileSystemTypeNativeLocal,
94 storage::FileSystemMountOption(),
95 base::FilePath(FPL("/g/c/d")));
96 system_mount_points->RegisterFileSystem("e",
97 storage::kFileSystemTypeNativeLocal,
98 storage::FileSystemMountOption(),
99 base::FilePath(FPL("/g/d/e")));
101 std::vector<base::FilePath> root_dirs = backend.GetRootDirectories();
102 std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end());
103 EXPECT_EQ(initial_root_dirs_size + 4, root_dirs.size());
104 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/a/b/c"))));
105 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/b/c/d"))));
106 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/c/d"))));
107 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/d/e"))));
110 TEST(ChromeOSFileSystemBackendTest, AccessPermissions) {
111 url::AddStandardScheme("chrome-extension");
113 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
114 new content::MockSpecialStoragePolicy();
115 scoped_refptr<storage::ExternalMountPoints> mount_points(
116 storage::ExternalMountPoints::CreateRefCounted());
117 scoped_refptr<storage::ExternalMountPoints> system_mount_points(
118 storage::ExternalMountPoints::CreateRefCounted());
119 chromeos::FileSystemBackend backend(NULL, // drive_delegate
120 NULL, // file_system_provider_delegate
121 NULL, // mtp_delegate
122 storage_policy,
123 mount_points.get(),
124 system_mount_points.get());
126 std::string extension("ddammdhioacbehjngdmkjcjbnfginlla");
128 storage_policy->AddFileHandler(extension);
130 // Initialize mount points.
131 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
132 "system",
133 storage::kFileSystemTypeNativeLocal,
134 storage::FileSystemMountOption(),
135 base::FilePath(FPL("/g/system"))));
136 ASSERT_TRUE(mount_points->RegisterFileSystem(
137 "removable",
138 storage::kFileSystemTypeNativeLocal,
139 storage::FileSystemMountOption(),
140 base::FilePath(FPL("/media/removable"))));
141 ASSERT_TRUE(mount_points->RegisterFileSystem(
142 "oem",
143 storage::kFileSystemTypeRestrictedNativeLocal,
144 storage::FileSystemMountOption(),
145 base::FilePath(FPL("/usr/share/oem"))));
147 // Backend specific mount point access.
148 EXPECT_FALSE(backend.IsAccessAllowed(
149 CreateFileSystemURL(extension, "removable/foo", mount_points.get())));
151 backend.GrantFileAccessToExtension(extension,
152 base::FilePath(FPL("removable/foo")));
153 EXPECT_TRUE(backend.IsAccessAllowed(
154 CreateFileSystemURL(extension, "removable/foo", mount_points.get())));
155 EXPECT_FALSE(backend.IsAccessAllowed(
156 CreateFileSystemURL(extension, "removable/foo1", mount_points.get())));
158 // System mount point access.
159 EXPECT_FALSE(backend.IsAccessAllowed(
160 CreateFileSystemURL(extension, "system/foo", system_mount_points.get())));
162 backend.GrantFileAccessToExtension(extension,
163 base::FilePath(FPL("system/foo")));
164 EXPECT_TRUE(backend.IsAccessAllowed(
165 CreateFileSystemURL(extension, "system/foo", system_mount_points.get())));
166 EXPECT_FALSE(backend.IsAccessAllowed(
167 CreateFileSystemURL(extension, "system/foo1",
168 system_mount_points.get())));
170 // oem is restricted file system.
171 backend.GrantFileAccessToExtension(
172 extension, base::FilePath(FPL("oem/foo")));
173 // The extension should not be able to access the file even if
174 // GrantFileAccessToExtension was called.
175 EXPECT_FALSE(backend.IsAccessAllowed(
176 CreateFileSystemURL(extension, "oem/foo", mount_points.get())));
178 backend.GrantFullAccessToExtension(extension);
179 // The extension should be able to access restricted file system after it was
180 // granted full access.
181 EXPECT_TRUE(backend.IsAccessAllowed(
182 CreateFileSystemURL(extension, "oem/foo", mount_points.get())));
183 // The extension which was granted full access should be able to access any
184 // path on current file systems.
185 EXPECT_TRUE(backend.IsAccessAllowed(
186 CreateFileSystemURL(extension, "removable/foo1", mount_points.get())));
187 EXPECT_TRUE(backend.IsAccessAllowed(
188 CreateFileSystemURL(extension, "system/foo1",
189 system_mount_points.get())));
191 // The extension cannot access new mount points.
192 // TODO(tbarzic): This should probably be changed.
193 ASSERT_TRUE(
194 mount_points->RegisterFileSystem("test",
195 storage::kFileSystemTypeNativeLocal,
196 storage::FileSystemMountOption(),
197 base::FilePath(FPL("/foo/test"))));
198 EXPECT_FALSE(backend.IsAccessAllowed(
199 CreateFileSystemURL(extension, "test_/foo", mount_points.get())));
201 backend.RevokeAccessForExtension(extension);
202 EXPECT_FALSE(backend.IsAccessAllowed(
203 CreateFileSystemURL(extension, "removable/foo", mount_points.get())));
206 TEST(ChromeOSFileSystemBackendTest, GetVirtualPathConflictWithSystemPoints) {
207 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
208 new content::MockSpecialStoragePolicy();
209 scoped_refptr<storage::ExternalMountPoints> mount_points(
210 storage::ExternalMountPoints::CreateRefCounted());
211 scoped_refptr<storage::ExternalMountPoints> system_mount_points(
212 storage::ExternalMountPoints::CreateRefCounted());
213 chromeos::FileSystemBackend backend(NULL, // drive_delegate
214 NULL, // file_system_provider_delegate
215 NULL, // mtp_delegate
216 storage_policy,
217 mount_points.get(),
218 system_mount_points.get());
220 const storage::FileSystemType type = storage::kFileSystemTypeNativeLocal;
221 const storage::FileSystemMountOption option =
222 storage::FileSystemMountOption();
224 // Backend specific mount points.
225 ASSERT_TRUE(mount_points->RegisterFileSystem(
226 "b", type, option, base::FilePath(FPL("/a/b"))));
227 ASSERT_TRUE(mount_points->RegisterFileSystem(
228 "y", type, option, base::FilePath(FPL("/z/y"))));
229 ASSERT_TRUE(mount_points->RegisterFileSystem(
230 "n", type, option, base::FilePath(FPL("/m/n"))));
232 // System mount points
233 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
234 "gb", type, option, base::FilePath(FPL("/a/b"))));
235 ASSERT_TRUE(
236 system_mount_points->RegisterFileSystem(
237 "gz", type, option, base::FilePath(FPL("/z"))));
238 ASSERT_TRUE(system_mount_points->RegisterFileSystem(
239 "gp", type, option, base::FilePath(FPL("/m/n/o/p"))));
241 struct TestCase {
242 const base::FilePath::CharType* const local_path;
243 bool success;
244 const base::FilePath::CharType* const virtual_path;
247 const TestCase kTestCases[] = {
248 // Same paths in both mount points.
249 { FPL("/a/b/c/d"), true, FPL("b/c/d") },
250 // System mount points path more specific.
251 { FPL("/m/n/o/p/r/s"), true, FPL("n/o/p/r/s") },
252 // System mount points path less specific.
253 { FPL("/z/y/x"), true, FPL("y/x") },
254 // Only system mount points path matches.
255 { FPL("/z/q/r/s"), true, FPL("gz/q/r/s") },
256 // No match.
257 { FPL("/foo/xxx"), false, FPL("") },
260 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
261 // Initialize virtual path with a value.
262 base::FilePath virtual_path(FPL("/mount"));
263 base::FilePath local_path(kTestCases[i].local_path);
264 EXPECT_EQ(kTestCases[i].success,
265 backend.GetVirtualPath(local_path, &virtual_path))
266 << "Resolving " << kTestCases[i].local_path;
268 // There are no guarantees for |virtual_path| value if |GetVirtualPath|
269 // fails.
270 if (!kTestCases[i].success)
271 continue;
273 base::FilePath expected_virtual_path(kTestCases[i].virtual_path);
274 EXPECT_EQ(expected_virtual_path, virtual_path)
275 << "Resolving " << kTestCases[i].local_path;
279 } // namespace