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 "webkit/browser/fileapi/file_system_context.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "content/public/test/test_file_system_options.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webkit/browser/fileapi/external_mount_points.h"
13 #include "webkit/browser/fileapi/file_system_backend.h"
14 #include "webkit/browser/fileapi/isolated_context.h"
15 #include "webkit/browser/quota/mock_quota_manager.h"
16 #include "webkit/browser/quota/mock_special_storage_policy.h"
18 #define FPL(x) FILE_PATH_LITERAL(x)
20 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
21 #define DRIVE FPL("C:")
26 using fileapi::ExternalMountPoints
;
27 using fileapi::FileSystemBackend
;
28 using fileapi::FileSystemContext
;
29 using fileapi::FileSystemMountOption
;
30 using fileapi::FileSystemType
;
31 using fileapi::FileSystemURL
;
32 using fileapi::IsolatedContext
;
38 const char kTestOrigin
[] = "http://chromium.org/";
40 GURL
CreateRawFileSystemURL(const std::string
& type_str
,
41 const std::string
& fs_id
) {
42 std::string url_str
= base::StringPrintf(
43 "filesystem:http://chromium.org/%s/%s/root/file",
49 class FileSystemContextTest
: public testing::Test
{
51 FileSystemContextTest() {}
53 virtual void SetUp() {
54 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
56 storage_policy_
= new quota::MockSpecialStoragePolicy();
59 new quota::MockQuotaManager(false /* is_incognito */,
61 base::MessageLoopProxy::current().get(),
62 base::MessageLoopProxy::current().get(),
63 storage_policy_
.get());
67 FileSystemContext
* CreateFileSystemContextForTest(
68 fileapi::ExternalMountPoints
* external_mount_points
) {
69 return new FileSystemContext(base::MessageLoopProxy::current().get(),
70 base::MessageLoopProxy::current().get(),
71 external_mount_points
,
72 storage_policy_
.get(),
73 mock_quota_manager_
->proxy(),
74 ScopedVector
<FileSystemBackend
>(),
76 CreateAllowFileAccessOptions());
79 // Verifies a *valid* filesystem url has expected values.
80 void ExpectFileSystemURLMatches(const FileSystemURL
& url
,
81 const GURL
& expect_origin
,
82 FileSystemType expect_mount_type
,
83 FileSystemType expect_type
,
84 const base::FilePath
& expect_path
,
85 const base::FilePath
& expect_virtual_path
,
86 const std::string
& expect_filesystem_id
) {
87 EXPECT_TRUE(url
.is_valid());
89 EXPECT_EQ(expect_origin
, url
.origin());
90 EXPECT_EQ(expect_mount_type
, url
.mount_type());
91 EXPECT_EQ(expect_type
, url
.type());
92 EXPECT_EQ(expect_path
, url
.path());
93 EXPECT_EQ(expect_virtual_path
, url
.virtual_path());
94 EXPECT_EQ(expect_filesystem_id
, url
.filesystem_id());
98 base::ScopedTempDir data_dir_
;
99 base::MessageLoop message_loop_
;
100 scoped_refptr
<quota::SpecialStoragePolicy
> storage_policy_
;
101 scoped_refptr
<quota::MockQuotaManager
> mock_quota_manager_
;
104 // It is not valid to pass NULL ExternalMountPoints to FileSystemContext on
106 #if !defined(OS_CHROMEOS)
107 TEST_F(FileSystemContextTest
, NullExternalMountPoints
) {
108 scoped_refptr
<FileSystemContext
> file_system_context(
109 CreateFileSystemContextForTest(NULL
));
111 // Cracking system external mount and isolated mount points should work.
112 std::string isolated_name
= "root";
113 std::string isolated_id
=
114 IsolatedContext::GetInstance()->RegisterFileSystemForPath(
115 fileapi::kFileSystemTypeNativeLocal
,
116 base::FilePath(DRIVE
FPL("/test/isolated/root")),
118 // Register system external mount point.
119 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
121 fileapi::kFileSystemTypeNativeLocal
,
122 FileSystemMountOption(),
123 base::FilePath(DRIVE
FPL("/test/sys/"))));
125 FileSystemURL cracked_isolated
= file_system_context
->CrackURL(
126 CreateRawFileSystemURL("isolated", isolated_id
));
128 ExpectFileSystemURLMatches(
131 fileapi::kFileSystemTypeIsolated
,
132 fileapi::kFileSystemTypeNativeLocal
,
134 DRIVE
FPL("/test/isolated/root/file")).NormalizePathSeparators(),
135 base::FilePath::FromUTF8Unsafe(isolated_id
).Append(FPL("root/file")).
136 NormalizePathSeparators(),
139 FileSystemURL cracked_external
= file_system_context
->CrackURL(
140 CreateRawFileSystemURL("external", "system"));
142 ExpectFileSystemURLMatches(
145 fileapi::kFileSystemTypeExternal
,
146 fileapi::kFileSystemTypeNativeLocal
,
148 DRIVE
FPL("/test/sys/root/file")).NormalizePathSeparators(),
149 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(),
153 IsolatedContext::GetInstance()->RevokeFileSystem(isolated_id
);
154 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system");
156 #endif // !defiend(OS_CHROMEOS)
158 TEST_F(FileSystemContextTest
, FileSystemContextKeepsMountPointsAlive
) {
159 scoped_refptr
<ExternalMountPoints
> mount_points
=
160 ExternalMountPoints::CreateRefCounted();
162 // Register system external mount point.
163 ASSERT_TRUE(mount_points
->RegisterFileSystem(
165 fileapi::kFileSystemTypeNativeLocal
,
166 FileSystemMountOption(),
167 base::FilePath(DRIVE
FPL("/test/sys/"))));
169 scoped_refptr
<FileSystemContext
> file_system_context(
170 CreateFileSystemContextForTest(mount_points
.get()));
172 // Release a MountPoints reference created in the test.
175 // FileSystemContext should keep a reference to the |mount_points|, so it
176 // should be able to resolve the URL.
177 FileSystemURL cracked_external
= file_system_context
->CrackURL(
178 CreateRawFileSystemURL("external", "system"));
180 ExpectFileSystemURLMatches(
183 fileapi::kFileSystemTypeExternal
,
184 fileapi::kFileSystemTypeNativeLocal
,
186 DRIVE
FPL("/test/sys/root/file")).NormalizePathSeparators(),
187 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(),
190 // No need to revoke the registered filesystem since |mount_points| lifetime
191 // is bound to this test.
194 TEST_F(FileSystemContextTest
, CrackFileSystemURL
) {
195 scoped_refptr
<ExternalMountPoints
> external_mount_points(
196 ExternalMountPoints::CreateRefCounted());
197 scoped_refptr
<FileSystemContext
> file_system_context(
198 CreateFileSystemContextForTest(external_mount_points
.get()));
200 // Register an isolated mount point.
201 std::string isolated_file_system_name
= "root";
202 const std::string kIsolatedFileSystemID
=
203 IsolatedContext::GetInstance()->RegisterFileSystemForPath(
204 fileapi::kFileSystemTypeNativeLocal
,
205 base::FilePath(DRIVE
FPL("/test/isolated/root")),
206 &isolated_file_system_name
);
207 // Register system external mount point.
208 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
210 fileapi::kFileSystemTypeDrive
,
211 FileSystemMountOption(),
212 base::FilePath(DRIVE
FPL("/test/sys/"))));
213 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
215 fileapi::kFileSystemTypeNativeLocal
,
216 FileSystemMountOption(),
217 base::FilePath(DRIVE
FPL("/test/ext"))));
218 // Register a system external mount point with the same name/id as the
219 // registered isolated mount point.
220 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
221 kIsolatedFileSystemID
,
222 fileapi::kFileSystemTypeRestrictedNativeLocal
,
223 FileSystemMountOption(),
224 base::FilePath(DRIVE
FPL("/test/system/isolated"))));
225 // Add a mount points with the same name as a system mount point to
226 // FileSystemContext's external mount points.
227 ASSERT_TRUE(external_mount_points
->RegisterFileSystem(
229 fileapi::kFileSystemTypeNativeLocal
,
230 FileSystemMountOption(),
231 base::FilePath(DRIVE
FPL("/test/local/ext/"))));
233 const GURL kTestOrigin
= GURL("http://chromium.org/");
234 const base::FilePath kVirtualPathNoRoot
= base::FilePath(FPL("root/file"));
239 std::string type_str
;
241 // Expected test results.
242 bool expect_is_valid
;
243 FileSystemType expect_mount_type
;
244 FileSystemType expect_type
;
245 const base::FilePath::CharType
* expect_path
;
246 std::string expect_filesystem_id
;
249 const TestCase kTestCases
[] = {
250 // Following should not be handled by the url crackers:
252 "pers_mount", "persistent", true /* is_valid */,
253 fileapi::kFileSystemTypePersistent
, fileapi::kFileSystemTypePersistent
,
254 FPL("pers_mount/root/file"),
255 std::string() /* filesystem id */
258 "temp_mount", "temporary", true /* is_valid */,
259 fileapi::kFileSystemTypeTemporary
, fileapi::kFileSystemTypeTemporary
,
260 FPL("temp_mount/root/file"),
261 std::string() /* filesystem id */
263 // Should be cracked by isolated mount points:
265 kIsolatedFileSystemID
, "isolated", true /* is_valid */,
266 fileapi::kFileSystemTypeIsolated
, fileapi::kFileSystemTypeNativeLocal
,
267 DRIVE
FPL("/test/isolated/root/file"),
268 kIsolatedFileSystemID
270 // Should be cracked by system mount points:
272 "system", "external", true /* is_valid */,
273 fileapi::kFileSystemTypeExternal
, fileapi::kFileSystemTypeDrive
,
274 DRIVE
FPL("/test/sys/root/file"),
278 kIsolatedFileSystemID
, "external", true /* is_valid */,
279 fileapi::kFileSystemTypeExternal
,
280 fileapi::kFileSystemTypeRestrictedNativeLocal
,
281 DRIVE
FPL("/test/system/isolated/root/file"),
282 kIsolatedFileSystemID
284 // Should be cracked by FileSystemContext's ExternalMountPoints.
286 "ext", "external", true /* is_valid */,
287 fileapi::kFileSystemTypeExternal
, fileapi::kFileSystemTypeNativeLocal
,
288 DRIVE
FPL("/test/local/ext/root/file"),
291 // Test for invalid filesystem url (made invalid by adding invalid
294 "sytem", "external", false /* is_valid */,
295 // The rest of values will be ignored.
296 fileapi::kFileSystemTypeUnknown
, fileapi::kFileSystemTypeUnknown
,
297 FPL(""), std::string()
299 // Test for URL with non-existing filesystem id.
301 "invalid", "external", false /* is_valid */,
302 // The rest of values will be ignored.
303 fileapi::kFileSystemTypeUnknown
, fileapi::kFileSystemTypeUnknown
,
304 FPL(""), std::string()
308 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
309 const base::FilePath virtual_path
=
310 base::FilePath::FromUTF8Unsafe(
311 kTestCases
[i
].root
).Append(kVirtualPathNoRoot
);
314 CreateRawFileSystemURL(kTestCases
[i
].type_str
, kTestCases
[i
].root
);
315 FileSystemURL cracked_url
= file_system_context
->CrackURL(raw_url
);
317 SCOPED_TRACE(testing::Message() << "Test case " << i
<< ": "
318 << "Cracking URL: " << raw_url
);
320 EXPECT_EQ(kTestCases
[i
].expect_is_valid
, cracked_url
.is_valid());
321 if (!kTestCases
[i
].expect_is_valid
)
324 ExpectFileSystemURLMatches(
327 kTestCases
[i
].expect_mount_type
,
328 kTestCases
[i
].expect_type
,
329 base::FilePath(kTestCases
[i
].expect_path
).NormalizePathSeparators(),
330 virtual_path
.NormalizePathSeparators(),
331 kTestCases
[i
].expect_filesystem_id
);
334 IsolatedContext::GetInstance()->RevokeFileSystemByPath(
335 base::FilePath(DRIVE
FPL("/test/isolated/root")));
336 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system");
337 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("ext");
338 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
339 kIsolatedFileSystemID
);
342 TEST_F(FileSystemContextTest
, CanServeURLRequest
) {
343 scoped_refptr
<ExternalMountPoints
> external_mount_points(
344 ExternalMountPoints::CreateRefCounted());
345 scoped_refptr
<FileSystemContext
> context(
346 CreateFileSystemContextForTest(external_mount_points
.get()));
348 // A request for a sandbox mount point should be served.
349 FileSystemURL cracked_url
=
350 context
->CrackURL(CreateRawFileSystemURL("persistent", "pers_mount"));
351 EXPECT_EQ(fileapi::kFileSystemTypePersistent
, cracked_url
.mount_type());
352 EXPECT_TRUE(context
->CanServeURLRequest(cracked_url
));
354 // A request for an isolated mount point should NOT be served.
355 std::string isolated_fs_name
= "root";
356 std::string isolated_fs_id
=
357 IsolatedContext::GetInstance()->RegisterFileSystemForPath(
358 fileapi::kFileSystemTypeNativeLocal
,
359 base::FilePath(DRIVE
FPL("/test/isolated/root")),
361 cracked_url
= context
->CrackURL(
362 CreateRawFileSystemURL("isolated", isolated_fs_id
));
363 EXPECT_EQ(fileapi::kFileSystemTypeIsolated
, cracked_url
.mount_type());
364 EXPECT_FALSE(context
->CanServeURLRequest(cracked_url
));
366 // A request for an external mount point should be served.
367 const std::string kExternalMountName
= "ext_mount";
368 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
369 kExternalMountName
, fileapi::kFileSystemTypeDrive
,
370 FileSystemMountOption(),
372 cracked_url
= context
->CrackURL(
373 CreateRawFileSystemURL("external", kExternalMountName
));
374 EXPECT_EQ(fileapi::kFileSystemTypeExternal
, cracked_url
.mount_type());
375 EXPECT_TRUE(context
->CanServeURLRequest(cracked_url
));
377 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
379 IsolatedContext::GetInstance()->RevokeFileSystem(isolated_fs_id
);
384 } // namespace content