1 // Copyright (c) 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/fileapi/file_system_context.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop.h"
9 #include "base/stringprintf.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "webkit/browser/fileapi/external_mount_points.h"
12 #include "webkit/browser/fileapi/file_system_mount_point_provider.h"
13 #include "webkit/browser/fileapi/file_system_task_runners.h"
14 #include "webkit/browser/fileapi/isolated_context.h"
15 #include "webkit/browser/fileapi/mock_file_system_options.h"
16 #include "webkit/quota/mock_quota_manager.h"
17 #include "webkit/quota/mock_special_storage_policy.h"
19 #define FPL(x) FILE_PATH_LITERAL(x)
21 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
22 #define DRIVE FPL("C:")
31 const char kTestOrigin
[] = "http://chromium.org/";
32 const base::FilePath::CharType kVirtualPathNoRoot
[] = FPL("root/file");
34 GURL
CreateRawFileSystemURL(const std::string
& type_str
,
35 const std::string
& fs_id
) {
36 std::string url_str
= base::StringPrintf(
37 "filesystem:http://chromium.org/%s/%s/root/file",
43 class FileSystemContextTest
: public testing::Test
{
45 FileSystemContextTest() {}
47 virtual void SetUp() {
48 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
50 storage_policy_
= new quota::MockSpecialStoragePolicy();
52 mock_quota_manager_
= new quota::MockQuotaManager(
53 false /* is_incognito */,
55 base::MessageLoopProxy::current(),
56 base::MessageLoopProxy::current(),
61 FileSystemContext
* CreateFileSystemContextForTest(
62 ExternalMountPoints
* external_mount_points
) {
63 return new FileSystemContext(
64 FileSystemTaskRunners::CreateMockTaskRunners(),
65 external_mount_points
,
67 mock_quota_manager_
->proxy(),
68 ScopedVector
<FileSystemMountPointProvider
>(),
70 CreateAllowFileAccessOptions());
73 // Verifies a *valid* filesystem url has expected values.
74 void ExpectFileSystemURLMatches(const FileSystemURL
& url
,
75 const GURL
& expect_origin
,
76 FileSystemType expect_mount_type
,
77 FileSystemType expect_type
,
78 const base::FilePath
& expect_path
,
79 const base::FilePath
& expect_virtual_path
,
80 const std::string
& expect_filesystem_id
) {
81 EXPECT_TRUE(url
.is_valid());
83 EXPECT_EQ(expect_origin
, url
.origin());
84 EXPECT_EQ(expect_mount_type
, url
.mount_type());
85 EXPECT_EQ(expect_type
, url
.type());
86 EXPECT_EQ(expect_path
, url
.path());
87 EXPECT_EQ(expect_virtual_path
, url
.virtual_path());
88 EXPECT_EQ(expect_filesystem_id
, url
.filesystem_id());
92 base::ScopedTempDir data_dir_
;
93 base::MessageLoop message_loop_
;
94 scoped_refptr
<quota::SpecialStoragePolicy
> storage_policy_
;
95 scoped_refptr
<quota::MockQuotaManager
> mock_quota_manager_
;
98 // It is not valid to pass NULL ExternalMountPoints to FileSystemContext on
100 #if !defined(OS_CHROMEOS)
101 TEST_F(FileSystemContextTest
, NullExternalMountPoints
) {
102 scoped_refptr
<FileSystemContext
> file_system_context(
103 CreateFileSystemContextForTest(NULL
));
105 // Cracking system external mount and isolated mount points should work.
106 std::string isolated_name
= "root";
107 std::string isolated_id
=
108 IsolatedContext::GetInstance()->RegisterFileSystemForPath(
109 kFileSystemTypeNativeLocal
,
110 base::FilePath(DRIVE
FPL("/test/isolated/root")),
112 // Register system external mount point.
113 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
115 kFileSystemTypeNativeLocal
,
116 base::FilePath(DRIVE
FPL("/test/sys/"))));
118 FileSystemURL cracked_isolated
= file_system_context
->CrackURL(
119 CreateRawFileSystemURL("isolated", isolated_id
));
121 ExpectFileSystemURLMatches(
124 kFileSystemTypeIsolated
,
125 kFileSystemTypeNativeLocal
,
126 base::FilePath(DRIVE
FPL("/test/isolated/root/file")).NormalizePathSeparators(),
127 base::FilePath::FromUTF8Unsafe(isolated_id
).Append(FPL("root/file")).
128 NormalizePathSeparators(),
131 FileSystemURL cracked_external
= file_system_context
->CrackURL(
132 CreateRawFileSystemURL("external", "system"));
134 ExpectFileSystemURLMatches(
137 kFileSystemTypeExternal
,
138 kFileSystemTypeNativeLocal
,
140 DRIVE
FPL("/test/sys/root/file")).NormalizePathSeparators(),
141 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(),
145 IsolatedContext::GetInstance()->RevokeFileSystem(isolated_id
);
146 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system");
148 #endif // !defiend(OS_CHROMEOS)
150 TEST_F(FileSystemContextTest
, FileSystemContextKeepsMountPointsAlive
) {
151 scoped_refptr
<ExternalMountPoints
> mount_points
=
152 ExternalMountPoints::CreateRefCounted();
154 // Register system external mount point.
155 ASSERT_TRUE(mount_points
->RegisterFileSystem(
157 kFileSystemTypeNativeLocal
,
158 base::FilePath(DRIVE
FPL("/test/sys/"))));
160 scoped_refptr
<FileSystemContext
> file_system_context(
161 CreateFileSystemContextForTest(mount_points
.get()));
163 // Release a MountPoints reference created in the test.
166 // FileSystemContext should keep a reference to the |mount_points|, so it
167 // should be able to resolve the URL.
168 FileSystemURL cracked_external
= file_system_context
->CrackURL(
169 CreateRawFileSystemURL("external", "system"));
171 ExpectFileSystemURLMatches(
174 kFileSystemTypeExternal
,
175 kFileSystemTypeNativeLocal
,
177 DRIVE
FPL("/test/sys/root/file")).NormalizePathSeparators(),
178 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(),
181 // No need to revoke the registered filesystem since |mount_points| lifetime
182 // is bound to this test.
185 TEST_F(FileSystemContextTest
, CrackFileSystemURL
) {
186 scoped_refptr
<ExternalMountPoints
> external_mount_points(
187 ExternalMountPoints::CreateRefCounted());
188 scoped_refptr
<FileSystemContext
> file_system_context(
189 CreateFileSystemContextForTest(external_mount_points
));
191 // Register an isolated mount point.
192 std::string isolated_file_system_name
= "root";
193 const std::string kIsolatedFileSystemID
=
194 IsolatedContext::GetInstance()->RegisterFileSystemForPath(
195 kFileSystemTypeNativeLocal
,
196 base::FilePath(DRIVE
FPL("/test/isolated/root")),
197 &isolated_file_system_name
);
198 // Register system external mount point.
199 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
201 kFileSystemTypeDrive
,
202 base::FilePath(DRIVE
FPL("/test/sys/"))));
203 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
205 kFileSystemTypeNativeLocal
,
206 base::FilePath(DRIVE
FPL("/test/ext"))));
207 // Register a system external mount point with the same name/id as the
208 // registered isolated mount point.
209 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
210 kIsolatedFileSystemID
,
211 kFileSystemTypeRestrictedNativeLocal
,
212 base::FilePath(DRIVE
FPL("/test/system/isolated"))));
213 // Add a mount points with the same name as a system mount point to
214 // FileSystemContext's external mount points.
215 ASSERT_TRUE(external_mount_points
->RegisterFileSystem(
217 kFileSystemTypeNativeLocal
,
218 base::FilePath(DRIVE
FPL("/test/local/ext/"))));
220 const GURL kTestOrigin
= GURL("http://chromium.org/");
221 const base::FilePath kVirtualPathNoRoot
= base::FilePath(FPL("root/file"));
226 std::string type_str
;
228 // Expected test results.
229 bool expect_is_valid
;
230 FileSystemType expect_mount_type
;
231 FileSystemType expect_type
;
232 const base::FilePath::CharType
* expect_path
;
233 std::string expect_filesystem_id
;
236 const TestCase kTestCases
[] = {
237 // Following should not be handled by the url crackers:
239 "pers_mount", "persistent", true /* is_valid */,
240 kFileSystemTypePersistent
, kFileSystemTypePersistent
,
241 FPL("pers_mount/root/file"),
242 std::string() /* filesystem id */
245 "temp_mount", "temporary", true /* is_valid */,
246 kFileSystemTypeTemporary
, kFileSystemTypeTemporary
,
247 FPL("temp_mount/root/file"),
248 std::string() /* filesystem id */
250 // Should be cracked by isolated mount points:
252 kIsolatedFileSystemID
, "isolated", true /* is_valid */,
253 kFileSystemTypeIsolated
, kFileSystemTypeNativeLocal
,
254 DRIVE
FPL("/test/isolated/root/file"),
255 kIsolatedFileSystemID
257 // Should be cracked by system mount points:
259 "system", "external", true /* is_valid */,
260 kFileSystemTypeExternal
, kFileSystemTypeDrive
,
261 DRIVE
FPL("/test/sys/root/file"),
265 kIsolatedFileSystemID
, "external", true /* is_valid */,
266 kFileSystemTypeExternal
, kFileSystemTypeRestrictedNativeLocal
,
267 DRIVE
FPL("/test/system/isolated/root/file"),
268 kIsolatedFileSystemID
270 // Should be cracked by FileSystemContext's ExternalMountPoints.
272 "ext", "external", true /* is_valid */,
273 kFileSystemTypeExternal
, kFileSystemTypeNativeLocal
,
274 DRIVE
FPL("/test/local/ext/root/file"),
277 // Test for invalid filesystem url (made invalid by adding invalid
280 "sytem", "external", false /* is_valid */,
281 // The rest of values will be ignored.
282 kFileSystemTypeUnknown
, kFileSystemTypeUnknown
, FPL(""),
285 // Test for URL with non-existing filesystem id.
287 "invalid", "external", false /* is_valid */,
288 // The rest of values will be ignored.
289 kFileSystemTypeUnknown
, kFileSystemTypeUnknown
, FPL(""),
294 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestCases
); ++i
) {
295 const base::FilePath virtual_path
=
296 base::FilePath::FromUTF8Unsafe(kTestCases
[i
].root
).Append(kVirtualPathNoRoot
);
299 CreateRawFileSystemURL(kTestCases
[i
].type_str
, kTestCases
[i
].root
);
300 FileSystemURL cracked_url
= file_system_context
->CrackURL(raw_url
);
302 SCOPED_TRACE(testing::Message() << "Test case " << i
<< ": "
303 << "Cracking URL: " << raw_url
);
305 EXPECT_EQ(kTestCases
[i
].expect_is_valid
, cracked_url
.is_valid());
306 if (!kTestCases
[i
].expect_is_valid
)
309 ExpectFileSystemURLMatches(
312 kTestCases
[i
].expect_mount_type
,
313 kTestCases
[i
].expect_type
,
314 base::FilePath(kTestCases
[i
].expect_path
).NormalizePathSeparators(),
315 virtual_path
.NormalizePathSeparators(),
316 kTestCases
[i
].expect_filesystem_id
);
319 IsolatedContext::GetInstance()->RevokeFileSystemByPath(
320 base::FilePath(DRIVE
FPL("/test/isolated/root")));
321 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system");
322 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("ext");
323 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
324 kIsolatedFileSystemID
);
329 } // namespace fileapi