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 "storage/browser/fileapi/file_system_dir_url_request_job.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/format_macros.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_piece.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "content/public/test/mock_special_storage_policy.h"
20 #include "content/public/test/test_file_system_backend.h"
21 #include "content/public/test/test_file_system_context.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h"
24 #include "net/base/request_priority.h"
25 #include "net/http/http_request_headers.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_context.h"
28 #include "net/url_request/url_request_test_util.h"
29 #include "storage/browser/fileapi/external_mount_points.h"
30 #include "storage/browser/fileapi/file_system_context.h"
31 #include "storage/browser/fileapi/file_system_file_util.h"
32 #include "storage/browser/fileapi/file_system_operation_context.h"
33 #include "storage/browser/fileapi/file_system_url.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "third_party/icu/source/i18n/unicode/regex.h"
37 using storage::FileSystemContext
;
38 using storage::FileSystemOperationContext
;
39 using storage::FileSystemURL
;
44 // We always use the TEMPORARY FileSystem in this test.
45 const char kFileSystemURLPrefix
[] = "filesystem:http://remote/temporary/";
47 const char kValidExternalMountPoint
[] = "mnt_name";
49 // An auto mounter that will try to mount anything for |storage_domain| =
50 // "automount", but will only succeed for the mount point "mnt_name".
51 bool TestAutoMountForURLRequest(
52 const net::URLRequest
* /*url_request*/,
53 const storage::FileSystemURL
& filesystem_url
,
54 const std::string
& storage_domain
,
55 const base::Callback
<void(base::File::Error result
)>& callback
) {
56 if (storage_domain
!= "automount")
59 std::vector
<base::FilePath::StringType
> components
;
60 filesystem_url
.path().GetComponents(&components
);
61 std::string mount_point
= base::FilePath(components
[0]).AsUTF8Unsafe();
63 if (mount_point
== kValidExternalMountPoint
) {
64 storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
65 kValidExternalMountPoint
,
66 storage::kFileSystemTypeTest
,
67 storage::FileSystemMountOption(),
69 callback
.Run(base::File::FILE_OK
);
71 callback
.Run(base::File::FILE_ERROR_NOT_FOUND
);
76 class FileSystemDirURLRequestJobFactory
: public net::URLRequestJobFactory
{
78 FileSystemDirURLRequestJobFactory(const std::string
& storage_domain
,
79 FileSystemContext
* context
)
80 : storage_domain_(storage_domain
), file_system_context_(context
) {
83 net::URLRequestJob
* MaybeCreateJobWithProtocolHandler(
84 const std::string
& scheme
,
85 net::URLRequest
* request
,
86 net::NetworkDelegate
* network_delegate
) const override
{
87 return new storage::FileSystemDirURLRequestJob(
88 request
, network_delegate
, storage_domain_
, file_system_context_
);
91 net::URLRequestJob
* MaybeInterceptRedirect(
92 net::URLRequest
* request
,
93 net::NetworkDelegate
* network_delegate
,
94 const GURL
& location
) const override
{
98 net::URLRequestJob
* MaybeInterceptResponse(
99 net::URLRequest
* request
,
100 net::NetworkDelegate
* network_delegate
) const override
{
104 bool IsHandledProtocol(const std::string
& scheme
) const override
{
108 bool IsHandledURL(const GURL
& url
) const override
{ return true; }
110 bool IsSafeRedirectTarget(const GURL
& location
) const override
{
115 std::string storage_domain_
;
116 FileSystemContext
* file_system_context_
;
122 class FileSystemDirURLRequestJobTest
: public testing::Test
{
124 FileSystemDirURLRequestJobTest()
125 : weak_factory_(this) {
128 void SetUp() override
{
129 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
131 special_storage_policy_
= new MockSpecialStoragePolicy
;
132 file_system_context_
= CreateFileSystemContextForTesting(
133 NULL
, temp_dir_
.path());
135 file_system_context_
->OpenFileSystem(
136 GURL("http://remote/"),
137 storage::kFileSystemTypeTemporary
,
138 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT
,
139 base::Bind(&FileSystemDirURLRequestJobTest::OnOpenFileSystem
,
140 weak_factory_
.GetWeakPtr()));
141 base::RunLoop().RunUntilIdle();
144 void TearDown() override
{
145 // NOTE: order matters, request must die before delegate
146 request_
.reset(NULL
);
147 delegate_
.reset(NULL
);
150 void SetUpAutoMountContext(base::FilePath
* mnt_point
) {
151 *mnt_point
= temp_dir_
.path().AppendASCII("auto_mount_dir");
152 ASSERT_TRUE(base::CreateDirectory(*mnt_point
));
154 ScopedVector
<storage::FileSystemBackend
> additional_providers
;
155 additional_providers
.push_back(new TestFileSystemBackend(
156 base::MessageLoopProxy::current().get(), *mnt_point
));
158 std::vector
<storage::URLRequestAutoMountHandler
> handlers
;
159 handlers
.push_back(base::Bind(&TestAutoMountForURLRequest
));
161 file_system_context_
= CreateFileSystemContextWithAutoMountersForTesting(
162 NULL
, additional_providers
.Pass(), handlers
, temp_dir_
.path());
165 void OnOpenFileSystem(const GURL
& root_url
,
166 const std::string
& name
,
167 base::File::Error result
) {
168 ASSERT_EQ(base::File::FILE_OK
, result
);
171 void TestRequestHelper(const GURL
& url
, bool run_to_completion
,
172 FileSystemContext
* file_system_context
) {
173 delegate_
.reset(new net::TestDelegate());
174 delegate_
->set_quit_on_redirect(true);
175 job_factory_
.reset(new FileSystemDirURLRequestJobFactory(
176 url
.GetOrigin().host(), file_system_context
));
177 empty_context_
.set_job_factory(job_factory_
.get());
179 request_
= empty_context_
.CreateRequest(
180 url
, net::DEFAULT_PRIORITY
, delegate_
.get(), NULL
);
182 ASSERT_TRUE(request_
->is_pending()); // verify that we're starting async
183 if (run_to_completion
)
184 base::MessageLoop::current()->Run();
187 void TestRequest(const GURL
& url
) {
188 TestRequestHelper(url
, true, file_system_context_
.get());
191 void TestRequestWithContext(const GURL
& url
,
192 FileSystemContext
* file_system_context
) {
193 TestRequestHelper(url
, true, file_system_context
);
196 void TestRequestNoRun(const GURL
& url
) {
197 TestRequestHelper(url
, false, file_system_context_
.get());
200 FileSystemURL
CreateURL(const base::FilePath
& file_path
) {
201 return file_system_context_
->CreateCrackedFileSystemURL(
202 GURL("http://remote"), storage::kFileSystemTypeTemporary
, file_path
);
205 FileSystemOperationContext
* NewOperationContext() {
206 FileSystemOperationContext
* context(
207 new FileSystemOperationContext(file_system_context_
.get()));
208 context
->set_allowed_bytes_growth(1024);
212 void CreateDirectory(const base::StringPiece
& dir_name
) {
213 base::FilePath path
= base::FilePath().AppendASCII(dir_name
);
214 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
215 ASSERT_EQ(base::File::FILE_OK
, file_util()->CreateDirectory(
218 false /* exclusive */,
219 false /* recursive */));
222 void EnsureFileExists(const base::StringPiece file_name
) {
223 base::FilePath path
= base::FilePath().AppendASCII(file_name
);
224 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
225 ASSERT_EQ(base::File::FILE_OK
, file_util()->EnsureFileExists(
226 context
.get(), CreateURL(path
), NULL
));
229 void TruncateFile(const base::StringPiece file_name
, int64 length
) {
230 base::FilePath path
= base::FilePath().AppendASCII(file_name
);
231 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
232 ASSERT_EQ(base::File::FILE_OK
, file_util()->Truncate(
233 context
.get(), CreateURL(path
), length
));
236 base::File::Error
GetFileInfo(const base::FilePath
& path
,
237 base::File::Info
* file_info
,
238 base::FilePath
* platform_file_path
) {
239 scoped_ptr
<FileSystemOperationContext
> context(NewOperationContext());
240 return file_util()->GetFileInfo(context
.get(),
242 file_info
, platform_file_path
);
245 // If |size| is negative, the reported size is ignored.
246 void VerifyListingEntry(const std::string
& entry_line
,
247 const std::string
& name
,
248 const std::string
& url
,
251 #define STR "([^\"]*)"
252 icu::UnicodeString
pattern("^<script>addRow\\(\"" STR
"\",\"" STR
253 "\",(0|1),\"" STR
"\",\"" STR
"\"\\);</script>");
255 icu::UnicodeString
input(entry_line
.c_str());
257 UErrorCode status
= U_ZERO_ERROR
;
258 icu::RegexMatcher
match(pattern
, input
, 0, status
);
260 EXPECT_TRUE(match
.find());
261 EXPECT_EQ(5, match
.groupCount());
262 EXPECT_EQ(icu::UnicodeString(name
.c_str()), match
.group(1, status
));
263 EXPECT_EQ(icu::UnicodeString(url
.c_str()), match
.group(2, status
));
264 EXPECT_EQ(icu::UnicodeString(is_directory
? "1" : "0"),
265 match
.group(3, status
));
267 icu::UnicodeString
size_string(FormatBytesUnlocalized(size
).c_str());
268 EXPECT_EQ(size_string
, match
.group(4, status
));
272 icu::UnicodeString
date_ustr(match
.group(5, status
));
273 std::string date_str
;
274 base::UTF16ToUTF8(date_ustr
.getBuffer(), date_ustr
.length(), &date_str
);
275 EXPECT_TRUE(base::Time::FromString(date_str
.c_str(), &date
));
276 EXPECT_FALSE(date
.is_null());
279 GURL
CreateFileSystemURL(const std::string path
) {
280 return GURL(kFileSystemURLPrefix
+ path
);
283 storage::FileSystemFileUtil
* file_util() {
284 return file_system_context_
->sandbox_delegate()->sync_file_util();
287 // Put the message loop at the top, so that it's the last thing deleted.
288 // Delete all MessageLoopProxy objects before the MessageLoop, to help prevent
289 // leaks caused by tasks posted during shutdown.
290 base::MessageLoopForIO message_loop_
;
292 base::ScopedTempDir temp_dir_
;
293 net::URLRequestContext empty_context_
;
294 scoped_ptr
<net::TestDelegate
> delegate_
;
295 scoped_ptr
<net::URLRequest
> request_
;
296 scoped_ptr
<FileSystemDirURLRequestJobFactory
> job_factory_
;
297 scoped_refptr
<MockSpecialStoragePolicy
> special_storage_policy_
;
298 scoped_refptr
<FileSystemContext
> file_system_context_
;
299 base::WeakPtrFactory
<FileSystemDirURLRequestJobTest
> weak_factory_
;
304 TEST_F(FileSystemDirURLRequestJobTest
, DirectoryListing
) {
305 CreateDirectory("foo");
306 CreateDirectory("foo/bar");
307 CreateDirectory("foo/bar/baz");
309 EnsureFileExists("foo/bar/hoge");
310 TruncateFile("foo/bar/hoge", 10);
312 TestRequest(CreateFileSystemURL("foo/bar/"));
314 ASSERT_FALSE(request_
->is_pending());
315 EXPECT_EQ(1, delegate_
->response_started_count());
316 EXPECT_FALSE(delegate_
->received_data_before_response());
317 EXPECT_GT(delegate_
->bytes_received(), 0);
319 std::istringstream
in(delegate_
->data_received());
321 EXPECT_TRUE(!!std::getline(in
, line
));
324 EXPECT_EQ("<script>start(\"foo\\\\bar\");</script>", line
);
325 #elif defined(OS_POSIX)
326 EXPECT_EQ("<script>start(\"/foo/bar\");</script>", line
);
329 EXPECT_TRUE(!!std::getline(in
, line
));
330 VerifyListingEntry(line
, "hoge", "hoge", false, 10);
332 EXPECT_TRUE(!!std::getline(in
, line
));
333 VerifyListingEntry(line
, "baz", "baz", true, 0);
334 EXPECT_FALSE(!!std::getline(in
, line
));
337 TEST_F(FileSystemDirURLRequestJobTest
, InvalidURL
) {
338 TestRequest(GURL("filesystem:/foo/bar/baz"));
339 ASSERT_FALSE(request_
->is_pending());
340 EXPECT_TRUE(delegate_
->request_failed());
341 ASSERT_FALSE(request_
->status().is_success());
342 EXPECT_EQ(net::ERR_INVALID_URL
, request_
->status().error());
345 TEST_F(FileSystemDirURLRequestJobTest
, NoSuchRoot
) {
346 TestRequest(GURL("filesystem:http://remote/persistent/somedir/"));
347 ASSERT_FALSE(request_
->is_pending());
348 ASSERT_FALSE(request_
->status().is_success());
349 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
352 TEST_F(FileSystemDirURLRequestJobTest
, NoSuchDirectory
) {
353 TestRequest(CreateFileSystemURL("somedir/"));
354 ASSERT_FALSE(request_
->is_pending());
355 ASSERT_FALSE(request_
->status().is_success());
356 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
359 TEST_F(FileSystemDirURLRequestJobTest
, Cancel
) {
360 CreateDirectory("foo");
361 TestRequestNoRun(CreateFileSystemURL("foo/"));
362 // Run StartAsync() and only StartAsync().
363 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, request_
.release());
364 base::RunLoop().RunUntilIdle();
365 // If we get here, success! we didn't crash!
368 TEST_F(FileSystemDirURLRequestJobTest
, Incognito
) {
369 CreateDirectory("foo");
371 scoped_refptr
<FileSystemContext
> file_system_context
=
372 CreateIncognitoFileSystemContextForTesting(NULL
, temp_dir_
.path());
374 TestRequestWithContext(CreateFileSystemURL("/"),
375 file_system_context
.get());
376 ASSERT_FALSE(request_
->is_pending());
377 ASSERT_TRUE(request_
->status().is_success());
379 std::istringstream
in(delegate_
->data_received());
381 EXPECT_TRUE(!!std::getline(in
, line
));
382 EXPECT_FALSE(!!std::getline(in
, line
));
384 TestRequestWithContext(CreateFileSystemURL("foo"),
385 file_system_context
.get());
386 ASSERT_FALSE(request_
->is_pending());
387 ASSERT_FALSE(request_
->status().is_success());
388 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
391 TEST_F(FileSystemDirURLRequestJobTest
, AutoMountDirectoryListing
) {
392 base::FilePath mnt_point
;
393 SetUpAutoMountContext(&mnt_point
);
394 ASSERT_TRUE(base::CreateDirectory(mnt_point
));
395 ASSERT_TRUE(base::CreateDirectory(mnt_point
.AppendASCII("foo")));
397 base::WriteFile(mnt_point
.AppendASCII("bar"), "1234567890", 10));
399 TestRequest(GURL("filesystem:http://automount/external/mnt_name"));
401 ASSERT_FALSE(request_
->is_pending());
402 EXPECT_EQ(1, delegate_
->response_started_count());
403 EXPECT_FALSE(delegate_
->received_data_before_response());
404 EXPECT_GT(delegate_
->bytes_received(), 0);
406 std::istringstream
in(delegate_
->data_received());
408 EXPECT_TRUE(!!std::getline(in
, line
)); // |line| contains the temp dir path.
410 // Result order is not guaranteed, so sort the results.
411 std::vector
<std::string
> listing_entries
;
412 while (!!std::getline(in
, line
))
413 listing_entries
.push_back(line
);
415 ASSERT_EQ(2U, listing_entries
.size());
416 std::sort(listing_entries
.begin(), listing_entries
.end());
417 VerifyListingEntry(listing_entries
[0], "bar", "bar", false, 10);
418 VerifyListingEntry(listing_entries
[1], "foo", "foo", true, -1);
421 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
422 kValidExternalMountPoint
));
425 TEST_F(FileSystemDirURLRequestJobTest
, AutoMountInvalidRoot
) {
426 base::FilePath mnt_point
;
427 SetUpAutoMountContext(&mnt_point
);
428 TestRequest(GURL("filesystem:http://automount/external/invalid"));
430 ASSERT_FALSE(request_
->is_pending());
431 ASSERT_FALSE(request_
->status().is_success());
432 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
435 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
439 TEST_F(FileSystemDirURLRequestJobTest
, AutoMountNoHandler
) {
440 base::FilePath mnt_point
;
441 SetUpAutoMountContext(&mnt_point
);
442 TestRequest(GURL("filesystem:http://noauto/external/mnt_name"));
444 ASSERT_FALSE(request_
->is_pending());
445 ASSERT_FALSE(request_
->status().is_success());
446 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request_
->status().error());
449 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
450 kValidExternalMountPoint
));
453 } // namespace (anonymous)
454 } // namespace content